From 84b3a071af706785fa07c34c1c3da8f9df1224ce Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 19 Oct 2024 09:02:18 +0900 Subject: [PATCH] support bootloader update with CAN bootloader --- Makefile | 7 +-- bl_update/ldscript_bl_CAN.ld | 2 + bl_update/main.c | 32 ++++++++++++- bl_update/set_app_signature.py | 85 ++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 4 deletions(-) create mode 100755 bl_update/set_app_signature.py diff --git a/Makefile b/Makefile index 964087a6..038f6c10 100644 --- a/Makefile +++ b/Makefile @@ -174,7 +174,7 @@ $(ELF_FILE): $$(SRC_$(MCU)_BL) $$(SRC_BL) $$(SRC_DRONECAN) $(H_FILE): $(BIN_FILE) $$(QUIET)python3 bl_update/make_binheader.py $(BIN_FILE) $(H_FILE) -$(BLU_ELF_FILE): CFLAGS_BLU := $$(MCU_$(MCU)) $$(CFLAGS_$(MCU)) $$(CFLAGS_BASE) -DBOOTLOADER -DUSE_$(PIN) $(EXTRA_CFLAGS) -Wno-unused-variable -Wno-unused-function +$(BLU_ELF_FILE): CFLAGS_BLU := -DAM32_MCU=\"$(MCU)\" $$(MCU_$(MCU)) $$(CFLAGS_$(MCU)) $$(CFLAGS_BASE) -DBOOTLOADER -DUSE_$(PIN) $(EXTRA_CFLAGS) -Wno-unused-variable -Wno-unused-function $(BLU_ELF_FILE): LDFLAGS_BLU := $$(LDFLAGS_COMMON) $$(LDFLAGS_$(MCU)) -T$(xBLU_LDSCRIPT) $(BLU_ELF_FILE): $$(SRC_$(MCU)_BL) $$(SRC_BLU) $(H_FILE) $$(QUIET)echo building bootloader updater for $(BUILD) with pin $(PIN) @@ -191,12 +191,13 @@ $(HEX_FILE): $$(ELF_FILE) $(BIN_FILE): $$(HEX_FILE) # Generate bin and hex files for bl updater -$(BLU_HEX_FILE): $$(BLU_ELF_FILE) +$(BLU_BIN_FILE): $$(BLU_ELF_FILE) $$(QUIET)echo Generating $(notdir $$@) $$(QUIET)$(xOBJCOPY) -O binary $$(<) $$(@:.hex=.bin) + $$(QUIET)python3 bl_update/set_app_signature.py $$@ $$(<) $$(QUIET)$(xOBJCOPY) $$(<) -O ihex $$(@:.bin=.hex) -$(BLU_AMJ_FILE): $$(BLU_HEX_FILE) +$(BLU_AMJ_FILE): $$(BLU_BIN_FILE) $$(QUIET)echo Generating $(notdir $$@) $$(QUIET)python3 bl_update/make_amj.py --type bl_update --githash $(shell git rev-parse HEAD) $(BLU_HEX_FILE) $(BLU_AMJ_FILE) diff --git a/bl_update/ldscript_bl_CAN.ld b/bl_update/ldscript_bl_CAN.ld index fd66569c..84c216fc 100644 --- a/bl_update/ldscript_bl_CAN.ld +++ b/bl_update/ldscript_bl_CAN.ld @@ -25,6 +25,8 @@ SECTIONS . = ALIGN(4); KEEP(*(.isr_vector)) /* Startup code */ . = ALIGN(4); + KEEP(*(.app_signature)) + . = ALIGN(4); } >FLASH /* The program code and other data goes into FLASH */ diff --git a/bl_update/main.c b/bl_update/main.c index d34889ad..c4e398bd 100644 --- a/bl_update/main.c +++ b/bl_update/main.c @@ -20,10 +20,40 @@ // dummy pin and port so we can re-use blutil.h static GPIO_PORT_TYPE input_port; static uint32_t input_pin; -#define FIRMWARE_RELATIVE_START 0x1000 #define MCU_FLASH_START 0x08000000 + +#if !DRONECAN_SUPPORT +#define FIRMWARE_RELATIVE_START 0x1000 +#else + +#define FIRMWARE_RELATIVE_START 0x4000 + +#define APP_SIGNATURE_MAGIC1 0x68f058e6 +#define APP_SIGNATURE_MAGIC2 0xafcee5a0 + +/* + application signature, filled in by set_app_signature.py + */ +const struct { + uint32_t magic1; + uint32_t magic2; + uint32_t fwlen; // total fw length in bytes + uint32_t crc1; // crc32 up to start of app_signature + uint32_t crc2; // crc32 from end of app_signature to end of fw + char mcu[16]; + uint32_t unused[2]; +} app_signature __attribute__((section(".app_signature"))) = { + .magic1 = APP_SIGNATURE_MAGIC1, + .magic2 = APP_SIGNATURE_MAGIC2, + .fwlen = 0, + .crc1 = 0, + .crc2 = 0, + .mcu = AM32_MCU, +}; +#endif + /* use stringize to construct an include of the right bootloader header */ diff --git a/bl_update/set_app_signature.py b/bl_update/set_app_signature.py new file mode 100755 index 00000000..aa52d2f5 --- /dev/null +++ b/bl_update/set_app_signature.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 +''' +set application signature in bin and elf files +''' + +import struct +import sys + +APP_SIGNATURE_MAGIC1 = 0x68f058e6 +APP_SIGNATURE_MAGIC2 = 0xafcee5a0 + +def crc32(buf): + '''crc32 implementation''' + crc = 0 + size = len(buf) + for i in range(size): + byte = buf[i] + crc ^= byte + for _ in range(8): + mask = -(crc & 1) + crc >>= 1 + crc ^= (0xEDB88320 & mask) + return crc + +# Example usage: +# buf = bytearray([0x31, 0x32, 0x33, 0x34]) # Sample input +# result = crc32(buf, len(buf)) +# print(f"CRC32: {hex(result)}") + +def set_app_descriptor(bin_file, elf_file): + '''setup app descriptor in bin file and elf file''' + descriptor = struct.pack("