diff --git a/AGENTS.md b/AGENTS.md index e60b56d4f..33f49418b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -58,7 +58,7 @@ Omarchy Mobile runs native Omarchy (Arch Linux ARM, Hyprland, Omarchy's packages This leaves other people's staged and unstaged changes alone. - **Moving a feature from bring-up into a package:** bump the package `version`, enable nothing that isn't hang-safe, and delete the bring-up files from `/etc/modprobe.d` on the phone (`omamobile-*-bringup.conf`, and any hand-copied package file). A file in `/etc/modprobe.d` replaces the package's `/usr/lib/modprobe.d` file of the same name, so a stale `omamobile-aoc.conf` there hid the package's new blacklist. - **Kernel releases change with every push** (`7.3.0-rc6-omamobile-push1-NNNNN-g`). `bin/omamobile push` runs `modules-push` when the tarball's checksum differs. Without its modules a new kernel boots without its drivers, the GPU among them. `modules-push` replaces `/usr/lib/modules/` as a whole; on the 9a that once took a hand-installed panel driver with it, and the display stayed on the boot logo. It now refuses when the folder holds modules added or swapped by hand since the last push (it records what each push installed in `.omamobile-modules.sha256`), and `OMAMOBILE_MODULES_REPLACE=1` overrides that, wiping them. Pin a daily release whose folder carries hand-installed modules (`/usr/lib/modules/.omamobile-pinned`), and keep a copy before overriding (`tar -C /usr/lib/modules -cf … `). -- **Stop the modem before a `push` or a `reboot-to`.** `push` ends in `reboot-to`, a raw reboot syscall that stops no unit, so `omamobile-modem.service` never runs its clean stop and its `modem-up` marker stays. On the 7 Pro, where the modem starts at boot (device-cheetah 0.1.43+), the next boot counts that as an unclean end ("ended without a clean stop: a hang or a reset"); from `omamobile-modem` 0.1.19 it starts the modem all the same (before, it skipped the modem once), and a second such end before a clean stop holds it off until `omamobile-modem-start --retry`. So a skipped stop no longer costs a boot's modem, but it uses up the one unclean end the guard allows. This happened on 2026-11-08 after a `reboot-to bootloader` taken to charge a flat battery. So run `systemctl stop omamobile-modem` first, as part of the reboot the human said yes to; a reboot that needs no reason can be `systemctl reboot` instead, which stops it. The stop powers the CP off (one recovered UFS error-handler pass, as any reboot with the modem up costs) and, with the NV overlay on, captures its writes. For now it takes about 40 s and ends as "failed", with the CP's power-off logged as a crash; the marker is still removed ([#532](https://github.com/derekross/omarchy-mobile/issues/532)). This is not the restart the modem notes below forbid: nothing starts the modem again before the reboot. [#403](https://github.com/derekross/omarchy-mobile/pull/403), still open, would let the next boot start the modem after a `reboot-to`; the CP's ordered power-off and the NV capture would still come only from the unit's stop. [MODEM-PLAN](device/cheetah/MODEM-PLAN.md) M4.6b +- **Stop the modem before a `push` or a `reboot-to`.** `push` ends in `reboot-to`, a raw reboot syscall that stops no unit, so `omamobile-modem.service` never runs its clean stop and its `modem-up` marker stays. On the 7 Pro, where the modem starts at boot (device-cheetah 0.1.43+), the next boot counts that as an unclean end ("ended without a clean stop: a hang or a reset"); from `omamobile-modem` 0.1.19 it starts the modem all the same (before, it skipped the modem once), and a second such end before a clean stop holds it off until `omamobile-modem-start --retry`. So a skipped stop no longer costs a boot's modem, but it uses up the one unclean end the guard allows. This happened on 2026-11-08 after a `reboot-to bootloader` taken to charge a flat battery. So run `systemctl stop omamobile-modem` first, as part of the reboot the human said yes to; a reboot that needs no reason can be `systemctl reboot` instead, which stops it. The stop powers the CP off (one recovered UFS error-handler pass, as any reboot with the modem up costs) and, with the NV overlay on, captures its writes. For now it takes about 40 s and ends as "failed", with the CP's power-off logged as a crash; the marker is still removed ([#532](https://github.com/derekross/omarchy-mobile/issues/532)). This is not the restart the modem notes below forbid: nothing starts the modem again before the reboot. From `omamobile-modem` 0.1.21, with a device package whose `reboot-to` notes the boot it ends ([#403](https://github.com/derekross/omarchy-mobile/pull/403)), the next boot doesn't count a marker from that boot, so a `reboot-to` no longer uses up the unclean end (one already counted stays counted); the CP's ordered power-off and the NV capture still come only from the unit's stop, so the rule stands. [MODEM-PLAN](device/cheetah/MODEM-PLAN.md) M4.6b ## 3. How the tools fit together diff --git a/packages/omamobile-device-akita/src/reboot-to.c b/packages/omamobile-device-akita/src/reboot-to.c index f79e948ed..7ba6f5a2f 100644 --- a/packages/omamobile-device-akita/src/reboot-to.c +++ b/packages/omamobile-device-akita/src/reboot-to.c @@ -3,7 +3,9 @@ // e.g. "reboot-to bootloader" lands in fastboot. // The kernel's syscon-reboot-mode driver stores the reason in the PMU // before the reset; busybox reboot can't pass one. +#include #include +#include #include #include @@ -12,10 +14,89 @@ #define LINUX_REBOOT_MAGIC2 672274793 #define LINUX_REBOOT_CMD_RESTART2 0xA1B2C3D4 +/* + * The modem's start refuses after a boot that ended with the modem up and + * no clean stop (a hang or a reset). This restart skips systemd's shutdown, + * so it notes the boot it ends, before rebooting: the next start then knows + * this boot ended on purpose. Best effort: without /var/lib/omamobile (the + * initramfs) there is no note, and the start refuses as before. + * + * Only a boot with the modem up writes the note: its marker (the 7 Pro's + * modem-up, the 9a's modem-bringup-started) names this boot. A restart in + * a later boot without the modem would otherwise replace the note, and the + * boot that had the modem up would read as a hang at the next start. + */ +#ifndef NOTE_DIR +#define NOTE_DIR "/var/lib/omamobile" +#endif +#define NOTE NOTE_DIR "/deliberate-restart" + +static int marker_names_boot(const char *path, const char *id) +{ + char buf[512], want[80]; + FILE *f = fopen(path, "r"); + size_t n; + char *p; + + if (!f) + return 0; + n = fread(buf, 1, sizeof(buf) - 1, f); + fclose(f); + buf[n] = '\0'; + /* No printf family here: the initramfs links this file against musl + * without compiler builtins, and musl's vfprintf needs them. */ + strcpy(want, "boot "); + strcat(want, id); + for (p = strstr(buf, want); p; p = strstr(p + 1, want)) { + char end = p[strlen(want)]; + + if (end == '\n' || end == '\0') + return 1; + } + return 0; +} + +static void note_deliberate_restart(void) +{ + char id[64]; + FILE *f = fopen("/proc/sys/kernel/random/boot_id", "r"); + int fd, dir; + + if (!f) + return; + if (!fgets(id, sizeof(id), f)) { + fclose(f); + return; + } + fclose(f); + id[strcspn(id, "\n")] = '\0'; + if (!marker_names_boot(NOTE_DIR "/modem-up", id) && + !marker_names_boot(NOTE_DIR "/modem-bringup-started", id)) + return; + strcat(id, "\n"); + fd = open(NOTE ".new", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); + if (fd < 0) + return; + if (write(fd, id, strlen(id)) != (ssize_t)strlen(id) || fsync(fd)) { + close(fd); + unlink(NOTE ".new"); + return; + } + close(fd); + if (rename(NOTE ".new", NOTE)) + return; + dir = open(NOTE_DIR, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (dir >= 0) { + fsync(dir); + close(dir); + } +} + int main(int argc, char **argv) { const char *reason = argc > 1 ? argv[1] : "bootloader"; + note_deliberate_restart(); sync(); syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, reason); diff --git a/packages/omamobile-device-akita/version b/packages/omamobile-device-akita/version index 17e51c385..d917d3e26 100644 --- a/packages/omamobile-device-akita/version +++ b/packages/omamobile-device-akita/version @@ -1 +1 @@ -0.1.1 +0.1.2 diff --git a/packages/omamobile-device-cheetah/src/reboot-to.c b/packages/omamobile-device-cheetah/src/reboot-to.c index f79e948ed..7ba6f5a2f 100644 --- a/packages/omamobile-device-cheetah/src/reboot-to.c +++ b/packages/omamobile-device-cheetah/src/reboot-to.c @@ -3,7 +3,9 @@ // e.g. "reboot-to bootloader" lands in fastboot. // The kernel's syscon-reboot-mode driver stores the reason in the PMU // before the reset; busybox reboot can't pass one. +#include #include +#include #include #include @@ -12,10 +14,89 @@ #define LINUX_REBOOT_MAGIC2 672274793 #define LINUX_REBOOT_CMD_RESTART2 0xA1B2C3D4 +/* + * The modem's start refuses after a boot that ended with the modem up and + * no clean stop (a hang or a reset). This restart skips systemd's shutdown, + * so it notes the boot it ends, before rebooting: the next start then knows + * this boot ended on purpose. Best effort: without /var/lib/omamobile (the + * initramfs) there is no note, and the start refuses as before. + * + * Only a boot with the modem up writes the note: its marker (the 7 Pro's + * modem-up, the 9a's modem-bringup-started) names this boot. A restart in + * a later boot without the modem would otherwise replace the note, and the + * boot that had the modem up would read as a hang at the next start. + */ +#ifndef NOTE_DIR +#define NOTE_DIR "/var/lib/omamobile" +#endif +#define NOTE NOTE_DIR "/deliberate-restart" + +static int marker_names_boot(const char *path, const char *id) +{ + char buf[512], want[80]; + FILE *f = fopen(path, "r"); + size_t n; + char *p; + + if (!f) + return 0; + n = fread(buf, 1, sizeof(buf) - 1, f); + fclose(f); + buf[n] = '\0'; + /* No printf family here: the initramfs links this file against musl + * without compiler builtins, and musl's vfprintf needs them. */ + strcpy(want, "boot "); + strcat(want, id); + for (p = strstr(buf, want); p; p = strstr(p + 1, want)) { + char end = p[strlen(want)]; + + if (end == '\n' || end == '\0') + return 1; + } + return 0; +} + +static void note_deliberate_restart(void) +{ + char id[64]; + FILE *f = fopen("/proc/sys/kernel/random/boot_id", "r"); + int fd, dir; + + if (!f) + return; + if (!fgets(id, sizeof(id), f)) { + fclose(f); + return; + } + fclose(f); + id[strcspn(id, "\n")] = '\0'; + if (!marker_names_boot(NOTE_DIR "/modem-up", id) && + !marker_names_boot(NOTE_DIR "/modem-bringup-started", id)) + return; + strcat(id, "\n"); + fd = open(NOTE ".new", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); + if (fd < 0) + return; + if (write(fd, id, strlen(id)) != (ssize_t)strlen(id) || fsync(fd)) { + close(fd); + unlink(NOTE ".new"); + return; + } + close(fd); + if (rename(NOTE ".new", NOTE)) + return; + dir = open(NOTE_DIR, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (dir >= 0) { + fsync(dir); + close(dir); + } +} + int main(int argc, char **argv) { const char *reason = argc > 1 ? argv[1] : "bootloader"; + note_deliberate_restart(); sync(); syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, reason); diff --git a/packages/omamobile-device-cheetah/test/all b/packages/omamobile-device-cheetah/test/all index 7f12e191f..01d3b48d3 100755 --- a/packages/omamobile-device-cheetah/test/all +++ b/packages/omamobile-device-cheetah/test/all @@ -17,6 +17,17 @@ pass "build stays inside the package directory" ./build "$tmp/build" || fail "C tools compile" pass "C tools compile" +# reboot-to.c is also linked into the initramfs, and for a phone without +# bootctl that link is against musl with no compiler builtins +# (bin/omamobile initramfs). musl's vfprintf needs those builtins +# (__addtf3, __extenddftf2 and others, for long double), so one printf-family +# call stops that build with "reboot-to failed to link", and CI doesn't build +# that path. The other device packages hold their copy to this one. +if grep -nE '\ #include +#include #include #include @@ -12,10 +14,89 @@ #define LINUX_REBOOT_MAGIC2 672274793 #define LINUX_REBOOT_CMD_RESTART2 0xA1B2C3D4 +/* + * The modem's start refuses after a boot that ended with the modem up and + * no clean stop (a hang or a reset). This restart skips systemd's shutdown, + * so it notes the boot it ends, before rebooting: the next start then knows + * this boot ended on purpose. Best effort: without /var/lib/omamobile (the + * initramfs) there is no note, and the start refuses as before. + * + * Only a boot with the modem up writes the note: its marker (the 7 Pro's + * modem-up, the 9a's modem-bringup-started) names this boot. A restart in + * a later boot without the modem would otherwise replace the note, and the + * boot that had the modem up would read as a hang at the next start. + */ +#ifndef NOTE_DIR +#define NOTE_DIR "/var/lib/omamobile" +#endif +#define NOTE NOTE_DIR "/deliberate-restart" + +static int marker_names_boot(const char *path, const char *id) +{ + char buf[512], want[80]; + FILE *f = fopen(path, "r"); + size_t n; + char *p; + + if (!f) + return 0; + n = fread(buf, 1, sizeof(buf) - 1, f); + fclose(f); + buf[n] = '\0'; + /* No printf family here: the initramfs links this file against musl + * without compiler builtins, and musl's vfprintf needs them. */ + strcpy(want, "boot "); + strcat(want, id); + for (p = strstr(buf, want); p; p = strstr(p + 1, want)) { + char end = p[strlen(want)]; + + if (end == '\n' || end == '\0') + return 1; + } + return 0; +} + +static void note_deliberate_restart(void) +{ + char id[64]; + FILE *f = fopen("/proc/sys/kernel/random/boot_id", "r"); + int fd, dir; + + if (!f) + return; + if (!fgets(id, sizeof(id), f)) { + fclose(f); + return; + } + fclose(f); + id[strcspn(id, "\n")] = '\0'; + if (!marker_names_boot(NOTE_DIR "/modem-up", id) && + !marker_names_boot(NOTE_DIR "/modem-bringup-started", id)) + return; + strcat(id, "\n"); + fd = open(NOTE ".new", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); + if (fd < 0) + return; + if (write(fd, id, strlen(id)) != (ssize_t)strlen(id) || fsync(fd)) { + close(fd); + unlink(NOTE ".new"); + return; + } + close(fd); + if (rename(NOTE ".new", NOTE)) + return; + dir = open(NOTE_DIR, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (dir >= 0) { + fsync(dir); + close(dir); + } +} + int main(int argc, char **argv) { const char *reason = argc > 1 ? argv[1] : "bootloader"; + note_deliberate_restart(); sync(); syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, reason); diff --git a/packages/omamobile-device-felix/version b/packages/omamobile-device-felix/version index c946ee616..11808190d 100644 --- a/packages/omamobile-device-felix/version +++ b/packages/omamobile-device-felix/version @@ -1 +1 @@ -0.1.6 +0.1.7 diff --git a/packages/omamobile-device-husky/src/reboot-to.c b/packages/omamobile-device-husky/src/reboot-to.c index f79e948ed..7ba6f5a2f 100644 --- a/packages/omamobile-device-husky/src/reboot-to.c +++ b/packages/omamobile-device-husky/src/reboot-to.c @@ -3,7 +3,9 @@ // e.g. "reboot-to bootloader" lands in fastboot. // The kernel's syscon-reboot-mode driver stores the reason in the PMU // before the reset; busybox reboot can't pass one. +#include #include +#include #include #include @@ -12,10 +14,89 @@ #define LINUX_REBOOT_MAGIC2 672274793 #define LINUX_REBOOT_CMD_RESTART2 0xA1B2C3D4 +/* + * The modem's start refuses after a boot that ended with the modem up and + * no clean stop (a hang or a reset). This restart skips systemd's shutdown, + * so it notes the boot it ends, before rebooting: the next start then knows + * this boot ended on purpose. Best effort: without /var/lib/omamobile (the + * initramfs) there is no note, and the start refuses as before. + * + * Only a boot with the modem up writes the note: its marker (the 7 Pro's + * modem-up, the 9a's modem-bringup-started) names this boot. A restart in + * a later boot without the modem would otherwise replace the note, and the + * boot that had the modem up would read as a hang at the next start. + */ +#ifndef NOTE_DIR +#define NOTE_DIR "/var/lib/omamobile" +#endif +#define NOTE NOTE_DIR "/deliberate-restart" + +static int marker_names_boot(const char *path, const char *id) +{ + char buf[512], want[80]; + FILE *f = fopen(path, "r"); + size_t n; + char *p; + + if (!f) + return 0; + n = fread(buf, 1, sizeof(buf) - 1, f); + fclose(f); + buf[n] = '\0'; + /* No printf family here: the initramfs links this file against musl + * without compiler builtins, and musl's vfprintf needs them. */ + strcpy(want, "boot "); + strcat(want, id); + for (p = strstr(buf, want); p; p = strstr(p + 1, want)) { + char end = p[strlen(want)]; + + if (end == '\n' || end == '\0') + return 1; + } + return 0; +} + +static void note_deliberate_restart(void) +{ + char id[64]; + FILE *f = fopen("/proc/sys/kernel/random/boot_id", "r"); + int fd, dir; + + if (!f) + return; + if (!fgets(id, sizeof(id), f)) { + fclose(f); + return; + } + fclose(f); + id[strcspn(id, "\n")] = '\0'; + if (!marker_names_boot(NOTE_DIR "/modem-up", id) && + !marker_names_boot(NOTE_DIR "/modem-bringup-started", id)) + return; + strcat(id, "\n"); + fd = open(NOTE ".new", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); + if (fd < 0) + return; + if (write(fd, id, strlen(id)) != (ssize_t)strlen(id) || fsync(fd)) { + close(fd); + unlink(NOTE ".new"); + return; + } + close(fd); + if (rename(NOTE ".new", NOTE)) + return; + dir = open(NOTE_DIR, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (dir >= 0) { + fsync(dir); + close(dir); + } +} + int main(int argc, char **argv) { const char *reason = argc > 1 ? argv[1] : "bootloader"; + note_deliberate_restart(); sync(); syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, reason); diff --git a/packages/omamobile-device-lynx/src/reboot-to.c b/packages/omamobile-device-lynx/src/reboot-to.c index f79e948ed..7ba6f5a2f 100644 --- a/packages/omamobile-device-lynx/src/reboot-to.c +++ b/packages/omamobile-device-lynx/src/reboot-to.c @@ -3,7 +3,9 @@ // e.g. "reboot-to bootloader" lands in fastboot. // The kernel's syscon-reboot-mode driver stores the reason in the PMU // before the reset; busybox reboot can't pass one. +#include #include +#include #include #include @@ -12,10 +14,89 @@ #define LINUX_REBOOT_MAGIC2 672274793 #define LINUX_REBOOT_CMD_RESTART2 0xA1B2C3D4 +/* + * The modem's start refuses after a boot that ended with the modem up and + * no clean stop (a hang or a reset). This restart skips systemd's shutdown, + * so it notes the boot it ends, before rebooting: the next start then knows + * this boot ended on purpose. Best effort: without /var/lib/omamobile (the + * initramfs) there is no note, and the start refuses as before. + * + * Only a boot with the modem up writes the note: its marker (the 7 Pro's + * modem-up, the 9a's modem-bringup-started) names this boot. A restart in + * a later boot without the modem would otherwise replace the note, and the + * boot that had the modem up would read as a hang at the next start. + */ +#ifndef NOTE_DIR +#define NOTE_DIR "/var/lib/omamobile" +#endif +#define NOTE NOTE_DIR "/deliberate-restart" + +static int marker_names_boot(const char *path, const char *id) +{ + char buf[512], want[80]; + FILE *f = fopen(path, "r"); + size_t n; + char *p; + + if (!f) + return 0; + n = fread(buf, 1, sizeof(buf) - 1, f); + fclose(f); + buf[n] = '\0'; + /* No printf family here: the initramfs links this file against musl + * without compiler builtins, and musl's vfprintf needs them. */ + strcpy(want, "boot "); + strcat(want, id); + for (p = strstr(buf, want); p; p = strstr(p + 1, want)) { + char end = p[strlen(want)]; + + if (end == '\n' || end == '\0') + return 1; + } + return 0; +} + +static void note_deliberate_restart(void) +{ + char id[64]; + FILE *f = fopen("/proc/sys/kernel/random/boot_id", "r"); + int fd, dir; + + if (!f) + return; + if (!fgets(id, sizeof(id), f)) { + fclose(f); + return; + } + fclose(f); + id[strcspn(id, "\n")] = '\0'; + if (!marker_names_boot(NOTE_DIR "/modem-up", id) && + !marker_names_boot(NOTE_DIR "/modem-bringup-started", id)) + return; + strcat(id, "\n"); + fd = open(NOTE ".new", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); + if (fd < 0) + return; + if (write(fd, id, strlen(id)) != (ssize_t)strlen(id) || fsync(fd)) { + close(fd); + unlink(NOTE ".new"); + return; + } + close(fd); + if (rename(NOTE ".new", NOTE)) + return; + dir = open(NOTE_DIR, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (dir >= 0) { + fsync(dir); + close(dir); + } +} + int main(int argc, char **argv) { const char *reason = argc > 1 ? argv[1] : "bootloader"; + note_deliberate_restart(); sync(); syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, reason); diff --git a/packages/omamobile-device-lynx/version b/packages/omamobile-device-lynx/version index ee1372d33..717903969 100644 --- a/packages/omamobile-device-lynx/version +++ b/packages/omamobile-device-lynx/version @@ -1 +1 @@ -0.2.2 +0.2.3 diff --git a/packages/omamobile-device-oriole/src/reboot-to.c b/packages/omamobile-device-oriole/src/reboot-to.c index f79e948ed..7ba6f5a2f 100644 --- a/packages/omamobile-device-oriole/src/reboot-to.c +++ b/packages/omamobile-device-oriole/src/reboot-to.c @@ -3,7 +3,9 @@ // e.g. "reboot-to bootloader" lands in fastboot. // The kernel's syscon-reboot-mode driver stores the reason in the PMU // before the reset; busybox reboot can't pass one. +#include #include +#include #include #include @@ -12,10 +14,89 @@ #define LINUX_REBOOT_MAGIC2 672274793 #define LINUX_REBOOT_CMD_RESTART2 0xA1B2C3D4 +/* + * The modem's start refuses after a boot that ended with the modem up and + * no clean stop (a hang or a reset). This restart skips systemd's shutdown, + * so it notes the boot it ends, before rebooting: the next start then knows + * this boot ended on purpose. Best effort: without /var/lib/omamobile (the + * initramfs) there is no note, and the start refuses as before. + * + * Only a boot with the modem up writes the note: its marker (the 7 Pro's + * modem-up, the 9a's modem-bringup-started) names this boot. A restart in + * a later boot without the modem would otherwise replace the note, and the + * boot that had the modem up would read as a hang at the next start. + */ +#ifndef NOTE_DIR +#define NOTE_DIR "/var/lib/omamobile" +#endif +#define NOTE NOTE_DIR "/deliberate-restart" + +static int marker_names_boot(const char *path, const char *id) +{ + char buf[512], want[80]; + FILE *f = fopen(path, "r"); + size_t n; + char *p; + + if (!f) + return 0; + n = fread(buf, 1, sizeof(buf) - 1, f); + fclose(f); + buf[n] = '\0'; + /* No printf family here: the initramfs links this file against musl + * without compiler builtins, and musl's vfprintf needs them. */ + strcpy(want, "boot "); + strcat(want, id); + for (p = strstr(buf, want); p; p = strstr(p + 1, want)) { + char end = p[strlen(want)]; + + if (end == '\n' || end == '\0') + return 1; + } + return 0; +} + +static void note_deliberate_restart(void) +{ + char id[64]; + FILE *f = fopen("/proc/sys/kernel/random/boot_id", "r"); + int fd, dir; + + if (!f) + return; + if (!fgets(id, sizeof(id), f)) { + fclose(f); + return; + } + fclose(f); + id[strcspn(id, "\n")] = '\0'; + if (!marker_names_boot(NOTE_DIR "/modem-up", id) && + !marker_names_boot(NOTE_DIR "/modem-bringup-started", id)) + return; + strcat(id, "\n"); + fd = open(NOTE ".new", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); + if (fd < 0) + return; + if (write(fd, id, strlen(id)) != (ssize_t)strlen(id) || fsync(fd)) { + close(fd); + unlink(NOTE ".new"); + return; + } + close(fd); + if (rename(NOTE ".new", NOTE)) + return; + dir = open(NOTE_DIR, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (dir >= 0) { + fsync(dir); + close(dir); + } +} + int main(int argc, char **argv) { const char *reason = argc > 1 ? argv[1] : "bootloader"; + note_deliberate_restart(); sync(); syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, reason); diff --git a/packages/omamobile-device-panther/src/reboot-to.c b/packages/omamobile-device-panther/src/reboot-to.c index f79e948ed..7ba6f5a2f 100644 --- a/packages/omamobile-device-panther/src/reboot-to.c +++ b/packages/omamobile-device-panther/src/reboot-to.c @@ -3,7 +3,9 @@ // e.g. "reboot-to bootloader" lands in fastboot. // The kernel's syscon-reboot-mode driver stores the reason in the PMU // before the reset; busybox reboot can't pass one. +#include #include +#include #include #include @@ -12,10 +14,89 @@ #define LINUX_REBOOT_MAGIC2 672274793 #define LINUX_REBOOT_CMD_RESTART2 0xA1B2C3D4 +/* + * The modem's start refuses after a boot that ended with the modem up and + * no clean stop (a hang or a reset). This restart skips systemd's shutdown, + * so it notes the boot it ends, before rebooting: the next start then knows + * this boot ended on purpose. Best effort: without /var/lib/omamobile (the + * initramfs) there is no note, and the start refuses as before. + * + * Only a boot with the modem up writes the note: its marker (the 7 Pro's + * modem-up, the 9a's modem-bringup-started) names this boot. A restart in + * a later boot without the modem would otherwise replace the note, and the + * boot that had the modem up would read as a hang at the next start. + */ +#ifndef NOTE_DIR +#define NOTE_DIR "/var/lib/omamobile" +#endif +#define NOTE NOTE_DIR "/deliberate-restart" + +static int marker_names_boot(const char *path, const char *id) +{ + char buf[512], want[80]; + FILE *f = fopen(path, "r"); + size_t n; + char *p; + + if (!f) + return 0; + n = fread(buf, 1, sizeof(buf) - 1, f); + fclose(f); + buf[n] = '\0'; + /* No printf family here: the initramfs links this file against musl + * without compiler builtins, and musl's vfprintf needs them. */ + strcpy(want, "boot "); + strcat(want, id); + for (p = strstr(buf, want); p; p = strstr(p + 1, want)) { + char end = p[strlen(want)]; + + if (end == '\n' || end == '\0') + return 1; + } + return 0; +} + +static void note_deliberate_restart(void) +{ + char id[64]; + FILE *f = fopen("/proc/sys/kernel/random/boot_id", "r"); + int fd, dir; + + if (!f) + return; + if (!fgets(id, sizeof(id), f)) { + fclose(f); + return; + } + fclose(f); + id[strcspn(id, "\n")] = '\0'; + if (!marker_names_boot(NOTE_DIR "/modem-up", id) && + !marker_names_boot(NOTE_DIR "/modem-bringup-started", id)) + return; + strcat(id, "\n"); + fd = open(NOTE ".new", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); + if (fd < 0) + return; + if (write(fd, id, strlen(id)) != (ssize_t)strlen(id) || fsync(fd)) { + close(fd); + unlink(NOTE ".new"); + return; + } + close(fd); + if (rename(NOTE ".new", NOTE)) + return; + dir = open(NOTE_DIR, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (dir >= 0) { + fsync(dir); + close(dir); + } +} + int main(int argc, char **argv) { const char *reason = argc > 1 ? argv[1] : "bootloader"; + note_deliberate_restart(); sync(); syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, reason); diff --git a/packages/omamobile-device-panther/version b/packages/omamobile-device-panther/version index 7ac4e5e38..71d6a66ed 100644 --- a/packages/omamobile-device-panther/version +++ b/packages/omamobile-device-panther/version @@ -1 +1 @@ -0.1.13 +0.1.14 diff --git a/packages/omamobile-device-raven/src/reboot-to.c b/packages/omamobile-device-raven/src/reboot-to.c index f79e948ed..7ba6f5a2f 100644 --- a/packages/omamobile-device-raven/src/reboot-to.c +++ b/packages/omamobile-device-raven/src/reboot-to.c @@ -3,7 +3,9 @@ // e.g. "reboot-to bootloader" lands in fastboot. // The kernel's syscon-reboot-mode driver stores the reason in the PMU // before the reset; busybox reboot can't pass one. +#include #include +#include #include #include @@ -12,10 +14,89 @@ #define LINUX_REBOOT_MAGIC2 672274793 #define LINUX_REBOOT_CMD_RESTART2 0xA1B2C3D4 +/* + * The modem's start refuses after a boot that ended with the modem up and + * no clean stop (a hang or a reset). This restart skips systemd's shutdown, + * so it notes the boot it ends, before rebooting: the next start then knows + * this boot ended on purpose. Best effort: without /var/lib/omamobile (the + * initramfs) there is no note, and the start refuses as before. + * + * Only a boot with the modem up writes the note: its marker (the 7 Pro's + * modem-up, the 9a's modem-bringup-started) names this boot. A restart in + * a later boot without the modem would otherwise replace the note, and the + * boot that had the modem up would read as a hang at the next start. + */ +#ifndef NOTE_DIR +#define NOTE_DIR "/var/lib/omamobile" +#endif +#define NOTE NOTE_DIR "/deliberate-restart" + +static int marker_names_boot(const char *path, const char *id) +{ + char buf[512], want[80]; + FILE *f = fopen(path, "r"); + size_t n; + char *p; + + if (!f) + return 0; + n = fread(buf, 1, sizeof(buf) - 1, f); + fclose(f); + buf[n] = '\0'; + /* No printf family here: the initramfs links this file against musl + * without compiler builtins, and musl's vfprintf needs them. */ + strcpy(want, "boot "); + strcat(want, id); + for (p = strstr(buf, want); p; p = strstr(p + 1, want)) { + char end = p[strlen(want)]; + + if (end == '\n' || end == '\0') + return 1; + } + return 0; +} + +static void note_deliberate_restart(void) +{ + char id[64]; + FILE *f = fopen("/proc/sys/kernel/random/boot_id", "r"); + int fd, dir; + + if (!f) + return; + if (!fgets(id, sizeof(id), f)) { + fclose(f); + return; + } + fclose(f); + id[strcspn(id, "\n")] = '\0'; + if (!marker_names_boot(NOTE_DIR "/modem-up", id) && + !marker_names_boot(NOTE_DIR "/modem-bringup-started", id)) + return; + strcat(id, "\n"); + fd = open(NOTE ".new", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); + if (fd < 0) + return; + if (write(fd, id, strlen(id)) != (ssize_t)strlen(id) || fsync(fd)) { + close(fd); + unlink(NOTE ".new"); + return; + } + close(fd); + if (rename(NOTE ".new", NOTE)) + return; + dir = open(NOTE_DIR, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (dir >= 0) { + fsync(dir); + close(dir); + } +} + int main(int argc, char **argv) { const char *reason = argc > 1 ? argv[1] : "bootloader"; + note_deliberate_restart(); sync(); syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, reason); diff --git a/packages/omamobile-device-raven/version b/packages/omamobile-device-raven/version index 9dd179330..a52e04166 100644 --- a/packages/omamobile-device-raven/version +++ b/packages/omamobile-device-raven/version @@ -1 +1 @@ -0.1.34 +0.1.35 diff --git a/packages/omamobile-device-tegu/src/reboot-to.c b/packages/omamobile-device-tegu/src/reboot-to.c index f79e948ed..7ba6f5a2f 100644 --- a/packages/omamobile-device-tegu/src/reboot-to.c +++ b/packages/omamobile-device-tegu/src/reboot-to.c @@ -3,7 +3,9 @@ // e.g. "reboot-to bootloader" lands in fastboot. // The kernel's syscon-reboot-mode driver stores the reason in the PMU // before the reset; busybox reboot can't pass one. +#include #include +#include #include #include @@ -12,10 +14,89 @@ #define LINUX_REBOOT_MAGIC2 672274793 #define LINUX_REBOOT_CMD_RESTART2 0xA1B2C3D4 +/* + * The modem's start refuses after a boot that ended with the modem up and + * no clean stop (a hang or a reset). This restart skips systemd's shutdown, + * so it notes the boot it ends, before rebooting: the next start then knows + * this boot ended on purpose. Best effort: without /var/lib/omamobile (the + * initramfs) there is no note, and the start refuses as before. + * + * Only a boot with the modem up writes the note: its marker (the 7 Pro's + * modem-up, the 9a's modem-bringup-started) names this boot. A restart in + * a later boot without the modem would otherwise replace the note, and the + * boot that had the modem up would read as a hang at the next start. + */ +#ifndef NOTE_DIR +#define NOTE_DIR "/var/lib/omamobile" +#endif +#define NOTE NOTE_DIR "/deliberate-restart" + +static int marker_names_boot(const char *path, const char *id) +{ + char buf[512], want[80]; + FILE *f = fopen(path, "r"); + size_t n; + char *p; + + if (!f) + return 0; + n = fread(buf, 1, sizeof(buf) - 1, f); + fclose(f); + buf[n] = '\0'; + /* No printf family here: the initramfs links this file against musl + * without compiler builtins, and musl's vfprintf needs them. */ + strcpy(want, "boot "); + strcat(want, id); + for (p = strstr(buf, want); p; p = strstr(p + 1, want)) { + char end = p[strlen(want)]; + + if (end == '\n' || end == '\0') + return 1; + } + return 0; +} + +static void note_deliberate_restart(void) +{ + char id[64]; + FILE *f = fopen("/proc/sys/kernel/random/boot_id", "r"); + int fd, dir; + + if (!f) + return; + if (!fgets(id, sizeof(id), f)) { + fclose(f); + return; + } + fclose(f); + id[strcspn(id, "\n")] = '\0'; + if (!marker_names_boot(NOTE_DIR "/modem-up", id) && + !marker_names_boot(NOTE_DIR "/modem-bringup-started", id)) + return; + strcat(id, "\n"); + fd = open(NOTE ".new", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); + if (fd < 0) + return; + if (write(fd, id, strlen(id)) != (ssize_t)strlen(id) || fsync(fd)) { + close(fd); + unlink(NOTE ".new"); + return; + } + close(fd); + if (rename(NOTE ".new", NOTE)) + return; + dir = open(NOTE_DIR, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (dir >= 0) { + fsync(dir); + close(dir); + } +} + int main(int argc, char **argv) { const char *reason = argc > 1 ? argv[1] : "bootloader"; + note_deliberate_restart(); sync(); syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, reason); diff --git a/packages/omamobile-modem/lib/omamobile-modem-start b/packages/omamobile-modem/lib/omamobile-modem-start index 7f4af084a..43f4d9989 100755 --- a/packages/omamobile-modem/lib/omamobile-modem-start +++ b/packages/omamobile-modem/lib/omamobile-modem-start @@ -64,6 +64,7 @@ MARK=${OMAMOBILE_MODEM_LATCH:-/var/lib/omamobile/modem-bringup-started} UP=${OMAMOBILE_MODEM_UP_MARK:-/var/lib/omamobile/modem-up} HOLD=${OMAMOBILE_MODEM_HOLD:-/etc/omamobile/modem-hold} REFUSED=${OMAMOBILE_MODEM_REFUSED:-/run/omamobile/modem-refused} +NOTE=${OMAMOBILE_DELIBERATE_RESTART:-/var/lib/omamobile/deliberate-restart} LIB=/usr/lib/omamobile-vendor # What tools/phone-provision puts on the phone (the unit's conditions name # the same three): without them the modem can't start, so nothing is @@ -166,6 +167,20 @@ if [[ -e $MARK ]]; then say "the last bring-up ($(cat "$MARK")) never reached ONLINE; skipping the modem (omamobile-modem-start --retry)" refuse "Off: last bring-up hung" fi +# reboot-to notes the boot it ends (/var/lib/omamobile/deliberate-restart), +# since it skips systemd's shutdown and so the unit's stop: a modem-up marker +# from that very boot ended on purpose (bin/omamobile push, a reboot to the +# bootloader), not in a hang or a reset, so it isn't counted as an unclean +# end (two of those in a row hold the modem off). It doesn't end a streak +# either: an unclean end already counted stays counted, as on the 9a, since +# the note only says a restart was asked for, where a clean stop has seen +# the CP powered off with the phone alive. A hang in the restart itself is +# the same gap a clean systemctl reboot has. +if [[ -e $UP && -s $NOTE ]] && grep -q "boot $(head -c 64 "$NOTE" | tr -d '\n')\$" "$UP"; then + say "the last boot with the modem up ($(cat "$UP")) ended with reboot-to, on purpose: $UP removed, not counted as an unclean end" + rm -f "$UP" "$NOTE" + sync +fi [[ -e $UP ]] && refuse_unclean # Past the marker decision. The test's stop point (see the header): nothing diff --git a/packages/omamobile-modem/test/all b/packages/omamobile-modem/test/all index 020ea7209..4b0f8abb4 100755 --- a/packages/omamobile-modem/test/all +++ b/packages/omamobile-modem/test/all @@ -339,6 +339,36 @@ out=$(start) && fail "the second unclean boot in a row is refused" [[ $(cat "$m/run/refused") == "Off after a hang or reset (held)" ]] || fail "the held reason for the tile: $(cat "$m/run/refused")" out=$(start) && fail "the held marker refuses the boot after too" [[ $out == *"no clean stop since the last refusal"* && -e $m/up && $(ls "$m"/up.refused-* | wc -l) -eq 1 ]] || fail "held: the marker and the one copy stay: $out" +# reboot-to's note (in a directory of its own, so the state above is kept): +# a modem-up marker from the boot the note names ended on purpose (push, a +# reboot to the bootloader) and isn't counted: the marker and the note go and +# the start gets past the markers, also where an unclean end is already +# counted. That counted copy stays: the note doesn't end a streak, so a hang, +# a push and a second hang are held. A note naming another boot changes +# nothing: the marker is counted as an unclean end as above, and with one +# already counted it is held. +n=$t/note-markers; mkdir -p "$n/run" +nstart() { OMAMOBILE_MODEM_LATCH=$n/latch OMAMOBILE_MODEM_UP_MARK=$n/up OMAMOBILE_MODEM_HOLD=$n/hold OMAMOBILE_MODEM_REFUSED=$n/run/refused \ + OMAMOBILE_DELIBERATE_RESTART=$n/note OMAMOBILE_MODEM_TEST_STOP_AFTER_CHECKS=1 timeout 20 bash $s 2>&1; } +printf '%s boot %s\n' 2026-01-01T00:00:00+00:00 00000000-0000-0000-0000-000000000000 > "$n/up" +printf '%s\n' 11111111-1111-1111-1111-111111111111 > "$n/note" +out=$(nstart) || fail "a note from another boot leaves the first unclean end to start as without it (rc=$?): $out" +[[ -e $n/note && $out == *"ended without a clean stop"* && $out != *"ended with reboot-to"* ]] || fail "a note from another boot clears nothing: $out" +[[ ! -e $n/up && $(ls "$n"/up.refused-* | wc -l) -eq 1 ]] || fail "a note from another boot: the marker is counted as an unclean end: $(ls "$n")" +printf '%s boot %s\n' 2026-01-01T00:00:00+00:00 00000000-0000-0000-0000-000000000000 > "$n/up" +out=$(nstart) && fail "a note from another boot doesn't save the second unclean end from the hold: $out" +[[ -e $n/note && -e $n/up && $out == *"no clean stop since the last refusal"* && $out != *"ended with reboot-to"* ]] || fail "a note from another boot, a second unclean end: held: $out" +rm -f "$n"/up* "$n/note" "$n/run/refused" +printf '%s boot %s\n' 2026-01-01T00:00:00+00:00 00000000-0000-0000-0000-000000000000 > "$n/up" +: > "$n/up.refused-x" +printf '%s\n' 00000000-0000-0000-0000-000000000000 > "$n/note" +out=$(nstart) || fail "a deliberate restart gets past the markers (rc=$?): $out" +[[ $out == *"ended with reboot-to"* && $out == *"$stop_line"* && $out != *"ended without a clean stop"* ]] || fail "a deliberate restart isn't a hang: $out" +[[ ! -e $n/up && ! -e $n/note && ! -e $n/latch ]] || fail "the note clears the marker and itself, and writes no latch: $(ls "$n")" +[[ -e $n/up.refused-x ]] || fail "the note keeps an unclean end already counted: $(ls "$n")" +printf '%s boot %s\n' 2026-01-01T00:00:00+00:00 22222222-2222-2222-2222-222222222222 > "$n/up" +out=$(nstart) && fail "a hang, a deliberate restart and a second hang are held: $out" +[[ -e $n/up && $out == *"no clean stop since the last refusal"* ]] || fail "the streak outlives a deliberate restart: $out" printf '%s boot %s\n' "$(date -Is)" "$(cat /proc/sys/kernel/random/boot_id)" > "$m/up" out=$(SERVICE_RESULT=success bash $s --stopped 2>&1) || fail "--stopped exits 0" [[ ! -e $m/up && -z $(ls "$m"/up.refused-* 2>/dev/null) ]] || fail "a clean stop of this boot's marker removes the refused copies: $(ls "$m")" diff --git a/packages/omamobile-modem/version b/packages/omamobile-modem/version index baa983785..790629964 100644 --- a/packages/omamobile-modem/version +++ b/packages/omamobile-modem/version @@ -1 +1 @@ -0.1.20 +0.1.21