chitu_crypt.py 3.5 KB

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