My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

chitu_crypt.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #
  2. # buildroot/share/PlatformIO/scripts/chitu_crypt.py
  3. # Customizations for Chitu boards
  4. #
  5. import os,random,struct,uuid,marlin
  6. # Relocate firmware from 0x08000000 to 0x08008800
  7. marlin.relocate_firmware("0x08008800")
  8. def calculate_crc(contents, seed):
  9. accumulating_xor_value = seed;
  10. for i in range(0, len(contents), 4):
  11. value = struct.unpack('<I', contents[ i : i + 4])[0]
  12. accumulating_xor_value = accumulating_xor_value ^ value
  13. return accumulating_xor_value
  14. def xor_block(r0, r1, block_number, block_size, file_key):
  15. # This is the loop counter
  16. loop_counter = 0x0
  17. # This is the key length
  18. key_length = 0x18
  19. # This is an initial seed
  20. xor_seed = 0x4BAD
  21. # This is the block counter
  22. block_number = xor_seed * block_number
  23. #load the xor key from the file
  24. r7 = file_key
  25. for loop_counter in range(0, block_size):
  26. # meant to make sure different bits of the key are used.
  27. xor_seed = int(loop_counter / key_length)
  28. # IP is a scratch register / R12
  29. ip = loop_counter - (key_length * xor_seed)
  30. # xor_seed = (loop_counter * loop_counter) + block_number
  31. xor_seed = (loop_counter * loop_counter) + block_number
  32. # shift the xor_seed left by the bits in IP.
  33. xor_seed = xor_seed >> ip
  34. # load a byte into IP
  35. ip = r0[loop_counter]
  36. # XOR the seed with r7
  37. xor_seed = xor_seed ^ r7
  38. # and then with IP
  39. xor_seed = xor_seed ^ ip
  40. #Now store the byte back
  41. r1[loop_counter] = xor_seed & 0xFF
  42. #increment the loop_counter
  43. loop_counter = loop_counter + 1
  44. def encrypt_file(input, output_file, file_length):
  45. input_file = bytearray(input.read())
  46. block_size = 0x800
  47. key_length = 0x18
  48. uid_value = uuid.uuid4()
  49. file_key = int(uid_value.hex[0:8], 16)
  50. xor_crc = 0xEF3D4323;
  51. # the input file is exepcted to be in chunks of 0x800
  52. # so round the size
  53. while len(input_file) % block_size != 0:
  54. input_file.extend(b'0x0')
  55. # write the file header
  56. output_file.write(struct.pack(">I", 0x443D2D3F))
  57. # encrypt the contents using a known file header key
  58. # write the file_key
  59. output_file.write(struct.pack("<I", file_key))
  60. #TODO - how to enforce that the firmware aligns to block boundaries?
  61. block_count = int(len(input_file) / block_size)
  62. print ("Block Count is ", block_count)
  63. for block_number in range(0, block_count):
  64. block_offset = (block_number * block_size)
  65. block_end = block_offset + block_size
  66. block_array = bytearray(input_file[block_offset: block_end])
  67. xor_block(block_array, block_array, block_number, block_size, file_key)
  68. for n in range (0, block_size):
  69. input_file[block_offset + n] = block_array[n]
  70. # update the expected CRC value.
  71. xor_crc = calculate_crc(block_array, xor_crc)
  72. # write CRC
  73. output_file.write(struct.pack("<I", xor_crc))
  74. # finally, append the encrypted results.
  75. output_file.write(input_file)
  76. return
  77. # Encrypt ${PROGNAME}.bin and save it as 'update.cbd'
  78. def encrypt(source, target, env):
  79. firmware = open(target[0].path, "rb")
  80. update = open(target[0].dir.path + '/update.cbd', "wb")
  81. length = os.path.getsize(target[0].path)
  82. encrypt_file(firmware, update, length)
  83. firmware.close()
  84. update.close()
  85. marlin.add_post_action(encrypt);