Nico Huber | ca41a6a | 2016-01-23 21:29:47 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Patrick Georgi | 1afe286 | 2020-05-10 17:34:15 +0200 | [diff] [blame] | 2 | # SPDX-License-Identifier: BSD-2-Clause |
huang lin | 817e455 | 2014-08-26 17:31:28 +0800 | [diff] [blame] | 3 | |
| 4 | import struct |
| 5 | import sys |
| 6 | from io import SEEK_SET, SEEK_END |
| 7 | |
| 8 | class IDBTool: |
| 9 | def __init__(self): |
| 10 | print "Initialize IDBTool" |
| 11 | |
| 12 | def p_rc4(self, buf, length): |
| 13 | key = (124,78,3,4,85,5,9,7,45,44,123,56,23,13,23,17) |
| 14 | K = key * 16 |
| 15 | S = [i for i in range(256)] |
| 16 | |
| 17 | j = 0 |
| 18 | for i in range(256): |
| 19 | j = (j + S[i] + K[i]) % 256 |
| 20 | temp = S[i]; S[i] = S[j]; S[j] = temp; |
| 21 | |
| 22 | i = j = k = 0 |
| 23 | for x in range(length): |
| 24 | i = (i+1) % 256 |
| 25 | j = (j + S[i]) % 256 |
| 26 | temp = S[i]; S[i] = S[j]; S[j] = temp |
| 27 | k = (S[i] + S[j]) % 256 |
| 28 | buf[x] = struct.pack('B', ord(buf[x]) ^ S[k]) |
| 29 | |
huang lin | 1129f7f | 2016-03-02 16:02:45 +0800 | [diff] [blame] | 30 | def makeIDB(self, chip, from_file, to_file, rc4_flag = False, align_flag = False): |
huang lin | 817e455 | 2014-08-26 17:31:28 +0800 | [diff] [blame] | 31 | try: |
| 32 | fin = open(from_file, 'rb') |
| 33 | except: |
| 34 | sys.exit("Failed to open file : " + from_file) |
| 35 | |
| 36 | try: |
| 37 | fin.seek(0, SEEK_END) |
| 38 | if (fin.tell() > 4 * 1024 * 1024): |
| 39 | sys.exit("Input file is more than 4MB") |
| 40 | fin.seek(0) |
| 41 | data = fin.read() |
| 42 | finally: |
| 43 | fin.close() |
| 44 | |
| 45 | data_len = len(data) |
| 46 | SECTOR_SIZE = 512 |
| 47 | PAGE_ALIGN = 4 |
| 48 | sectors = (data_len + 4 - 1) / SECTOR_SIZE + 1 |
| 49 | pages = (sectors - 1) / PAGE_ALIGN + 1 |
| 50 | sectors = pages * PAGE_ALIGN; |
| 51 | |
| 52 | buf = [B'\0'] * sectors * SECTOR_SIZE |
huang lin | 1129f7f | 2016-03-02 16:02:45 +0800 | [diff] [blame] | 53 | buf[:4] = chip |
huang lin | 817e455 | 2014-08-26 17:31:28 +0800 | [diff] [blame] | 54 | buf[4 : 4+data_len] = data |
| 55 | |
| 56 | idblock = [B'\0'] * 4 * SECTOR_SIZE |
| 57 | blank = [B'\0'] * 4 * SECTOR_SIZE |
| 58 | idblock[:4] = ['\x55', '\xAA', '\xF0', '\x0F'] |
| 59 | |
| 60 | if (not rc4_flag): |
| 61 | idblock[8:12] = struct.pack("<I", 1) |
| 62 | else: |
| 63 | for i in range(sectors): |
| 64 | list_tmp = buf[SECTOR_SIZE*i : SECTOR_SIZE*(i+1)] |
| 65 | self.p_rc4(list_tmp, SECTOR_SIZE) |
| 66 | buf[SECTOR_SIZE*i : SECTOR_SIZE*(i+1)] = list_tmp |
| 67 | |
| 68 | idblock[12:16] = struct.pack("<HH", 4, 4); |
| 69 | idblock[506:510] = struct.pack("<HH", sectors, sectors); |
| 70 | self.p_rc4(idblock, SECTOR_SIZE) |
| 71 | |
| 72 | try: |
| 73 | fout = open(to_file, "wb+") |
| 74 | except: |
| 75 | sys.exit("Failed to open output file : " + to_file) |
| 76 | |
| 77 | try: |
| 78 | if (align_flag): |
| 79 | fout.write(''.join(idblock)) |
| 80 | fout.write(''.join(blank)) |
| 81 | |
| 82 | for s in xrange(0, sectors * SECTOR_SIZE, PAGE_ALIGN * SECTOR_SIZE): |
| 83 | fout.write(''.join(buf[s : s + PAGE_ALIGN * SECTOR_SIZE])) |
| 84 | fout.write(''.join(blank)) |
| 85 | else: |
| 86 | fout.write(''.join(idblock)) |
| 87 | fout.write(''.join(buf)) |
| 88 | fout.flush() |
| 89 | except: |
| 90 | sys.exit("Failed to write data to : " + to_file) |
| 91 | finally: |
| 92 | fout.close() |
| 93 | print "DONE" |
| 94 | |
| 95 | def usage(): |
huang lin | 1129f7f | 2016-03-02 16:02:45 +0800 | [diff] [blame] | 96 | print "Usage: make_idb.py [--chip=RKXX] [--enable-rc4] [--enable-align] [--to=out] --from=in" |
| 97 | print " --chip: default is RK32" |
huang lin | 817e455 | 2014-08-26 17:31:28 +0800 | [diff] [blame] | 98 | |
| 99 | if __name__ == '__main__': |
| 100 | rc4_flag = align_flag = False |
| 101 | to_file = "IDBlock.bin" |
huang lin | 1129f7f | 2016-03-02 16:02:45 +0800 | [diff] [blame] | 102 | chip = "RK32" |
huang lin | 817e455 | 2014-08-26 17:31:28 +0800 | [diff] [blame] | 103 | |
| 104 | for para in sys.argv[1:]: |
| 105 | if (para == "--enable-rc4"): |
| 106 | rc4_flag = True |
| 107 | elif (para == "--enable-align"): |
| 108 | align_flag = True |
| 109 | elif (para.startswith("--to=")): |
| 110 | to_file = para.split('=')[1] |
| 111 | elif (para.startswith("--from=")): |
| 112 | from_file = para.split('=')[1] |
huang lin | 1129f7f | 2016-03-02 16:02:45 +0800 | [diff] [blame] | 113 | elif (para.startswith("--chip=")): |
| 114 | chip = para.split('=')[1] |
huang lin | 817e455 | 2014-08-26 17:31:28 +0800 | [diff] [blame] | 115 | elif (para == "--help" or para == "-h"): |
| 116 | usage() |
| 117 | sys.exit() |
| 118 | else: |
| 119 | usage() |
| 120 | sys.exit() |
| 121 | if ('from_file' not in vars() or to_file == ''): |
| 122 | usage() |
| 123 | sys.exit() |
| 124 | |
| 125 | idbtool = IDBTool() |
huang lin | 1129f7f | 2016-03-02 16:02:45 +0800 | [diff] [blame] | 126 | idbtool.makeIDB(chip, from_file, to_file, rc4_flag, align_flag) |