Skip to content

Commit

Permalink
🏗️ Refactor build encrypt / rename (#22124)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jun 15, 2021
1 parent 14ffc66 commit 2aa3557
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 63 deletions.
4 changes: 2 additions & 2 deletions buildroot/share/PlatformIO/scripts/STM32F103RC_fysetc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

# 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
Expand Down
15 changes: 8 additions & 7 deletions buildroot/share/PlatformIO/scripts/lerdge.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,21 @@ 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)

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);
8 changes: 5 additions & 3 deletions buildroot/share/PlatformIO/scripts/marlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
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:
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 5 additions & 4 deletions buildroot/share/PlatformIO/scripts/mks_encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand All @@ -11,18 +12,18 @@
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);
17 changes: 10 additions & 7 deletions buildroot/share/PlatformIO/scripts/openblt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

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")
)
25 changes: 12 additions & 13 deletions buildroot/share/PlatformIO/scripts/stm32_bootloader.py
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -35,9 +30,13 @@ def noencrypt(source, target, env):
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)
14 changes: 4 additions & 10 deletions ini/stm32f1.ini
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ platform = ${common_stm32.platform}
extends = common_STM32F103RC
build_flags = ${common_stm32.build_flags} -DDEBUG_LEVEL=0 -DTIMER_SERVO=TIM5
board_build.offset = 0x7000
board_build.encrypt = No
board_build.firmware = firmware.bin
board_upload.offset_address = 0x08007000

[env:STM32F103RC_btt_USB]
Expand Down Expand Up @@ -113,8 +111,7 @@ board_build.core = stm32
board_build.variant = MARLIN_F103Zx
board_build.ldscript = ldscript.ld
board_build.offset = 0x7000
board_build.encrypt = Yes
board_build.firmware = Robin.bin
board_build.encrypt = Robin.bin
build_flags = ${common_stm32.build_flags}
-DENABLE_HWSERIAL3 -DTIMER_SERIAL=TIM5
build_unflags = ${common_stm32.build_unflags}
Expand All @@ -136,8 +133,7 @@ build_flags = ${common_stm32.build_flags}
build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC
monitor_speed = 115200
board_build.offset = 0x5000
board_build.encrypt = Yes
board_build.firmware = Robin_e3.bin
board_build.encrypt = Robin_e3.bin
board_upload.offset_address = 0x08005000
debug_tool = stlink
extra_scripts = ${env:STM32F103RC.extra_scripts}
Expand Down Expand Up @@ -215,8 +211,7 @@ board_build.core = stm32
board_build.variant = MARLIN_F103Vx
board_build.ldscript = ldscript.ld
board_build.offset = 0x7000
board_build.firmware = Robin_mini.bin
board_build.encrypt = Yes
board_build.encrypt = Robin_mini.bin
board_upload.offset_address = 0x08007000
build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC
extra_scripts = ${common_stm32.extra_scripts}
Expand All @@ -236,8 +231,7 @@ board_build.core = stm32
board_build.variant = MARLIN_F103Vx
board_build.ldscript = ldscript.ld
board_build.offset = 0x7000
board_build.encrypt = Yes
board_build.firmware = Robin_nano35.bin
board_build.encrypt = Robin_nano35.bin
board_upload.offset_address = 0x08007000
build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC
debug_tool = jlink
Expand Down
30 changes: 13 additions & 17 deletions ini/stm32f4.ini
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ board = marlin_STM32F407VGT6_CCM
board_build.core = stm32
board_build.variant = MARLIN_F4x7Vx
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
board_build.encrypt = Yes
board_build.offset = 0x10000
board_upload.offset_address = 0x08010000
build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC -DUSBD_VID=0x0483
Expand Down Expand Up @@ -265,7 +264,7 @@ extends = common_stm32
board = marlin_STM32F407ZGT6
board_build.variant = MARLIN_LERDGE
board_build.offset = 0x10000
board_build.encrypt = Yes
board_build.encrypt = firmware.bin
extra_scripts = ${common.extra_scripts}
pre:buildroot/share/PlatformIO/scripts/generic_create_variant.py
buildroot/share/PlatformIO/scripts/stm32_bootloader.py
Expand All @@ -280,9 +279,9 @@ build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC -DUS
# Lerdge X
#
[env:LERDGEX]
platform = ${lerdge_common.platform}
extends = lerdge_common
board_build.firmware = Lerdge_X_firmware_force.bin
platform = ${lerdge_common.platform}
extends = lerdge_common
board_build.encrypt = Lerdge_X_firmware_force.bin

#
# Lerdge X with USB Flash Drive Support
Expand All @@ -297,9 +296,9 @@ build_flags = ${stm_flash_drive.build_flags} ${lerdge_common.build_flags}
# Lerdge S
#
[env:LERDGES]
platform = ${lerdge_common.platform}
extends = lerdge_common
board_build.firmware = Lerdge_firmware_force.bin
platform = ${lerdge_common.platform}
extends = lerdge_common
board_build.encrypt = Lerdge_firmware_force.bin

#
# Lerdge S with USB Flash Drive Support
Expand All @@ -314,10 +313,10 @@ build_flags = ${stm_flash_drive.build_flags} ${lerdge_common.build_flags}
# Lerdge K
#
[env:LERDGEK]
platform = ${lerdge_common.platform}
extends = lerdge_common
board_build.firmware = Lerdge_K_firmware_force.bin
build_flags = ${lerdge_common.build_flags} -DLERDGEK
platform = ${lerdge_common.platform}
extends = lerdge_common
board_build.encrypt = Lerdge_K_firmware_force.bin
build_flags = ${lerdge_common.build_flags} -DLERDGEK

#
# Lerdge K with USB Flash Drive Support
Expand Down Expand Up @@ -347,8 +346,6 @@ board_build.core = stm32
board_build.variant = MARLIN_F446VE
board_build.ldscript = ldscript.ld
board_build.offset = 0x0000
board_build.encrypt = No
board_build.firmware = firmware.bin
extra_scripts = ${common.extra_scripts}
pre:buildroot/share/PlatformIO/scripts/generic_create_variant.py
buildroot/share/PlatformIO/scripts/stm32_bootloader.py
Expand All @@ -365,7 +362,6 @@ board = genericSTM32F407VET6
board_build.core = stm32
board_build.variant = MARLIN_F4x7Vx
board_build.ldscript = ldscript.ld
board_build.firmware = firmware.bin
board_build.offset = 0x0000
board_upload.offset_address = 0x08000000
build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC
Expand All @@ -392,7 +388,7 @@ board = marlin_STM32F407VGT6_CCM
board_build.core = stm32
board_build.variant = MARLIN_F4x7Vx
board_build.ldscript = ldscript.ld
board_build.firmware = Robin_nano_v3.bin
board_build.rename = Robin_nano_v3.bin
board_build.offset = 0xC000
board_upload.offset_address = 0x0800C000
build_unflags = ${common_stm32.build_unflags}
Expand Down

0 comments on commit 2aa3557

Please sign in to comment.