🏗️ Refactor build encrypt / rename (#22124)

This commit is contained in:
Scott Lahteine
2021-06-13 15:43:33 -05:00
committed by Scott Lahteine
parent 14ffc66c45
commit 2aa35577f2
8 changed files with 59 additions and 63 deletions

View File

@ -8,10 +8,10 @@ Import("env")
# Custom HEX from ELF
env.AddPostAction(
join("$BUILD_DIR","${PROGNAME}.elf"),
join("$BUILD_DIR", "${PROGNAME}.elf"),
env.VerboseAction(" ".join([
"$OBJCOPY", "-O ihex", "$TARGET", # TARGET=.pio/build/fysetc_STM32F1/firmware.elf
"\"" + join("$BUILD_DIR","${PROGNAME}.hex") + "\"", # Note: $BUILD_DIR is a full path
"\"" + join("$BUILD_DIR", "${PROGNAME}.hex") + "\"", # Note: $BUILD_DIR is a full path
]), "Building $TARGET"))
# In-line command with arguments

View File

@ -27,11 +27,12 @@ def encrypt_file(input, output_file, file_length):
output_file.write(input_file)
return
# Encrypt ${PROGNAME}.bin and save it as build.firmware
# Encrypt ${PROGNAME}.bin and save it with the name given in build.encrypt
def encrypt(source, target, env):
print("Encrypting to:", board.get("build.firmware"))
fwname = board.get("build.encrypt")
print("Encrypting %s to %s" % (target[0].path, fwname))
firmware = open(target[0].path, "rb")
renamed = open(target[0].dir.path + "/" + board.get("build.firmware"), "wb")
renamed = open(target[0].dir.path + "/" + fwname, "wb")
length = os.path.getsize(target[0].path)
encrypt_file(firmware, renamed, length)
@ -39,8 +40,8 @@ def encrypt(source, target, env):
firmware.close()
renamed.close()
if 'firmware' in board.get("build").keys():
marlin.add_post_action(encrypt);
if 'encrypt' in board.get("build").keys():
marlin.add_post_action(encrypt);
else:
print("You need to define output file via board_build.firmware = 'filename' parameter")
exit(1);
print("LERDGE builds require output file via board_build.encrypt = 'filename' parameter")
exit(1);

View File

@ -7,10 +7,12 @@ import os,shutil
from SCons.Script import DefaultEnvironment
env = DefaultEnvironment()
from os.path import join
def copytree(src, dst, symlinks=False, ignore=None):
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
s = join(src, item)
d = join(dst, item)
if os.path.isdir(s):
shutil.copytree(s, d, symlinks, ignore)
else:
@ -64,7 +66,7 @@ def encrypt_mks(source, target, env, new_name):
renamed.close()
def add_post_action(action):
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", action);
env.AddPostAction(join("$BUILD_DIR", "${PROGNAME}.bin"), action);
# Apply customizations for a MKS Robin
def prepare_robin(address, ldname, fwname):

View File

@ -3,6 +3,7 @@
#
# Apply encryption and save as 'build.firmware' for these environments:
# - env:mks_robin
# - env:mks_robin_e3
# - env:flsun_hispeedv1
# - env:mks_robin_nano35
#
@ -11,18 +12,18 @@ Import("env")
from SCons.Script import DefaultEnvironment
board = DefaultEnvironment().BoardConfig()
if 'firmware' in board.get("build").keys():
if 'encrypt' in board.get("build").keys():
import marlin
# Encrypt ${PROGNAME}.bin and save it as build.firmware
# Encrypt ${PROGNAME}.bin and save it with the name given in build.encrypt
def encrypt(source, target, env):
marlin.encrypt_mks(source, target, env, board.get("build.firmware"))
marlin.encrypt_mks(source, target, env, board.get("build.encrypt"))
marlin.add_post_action(encrypt);
else:
import sys
print("You need to define output file via board_build.firmware = 'filename' parameter", file=sys.stderr)
print("You need to define output file via board_build.encrypt = 'filename' parameter", file=sys.stderr)
env.Exit(1);

View File

@ -6,10 +6,13 @@ from os.path import join
Import("env")
env.AddPostAction(
"$BUILD_DIR/${PROGNAME}.elf",
env.VerboseAction(" ".join([
"$OBJCOPY", "-O", "srec",
"\"$BUILD_DIR/${PROGNAME}.elf\"", "\"$BUILD_DIR/${PROGNAME}.srec\""
]), "Building " + join("$BUILD_DIR", "${PROGNAME}.srec"))
)
board = env.BoardConfig()
board_keys = board.get("build").keys()
if 'encrypt' in board_keys:
env.AddPostAction(
join("$BUILD_DIR", "${PROGNAME}.bin"),
env.VerboseAction(" ".join([
"$OBJCOPY", "-O", "srec",
"\"$BUILD_DIR/${PROGNAME}.elf\"", "\"" + join("$BUILD_DIR", board.get("build.encrypt")) + "\""
]), "Building $TARGET")
)

View File

@ -1,23 +1,18 @@
#
# stm32_bootloader.py
#
import os,sys,shutil,marlin
import os,sys,marlin
Import("env")
from SCons.Script import DefaultEnvironment
board = DefaultEnvironment().BoardConfig()
#
# Copy the firmware.bin file to build.firmware, no encryption
#
def noencrypt(source, target, env):
firmware = os.path.join(target[0].dir.path, board.get("build.firmware"))
shutil.copy(target[0].path, firmware)
board_keys = board.get("build").keys()
#
# For build.offset define LD_FLASH_OFFSET, used by ldscript.ld
#
if 'offset' in board.get("build").keys():
if 'offset' in board_keys:
LD_FLASH_OFFSET = board.get("build.offset")
marlin.relocate_vtab(LD_FLASH_OFFSET)
@ -35,9 +30,13 @@ if 'offset' in board.get("build").keys():
env["LINKFLAGS"][i] = "-Wl,--defsym=LD_MAX_DATA_SIZE=" + str(maximum_ram_size - 40)
#
# Only copy the file if there's no encrypt
# For build.rename simply rename the firmware file.
#
board_keys = board.get("build").keys()
if 'firmware' in board_keys and ('encrypt' not in board_keys or board.get("build.encrypt") == 'No'):
import marlin
marlin.add_post_action(noencrypt)
if 'rename' in board_keys:
def rename_target(source, target, env):
firmware = os.path.join(target[0].dir.path, board.get("build.rename"))
import shutil
shutil.copy(target[0].path, firmware)
marlin.add_post_action(rename_target)