2021-02-27 22:38:57 -06:00
|
|
|
#
|
2021-04-27 04:49:21 -05:00
|
|
|
# buildroot/share/PlatformIO/scripts/lerdge.py
|
|
|
|
# Customizations for Lerdge build environments:
|
|
|
|
# env:LERDGEX env:LERDGEX_usb_flash_drive
|
|
|
|
# env:LERDGES env:LERDGES_usb_flash_drive
|
|
|
|
# env:LERDGEK env:LERDGEK_usb_flash_drive
|
2021-02-27 22:38:57 -06:00
|
|
|
#
|
2021-04-27 04:49:21 -05:00
|
|
|
import os,marlin
|
2020-07-06 17:08:52 -05:00
|
|
|
Import("env")
|
|
|
|
|
|
|
|
from SCons.Script import DefaultEnvironment
|
|
|
|
board = DefaultEnvironment().BoardConfig()
|
|
|
|
|
|
|
|
def encryptByte(byte):
|
|
|
|
byte = 0xFF & ((byte << 6) | (byte >> 2))
|
|
|
|
i = 0x58 + byte
|
|
|
|
j = 0x05 + byte + (i >> 8)
|
|
|
|
byte = (0xF8 & i) | (0x07 & j)
|
|
|
|
return byte
|
|
|
|
|
|
|
|
def encrypt_file(input, output_file, file_length):
|
|
|
|
input_file = bytearray(input.read())
|
|
|
|
for i in range(len(input_file)):
|
|
|
|
result = encryptByte(input_file[i])
|
|
|
|
input_file[i] = result
|
|
|
|
|
|
|
|
output_file.write(input_file)
|
|
|
|
return
|
|
|
|
|
2021-06-13 15:43:33 -05:00
|
|
|
# Encrypt ${PROGNAME}.bin and save it with the name given in build.encrypt
|
2020-07-06 17:08:52 -05:00
|
|
|
def encrypt(source, target, env):
|
2021-06-13 15:43:33 -05:00
|
|
|
fwname = board.get("build.encrypt")
|
|
|
|
print("Encrypting %s to %s" % (target[0].path, fwname))
|
2020-07-06 17:08:52 -05:00
|
|
|
firmware = open(target[0].path, "rb")
|
2021-06-13 15:43:33 -05:00
|
|
|
renamed = open(target[0].dir.path + "/" + fwname, "wb")
|
2020-07-06 17:08:52 -05:00
|
|
|
length = os.path.getsize(target[0].path)
|
|
|
|
|
2021-04-27 04:49:21 -05:00
|
|
|
encrypt_file(firmware, renamed, length)
|
2020-07-06 17:08:52 -05:00
|
|
|
|
|
|
|
firmware.close()
|
2021-04-27 04:49:21 -05:00
|
|
|
renamed.close()
|
2020-07-06 17:08:52 -05:00
|
|
|
|
2021-06-13 15:43:33 -05:00
|
|
|
if 'encrypt' in board.get("build").keys():
|
2021-09-01 18:55:36 -05:00
|
|
|
if board.get("build.encrypt") != "":
|
|
|
|
marlin.add_post_action(encrypt)
|
2020-07-06 17:08:52 -05:00
|
|
|
else:
|
2021-06-13 15:43:33 -05:00
|
|
|
print("LERDGE builds require output file via board_build.encrypt = 'filename' parameter")
|
2021-09-01 18:55:36 -05:00
|
|
|
exit(1)
|