|
@@ -0,0 +1,119 @@
|
|
1
|
+Import("env")
|
|
2
|
+import struct
|
|
3
|
+
|
|
4
|
+# Relocate firmware from 0x08000000 to 0x08008800
|
|
5
|
+for define in env['CPPDEFINES']:
|
|
6
|
+ if define[0] == "VECT_TAB_ADDR":
|
|
7
|
+ env['CPPDEFINES'].remove(define)
|
|
8
|
+env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x8008800"))
|
|
9
|
+env.Replace(LDSCRIPT_PATH="buildroot/share/PlatformIO/ldscripts/chitu_f103.ld")
|
|
10
|
+
|
|
11
|
+def calculate_crc(contents, seed):
|
|
12
|
+ accumulating_xor_value = seed;
|
|
13
|
+
|
|
14
|
+ for i in range(0, len(contents), 4):
|
|
15
|
+ value = struct.unpack('<I', contents[ i : i + 4])[0]
|
|
16
|
+ accumulating_xor_value = accumulating_xor_value ^ value
|
|
17
|
+ return accumulating_xor_value
|
|
18
|
+
|
|
19
|
+def xor_block(r0, r1, block_number, block_size, file_key):
|
|
20
|
+ # This is the loop counter
|
|
21
|
+ loop_counter = 0x0
|
|
22
|
+
|
|
23
|
+ # This is the key length
|
|
24
|
+ key_length = 0x18
|
|
25
|
+
|
|
26
|
+ # This is an initial seed
|
|
27
|
+ xor_seed = 0x4bad
|
|
28
|
+
|
|
29
|
+ # This is the block counter
|
|
30
|
+ block_number = xor_seed * block_number
|
|
31
|
+
|
|
32
|
+ #load the xor key from the file
|
|
33
|
+ r7 = file_key
|
|
34
|
+
|
|
35
|
+ for loop_counter in range(0, block_size):
|
|
36
|
+ # meant to make sure different bits of the key are used.
|
|
37
|
+ xor_seed = int(loop_counter/key_length)
|
|
38
|
+
|
|
39
|
+ # IP is a scratch register / R12
|
|
40
|
+ ip = loop_counter - (key_length * xor_seed)
|
|
41
|
+
|
|
42
|
+ # xor_seed = (loop_counter * loop_counter) + block_number
|
|
43
|
+ xor_seed = (loop_counter * loop_counter) + block_number
|
|
44
|
+
|
|
45
|
+ # shift the xor_seed left by the bits in IP.
|
|
46
|
+ xor_seed = xor_seed >> ip
|
|
47
|
+
|
|
48
|
+ # load a byte into IP
|
|
49
|
+ ip = r0[loop_counter]
|
|
50
|
+
|
|
51
|
+ # XOR the seed with r7
|
|
52
|
+ xor_seed = xor_seed ^ r7
|
|
53
|
+
|
|
54
|
+ # and then with IP
|
|
55
|
+ xor_seed = xor_seed ^ ip
|
|
56
|
+
|
|
57
|
+ #Now store the byte back
|
|
58
|
+ r1[loop_counter] = xor_seed & 0xFF
|
|
59
|
+
|
|
60
|
+ #increment the loop_counter
|
|
61
|
+ loop_counter = loop_counter + 1
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+def encrypt_file(input, output_file, file_length):
|
|
65
|
+ input_file = bytearray(input.read())
|
|
66
|
+ block_size = 0x800
|
|
67
|
+ key_length = 0x18
|
|
68
|
+ file_key = 0xDAB27F94
|
|
69
|
+
|
|
70
|
+ xor_crc = 0xef3d4323;
|
|
71
|
+
|
|
72
|
+ # the input file is exepcted to be in chunks of 0x800
|
|
73
|
+ # so round the size
|
|
74
|
+ while len(input_file) % block_size != 0:
|
|
75
|
+ input_file.extend(b'0x0')
|
|
76
|
+
|
|
77
|
+ # write the file header
|
|
78
|
+ output_file.write(struct.pack(">I", 0x443D2D3F))
|
|
79
|
+ # encrypt the contents using a known file header key
|
|
80
|
+
|
|
81
|
+ # write the file_key
|
|
82
|
+ output_file.write(struct.pack(">I", 0x947FB2DA))
|
|
83
|
+
|
|
84
|
+ #TODO - how to enforce that the firmware aligns to block boundaries?
|
|
85
|
+ block_count = int(len(input_file) / block_size)
|
|
86
|
+ print "Block Count is ", block_count
|
|
87
|
+ for block_number in range(0, block_count):
|
|
88
|
+ block_offset = (block_number * block_size)
|
|
89
|
+ block_end = block_offset + block_size
|
|
90
|
+ block_array = bytearray(input_file[block_offset: block_end])
|
|
91
|
+ xor_block(block_array, block_array, block_number, block_size, file_key)
|
|
92
|
+ for n in range (0, block_size):
|
|
93
|
+ input_file[block_offset + n] = block_array[n]
|
|
94
|
+
|
|
95
|
+ # update the expected CRC value.
|
|
96
|
+ xor_crc = calculate_crc(block_array, xor_crc)
|
|
97
|
+
|
|
98
|
+ # write CRC
|
|
99
|
+ output_file.write(struct.pack("<I", xor_crc))
|
|
100
|
+
|
|
101
|
+ # finally, append the encrypted results.
|
|
102
|
+ output_file.write(input_file)
|
|
103
|
+ return
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+# Encrypt ${PROGNAME}.bin and save it as 'update.cbd'
|
|
107
|
+def encrypt(source, target, env):
|
|
108
|
+ import os
|
|
109
|
+
|
|
110
|
+ firmware = open(target[0].path, "rb")
|
|
111
|
+ update = open(target[0].dir.path +'/update.cbd', "wb")
|
|
112
|
+ length = os.path.getsize(target[0].path)
|
|
113
|
+
|
|
114
|
+ encrypt_file(firmware, update, length)
|
|
115
|
+
|
|
116
|
+ firmware.close()
|
|
117
|
+ update.close()
|
|
118
|
+
|
|
119
|
+env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt);
|