diff --git a/packages/omamobile-modem/README.md b/packages/omamobile-modem/README.md new file mode 100644 index 000000000..30405c55d --- /dev/null +++ b/packages/omamobile-modem/README.md @@ -0,0 +1,34 @@ + +# Modem link modules + +The default link remains `modprobe pcie-gs201-cp allow_hw=1` when no device +configuration exists. The Pixel 9 Pro XL needs a separate PHY before its PCIe +host; its device package may supply the ordered list as +`/usr/lib/omamobile-modem/link.conf`. This directory is staged by this package +and is separate from extracted vendor files. This package does not install +link.conf or enable another device's drivers. The device package owns its file. + +The file is data, never a shell fragment. Each nonempty, noncomment line is: + +``` +module key=value key=value +``` + +A module starts with an ASCII letter and contains only letters, digits, +underscores and hyphens. Parameter names start with a letter or underscore; +remaining characters are letters, digits or underscores. Values are nonempty +and contain only letters, digits, underscore, period, comma, colon, plus, +slash or hyphen. Arguments are separated by spaces or tabs; a module without +parameters is valid. Blank lines and whole-line comments are allowed. Shell +quotes, substitutions, assignments, functions, options and inline comments +are refused, as are empty configurations, symlinks and unreadable/nonregular +files. A raw ASCII-text check rejects NUL and other non-text bytes before +Bash reads lines; both passes use the same opened file. A missing file preserves the default; a present invalid file refuses. + +All entries are validated before checking already-loaded parameters, checking +the sleep inhibitor, creating the bring-up latch or loading modules. Missing +or mismatched parameters on loaded modules still require a reboot; the +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. diff --git a/packages/omamobile-modem/install b/packages/omamobile-modem/install index 91a1006d4..ebf06f7e1 100755 --- a/packages/omamobile-modem/install +++ b/packages/omamobile-modem/install @@ -12,6 +12,9 @@ set -euo pipefail cd -- "$(dirname -- "${BASH_SOURCE[0]}")" d=${1:?Usage: ./install DESTDIR} [[ $d == /* && $d != "/" ]] || { echo "Use an absolute staging directory" >&2; exit 1; } +# Device packages may install a data-only link.conf here. Vendor extraction +# never populates this directory; no config is needed for the gs201 default. +install -dm755 "$d/usr/lib/omamobile-modem" 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" @@ -25,5 +28,6 @@ for u in omamobile-modem.service omamobile-modem-inhibit.service omamobile-modem done install -Dm644 udev/60-omamobile-modem-nv-ro.rules "$d/usr/lib/udev/rules.d/60-omamobile-modem-nv-ro.rules" install -Dm644 udev/61-omamobile-vendor-udisks-ignore.rules "$d/usr/lib/udev/rules.d/61-omamobile-vendor-udisks-ignore.rules" +install -Dm644 README.md "$d/usr/share/doc/omamobile-modem/README.md" install -Dm644 LICENSE "$d/usr/share/licenses/omamobile-modem/LICENSE" install -Dm644 version "$d/usr/share/omamobile/modem/version" diff --git a/packages/omamobile-modem/lib/omamobile-modem-start b/packages/omamobile-modem/lib/omamobile-modem-start index aea70393d..8b90ad187 100755 --- a/packages/omamobile-modem/lib/omamobile-modem-start +++ b/packages/omamobile-modem/lib/omamobile-modem-start @@ -151,13 +151,57 @@ if [[ ${OMAMOBILE_MODEM_TEST_STOP_AFTER_CHECKS:-} == 1 ]]; then exit 0 fi +# The Pixel 9 Pro XL needs its PHY loaded before its PCIe host. Device +# packages can provide this ordered, data-only list outside vendor extraction. +# Without a file, retain the Pixel 7 Pro's existing gs201 invocation. +LINK_CONFIG=/usr/lib/omamobile-modem/link.conf +LINK_MODULES=("pcie-gs201-cp allow_hw=1") +read_link_config() { + local path=$1 entry config_fd LC_ALL=C + [[ -e $path || -L $path ]] || return 0 + [[ -f $path && -r $path && ! -L $path ]] || refuse "Off: invalid link configuration" + # Bash read discards NUL bytes. Reject binary/non-ASCII bytes first, + # through the same opened file, before read can normalize their contents. + exec {config_fd}< "$path" || refuse "Off: invalid link configuration" + if LC_ALL=C grep -aq '[^[:print:][:blank:]]' "/proc/self/fd/$config_fd"; then + refuse "Off: invalid link configuration" + else + [[ $? == 1 ]] || refuse "Off: invalid link configuration" + fi + LINK_MODULES=() + while IFS= read -r entry || [[ -n $entry ]]; do + [[ $entry =~ ^[[:blank:]]*$ || $entry =~ ^[[:blank:]]*# ]] && continue + # A module followed by zero or more key=value arguments. No shell syntax, + # quotes, substitutions, options or inline comments are accepted. + [[ $entry =~ ^[a-zA-Z][a-zA-Z0-9_-]*([[:blank:]]+[a-zA-Z_][a-zA-Z0-9_]*=[a-zA-Z0-9_.,:+/-]+)*[[:blank:]]*$ ]] || + refuse "Off: invalid link configuration" + LINK_MODULES+=("$entry") + done <&"$config_fd" + exec {config_fd}<&- + ((${#LINK_MODULES[@]})) || refuse "Off: empty link configuration" +} +read_link_config "$LINK_CONFIG" + # The link driver must allow hardware access, or cbd's power-on is refused -# (-EPERM). allow_hw is fixed at load time: a copy loaded without it (by -# hand, M3.3-B) needs a reboot. -if [[ -d /sys/module/pcie_gs201_cp ]]; then - [[ $(cat /sys/module/pcie_gs201_cp/parameters/allow_hw) == Y ]] || - { say "pcie_gs201_cp is loaded without allow_hw=1: reboot first"; refuse "Off: reboot needed"; } -fi +# (-EPERM), and parameters are fixed at load time: a copy already loaded +# with other values (by hand, M3.3-B, or a hardware-disabled test this boot) +# needs a reboot. Booleans read back as Y/N. +link_param_ok() { + local mod=$1 kv=$2 key val have + key=${kv%%=*} val=${kv#*=} + [[ -r /sys/module/$mod/parameters/$key ]] || return 1 + have=$(cat "/sys/module/$mod/parameters/$key") + [[ $have == "$val" ]] || { [[ $val == 1 && $have == Y ]]; } || { [[ $val == 0 && $have == N ]]; } +} +for entry in "${LINK_MODULES[@]}"; do + read -r -a words <<< "$entry" + mod=${words[0]//-/_} + [[ -d /sys/module/$mod ]] || continue + for kv in "${words[@]:1}"; do + link_param_ok "$mod" "$kv" || + { say "$mod is loaded without $kv: reboot first"; refuse "Off: reboot needed"; } + done +done # The sleep inhibitor before the drivers: the unit requires and orders after # omamobile-modem-inhibit.service, but a Type=simple unit is "started" at @@ -178,7 +222,10 @@ sync # CPIF and the regions are blacklisted by the device's modem policy (the # package providing omamobile-modem-policy): only this loads them. Never # unload cpif (it is permanent by design). -modprobe pcie-gs201-cp allow_hw=1 +for entry in "${LINK_MODULES[@]}"; do + read -r -a words <<< "$entry" + modprobe "${words[@]}" +done modprobe shm_ipc modprobe cpif # The link to the modem with ASPM off, as every link test so far ran diff --git a/packages/omamobile-modem/test/all b/packages/omamobile-modem/test/all index 70f5ae26d..141aef911 100755 --- a/packages/omamobile-modem/test/all +++ b/packages/omamobile-modem/test/all @@ -16,6 +16,7 @@ if command -v shellcheck >/dev/null; then shellcheck -S warning lib/omamobile-modem-start install || fail "shellcheck" fi pass "scripts parse" +python3 test/test_link.py || fail "device link module parameters and load order" # spawn_optional keeps its job alive whatever the daemon's exit: run the # function from modem.conf under the library's errexit and pipefail with a @@ -84,7 +85,9 @@ pass "NV checksums" stage=$t/stage ./install "$stage" -for f in usr/lib/omamobile-vendor/omamobile-modem-start usr/lib/omamobile-vendor/omamobile-modem-cp \ +[[ -d $stage/usr/lib/omamobile-modem ]] || fail "package-owned link configuration directory" +[[ ! -e $stage/usr/lib/omamobile-modem/link.conf ]] || fail "default needs no device link configuration" +for f in usr/share/doc/omamobile-modem/README.md usr/lib/omamobile-vendor/omamobile-modem-start usr/lib/omamobile-vendor/omamobile-modem-cp \ 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 \ @@ -165,7 +168,7 @@ s=lib/omamobile-modem-start lineof() { grep -n "$1" $s | head -1 | cut -d: -f1; } l_prov_refuse=$(lineof "isn't provisioned"); l_mark_refuse=$(lineof 'never reached ONLINE; skipping'); l_up_refuse=$(lineof 'refuse_unclean$') l_inhibit=$(lineof 'no sleep inhibitor held'); l_latch=$(lineof '> "\$MARK"'); l_latch_sync=$(awk -v n="$l_latch" 'NR == n + 1' $s) -l_modprobe=$(lineof '^modprobe '); l_check=$(lineof 'omamobile-vendor-chroot" modem check'); l_rm=$(lineof '^ rm -f "\$MARK"') +l_modprobe=$(lineof '^[[:space:]]*modprobe '); l_check=$(lineof 'omamobile-vendor-chroot" modem check'); l_rm=$(lineof '^ rm -f "\$MARK"') l_ready=$(lineof 'systemd-notify --ready'); l_exec=$(lineof 'exec "\$LIB/omamobile-vendor-chroot" modem start') l_reason_rm=$(lineof '^rm -f "\$REFUSED"') for v in l_prov_refuse l_mark_refuse l_up_refuse l_inhibit l_reason_rm l_latch l_modprobe l_check l_rm l_ready l_exec; do @@ -175,7 +178,7 @@ done [[ $l_prov_refuse -lt $l_mark_refuse && $l_mark_refuse -lt $l_up_refuse && $l_up_refuse -lt $l_inhibit && $l_inhibit -lt $l_reason_rm && $l_reason_rm -lt $l_latch && $l_latch -lt $l_modprobe && $l_modprobe -lt $l_check && $l_check -lt $l_rm && $l_rm -lt $l_ready && $l_ready -lt $l_exec ]] || fail "start script order: refuse($l_prov_refuse,$l_mark_refuse,$l_up_refuse) inhibit($l_inhibit) reason-rm($l_reason_rm) latch($l_latch) modprobe($l_modprobe) check($l_check) rm($l_rm) ready($l_ready) exec($l_exec)" -[[ $(grep -c '^modprobe ' $s) -eq 3 && $(grep -c 'modprobe' <(sed -n "1,${l_latch}p" $s) ) -eq 0 ]] || +[[ $(grep -c '^[[:space:]]*modprobe ' $s) -eq 3 && $(grep -c 'modprobe' <(sed -n "1,${l_latch}p" $s) ) -eq 0 ]] || fail "no modprobe before the latch" grep -q 'OMAMOBILE_MODEM_MARK=\$MARK OMAMOBILE_MODEM_UP_MARK=\${INVOCATION_ID:+\$UP} exec' $s || fail "both markers go to the profile's watcher (the modem-up one only under the unit)" grep -q "OMAMOBILE_MODEM_UP_MARK" $cp || fail "the watcher writes the modem-up marker" @@ -201,7 +204,7 @@ past() { OMAMOBILE_MODEM_TEST_STOP_AFTER_CHECKS=1 start "$@"; } stop_line='past the marker checks, stopping here (test only)' ! grep -rq 'OMAMOBILE_MODEM_TEST_STOP_AFTER_CHECKS' systemd install || fail "the test stop point is set by no unit or install step" l_stop=$(lineof 'OMAMOBILE_MODEM_TEST_STOP_AFTER_CHECKS:-') -[[ $l_up_refuse -lt $l_stop && $l_stop -lt $(lineof '/sys/module/pcie_gs201_cp') && $l_stop -lt $l_inhibit && $l_stop -lt $l_latch ]] || +[[ $l_up_refuse -lt $l_stop && $l_stop -lt $(lineof '^link_param_ok()') && $l_stop -lt $l_inhibit && $l_stop -lt $l_latch ]] || fail "the test stop point sits after the marker decision and before the sysfs read, the inhibitor and the latch (stop=$l_stop)" # Not provisioned (a fresh install before tools/phone-provision): one line # per missing file, no latch, the reason says so. The unit itself is skipped diff --git a/packages/omamobile-modem/test/test_link.py b/packages/omamobile-modem/test/test_link.py new file mode 100644 index 000000000..c7893dd42 --- /dev/null +++ b/packages/omamobile-modem/test/test_link.py @@ -0,0 +1,204 @@ +# SPDX-License-Identifier: MIT +"""Run the starter's link checks with synthetic sysfs and inert module loads.""" +from pathlib import Path +import re +import os +import subprocess +import tempfile +import unittest + +SOURCE = (Path(__file__).resolve().parents[1] / 'lib/omamobile-modem-start').read_text() + + +class LinkTests(unittest.TestCase): + def setUp(self): + self.directory = tempfile.TemporaryDirectory() + self.addCleanup(self.directory.cleanup) + self.root = Path(self.directory.name) + self.function = re.search(r'^link_param_ok\(\) \{\n.*?^\}', SOURCE, re.M | re.S).group() + self.function = self.function.replace('/sys/module/', str(self.root) + '/') + + def run_value(self, value, wanted): + parameter = self.root / 'test/parameters/flag' + parameter.parent.mkdir(parents=True, exist_ok=True) + if value is not None: + parameter.write_text(value + '\n') + return subprocess.run(['bash', '-c', 'set -euo pipefail\n' + self.function + + '\nlink_param_ok test flag=' + wanted], capture_output=True).returncode + + def test_missing_parameter_refuses(self): + self.assertNotEqual(self.run_value(None, '1'), 0) + + def test_boolean_and_literal_values(self): + for value, wanted in [('Y', '1'), ('N', '0'), ('1', '1'), ('0', '0'), ('42', '42')]: + self.assertEqual(self.run_value(value, wanted), 0) + + def test_mismatched_values_refuse(self): + for value, wanted in [('N', '1'), ('Y', '0'), ('unknown', '1')]: + self.assertNotEqual(self.run_value(value, wanted), 0) + + def test_device_modules_load_in_order_with_arguments(self): + loop = re.search(r'for entry in "\$\{LINK_MODULES\[@\]\}"; do\n read -r -a words <<< "\$entry"\n modprobe "\$\{words\[@\]\}"\ndone', SOURCE).group() + script = ('LINK_MODULES=("first allow_hw=1" "second cpif=1")\n' + 'modprobe() { printf "%s " "$@"; printf "\\n"; }\n' + loop) + result = subprocess.run(['bash', '-c', script], text=True, capture_output=True, check=True) + self.assertEqual(result.stdout, 'first allow_hw=1 \nsecond cpif=1 \n') + + def test_default_keeps_existing_gs201_parameters(self): + self.assertIn('LINK_MODULES=("pcie-gs201-cp allow_hw=1")', SOURCE) + + def test_loaded_module_mismatch_refuses_before_loading(self): + block = SOURCE[SOURCE.index('link_param_ok() {'):SOURCE.index('# The sleep inhibitor before the drivers:')] + block = block.replace('/sys/module/', str(self.root) + '/') + parameter = self.root / 'second/parameters/cpif' + parameter.parent.mkdir(parents=True) + for value in [None, 'N', 'Y']: + if value is not None: + parameter.write_text(value + '\n') + script = ('set -euo pipefail\nLINK_MODULES=("first allow_hw=1" "second cpif=1")\n' + 'say() { :; }; refuse() { exit 7; }\n' + block + '\necho load-reached\n') + result = subprocess.run(['bash', '-c', script], text=True, capture_output=True) + self.assertEqual(result.returncode, 0 if value == 'Y' else 7) + self.assertEqual('load-reached' in result.stdout, value == 'Y') + + def run_starter(self, config, loaded=None): + """Run actual starter with only paths redirected and inert external tools.""" + config_path = self.root / 'link.conf' + if config is not None: + if isinstance(config, bytes): + config_path.write_bytes(config) + else: + config_path.write_text(config) + vendor = self.root / 'vendor' + vendor.mkdir() + profile = vendor / 'omamobile-vendor-chroot' + profile.write_text('#!/bin/bash\necho profile-$2 >> "$TRACE"\nexit 1\n') + profile.chmod(0o700) + sysfs = self.root / 'sys' + sysfs.mkdir() + if loaded is not None: + parameter = sysfs / 'second/parameters/cpif' + parameter.parent.mkdir(parents=True) + if loaded != 'missing': + parameter.write_text(loaded + '\n') + tools = self.root / 'bin' + tools.mkdir() + for name, body in { + 'systemd-inhibit': 'echo inhibitor >> "$TRACE"; echo "omamobile-modem held"', + 'sync': ':', + 'modprobe': 'test -f "$OMAMOBILE_MODEM_LATCH" || exit 23; echo "load:$*" >> "$TRACE"', + }.items(): + tool = tools / name + tool.write_text('#!/bin/bash\n' + body + '\n') + tool.chmod(0o700) + provision = self.root / 'provisioned' + provision.touch() + starter = self.root / 'starter' + starter.write_text(SOURCE.replace('LIB=/usr/lib/omamobile-vendor', + 'LIB=' + str(vendor)).replace( + 'LINK_CONFIG=/usr/lib/omamobile-modem/link.conf', + 'LINK_CONFIG=' + str(config_path)).replace( + '/sys/module/', str(sysfs) + '/')) + trace = self.root / 'trace' + env = os.environ.copy() + env.update(PATH=str(tools) + ':' + env['PATH'], TRACE=str(trace), + OMAMOBILE_MODEM_LATCH=str(self.root / 'latch'), + OMAMOBILE_MODEM_UP_MARK=str(self.root / 'up'), + OMAMOBILE_MODEM_HOLD=str(self.root / 'hold'), + OMAMOBILE_MODEM_REFUSED=str(self.root / 'refused'), + OMAMOBILE_MODEM_CHROOT=str(provision), + OMAMOBILE_MODEM_NV_SUMS=str(provision), + OMAMOBILE_MODEM_PROPS=str(provision)) + env.pop('OMAMOBILE_MODEM_TEST_STOP_AFTER_CHECKS', None) + result = subprocess.run(['bash', str(starter)], env=env, + capture_output=True, text=True, timeout=5) + return result, trace.read_text().splitlines() if trace.exists() else [] + + def test_data_list_reaches_inhibitor_then_ordered_loads_with_latch(self): + result, trace = self.run_starter('# device list\nfirst allow_hw=1\nsecond cpif=1', 'Y') + self.assertEqual(result.returncode, 1) # inert profile check deliberately refuses + self.assertEqual(trace, ['inhibitor', 'inhibitor', 'load:first allow_hw=1', + 'load:second cpif=1', 'load:shm_ipc', 'load:cpif', 'profile-check']) + + def test_no_config_preserves_real_default_load(self): + result, trace = self.run_starter(None) + self.assertEqual(result.returncode, 1) + self.assertEqual(trace[2], 'load:pcie-gs201-cp allow_hw=1') + + def test_data_substitution_refuses_without_executing_marker(self): + marker = self.root / 'injected' + result, trace = self.run_starter('first allow_hw=$(touch ' + str(marker) + ')\n') + self.assertFalse(marker.exists()) + self.assertEqual(result.returncode, 1) + self.assertEqual(trace, []) + self.assertFalse((self.root / 'latch').exists()) + self.assertEqual((self.root / 'refused').read_text().strip(), + 'Off: invalid link configuration') + + def assert_binary_config_refused(self, raw): + result, trace = self.run_starter(raw) + self.assertEqual(trace, []) + self.assertEqual(result.returncode, 1) + self.assertFalse((self.root / 'latch').exists()) + self.assertEqual((self.root / 'refused').read_text().strip(), + 'Off: invalid link configuration') + + def test_nul_inside_module_cannot_normalize_to_valid_load(self): + self.assert_binary_config_refused(b'fi\x00rst allow_hw=1\n') + + def test_nul_inside_parameter_cannot_normalize_to_valid_load(self): + self.assert_binary_config_refused(b'first allow_hw=\x001\n') + + def test_nul_only_configuration_is_invalid(self): + self.assert_binary_config_refused(b'\x00') + + def test_invalid_data_cannot_override_guards(self): + # Execute the actual parser once per hostile data sample, never source it. + function = re.search(r'^read_link_config\(\) \{\n.*?^\}', SOURCE, re.M | re.S).group() + for text in ['MARK=/wrong', 'HOLD=/wrong', 'refuse() { :; }', + 'first a=`true`', 'first a="1"', '-r first', + 'first a=1; true', 'first a=', 'first ../a=1', + 'first a=1 # inline', '', '# comment only\n']: + path = self.root / 'invalid.conf' + path.write_text(text) + result = subprocess.run(['bash', '-c', + 'set -euo pipefail\nrefuse() { exit 7; }\n' + function + + '\nread_link_config "$1"\necho accepted', 'test', str(path)], + capture_output=True, text=True) + self.assertEqual(result.returncode, 7, text) + self.assertNotIn('accepted', result.stdout) + + def test_full_starter_loaded_parameter_refuses_before_inhibitor_or_latch(self): + result, trace = self.run_starter('first allow_hw=1\nsecond cpif=1\n', 'missing') + self.assertEqual(result.returncode, 1) + self.assertEqual(trace, []) + self.assertFalse((self.root / 'latch').exists()) + self.assertEqual((self.root / 'refused').read_text().strip(), 'Off: reboot needed') + + def test_parser_accepts_only_named_module_arguments_and_refuses_symlink(self): + function = re.search(r'^read_link_config\(\) \{\n.*?^\}', SOURCE, re.M | re.S).group() + path = self.root / 'valid.conf' + path.write_text('first\nsecond\tflag=1,2 value=a/b:c+_-.\n') + script = ('set -euo pipefail\nrefuse() { exit 7; }\n' + function + + '\nread_link_config "$1"\nprintf "%s\\n" "${LINK_MODULES[@]}"') + result = subprocess.run(['bash', '-c', script, 'test', str(path)], + capture_output=True, text=True) + self.assertEqual(result.returncode, 0) + self.assertEqual(result.stdout.splitlines(), ['first', 'second\tflag=1,2 value=a/b:c+_-.']) + link = self.root / 'alias.conf' + link.symlink_to(path) + result = subprocess.run(['bash', '-c', script, 'test', str(link)], + capture_output=True, text=True) + self.assertEqual(result.returncode, 7) + + def test_config_read_precedes_parameter_check_inhibitor_and_latch(self): + self.assertLess(SOURCE.index('read_link_config "$LINK_CONFIG"'), + SOURCE.index('link_param_ok() {')) + self.assertLess(SOURCE.index('link_param_ok() {'), + SOURCE.index('# The sleep inhibitor before the drivers:')) + self.assertLess(SOURCE.index('# The sleep inhibitor before the drivers:'), + SOURCE.index("printf '%s boot %s")) + + +if __name__ == '__main__': + unittest.main() diff --git a/packages/omamobile-modem/version b/packages/omamobile-modem/version index e8e277f2f..04c5555c6 100644 --- a/packages/omamobile-modem/version +++ b/packages/omamobile-modem/version @@ -1 +1 @@ -0.1.16 +0.1.17