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
226 changes: 182 additions & 44 deletions boot/initramfs/root/init
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ exec </dev/console >/dev/console 2>&1

stop() {
echo "orchard initrd: $1"
# The serial-only rescue gadget is offered only for orchard.root=dm: those
# roots are the one stop path with no other way in (the whole disk map is
# checked here), and it exists so a bad map can be inspected over USB. On
# every other stop() it would raise a second gadget and a second shell on
# ttyGS0 where the normal gadget may already be up, and no board has booted
# that path yet. Widen it once a RAM boot has shown it safe.
if [ -n "$mapped_root" ]; then
if usb_gadget rescue; then
echo 'Starting a rescue shell on USB serial.'
(
while :; do
if [ -c /dev/ttyGS0 ]; then
(
echo "orchard initrd: $1"
exec setsid sh -i
) </dev/ttyGS0 >/dev/ttyGS0 2>&1
fi
sleep 1
done
) &
else
echo 'orchard initrd: USB serial rescue is unavailable.'
fi
fi
echo 'Starting a shell on the console; the system did not start.'
if [ -c /dev/ttyGS0 ]; then
echo "orchard initrd: $1" >/dev/ttyGS0 2>&1
Expand All @@ -21,14 +45,65 @@ stop() {
exec sh
}

root= mode=rw flags= fstype= init=/sbin/init
# USB for the host: a serial console (ttyGS0), and on normal boots network
# (usb0) and the installed system's name.
usb_gadget() {
g=/sys/kernel/config/usb_gadget/orchard-system
mkdir -p /sys/kernel/config && mount -t configfs -o nodev,noexec,nosuid configfs /sys/kernel/config 2>/dev/null
[ ! -e "$g" ] || return 0
if [ "${1:-}" = rescue ]; then
name='Orchard initrd rescue'
configuration='USB serial rescue'
else
name=$(sed -n 's/^NAME=//p' /newroot/etc/os-release /newroot/usr/lib/os-release 2>/dev/null | head -1 | tr -d "\"'")
configuration='USB network and serial'
fi
board=$(tr '\000' '\n' < /proc/device-tree/compatible | head -1)
# One serial for every boot of this unit (docs/device-protocol.md), or
# the per-boot one if the helper fails: USB must always come up.
serial=$(sh /etc/orchard/usb-identity 2>/dev/null) ||
serial="BOOT:$(cat /proc/sys/kernel/random/boot_id) BOARD:${board#apple,}"
# Apple's vendor ID with a product ID macOS lists among the devices it
# grants 2400 mA (docs/device-protocol.md), so a Mac charges the tablet
# at full speed. As an interface-association composite (class 0xef/2/1)
# macOS binds its generic composite driver, which delivers the grant,
# and still the CDC network and serial drivers.
mkdir "$g" &&
echo 0x05ac > "$g/idVendor" &&
if [ "${1:-}" = rescue ]; then
echo 0x4144 > "$g/idProduct"
else
echo 0x1014 > "$g/idProduct"
fi &&
echo 0xef > "$g/bDeviceClass" &&
echo 0x02 > "$g/bDeviceSubClass" &&
echo 0x01 > "$g/bDeviceProtocol" &&
mkdir -p "$g/strings/0x409" "$g/configs/c.1/strings/0x409" &&
echo 'Orchard Linux' > "$g/strings/0x409/manufacturer" &&
echo "${name:-Linux}" > "$g/strings/0x409/product" &&
echo "$serial" > "$g/strings/0x409/serialnumber" &&
echo "$configuration" > "$g/configs/c.1/strings/0x409/configuration" &&
echo 500 > "$g/configs/c.1/MaxPower" &&
mkdir "$g/functions/acm.usb0" &&
if [ "${1:-}" != rescue ]; then
mkdir "$g/functions/ncm.usb0" &&
ln -s "$g/functions/ncm.usb0" "$g/configs/c.1/ncm.usb0"
fi &&
ln -s "$g/functions/acm.usb0" "$g/configs/c.1/acm.usb0" &&
basename "$(ls -d /sys/class/udc/* | head -1)" > "$g/UDC"
}

root= mode=rw flags= fstype= init=/sbin/init mapped_root= overlay= lwvm_crc=
for argument in $(cat /proc/cmdline); do
case "$argument" in
root=PARTUUID=*) root=$(echo "${argument#root=PARTUUID=}" | tr A-F a-f) ;;
rootfstype=*) fstype=${argument#rootfstype=} ;;
rootflags=*) flags=${argument#rootflags=} ;;
ro | rw) mode=$argument ;;
init=*) init=${argument#init=} ;;
orchard.root=dm) mapped_root=1 ;;
orchard.overlay=tmpfs) overlay=1 ;;
orchard.lwvm_crc=*) lwvm_crc=${argument#orchard.lwvm_crc=} ;;
esac
done

Expand All @@ -54,6 +129,7 @@ if [ -r /run/orchard/disk ]; then
[ -b "$disk" ] && : < "$disk"
fi
[ -n "$root" ] || stop 'the boot entry names no root=PARTUUID='
[ -z "$mapped_root" ] || [ -n "$overlay" ] || stop 'orchard.root=dm requires orchard.overlay=tmpfs'

# gpt_partuuid DISK SECTOR_SIZE NUMBER: the unique GUID of GPT entry NUMBER,
# read from the disk itself: the kernel's uevent does not report PARTUUID.
Expand All @@ -78,9 +154,7 @@ lwvm_partuuid() {
echo "$1$2$3$4-$5$6-$7$8-$9${10}-${11}${12}${13}${14}${15}${16}"
}

device=
ticks=0
while [ -z "$device" ]; do
partition_root() {
for number in /sys/class/block/*/partition; do
[ -e "$number" ] || continue
part=${number%/partition}
Expand All @@ -90,59 +164,123 @@ while [ -z "$device" ]; do
id=$(gpt_partuuid "/dev/$disk" "$sector" "$(cat "$number")") ||
id=$(lwvm_partuuid "/dev/$disk" "$(cat "$number")")
if [ "$id" = "$root" ]; then
device=/dev/${part##*/}
break
echo "/dev/${part##*/}"
return
fi
done
}

