U-Boot Build, Banner & Persistent Environment
Start from why an ARM Cortex-A7 cannot boot Linux directly out of raw flash before DDR3 is trained, cross-build U-Boot 2025.x out of tree, separate running RAM monitor identity from storage boot selection, and verify full-span reflected CRC-32 environment persistence.
What you'll learn
- Distinguish the running U-Boot image from the image selected by the storage boot path.
- Compute the environment CRC over the complete fixed-size data area.
- Diagnose variables that survive a warm prompt but disappear after reset.
Why Embedded Linux Systems Need a Bootloader and Persistent Environment
Before typing a single make or serial command, ask the physical first-principles question: why cannot an ARM Cortex-A7 chip jump straight from cold power-on into a Linux 6.12 zImage stored on eMMC or an SD card? At t = 0 when POR_B (Power-On Reset) deasserts, external DDR3 SDRAM (0x80000000) is completely unusable—its clock gates, ZQ impedance resistors, DQS read/write delay lines, and row/column address multiplexers are unconfigured. Meanwhile, the Cortex-A7 core fetches its very first instruction from physical address 0x00000000, which maps to on-chip Mask Boot ROM with only 128 KiB of internal OCRAM (0x00900000) available, whereas the Linux kernel expects tens of megabytes of initialized DRAM plus a hardware-describing (DTB) pointer in register r2.
As Figure 1.1a traces from left to right, embedded boot solves this chicken-and-egg boundary in stages. First, the i.MX6ULL Boot ROM samples BOOT_MODE[1:0] and BT_CFG fuses/pins to pick one boot medium (SD, eMMC boot0, QSPI, or USB Serial Download Protocol SDP) and reads u-boot.imx starting at byte offset 0x400 (1 KiB). Inside u-boot.imx, the Image Vector Table (IVT, tag 0xD1) points the ROM at the Device Configuration Data (DCD, tag 0xD2) table—a register-write list that the ROM executes to train the Multi-Mode DDR Controller (MMDC) before copying U-Boot 2025.x into DDR3 and jumping to _start. Consequently, seeing a U-Boot 2025.01 banner on UART1 proves which monitor image is currently executing in DRAM, not which storage copy a future cold reset will select! If DIP switches booted an SD card or USB SDP payload while eMMC boot0 still holds a 2023 binary, flipping the boot switch reverts the board immediately.
Figure 1.1a. Cold-reset hardware chain (POR_B -> Boot ROM -> DCD DDR3 init -> U-Boot 2025.x) and the separation between running RAM state and persistent flash sectors.
Volatile RAM Hash Tables Versus CRC-Checked Flash Sectors
Once U-Boot reaches its command loop, configuration strings (bootdelay, bootcmd, bootargs) live inside a volatile DRAM hash table (hsearch_r). Running setenv ksa_probe one mutates only those DRAM bytes; printenv ksa_probe reads them back instantaneously, tempting engineers to think configuration is finished. Only saveenv serializes the dictionary to the configured non-volatile backend (MMC, SPI NOR, or nowhere when CONFIG_ENV_IS_NOWHERE=y). On the next cold reset, env_load() reads that raw storage block (CONFIG_ENV_SIZE, typically 8 KiB = 8192 B), recomputes a 32-bit reflected Cyclic Redundancy Check (CRC-32, polynomial 0xEDB88320) across the entire S_env - 4 byte payload including trailing pad bytes, and falls back to compiled-in default_environment[] (*** Warning - bad CRC, using default environment) if a single bit disagrees.
mental model: Editing variables with setenv without running saveenv is like scribbling fuel calculations on a cockpit whiteboard instead of flashing the flight-management EEPROM: printenv reads the whiteboard during the current power cycle, but toggling POR_B wipes DRAM and reloads whatever saveenv last committed.
Where it stops working: Unlike a physical whiteboard, persistent storage blocks are validated by an unauthenticated CRC-32 over every byte including trailing padding—anyone who can rewrite the flash sector can recompute a valid checksum.
definition (U-Boot Image Identity & CRC-32 Persistent Environment): Running monitor identity (version + cold-boot serial banner + bdinfo) identifies the U-Boot executable active in DRAM, whereas storage boot selection (BOOT_MODE straps + eMMC PARTCONF + media offset 0x400) governs which u-boot.imx the Boot ROM loads on cold reset. The persistent environment stores a 4-byte little-endian reflected CRC-32 (0xEDB88320) followed by N = CONFIG_ENV_SIZE - 4 bytes of name=value\0 entries, a second \0 dictionary terminator, and trailing pad bytes up to S_env - 1 (minus 1 flag byte when redundant dual-slot CONFIG_SYS_REDUNDAND_ENVIRONMENT is active).
Walk the four distinct boundaries in the interactive sequence below before touching any board storage:
- Step 1. Step 1: Cross-build U-Boot 2025.x in an isolated O=out/ksa directory and record sha256sum out/ksa/u-boot.imx.
- Step 2. Step 2: Audit version, bdinfo, and env info on serial; remember setenv mutates volatile DRAM only.
- Step 3. Step 3: Persist via saveenv, writing the 4-byte reflected CRC-32 over all S_env - 4 serialized and padding bytes.
- Step 4. Step 4: Trigger a cold POR_B reset and verify both the expected build banner and printenv readback.
Figure 1.1b. Never conflate make output, setenv in DRAM, saveenv on MMC, and cold POR_B boot selection—verify each boundary with its own producer artifact.
Binary Environment Layout, Redundant Slots & Target Audit Workflow
Inspect Figure 1.1b to see why host provisioning tools (mkenvimage / fw_setenv) fail whenever an engineer checksums strlen(data) instead of the full S_env - 4 buffer. Because every name=value pair ends with a NUL byte (0x00), strlen() halts at the end of the very first variable (bootdelay=3)—ignoring every subsequent variable, the double-NUL (\0\0) end-of-dictionary marker, and all trailing pad bytes (0x00 or 0xFF) through data[N - 1]. Flipping even data[63] ^= 1U inside unused padding leaves printenv strings visually identical yet invalidates crc32, causing cold boot to discard the sector!
Figure 1.1b. Binary layout of U-Boot environment sectors — reflected CRC-32 covers every byte up to S_env - 1, while dual redundant slots survive power loss mid-saveenv.
Quantitatively, for S_env = 8192 B (8 KiB), a single-slot header spends 4 B on crc32, leaving N = 8188 B (65,504 bitwise shift-XOR steps across 8 * N bits; 512 steps on our 64 B host fixture). A redundant layout (2 * 8 KiB = 16 KiB) reserves 1 B per slot for a generation/active flag (8187 B data) so an interrupted erase/write on the inactive slot never bricks boot state. Crucially, CRC-32 is an error-detecting code, never cryptographic authentication: an attacker with flash access can modify bootcmd and recompute crc32 in microseconds. Production secure boot therefore locks environment policy (ENV_IS_NOWHERE or signed FIT enforcement).
| Environment Strategy | Storage Footprint & CRC Work | Power-Loss & Security Boundary | Production Selection Rule |
|---|---|---|---|
| Single raw MMC slot | 1 * S_env (8 KiB); 1 CRC pass over 8188 B | Torn saveenv corrupts the sole copy and reverts to defaults; 0 cryptographic authenticity | Lab bring-up where recovery defaults are safe and layout is simple |
| Redundant A/B MMC slots | 2 * S_env (16 KiB); 2 CRC checks on cold boot | Survives power loss mid-saveenv via intact sibling slot; mutable bootcmd still untrusted | Field systems requiring durable A/B slot counters across power cuts |
Compiled-in only (ENV_IS_NOWHERE) | 0 B external media reservation; 0 flash reads | saveenv disabled; prompt setenv edits always vanish on cold reset | Locked-down secure-boot products forbidding mutable bootcmd |
env / mkenvimage(1) GL-E03 Quick Reference env / mkenvimage(1) NAME printenv, setenv, saveenv, env info - U-Boot 2025.x environment lifecycle BINARY FORMAT Single: [uint32_t crc32 (4B LE)][data: S_env - 4B ("k=v\0...\0\0" + pad)] Redundant: [uint32_t crc32 (4B LE)][uint8_t flags (1B)][data: S_env - 5B] Polynomial: Reflected IEEE 802.3 CRC-32 0xEDB88320 ("123456789" -> 0xCBF43926) INVARIANTS 1. Inspect `version`, `bdinfo`, and `env info` BEFORE running `saveenv`. 2. Checksum covers ALL N bytes including padding; CRC != authentication. Kernel Systems Academy Lesson 1.1 env / mkenvimage(1) GL-E03 quick reference · full manual page: man 1 env / mkenvimage Study the out-of-tree cross-build (ARCH=arm, CROSS_COMPILE=arm-linux-gnueabihf-, O=out/ksa) and read-only serial inspection sequence below. Notice that imx6ull_14x14_evk_defconfig serves solely as an initial compilation baseline before custom DDR3 and pin-mux porting in Module m03, and saveenv stays commented until env info confirms CONFIG_ENV_OFFSET will not overwrite adjacent partitions.
learner@ksa:~/gl-e03/m01-l01$ bat -H 1 -H 2 -H 3 -H 8 -H 9 -H 10 build-and-inspect.sh File: build-and-inspect.sh make O=out/ksa ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- \ imx6ull_14x14_evk_defconfig make O=out/ksa ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j8 sha256sum out/ksa/u-boot.imx # At the target console, before any persistent write: version bdinfo printenv bootcmd bootargs env info setenv ksa_probe one printenv ksa_probe # Only on reviewed storage: # saveenv # reset # printenv ksa_probe- Record the exact SHA-256 digest of out/ksa/u-boot.imx alongside the Git commit and boot-switch setting before flashing.
- Verify the configured storage backend and offset via env info before uncommenting saveenv on target media.
Proving Reflected CRC-32 Coverage & Padding Sensitivity in ISO C11
On our development host we do not write raw MMC sectors; instead, m01-l01.c proves the exact binary format and reflected CRC-32 (0xEDB88320) contract over a 64-byte environment payload in strict ISO C11. First, asserting the IEEE standard check vector env_crc("123456789", 9U) == 0xCBF43926U rules out bit-order or initial/final inversion bugs where a broken writer and reader would agree with each other while failing on real U-Boot sectors. Second, 0U - (c & 1U) generates a branchless unsigned mask (0x00000000U or 0xFFFFFFFFU) with zero signed-shift undefined behavior. Finally, flipping trailing padding byte data[63] ^= 1U proves that corruption well past the \0\0 string terminator is caught immediately.
learner@ksa:~/gl-e03/m01-l01$ bat -H 10 -H 11 -H 12 -H 13 -H 14 -H 15 -H 21 -H 22 -H 23 -H 24 -H 25 -H 26 m01-l01.c File: m01-l01.c #include <assert.h> #include <inttypes.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <string.h> _Static_assert(sizeof(uint32_t) == 4, "32-bit format words required"); static uint32_t env_crc(const unsigned char *p, size_t n) { uint32_t c = UINT32_MAX; for (size_t i = 0; i < n; ++i) { c ^= p[i]; for (unsigned b = 0; b < 8U; ++b) c = (c >> 1) ^ (0xEDB88320U & (0U - (c & 1U))); } return ~c; } int main(void) { const unsigned char known[] = "123456789"; unsigned char data[64] = "bootdelay=3\0bootcmd=run mmcboot\0"; assert(env_crc(known, 9U) == 0xCBF43926U); uint32_t stored = env_crc(data, sizeof data); data[63] ^= 1U; assert(env_crc(data, sizeof data) != stored); data[63] ^= 1U; assert(env_crc(data, sizeof data) == stored); printf("known_crc=%08" PRIX32 " data_bytes=%zu\n", env_crc(known, 9U), sizeof data); puts("padding_corruption=rejected restored_environment=accepted"); return 0; }- Use unsigned mask 0U - (c & 1U) with reflected polynomial 0xEDB88320U and invert ~c on return, anchored against IEEE test vector 0xCBF43926U.
- Flipping data[63] far past the double-NUL terminator changes env_crc(data, sizeof data), proving trailing padding bytes participate in sector validation.
Running the Checksum Verifier & Mutation Experiments
Compile and execute m01-l01.c below. Verify known_crc=CBF43926 data_bytes=64 and padding_corruption=rejected restored_environment=accepted, and inspect the strace capture confirming the host binary touches zero block devices (openat loads only libc.so.6 before write(1, ...)). Next, test the classic provisioning bug yourself: replace sizeof data on line 25 with strlen((const char *)data) and watch assert(env_crc(...) != stored) abort because strlen stops at byte 11 (bootdelay=3) and never sees bootcmd or data[63].
learner@ksa:~/gl-e03/m01-l01gcc -std=c11 -Wall -Wextra -Werror \ m01-l01.c -o m01-l01learner@ksa:~/gl-e03/m01-l01./m01-l01known_crc=CBF43926 data_bytes=64padding_corruption=rejected restored_environment=acceptedlearner@ksa:~/gl-e03/m01-l01learner@ksa:~/gl-e03/m01-l01strace -e trace=openat,read,write,exit_group \./m01-l01openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\243\2\0\0\0\0\0"..., 832) = 832write(1, "known_crc=CBF43926 data_bytes=64"..., 91) = 91exit_group(0) = ?+++ exited with 0 +++Separating Build, Runtime, Persistence & Cold-Boot Evidence
Every mystery where *"my U-Boot change disappeared after reboot"* reduces to crossing two of the four boundaries you just traced:
setenvworked, cold reset lost the variable: Eithersaveenvwas never run,CONFIG_ENV_IS_NOWHEREis active,saveenvwrote toMMCuser space while boot readsboot0, orCONFIG_ENV_SIZEdisagreed with the factory image soenv_load()rejected theCRC-32and loaded defaults.saveenvsurvived, butversionshows an oldU-Bootbuild date: Persistent environment storage and the Boot ROM's0x400executable live at separate offsets (or separate media:SDvseMMCboot0). Updating one never updates the other.
Keep three separate records in your bring-up log: host C11 format proof, sha256sum of u-boot.imx, and a cold POR_B serial capture from the very first ROM/banner byte. Later: Lesson m01-l02 maps physical DRAM intervals, 512 B MMC block transfers, and RMII TFTP network diagnostics so staging kernels never overwrites live monitor memory, and m01-l03 adds cryptographic FIT signatures where CRC-32 is insufficient.
Common Production Pitfalls
warning: Symptom: A variable set with setenv works during interactive testing (printenv prints the new value), yet vanishes immediately after pressing the hardware POR_B reset button. Root Cause: setenv updates only the volatile DRAM hash table (hsearch_r), or saveenv wrote to a different MMC hardware partition than env_load() reads on cold boot. Fix: Inspect env info first, execute saveenv to write the CRC-32-guarded sector to the verified backend, power-cycle cold, and confirm no bad CRC warning appears in the boot log.
warning: Symptom: A factory-flashed environment image generated by a custom Python/C script triggers *** Warning - bad CRC, using default environment on U-Boot 2025.x, or an engineer mistakenly treats a passing CRC-32 as Secure Boot verification. Root Cause: Either the script computed CRC-32 over strlen() / non-padded bytes instead of the full CONFIG_ENV_SIZE - 4 region (or omitted the 1-byte redundant flag), or confused unkeyed CRC-32 error detection with cryptographic signing. Fix: Pad the payload to exact CONFIG_ENV_SIZE - 4 (- 5 when redundant) with 0xFF/0x00 before computing reflected 0xEDB88320 CRC-32, and disable mutable environment boot paths (ENV_IS_NOWHERE + signed FIT) when defending against flash tampering.
Practice & Retrieval
Guided Practice: Diagnosing a Warm-Prompt vs Cold-Reset Mismatch
An engineer boots a board over USB SDP (uuu out/ksa/u-boot.imx), runs setenv bootdelay 5; saveenv, sees Saving Environment to MMC... OK, toggles DIP switches back to eMMC boot, and resets. After reset, printenv bootdelay shows 5, yet version prints a six-month-old U-Boot timestamp! Step 1 (Separate Producers): Identify which storage region produced version (eMMC boot0 @ 0x400) versus which storage region produced bootdelay=5 (CONFIG_ENV_OFFSET on MMC). Step 2 (Verify): List the exact commands and sha256sum checks required before flashing out/ksa/u-boot.imx to the boot partition.
Independent Practice: Redundant Dual-Slot Environment Selector (env_redund_sim.c)
Extend m01-l01.c into a standalone host program env_redund_sim.c modeling two 64-byte redundant slots (struct env_slot { uint32_t crc; uint8_t flags; unsigned char data[59]; }). Implement select_active_slot(a, b) covering all four cases: both valid (uint8_t serial counter (flags_b - flags_a) == 1 picks the newer slot cleanly across 255 -> 0 wrap), only A valid (power cut tore B mid-saveenv), only B valid, and both corrupt (returns -1 to trigger default_environment). Assert all four transitions under -std=c11 -Wall -Wextra -Werror.
Key takeaways
- Cold
ARM Cortex-A7boot requires the on-chip Boot ROM (0x00000000) to readu-boot.imx(IVT + boot_data + DCD) at media offset0x400and train externalDDR3viaMMDCbeforeU-Boot 2025.xor theLinux kernelcan execute in DRAM. - Keep running monitor identity (
version,bdinfo) separate from storage boot path (BOOT_MODEstraps,SDvseMMCboot0): booting overUSBSDPorSDnever updates staleeMMCflash automatically. setenvmutates only the volatile DRAM hash table (hsearch_r);saveenvwrites a4-byte reflectedCRC-32(0xEDB88320) computed across allCONFIG_ENV_SIZE - 4bytes (- 5with redundant flags), including trailing pad bytes.- Treat
CRC-32strictly as corruption/torn-write detection—never as cryptographic authentication ofbootcmd. - The CRC covers the fixed-size serialized data region, including padding.
- A reset reconstructs the RAM dictionary from the configured persistent source.
- CRC detects accidental changes but does not establish a trusted signer.