2021-02-28 04:38:57 +00:00
|
|
|
#
|
2021-11-04 10:28:42 +00:00
|
|
|
# lerdge.py
|
2021-04-27 09:49:21 +00:00
|
|
|
# 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-28 04:38:57 +00:00
|
|
|
#
|
2021-11-04 10:28:42 +00:00
|
|
|
import pioutil
|
|
|
|
if pioutil.is_pio_build():
|
|
|
|
import os,marlin
|
2020-07-06 22:08:52 +00:00
|
|
|
|
2022-08-01 06:14:58 +00:00
|
|
|
board = marlin.env.BoardConfig()
|
2020-07-06 22:08:52 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
def encryptByte(byte):
|
|
|
|
byte = 0xFF & ((byte << 6) | (byte >> 2))
|
|
|
|
i = 0x58 + byte
|
|
|
|
j = 0x05 + byte + (i >> 8)
|
|
|
|
byte = (0xF8 & i) | (0x07 & j)
|
|
|
|
return byte
|
2020-07-06 22:08:52 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
def encrypt_file(input, output_file, file_length):
|
|
|
|
input_file = bytearray(input.read())
|
|
|
|
for i in range(len(input_file)):
|
|
|
|
input_file[i] = encryptByte(input_file[i])
|
|
|
|
output_file.write(input_file)
|
2020-07-06 22:08:52 +00:00
|
|
|
|
2022-06-24 04:58:26 +00:00
|
|
|
# Encrypt ${PROGNAME}.bin and save it with the name given in build.crypt_lerdge
|
2021-11-04 10:28:42 +00:00
|
|
|
def encrypt(source, target, env):
|
|
|
|
fwpath = target[0].path
|
2022-06-24 04:58:26 +00:00
|
|
|
enname = board.get("build.crypt_lerdge")
|
2021-11-04 10:28:42 +00:00
|
|
|
print("Encrypting %s to %s" % (fwpath, enname))
|
|
|
|
fwfile = open(fwpath, "rb")
|
|
|
|
enfile = open(target[0].dir.path + "/" + enname, "wb")
|
|
|
|
length = os.path.getsize(fwpath)
|
2020-07-06 22:08:52 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
encrypt_file(fwfile, enfile, length)
|
2020-07-06 22:08:52 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
fwfile.close()
|
|
|
|
enfile.close()
|
|
|
|
os.remove(fwpath)
|
2020-07-06 22:08:52 +00:00
|
|
|
|
2022-06-24 04:58:26 +00:00
|
|
|
if 'crypt_lerdge' in board.get("build").keys():
|
|
|
|
if board.get("build.crypt_lerdge") != "":
|
2021-11-04 10:28:42 +00:00
|
|
|
marlin.add_post_action(encrypt)
|
|
|
|
else:
|
2022-06-24 04:58:26 +00:00
|
|
|
print("LERDGE builds require output file via board_build.crypt_lerdge = 'filename' parameter")
|
2021-11-04 10:28:42 +00:00
|
|
|
exit(1)
|