mirror of
https://github.com/dikeckaan/mu300-linux.git
synced 2026-12-06 00:03:00 +00:00
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
536 lines
27 KiB
Plaintext
536 lines
27 KiB
Plaintext
#!/bin/busybox sh
|
|
# MU300 standalone Linux initramfs v10 (switch_root to Ubuntu on the free eMMC region; comm fix; custom kernel + modem_control + node perms + slot a modem) (slot b, tries=2 one-shot trial).
|
|
# Every stage is written to /dev/kmsg at KERN_ERR so it survives in
|
|
# /sys/fs/pstore/console-ramoops-0 after the fallback boot into Android.
|
|
/bin/busybox mkdir -p /bin /sbin /proc /sys /dev /run /tmp /root /etc /config
|
|
/bin/busybox --install -s /bin
|
|
export PATH=/bin:/sbin HOME=/root TERM=vt100
|
|
mount -t proc proc /proc
|
|
mount -t sysfs sysfs /sys
|
|
echo 7 > /proc/sys/kernel/printk
|
|
# the custom kernel has devtmpfs; fall back to tmpfs + mknod for safety
|
|
if ! mount -t devtmpfs -o mode=0755 devtmpfs /dev; then
|
|
mount -t tmpfs -o mode=0755 tmpfs /dev
|
|
mknod /dev/null c 1 3
|
|
mknod /dev/console c 5 1
|
|
mknod /dev/kmsg c 1 11
|
|
mknod /dev/zero c 1 5
|
|
mknod /dev/urandom c 1 9
|
|
fi
|
|
pmsg_major=$(awk '$2=="pmsg"{print $1}' /proc/devices)
|
|
[ -n "$pmsg_major" ] && [ ! -e /dev/pmsg0 ] && mknod /dev/pmsg0 c "$pmsg_major" 0
|
|
|
|
log() {
|
|
echo "<3>MU300-LINUX: $*" > /dev/kmsg
|
|
[ -c /dev/pmsg0 ] && echo "MU300-LINUX: $*" > /dev/pmsg0
|
|
[ -d /run ] && echo "$(cut -d' ' -f1 /proc/uptime) $*" >> /run/stages
|
|
}
|
|
|
|
# Persistent log: 8 MiB at 48 MiB inside boot_b, past the image and its AVB data
|
|
# (image ends near 39 MiB). Survives a cold power cycle, unlike pstore.
|
|
bootb=
|
|
persist() {
|
|
# subshells (the reboot timer) started before the eMMC was found learn the device from this file
|
|
[ -n "$bootb" ] || bootb=$(cat /run/bootb 2>/dev/null)
|
|
[ -n "$bootb" ] && [ -b "$bootb" ] || return
|
|
# the periodic loop and the reboot timer both call this
|
|
mkdir /run/persist.lock 2>/dev/null || return
|
|
{
|
|
echo "MU300-PERSIST-BEGIN uptime=$(cut -d' ' -f1 /proc/uptime)"
|
|
cat /run/stages
|
|
echo "--- lsmod"
|
|
cat /proc/modules
|
|
echo "--- udc"
|
|
ls /sys/class/udc 2>&1
|
|
echo "--- logcat"
|
|
tail -c 400000 /run/logcat.txt 2>&1
|
|
echo "--- modem_control"
|
|
cat /run/modem_control.log 2>&1 | tail -c 200000
|
|
echo "--- deferred"
|
|
cat /sys/kernel/debug/devices_deferred 2>&1
|
|
echo "--- dmesg"
|
|
dmesg | tail -c 6000000
|
|
echo "MU300-PERSIST-END"
|
|
} > /run/persist.txt 2>&1
|
|
head -c 65536 /dev/zero >> /run/persist.txt
|
|
dd if=/run/persist.txt of="$bootb" bs=4096 seek=12288 count=2048 conv=notrunc 2>/dev/null
|
|
sync
|
|
rmdir /run/persist.lock
|
|
}
|
|
log "stage=init-start pid=$$"
|
|
|
|
mkdir -p /dev/pts
|
|
mount -t devpts -o mode=0620,ptmxmode=0666 devpts /dev/pts
|
|
ln -sf pts/ptmx /dev/ptmx
|
|
mount -t tmpfs tmpfs /run
|
|
mount -t tmpfs tmpfs /tmp
|
|
mount -t debugfs debugfs /sys/kernel/debug
|
|
echo /bin/mdev > /proc/sys/kernel/hotplug
|
|
mdev -s
|
|
log "stage=devfs-ready"
|
|
|
|
# Turn hangs into panics; the vendor kernel reboots after a panic.
|
|
echo 1 > /proc/sys/kernel/softlockup_panic
|
|
echo 1 > /proc/sys/kernel/panic_on_oops
|
|
[ -e /proc/sys/kernel/hardlockup_panic ] && echo 1 > /proc/sys/kernel/hardlockup_panic
|
|
echo 1 > /proc/sys/kernel/sysrq
|
|
|
|
# Safety net: reboot after 5 minutes. By then misc points back at slot a.
|
|
# Create /run/stay (e.g. over telnet) to keep Linux running.
|
|
(sleep 300
|
|
if [ -e /run/stay ]; then log "stage=timer-skipped"; exit 0; fi
|
|
log "stage=timer-reboot"
|
|
while ! persist_done=$(mkdir /run/persist.lock 2>/dev/null && echo 1); do sleep 1; done
|
|
rmdir /run/persist.lock
|
|
persist; reboot -f; sleep 10; echo b > /proc/sysrq-trigger) &
|
|
|
|
# Which device this is. The ZTE U30 Air is the F50's board with a battery: ZTE's SQC charger stack, USB-PD, LEDs.
|
|
# It runs the same kernel with its own build of a few modules (linux-modules/u30air/, see kernel/build-u30air.sh)
|
|
# and its own load order; without them the charger manager never probes and USB stays down. The installer names
|
|
# the device in /etc/mu300-device (the device segment of the ramdisk, which the generic segment of an update does
|
|
# not replace); without that file the stock device tree tells: only the U30 Air's has the charger policy service.
|
|
if [ -s /etc/mu300-device ]; then
|
|
DEVICE=$(cat /etc/mu300-device)
|
|
elif [ -d /proc/device-tree/charger_policy_service ]; then
|
|
DEVICE=u30air
|
|
else
|
|
DEVICE=f50
|
|
fi
|
|
# its modules only for the kernel they were built for (a mainline kernel brings its own set and order)
|
|
MODULE_ORDER=/etc/module-order
|
|
if [ "$DEVICE" != f50 ] && [ -s /etc/module-order-$DEVICE ] &&
|
|
[ "$(cat /linux-modules/$DEVICE/kernel.release 2>/dev/null)" = "$(uname -r)" ]; then
|
|
MODULE_ORDER=/etc/module-order-$DEVICE
|
|
else
|
|
DEVICE_MODULES=none
|
|
fi
|
|
log "stage=device $DEVICE order=$MODULE_ORDER"
|
|
|
|
# The USB network. Its subnet goes with the kind of device, so that an F50 and a U30 Air on one computer do not
|
|
# collide: F50 192.168.77.1, U30 Air 192.168.78.1 (the system's lan.conf overrides it after switch_root). The
|
|
# gadget's MAC addresses and USB serial number go with the device's serial number: two devices on one computer
|
|
# used to have the same ones, and macOS then gave the second one no network interface at all.
|
|
case $DEVICE in u30air) NET=192.168.78 ;; *) NET=192.168.77 ;; esac
|
|
# (a mainline kernel forces its own command line: the bootloader's is still in the device tree)
|
|
SERIALNO=$(cat /proc/cmdline /proc/device-tree/chosen/bootargs 2>/dev/null | tr '\0' ' ' |
|
|
sed -n 's/.*androidboot.serialno=\([^ ]*\).*/\1/p' | head -n1)
|
|
h=$(echo "$SERIALNO" | md5sum)
|
|
MAC=02:50:$(echo "$h" | cut -c1-2):$(echo "$h" | cut -c3-4):${NET##*.}
|
|
|
|
load_vendor_modules() {
|
|
loaded=0
|
|
failed=0
|
|
while read module; do
|
|
# vendor_boot's /lib/modules were built for the stock kernel; only our rebuilt copies match
|
|
file="/linux-modules/$module"
|
|
[ -z "${DEVICE_MODULES:-}" ] && [ -f "/linux-modules/$DEVICE/$module" ] && file="/linux-modules/$DEVICE/$module"
|
|
if insmod "$file" 2>/run/insmod.err; then
|
|
loaded=$((loaded + 1))
|
|
else
|
|
failed=$((failed + 1))
|
|
log "insmod-fail $module $(cat /run/insmod.err)"
|
|
fi
|
|
done < $MODULE_ORDER
|
|
mdev -s
|
|
log "stage=modules-done loaded=$loaded failed=$failed"
|
|
}
|
|
|
|
# misc and boot_b: where they are is known only once the eMMC is there, and its driver (sdhci-sprd) is one of
|
|
# the vendor modules - an earlier lookup found nothing (U30 Air: misc-NOT-FOUND, no persistent log). So this runs
|
|
# after load_vendor_modules; whether slot a is restored is decided once the rootfs is known.
|
|
misc=
|
|
find_partitions() {
|
|
for n in $(seq 1 20); do
|
|
for u in /sys/class/block/mmcblk*p*/uevent; do
|
|
grep -qx PARTNAME=misc "$u" 2>/dev/null && misc=/dev/$(basename "$(dirname "$u")")
|
|
grep -qx PARTNAME=boot_b "$u" 2>/dev/null && bootb=/dev/$(basename "$(dirname "$u")")
|
|
done
|
|
[ -b "$misc" ] && [ -b "$bootb" ] && break
|
|
sleep 1
|
|
mdev -s
|
|
done
|
|
[ -b "$misc" ] || misc=
|
|
[ -b "$bootb" ] || bootb=
|
|
echo "$bootb" > /run/bootb
|
|
log "stage=persist-target dev=${bootb:-none} misc=${misc:-none}"
|
|
persist
|
|
(while :; do sleep 15; persist; done) &
|
|
}
|
|
|
|
write_misc_bc() { # write_misc_bc FILE STAGE
|
|
[ -n "$misc" ] || { log "stage=misc-NOT-FOUND"; return 1; }
|
|
dd if="$1" of="$misc" bs=1 seek=2048 conv=notrunc 2>/dev/null
|
|
sync
|
|
dd if="$misc" of=/run/bc.bin bs=1 skip=2048 count=32 2>/dev/null
|
|
if cmp -s /run/bc.bin "$1"; then log "stage=$2 dev=$misc"; else log "stage=$2-MISMATCH dev=$misc"; fi
|
|
}
|
|
restore_slot_a() { write_misc_bc /etc/misc-bc-slot-a.bin misc-restored; }
|
|
|
|
hostname mu300-linux
|
|
ifconfig lo 127.0.0.1 up
|
|
setup_usb_gadget() {
|
|
log "stage=usb-configfs"
|
|
mount -t configfs configfs /config
|
|
g=/config/usb_gadget/linux
|
|
mkdir -p "$g/strings/0x409" "$g/configs/c.1/strings/0x409"
|
|
# The USB network functions: whichever of these the kernel actually has. Creating one it was not built with
|
|
# fails here, and a gadget that is never bound leaves the device with no console and no network at all - which
|
|
# is exactly what an "improvement" to NCM cost once (on a mainline build without it; both kernels have it now).
|
|
# NCM by default, in a single configuration with the ACM console: Windows 10 2004+/11 bind their inbox
|
|
# UsbNcm.sys to it, macOS and Linux their own drivers - all measured, none needs an INF. ECM, which Windows has
|
|
# no driver for, is the fallback. MU300_USBNET (kernel command line) picks the functions.
|
|
# "rndis ncm" is for Windows without NCM (7, 8, 10 before 2004): RNDIS gets configuration 1 and NCM moves to
|
|
# configuration 2. Windows only ever uses the first, and binds rndis.sys to it through the MS OS descriptors
|
|
# below; Linux skips RNDIS configurations on purpose. macOS does not: it takes configuration 1 as well, finds
|
|
# no driver for RNDIS and has no network (measured) - which is why RNDIS is not the default.
|
|
rndis= netfunc=
|
|
for f in ${MU300_USBNET:-ncm ecm}; do
|
|
case $f in
|
|
rndis) [ -z "$rndis" ] && mkdir -p "$g/functions/rndis.rn0" 2>/dev/null && rndis=1 ;;
|
|
*) [ -z "$netfunc" ] && mkdir -p "$g/functions/$f.usb0" 2>/dev/null && netfunc=$f ;;
|
|
esac
|
|
done
|
|
[ -n "$netfunc$rndis" ] || log "stage=usb-no-net-function (console only)"
|
|
cdc=c.1
|
|
[ -n "$rndis" ] && cdc=c.2
|
|
echo 0x0525 > "$g/idVendor"
|
|
echo 0xa4a1 > "$g/idProduct"
|
|
echo 0x0200 > "$g/bcdUSB"
|
|
# Windows asks for the MS OS descriptors (below) once per VID/PID/bcdDevice and caches the answer, "none"
|
|
# included: the default bcdDevice follows the kernel version and was cached as "none" by every PC that met
|
|
# the earlier gadget. A value of our own makes them ask again. Change it whenever the descriptors change.
|
|
echo 0x0301 > "$g/bcdDevice"
|
|
# Miscellaneous / IAD: the network and the ACM functions each span two interfaces, and without this device class
|
|
# Windows' composite driver ignores their association descriptors and hands out the interfaces one by one, so a
|
|
# class driver never sees its function as a whole
|
|
echo 0xEF > "$g/bDeviceClass"
|
|
echo 0x02 > "$g/bDeviceSubClass"
|
|
echo 0x01 > "$g/bDeviceProtocol"
|
|
echo "MU300LINUX${SERIALNO:+-$SERIALNO}" > "$g/strings/0x409/serialnumber"
|
|
echo Linux > "$g/strings/0x409/manufacturer"
|
|
echo "MU300 Linux USB Ethernet" > "$g/strings/0x409/product"
|
|
# USB serial console alongside the network function, in every configuration: recovery/debug access that does
|
|
# not depend on networking
|
|
mkdir -p "$g/functions/acm.GS0"
|
|
if [ -n "$rndis" ]; then
|
|
echo "RNDIS + ACM console" > "$g/configs/c.1/strings/0x409/configuration"
|
|
echo 250 > "$g/configs/c.1/MaxPower"
|
|
echo $MAC:03 > "$g/functions/rndis.rn0/dev_addr"
|
|
echo $MAC:04 > "$g/functions/rndis.rn0/host_addr"
|
|
ln -s "$g/functions/rndis.rn0" "$g/configs/c.1/f1"
|
|
ln -s "$g/functions/acm.GS0" "$g/configs/c.1/f2"
|
|
# MS OS descriptors: Windows asks for them with vendor request 0xcd and binds rndis.sys by compatible ID.
|
|
# They are what identifies the function: the stock f_rndis' control interface is 02/02/ff, which Windows
|
|
# would otherwise hand to usbser (a device with two configurations is never composite on Windows, so the
|
|
# first interface names the whole device).
|
|
echo 1 > "$g/os_desc/use"
|
|
echo 0xcd > "$g/os_desc/b_vendor_code"
|
|
echo MSFT100 > "$g/os_desc/qw_sign"
|
|
echo RNDIS > "$g/functions/rndis.rn0/os_desc/interface.rndis/compatible_id"
|
|
echo 5162001 > "$g/functions/rndis.rn0/os_desc/interface.rndis/sub_compatible_id"
|
|
ln -s "$g/configs/c.1" "$g/os_desc/c.1"
|
|
fi
|
|
if [ -n "$netfunc" ] || [ -z "$rndis" ]; then
|
|
mkdir -p "$g/configs/$cdc/strings/0x409"
|
|
echo "${netfunc:-no} net + ACM console" > "$g/configs/$cdc/strings/0x409/configuration"
|
|
echo 250 > "$g/configs/$cdc/MaxPower"
|
|
if [ -n "$netfunc" ]; then
|
|
echo $MAC:01 > "$g/functions/$netfunc.usb0/dev_addr"
|
|
echo $MAC:02 > "$g/functions/$netfunc.usb0/host_addr"
|
|
ln -s "$g/functions/$netfunc.usb0" "$g/configs/$cdc/f1"
|
|
fi
|
|
ln -s "$g/functions/acm.GS0" "$g/configs/$cdc/f2"
|
|
fi
|
|
log "stage=usb-net-function ${rndis:+rndis+}${netfunc:-none}"
|
|
log "stage=usb-wait-udc"
|
|
for n in $(seq 1 40); do
|
|
[ -e /sys/class/udc/25100000.dwc3 ] && break
|
|
sleep 1
|
|
done
|
|
udc=
|
|
[ -e /sys/class/udc/25100000.dwc3 ] && udc=25100000.dwc3
|
|
[ -z "$udc" ] && for u in /sys/class/udc/*; do [ -e "$u" ] && { udc=$(basename "$u"); break; }; done
|
|
log "stage=udc udc=${udc:-none} available=$(ls /sys/class/udc 2>/dev/null | tr '\n' ',')"
|
|
persist
|
|
sleep 1
|
|
# Never rebind the gadget from a shell running on its own ACM console: the console dies with the unbind and the
|
|
# rebind never runs, which leaves the device with no USB at all until it is power-cycled.
|
|
log "stage=usb-bind-start"
|
|
[ -n "$udc" ] && echo "$udc" > "$g/UDC"
|
|
log "stage=usb-bind-done rc=$?"
|
|
persist
|
|
|
|
# The functions register their netdevs as they are bound, configuration by configuration, so RNDIS would take
|
|
# usb0 - the name lan-start and the OpenWrt bridge expect for the CDC link. Name them by function instead.
|
|
if [ -n "$rndis" ]; then
|
|
r=$(cat "$g/functions/rndis.rn0/ifname" 2>/dev/null)
|
|
c=$(cat "$g/functions/$netfunc.usb0/ifname" 2>/dev/null)
|
|
case $r in ""|"(unnamed net_device)"|rndis0) ;; *) ip link set "$r" name rndis0 ;; esac
|
|
case $c in ""|"(unnamed net_device)"|usb0) ;; *) ip link set "$c" name usb0 ;; esac
|
|
log "stage=usb-ifnames rndis=$r->rndis0 cdc=${c:-none}->usb0"
|
|
ifconfig rndis0 up
|
|
fi
|
|
|
|
# Bring usb0 up immediately after binding, before the host selects the data interface:
|
|
# f_ecm (and f_ncm alike) reports the link state in its first CONNECT notification, and macOS' ECM driver does not
|
|
# pick up a later "connected" notification (v10 left usb0 down until systemd ran -> host "inactive").
|
|
ifconfig usb0 up
|
|
}
|
|
|
|
# The vendor modules first, the gadget last. The host starts asking for a DHCP lease the moment the gadget
|
|
# appears and gives up after a few seconds, so the closer the gadget comes to the system that answers (the DHCP
|
|
# server starts right after switch_root), the more reliably the host gets an address.
|
|
load_vendor_modules
|
|
find_partitions
|
|
setup_usb_gadget
|
|
# The SD card as the home of the Linux filesystem: ext4 with the label mu300sd, on the card's first partition or
|
|
# on the whole card. Its own label, not mu300root - a device can hold both, and they must never be confused.
|
|
# --- sd-root begin
|
|
ext4_label() { # ext4_label DEV: the volume label, nothing (and status 1) when DEV is not ext4
|
|
[ "$(dd if="$1" bs=1 skip=1080 count=2 2>/dev/null | od -An -tx1 | tr -d ' \n')" = 53ef ] || return 1
|
|
dd if="$1" bs=1 skip=1144 count=16 2>/dev/null | tr -d '\000'; echo
|
|
}
|
|
find_sd_root() { # the first card device that holds mu300sd; the eMMC is mmcblk0 and never looked at
|
|
for d in ${MU300_SD_GLOB:-/dev/mmcblk[1-9]p1 /dev/mmcblk[1-9]}; do
|
|
[ -e "$d" ] || continue
|
|
[ "$(ext4_label "$d")" = mu300sd ] && { echo "$d"; return 0; }
|
|
done
|
|
return 1
|
|
}
|
|
wait_sd_root() { # wait_sd_root SECONDS: a card that the polled host has not found yet
|
|
n=0
|
|
while [ "$n" -lt "$1" ]; do
|
|
find_sd_root && return 0
|
|
sleep 1; mdev -s; n=$((n + 1))
|
|
done
|
|
return 1
|
|
}
|
|
# --- sd-root end
|
|
ROOT_OFFSET=27762098176 # linux-boot/rootfs-region.env; outside every GPT partition
|
|
# The region is not at the same place on every device (eMMC size, partition layout), and a number in this file is
|
|
# not enough: the installer writes the device's offset in here, but the generic ramdisk segment that updates put
|
|
# behind this one (mu300-update, and a mainline kernel from the installer) carries its own init, which replaces
|
|
# this file - with the default above. Devices whose region is elsewhere then booted into standalone mode and went
|
|
# back to Android. So find it the way the installer places it - the first 2 MiB boundary after the last partition
|
|
# - and take the first candidate that really holds the mu300root filesystem (ext4 magic and label).
|
|
is_mu300root() { # is_mu300root OFFSET
|
|
[ "$(dd if=/dev/mmcblk0 bs=1 skip=$(($1 + 1080)) count=2 2>/dev/null | od -An -tx1 | tr -d ' \n')" = 53ef ] &&
|
|
[ "$(dd if=/dev/mmcblk0 bs=1 skip=$(($1 + 1144)) count=9 2>/dev/null)" = mu300root ]
|
|
}
|
|
find_root_offset() {
|
|
e=0
|
|
for p in /sys/block/mmcblk0/mmcblk0p*; do
|
|
[ -r "$p/start" ] || continue
|
|
x=$(( $(cat "$p/start") + $(cat "$p/size") ))
|
|
[ "$x" -gt "$e" ] && e=$x
|
|
done
|
|
for c in $(( (e / 4096 + 1) * 4096 * 512 )) $ROOT_OFFSET; do
|
|
is_mu300root "$c" && { echo "$c"; return 0; }
|
|
done
|
|
return 1
|
|
}
|
|
# The ext4 area holds one or more systems: /ubuntu, /openwrt (selected by /.mu300/boot-os), or a single
|
|
# Ubuntu directly in its root (first installs). Pick one and remember how to start it.
|
|
has_init() { [ -x "$1/lib/systemd/systemd" ] || [ -x "$1/sbin/init" ]; }
|
|
pick_root() {
|
|
want=$(cat /disk/.mu300/boot-os 2>/dev/null)
|
|
for os in $want ubuntu openwrt; do
|
|
[ -d "/disk/$os" ] && has_init "/disk/$os" && { echo "/disk/$os"; return; }
|
|
# legacy layout: Ubuntu in the filesystem root
|
|
[ "$os" = ubuntu ] && [ -x /disk/lib/systemd/systemd ] && { echo /disk; return; }
|
|
done
|
|
}
|
|
mkdir -p /disk /newroot
|
|
# --- root-select begin
|
|
mount_internal() { # the mu300root region on the eMMC; busybox mount sets up a loop only for regular files, so
|
|
# attach it explicitly and verify its offset (the loop options would be handed to ext4: EINVAL)
|
|
if found=$(find_root_offset); then
|
|
[ "$found" = "$ROOT_OFFSET" ] || log "stage=root-offset found=$found (default $ROOT_OFFSET)"
|
|
ROOT_OFFSET=$found
|
|
else
|
|
log "stage=root-offset not-found (trying the default $ROOT_OFFSET)"
|
|
fi
|
|
rootloop=$(losetup -f 2>/run/rootmount.err)
|
|
[ -n "$rootloop" ] && losetup -o $ROOT_OFFSET "$rootloop" /dev/mmcblk0 2>>/run/rootmount.err \
|
|
&& [ "$(cat /sys/block/${rootloop#/dev/}/loop/offset)" = "$ROOT_OFFSET" ] \
|
|
&& [ "$(dd if="$rootloop" bs=1 skip=1080 count=2 2>/dev/null | od -An -tx1 | tr -d ' ')" = 53ef ] \
|
|
&& mount -t ext4 -o noatime "$rootloop" /disk 2>>/run/rootmount.err \
|
|
&& echo "mmcblk0@$ROOT_OFFSET" > /run/mu300-root-dev && diskdev=$rootloop
|
|
}
|
|
mount_sd() { # mount_sd DEV: the card, a block device of its own - no loop
|
|
mount -t ext4 -o noatime "$1" /disk 2>>/run/rootmount.err || return 1
|
|
if [ -z "$(pick_root)" ]; then
|
|
# the label without a system inside: an installation that was interrupted. Not a reason to give up on
|
|
# an internal system.
|
|
log "stage=sd-root-empty dev=$1"; umount /disk; return 1
|
|
fi
|
|
echo "$1" > /run/mu300-root-dev; diskdev=$1
|
|
log "stage=sd-root dev=$1"
|
|
}
|
|
sd_wait() { wait_sd_root 8; }
|
|
root_mounted=0 diskdev=
|
|
if sd=$(find_sd_root) && mount_sd "$sd"; then
|
|
root_mounted=1
|
|
elif mount_internal; then
|
|
root_mounted=1
|
|
# A card installation on a device that also has an internal one leaves this marker in the internal one: the
|
|
# card is what should boot, and the polled SD host may simply not have found it yet. Only then is it worth
|
|
# waiting; a device without a card installation never waits.
|
|
if [ -e /disk/.mu300/root-on-sd ]; then
|
|
if sd=$(sd_wait); then
|
|
umount /disk && losetup -d "$rootloop" 2>/dev/null
|
|
mount_sd "$sd" || { log "stage=sd-root-failed dev=$sd"; mount_internal || root_mounted=0; }
|
|
else
|
|
log "stage=sd-root-missing (marker present, booting the internal system)"
|
|
fi
|
|
fi
|
|
else
|
|
# nothing internal: the card is the only place a system can be, so give it its 8 seconds
|
|
log "stage=sd-wait (no internal system)"
|
|
sd=$(sd_wait) && mount_sd "$sd" && root_mounted=1
|
|
fi
|
|
# --- root-select end
|
|
if [ "$root_mounted" = 1 ] && rootdir=$(pick_root) && [ -n "$rootdir" ]; then
|
|
# A way back to Android with no computer and no keyboard: cut the power during boot N times in a row (N is
|
|
# .mu300/boot-attempts, 5 without it). Every boot counts up here and mu300-next-boot --rearm clears the count
|
|
# once the system is really up, so only boots that never got that far add up. LK does the same with the
|
|
# tries it was armed with (N+1) and normally gets there first; this is the backstop, and it goes to Android
|
|
# on the boot after the Nth failed one, exactly when LK would.
|
|
# only in default-Linux mode: with the one-shot trial every power cut already lands in Android
|
|
if [ "$(cat "$rootdir/etc/mu300/default-boot" 2>/dev/null)" = linux ]; then
|
|
tries=$(cat /disk/.mu300/boot-tries 2>/dev/null || echo 0)
|
|
case $tries in ''|*[!0-9]*) tries=0 ;; esac
|
|
tries=$((tries + 1))
|
|
mkdir -p /disk/.mu300 && echo "$tries" > /disk/.mu300/boot-tries && sync
|
|
else
|
|
tries=0
|
|
fi
|
|
max=$(cat /disk/.mu300/boot-attempts 2>/dev/null); case $max in [1-6]) ;; *) max=5 ;; esac
|
|
if [ "$tries" -gt "$max" ]; then
|
|
log "stage=boot-tries-exceeded count=$tries (booting Android instead)"
|
|
echo 0 > /disk/.mu300/boot-tries && sync
|
|
restore_slot_a
|
|
persist
|
|
sync
|
|
reboot -f
|
|
sleep 30
|
|
fi
|
|
|
|
if [ "$rootdir" = /disk ]; then
|
|
mount --move /disk /newroot
|
|
else
|
|
mount --bind "$rootdir" /newroot
|
|
# keep the whole disk reachable from inside (switching systems, editing the other one)
|
|
mkdir -p /newroot/mnt/mu300-disk && mount --move /disk /newroot/mnt/mu300-disk
|
|
fi
|
|
# Default-Linux mode: leave LK's decremented tries (b: not successful). Userspace re-arms slot b (tries N+1)
|
|
# once the system is up; N boots in a row that never get there and LK falls back to Android on the next one.
|
|
trial=1
|
|
if [ "$(cat /newroot/etc/mu300/default-boot 2>/dev/null)" = linux ]; then
|
|
log "stage=default-boot-linux (slot a restore skipped)"
|
|
trial=
|
|
else
|
|
restore_slot_a
|
|
fi
|
|
mkdir -p /run/mu300 && cp /etc/misc-bc-slot-a.bin /etc/misc-bc-slot-b-trial.bin /run/mu300/
|
|
[ -n "$misc" ] && echo "$misc" > /run/mu300/misc-dev
|
|
# the device /disk was mounted from: the card, or the loop device over the internal region
|
|
echo "$diskdev" > /run/mu300/disk-dev
|
|
echo "$DEVICE" > /run/mu300/device
|
|
init=/sbin/init
|
|
[ -x /newroot/lib/systemd/systemd ] && init=/lib/systemd/systemd
|
|
log "stage=switch-root root=$rootdir init=$init os=$(sed -n 's/^PRETTY_NAME=//p' /newroot/etc/os-release)"
|
|
persist
|
|
kill $(jobs -p) 2>/dev/null
|
|
for p in /proc/[0-9]*; do [ "${p#/proc/}" = 1 ] || kill -9 "${p#/proc/}" 2>/dev/null; done
|
|
# Trial guard, only in images built with --trial-guard SECONDS (tools/flash-trial.sh experiments, never an
|
|
# install): the timer above ends here, and a trial kernel that boots but never brings up USB (6.18 on the U30
|
|
# Air) left a device that only failing five boots by hand brought back. So one more timer outlives
|
|
# switch_root: unless /run/stay exists by then (created over SSH), it reboots - into Android, as slot a is
|
|
# already restored. switch_root deletes this ramdisk, so it runs from a copy of busybox in /run, reached
|
|
# through its working directory once /run has moved.
|
|
# Only in a trial boot: mu300-update keeps the device segment of whatever image is in boot_b, and a guard that
|
|
# came along from an experiment into an installed system restarted it every 10 minutes (FINDINGS 33e)
|
|
if [ -s /etc/mu300-trial-guard ] && [ -z "$trial" ]; then
|
|
log "stage=trial-guard-ignored (Linux is the default: not a trial)"
|
|
elif [ -s /etc/mu300-trial-guard ]; then
|
|
g=$(cat /etc/mu300-trial-guard)
|
|
# (named busybox: busybox takes the applet from its own name when that is not "busybox")
|
|
mkdir -p /run/.trial-guard && cp /bin/busybox /run/.trial-guard/busybox
|
|
log "stage=trial-guard ${g}s"
|
|
(cd /run && exec .trial-guard/busybox sh -c ".trial-guard/busybox sleep $g; [ -e stay ] && exit 0; .trial-guard/busybox sync; .trial-guard/busybox reboot -f") \
|
|
</dev/null >/dev/null 2>&1 &
|
|
fi
|
|
umount /config 2>/dev/null
|
|
mkdir -p /newroot/dev /newroot/proc /newroot/sys /newroot/run
|
|
mount --move /dev /newroot/dev
|
|
mount --move /proc /newroot/proc
|
|
mount --move /sys /newroot/sys
|
|
mount --move /run /newroot/run 2>/dev/null
|
|
exec switch_root /newroot $init
|
|
fi
|
|
log "stage=rootfs-unavailable $(cat /run/rootmount.err)"
|
|
restore_slot_a
|
|
ifconfig usb0 $NET.1 netmask 255.255.255.0 up
|
|
log "stage=usb0 $(ifconfig usb0 2>&1 | head -1)"
|
|
cat > /etc/udhcpd.conf <<EOF
|
|
start $NET.2
|
|
end $NET.9
|
|
interface usb0
|
|
option subnet 255.255.255.0
|
|
option lease 600
|
|
lease_file /run/udhcpd.leases
|
|
pidfile /run/udhcpd.pid
|
|
EOF
|
|
touch /run/udhcpd.leases
|
|
udhcpd /etc/udhcpd.conf
|
|
telnetd -b $NET.1 -p 23 -l /bin/sh
|
|
mkdir -p /run/www
|
|
echo "MU300 standalone Linux v5: telnet $NET.1; auto reboot to Android after 300s unless /run/stay exists" > /run/www/index.html
|
|
httpd -p $NET.1:80 -h /run/www
|
|
log "stage=ready"
|
|
|
|
# Android vendor services in a chroot. modem_control reloads pm_sys through Trusty; only then
|
|
# can sprd_pmic_wdt send "watchdog rstoff" and stop the PM co-processor from cutting power.
|
|
A=/android
|
|
cp -a $A/dev/__properties__ /dev/__properties__
|
|
mkdir -p /dev/block/by-name
|
|
for u in /sys/class/block/mmcblk0p*/uevent; do
|
|
d=$(basename "$(dirname "$u")")
|
|
n=$(sed -n 's/^PARTNAME=//p' "$u")
|
|
ln -sf /dev/$d /dev/block/$d
|
|
[ -n "$n" ] && ln -sf /dev/$d /dev/block/by-name/$n
|
|
done
|
|
mkdir -p $A/dev $A/proc $A/sys $A/data/vendor $A/mnt $A/tmp
|
|
mount --bind /dev $A/dev
|
|
mount -t proc proc $A/proc
|
|
mount -t sysfs sysfs $A/sys
|
|
# modem_control switches to uid system(1000); apply Android's ueventd/init node ownership
|
|
sh /etc/ueventd-perms.sh
|
|
# LK boots us from slot b; the modem images Android uses (and keeps updated) are the _a copies
|
|
# the vendor daemons read androidboot.* (slot, modem config) from the command line; the mainline kernel forces its
|
|
# own command line, so fall back to the bootargs LK put in the device tree
|
|
src=/proc/cmdline
|
|
grep -q androidboot.slot_suffix /proc/cmdline || [ ! -r /proc/device-tree/chosen/bootargs ] || src=/proc/device-tree/chosen/bootargs
|
|
tr -d '\000' < $src | sed 's/androidboot.slot_suffix=_b/androidboot.slot_suffix=_a/' > /run/cmdline.android
|
|
mount --bind /run/cmdline.android $A/proc/cmdline
|
|
# liblog sends to logd's datagram socket; capture it so vendor daemons are debuggable
|
|
mkdir -p /dev/socket
|
|
/bin/logdw /dev/socket/logdw > /run/logcat.txt 2>&1 &
|
|
sleep 1
|
|
log "stage=android-chroot-ready nodes=$(ls /dev/modem /dev/pmsys /dev/sctl_pm /dev/trusty-ipc-dev0 2>&1 | tr '\n' ',')"
|
|
# exec the binary itself (interpreter /system/bin/linker64): sprd_modem_loader only accepts
|
|
# ioctls/writes from a task whose comm is exactly "modem_control"
|
|
LD_LIBRARY_PATH=/apex/com.android.runtime/lib64/bionic:/system/lib64:/vendor/lib64 \
|
|
chroot $A /vendor/bin/modem_control > /run/modem_control.log 2>&1 &
|
|
log "stage=modem_control-started pid=$!"
|
|
(for n in $(seq 1 120); do
|
|
if dmesg | grep -q "sbuf ready for pmic wdt init"; then log "stage=pmic-wdt-rstoff-ready t=$n"; exit 0; fi
|
|
sleep 2
|
|
done; log "stage=pmic-wdt-NOT-ready") &
|
|
while :; do
|
|
dmesg > /run/www/dmesg.txt
|
|
cat /proc/modules > /run/www/modules.txt
|
|
sleep 5
|
|
done
|