diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f898a92..86451996 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## Unreleased + +- Fix installation on NEMESIS 1.17.1/1.17.2 when LivePatch keeps a destination + directory read-only (#477): bypass the farm during install and uninstall, with + an ordinary-remount fallback for older NEMESIS. Check all destination directories + before installing runtime files. Reinstalling the fixed package completes an + incomplete install; package and USB uninstall can remove its leftovers too. + Older media retain a newer on-unit uninstaller, and failed remounts restore + the LivePatch farms before reporting failure. + ## 1.1.0 (2026-11-28) 1.1.0-rc.10's code: only the version, this heading and the release notes changed. It is the diff --git a/install/common/install-test.sh b/install/common/install-test.sh index dd8aa7da..0fd10e19 100644 --- a/install/common/install-test.sh +++ b/install/common/install-test.sh @@ -673,4 +673,211 @@ run $P/install.sh $P has "NEMESIS's script without app_process.real refused" "app_process.real, the binary it runs, is missing, so nothing was changed" "$out" check "... /system unchanged" "$B4" "$(tree)" cp $T/wrapper.old $P/app_process + +# #477: keep all prior NEMESIS compatibility and recovery regressions above. +# Start these read-only-directory and partial-install cases from stock. +rm -f $R/system/bin/app_process.real $R/system/bin/app_process.hmgapi-stock +cp $T/newfw $R/system/bin/app_process +rm -rf $R/data/hmgapi +# The historical cases finish by restoring the original wrapper in the payload. +(cd $P && md5sum hmgapid lpatch app_process libhmgapi_hook.so hmgapi-hook.jar HmgApi.apk install.sh uninstall.sh lib.sh stock.txt app-pending.sh app-open.sh hook-check.sh boot.sh MAPS.md5 > MANIFEST) +# Directory preflight: a destination that cannot take a new file must stop the +# installer before hmgapid/lpatch go in (#477). Directory permissions simulate +# read-only mounts without privileged mounts; root skips these checks. +rm -rf $R/data/hmgapi +for d in bin lib framework etc; do + chmod a-w $R/system/$d + if [ -w $R/system/$d ]; then + chmod u+w $R/system/$d + echo "skip unwritable $d (root can write despite mode bits)" + continue + fi + B4=$(tree) + run $P/install.sh $P + check "unwritable $d: install fails" 1 $rc + has "... names the destination" "can't write /system/$d" "$out" + check "... /system unchanged" "$B4" "$(tree)" + check "... no install record" no "$([ -f $R/data/hmgapi/installed.txt ] && echo yes || echo no)" + chmod u+w $R/system/$d + rm -rf $R/data/hmgapi +done + +# Package recovery of the exact partial install from #477, using real uninstall +# code and a NEMESIS stub that requires the LivePatch bypass flag. A .real +# stock binary and untouched boot scripts must stay byte-identical. +cp $R/system/xbin/nemesis $T/nemesis.old +cat > $R/system/xbin/nemesis <<'NEM' +#!/bin/sh +echo "$*" >> "$ROOT/remount.log" +case "$*" in + 'remount rw --bypass-livepatch') + [ "$OLD_NEMESIS" != 1 ] || exit 1 + [ "$HMGAPI_TEST_FARM" != 1 ] || chmod u+w "$ROOT/system/lib" + [ "$FAIL_REMOUNT" != 1 ] ;; + 'remount rw') [ "$FAIL_REMOUNT" != 1 ] ;; + 'remount ro') + [ "$HMGAPI_TEST_FARM" != 1 ] || chmod a-w "$ROOT/system/lib" + exit 0 ;; + *) exit 1 ;; +esac +NEM +chmod +x $R/system/xbin/nemesis +# The farm stands in for the read-only /system/lib from #477. A plain rw +# remount succeeds but leaves it read-only; --bypass-livepatch removes it. +: > $R/remount.log +export HMGAPI_TEST_REMOUNT=1 HMGAPI_TEST_FARM=1 +chmod a-w $R/system/lib +if [ ! -w $R/system/lib ]; then + run $P/install.sh $P + check "LivePatch farm: install succeeds with bypass" 0 $rc + has "... requested rw bypass" "remount rw --bypass-livepatch" "$(cat $R/remount.log)" + check "... farm restored by ro remount" no "$([ -w $R/system/lib ] && echo yes || echo no)" + check "... install recorded" yes "$([ -s $R/data/hmgapi/installed.txt ] && echo yes || echo no)" + export FAIL_REMOUNT=1 + B4=$(tree); : > $R/remount.log + run $P/uninstall.sh + check "uninstall rw remount fails after dropping farm: fails safely" 1 $rc + check "... farm restored despite rw failure" no "$([ -w $R/system/lib ] && echo yes || echo no)" + has "... ro attempted on failure" "remount ro" "$(cat $R/remount.log)" + check "... /system files unchanged" "$B4" "$(tree)" + unset FAIL_REMOUNT + run $P/uninstall.sh + check "... uninstall succeeds with the farm up" 0 $rc + check "... daemon removed" no "$([ -e $R/system/bin/hmgapid ] && echo yes || echo no)" + export FAIL_REMOUNT=1 + B4=$(tree); : > $R/remount.log + run $P/install.sh $P + check "install rw remount fails after dropping farm: fails safely" 1 $rc + check "... farm restored despite rw failure" no "$([ -w $R/system/lib ] && echo yes || echo no)" + has "... ro attempted on failure" "remount ro" "$(cat $R/remount.log)" + check "... /system files unchanged" "$B4" "$(tree)" + unset FAIL_REMOUNT + # An unrecognised bypass flag must fall back, then preflight refuses the + # still-read-only folder before writing hmgapid or lpatch. + rm -rf $R/data/hmgapi; : > $R/remount.log + export OLD_NEMESIS=1 + B4=$(tree); run $P/install.sh $P + check "farm still read-only after fallback: install refused" 1 $rc + has "... names /system/lib" "can't write /system/lib" "$out" + check "... /system unchanged" "$B4" "$(tree)" + check "... no incomplete install record" no "$([ -f $R/data/hmgapi/installed.txt ] && echo yes || echo no)" + unset OLD_NEMESIS +else + echo "skip LivePatch read-only farm (root ignores directory mode bits)" +fi +chmod u+w $R/system/lib; unset HMGAPI_TEST_FARM +# Older NEMESIS has no farms and rejects the flag: the ordinary remount works. +export OLD_NEMESIS=1 +: > $R/remount.log +run $P/install.sh $P +check "older NEMESIS: install succeeds via ordinary remount fallback" 0 $rc +has "... ordinary rw requested" "remount rw +" "$(cat $R/remount.log)" +run $P/uninstall.sh +check "... uninstall succeeds via fallback too" 0 $rc +unset OLD_NEMESIS HMGAPI_TEST_REMOUNT + +partial_install() { + rm -rf $R/data/hmgapi + mkdir -p $R/data/hmgapi/stock/system/bin $R/data/hmgapi/hook + cp $T/newfw $R/system/bin/app_process.real + printf '%s\n' "$NLP" > $R/system/bin/app_process + cp $T/newfw $R/data/hmgapi/stock/system/bin/app_process + cp $P/hmgapid $R/system/bin/hmgapid; cp $P/lpatch $R/system/bin/lpatch + : > $R/remount.log +} +REPO=$PWD +# The existing P already is the payload; give the package a payload/ beside scripts. +PKG=$T/package; mkdir -p $PKG; ln -s $P $PKG/payload +# Use an absolute script path; the package's current directory selects its payload. +preuninstall() { + set +e + out=$(cd $PKG && env PATH="$T/bin:$PATH" ROOT=$R CAPCHECK=0 HMGAPI_TEST_REMOUNT=1 $SH "$REPO/install/nemesis/scripts/preuninstall.sh" 2>&1) + rc=$?; set -e +} +# Reinstalling a fixed package is the one-step recovery when the owner wants +# to keep HMG-API: complete the partial install with LivePatch's farm up. +partial_install +chmod a-w $R/system/lib +if [ ! -w $R/system/lib ]; then + export HMGAPI_TEST_REMOUNT=1 HMGAPI_TEST_FARM=1 + run $P/install.sh $P + check "partial install + read-only farm: reinstall completes" 0 $rc + check "... wrapper installed in .real" "$(md5sum < $P/app_process)" "$(md5sum < $R/system/bin/app_process.real)" + check "... NEMESIS script preserved" "$NLP" "$(cat $R/system/bin/app_process)" + check "... wrapper's stock copy is stock" "$STK" "$(md5sum < $R/system/bin/app_process.hmgapi-stock)" + check "... farm is read-only again" no "$([ -w $R/system/lib ] && echo yes || echo no)" + check "... install record exists" yes "$([ -s $R/data/hmgapi/installed.txt ] && echo yes || echo no)" + run $P/uninstall.sh + check "... uninstall after reinstall succeeds" 0 $rc + check "... stock .real restored" "$STK" "$(md5sum < $R/system/bin/app_process.real)" + check "... NEMESIS script still preserved" "$NLP" "$(cat $R/system/bin/app_process)" + unset HMGAPI_TEST_REMOUNT HMGAPI_TEST_FARM +else + echo "skip partial reinstall with read-only farm (root ignores directory mode bits)" +fi +chmod u+w $R/system/lib +for old in 0 1; do + partial_install; export OLD_NEMESIS=$old + IR=$(md5sum < $R/system/etc/install-recovery.sh); QB=$(md5sum < $R/system/etc/qb_runtime.sh) + preuninstall + check "partial install (old NEMESIS=$old): package uninstall succeeds" 0 $rc + has "... identifies incomplete install" "install is incomplete" "$out" + check "... daemon and patcher removed" no "$([ -e $R/system/bin/hmgapid ] || [ -e $R/system/bin/lpatch ] && echo yes || echo no)" + check "... stock .real preserved" "$STK" "$(md5sum < $R/system/bin/app_process.real)" + check "... LivePatch script preserved" "$NLP" "$(cat $R/system/bin/app_process)" + check "... boot scripts untouched" "$IR|$QB" "$(md5sum < $R/system/etc/install-recovery.sh)|$(md5sum < $R/system/etc/qb_runtime.sh)" + has "... bypass requested for recovery" "remount rw --bypass-livepatch" "$(cat $R/remount.log)" + has "... ordinary ro remount restores farms" "remount ro" "$(cat $R/remount.log)" +done +unset OLD_NEMESIS +for bad in missing script truncated unknown; do + partial_install + case $bad in + missing) rm $R/data/hmgapi/stock/system/bin/app_process ;; + script) printf '%s\n' "$NLP" > $R/data/hmgapi/stock/system/bin/app_process; cp $P/app_process $R/system/bin/app_process.real ;; + truncated) head -c 16 $T/newfw > $R/data/hmgapi/stock/system/bin/app_process ;; + unknown) printf '\177ELFunknown' > $R/data/hmgapi/stock/system/bin/app_process ;; + esac + B4=$(tree); preuninstall + check "partial recovery with $bad backup: fails (package must stay)" 1 $rc + check "... /system unchanged" "$B4" "$(tree)" + check "... no remount before validation" "" "$(cat $R/remount.log)" +done +partial_install; mv $PKG/payload $PKG/payload.away +B4=$(tree); preuninstall +check "partial recovery without package payload: fails" 1 $rc +check "... /system unchanged" "$B4" "$(tree)" +mv $PKG/payload.away $PKG/payload +rm -f $R/system/bin/hmgapid $R/system/bin/lpatch; rm -rf $R/data/hmgapi +B4=$(tree); preuninstall +check "no installation: package uninstall is a no-op" 0 $rc +check "... /system unchanged" "$B4" "$(tree)" +# Registered packages have no payload; a complete install uses the on-unit script. +rm -f $R/system/bin/app_process.real; cp $T/newfw $R/system/bin/app_process +cp $T/nemesis.old $R/system/xbin/nemesis +run $P/install.sh $P +mv $PKG/payload $PKG/payload.away +preuninstall +check "registered package: on-unit uninstall works" 0 $rc +check "... stock app_process restored" "$STK" "$(md5sum < $R/system/bin/app_process)" +mv $PKG/payload.away $PKG/payload +# Package uninstaller selection, using stand-ins for the scripts to observe +# selection without changing /system. Preserve the payload for later callers. +cp $P/uninstall.sh $T/uninstall.real; cp $P/VERSION $T/version.real +printf '#!/system/bin/sh\necho payload-uninstaller\n' > $P/uninstall.sh +mkdir -p $R/data/hmgapi +printf '#!/system/bin/sh\necho on-unit-uninstaller\n' > $R/data/hmgapi/uninstall.sh +for installed in 1.0.2 1.1.0 2.0.0 unknown; do + echo 1.1.0 > $P/VERSION + echo "version $installed" > $R/data/hmgapi/installed.txt + case $installed in 1.0.2|1.1.0) want=payload-uninstaller ;; *) want=on-unit-uninstaller ;; esac + preuninstall + check "package 1.1.0, installed $installed: uninstaller selection" "$want" "$out" + check "... succeeds" 0 $rc +done +rm $R/data/hmgapi/uninstall.sh +preuninstall +check "package older but on-unit script missing: payload recovery selected" payload-uninstaller "$out" +mv $T/uninstall.real $P/uninstall.sh; mv $T/version.real $P/VERSION exit $fail diff --git a/install/common/install.sh b/install/common/install.sh index bc889914..785ed606 100644 --- a/install/common/install.sh +++ b/install/common/install.sh @@ -35,8 +35,17 @@ capcheck() { # mount (refused on these units, even as root, except in the DSP OTA's process, # where the USB installer runs) remount() { - [ -n "$ROOT" ] && return 0 - if [ -x /system/xbin/nemesis ] && nemesis remount "$1" >/dev/null 2>&1; then return 0; fi + [ -n "$ROOT" ] && [ "$HMGAPI_TEST_REMOUNT" != 1 ] && return 0 + # NEMESIS 1.17.1+ keeps LivePatch farms read-only after a plain remount. + # Bypass them while writing; ordinary remount ro restores them. Older + # NEMESIS rejects the flag, so fall back to its existing remount command. + if [ -x "$ROOT/system/xbin/nemesis" ]; then + if [ "$1" = rw ]; then + "$ROOT/system/xbin/nemesis" remount rw --bypass-livepatch >/dev/null 2>&1 && return 0 + fi + "$ROOT/system/xbin/nemesis" remount "$1" >/dev/null 2>&1 && return 0 + fi + [ -n "$ROOT" ] && return 1 "$P/hmgapid" remount "$1" >/dev/null 2>&1 && return 0 mount -o remount,"$1" /system >/dev/null 2>&1 } @@ -255,8 +264,20 @@ fi backup /system/etc/install-recovery.sh backup /system/etc/qb_runtime.sh -remount rw || fail "can't remount /system rw (needs NEMESIS or a running hmgapid)" +# NEMESIS may take farms down before its underlying remount fails. fail() +# must restore them even when no writable remount ultimately succeeds. REMOUNTED=1 +remount rw || fail "can't remount /system rw (needs NEMESIS or a running hmgapid)" +# Check every destination before putting any runtime files on /system. A +# successful remount alone does not prove that each directory accepts writes. +for d in /system/bin /system/lib /system/framework /system/etc; do + t=$ROOT$d/.hmgapi-write-test-$$ + if (set -C; : > "$t") 2>/dev/null; then + rm -f "$t" || fail "can't remove the write check in $d; no runtime files were installed" + else + fail "can't write $d; no runtime files were installed on /system. See https://github.com/Open-HMG/HMG-API/issues/477" + fi +done put hmgapid /system/bin/hmgapid 755 shell put lpatch /system/bin/lpatch 755 shell put libhmgapi_hook.so /system/lib/libhmgapi_hook.so 644 root diff --git a/install/common/lib.sh b/install/common/lib.sh index 295394e6..a2f16855 100644 --- a/install/common/lib.sh +++ b/install/common/lib.sh @@ -62,6 +62,56 @@ fail() { exit 1 } +# Installer versions: the release forms emitted by HMG-API (x.y.z and +# x.y.z-alpha.N/-beta.N/-rc.N). Unknown forms keep the on-unit uninstaller. +# Build metadata does not change precedence. Bounded decimal fields also avoid +# octal interpretation and overflow in the head unit's shell arithmetic/tests. +uninstall_version_key() { + local v=${1%%+*} core pre stage=3 n=0 part key= oldifs=$IFS + core=${v%%-*} + case "$core" in ''|*[!0-9.]*|.*|*.|*..*) return 1 ;; esac + IFS=.; set -- $core; IFS=$oldifs + [ $# = 3 ] || return 1 + for part in "$@"; do + [ ${#part} -le 9 ] || return 1 + while [ "${part#0}" != "$part" ]; do part=${part#0}; done + key="$key${part:-0} " + done + case "$v" in + *-*) + pre=${v#*-} + case "$pre" in + alpha.*) stage=0; n=${pre#alpha.} ;; + beta.*) stage=1; n=${pre#beta.} ;; + rc.*) stage=2; n=${pre#rc.} ;; + *) return 1 ;; + esac + case "$n" in ''|*[!0-9]*|0?*) return 1 ;; esac + [ ${#n} -le 9 ] || return 1 ;; + esac + echo "$key$stage $n" +} +# payload_uninstaller_ok : +# a missing on-unit script needs recovery regardless of the recorded version. +# Otherwise only use a payload whose version is known to be at least as new. +payload_uninstaller_ok() { + local k v rest pv a b x y + [ -f "$3" ] || return 0 + [ -f "$2" ] || return 1 + while read -r k v rest; do [ "$k" = version ] && break; done < "$2" + [ "$k" = version ] && [ -n "$v" ] || return 1 + pv=$(cat "$1/VERSION" 2>/dev/null) + a=$(uninstall_version_key "$pv") || return 1 + b=$(uninstall_version_key "$v") || return 1 + while :; do + x=${a%% *}; y=${b%% *} + [ "$x" -gt "$y" ] && return 0 + [ "$x" -lt "$y" ] && return 1 + [ "$a" != "${a#* }" ] || return 0 + a=${a#* }; b=${b#* } + done +} + # The boot hook: `hmgapid boot`, which returns at once (the boot script goes straight on # to NEMESIS's start) and, detached, starts hmgapid and runs /data/hmgapi/boot.sh # (HMG-API's boot steps) once per boot. diff --git a/install/common/test.sh b/install/common/test.sh index 21bc1355..7eb0821c 100644 --- a/install/common/test.sh +++ b/install/common/test.sh @@ -135,4 +135,43 @@ check "... and boot.log says so" "1" "$(grep -c "recover didn't finish" "$T/boot rm -f "$D/state/journal.json"; rm -f "$T/hmgapid.calls" env ROOT="$T" $SH "$PWD/boot.sh" > /dev/null 2>&1; wait check "boot: no journal: recover isn't run" "" "$(calls)" +# Recovery media may replace the on-unit uninstaller only at equal/newer +# precedence. An unknown recorded or payload version keeps the on-unit script. +mkdir -p "$T/payload" +: > "$T/on-unit-uninstall.sh" +while read pv iv expected; do + echo "$pv" > "$T/payload/VERSION" + echo "version $iv" > "$T/installed.txt" + got=no + if L payload_uninstaller_ok "$T/payload" "$T/installed.txt" "$T/on-unit-uninstall.sh"; then got=yes; fi + check "payload $pv vs installed $iv: use payload" "$expected" "$got" +done <<'VERSIONS' +1.1.0 1.1.0 yes +1.1.0 1.1.1 no +1.1.1 1.1.0 yes +1.1.0 2.0.0 no +2.0.0 1.9.9 yes +1.10.0 1.9.0 yes +1.1.10 1.1.9 yes +1.1.0 1.1.0-rc.10 yes +1.1.0-rc.10 1.1.0 no +1.1.0-rc.9 1.1.0-rc.10 no +1.1.0-rc.10 1.1.0-rc.9 yes +1.1.0-beta.2 1.1.0-alpha.99 yes +1.1.0-beta.2 1.1.0-rc.1 no +1.1.0+build.1 1.1.0+build.2 yes +1.1.0-test.1 1.1.0 no +1.1.0 1.1.0-test.1 no +1.1.0-rc.01 1.1.0-rc.1 no +9999999999.0.0 1.1.0 no +1.1 1.1.0 no +VERSIONS +rm "$T/payload/VERSION" +got=no +if L payload_uninstaller_ok "$T/payload" "$T/installed.txt" "$T/on-unit-uninstall.sh"; then got=yes; fi +check "missing payload version: keep on-unit uninstaller" no "$got" +rm "$T/on-unit-uninstall.sh" +got=no +if L payload_uninstaller_ok "$T/payload" "$T/installed.txt" "$T/on-unit-uninstall.sh"; then got=yes; fi +check "missing on-unit uninstaller: allow recovery without version comparison" yes "$got" exit $fail diff --git a/install/common/uninstall.sh b/install/common/uninstall.sh index 4b9ac981..500c4f73 100644 --- a/install/common/uninstall.sh +++ b/install/common/uninstall.sh @@ -16,8 +16,16 @@ ROOT=${ROOT:-} D=$ROOT/data/hmgapi LOG=$D/install.log remount() { - [ -n "$ROOT" ] && return 0 - if [ -x /system/xbin/nemesis ] && nemesis remount "$1" >/dev/null 2>&1; then return 0; fi + [ -n "$ROOT" ] && [ "$HMGAPI_TEST_REMOUNT" != 1 ] && return 0 + # Like install.sh, bypass NEMESIS 1.17.1+ read-only LivePatch farms while + # writing; ordinary remount ro restores them. Older NEMESIS falls back. + if [ -x "$ROOT/system/xbin/nemesis" ]; then + if [ "$1" = rw ]; then + "$ROOT/system/xbin/nemesis" remount rw --bypass-livepatch >/dev/null 2>&1 && return 0 + fi + "$ROOT/system/xbin/nemesis" remount "$1" >/dev/null 2>&1 && return 0 + fi + [ -n "$ROOT" ] && return 1 $ROOT/system/bin/hmgapid remount "$1" >/dev/null 2>&1 && return 0 mount -o remount,"$1" /system >/dev/null 2>&1 # works in the DSP OTA's process (USB uninstall) } @@ -144,8 +152,10 @@ fi : > "$D/hook/disable" 2>/dev/null # 4. /system back -remount rw || fail "can't remount /system rw" +# NEMESIS may take farms down before its underlying remount fails. fail() +# must restore them even when no writable remount ultimately succeeds. REMOUNTED=1 +remount rw || fail "can't remount /system rw" if [ -n "$RESET" ]; then log "app_process: the new firmware's, left as it is" else diff --git a/install/nemesis/scripts/preuninstall.sh b/install/nemesis/scripts/preuninstall.sh index 2805d36b..f37efc27 100644 --- a/install/nemesis/scripts/preuninstall.sh +++ b/install/nemesis/scripts/preuninstall.sh @@ -22,6 +22,31 @@ case $caller in exit 0 fi ;; esac -[ -f "${ROOT:-}/data/hmgapi/installed.txt" ] && [ -f "${ROOT:-}/data/hmgapi/uninstall.sh" ] || { echo "HMG-API isn't installed"; exit 0; } export HMGAPI_FROM_NEMESIS=1 # uninstall.sh: NEMESIS is unlisting it already, no NEMESIS sync -exec sh "${ROOT:-}/data/hmgapi/uninstall.sh" +ON_UNIT=${ROOT:-}/data/hmgapi/uninstall.sh +UNINSTALL=$ON_UNIT +# A newer installed release knows its own file list and stock builds best. +# Use package recovery for equal/newer payloads or a missing on-unit script. +if [ -f ./payload/uninstall.sh ] && [ -f ./payload/lib.sh ] && [ -s ./payload/stock.txt ]; then + if [ ! -f "$ON_UNIT" ] || sh -c '. "$1/lib.sh"; payload_uninstaller_ok "$@"' sh ./payload "${ROOT:-}/data/hmgapi/installed.txt" "$ON_UNIT" 2>/dev/null; then + UNINSTALL=./payload/uninstall.sh + fi +fi +if [ -f "${ROOT:-}/data/hmgapi/installed.txt" ] && [ -f "$UNINSTALL" ]; then + exec sh "$UNINSTALL" +fi +# #477: the hook-library copy failed after hmgapid/lpatch but before the record +# and on-unit uninstaller existed. Recovery still validates the stock binary. +# A nonzero result keeps NEMESIS's package listed for another recovery attempt. +if [ -e "${ROOT:-}/system/bin/hmgapid" ] || [ -e "${ROOT:-}/system/bin/lpatch" ] \ + || [ -e "${ROOT:-}/system/bin/app_process.hmgapi-stock" ] \ + || [ -f "${ROOT:-}/data/hmgapi/installed.txt" ]; then + if [ -f "$UNINSTALL" ] && [ -s "${ROOT:-}/data/hmgapi/stock/system/bin/app_process" ]; then + echo "HMG-API's install is incomplete: removing it with the recovery uninstaller" + exec sh "$UNINSTALL" + fi + echo "HMG-API files remain, but the recovery uninstaller or stock backup is missing. Nothing was removed. See https://github.com/Open-HMG/HMG-API/issues/477" + exit 1 +fi +echo "HMG-API isn't installed" +exit 0 diff --git a/install/nemesis/sync-test.sh b/install/nemesis/sync-test.sh index 1d6f4762..d9425684 100644 --- a/install/nemesis/sync-test.sh +++ b/install/nemesis/sync-test.sh @@ -193,8 +193,13 @@ D=$R/data/nemesis/apps/hmgapi o=$(cd $D && env HMGAPI_NEMESIS_SYNC=1 ROOT=$R $SH scripts/preuninstall.sh 2>&1) has "HMG-API's preuninstall, list-only: does nothing" "list only, nothing uninstalled" "$o" rm $R/data/hmgapi/installed.txt +set +e; o=$(cd $D && env ROOT=$R $SH scripts/preuninstall.sh uninstall 2>&1); r=$?; set -e +check "... daemon without installed.txt or recovery payload: refuses to unlist" 1 $r +has "... explains that recovery is needed" "HMG-API files remain" "$o" +mv $R/system/bin/hmgapid $T/hmgapid.keep o=$(cd $D && env ROOT=$R $SH scripts/preuninstall.sh uninstall 2>&1) has "... uninstalled already (uninstall.sh left in /data/hmgapi): nothing to do" "HMG-API isn't installed" "$o" +mv $T/hmgapid.keep $R/system/bin/hmgapid set +e; o=$(cd $D && env ROOT=$R $SH scripts/postinstall.sh 2>&1); r=$?; set -e has "a listing package without HMG-API installed: says to install HMG-API" "isn't installed: install HMG-API's own download" "$o" check "... and fails" 1 $r diff --git a/install/usb/README.txt b/install/usb/README.txt index e1717ae7..7b058739 100644 --- a/install/usb/README.txt +++ b/install/usb/README.txt @@ -20,6 +20,14 @@ Uninstall with the stick (route A): add an empty file named UNINSTALL to the HMGAPI folder (UNINSTALL-PURGE also deletes HMG-API's data: mods, settings, backups). Every change HMG-API or its mods made goes back to stock. +If installation stopped partway, installing the fixed package again (route A +or B) completes the installation and keeps HMG-API installed. To remove it +instead, use a complete current USB package with HMGAPI/UNINSTALL. The stick carries its own uninstaller, so cleanup also works +when the failed installation never saved one on the unit. Recovery stops if +the required stock backup cannot be validated; check HMGAPI/install.log. +When a newer release is installed, an older stick uses the on-unit uninstaller +if it exists, so that release's file list and stock checks are retained. + The result is written to HMGAPI/install.log on the stick. Take the stick out afterwards: with it in, the head unit can try to start diff --git a/install/usb/test.sh b/install/usb/test.sh index a78605a7..ff61f004 100755 --- a/install/usb/test.sh +++ b/install/usb/test.sh @@ -184,4 +184,50 @@ rm $R/data/local/tmp/nemesis/stage/hmgapi/NEMESIS.data # left over from an int CWD=$A; run scripts/preuninstall.sh; CWD= has "NEMESIS uninstall: uninstalled (a leftover stage doesn't stop it)" "HMG-API uninstalled" "$out" check "/system byte-identical to stock again" "$BEFORE" "$(tree)" +# #477: USB recovery must work without installed.txt or the on-unit uninstaller. +NLP='#!/system/bin/sh +# NEMESIS LivePatch: apply patches before the zygote starts, then run the real binary. +exec /system/bin/app_process.real "$@"' +cp $R/system/bin/app_process $R/system/bin/app_process.real +printf '%s\n' "$NLP" > $R/system/bin/app_process +rm -rf $R/data/hmgapi +mkdir -p $R/data/hmgapi/stock/system/bin $R/data/hmgapi/hook +cp $R/system/bin/app_process.real $R/data/hmgapi/stock/system/bin/app_process +cp $P/hmgapid $R/system/bin/hmgapid; cp $P/lpatch $R/system/bin/lpatch +: > $S/HMGAPI/UNINSTALL +run $TS +check "partial install: USB uninstall succeeds" 0 $rc +has "... recovery log copied to the stick" "HMG-API uninstalled" "$(cat $S/HMGAPI/install.log)" +check "... daemon and patcher removed" no "$([ -e $R/system/bin/hmgapid ] || [ -e $R/system/bin/lpatch ] && echo yes || echo no)" +check "... NEMESIS script preserved" "$NLP" "$(cat $R/system/bin/app_process)" +check "... stock .real preserved" "$(md5sum < $R/data/hmgapi/stock/system/bin/app_process)" "$(md5sum < $R/system/bin/app_process.real)" +check "... recovery staging removed" no "$([ -e $R/data/local/tmp/hmgapi-usb ] && echo yes || echo no)" +cp $P/hmgapid $R/system/bin/hmgapid +rm $R/data/hmgapi/stock/system/bin/app_process +B4=$(tree); run $TS +check "partial install without backup: USB recovery fails" 1 $rc +check "... /system unchanged" "$B4" "$(tree)" +mv $P $P.away +run $TS +check "partial install without recovery payload: refuses" 1 $rc +has "... asks for the full download" "recovery payload is missing" "$out" +check "... /system unchanged" "$B4" "$(tree)" +mv $P.away $P +# A complete newer install keeps its on-unit uninstaller even with older +# recovery media. Equal/newer media use their own recovery script. +cp $P/uninstall.sh $T/uninstall.real; cp $P/VERSION $T/version.real +printf '#!/system/bin/sh\necho payload-uninstaller\n' > $P/uninstall.sh +printf '#!/system/bin/sh\necho on-unit-uninstaller\n' > $R/data/hmgapi/uninstall.sh +for installed in 1.0.2 1.1.0 2.0.0 unknown; do + echo 1.1.0 > $P/VERSION + echo "version $installed" > $R/data/hmgapi/installed.txt + case $installed in 1.0.2|1.1.0) want=payload-uninstaller ;; *) want=on-unit-uninstaller ;; esac + run $TS + check "USB 1.1.0, installed $installed: uninstaller selection" "$want" "$(cat $S/HMGAPI/install.log)" + check "... succeeds" 0 $rc +done +rm $R/data/hmgapi/uninstall.sh +run $TS +check "USB with missing on-unit script: payload recovery selected" payload-uninstaller "$(cat $S/HMGAPI/install.log)" +mv $T/uninstall.real $P/uninstall.sh; mv $T/version.real $P/VERSION exit $fail diff --git a/install/usb/troubleShooter b/install/usb/troubleShooter index a65670ed..228f047b 100755 --- a/install/usb/troubleShooter +++ b/install/usb/troubleShooter @@ -8,8 +8,11 @@ # HMGAPI/UNINSTALL uninstall instead (HMGAPI/UNINSTALL-PURGE: also /data/hmgapi) # Copies its log to HMGAPI/install.log, shows the result, and restarts the unit. # Toolbox and mksh only: the unit has no busybox. ROOT and HMGAPI_USB_DIRS: off-car tests. -export PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin${PATH:+:$PATH} -export LD_LIBRARY_PATH=/vendor/lib:/system/lib +# ROOT is an off-car test tree: keep its tool stand-ins ahead of the host tools. +if [ -z "${ROOT:-}" ]; then + export PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin${PATH:+:$PATH} + export LD_LIBRARY_PATH=/vendor/lib:/system/lib +fi umask 077 ROOT=${ROOT:-} STAGE=$ROOT/data/local/tmp/hmgapi-usb @@ -55,17 +58,33 @@ fi # stick file back, uncomment this line and the one in install/nemesis/scripts/postinstall.sh #[ -f "$U/HMGAPI/FORCE" ] && export HMGAPI_FORCE=1 if [ -f "$U/HMGAPI/UNINSTALL" ] || [ -f "$U/HMGAPI/UNINSTALL-PURGE" ]; then - if [ ! -f "$ROOT/data/hmgapi/uninstall.sh" ]; then + UNINSTALL=$ROOT/data/hmgapi/uninstall.sh + P=$U/HMGAPI/install/payload + # A failed install (#477) can leave binaries without an on-unit uninstaller. + # Prefer equal/newer recovery code; a newer installed release keeps its own. + if [ -f "$ROOT/data/hmgapi/installed.txt" ] || [ -e "$ROOT/system/bin/hmgapid" ] \ + || [ -e "$ROOT/system/bin/lpatch" ] || [ -e "$ROOT/system/bin/app_process.hmgapi-stock" ]; then + if [ -f "$P/uninstall.sh" ] && [ -f "$P/lib.sh" ] && [ -s "$P/stock.txt" ] \ + && { [ ! -f "$UNINSTALL" ] || sh -c '. "$1/lib.sh"; payload_uninstaller_ok "$@"' sh "$P" "$ROOT/data/hmgapi/installed.txt" "$UNINSTALL" 2>/dev/null; }; then + rm -r "$STAGE" 2>/dev/null + mkdir -p "${STAGE%/*}" && cp -r "$P" "$STAGE" || { toast "HMG-API: can't copy the recovery payload. Nothing was changed."; finish 1; } + UNINSTALL=$STAGE/uninstall.sh + elif [ ! -f "$UNINSTALL" ]; then + toast "HMG-API files remain, but the recovery payload is missing. Copy the complete HMG-API download to this stick. Nothing was changed." + finish 1 + fi + fi + if [ ! -f "$UNINSTALL" ]; then toast "HMG-API isn't installed. Nothing was changed." finish 1 fi toast "HMG-API: uninstalling..." PURGE= [ -f "$U/HMGAPI/UNINSTALL-PURGE" ] && PURGE=--purge - sh "$ROOT/data/hmgapi/uninstall.sh" $PURGE > "$STAGE.log" 2>&1 + sh "$UNINSTALL" $PURGE > "$STAGE.log" 2>&1 rc=$? cp "$STAGE.log" "$U/HMGAPI/install.log" 2>/dev/null - rm -f "$STAGE.log" + rm -rf "$STAGE"; rm -f "$STAGE.log" if [ $rc = 0 ]; then toast "HMG-API uninstalled, restarting." elif grep -q "isn't a known stock build" "$U/HMGAPI/install.log" 2>/dev/null; then toast "$UNKNOWN_TOAST" else toast "HMG-API uninstall failed: see HMGAPI/install.log on the stick."; fi