diff options
Diffstat (limited to 'scripts/update_chitu.py')
-rwxr-xr-x | scripts/update_chitu.py | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/scripts/update_chitu.py b/scripts/update_chitu.py index cf7fcfe9..62145371 100755 --- a/scripts/update_chitu.py +++ b/scripts/update_chitu.py @@ -11,14 +11,16 @@ import uuid import sys import hashlib + def calculate_crc(contents, seed): - accumulating_xor_value = seed; + accumulating_xor_value = seed for i in range(0, len(contents), 4): - value = struct.unpack('<I', contents[ i : i + 4])[0] + value = struct.unpack("<I", contents[i : i + 4])[0] accumulating_xor_value = accumulating_xor_value ^ value return accumulating_xor_value + def xor_block(r0, r1, block_number, block_size, file_key): # This is the loop counter loop_counter = 0x0 @@ -27,17 +29,17 @@ def xor_block(r0, r1, block_number, block_size, file_key): key_length = 0x18 # This is an initial seed - xor_seed = 0x4bad + xor_seed = 0x4BAD # This is the block counter block_number = xor_seed * block_number - #load the xor key from the file - r7 = file_key + # load the xor key from the file + r7 = file_key for loop_counter in range(0, block_size): # meant to make sure different bits of the key are used. - xor_seed = int(loop_counter/key_length) + xor_seed = int(loop_counter / key_length) # IP is a scratch register / R12 ip = loop_counter - (key_length * xor_seed) @@ -57,10 +59,10 @@ def xor_block(r0, r1, block_number, block_size, file_key): # and then with IP xor_seed = xor_seed ^ ip - #Now store the byte back + # Now store the byte back r1[loop_counter] = xor_seed & 0xFF - #increment the loop_counter + # increment the loop_counter loop_counter = loop_counter + 1 @@ -74,12 +76,12 @@ def encode_file(input, output_file, file_length): print("Update UUID ", uid_value) file_key = int(uid_value.hex[0:8], 16) - xor_crc = 0xef3d4323; + xor_crc = 0xEF3D4323 # the input file is expected to be in chunks of 0x800 # so round the size while len(input_file) % block_size != 0: - input_file.extend(b'0x0') + input_file.extend(b"0x0") # write the file header output_file.write(struct.pack(">I", 0x443D2D3F)) @@ -88,15 +90,15 @@ def encode_file(input, output_file, file_length): # write the file_key output_file.write(struct.pack("<I", file_key)) - #TODO - how to enforce that the firmware aligns to block boundaries? + # TODO - how to enforce that the firmware aligns to block boundaries? block_count = int(len(input_file) / block_size) print("Block Count is ", block_count) for block_number in range(0, block_count): - block_offset = (block_number * block_size) + block_offset = block_number * block_size block_end = block_offset + block_size - block_array = bytearray(input_file[block_offset: block_end]) + block_array = bytearray(input_file[block_offset:block_end]) xor_block(block_array, block_array, block_number, block_size, file_key) - for n in range (0, block_size): + for n in range(0, block_size): input_file[block_offset + n] = block_array[n] # update the expected CRC value. @@ -109,6 +111,7 @@ def encode_file(input, output_file, file_length): output_file.write(input_file) return + def main(): if len(sys.argv) != 3: print("Usage: update_chitu <input_file> <output_file>") @@ -132,5 +135,6 @@ def main(): print("Encoding complete.") -if __name__ == '__main__': + +if __name__ == "__main__": main() |