Files
makronix/mobile/base/initramfs/init
T
lotherkandClaude Opus 5.5 d29094e24f mobile: our initramfs replaces pmOS's in the boot image
- base/initramfs/init builds the gadget with NCM and ACM at once: the root's
  stage 1 no longer rebinds it, so the host keeps its link and address and
  netconsole runs from 1.5 s.
- The root grows itself online to its partition
  (base/void/rootfs/.../03-grow-root.sh), which the pmOS initramfs did;
  tools/mkrootimg.sh copies base/<base>/rootfs/ before the SoC's files.
- tools/build-busybox.sh: SOURCE_DATE_EPOCH of the pinned source, so the
  initramfs is reproducible; pinned in device.conf (4950e286679ca4fa).
- Makefile: INITRAMFS defaults to ours; bootimg checks the pins after the
  inputs are built.

Measured on hotdogb: switch_root at 1.27 s (pmOS: about 3.5 s), a fresh
root grown from 281468 to 25493504 blocks, 10 of 10 warm reboots, no
WARNING, make flash end to end.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 13:45:42 +00:00

113 lines
3.7 KiB
Plaintext
Executable File

#!/bin/busybox sh
#
# /init of the Makronix Mobile initramfs: bring up the USB network, find the
# root file system by UUID, hand over to its /sbin/init. Run by the static
# busybox of the base (tools/mkinitramfs.sh); POSIX sh, busybox applets only.
#
# Kernel arguments read here:
# root=UUID=<uuid> the root file system (tools/mkbootimg.sh, ROOT_UUID)
# makronix.debug stop before switch_root and serve a shell
#
# The kernel has everything built in that this needs (UFS, sd, DWC3, NCM,
# configfs, ext4): no modules are loaded here. On any failure the init does
# not halt: it serves a root shell with telnetd on USB_IP and waits.
/bin/busybox --install -s /bin
export PATH=/bin
USB_IP=172.16.42.1
HOST_IP=172.16.42.2
CONFIGFS=/sys/kernel/config/usb_gadget
log() {
echo "makronix-rd: $*" > /dev/kmsg
}
rescue() {
log "$* -- shell: telnet $USB_IP"
mkdir -p /dev/pts
mountpoint -q /dev/pts || mount -t devpts devpts /dev/pts
telnetd -l /bin/sh -b "$USB_IP"
while :; do sleep 3600; done
}
mkdir -p /proc /sys /dev /run /sysroot /etc
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev
mount -t tmpfs -o mode=0755,nosuid,nodev tmpfs /run
mount -t configfs configfs /sys/kernel/config
root= debug=
for a in $(cat /proc/cmdline); do
case $a in
root=UUID=*) root=${a#root=UUID=} ;;
makronix.debug) debug=1 ;;
esac
done
log "start, root UUID=$root"
# USB: one gadget (g1) with the network (NCM, as the pmOS initramfs set it
# up, the same addresses) and a serial port (ACM: /dev/ttyACM0 on the host,
# ttyGS0 here, console=ttyGS0 and a getty in the root). Both from the start,
# so the root does not have to rebind the gadget to add the serial port
# (its 01-acm-console.sh then has nothing to do), and the host keeps its
# address and the netconsole its link.
usb_up() {
i=0
while [ -z "$(ls /sys/class/udc 2>/dev/null)" ]; do
i=$((i + 1)); [ $i -le 100 ] || { log "no UDC"; return 1; }
sleep 0.1
done
g=$CONFIGFS/g1
mkdir -p "$g/strings/0x409" "$g/functions/ncm.usb0" "$g/functions/acm.GS0" \
"$g/configs/c.1/strings/0x409"
echo 0x18D1 > "$g/idVendor"
echo 0xD001 > "$g/idProduct"
echo Makronix > "$g/strings/0x409/manufacturer"
echo Makronix > "$g/strings/0x409/serialnumber"
echo "Makronix Mobile" > "$g/strings/0x409/product"
echo "USB network and serial" > "$g/configs/c.1/strings/0x409/configuration"
ln -s "$g/functions/ncm.usb0" "$g/configs/c.1/"
ln -s "$g/functions/acm.GS0" "$g/configs/c.1/"
ls /sys/class/udc | head -n 1 > "$g/UDC"
ifname=$(cat "$g/functions/ncm.usb0/ifname")
ifconfig "$ifname" "$USB_IP" netmask 255.255.255.0 up
# one lease, ten days: the host keeps its address after the handover,
# when the root's NetworkManager holds USB_IP statically
cat > /etc/udhcpd.conf <<EOF
start $HOST_IP
end $HOST_IP
max_leases 1
interface $ifname
lease_file /run/udhcpd.leases
option subnet 255.255.255.0
option lease 864000
EOF
udhcpd /etc/udhcpd.conf
log "usb network up: $ifname $USB_IP"
}
usb_up || rescue "usb network failed"
[ -n "$root" ] || rescue "no root=UUID= on the command line"
i=0
while :; do
dev=$(findfs "UUID=$root" 2>/dev/null) && break
i=$((i + 1)); [ $i -le 300 ] || rescue "root UUID=$root not found"
sleep 0.1
done
log "root is $dev"
# read-only: the root's init checks the file system and remounts it
mount -o ro "$dev" /sysroot || rescue "cannot mount $dev"
# -L too: Debian's /sbin/init is an absolute link, resolved only after switch_root
[ -x /sysroot/sbin/init ] || [ -L /sysroot/sbin/init ] || rescue "no /sbin/init on $dev"
[ -z "$debug" ] || rescue "makronix.debug: stopped before switch_root"
log "switch_root to /sbin/init"
killall udhcpd 2>/dev/null
for m in /dev /proc /sys /run; do
mount --move "$m" "/sysroot$m" || rescue "cannot move $m"
done
exec switch_root /sysroot /sbin/init