🏗️ 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 # Custom HEX from ELF
env.AddPostAction( env.AddPostAction(
join("$BUILD_DIR","${PROGNAME}.elf"), join("$BUILD_DIR", "${PROGNAME}.elf"),
env.VerboseAction(" ".join([ env.VerboseAction(" ".join([
"$OBJCOPY", "-O ihex", "$TARGET", # TARGET=.pio/build/fysetc_STM32F1/firmware.elf "$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")) ]), "Building $TARGET"))
# In-line command with arguments # In-line command with arguments

View File

@ -27,11 +27,12 @@ def encrypt_file(input, output_file, file_length):
output_file.write(input_file) output_file.write(input_file)
return 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): 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") 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) length = os.path.getsize(target[0].path)
encrypt_file(firmware, renamed, length) encrypt_file(firmware, renamed, length)
@ -39,8 +40,8 @@ def encrypt(source, target, env):
firmware.close() firmware.close()
renamed.close() renamed.close()
if 'firmware' in board.get("build").keys(): if 'encrypt' in board.get("build").keys():
marlin.add_post_action(encrypt); marlin.add_post_action(encrypt);
else: else:
print("You need to define output file via board_build.firmware = 'filename' parameter") print("LERDGE builds require output file via board_build.encrypt = 'filename' parameter")
exit(1); exit(1);

View File

@ -7,10 +7,12 @@ import os,shutil
from SCons.Script import DefaultEnvironment from SCons.Script import DefaultEnvironment
env = DefaultEnvironment() env = DefaultEnvironment()
from os.path import join
def copytree(src, dst, symlinks=False, ignore=None): def copytree(src, dst, symlinks=False, ignore=None):
for item in os.listdir(src): for item in os.listdir(src):
s = os.path.join(src, item) s = join(src, item)
d = os.path.join(dst, item) d = join(dst, item)
if os.path.isdir(s): if os.path.isdir(s):
shutil.copytree(s, d, symlinks, ignore) shutil.copytree(s, d, symlinks, ignore)
else: else:
@ -64,7 +66,7 @@ def encrypt_mks(source, target, env, new_name):
renamed.close() renamed.close()
def add_post_action(action): 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 # Apply customizations for a MKS Robin
def prepare_robin(address, ldname, fwname): def prepare_robin(address, ldname, fwname):

View File

@ -3,6 +3,7 @@
# #
# Apply encryption and save as 'build.firmware' for these environments: # Apply encryption and save as 'build.firmware' for these environments:
# - env:mks_robin # - env:mks_robin
# - env:mks_robin_e3
# - env:flsun_hispeedv1 # - env:flsun_hispeedv1
# - env:mks_robin_nano35 # - env:mks_robin_nano35
# #
@ -11,18 +12,18 @@ Import("env")
from SCons.Script import DefaultEnvironment from SCons.Script import DefaultEnvironment
board = DefaultEnvironment().BoardConfig() board = DefaultEnvironment().BoardConfig()
if 'firmware' in board.get("build").keys(): if 'encrypt' in board.get("build").keys():
import marlin 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): 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); marlin.add_post_action(encrypt);
else: else:
import sys 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); env.Exit(1);

View File

@ -6,10 +6,13 @@ from os.path import join
Import("env") Import("env")
env.AddPostAction( board = env.BoardConfig()
"$BUILD_DIR/${PROGNAME}.elf", board_keys = board.get("build").keys()
env.VerboseAction(" ".join([ if 'encrypt' in board_keys:
"$OBJCOPY", "-O", "srec", env.AddPostAction(
"\"$BUILD_DIR/${PROGNAME}.elf\"", "\"$BUILD_DIR/${PROGNAME}.srec\"" join("$BUILD_DIR", "${PROGNAME}.bin"),
]), "Building " + join("$BUILD_DIR", "${PROGNAME}.srec")) 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 # stm32_bootloader.py
# #
import os,sys,shutil,marlin import os,sys,marlin
Import("env") Import("env")
from SCons.Script import DefaultEnvironment from SCons.Script import DefaultEnvironment
board = DefaultEnvironment().BoardConfig() board = DefaultEnvironment().BoardConfig()
# board_keys = board.get("build").keys()
# 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)
# #
# For build.offset define LD_FLASH_OFFSET, used by ldscript.ld # 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") LD_FLASH_OFFSET = board.get("build.offset")
marlin.relocate_vtab(LD_FLASH_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) 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 'rename' in board_keys:
if 'firmware' in board_keys and ('encrypt' not in board_keys or board.get("build.encrypt") == 'No'):
import marlin def rename_target(source, target, env):
marlin.add_post_action(noencrypt) 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)

View File

@ -84,8 +84,6 @@ platform = ${common_stm32.platform}
extends = common_STM32F103RC extends = common_STM32F103RC
build_flags = ${common_stm32.build_flags} -DDEBUG_LEVEL=0 -DTIMER_SERVO=TIM5 build_flags = ${common_stm32.build_flags} -DDEBUG_LEVEL=0 -DTIMER_SERVO=TIM5
board_build.offset = 0x7000 board_build.offset = 0x7000
board_build.encrypt = No
board_build.firmware = firmware.bin
board_upload.offset_address = 0x08007000 board_upload.offset_address = 0x08007000
[env:STM32F103RC_btt_USB] [env:STM32F103RC_btt_USB]
@ -113,8 +111,7 @@ board_build.core = stm32
board_build.variant = MARLIN_F103Zx board_build.variant = MARLIN_F103Zx
board_build.ldscript = ldscript.ld board_build.ldscript = ldscript.ld
board_build.offset = 0x7000 board_build.offset = 0x7000
board_build.encrypt = Yes board_build.encrypt = Robin.bin
board_build.firmware = Robin.bin
build_flags = ${common_stm32.build_flags} build_flags = ${common_stm32.build_flags}
-DENABLE_HWSERIAL3 -DTIMER_SERIAL=TIM5 -DENABLE_HWSERIAL3 -DTIMER_SERIAL=TIM5
build_unflags = ${common_stm32.build_unflags} build_unflags = ${common_stm32.build_unflags}
@ -136,8 +133,7 @@ build_flags = ${common_stm32.build_flags}
build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC
monitor_speed = 115200 monitor_speed = 115200
board_build.offset = 0x5000 board_build.offset = 0x5000
board_build.encrypt = Yes board_build.encrypt = Robin_e3.bin
board_build.firmware = Robin_e3.bin
board_upload.offset_address = 0x08005000 board_upload.offset_address = 0x08005000
debug_tool = stlink debug_tool = stlink
extra_scripts = ${env:STM32F103RC.extra_scripts} extra_scripts = ${env:STM32F103RC.extra_scripts}
@ -215,8 +211,7 @@ board_build.core = stm32
board_build.variant = MARLIN_F103Vx board_build.variant = MARLIN_F103Vx
board_build.ldscript = ldscript.ld board_build.ldscript = ldscript.ld
board_build.offset = 0x7000 board_build.offset = 0x7000
board_build.firmware = Robin_mini.bin board_build.encrypt = Robin_mini.bin
board_build.encrypt = Yes
board_upload.offset_address = 0x08007000 board_upload.offset_address = 0x08007000
build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC
extra_scripts = ${common_stm32.extra_scripts} extra_scripts = ${common_stm32.extra_scripts}
@ -236,8 +231,7 @@ board_build.core = stm32
board_build.variant = MARLIN_F103Vx board_build.variant = MARLIN_F103Vx
board_build.ldscript = ldscript.ld board_build.ldscript = ldscript.ld
board_build.offset = 0x7000 board_build.offset = 0x7000
board_build.encrypt = Yes board_build.encrypt = Robin_nano35.bin
board_build.firmware = Robin_nano35.bin
board_upload.offset_address = 0x08007000 board_upload.offset_address = 0x08007000
build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC
debug_tool = jlink debug_tool = jlink

View File

@ -113,9 +113,8 @@ board = marlin_STM32F407VGT6_CCM
board_build.core = stm32 board_build.core = stm32
board_build.variant = MARLIN_F4x7Vx board_build.variant = MARLIN_F4x7Vx
board_build.ldscript = ldscript.ld board_build.ldscript = ldscript.ld
board_build.firmware = firmware.srec board_build.encrypt = firmware.srec
# Just openblt.py (not stm32_bootloader.py) generates the file # Just openblt.py (not stm32_bootloader.py) generates the file
board_build.encrypt = Yes
board_build.offset = 0x10000 board_build.offset = 0x10000
board_upload.offset_address = 0x08010000 board_upload.offset_address = 0x08010000
build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC -DUSBD_VID=0x0483 build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC -DUSBD_VID=0x0483
@ -265,7 +264,7 @@ extends = common_stm32
board = marlin_STM32F407ZGT6 board = marlin_STM32F407ZGT6
board_build.variant = MARLIN_LERDGE board_build.variant = MARLIN_LERDGE
board_build.offset = 0x10000 board_build.offset = 0x10000
board_build.encrypt = Yes board_build.encrypt = firmware.bin
extra_scripts = ${common.extra_scripts} extra_scripts = ${common.extra_scripts}
pre:buildroot/share/PlatformIO/scripts/generic_create_variant.py pre:buildroot/share/PlatformIO/scripts/generic_create_variant.py
buildroot/share/PlatformIO/scripts/stm32_bootloader.py buildroot/share/PlatformIO/scripts/stm32_bootloader.py
@ -280,9 +279,9 @@ build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC -DUS
# Lerdge X # Lerdge X
# #
[env:LERDGEX] [env:LERDGEX]
platform = ${lerdge_common.platform} platform = ${lerdge_common.platform}
extends = lerdge_common extends = lerdge_common
board_build.firmware = Lerdge_X_firmware_force.bin board_build.encrypt = Lerdge_X_firmware_force.bin
# #
# Lerdge X with USB Flash Drive Support # Lerdge X with USB Flash Drive Support
@ -297,9 +296,9 @@ build_flags = ${stm_flash_drive.build_flags} ${lerdge_common.build_flags}
# Lerdge S # Lerdge S
# #
[env:LERDGES] [env:LERDGES]
platform = ${lerdge_common.platform} platform = ${lerdge_common.platform}
extends = lerdge_common extends = lerdge_common
board_build.firmware = Lerdge_firmware_force.bin board_build.encrypt = Lerdge_firmware_force.bin
# #
# Lerdge S with USB Flash Drive Support # Lerdge S with USB Flash Drive Support
@ -314,10 +313,10 @@ build_flags = ${stm_flash_drive.build_flags} ${lerdge_common.build_flags}
# Lerdge K # Lerdge K
# #
[env:LERDGEK] [env:LERDGEK]
platform = ${lerdge_common.platform} platform = ${lerdge_common.platform}
extends = lerdge_common extends = lerdge_common
board_build.firmware = Lerdge_K_firmware_force.bin board_build.encrypt = Lerdge_K_firmware_force.bin
build_flags = ${lerdge_common.build_flags} -DLERDGEK build_flags = ${lerdge_common.build_flags} -DLERDGEK
# #
# Lerdge K with USB Flash Drive Support # Lerdge K with USB Flash Drive Support
@ -347,8 +346,6 @@ board_build.core = stm32
board_build.variant = MARLIN_F446VE board_build.variant = MARLIN_F446VE
board_build.ldscript = ldscript.ld board_build.ldscript = ldscript.ld
board_build.offset = 0x0000 board_build.offset = 0x0000
board_build.encrypt = No
board_build.firmware = firmware.bin
extra_scripts = ${common.extra_scripts} extra_scripts = ${common.extra_scripts}
pre:buildroot/share/PlatformIO/scripts/generic_create_variant.py pre:buildroot/share/PlatformIO/scripts/generic_create_variant.py
buildroot/share/PlatformIO/scripts/stm32_bootloader.py buildroot/share/PlatformIO/scripts/stm32_bootloader.py
@ -365,7 +362,6 @@ board = genericSTM32F407VET6
board_build.core = stm32 board_build.core = stm32
board_build.variant = MARLIN_F4x7Vx board_build.variant = MARLIN_F4x7Vx
board_build.ldscript = ldscript.ld board_build.ldscript = ldscript.ld
board_build.firmware = firmware.bin
board_build.offset = 0x0000 board_build.offset = 0x0000
board_upload.offset_address = 0x08000000 board_upload.offset_address = 0x08000000
build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC
@ -392,7 +388,7 @@ board = marlin_STM32F407VGT6_CCM
board_build.core = stm32 board_build.core = stm32
board_build.variant = MARLIN_F4x7Vx board_build.variant = MARLIN_F4x7Vx
board_build.ldscript = ldscript.ld board_build.ldscript = ldscript.ld
board_build.firmware = Robin_nano_v3.bin board_build.rename = Robin_nano_v3.bin
board_build.offset = 0xC000 board_build.offset = 0xC000
board_upload.offset_address = 0x0800C000 board_upload.offset_address = 0x0800C000
build_unflags = ${common_stm32.build_unflags} build_unflags = ${common_stm32.build_unflags}