offset_and_rename.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #
  2. # offset_and_rename.py
  3. #
  4. # - If 'board_build.offset' is provided, either by JSON or by the environment...
  5. # - Set linker flag LD_FLASH_OFFSET and relocate the VTAB based on 'build.offset'.
  6. # - Set linker flag LD_MAX_DATA_SIZE based on 'build.maximum_ram_size'.
  7. # - Define STM32_FLASH_SIZE from 'upload.maximum_size' for use by Flash-based EEPROM emulation.
  8. #
  9. # - For 'board_build.rename' add a post-action to rename the firmware file.
  10. #
  11. import pioutil
  12. if pioutil.is_pio_build():
  13. import marlin
  14. env = marlin.env
  15. board = env.BoardConfig()
  16. board_keys = board.get("build").keys()
  17. #
  18. # For build.offset define LD_FLASH_OFFSET, used by ldscript.ld
  19. #
  20. if 'offset' in board_keys:
  21. LD_FLASH_OFFSET = board.get("build.offset")
  22. marlin.relocate_vtab(LD_FLASH_OFFSET)
  23. # Flash size
  24. maximum_flash_size = board.get("upload.maximum_size") // 1024
  25. marlin.replace_define('STM32_FLASH_SIZE', maximum_flash_size)
  26. # Get upload.maximum_ram_size (defined by /buildroot/share/PlatformIO/boards/VARIOUS.json)
  27. maximum_ram_size = board.get("upload.maximum_ram_size")
  28. for i, flag in enumerate(env["LINKFLAGS"]):
  29. if "-Wl,--defsym=LD_FLASH_OFFSET" in flag:
  30. env["LINKFLAGS"][i] = "-Wl,--defsym=LD_FLASH_OFFSET=" + LD_FLASH_OFFSET
  31. if "-Wl,--defsym=LD_MAX_DATA_SIZE" in flag:
  32. env["LINKFLAGS"][i] = "-Wl,--defsym=LD_MAX_DATA_SIZE=" + str(maximum_ram_size - 40)
  33. #
  34. # For build.encrypt_mks rename and encode the firmware file.
  35. #
  36. if 'encrypt_mks' in board_keys:
  37. # Encrypt ${PROGNAME}.bin and save it with the name given in build.encrypt_mks
  38. def encrypt(source, target, env):
  39. marlin.encrypt_mks(source, target, env, board.get("build.encrypt_mks"))
  40. if board.get("build.encrypt_mks") != "":
  41. marlin.add_post_action(encrypt)
  42. #
  43. # For build.rename simply rename the firmware file.
  44. #
  45. if 'rename' in board_keys:
  46. # If FIRMWARE_BIN is defined by config, override all
  47. mf = env["MARLIN_FEATURES"]
  48. if "FIRMWARE_BIN" in mf: new_name = mf["FIRMWARE_BIN"]
  49. else: new_name = board.get("build.rename")
  50. def rename_target(source, target, env):
  51. from pathlib import Path
  52. from datetime import datetime
  53. from os import path
  54. _newpath = Path(target[0].dir.path, datetime.now().strftime(new_name.replace('{date}', '%Y%m%d').replace('{time}', '%H%M%S')))
  55. Path(target[0].path).replace(_newpath)
  56. env['PROGNAME'] = path.splitext(_newpath)[0]
  57. marlin.add_post_action(rename_target)