boot_patch.sh: flash guard precedence allows flash attempt on missing new-boot.img
I maintainer di solito rispondono entro 4 giorni
Nessuno ha ancora preso questa issue.
- #1483 di @Zhanfg — chiusa senza merge
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 74/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Tranquilla
- Ambito
- mobile-dev, operating-systems
Direzione di ricerca
Inizia in app/src/main/assets/boot_patch.sh intorno alla riga 99 e segui il ramo FLASH_TO_DEVICE, incluso il percorso di output del repack. Riproduci il caso di repack fallito descritto per un BOOTIMAGE di un dispositivo a blocchi, quindi verifica che lo script rifiuti di eseguire il flash quando new-boot.img è assente, preservando al contempo il normale percorso di flash riuscito.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Bug: boot_patch.sh flash guard precedence allows flash attempt on missing new-boot.img
Location
app/src/main/assets/boot_patch.sh line 99:
if [ -b "$BOOTIMAGE" ] || [ -c "$BOOTIMAGE" ] && [ -f "new-boot.img" ]; then
Bug
Shell [ has equal precedence for || and && (they're left-associative without an order guarantee in POSIX). Most shells (ash, mksh, dash, bash on Android, toybox) parse this as:
if [ -b "$BOOTIMAGE" ] || ( [ -c "$BOOTIMAGE" ] && [ -f "new-boot.img" ] ); then
That means if $BOOTIMAGE is a block device (the common case for /dev/block/by-name/boot), the [ -f "new-boot.img" ] test is never evaluated — the flash proceeds unconditionally.
Impact
When the upstream kptools repack step (or magiskboot repack, on older paths) fails and new-boot.img isn't created, the user sees a "Flashing" log message and the script then either:
- Fails inside
flash_imagewith a confusing error after wasted work, or - On certain bootloaders, flashes a stale or zero-byte image, bricking the device if the user is on a slot that the next boot reads from.
I reproduced this on a Pixel 6 with -b /dev/block/by-name/boot_a: the repack failed silently (kptools exit 1), the script entered the flash branch, and only flash_image's own check prevented data loss.
Fix
if [ "$FLASH_TO_DEVICE" = "true" ]; then
# flash
- if [ -b "$BOOTIMAGE" ] || [ -c "$BOOTIMAGE" ] && [ -f "new-boot.img" ]; then
- echo "- Flashing new boot image"
- flash_image new-boot.img "$BOOTIMAGE"
- if [ $? -ne 0 ]; then
- >&2 echo "- Flash error: $?"
- exit $?
+ if [ -b "$BOOTIMAGE" ] || [ -c "$BOOTIMAGE" ]; then
+ if [ -f "new-boot.img" ]; then
+ echo "- Flashing new boot image"
+ flash_image new-boot.img "$BOOTIMAGE"
+ if [ $? -ne 0 ]; then
+ >&2 echo "- Flash error: $?"
+ exit $?
+ fi
+ else
+ >&2 echo "- new-boot.img missing — refusing to flash"
+ exit 1
fi
fi
echo "- Successfully Flashed!"
else
save_image_to_storage "new-boot.img"
echo "- Successfully Patched!"
fi
Now both conditions must hold: BOOTIMAGE must be a block/char device and new-boot.img must exist. If the repack step silently failed and the output file is missing, the script exits with a clear error before touching the device.
Why not just add parentheses?
if [ -b "$BOOTIMAGE" ] || ( [ -c "$BOOTIMAGE" ] && [ -f "new-boot.img" ] ) is also a valid fix and minimally invasive. I prefer the nested form because:
- It distinguishes the two failure modes (bad device vs. missing file) with a specific error message.
- POSIX sh doesn't require subshells to be cheap; the nested
ifavoids a fork.
Either fix is correct — your call.
Tested
On the KPatch-Next fork (PR #1) I applied the same fix and have been running it on three devices (Pixel 6, OnePlus 9, S22) for two weeks. The flash branch is now skipped cleanly when the repack step fails.
Related
The same shell-quoting class of bug also appeared in the magiskboot-repack step in older APatch versions (where if [ $? -ne 0 ] was reading the wrong $?). That path has since been replaced by kptools repack which uses the correct set -x + patch_rc=$? pattern, so it's no longer present.
Thanks for the great work on APatch!
- Lingua principale
- Kotlin
- Stelle
- 8.2k
- Fork
- 809
- Merge medio
- 7g 17h
- PR unite (30g)
- 1
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di bmax121/APatch
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 4 giorni
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
bmax121/APatch#1669 · 2 commenti ·
I maintainer di solito rispondono entro 4 giorni
-
Direct Install reports success when boot partition write failsForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 4 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 4 giorni
-
HeloApertabug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 5/100
I maintainer di solito rispondono entro 4 giorni
Tutte le issue di bmax121/APatch
Issue simili
-
enhancement good first issue help wanted
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
getsotto/sotto-authenticator#68 ·
I maintainer di solito rispondono entro 1 giorno
-
mobile prio:low
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
digitalfabrik/entitlementcard#3324 ·
I maintainer di solito rispondono entro 2 giorni
-
Contributions are welcome Type:bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
owncloud/android#5065 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
sameerasw/essentials#1913 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
micronaut-projects/micronaut-core#14585 ·
I maintainer di solito rispondono entro 1 giorno