From 83aef192e447c800cba6f4dbde8a450a4f8581a5 Mon Sep 17 00:00:00 2001 From: Jacques Comeaux Date: Mon, 1 Jul 2024 12:16:02 -0500 Subject: Use in-house CRC32 computation for boot sector --- octedit/.gitignore | 1 + octedit/Makefile | 13 ++++++++----- octedit/crc.s | 6 ------ octedit/crc32.c | 22 ++++++++++++++++++++++ octedit/pico_persist.ld | 5 ++--- 5 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 octedit/.gitignore delete mode 100644 octedit/crc.s create mode 100644 octedit/crc32.c (limited to 'octedit') diff --git a/octedit/.gitignore b/octedit/.gitignore new file mode 100644 index 0000000..2d5bda1 --- /dev/null +++ b/octedit/.gitignore @@ -0,0 +1 @@ +crc32 diff --git a/octedit/Makefile b/octedit/Makefile index 74ce9d4..cc3773d 100644 --- a/octedit/Makefile +++ b/octedit/Makefile @@ -15,20 +15,23 @@ octedit.uf2: octedit.bin printf %220s | tr " " "\0" >> octedit.uf2 # 0 padding echo -ne "\x30\x6F\xB1\x0A" >> octedit.uf2 # Magic end -octedit.bin: octedit.elf +octedit.bin: octedit.elf crc32 arm-none-eabi-objcopy -O binary octedit.elf octedit.bin + ./crc32 < octedit.bin >> octedit.bin -objects = setup.o octedit.o uart.o crc.o +crc32: crc32.c + gcc crc32.c -o crc32 -octedit.elf: $(objects) +objects = setup.o octedit.o uart.o + +octedit.elf: $(objects) pico_persist.ld arm-none-eabi-ld -T pico_persist.ld -o octedit.elf $(objects) - ./checksum_pico_elf.py octedit.elf $(objects): %.o: %.s arm-none-eabi-as -o $@ $< clean: - rm octedit.elf octedit.bin octedit.uf2 *.o + rm octedit.elf octedit.bin octedit.uf2 *.o crc32 flash: octedit.uf2 [ -h /dev/disk/by-label/RPI-RP2 ] || sleep 3s diff --git a/octedit/crc.s b/octedit/crc.s deleted file mode 100644 index 8019c6d..0000000 --- a/octedit/crc.s +++ /dev/null @@ -1,6 +0,0 @@ -.syntax unified -.cpu cortex-m0plus -.thumb - -.section .stage_2_crc -.word 0x00000000 diff --git a/octedit/crc32.c b/octedit/crc32.c new file mode 100644 index 0000000..175ab50 --- /dev/null +++ b/octedit/crc32.c @@ -0,0 +1,22 @@ +#include +#include +#include + +#define DATA_LEN 252 +#define DIVISOR 0x04C11DB7 + +int main() { + uint8_t data[DATA_LEN]; + read(STDIN_FILENO, data, DATA_LEN); + uint32_t crc = 0xFFFFFFFF; + for (size_t i = 0; i < DATA_LEN; i++) { + crc ^= data[i] << 24; + for (uint8_t j = 0; j < 8; j++) { + uint8_t nskip = crc >> 31; + crc <<= 1; + if (nskip) crc ^= DIVISOR; + } + } + write(STDOUT_FILENO, (uint8_t *) &crc, 4); + return 0; +} diff --git a/octedit/pico_persist.ld b/octedit/pico_persist.ld index fd9cf67..14caad7 100644 --- a/octedit/pico_persist.ld +++ b/octedit/pico_persist.ld @@ -1,6 +1,6 @@ MEMORY { FLASH(rx) : ORIGIN = 0x10000000, LENGTH = 2M - SRAM(rx) : ORIGIN = 0x20041f00, LENGTH = 256 + SRAM(rx) : ORIGIN = 0x20041f00, LENGTH = 252 } SECTIONS { @@ -8,7 +8,6 @@ SECTIONS { setup.o(.text); octedit.o(.text); uart.o(.text); - . = 252; - *(.stage_2_crc); + . = LENGTH(SRAM); } >SRAM AT>FLASH } -- cgit v1.2.3