From 3b94a3f6601af4aad70451cce792a1f08160c49f Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Fri, 27 Nov 2026 09:32:35 -0500 Subject: [PATCH 1/4] Restarts, applies and installs only start with the car in P A restart takes the reverse camera's picture away for about a minute, and on 2026-11-27 a NEMESIS install (no HMG-API) cut off by a shift into reverse left a unit that didn't start. So every restart path, every change to /system and every install or update now reads gear.park first. - hmgapid reboot: refused out of P (75); with no gear reading refused (76) unless --gear-unknown-ok. - Engine: apply and restore that change files (and so may hold system apps), and update-hmgapi, check the gear (Gear): not in P 75, no reading 76 unless --gear-unknown-ok. The apps hold() stops are written down first (state/held.txt) and turned on again by the next start's recover, since pm disable lasts across restarts. - install.sh and uninstall.sh (every route): gear_gate in lib.sh, not in P stops with nothing changed, no reading warns and goes on. They and the USB installer ignore a closed pipe and hang-ups, and each file is synced before its rename. The USB installer's toasts say to stay in P, and its restart waits while the car isn't in P. - boot.sh: the restart after a rollback waits in the background until the car is parked (parked state, or P with the car off); no reading for 10 minutes: no restart, and a notice for the app. - The vehicle service always runs, so every car with a gear point gives the reading. - App: Restart, Apply (when files change), Install and Update HMG-API check the gear first (Park), say to stay in P, and ask the owner when the car gives no reading. Tested off-car only (tools/test-all.sh --fast all pass; the ARM tests could not run on this machine). What shifting into reverse does to a running install on a car is still to check: docs/car-test.md 10b. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 28 ++++ CLAUDE.md | 2 +- core/app/java/hmgapi/app/MainActivity.java | 67 ++++---- core/app/java/hmgapi/app/Park.java | 157 ++++++++++++++++++ core/app/java/hmgapi/app/UpdatesActivity.java | 51 ++++-- core/hmgapid/README.md | 2 +- core/hmgapid/hmgapid.c | 61 ++++++- core/hmgapid/test.sh | 23 ++- core/runtime/engine-test.sh | 46 +++++ core/runtime/homewifi-test.sh | 2 +- core/runtime/java/hmgapi/engine/Engine.java | 64 ++++++- core/runtime/java/hmgapi/engine/Gear.java | 70 ++++++++ .../java/hmgapi/engine/ModRuntime.java | 12 +- .../java/hmgapi/engine/UsbUpdates.java | 3 + core/runtime/java/hmgapi/vehicle/Hub.java | 5 +- core/runtime/runtime-test.sh | 10 +- docs/car-test.md | 33 ++++ docs/mod-reference.md | 4 +- install/common/boot.sh | 53 +++++- install/common/install.sh | 9 + install/common/lib.sh | 26 +++ install/common/test.sh | 108 +++++++++++- install/common/uninstall.sh | 10 +- install/nemesis/mod-test.sh | 2 +- install/nemesis/mod/scripts/postinstall.sh | 6 +- install/nemesis/mod/scripts/preuninstall.sh | 2 +- install/nemesis/sync-test.sh | 2 +- install/usb/README.txt | 4 + install/usb/troubleShooter | 22 ++- wiki/Installing-HMG-API.md | 5 + 30 files changed, 787 insertions(+), 102 deletions(-) create mode 100644 core/app/java/hmgapi/app/Park.java create mode 100644 core/runtime/java/hmgapi/engine/Gear.java diff --git a/CHANGELOG.md b/CHANGELOG.md index c4168555..697fbc9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,33 @@ # Changelog +## Unreleased + +- **Restarts, applies and installs only start with the car in P.** A restart takes the reverse + camera's picture away for about a minute (Android shows it on these units), and on 2026-11-27 a + NEMESIS install (no HMG-API) cut off by a shift into reverse left a unit that didn't start. + `hmgapid reboot` (every restart: the app's buttons, the boot steps), `engine apply` and + `engine restore` when they change files on `/system` (and so may stop system apps for the + swap), `engine update-hmgapi`, and `install.sh`/`uninstall.sh` (every install route) read + `gear.park` first. Not in P: refused (exit 75), nothing changed, no override. No reading (the + car's map has no gear point, or the vehicle service isn't up yet): `hmgapid reboot` and the + engine refuse (exit 76) unless told `--gear-unknown-ok`, which the app passes once the owner + says the car is in P; the USB, NEMESIS and adb routes, with no one to ask, warn and go on. The + app tells the owner to stay in P until it's done, as does the USB installer's toast. +- **HMG-API's vehicle service always runs**, not only while a mod reads the car, so every car + with a gear point in its map gives the reading the checks above need. +- **The restart after a boot-time rollback waits for the car to be parked**, in the background + (the boot never waits): HMG-API's parked state, or in P with the car off (in P for two minutes + when the power status can't be read). Until then the app shows Restart needed; with no gear + reading for 10 minutes there's no restart, and the app's Restart asks the owner. +- **A cut-off install or apply leaves less behind.** `install.sh`, `uninstall.sh` and the USB + installer ignore a closed output pipe and hang-ups (a screen that closed under them, an adb + that dropped), and sync each file before renaming it into place, so a reset right after never + leaves an empty `app_process`. The engine writes down the apps it stops (`state/held.txt`) + before `pm disable`, which lasts across restarts; an apply cut off before it turned them on + again has them turned on by the next start's `engine recover`. The USB installer's restart + waits while the car isn't in P. Tested off-car only: what shifting into reverse does to a + running install on a car is still to be checked (`docs/car-test.md`). + ## 1.1.0-rc.10 (2026-11-25) - **No owner-facing way to force an unknown `app_process`, for now.** An empty `HMGAPI/FORCE` diff --git a/CLAUDE.md b/CLAUDE.md index 9d4f13ff..53ebce43 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -17,7 +17,7 @@ HMG-API (1.1.0-rc.10) is a mod manager for Hyundai/Kia/Genesis **Gen5W** head un ## Recommendations (warn, then follow the user) -Owners get to decide for their own units. Follow these by default. If the user asks to go against one, say the risk in a line or two, then do what they asked. HMG-API does the same for its users: a warning plus an override, never a silent block. For now one exception: an `app_process` not in `install/common/stock.txt` is refused with no owner-facing override (no stick file, no message names one); `HMGAPI_FORCE=1` over adb stays for maintainers. +Owners get to decide for their own units. Follow these by default. If the user asks to go against one, say the risk in a line or two, then do what they asked. HMG-API does the same for its users: a warning plus an override, never a silent block. For now two exceptions: an `app_process` not in `install/common/stock.txt` is refused with no owner-facing override (no stick file, no message names one); `HMGAPI_FORCE=1` over adb stays for maintainers. And a restart, an apply or restore that changes `/system`, or an install or update while `gear.park` says the car isn't in P is refused (exit 75), saying to shift to P; with no gear reading it's the usual warning plus override (`--gear-unknown-ok`; the installers, with no one to ask, warn and go on). A restart takes the reverse camera's picture away for about a minute, and a shift into reverse can cut an install off (`hmgapi.engine.Gear`, `hmgapid reboot`, `lib.sh` `gear_gate`). New steps of that kind go through the same check. - Only firmware from 2026 onward (`26xxxx`) is supported for now; maps target each car's newest for its region: US 260507 (some models are on 260903: `maps/hyundai/neev23/usa/260903`), Canada 260206. Each region has its own builds, so maps (`maps////`, `car.json` `products`) and a mod's `"system"` match the full region, e.g. `CVEV.USA.`. Don't port pre-2026 firmware by default; ask the owner to update the head unit. With no map for the car, mods still install, points it can't find stay off, and a car missing from a mod's `"system"` is a warning with an override. - Don't flash MICOM, the bootloader or modem firmware, or firmware meant for another car: these can brick the unit or the car beyond what a USB stick can fix. diff --git a/core/app/java/hmgapi/app/MainActivity.java b/core/app/java/hmgapi/app/MainActivity.java index cc499f09..c00b935c 100644 --- a/core/app/java/hmgapi/app/MainActivity.java +++ b/core/app/java/hmgapi/app/MainActivity.java @@ -77,7 +77,7 @@ public void onClick(View v) { Ui.button(actions, "Restart unit", new View.OnClickListener() { @Override public void onClick(View v) { - confirmRestart(); + Park.confirmRestart(MainActivity.this); } }); top.addView(actions); @@ -223,6 +223,8 @@ static long boot() { } boolean restartNeeded() { + // boot.sh rolled a cut-off apply back and waits for the car to be parked to restart, or left it to the owner + if (new java.io.File("/data/hmgapi/public/restart-needed").isFile()) return true; return getSharedPreferences("hmgapi", MODE_PRIVATE).getLong("restart_boot", -1) == boot(); } @@ -273,25 +275,38 @@ void done(Hmgapid.Result r, Exception e) { : "Everything is applied."); return; } - new AlertDialog.Builder(MainActivity.this).setTitle("Apply these changes?").setMessage(shown) + final boolean files = filesChange(r.output); + new AlertDialog.Builder(MainActivity.this).setTitle("Apply these changes?") + .setMessage(files ? shown + "\n\nThis changes system files, so it only starts with the car in P. " + Park.STAY : shown) .setNegativeButton("Cancel", null) .setPositiveButton("Apply", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface d, int which) { - apply(); + if (!files) { + apply(false); + return; + } + Park.then(MainActivity.this, APPLYING, new Park.Go() { + @Override + public void go(boolean unknownOk) { + apply(unknownOk); + } + }); } }).show(); } }.start(this); } - private void apply() { + static final String APPLYING = "Applying changes to system files"; + + private void apply(final boolean unknownOk) { final AlertDialog wait = new AlertDialog.Builder(this) .setMessage("Applying… (rebuilding apps can take a few minutes; leave the head unit on)").setCancelable(false).show(); new Ui.Job() { @Override Hmgapid.Result work() throws Exception { - return Hmgapid.call(900000, "engine", "apply"); + return Hmgapid.call(900000, Park.with(unknownOk, "engine", "apply")); } @Override @@ -301,6 +316,13 @@ void done(Hmgapid.Result r, Exception e) { message("Lost contact while applying", e.getMessage() + ". The apply finishes on its own; open HMG-API again to check."); return; } + // the car left P since the check (or gives no reading): nothing was changed + if (Park.refused(MainActivity.this, r, APPLYING, new Park.Go() { + @Override + public void go(boolean ok) { + apply(ok); + } + })) return; boolean restart = restartNeeded() || r.output.contains("restart the head unit"); if (r.output.contains("restart the head unit")) needRestart(); AlertDialog.Builder d = new AlertDialog.Builder(MainActivity.this) @@ -327,6 +349,14 @@ void message(String title, String text) { new AlertDialog.Builder(this).setTitle(title).setMessage(text).setPositiveButton("OK", null).show(); } + /** Does an engine plan change files on /system (only in P: hmgapi.engine.Gear)? */ + static boolean filesChange(String out) { + for (String l : out.split("\n")) { + if (l.startsWith("change ") || l.startsWith("update ") || l.startsWith("restore ")) return true; + } + return false; + } + /** Does an engine plan change anything? */ static boolean changes(String out) { for (String l : out.split("\n")) { @@ -347,32 +377,7 @@ static String readable(String out) { return b.toString().trim(); } - private void confirmRestart() { - new AlertDialog.Builder(this) - .setTitle("Restart the head unit?") - .setMessage("The screen goes dark for about a minute. Don't use this while driving with navigation or a call active.") - .setNegativeButton("Cancel", null) - .setPositiveButton("Restart", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface d, int which) { - restart(); - } - }) - .show(); - } - void restart() { - new Ui.Job() { - @Override - Hmgapid.Result work() throws Exception { - return Hmgapid.call("reboot"); - } - - @Override - void done(Hmgapid.Result r, Exception e) { - if (e == null && r.code == 0) return; - message("Couldn't restart", e != null ? "hmgapid isn't running (" + e.getMessage() + ")" : r.output.trim()); - } - }.start(this); + Park.restart(this); } } diff --git a/core/app/java/hmgapi/app/Park.java b/core/app/java/hmgapi/app/Park.java new file mode 100644 index 00000000..d113e30a --- /dev/null +++ b/core/app/java/hmgapi/app/Park.java @@ -0,0 +1,157 @@ +package hmgapi.app; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; + +/** + * Steps that only start with the car in P: a restart (it takes the reverse camera's picture + * away for about a minute), and anything that changes /system or stops system apps for a + * while (an apply, an install, an HMG-API update), which a shift into reverse can cut off half + * done. The gear comes from HMG-API's vehicle reading (hmgapid `vehicle gear.park`). Not in P: + * the owner is told to shift to P, and nothing starts. No reading on this car: the warning + * plus override (the owner says the car is in P, and the step runs with --gear-unknown-ok). + * hmgapid's reboot and the engine check again (hmgapi.engine.Gear: exit codes 75 and 76), so + * a shift between the check here and the step still stops it. + */ +final class Park { + private Park() {} + + static final int NOT_PARKED = 75; + static final int UNKNOWN = 76; + static final String OVERRIDE = "--gear-unknown-ok"; + static final String STAY = "Keep the car in P until it's done."; + + interface Go { + /** the step itself; unknownOk: add OVERRIDE (no gear reading, the owner said go on) */ + void go(boolean unknownOk); + } + + /** what: the step, for the messages ("Restarting the head unit") */ + static void then(final Activity a, final String what, final Go go) { + new Ui.Job() { + @Override + Integer work() throws Exception { + Hmgapid.Result r = Hmgapid.call("vehicle", "gear.park"); + return r.code == 0 ? parse(r.output) : -1; + } + + @Override + void done(Integer g, Exception e) { + int v = e != null || g == null ? -1 : g; + if (v == 1) go.go(false); + else if (v == 0) notParked(a, what); + else unknown(a, what, go); + } + }.start(a); + } + + /** hmgapid's answer, "gear.park 1" ("-" when the car didn't answer): 1, 0, or -1 */ + static int parse(String out) { + for (String l : out.split("\n")) { + l = l.trim(); + if (l.equals("gear.park 1")) return 1; + if (l.equals("gear.park 0")) return 0; + } + return -1; + } + + static void notParked(Activity a, String what) { + new AlertDialog.Builder(a).setTitle("Shift to P first") + .setMessage(what + " only starts with the car in P: shifting into reverse while it runs can cut it off " + + "half done, and a restart takes the reverse camera's picture away for about a minute. Nothing was changed.") + .setPositiveButton("OK", null).show(); + } + + static void unknown(Activity a, final String what, final Go go) { + new AlertDialog.Builder(a).setTitle("Is the car in P?") + .setMessage("HMG-API can't read the gear on this car, so it can't check that it's in P. " + what + + " should only run in P: shifting into reverse while it runs can cut it off half done, and a " + + "restart takes the reverse camera's picture away for about a minute.\n\nPut the car in P. " + STAY) + .setNegativeButton("Cancel", null) + .setPositiveButton("It's in P: go on", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface d, int which) { + go.go(true); + } + }).show(); + } + + /** + * An op the engine or hmgapid refused for the gear (75, 76): said here, true. For 76 the + * owner may go on: retry runs the step again with OVERRIDE. + */ + static boolean refused(Activity a, Hmgapid.Result r, String what, Go retry) { + if (r == null) return false; + if (r.code == NOT_PARKED) { + notParked(a, what); + return true; + } + if (r.code == UNKNOWN) { + unknown(a, what, retry); + return true; + } + return false; + } + + /** a command with OVERRIDE added when asked */ + static String[] with(boolean unknownOk, String... cmd) { + if (!unknownOk) return cmd; + String[] c = new String[cmd.length + 1]; + System.arraycopy(cmd, 0, c, 0, cmd.length); + c[cmd.length] = OVERRIDE; + return c; + } + + /** Restart the head unit: asked first, then only in P (every restart button: Main, Updates) */ + static void confirmRestart(final Activity a) { + new AlertDialog.Builder(a) + .setTitle("Restart the head unit?") + .setMessage("The screen goes dark for about a minute, and the reverse camera's picture with it, so this " + + "only runs with the car in P. Don't use it with navigation or a call active.") + .setNegativeButton("Cancel", null) + .setPositiveButton("Restart", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface d, int which) { + restart(a); + } + }) + .show(); + } + + /** the restart itself, after the owner's yes: the gear, then hmgapid reboot */ + static void restart(final Activity a) { + then(a, RESTART, new Go() { + @Override + public void go(boolean unknownOk) { + restartNow(a, unknownOk); + } + }); + } + + static final String RESTART = "Restarting the head unit"; + + /** hmgapid reboot, which checks the gear again (a shift since the check here: said, nothing done) */ + static void restartNow(final Activity a, final boolean unknownOk) { + new Ui.Job() { + @Override + Hmgapid.Result work() throws Exception { + return Hmgapid.call(with(unknownOk, "reboot")); + } + + @Override + void done(Hmgapid.Result r, Exception e) { + if (e == null && r.code == 0) return; + if (e == null && refused(a, r, RESTART, new Go() { + @Override + public void go(boolean ok) { + restartNow(a, ok); + } + })) return; + new AlertDialog.Builder(a).setTitle("Couldn't restart") + .setMessage(e != null ? "hmgapid isn't running (" + e.getMessage() + ")" : r.output.trim()) + .setPositiveButton("OK", null).show(); + } + }.start(a); + } +} diff --git a/core/app/java/hmgapi/app/UpdatesActivity.java b/core/app/java/hmgapi/app/UpdatesActivity.java index de25e9e2..6713bedc 100644 --- a/core/app/java/hmgapi/app/UpdatesActivity.java +++ b/core/app/java/hmgapi/app/UpdatesActivity.java @@ -159,8 +159,20 @@ public void onClick(DialogInterface d, int which) { }).show(); } + static final String INSTALLING = "Installing a mod"; + + /** a mod's install leads to an apply (Apply changes), so it starts only in P too (Park) */ private void install(final JSONObject p, final String uuid, final boolean anyCar) { - message.setText("Installing " + p.optString("name") + "…"); + Park.then(this, INSTALLING, new Park.Go() { + @Override + public void go(boolean unknownOk) { + installNow(p, uuid, anyCar); + } + }); + } + + private void installNow(final JSONObject p, final String uuid, final boolean anyCar) { + message.setText("Installing " + p.optString("name") + "… " + Park.STAY); new Ui.Job() { @Override Hmgapid.Result work() throws Exception { @@ -187,22 +199,30 @@ private void confirmHmgApi(final JSONObject p) { new AlertDialog.Builder(this) .setTitle("Update HMG-API to " + p.optString("version") + "?") .setMessage("HMG-API's installer runs (it takes a minute; leave the head unit on), then the head unit " - + "needs a restart. Your mods and settings stay.") + + "needs a restart. Your mods and settings stay.\n\nIt changes system files, so it only starts with " + + "the car in P. " + Park.STAY) .setNegativeButton("Cancel", null) .setPositiveButton("Update", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface d, int which) { - updateHmgApi(p); + Park.then(UpdatesActivity.this, UPDATING, new Park.Go() { + @Override + public void go(boolean unknownOk) { + updateHmgApi(p, unknownOk); + } + }); } }).show(); } - private void updateHmgApi(final JSONObject p) { - message.setText("Updating HMG-API…"); + static final String UPDATING = "Updating HMG-API"; + + private void updateHmgApi(final JSONObject p, final boolean unknownOk) { + message.setText("Updating HMG-API… " + Park.STAY); new Ui.Job() { @Override Hmgapid.Result work() throws Exception { - return Hmgapid.call(900000, "engine", "update-hmgapi", p.optString("file")); + return Hmgapid.call(900000, Park.with(unknownOk, "engine", "update-hmgapi", p.optString("file"))); } @Override @@ -212,21 +232,22 @@ void done(Hmgapid.Result r, Exception e) { + "own; restart the head unit once it's done."); return; } + if (Park.refused(UpdatesActivity.this, r, UPDATING, new Park.Go() { + @Override + public void go(boolean ok) { + updateHmgApi(p, ok); + } + })) { + message.setText("Not updated yet: the car must be in P."); + return; + } AlertDialog.Builder d = new AlertDialog.Builder(UpdatesActivity.this) .setTitle(r.code == 0 ? "HMG-API updated" : "Not updated").setMessage(r.output.trim()); if (r.code == 0) { d.setNegativeButton("Later", null).setPositiveButton("Restart now", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface x, int which) { - new Ui.Job() { - @Override - Hmgapid.Result work() throws Exception { - return Hmgapid.call("reboot"); - } - - @Override - void done(Hmgapid.Result rr, Exception ee) {} - }.start(UpdatesActivity.this); + Park.restart(UpdatesActivity.this); // only in P } }); } else { diff --git a/core/hmgapid/README.md b/core/hmgapid/README.md index 65f899e6..a95e1140 100644 --- a/core/hmgapid/README.md +++ b/core/hmgapid/README.md @@ -11,7 +11,7 @@ hmgapid [args...] # any user: asks the daemon, prints its output, exits | Caller (SO_PEERCRED) | Ops | |---|---| | root (0), shell (2000: adb) | everything: `sh `, `remount rw\|ro`, plus the HMG-API ops | -| the `hmgapi.app` uid (from `/data/system/packages.list`) | `mods`, `enable `, `disable `, `setting-get `, `setting-set `, `log`, `reboot`, `allow`, `park-rule`, `services [mod]`, `service-restart\|stop\|start `, `service-log `, `usb [mod]`, `vehicle …` | +| the `hmgapi.app` uid (from `/data/system/packages.list`) | `mods`, `enable `, `disable `, `setting-get `, `setting-set `, `log`, `reboot [--gear-unknown-ok]` (only in P: 75 not in P; 76 no gear reading, unless told to go on), `allow`, `park-rule`, `services [mod]`, `service-restart\|stop\|start `, `service-log `, `usb [mod]`, `vehicle …` | | an enabled mod's app (its `apps`, `state/apps.txt`) or service (by session) | its own mod only: `whoami`, `setting-get`, `setting-set `, `services`, `service-restart\|stop\|start `, `service-log `, `usb`, `vehicle …` (the readings its mod.json lists) | | anyone else | `ping`, `version` | diff --git a/core/hmgapid/hmgapid.c b/core/hmgapid/hmgapid.c index 1ea04bf3..2b9890b2 100644 --- a/core/hmgapid/hmgapid.c +++ b/core/hmgapid/hmgapid.c @@ -15,7 +15,7 @@ * anyone else ping, version * * HMG-API ops: mods, enable , disable , setting-get , - * setting-set , log, reboot, engine , log, reboot [--gear-unknown-ok] (only in P), engine (the patch engine, run as root with app_process and the * runtime jar). Root ops: sh , remount rw|ro. * @@ -643,7 +643,7 @@ static int op_engine(int fd, int argc, char **argv) || seq(sub, "recover") || seq(sub, "install") || seq(sub, "remove") || seq(sub, "map") || seq(sub, "verify-map") || seq(sub, "usb-scan") || seq(sub, "update-hmgapi") || seq(sub, "apps") || seq(sub, "probe") || seq(sub, "map-car") || seq(sub, "report") || seq(sub, "problem") || seq(sub, "nemesis-sync"))) { - say(fd, "usage: engine plan | apply [--trial] [--force] | keep | restore | status | recover | install | remove " + say(fd, "usage: engine plan | apply [--trial] [--force] [--gear-unknown-ok] | keep | restore | status | recover | install | remove " " | nemesis-sync | map | map-car [--local | --write | --forget] | verify-map | report [--usb]" " | problem record [--no-log] [--crash-dumps] [--crash-reports] [--kernel] [--usb] [--seconds N] | problem stop\n"); return 2; @@ -1570,6 +1570,52 @@ static int op_vehicle(int fd, const char *mod, int argc, char **argv) return n < 0 ? 1 : 0; } +/* The gear, for a restart (as hmgapi.engine.Gear): 1 in P, 0 not in P, -1 no reading (the + vehicle service isn't up yet, or the car didn't answer: "-"). */ +static int gear_park(void) +{ + static char buf[256]; + static const char req[] = "read gear.park\n"; + struct sun a; + int alen = sockaddr_of(&a, VEHICLE_SOCK); + sl s, n; + ul got = 0; + struct { sl sec, usec; } tv = { 3, 0 }; + s = x_socket(1, 1 | 02000000, 0); + if (s < 0) return -1; + x_setsockopt((int)s, 1 /* SOL_SOCKET */, 20 /* SO_RCVTIMEO */, &tv, sizeof tv); + if (x_connect(s, &a, alen) < 0 || wall((int)s, req, sizeof req - 1)) { x_close(s); return -1; } + while (got < sizeof buf - 1 && (n = x_read((int)s, buf + got, sizeof buf - 1 - got)) > 0) { + got += (ul)n; + if (buf[got - 1] == '\n') break; /* one line; the service's close may not reach us (#145) */ + } + x_close(s); + buf[got] = 0; + if (seq(buf, "gear.park 1\n")) return 1; + if (seq(buf, "gear.park 0\n")) return 0; + return -1; +} + +/* A restart only in P: it takes the reverse camera's picture away for about a minute (Android + shows it on these units), and a driver may be about to reverse. Not in P: refused (75), no + override. No reading: refused (76) unless the caller says to go on, after asking the owner + (the HMG-API app) or deciding it can't (boot.sh waits instead). */ +static int op_reboot(int fd, int unknown_ok) +{ + char *a[] = { REBOOT, 0 }; + int g = gear_park(); + if (g == 0) { + say(fd, "not restarting: the car isn't in P (a restart takes the reverse camera's picture away for about a minute). Shift to P first.\n"); + return 75; + } + if (g < 0 && !unknown_ok) { + say(fd, "not restarting: the gear can't be read, so hmgapid can't check that the car is in P. With the car in P: reboot --gear-unknown-ok\n"); + return 76; + } + say(fd, "restarting\n"); + return run(fd, a); +} + /* A vehicle command (hmgapi.engine.Vehicle.COMMANDS) through HMG-API's vehicle service, which applies its guard. A mod only the ones it declared (state/commands.txt: " "); mod 0 (root, HMG-API's app): any, as "-". */ @@ -1644,7 +1690,7 @@ static int op_micom_watch(int fd, int argc, char **argv) path3(want, sizeof want, want, i > 1 ? " " : "", argv[i]); } if (write_small(ROOTDIR "/state/micom-watch.txt", want) < 0) { say(fd, "can't save it\n"); return 1; } - publish_state(); // the vehicle service runs while the watch is on + publish_state(); // the vehicle service (always running) picks the watch up say(fd, want); say(fd, "\n"); return 0; @@ -1830,11 +1876,8 @@ static int handle(int fd, int whoami, int argc, char **argv) if (seq(op, "park-rule") && argc <= 2) return op_park_rule(fd, argc, argv); if (seq(op, "micom-watch") && argc <= 9) return op_micom_watch(fd, argc, argv); if (seq(op, "micom-log") && argc == 1) return op_svc_log(fd, "hmgapi", "micom"); - if (seq(op, "reboot") && argc == 1) { - char *a[] = { REBOOT, 0 }; - say(fd, "restarting\n"); - return run(fd, a); - } + if (seq(op, "reboot") && argc == 1) return op_reboot(fd, 0); + if (seq(op, "reboot") && argc == 2 && seq(argv[1], "--gear-unknown-ok")) return op_reboot(fd, 1); if (whoami != WHO_ROOT) { say(fd, "not allowed\n"); return 13; } if (seq(op, "whoami") && argc == 1) { say(fd, g_mod[0] ? g_mod : "root"); say(fd, "\n"); return 0; } @@ -2118,7 +2161,7 @@ int main(int argc, char **argv, char **envp) if (argc < 2) { out(2, "usage: hmgapid daemon | hmgapid boot | hmgapid [args...]\n" "ops: ping, version, mods, enable , disable , setting-get ,\n" - " setting-set , mod-file mod.json|settings.json, log, reboot, engine ,\n" + " setting-set , mod-file mod.json|settings.json, log, reboot [--gear-unknown-ok], engine ,\n" " services [mod], service-restart|service-stop|service-start , service-log ,\n" " usb [mod], sh , remount rw|ro, binds\n" "a mod's own app: whoami, setting-get, setting-set , services, service-restart|stop|start ,\n" diff --git a/core/hmgapid/test.sh b/core/hmgapid/test.sh index 4a237744..e3883828 100755 --- a/core/hmgapid/test.sh +++ b/core/hmgapid/test.sh @@ -123,7 +123,11 @@ check "app: mods" "0" "$(code app mods)" check "app: setting-set" "0" "$(code app setting-set x '{}')" check "app: sh refused" "13" "$(code app sh 'echo no')" check "app: remount refused" "13" "$(code app remount rw)" -check "app: reboot allowed" "0" "$(code app reboot)" +# a restart only in P (op_reboot): no vehicle service here, so no gear reading +check "app: reboot with no gear reading refused" "76" "$(code app reboot)" +has "... and says how to go on" "reboot --gear-unknown-ok" "$(run app reboot)" +check "app: reboot --gear-unknown-ok allowed" "0" "$(code app reboot --gear-unknown-ok)" +check "app: reboot with another argument refused" "2" "$(code app reboot --now)" check "app: engine allowed" "0" "$(code app engine status)" # --- anyone else @@ -246,18 +250,19 @@ cp "$T/mod/root/state/usb.txt" "$S/state/usb.txt" check "root: every mod's" "demo /storage/usb1/Dashcam other /storage/usb0/Other" "$(run root usb | sort | tr '\n' ' ' | sed 's/ $//')" # --- vehicle readings: relayed to HMG-API's vehicle service, only what the mod declared check "vehicle: service not running" 69 "$(code root vehicle speed)" -python3 - "hmgapi-vehicle-test-$$" "$T/vehicle.req" <<'PY' & -import socket, sys +python3 - "hmgapi-vehicle-test-$$" "$T/vehicle.req" "$T/gear" <<'PY' & +import os, socket, sys s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.bind("\0" + sys.argv[1]); s.listen(4); s.settimeout(30) held = [] -for _ in range(6): +for _ in range(9): try: c, _ = s.accept() except socket.timeout: break line = c.makefile().readline() open(sys.argv[2], "a").write(line) if line.startswith("do "): c.sendall(("ok %s\n" % line.strip()).encode()); c.close(); continue - c.sendall("".join("%s 42\n" % n for n in line.split()[1:]).encode()) + gear = open(sys.argv[3]).read().strip() if os.path.exists(sys.argv[3]) else "42" + c.sendall("".join("%s %s\n" % (n, gear if n == "gear.park" else "42") for n in line.split()[1:]).encode()) if "speed.held" in line: held.append(c); continue # the car's service: its close never reached hmgapid (#145) c.close() PY @@ -271,6 +276,12 @@ check "vehicle: bad name refused" 2 "$(code mod vehicle 'speed;x')" check "vehicle: root reads any" "hv.soc 42" "$(run root vehicle hv.soc)" check "vehicle: a whole answer with the connection left open is still ok (#145)" "0 speed.held 42" "$(set +e; o=$(run root vehicle speed.held); echo "$? $o")" check "vehicle: other app refused" 13 "$(code other vehicle speed)" +# the restart's gear check, through the same service +echo 1 > "$T/gear" +check "reboot: in P, restarts" "0 restarting" "$(set +e; o=$(run app reboot); echo "$? $o")" +echo 0 > "$T/gear" +check "reboot: not in P, refused" 75 "$(code app reboot)" +check "reboot: not in P, refused even with the override" 75 "$(code app reboot --gear-unknown-ok)" printf 'demo charge_door\n' > "$T/mod/root/state/commands.txt" check "vehicle-do: a declared command passed on with the mod" "ok do demo charge_door open" "$(run mod vehicle-do charge_door open)" check "vehicle-do: undeclared refused" 13 "$(code mod vehicle-do wheel_heat on)" @@ -306,7 +317,7 @@ check "park-rule: a mod's app can't" 13 "$(code mod park-rule closed)" check "park-rule: the HMG-API app can" closed "$(run app park-rule closed)" check "park-rule: back to door" door "$(run root park-rule door)" kill $VP 2>/dev/null || true; wait $VP 2>/dev/null || true -check "vehicle: nothing reached the service for refused asks" 5 "$(wc -l < "$T/vehicle.req" | tr -d ' ')" +check "vehicle: nothing reached the service for refused asks (5, and the 3 restarts' gear)" 8 "$(wc -l < "$T/vehicle.req" | tr -d ' ')" : > "$T/mod/root/state/apps.txt" check "mod turned off: its app is anyone again" 13 "$(code mod whoami)" diff --git a/core/runtime/engine-test.sh b/core/runtime/engine-test.sh index 89634abd..9a29bfd0 100644 --- a/core/runtime/engine-test.sh +++ b/core/runtime/engine-test.sh @@ -136,6 +136,52 @@ check "disable modb" 0 $rc check "library without modb" "$(md5 $T/lib.a)" "$(md5 $R/system/lib/libtest.so)" check "modb's prop gone" "" "$(grep persist.modb $R/system/build.prop || true)" +# ---- only in P (Gear): a change to /system starts only with the car in P; no reading: the owner's go-ahead +printf '#!/bin/sh\n[ "$*" = "vehicle gear.park" ] || exit 0\ncat "%s/gear.out"\ncase "$(cat "%s/gear.out")" in gear.park*) exit 0 ;; esac\nexit 69\n' "$T" "$T" > $T/hmgapid +chmod +x $T/hmgapid +export HMGAPI_HMGAPID=$T/hmgapid +rm $R/data/hmgapi/mods/modb/.disabled +LIB_NOW=$(md5 $R/system/lib/libtest.so) +echo "gear.park 0" > $T/gear.out +eng apply +check "not in P: apply refused" 75 $rc +has "... saying why" "refused: the car isn't in P, and the apply only starts in P" "$out" +check "... nothing changed" "$LIB_NOW" "$(md5 $R/system/lib/libtest.so)" +check "... no journal" no "$([ -e $R/data/hmgapi/state/journal.json ] && echo yes || echo no)" +eng apply --gear-unknown-ok +check "not in P: refused even with the override" 75 $rc +eng plan +check "a plan needs no P" 0 $rc +echo "the vehicle service isn't running (yet)" > $T/gear.out +eng apply +check "no gear reading: apply refused" 76 $rc +has "... naming the owner's go-ahead" "then go on anyway (--gear-unknown-ok)" "$out" +check "... nothing changed" "$LIB_NOW" "$(md5 $R/system/lib/libtest.so)" +echo "gear.park -" > $T/gear.out +eng apply +check "the car didn't answer: refused the same" 76 $rc +eng apply --gear-unknown-ok +check "no gear reading, with the owner's go-ahead: applied" 0 $rc +has "... with the warning" "warning: the gear can't be read on this car" "$out" +check "... modb back in" "$(md5 $T/lib.ab)" "$(md5 $R/system/lib/libtest.so)" +touch $R/data/hmgapi/mods/modb/.disabled +echo "gear.park 1" > $T/gear.out +eng apply +check "in P: applied" 0 $rc +has "... the owner told to stay in P" "keep the car in P until the apply is done" "$out" +check "... modb out again" "$(md5 $T/lib.a)" "$(md5 $R/system/lib/libtest.so)" +echo "gear.park 0" > $T/gear.out +eng apply +check "nothing to change on /system: no gear needed" "0" "$rc" +# apps an apply stopped while their files changed (pm disable lasts across restarts): an apply +# cut off before it let them go leaves the list, and recover (the next start) turns them on +printf 'dev.example.gpm\ndev.example.dipo\n' > $R/data/hmgapi/state/held.txt +eng recover +has "recover: the apps a cut-off apply stopped turned on again" "enabled dev.example.gpm (stopped by an apply that was cut off)" "$out" +has "... each" "enabled dev.example.dipo" "$out" +check "... the list gone" no "$([ -e $R/data/hmgapi/state/held.txt ] && echo yes || echo no)" +unset HMGAPI_HMGAPID + # ---- trial: undone by recover, or kept echo '{"extra": true}' > $R/data/hmgapi/settings/moda.json BP_BEFORE=$(md5 $R/system/build.prop) diff --git a/core/runtime/homewifi-test.sh b/core/runtime/homewifi-test.sh index a7e7d4ea..a0c31764 100644 --- a/core/runtime/homewifi-test.sh +++ b/core/runtime/homewifi-test.sh @@ -399,7 +399,7 @@ eng publish-state check "publish with a projection ask runs" 0 $rc check "the keeper's asks: the projection AP for aa, no home Wi-Fi" "projection aa" "$(cat "$D/state/wifi.txt")" has "the keeper runs without home Wi-Fi, from boot" "hmgapi wifi root boot yes script /data/hmgapi/state/wifi.sh" "$(cat "$D/state/services.txt")" -hasnt "... no vehicle service for it" "hmgapi vehicle " "$(cat "$D/state/services.txt")" +has "... the vehicle service runs anyway (the gear check)" "hmgapi vehicle " "$(cat "$D/state/services.txt")" eng plan has "the plan: the projection AP" "wifi the projection AP up from boot, once each boot, unless a Wi-Fi client is on [aa]" "$out" has "the plan: the hotspot feature comes with it (the chip powered)" "com.hkmc.software.wifihotspot" "$out" diff --git a/core/runtime/java/hmgapi/engine/Engine.java b/core/runtime/java/hmgapi/engine/Engine.java index 1a5de4e8..ece8bf3b 100644 --- a/core/runtime/java/hmgapi/engine/Engine.java +++ b/core/runtime/java/hmgapi/engine/Engine.java @@ -23,6 +23,8 @@ * * engine plan what would change, conflicts, restarts (writes nothing) * engine apply [--trial] build and install; --trial: undone at the next start unless kept + * (a change to /system only starts in P: Gear; --gear-unknown-ok + * when the car gives no gear reading) * engine keep keep a trial apply * engine restore every file and setting HMG-API changed back to stock * engine status files HMG-API owns: stock, mods, expected and live md5 @@ -68,6 +70,7 @@ public final class Engine { public static final String HMGAPI_API = HMGAPI_VERSION.replaceFirst("-.*$", ""); final String root = env("HMGAPI_ROOT", ""); final boolean host = "1".equals(env("HMGAPI_HOST", "")); + boolean gearUnknownOk; // --gear-unknown-ok: the owner's go-ahead when the car gives no gear reading (Gear) final String lpatch = env("HMGAPI_LPATCH", "/system/bin/lpatch"); /** How the enabled mods' hooks fit together (the same rules the zygote's loader uses). */ HookPlan hookPlan() { @@ -121,6 +124,7 @@ static String ownsWhy(String types) { final File prevDir = new File(stateDir, "prev"); final File stateFile = new File(stateDir, "files.json"); final File journalFile = new File(stateDir, "journal.json"); + final File heldFile = new File(stateDir, "held.txt"); // apps an apply stopped (hold), until it lets them go final List out = new ArrayList(); final List problems = new ArrayList(); // stop the apply @@ -358,6 +362,7 @@ int run(String[] args) throws Exception { for (int i = 1; i < args.length; i++) { if ("--trial".equals(args[i])) trial = true; else if ("--force".equals(args[i])) force = true; + else if (Gear.OVERRIDE.equals(args[i])) gearUnknownOk = true; } stateDir.mkdirs(); if (!lock(cmd)) return 4; @@ -385,7 +390,7 @@ int run(String[] args) throws Exception { } if ("update-hmgapi".equals(cmd)) { if (args.length < 2) { - out.add("usage: engine update-hmgapi [--force]"); + out.add("usage: engine update-hmgapi [--force] [--gear-unknown-ok]"); return 2; } return UsbUpdates.updateHmgApi(this, args[1], force); @@ -437,7 +442,7 @@ int run(String[] args) throws Exception { if (!map.runnable) out.add("hooks are off on this car (" + map.reason + "): mods apply without their hooks"); } if (!restoreAll && !"plan".equals(cmd) && !"apply".equals(cmd)) { - out.add("usage: engine plan | apply [--trial] | keep | restore | status | recover | firmware-reset | install | remove | nemesis-sync | map | map-car [--local [--if-needed] | --write | --forget] | verify-map | report [--usb] | usb-scan | update-hmgapi | publish-state | apps"); + out.add("usage: engine plan | apply [--trial] [--gear-unknown-ok] | keep | restore | status | recover | firmware-reset | install | remove | nemesis-sync | map | map-car [--local [--if-needed] | --write | --forget] | verify-map | report [--usb] | usb-scan | update-hmgapi | publish-state | apps"); return 2; } Plan p = plan(restoreAll, force); @@ -461,6 +466,11 @@ int run(String[] args) throws Exception { if ("apply".equals(cmd)) LiveState.publish(this); // a switch with no file change still reaches running apps return 0; } + // files on /system change (and apps may be held): only in P (Gear) + if (p.filesChange()) { + int g = Gear.check(this, restoreAll ? "the restore" : "the apply", gearUnknownOk); + if (g != 0) return g; + } apply(p, trial); LiveState.publish(this); return 0; @@ -930,6 +940,14 @@ final class Plan { Map rebasedFrom = new TreeMap(); // path -> what HMG-API had written (merges: checked after build) boolean force; // --force: owned files that aren't what HMG-API wrote are written again + /** what apply() writes on /system: a file that changes or goes back to stock */ + boolean filesChange() throws IOException { + for (Target t : targets.values()) { + if (t.restore || !t.result.equals(owned(t.path)) || (force && !t.result.equals(md5(sys(t.path))))) return true; + } + return false; + } + boolean isEmpty() throws IOException { if (!rebased.isEmpty()) return false; for (Target t : targets.values()) if (t.restore || t.result == null || !t.result.equals(owned(t.path))) return false; @@ -2038,6 +2056,13 @@ void put(File src, File dst, String mode) throws Exception { } } + /** + * The apps whose files change, stopped (pm disable) for the swap. pm disable lasts + * across restarts, so the list is written down first (state/held.txt): an apply cut off + * between here and release() (the engine killed, a reset) leaves them disabled, and + * recover() (the next start, with the journal) turns them on again. Only in P: run() + * checked the gear before the journal (Gear). + */ Set hold(List changed) throws Exception { Set held = new LinkedHashSet(); if (host || !"1".equals(getprop("sys.boot_completed"))) return held; @@ -2047,16 +2072,47 @@ Set hold(List changed) throws Exception { int eq = l.lastIndexOf('='); if (l.startsWith("package:") && eq > 0) pkgs.put(l.substring(8, eq), l.substring(eq + 1)); } + List want = new ArrayList(); for (Target t : changed) { String pkg = pkgs.get(t.path); - if (pkg != null && exec("/system/bin/pm", "disable", pkg).code == 0) held.add(pkg); + if (pkg != null) want.add(pkg); } + if (want.isEmpty()) return held; + StringBuilder b = new StringBuilder(); + for (String pkg : want) b.append(pkg).append('\n'); + write(heldFile, b.toString()); + for (String pkg : want) if (exec("/system/bin/pm", "disable", pkg).code == 0) held.add(pkg); if (!held.isEmpty()) out.add("stopped " + held + " while their files change"); return held; } void release(Set held) throws Exception { for (String pkg : held) exec("/system/bin/pm", "enable", pkg); + heldFile.delete(); + } + + /** + * Apps a cut-off apply left stopped (hold): on again. At boot this runs before Android + * is up (boot.sh), and pm needs the package manager: so it waits for Android (up to five + * minutes), and keeps the list until every app is on, for the next recover (boot.sh runs + * one while the list is there). + */ + void releaseLeftHeld() throws Exception { + if (!heldFile.isFile()) return; + for (int i = 0; !host && i < 150 && !"1".equals(getprop("sys.boot_completed")); i++) Thread.sleep(2000); + StringBuilder left = new StringBuilder(); + for (String pkg : read(heldFile).split("\n")) { + pkg = pkg.trim(); + if (pkg.isEmpty()) continue; + if (!host && exec("/system/bin/pm", "enable", pkg).code != 0) { + left.append(pkg).append('\n'); + out.add("couldn't turn on " + pkg + " (stopped by an apply that was cut off): tried again at the next start"); + continue; + } + out.add("enabled " + pkg + " (stopped by an apply that was cut off)"); + } + if (left.length() == 0) heldFile.delete(); + else write(heldFile, left.toString()); } void remount(String mode) throws Exception { @@ -2137,6 +2193,7 @@ int keep() throws Exception { @SuppressWarnings("unchecked") int recover() throws Exception { if (!journalFile.exists()) { + releaseLeftHeld(); out.add("nothing to recover"); return 0; } @@ -2162,6 +2219,7 @@ int recover() throws Exception { write(stateFile, Json.write(j.get("state"))); journalFile.delete(); rm(prevDir); + releaseLeftHeld(); // after the files are back: the apps start on the files they had out.add(Boolean.TRUE.equals(j.get("trial")) ? "trial undone" : "unfinished apply rolled back"); return 0; } diff --git a/core/runtime/java/hmgapi/engine/Gear.java b/core/runtime/java/hmgapi/engine/Gear.java new file mode 100644 index 00000000..631e44bb --- /dev/null +++ b/core/runtime/java/hmgapi/engine/Gear.java @@ -0,0 +1,70 @@ +package hmgapi.engine; + +/** + * The gear, for the steps that only start in P: anything that changes /system, holds + * system apps (pm disable while their files change) or restarts the unit. A restart takes + * the reverse camera's picture away for about a minute, and an install or apply cut off by + * a shift to reverse can leave the unit half changed (2026-11-27: a NEMESIS install, with + * no HMG-API, was cut off that way and the unit didn't start again). + * + * Read through hmgapid (`vehicle gear.park`, HMG-API's vehicle service, which hmgapid runs + * on every unit). Not in P: refused, with nothing changed, and no override. No reading (the + * service isn't up yet, or the car's map has no gear point): refused unless the owner says + * to go on (--gear-unknown-ok), the warning plus override. hmgapid's reboot and the + * installers (lib.sh, gear_gate) answer with the same exit codes. + */ +final class Gear { + private Gear() {} + + static final int NOT_PARKED = 75; + static final int UNKNOWN = 76; + static final String OVERRIDE = "--gear-unknown-ok"; + + /** 1 in P, 0 not in P, -1 no reading. Off-car: HMGAPI_HMGAPID (a stand-in), else in P. */ + static int read(Engine e) { + String hm = Engine.env("HMGAPI_HMGAPID", e.host ? null : "/system/bin/hmgapid"); + if (hm == null) return 1; + try { + Engine.Run r = Engine.exec(hm, "vehicle", "gear.park"); + return r.code == 0 ? parse(r.text) : -1; + } catch (Exception x) { + return -1; + } + } + + /** hmgapid's answer, "gear.park 1" ("-" when the car didn't answer) */ + static int parse(String text) { + for (String l : text.split("\n")) { + l = l.trim(); + if (!l.startsWith("gear.park ")) continue; + String v = l.substring(10).trim(); + return "1".equals(v) ? 1 : "0".equals(v) ? 0 : -1; + } + return -1; + } + + /** + * Before a step that only starts in P: 0 to go on (the owner told to stay in P), else the + * exit code, with the reason in the engine's output. what: "an apply", "the update"... + */ + static int check(Engine e, String what, boolean unknownOk) { + int g = read(e); + if (g == 1) { + e.out.add("in P keep the car in P until " + what + " is done"); + return 0; + } + if (g == 0) { + e.out.add("refused: the car isn't in P, and " + what + " only starts in P (shifting into reverse while it runs " + + "can cut it off half done). Shift to P, stay there, and start it again. Nothing was changed."); + return NOT_PARKED; + } + if (unknownOk) { + e.out.add("warning: the gear can't be read on this car, so HMG-API can't check that it's in P; going on as asked. " + + "Keep the car in P until " + what + " is done."); + return 0; + } + e.out.add("refused: the gear can't be read on this car (no vehicle reading yet), so HMG-API can't check that it's in P. " + + "Put the car in P and keep it there, then go on anyway (" + OVERRIDE + "). Nothing was changed."); + return UNKNOWN; + } +} diff --git a/core/runtime/java/hmgapi/engine/ModRuntime.java b/core/runtime/java/hmgapi/engine/ModRuntime.java index 9bade4be..3a8a8d24 100644 --- a/core/runtime/java/hmgapi/engine/ModRuntime.java +++ b/core/runtime/java/hmgapi/engine/ModRuntime.java @@ -40,8 +40,8 @@ * true): started again when it exits. usb: folders on a stick the mod's app and * services can ask about (hmgapid `usb`), and that the launcher announces * (hmgapi.intent.action.USB_FOLDER to the mod's apps). vehicle read: the readings - * (Vehicle.READINGS) the mod's app and services may ask hmgapid for; while any - * enabled mod has some, hmgapid runs HMG-API's vehicle service. not_while_driving: + * (Vehicle.READINGS) the mod's app and services may ask hmgapid for, answered by + * HMG-API's vehicle service (always run: the gear check, Gear). not_while_driving: * the mod's own apps that follow the car's driving restriction (hook.Driving). * provides.panel: app widgets (AppWidgetProvider) of the mod's own apps, shown in * the right-hand split-screen panel (hook.Panel). input: button bindings (Keys), @@ -406,7 +406,7 @@ static Map projection(Engine e, Map> return Projection.settle(asks, pkgs, ov instanceof Map ? (Map) ov : null, notes); } - /** HMG-API's own vehicle service (hmgapi.vehicle.Hub), run by hmgapid while a mod reads the car */ + /** HMG-API's own vehicle service (hmgapi.vehicle.Hub), always run by hmgapid (the gear check, mods' readings) */ static final String VEHICLE_SERVICE = "hmgapi vehicle root android yes jar /system/framework/hmgapi-hook.jar hmgapi.vehicle.Hub"; /** HMG-API's log keeper (hmgapi.engine.LogKeeper): always on, logs/hmgapi/log.log for the app's Logs and the Car Report */ static final String LOG_SERVICE = "hmgapi log root boot yes jar /system/framework/hmgapi-hook.jar hmgapi.engine.LogKeeper"; @@ -440,8 +440,10 @@ static void publish(Engine e, Map> json, Set for (String c : d.commands) cmd.append(id).append(' ').append(c).append('\n'); for (Event x : d.events) evs.append(id).append(' ').append(x.event).append(' ').append(x.service == null ? "-" : x.service.name).append('\n'); } - boolean watch = new File(e.stateDir, "micom-watch.txt").isFile(); // the owner's MICOM recorder (hmgapid micom-watch) - if (veh.length() > 0 || cmd.length() > 0 || evs.length() > 0 || watch || HomeWifi.on(e)) svcs.append(VEHICLE_SERVICE).append('\n'); // home Wi-Fi reads parked and power + // always: the gear check before a restart, an apply or an install reads gear.park through it (Gear, + // hmgapid reboot, lib.sh gear_gate), on a unit with no mod reading the car too; and mods' readings, + // commands and car events, the owner's MICOM recorder, home Wi-Fi (parked and power) + svcs.append(VEHICLE_SERVICE).append('\n'); svcs.append(LOG_SERVICE).append('\n'); e.stateDir.mkdirs(); String net = Network.publish(e, json, on); // the network keeper, while a mod has network asks (or some are still in) diff --git a/core/runtime/java/hmgapi/engine/UsbUpdates.java b/core/runtime/java/hmgapi/engine/UsbUpdates.java index a1531edd..502058df 100644 --- a/core/runtime/java/hmgapi/engine/UsbUpdates.java +++ b/core/runtime/java/hmgapi/engine/UsbUpdates.java @@ -181,6 +181,9 @@ static int updateHmgApi(Engine e, String path, boolean force) throws Exception { : "HMG-API " + r.version + " is older than the installed " + installedVersion(e) + " (--force installs it anyway)")); return 3; } + // the installer changes /system: only in P (Gear; install.sh checks again, lib.sh gear_gate) + int g = Gear.check(e, "the update", e.gearUnknownOk); + if (g != 0) return g; e.out.add("HMG-API " + r.version + " (" + r.status + "), signed with the release key; running its installer"); ProcessBuilder pb = Engine.child(new ProcessBuilder("/system/bin/sh", new File(dir, "install.sh").getPath(), dir.getPath())); if (e.host) { diff --git a/core/runtime/java/hmgapi/vehicle/Hub.java b/core/runtime/java/hmgapi/vehicle/Hub.java index f5ae9378..1204eb07 100644 --- a/core/runtime/java/hmgapi/vehicle/Hub.java +++ b/core/runtime/java/hmgapi/vehicle/Hub.java @@ -18,8 +18,9 @@ /** * HMG-API's vehicle service: answers readings (hmgapi.engine.Vehicle) for hmgapid, * which has already checked that the asking mod declared them, and commands - * (Commands, with their guards). Run by hmgapid as a core service while an enabled - * mod declares readings or commands; listens on the abstract socket @hmgapi-vehicle + * (Commands, with their guards). Run by hmgapid as a core service, always: HMG-API's + * gear check before a restart, an apply or an install reads gear.park here (engine.Gear, + * hmgapid reboot), with or without a mod that reads the car; listens on the abstract socket @hmgapi-vehicle * and serves only root (hmgapid). * * request: "read [...]\n" diff --git a/core/runtime/runtime-test.sh b/core/runtime/runtime-test.sh index a344beb0..cce2012c 100644 --- a/core/runtime/runtime-test.sh +++ b/core/runtime/runtime-test.sh @@ -44,7 +44,7 @@ check "publish runs" 0 $rc check "apps.txt: enabled mods' apps" "dev.example.cam cam dev.example.game game" "$(cat $D/state/apps.txt)" check "services.txt: hmgapid's lines" "cam recorder system boot yes jar svc/cam.jar dev.example.cam.Main --quiet dir=/storage -cam watch app:dev.example.cam android no script svc/watch.sh -" "$(grep -v -e '^hmgapi log ' -e '^hmgapi net ' $D/state/services.txt)" +cam watch app:dev.example.cam android no script svc/watch.sh -" "$(grep -v -e '^hmgapi log ' -e '^hmgapi net ' -e '^hmgapi vehicle ' $D/state/services.txt)" has "the network keeper runs while mods are on (it keeps them off cellular)" "hmgapi net root boot yes script" "$(cat $D/state/services.txt)" check "usb.txt" "cam Dashcam" "$(cat $D/state/usb.txt)" has "missing apk reported" "game: app dev.example.gone: no app/Gone.apk in the mod" "$out" @@ -134,7 +134,7 @@ has "an app only one mod may own" "left out: zz: app dev.example.cam is already rm -r "$D/mods/zz" # vehicle readings and the driving restriction -check "no readings: no vehicle service" "" "$(grep '^hmgapi vehicle ' $D/state/services.txt)" +check "no readings: the vehicle service still runs (the gear check)" "hmgapi vehicle root android yes jar /system/framework/hmgapi-hook.jar hmgapi.vehicle.Hub" "$(grep '^hmgapi vehicle ' $D/state/services.txt)" check "HMG-API's log keeper always runs" "hmgapi log root boot yes jar /system/framework/hmgapi-hook.jar hmgapi.engine.LogKeeper" "$(grep '^hmgapi log ' $D/state/services.txt)" mkdir -p "$D/mods/tube/app" echo t > "$D/mods/tube/app/T.apk" @@ -169,11 +169,11 @@ eng plan has "plan: a command with its guard" "vehicle may charge_door (gear in P; once every 5 s) [tube]" "$out" has "plan: the startup notice says it needs the owner" "vehicle may startup_notice (only if the owner allowed it" "$out" -# the owner's MICOM watch keeps the vehicle service running without any mod +# the owner's MICOM watch: the vehicle service runs without any mod (it always does: the gear check) rm -r "$D/mods/tube"; mkdir -p "$D/mods/tube/app"; echo t > "$D/mods/tube/app/T.apk" printf '{"id": "tube", "version": "1.0.0", "apps": [{"apk": "app/T.apk", "package": "dev.example.tube"}]}\n' > "$D/mods/tube/mod.json" eng publish-state -check "no readings, no watch: no vehicle service" "" "$(grep '^hmgapi vehicle ' $D/state/services.txt)" +check "no readings, no watch: the vehicle service still runs (the gear check)" "hmgapi vehicle root android yes jar /system/framework/hmgapi-hook.jar hmgapi.vehicle.Hub" "$(grep '^hmgapi vehicle ' $D/state/services.txt)" echo all > "$D/state/micom-watch.txt" eng publish-state check "MICOM watch on: the vehicle service runs" "hmgapi vehicle root android yes jar /system/framework/hmgapi-hook.jar hmgapi.vehicle.Hub" "$(grep '^hmgapi vehicle ' $D/state/services.txt)" @@ -315,5 +315,5 @@ rm -r "$D/mods/grid" "$D/settings/hmgapi.json" touch "$D/mods/tube/.disabled" eng publish-state check "mod off: no readings" "" "$(cat $D/state/vehicle.txt)" -check "... and no vehicle service" "" "$(grep '^hmgapi vehicle ' $D/state/services.txt)" +check "... and the vehicle service only once (the gear check)" "1" "$(grep -c '^hmgapi vehicle ' $D/state/services.txt)" exit $fail diff --git a/docs/car-test.md b/docs/car-test.md index 0a551859..ede7027c 100644 --- a/docs/car-test.md +++ b/docs/car-test.md @@ -294,6 +294,39 @@ adb shell "while true; do hmgapid service-log hmgapi vehicle; sleep 5; done" | g 3. Optional, at the very end: `./hmgapi-adb.sh uninstall`, then `adb reboot`: the unit is stock (the menu has no HMG-API tile; CarPlay/AA wired as stock). +## 10b. Only in P, and what reverse does to a running install + +Car on, foot on the brake, somewhere you can shift to R safely. Log capture running +(`carlog.sh`, plus `adb shell logcat -v time` for the activity manager). + +1. The gear checks (fast, nothing changes): + - In D (or N): `adb shell hmgapid reboot` → exit 75, "not restarting: the car isn't + in P". `adb shell hmgapid engine apply` with a change pending → exit 75, nothing + changed (`engine status`). The app's Restart unit and Apply say "Shift to P first". + - In P: `adb shell hmgapid vehicle gear.park` → `gear.park 1`; Restart works. +2. Is there a kernel early camera? Power the unit on (cold, not QuickBoot) and shift to + R at once: a camera picture before Android's boot animation ends is the kernel's + (`earlycamlog.sh` on the unit hints at one). Note how long the picture is missing + after an `hmgapid reboot` in P, then shifting to R: the "about a minute" HMG-API + warns of. +3. **Not tested yet: what reverse does to a running install.** Run each in P, then + shift to R a few seconds in, and back to P. For each, write down: does the camera + cover the screen; does the screen that started it close (Engineering Mode's OTA + screen, NEMESIS Manager, the HMG-API app); does the activity manager kill its process + (`am_kill`, `am_proc_died` in the log); does the head unit reset; and does the + install finish (its log ends with "installed", `engine status` clean, no + `state/journal.json`, no `state/held.txt`). + - The app's Apply with a mod that changes an app (so apps are held). + - The USB stick route (Engineering Mode > USB Copy > DSP > Update OTA), an update over + an installed HMG-API: with the vehicle service running it refuses out of P, so + start it in P and shift during "installing". + - NEMESIS Manager's install of an HMG-API mod package. + HMG-API's own steps are built to survive a closed screen (the engine runs under + hmgapid, and the installers ignore a closed pipe or hang-up) and a reset (each file + synced, then renamed; a cut-off apply rolled back at the next start, its held apps + turned on again). NEMESIS's own installs are not HMG-API's: report what you see to + NEMESIS too. + ## Results Copy this and fill it in: diff --git a/docs/mod-reference.md b/docs/mod-reference.md index 663fe6cf..1f2ddfca 100644 --- a/docs/mod-reference.md +++ b/docs/mod-reference.md @@ -490,8 +490,8 @@ Each event reaches the mod three ways, all at once: HMG-API waits for all three to finish (or their time to run out) before it sends the next event (`shutdown` doesn't wait for a `parked` still running), and at `shutdown` before it tells the car it's done. It only holds a shutdown up while an -enabled mod asks for `shutdown`. The vehicle service runs while any enabled mod asks for -an event. `hmgapid service-log hmgapi vehicle` says what it saw (power, doors) and what +enabled mod asks for `shutdown`. The vehicle service always runs (HMG-API's own gear check +before a restart or an install reads `gear.park` through it). `hmgapid service-log hmgapi vehicle` says what it saw (power, doors) and what each event did. ## Packaging diff --git a/install/common/boot.sh b/install/common/boot.sh index 6128e957..f40e57af 100644 --- a/install/common/boot.sh +++ b/install/common/boot.sh @@ -9,8 +9,9 @@ # hook back in a boot script something else rewrote; with NEMESIS, two minutes later, # engine nemesis-sync, so NEMESIS lists what HMG-API has installed. If an engine apply # was cut off (or a trial is waiting), roll it back, and restart once if anything was -# rolled back (apps may already have loaded the files). -# ROOT prefixes every path, for off-car tests only. +# rolled back (apps may already have loaded the files): in the background, once the car +# is parked (restart_when_parked), never right after power-on. +# ROOT prefixes every path, for off-car tests only (HMGAPI_PARK_POLL, _TRIES and _HOLD too). umask 077 ROOT=${ROOT:-} if [ -z "$ROOT" ]; then @@ -22,6 +23,47 @@ fi D=$ROOT/data/hmgapi H=$ROOT/system/bin/hmgapid echo "$(date) boot" +rm -f $D/public/restart-needed # a restart the last boot was waiting for: this start is it + +# The restart after a rollback, once the car is parked. A restart takes the reverse camera's +# picture away for about a minute (Android shows it on these units), and right after power-on, +# when this runs, the driver may be about to reverse: in P isn't enough then. So it waits, in +# the background, for HMG-API's parked state (the owner's rule: off, in P, the driver got out), +# or in P with the car off; on a car whose power status can't be read, in P for two minutes in +# a row. hmgapid's reboot checks P again. Meanwhile public/restart-needed tells the app (its +# Restart needed pill). With no gear reading for 10 minutes (the vehicle service never came up, +# or the car's map has no gear point), no restart: the notice stays, and the app's Restart asks +# the owner (the warning plus override). A QuickBoot suspend doesn't end the wait. +restart_when_parked() { + local poll=${HMGAPI_PARK_POLL:-10} tries=${HMGAPI_PARK_TRIES:-60} hold=${HMGAPI_PARK_HOLD:-120} unread=0 inp=0 g p k + echo "rolled back at $(date): restart once the car is parked" > $D/public/restart-needed + chmod 644 $D/public/restart-needed + while :; do + g=-; p=-; k=- + set -- $($H vehicle gear.park power parked 2>/dev/null) + while [ $# -ge 2 ]; do + case $1 in gear.park) g=$2 ;; power) p=$2 ;; parked) k=$2 ;; esac + shift 2 + done + case $g in + 1) unread=0; inp=$((inp + 1)) ;; + 0) unread=0; inp=0 ;; + *) unread=$((unread + 1)); inp=0 + if [ $unread -ge $tries ]; then + echo "$(date) no gear reading: not restarting (the app asks the owner)" + echo "rolled back at boot; the gear can't be read, so the restart was left to the owner" > $D/public/restart-needed + return 0 + fi ;; + esac + if [ "$k" = 1 ] || { [ "$g" = 1 ] && [ "$p" != on ] && [ "$p" != - ]; } \ + || { [ "$g" = 1 ] && [ "$p" = - ] && [ $((inp * poll)) -ge $hold ]; }; then + echo "$(date) parked (gear.park $g, power $p, parked $k): restarting" + $H reboot && return 0 + echo "$(date) hmgapid didn't restart: waiting again" + fi + sleep $poll + done +} # Already detached (own session, output to boot.log), so the background jobs start from # here: through `hmgapid sh` they'd hold its output pipe (mksh's saved fds), and this with it. if [ -s $D/state/binds.txt ]; then $H binds > $D/binds.log 2>&1 & fi @@ -32,15 +74,16 @@ if [ -x $ROOT/system/xbin/nemesis ]; then (sleep 120; $H engine nemesis-sync) $D/map-local.log 2>&1 & fi -if [ -f $D/state/journal.json ]; then +# the journal, or the apps a cut-off apply stopped (state/held.txt: engine recover turns them on) +if [ -f $D/state/journal.json ] || [ -f $D/state/held.txt ]; then # Restart only after a recover that finished (it takes the journal away). One that # failed part-way has "rolled back" in its log too and keeps the journal: restarting # on that would come straight back here, at every start. It's tried again at the # next start instead, and `hmgapid engine recover` says what's wrong. if $H engine recover > $D/recover.log 2>&1; then if grep -q "rolled back" $D/recover.log || grep -q "trial undone" $D/recover.log; then - echo "$(date) recover rolled something back: restarting" - $H reboot + echo "$(date) recover rolled something back: restarting once the car is parked" + restart_when_parked /dev/null [ "$(sum "$dst.hmgapi-new")" = "$want" ] || { rm -f "$dst.hmgapi-new"; fail "md5 mismatch after copying $1"; } + sync # on disk before the rename: a reset right after it never leaves an empty file (no zygote) mv "$dst.hmgapi-new" "$dst" || { rm -f "$dst.hmgapi-new"; fail "rename onto $2"; } log "installed $2" } @@ -75,6 +81,7 @@ backup() { capcheck log "HMG-API $VERSION install $(date)" +gear_gate "the install" # only in P, before anything changes (lib.sh) # the payload is whole while read m f; do @@ -267,11 +274,13 @@ if [ -z "$UPDATE" ]; then cp "$B" "$KEEP.hmgapi-new" || fail "copy app_process" chmod 755 "$KEEP.hmgapi-new"; chown root:shell "$KEEP.hmgapi-new" 2>/dev/null [ "$(sum "$KEEP.hmgapi-new")" = "$HAVE" ] || { rm -f "$KEEP.hmgapi-new"; fail "app_process copy md5 mismatch"; } + sync mv "$KEEP.hmgapi-new" "$KEEP" || fail "keep stock app_process" elif [ -n "$RESCUE" ]; then cp "$BAK" "$KEEP.hmgapi-new" || fail "copy the app_process backup" chmod 755 "$KEEP.hmgapi-new"; chown root:shell "$KEEP.hmgapi-new" 2>/dev/null [ "$(sum "$KEEP.hmgapi-new")" = "$RBAK" ] || { rm -f "$KEEP.hmgapi-new"; fail "app_process backup copy md5 mismatch"; } + sync mv "$KEEP.hmgapi-new" "$KEEP" || fail "keep stock app_process" log "app_process.hmgapi-stock: back from the stock backup ($RBAK)" fi diff --git a/install/common/lib.sh b/install/common/lib.sh index 295394e6..457b452c 100644 --- a/install/common/lib.sh +++ b/install/common/lib.sh @@ -62,6 +62,29 @@ fail() { exit 1 } +# gear_gate : an install or uninstall only starts in P (the engine's Gear, hmgapid reboot): +# shifting into reverse while one runs can cut it off half done (2026-11-27: a NEMESIS install, +# no HMG-API, cut off that way left a unit that didn't start). Read through a running hmgapid +# (`vehicle gear.park`, HMG-API's vehicle service). Not in P: exit 75, nothing changed, no +# override. No reading (no hmgapid yet, as on a first install, or its vehicle service isn't up): +# a warning to keep the car in P, and on: these routes (USB stick, NEMESIS, adb) have no one to +# ask, and the owner who started it is the override. HMGAPI_HMGAPID: a stand-in, for off-car +# tests (with ROOT and none, no check). +gear_gate() { + local what=$1 h=${HMGAPI_HMGAPID:-} g= + if [ -z "$h" ]; then + [ -n "$ROOT" ] && return 0 + h=/system/bin/hmgapid + fi + [ -x "$h" ] && set -- $("$h" vehicle gear.park 2>/dev/null) && [ "$1" = gear.park ] && g=$2 + case "$g" in + 1) log "in P: keep the car in P until $what is done" ;; + 0) log "FAIL: the car isn't in P, and $what only starts in P (shifting into reverse while it runs can cut it off half done). Shift to P, stay there, and start again. Nothing was changed." + exit 75 ;; + *) log "warning: the gear can't be read, so HMG-API can't check that the car is in P: keep it in P until $what is done" ;; + esac +} + # 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. @@ -107,6 +130,7 @@ hook_add() { chmod 755 "$_t" [ -x "$_f" ] || chmod 644 "$_t" fi + sync # whole on disk before it replaces the boot script mv "$_t" "$_f" } @@ -147,6 +171,7 @@ hook_restore() { chmod 755 "$_f.hmgapi-new" [ -x "$_f" ] || chmod 644 "$_f.hmgapi-new" [ "$(sum "$_f.hmgapi-new")" = "$(sum "$_b")" ] || { rm -f "$_f.hmgapi-new"; return 1; } + sync mv "$_f.hmgapi-new" "$_f" } @@ -162,5 +187,6 @@ hook_remove() { hook_strip "$_f" > "$_f.hmgapi-new" || return 1 chmod 755 "$_f.hmgapi-new" [ -x "$_f" ] || chmod 644 "$_f.hmgapi-new" + sync mv "$_f.hmgapi-new" "$_f" } diff --git a/install/common/test.sh b/install/common/test.sh index 21bc1355..b3bf1736 100644 --- a/install/common/test.sh +++ b/install/common/test.sh @@ -108,31 +108,123 @@ echo 999999 > "$D/state/hook-check.pid" HC > /dev/null check "hook-check: a stale lock (after a restart) doesn't stop it" "1" "$(grep -c 'HMGAPI_HOOK begin' "$T/system/etc/install-recovery.sh")" -# boot.sh: an apply that was cut off (or a trial) is rolled back, then one restart. A -# recover that fails part-way keeps its journal, so restarting on it would loop. -mkdir -p "$T/system/bin" +# gear_gate: an install or uninstall only starts in P (a running hmgapid's reading); with no +# reading, a warning and on +printf '#!/bin/sh\n[ "$*" = "vehicle gear.park" ] || exit 2\ncat "%s/gear.out" 2>/dev/null || { echo "the vehicle service isn'"'"'t running (yet)"; exit 69; }\n' "$T" > "$T/gear-hmgapid" +chmod +x "$T/gear-hmgapid" +G() { set +e; out=$(env ROOT="$T" LOG="$T/gear.log" HMGAPI_HMGAPID="$T/gear-hmgapid" $SH -c ". $PWD/lib.sh; gear_gate 'the install'; echo went on" 2>&1); rc=$?; set -e; } +echo "gear.park 1" > "$T/gear.out"; G +check "gear_gate: in P: on, the owner told to stay in P" "0|in P: keep the car in P until the install is done +went on" "$rc|$out" +echo "gear.park 0" > "$T/gear.out"; G +check "gear_gate: not in P: stops with 75, before anything" "75|0" "$rc|$(echo "$out" | grep -c 'went on')" +check "... says why" "1" "$(echo "$out" | grep -c "FAIL: the car isn't in P, and the install only starts in P")" +echo "gear.park -" > "$T/gear.out"; G +check "gear_gate: the car didn't answer: a warning, and on" "0|1|1" "$rc|$(echo "$out" | grep -c '^warning: the gear can')|$(echo "$out" | grep -c 'went on')" +rm -f "$T/gear.out"; G +check "gear_gate: no vehicle service: a warning, and on" "0|1" "$rc|$(echo "$out" | grep -c 'went on')" +set +e; out=$(env ROOT="$T" LOG="$T/gear.log" HMGAPI_HMGAPID="$T/missing" $SH -c ". $PWD/lib.sh; gear_gate 'the install'; echo went on" 2>&1); rc=$?; set -e +check "gear_gate: no hmgapid (a first install): a warning, and on" "0|1" "$rc|$(echo "$out" | grep -c 'went on')" +check "... in install.log too" "yes" "$(grep -q 'keep it in P until the install is done' "$T/gear.log" && echo yes || echo no)" +set +e; out=$(env ROOT="$T" LOG="$T/gear.log" $SH -c ". $PWD/lib.sh; gear_gate 'the install'; echo went on" 2>&1); rc=$?; set -e +check "gear_gate: off-car with no stand-in: no check" "0|went on" "$rc|$out" + +# boot.sh: an apply that was cut off (or a trial) is rolled back, then one restart, in the +# background once the car is parked (never holding the boot). A recover that fails part-way +# keeps its journal, so restarting on it would loop. +mkdir -p "$T/system/bin" "$D/public" cat > "$T/system/bin/hmgapid" <> "$T/hmgapid.calls" +case "\$1" in + vehicle) cat "$T/vehicle.out" 2>/dev/null || echo "the vehicle service isn't running (yet)"; exit 0 ;; + reboot) exit \$(cat "$T/reboot.rc" 2>/dev/null || echo 0) ;; +esac [ "\$1 \$2" = "engine recover" ] || exit 0 cat "$T/recover.out" exit \$(cat "$T/recover.rc") EOF chmod +x "$T/system/bin/hmgapid" -BOOT() { rm -f "$T/hmgapid.calls"; : > "$D/state/journal.json"; printf '%s\n' "$1" > "$T/recover.out"; echo "$2" > "$T/recover.rc"; env ROOT="$T" $SH "$PWD/boot.sh" > "$T/boot.out" 2>&1; wait; } +# the car: gear.park, power, parked (what hmgapid's vehicle reading answers) +car() { printf 'gear.park %s\npower %s\nparked %s\n' "$1" "$2" "$3" > "$T/vehicle.out"; } +BOOT() { rm -f "$T/hmgapid.calls"; : > "$D/state/journal.json"; printf '%s\n' "$1" > "$T/recover.out"; echo "$2" > "$T/recover.rc" + env ROOT="$T" HMGAPI_PARK_POLL=1 HMGAPI_PARK_TRIES=${TRIES:-60} HMGAPI_PARK_HOLD=${HOLD:-120} $SH "$PWD/boot.sh" > "$T/boot.out" 2>&1; } calls() { [ ! -f "$T/hmgapid.calls" ] || tr '\n' '|' < "$T/hmgapid.calls"; } +# the calls but the readings +once() { [ ! -f "$T/hmgapid.calls" ] || grep -v '^vehicle' "$T/hmgapid.calls" | tr '\n' '|'; } +# until the background wait has restarted or given up (15 s at most) +settled() { + i=0 + while [ $i -lt 30 ]; do + grep -q 'restarting$' "$T/boot.out" 2>/dev/null && grep -q '^reboot' "$T/hmgapid.calls" 2>/dev/null && return + grep -q 'not restarting' "$T/boot.out" 2>/dev/null && return + sleep 0.5; i=$((i + 1)) + done +} + +car 1 logic_on 0 BOOT "rolled back /system/app/A.apk unfinished apply rolled back" 0 -check "boot: an unfinished apply rolled back: one restart" "engine recover|reboot|" "$(calls)" +settled +check "boot: an unfinished apply rolled back: one restart, with the car off in P" "engine recover|reboot|" "$(once)" +check "... after reading the car" "vehicle gear.park power parked" "$(grep -m1 '^vehicle' "$T/hmgapid.calls")" +check "... the app told a restart is coming" "yes" "$([ -f "$D/public/restart-needed" ] && echo yes || echo no)" BOOT "trial undone" 0 -check "boot: a waiting trial undone: one restart" "engine recover|reboot|" "$(calls)" +settled +check "boot: a waiting trial undone: one restart" "engine recover|reboot|" "$(once)" + +# driving (not in P): boot.sh ends at once, and the restart waits +car 0 on 0 +t0=$(date +%s) +BOOT "unfinished apply rolled back" 0 +check "boot: driving: boot.sh doesn't wait for the car" "yes" "$([ $(( $(date +%s) - t0 )) -lt 3 ] && echo yes || echo no)" +sleep 2 +check "... no restart while not in P" "engine recover|" "$(once)" +# just switched on, in P: the driver may reverse next, so still no restart +car 1 on 0 +sleep 2 +check "... none in P with the car on either" "engine recover|" "$(once)" +car 1 on 1 +settled +check "... the restart once HMG-API's parked state is on" "engine recover|reboot|" "$(once)" + +# hmgapid refuses (shifted out of P in between): waits again, then restarts +car 1 logic_on 0 +echo 75 > "$T/reboot.rc" +BOOT "unfinished apply rolled back" 0 +i=0; while [ $i -lt 20 ] && ! grep -q "didn't restart" "$T/boot.out"; do sleep 0.5; i=$((i + 1)); done +echo 0 > "$T/reboot.rc" +i=0; while [ $i -lt 20 ] && [ "$(grep -c '^reboot' "$T/hmgapid.calls")" -lt 2 ]; do sleep 0.5; i=$((i + 1)); done +check "boot: hmgapid's reboot refused: tried again" "2" "$(grep -c '^reboot' "$T/hmgapid.calls")" +sleep 1 + +# no power status: in P for HMGAPI_PARK_HOLD seconds in a row +car 1 - 0 +HOLD=2 BOOT "unfinished apply rolled back" 0 +settled +check "boot: no power status: a restart once in P for a while" "engine recover|reboot|" "$(once)" + +# no gear reading at all: no restart, and the app asks the owner +rm -f "$T/vehicle.out" +TRIES=2 BOOT "unfinished apply rolled back" 0 +settled +check "boot: no gear reading: no restart" "engine recover|" "$(once)" +check "... the notice left for the app" "1" "$(grep -c 'left to the owner' "$D/public/restart-needed" 2>/dev/null)" +rm -f "$D/state/journal.json" +env ROOT="$T" $SH "$PWD/boot.sh" > /dev/null 2>&1 +check "... gone at the next start" "no" "$([ -f "$D/public/restart-needed" ] && echo yes || echo no)" + BOOT "nothing to recover" 0 -check "boot: nothing rolled back: no restart" "engine recover|" "$(calls)" +check "boot: nothing rolled back: no restart" "engine recover|" "$(once)" BOOT "rolled back /system/app/A.apk engine error: java.io.IOException: No space left on device" 1 -check "boot: recover failed part-way (journal kept): no restart, or it would restart at every start" "engine recover|" "$(calls)" +check "boot: recover failed part-way (journal kept): no restart, or it would restart at every start" "engine recover|" "$(once)" check "... and boot.log says so" "1" "$(grep -c "recover didn't finish" "$T/boot.out")" 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)" +echo dev.example.gpm > "$D/state/held.txt"; printf 'enabled dev.example.gpm (stopped by an apply that was cut off)\nnothing to recover\n' > "$T/recover.out"; echo 0 > "$T/recover.rc" +env ROOT="$T" $SH "$PWD/boot.sh" > /dev/null 2>&1 +check "boot: apps a cut-off apply stopped, no journal: recover turns them on, no restart" "engine recover|" "$(calls)" +rm -f "$D/state/held.txt" exit $fail diff --git a/install/common/uninstall.sh b/install/common/uninstall.sh index 4b9ac981..47db941a 100644 --- a/install/common/uninstall.sh +++ b/install/common/uninstall.sh @@ -10,6 +10,11 @@ # only, over adb, for now: no USB stick file sets it and no message names it. # ROOT prefixes every path, for off-car tests only. umask 077 +# a cut-off uninstall can leave /system half changed: a dropped reader of its output (adb, the +# NEMESIS Manager or Engineering Mode screen closed, say by the reverse camera taking over) or a +# hang-up doesn't end it. Each file still goes back whole (copied beside, synced, renamed), for a +# reset or a SIGKILL. +trap '' HUP PIPE INT TERM H=${0%/*} ROOT=${ROOT:-} . "$H/lib.sh" @@ -32,6 +37,7 @@ capcheck() { } capcheck log "HMG-API uninstall $(date)" +gear_gate "the uninstall" # only in P, before anything changes (lib.sh) # the zygote binaries the install wrapped: app_process unless it's NEMESIS 1.17's LivePatch script, # and app_process.real whenever it's there (lib.sh, zygote_files). The stock bytes go back into each, @@ -100,7 +106,8 @@ if [ -n "$RESET" ]; then elif [ -z "$ROOT" ] && [ -f "$D/state/files.json" ]; then /system/bin/hmgapid ping >/dev/null 2>&1 || /system/bin/hmgapid daemon >/dev/null 2>&1 # the engine runs through it [ -f "$D/state/journal.json" ] && /system/bin/hmgapid engine recover - if ! r=$(/system/bin/hmgapid engine restore 2>&1); then + # --gear-unknown-ok: gear_gate above checked (not in P stops there), and warned when it couldn't + if ! r=$(/system/bin/hmgapid engine restore --gear-unknown-ok 2>&1); then echo "$r" fail "engine restore didn't finish, so HMG-API stays installed (mods' files must go back first). See the lines above." fi @@ -155,6 +162,7 @@ else cp "$SRC" "$b.hmgapi-new" || fail "copy the stock app_process" chmod 755 "$b.hmgapi-new"; chown root:shell "$b.hmgapi-new" 2>/dev/null [ "$(sum "$b.hmgapi-new")" = "$ORIG" ] || { rm -f "$b.hmgapi-new"; fail "app_process md5 mismatch after copy"; } + sync # on disk before the rename: a reset right after it never leaves an empty file (no zygote) mv "$b.hmgapi-new" "$b" || { rm -f "$b.hmgapi-new"; fail "rename onto ${z#/system/bin/}"; } fi log "${z#/system/bin/}: stock again ($ORIG)" diff --git a/install/nemesis/mod-test.sh b/install/nemesis/mod-test.sh index 46b6f519..b82ae5c2 100755 --- a/install/nemesis/mod-test.sh +++ b/install/nemesis/mod-test.sh @@ -179,7 +179,7 @@ check "install" 0 $rc has "... through HMG-API's engine" "installed demo 1.0.0" "$out" check "... and applied" on "$(prop persist.demo)" check "... its file added" yes "$([ -f $R/system/etc/permissions/demo.xml ] && echo yes)" -check "... engine install (from NEMESIS: no sync) then apply" "engine install $R/data/nemesis/apps/hmgapi.demo/mod/package.zip --from-nemesis engine apply" "$(tr '\n' ' ' < $T/calls | sed 's/ $//')" +check "... engine install (from NEMESIS: no sync) then apply" "engine install $R/data/nemesis/apps/hmgapi.demo/mod/package.zip --from-nemesis engine apply --gear-unknown-ok" "$(tr '\n' ' ' < $T/calls | sed 's/ $//')" check "mod in HMG-API" "demo " "$(mods)" # ---- update: NEMESIS runs the old preuninstall first, with the new package staged diff --git a/install/nemesis/mod/scripts/postinstall.sh b/install/nemesis/mod/scripts/postinstall.sh index a6f9d166..83881b72 100755 --- a/install/nemesis/mod/scripts/postinstall.sh +++ b/install/nemesis/mod/scripts/postinstall.sh @@ -33,8 +33,12 @@ if ! "$H" engine install "$PWD/mod/package.zip" $any --from-nemesis; then toast "HMG-API didn't install $ID: see NEMESIS's log" exit 1 fi -if ! r=$("$H" engine apply 2>&1); then +# the apply only starts in P (the engine's gear check: not in P, refused with nothing changed); +# NEMESIS has no one to ask when the car gives no gear reading, and the owner started this: +# --gear-unknown-ok, with the engine's warning to keep the car in P +if ! r=$("$H" engine apply --gear-unknown-ok 2>&1); then echo "$r" + case $r in *"the car isn't in P"*) toast "HMG-API: shift to P, then install $ID again in NEMESIS Manager" ;; esac if [ $new = yes ]; then "$H" engine remove "$ID" --from-nemesis >/dev/null 2>&1 say "HMG-API couldn't apply it (see above), so it's not installed: nothing changed. Uninstall this package in NEMESIS Manager." diff --git a/install/nemesis/mod/scripts/preuninstall.sh b/install/nemesis/mod/scripts/preuninstall.sh index bc981053..b33f5685 100755 --- a/install/nemesis/mod/scripts/preuninstall.sh +++ b/install/nemesis/mod/scripts/preuninstall.sh @@ -38,7 +38,7 @@ fi "$H" engine remove "$ID" --from-nemesis || { echo "HMG-API didn't remove $ID (see above): nothing changed, the package stays"; exit 1; } # Removed from HMG-API either way now, so the package goes even when the apply fails: # the HMG-API app shows the pending change and its problem. -if ! "$H" engine apply; then +if ! "$H" engine apply --gear-unknown-ok; then # only in P (postinstall.sh says why) echo "WARNING: $ID is removed from HMG-API, but its files aren't back to stock yet (see above): open the HMG-API app and tap Apply changes" exit 0 fi diff --git a/install/nemesis/sync-test.sh b/install/nemesis/sync-test.sh index 1d6f4762..6c935b76 100644 --- a/install/nemesis/sync-test.sh +++ b/install/nemesis/sync-test.sh @@ -182,7 +182,7 @@ set +e; o=$(env -u HMGAPI_NEMESIS_SYNC PATH="$T/bin:$PATH" $R/system/xbin/nemesi check "NEMESIS uninstall of a listed mod: done" 0 $r check "... HMG-API removed the mod" "" "$(mods)" check "... and put its change back" stock "$(grep '^persist.demo=' $R/system/build.prop | cut -d= -f2)" -check "... through engine remove --from-nemesis and apply (no sync from inside NEMESIS)" "remove demo --from-nemesis|apply" "$(ecalls | sed 's/^engine //' | tr '\n' '|' | sed 's/|$//')" +check "... through engine remove --from-nemesis and apply (no sync from inside NEMESIS)" "remove demo --from-nemesis|apply --gear-unknown-ok" "$(ecalls | sed 's/^engine //' | tr '\n' '|' | sed 's/|$//')" check "... NEMESIS lists only HMG-API" "hmgapi " "$(listed)" # ---- HMG-API's own listing: another version is re-listed; a NEMESIS uninstall uninstalls only if installed diff --git a/install/usb/README.txt b/install/usb/README.txt index e1717ae7..78069d35 100644 --- a/install/usb/README.txt +++ b/install/usb/README.txt @@ -4,6 +4,10 @@ HMG-API @VERSION@ - USB stick Copy everything in this folder to the top level of an empty USB stick (FAT32). Then pick one of these. The head unit restarts when it's done. +Put the car in P first and keep it there until the head unit has restarted. +Shifting into reverse while an install runs can cut it off half done. HMG-API +refuses to start when it can tell the car isn't in P. + A. The head unit has nothing installed yet (no NEMESIS): 1. Plug the stick into the car's USB port. 2. Open Engineering Mode > USB Copy > DSP > Update OTA. diff --git a/install/usb/troubleShooter b/install/usb/troubleShooter index a65670ed..cde43395 100755 --- a/install/usb/troubleShooter +++ b/install/usb/troubleShooter @@ -11,6 +11,11 @@ export PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin${PATH:+:$PATH} export LD_LIBRARY_PATH=/vendor/lib:/system/lib umask 077 +# Engineering Mode (the DSP agent) runs this with its output on a pipe. If that screen closes +# while it runs (the reverse camera taking over the screen, say), the next line written would +# end this script, and with it the install it's in the middle of: so a closed pipe or a +# hang-up doesn't stop it (install.sh and uninstall.sh do the same, and only start in P). +trap '' HUP PIPE INT TERM ROOT=${ROOT:-} STAGE=$ROOT/data/local/tmp/hmgapi-usb @@ -24,15 +29,23 @@ toast() { } # restart once the DSP agent has the result; the rest runs from /data (deleting -# /storage/upgrade cuts off this script, which lives there) +# /storage/upgrade cuts off this script, which lives there). Not out of P: a restart takes the +# reverse camera's picture away for about a minute, so while HMG-API's vehicle reading says +# the car isn't in P, it waits (no reading, as on a first install: restarts as before). finish() { [ -n "$ROOT" ] && exit "$1" sync cat > /data/local/tmp/hmgapi-ota-finish.sh << 'END' #!/system/bin/sh +trap '' HUP PIPE INT TERM sleep 8 rm -rf /storage/upgrade/* sync +while [ -x /system/bin/hmgapid ]; do + set -- $(/system/bin/hmgapid vehicle gear.park 2>/dev/null) + [ "$1 $2" = "gear.park 0" ] || break + sleep 5 +done reboot END chmod 755 /data/local/tmp/hmgapi-ota-finish.sh @@ -59,7 +72,7 @@ if [ -f "$U/HMGAPI/UNINSTALL" ] || [ -f "$U/HMGAPI/UNINSTALL-PURGE" ]; then toast "HMG-API isn't installed. Nothing was changed." finish 1 fi - toast "HMG-API: uninstalling..." + toast "HMG-API: uninstalling. Keep the car in P until the head unit restarts." PURGE= [ -f "$U/HMGAPI/UNINSTALL-PURGE" ] && PURGE=--purge sh "$ROOT/data/hmgapi/uninstall.sh" $PURGE > "$STAGE.log" 2>&1 @@ -67,6 +80,7 @@ if [ -f "$U/HMGAPI/UNINSTALL" ] || [ -f "$U/HMGAPI/UNINSTALL-PURGE" ]; then cp "$STAGE.log" "$U/HMGAPI/install.log" 2>/dev/null rm -f "$STAGE.log" if [ $rc = 0 ]; then toast "HMG-API uninstalled, restarting." + elif [ $rc = 75 ]; then toast "HMG-API: the car isn't in P, so nothing was changed. Shift to P and start the uninstall again." 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 finish $rc @@ -77,7 +91,7 @@ if [ ! -f "$P/MANIFEST" ]; then toast "HMG-API: no HMGAPI/install/payload on the USB stick. Nothing was changed." finish 1 fi -toast "HMG-API $(cat "$P/VERSION" 2>/dev/null): installing..." +toast "HMG-API $(cat "$P/VERSION" 2>/dev/null): installing. Keep the car in P until the head unit restarts." # from /data: the stick may be pulled, and it may not allow running files rm -r "$STAGE" 2>/dev/null mkdir -p "${STAGE%/*}" && cp -r "$P" "$STAGE" || { toast "HMG-API: can't copy from the USB stick. Nothing was changed."; finish 1; } @@ -87,6 +101,8 @@ cp "$STAGE.log" "$U/HMGAPI/install.log" 2>/dev/null rm -r "$STAGE" "$STAGE.log" 2>/dev/null if [ $rc = 0 ]; then toast "HMG-API installed, restarting." +elif [ $rc = 75 ]; then + toast "HMG-API: the car isn't in P, so nothing was changed. Shift to P and start the install again." elif grep -q "isn't a known stock build" "$U/HMGAPI/install.log" 2>/dev/null; then toast "$UNKNOWN_TOAST" else diff --git a/wiki/Installing-HMG-API.md b/wiki/Installing-HMG-API.md index f13c5ca1..f927f9ed 100644 --- a/wiki/Installing-HMG-API.md +++ b/wiki/Installing-HMG-API.md @@ -13,6 +13,11 @@ Takes about 10 minutes. Park the car and keep it on (the head unit restarts once All three install the same thing, and [uninstalling](Uninstalling) puts every file back exactly as it was. +**Put the car in P first, and keep it there until the install is done and the head unit +has restarted.** Shifting into reverse while an install runs can cut it off half done. +Once HMG-API is installed it checks the gear itself: restarts, applying changes and +updates only start in P (on a car that gives no gear reading, the app asks you first). + ## USB stick 1. Unzip `hmgapi--usb.zip` onto an empty USB stick (FAT32), at the top level. From 77811fccf5dc2eb081d2b585081bd987bd917955 Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Fri, 27 Nov 2026 09:40:27 -0500 Subject: [PATCH 2/4] hmgapid test: the app's reboot with another argument is refused as not allowed (13) An op the app's uid may not use answers 13 before the root-only check that answers 2, as for every other op. Co-Authored-By: Claude Opus 5.5 --- core/hmgapid/test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/hmgapid/test.sh b/core/hmgapid/test.sh index e3883828..2f4e346d 100755 --- a/core/hmgapid/test.sh +++ b/core/hmgapid/test.sh @@ -127,7 +127,7 @@ check "app: remount refused" "13" "$(code app remount rw)" check "app: reboot with no gear reading refused" "76" "$(code app reboot)" has "... and says how to go on" "reboot --gear-unknown-ok" "$(run app reboot)" check "app: reboot --gear-unknown-ok allowed" "0" "$(code app reboot --gear-unknown-ok)" -check "app: reboot with another argument refused" "2" "$(code app reboot --now)" +check "app: reboot with another argument refused (not an op it may use)" "13" "$(code app reboot --now)" check "app: engine allowed" "0" "$(code app engine status)" # --- anyone else From 01befcfec77574cd177f84f4ccdff3e2d6917c18 Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Mon, 30 Nov 2026 16:20:30 -0500 Subject: [PATCH 3/4] Leave the held-apps journal to #434 #434 writes down the apps an apply stops before pm disable and turns them on again at recover, more fully than this PR did. Take state/held.txt out of the engine, boot.sh, the tests, the changelog and the car test, so this PR stays about the gear check. The rest of the cut-off-install hardening (ignoring a closed pipe, syncing before each rename) stays. Tested off-car: tools/test-all.sh --fast, 32 ok, 1 skipped (no ImageMagick). Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 14 +++---- core/runtime/engine-test.sh | 7 ---- core/runtime/java/hmgapi/engine/Engine.java | 44 +-------------------- docs/car-test.md | 5 +-- install/common/boot.sh | 3 +- install/common/test.sh | 4 -- 6 files changed, 11 insertions(+), 66 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d085e23e..b0c65f16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,14 +19,12 @@ (the boot never waits): HMG-API's parked state, or in P with the car off (in P for two minutes when the power status can't be read). Until then the app shows Restart needed; with no gear reading for 10 minutes there's no restart, and the app's Restart asks the owner. -- **A cut-off install or apply leaves less behind.** `install.sh`, `uninstall.sh` and the USB - installer ignore a closed output pipe and hang-ups (a screen that closed under them, an adb - that dropped), and sync each file before renaming it into place, so a reset right after never - leaves an empty `app_process`. The engine writes down the apps it stops (`state/held.txt`) - before `pm disable`, which lasts across restarts; an apply cut off before it turned them on - again has them turned on by the next start's `engine recover`. The USB installer's restart - waits while the car isn't in P. Tested off-car only: what shifting into reverse does to a - running install on a car is still to be checked (`docs/car-test.md`). +- **A cut-off install leaves less behind.** `install.sh`, `uninstall.sh` and the USB installer + ignore a closed output pipe and hang-ups (a screen that closed under them, an adb that + dropped), and sync each file before renaming it into place, so a reset right after never + leaves an empty `app_process`. The USB installer's restart waits while the car isn't in P. + Tested off-car only: what shifting into reverse does to a running install on a car is still + to be checked (`docs/car-test.md`). - **A root service can read its own settings** (`HmgApi.settings()`, `saveSettings()`): the daemon sees such a service as root, so the client names its mod (`HMGAPI_MOD`). diff --git a/core/runtime/engine-test.sh b/core/runtime/engine-test.sh index 9a29bfd0..677e387c 100644 --- a/core/runtime/engine-test.sh +++ b/core/runtime/engine-test.sh @@ -173,13 +173,6 @@ check "... modb out again" "$(md5 $T/lib.a)" "$(md5 $R/system/lib/libtest.so)" echo "gear.park 0" > $T/gear.out eng apply check "nothing to change on /system: no gear needed" "0" "$rc" -# apps an apply stopped while their files changed (pm disable lasts across restarts): an apply -# cut off before it let them go leaves the list, and recover (the next start) turns them on -printf 'dev.example.gpm\ndev.example.dipo\n' > $R/data/hmgapi/state/held.txt -eng recover -has "recover: the apps a cut-off apply stopped turned on again" "enabled dev.example.gpm (stopped by an apply that was cut off)" "$out" -has "... each" "enabled dev.example.dipo" "$out" -check "... the list gone" no "$([ -e $R/data/hmgapi/state/held.txt ] && echo yes || echo no)" unset HMGAPI_HMGAPID # ---- trial: undone by recover, or kept diff --git a/core/runtime/java/hmgapi/engine/Engine.java b/core/runtime/java/hmgapi/engine/Engine.java index ece8bf3b..67719862 100644 --- a/core/runtime/java/hmgapi/engine/Engine.java +++ b/core/runtime/java/hmgapi/engine/Engine.java @@ -124,7 +124,6 @@ static String ownsWhy(String types) { final File prevDir = new File(stateDir, "prev"); final File stateFile = new File(stateDir, "files.json"); final File journalFile = new File(stateDir, "journal.json"); - final File heldFile = new File(stateDir, "held.txt"); // apps an apply stopped (hold), until it lets them go final List out = new ArrayList(); final List problems = new ArrayList(); // stop the apply @@ -2056,13 +2055,7 @@ void put(File src, File dst, String mode) throws Exception { } } - /** - * The apps whose files change, stopped (pm disable) for the swap. pm disable lasts - * across restarts, so the list is written down first (state/held.txt): an apply cut off - * between here and release() (the engine killed, a reset) leaves them disabled, and - * recover() (the next start, with the journal) turns them on again. Only in P: run() - * checked the gear before the journal (Gear). - */ + /** The apps whose files change, stopped (pm disable) for the swap. Only in P: run() checked the gear first (Gear). */ Set hold(List changed) throws Exception { Set held = new LinkedHashSet(); if (host || !"1".equals(getprop("sys.boot_completed"))) return held; @@ -2072,47 +2065,16 @@ Set hold(List changed) throws Exception { int eq = l.lastIndexOf('='); if (l.startsWith("package:") && eq > 0) pkgs.put(l.substring(8, eq), l.substring(eq + 1)); } - List want = new ArrayList(); for (Target t : changed) { String pkg = pkgs.get(t.path); - if (pkg != null) want.add(pkg); + if (pkg != null && exec("/system/bin/pm", "disable", pkg).code == 0) held.add(pkg); } - if (want.isEmpty()) return held; - StringBuilder b = new StringBuilder(); - for (String pkg : want) b.append(pkg).append('\n'); - write(heldFile, b.toString()); - for (String pkg : want) if (exec("/system/bin/pm", "disable", pkg).code == 0) held.add(pkg); if (!held.isEmpty()) out.add("stopped " + held + " while their files change"); return held; } void release(Set held) throws Exception { for (String pkg : held) exec("/system/bin/pm", "enable", pkg); - heldFile.delete(); - } - - /** - * Apps a cut-off apply left stopped (hold): on again. At boot this runs before Android - * is up (boot.sh), and pm needs the package manager: so it waits for Android (up to five - * minutes), and keeps the list until every app is on, for the next recover (boot.sh runs - * one while the list is there). - */ - void releaseLeftHeld() throws Exception { - if (!heldFile.isFile()) return; - for (int i = 0; !host && i < 150 && !"1".equals(getprop("sys.boot_completed")); i++) Thread.sleep(2000); - StringBuilder left = new StringBuilder(); - for (String pkg : read(heldFile).split("\n")) { - pkg = pkg.trim(); - if (pkg.isEmpty()) continue; - if (!host && exec("/system/bin/pm", "enable", pkg).code != 0) { - left.append(pkg).append('\n'); - out.add("couldn't turn on " + pkg + " (stopped by an apply that was cut off): tried again at the next start"); - continue; - } - out.add("enabled " + pkg + " (stopped by an apply that was cut off)"); - } - if (left.length() == 0) heldFile.delete(); - else write(heldFile, left.toString()); } void remount(String mode) throws Exception { @@ -2193,7 +2155,6 @@ int keep() throws Exception { @SuppressWarnings("unchecked") int recover() throws Exception { if (!journalFile.exists()) { - releaseLeftHeld(); out.add("nothing to recover"); return 0; } @@ -2219,7 +2180,6 @@ int recover() throws Exception { write(stateFile, Json.write(j.get("state"))); journalFile.delete(); rm(prevDir); - releaseLeftHeld(); // after the files are back: the apps start on the files they had out.add(Boolean.TRUE.equals(j.get("trial")) ? "trial undone" : "unfinished apply rolled back"); return 0; } diff --git a/docs/car-test.md b/docs/car-test.md index ede7027c..fa131225 100644 --- a/docs/car-test.md +++ b/docs/car-test.md @@ -315,7 +315,7 @@ Car on, foot on the brake, somewhere you can shift to R safely. Log capture runn screen, NEMESIS Manager, the HMG-API app); does the activity manager kill its process (`am_kill`, `am_proc_died` in the log); does the head unit reset; and does the install finish (its log ends with "installed", `engine status` clean, no - `state/journal.json`, no `state/held.txt`). + `state/journal.json`, and the held apps turned on again). - The app's Apply with a mod that changes an app (so apps are held). - The USB stick route (Engineering Mode > USB Copy > DSP > Update OTA), an update over an installed HMG-API: with the vehicle service running it refuses out of P, so @@ -323,8 +323,7 @@ Car on, foot on the brake, somewhere you can shift to R safely. Log capture runn - NEMESIS Manager's install of an HMG-API mod package. HMG-API's own steps are built to survive a closed screen (the engine runs under hmgapid, and the installers ignore a closed pipe or hang-up) and a reset (each file - synced, then renamed; a cut-off apply rolled back at the next start, its held apps - turned on again). NEMESIS's own installs are not HMG-API's: report what you see to + synced, then renamed; a cut-off apply rolled back at the next start). NEMESIS's own installs are not HMG-API's: report what you see to NEMESIS too. ## Results diff --git a/install/common/boot.sh b/install/common/boot.sh index f40e57af..242c5dad 100644 --- a/install/common/boot.sh +++ b/install/common/boot.sh @@ -74,8 +74,7 @@ if [ -x $ROOT/system/xbin/nemesis ]; then (sleep 120; $H engine nemesis-sync) $D/map-local.log 2>&1 & fi -# the journal, or the apps a cut-off apply stopped (state/held.txt: engine recover turns them on) -if [ -f $D/state/journal.json ] || [ -f $D/state/held.txt ]; then +if [ -f $D/state/journal.json ]; then # Restart only after a recover that finished (it takes the journal away). One that # failed part-way has "rolled back" in its log too and keeps the journal: restarting # on that would come straight back here, at every start. It's tried again at the diff --git a/install/common/test.sh b/install/common/test.sh index b3bf1736..e8178fe8 100644 --- a/install/common/test.sh +++ b/install/common/test.sh @@ -223,8 +223,4 @@ 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)" -echo dev.example.gpm > "$D/state/held.txt"; printf 'enabled dev.example.gpm (stopped by an apply that was cut off)\nnothing to recover\n' > "$T/recover.out"; echo 0 > "$T/recover.rc" -env ROOT="$T" $SH "$PWD/boot.sh" > /dev/null 2>&1 -check "boot: apps a cut-off apply stopped, no journal: recover turns them on, no restart" "engine recover|" "$(calls)" -rm -f "$D/state/held.txt" exit $fail From 715402e495b0853cfceec826c848f19021cead17 Mon Sep 17 00:00:00 2001 From: anthonyanderson Date: Tue, 1 Dec 2026 15:32:51 -0500 Subject: [PATCH 4/4] Uninstall goes on out of P; the rest stays refused The review's decision (option 2): a car whose gear.park reads 0 while parked must never be kept from stock. uninstall.sh (gear_gate ... warn) and engine restore, its first half, now warn out of P and go on. Restarts, applies, installs and updates are still refused out of P with no override; a map that reads P wrong is fixed in the map. The USB uninstall reads the gear before it runs (the uninstall takes hmgapid away). Out of P it uninstalls, doesn't restart, and its toast says to restart once in P. CLAUDE.md's Recommendations now list this as an exception: no override for "not in P", and the way back to stock never refused. Tested off-car: engine-test, install/common/test.sh, the NEMESIS mod and sync tests and check-scripts pass. install/usb/test.sh (its new uninstall-out-of-P case) needs the ARM payload build, so CI runs it. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 10 ++++++---- CLAUDE.md | 2 +- core/runtime/engine-test.sh | 9 +++++++-- core/runtime/java/hmgapi/engine/Engine.java | 5 +++-- core/runtime/java/hmgapi/engine/Gear.java | 14 ++++++++++++++ docs/car-test.md | 2 ++ install/common/lib.sh | 13 +++++++++---- install/common/test.sh | 2 ++ install/common/uninstall.sh | 5 +++-- install/usb/README.txt | 3 ++- install/usb/test.sh | 11 +++++++++++ install/usb/troubleShooter | 18 +++++++++++++++--- wiki/Installing-HMG-API.md | 1 + 13 files changed, 76 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d9c002d..a79f17a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,12 @@ - **Restarts, applies and installs only start with the car in P.** A restart takes the reverse camera's picture away for about a minute (Android shows it on these units), and on 2026-11-27 a NEMESIS install (no HMG-API) cut off by a shift into reverse left a unit that didn't start. - `hmgapid reboot` (every restart: the app's buttons, the boot steps), `engine apply` and - `engine restore` when they change files on `/system` (and so may stop system apps for the - swap), `engine update-hmgapi`, and `install.sh`/`uninstall.sh` (every install route) read - `gear.park` first. Not in P: refused (exit 75), nothing changed, no override. No reading (the + `hmgapid reboot` (every restart: the app's buttons, the boot steps), `engine apply` when it + changes files on `/system` (and so may stop system apps for the swap), `engine update-hmgapi`, + and `install.sh` (every install route) read `gear.park` first. Not in P: refused (exit 75), + nothing changed, no override. The way back to stock is never refused: `uninstall.sh` and + `engine restore` warn and go on, so a car whose map reads P wrong can always uninstall; the + USB uninstall then doesn't restart, and says to restart once in P. No reading (the car's map has no gear point, or the vehicle service isn't up yet): `hmgapid reboot` and the engine refuse (exit 76) unless told `--gear-unknown-ok`, which the app passes once the owner says the car is in P; the USB, NEMESIS and adb routes, with no one to ask, warn and go on. The diff --git a/CLAUDE.md b/CLAUDE.md index bdb41d35..547fb9b6 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -17,7 +17,7 @@ HMG-API (1.1.0) is a mod manager for Hyundai/Kia/Genesis **Gen5W** head units (T ## Recommendations (warn, then follow the user) -Owners get to decide for their own units. Follow these by default. If the user asks to go against one, say the risk in a line or two, then do what they asked. HMG-API does the same for its users: a warning plus an override, never a silent block. For now two exceptions: an `app_process` not in `install/common/stock.txt` is refused with no owner-facing override (no stick file, no message names one); `HMGAPI_FORCE=1` over adb stays for maintainers. And a restart, an apply or restore that changes `/system`, or an install or update while `gear.park` says the car isn't in P is refused (exit 75), saying to shift to P; with no gear reading it's the usual warning plus override (`--gear-unknown-ok`; the installers, with no one to ask, warn and go on). A restart takes the reverse camera's picture away for about a minute, and a shift into reverse can cut an install off (`hmgapi.engine.Gear`, `hmgapid reboot`, `lib.sh` `gear_gate`). New steps of that kind go through the same check. +Owners get to decide for their own units. Follow these by default. If the user asks to go against one, say the risk in a line or two, then do what they asked. HMG-API does the same for its users: a warning plus an override, never a silent block. For now two exceptions: an `app_process` not in `install/common/stock.txt` is refused with no owner-facing override (no stick file, no message names one); `HMGAPI_FORCE=1` over adb stays for maintainers. And a restart, an apply that changes `/system`, or an install or update while `gear.park` says the car isn't in P is refused (exit 75) with no override (no flag, no "the reading is wrong" button), saying to shift to P: a car whose map reads P wrong gets its map fixed. The way back to stock is never refused: `uninstall.sh` and `engine restore` warn out of P and go on (the USB uninstall then leaves the restart to the owner). With no gear reading it's the usual warning plus override (`--gear-unknown-ok`; the installers, with no one to ask, warn and go on). A restart takes the reverse camera's picture away for about a minute, and a shift into reverse can cut an install off (`hmgapi.engine.Gear`, `hmgapid reboot`, `lib.sh` `gear_gate`). New steps of that kind go through the same check. - Only firmware from 2026 onward (`26xxxx`) is supported for now; maps target each car's newest for its region: US 260507 (some models are on 260903: `maps/hyundai/neev23/usa/260903`), Canada 260206. Each region has its own builds, so maps (`maps////`, `car.json` `products`) and a mod's `"system"` match the full region, e.g. `CVEV.USA.`. Don't port pre-2026 firmware by default; ask the owner to update the head unit. With no map for the car, mods still install, points it can't find stay off, and a car missing from a mod's `"system"` is a warning with an override. - Don't flash MICOM, the bootloader or modem firmware, or firmware meant for another car: these can brick the unit or the car beyond what a USB stick can fix. diff --git a/core/runtime/engine-test.sh b/core/runtime/engine-test.sh index 677e387c..1251cba3 100644 --- a/core/runtime/engine-test.sh +++ b/core/runtime/engine-test.sh @@ -220,10 +220,15 @@ eng firmware-reset has "firmware-reset with nothing replaced: nothing dropped" "no file HMG-API changed needed dropping" "$out" STOCK_PROP=$NEWPROP # the update's build.prop is stock from here on -# ---- restore everything +# ---- restore everything. Back to stock (uninstall's first half) goes on out of P, with a +# warning: a car whose map reads P wrong is never kept from stock rm -f $R/data/hmgapi/mods/modb/.disabled +export HMGAPI_HMGAPID=$T/hmgapid +echo "gear.park 0" > $T/gear.out eng restore -check "restore exit" 0 $rc +unset HMGAPI_HMGAPID +check "restore exit (the car not in P: on all the same)" 0 $rc +has "... with the warning" "warning: the car doesn't read as in P; going on, since the restore goes back to stock" "$out" check "build.prop stock" "$STOCK_PROP" "$(md5 $R/system/build.prop)" check "featurexml stock" "$STOCK_CFG" "$(md5 $R/system/onebin/cvev_us/featurexml.cfg)" check "library stock" "$STOCK_LIB" "$(md5 $R/system/lib/libtest.so)" diff --git a/core/runtime/java/hmgapi/engine/Engine.java b/core/runtime/java/hmgapi/engine/Engine.java index d4d8d979..f021d6e9 100644 --- a/core/runtime/java/hmgapi/engine/Engine.java +++ b/core/runtime/java/hmgapi/engine/Engine.java @@ -465,9 +465,10 @@ int run(String[] args) throws Exception { if ("apply".equals(cmd)) LiveState.publish(this); // a switch with no file change still reaches running apps return 0; } - // files on /system change (and apps may be held): only in P (Gear) + // files on /system change (and apps may be held): only in P (Gear). The restore (back + // to stock, uninstall's first half) warns out of P and goes on if (p.filesChange()) { - int g = Gear.check(this, restoreAll ? "the restore" : "the apply", gearUnknownOk); + int g = restoreAll ? Gear.check(this, "the restore", gearUnknownOk, true) : Gear.check(this, "the apply", gearUnknownOk); if (g != 0) return g; } apply(p, trial); diff --git a/core/runtime/java/hmgapi/engine/Gear.java b/core/runtime/java/hmgapi/engine/Gear.java index 631e44bb..365b0e44 100644 --- a/core/runtime/java/hmgapi/engine/Gear.java +++ b/core/runtime/java/hmgapi/engine/Gear.java @@ -12,6 +12,10 @@ * service isn't up yet, or the car's map has no gear point): refused unless the owner says * to go on (--gear-unknown-ok), the warning plus override. hmgapid's reboot and the * installers (lib.sh, gear_gate) answer with the same exit codes. + * + * The one step that goes on out of P is the way back to stock (`engine restore`, the first + * half of uninstall.sh): a warning, and on, so a wrong gear.park reading (a car whose map + * reads P wrong) never locks the owner out of stock. The fix for such a car is its map. */ final class Gear { private Gear() {} @@ -48,11 +52,21 @@ static int parse(String text) { * exit code, with the reason in the engine's output. what: "an apply", "the update"... */ static int check(Engine e, String what, boolean unknownOk) { + return check(e, what, unknownOk, false); + } + + /** notParkedOk: the way back to stock (restore), which warns out of P and goes on */ + static int check(Engine e, String what, boolean unknownOk, boolean notParkedOk) { int g = read(e); if (g == 1) { e.out.add("in P keep the car in P until " + what + " is done"); return 0; } + if (g == 0 && notParkedOk) { + e.out.add("warning: the car doesn't read as in P; going on, since " + what + " goes back to stock (a wrong " + + "reading mustn't keep a car from stock). Shift to P and keep it there until " + what + " is done."); + return 0; + } if (g == 0) { e.out.add("refused: the car isn't in P, and " + what + " only starts in P (shifting into reverse while it runs " + "can cut it off half done). Shift to P, stay there, and start it again. Nothing was changed."); diff --git a/docs/car-test.md b/docs/car-test.md index fa131225..447d2b8c 100644 --- a/docs/car-test.md +++ b/docs/car-test.md @@ -303,6 +303,8 @@ Car on, foot on the brake, somewhere you can shift to R safely. Log capture runn - In D (or N): `adb shell hmgapid reboot` → exit 75, "not restarting: the car isn't in P". `adb shell hmgapid engine apply` with a change pending → exit 75, nothing changed (`engine status`). The app's Restart unit and Apply say "Shift to P first". + The USB stick's UNINSTALL in D still uninstalls (install.log: "warning: the car + doesn't read as in P"), and its toast says the head unit isn't restarting. - In P: `adb shell hmgapid vehicle gear.park` → `gear.park 1`; Restart works. 2. Is there a kernel early camera? Power the unit on (cold, not QuickBoot) and shift to R at once: a camera picture before Android's boot animation ends is the kernel's diff --git a/install/common/lib.sh b/install/common/lib.sh index 457b452c..649a72f6 100644 --- a/install/common/lib.sh +++ b/install/common/lib.sh @@ -62,16 +62,17 @@ fail() { exit 1 } -# gear_gate : an install or uninstall only starts in P (the engine's Gear, hmgapid reboot): +# gear_gate [warn]: an install only starts in P (the engine's Gear, hmgapid reboot): # shifting into reverse while one runs can cut it off half done (2026-11-27: a NEMESIS install, # no HMG-API, cut off that way left a unit that didn't start). Read through a running hmgapid # (`vehicle gear.park`, HMG-API's vehicle service). Not in P: exit 75, nothing changed, no -# override. No reading (no hmgapid yet, as on a first install, or its vehicle service isn't up): +# override. warn (the uninstall, the way back to stock): not in P is a warning, and on, so a +# car whose map reads P wrong is never kept from stock. No reading (no hmgapid yet, as on a first install, or its vehicle service isn't up): # a warning to keep the car in P, and on: these routes (USB stick, NEMESIS, adb) have no one to # ask, and the owner who started it is the override. HMGAPI_HMGAPID: a stand-in, for off-car # tests (with ROOT and none, no check). gear_gate() { - local what=$1 h=${HMGAPI_HMGAPID:-} g= + local what=$1 mode=${2:-} h=${HMGAPI_HMGAPID:-} g= if [ -z "$h" ]; then [ -n "$ROOT" ] && return 0 h=/system/bin/hmgapid @@ -79,7 +80,11 @@ gear_gate() { [ -x "$h" ] && set -- $("$h" vehicle gear.park 2>/dev/null) && [ "$1" = gear.park ] && g=$2 case "$g" in 1) log "in P: keep the car in P until $what is done" ;; - 0) log "FAIL: the car isn't in P, and $what only starts in P (shifting into reverse while it runs can cut it off half done). Shift to P, stay there, and start again. Nothing was changed." + 0) if [ "$mode" = warn ]; then + log "warning: the car doesn't read as in P; going on, since $what goes back to stock (a wrong reading mustn't keep a car from stock). Shift to P and keep it there until $what is done." + return 0 + fi + log "FAIL: the car isn't in P, and $what only starts in P (shifting into reverse while it runs can cut it off half done). Shift to P, stay there, and start again. Nothing was changed." exit 75 ;; *) log "warning: the gear can't be read, so HMG-API can't check that the car is in P: keep it in P until $what is done" ;; esac diff --git a/install/common/test.sh b/install/common/test.sh index e8178fe8..5f52f879 100644 --- a/install/common/test.sh +++ b/install/common/test.sh @@ -119,6 +119,8 @@ went on" "$rc|$out" echo "gear.park 0" > "$T/gear.out"; G check "gear_gate: not in P: stops with 75, before anything" "75|0" "$rc|$(echo "$out" | grep -c 'went on')" check "... says why" "1" "$(echo "$out" | grep -c "FAIL: the car isn't in P, and the install only starts in P")" +set +e; out=$(env ROOT="$T" LOG="$T/gear.log" HMGAPI_HMGAPID="$T/gear-hmgapid" $SH -c ". $PWD/lib.sh; gear_gate 'the uninstall' warn; echo went on" 2>&1); rc=$?; set -e +check "gear_gate warn (the uninstall, back to stock): not in P: a warning, and on" "0|1|1" "$rc|$(echo "$out" | grep -c "^warning: the car doesn't read as in P; going on, since the uninstall goes back to stock")|$(echo "$out" | grep -c 'went on')" echo "gear.park -" > "$T/gear.out"; G check "gear_gate: the car didn't answer: a warning, and on" "0|1|1" "$rc|$(echo "$out" | grep -c '^warning: the gear can')|$(echo "$out" | grep -c 'went on')" rm -f "$T/gear.out"; G diff --git a/install/common/uninstall.sh b/install/common/uninstall.sh index 47db941a..9517c18d 100644 --- a/install/common/uninstall.sh +++ b/install/common/uninstall.sh @@ -37,7 +37,7 @@ capcheck() { } capcheck log "HMG-API uninstall $(date)" -gear_gate "the uninstall" # only in P, before anything changes (lib.sh) +gear_gate "the uninstall" warn # back to stock: out of P a warning, and on (lib.sh) # the zygote binaries the install wrapped: app_process unless it's NEMESIS 1.17's LivePatch script, # and app_process.real whenever it's there (lib.sh, zygote_files). The stock bytes go back into each, @@ -106,7 +106,8 @@ if [ -n "$RESET" ]; then elif [ -z "$ROOT" ] && [ -f "$D/state/files.json" ]; then /system/bin/hmgapid ping >/dev/null 2>&1 || /system/bin/hmgapid daemon >/dev/null 2>&1 # the engine runs through it [ -f "$D/state/journal.json" ] && /system/bin/hmgapid engine recover - # --gear-unknown-ok: gear_gate above checked (not in P stops there), and warned when it couldn't + # --gear-unknown-ok: gear_gate above warned when it couldn't read the gear (the restore, back + # to stock, warns out of P and goes on, like gear_gate) if ! r=$(/system/bin/hmgapid engine restore --gear-unknown-ok 2>&1); then echo "$r" fail "engine restore didn't finish, so HMG-API stays installed (mods' files must go back first). See the lines above." diff --git a/install/usb/README.txt b/install/usb/README.txt index 78069d35..224ffd78 100644 --- a/install/usb/README.txt +++ b/install/usb/README.txt @@ -6,7 +6,8 @@ Then pick one of these. The head unit restarts when it's done. Put the car in P first and keep it there until the head unit has restarted. Shifting into reverse while an install runs can cut it off half done. HMG-API -refuses to start when it can tell the car isn't in P. +refuses to install when it can tell the car isn't in P. An uninstall always +runs, but then the head unit doesn't restart on its own: restart it once in P. A. The head unit has nothing installed yet (no NEMESIS): 1. Plug the stick into the car's USB port. diff --git a/install/usb/test.sh b/install/usb/test.sh index a78605a7..b1d03050 100755 --- a/install/usb/test.sh +++ b/install/usb/test.sh @@ -131,6 +131,17 @@ run $TS has "UNINSTALL on the stick: uninstalled" "HMG-API uninstalled, restarting" "$out" check "/system byte-identical to stock" "$BEFORE" "$(tree)" has "uninstall log on the stick" "HMG-API uninstalled" "$(cat $S/HMGAPI/install.log)" +# out of P: the uninstall goes on (back to stock is never refused), the restart doesn't +rm $S/HMGAPI/UNINSTALL +run $TS +check "stick install again: HMG-API is in" yes "$(installed)" +printf '#!/bin/sh\n[ "$*" = "vehicle gear.park" ] && echo "gear.park 0"\n' > $T/gear0; chmod 755 $T/gear0 +: > $S/HMGAPI/UNINSTALL +export HMGAPI_HMGAPID=$T/gear0; run $TS; unset HMGAPI_HMGAPID +check "UNINSTALL, the car not in P: uninstalled all the same" "0|no" "$rc|$(installed)" +has "... saying it isn't restarting" "the head unit isn't restarting: with the car in P, restart it to finish" "$out" +has "... and the log warns" "warning: the car doesn't read as in P; going on, since the uninstall goes back to stock" "$(cat $S/HMGAPI/install.log)" +check "... /system byte-identical to stock" "$BEFORE" "$(tree)" rm -r $R/data/hmgapi run $TS has "UNINSTALL with nothing installed: says so" "HMG-API isn't installed. Nothing was changed" "$out" diff --git a/install/usb/troubleShooter b/install/usb/troubleShooter index cde43395..7ca88dd4 100755 --- a/install/usb/troubleShooter +++ b/install/usb/troubleShooter @@ -32,15 +32,18 @@ toast() { # /storage/upgrade cuts off this script, which lives there). Not out of P: a restart takes the # reverse camera's picture away for about a minute, so while HMG-API's vehicle reading says # the car isn't in P, it waits (no reading, as on a first install: restarts as before). +# finish norestart: the clean-up only (an uninstall out of P: the owner restarts). finish() { [ -n "$ROOT" ] && exit "$1" sync + if [ "${2:-}" = norestart ]; then : > /data/local/tmp/hmgapi-ota-norestart; else rm -f /data/local/tmp/hmgapi-ota-norestart; fi cat > /data/local/tmp/hmgapi-ota-finish.sh << 'END' #!/system/bin/sh trap '' HUP PIPE INT TERM sleep 8 rm -rf /storage/upgrade/* sync +if [ -f /data/local/tmp/hmgapi-ota-norestart ]; then rm -f /data/local/tmp/hmgapi-ota-norestart; exit 0; fi while [ -x /system/bin/hmgapid ]; do set -- $(/system/bin/hmgapid vehicle gear.park 2>/dev/null) [ "$1 $2" = "gear.park 0" ] || break @@ -72,15 +75,24 @@ if [ -f "$U/HMGAPI/UNINSTALL" ] || [ -f "$U/HMGAPI/UNINSTALL-PURGE" ]; then toast "HMG-API isn't installed. Nothing was changed." finish 1 fi - toast "HMG-API: uninstalling. Keep the car in P until the head unit restarts." + # The way back to stock goes on out of P (uninstall.sh warns), so a wrong gear reading never + # keeps a car from stock; the restart after it doesn't (the reverse camera). Read now: the + # uninstall takes hmgapid away. HMGAPI_HMGAPID: a stand-in, for off-car tests. + G= HM=${HMGAPI_HMGAPID:-} + [ -n "$HM" ] || [ -n "$ROOT" ] || HM=/system/bin/hmgapid + [ -n "$HM" ] && [ -x "$HM" ] && set -- $("$HM" vehicle gear.park 2>/dev/null) && [ "$1" = gear.park ] && G=$2 + if [ "$G" = 0 ]; then toast "HMG-API: uninstalling, though the car doesn't read as in P. Shift to P and keep it there." + else toast "HMG-API: uninstalling. Keep the car in P until the head unit restarts."; fi PURGE= [ -f "$U/HMGAPI/UNINSTALL-PURGE" ] && PURGE=--purge sh "$ROOT/data/hmgapi/uninstall.sh" $PURGE > "$STAGE.log" 2>&1 rc=$? cp "$STAGE.log" "$U/HMGAPI/install.log" 2>/dev/null rm -f "$STAGE.log" - if [ $rc = 0 ]; then toast "HMG-API uninstalled, restarting." - elif [ $rc = 75 ]; then toast "HMG-API: the car isn't in P, so nothing was changed. Shift to P and start the uninstall again." + if [ $rc = 0 ] && [ "$G" = 0 ]; then + toast "HMG-API uninstalled. The car doesn't read as in P, so the head unit isn't restarting: with the car in P, restart it to finish." + finish 0 norestart + elif [ $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 finish $rc diff --git a/wiki/Installing-HMG-API.md b/wiki/Installing-HMG-API.md index f927f9ed..a12a08bc 100644 --- a/wiki/Installing-HMG-API.md +++ b/wiki/Installing-HMG-API.md @@ -17,6 +17,7 @@ back exactly as it was. has restarted.** Shifting into reverse while an install runs can cut it off half done. Once HMG-API is installed it checks the gear itself: restarts, applying changes and updates only start in P (on a car that gives no gear reading, the app asks you first). +Uninstalling always runs, so you can always get back to stock. ## USB stick