diff --git a/example/boot2/Makefile b/example/boot2/Makefile index d383a42..d1ee13c 100644 --- a/example/boot2/Makefile +++ b/example/boot2/Makefile @@ -15,6 +15,7 @@ endif # add custom cmake definition #cmake_definition+=-Dxxx=sss -LDFLAGS += -T$(EXAMPLE_BASE_DIR)/flash.ld +LDFLAGS += -T$(EXAMPLE_BASE_DIR)/boot2.ld +BOOT2_PRE_CRC ?= boot2_pre_crc.bin include $(SDK_BASE_DIR)/project.build diff --git a/example/boot2/flash.ld b/example/boot2/boot2.ld similarity index 92% rename from example/boot2/flash.ld rename to example/boot2/boot2.ld index dc5ade1..4b0f09e 100644 --- a/example/boot2/flash.ld +++ b/example/boot2/boot2.ld @@ -24,6 +24,7 @@ SECTIONS { .boot2_pre : { + KEEP(*boot2_pre.*(.text.boot2_pre)) KEEP(*(.text.boot2_pre)) _boot2_pre_size = . - ADDR(.boot2_pre); ASSERT(_boot2_pre_size <= 252, "Error: .boot2_pre size exceeds 252 bytes!"); diff --git a/example/boot2/boot2_pre.S b/example/boot2/boot2_pre.S index 1910ca1..3cf46a1 100644 --- a/example/boot2/boot2_pre.S +++ b/example/boot2/boot2_pre.S @@ -7,15 +7,11 @@ .section .text.boot2_pre .type boot2_pre, %function boot2_pre: - b boot2_copy_self + ldr r0, =_stack_top + msr msp, r0 + bl boot2_copy_self + bl main .word 0x11223344 .word 0xabcdef58 - - -boot2_copy_self: - - - bl main - .size .boot2_pre, .-boot2_pre diff --git a/example/boot2/main.c b/example/boot2/main.c index 43df275..eef6389 100644 --- a/example/boot2/main.c +++ b/example/boot2/main.c @@ -1,3 +1,8 @@ +void __attribute__((section(".text.boot2_pre"))) boot2_copy_self(void) +{ + return; +} + int main(void) { return 0; diff --git a/project.build b/project.build index b3fba9b..6a72c46 100644 --- a/project.build +++ b/project.build @@ -53,17 +53,25 @@ cmake_definition+= -DEXAMPLE_NAME=$(EXAMPLE_NAME) FINAL_NAME_PRE := $(EXAMPLE_BASE_DIR)/build/$(EXAMPLE_NAME) +post:build +ifdef BOOT2_PRE_CRC + @$(CROSS_COMPILE)objcopy -O binary --only-section=.boot2_pre $(FINAL_NAME_PRE).elf $(BOOT2_PRE_CRC) + @python3 $(SDK_BASE_DIR)/tools/boot2_pre_crc.py $(BOOT2_PRE_CRC) + @$(CROSS_COMPILE)objcopy --update-section .boot2_pre=$(BOOT2_PRE_CRC) $(FINAL_NAME_PRE).elf + @rm $(BOOT2_PRE_CRC) +endif + @$(CROSS_COMPILE)objcopy -O binary $(FINAL_NAME_PRE).elf $(FINAL_NAME_PRE).bin + @$(CROSS_COMPILE)objdump -d -S $(FINAL_NAME_PRE).elf > $(FINAL_NAME_PRE).asm + @$(CROSS_COMPILE)nm $(FINAL_NAME_PRE).elf > $(FINAL_NAME_PRE).nm + @$(ELF2UF2) $(FINAL_NAME_PRE).elf $(FINAL_NAME_PRE).uf2 + @size $(FINAL_NAME_PRE).elf + @sha256sum $(FINAL_NAME_PRE).bin + build:Makefile $(CMAKE) -S . -B build -G "Unix Makefiles" $(cmake_definition) $(MAKE) -C build -j - $(CROSS_COMPILE)objcopy -O binary $(FINAL_NAME_PRE).elf $(FINAL_NAME_PRE).bin - $(CROSS_COMPILE)objdump -d -S $(FINAL_NAME_PRE).elf > $(FINAL_NAME_PRE).asm - $(CROSS_COMPILE)nm $(FINAL_NAME_PRE).elf > $(FINAL_NAME_PRE).nm - $(ELF2UF2) $(FINAL_NAME_PRE).elf $(FINAL_NAME_PRE).uf2 - size $(FINAL_NAME_PRE).elf - sha256sum $(FINAL_NAME_PRE).bin clean:: $(RM) build -.PHONY:build clean +.PHONY:post build clean diff --git a/tools/boot2_pre_crc.py b/tools/boot2_pre_crc.py new file mode 100644 index 0000000..c3bd4c3 --- /dev/null +++ b/tools/boot2_pre_crc.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import binascii +import struct +import argparse + +def calculate_mpeg2_crc32(data): + """ + Calculate MPEG-2 style CRC32 (Polynomial: 0x04C11DB7, Initial: 0xFFFFFFFF) + Note: Result is NOT inverted (unlike standard CRC32) + """ + crc = 0xFFFFFFFF + for byte in data: + crc ^= byte << 24 + for _ in range(8): + if crc & 0x80000000: + crc = (crc << 1) ^ 0x04C11DB7 + else: + crc = crc << 1 + crc &= 0xFFFFFFFF + return crc + +def process_file(filename): + try: + with open(filename, 'rb+') as f: + # Verify exact 256 bytes size + f.seek(0, 2) + if f.tell() != 256: + raise ValueError(f"File must be exactly 256 bytes (got {f.tell()} bytes)") + + f.seek(0) + data = f.read(252) + if len(data) != 252: + raise ValueError("Couldn't read 252 bytes from file") + + crc = calculate_mpeg2_crc32(data) + f.seek(252) + f.write(struct.pack('