Reproduction · OpenHW on ALINX · Episode 09

SPI Flash bootloader and A/B upgrade: CRC failure falls back without a chip reset

This episode moves CVE2 from "download over JTAG every time" to "boot from flash on power-up". The bootloader occupies the first 16 KiB of a 64 KiB unified RAM; slot A and slot B are 512 KiB each at 0x00C00000 and 0x00C80000 of a GD25Q128E, with one 4 KiB metadata sector per slot. A host sends a 15-byte header plus payload over 115200 UART; the board checks CRC32, writes metadata and jumps to the application. A bad image makes it boot the older slot instead.

Verified on board · 2026-11-21 CVE2 · 50 MHz · RV32IM GD25Q128E · 16 MiB 64 KiB unified RAM 09 / 12
7,192 B
Bootloader binary, placed in the 16 KiB code region
1,021 B
App A / App B; 1,036 B UART frame
2 × 512 KiB
A/B flash slots; 2 × 4 KiB metadata
336 B × 2
Two independent cold-boot logs, byte-identical
0 resets
Forced bad CRC falls back to B without rebooting
0x00c84018
GD25Q128E JEDEC ID, SPI 6.25 MHz
+0.735 / +0.054 ns
WNS / WHS, 0 failing, 11,147 endpoints
4,261 LUT / 16 BRAM
20.49% LUT, 32% BRAM, DRC 0 errors
EP08 was not the cause of the reboot loop. EP08 only compares DDR3 and BRAM bandwidth/latency; it does not erase the on-board flash or change the boot path. Reconfiguring for EP09 overwrote the EP08 logic. The faults were all inside EP09: the first bitstream packaged a stale firmware.hex, instruction and data RAM were two separate simple_ram instances, and the loader returned to main() after a non-P byte, which armed the watchdog and reset about every 500 ms — roughly a one-second loop.

Board result: A → B → bad CRC → two cold boots → restore A

The order proves four separate things: a write works, a switch works, a bad image falls back, and the same fallback happens after power-up without a host tool.

StepMeta seqWrite / resultBoot result
Slot A, good image11,021 B, CRC a8a0cdcdLOAD_PASS → EP09 APP A + 5 heartbeats
Slot B, good image21,021 B, CRC 0a74fc2cLOAD_PASS → EP09 APP B + 5 heartbeats
Force A with bad CRC3advertised 575f3232, actual a8a0cdcdA LOAD_FAIL → FALLBACK → B LOAD_PASS
Cold boot run 1 / run 23336 B each, cmp identicalA FAIL → B PASS, EP09 APP B + 5 heartbeats
Restore A, good image41,021 B, CRC a8a0cdcdLOAD_PASS → EP09 APP A + 5 heartbeats
Final cold boot4268 BA direct LOAD_PASS, no FALLBACK

The seq column is metadata sequence, not the App banner string. App A always prints VER=00000001; A4 metadata version is 4. Use the LOAD SLOT / META_PASS line for the real image version.

Key UART evidence

# Slot A
EP09 FLASH BOOTLOADER
FLASH ID=00c84018 OK
PRESS L FOR LOADER (3s)...
LOADER READY
RECV SLOT=A LEN=1021...
RX_CRC=a8a0cdcd EXPECT=a8a0cdcd
PROGRAM_PASS
META_PASS SEQ=1 ACTIVE=A
LOAD SLOT=A LEN=1021 VER=00000001 CRC=a8a0cdcd LOAD_PASS
EP09 APP A VER=00000001 RUNNING

# Slot B
RECV SLOT=B LEN=1021...
RX_CRC=0a74fc2c EXPECT=0a74fc2c
PROGRAM_PASS
META_PASS SEQ=2 ACTIVE=B
LOAD SLOT=B LEN=1021 VER=00000002 CRC=0a74fc2c LOAD_PASS
EP09 APP B VER=00000002 RUNNING
# Force A: advertised CRC is wrong, payload is still a good image
FRAME slot=0 force=1 len=1021 crc=575f3232 ver=3
RX_CRC=a8a0cdcd EXPECT=575f3232
PROGRAM_PASS
META_PASS SEQ=3 ACTIVE=A
LOAD SLOT=A LEN=1021 VER=00000003 CRC=a8a0cdcd LOAD_FAIL
SWITCH_FAIL
LOAD SLOT=A LEN=1021 VER=00000003 CRC=a8a0cdcd LOAD_FAIL
FALLBACK
LOAD SLOT=B LEN=1021 VER=00000002 CRC=0a74fc2c LOAD_PASS
EP09 APP B VER=00000002 RUNNING

