2021-04-27 04:49:21 -05:00
|
|
|
#
|
2021-11-04 05:28:42 -05:00
|
|
|
# marlin.py
|
2021-04-27 04:49:21 -05:00
|
|
|
# Helper module with some commonly-used functions
|
|
|
|
#
|
|
|
|
import os,shutil
|
|
|
|
|
|
|
|
from SCons.Script import DefaultEnvironment
|
|
|
|
env = DefaultEnvironment()
|
|
|
|
|
2021-06-13 15:43:33 -05:00
|
|
|
from os.path import join
|
|
|
|
|
2021-04-27 04:49:21 -05:00
|
|
|
def copytree(src, dst, symlinks=False, ignore=None):
|
2021-11-04 05:28:42 -05:00
|
|
|
for item in os.listdir(src):
|
|
|
|
s = join(src, item)
|
|
|
|
d = join(dst, item)
|
|
|
|
if os.path.isdir(s):
|
|
|
|
shutil.copytree(s, d, symlinks, ignore)
|
|
|
|
else:
|
|
|
|
shutil.copy2(s, d)
|
2021-04-27 04:49:21 -05:00
|
|
|
|
|
|
|
def replace_define(field, value):
|
|
|
|
for define in env['CPPDEFINES']:
|
|
|
|
if define[0] == field:
|
|
|
|
env['CPPDEFINES'].remove(define)
|
|
|
|
env['CPPDEFINES'].append((field, value))
|
|
|
|
|
|
|
|
# Relocate the firmware to a new address, such as "0x08005000"
|
|
|
|
def relocate_firmware(address):
|
|
|
|
replace_define("VECT_TAB_ADDR", address)
|
|
|
|
|
|
|
|
# Relocate the vector table with a new offset
|
|
|
|
def relocate_vtab(address):
|
|
|
|
replace_define("VECT_TAB_OFFSET", address)
|
|
|
|
|
|
|
|
# Replace the existing -Wl,-T with the given ldscript path
|
|
|
|
def custom_ld_script(ldname):
|
|
|
|
apath = os.path.abspath("buildroot/share/PlatformIO/ldscripts/" + ldname)
|
|
|
|
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
|
|
|
|
|
|
|
|
# Encrypt ${PROGNAME}.bin and save it with a new name
|
|
|
|
# Called by specific encrypt() functions, mostly for MKS boards
|
|
|
|
def encrypt_mks(source, target, env, new_name):
|
|
|
|
import sys
|
|
|
|
|
|
|
|
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-12-24 23:47:52 -06:00
|
|
|
# If FIRMWARE_BIN is defined by config, override all
|
2022-01-09 01:06:19 -06:00
|
|
|
mf = env["MARLIN_FEATURES"]
|
|
|
|
if "FIRMWARE_BIN" in mf: new_name = mf["FIRMWARE_BIN"]
|
2021-12-24 23:47:52 -06:00
|
|
|
|
2021-10-19 02:56:44 -05:00
|
|
|
fwpath = target[0].path
|
|
|
|
fwfile = open(fwpath, "rb")
|
|
|
|
enfile = open(target[0].dir.path + "/" + new_name, "wb")
|
|
|
|
length = os.path.getsize(fwpath)
|
2021-04-27 04:49:21 -05:00
|
|
|
position = 0
|
|
|
|
try:
|
|
|
|
while position < length:
|
2021-10-19 02:56:44 -05:00
|
|
|
byte = fwfile.read(1)
|
2021-04-27 04:49:21 -05:00
|
|
|
if position >= 320 and position < 31040:
|
|
|
|
byte = chr(ord(byte) ^ key[position & 31])
|
|
|
|
if sys.version_info[0] > 2:
|
|
|
|
byte = bytes(byte, 'latin1')
|
2021-10-19 02:56:44 -05:00
|
|
|
enfile.write(byte)
|
2021-04-27 04:49:21 -05:00
|
|
|
position += 1
|
|
|
|
finally:
|
2021-10-19 02:56:44 -05:00
|
|
|
fwfile.close()
|
|
|
|
enfile.close()
|
|
|
|
os.remove(fwpath)
|
2021-04-27 04:49:21 -05:00
|
|
|
|
|
|
|
def add_post_action(action):
|
2021-06-13 15:43:33 -05:00
|
|
|
env.AddPostAction(join("$BUILD_DIR", "${PROGNAME}.bin"), action);
|