Skip to content

Commit

Permalink
Helper module for build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Apr 25, 2021
1 parent cfb973a commit 887e30a
Show file tree
Hide file tree
Showing 23 changed files with 219 additions and 544 deletions.
8 changes: 2 additions & 6 deletions buildroot/share/PlatformIO/scripts/STM32F103RC_MEEB_3DP.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,5 @@ def after_upload(source, target, env):
print('Use the {0:s} address as the marlin app entry point.'.format(vect_tab_addr))
print('Use the {0:d}KB flash version of stm32f103rct6 chip.'.format(flash_size))

custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/STM32F103RC_MEEB_3DP.ld")
for i, flag in enumerate(env["LINKFLAGS"]):
if "-Wl,-T" in flag:
env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script
elif flag == "-T":
env["LINKFLAGS"][i + 1] = custom_ld_script
import marlin
marlin.custom_ld_script("STM32F103RC_MEEB_3DP.ld")
22 changes: 8 additions & 14 deletions buildroot/share/PlatformIO/scripts/STM32F103RC_SKR_MINI.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
#
# STM32F103RC_SKR_MINI.py
# buildroot/share/PlatformIO/scripts/STM32F103RC_SKR_MINI.py
# Customizations for env:STM32F103RC_btt
#
import os
import marlin
Import("env")

STM32_FLASH_SIZE = 256
# Relocate firmware from 0x08000000 to 0x08007000
marlin.relocate_firmware("0x08007000")

# Use STM32_FLASH_SIZE to choose a linker script
STM32_FLASH_SIZE = 256
for define in env['CPPDEFINES']:
if define[0] == "VECT_TAB_ADDR":
env['CPPDEFINES'].remove(define)
if define[0] == "STM32_FLASH_SIZE":
STM32_FLASH_SIZE = define[1]

# Relocate firmware from 0x08000000 to 0x08007000
env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08007000"))

custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/STM32F103RC_SKR_MINI_" + str(STM32_FLASH_SIZE) + "K.ld")
for i, flag in enumerate(env["LINKFLAGS"]):
if "-Wl,-T" in flag:
env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script
elif flag == "-T":
env["LINKFLAGS"][i + 1] = custom_ld_script
marlin.custom_ld_script("STM32F103RC_SKR_MINI_" + str(STM32_FLASH_SIZE) + "K.ld")
15 changes: 1 addition & 14 deletions buildroot/share/PlatformIO/scripts/STM32F103RC_fysetc.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
#
# STM32F103RC_fysetc.py
# buildroot/share/PlatformIO/scripts/STM32F103RC_fysetc.py
#
import os
from os.path import join
from os.path import expandvars
Import("env")

# Relocate firmware from 0x08000000 to 0x08008000
#for define in env['CPPDEFINES']:
# if define[0] == "VECT_TAB_ADDR":
# env['CPPDEFINES'].remove(define)
#env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08008000"))

#custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/fysetc_stm32f103rc.ld")
#for i, flag in enumerate(env["LINKFLAGS"]):
# if "-Wl,-T" in flag:
# env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script
# elif flag == "-T":
# env["LINKFLAGS"][i + 1] = custom_ld_script

