Skip to content

Commit b4904cc

Browse files
committed
🔨 Delete after encrypt. Lerdge encrypt only once
1 parent 2c6fe45 commit b4904cc

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

buildroot/share/PlatformIO/scripts/lerdge.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,36 @@
1212
board = DefaultEnvironment().BoardConfig()
1313

1414
def encryptByte(byte):
15-
byte = 0xFF & ((byte << 6) | (byte >> 2))
16-
i = 0x58 + byte
17-
j = 0x05 + byte + (i >> 8)
18-
byte = (0xF8 & i) | (0x07 & j)
19-
return byte
15+
byte = 0xFF & ((byte << 6) | (byte >> 2))
16+
i = 0x58 + byte
17+
j = 0x05 + byte + (i >> 8)
18+
byte = (0xF8 & i) | (0x07 & j)
19+
return byte
2020

2121
def encrypt_file(input, output_file, file_length):
22-
input_file = bytearray(input.read())
23-
for i in range(len(input_file)):
24-
result = encryptByte(input_file[i])
25-
input_file[i] = result
26-
27-
output_file.write(input_file)
28-
return
22+
input_file = bytearray(input.read())
23+
for i in range(len(input_file)):
24+
input_file[i] = encryptByte(input_file[i])
25+
output_file.write(input_file)
2926

3027
# Encrypt ${PROGNAME}.bin and save it with the name given in build.encrypt
3128
def encrypt(source, target, env):
32-
fwname = board.get("build.encrypt")
33-
print("Encrypting %s to %s" % (target[0].path, fwname))
34-
firmware = open(target[0].path, "rb")
35-
renamed = open(target[0].dir.path + "/" + fwname, "wb")
36-
length = os.path.getsize(target[0].path)
29+
fwpath = target[0].path
30+
enname = board.get("build.encrypt")
31+
print("Encrypting %s to %s" % (fwpath, enname))
32+
fwfile = open(fwpath, "rb")
33+
enfile = open(target[0].dir.path + "/" + enname, "wb")
34+
length = os.path.getsize(fwpath)
3735

38-
encrypt_file(firmware, renamed, length)
36+
encrypt_file(fwfile, enfile, length)
3937

40-
firmware.close()
41-
renamed.close()
38+
fwfile.close()
39+
enfile.close()
40+
os.remove(fwpath)
4241

4342
if 'encrypt' in board.get("build").keys():
44-
if board.get("build.encrypt") != "":
45-
marlin.add_post_action(encrypt)
43+
if board.get("build.encrypt") != "":
44+
marlin.add_post_action(encrypt)
4645
else:
47-
print("LERDGE builds require output file via board_build.encrypt = 'filename' parameter")
48-
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: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,24 @@ def encrypt_mks(source, target, env, new_name):
4848

4949
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]
5050

51-
firmware = open(target[0].path, "rb")
52-
renamed = open(target[0].dir.path + "/" + new_name, "wb")
53-
length = os.path.getsize(target[0].path)
51+
fwpath = target[0].path
52+
fwfile = open(fwpath, "rb")
53+
enfile = open(target[0].dir.path + "/" + new_name, "wb")
54+
length = os.path.getsize(fwpath)
5455
position = 0
5556
try:
5657
while position < length:
57-
byte = firmware.read(1)
58+
byte = fwfile.read(1)
5859
if position >= 320 and position < 31040:
5960
byte = chr(ord(byte) ^ key[position & 31])
6061
if sys.version_info[0] > 2:
6162
byte = bytes(byte, 'latin1')
62-
renamed.write(byte)
63+
enfile.write(byte)
6364
position += 1
6465
finally:
65-
firmware.close()
66-
renamed.close()
66+
fwfile.close()
67+
enfile.close()
68+
os.remove(fwpath)
6769

6870
def add_post_action(action):
6971
env.AddPostAction(join("$BUILD_DIR", "${PROGNAME}.bin"), action);

ini/stm32f4.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,13 @@ platform = ${common_stm32.platform}
263263
extends = stm32_variant
264264
board = marlin_STM32F407ZGT6
265265
board_build.variant = MARLIN_LERDGE
266-
board_build.encrypt = firmware.bin
267266
board_build.offset = 0x10000
268267
build_flags = ${stm32_variant.build_flags}
269268
-DSTM32F4 -DSTM32F4xx -DTARGET_STM32F4
270269
-DDISABLE_GENERIC_SERIALUSB -DARDUINO_ARCH_STM32 -DLERDGE_TFT35
271270
build_unflags = ${stm32_variant.build_unflags} -DUSBCON -DUSBD_USE_CDC -DUSBD_VID=0x0483
272-
extra_scripts = ${stm32_variant.extra_scripts}
271+
extra_scripts = ${common_stm32.extra_scripts}
272+
pre:buildroot/share/PlatformIO/scripts/generic_create_variant.py
273273
buildroot/share/PlatformIO/scripts/lerdge.py
274274

275275
#

0 commit comments

Comments
 (0)