# dm-mod.create registers the mapping before /init. Match its UUID to the
# LwVM PARTUUID, since the assigned dm-N number depends on other mappings.
dm_root() {
for part in /sys/class/block/dm-*; do
[ -r "$part/dm/uuid" ] || continue
[ "$(cat "$part/dm/uuid")" = "orchard-lwvm-$root" ] || continue
[ "$(cat "$part/ro")" = 1 ] || continue
[ -b "/dev/${part##*/}" ] || continue
echo "/dev/${part##*/}"
return
done
}

# Print the current LwVM header's CRC as the four stored bytes, in hex.
# Verify the checksum before considering the backup: a valid primary takes
# precedence, even if an older backup happens to match the boot entry.
lwvm_header_crc() {
local header=$1 sectors=$2 stored calculated
[ "$(wc -c < "$header")" -eq 4096 ] || return 1
[ "$(od -An -tx1 -v -N 16 "$header" | tr -d ' \n')" = 6a9088cf8afd630ae351e24887e0b98b ] || return 1
set -- $(od -An -tx1 -v -j 32 -N 8 "$header")
[ "$#" -eq 8 ] && [ "$((0x$8$7$6$5$4$3$2$1))" -eq "$((sectors * 512))" ] || return 1
stored=$(od -An -tx1 -v -j 44 -N 4 "$header" | tr -d ' \n')
# gzip's trailer starts with the same little-endian CRC32 that LwVM uses.
calculated=$(
{ head -c 44 "$header"; printf '\000\000\000\000'; tail -c 4048 "$header"; } |
gzip -c | tail -c 8 | od -An -tx1 -v -N 4 | tr -d ' \n'
)
[ "${#stored}" -eq 8 ] && [ "$stored" = "$calculated" ] || return 1
echo "$stored"
}

# Arguments: whole backing disk and disk size in 512-byte sectors from sysfs.
lwvm_disk_crc() {
local header crc chunk
[ "$2" -ge 16384 ] || return 1
# /dev is the devtmpfs mounted at the top of this script; /newroot does not exist yet.
header=$(mktemp /dev/.lwvm-header.XXXXXX) || return 1
if dd if="$1" of="$header" bs=4096 count=1 2>/dev/null && crc=$(lwvm_header_crc "$header" "$2"); then
:
else
chunk=16
while [ $((chunk * 1024)) -lt "$2" ]; do
chunk=$((chunk * 2))
done
crc=
if dd if="$1" of="$header" bs=4096 skip=$((chunk / 8 - 1)) count=1 2>/dev/null; then
crc=$(lwvm_header_crc "$header" "$2")
fi
fi
rm -f "$header"
[ -n "$crc" ] || return 1
echo "$crc"
}

check_lwvm_mapping() {
[ "${#lwvm_crc}" -eq 8 ] || stop 'the boot entry needs orchard.lwvm_crc= with eight hex digits'
case "$lwvm_crc" in
*[!0-9a-f]*) stop 'the boot entry needs orchard.lwvm_crc= with eight hex digits' ;;
esac
set -- "/sys/class/block/${device##*/}/slaves/"*
[ "$#" -eq 1 ] && [ -e "$1" ] || stop "expected one backing disk for $device"
disk=/dev/${1##*/}
sectors=$(cat "$1/size") || stop "could not read the size of $disk"
actual_crc=$(lwvm_disk_crc "$disk" "$sectors") || stop "could not read a valid LwVM header on $disk"
[ "$actual_crc" = "$lwvm_crc" ] ||
stop "LwVM header CRC mismatch on $disk (expected $lwvm_crc, found $actual_crc); regenerate the boot options"
}