# Custom HEX from ELF
env.AddPostAction(
join("$BUILD_DIR","${PROGNAME}.elf"),
Expand Down
19 changes: 4 additions & 15 deletions buildroot/share/PlatformIO/scripts/STM32F103RET6_creality.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
#
# STM32F103RET6_creality.py
# buildroot/share/PlatformIO/scripts/STM32F103RET6_creality.py
#
import os
Import("env")
import marlin

# Relocate firmware from 0x08000000 to 0x08007000
for define in env['CPPDEFINES']:
if define[0] == "VECT_TAB_ADDR":
env['CPPDEFINES'].remove(define)
marlin.relocate_firmware("0x08007000")

env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08007000"))

custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/creality.ld")

for i, flag in enumerate(env['LINKFLAGS']):
if "-Wl,-T" in flag:
env['LINKFLAGS'][i] = "-Wl,-T" + custom_ld_script
elif flag == "-T":
env['LINKFLAGS'][i + 1] = custom_ld_script
marlin.custom_ld_script("creality.ld")
18 changes: 5 additions & 13 deletions buildroot/share/PlatformIO/scripts/STM32F103RE_SKR_E3_DIP.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
#
# STM32F103RE_SKR_E3_DIP.py
# buildroot/share/PlatformIO/scripts/STM32F103RE_SKR_E3_DIP.py
# Customizations for env:STM32F103RE_btt
#
import os
import marlin
Import("env")

for define in env['CPPDEFINES']:
if define[0] == "VECT_TAB_ADDR":
env['CPPDEFINES'].remove(define)

# Relocate firmware from 0x08000000 to 0x08007000
env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08007000"))
marlin.relocate_firmware("0x08007000")

custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/STM32F103RE_SKR_E3_DIP.ld")
for i, flag in enumerate(env["LINKFLAGS"]):
if "-Wl,-T" in flag:
env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script
elif flag == "-T":
env["LINKFLAGS"][i + 1] = custom_ld_script
marlin.custom_ld_script("STM32F103RE_SKR_E3_DIP.ld")
24 changes: 8 additions & 16 deletions buildroot/share/PlatformIO/scripts/STM32F103VE_longer.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
#
# STM32F103VE_longer.py
# buildroot/share/PlatformIO/scripts/STM32F103VE_longer.py
# Customizations for env:STM32F103VE_longer
#
import os
Import("env")
import os,marlin

# Relocate firmware from 0x08000000 to 0x08010000
for define in env['CPPDEFINES']:
if define[0] == "VECT_TAB_ADDR":
env['CPPDEFINES'].remove(define)
env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08010000"))
marlin.relocate_firmware("0x08010000")

custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/STM32F103VE_longer.ld")
for i, flag in enumerate(env["LINKFLAGS"]):
if "-Wl,-T" in flag:
env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script
elif flag == "-T":
env["LINKFLAGS"][i + 1] = custom_ld_script
marlin.custom_ld_script("STM32F103VE_longer.ld")

# Rename ${PROGNAME}.bin and save it as 'project.bin' (No encryption on the Longer3D)
def encrypt(source, target, env):
firmware = open(target[0].path, "rb")
marlin_alfa = open(target[0].dir.path +'/project.bin', "wb")
renamed = open(target[0].dir.path + '/project.bin', "wb")
length = os.path.getsize(target[0].path)
position = 0
try:
while position < length:
byte = firmware.read(1)
marlin_alfa.write(byte)
renamed.write(byte)
position += 1
finally:
firmware.close()
marlin_alfa.close()
renamed.close()

env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt);
marlin.add_post_action(encrypt);
24 changes: 7 additions & 17 deletions buildroot/share/PlatformIO/scripts/chitu_crypt.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
#
# chitu_crypt.py
# buildroot/share/PlatformIO/scripts/chitu_crypt.py
# Customizations for Chitu boards
#
Import("env")
import os,random,struct,uuid
import os,random,struct,uuid,marlin

# Relocate firmware from 0x08000000 to 0x08008800
env['CPPDEFINES'].remove(("VECT_TAB_ADDR", "0x8000000"))
env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08008800"))

custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/chitu_f103.ld")
for i, flag in enumerate(env["LINKFLAGS"]):
if "-Wl,-T" in flag:
env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script
elif flag == "-T":
env["LINKFLAGS"][i + 1] = custom_ld_script
marlin.relocate_firmware("0x08008800")

marlin.custom_ld_script("chitu_f103.ld")

def calculate_crc(contents, seed):
accumulating_xor_value = seed;
Expand Down Expand Up @@ -43,7 +35,7 @@ def xor_block(r0, r1, block_number, block_size, file_key):

for loop_counter in range(0, block_size):
# meant to make sure different bits of the key are used.
xor_seed = int(loop_counter/key_length)
xor_seed = int(loop_counter / key_length)

# IP is a scratch register / R12
ip = loop_counter - (key_length * xor_seed)
Expand All @@ -69,7 +61,6 @@ def xor_block(r0, r1, block_number, block_size, file_key):
#increment the loop_counter
loop_counter = loop_counter + 1


def encrypt_file(input, output_file, file_length):
input_file = bytearray(input.read())
block_size = 0x800
Expand Down Expand Up @@ -113,16 +104,15 @@ def encrypt_file(input, output_file, file_length):
output_file.write(input_file)
return


# Encrypt ${PROGNAME}.bin and save it as 'update.cbd'
def encrypt(source, target, env):
firmware = open(target[0].path, "rb")
update = open(target[0].dir.path +'/update.cbd', "wb")
update = open(target[0].dir.path + '/update.cbd', "wb")
length = os.path.getsize(target[0].path)

encrypt_file(firmware, update, length)

firmware.close()
update.close()

env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt);
marlin.add_post_action(encrypt);
3 changes: 2 additions & 1 deletion buildroot/share/PlatformIO/scripts/download_mks_assets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#
# download_mks_assets.py
# buildroot/share/PlatformIO/scripts/download_mks_assets.py
# Added by HAS_TFT_LVGL_UI to download assets from Makerbase repo
#
Import("env")
import os,requests,zipfile,tempfile,shutil
Expand Down
18 changes: 4 additions & 14 deletions buildroot/share/PlatformIO/scripts/fly_mini.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
#
# fly_mini.py
# buildroot/share/PlatformIO/scripts/fly_mini.py
# Customizations for env:FLY_MINI
#
import os
Import("env")
import marlin

# Relocate firmware from 0x08000000 to 0x08005000
for define in env['CPPDEFINES']:
if define[0] == "VECT_TAB_ADDR":
env['CPPDEFINES'].remove(define)
env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x08005000"))

custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/fly_mini.ld")
for i, flag in enumerate(env["LINKFLAGS"]):
if "-Wl,-T" in flag:
env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script
elif flag == "-T":
env["LINKFLAGS"][i + 1] = custom_ld_script
marlin.relocate_firmware("0x08005000")

marlin.custom_ld_script("fly_mini.ld")
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
import os
#
# buildroot/share/PlatformIO/scripts/jgaurora_a5s_a1_with_bootloader.py
# Customizations for env:jgaurora_a5s_a1
#
import os,marlin
Import("env")

# Relocate firmware from 0x08000000 to 0x0800A000
env['CPPDEFINES'].remove(("VECT_TAB_ADDR", "0x8000000"))
#alternatively, for STSTM <=5.1.0 use line below
#env['CPPDEFINES'].remove(("VECT_TAB_ADDR", 134217728))
env['CPPDEFINES'].append(("VECT_TAB_ADDR", "0x0800A000"))
marlin.relocate_firmware("0x0800A000")

custom_ld_script = os.path.abspath("buildroot/share/PlatformIO/ldscripts/jgaurora_a5s_a1.ld")
for i, flag in enumerate(env["LINKFLAGS"]):
if "-Wl,-T" in flag:
env["LINKFLAGS"][i] = "-Wl,-T" + custom_ld_script
elif flag == "-T":
env["LINKFLAGS"][i + 1] = custom_ld_script
marlin.custom_ld_script("jgaurora_a5s_a1.ld")

#append ${PROGNAME}.bin firmware after bootloader and save it as 'jgaurora_firmware.bin'
def addboot(source,target,env):
# Append ${PROGNAME}.bin firmware after bootloader and save it as 'jgaurora_firmware.bin'
def addboot(source, target, env):
firmware = open(target[0].path, "rb")
lengthfirmware = os.path.getsize(target[0].path)
bootloader_dir = "buildroot/share/PlatformIO/scripts/jgaurora_bootloader.bin"
bootloader = open(bootloader_dir, "rb")
lengthbootloader = os.path.getsize(bootloader_dir)
firmware_with_boothloader_dir = target[0].dir.path +'/firmware_with_bootloader.bin'
if os.path.exists(firmware_with_boothloader_dir):
os.remove(firmware_with_boothloader_dir)
firmwareimage = open(firmware_with_boothloader_dir, "wb")
bootloader_bin = "buildroot/share/PlatformIO/scripts/" + "jgaurora_bootloader.bin"
bootloader = open(bootloader_bin, "rb")
lengthbootloader = os.path.getsize(bootloader_bin)

firmware_with_boothloader_bin = target[0].dir.path + '/firmware_with_bootloader.bin'
if os.path.exists(firmware_with_boothloader_bin):
os.remove(firmware_with_boothloader_bin)
firmwareimage = open(firmware_with_boothloader_bin, "wb")
position = 0
while position < lengthbootloader:
byte = bootloader.read(1)
Expand All @@ -38,11 +35,11 @@ def addboot(source,target,env):
bootloader.close()
firmware.close()
firmwareimage.close()
firmware_without_bootloader_dir = target[0].dir.path+'/firmware_for_sd_upload.bin'
if os.path.exists(firmware_without_bootloader_dir):
os.remove(firmware_without_bootloader_dir)
os.rename(target[0].path, firmware_without_bootloader_dir)
#os.rename(target[0].dir.path+'/firmware_with_bootloader.bin', target[0].dir.path+'/firmware.bin')

env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", addboot);
firmware_without_bootloader_bin = target[0].dir.path + '/firmware_for_sd_upload.bin'
if os.path.exists(firmware_without_bootloader_bin):
os.remove(firmware_without_bootloader_bin)
os.rename(target[0].path, firmware_without_bootloader_bin)
#os.rename(target[0].dir.path+'/firmware_with_bootloader.bin', target[0].dir.path+'/firmware.bin')

marlin.add_post_action(addboot);
17 changes: 10 additions & 7 deletions buildroot/share/PlatformIO/scripts/lerdge.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#
# lerdge.py
# Customizations for Lerdge build environments
# 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
#
import os,sys
import os,marlin
Import("env")

from SCons.Script import DefaultEnvironment
Expand All @@ -28,16 +31,16 @@ def encrypt_file(input, output_file, file_length):
def encrypt(source, target, env):
print("Encrypting to:", board.get("build.firmware"))
firmware = open(target[0].path, "rb")
result = open(target[0].dir.path + "/" + board.get("build.firmware"), "wb")
renamed = open(target[0].dir.path + "/" + board.get("build.firmware"), "wb")
length = os.path.getsize(target[0].path)

encrypt_file(firmware, result, length)
encrypt_file(firmware, renamed, length)

firmware.close()
result.close()
renamed.close()

if 'firmware' in board.get("build").keys():
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", encrypt);
marlin.add_post_action(encrypt);
else:
print("You need to define output file via board_build.firmware = 'filename' parameter")
exit(1);
Loading

0 comments on commit 887e30a

Please sign in to comment.