summaryrefslogtreecommitdiff
path: root/utils/fiio_m3k_tools
diff options
context:
space:
mode:
Diffstat (limited to 'utils/fiio_m3k_tools')
-rwxr-xr-xutils/fiio_m3k_tools/nand_patcher.py69
1 files changed, 69 insertions, 0 deletions
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 @@
1#!/usr/bin/python3
2
3import sys
4
5IMAGE_SIZE = 128 * 1024 # image is an 128 KiB erase block
6SPL_SIZE = 12 * 1024 # SPL is at most 12 KiB
7BOOT_SIZE = 102 * 1024 # bootloader at most 102 KiB
8BOOT_OFF = 26 * 1024 # offset of bootloader in image
9BOOT_END = BOOT_OFF+BOOT_SIZE
10
11def patch(in_path, boot_path, spl_path, out_path):
12 # Open the input files
13 in_file = open(in_path, 'rb')
14 boot_file = open(boot_path, 'rb')
15 spl_file = open(spl_path, 'rb')
16
17 # Read the data
18 in_data = in_file.read()
19 boot_data = boot_file.read()
20 spl_data = spl_file.read()
21
22 # Close input files
23 in_file.close()
24 boot_file.close()
25 spl_file.close()
26
27 if len(in_data) != IMAGE_SIZE:
28 print("error: input image is %d bytes, expected %d" % (len(in_data), IMAGE_SIZE))
29 sys.exit(1)
30
31 if len(spl_data) > SPL_SIZE:
32 print("error: SPL is %d bytes, maximum is %d" % (len(spl_data), SPL_SIZE))
33 sys.exit(1)
34
35 if len(boot_data) > BOOT_SIZE:
36 print("error: bootloader is %d bytes, maximum is %d" % (len(boot_data), SPL_SIZE))
37 sys.exit(1)
38
39 print('Patching input image %s' % in_path)
40 print('- SPL size %d' % len(spl_data))
41 print('- Boot size %d' % len(boot_data))
42
43 # Construct output image
44 out_data = b''
45 out_data += spl_data
46 out_data += b'\xff' * (SPL_SIZE - len(spl_data))
47 out_data += in_data[SPL_SIZE:BOOT_OFF]
48 out_data += boot_data
49 out_data += b'\xff' * (BOOT_SIZE - len(boot_data))
50
51 # Sanity check
52 assert( len(out_data) == IMAGE_SIZE )
53
54 # Write output
55 print('Writing output image %s' % out_path)
56 out_file = open(out_path, 'wb')
57 out_file.write(out_data)
58 out_file.close()
59
60
61def main():
62 if len(sys.argv) != 5:
63 print("usage: nand_patcher.py IN_FILE BOOT_FILE SPL_FILE OUT_FILE")
64 sys.exit(1)
65
66 patch(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
67
68if __name__ == '__main__':
69 main()