Both independent cold-boot logs are 336 B and byte-identical: pick A (seq 3, bad CRC), LOAD_FAIL, FALLBACK, pick B (seq 2, good CRC), LOAD_PASS, five heartbeats. After restoring A4, the final cold boot is 268 B and starts A directly with no FALLBACK.

Why the first version rebooted every second

The first board showed only a repeating EP09 FLASH BOOTLOADER / FLASH ID=00c84018 OK / PRESS L FO... about once per second. Four independent faults overlapped:

  1. Stale firmware in the bitstream. build_ep09.tcl only read an existing firmware.hex instead of compiling first, so the first bitstream carried an old image and never printed the NO_VALID_META / LOADER READY lines that the current main.c must print.
  2. Instruction RAM and data RAM were separate. The bootloader wrote the App to data RAM at 0x4000; the instruction port could not see it, so the jump still fetched stale code.
  3. The loader returned to main. loader() did a bare return on a non-P byte; main() called reboot(), armed the watchdog and reset after 500 ms.
  4. Vivado inherited its own Python. PYTHON ?= python3 expanded to Vivado's Python directory, so bin2hex.py failed.

The shipped design uses one 64 KiB dpram_64k (Port A instruction-only, Port B data read/write, 16 BRAM tiles), bin2hex.py always emits 16,384 words with NOP fill, build_ep09.tcl forces make clean all and checks the bitstream mtime against the hex, and the loader is an infinite frame loop that goes straight to load_and_jump on a good write; a bad CRC calls boot() on the older slot. The main flow no longer calls reboot().

Two test-tool faults also had to be fixed: a bare python3 cap_serial.py ... & was reaped by the SSH session, and a single 1,021-byte write at 115200 overflowed the board FIFO. The working host sends 32-byte chunks with 3 ms gaps.

Flash and RAM map

RegionAddressSizeUse
Configuration0x00000000..0x00BFFFFF12 MiBFPGA bitstream / factory area, untouched
Slot A0x00C00000..0x00C7FFFF512 KiBApp A
Slot B0x00C80000..0x00CFFFFF512 KiBApp B
META_A0x00D00000..0x00D00FFF4 KiBMetadata sector A
META_B0x00D01000..0x00D01FFF4 KiBMetadata sector B
Bootloader0x0000..0x3FFF16 KiBStack top 0x4000
App0x4000..0xFFFF48 KiBStack top 0x10000

The 512 KiB slot is flash capacity; this App is 1,021 B and the runtime image is capped at 48 KiB by RAM. Large-image erase time is not measured here.

Boot flow and UART frame

PC / UART115200 8N1
15 B header + payload
→
Bootloader3 s window
CRC32 + metadata
→
Slot A / B512 KiB each
GD25Q128E
→
App0x4000..0xFFFF
jump, no reset

Header: 15 bytes, little-endian: 'P' | slot | force | len(u32) | crc32(u32) | version(u32), followed by payload. CRC is standard CRC-32 / zlib (polynomial 0xEDB88320). force=1 writes the payload but keeps the advertised CRC wrong; only for fallback testing.

Metadata record: 32 bytes — magic, seq, slot, len, crc, version, attempts, rec_crc. magic is 0x45503952; rec_crc covers the first 28 bytes. The larger seq wins; if its image fails CRC, boot() tries the other record.

Timing, resources, DRC

0.735 ns
WNS, 11,147 endpoints, 0 failing
0.054 ns
WHS, TNS/THS zero
20.49%
LUT 4,261; FF 3,720 (8.94%)
16 / 50
BRAM 32%; DSP 11 (12.22%)

The final routed report is 08:13:41 and the bitstream is 08:14:13. DRC: 0 errors, 76 warnings, 0 critical — 33 DPIP, 12 DPOP, 21 REQP-1839, 10 other. REQP-1839 is the CVE2 register-file asynchronous reset driving BRAM address pins; the reset is only asserted during configuration, kept as a boundary.

Boundaries

Reproduce

