MBR/GPT Sector Offsets, Removable-Disk Guards & Numeric-Owner Rootfs Archives

One wrong letter in dd of=/dev/sda wipes the workstation OS, omitting seek=1 or starting Partition 1 below LBA 2048 overwrites the MBR or FAT32 superblock on i.MX6ULL SD cards, and packing a rootfs without --numeric-owner contaminates target / with host UIDs.

20 minfree previewLinux 6.12C11glibc 2.39→ module 2 lab
What you'll learn
  • Audit target block devices via /sys/block/<dev>/removable, size, and mount tables before executing raw dd or sfdisk commands.
  • Calculate exact byte and 512-byte LBA boundaries across MBR (0x000), i.MX6ULL IVT + U-Boot (0x400), Partition 1 FAT32 (LBA 2048), and Partition 2 ext4.
  • Construct and verify POSIX ustar --numeric-owner rootfs tarballs and complete the Module 2 partition/permission guard studio.

Three Ways SD Card Flashing Goes Catastrophically Wrong

dd is affectionately nicknamed disk destroyer by kernel engineers for good reason: block devices (/dev/sda, /dev/sdb, /dev/mmcblk0) expose raw 512-byte sectors from byte 0 to the end of the drive with zero confirmation prompts. On a desktop or VM where the root OS lives on /dev/sda, running sudo dd if=u-boot.imx of=/dev/sda instantly overwrites the host's MBR/GPT partition table!

Even when you select the right USB SD reader (/dev/sdb), omitting bs=1K seek=1 writes u-boot.imx at byte 0x000 (LBA 0) instead of byte 0x400 (1 KiB, LBA 2). That single mistake erases your freshly created MBR partition table AND leaves the i.MX6ULL internal Boot ROM staring at zeros at 0x400!

Once Partition 1 (FAT32 holding zImage + .dtb) and Partition 2 (ext4 rootfs) are formatted, a third silent failure strikes during rootfs extraction: running tar -xpf rootfs.tar.xz without --numeric-owner looks up uname/gname strings from the tarball inside the host's /etc/passwd, mapping target daemon accounts onto random workstation UIDs—or worse, packing an unprivileged staging directory leaves /sbin/init and /etc/sudoers owned by UID 1000 on the board!

fig_m02_l03_a.svg — Leaving LBA 2..2047 (1,023 KiB from 0x400 to 0xFFFFF)…

Figure 2.3a. Leaving LBA 2..2047 (1,023 KiB from 0x400 to 0xFFFFF) unpartitioned lets raw U-Boot coexist cleanly alongside standard MBR partitions.

mental model: An i.MX6ULL SD card is laid out like a hardcover reference book: page 0 (LBA 0, 0x000) is the Table of Contents (MBR listing where Chapter 1 and Chapter 2 begin), pages 2..2047 (0x400..0xFFFFF) are a dedicated preface reserved for the silicon Boot ROM's IVT and U-Boot binary, and Chapter 1 (Partition 1 FAT32) begins strictly at page 2048 (1 MiB). Forgetting seek=1 glues the preface right over the Table of Contents, while starting Chapter 1 on page 63 lets a 700 KiB preface bleed straight across Chapter 1's opening pages!

Where it stops working: Not all SoCs sample LBA 2 (1 KiB): i.MX8M moves the Boot ROM offset to 32 KiB or 33 KiB so a full 34-sector (17 KiB) GUID Partition Table (GPT header at LBA 1 + 128 partition entries across LBA 2..33) can sit below U-Boot—whereas on i.MX6ULL, standard GPT entries at LBA 2 collide directly with 0x400 unless GPT entry array LBA is relocated or classic MBR is used!

definition (Image Vector Table (IVT @ 0x400) and POSIX ustar Numeric-Owner Header): On power-on reset in SD/eMMC boot mode, the i.MX6ULL internal Boot ROM reads byte offset 0x400 (LBA 2), checking for a 32-byte Image Vector Table (IVT) header starting with tag byte 0xD1 (0x402000D1 or 0x412000D1) followed by DCD DDR3 init commands and U-Boot. Separately, every file in a POSIX.1-1988 ustar .tar archive carries a 512-byte header containing octal ASCII uid (offset 108) and gid (offset 116) plus string uname (offset 265) and gname (offset 297). By default, GNU tar prefers uname/gname strings; passing --numeric-owner forces tar to ignore string names and honor raw 0:0 integers.

