Skip to content
Draft
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
151 changes: 129 additions & 22 deletions bin/omamobile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ UFS_RW_ALLOWED="boot_b init_boot_b vendor_boot_b vendor_kernel_boot_b dtbo_b use
# backslashes, so other tools can read it too. Nothing in it is executed.
load_device() {
local file="$ROOT/device/$DEVICE/device.conf" line n=0 key p
if [[ ${FLAME_NATIVE:-0} == 1 ]]; then
[[ $DEVICE == flame ]] || die "FLAME_NATIVE=1 requires DEVICE=flame"
file="$ROOT/device/flame/native/device.conf"
fi
local quoted='^([A-Z][A-Z0-9_]*)="([^"$`\\]*)"$' bare='^([A-Z][A-Z0-9_]*)=([^"$`\\'"'"'[:space:]]*)$'
DEV_BOOT_METHOD=android-v4 # legacy descriptors omit it
[[ $DEVICE =~ ^[a-z0-9]+$ ]] || die "DEVICE must be a codename, not '$DEVICE'"
Expand All @@ -74,27 +78,37 @@ load_device() {
die "$file:$n: not a KEY=value line"
fi
done < "$file"
[[ $DEV_BOOT_METHOD == android-v4 ]] \
|| die "$file: unsupported BOOT_METHOD=$DEV_BOOT_METHOD; this command supports only android-v4"
for key in CODENAME SOC PACKAGE DTB DTBO_SOURCE DTBO_BOARD_ID DTBO_BOARD_REV VENDOR_CMDLINE \
VENDOR_PAGESIZE OS_VERSION OS_PATCH_LEVEL BOOT_IMAGES UFS_RW INITRAMFS_INIT PANEL_SIZE; do
local required="CODENAME SOC BOOT_IMAGES UFS_RW PANEL_SIZE"
case $DEV_BOOT_METHOD in
android-v4)
required+=" PACKAGE DTB DTBO_SOURCE DTBO_BOARD_ID DTBO_BOARD_REV VENDOR_CMDLINE VENDOR_PAGESIZE OS_VERSION OS_PATCH_LEVEL INITRAMFS_INIT"
;;
uboot-ext4)
[[ ${DEV_CODENAME:-} == flame && ${DEV_SOC:-} == sm8150 ]] || die "$file: unsupported BOOT_METHOD=$DEV_BOOT_METHOD; uboot-ext4 is implemented only for flame"
[[ ${DEV_BOOT_IMAGES:-} == boot && ${DEV_UFS_RW:-} == 'boot_b userdata' ]] || die 'uboot-ext4 requires boot only and UFS_RW="boot_b userdata"'
;;
*) die "$file: unsupported BOOT_METHOD=$DEV_BOOT_METHOD; use a supported device profile" ;;
esac
for key in $required; do
p="DEV_$key"
[[ -n ${!p:-} ]] || die "$file: $key is missing"
done
# ABL picks the base DTB by /soc_id (gs101, gs201) or by /soc_compatible
# (zuma): a descriptor names exactly one of the two.
if [[ -n ${DEV_SOC_IDS:-} && -n ${DEV_SOC_COMPATIBLE:-} ]]; then
die "$file: SOC_IDS and SOC_COMPATIBLE are exclusive; name the one this bootloader matches on"
elif [[ -z ${DEV_SOC_IDS:-} && -z ${DEV_SOC_COMPATIBLE:-} ]]; then
die "$file: SOC_IDS is missing (or SOC_COMPATIBLE, for a bootloader that matches /soc_compatible)"
if [[ $DEV_BOOT_METHOD == android-v4 ]]; then
# ABL picks the base DTB by /soc_id (gs101, gs201) or by /soc_compatible
# (zuma): a descriptor names exactly one of the two.
if [[ -n ${DEV_SOC_IDS:-} && -n ${DEV_SOC_COMPATIBLE:-} ]]; then
die "$file: SOC_IDS and SOC_COMPATIBLE are exclusive; name the one this bootloader matches on"
elif [[ -z ${DEV_SOC_IDS:-} && -z ${DEV_SOC_COMPATIBLE:-} ]]; then
die "$file: SOC_IDS is missing (or SOC_COMPATIBLE, for a bootloader that matches /soc_compatible)"
fi
local sc='^[a-z0-9]+(-[a-z0-9]+)*:0x[0-9a-f]+:[0-9]+:[0-9]+:[0-9]+$' labels=" "
for p in ${DEV_SOC_COMPATIBLE:-}; do
[[ $p =~ $sc ]] || die "$file: SOC_COMPATIBLE entries are label:product_id:major:minor:pkg_mode, not $p"
# One child node per entry: the label names it, so it must be unique
[[ $labels != *" ${p%%:*} "* ]] || die "$file: SOC_COMPATIBLE names ${p%%:*} twice"
labels+="${p%%:*} "
done
fi
local sc='^[a-z0-9]+(-[a-z0-9]+)*:0x[0-9a-f]+:[0-9]+:[0-9]+:[0-9]+$' labels=" "
for p in ${DEV_SOC_COMPATIBLE:-}; do
[[ $p =~ $sc ]] || die "$file: SOC_COMPATIBLE entries are label:product_id:major:minor:pkg_mode, not $p"
# One child node per entry: the label names it, so it must be unique
[[ $labels != *" ${p%%:*} "* ]] || die "$file: SOC_COMPATIBLE names ${p%%:*} twice"
labels+="${p%%:*} "
done
[[ $DEV_CODENAME == "$DEVICE" ]] || die "$file: CODENAME=$DEV_CODENAME, expected $DEVICE"
# Safety: the guard's allowlist may name slot B's boot images and userdata
# only, must hold userdata, and every boot image we write must be on it.
Expand All @@ -119,9 +133,11 @@ load_device() {
[[ $seen != *" $p "* ]] || die "$file: BOOT_IMAGES names $p twice"
seen+="$p "
done
for p in boot vendor_boot dtbo; do
[[ $seen == *" $p "* ]] || die "$file: BOOT_IMAGES must include $p"
done
if [[ $DEV_BOOT_METHOD == android-v4 ]]; then
for p in boot vendor_boot dtbo; do
[[ $seen == *" $p "* ]] || die "$file: BOOT_IMAGES must include $p"
done
fi
# AVB hash footers, for a bootloader whose vbmeta chains these partitions
# (zuma): image:partition_size, each a boot image, named once, the size
# a whole number of 4 KiB blocks. Their rollback index and security patch
Expand Down Expand Up @@ -329,6 +345,7 @@ cmd_initramfs_config() {
cmd_initramfs() {
local dl="$WORK/downloads/alpine" stage="$KERNEL_OUT/initramfs" ver=3.24.2
load_device
[[ $DEV_BOOT_METHOD == android-v4 ]] || die "use tools/build-flame-system for the native flame payload"
# A leading slash is repo-root relative, so devices share initramfs/init.
# Anything else is relative to device/<codename>/.
local init
Expand Down Expand Up @@ -452,6 +469,45 @@ cmd_initramfs() {

cmd_images() {
load_device
if [[ $DEV_BOOT_METHOD == uboot-ext4 ]]; then
local image=${UBOOT_IMAGE:-} expected=${UBOOT_SHA256:-} got role=${FLAME_BOOT_ROLE:-loader}
[[ $role == loader || $role == stock-recovery ]] || die 'FLAME_BOOT_ROLE must be loader or stock-recovery'
[[ -f $image && $expected =~ ^[a-f0-9]{64}$ ]] || die 'supply the reviewed UBOOT_IMAGE and UBOOT_SHA256'
got=$(sha256sum < "$image" | cut -d' ' -f1)
[[ $got == "$expected" ]] || die 'U-Boot image checksum mismatch'
python3 - "$image" "$role" <<'PY'
import pathlib, struct, sys
p = pathlib.Path(sys.argv[1])
with p.open('rb') as stream:
header = stream.read(48)
version = 0 if sys.argv[2] == 'loader' else 2
if len(header) != 48 or header[:8] != b'ANDROID!' or struct.unpack_from('<I', header, 40)[0] != version:
sys.exit('Wrong Android header for the selected flame boot role')
if not 4096 < p.stat().st_size <= 67108864:
sys.exit('U-Boot image exceeds the boot partition or is truncated')
if sys.argv[2] == 'stock-recovery' and p.stat().st_size != 67108864:
sys.exit('Expected the verified 64 MiB flame stock boot backup')
if sys.argv[2] == 'loader':
kernel, address, ramdisk = struct.unpack_from('<3I', header, 8)
page, = struct.unpack_from('<I', header, 36)
if page != 4096 or address != 0x8000 or ramdisk != 0 or not 0 < kernel <= 4*1024*1024:
sys.exit('Invalid flame loader page, address, ramdisk or kernel size')
if p.stat().st_size != 4096 + ((kernel+4095)//4096)*4096:
sys.exit('Truncated or extra loader image sections')
with p.open('rb') as stream:
stream.seek(4096)
if stream.read(3) != b'\x1f\x8b\x08':
sys.exit('Expected compressed U-Boot kernel payload')
PY
mkdir -p "$KERNEL_OUT/images"
cp "$image" "$KERNEL_OUT/images/boot.img"
printf '%s\n' "$DEVICE" > "$KERNEL_OUT/images/DEVICE"
printf '%s\n' "$DEV_BOOT_METHOD" > "$KERNEL_OUT/images/BOOT_METHOD"
printf '%s\n' "$role" > "$KERNEL_OUT/images/BOOT_ROLE"
(cd "$KERNEL_OUT/images" && sha256sum boot.img > SHA256SUMS)
say 'Staged reviewed U-Boot image; Linux stays in /boot/omamobile/boot.img on userdata'
return
fi
local img="$KERNEL_OUT/images" tools="$SRC_DIR/system/tools/mkbootimg"
local dtc="$KERNEL_OUT/scripts/dtc/dtc" boot="$KERNEL_OUT/arch/arm64/boot"
local dtb="$boot/dts/$DEV_DTB"
Expand Down Expand Up @@ -663,6 +719,18 @@ fastboot_serial() {
}

cmd_fastboot() {
if [[ ${FLAME_NATIVE:-0} == 1 ]]; then
load_device
in_fastboot && return 0
"$ROOT/tools/phone-ssh" 'systemctl --no-block --reboot-argument=bootloader reboot' \
|| die 'native flame reboot request failed; use Power + Volume Down'
local attempt
for ((attempt=0; attempt<30; attempt++)); do
sleep 2
in_fastboot && return 0
done
die 'native flame did not enter Android fastboot; use the buttons. No slot-A fallback attempted.'
fi
local ssh="$ROOT/tools/phone-ssh" i
in_fastboot && { say "Already in fastboot"; return 0; }
for i in $(seq 1 12); do
Expand Down Expand Up @@ -699,32 +767,71 @@ cmd_flash() {
serial=$(fastboot_serial) || exit 1
# Every image and allowlist entry first: never start writing slot B and
# find out halfway that a later image is missing.
if [[ $DEV_BOOT_METHOD == uboot-ext4 ]]; then
[[ $(cat "$img/BOOT_METHOD" 2>/dev/null) == uboot-ext4 ]] || die 'image boot method mismatch'
(cd "$img" && sha256sum -c SHA256SUMS) || die 'staged boot checksum mismatch'
local facts role
role=$(cat "$img/BOOT_ROLE" 2>/dev/null)
[[ $role == loader || $role == stock-recovery ]] || die 'missing flame image role'
facts=''
local variable value
for variable in product unlocked current-slot version-bootloader slot-successful:b; do
value=$(timeout 10 fastboot -s "$serial" getvar "$variable" 2>&1) || die "cannot inspect Android fastboot: $variable"
facts+="$value"$'\n'
done
[[ $facts != *U-Boot* ]] || die 'return to Android fastboot before flashing'
printf '%s\n' "$facts" | grep -Eq '(^|[[:space:]])product:[[:space:]]*flame[[:space:]]*$' || die 'connected product is not flame'
printf '%s\n' "$facts" | grep -Eq '(^|[[:space:]])unlocked:[[:space:]]*yes[[:space:]]*$' || die 'bootloader is locked'
printf '%s\n' "$facts" | grep -Eq '(^|[[:space:]])current-slot:[[:space:]]*b[[:space:]]*$' || die 'flame requires the already-active, recovery-tested slot B'
if [[ $role == loader ]]; then
printf '%s\n' "$facts" | grep -Eq '(^|[[:space:]])slot-successful:b:[[:space:]]*yes[[:space:]]*$' || die 'slot B must already be marked successful'
fi
fi
# Check every file before starting any write, including shared Android paths.
for p in $DEV_BOOT_IMAGES; do
slot_b_allowed "$p" || die "${p}_b is not on device/$DEVICE's storage-guard allowlist; refusing"
[[ -f "$img/$p.img" ]] || die "missing $img/$p.img (run: omamobile images)"
done
local out rc
for p in $DEV_BOOT_IMAGES; do
if [[ $DEV_BOOT_METHOD == uboot-ext4 && $(cat "$img/BOOT_ROLE") == loader ]]; then
timeout 30 fastboot -s "$serial" erase "${p}_b" || die 'boot B erase failed; staying in fastboot'
fi
rc=0
out=$(timeout 60 fastboot -s "$serial" flash "${p}_b" "$img/$p.img" 2>&1) || rc=$?
echo "$out" | grep -E 'OKAY|FAIL' | tail -1 | sed "s/^/$p: /" || true
# Stop before activating or rebooting into a half-written slot B
# Stop before activating or rebooting into a half-written slot B.
((rc == 0)) || die "flashing ${p}_b failed (exit $rc): slot B is incomplete, flash again before rebooting"
done
timeout 20 fastboot -s "$serial" --set-active=b >/dev/null 2>&1
if [[ $DEV_BOOT_METHOD == uboot-ext4 && $(cat "$img/BOOT_ROLE") == stock-recovery ]]; then
say 'Verified stock boot B restored; staying in Android fastboot. Factory-reset Android data through recovery before starting Android.'
return
fi
# Keep flame's existing successful-slot metadata: set_active would reset it,
# and its protected devinfo is never written by the Linux development root.
if [[ $DEV_BOOT_METHOD == android-v4 ]]; then
timeout 20 fastboot -s "$serial" --set-active=b >/dev/null 2>&1
else
timeout 10 fastboot -s "$serial" getvar slot-successful:b 2>&1 | grep -Eq '(^|[[:space:]])slot-successful:b:[[:space:]]*yes[[:space:]]*$' \
|| die 'slot B success is clear; staying in fastboot. The boot-success policy decision is pending; use physical Fastboot recovery before any further boots.'
fi
timeout 20 fastboot -s "$serial" reboot >/dev/null 2>&1
say "Booting slot B; waiting for SSH"
local i
for i in $(seq 1 45); do
"$ROOT/tools/phone-ssh" 'uname -v; cut -d" " -f1 /proc/uptime' 2>/dev/null && return 0
sleep 2
done
if [[ $DEV_BOOT_METHOD == uboot-ext4 ]]; then
die 'no SSH after flame boot; use the buttons to return to Android fastboot and follow the native recovery runbook'
fi
die "no SSH after boot (the safety net returns to fastboot within about a minute)"
}

cmd_push() {
local img="$KERNEL_OUT/images" ssh="$ROOT/tools/phone-ssh" p dev want got rel
load_device
[[ $DEV_BOOT_METHOD == android-v4 ]] || die "native flame writes require Android fastboot; use flash"
images_for_device "$img"
"$ssh" true || die "phone not reachable at 172.16.42.1"
# Refuse before any write. A phone whose initramfs has no reboot-to would
Expand Down
95 changes: 95 additions & 0 deletions device/flame/native/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/sh
# SPDX-License-Identifier: MIT
# Runs inside tools/build-flame-probe's disposable aarch64 Alpine container.
set -eu
apk add --no-cache gcc musl-dev cpio dtc python3 gzip kmod zstd

[ "$(fdtget /input/flame.dtb / model)" = 'Google Pixel 4' ] || {
echo 'Not a Google Pixel 4 device tree' >&2; exit 1;
}
case " $(fdtget /input/flame.dtb / compatible) " in
*' qcom,sm8150 '*) ;;
*) echo 'Not an SM8150 device tree' >&2; exit 1 ;;
esac
gzip -t /input/kernel || { echo 'Expected a gzip-compressed ARM64 Image' >&2; exit 1; }
gzip -dc /input/kernel > /tmp/Image
python3 - <<'PY'
from pathlib import Path
import sys
with Path('/tmp/Image').open('rb') as stream:
stream.seek(56)
if stream.read(4) != b'ARM\x64':
sys.exit('Kernel has no ARM64 Image header')
PY
release=$(strings /tmp/Image | sed -n 's/^Linux version \([^ ]*\) .*/\1/p' | head -1)
rm /tmp/Image

stage=/tmp/rootfs
mkdir -p "$stage"
apk --root "$stage" --initdb --no-cache \
--repositories-file /etc/apk/repositories --keys-dir /etc/apk/keys add \
alpine-baselayout busybox dropbear iproute2 util-linux busybox-extras e2fsprogs
install -m755 /system-init "$stage/init"
mkdir -p "$stage/root/.ssh" "$stage/proc" "$stage/sys" "$stage/dev" "$stage/run"
cp /input/key.pub "$stage/root/.ssh/authorized_keys"
chmod 700 "$stage/root/.ssh"
chmod 600 "$stage/root/.ssh/authorized_keys"
gcc -Os -static -o "$stage/sbin/reboot-to" \
/src/packages/omamobile-device-cheetah/src/reboot-to.c
mkdir -p "$stage/lib/modules"
module=$stage/lib/modules/qcom-wdt.ko
if zstd -t /input/watchdog 2>/dev/null; then
zstd -dc /input/watchdog > "$module"
else
cp /input/watchdog "$module"
fi
[ "$(modinfo -F name "$module")" = qcom_wdt ] || { echo 'Expected qcom-wdt module' >&2; exit 1; }
if [ -z "$release" ] || [ "$(modinfo -F vermagic "$module" | cut -d ' ' -f1)" != "$release" ]; then
echo 'Watchdog module does not match the kernel release' >&2
exit 1
fi
[ -z "$(modinfo -F depends "$module")" ] || { echo 'Watchdog needs additional modules' >&2; exit 1; }

# A separate RAM rescue root survives the desktop handoff.
rescue=/tmp/rescue-base
mkdir -p "$rescue"
apk --root "$rescue" --initdb --no-cache \
--repositories-file /etc/apk/repositories --keys-dir /etc/apk/keys add \
alpine-baselayout busybox dropbear iproute2 util-linux
mkdir -p "$rescue/root/.ssh" "$rescue/proc" "$rescue/sys" "$rescue/dev" "$rescue/run" "$rescue/realroot"
cp /input/key.pub "$rescue/root/.ssh/authorized_keys"
chmod 700 "$rescue/root/.ssh"
chmod 600 "$rescue/root/.ssh/authorized_keys"
cp "$stage/sbin/reboot-to" "$rescue/sbin/reboot-to"
mv "$rescue" "$stage/rescue-base"

# No vendor firmware enters the probe. Only the watchdog module is included;
# remote processors stay disabled unless the explicit Wi-Fi profile below is used.
cp /input/flame.dtb /out/flame.dtb
usb=/soc@0/usb@a6f8800/usb@a600000
fdtput -t s /out/flame.dtb "$usb" dr_mode peripheral
fdtput -d /out/flame.dtb "$usb" usb-role-switch
for node in remoteproc@2400000 remoteproc@4080000 remoteproc@8300000 remoteproc@17300000; do
fdtput -t s /out/flame.dtb "/soc@0/$node" status disabled
done
if [ "${FLAME_WIFI:-0}" = 1 ]; then
[ "$(fdtget /out/flame.dtb /soc@0/remoteproc@4080000 compatible)" = qcom,sm8150-mpss-pas ] || exit 1
fdtput -t s /out/flame.dtb /soc@0/remoteproc@4080000 status okay
fi
(cd "$stage" && find . -print0 | cpio --null --quiet -o -H newc --owner=0:0) \
| gzip -n -9 > /out/initramfs.cpio.gz
# U-Boot recognizes these standard Android addresses as sentinels: decompress
# to its kernel_addr_r and leave the ramdisk in its download buffer. Using
# Android ABL's base=0 instead asks U-Boot to write to physical 0x8000.
# Keep /dev/console off USB for standalone operation. USB networking and an
# explicitly started serial login remain available without being boot consoles.
python3 /mkbootimg/mkbootimg.py --header_version 2 \
--base 0x10000000 --kernel_offset 0x8000 --ramdisk_offset 0x1000000 \
--dtb_offset 0x1f00000 --pagesize 4096 \
--kernel /input/kernel --ramdisk /out/initramfs.cpio.gz --dtb /out/flame.dtb \
--os_version 13.0.0 --os_patch_level 2022-10 \
--cmdline 'console=tty0 rdinit=/init clk_ignore_unused pd_ignore_unused fw_devlink=permissive panic=10 printk.devkmsg=on' \
-o /out/boot.img
size=$(stat -c %s /out/boot.img)
[ "$size" -le 67108864 ] || { rm /out/boot.img; echo 'Probe exceeds the 64 MiB boot partition' >&2; exit 1; }
(cd /out && sha256sum boot.img flame.dtb initramfs.cpio.gz > SHA256SUMS)
14 changes: 14 additions & 0 deletions device/flame/native/device.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SPDX-License-Identifier: MIT
# Explicit opt-in development profile: DEVICE=flame FLAME_NATIVE=1.
# The default device/flame/device.conf remains RAM-only.
# Experimental U-Boot layout. Requires a locally built, reviewed loader and
# a separately tested native root. No Tensor images or DTBO are written.
CODENAME=flame
NAME="Google Pixel 4"
SOC=sm8150
COMPATIBLE="google,floral-sm8150"
BOOT_METHOD=uboot-ext4
BOOT_IMAGES="boot"
UFS_RW="boot_b userdata"
PANEL_SIZE=1080x2280
PACKAGE=omamobile-device-flame
Loading