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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/omamobile-modem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ configuration cannot reassign MARK/HOLD or redefine refuse. The normal marker,
unclean-stop and hold decisions remain unchanged. Device-specific configuration
does not authorize starting a modem or replace its watchdog/backup/recovery
qualification. The maintainer allocates the package version at merge.

# On or off from the shell

`omamobile-modem-switch` (in `/usr/lib/omamobile-vendor/`) backs the Modem
switch on the shell's Mobile network page. It is offered only where
something starts `omamobile-modem-boot.target` at boot (the Pixel 7 Pro's
device package links it into `multi-user.target`) or where the switch has
masked it; elsewhere it refuses. "Off" masks the target and stops nothing in
this boot; "on" unmasks it and starts the stack only if this boot's modem
never started. The polkit action `org.omamobile.modem.switch` lets the user
of the active local session switch it without a password; anyone else is
refused.
2 changes: 2 additions & 0 deletions packages/omamobile-modem/install
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ v=$d/usr/lib/omamobile-vendor
install -Dm755 lib/omamobile-modem-start "$v/omamobile-modem-start"
install -Dm755 lib/omamobile-modem-cp "$v/omamobile-modem-cp"
install -Dm755 lib/omamobile-modem-nv-overlay "$v/omamobile-modem-nv-overlay"
install -Dm755 lib/omamobile-modem-switch "$v/omamobile-modem-switch"
install -Dm644 polkit/org.omamobile.modem.policy "$d/usr/share/polkit-1/actions/org.omamobile.modem.policy"
install -Dm755 bin/omamobile-modem "$d/usr/bin/omamobile-modem"
install -Dm644 profiles/modem.conf "$v/profiles/modem.conf"
install -Dm644 modem/props.conf "$v/modem/props.conf"
Expand Down
114 changes: 114 additions & 0 deletions packages/omamobile-modem/lib/omamobile-modem-switch
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/bin/bash
# SPDX-License-Identifier: MIT
# omamobile-modem-switch: the modem stack on or off at boot, for the shell's
# Mobile network page (omamobile-mobile-network modem on|off, under pkexec:
# org.omamobile.modem.switch, the active local user).
#
# omamobile-modem-switch status one JSON object, any user (reads only)
# omamobile-modem-switch on unmask omamobile-modem-boot.target and,
# if the modem hasn't started this boot,
# start the stack now
# omamobile-modem-switch off mask omamobile-modem-boot.target: the
# stack stays off from the next boot
#
# The switch is offered only where something starts the target at boot:
# it is wanted by multi-user.target (the 7 Pro's device-cheetah vendor
# link, or an /etc link) or masked by this switch. Elsewhere (the 9a has
# its own modem start, the 6 Pro none at boot) status says
# "available": false, the page hides the switch, and "on" and "off" refuse,
# so the shell never starts a modem outside that phone's own starter.
#
# "off" stops nothing in this boot. Once the modem's drivers have loaded,
# its sleep inhibitor stays until the reboot whatever happens to the modem
# unit (omamobile-modem-inhibit; only a start refused before any driver
# loads releases it), and a modem powered off can't be started again before
# a reboot (the profile's once-per-boot mark), so a stop now would cost the
# phone its modem for the boot and still not let it sleep; and every modem
# power-off is followed by one recovered UFS error-handler pass (MODEM-PLAN
# M2.6). The page offers the restart instead. "on" starts the stack only
# when this boot's modem has never started (no
# /run/omamobile-modem/profile-started): the same start as at boot, through
# the target, never a second CP power-on.
#
# status:
# {"available": true, "enabled": true, "running": true, "started": true,
# "provisioned": true, "sleep_blocked": true, "restart_needed": false}
# available: the target is wanted at boot or masked (above). enabled:
# available and not masked (it starts at boot). running: the modem unit is
# active. started: the modem profile started this boot. provisioned: the
# three files tools/phone-provision puts there exist (without them the
# units skip themselves). sleep_blocked: the modem's sleep inhibitor is
# held. restart_needed: what the switch says differs from this boot (off
# but started, or on but started and stopped).
set -euo pipefail

TARGET=omamobile-modem-boot.target
MARK=${OMAMOBILE_MODEM_BOOT_MARK:-/run/omamobile-modem/profile-started}
SYSTEMCTL=${OMAMOBILE_SYSTEMCTL:-systemctl}
PROVISION=(/usr/lib/omamobile-vendor/modem/root /etc/omamobile/modem-nv.sha256 /var/lib/omamobile-vendor/modem/props.conf)
[[ -n ${OMAMOBILE_MODEM_PROVISION:-} ]] && read -ra PROVISION <<< "$OMAMOBILE_MODEM_PROVISION"