ast_m02_l03.svg — Check removable==1, start p1 >= LBA 2048, flash with…step 1/4
  1. Step 1. Gate every raw flash script on /sys/block/<dev>/removable == 1 and zero active /proc/mounts entries.
  2. Step 2. Script deterministic partition boundaries with sfdisk starting Partition 1 at LBA 2048 (1 MiB) so U-Boot never overlaps FAT32.
  3. Step 3. Flash u-boot-dtb.imx at byte 0x400 (bs=1K seek=1) with conv=fsync so unplugging the card never truncates buffered sectors.
  4. Step 4. Unpack the target rootfs onto mounted Partition 2 with --numeric-owner and run sync + umount before ejecting.

Figure 2.3b. Check removable==1, start p1 >= LBA 2048, flash with bs=1K seek=1, and --numeric-owner.

Exact MBR + IVT Byte Offsets, GPT LBA-2 Collision & ustar Checksums

Let us verify the exact byte arithmetic on an i.MX6ULL bootable SD card. LBA 0 spans 0x000..0x1FF (512 B): the 4 primary partition entries occupy 0x1BE..0x1FD (64 B), where Partition 1's 32-bit little-endian starting LBA sits at 0x1BE + 8 = 0x1C6, followed by MBR signature 0x55, 0xAA at 0x1FE..0x1FF.

LBA 1 (0x200..0x3FF) is unused on MBR, whereas standard GPT places its header at LBA 1 and partition entries across LBA 2..33, colliding straight into 0x400! Starting Partition 1 at LBA 2048 (2048 * 512 = 1,048,576 = 0x100000) gives u-boot-dtb.imx 1,047,552 bytes (1,023 KiB) of headroom above 0x400.

Inside a .tar rootfs archive, each 512-byte ustar block stores magic "ustar" at offset 257, uid (0000000\0) at 108, gid (0000000\0) at 116, and chksum at 148..155. Per POSIX.1-1988, the header checksum equals the unsigned sum of all 512 header bytes when chksum[0..7] is filled with eight ASCII spaces (0x20). Packing a staging folder via tar --numeric-owner --owner=0 --group=0 -cpJf rootfs.tar.xz -C stage . zeroes both uid/gid and uname/gname cleanly without needing sudo chown on the host!

fig_m02_l03_b.svg — Byte-exact arithmetic proves 0x400 + uboot_bytes <=…

Figure 2.3b. Byte-exact arithmetic proves 0x400 + uboot_bytes <= p1_lba_start * 512 and ustar uid/gid == 0 before hardware flashing.

OperationDangerous CommandSafe Production CommandFailure Prevented
Target device checkBlind DEV=/dev/sdaVerify /sys/block/${DEV}/removable == 1 & lsblk -o NAME,RM,SIZE,MOUNTPOINTSPrevents overwriting host OS NVMe/SATA disk
Flash U-Boot (i.MX6ULL)dd if=u-boot.imx of=/dev/sdbdd if=u-boot-dtb.imx of=/dev/sdb bs=1K seek=1 conv=fsyncPreserves LBA 0 MBR (seek=1) and flushes dirty cache (conv=fsync)
Pack staged rootfstar -cpJf rootfs.tar.xz -C stage .tar --numeric-owner --owner=0 --group=0 -cpJf rootfs.tar.xz -C stage .Stamps UID 0:0 without leaking host UID 1000
Unpack onto SD ext4sudo tar -xpf rootfs.tar.xz -C /mnt/p2sudo tar --numeric-owner -xpf rootfs.tar.xz -C /mnt/p2 && syncAvoids remapping target names to host /etc/passwd UIDs
learner@ksa: ~/gl-e00/m02-l03
learner@ksa:~/gl-e00/m02-l03$ bat -H 8 -H 14 flash_sdcard_safe.sh File: flash_sdcard_safe.sh    #!/usr/bin/env bash    set -euo pipefail        DEV_NAME="${1:?Usage: $0 sdX (e.g. sdb)}"    SYS_BLK="/sys/block/${DEV_NAME}"        # 1. Mandatory host-disk destruction guards  [[ "$(cat "${SYS_BLK}/removable")" == "1" ]] || { echo "FATAL: ${DEV_NAME} is NOT removable!" >&2; exit 1; }    ! grep -Eq "^/dev/${DEV_NAME}[0-9p]*" /proc/mounts || { echo "FATAL: /dev/${DEV_NAME} has mounted partitions!" >&2; exit 1; }        # 2. Partition starting at LBA 2048 (1 MiB) and flash U-Boot at 1 KiB (0x400)    sfdisk "/dev/${DEV_NAME}" <<'EOF'    label: dos  start=2048, size=131072, type=c, bootable    start=133120, type=83    EOF    dd if=u-boot-dtb.imx of="/dev/${DEV_NAME}" bs=1K seek=1 conv=fsync status=progress
  1. Refuses non-removable SATA/NVMe disks and mounted filesystems before sfdisk or dd can touch a single sector.
  2. start=2048 places Partition 1 at 1 MiB (0x100000), leaving LBA 2..2047 clear for dd bs=1K seek=1.
