[feat] add crc32 for boot2_pre
This commit is contained in:
parent
e25e3b95a3
commit
f40cd252ef
@ -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
|
||||
|
||||
@ -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!");
|
||||
@ -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
|
||||
|
||||
@ -1,3 +1,8 @@
|
||||
void __attribute__((section(".text.boot2_pre"))) boot2_copy_self(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
return 0;
|
||||
|
||||
@ -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
|
||||
|
||||
55
tools/boot2_pre_crc.py
Normal file
55
tools/boot2_pre_crc.py
Normal file
@ -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('<I', crc))
|
||||
|
||||
print(f"Success: Updated {filename} with CRC32 0x{crc:08X} at offset 252")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error processing {filename}: {str(e)}")
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Calculate MPEG-2 CRC32 for first 252 bytes and write to last 4 bytes of 256-byte file')
|
||||
parser.add_argument('filename', help='Binary file to process (must be 256 bytes)')
|
||||
args = parser.parse_args()
|
||||
|
||||
if not process_file(args.filename):
|
||||
exit(1)
|
||||
Loading…
Reference in New Issue
Block a user