Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
207 changes: 207 additions & 0 deletions install/common/install-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 24 additions & 3 deletions install/common/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions install/common/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <payload dir> <installed.txt> <on-unit uninstall.sh>:
# 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.
Expand Down
39 changes: 39 additions & 0 deletions install/common/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading
Loading