dd(1) at a glance — GL-E00
dd(1)                       GL-E00 Quick Reference                       dd(1) NAME       dd, sfdisk, tar - raw sector flashing, MBR partitioning, and       numeric-owner archives SYNOPSIS       dd if=u-boot-dtb.imx of=/dev/sdX bs=1K seek=1 conv=fsync tar       --numeric-owner --owner=0 --group=0 -cpJf rootfs.tar.xz -C _stage .       sudo tar --numeric-owner -xpf rootfs.tar.xz -C /mnt/p2 && sync CRITICAL FLAGS       seek=N Skip N output blocks (bs=1K seek=1 -> byte 0x400 = LBA 2 on       i.MX6ULL). conv=fsync Flush kernel block page cache to physical SD/USB       media before dd exits. --numeric-owner Ignore uname/gname strings;       store/restore raw numeric UID/GID. Kernel Systems Academy                    Lesson 2.3                     dd(1) GL-E00 quick reference · full manual page: man 1 dd 

Auditing MBR/IVT Overlap and ustar Numeric-Owner Headers in C11

In verify.c, we synthesize a byte-accurate 512-byte MBR (0x55AA signature + Partition 1 starting LBA in little-endian at 0x1C6) alongside a 512-byte POSIX ustar header in ISO C11. Our auditor checks /sys/block removable flags, verifies whether 0x400 + uboot_size collides with p1_lba_start * 512 (catching legacy LBA 63 overlap on a 650 KiB U-Boot image!), and computes the exact ustar space-filled header checksum while enforcing uid == 0, gid == 0, and empty uname/gname.

