summaryrefslogtreecommitdiff
path: root/binmaker.py
diff options
context:
space:
mode:
authorAlex Evans <715855+mmalex@users.noreply.github.com>2024-11-07 12:01:15 +0000
committerAlex Evans <715855+mmalex@users.noreply.github.com>2024-11-07 12:01:15 +0000
commitcc7afbbb54d2b66cc3a89f2748c85fc632634d1f (patch)
tree5f5c07c1aa0c6a9cf8dcf3543e68fd4626a8769f /binmaker.py
parent78a4df6ca67f937cda8462d43cbd9547994b6474 (diff)
downloadplinky-cc7afbbb54d2b66cc3a89f2748c85fc632634d1f.tar.gz
work on build tooling to choose a golden bootloader (dubbed, v1.04) and then make a BOOTLOAD.UF2 that actually encompasses latest firmware and the golden bootloader
Diffstat (limited to 'binmaker.py')
-rw-r--r--binmaker.py62
1 files changed, 41 insertions, 21 deletions
diff --git a/binmaker.py b/binmaker.py
index c48e572..11f3c3a 100644
--- a/binmaker.py
+++ b/binmaker.py
@@ -1,10 +1,19 @@
+# this script takes the compiled firmware (sw/Release/plinkyblack.bin) and
+# bundles it with a bootloader (golden_bootloader.bin) into a single binary file
+# with a version number and filename extracted from the firmware.
+# it USED to read the bootloader from (bootloader/Release/plinkybl.bin) but
+# to avoid unnecessary flashing, it now reads the bootloader from the copy.
+# if you want to update the bootloader, you need to copy it over the golden_bootloader.bin
+# and rerun this script with the magic argument 'bootloader'
+
def main():
# bin file maker
ver1 = '0'
ver2 = '0'
ver3 = '0'
+ bootloader_file = 'golden_bootloader.bin' # "bootloader/Release/plinkybl.bin"
try:
- with open("bootloader/Release/plinkybl.bin", "rb") as f1:
+ with open(bootloader_file, "rb") as f1:
bl_content = f1.read()
with open("sw/Release/plinkyblack.bin", "rb") as f2:
app_content = f2.read()
@@ -12,10 +21,16 @@ def main():
print(f"Failed to open file: {e}")
exit(2)
appsize = len(app_content)
- print(f'{appsize} app, {len(bl_content)} bootloader')
+ bootloader_version = "0000"
+ if chr(bl_content[-3])=='.':
+ bootloader_version = chr(bl_content[-4]) + chr(bl_content[-3]) + chr(bl_content[-2]) + chr(bl_content[-1])
+ print(f'SIZES: {appsize} app, {len(bl_content)} bootloader, bootloader version [{bootloader_version}]')
# pad bl_content and app_content to reach their sizes
- bl_content += b'\xff' * (65536 - len(bl_content))
- app_content += b'\xff' * (1024 * 1024 - 65536 - len(app_content))
+ og_bl_size = len(bl_content)
+ bl_content += b'\xff' * (65536 - len(bl_content) - 4)
+ bl_content += bootloader_version.encode('ascii')
+ assert(len(bl_content) == 65536)
+ app_content += b'\xff' * (1024 * 1024 - 65536 - 2048 - len(app_content)) # leave the last sector empty for the calibration
app = bl_content + app_content
@@ -45,28 +60,33 @@ def main():
uf2args.convert=True
uf2args.family='STM32L4'
uf2args.input='sw/Release/plinkyblack.hex'
- uf2args.base='0x2000'
+ uf2args.base='0x2000' # lol this is wrong, but luckily we were using .hex which has addresses. phew.
+ # the actual base address is 0x08010000 ie 64k into the flash.
import uf2conv
uf2conv.uf2conv(uf2args)
- # read 'bootloader/Release/plinkybl.bin', pad to 64k, and write it back
- try:
- with open("bootloader/Release/plinkybl.bin", "rb") as f1:
- bl_content = f1.read()
- if len(bl_content) < 65536:
- bl_content += b'\xff' * (65536 - len(bl_content))
- with open("bootloader/Release/plinkybl.bin", "wb") as f1:
- f1.write(bl_content)
- print('padded bootloader to 64k')
- except IOError as e:
- print(f"Failed to open file: {e}")
- exit(2)
- uf2args.input='bootloader/Release/plinkybl.bin'
+ with open("tempbl.bin", "wb") as f1:
+ f1.write(bl_content)
+ uf2args.input='tempbl.bin'
uf2args.base='0x20008000'
- uf2args.output=f"boot{ver1}{ver2}{ver3}.uf2"
+ uf2args.output=f"tempbl.uf2"
uf2conv.uf2conv(uf2args)
-
-
+ # concatenate the two uf2 files tempbl.uf2 and fname
+ with open(f"BOOTLOAD.UF2", "wb") as f1:
+ with open("tempbl.uf2", "rb") as f2:
+ f1.write(f2.read())
+ with open(fname, "rb") as f2:
+ f1.write(f2.read())
+ # delete temp files
+ import os
+ os.remove("tempbl.bin")
+ os.remove("tempbl.uf2")
+ print("wrote a combiled firmware and bootloader package to BOOTLOAD.UF2")
+ checksum=0
+ # interpret bl_content as a list of 32-bit words
+ for i in range(0, len(bl_content), 4):
+ checksum = (checksum * 23 + int.from_bytes(bl_content[i:i+4], 'little')) & 0xffffffff
+ print(f"bootloader checksum: 0x{checksum:08x} - this should be copied to GOLDEN_CHECKSUM in config.h")
if __name__ == "__main__":
main()