die() { echo "omamobile-modem-switch: $*" >&2; exit 1; }

masked() { [[ $("$SYSTEMCTL" is-enabled "$TARGET" 2>/dev/null || true) == masked* ]]; }
# WantedBy sees both the vendor link under /usr/lib and links under /etc;
# `is-enabled` reports a vendor link as "static" or "disabled".
wanted() { [[ " $("$SYSTEMCTL" show -p WantedBy --value "$TARGET" 2>/dev/null || true) " == *" multi-user.target "* ]]; }
available() { wanted || masked; }
enabled() { available && ! masked; }
sleep_blocked() { "$SYSTEMCTL" is-active --quiet omamobile-modem-inhibit.service; }
running() { "$SYSTEMCTL" is-active --quiet omamobile-modem.service; }
started() { [[ -e $MARK ]]; }
provisioned() {
local p
for p in "${PROVISION[@]}"; do
[[ -e $p ]] && continue
# status runs as the user, and props.conf sits in a root-only folder
# (the phone's board id): a folder that is there but can't be searched
# counts as provisioned here; "on" runs as root and sees the file.
[[ -d ${p%/*} && ! -x ${p%/*} ]] && continue
return 1
done
}
json_bool() { if "$@"; then echo true; else echo false; fi; }

status() {
local a e r s p b need=false
a=$(json_bool available); e=$(json_bool enabled); r=$(json_bool running); s=$(json_bool started)
p=$(json_bool provisioned); b=$(json_bool sleep_blocked)
if [[ $a == false ]]; then
need=false
elif [[ $e == false && $s == true ]] || [[ $e == true && $s == true && $r == false ]]; then
need=true
fi
printf '{"available": %s, "enabled": %s, "running": %s, "started": %s, "provisioned": %s, "sleep_blocked": %s, "restart_needed": %s}\n' \
"$a" "$e" "$r" "$s" "$p" "$b" "$need"
}

case ${1:-} in
status)
[[ $# -eq 1 ]] || die "usage: omamobile-modem-switch status|on|off"
status
;;
on|off)
[[ $# -eq 1 ]] || die "usage: omamobile-modem-switch status|on|off"
[[ ${OMAMOBILE_MODEM_SWITCH_UID:-$(id -u)} -eq 0 ]] || die "run as root (the shell uses pkexec)"
available || die "nothing starts the modem at boot on this phone ($TARGET isn't wanted by multi-user.target): no switch here"
if [[ $1 == off ]]; then
"$SYSTEMCTL" mask "$TARGET" >/dev/null
else
"$SYSTEMCTL" unmask "$TARGET" >/dev/null
# Started at boot or by an earlier "on": leave it (the unit itself
# refuses a second CP power-on). Not provisioned: the units would skip
# themselves anyway. Not wanted once unmasked (a hand mask on a phone
# without the boot link): nothing to start here.
if wanted && ! started && ! running && provisioned; then
"$SYSTEMCTL" start --no-block "$TARGET"
fi
fi
status
;;
*)
die "usage: omamobile-modem-switch status|on|off"
;;
esac
22 changes: 22 additions & 0 deletions packages/omamobile-modem/polkit/org.omamobile.modem.policy
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- SPDX-License-Identifier: MIT -->
<!DOCTYPE policyconfig PUBLIC "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
"http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<!-- The shell's Mobile network page: the modem stack on or off at boot,
for the user at the phone (the active local session), through a helper
that only masks or unmasks omamobile-modem-boot.target and starts it
when this boot's modem never started. -->
<policyconfig>
<vendor>Omarchy Mobile</vendor>
<vendor_url>https://github.com/derekross/omarchy-mobile</vendor_url>
<action id="org.omamobile.modem.switch">
<description>Turn the modem on or off</description>
<message>Authentication is required to turn the modem on or off</message>
<defaults>
<allow_any>no</allow_any>
<allow_inactive>no</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">/usr/lib/omamobile-vendor/omamobile-modem-switch</annotate>
</action>
</policyconfig>
97 changes: 94 additions & 3 deletions packages/omamobile-modem/test/all
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ pass() { printf 'ok - %s\n' "$1"; }
export PYTHONDONTWRITEBYTECODE=1
cp=lib/omamobile-modem-cp

bash -n install && bash -n lib/omamobile-modem-start && bash -n profiles/modem.conf || fail "shell syntax"
bash -n install && bash -n lib/omamobile-modem-start && bash -n profiles/modem.conf &&
bash -n lib/omamobile-modem-switch || fail "shell syntax"
python3 -c 'import ast, sys; ast.parse(open(sys.argv[1]).read())' "$cp" || fail "$cp parses"
if command -v shellcheck >/dev/null; then
shellcheck -S warning lib/omamobile-modem-start install || fail "shellcheck"
shellcheck -S warning lib/omamobile-modem-start lib/omamobile-modem-switch install || fail "shellcheck"
fi
pass "scripts parse"
python3 test/test_link.py || fail "device link module parameters and load order"
Expand Down Expand Up @@ -140,7 +141,8 @@ for f in usr/share/doc/omamobile-modem/README.md usr/lib/omamobile-vendor/omamob
usr/lib/omamobile-vendor/profiles/modem.conf usr/lib/omamobile-vendor/modem/props.conf \
usr/lib/systemd/system/omamobile-modem.service usr/lib/udev/rules.d/60-omamobile-modem-nv-ro.rules \
usr/lib/omamobile-vendor/omamobile-modem-nv-overlay usr/bin/omamobile-modem \
usr/lib/systemd/system/omamobile-modem-nv-overlay.service usr/lib/systemd/system/omamobile-modem-nv-overlay.timer; do
usr/lib/systemd/system/omamobile-modem-nv-overlay.service usr/lib/systemd/system/omamobile-modem-nv-overlay.timer \
usr/lib/omamobile-vendor/omamobile-modem-switch usr/share/polkit-1/actions/org.omamobile.modem.policy; do
[[ -e $stage/$f ]] || fail "staged $f"
done
grep -qx 'persist.vendor.modem.efs.clear.action=none' modem/props.conf ||
Expand Down Expand Up @@ -449,5 +451,94 @@ 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 on/off switch (the shell's Mobile network page): a fake systemctl
# records what it's asked. "off" only masks; "on" unmasks and starts the
# target only when this boot's modem never started and the phone is
# provisioned; status reads without root. The switch exists only where the
# target is wanted at boot (SW_WANTED: the 7 Pro's vendor link) or masked.
sw=lib/omamobile-modem-switch
mkdir -p "$t/sw/bin" "$t/sw/prov"
cat > "$t/sw/bin/systemctl" <<'SH'
#!/bin/bash
echo "$*" >> "$SW_LOG"
case $1 in
is-enabled) if [[ -e $SW_MASKED ]]; then echo masked; else echo disabled; fi ;;
is-active) if [[ $3 == omamobile-modem-inhibit.service ]]; then [[ -e $SW_INHIBIT ]]; else [[ -e $SW_ACTIVE ]]; fi ;;
show) [[ $2 == -p && $3 == WantedBy ]] || exit 1; [[ -e $SW_WANTED ]] && echo "multi-user.target" || echo ;;
mask) touch "$SW_MASKED" ;;
unmask) rm -f "$SW_MASKED" ;;
esac
SH
chmod +x "$t/sw/bin/systemctl"
: > "$t/sw/prov/a"; : > "$t/sw/wanted"
swrun() {
SW_LOG=$t/sw/log SW_MASKED=$t/sw/masked SW_ACTIVE=$t/sw/active SW_WANTED=$t/sw/wanted SW_INHIBIT=$t/sw/inhibit \
OMAMOBILE_SYSTEMCTL=$t/sw/bin/systemctl OMAMOBILE_MODEM_BOOT_MARK=$t/sw/mark \
OMAMOBILE_MODEM_PROVISION="${SW_PROV:-$t/sw/prov/a}" "$@"
}
swfield() { python3 -c 'import json,sys; print(str(json.loads(sys.stdin.read())[sys.argv[1]]).lower())' "$1"; }
[[ $(swrun "$sw" status | swfield enabled) == true ]] || fail "switch: on by default"
[[ $(swrun "$sw" status | swfield available) == true ]] || fail "switch: available where the target is wanted at boot"
[[ $(swrun "$sw" status | swfield sleep_blocked) == false ]] || fail "switch: no inhibitor, no sleep block"
: > "$t/sw/inhibit"
[[ $(swrun "$sw" status | swfield sleep_blocked) == true ]] || fail "switch: the inhibitor reads as the sleep block"
rm -f "$t/sw/inhibit"
OMAMOBILE_MODEM_SWITCH_UID=1000 swrun "$sw" off 2>/dev/null && fail "switch: off needs root"
rm -f "$t/sw/log"
out=$(OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" off)
[[ $(swfield enabled <<< "$out") == false ]] || fail "switch: off masks"
grep -c -e stop -e start "$t/sw/log" >/dev/null && fail "switch: off stops or starts nothing"
# Off while this boot's modem runs: a restart is needed.
date > "$t/sw/mark"; : > "$t/sw/active"
[[ $(swrun "$sw" status | swfield restart_needed) == true ]] || fail "switch: off with the modem up needs a restart"
# On again, the modem already started: unmasked, nothing started.
rm -f "$t/sw/log"
out=$(OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" on)
[[ $(swfield enabled <<< "$out") == true && $(swfield restart_needed <<< "$out") == false ]] || fail "switch: on again with the modem up"
grep -c start "$t/sw/log" >/dev/null && fail "switch: never a second start in a boot"
# Started and stopped this boot, switched on: a restart, not a start.
rm -f "$t/sw/active" "$t/sw/log"
out=$(OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" on)
grep -c start "$t/sw/log" >/dev/null && fail "switch: a stopped modem is not started again in the boot"
[[ $(swfield restart_needed <<< "$out") == true ]] || fail "switch: a stopped modem needs a restart"
# Never started this boot (masked at boot), switched on: started now.
rm -f "$t/sw/mark" "$t/sw/log"
OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" off >/dev/null
OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" on >/dev/null
grep -qx 'start --no-block omamobile-modem-boot.target' "$t/sw/log" || fail "switch: on starts a modem that never started this boot"
# Not provisioned: unmasked, not started.
rm -f "$t/sw/log" "$t/sw/prov/a"
OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" on >/dev/null
grep -c start "$t/sw/log" >/dev/null && fail "switch: an unprovisioned phone is not started"
[[ $(swrun "$sw" status | swfield provisioned) == false ]] || fail "switch: provisioned reads the files"
# props.conf sits in a root-only folder on the phone, and status runs as
# the user: a folder that can't be searched counts, a missing one doesn't.
mkdir -p "$t/sw/priv"; : > "$t/sw/priv/props.conf"; chmod 000 "$t/sw/priv"
[[ $(SW_PROV="$t/sw/priv/props.conf" swrun "$sw" status | swfield provisioned) == true ]] ||
fail "switch: a file in a root-only folder reads as provisioned"
[[ $(SW_PROV="$t/sw/none/props.conf" swrun "$sw" status | swfield provisioned) == false ]] ||
fail "switch: a missing folder is not provisioned"
chmod 700 "$t/sw/priv"
[[ $(SW_PROV="$t/sw/prov/a $t/sw/none/x" swrun "$sw" status | swfield provisioned) == false ]] ||
fail "switch: every file in the override list counts"
swrun "$sw" reboot 2>/dev/null && fail "switch: unknown commands refused"
# Not masked and not wanted at boot (the 9a, the 6 Pro): no switch, and
# "on" and "off" refuse without touching systemd.
: > "$t/sw/prov/a"; rm -f "$t/sw/wanted" "$t/sw/masked" "$t/sw/mark" "$t/sw/active" "$t/sw/log"
out=$(swrun "$sw" status)
[[ $(swfield available <<< "$out") == false && $(swfield enabled <<< "$out") == false ]] ||
fail "switch: not masked, not wanted reads as not available and not on"
OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" on 2>/dev/null && fail "switch: on refused where nothing starts the modem at boot"
OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" off 2>/dev/null && fail "switch: off refused where nothing starts the modem at boot"
grep -c -e mask -e start "$t/sw/log" >/dev/null && fail "switch: a refusal touches nothing"
# Masked by hand on such a phone: "on" unmasks, and starts nothing.
: > "$t/sw/masked"; rm -f "$t/sw/log"
OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" on >/dev/null
grep -c start "$t/sw/log" >/dev/null && fail "switch: on starts nothing that isn't wanted at boot"
[[ $(swrun "$sw" status | swfield available) == false ]] || fail "switch: unmasked and not wanted is not available"
grep -q 'exec.path">/usr/lib/omamobile-vendor/omamobile-modem-switch<' polkit/org.omamobile.modem.policy ||
fail "switch: the polkit action names the helper"
pass "on/off switch: off masks only, on starts only a modem that never started this boot"

# The NV overlay: its own tests (synthetic NV-like files only).
test/nv-overlay
2 changes: 1 addition & 1 deletion packages/omamobile-modem/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.21
0.1.24
Loading