From 3ec66893e377b088c1284d2d23adb2aeea6d7965 Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Sat, 27 Feb 2021 22:08:58 +0000 Subject: New port: FiiO M3K on bare metal Change-Id: I7517e7d5459e129dcfc9465c6fbd708619888fbe --- utils/fiio_m3k_tools/nand_patcher.py | 69 ++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100755 utils/fiio_m3k_tools/nand_patcher.py (limited to 'utils/fiio_m3k_tools') diff --git a/utils/fiio_m3k_tools/nand_patcher.py b/utils/fiio_m3k_tools/nand_patcher.py new file mode 100755 index 0000000000..261a4de678 --- /dev/null +++ b/utils/fiio_m3k_tools/nand_patcher.py @@ -0,0 +1,69 @@ +#!/usr/bin/python3 + +import sys + +IMAGE_SIZE = 128 * 1024 # image is an 128 KiB erase block +SPL_SIZE = 12 * 1024 # SPL is at most 12 KiB +BOOT_SIZE = 102 * 1024 # bootloader at most 102 KiB +BOOT_OFF = 26 * 1024 # offset of bootloader in image +BOOT_END = BOOT_OFF+BOOT_SIZE + +def patch(in_path, boot_path, spl_path, out_path): + # Open the input files + in_file = open(in_path, 'rb') + boot_file = open(boot_path, 'rb') + spl_file = open(spl_path, 'rb') + + # Read the data + in_data = in_file.read() + boot_data = boot_file.read() + spl_data = spl_file.read() + + # Close input files + in_file.close() + boot_file.close() + spl_file.close() + + if len(in_data) != IMAGE_SIZE: + print("error: input image is %d bytes, expected %d" % (len(in_data), IMAGE_SIZE)) + sys.exit(1) + + if len(spl_data) > SPL_SIZE: + print("error: SPL is %d bytes, maximum is %d" % (len(spl_data), SPL_SIZE)) + sys.exit(1) + + if len(boot_data) > BOOT_SIZE: + print("error: bootloader is %d bytes, maximum is %d" % (len(boot_data), SPL_SIZE)) + sys.exit(1) + + print('Patching input image %s' % in_path) + print('- SPL size %d' % len(spl_data)) + print('- Boot size %d' % len(boot_data)) + + # Construct output image + out_data = b'' + out_data += spl_data + out_data += b'\xff' * (SPL_SIZE - len(spl_data)) + out_data += in_data[SPL_SIZE:BOOT_OFF] + out_data += boot_data + out_data += b'\xff' * (BOOT_SIZE - len(boot_data)) + + # Sanity check + assert( len(out_data) == IMAGE_SIZE ) + + # Write output + print('Writing output image %s' % out_path) + out_file = open(out_path, 'wb') + out_file.write(out_data) + out_file.close() + + +def main(): + if len(sys.argv) != 5: + print("usage: nand_patcher.py IN_FILE BOOT_FILE SPL_FILE OUT_FILE") + sys.exit(1) + + patch(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]) + +if __name__ == '__main__': + main() -- cgit v1.2.3