learner@ksa: ~/gl-e00/m02-l03
learner@ksa:~/gl-e00/m02-l03$ bat -H 16 -H 45 verify.c File: verify.c    #define _POSIX_C_SOURCE 200809L    #include <stdint.h>    #include <stdio.h>    #include <stdlib.h>    #include <string.h>        static int check_mbr_uboot_fit(const unsigned char mbr[512], int is_removable,                                   uint32_t uboot_bytes) {        if (!is_removable) return -1;        if (mbr[510] != 0x55U || mbr[511] != 0xAAU) return -2;        uint32_t p1_lba = (uint32_t)mbr[0x1C6] |                          ((uint32_t)mbr[0x1C7] << 8) |                          ((uint32_t)mbr[0x1C8] << 16) |                          ((uint32_t)mbr[0x1C9] << 24);        if (p1_lba < 2048U) return -3;      uint64_t uboot_end = 0x400ULL + (uint64_t)uboot_bytes;        uint64_t p1_byte_start = (uint64_t)p1_lba * 512ULL;        return (uboot_end <= p1_byte_start) ? 0 : -4;    }        static void build_ustar_hdr(unsigned char h[512], const char *name,                                unsigned int uid, const char *uname_str) {        memset(h, 0, 512);        snprintf((char *)(h + 0), 100, "%s", name);        snprintf((char *)(h + 100), 8, "%07o", 0755U);        snprintf((char *)(h + 108), 8, "%07o", uid);        snprintf((char *)(h + 116), 8, "%07o", 0U);        snprintf((char *)(h + 124), 12, "%011o", 0U);        h[156] = '0';        memcpy(h + 257, "ustar", 6);        memcpy(h + 263, "00", 2);        snprintf((char *)(h + 265), 32, "%s", uname_str);        memset(h + 148, ' ', 8);        unsigned int sum = 0;        for (int i = 0; i < 512; ++i) sum += h[i];        snprintf((char *)(h + 148), 7, "%06o", sum);        h[154] = '\0';        h[155] = ' ';    }        static int verify_ustar_numeric_root(const unsigned char h[512]) {        if (memcmp(h + 257, "ustar", 5) != 0) return -1;        unsigned int expected = (unsigned int)strtoul((const char *)(h + 148), NULL, 8);        unsigned int sum = 0;      for (int i = 0; i < 512; ++i) {            sum += (i >= 148 && i < 156) ? (unsigned char)' ' : h[i];        }        if (sum != expected) return -2;        unsigned long uid = strtoul((const char *)(h + 108), NULL, 8);        unsigned long gid = strtoul((const char *)(h + 116), NULL, 8);        if (uid != 0UL || gid != 0UL) return -3;        if (h[265] != '\0') return -4; /* Host username leaked without --numeric-owner */        return 0;    }        int main(void) {        unsigned char mbr_ok[512] = {0}, mbr_lba63[512] = {0};        mbr_ok[510] = 0x55U; mbr_ok[511] = 0xAAU;        mbr_ok[0x1C6] = 0x00U; mbr_ok[0x1C7] = 0x08U; /* 2048 LE = 0x00000800 */            mbr_lba63[510] = 0x55U; mbr_lba63[511] = 0xAAU;        mbr_lba63[0x1C6] = 63U; /* Legacy LBA 63 = 31.5 KiB */            uint32_t uboot_sz = 650U * 1024U;        int rc_fixed_sda = check_mbr_uboot_fit(mbr_ok, 0, uboot_sz);        int rc_lba63 = check_mbr_uboot_fit(mbr_lba63, 1, uboot_sz);        int rc_lba2048 = check_mbr_uboot_fit(mbr_ok, 1, uboot_sz);            unsigned char tar_good[512], tar_leaked[512];        build_ustar_hdr(tar_good, "sbin/init", 0U, "");        build_ustar_hdr(tar_leaked, "sbin/init", 1000U, "developer");        int rc_tar_ok = verify_ustar_numeric_root(tar_good);        int rc_tar_bad = verify_ustar_numeric_root(tar_leaked);            printf("sdcard_guard: fixed_sda=%d lba63_overlap=%d lba2048_ok=%d\n",               rc_fixed_sda, rc_lba63, rc_lba2048);        printf("ustar_guard: numeric_uid0=%d host_uid1000_leak=%d\n",               rc_tar_ok, rc_tar_bad);            return (rc_fixed_sda == -1 && rc_lba63 == -3 && rc_lba2048 == 0 &&                rc_tar_ok == 0 && rc_tar_bad == -3) ? 0 : 1;    }
  1. Checks 0x400 + uboot_bytes <= p1_lba * 512 so raw U-Boot never clobbers Partition 1's FAT32 superblock.
  2. Recomputes the POSIX.1-1988 ustar checksum with bytes 148..155 treated as 0x20 spaces and rejects non-zero UIDs.

Confirming Sector Boundary & ustar Header Rejections

Review the execution output: fixed non-removable disks (sda) fail immediately (-1), legacy LBA 63 partitioning fails (-3), and LBA 2048 (1 MiB) accommodates a 650 KiB U-Boot image cleanly (0). On the archive side, sbin/init packed with UID 0 and empty uname passes (0), whereas a host-contaminated UID 1000 header is rejected (-3).

learner@ksa: ~/gl-e00/m02-l03
learner@ksa:~/gl-e00/m02-l03gcc -std=c11 -Wall -Wextra -Werror verify.c -o verifylearner@ksa:~/gl-e00/m02-l03./verifysdcard_guard: fixed_sda=-1 lba63_overlap=-3 lba2048_ok=0ustar_guard: numeric_uid0=0 host_uid1000_leak=-3learner@ksa:~/gl-e00/m02-l03
strace — learner@ksa: ~/gl-e00/m02-l03
learner@ksa:~/gl-e00/m02-l03strace -e trace=write,exit_group ./verifywrite(1, "sdcard_guard: fixed_sda=-1 lba63"..., 106) = 106exit_group(0)                           = ?+++ exited with 0 +++

Page-Cache Eject Corruption & GPT-vs-IVT Collisions

