From e87244092af1976adac9ded11197e1084f844e42 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Wed, 18 Nov 2026 02:42:49 +0000 Subject: [PATCH] extract-vendor-firmware: say why build.prop can't be read When debugfs can't copy build.prop out of an ext4 vendor.img, the tool refused with "build.prop can't be read from vendor.img; use -f to override" and dropped what debugfs had said, so a missing file, a full work directory and a damaged image all read the same. #795 gave extract-vendor-chroot and extract-fingerprint-vendor debugfs's reason on this refusal; this gives it to the third extractor, in the same words: error: build.prop can't be read from vendor.img: dump: No space left on device while writing file; use -f to override dfs() now keeps debugfs's last stderr line in $reason, none when that line is only its banner, and the refusal adds it with #795's ${reason:+: $reason} before "; use -f to override". The reason is the last line, as in #795, rather than the line dfs() stopped on: with real debugfs 1.47.2 the two are the same line for a missing build.prop, an empty one, a full work directory, an image cut short and a damaged inode, and differ only for a damaged superblock, where the first line names the work directory's path ("The ext2 superblock is corrupt while trying to open .../vendor.img") and the last is "dump: Filesystem not open", what the other two tools print. A lookup error ends in a blank, which is dropped here because text follows it. The EROFS path has no debugfs and keeps its words; $reason is set empty before the format check, so nothing from the environment reaches it. -f, the exit status, the staging generation and every other refusal are unchanged; the map loop's, the registry's and the camera APEX's refusals are left as they are. Tested: tests/extract-vendor-firmware checks the whole refusal line for no build.prop and a failed copy (its stand-in debugfs now prints real debugfs's words for both), no reason for an empty build.prop or on EROFS, and on real ext4 images without build.prop or with a damaged superblock the line ends with what the host's debugfs says; with base's tool 4 of those checks fail, and only they. Each new line of the fix, undone, fails the suite, except the one that carries base's "|| return 1" over (debugfs exiting non-zero), which no check covered before either. The tool ran through its own command line on made-up ext4 image zips with the host's debugfs, before and after. tools/pr-check origin/main passes, and tests/all with a stand-in fastboot on PATH (main's tests/install-slot-descriptor needs one, and fails without it on main's own tree too). Not tested: Google factory images, a sparse or super.img vendor, debugfs exiting non-zero, and other e2fsprogs versions. Co-Authored-By: Claude Opus 5.5 --- tests/extract-vendor-firmware | 67 ++++++++++++++++++++++++----------- tools/extract-vendor-firmware | 13 ++++--- 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/tests/extract-vendor-firmware b/tests/extract-vendor-firmware index 4703432ef..ffb79e7cf 100755 --- a/tests/extract-vendor-firmware +++ b/tests/extract-vendor-firmware @@ -15,15 +15,17 @@ # missing camera APEX) leaves the previous outputs and the -c file byte # for byte, so an older file can't pass for a missing one; an image whose # build.prop is missing, empty or not copied is refused, as it can't be -# checked, and staged with -f; a -c that is a directory or inside the -# outputs is refused; an -o that resolves to / is refused before anything -# is written; and a move that fails while publishing puts the previous -# outputs back. With mkfs.ext4 and debugfs on the host, the same on a real -# ext4 image whose registry belongs to root, work and output paths with a -# space and a double quote in them, a copy that a file size limit cuts -# short, and an image without build.prop. EVF=path -# runs another copy of the tool (a negative control), so every check runs -# and the failures are counted rather than stopping at the first. +# checked, with debugfs's reason when it gave one, and staged with -f; a +# -c that is a directory or inside the outputs is refused; an -o that +# resolves to / is refused before anything is written; and a move that +# fails while publishing puts the previous outputs back. With mkfs.ext4 +# and debugfs on the host, the same on a real ext4 image whose registry +# belongs to root, work and output paths with a space and a double quote +# in them, a copy that a file size limit cuts short, and images without +# build.prop or with a damaged superblock, whose refusals end with +# debugfs's own last line. EVF=path runs another copy of the tool (a +# negative control), so every check runs and the failures are counted +# rather than stopping at the first. set -euo pipefail cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." repo=$PWD @@ -53,7 +55,7 @@ if [[ -f $FAKE_VENDOR/.fail-dump && $src == "/$(cat "$FAKE_VENDOR/.fail-dump")" [[ $cmd == rdump && -f $FAKE_VENDOR/.fail-rdump ]]; then if [[ $cmd == dump ]]; then echo partial > "$out" - echo "dump_file: File too large while writing file \"$src\"" >&2 + echo "dump: No space left on device while writing file" >&2 else # after many real notes, an error whose file name holds their words mkdir -p "$out/${src##*/}" && echo partial > "$out/${src##*/}/part.json" for n in $(seq 800); do @@ -68,7 +70,7 @@ case $cmd in rdump) [[ -d $FAKE_VENDOR$src ]] && cp -r "$FAKE_VENDOR$src" "$out/" && echo "dump_file: Operation not permitted while changing ownership of $out/${src##*/}/x" >&2 && echo "rdump: Operation not permitted while changing ownership of $out/${src##*/}" >&2 ;; -esac || echo "$cmd: File not found by ext2_lookup while looking up $src" >&2 +esac || echo "$src: File not found by ext2_lookup " >&2 # with its trailing blank exit 0 EOF cat > "$t/bin/fsck.erofs" <<'EOF' @@ -184,6 +186,12 @@ refuses() { # NAME EXPECTED-ERROR VENDOR IMG ARGS... [[ $(snap "$o") == "$before" ]] || { diff <(echo "$before") <(snap "$o") || true; fail "$name: the previous outputs stay as they were"; return; } pass "$name: refused, the previous outputs untouched" } +# says NAME LINE: the last run's error line is LINE, byte for byte +says() { + local got + got=$(sed -n '/^error: /p' "$t/log") + if [[ $got == "$2" ]]; then pass "$1: says $2"; else printf '%s\n' "$got"; fail "$1: says $2"; fi +} # --- A fresh run --------------------------------------------------------------- o=$t/fresh n=$failed @@ -282,10 +290,18 @@ pass_since "$n" "a registry short write under a file size limit: refused, the pr rm -rf "$t/v2-noprop"; cp -r "$t/v2" "$t/v2-noprop"; rm "$t/v2-noprop/build.prop" rm -rf "$t/v2-emptyprop"; cp -r "$t/v2" "$t/v2-emptyprop"; : > "$t/v2-emptyprop/build.prop" rm -rf "$t/v2-propfail"; cp -r "$t/v2" "$t/v2-propfail"; echo build.prop > "$t/v2-propfail/.fail-dump" +# The refusal passes on debugfs's last line, its reason (a lookup error's +# trailing blank dropped); only its banner adds none, and nothing reaches +# the EROFS refusal, not even a "reason" in the environment +msg="error: build.prop can't be read from vendor.img" use_f="; use -f to override" refuses "no build.prop (ext4)" "build.prop can't be read from vendor.img" "$t/v2-noprop" "$t/ext4.img" -refuses "no build.prop (EROFS)" "build.prop can't be read from vendor.img" "$t/v2-noprop" "$t/erofs.img" +says "no build.prop (ext4)" "$msg: /build.prop: File not found by ext2_lookup$use_f" +reason="a stale reason" refuses "no build.prop (EROFS)" "build.prop can't be read from vendor.img" "$t/v2-noprop" "$t/erofs.img" +says "no build.prop (EROFS)" "$msg$use_f" refuses "an empty build.prop" "build.prop can't be read from vendor.img" "$t/v2-emptyprop" "$t/ext4.img" +says "an empty build.prop" "$msg$use_f" refuses "a build.prop whose copy fails" "build.prop can't be read from vendor.img" "$t/v2-propfail" "$t/ext4.img" +says "a build.prop whose copy fails" "$msg: dump: No space left on device while writing file$use_f" o=$t/noprop-forced seed "$o" if run "$t/v2-noprop" "$t/ext4.img" -f -o "$o" && [[ $(listed "$o") == "$CHEETAH_FILES" ]]; then @@ -419,14 +435,25 @@ if realdfs=$(command -v debugfs) && mkfs.ext4 -q -F -d "$t/v2" "$t/real.img" 8M else pass "real ext4: a missing file with an older copy in OUT is refused, the previous outputs untouched" fi - before=$(snap "$o") - if "$EVF" -w "$t/work" -o "$o" "$t/real-noprop.img" > "$t/log" 2>&1; then - fail "real ext4: an image without build.prop is refused" - elif ! grep -qF "build.prop can't be read from vendor.img" "$t/log" || [[ $(snap "$o") != "$before" ]]; then - cat "$t/log"; fail "real ext4: an image without build.prop is refused, the previous outputs untouched" - else - pass "real ext4: an image without build.prop is refused, the previous outputs untouched" - fi + # The refusal ends with the last line the host's debugfs itself writes for + # the image: one without build.prop, and one whose superblock's block size + # is damaged (the magic intact), where debugfs first says it can't open it + cp "$t/real.img" "$t/real-sb.img" + printf '\xff\xff\xff\x7f' | dd of="$t/real-sb.img" bs=1 seek=1048 conv=notrunc status=none + for image in noprop sb; do + why=$("$realdfs" -R "dump /build.prop \"$t/probe.prop\"" "$t/real-$image.img" 2>&1 >/dev/null) + why=${why##*$'\n'} why=${why% } + before=$(snap "$o") + if [[ -z $why || $why == "debugfs "[0-9]* || -e $t/probe.prop ]]; then + fail "real ext4 $image: debugfs itself fails to copy build.prop, with a reason" + elif "$EVF" -w "$t/work" -o "$o" "$t/real-$image.img" > "$t/log" 2>&1; then + fail "real ext4 $image: build.prop that can't be read is refused" + elif [[ $(sed -n '/^error: /p' "$t/log") != "$msg: $why$use_f" || $(snap "$o") != "$before" ]]; then + cat "$t/log"; fail "real ext4 $image: refused with debugfs's last line, the previous outputs untouched" + else + pass "real ext4 $image: refused with debugfs's last line, the previous outputs untouched" + fi + done before=$(snap "$o") if PATH=$t/limited:$PATH "$EVF" -w "$t/work" -o "$o" "$t/real-big.img" > "$t/log" 2>&1; then fail "real ext4: a copy cut short by debugfs is refused" diff --git a/tools/extract-vendor-firmware b/tools/extract-vendor-firmware index d256625a5..425f2e66d 100755 --- a/tools/extract-vendor-firmware +++ b/tools/extract-vendor-firmware @@ -383,6 +383,7 @@ if [[ "$(magic_at "$img" 0)" == ed26ff3a ]]; then fi # Copy one file out of the vendor filesystem ($1, no leading /) to $2. +reason="" # why debugfs couldn't (dfs, below); EROFS gives none if [[ "$(od -An -tx2 -j 1080 -N2 "$img" | tr -d ' ')" == ef53 ]]; then need debugfs "install e2fsprogs" say "vendor.img is ext4; reading it with debugfs (no mount)" @@ -390,10 +391,14 @@ if [[ "$(od -An -tx2 -j 1080 -N2 "$img" | tr -d ' ')" == ef53 ]]; then # full disk) and says so on stderr: anything there but its banner, or # rdump's notes that it can't give the files the image's owners (as a # user), fails the copy. The copy goes to a new path, so no older file - # can stand in for it. + # can stand in for it. Its last line says why, and is kept in $reason + # for the build.prop refusal (a lookup error's trailing blank dropped). dfs() { - local err line - err=$(debugfs -R "$1" "$img" 2>&1 >/dev/null) || return 1 + local err line rc=0 + err=$(debugfs -R "$1" "$img" 2>&1 >/dev/null) || rc=1 + reason=${err##*$'\n'} reason=${reason% } + [[ $reason != "debugfs "[0-9]* ]] || reason="" # only its banner: no reason + ((rc == 0)) || return 1 while [[ -n $err ]]; do # line by line in the shell: nothing here can fail line=${err%%$'\n'*} if [[ $err == *$'\n'* ]]; then err=${err#*$'\n'}; else err=""; fi @@ -428,7 +433,7 @@ if get_file build.prop "$WORK/build.prop"; then die "vendor partition is for '${dev:-unknown}', not $DEVICE; use -f to override" say "Vendor partition: $(sed -n 's/^ro.vendor.build.fingerprint=//p' "$WORK/build.prop" | head -1)" else - [[ -n "$FORCE" ]] || die "build.prop can't be read from vendor.img; use -f to override" + [[ -n "$FORCE" ]] || die "build.prop can't be read from vendor.img${reason:+: $reason}; use -f to override" fi # The new generation: everything below is written into $new, which starts