2021-04-27 09:49:21 +00:00
|
|
|
#
|
2021-11-04 10:28:42 +00:00
|
|
|
# marlin.py
|
2021-04-27 09:49:21 +00:00
|
|
|
# Helper module with some commonly-used functions
|
|
|
|
#
|
2022-08-01 06:14:58 +00:00
|
|
|
import shutil
|
|
|
|
from pathlib import Path
|
2021-04-27 09:49:21 +00:00
|
|
|
|
|
|
|
from SCons.Script import DefaultEnvironment
|
|
|
|
env = DefaultEnvironment()
|
|
|
|
|
|
|
|
def copytree(src, dst, symlinks=False, ignore=None):
|
2022-08-19 16:00:52 +00:00
|
|
|
for item in src.iterdir():
|
|
|
|
if item.is_dir():
|
|
|
|
shutil.copytree(item, dst / item.name, symlinks, ignore)
|
|
|
|
else:
|
|
|
|
shutil.copy2(item, dst / item.name)
|
2021-04-27 09:49:21 +00:00
|
|
|
|
|
|
|
def replace_define(field, value):
|
2023-03-26 22:49:51 +00:00
|
|
|
envdefs = env['CPPDEFINES'].copy()
|
|
|
|
for define in envdefs:
|
2022-08-19 16:00:52 +00:00
|
|
|
if define[0] == field:
|
|
|
|
env['CPPDEFINES'].remove(define)
|
|
|
|
env['CPPDEFINES'].append((field, value))
|
2021-04-27 09:49:21 +00:00
|
|
|
|
|
|
|
# Relocate the firmware to a new address, such as "0x08005000"
|
|
|
|
def relocate_firmware(address):
|
2022-08-19 16:00:52 +00:00
|
|
|
replace_define("VECT_TAB_ADDR", address)
|
2021-04-27 09:49:21 +00:00
|
|
|
|
|
|
|
# Relocate the vector table with a new offset
|
|
|
|
def relocate_vtab(address):
|
2022-08-19 16:00:52 +00:00
|
|
|
replace_define("VECT_TAB_OFFSET", address)
|
2021-04-27 09:49:21 +00:00
|
|
|
|
|
|
|
# Replace the existing -Wl,-T with the given ldscript path
|
|
|
|
def custom_ld_script(ldname):
|
2022-08-19 16:00:52 +00:00
|
|
|
apath = str(Path("buildroot/share/PlatformIO/ldscripts", ldname).resolve())
|
|
|
|
for i, flag in enumerate(env["LINKFLAGS"]):
|
|
|
|
if "-Wl,-T" in flag:
|
|
|
|
env["LINKFLAGS"][i] = "-Wl,-T" + apath
|
|
|
|
elif flag == "-T":
|
|
|
|
env["LINKFLAGS"][i + 1] = apath
|
2021-04-27 09:49:21 +00:00
|
|
|
|
2022-06-24 04:58:26 +00:00
|
|
|
# Encrypt ${PROGNAME}.bin and save it with a new name. This applies (mostly) to MKS boards
|
|
|
|
# This PostAction is set up by offset_and_rename.py for envs with 'build.encrypt_mks'.
|
2021-04-27 09:49:21 +00:00
|
|
|
def encrypt_mks(source, target, env, new_name):
|
2022-08-19 16:00:52 +00:00
|
|
|
import sys
|
2021-04-27 09:49:21 +00:00
|
|
|
|
2022-08-19 16:00:52 +00:00
|
|
|
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]
|
2021-04-27 09:49:21 +00:00
|
|
|
|
2022-08-19 16:00:52 +00:00
|
|
|
# If FIRMWARE_BIN is defined by config, override all
|
|
|
|
mf = env["MARLIN_FEATURES"]
|
|
|
|
if "FIRMWARE_BIN" in mf: new_name = mf["FIRMWARE_BIN"]
|
2021-12-25 05:47:52 +00:00
|
|
|
|
2022-08-19 16:00:52 +00:00
|
|
|
fwpath = Path(target[0].path)
|
|
|
|
fwfile = fwpath.open("rb")
|
|
|
|
enfile = Path(target[0].dir.path, new_name).open("wb")
|
|
|
|
length = fwpath.stat().st_size
|
|
|
|
position = 0
|
|
|
|
try:
|
|
|
|
while position < length:
|
|
|
|
byte = fwfile.read(1)
|
|
|
|
if 320 <= position < 31040:
|
|
|
|
byte = chr(ord(byte) ^ key[position & 31])
|
|
|
|
if sys.version_info[0] > 2:
|
|
|
|
byte = bytes(byte, 'latin1')
|
|
|
|
enfile.write(byte)
|
|
|
|
position += 1
|
|
|
|
finally:
|
|
|
|
fwfile.close()
|
|
|
|
enfile.close()
|
|
|
|
fwpath.unlink()
|
2021-04-27 09:49:21 +00:00
|
|
|
|
|
|
|
def add_post_action(action):
|
2022-08-19 16:00:52 +00:00
|
|
|
env.AddPostAction(str(Path("$BUILD_DIR", "${PROGNAME}.bin")), action);
|