With removable-disk sysfs checks, LBA 2048 alignment, dd bs=1K seek=1 conv=fsync, and tar --numeric-owner, SD and eMMC provisioning becomes deterministic and repeatable. Proceed to labs/lab_m02_perm_partition_guard/ to complete the Module 2 studio.

Pulling the SD Card Right After dd or cp Finishes Without conv=fsync / sync:

  • Symptom: dd prints 680240 bytes copied and returns to the shell prompt in 0.02 s, you pull the USB SD reader out, and the board refuses to boot or prints EXT4-fs error: bad magic.
  • Root Cause: Without conv=fsync (or oflag=direct / sync), write(2) copies sectors into the Linux kernel dirty page cache in host RAM while USB mass-storage writeback is still in flight!
  • Fix: Always append conv=fsync to raw dd invocations, and after copying files to mounted FAT32/ext4 partitions run sync && sudo umount /dev/sdX* and wait for umount to return before unplugging.

Formatting an i.MX6ULL Boot SD Card With Default GPT (gdisk / parted mklabel gpt):

  • Symptom: Partitioning succeeds in gdisk, then running dd if=u-boot-dtb.imx of=/dev/sdb bs=1K seek=1 causes fdisk -l to report GPT PMBR size mismatch / corrupt primary GPT table!
  • Root Cause: Standard GPT places its Primary Header at LBA 1 (0x200) and its 128-entry Partition Array across LBA 2..33 (0x400..0x43FF)—the exact 0x400 byte offset where i.MX6ULL requires its IVT!
  • Fix: Use MBR (label: dos in sfdisk) on i.MX6ULL SD/eMMC boot media, or if GPT is mandatory, relocate the GPT partition entry array (--move-main-table in sgdisk -j 2048) past U-Boot.

Guided Practice

Calculate the maximum u-boot-dtb.imx file size (in bytes and KiB) that fits between byte 0x400 (1 KiB) and Partition 1 at LBA 2048 (1 MiB) when U-Boot also stores a 128 KiB persistent environment block at byte offset 0xC0000 (768 KiB). Check your formula in verify.c!

Independent Practice

Complete labs/lab_m02_perm_partition_guard/ by repairing starter/core.c so all 5 TAP version 13 checks pass (sh check.sh) and record CLOCK_MONOTONIC percentiles with ./files/lab_cli --bench.

Verification Checks

quiz · 1 of 3

Why must dd if=u-boot-dtb.imx of=/dev/sdb bs=1K seek=1 conv=fsync include bs=1K seek=1 when flashing an i.MX6ULL SD card?

quiz · 2 of 3

An unprivileged engineer stages a target rootfs in /home/alex/rootfs (UID 1000:1000) and packs it with plain tar -cpJf rootfs.tar.xz -C /home/alex/rootfs .. What goes wrong on the target board?

quiz · 3 of 3

Why does formatting an i.MX6ULL boot SD card with a default GPT partition table break as soon as dd bs=1K seek=1 writes u-boot-dtb.imx?

SD/eMMC Partition-Offset, Sticky Unlink & ustar Rootfs Verifier

env: host

1. Motivation

Automated BSP image-creation pipelines must guarantee three non-negotiable safety invariants: never write raw bootloaders onto non-removable host disks or across Partition 1's FAT32 boundary (0x400 + sizeof(u-boot.imx) <= p1_lba * 512), enforce VFS sticky/triad permissions accurately, and ensure staged rootfs tarballs carry clean UID 0:0 --numeric-owner headers.

2. Learning objectives

  • Guard raw block writes by validating removable == 1, mounted_partitions == 0, and exact 0x400 + uboot_bytes <= p1_lba_start * 512 bounds.
  • Implement Linux VFS first-match credential triads plus parent w+x (03) and 01000 sticky unlink authorization.
  • Verify 512-byte POSIX ustar tar headers for space-filled checksums, octal 0:0 ownership, and empty uname/gname strings.

3. Environment requirements

All automated verification steps run in unprivileged user space on a POSIX Linux host using gcc -std=c11 -Wall -Wextra -Werror and clock_gettime(CLOCK_MONOTONIC). Recorded transcripts in captures.json were generated on x86_64 running kernel 7.0.0-42-generic and prove protocol, state-machine, and memory invariants only. Physical bus timing, DMA cache coherency, and board peripheral behavior require target validation on Linux kernel 6.12 LTS (ARMv7-A Cortex-A7).

