Skip to content

Commit 2aa3557

Browse files
committed
🏗️ Refactor build encrypt / rename (MarlinFirmware#22124)
1 parent 14ffc66 commit 2aa3557

File tree

8 files changed

+59
-63
lines changed

8 files changed

+59
-63
lines changed

buildroot/share/PlatformIO/scripts/STM32F103RC_fysetc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
# Custom HEX from ELF
1010
env.AddPostAction(
11-
join("$BUILD_DIR","${PROGNAME}.elf"),
11+
join("$BUILD_DIR", "${PROGNAME}.elf"),
1212
env.VerboseAction(" ".join([
1313
"$OBJCOPY", "-O ihex", "$TARGET", # TARGET=.pio/build/fysetc_STM32F1/firmware.elf
14-
"\"" + join("$BUILD_DIR","${PROGNAME}.hex") + "\"", # Note: $BUILD_DIR is a full path
14+
"\"" + join("$BUILD_DIR", "${PROGNAME}.hex") + "\"", # Note: $BUILD_DIR is a full path
1515
]), "Building $TARGET"))
1616

1717
# In-line command with arguments

buildroot/share/PlatformIO/scripts/lerdge.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,21 @@ def encrypt_file(input, output_file, file_length):
2727
output_file.write(input_file)
2828
return
2929

30-
# Encrypt ${PROGNAME}.bin and save it as build.firmware
30+
# Encrypt ${PROGNAME}.bin and save it with the name given in build.encrypt
3131
def encrypt(source, target, env):
32-
print("Encrypting to:", board.get("build.firmware"))
32+
fwname = board.get("build.encrypt")
33+
print("Encrypting %s to %s" % (target[0].path, fwname))
3334
firmware = open(target[0].path, "rb")
34-
renamed = open(target[0].dir.path + "/" + board.get("build.firmware"), "wb")
35+
renamed = open(target[0].dir.path + "/" + fwname, "wb")
3536
length = os.path.getsize(target[0].path)
3637

3738
encrypt_file(firmware, renamed, length)
3839

3940
firmware.close()
4041
renamed.close()
4142

42-
if 'firmware' in board.get("build").keys():
43-
marlin.add_post_action(encrypt);
43+
if 'encrypt' in board.get("build").keys():
44+
marlin.add_post_action(encrypt);
4445
else:
45-
print("You need to define output file via board_build.firmware = 'filename' parameter")
46-
exit(1);
46+
print("LERDGE builds require output file via board_build.encrypt = 'filename' parameter")
47+
exit(1);

buildroot/share/PlatformIO/scripts/marlin.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
from SCons.Script import DefaultEnvironment
88
env = DefaultEnvironment()
99

10+
from os.path import join
11+
1012
def copytree(src, dst, symlinks=False, ignore=None):
1113
for item in os.listdir(src):
12-
s = os.path.join(src, item)
13-
d = os.path.join(dst, item)
14+
s = join(src, item)
15+
d = join(dst, item)
1416
if os.path.isdir(s):
1517
shutil.copytree(s, d, symlinks, ignore)
1618
else:
@@ -64,7 +66,7 @@ def encrypt_mks(source, target, env, new_name):
6466
renamed.close()
6567

6668
def add_post_action(action):
67-
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", action);
69+
env.AddPostAction(join("$BUILD_DIR", "${PROGNAME}.bin"), action);
6870

6971
# Apply customizations for a MKS Robin
7072
def prepare_robin(address, ldname, fwname):

buildroot/share/PlatformIO/scripts/mks_encrypt.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#
44
# Apply encryption and save as 'build.firmware' for these environments:
55
# - env:mks_robin
6+
# - env:mks_robin_e3
67
# - env:flsun_hispeedv1
78
# - env:mks_robin_nano35
89
#
@@ -11,18 +12,18 @@
1112
from SCons.Script import DefaultEnvironment
1213
board = DefaultEnvironment().BoardConfig()
1314

14-
if 'firmware' in board.get("build").keys():
15+
if 'encrypt' in board.get("build").keys():
1516

1617
import marlin
1718

18-
# Encrypt ${PROGNAME}.bin and save it as build.firmware
19+
# Encrypt ${PROGNAME}.bin and save it with the name given in build.encrypt
1920
def encrypt(source, target, env):
20-
marlin.encrypt_mks(source, target, env, board.get("build.firmware"))
21+
marlin.encrypt_mks(source, target, env, board.get("build.encrypt"))
2122

2223
marlin.add_post_action(encrypt);
2324

2425
else:
2526

2627
import sys
27-
print("You need to define output file via board_build.firmware = 'filename' parameter", file=sys.stderr)
28+
print("You need to define output file via board_build.encrypt = 'filename' parameter", file=sys.stderr)
2829
env.Exit(1);

buildroot/share/PlatformIO/scripts/openblt.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66

77
Import("env")
88

9-
env.AddPostAction(
10-
"$BUILD_DIR/${PROGNAME}.elf",
11-
env.VerboseAction(" ".join([
12-
"$OBJCOPY", "-O", "srec",
13-
"\"$BUILD_DIR/${PROGNAME}.elf\"", "\"$BUILD_DIR/${PROGNAME}.srec\""
14-
]), "Building " + join("$BUILD_DIR", "${PROGNAME}.srec"))
15-
)
9+
board = env.BoardConfig()
10+
board_keys = board.get("build").keys()
11+
if 'encrypt' in board_keys:
12+
env.AddPostAction(
13+
join("$BUILD_DIR", "${PROGNAME}.bin"),
14+
env.VerboseAction(" ".join([
15+
"$OBJCOPY", "-O", "srec",
16+
"\"$BUILD_DIR/${PROGNAME}.elf\"", "\"" + join("$BUILD_DIR", board.get("build.encrypt")) + "\""
17+
]), "Building $TARGET")
18+
)
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
#
22
# stm32_bootloader.py
33
#
4-
import os,sys,shutil,marlin
4+
import os,sys,marlin
55
Import("env")
66

77
from SCons.Script import DefaultEnvironment
88
board = DefaultEnvironment().BoardConfig()
99

10-
#
11-
# Copy the firmware.bin file to build.firmware, no encryption
12-
#
13-
def noencrypt(source, target, env):
14-
firmware = os.path.join(target[0].dir.path, board.get("build.firmware"))
15-
shutil.copy(target[0].path, firmware)
10+
board_keys = board.get("build").keys()
1611

1712
#
1813
# For build.offset define LD_FLASH_OFFSET, used by ldscript.ld
1914
#
20-
if 'offset' in board.get("build").keys():
15+
if 'offset' in board_keys:
2116
LD_FLASH_OFFSET = board.get("build.offset")
2217
marlin.relocate_vtab(LD_FLASH_OFFSET)
2318

@@ -35,9 +30,13 @@ def noencrypt(source, target, env):
3530
env["LINKFLAGS"][i] = "-Wl,--defsym=LD_MAX_DATA_SIZE=" + str(maximum_ram_size - 40)
3631

3732
#
38-
# Only copy the file if there's no encrypt
33+
# For build.rename simply rename the firmware file.
3934
#
40-
board_keys = board.get("build").keys()
41-
if 'firmware' in board_keys and ('encrypt' not in board_keys or board.get("build.encrypt") == 'No'):
42-
import marlin
43-
marlin.add_post_action(noencrypt)
35+
if 'rename' in board_keys:
36+
37+
def rename_target(source, target, env):
38+
firmware = os.path.join(target[0].dir.path, board.get("build.rename"))
39+
import shutil
40+
shutil.copy(target[0].path, firmware)
41+
42+
marlin.add_post_action(rename_target)

ini/stm32f1.ini

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ platform = ${common_stm32.platform}
8484
extends = common_STM32F103RC
8585
build_flags = ${common_stm32.build_flags} -DDEBUG_LEVEL=0 -DTIMER_SERVO=TIM5
8686
board_build.offset = 0x7000
87-
board_build.encrypt = No
88-
board_build.firmware = firmware.bin
8987
board_upload.offset_address = 0x08007000
9088

9189
[env:STM32F103RC_btt_USB]
@@ -113,8 +111,7 @@ board_build.core = stm32
113111
board_build.variant = MARLIN_F103Zx
114112
board_build.ldscript = ldscript.ld
115113
board_build.offset = 0x7000
116-
board_build.encrypt = Yes
117-
board_build.firmware = Robin.bin
114+
board_build.encrypt = Robin.bin
118115
build_flags = ${common_stm32.build_flags}
119116
-DENABLE_HWSERIAL3 -DTIMER_SERIAL=TIM5
120117
build_unflags = ${common_stm32.build_unflags}
@@ -136,8 +133,7 @@ build_flags = ${common_stm32.build_flags}
136133
build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC
137134
monitor_speed = 115200
138135
board_build.offset = 0x5000
139-
board_build.encrypt = Yes
140-
board_build.firmware = Robin_e3.bin
136+
board_build.encrypt = Robin_e3.bin
141137
board_upload.offset_address = 0x08005000
142138
debug_tool = stlink
143139
extra_scripts = ${env:STM32F103RC.extra_scripts}
@@ -215,8 +211,7 @@ board_build.core = stm32
215211
board_build.variant = MARLIN_F103Vx
216212
board_build.ldscript = ldscript.ld
217213
board_build.offset = 0x7000
218-
board_build.firmware = Robin_mini.bin
219-
board_build.encrypt = Yes
214+
board_build.encrypt = Robin_mini.bin
220215
board_upload.offset_address = 0x08007000
221216
build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC
222217
extra_scripts = ${common_stm32.extra_scripts}
@@ -236,8 +231,7 @@ board_build.core = stm32
236231
board_build.variant = MARLIN_F103Vx
237232
board_build.ldscript = ldscript.ld
238233
board_build.offset = 0x7000
239-
board_build.encrypt = Yes
240-
board_build.firmware = Robin_nano35.bin
234+
board_build.encrypt = Robin_nano35.bin
241235
board_upload.offset_address = 0x08007000
242236
build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC
243237
debug_tool = jlink

ini/stm32f4.ini

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,8 @@ board = marlin_STM32F407VGT6_CCM
113113
board_build.core = stm32
114114
board_build.variant = MARLIN_F4x7Vx
115115
board_build.ldscript = ldscript.ld
116-
board_build.firmware = firmware.srec
116+
board_build.encrypt = firmware.srec
117117
# Just openblt.py (not stm32_bootloader.py) generates the file
118-
board_build.encrypt = Yes
119118
board_build.offset = 0x10000
120119
board_upload.offset_address = 0x08010000
121120
build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC -DUSBD_VID=0x0483
@@ -265,7 +264,7 @@ extends = common_stm32
265264
board = marlin_STM32F407ZGT6
266265
board_build.variant = MARLIN_LERDGE
267266
board_build.offset = 0x10000
268-
board_build.encrypt = Yes
267+
board_build.encrypt = firmware.bin
269268
extra_scripts = ${common.extra_scripts}
270269
pre:buildroot/share/PlatformIO/scripts/generic_create_variant.py
271270
buildroot/share/PlatformIO/scripts/stm32_bootloader.py
@@ -280,9 +279,9 @@ build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC -DUS
280279
# Lerdge X
281280
#
282281
[env:LERDGEX]
283-
platform = ${lerdge_common.platform}
284-
extends = lerdge_common
285-
board_build.firmware = Lerdge_X_firmware_force.bin
282+
platform = ${lerdge_common.platform}
283+
extends = lerdge_common
284+
board_build.encrypt = Lerdge_X_firmware_force.bin
286285

287286
#
288287
# Lerdge X with USB Flash Drive Support
@@ -297,9 +296,9 @@ build_flags = ${stm_flash_drive.build_flags} ${lerdge_common.build_flags}
297296
# Lerdge S
298297
#
299298
[env:LERDGES]
300-
platform = ${lerdge_common.platform}
301-
extends = lerdge_common
302-
board_build.firmware = Lerdge_firmware_force.bin
299+
platform = ${lerdge_common.platform}
300+
extends = lerdge_common
301+
board_build.encrypt = Lerdge_firmware_force.bin
303302

304303
#
305304
# Lerdge S with USB Flash Drive Support
@@ -314,10 +313,10 @@ build_flags = ${stm_flash_drive.build_flags} ${lerdge_common.build_flags}
314313
# Lerdge K
315314
#
316315
[env:LERDGEK]
317-
platform = ${lerdge_common.platform}
318-
extends = lerdge_common
319-
board_build.firmware = Lerdge_K_firmware_force.bin
320-
build_flags = ${lerdge_common.build_flags} -DLERDGEK
316+
platform = ${lerdge_common.platform}
317+
extends = lerdge_common
318+
board_build.encrypt = Lerdge_K_firmware_force.bin
319+
build_flags = ${lerdge_common.build_flags} -DLERDGEK
321320

322321
#
323322
# Lerdge K with USB Flash Drive Support
@@ -347,8 +346,6 @@ board_build.core = stm32
347346
board_build.variant = MARLIN_F446VE
348347
board_build.ldscript = ldscript.ld
349348
board_build.offset = 0x0000
350-
board_build.encrypt = No
351-
board_build.firmware = firmware.bin
352349
extra_scripts = ${common.extra_scripts}
353350
pre:buildroot/share/PlatformIO/scripts/generic_create_variant.py
354351
buildroot/share/PlatformIO/scripts/stm32_bootloader.py
@@ -365,7 +362,6 @@ board = genericSTM32F407VET6
365362
board_build.core = stm32
366363
board_build.variant = MARLIN_F4x7Vx
367364
board_build.ldscript = ldscript.ld
368-
board_build.firmware = firmware.bin
369365
board_build.offset = 0x0000
370366
board_upload.offset_address = 0x08000000
371367
build_unflags = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC
@@ -392,7 +388,7 @@ board = marlin_STM32F407VGT6_CCM
392388
board_build.core = stm32
393389
board_build.variant = MARLIN_F4x7Vx
394390
board_build.ldscript = ldscript.ld
395-
board_build.firmware = Robin_nano_v3.bin
391+
board_build.rename = Robin_nano_v3.bin
396392
board_build.offset = 0xC000
397393
board_upload.offset_address = 0x0800C000
398394
build_unflags = ${common_stm32.build_unflags}

0 commit comments

Comments
 (0)