description="partition $root"
[ -z "$mapped_root" ] || description="read-only device-mapper root $root"
ticks=0
while :; do
if [ -n "$mapped_root" ]; then
device=$(dm_root)
else
device=$(partition_root)
fi
[ -n "$device" ] && break
if sleep 0.05 2>/dev/null; then
ticks=$((ticks + 1))
[ "$ticks" -le 600 ] || stop "no partition $root after 30 s"
else
ticks=$((ticks + 20))
[ "$ticks" -le 600 ] || stop "no partition $root after 30 s"
sleep 1
fi
[ "$ticks" -lt 600 ] || stop "no $description after 30 s"
done

mkdir -p /newroot
mount ${fstype:+-t "$fstype"} -o "$mode${flags:+,$flags}" "$device" /newroot || stop "could not mount $device"
[ -x "/newroot$init" ] || [ -L "/newroot$init" ] || stop "the root has no $init"
[ -z "$mapped_root" ] || check_lwvm_mapping

# USB for the host: network (usb0) and a serial console (ttyGS0), named after
# the system, so `orchard devices` sees it. What runs on them is the system's.
usb_gadget() {
g=/sys/kernel/config/usb_gadget/orchard-system
mkdir -p /sys/kernel/config && mount -t configfs -o nodev,noexec,nosuid configfs /sys/kernel/config 2>/dev/null
[ ! -e "$g" ] || return 0
name=$(sed -n 's/^NAME=//p' /newroot/etc/os-release /newroot/usr/lib/os-release 2>/dev/null | head -1 | tr -d "\"'")
board=$(tr '\000' '\n' < /proc/device-tree/compatible | head -1)
# One serial for every boot of this unit (docs/device-protocol.md), or
# the per-boot one if the helper fails: USB must always come up.
serial=$(sh /etc/orchard/usb-identity 2>/dev/null) ||
serial="BOOT:$(cat /proc/sys/kernel/random/boot_id) BOARD:${board#apple,}"
# Apple's vendor ID with a product ID macOS lists among the devices it
# grants 2400 mA (docs/device-protocol.md), so a Mac charges the tablet
# at full speed. As an interface-association composite (class 0xef/2/1)
# macOS binds its generic composite driver, which delivers the grant,
# and still the CDC network and serial drivers.
mkdir "$g" &&
echo 0x05ac > "$g/idVendor" &&
echo 0x1014 > "$g/idProduct" &&
echo 0xef > "$g/bDeviceClass" &&
echo 0x02 > "$g/bDeviceSubClass" &&
echo 0x01 > "$g/bDeviceProtocol" &&
mkdir -p "$g/strings/0x409" "$g/configs/c.1/strings/0x409" &&
echo 'Orchard Linux' > "$g/strings/0x409/manufacturer" &&
echo "${name:-Linux}" > "$g/strings/0x409/product" &&
echo "$serial" > "$g/strings/0x409/serialnumber" &&
echo 'USB network and serial' > "$g/configs/c.1/strings/0x409/configuration" &&
echo 500 > "$g/configs/c.1/MaxPower" &&
mkdir "$g/functions/ncm.usb0" "$g/functions/acm.usb0" &&
ln -s "$g/functions/ncm.usb0" "$g/configs/c.1/ncm.usb0" &&
ln -s "$g/functions/acm.usb0" "$g/configs/c.1/acm.usb0" &&
basename "$(ls -d /sys/class/udc/* | head -1)" > "$g/UDC"
mount_root() {
mkdir -p /newroot || stop 'could not create the root mount point'
if [ -z "$overlay" ]; then
mount ${fstype:+-t "$fstype"} -o "$mode${flags:+,$flags}" "$device" /newroot ||
stop "could not mount $device"
return
fi

# Keep the filesystem and the overlay's backing directories on separate
# mounts. OverlayFS holds references to both after switch_root.
mkdir -p /root-lower /root-overlay || stop 'could not create the overlay mount points'
mount ${fstype:+-t "$fstype"} -o "${flags:+$flags,}ro" "$device" /root-lower ||
stop "could not mount $device read-only"
mount -t tmpfs -o mode=0755,size=40% tmpfs /root-overlay || stop 'could not mount the overlay tmpfs'
mkdir /root-overlay/upper /root-overlay/work || stop 'could not create the overlay directories'
mount -t overlay -o lowerdir=/root-lower,upperdir=/root-overlay/upper,workdir=/root-overlay/work overlay /newroot ||
stop 'could not mount the root overlay'
}
mount_root
[ -x "/newroot$init" ] || [ -L "/newroot$init" ] || stop "the root has no $init"

usb_gadget || echo 'orchard initrd: no USB gadget; continuing without USB'

for name in dev proc sys; do
Expand Down
51 changes: 51 additions & 0 deletions docs/boot-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ modules only. It loads them, waits up to 30 s for the partition that
given) and starts its `/sbin/init` (or `init=`). If any step fails, it says
which one and opens a shell on the console.