4. Required software and hardware

  • Host workstation: gcc (-std=c11 -Wall -Wextra -Werror), GNU make, ar (binutils), and POSIX /bin/sh.
  • Target environment (optional hardware step, Section 13): MicroSD card (>= 8 GiB) inspected via /sys/block/sdX/removable and booted on an i.MX6ULL ARMv7-A board (BOOT_MODE = 10 internal SD boot) to confirm IVT load at 0x400 and FAT32 zImage/.dtb mount.

5. Complete executable commands

learner@ksa: ~/gl-e00/m02-l03
learner@ksa:~/gl-e00/m02-l03$ bat script.sh File: script.sh    make -B -C files    ./files/lab_cli    ./files/lab_cli --bench    sh check.sh

6. Annotated source code

files/core.c enforces the production invariants that starter/core.c breaks:

learner@ksa: ~/gl-e00/m02-l03
learner@ksa:~/gl-e00/m02-l03$ bat example.c File: example.c    uint64_t uboot_end_byte = 0x400ULL + (uint64_t)uboot_bytes;    uint64_t p1_start_byte = (uint64_t)p1_lba_start * 512ULL;    if (uboot_end_byte > p1_start_byte) {        return -7; /* Raw U-Boot image overlaps Partition 1 FAT32 superblock */    }

starter/core.c seeds three subtle bugs: (1) ksa_guard_sdcard_layout() forgets the 0x400 (1 KiB) IVT offset, allowing a 1024 KiB U-Boot binary to overwrite the first 1 KiB of Partition 1 (FAT32 boot sector at 0x100000!); (2) ksa_check_sticky_unlink() masks 04000U (setuid) instead of 01000U (sticky); and (3) ksa_verify_ustar_root() skips checking uname/gname at bytes 265 and 297.

7. Expected output

Passing files/ run (sh check.sh, exit code 0):

learner@ksa: ~/gl-e00/m02-l03
learner@ksa:~/gl-e00/m02-l03$ bat listing.txt File: listing.txt    TAP version 13    1..5    ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror    ok 2 - archives core.o into libcore.a and links independent TAP verifier    ok 3 - enforces removable==1, 0 mounts, and 0x400 + uboot_bytes <= p1_lba_start * 512    ok 4 - enforces VFS first-match triad, directory w+x (03), and 01000 sticky unlink ownership    ok 5 - validates POSIX ustar 512-byte checksum, octal uid/gid == 0, and empty uname/gname    # pass 5/5

CLI verification output (./files/lab_cli):

learner@ksa: ~/gl-e00/m02-l03
learner@ksa:~/gl-e00/m02-l03$ bat listing.txt File: listing.txt    sdcard_guard: sda_fixed=-2 sdb_ok=0 sdb_1200k_overlap=-7    sticky_unlink: stranger=0 file_owner=1    ustar_verify: numeric_root=0 leaked_uid1000=-4

Defective starter/ run (exit code 1): emits FAIL: diagnostics on the broken contract assertions.

8. Explanation of observed behavior

Look closely at boundary test case 1 in tests.c: when p1_lba_start = 2048 (0x100000 = 1,048,576 B), a 1,023 KiB (1,047,552 B) U-Boot image starting at 0x400 (1,024 B) ends at exact byte 1,048,576 (rc == 0), whereas a 1,024 KiB image overflows by 1,024 bytes into Partition 1 (rc == -7)! Restoring all three checks turns all 5 TAP version 13 assertions green.

9. Experiment variations

  • Near transfer: Extend ksa_guard_sdcard_layout() to accept a persistent U-Boot environment region (env_offset = 0xC0000, env_size = 0x20000) and verify that 0x400 + uboot_bytes <= env_offset so growing U-Boot binaries never overwrite saved board MAC addresses or bootargs.
  • Far transfer: Write a host CLI tool that opens an actual .tar (uncompressed) rootfs image, iterates 512-byte ustar headers (advancing by ((size + 511) / 512) * 512 data blocks), and reports any entry with non-zero UID/GID or host-absolute symlink targets (linkname at offset 157).

10. Performance measurements

Recorded host benchmark (./files/lab_cli --bench, CLOCK_MONOTONIC, warmup=5, N=40 batches):

learner@ksa: ~/gl-e00/m02-l03
learner@ksa:~/gl-e00/m02-l03$ bat listing.txt File: listing.txt    BENCH lab_m02_perm_partition_guard clock=CLOCK_MONOTONIC warmup=5 samples=40 p50_ns=863 p95_ns=1210 p99_ns=1210 sink=0

