diff --git a/device/raven/MODEM-PLAN.md b/device/raven/MODEM-PLAN.md index 5c2d1dbed..dc865c568 100644 --- a/device/raven/MODEM-PLAN.md +++ b/device/raven/MODEM-PLAN.md @@ -784,3 +784,113 @@ binary: a single-module `make` had rebuilt nothing): - **Next:** what kills the link (the crash record should now keep it); then recovery without a reboot (a CP power cycle that reaches ONLINE). + +## 11. The link dies at system suspend (2026-10-27, `g97f09c8cfada`) + +- **What happened:** the owner took the phone out unplugged and found no + modem. Boot 13:24: the CP came up clean (NV copies matching the + backup), sitd polled the SIM for 54 minutes (it never reported ready + that boot). At 14:18:45 `omamobile-sleepd` suspended (s2idle, "dark and + locked for 11 s", the first time that boot with no USB host). In the + suspend the PM core logged `pcieport 0001:00:00.0: Unable to change + power state from D0 to D3hot, device inaccessible`, then `D3cold to D0` + and `Data Link Layer Link Active not set in 100 msec`; UFS logged its + `dme-set: attr-id 0x4021 ... failed` in the same resume (the M2.5 + pattern). After resume the link trained (`Gen3 x2`), but CPIF's + `s51xx_pcie_restore_state` couldn't set D0 and the doorbell read + `0xffff` (no bus mastering). At 14:18:58 the dead-link check (§10's + fix) forced the crash: `link down for 8968 ms and recovery did not + clear it`, reason 22, ONLINE to CRASH_EXIT. rfsd's NV validation came + out clean; the watcher recorded the crash (its notification sends no + log line, so the journal can't confirm it). + sitd died with a traceback (`EOFError: SIT channel closed`, from its + bring-up SIM poll). The owner rebooted at 15:11. +- **Not a one-off:** the next boot (15:11) lost the CP the same way at + its first suspend, 54 s after boot (crash at 15:12:28, 5072 ms after + the failed resume), and the 21 suspends after it ran without a modem; + nobody noticed. Two suspends with the CP ONLINE, two crashes. §10's + 93-minute link death may have been the same thing before the dead-link + check existed. +- **Fixes:** + - omamobile-modem, in the release #389 lands in: a crash raises a + notification ("Cellular stopped") with the crash record's path; + - omamobile-modem-sit, in the release #389 lands in: when the CP is + gone, sitd exits with one line ("the modem went away ...") instead of + a traceback. + - During bring-up the watcher also held a logind sleep inhibitor while + the CP was ONLINE. It isn't in the package: it would cost every + phone's battery for a raven kernel problem, and the kernel fixes + below make it unnecessary. + (2026-11-04: since omamobile-modem 0.1.13 it is in the package after + all, as `omamobile-modem-inhibit`, which cheetah's MODEM-PLAN 6.3 asks + for on every phone.) +- **Next:** CPIF's system suspend. The root port has to stay reachable + across s2idle, or CPIF has to take the link down itself first (what + its `s5100_pm_notifier` does on stock is still to read). Separately: + why the SIM never became ready at 13:24. + +The three `pcie-gs201-cp` fixes below are on the branch +[`omamobile-gs101-cpif-suspend`](https://github.com/robotosaith/linux-omarchy-mobile/commits/omamobile-gs101-cpif-suspend) +(on `omamobile-gs101-cpif`, not yet signed off), as they ran on +`omamobile-gs101-int-power`. + +**Suspend with the modem up, first fix (2026-10-28, `gdd5e1655ef91`).** The +cause, from the kernel log and Google's raven `pcie-exynos-rc.c`: with the +link idle, `cp_link_down()` leaves the CP PCIe controller unclocked and its +PHY isolated, so the PCI core's save, D3hot and restore of the root port in +a system sleep read all ones and leave it marked D3cold; nothing below it +can then be brought to D0. Google's driver takes the PHY out of isolation +in `prepare` and isolates it again in `complete`. `pcie-gs201-cp` now does +the same (PHY inited, root port config allowed, PERST# asserted, endpoint +closed); not published yet. + +- **S2 (one `rtcwake -m freeze -s 20`, modem ONLINE): passed.** No + "inaccessible" on the root port; the link came straight back; CP ONLINE + 35 s later, SIM 2 on LTE with data, no UFS error handler, no crash. The + endpoint logged "Unable to change power state from D3hot to D0, device + inaccessible" at resume (its link was down) without harm. +- **S3 (five cycles, 30 s awake between):** cycles 1 and 2 were woken by the + modem (`cp2ap_wakeup`) after 2 and 5 s, CP ONLINE after each. Cycle 3 + slept **558 s** (the RTC alarm didn't fire; IRQ 111, the PMIC, woke it) + and the CP was still ONLINE at resume, but the first poweron after it got + `s51xx_pcie_restore_state: Can't set D0`, doorbells read `cmd reg 0xffff`, + and the dead-link check crashed the CP 21 s after resume. Cycles 4-5 ran + with the CP down. No UFS errors in any cycle. +- **Next:** the endpoint. The PCI core also suspends and resumes it with the + link down, fails, and keeps a wrong power state; after a long sleep CPIF's + D0 restore trips over it. Read the PCI core's and CPIF's D0 paths, keep the + endpoint's bookkeeping sane across the sleep (or restore it before CPIF's + restore), then S2/S3 again with a long sleep. + +**Second fix and the soak (2026-10-28, `g378edcff3ed7`).** `pcie-gs201-cp` +now also waits, on every poweron after the first, for the endpoint's Vendor +ID (every 10 ms, up to 1 s) before its state is restored (not published +yet), on the theory that after a long sleep the endpoint wasn't ready +0.1 ms after the link came up (PCIe r6.0 sec 6.6.1). + +- Two 300 s sleeps, three of 20 s: all CP ONLINE 60 s later; the modem woke + the first after 77 s, the PMIC the second after 165 s. +- Two 900 s sleeps asked: the modem woke the phone after 8 and 6 s. +- **Soak, 30 min of back-to-back sleeps (5 s awake between): passed.** 18 + sleeps in 2387 s, about 2234 s asleep, three of about 10 min (591, 591, + 600 s); woken by the modem 14 times, the RTC 3, the power key once. CP + ONLINE after every wake, no crash, no UFS error handler, both SIMs + registered. The new wait never logged (the endpoint answered at once + every time), so the 558 s failure didn't recur but the wait is not proven + to be what prevents it; it stays as a guard. +- **Trial in daily use from 2026-10-28 13:25:** suspend with the modem up + and no sleep inhibitor. Watch for "Cellular stopped" and + `/var/log/omamobile/modem-crashes.log`. + +**The trial's first crash, a race (2026-10-28 14:09, `gdcdd42db122d`).** +During the drain baseline (17 suspends in 20 min, POWER-PLAN) the root +port failed as before the hooks ("Unable to change power state from D0 to +D3hot, device inaccessible"), right after a "PHY isolated": the link went +down between prepare and the PCI core's suspend of the root port, and +cp_link_down() isolated the PHY again. CPIF's restore then got "Can't set +D0" and the dead-link check crashed the CP. The endpoint wait logged +nothing (the endpoint answered at once), so the earlier "slow endpoint" +theory is ruled out. The fix (not published yet): from prepare to complete, +cp_link_down() takes the link down but keeps the PHY inited and the root +port reachable; complete isolates it. On the phone as `g244c690c1fd1`, the +trial continuing. diff --git a/packages/omamobile-device-raven/README.md b/packages/omamobile-device-raven/README.md index 0a0873bc5..5726a3cf3 100644 --- a/packages/omamobile-device-raven/README.md +++ b/packages/omamobile-device-raven/README.md @@ -15,7 +15,7 @@ the counterpart of `omamobile-device-cheetah`. Bring-up and results: | The user may switch the panel off (`bl_power`) and inhibit the touchscreen, for Omarchy's display off | `udev/rules.d/20-omamobile-display.rules` | | Wi-Fi power save on | `NetworkManager/conf.d/omamobile-raven-wifi-powersave.conf` | | Sleep (s2idle) once locked with the panel off, behind `omarchy-toggle omamobile-sleep on` (the watcher, `omamobile-sleepd`, is omamobile-shell's); Wi-Fi stays connected across sleep (unloaded instead after a D3 failure, or with `/etc/omamobile/wifi-unload-for-sleep`) | `omamobile-sleep` (`system-sleep/omamobile`), `sleep.conf.d/10-omamobile.conf` | -| Modem bring-up only: its PCIe driver and CPIF never load at boot, and the modem's units aren't enabled (as on main; `systemctl enable --now omamobile-modem omamobile-sitd` to use it, MODEM-PLAN) | `modprobe.d/omamobile-raven-modem.conf` | +| Modem bring-up only: its PCIe driver and CPIF never load at boot, and the modem's units aren't enabled (as on main; `systemctl enable --now omamobile-modem omamobile-sitd` to use it, MODEM-PLAN); once enabled, sitd waits for CP ONLINE and the data fallback needs the modem's port, as on cheetah | `modprobe.d/omamobile-raven-modem.conf`, `omamobile-sitd.service.d/`, `omamobile-sit-data.service.d/` | | Bluetooth on at boot; the session waits (at most 15 s) for the adapter | `bluetooth.service` (enabled), `sddm.service.d/`, `omamobile-wait-bluetooth` | | The speakers through speaker protection at stage media (raven's own limits: AUDIO-PLAN R6), the microphones with them; stage c9 and earpiece mode stay off on raven | `omamobile-speaker-protection.service` from omamobile-audio 0.5.15 (enabled); `snd_soc_cs35l41_spi` blacklisted here too, so only that service loads it | `omamobile-raven.conf` | diff --git a/packages/omamobile-device-raven/test/all b/packages/omamobile-device-raven/test/all index de62c046a..e1a0af004 100755 --- a/packages/omamobile-device-raven/test/all +++ b/packages/omamobile-device-raven/test/all @@ -110,6 +110,14 @@ grep -q "'omamobile-audio>=0.5.15'" PKGBUILD || fail "device-raven depends on om || fail "raven never sets aoc_alsa_dev speaker_protection (it would allow stage c9)" pass "speakers through protection at stage media, the microphones with them" +# The modem's units, when enabled: sitd waits for CP ONLINE (as on cheetah) +d=$tmp/root/usr/lib/systemd/system/omamobile-sitd.service.d/20-cp-online.conf +grep -qx 'ExecStartPre=/usr/lib/omamobile-vendor/omamobile-modem-cp wait-online 120' "$d" || fail "sitd waits for CP ONLINE with the cbd timeout" +grep -q '^\[Service\]' "$d" && ! grep -q '^\[Unit\]' "$d" || fail "the sitd drop-in only adds to [Service]" +d=$tmp/root/usr/lib/systemd/system/omamobile-sit-data.service.d/20-cp-online.conf +grep -qx 'ConditionPathExists=/dev/umts_ipc0' "$d" || fail "sit-data is skipped without the modem's port" +pass "sitd waits for the CP; sit-data needs the modem's port" + # Policy that keeps the phone safe at boot for m in tcpci_maxim gpio_max77759; do grep -qx "blacklist $m" vendor/modprobe.d/omamobile-raven.conf || fail "$m stays blacklisted" diff --git a/packages/omamobile-device-raven/vendor/systemd/system/omamobile-sit-data.service.d/20-cp-online.conf b/packages/omamobile-device-raven/vendor/systemd/system/omamobile-sit-data.service.d/20-cp-online.conf new file mode 100644 index 000000000..9e8403780 --- /dev/null +++ b/packages/omamobile-device-raven/vendor/systemd/system/omamobile-sit-data.service.d/20-cp-online.conf @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: MIT +# The data fallback only with a modem. omamobile-sit-data requires sitd, but +# a sitd skipped by its own condition (no /dev/umts_ipc0: the modem unit was +# skipped on a phone that isn't provisioned) counts as started, and the +# fallback would then wait 60 s for a bearer, fail, and be restarted every +# 10 s for the whole boot. With the modem up the port exists, and +# `omamobile-sit data on` works as before. +[Unit] +ConditionPathExists=/dev/umts_ipc0 diff --git a/packages/omamobile-device-raven/vendor/systemd/system/omamobile-sitd.service.d/20-cp-online.conf b/packages/omamobile-device-raven/vendor/systemd/system/omamobile-sitd.service.d/20-cp-online.conf new file mode 100644 index 000000000..206b527a2 --- /dev/null +++ b/packages/omamobile-device-raven/vendor/systemd/system/omamobile-sitd.service.d/20-cp-online.conf @@ -0,0 +1,11 @@ +# SPDX-License-Identifier: MIT +# Raven's sitd waits for the CP, as the Pixel 7 Pro's does (device-cheetah's +# drop-in of the same name). omamobile-modem.service is ready once CPIF's +# ports exist, before cbd has booted the CP (about 3 s more on raven), and a +# sitd started on that alone exits ("CP is not ONLINE": at every raven boot +# with the modem enabled, 2026-11-04). 120 s is the profile's own cbd +# timeout: a CP that never comes up fails sitd here, and the data fallback +# and the Phone and Messages bridge, ordered after it, stay down until the +# reboot. +[Service] +ExecStartPre=/usr/lib/omamobile-vendor/omamobile-modem-cp wait-online 120 diff --git a/packages/omamobile-device-raven/version b/packages/omamobile-device-raven/version index a52e04166..072d0fa39 100644 --- a/packages/omamobile-device-raven/version +++ b/packages/omamobile-device-raven/version @@ -1 +1 @@ -0.1.35 +0.1.36 diff --git a/packages/omamobile-modem-sit/lib/service.py b/packages/omamobile-modem-sit/lib/service.py index cacfc0508..3008a89bf 100644 --- a/packages/omamobile-modem-sit/lib/service.py +++ b/packages/omamobile-modem-sit/lib/service.py @@ -969,6 +969,18 @@ def stop(*_): if not serve_one(service, connection, clients): connection.close() service.broadcast() + except (EOFError, OSError, RuntimeError) as error: + try: + online = cp.online() + except OSError: + online = False + if online: + raise + # The CP crashed under us: omamobile-modem.service records it, tells + # the user and leaves the modem alone until a reboot. Say so in one + # line, not as a traceback through the SIT read. + raise SystemExit('the modem went away (%s): calls, SMS and data are ' + 'off until the phone restarts' % error) from None finally: try: if service and service.live() and not service.release_unconfirmed: diff --git a/packages/omamobile-modem-sit/test/test_service.py b/packages/omamobile-modem-sit/test/test_service.py index 5cd82e4d5..3d6f3af50 100644 --- a/packages/omamobile-modem-sit/test/test_service.py +++ b/packages/omamobile-modem-sit/test/test_service.py @@ -248,6 +248,22 @@ def test_unconfirmed_release_turns_radio_off_and_keeps_call_ownership(self): self.assertTrue(self.service.release_unconfirmed) self.assertEqual(self.service.live(), [call]) self.assertTrue(json.loads((self.root/'active.json').read_text())) + def run_main_with_channel_closed(self,online): + # raven, 2026-10-27: the CP crashed during start-up and sitd died with + # a traceback from the SIT read. + with patch('sys.argv',['omamobile-sitd']), \ + patch.object(app_service.paths,'STATE',self.root), \ + patch.object(app_service.cp,'online',side_effect=[True]+online), \ + patch.object(app_service,'Sit'), \ + patch.object(app_service,'database'), \ + patch.object(app_service,'Service',side_effect=EOFError('SIT channel closed')), \ + patch.object(app_service.signal,'signal'): + app_service.main() + def test_a_crashed_cp_ends_the_service_in_one_line(self): + with self.assertRaises(SystemExit) as raised: self.run_main_with_channel_closed([False]) + self.assertIn('the modem went away (SIT channel closed)',str(raised.exception.code)) + def test_a_closed_channel_with_the_cp_online_still_raises(self): + with self.assertRaises(EOFError): self.run_main_with_channel_closed([True]) class RefusedAudioTests(unittest.TestCase): diff --git a/packages/omamobile-modem-sit/version b/packages/omamobile-modem-sit/version index 9f558ed0f..1ff34fb37 100644 --- a/packages/omamobile-modem-sit/version +++ b/packages/omamobile-modem-sit/version @@ -1 +1 @@ -0.3.34 +0.3.35 diff --git a/packages/omamobile-modem/PKGBUILD b/packages/omamobile-modem/PKGBUILD index f010fc4bc..67221801e 100644 --- a/packages/omamobile-modem/PKGBUILD +++ b/packages/omamobile-modem/PKGBUILD @@ -3,7 +3,7 @@ pkgname=omamobile-modem pkgver=$(cat "${startdir:-.}/version") pkgrel=1 -pkgdesc="Omarchy Mobile modem runtime for the Pixel 7 Pro: stock rfsd and cbd in a vendor chroot on RAM copies of NV, with a per-phone overlay for the modem's own NV writes" +pkgdesc="Omarchy Mobile modem runtime for the Pixel 7 Pro and 6 Pro: stock rfsd and cbd in a vendor chroot on RAM copies of NV, with a per-phone overlay for the modem's own NV writes" arch=(aarch64) url="https://github.com/derekross/omarchy-mobile" license=(MIT) diff --git a/packages/omamobile-modem/lib/omamobile-modem-cp b/packages/omamobile-modem/lib/omamobile-modem-cp index 9f0166ce5..c0d1ca5ce 100755 --- a/packages/omamobile-modem/lib/omamobile-modem-cp +++ b/packages/omamobile-modem/lib/omamobile-modem-cp @@ -14,7 +14,8 @@ date and boot id, which the unit's clean stop removes); when it leaves ONLINE afterwards, record the crash once and keep running (nothing restarts - or powers off the CP) + or powers off the CP); the user is told unless + this invocation's stop intent is in place baseband-version FILE the firmware version in a modem.bin's TOC nv-check EFS EFS_BACKUP the stock salted MD5 of the three NV files in the RAM copies; exit 1 on a mismatch @@ -28,6 +29,8 @@ import fcntl import hashlib import os from pathlib import Path +import re +import shutil import struct import subprocess import sys @@ -123,6 +126,106 @@ def power_off(): ALLOW.write_text(previous) +# The modem's kernel lines: CPIF and the PCIe link to the modem. Not +# CPIF's open/release lines for umts_boot0: this tool's own ioctls. +KERNEL_KEYS = ('cpif', 'mif:', 'pcie-cp', '.pcie:', 's51xx') +KERNEL_NOISE = ('bootdump_open', 'bootdump_release') + + +def kernel_lines(kernel, keep=150): + """The modem's last kernel lines, a run of the same message folded + into one line with its count (a dead link repeats one message + thousands of times, and would push out how it started).""" + out, last, count = [], None, 0 + for line in kernel: + low = line.lower() + if not any(k in low for k in KERNEL_KEYS) or any(n in low for n in KERNEL_NOISE): + continue + text = line.split('] ', 1)[-1] + if text == last: + count += 1 + continue + if count: + out.append(f' (repeated {count} more times)') + out.append(line) + last, count = text, 0 + if count: + out.append(f' (repeated {count} more times)') + return out[-keep:] + + +def host_root(): + """Where PID 1's /run/user is seen from here. The watcher runs in the + profile's private mount namespace, made at boot before the user's + /run/user/ is mounted, so inside it that directory stays empty; + /proc/1/root reads through PID 1's mounts. Empty when the namespaces + match.""" + try: + same = os.readlink('/proc/self/ns/mnt') == os.readlink('/proc/1/ns/mnt') + except OSError: + same = True + return '' if same else '/proc/1/root' + + +def send_command(uid, bus, send): + """The send as a transient unit of PID 1's, as the user: it runs in the + host's namespaces and its own cgroup, never the profile's. From inside + the profile, busctl --user (omarchy-notification-send) is refused even + in PID 1's mount namespace ("Failed to connect to user scope bus via + local transport: Operation not permitted", raven, 2026-11-11). Its + output goes to the journal.""" + return ['systemd-run', '--quiet', '--wait', '--collect', f'--uid={uid}', + f'--setenv=DBUS_SESSION_BUS_ADDRESS=unix:path={bus}', + f'--setenv=XDG_RUNTIME_DIR=/run/user/{uid}', '--'] + send + + +def sessions(root=''): + """(uid, bus) for each logged-in user with a session bus under + root/run/user, read at the call: a bus that appears after the modem + started is found.""" + try: + uids = sorted(int(u) for u in os.listdir(f'{root}/run/user') if u.isdigit() and int(u) >= 1000) + except OSError: + return [] + return [(uid, f'/run/user/{uid}/bus') for uid in uids if os.path.exists(f'{root}/run/user/{uid}/bus')] + + +def notify(title, body): + """A critical notification in every logged-in session, as root.""" + cmd = os.environ.get('OMAMOBILE_NOTIFY_CMD') + if cmd: + subprocess.run([cmd, title, body], timeout=30, check=False) + say(f'notified: {title} (through {cmd})') + return + if os.geteuid() != 0: + say(f'not notified: {title} (not root)') + return + found = sessions(host_root()) + sent = 0 + for uid, bus in found: + if shutil.which('omarchy-notification-send'): + send = ['omarchy-notification-send', '-u', 'critical', title, body] + elif shutil.which('notify-send'): + send = ['notify-send', '-u', 'critical', '-a', 'Omarchy Mobile', title, body] + else: + say(f'not notified: {title} (no notification tool)') + return + try: + result = subprocess.run(send_command(uid, bus, send), timeout=15, check=False, + stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) + except (OSError, subprocess.SubprocessError) as error: + say(f'not notified: uid {uid}: {error}') + continue + if result.returncode == 0: + sent += 1 + else: + # The last line only: it says why (a missing bus, a refused + # unit), and the notification text isn't repeated. + why = (result.stderr or b'').decode(errors='replace').strip().splitlines()[-1:] or ['no message'] + say(f'not notified: uid {uid}: exit {result.returncode}: {why[0][:200]}') + say(f'notified {sent} of {len(found)} session(s): {title}') + + def record_crash(): """Before anything else touches the CP, keep what it left (stock's cbd would collect a dump; that path is not used here). Root-only, never in @@ -145,7 +248,7 @@ def record_crash(): entry.append(f'boot log unavailable: {error}') try: kernel = subprocess.run(['dmesg'], capture_output=True, text=True, timeout=10).stdout.splitlines() - entry += [line for line in kernel if 'cpif' in line.lower() or 'mif:' in line][-60:] + entry += kernel_lines(kernel) except (OSError, subprocess.SubprocessError) as error: entry.append(f'kernel log unavailable: {error}') say(entry[0]) @@ -176,6 +279,22 @@ def watch_seconds(): return seconds +def stop_intended(): + """This unit invocation's orderly stop is under way: profile_stop wrote + $RUN/intent-, holding that ID, before the power-off. The + same test as rfsd's job in the profile (spawn_modem_rfsd): a 32-hex ID, a + regular file that isn't a symlink, its content the ID.""" + invocation = os.environ.get('INVOCATION_ID', '') + if not re.fullmatch(r'[0-9a-f]{32}', invocation): + return False + intent = RUN / f'intent-{invocation}' + try: + return (not intent.is_symlink() and intent.is_file() + and intent.read_text().rstrip('\n') == invocation) + except OSError: + return False + + def watch(marker=None): seconds = watch_seconds() was_online = False @@ -215,8 +334,17 @@ def watch(marker=None): record_crash() RUN.mkdir(parents=True, exist_ok=True) (RUN / 'crashed').write_text(STATES.get(now, str(now)) + '\n') - say('the CP left ONLINE: crash recorded; nothing restarts it. ' - 'Reboot to recover (the reboot powers it off).') + # A stop powers the CP off while this still polls: no notice + # then (#389 review). The record stays for now (#532). + if stop_intended(): + say("the CP left ONLINE during this invocation's stop: the modem was stopped, " + 'nothing sent') + else: + say('the CP left ONLINE: crash recorded; nothing restarts it. ' + 'Reboot to recover (the reboot powers it off).') + notify('Cellular stopped', + 'The modem crashed: no calls, texts or mobile data until the phone restarts. ' + f'Details: {CRASH_LOG}') time.sleep(seconds) diff --git a/packages/omamobile-modem/lib/omamobile-modem-nv-overlay b/packages/omamobile-modem/lib/omamobile-modem-nv-overlay index b5cc770a7..b44444b80 100755 --- a/packages/omamobile-modem/lib/omamobile-modem-nv-overlay +++ b/packages/omamobile-modem/lib/omamobile-modem-nv-overlay @@ -27,9 +27,10 @@ of the partitions, checked against the off-repo backup's checksums before anything here runs; the overlay is bound to those checksums, so a new backup sets it aside. -Granularity: whole files. rfsd keeps the modem's NV as files in the three f2fs -filesystems (nv_normal.bin and nv_protected.bin, each with a salted MD5 beside -it), not as raw blocks, so a file is the unit that can be allowed or refused. +Granularity: whole files. rfsd keeps the modem's NV as files in the three NV +filesystems (f2fs on gs201, ext4 on gs101: nv_normal.bin and nv_protected.bin, +each with a salted MD5 beside it), not as raw blocks, so a file is the unit +that can be allowed or refused. Store layout (written only by capture): MANIFEST header, binding, entries, its own checksum (0600) diff --git a/packages/omamobile-modem/modem/props.conf b/packages/omamobile-modem/modem/props.conf index fad706c5f..da1bf5d2d 100644 --- a/packages/omamobile-modem/modem/props.conf +++ b/packages/omamobile-modem/modem/props.conf @@ -7,6 +7,7 @@ # What Android's init would have set from the bootloader. The slot is the # one the RAM copy of the modem image is named after (by-name/modem_a). +# cheetah's; a gs101 phone overrides both in $STATE/props.conf, naming its board ro.boot.hardware.platform=gs201 ro.hardware=cheetah ro.build.type=user diff --git a/packages/omamobile-modem/profiles/modem.conf b/packages/omamobile-modem/profiles/modem.conf index 8efcb772b..6eaebe96c 100644 --- a/packages/omamobile-modem/profiles/modem.conf +++ b/packages/omamobile-modem/profiles/modem.conf @@ -15,7 +15,8 @@ # since the backup, so take a fresh backup first. # - The chroot gets loop devices of the copies: the modem image read-only # (by-name/modem_a and /mnt/vendor/modem_img), efs, efs_backup and -# modem_userdata read-write (f2fs), and a copy of persist/modem. +# modem_userdata read-write (f2fs on gs201, ext4 on gs101: mounted as +# what the copy is), and a copy of persist/modem. # - The NV overlay (MODEM-PLAN "NV overlay", off unless # /etc/omamobile/modem-nv-overlay-enable exists): the modem's own # writes to allowed files (nv_normal.bin and its checksum, never the @@ -104,6 +105,25 @@ modem_cdt_check() { return 0 } +# The device the daemons are told about: the package's props.conf says +# cheetah (gs201); on a gs101 phone the per-phone $STATE/props.conf, read +# after it, must name this board (raven for google,gs101-raven, oriole for +# google,gs101-oriole, ...), or cbd and rfsd would take another board's +# paths. $1: the file; MODEM_DT_COMPATIBLE: the compatible list (tests). +modem_device_check() { + local f=$1 board hw plat + board=$(tr '\0' '\n' < "${MODEM_DT_COMPATIBLE:-/proc/device-tree/compatible}" 2>/dev/null | + sed -n 's/^google,gs101-//p' | head -1) + [[ -n $board ]] || return 0 + hw=$(sed -n 's/^ro\.hardware=//p' "$f" 2>/dev/null | tail -1) + plat=$(sed -n 's/^ro\.boot\.hardware\.platform=//p' "$f" 2>/dev/null | tail -1) + if [[ $hw != "$board" || $plat != gs101 ]]; then + say "a gs101 phone ($board): $f needs ro.hardware=$board and ro.boot.hardware.platform=gs101" + return 1 + fi + return 0 +} + profile_check() { local ok=0 n p d modem_once_check @@ -123,8 +143,7 @@ profile_check() { [[ -s $NV_SUMS ]] || { say "no $NV_SUMS (checksums from the off-repo backup)"; ok=1; } [[ -x $MODEM_CP ]] || { say "no $MODEM_CP"; ok=1; } [[ -x $MODEM_NV_OVERLAY ]] || { say "no $MODEM_NV_OVERLAY"; ok=1; } - grep -qw f2fs /proc/filesystems || modprobe -q f2fs 2>/dev/null || - { say "no f2fs in the kernel (CONFIG_F2FS_FS)"; ok=1; } + modem_device_check "$STATE/props.conf" || ok=1 return $ok } @@ -160,7 +179,7 @@ modem_gnss_nodes() { modem_gnss() { [[ $MODEM_GNSS == 1 ]] && modem_gnss_nodes; } profile_setup() { - local p l mnt=$ROOT/mnt/vendor + local p l t mnt=$ROOT/mnt/vendor mkdir -p "$NV" mount -t tmpfs -o mode=0700,nosuid,nodev,noexec,size=640m modem-nv "$NV" for p in modem_a efs efs_backup modem_userdata persist; do modem_copy "$p"; done @@ -174,8 +193,13 @@ profile_setup() { mount -t ext4 -o ro,noatime,noload "$l" "$mnt/modem_img" modem_autoclear "$l" for p in efs efs_backup modem_userdata; do + t=$(blkid -o value -s TYPE "$NV/$p.img" || true) + [[ $t == ext4 || $t == f2fs ]] || die "$p is ${t:-of no known type}, not ext4 or f2fs: stopping" + if [[ $t == f2fs ]] && ! grep -qw f2fs /proc/filesystems; then + modprobe -q f2fs || die "no f2fs in the kernel (CONFIG_F2FS_FS)" + fi l=$(modem_loop "$NV/$p.img" rw) - mount -t f2fs -o noatime "$l" "$mnt/$p" + mount -t "$t" -o noatime "$l" "$mnt/$p" modem_autoclear "$l" done # The overlay over the verified copies (it records the base first, then diff --git a/packages/omamobile-modem/systemd/omamobile-modem.service b/packages/omamobile-modem/systemd/omamobile-modem.service index 8f1de2d7f..d7c0670bd 100644 --- a/packages/omamobile-modem/systemd/omamobile-modem.service +++ b/packages/omamobile-modem/systemd/omamobile-modem.service @@ -32,7 +32,7 @@ # captures them every 30 minutes; both do nothing without # /etc/omamobile/modem-nv-overlay-enable. [Unit] -Description=Omarchy Mobile modem (S5300: rfsd and cbd on RAM copies of NV) +Description=Omarchy Mobile modem (S5300 or S5123: rfsd and cbd on RAM copies of NV) Requires=omamobile-modem-inhibit.service After=omamobile-modem-inhibit.service systemd-modules-load.service omamobile-aoc.service omamobile-audio.service Wants=omamobile-modem-nv-overlay.timer diff --git a/packages/omamobile-modem/test/all b/packages/omamobile-modem/test/all index 4b0f8abb4..a0d00d21b 100755 --- a/packages/omamobile-modem/test/all +++ b/packages/omamobile-modem/test/all @@ -44,6 +44,11 @@ pass "rfsd's exit ends the profile except during this invocation's orderly stop" python3 test/power-off.py || fail "omamobile-modem-cp power-off" pass "power-off confirms OFFLINE from the device, never from its absence" +# The crash notice: not during this invocation's orderly stop (its intent +# file, as rfsd's job tests it), once otherwise (#389 review) +python3 test/watch-stop.py || fail "omamobile-modem-cp watch through a stop" +pass "no crash notice during this invocation's stop" + # The stop witness: profile_stop, with a stand-in CP helper and capture. A # failed power-off still captures the NV but fails the stop and leaves no # witness; a confirmed one writes its invocation's witness once; no main @@ -122,6 +127,37 @@ import sys; g = {"__name__": "cp"}; exec(open(sys.argv[1]).read(), g); print(g[" fail "a bad watch interval leaves other subcommands alone" pass "watch interval" +# The crash notice reaches a session whose bus appears after the modem +# started: the watcher's private mount namespace is made at boot, before +# /run/user/ is mounted, so it looks through PID 1's view at each call, +# and sends as a transient unit of PID 1's, outside the profile (#389 review) +mkdir -p "$t/host/run/user/1000" "$t/host/run/user/0" +out=$(python3 - "$cp" "$t/host" 2>/dev/null <<'PY' +import sys, types +g = {"__name__": "cp"} +exec(open(sys.argv[1]).read(), g) +host = sys.argv[2] +calls = [] +g["host_root"] = lambda: host +g["os"].geteuid = lambda: 0 +g["shutil"].which = lambda name: "/usr/bin/" + name if name == "omarchy-notification-send" else None +g["subprocess"].run = lambda argv, **kw: calls.append(argv) or types.SimpleNamespace(returncode=0, stderr=b"") +g["notify"]("Cellular stopped", "test") +print("before", len(calls)) +open(host + "/run/user/1000/bus", "w").close() +g["notify"]("Cellular stopped", "test") +print("after", len(calls), " ".join(calls[-1]) if calls else "") +print("sessions", g["sessions"](host)) +PY +) +[[ $out == *"before 0"* ]] || fail "no bus yet: nothing sent ($out)" +[[ $out == *"after 1 systemd-run --quiet --wait --collect --uid=1000 --setenv=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus --setenv=XDG_RUNTIME_DIR=/run/user/1000 -- omarchy-notification-send -u critical Cellular stopped test"* ]] || + fail "the bus that appeared later is used, through a transient unit as the user ($out)" +[[ $out == *"[(1000, '/run/user/1000/bus')]"* ]] || fail "sessions: uids from 1000 only, the bus path as the session sees it ($out)" +view=$(python3 -c 'import sys; g = {"__name__": "cp"}; exec(open(sys.argv[1]).read(), g); print(repr(g["host_root"]()))' "$cp") +[[ $view == "''" ]] || fail "the same mount namespace as PID 1: its own /run ($view)" +pass "crash notice outside the profile, bus found at each call" + mkdir -p "$t/efs" "$t/efs_backup" for f in efs/nv_normal.bin efs/nv_protected.bin efs_backup/nv_protected.bak; do printf 'made-up %s' "$f" > "$t/$f" @@ -449,5 +485,41 @@ printf 'ro.boot.cdt_hwid=TESTID\n' > "$t/p4"; cdt "$t/p4" || fail "the persist c cdt "$t/missing" && fail "a missing props file is refused" pass "board id required" +# The board the daemons are told about: on a gs101 phone, props.conf names +# that board (from google,gs101-); other SoCs aren't checked here +dev() { bash -c 'say() { :; }; eval "$(sed -n "/^modem_device_check() {/,/^}/p" profiles/modem.conf)"; modem_device_check "$1"' _ "$1"; } +compat() { printf '%s\0' "$@" > "$t/compat"; } +export MODEM_DT_COMPATIBLE=$t/compat +printf 'ro.hardware=raven\nro.boot.hardware.platform=gs101\n' > "$t/raven.props" +printf 'ro.hardware=oriole\nro.boot.hardware.platform=gs101\n' > "$t/oriole.props" +compat google,gs101-raven google,gs101; dev "$t/raven.props" || fail "raven with raven's props passes" +dev "$t/oriole.props" && fail "raven with oriole's props is refused" +compat google,gs101-oriole google,gs101; dev "$t/oriole.props" || fail "oriole with oriole's props passes" +dev "$t/raven.props" && fail "oriole with raven's props is refused" +printf 'ro.hardware=bluejay\nro.boot.hardware.platform=gs101\n' > "$t/bluejay.props" +compat google,gs101-bluejay google,gs101; dev "$t/bluejay.props" || fail "bluejay with bluejay's props passes" +printf 'ro.hardware=raven\nro.boot.hardware.platform=gs201\n' > "$t/badplat.props" +compat google,gs101-raven google,gs101; dev "$t/badplat.props" && fail "a gs101 board with another platform is refused" +compat google,gs201-cheetah google,gs201; dev "$t/missing" || fail "cheetah (gs201) isn't checked here" +unset MODEM_DT_COMPATIBLE +pass "a gs101 board's props name that board" + +# The crash record's kernel lines: the modem's lines only, without this +# tool's own umts_boot0 opens, and a flood folded into one line and a count +python3 - "$cp" <<'PY' || fail "crash record folds a repeated kernel line" +import importlib.machinery, importlib.util, sys +loader = importlib.machinery.SourceFileLoader('cp', sys.argv[1]) +cp = importlib.util.module_from_spec(importlib.util.spec_from_loader('cp', loader)) +loader.exec_module(cp) +log = ['[1.0] gs201-pcie-cp 11920000.pcie: link-down recovery requested', + '[1.1] wlan: unrelated'] +log += [f'[2.{i}] cpif: s51xx_pcie_send_doorbell_int: cmd reg = 0xffff' for i in range(5000)] +log += ['[3.0] cpif: bootdump_open: umts_boot0 (opened 2) by python3', + '[3.1] cpif: link down for 5000 ms and recovery did not clear it: force crash'] +out = cp.kernel_lines(log) +assert out == [log[0], log[2], ' (repeated 4999 more times)', log[-1]], out +assert len(cp.kernel_lines(log[2:3] * 3 + [f'[4.{i}] cpif: x{i}' for i in range(300)])) == 150 +PY +pass "crash record folds a repeated kernel line" # The NV overlay: its own tests (synthetic NV-like files only). test/nv-overlay diff --git a/packages/omamobile-modem/test/nv-overlay b/packages/omamobile-modem/test/nv-overlay index 9e78f6a77..e66d0944a 100755 --- a/packages/omamobile-modem/test/nv-overlay +++ b/packages/omamobile-modem/test/nv-overlay @@ -272,7 +272,7 @@ pass "set-aside copies bounded" # the CP power-off; the CLI's commands; the units. conf=profiles/modem.conf setup=$(sed -n '/^profile_setup() {$/,/^}$/p' $conf) -m=$(grep -n 'mount -t f2fs -o noatime "$l" "$mnt/$p"' <<<"$setup" | cut -d: -f1); a=$(grep -n 'MODEM_NV_OVERLAY" apply' <<<"$setup" | cut -d: -f1); c=$(grep -n 'MODEM_CP" nv-check' <<<"$setup" | cut -d: -f1) +m=$(grep -n 'mount -t "$t" -o noatime "$l" "$mnt/$p"' <<<"$setup" | cut -d: -f1); a=$(grep -n 'MODEM_NV_OVERLAY" apply' <<<"$setup" | cut -d: -f1); c=$(grep -n 'MODEM_CP" nv-check' <<<"$setup" | cut -d: -f1) [[ -n $m && -n $a && -n $c && $m -lt $a && $a -lt $c ]] || fail "apply runs after the f2fs mounts and before nv-check (mount=$m apply=$a check=$c)" grep -q 'modem_copy "\$p"; done' <<<"$setup" || fail "the base copies are still read and checked first" stop=$(sed -n '/^profile_stop() {$/,/^}$/p' $conf) diff --git a/packages/omamobile-modem/test/watch-stop.py b/packages/omamobile-modem/test/watch-stop.py new file mode 100644 index 000000000..7401bcdf4 --- /dev/null +++ b/packages/omamobile-modem/test/watch-stop.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: MIT +"""omamobile-modem-cp watch when the CP leaves ONLINE: during this unit +invocation's orderly stop (profile_stop's intent file, holding the ID) no +notice goes out; without one, or with a wrong or symlinked intent, one does +(#389 review).""" +import importlib.machinery +import importlib.util +import os +from pathlib import Path +import tempfile +import unittest +from unittest.mock import patch + +loader = importlib.machinery.SourceFileLoader('modem_cp', 'lib/omamobile-modem-cp') +cp = importlib.util.module_from_spec(importlib.util.spec_from_loader('modem_cp', loader)) +loader.exec_module(cp) + +INVOCATION = '0123456789abcdef0123456789abcdef' +OTHER = 'fedcba9876543210fedcba9876543210' + + +class Done(Exception): + pass + + +class WatchStop(unittest.TestCase): + def run_watch(self, intent=None, symlink=False): + """ONLINE, then OFFLINE; returns the notices sent.""" + with tempfile.TemporaryDirectory() as directory: + run = Path(directory) / 'run' + run.mkdir() + if intent is not None: + target = run / f'intent-{INVOCATION}' + if symlink: + real = Path(directory) / 'elsewhere' + real.write_text(intent + '\n') + target.symlink_to(real) + else: + target.write_text(intent + '\n') + states = iter([cp.ONLINE, 0, 0]) + sleeps = [] + + def sleep(seconds): + sleeps.append(seconds) + if len(sleeps) == 3: + raise Done + + sent = [] + with patch.object(cp, 'RUN', run), \ + patch.dict(os.environ, {'INVOCATION_ID': INVOCATION}), \ + patch.object(cp.os, 'open', return_value=99), \ + patch.object(cp.fcntl, 'ioctl', side_effect=lambda fd, cmd: next(states)), \ + patch.object(cp, 'record_crash'), \ + patch.object(cp, 'notify', side_effect=lambda title, body: sent.append(title)), \ + patch.object(cp.time, 'sleep', side_effect=sleep): + with self.assertRaises(Done): + cp.watch() + return sent + + def test_stop_with_this_invocations_intent_sends_nothing(self): + self.assertEqual(self.run_watch(intent=INVOCATION), []) + + def test_without_intent_sends_once(self): + self.assertEqual(self.run_watch(), ['Cellular stopped']) + + def test_wrong_intent_sends_once(self): + self.assertEqual(self.run_watch(intent=OTHER), ['Cellular stopped']) + + def test_symlinked_intent_sends_once(self): + self.assertEqual(self.run_watch(intent=INVOCATION, symlink=True), ['Cellular stopped']) + + def test_no_invocation_id_is_no_intent(self): + with patch.dict(os.environ, {'INVOCATION_ID': ''}): + self.assertFalse(cp.stop_intended()) + + +if __name__ == '__main__': + unittest.main() diff --git a/packages/omamobile-modem/version b/packages/omamobile-modem/version index 790629964..7e72641b4 100644 --- a/packages/omamobile-modem/version +++ b/packages/omamobile-modem/version @@ -1 +1 @@ -0.1.21 +0.1.22