offset = int(offestStr, 16) size = int(sizeStr, 16) # Create out directory print("[i] Create output directory") utils.createDirectory(outFolder) # Get the key bank section and store it outEncKeyBankFile = os.path.join(outFolder, 'key_bank.bin') print("[i] Save mstar key bank to {}".format(outEncKeyBankFile)) utils.copyPart(mboot, outEncKeyBankFile, offset, size) # Unpack the key bank to key bank structure print("[i] Unpack key bank structure") keyBankBytes = utils.loadPart(outEncKeyBankFile, 0, size) keyBank = utils.unpackStructure(CUSTOMER_KEY_BANK, keyBankBytes) if (DEBUG): # Print all print("[i] u8SecIdentify:\n{}".format( utils.hexString(keyBank.customer.u8SecIdentify))) print("[i] u32Num: 0x{:08x}".format(keyBank.customer.info.u32Num)) print("[i] u32Size: 0x{:08x}".format(keyBank.customer.info.u32Size)) print("[i] u8Signature:\n{}".format( utils.hexString(keyBank.customer.u8Signature))) print("[i] u8RSABootPublicKey N:\n{}".format( utils.hexString(keyBank.u8RSABootPublicKey.N))) print("[i] u8RSABootPublicKey E:\n{}".format( utils.hexString(keyBank.u8RSABootPublicKey.E)))
header.write('\n'.encode()) header.write('# Header suffix'.encode()) header.write(headerScriptSuffix.encode()) header.write('\n'.encode()) header.write('% <- this is end of file symbol\n'.encode()) header.flush() print('[i] Fill header script to 16KB') header.write(('\xff' * (HEADER_SIZE - os.path.getsize(headerPart))).encode( encoding='iso-8859-1')) print('[i] Generating footer ...') headerCRC = utils.crc32(headerPart) binCRC = utils.crc32(binPart) header16bytes = utils.loadPart(headerPart, 0, 16) with open(footerPart, 'wb') as footer: print('[i] Magic: {}'.format(MAGIC_FOOTER)) footer.write(MAGIC_FOOTER.encode()) print('[i] Header CRC: 0x{:02X}'.format(headerCRC)) footer.write(struct.pack( 'L', headerCRC)) # struct.pack('L', data) <- returns byte swapped data print('[i] Bin CRC: 0x{:02X}'.format(binCRC)) footer.write(struct.pack('L', binCRC)) print('[i] First 16 bytes of header: {}'.format(header16bytes)) footer.write(header16bytes) print('[i] Merging header, bin, footer ...') open(firmwareFileName, 'w').close() utils.appendFile(headerPart, firmwareFileName) utils.appendFile(binPart, firmwareFileName)
print("No such file: {}".format(inputFile)) quit() if len(sys.argv) == 3: outputDirectory = sys.argv[2] else: outputDirectory = 'unpacked' # Create output directory utils.createDirectory(outputDirectory) # Find header script # Header size is 16KB # Non used part is filled by 0xFF print("[i] Analizing header ...") header = utils.loadPart(inputFile, 0, HEADER_SIZE) utils.copyPart(inputFile, os.path.join(outputDirectory, "~header"), 0, HEADER_SIZE) offset = header.find('\xff'.encode(encoding='iso-8859-1')) if offset != -1: headerScript = header[:offset].decode() headerScriptFound = True if not headerScriptFound: print("[!] Could not find header script!") quit() if DEBUG: print(headerScript)