123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
-
-
-
-
- import pioutil
- if pioutil.is_pio_build():
- import os,random,struct,uuid,marlin
-
- marlin.relocate_firmware("0x08008800")
-
- def calculate_crc(contents, seed):
- accumulating_xor_value = seed;
-
- for i in range(0, len(contents), 4):
- 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):
-
- loop_counter = 0x0
-
-
- key_length = 0x18
-
-
- xor_seed = 0x4BAD
-
-
- block_number = xor_seed * block_number
-
-
- r7 = file_key
-
- for loop_counter in range(0, block_size):
-
- xor_seed = int(loop_counter / key_length)
-
-
- ip = loop_counter - (key_length * xor_seed)
-
-
- xor_seed = (loop_counter * loop_counter) + block_number
-
-
- xor_seed = xor_seed >> ip
-
-
- ip = r0[loop_counter]
-
-
- xor_seed = xor_seed ^ r7
-
-
- xor_seed = xor_seed ^ ip
-
-
- r1[loop_counter] = xor_seed & 0xFF
-
-
- loop_counter = loop_counter + 1
-
- def encrypt_file(input, output_file, file_length):
- input_file = bytearray(input.read())
- block_size = 0x800
- key_length = 0x18
-
- uid_value = uuid.uuid4()
- file_key = int(uid_value.hex[0:8], 16)
-
- xor_crc = 0xEF3D4323;
-
-
-
- while len(input_file) % block_size != 0:
- input_file.extend(b'0x0')
-
-
- output_file.write(struct.pack(">I", 0x443D2D3F))
-
-
-
- output_file.write(struct.pack("<I", file_key))
-
-
- 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_end = block_offset + block_size
- 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):
- input_file[block_offset + n] = block_array[n]
-
-
- xor_crc = calculate_crc(block_array, xor_crc)
-
-
- output_file.write(struct.pack("<I", xor_crc))
-
-
- output_file.write(input_file)
- return
-
-
- def encrypt(source, target, env):
- firmware = open(target[0].path, "rb")
- update = open(target[0].dir.path + '/update.cbd', "wb")
- length = os.path.getsize(target[0].path)
-
- encrypt_file(firmware, update, length)
-
- firmware.close()
- update.close()
-
- marlin.add_post_action(encrypt);
|