mks_robin.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import os, marlin
  2. Import("env")
  3. # Relocate firmware from 0x08000000 to 0x08007000
  4. marlin.relocate_firmware("0x08007000")
  5. custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/mks_robin.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. # Encrypt ${PROGNAME}.bin and save it as 'Robin.bin'
  12. def encrypt(source, target, env):
  13. import sys
  14. key = [0xA3, 0xBD, 0xAD, 0x0D, 0x41, 0x11, 0xBB, 0x8D, 0xDC, 0x80, 0x2D, 0xD0, 0xD2, 0xC4, 0x9B, 0x1E, 0x26, 0xEB, 0xE3, 0x33, 0x4A, 0x15, 0xE4, 0x0A, 0xB3, 0xB1, 0x3C, 0x93, 0xBB, 0xAF, 0xF7, 0x3E]
  15. firmware = open(target[0].path, "rb")
  16. robin = open(target[0].dir.path +'/Robin.bin', "wb")
  17. length = os.path.getsize(target[0].path)
  18. position = 0
  19. try:
  20. while position < length:
  21. byte = firmware.read(1)
  22. if position >= 320 and position < 31040:
  23. byte = chr(ord(byte) ^ key[position & 31])
  24. if sys.version_info[0] > 2:
  25. byte = bytes(byte, 'latin1')
  26. robin.write(byte)
  27. position += 1
  28. finally:
  29. firmware.close()
  30. robin.close()
  31. env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt);