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.7KB

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