diff --git a/device/dipper/BRINGUP-M1.md b/device/dipper/BRINGUP-M1.md new file mode 100644 index 000000000..4486a4c80 --- /dev/null +++ b/device/dipper/BRINGUP-M1.md @@ -0,0 +1,133 @@ + +# Xiaomi Mi 8 (dipper): RAM-only first light + +**Status (2026-11-10):** the combined #503/#505 non-NFS image `540b5d31ea73a31ade9a8d8cb5152cf701dc3c2ad8256734790b0ef2db6dbdd0` booted by RAM on the Xiaomi Mi 8 with kernel/DTS PR #220 at `055a4019998f22cc0e2a8afef7650274a6ce0657`. Its initramfs root had all 85 UFS partition flags at `ro=1`. `/sys/block/sda/ro` was `0`, but `/dev/sda` was absent; `sdb`–`sdf` were `ro=1`. The guard confirmed this state. `qcom_wdt` and its feeder ran, simpledrm registered `fb0`, USB/SSH worked, and `/proc/iomem` reserved `0x85fc0000–0x8a8fffff`, covering the former TZ range. The pinned rescue SSH key matched, and `reboot-to` returned the phone to fastboot. Physical input events were not captured on this exact image. + +A separate RAM NFS-root run used image `a2d435a7430e7e4c145c61cdb9866d16929731859d91c7105e6bfcaf9e3e9dd7` with the rebased #503 init. BusyBox `mount` switched to the test NFS root; all six disk flags and all 85 partition flags were `ro=1`. Systemd reached the multi-user target with `RuntimeWatchdogSec=60s`, and SSH remained available. `reboot-to bootloader` returned the phone to fastboot. The exported root was a generic Arch base; this did not test the Omarchy desktop or link-loss recovery. Full results are in [NOTES.md]. +The FTS5 driver from [touch #219] is merged into linux-omarchy-mobile. The +Dipper board DTS is under review in [DTS #220], and the EA8074 panel driver is +under review in [panel #218]. +The EA8074 panel test is recorded in [NOTES.md]. The FTS5 touchscreen +still needs a hardware test. +On 2026-11-09, a guarded RAM boot was used to capture all 84 GPT partitions +other than `userdata` (the phone has no `super`); source and compressed-image +checksums passed. The separate live userdata archive also passed its checksum +and zstd integrity checks. It is a file-level, non-atomic archive, not a raw +partition image. + +This is a RAM-only bring-up. Dipper has one OS `boot` partition and no alternate +OS slot. The descriptor sets `BOOT_METHOD=ram-only`, the builder makes only a +`fastboot boot` image, and the initramfs keeps every UFS partition read-only. +There is no supported flash, push, installer or persistent update path here. + +## Recovery and safe boot + +Each gate below must pass before proceeding. Set `FASTBOOT_SERIAL` to the +intended handset's serial, `BACKUP_CHECKSUMS` to the locally held stock +partition manifest, and `USERDATA_BACKUP_CHECKSUMS` to the private userdata +archive's checksum file. Do not boot if either backup is missing or fails. + +1. **Backup gate** — Expect: the verified backup of the installed system's 84 + non-userdata GPT partitions, both checksum manifests, and the userdata archive + are available off-device. Pass: both + `sha256sum -c "$BACKUP_CHECKSUMS"` and + `sha256sum -c "$USERDATA_BACKUP_CHECKSUMS"` report every entry OK (run each + from the directory containing its manifest). The userdata archive is a live + file-level copy, not a raw partition image or an atomic snapshot. Stop: any + missing or mismatched entry. Rollback: do not boot; there is no reviewed + restore procedure yet, and nothing in this runbook writes UFS. Risk: an + unverified backup cannot recover a damaged single-slot phone. +2. **Fastboot gate** — Enter fastboot with **Volume Down + Power**; the §3.1 + entry is in [CONTRIBUTING.md §3.1](../../CONTRIBUTING.md#phones-without-ab-slots). + Run `fastboot -s "$FASTBOOT_SERIAL" getvar product` and + `fastboot -s "$FASTBOOT_SERIAL" getvar unlocked`. Expect `dipper` and `yes`. + Pass: exactly one device is listed, both values match, and the serial pins + every fastboot command. Stop: absent/ambiguous device, wrong product or + locked bootloader. Rollback: leave fastboot without writing. Risk: this + device has no alternate OS slot. +3. **Recovery gate** — Fastboot entry uses **Volume Down + Power**; the forced + restart is separate. On 2026-11-09, an attended test found that holding + **Power alone for 10 seconds** restarted into Android from both installed + Android and the Omarchy Mobile RAM kernel. Volume Down + Power then entered + fastboot from Android. Expect: the Power-only hold restarts the phone. Pass: + the observed procedure above is recorded and the phone can enter fastboot. + Stop if Power alone does not restart it. Rollback: do not start a RAM boot + until this gate passes. Risk: a hard kernel hang has not been simulated; + recovery from one still depends on the hardware button reset working. +4. **RAM boot gate** — Run + `fastboot -s "$FASTBOOT_SERIAL" boot boot.img`. Expect: the image starts + without changing UFS, and the screen stays lit because simpledrm holds the + panel's VCI and VDDIO rails. If it goes dark about 30 seconds in, record + `dmesg | grep -i -E 'regulator|simple'` and stop. + Pass only when `uname -r`, the `boot.img` sha256, all partition `ro` values, + whole-disk `ro` values, and `ufs-ro` lines from `dmesg` are recorded. + `/dev/sda` must be absent or present with `/sys/block/sda/ro=1` in NFS mode; + each other whole-disk node must be absent or present with its `ro=1`. Every + partition must have `ro=1`. Stop if any partition or present whole-disk node + has `ro=0`. + Also record the reserved-memory lines from + `grep -i -E 'reserved|System RAM' /proc/iomem`, the bound `qcom_wdt` + driver, its running watchdog feeder, and USB/SSH are recorded and correct. + Stop if any partition or present whole-disk node has `ro=0`, the watchdog + is not fed, USB/SSH fails, + or anything asks to flash. Rollback: use the verified forced-restart + procedure from step 3, then recover from the verified backup if needed. + Risk: a USB host that never configures still reaches the existing watcher + panic after about 65 seconds; on this single-slot phone, recovery depends on + the tested procedure from step 3. + +The current recorded first light used the stock `dtbo` untouched. The image is +RAM-only; never use `fastboot flash`, `bin/omamobile flash`, `bin/omamobile +push`, or the installer. Keep the phone and USB cable present during a probe. + +## Build and probe + +Build a kernel checkout containing the Dipper kernel PRs with +`device/dipper/kernel.config` merged after the shared Omarchy Mobile fragment. +Set `DEVICE=dipper`, `KERNEL_DIR` (source), and `KERNEL_OUT` (build output), then +run `bin/omamobile kernel`. The output must include `Image`, `Image.gz`, +`arch/arm64/boot/dts/qcom/sdm845-xiaomi-dipper.dtb`, and modules from that same +kernel release. Then set `SSH_PUBKEY` to an OpenSSH public key and run +`tools/dipper/build` with the same `KERNEL_DIR` and `KERNEL_OUT`. For a local +NFS-root probe, pass `OMAMOBILE_NFSROOT=:/` to the builder; the +host and export are build inputs, never descriptor values. The builder requires +Docker with arm64 emulation; it writes its output inside `out/dipper` by +default. Do not commit the key or generated images. + +The initramfs provides USB networking, SSH and a rescue shell. It does not +mount `persist`, read or embed per-phone identifiers, start Wi-Fi, or install a +root filesystem. The network daemon binds only to the IPv4 address assigned to +`usb0`. No Wi-Fi credentials, MAC addresses or private keys belong in the +repository; the SSH public key and generated host key stay in the local output. + +## What remains + +- Do not flash until there is a reviewed, device-specific recovery plan and a + verified backup of the installed system. This PR deliberately adds no + persistent writer. +- The initramfs has no Arch root yet. A later hard NFS-root mount can freeze the + root session if the host drops; use it only for a RAM-only probe. NFS-root + needs a root with `RuntimeWatchdogSec=60s`, because `kill -9 -1` stops the + userspace feeder before systemd can take over. It must never fall through to + writable userdata. +- Wi-Fi needs firmware and the phone's own WLAN address. Read identity files + from `persist` with `debugfs` (read-only) before enabling that service; never + mount `persist`. +- The exact systemd journal line from the earlier failed `qcom_wdt` timeout + change was not retained. The 2026-11-10 NFS test root reached systemd with + `RuntimeWatchdogSec=60s`; NFS link-loss recovery and the Omarchy desktop on + that root remain untested. +- Test the FTS5 touchscreen changes and GPU rendering on the phone before + marking them supported. + +From this rescue image, an SSH session can return to fastboot with +`reboot-to bootloader`. The installed ROM's `recovery` image was verified from +its 4.9.337 kernel header, but recovery-mode reboot was not tested; this runbook +uses only the verified bootloader path. The helper asks the PMIC reboot-mode +driver to select the bootloader and writes no UFS partition. The PMIC reset path +was verified on the handset on 2026-11-09. The packaged helper returned the +running image to fastboot on 2026-11-09 without writing UFS. + +[panel #218]: https://github.com/derekross/linux-omarchy-mobile/pull/218 +[touch #219]: https://github.com/derekross/linux-omarchy-mobile/pull/219 +[DTS #220]: https://github.com/derekross/linux-omarchy-mobile/pull/220 diff --git a/device/dipper/NOTES.md b/device/dipper/NOTES.md index 3a0ec67dd..50f49859f 100644 --- a/device/dipper/NOTES.md +++ b/device/dipper/NOTES.md @@ -9,6 +9,7 @@ the facts needed for review. | Date | `boot.img` SHA-256 | Source, as recorded | `/dev/sda` and UFS partitions | Checks and gaps | | --- | --- | --- | --- | --- | +| 2026-11-11 | `665fcbd682cd8e64b3ffec1ff7f4ab9ca33a26b9b47b11073be7a3c267abf2b1` | #505 `2d67708e07728b13b5d2a9eda299ace8a1608163`; kernel/DTS #220 `26aa77c317cd029d16e58890c532562ca275c9f0` | Node absent, disk `ro=0`; all 85 partitions `ro=1`; `sdb`–`sdf` `ro=1` | `uname -r` `7.3.0-rc6-omamobile-push1`; `qcom_wdt` bound and feeder running; simpledrm fb0, USB/SSH and reserved `0x85fc0000–0x8a8fffff` passed. Physical screen, keys, touch, GPU and Wi-Fi not tested. RAM boot only; no UFS write. | | 2026-11-10 | `540b5d31ea73a31ade9a8d8cb5152cf701dc3c2ad8256734790b0ef2db6dbdd0` | Combined #503/#505; kernel/DTS #220 `055a4019998f22cc0e2a8afef7650274a6ce0657` | Node absent, disk `ro=0`; all 85 partitions `ro=1`; `sdb`–`sdf` `ro=1` | Watchdog/feeder, simpledrm, USB/SSH, reserved TZ range, pinned host key and `reboot-to bootloader` passed; physical keys not captured. | | 2026-11-10 | `a2d435a7430e7e4c145c61cdb9866d16929731859d91c7105e6bfcaf9e3e9dd7` | Rebased #503 NFS image with #220 `055a4019998f22cc0e2a8afef7650274a6ce0657` | `/dev/sda` present, set `ro=1`; six disks and all 85 partitions read back `ro=1` | `uname`, NFS root, multi-user systemd, watchdog and SSH passed; generic Arch test root only; link-loss recovery and Omarchy desktop untested. | | 2026-11-10 | `0ee4f11d1973085c434e741a2e9dd73af5a9cd157296c25801ed5a92a6b9e9d9` | #505 `994944d7ecbacd69444221e6951153cb329a374e`; kernel/DTS #220 `055a4019998f22cc0e2a8afef7650274a6ce0657` | Node absent, disk `ro=0`; all 85 partitions `ro=1`; `sdb`–`sdf` `ro=1` | Watchdog/feeder, framebuffer, USB/SSH, reserved TZ range and `reboot-to` passed; physical keys not captured. | @@ -26,6 +27,21 @@ name list. The retained backup and kernel records cover 84 non-userdata GPT partitions plus `userdata`; the other seven names cannot be identified from the retained artifacts and remain unknown. +## 2026-11-11 — rebuilt #505 RAM image + +Built and booted `boot.img` SHA-256 +`665fcbd682cd8e64b3ffec1ff7f4ab9ca33a26b9b47b11073be7a3c267abf2b1` with +`fastboot boot` from #505 `2d67708e07728b13b5d2a9eda299ace8a1608163` and +kernel/DTS #220 `26aa77c317cd029d16e58890c532562ca275c9f0`. `uname -r` was +`7.3.0-rc6-omamobile-push1`. The `/dev/sda` whole-disk node was absent; its +sysfs flag remained `ro=0`. Nodes `/dev/sdb`–`/dev/sdf` were present with +`ro=1`; all 85 partition flags were `ro=1`, and `ufs-ro` logged the guard for +each. `qcom_wdt` was bound and the watchdog feeder was running. simpledrm +registered `fb0`; authenticated USB SSH worked. `/proc/iomem` reserved +`0x85fc0000–0x8a8fffff`, including the former TZ range. The physical screen, +keys, touch, GPU rendering and Wi-Fi were not tested in this run. Only +`fastboot boot` was used; no UFS partition was written. + ## 2026-11-10 — final PR #505 RAM image Booted image SHA-256 `0ee4f11d1973085c434e741a2e9dd73af5a9cd157296c25801ed5a92a6b9e9d9` with `fastboot boot`; it was built from PR #505 head `994944d7ecbacd69444221e6951153cb329a374e` and kernel/DTS PR #220 head `055a4019998f22cc0e2a8afef7650274a6ce0657` (touch PR #219 is merged). `uname -r` was `7.3.0-rc6-omamobile-push1`; `/` was the initramfs (`rootfs`). The guard reported the UFS devices read-only. Sysfs showed six disks: `sda` had `ro=0` but its whole-disk `/dev/sda` node was absent; `sdb`–`sdf` each had `ro=1` and present nodes. All 85 partitions had `ro=1`. This matches PR #505's intended missing-node case for `sda`; no UFS write was attempted. diff --git a/device/dipper/device.conf b/device/dipper/device.conf new file mode 100644 index 000000000..22f7407ab --- /dev/null +++ b/device/dipper/device.conf @@ -0,0 +1,11 @@ +# SPDX-License-Identifier: MIT +# RAM-only first-light descriptor for Xiaomi Mi 8 (dipper). +# Persistent install/update is disabled: the phone has one OS boot partition. +CODENAME=dipper +NAME="Xiaomi Mi 8" +SOC=sdm845 +COMPATIBLE="xiaomi,dipper" +BOOT_METHOD=ram-only +BOOT_IMAGES="" +UFS_RW="" +PANEL_SIZE=1080x2248 diff --git a/device/dipper/kernel.config b/device/dipper/kernel.config new file mode 100644 index 000000000..f24173e2f --- /dev/null +++ b/device/dipper/kernel.config @@ -0,0 +1,87 @@ +# SPDX-License-Identifier: MIT +# dipper's kernel fragment, merged after kernel/configs/omamobile.config. +# +# Everything the phone needs to reach a shell is built in, not a module. The +# first boot proved why: arm64 defconfig makes the Qualcomm PHY drivers and the +# UFS glue modules, while dwc3 and ufshcd are built in. So they probed, found no +# PHY, and gave up -- the kernel logged +# platform a600000.usb: deferred probe pending: dwc3: failed to initialize core +# gcc-sdm845 100000.clock-controller: sync_state() pending due to 88e8000.phy +# and the initramfs never got USB or storage. Modules in the initramfs do not +# help when nothing loads them before the devices probe. + +# USB and UFS PHYs. Without these, both the USB gadget and storage are dead. +CONFIG_PHY_QCOM_QMP=y +CONFIG_PHY_QCOM_QMP_COMBO=y +CONFIG_PHY_QCOM_QMP_UFS=y +CONFIG_PHY_QCOM_QUSB2=y + +# Storage: the core is already built in, the Qualcomm glue was not. +CONFIG_SCSI_UFS_QCOM=y + +# The Type-C port manager on the PMIC, for the role switch and VBUS detection. +CONFIG_TYPEC_QCOM_PMIC=y + +# Populate the PMIC power/resin key children before the initramfs checks keys. +CONFIG_POWER_RESET_QCOM_PON=y + +# The FTS5 driver is upstream after PR #219, but PR #220 has no Dipper touch +# node yet. Keep it built in for a later node/overlay test. +CONFIG_TOUCHSCREEN_STMFTS=y + +# Console log into the stock ramoops region: the only way to read an early hang +# on a phone with no serial header. +CONFIG_PSTORE=y +CONFIG_PSTORE_RAM=y +CONFIG_PSTORE_CONSOLE=y + +# --- the same lesson, applied to everything first light touches --------------- +# Devices probe once, early. A driver that only turns up later as a module never +# gets a second chance: the device is left unbound and not even on the deferred +# list. The first boot proved it for the PHYs; the second proved it again for +# i2c (geni_i2c loaded by hand bound to nothing, and `bind` by hand failed). +# So everything needed to reach a usable system is built in. + +# i2c: built in for the follow-up FTS5 node; PR #220 does not enable it yet. +CONFIG_I2C_QCOM_GENI=y +CONFIG_QCOM_GPI_DMA=y +# Nothing of ours is on SPI, so it stays a module: every built-in byte counts +# against the ABL kernel limit below. +CONFIG_SPI_QCOM_GENI=m + +# Display comes from the bootloader's framebuffer through simpledrm, not from +# the MSM display driver, so DRM_MSM stays a module. Built in it pushed the +# kernel past about 16 MiB, which this ABL refuses outright: +# Booting FAILED (remote: 'Failed to load/authenticate boot image: 00000050') +# Nothing loads it; the panel path is open work (see NOTES.md). +CONFIG_DRM_MSM=m +CONFIG_DRM_SIMPLEDRM=y +CONFIG_SYSFB_SIMPLEFB=y + +# The remoteprocs Wi-Fi depends on (the modem hosts the WLAN firmware) stay +# modules. Built in they pushed the kernel past what this ABL will load: a +# kernel+dtb of 16,247,767 bytes boots and 16,376,528 does not, with only +# Booting FAILED (remote: 'Failed to load/authenticate boot image: 00000050') +# to go on. In the root filesystem udev loads them; the bring-up initramfs does +# not need the modem. +CONFIG_QCOM_RPROC_COMMON=m +CONFIG_QCOM_Q6V5_PAS=m +CONFIG_QCOM_Q6V5_MSS=m +CONFIG_QCOM_SYSMON=m +CONFIG_RPMSG_QCOM_GLINK_SMEM=m + +# Left as modules with DRM_MSM: pinning them only mattered to allow DRM_MSM=y, +# which the ABL size limit ruled out. +CONFIG_QCOM_OCMEM=m +CONFIG_QCOM_LLCC=m + +# This phone's ABL refuses a boot image whose kernel is too large, saying only +# Booting FAILED (remote: 'Failed to load/authenticate boot image: 00000050') +# Measured: kernel+dtb of 16,247,767 bytes boots, 16,343,459 does not. The +# boundary sits at 0xF80000 = 16,252,928, which tools/dipper/build-boot.py now +# refuses to cross so the failure is ours and explained, not the bootloader's +# and silent. + +# The DSI core can be a module: the panel driver that needs it is one too, and +# the display comes from simpledrm over the bootloader's framebuffer. +CONFIG_DRM_MIPI_DSI=m diff --git a/docs/DEVICES.md b/docs/DEVICES.md index d8de37503..f781678ce 100644 --- a/docs/DEVICES.md +++ b/docs/DEVICES.md @@ -78,6 +78,7 @@ Devices we'd like to support, with the closest starting point. Open an issue to | Pixel 11 Pro, 11 Pro XL, 11 Pro Fold | grizzly, kodiak, yogi | malibu (Tensor G6) | stock facts, contributors welcome: 11 Pro ([#26](https://github.com/derekross/omarchy-mobile/issues/26)), 11 Pro XL ([#199](https://github.com/derekross/omarchy-mobile/issues/199), [notes](../device/kodiak/NOTES.md)), 11 Pro Fold ([#40](https://github.com/derekross/omarchy-mobile/issues/40)) | | Pixel 4 | flame | SM8150 (Snapdragon 855) | **WIP, community port**, in the matrix above: the RAM-only first-light probe is merged ([BRINGUP-M1](../device/flame/BRINGUP-M1.md)); the native install is in review, split out of [#17](https://github.com/derekross/omarchy-mobile/pull/17) into [#55](https://github.com/derekross/omarchy-mobile/pull/55), [#56](https://github.com/derekross/omarchy-mobile/pull/56), [#58](https://github.com/derekross/omarchy-mobile/pull/58) and [#59](https://github.com/derekross/omarchy-mobile/pull/59) | | Pixel 4 XL | coral | SM8150 (Snapdragon 855) | **WIP, community port**, in the matrix above: bring-up notes merged ([#52](https://github.com/derekross/omarchy-mobile/pull/52): [BRINGUP-M1](../device/coral/BRINGUP-M1.md), [NOTES](../device/coral/NOTES.md)), with companion kernel/userspace sources ([omarchy-mobile-coral v0.2.3](https://github.com/HemangKumawat/omarchy-mobile-coral/tree/v0.2.3)); slot-B installer and `CONTRIBUTING.md` §3.1 entry still to land | +| Xiaomi Mi 8 | dipper | SDM845 | **WIP, community port:** the RAM-only first-light probe is merged ([BRINGUP-M1](../device/dipper/BRINGUP-M1.md), [NOTES](../device/dipper/NOTES.md)); single-slot, no install path yet. Kernel: touch [#219](https://github.com/derekross/linux-omarchy-mobile/pull/219) merged, board DTS [#220](https://github.com/derekross/linux-omarchy-mobile/pull/220) and panel [#218](https://github.com/derekross/linux-omarchy-mobile/pull/218) in review. | | POCO F3 | alioth | SM8250 (Snapdragon 870) | the first non-Pixel port: a guarded installer and root image in review ([#25](https://github.com/derekross/omarchy-mobile/pull/25)) | | OnePlus 13 | dodge | SM8750 (Snapdragon 8 Elite) | **WIP, community port**, in the matrix above: device files, notes and the qcom-abl boot images merged ([#111](https://github.com/derekross/omarchy-mobile/pull/111)); its kernel stays in the contributor's fork ([mrlund/linux-omarchy-mobile `dodge-v3-20261101`](https://github.com/mrlund/linux-omarchy-mobile/tree/dodge-v3-20261101); [kernel #115](https://github.com/derekross/linux-omarchy-mobile/pull/115) closed: the SoC series is out of this tree's scope); the guarded installer follows in a later pull request | | OnePlus 6 | enchilada | SDM845 (Snapdragon 845) | **WIP, community port: [#432](https://github.com/derekross/omarchy-mobile/issues/432)**, in the matrix above: descriptor and notes; RAM-only (`fastboot boot`) until the slot-B install and its CONTRIBUTING.md §3.1 entry are agreed; `omamobile images` packs its header-v0 boot image, and its `UFS_RW_LUNS=sde` (the guard's second-LUN list, #530) comes with the slot-B install | diff --git a/tests/all b/tests/all index 27a3b1482..31e7ed5a8 100755 --- a/tests/all +++ b/tests/all @@ -43,9 +43,10 @@ sh -n initramfs/init sh -n device/flame/initramfs/init sh -n device/flame/initramfs/build bash -n tools/build-flame-probe +bash -n tools/dipper/build tools/dipper/initramfs-inside.sh echo "ok - scripts parse" python3 tests/bt-uart-gpio.py -for t in tests/pr-check tests/kernel-pr-check tests/phone-smoke tests/alarm-wake-test tests/install-phone tests/build-rootfs tests/rootfs-resolve tests/phone-provision tests/nv-backup tests/initramfs-grow tests/images-layout tests/boot-update-parity tests/stage-boot-files tests/build-packages-host-tools tests/initramfs-reproducible tests/device-overlays tests/simplefb-trigger tests/initramfs-steps tests/storage-guard tests/usb-product tests/grep-q-pipes tests/boot-method tests/modules-push tests/make-bundle-keys tests/make-bundle tests/release-signing tests/pinned-packages tests/flame-probe packages/omamobile-firstboot/test/all packages/omamobile-device-felix/test/all packages/omamobile-device-lynx/test/all packages/omamobile-device-panther/test/all tests/local-builds tests/phone-install-package tests/publish-kernel tests/fetch-toolchain tests/release-upload tests/extract-camera-data tests/release tests/lineage-ref tests/repo-publish tests/repo-publish-stage tests/extract-vendor-firmware tests/alpine-fetch-callers tests/alpine-fetch tests/tools-help tests/install-phone-initramfs; do +for t in tests/pr-check tests/kernel-pr-check tests/phone-smoke tests/alarm-wake-test tests/install-phone tests/build-rootfs tests/rootfs-resolve tests/phone-provision tests/nv-backup tests/initramfs-grow tests/images-layout tests/boot-update-parity tests/stage-boot-files tests/build-packages-host-tools tests/initramfs-reproducible tests/device-overlays tests/simplefb-trigger tests/initramfs-steps tests/storage-guard tests/usb-product tests/grep-q-pipes tests/boot-method tests/modules-push tests/make-bundle-keys tests/make-bundle tests/release-signing tests/pinned-packages tests/flame-probe tests/dipper-probe packages/omamobile-firstboot/test/all packages/omamobile-device-felix/test/all packages/omamobile-device-lynx/test/all packages/omamobile-device-panther/test/all tests/local-builds tests/phone-install-package tests/publish-kernel tests/fetch-toolchain tests/release-upload tests/extract-camera-data tests/release tests/lineage-ref tests/repo-publish tests/repo-publish-stage tests/extract-vendor-firmware tests/alpine-fetch-callers tests/alpine-fetch tests/tools-help tests/install-phone-initramfs; do echo "# $t" "$t" done diff --git a/tests/dipper-probe b/tests/dipper-probe new file mode 100755 index 000000000..033977958 --- /dev/null +++ b/tests/dipper-probe @@ -0,0 +1,71 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +set -euo pipefail +cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." + +bash -n tools/dipper/build +sh -n tools/dipper/initramfs-inside.sh +python3 - <<'PY' +from pathlib import Path + +script = Path('tools/dipper/initramfs-inside.sh').read_text() +assert 'patch-init.py "$stage/init"' in script +assert 'OMAMOBILE_NFSROOT' in Path('tools/dipper/build').read_text() +assert 'omamobile-nfsroot' in script +builder = Path('tools/dipper/build').read_text() +assert 'OUT=${OUT:-$repo/out/dipper}' in builder +assert 'modules_install' not in builder +assert 'FIRMWARE_DIR' not in builder +assert 'fastboot -s boot boot.img' in builder +assert 'panel-samsung-ea8074' not in script +for path in ('init', 'prune-modules.py', 'patch-init.py', 'initramfs-inside.sh', + 'reboot-to.c', 'key.pub'): + assert f':/input/{path}:ro' in builder, f'missing read-only container mount for /input/{path}' +assert 'sh /input/initramfs-inside.sh' in builder +print('ok - Dipper initramfs patch anchors and RAM-only builder inputs') +PY + +python3 - <<'PY' +from pathlib import Path +import subprocess +import tempfile + +with tempfile.TemporaryDirectory() as tmp: + init = Path(tmp) / 'init' + init.write_bytes(Path('initramfs/init').read_bytes()) + subprocess.run(['python3', 'tools/dipper/patch-init.py', str(init)], check=True) + subprocess.run(['sh', '-n', str(init)], check=True) + patched = init.read_text() + assert 'modprobe qcom_wdt' in patched + assert 'watchdog -t 5 -T 30 /dev/watchdog0' in patched + assert 'dropbear -E -s -p "$usb_addr:22" -r /etc/dropbear/dropbear_ed25519_host_key 2>/dev/null && say "ssh: root@$usb_addr"' in patched + assert 'dropbear -R -E -p 22 2>/dev/null && say "ssh: root@172.16.42.1"' not in patched + assert 'no USB host; RAM-only probe will reboot in 20 s' in patched +print('ok - Dipper patch step applies to the checked-in shared init') +PY + +python3 - <<'PY' +import json +from pathlib import Path +import subprocess +import tempfile + +with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + kernel, dtb, ramdisk, out = (root / n for n in ('Image.gz', 'dipper.dtb', 'initramfs.gz', 'out')) + kernel.write_bytes(b'kernel') + dtb.write_bytes(b'\xd0\x0d\xfe\xed' + b'dtb') + ramdisk.write_bytes(b'ramdisk') + subprocess.run(['python3', 'tools/dipper/build-boot.py', '--kernel', str(kernel), + '--dtb', str(dtb), '--ramdisk', str(ramdisk), '--out', str(out)], + check=True, capture_output=True, text=True) + manifest = json.loads((out / 'BOOT-MANIFEST.json').read_text()) + assert manifest['hardware_boot_tested'] is False + assert (out / 'boot.img').read_bytes().startswith(b'ANDROID!') + kernel.write_bytes(b'k' * 0xF80001) + result = subprocess.run(['python3', 'tools/dipper/build-boot.py', '--kernel', str(kernel), + '--dtb', str(dtb), '--ramdisk', str(ramdisk), '--out', str(out)], + capture_output=True, text=True) + assert result.returncode != 0 and 'ABL refuses' in result.stderr +print('ok - Dipper boot image header, size refusal, appended DTB, and manifest') +PY diff --git a/tools/dipper/build b/tools/dipper/build new file mode 100755 index 000000000..3bf10e30e --- /dev/null +++ b/tools/dipper/build @@ -0,0 +1,84 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MIT +# Build a RAM-only Dipper probe: an initramfs (in an aarch64 container), then +# an Android v1 boot image with the DTB appended to the kernel. This tool does +# not flash or push anything to a phone. +# +# Needs: the kernel already built in $KERNEL_SRC (Image.gz, the dipper DTB and +# modules), docker with arm64 emulation registered, zopfli, and SSH_PUBKEY. +set -euo pipefail + +here=$(cd "$(dirname "$0")" && pwd) +repo=$(cd "$here/../.." && pwd) + +KERNEL_DIR=${KERNEL_DIR:-$repo/../linux} +KERNEL_OUT=${KERNEL_OUT:-$KERNEL_DIR} +OUT=${OUT:-$repo/out/dipper} +die() { echo "error: $*" >&2; exit 1; } +: "${SSH_PUBKEY:?set SSH_PUBKEY to your authorized public key}" +ALPINE=${ALPINE:-alpine:3.22} +if [[ -n ${OMAMOBILE_NFSROOT:-} ]]; then + [[ $OMAMOBILE_NFSROOT =~ ^[A-Za-z0-9.-]+:/[A-Za-z0-9._/-]+$ && $OMAMOBILE_NFSROOT != *..* && $OMAMOBILE_NFSROOT != *//* ]] || + die "OMAMOBILE_NFSROOT must be a host and absolute export path, for example 172.16.42.2:/srv/nfs/dipper" +fi + +[[ -d $KERNEL_DIR && -d $KERNEL_OUT ]] || die "set KERNEL_DIR and KERNEL_OUT to the kernel source and build directories" +image=$KERNEL_OUT/arch/arm64/boot/Image.gz +dtb=$KERNEL_OUT/arch/arm64/boot/dts/qcom/sdm845-xiaomi-dipper.dtb +[[ -f $image ]] || die "no $image -- build the kernel first" +[[ -f $dtb ]] || die "no $dtb -- build qcom/sdm845-xiaomi-dipper.dtb first" +[[ -r $SSH_PUBKEY ]] || die "no SSH public key at $SSH_PUBKEY (set SSH_PUBKEY)" +grep -q '^ssh-' "$SSH_PUBKEY" || die "$SSH_PUBKEY is not an OpenSSH public key" +command -v docker >/dev/null || die "docker is needed for the aarch64 initramfs" + +release=$(cat "$KERNEL_OUT/include/config/kernel.release") +echo "kernel release: $release" + +umask 077 +mkdir -p "$OUT"; chmod 700 "$OUT" +mods=$OUT/modules +rm -rf "$mods" "$KERNEL_OUT/modules-staging" +DEVICE=dipper KERNEL_DIR="$KERNEL_DIR" KERNEL_OUT="$KERNEL_OUT" \ + "$repo/bin/omamobile" modules +mkdir -p "$mods/lib/modules" +tar --zstd -xf "$KERNEL_OUT/modules-$release.tar.zst" -C "$mods/lib/modules" + +echo "building the initramfs in $ALPINE (linux/arm64)" +docker run --rm --platform linux/arm64 \ + -v "$repo/initramfs/init:/input/init:ro" \ + -v "$repo/tools/dipper/prune-modules.py:/input/prune-modules.py:ro" \ + -v "$repo/tools/dipper/patch-init.py:/input/patch-init.py:ro" \ + -v "$repo/tools/dipper/initramfs-inside.sh:/input/initramfs-inside.sh:ro" \ + -v "$repo/packages/omamobile-device-cheetah/src/reboot-to.c:/input/reboot-to.c:ro" \ + -v "$mods:/modules:ro" -v "$SSH_PUBKEY:/input/key.pub:ro" \ + -v "$OUT:/out" \ + -e "KERNEL_RELEASE=$release" -e "OUT_UID=$(id -u)" -e "OUT_GID=$(id -g)" \ + -e "OMAMOBILE_NFSROOT=${OMAMOBILE_NFSROOT:-}" \ + "$ALPINE" sh /input/initramfs-inside.sh + +# Pack a copy of the DTB so nothing we do can dirty the kernel tree. +packdtb=$OUT/sdm845-xiaomi-dipper.dtb +cp "$dtb" "$packdtb" +kernel_image=$image +if command -v zopfli >/dev/null; then + echo "compressing the kernel with zopfli for dipper's ABL size limit" + zopfli -c --i1 "$KERNEL_OUT/arch/arm64/boot/Image" > "$OUT/Image.gz" + kernel_image=$OUT/Image.gz +fi +# A failed size check must not leave an older boot.img under the current name. +rm -f "$OUT/images/boot.img" "$OUT/images/BOOT-MANIFEST.json" +"$here/build-boot.py" --kernel "$kernel_image" --dtb "$packdtb" \ + --ramdisk "$OUT/initramfs.cpio.gz" --out "$OUT/images" + +echo +echo "artefacts in $OUT/images:" +ls -l "$OUT/images" +cat <<'NOTE' + +To try it without writing to the phone (nothing persists): + + fastboot -s boot boot.img + +Do not flash this image. Dipper has no second OS slot and this RAM-only path +has no persistent update or recovery flow. +NOTE diff --git a/tools/dipper/build-boot.py b/tools/dipper/build-boot.py new file mode 100755 index 000000000..d1210b2e8 --- /dev/null +++ b/tools/dipper/build-boot.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: MIT +"""Pack dipper's kernel + appended DTB and the initramfs into an Android v1 boot image. + +Every offset here was read off the phone's own stock boot image (header v1, +page 4096, kernel 0x8000, ramdisk 0x1000000, tags 0x100, base 0), so the image +we hand ABL has the same geometry as the one it already boots. The DTB is +appended to the kernel because dipper's stock image carries its FDTs that way +(two concatenated FDTs at the end of the kernel blob, selected by msm-id). + +No mkbootimg dependency: the v1 header is 1648 bytes of fixed layout and we +verify what we wrote before returning. +""" +import argparse, hashlib, json, struct, sys +from pathlib import Path + +PAGE = 4096 +BASE = 0x00000000 +KERNEL_OFF = 0x00008000 +# Stock's own value, and the only one seen to boot on this phone. It leaves +# 16 MiB - 32 KiB for the kernel; check_layout() refuses anything larger rather +# than letting ABL answer with +# Failed to load/authenticate boot image: 00000050 +# which it does with no kernel output and no clue. +RAMDISK_OFF = 0x01000000 +SECOND_OFF = 0x00f00000 +TAGS_OFF = 0x00000100 +HDR_VERSION = 1 +HDR_SIZE = 1648 +# From the phone: console on the framebuffer, early console on the SoC's UART +# (stock passes earlycon=msm_geni_serial,0xA84000), then the shared init. +CMDLINE = ("console=tty0 earlycon=msm_geni_serial,0xa84000 rdinit=/init " + "panic=10 printk.devkmsg=on rootwait") +# dipper's boot partition, measured: 65536 blocks of 1 KiB. +BOOT_PARTITION_BYTES = 64 * 1024 * 1024 +# This ABL refuses a larger kernel with only +# Booting FAILED (remote: 'Failed to load/authenticate boot image: 00000050') +# Measured on the phone: 16,247,767 bytes boots, 16,343,459 does not. +KERNEL_MAX = 0xF80000 + + +def pad(b: bytes) -> bytes: + r = len(b) % PAGE + return b + (b"\0" * (PAGE - r) if r else b"") + + +def check_layout(kernel: bytes, ramdisk: bytes) -> None: + """The kernel must end before the ramdisk begins, or ABL rejects the image.""" + if len(kernel) > KERNEL_MAX: + raise SystemExit( + f"kernel+dtb is {len(kernel)} bytes; this phone's ABL refuses more " + f"than {KERNEL_MAX} ({KERNEL_MAX / 1048576:.2f} MiB) and says only " + "'Failed to load/authenticate boot image: 00000050'. Move drivers " + "to modules.") + room = RAMDISK_OFF - KERNEL_OFF + if len(kernel) > room: + raise SystemExit( + f"kernel+dtb is {len(kernel)} bytes but only {room} fit between " + f"kernel_offset {KERNEL_OFF:#x} and ramdisk_offset {RAMDISK_OFF:#x}; " + "raise RAMDISK_OFF or shrink the kernel") + end = RAMDISK_OFF + len(ramdisk) + if end > BOOT_PARTITION_BYTES * 4: + raise SystemExit(f"the loaded image would reach {end:#x}, which is implausible") + + +def build_header(kernel: bytes, ramdisk: bytes, cmdline: str) -> bytes: + cmd = cmdline.encode() + if len(cmd) > 511: + raise SystemExit(f"cmdline is {len(cmd)} bytes; the v1 header holds 512 with its NUL") + h = bytearray(PAGE) + h[0:8] = b"ANDROID!" + struct.pack_into("<10I", h, 8, + len(kernel), BASE + KERNEL_OFF, + len(ramdisk), BASE + RAMDISK_OFF, + 0, BASE + SECOND_OFF, # no second stage: the DTB is appended + BASE + TAGS_OFF, PAGE, HDR_VERSION, 0) + h[48:64] = b"\0" * 16 # product name: stock leaves it empty too + h[64:64 + len(cmd)] = cmd + # id[32] at 576 and extra_cmdline[1024] at 608 stay zero. + struct.pack_into(" None: + """Re-read what we wrote: a wrong header silently bricks a boot attempt.""" + if img[:8] != b"ANDROID!": + raise SystemExit("bad magic in the image we just built") + ks, ka, rs, ra, ss, sa, tags, ps, hv, _ = struct.unpack_from("<10I", img, 8) + if (ks, rs, ss) != (len(kernel), len(ramdisk), 0): + raise SystemExit(f"sizes disagree: {(ks, rs, ss)}") + if (ka, ra, tags, ps, hv) != (BASE + KERNEL_OFF, BASE + RAMDISK_OFF, BASE + TAGS_OFF, PAGE, HDR_VERSION): + raise SystemExit(f"geometry disagrees with the stock image: {(ka, ra, tags, ps, hv)}") + if struct.unpack_from(" int: + ap = argparse.ArgumentParser() + ap.add_argument("--kernel", type=Path, required=True, help="Image.gz") + ap.add_argument("--dtb", type=Path, required=True, help="sdm845-xiaomi-dipper.dtb") + ap.add_argument("--ramdisk", type=Path, required=True, help="initramfs.cpio.gz") + ap.add_argument("--out", type=Path, required=True, help="output directory") + ap.add_argument("--cmdline", default=CMDLINE) + a = ap.parse_args() + + a.out.mkdir(parents=True, exist_ok=True) + kernel = a.kernel.read_bytes() + a.dtb.read_bytes() # Image.gz-dtb + ramdisk = a.ramdisk.read_bytes() + (a.out / "Image.gz-dtb").write_bytes(kernel) + + check_layout(kernel, ramdisk) + img = build_header(kernel, ramdisk, a.cmdline) + pad(kernel) + pad(ramdisk) + verify(img, kernel, ramdisk, a.cmdline) + if len(img) > BOOT_PARTITION_BYTES: + raise SystemExit(f"image is {len(img)} bytes; dipper's boot partition holds " + f"{BOOT_PARTITION_BYTES}") + + out = a.out / "boot.img" + out.write_bytes(img) + manifest = { + "device": "dipper", + "header_version": HDR_VERSION, + "pagesize": PAGE, + "cmdline": a.cmdline, + "kernel_bytes": len(kernel), + "dtb_bytes": a.dtb.stat().st_size, + "ramdisk_bytes": len(ramdisk), + "boot_bytes": len(img), + "boot_partition_bytes": BOOT_PARTITION_BYTES, + "headroom_bytes": BOOT_PARTITION_BYTES - len(img), + "kernel_sha256": hashlib.sha256(kernel).hexdigest(), + "ramdisk_sha256": hashlib.sha256(ramdisk).hexdigest(), + "boot_sha256": hashlib.sha256(img).hexdigest(), + "hardware_boot_tested": False, + } + (a.out / "BOOT-MANIFEST.json").write_text(json.dumps(manifest, indent=2) + "\n") + print(f"packed {out} ({len(img)} bytes, {manifest['headroom_bytes']} spare in the partition)") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/dipper/initramfs-inside.sh b/tools/dipper/initramfs-inside.sh new file mode 100755 index 000000000..96f70c870 --- /dev/null +++ b/tools/dipper/initramfs-inside.sh @@ -0,0 +1,98 @@ +#!/bin/sh +# SPDX-License-Identifier: MIT +# Runs inside an aarch64 Alpine container. Only files under /out change. +set -eu + +: "${KERNEL_RELEASE:?KERNEL_RELEASE must be passed in}" + +apk add --no-cache cpio gcc musl-dev gzip kmod python3 dropbear + +stage=/tmp/dipper-initramfs +mkdir -p "$stage" +apk --root "$stage" --initdb --no-cache \ + --repositories-file /etc/apk/repositories --keys-dir /etc/apk/keys add \ + alpine-baselayout busybox busybox-extras dropbear iproute2 util-linux \ + e2fsprogs e2fsprogs-extra kmod + +mkdir -p "$stage/proc" "$stage/sys" "$stage/dev" "$stage/run" "$stage/root/.ssh" \ + "$stage/usr/lib/modules" "$stage/usr/lib/firmware" "$stage/etc/dropbear" "$stage/usr/local/bin" +# Alpine is not usr-merged; kmod and the kernel look below /lib. +ln -s ../usr/lib/modules "$stage/lib/modules" +ln -s ../usr/lib/firmware "$stage/lib/firmware" + +cp /input/key.pub "$stage/root/.ssh/authorized_keys" +chmod 700 "$stage/root/.ssh" +chmod 600 "$stage/root/.ssh/authorized_keys" + +# Keep the rescue server's host key stable across rebuilds, so the laptop's +# known_hosts does not change under us. Private key stays in local output. +hostkey=/out/dropbear_ed25519_host_key +if [ ! -s "$hostkey" ]; then + umask 077 + dropbearkey -t ed25519 -f "$hostkey" >/dev/null 2>&1 +fi +cp "$hostkey" "$stage/etc/dropbear/dropbear_ed25519_host_key" +chmod 600 "$stage/etc/dropbear/dropbear_ed25519_host_key" +chown "${OUT_UID:-0}:${OUT_GID:-0}" "$hostkey" + +cp -a "/modules/lib/modules/$KERNEL_RELEASE" "$stage/usr/lib/modules/" +# The build tree symlinks point at the builder's paths; they are useless here. +rm -f "$stage/usr/lib/modules/$KERNEL_RELEASE/build" \ + "$stage/usr/lib/modules/$KERNEL_RELEASE/source" +depmod -b "$stage" "$KERNEL_RELEASE" + +# Keep only the watchdog module this initramfs loads, with its dependencies. +# The full tree is ~1764 modules and dominates the image: with the PHYs, UFS, +# i2c and simpledrm built in, almost none of it is reachable here, and ABL +# refuses a boot image that grows too large. Everything else belongs in the +# root filesystem, not here. The pruner reads modules.dep and fails loudly +# rather than silently emptying the tree. +python3 /input/prune-modules.py \ + "$stage/usr/lib/modules/$KERNEL_RELEASE" qcom_wdt +depmod -b "$stage" "$KERNEL_RELEASE" + +# The modules the init loads must exist AND match this kernel exactly, or the +# phone boots to a black screen with no watchdog taken over. +for module in qcom_wdt; do + path=$(modinfo -b "$stage" -k "$KERNEL_RELEASE" -n "$module") + [ "$(modinfo -F vermagic "$path" | cut -d ' ' -f1)" = "$KERNEL_RELEASE" ] || { + echo "module $module does not match $KERNEL_RELEASE" >&2; exit 1; } +done + +cp /input/init "$stage/init" +python3 /input/patch-init.py "$stage/init" +chmod 755 "$stage/init" + +echo xiaomi,dipper > "$stage/etc/omamobile-compatible" +: > "$stage/etc/omamobile-ufs-rw" +if [ -n "${OMAMOBILE_NFSROOT:-}" ]; then + case "$OMAMOBILE_NFSROOT" in + *[!A-Za-z0-9.:/_-]*|*..*|*//*|:*:*) echo "invalid OMAMOBILE_NFSROOT" >&2; exit 1 ;; + esac + case "$OMAMOBILE_NFSROOT" in *:/*) ;; *) echo "invalid OMAMOBILE_NFSROOT" >&2; exit 1 ;; esac + printf '%s\n' "$OMAMOBILE_NFSROOT" > "$stage/etc/omamobile-nfsroot" +fi + +echo 'Xiaomi Mi 8 Omarchy' > "$stage/etc/omamobile-usb-product" + +gcc -Os -static -o "$stage/usr/local/bin/reboot-to" \ + /input/reboot-to.c + +# Prove the packaged userspace actually runs on ARM before shipping it. +chroot "$stage" /bin/busybox true +chroot "$stage" /usr/sbin/dropbear -h >/tmp/dropbear-help 2>&1 || [ "$?" = 1 ] +grep -q 'Dropbear' /tmp/dropbear-help + +# Reproducible archive: fixed mtimes, sorted, no owners. +python3 - "$stage" <<'PY' +import os, sys +from pathlib import Path +root = Path(sys.argv[1]) +for path in list(root.rglob('*')) + [root]: + os.utime(path, (1790636353, 1790636353), follow_symlinks=False) +PY +(cd "$stage" && find . -print0 | sort -z | cpio --null --quiet -o -H newc --owner=0:0 --reproducible) \ + | gzip -n -9 > /out/initramfs.cpio.gz +cp /tmp/dropbear-help /out/dropbear-help.txt +chown "${OUT_UID:-0}:${OUT_GID:-0}" /out/initramfs.cpio.gz /out/dropbear-help.txt +echo "dipper initramfs built for $KERNEL_RELEASE; all UFS partitions stay read-only" diff --git a/tools/dipper/patch-init.py b/tools/dipper/patch-init.py new file mode 100644 index 000000000..a2e1b46f3 --- /dev/null +++ b/tools/dipper/patch-init.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: MIT +from pathlib import Path +import sys + +p = Path(sys.argv[1]) +s = p.read_text() +patches = ( + ("hostname omamobile\n", + """# SDM845's watchdogs keep running until a driver claims them. +modprobe qcom_wdt || echo "omamobile: qcom watchdog module failed" >/dev/kmsg +if [ -c /dev/watchdog0 ]; then + watchdog -t 5 -T 30 /dev/watchdog0 +fi +hostname omamobile +""", "hostname anchor"), + ('dropbear -R -E -p 22 2>/dev/null && say "ssh: root@172.16.42.1"\n', + """usb_addr=$(ip -o -4 addr show dev usb0 scope global | awk '{ sub(/\\/.*/, "", $4); print $4; exit }') +if [ -n "$usb_addr" ]; then + dropbear -E -s -p "$usb_addr:22" -r /etc/dropbear/dropbear_ed25519_host_key 2>/dev/null && say "ssh: root@$usb_addr" +fi +""", "dropbear anchor"), + ("no USB host, crashing in 20 s so ABL falls back to slot A", + "no USB host; RAM-only probe will reboot in 20 s", + "no-USB recovery message"), +) +for old, new, name in patches: + assert s.count(old) == 1, f"shared init changed: {name}" + s = s.replace(old, new) +p.write_text(s) diff --git a/tools/dipper/prune-modules.py b/tools/dipper/prune-modules.py new file mode 100755 index 000000000..cc737b13f --- /dev/null +++ b/tools/dipper/prune-modules.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: MIT +"""Keep only the named modules and their dependencies in a staged module tree. + +Reads modules.dep directly rather than asking modprobe: its -d/-b/-S flags are +easy to get wrong (-b is --use-blacklist, not a base directory), and a silent +mis-resolution deletes the whole tree. + + prune-modules.py [name ...] +""" +import sys +from pathlib import Path + + +def basename(path: str) -> str: + n = Path(path).name + for suffix in (".ko.zst", ".ko.xz", ".ko.gz", ".ko"): + if n.endswith(suffix): + return n[: -len(suffix)] + return n + + +def main() -> int: + moddir = Path(sys.argv[1]) + wanted = {n.replace("-", "_") for n in sys.argv[2:]} + dep = moddir / "modules.dep" + if not dep.is_file(): + raise SystemExit(f"no {dep}") + + deps, by_name = {}, {} + for line in dep.read_text().splitlines(): + if ":" not in line: + continue + target, rest = line.split(":", 1) + target = target.strip() + deps[target] = rest.split() + by_name[basename(target).replace("-", "_")] = target + + missing = wanted - set(by_name) + if missing: + raise SystemExit(f"not in modules.dep: {', '.join(sorted(missing))}") + + keep, queue = set(), [by_name[n] for n in wanted] + while queue: + cur = queue.pop() + if cur in keep: + continue + keep.add(cur) + queue.extend(deps.get(cur, [])) + + kept_paths = {(moddir / k).resolve() for k in keep} + before = removed = 0 + for f in moddir.rglob("*"): + if not f.is_file() or ".ko" not in f.name: + continue + before += 1 + if f.resolve() not in kept_paths: + f.unlink() + removed += 1 + for d in sorted(moddir.rglob("*"), key=lambda p: -len(p.parts)): + if d.is_dir() and not any(d.iterdir()): + d.rmdir() + + print(f"kept {len(keep)} of {before} modules: " + f"{', '.join(sorted(basename(k) for k in keep))}") + if len(keep) < len(wanted): + raise SystemExit("fewer modules kept than asked for") + return 0 + + +if __name__ == "__main__": + sys.exit(main())