Lab 1: U-Boot 2025.01 Environment CRC, Block Bounds & Handoff Policy
Implement, compile, and verify lab_m01_uboot_env_bootcmd against ./check.sh.
Prerequisites in this module
What ./check.sh verifies
- Strict C11 module build --- message: compiler rejected module sources hint: inspect Makefile and warnings ...
- Environment positive and negative invariants --- message: computed result or rejection assertion failed hint: repair the environment invariant, not the expected transcript ...
- Media positive and negative invariants --- message: computed result or rejection assertion failed hint: repair the media invariant, not the expected transcript ...
- Handoff positive and negative invariants --- message: computed result or rejection assertion failed hint: repair the handoff invariant, not the expected transcript ...
step 1/15 · inspect starter files and specification
Early bring-up teams routinely lose hours chasing phantom kernel crashes caused two stages earlier in U-Boot: a corrupted persistent environment block, an mmc read count treated as bytes instead of 512 B sectors, or a failed load followed by ; bootz into stale DRAM. Converting those three contracts into a deterministic host C11 verifier turns silent boot corruption into an immediate assertion failure before anyone touches board flash.
learner@ksa:~/gl-e03/lab-m01ls -lastarter/Makefilestarter/contracts.hstarter/environment.cstarter/handoff.cstarter/main.cstarter/media.cstarter/target/board.itsstarter/target/build-and-inspect.shstarter/target/inspect-media.cmdlearner@ksa:~/gl-e03/lab-m01starter: starter/Makefile
learner@ksa:~/gl-e03/lab-m01$ bat starter/Makefile File: starter/Makefile CC = gcc CFLAGS = -std=c11 -Wall -Wextra -Werror -O2 all: boot-contract boot-contract: main.c environment.c media.c handoff.c contracts.h $(CC) $(CFLAGS) main.c environment.c media.c handoff.c -o $@ clean: rm -f boot-contract .PHONY: all cleanstarter: starter/contracts.h
learner@ksa:~/gl-e03/lab-m01$ bat starter/contracts.h File: starter/contracts.h #ifndef KSA_CONTRACTS_H #define KSA_CONTRACTS_H int verify_environment(void); int verify_media(void); int verify_handoff(void); #endifstarter: starter/environment.c
learner@ksa:~/gl-e03/lab-m01$ bat starter/environment.c File: starter/environment.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) ^ (0x04C11DB7U & (0U - (c & 1U))); } return ~c; } int verify_environment(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; }starter: starter/handoff.c
learner@ksa:~/gl-e03/lab-m01$ bat starter/handoff.c File: starter/handoff.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"); enum action { REJECT, LOCAL, RECOVERY }; static enum action choose_boot(int kernel_loaded, int fdt_loaded, int signature_valid, int recovery_allowed) { if (!signature_valid) return REJECT; if (kernel_loaded && fdt_loaded) return LOCAL; return recovery_allowed ? RECOVERY : REJECT; } static int arguments(char *dst, size_t n) { int written = snprintf(dst, n, "console=ttymxc0,115200 root=PARTUUID=demo rootwait"); return written >= 0 && (size_t)written < n; } int verify_handoff(void) { char complete[128], truncated[128]; volatile size_t short_capacity = 16U; assert(choose_boot(1, 1, 1, 1) == LOCAL); assert(choose_boot(0, 1, 1, 1) == RECOVERY); assert(choose_boot(1, 1, 0, 1) == REJECT); assert(choose_boot(1, 0, 1, 0) == REJECT); assert(arguments(complete, sizeof complete)); assert(!arguments(truncated, short_capacity)); printf("bootargs=%s\n", complete); puts("stale_load=blocked invalid_signature=rejected truncation=rejected"); return 0; }starter: starter/main.c
learner@ksa:~/gl-e03/lab-m01$ bat starter/main.c File: starter/main.c #include "contracts.h" #include <stdio.h> #include <string.h> int main(int argc, char **argv) { if (argc == 2) { if (strcmp(argv[1], "environment") == 0) return verify_environment(); if (strcmp(argv[1], "media") == 0) return verify_media(); if (strcmp(argv[1], "handoff") == 0) return verify_handoff(); } fputs("FAIL: choose a documented verification scenario\n", stderr); return 2; }starter: starter/media.c
learner@ksa:~/gl-e03/lab-m01$ bat starter/media.c File: starter/media.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 int ram_fits(uint32_t start, uint32_t n, uint32_t end) { return n != 0U && start <= end && n <= end - start; } static int read_fits(uint32_t lba, uint32_t count, uint32_t blocks, uint32_t dst, uint32_t ram_end) { if (count == 0U || lba > blocks || count > blocks - lba) return 0; uint64_t bytes = (uint64_t)count * 512U; return bytes <= UINT32_MAX && ram_fits(dst, (uint32_t)bytes, ram_end); } int verify_media(void) { assert(read_fits(2048U, 2048U, 8192U, 0x82000000U, 0x84000000U)); assert(read_fits(8191U, 1U, 8192U, 0x82000000U, 0x84000000U)); assert(!read_fits(8192U, 1U, 8192U, 0x82000000U, 0x84000000U)); assert(!ram_fits(0xFFFFFFF0U, 64U, UINT32_MAX)); assert(!read_fits(0U, UINT32_MAX, UINT32_MAX, 0U, UINT32_MAX)); printf("blocks=2048 bytes=%" PRIu64 "\n", (uint64_t)2048U * 512U); puts("last_block=accepted partition_overrun=rejected wrap=rejected"); return 0; }starter: starter/target/board.its
learner@ksa:~/gl-e03/lab-m01$ bat starter/target/board.its File: starter/target/board.its /dts-v1/; / { description = "ARMv7-A lab kernel and board tree"; #address-cells = <1>; images { kernel { data = /incbin/("zImage"); type = "kernel"; arch = "arm"; os = "linux"; compression = "none"; load = <0x82000000>; entry = <0x82000000>; hash-1 { algo = "sha256"; }; }; fdt { data = /incbin/("imx6ull-ksa-lab.dtb"); type = "flat_dt"; arch = "arm"; compression = "none"; hash-1 { algo = "sha256"; }; }; }; configurations { default = "conf-ksa"; conf-ksa { kernel = "kernel"; fdt = "fdt"; }; }; }; /* Boot command shape, at the target: * if load mmc 0:1 ${kernel_addr_r} zImage && * load mmc 0:1 ${fdt_addr_r} imx6ull-ksa-lab.dtb; then * setenv bootargs console=ttymxc0,115200 root=PARTUUID=REVIEWED rootwait * bootz ${kernel_addr_r} - ${fdt_addr_r} * fi */starter: starter/target/build-and-inspect.sh
learner@ksa:~/gl-e03/lab-m01$ bat starter/target/build-and-inspect.sh File: starter/target/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_probestarter: starter/target/inspect-media.cmd
learner@ksa:~/gl-e03/lab-m01$ bat starter/target/inspect-media.cmd File: starter/target/inspect-media.cmd bdinfo mmc list mmc dev 0 0 mmc part fatls mmc 0:1 / ext4ls mmc 0:2 / # Example RAM addresses require a reviewed free-memory map. fatload mmc 0:1 0x82000000 zImage setenv kernel_size ${filesize} md.b 0x82000000 0x40 setenv autoload no dhcp ping ${serverip} tftpboot 0x84000000 zImage cmp.b 0x82000000 0x84000000 ${kernel_size} # Alternative transport, not a kernel root mount: # nfs 0x84000000 ${serverip}:/srv/boot/zImagestep 2/15 · 2. learning objectives
- Compute reflected
CRC-32(0xEDB88320) across the entire fixed-size64 Bserialized environment buffer, including NUL-padded tail bytes, inenvironment.c. - Validate
512 BLBA partition intervals and32-bithalf-open RAM windows[dst, ram_end)without unsigned addition wraparound inmedia.c. - Enforce fail-closed
bootz/ FIT handoff selection andsnprintf()bootargstruncation rejection inhandoff.c. - Diagnose and repair the seeded polynomial defect in
starter/environment.cfrom TAP failure diagnostics without altering expected transcripts.
step 3/15 · 3. environment requirements
Execute all graded commands from course_creator/courses/imx6ull-uboot-kernel-rootfs/labs/lab_m01_uboot_env_bootcmd on a Linux workstation (7.0.0-42-generic in the recorded capture) with a POSIX /bin/sh shell, GNU Make, and a C11 compiler. No physical board, USB serial cable, or block device is accessed during host verification; keep build artifacts inside this directory and never pass /dev/sdX or /dev/mmcblkX to host commands.
step 4/15 · 4. required software and hardware
Host software: gcc supporting -std=c11 -Wall -Wextra -Werror -O2, make, and /bin/sh to run check.sh. Host source tree: contracts.h, main.c, environment.c, media.c, handoff.c, and Makefile under both starter/ and files/, plus target reference listings files/target/board.its and files/target/build-and-inspect.sh. Hardware: none for graded TAP verification; an ARMv7-A Cortex-A7 lab board running U-Boot 2025.01 is optional for the ungraded read-only serial inspection in files/target/build-and-inspect.sh.
step 5/15 · 5. complete executable commands
Inspect the seeded starter failure in an isolated scratch copy first, then build and verify the reference implementation:
learner@ksa:~/gl-e03/lab-m01$ bat script.sh File: script.sh rm -rf .work && mkdir -p .work cp -R starter .work/files && cp check.sh .work/ (cd .work && sh ./check.sh) # Expected: not ok 2 + FAIL: on stderr (exit 1) make -C files # Compile warning-clean reference binary ./files/boot-contract environment # Verify CRC-32 vector and padding mutation check ./files/boot-contract media # Verify LBA*512 and [0x82000000, 0x84000000) bounds ./files/boot-contract handoff # Verify signature/load policy and bootargs capacity ./check.sh # Run full 5-test TAP version 13 suite (exit 0) rm -rf .workcheck.sh copies files/ into a temporary .verify.XXXXXX sandbox so compiling never pollutes tracked sources. Repair your copy of starter/environment.c inside .work/files/ and rerun (cd .work && sh ./check.sh) until all five TAP checks pass.
step 6/15 · 6. annotated source code
| Scenario verb | Source file & entry point | Verified positive output | Verified negative rejections |
|---|---|---|---|
environment | environment.c -> verify_environment() | known_crc=CBF43926 data_bytes=64 | padding_corruption=rejected |
media | media.c -> verify_media() | blocks=2048 bytes=1048576, last_block=accepted | partition_overrun=rejected, wrap=rejected |
handoff | handoff.c -> verify_handoff() | bootargs=console=ttymxc0,115200 root=PARTUUID=demo rootwait | stale_load=blocked, invalid_signature=rejected, truncation=rejected |
Inspect the core checksum loop in files/environment.c alongside the seeded bug in starter/environment.c:
learner@ksa:~/gl-e03/lab-m01$ bat example.c File: example.c static uint32_t env_crc(const unsigned char *p, size_t n) { uint32_t c = UINT32_MAX; /* Pre-invert initial CRC state */ for (size_t i = 0; i < n; ++i) { /* Cover all n == sizeof data bytes, including pad */ c ^= p[i]; for (unsigned b = 0; b < 8U; ++b) c = (c >> 1) ^ (0xEDB88320U & (0U - (c & 1U))); /* starter bug: 0x04C11DB7U */ } return ~c; /* Post-invert final remainder */ }media.c:read_fits()guardscount == 0U || lba > blocks || count > blocks - lba, widensbytes = (uint64_t)count * 512Ubefore checkingbytes <= UINT32_MAX, and callsram_fits(dst, (uint32_t)bytes, ram_end)using subtractionn <= end - start.handoff.c:choose_boot()rejects invalid signatures unconditionally (!signature_valid -> REJECT) even whenrecovery_allowed == 1, andarguments()checkswritten >= 0 && (size_t)written < nso a16 Bdestination refuses a50 Bbootargsstring.files/target/:board.itsillustrates FITsha256component nodes and&&-chainedbootz ${kernel_addr_r} - ${fdt_addr_r}syntax;build-and-inspect.shlists read-onlyversion,bdinfo, andenv infocommands prior to anysaveenv.
step 7/15 · 7. expected output
Unmodified starter/ fails check 2 in captures.json with exit code 1:
learner@ksa:~/gl-e03/lab-m01$ bat listing.txt File: listing.txt not ok 2 - environment positive and negative invariants --- message: computed result or rejection assertion failed hint: repair the environment invariant, not the expected transcript ... # pass 4/5 FAIL: repair the failed engineering contractRunning ./check.sh against the reference files/ tree (or a repaired starter/ copy) produces this host TAP transcript (exit 0):
learner@ksa:~/gl-e03/lab-m01$ bat listing.txt File: listing.txt TAP version 13 1..5 ok 1 - strict C11 module build ok 2 - environment positive and negative invariants ok 3 - media positive and negative invariants ok 4 - handoff positive and negative invariants ok 5 - unsupported scenario refused # pass 5/5These transcripts establish host C11 model behavior only, not physical MMC persistence or board boot execution.
step 8/15 · 8. explanation of observed behavior
Test 2 aborts on starter/environment.c because right-shifting bit-first CRC-32 requires the bit-reversed generator 0xEDB88320U; pairing (c >> 1) with left-shift polynomial 0x04C11DB7U scrambles bit significance and fails env_crc("123456789", 9U) == 0xCBF43926U before reaching the data[63] ^= 1U padding test. Once repaired, flipping byte 63 (far past "bootdelay=3\0bootcmd=run mmcboot\0") proves that U-Boot checksums all sizeof data bytes rather than stopping at strlen(). Tests 3, 4, and 5 confirm boundary block 8191 acceptance, 8192 overrun rejection, 0xFFFFFFF0U + 64U wrap rejection, stale-buffer blocking, and exit code 2 on unknown CLI verbs.
step 9/15 · 9. experiment variations
- Boundary LBA shift: in a scratch copy of
media.c, testread_fits(6144U, 2048U, 8192U, ...)(exact end8192) versusread_fits(6145U, 2048U, 8192U, ...)(1block past partition end) and verify the first passes while the second returns0. - Naive
strlenbug reproduction: temporarily passstrlen((const char *)data)instead ofsizeof datatoenv_crc()and observe thatdata[63] ^= 1Usilently passes, reproducing a provisioning-tool mismatch bug. - Far-transfer policy check: extend
choose_boot()in your scratch copy with anenv_authenticatedparameter that blocks mutablebootargsoverrides when secure boot is active, stating how that differs from an unlocked lab board.
step 10/15 · 10. performance measurements
The functional checker runs in single-digit milliseconds without claiming target storage throughput. To benchmark env_crc() throughput over realistic 8 KiB (8188 B) environment payloads on your host, wrap env_crc(buf, 8188U) in a standalone C11 harness timed with clock_gettime(CLOCK_MONOTONIC): discard 5 warm-up runs, record N = 30 batches of 1000 checksums, and compute p50, p95, and p99 nanoseconds per 8 KiB buffer alongside compiler flags (-O2) and host CPU model. Never extrapolate x86_64 host CRC-32 timings to a 792 MHz Cortex-A7 or eMMC write latency.
step 11/15 · 11. troubleshooting
| Symptom | Root cause | Corrective action |
|---|---|---|
./check.sh passes even before you edit starter/environment.c | ./check.sh validates ./files relative to its own directory | Copy starter to .work/files, copy check.sh to .work/, and run (cd .work && sh ./check.sh) |
not ok 1 - strict C11 module build | Signed/unsigned comparison or unused variable tripped -Wall -Wextra -Werror | Use size_t / uint32_t with U suffixes (0xEDB88320U) and inspect make -C .work/files compiler output |
not ok 5 - unsupported scenario refused | main.c returned 1 instead of 2 for unrecognized CLI arguments | Keep return 2; after fputs("FAIL: ...", stderr) in main.c |
step 12/15 · 12. guided questions
- Why does
read_fits()multiply(uint64_t)count * 512Uin 64-bit arithmetic before callingram_fits()?
- Hint 1: Evaluate
count * 512Uinuint32_twhencount = 0x00800001U(8388609blocks). - Hint 2: Notice
(uint32_t)(0x00800001U * 512U)truncates mod2^32to512U, which would falsely fit inside a32 MiBRAM window if not widened first.
- In
handoff.c, why doeschoose_boot(1, 1, 0, 1)returnREJECTeven thoughrecovery_allowedis1?
- Hint 1: Compare an I/O read error on local flash against a failed cryptographic signature on a loaded image.
- Hint 2: Falling back from a signature failure to an unauthenticated recovery path lets an attacker corrupt the primary signature to force unsigned code execution.
step 13/15 · 13. independent challenges
- Near-transfer: add a redundant-environment selector helper
pick_redundant_env(int crc_a_ok, uint8_t flags_a, int crc_b_ok, uint8_t flags_b)in your scratch copy that handles wraparound from serial255(0xFF) to0(0x00) when both CRCs pass, and test all single-valid and wrap cases. - Target acceptance plan: using
files/target/build-and-inspect.shandboard.itsas starting points, list the exact serial commands and cold-reset observations required on a Cortex-A7 board to prove (a) which hardware partitionsaveenvwrites and (b) thatbootzrefuses to run when.dtbloading fails.
step 14/15 · 14. solutions and explanations
Compare your repaired .work/files/environment.c against files/environment.c line 14: restoring 0xEDB88320U inside c = (c >> 1) ^ (0xEDB88320U & (0U - (c & 1U))); aligns the right-shifting bit order with IEEE 802.3 reflected CRC-32, yielding known_crc=CBF43926 and restoring 5/5 TAP passes (exit 0). Host success proves the C11 checksum, interval, and policy predicates; physical eMMC partition safety, saveenv power-cut behavior, and signed FIT enforcement still require dedicated target serial verification.
step 15/15 · run verification and inspect output
Run the verification command and confirm the output matches the expected contract.
learner@ksa:~/gl-e03/lab-m01./files/boot-contract environmentlearner@ksa:~/gl-e03/lab-m01known_crc=CBF43926 data_bytes=64padding_corruption=rejected restored_environment=acceptedProgressive hints & solution
hint 1
Compare starter/Makefile against the module lessons. Focus first on: Strict C11 module build --- message: compiler rejected module sources hint: inspect Makefile and warnings ...
hint 2
Compile with -std=c11 -Wall -Wextra -Werror, check every system-call return value before touching output buffers, and verify cleanup on error paths.
solution
warning: Reveal the reference solution files? Running ./check.sh against your own attempt first builds stronger retention.
solution: files/Makefile
learner@ksa:~/gl-e03/lab-m01$ bat files/Makefile File: files/Makefile CC = gcc CFLAGS = -std=c11 -Wall -Wextra -Werror -O2 all: boot-contract boot-contract: main.c environment.c media.c handoff.c contracts.h $(CC) $(CFLAGS) main.c environment.c media.c handoff.c -o $@ clean: rm -f boot-contract .PHONY: all cleansolution: files/contracts.h
learner@ksa:~/gl-e03/lab-m01$ bat files/contracts.h File: files/contracts.h #ifndef KSA_CONTRACTS_H #define KSA_CONTRACTS_H int verify_environment(void); int verify_media(void); int verify_handoff(void); #endifsolution: files/environment.c
learner@ksa:~/gl-e03/lab-m01$ bat files/environment.c File: files/environment.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 verify_environment(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; }solution: files/handoff.c
learner@ksa:~/gl-e03/lab-m01$ bat files/handoff.c File: files/handoff.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"); enum action { REJECT, LOCAL, RECOVERY }; static enum action choose_boot(int kernel_loaded, int fdt_loaded, int signature_valid, int recovery_allowed) { if (!signature_valid) return REJECT; if (kernel_loaded && fdt_loaded) return LOCAL; return recovery_allowed ? RECOVERY : REJECT; } static int arguments(char *dst, size_t n) { int written = snprintf(dst, n, "console=ttymxc0,115200 root=PARTUUID=demo rootwait"); return written >= 0 && (size_t)written < n; } int verify_handoff(void) { char complete[128], truncated[128]; volatile size_t short_capacity = 16U; assert(choose_boot(1, 1, 1, 1) == LOCAL); assert(choose_boot(0, 1, 1, 1) == RECOVERY); assert(choose_boot(1, 1, 0, 1) == REJECT); assert(choose_boot(1, 0, 1, 0) == REJECT); assert(arguments(complete, sizeof complete)); assert(!arguments(truncated, short_capacity)); printf("bootargs=%s\n", complete); puts("stale_load=blocked invalid_signature=rejected truncation=rejected"); return 0; }solution: files/main.c
learner@ksa:~/gl-e03/lab-m01$ bat files/main.c File: files/main.c #include "contracts.h" #include <stdio.h> #include <string.h> int main(int argc, char **argv) { if (argc == 2) { if (strcmp(argv[1], "environment") == 0) return verify_environment(); if (strcmp(argv[1], "media") == 0) return verify_media(); if (strcmp(argv[1], "handoff") == 0) return verify_handoff(); } fputs("FAIL: choose a documented verification scenario\n", stderr); return 2; }solution: files/media.c
learner@ksa:~/gl-e03/lab-m01$ bat files/media.c File: files/media.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 int ram_fits(uint32_t start, uint32_t n, uint32_t end) { return n != 0U && start <= end && n <= end - start; } static int read_fits(uint32_t lba, uint32_t count, uint32_t blocks, uint32_t dst, uint32_t ram_end) { if (count == 0U || lba > blocks || count > blocks - lba) return 0; uint64_t bytes = (uint64_t)count * 512U; return bytes <= UINT32_MAX && ram_fits(dst, (uint32_t)bytes, ram_end); } int verify_media(void) { assert(read_fits(2048U, 2048U, 8192U, 0x82000000U, 0x84000000U)); assert(read_fits(8191U, 1U, 8192U, 0x82000000U, 0x84000000U)); assert(!read_fits(8192U, 1U, 8192U, 0x82000000U, 0x84000000U)); assert(!ram_fits(0xFFFFFFF0U, 64U, UINT32_MAX)); assert(!read_fits(0U, UINT32_MAX, UINT32_MAX, 0U, UINT32_MAX)); printf("blocks=2048 bytes=%" PRIu64 "\n", (uint64_t)2048U * 512U); puts("last_block=accepted partition_overrun=rejected wrap=rejected"); return 0; }solution: files/target/board.its
learner@ksa:~/gl-e03/lab-m01$ bat files/target/board.its File: files/target/board.its /dts-v1/; / { description = "ARMv7-A lab kernel and board tree"; #address-cells = <1>; images { kernel { data = /incbin/("zImage"); type = "kernel"; arch = "arm"; os = "linux"; compression = "none"; load = <0x82000000>; entry = <0x82000000>; hash-1 { algo = "sha256"; }; }; fdt { data = /incbin/("imx6ull-ksa-lab.dtb"); type = "flat_dt"; arch = "arm"; compression = "none"; hash-1 { algo = "sha256"; }; }; }; configurations { default = "conf-ksa"; conf-ksa { kernel = "kernel"; fdt = "fdt"; }; }; }; /* Boot command shape, at the target: * if load mmc 0:1 ${kernel_addr_r} zImage && * load mmc 0:1 ${fdt_addr_r} imx6ull-ksa-lab.dtb; then * setenv bootargs console=ttymxc0,115200 root=PARTUUID=REVIEWED rootwait * bootz ${kernel_addr_r} - ${fdt_addr_r} * fi */solution: files/target/build-and-inspect.sh
learner@ksa:~/gl-e03/lab-m01$ bat files/target/build-and-inspect.sh File: files/target/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_probesolution: files/target/inspect-media.cmd
learner@ksa:~/gl-e03/lab-m01$ bat files/target/inspect-media.cmd File: files/target/inspect-media.cmd bdinfo mmc list mmc dev 0 0 mmc part fatls mmc 0:1 / ext4ls mmc 0:2 / # Example RAM addresses require a reviewed free-memory map. fatload mmc 0:1 0x82000000 zImage setenv kernel_size ${filesize} md.b 0x82000000 0x40 setenv autoload no dhcp ping ${serverip} tftpboot 0x84000000 zImage cmp.b 0x82000000 0x84000000 ${kernel_size} # Alternative transport, not a kernel root mount: # nfs 0x84000000 ${serverip}:/srv/boot/zImageRun the check
From labs/lab_m01_uboot_env_bootcmd/, run ./check.sh. A complete solution emits TAP version 13 with every test point marked ok and exits 0:
learner@ksa:~/gl-e03/lab-m01./check.shTAP version 131..5ok 1 - strict C11 module buildok 2 - environment positive and negative invariantsok 3 - media positive and negative invariantsok 4 - handoff positive and negative invariantsok 5 - unsupported scenario refused# pass 5/5On an incomplete starter tree, ./check.sh exits non-zero and marks the unmet assertion not ok with an indented YAML diagnostic block:
learner@ksa:~/gl-e03/lab-m01./check.shTAP version 131..5ok 1 - strict C11 module buildnot ok 2 - environment positive and negative invariants --- message: computed result or rejection assertion failed hint: repair the environment invariant, not the expected transcript ...ok 3 - media positive and negative invariantsok 4 - handoff positive and negative invariantsok 5 - unsupported scenario refused# pass 4/5AbortedFAIL: repair the failed engineering contractnote: How to read TAP diagnostics: 1..N states the total test plan; each ok k confirms a passed check; not ok k pinpoints the failing check alongside message: (what went wrong) and hint: (where to look next) between --- and ....
inspect harness: check.sh
learner@ksa:~/gl-e03/lab-m01$ bat check.sh File: check.sh #!/bin/sh set -u cd "$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)" work=$(mktemp -d "./.verify.XXXXXX") || exit 1 trap 'rm -rf "$work"' EXIT HUP INT TERM pass=0 printf 'TAP version 13\n1..5\n' cp -R files "$work/files" if make -C "$work/files" >"$work/build.log" 2>&1; then printf 'ok 1 - strict C11 module build\n'; pass=$((pass+1)) else printf 'not ok 1 - strict C11 module build\n ---\n message: compiler rejected module sources\n hint: inspect Makefile and warnings\n ...\n'; fi bin="$work/files/boot-contract" expected='known_crc=CBF43926 data_bytes=64 padding_corruption=rejected restored_environment=accepted' if actual=$("$bin" environment 2>"$work/error.log") && [ "$actual" = "$expected" ]; then printf 'ok 2 - environment positive and negative invariants\n'; pass=$((pass+1)) else printf 'not ok 2 - environment positive and negative invariants\n ---\n message: computed result or rejection assertion failed\n hint: repair the environment invariant, not the expected transcript\n ...\n'; fi expected='blocks=2048 bytes=1048576 last_block=accepted partition_overrun=rejected wrap=rejected' if actual=$("$bin" media 2>"$work/error.log") && [ "$actual" = "$expected" ]; then printf 'ok 3 - media positive and negative invariants\n'; pass=$((pass+1)) else printf 'not ok 3 - media positive and negative invariants\n ---\n message: computed result or rejection assertion failed\n hint: repair the media invariant, not the expected transcript\n ...\n'; fi expected='bootargs=console=ttymxc0,115200 root=PARTUUID=demo rootwait stale_load=blocked invalid_signature=rejected truncation=rejected' if actual=$("$bin" handoff 2>"$work/error.log") && [ "$actual" = "$expected" ]; then printf 'ok 4 - handoff positive and negative invariants\n'; pass=$((pass+1)) else printf 'not ok 4 - handoff positive and negative invariants\n ---\n message: computed result or rejection assertion failed\n hint: repair the handoff invariant, not the expected transcript\n ...\n'; fi rc=0 "$bin" undocumented >"$work/invalid.out" 2>&1 || rc=$? if [ "$rc" -eq 2 ]; then printf 'ok 5 - unsupported scenario refused\n'; pass=$((pass+1)) else printf 'not ok 5 - unsupported scenario refused\n ---\n message: unsupported input was accepted\n hint: retain explicit CLI validation\n ...\n'; fi printf '# pass %s/5\n' "$pass" [ "$pass" -eq 5 ] || { echo "FAIL: repair the failed engineering contract" >&2; exit 1; }Definition of done
Verification checklist
status: 0/3 checks verified
Troubleshooting
Compiler fails under -Werror on sign conversion or unused args
Use exact POSIX return types (ssize_t for read()/write(), size_t for byte counts) rather than relaxing flags.
./check.sh reports missing files in files/
Invoke ./check.sh from inside labs/lab_m01_uboot_env_bootcmd/ so relative paths to files/ resolve directly.