# Vivado 2025.2, xc7a35t-2fgg484
vivado -mode batch -source build_ep09.tcl
vivado -mode batch -source program_ep09.tcl

# firmware
cd sw && make clean all
cd sw/app && make clean all

# host loader: bin slot force version log
python3 host_flash_loader.py sw/app/firmware_a.bin 0 0 1 /tmp/loadA.log
python3 host_flash_loader.py sw/app/firmware_b.bin 1 0 2 /tmp/loadB.log
python3 host_flash_loader.py sw/app/firmware_a.bin 0 1 3 /tmp/loadF.log

# cold boot capture
python3 cap_serial.py 8 /tmp/cold.bin

The bitstream embeds sw/firmware.hex through dpram_64k's INIT_FILE, so bootloader changes need re-synthesis; App A/B go over UART and do not. The programming script must be sourced by absolute path; a relative path in an SSH compound command once failed with couldn't read file.

Related

Evidence files

53 files, about 3.3 MB: bitstream, routed reports, synthesis/implementation/programming logs, raw UART captures, RTL/XDC/Tcl, bootloader and App source/ELF/HEX/disassembly, host tools, pin table and fix record.

README.txtEvidence package README · 7 KB metrics/ep09_metrics.jsonStructured metrics · 5 KB bit/top_cve2_ep09.bitFinal bitstream · 2.2 MB reports/ep09_timing_final.rptRouted timing report · 405 KB reports/ep09_util_final.rptUtilization report · 11 KB reports/ep09_drc_final.rptDRC report · 39 KB logs/build_ep09.logVivado build log · 158 KB logs/impl_ep09.logImplementation log · 50 KB logs/make_ep09.logFirmware make log · 725 B logs/program_final_boot.logFinal programming log · 3 KB logs/program_force_a.logForce programming log · 3 KB logs/program_restore_a.logRestore A programming log · 3 KB logs/program_slot_a.logSlot A programming log · 3 KB logs/program_slot_b.logSlot B programming log · 3 KB logs/synth_ep09.logSynthesis log · 87 KB logs/uart_ep09_boot_probe.txtFirst boot probe · 75 B logs/uart_ep09_cold_fallback_run1.txtCold fallback run 1 · 336 B logs/uart_ep09_cold_fallback_run2.txtCold fallback run 2 · 336 B logs/uart_ep09_final_boot_a.txtFinal A boot · 268 B logs/uart_ep09_force_fallback.txtForce CRC fallback · 598 B logs/uart_ep09_restore_a.txtRestore A UART · 459 B logs/uart_ep09_slot_a.txtSlot A UART · 459 B logs/uart_ep09_slot_b.txtSlot B UART · 459 B rtl/top_cve2_ep09.svTop-level RTL · 49 KB xdc/pi_ar035_f1_1_cve2_ep09.xdcPin constraints · 3 KB tcl/build_ep09.tclBuild Tcl · 3 KB tcl/program_ep09.tclProgram Tcl · 540 B tcl/report_drc_ep09.tclDRC Tcl · 173 B tcl/report_ep09.tclReport Tcl · 322 B sw/bin2hex.pyHEX generator · 584 B sw/firmware.binBootloader binary · 7 KB sw/firmware.disBootloader disassembly · 74 KB sw/firmware.elfBootloader ELF · 13 KB sw/firmware.hex16,384-word HEX · 147 KB sw/link.ldBootloader linker script · 553 B sw/main.cBootloader main · 11 KB sw/MakefileBootloader make · 662 B sw/start.SBootloader startup · 983 B app/app.ldApp linker script · 553 B app/firmware_a.binApp A binary · 1 KB app/firmware_a.disApp A disassembly · 6 KB app/firmware_a.elfApp A ELF · 6 KB app/firmware_b.binApp B binary · 1 KB app/firmware_b.disApp B disassembly · 6 KB app/firmware_b.elfApp B ELF · 6 KB app/main.cApp main · 1 KB app/MakefileApp make · 795 B app/start.SApp startup · 603 B tools/cap_serial.pySerial capture · 814 B tools/host_flash_loader.pyHost loader · 3 KB pins/board_pins.csvBoard pin table · 578 B EP09_修复记录.mdRoot-cause and fix record · 5 KB SHA256SUMS.txtPackage SHA-256 manifest · 5 KB