Host CLOCK_MONOTONIC percentiles reflect CPU L1-cached arithmetic and 512-byte checksum loops (~hundreds of nanoseconds), whereas physical SDHC/eMMC sector writes take milliseconds due to NAND block erase cycles.

11. Troubleshooting

SymptomRoot causeFix
not ok 3 - enforces removable==1... (ovl=0 instead of < 0)uboot_end_byte omitted 0x400ULL, misjudging a 1024 KiB image against LBA 2048.Compute 0x400ULL + (uint64_t)uboot_bytes and reject > (uint64_t)p1_lba_start * 512ULL.
not ok 4 - enforces VFS first-match triad... (str=1)Checked 04000U (S_ISUID) instead of 01000U (S_ISVTX sticky bit).Test (dir_mode & 01000U) != 0U in ksa_check_sticky_unlink().
not ok 5 - validates POSIX ustar 512-byte checksum...Disabled check on hdr[265] (uname) and hdr[297] (gname).Return -5 whenever `hdr[265] != '\0'hdr[297] != '\0'`.

12. Guided questions

  1. Why does a 1,024 KiB (1 MiB) u-boot-dtb.imx overflow Partition 1 when Partition 1 starts at 1 MiB (LBA 2048)?
  • *Hint 1:* At what byte offset on the SD card does dd bs=1K seek=1 begin writing u-boot-dtb.imx?
  • *Hint 2:* Because writing starts at 0x400 (1 KiB), a 1,024 KiB image spans 0x400..0x1003FF, overwriting the first 1,024 bytes (2 sectors) of Partition 1 at 0x100000!
  1. Why does the POSIX.1-1988 ustar checksum loop treat bytes 148..155 as ASCII ' ' (0x20) instead of 0x00?
  • *Hint 1:* What characters occupy hdr[148..155] at the exact moment tar computes the sum before formatting the octal checksum string into that same field?
  • *Hint 2:* Before writing the octal digits into hdr[148..155], a compliant writer pre-fills the 8-byte checksum field with spaces (0x20), sums all 512 bytes, and then writes the octal sum into hdr[148..154].

13. Independent challenges

Add ksa_parse_mbr_table(const unsigned char mbr[512], uint32_t *p1_lba, uint32_t *p2_lba) to verify 0x55AA, ensure Partition 1 (0x1BE) has type 0x0C (FAT32 LBA), Partition 2 (0x1CE) has type 0x83 (Linux), and confirm p1_lba + p1_sectors <= p2_lba (zero partition overlap).

14. Solutions and explanations

The reference files/core.c adds 0x400ULL to uboot_bytes before comparing against p1_lba_start * 512ULL, tests 01000U for sticky directory unlinks after first-match triad w+x evaluation, and verifies both 0:0 numeric IDs and empty uname/gname fields in ustar headers.

Run locally in your terminal and check off each verification milestone.

Not started

Key takeaways

  • Before raw SD card writes, scripts must verify /sys/block/<dev>/removable == 1, zero mounted partitions in /proc/mounts, and sane capacity bounds so dd never targets host /dev/sda or /dev/nvme0n1.
  • On i.MX6ULL SD/eMMC boot media, LBA 0 (0x000..0x1FF) holds the 512-byte MBR, LBA 2 (byte 0x400, 1 KiB) holds the raw Boot ROM IVT + U-Boot image (dd bs=1K seek=1), and Partition 1 starts at LBA 2048 (1 MiB).
  • Partition 1 (FAT32, 0x0C) stores zImage and Device Tree .dtb files read by U-Boot, while Partition 2 (ext4, 0x83) stores the persistent Linux root filesystem.
  • Packing and extracting target rootfs archives with tar --numeric-owner --owner=0 --group=0 -cpf preserves raw UID/GID 0:0 integers in POSIX ustar headers without host /etc/passwd name contamination.
Progress is saved on this device.
up next · module 2 labLab: SD/eMMC Partition-Offset, Sticky Unlink & ustar Rootfs Verifier (60 min)
→ module 2 lab

search — fzf

0/0 ───────────────────────────────────────────

keyboard shortcuts — less

/
search
f
focus mode
[
previous
]
next
?
help
Esc
close

figure