Build and image
Toolchains
GCC only, three of them:
Variable |
Default |
Builds |
|---|---|---|
|
|
the firmware image (bare metal) |
|
|
the test suite as a static rv64gc Linux binary, run under |
|
|
the test suite natively, with ASan/UBSan; |
|
|
runs the rv64gc test binary |
The code needs a compiler with C23 support (-std=c23: constexpr,
nullptr, [[nodiscard]], typed enums, digit separators).
Makefile targets
Run from src/ (or with make -C src):
Target |
What it does |
Output |
|---|---|---|
|
builds the firmware image |
|
|
builds the test suite for rv64gc and runs it under QEMU |
|
|
builds and runs the same suite natively with
|
|
|
builds one binary per error-injection flag and runs |
|
|
builds |
|
|
runs the mutation-testing script |
|
|
|
|
|
removes |
Variables: BUILD (default ../build, i.e. build/, ignored by git),
TOOLS (default ../tools; the test sources are in $(TOOLS)/test),
BLOB_DEFS (defaults to -DK1_INDEP_EYE, the production data-eye path;
passing an empty BLOB_DEFS= to make overrides that and drops the
independent eye — tools/vehicle/build-vehicle.sh therefore omits the
variable unless EXP= is set),
SPEC_DOCS (directory of the specification in the format check_tables.py
parses; quote it if the path has spaces).
The test sources include "../lib/…". Those includes resolve through the
-Iinclude -Ilib search path of the Makefile, which is why the tests compile
from src/ even though they live in tools/test/.
Compiler flags of the image
-std=c23 -march=rv64gc -mabi=lp64d -mcmodel=medany
-Wall -Wextra -Werror -Wshadow -Wundef -Wstrict-prototypes
-Wmissing-prototypes -Wpointer-arith -Wvla -Wformat=2
-Os -ffreestanding -fno-builtin -fno-pic -fno-pie -mno-relax
-fno-stack-protector -fno-asynchronous-unwind-tables -fno-unwind-tables
-ffunction-sections -fdata-sections -fno-common
Linked with -nostdlib -static -no-pie -T blob/blob.ld --gc-sections
--no-relax --build-id=none and -lgcc. After linking, the Makefile fails
the build if nm -u reports any unresolved symbol, then objcopy -O binary
produces the raw image.
Why these choices:
-mcmodel=medany+-fno-pic: the image runs at a fixed address (0xc082d000) above 2 GiB; medany allows PC-relative addressing there.-mno-relax/--no-relax: nogp-relative relaxation. The image runs on the SPL’s registers, and U-Boot on RISC-V keeps its global-data pointer ingp, so the image must neither use nor change it.-ffunction-sections+--gc-sections: drops unused code (for example functions only the tests call).-fno-stack-protector, no unwind tables: nothing in the image provides the runtime they need.-Werrorplus the strict warnings: the image is small; warnings are bugs.
The test builds use -O2 -g -DK1_DDRT_MOCK with the same warnings.
Linker script (blob/blob.ld)
MEMORY { sram (rwx) : ORIGIN = 0xc082d000, LENGTH = 0xb000 }
.text : KEEP(*(.text.entry)) then *(.text .text.*)
.rodata : *(.rodata .rodata.* .srodata .srodata.*)
.data : must be empty ASSERT(SIZEOF(.data) == 0)
.bss : must be empty ASSERT(SIZEOF(.bss) == 0)
ASSERT(_start == ORIGIN(sram))
__blob_size = end of .data - origin
The SPL copies the raw bytes and executes
fence.i; nothing initializes.dataor zeroes.bss, so both are forbidden and the build fails if any object lands there..text.entry(fromstart.S) is forced first so that offset 0 is the entry point.The
0xb000-byte window ends before the BootROM data area near0xc0838000; the linker fails if the image outgrows it.__blob_sizeis exported into the header.
Image layout
offset content
0x00 j k1_blob_ddr_init mainline entry (ddr_init ABI)
0x04 j k1_blob_retrain re-train entry (validation only)
0x08 "K1DDRGPL" magic, 8 ASCII bytes
0x10 1 layout version (u32)
0x14 __blob_size image size in bytes (u32)
0x18 code (.text), then constant tables and strings (.rodata)
Both entries are plain j instructions assembled with .option norvc, so
they are exactly 4 bytes each and a0..a3 and ra reach the C functions
untouched; the C functions return directly to the SPL. The magic and size let a
loader verify the image (the validation hook in U-Boot checks the magic before
using the re-train entry, Validation vehicle).
Size
Current build (GCC 16.2, -Os):
Section |
Bytes |
|---|---|
|
13 782 |
|
6 376 |
raw image |
20 160 (of 45 056 available) |
The build is reproducible: with the same toolchain the image is byte-identical across builds.
Stack budget
The firmware has no stack of its own; it runs on the SPL’s stack in SRAM. Frame
sizes from -fstack-usage (largest first):
Function |
Frame |
|---|---|
|
944 B (two |
|
464 B (one |
|
352 B (boot report) |
|
304 B |
|
224 B each |
|
208 B |
|
192 B |
|
176 B each |
|
144 B |
GCC’s call-graph output does not follow the indirect calls (the logging sink,
the stage table and the on_pass observer). Adding them by hand, the deepest
chain is a log line from inside a training stage during a cold boot:
k1_blob_ddr_init 352 -> k1_ddr_boot 304 -> k1_boot_pass 464 ->
k1_ddrt_train_pass 208 -> k1_stage_tx 176 -> k1_fail 48 -> k1_log 80 ->
blob_vlog 224 -> k1_vsnprintf 144 -> k1_put_field 64 ~ 2.1 KiB
The re-train entry peaks at about 1.9 KiB. Keep the SPL stack comfortably above
2 KiB of free space when calling the image; adding fields to
struct k1_ddrt_result grows the two largest frames directly.
To reproduce the measurement, build a copy with
make blob BLOB_DEFS="-fstack-usage -fcallgraph-info=su" (the .su/.ci
files land next to the ELF).
Using the image with mainline U-Boot
make -C src blob
export DDR_FW_FILE=$PWD/build/blob/free_spacemit_k1_ddr_fw.bin
# in the U-Boot tree:
make spacemit_k1_defconfig && make # -> u-boot-spl-ddr.bin
Mainline’s binman places the file after the SPL, 64-byte aligned, and the SPL
copies it to 0xc082d000 at run time. The build-time wiring is described in
Integration into the build.