`orchard.overlay=tmpfs` makes the initrd mount the root filesystem read-only
and start the system on an OverlayFS mount backed by tmpfs (limited to 40%
of RAM). All changes to that overlay disappear at reboot. Cleanly unmount the
lower filesystem before boot: journal replay cannot write to a read-only
backing device, and skipping replay could expose an inconsistent filesystem.
The installed system must not try to fsck or remount the lower filesystem
through an `/` entry in `/etc/fstab`. The overlay
requires kernel tmpfs and OverlayFS support. Without the option the initrd
mounts the filesystem directly as usual.

## Partitions (GPT)

| Partition | Type GUID | Filesystem | Count |
Expand All @@ -130,6 +140,47 @@ take over. Meanwhile the host keeps the Cascadia bundle that starts the
installed system (its tree's `bootargs` carry `root=PARTUUID=`), which has to be
placed where iOS's boot chain finds it.

### Booting an existing scattered Linux partition

The kernel's LwVM reader only registers contiguous partitions. To boot a
scattered Linux partition, generate the entry's options on the host from a
current 4096-byte LwVM header (primary or copy) and the partition's GUID:

```sh
bin/orchard python -m orchard.lwvm_root lwvm-header.bin LINUX-PARTUUID
```

The generator also accepts a disk image or block device, in which case it
reads the primary header and checks the recorded medium size against its
backing size. The options include `root=PARTUUID=…`, `orchard.root=dm`,
`orchard.overlay=tmpfs`, `orchard.lwvm_crc=` (the four stored CRC bytes as
lowercase hex), and a quoted `dm-mod.create=` with a read-only linear map.
The backing disk defaults to `/dev/applesftl0`; pass `--disk` if
the kernel exposes it under another name. Use the emitted line as the root
options for the entry or pass it as `bootargs` to Cascadia assembly, keeping
the literal double quotes around the map. Include any board-specific
console arguments as usual. The complete armv7 command line must fit in
1023 bytes; maps that would exceed that limit by themselves are rejected.

This path requires an armv7 kernel with `CONFIG_MD=y`, `CONFIG_BLK_DEV_DM=y`
and `CONFIG_DM_INIT=y` built in; the armv7 kernel configuration
(`kernel/configs/apple-armv7/config`) enables all three. The backing disk
must be available at device-mapper initialization.

The shared initrd waits up to 30 s for the read-only mapper device whose
UUID matches the root PARTUUID. Before mounting, it verifies the LwVM
header checksum and medium size against the mapping's backing disk and
compares the checksum with `orchard.lwvm_crc=`. It uses the backup at the
end of the first chunk only if the primary is unreadable or invalid. If the
mapper device never appears, the header cannot be read, or the checksum
differs, the initrd stops and offers a serial-only USB (ACM) rescue shell
in addition to the local console when the USB controller is available.

Recompute the options and regenerate the payload whenever the LwVM map
changes. This path boots an already prepared filesystem. The shared initrd's
device-mapper path has not yet booted on a device; one RAM boot is pending. The automatic
installer does not generate these options or populate scattered partitions.

## Entries

The layout follows BLS:
Expand Down
6 changes: 6 additions & 0 deletions docs/device-protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ serial. No descriptor changes per boot, so macOS can retain accessory approval.
Trees without a serial number warn and retain the legacy `BOOT:` identity to
keep rescue USB available; those still need approval each boot.

The installed initrd's [scattered LwVM root rescue](boot-contract.md#booting-an-existing-scattered-linux-partition)
uses `0x05ac:0x4144` and the same stable `BOARD:<profile> ID:<hash>` serial
(or the same `BOOT:` fallback). Hosts may list it as a RAM payload because
it shares the RAM product ID. It offers only an ACM console, without USB
networking or the RAM session's SSH bootstrap.

For RAM sessions, ACM returns `/proc/sys/kernel/random/boot_id` and the SSH
host public key together on every connection. The host validates the boot ID
and pins that boot's key. Installed systems use their persistent SSH keys and
Expand Down
Loading
Loading