Files
lotherkandClaude Sonnet 5.5 73c9de51a6 licence: the CNCL line in every file Makronix wrote (tools, docs, www, mobile, top level)
consmirror.c: the BSD notice is replaced (Makronix's own file).  Files with
changes of other sessions pending are left for their commit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:50:26 +00:00

201 lines
7.5 KiB
Bash

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# Shared by the tools: load a device's configuration.
#
# . "$(dirname "$0")/lib.sh"
# load_device devices/hotdogb
#
# The device's SOC (set in its device.conf) names soc/<SOC>/soc.conf, which is
# read first; the device.conf is read second and overrides it. Afterwards:
#
# DEVDIR the device directory
# DTB, CMDLINE, KERNEL_CONFIG ... made absolute
# everything else as set in the two files
#
# Adding a phone is adding devices/<codename>/ (docs/PORTING.md); no tool
# changes.
ROOT=$(cd "$(dirname "$0")/.." && pwd)
load_device() {
DEVDIR=$(cd "$1" && pwd) || { echo "no such device dir: $1" >&2; exit 2; }
[ -f "$DEVDIR/device.conf" ] || { echo "$DEVDIR: no device.conf" >&2; exit 2; }
SOC=$(sed -n 's/^SOC=//p' "$DEVDIR/device.conf" | head -n 1)
[ -n "$SOC" ] || { echo "$DEVDIR/device.conf: SOC not set" >&2; exit 2; }
[ -f "$ROOT/soc/$SOC/soc.conf" ] || { echo "no soc/$SOC/soc.conf" >&2; exit 2; }
. "$ROOT/soc/$SOC/soc.conf"
. "$DEVDIR/device.conf"
for _v in DTB CMDLINE KERNEL_CONFIG KERNEL_LOGO; do
eval "_p=\${$_v:-}"
[ -z "$_p" ] || eval "$_v=\$DEVDIR/\$_p"
done
}
die() {
echo "$0: $*" >&2
exit 1
}
# makronix_version: MX_RELEASE (0.99.9), MX_CODENAME (Camouflage) and
# MX_CHANNEL from the files the NetBSD side reads (src/sys/conf/newvers.sh,
# src/etc/Makefile): makronix-release and makronix-codename. The channel is
# unstable while the release is an x.99.y -- NetBSD's convention for
# -current, not a release -- and stable otherwise.
makronix_version() {
_mx=$ROOT/../src/sys/conf
MX_RELEASE=$(cat "$_mx/makronix-release") || die "no $_mx/makronix-release"
MX_CODENAME=$(cat "$_mx/makronix-codename") || die "no $_mx/makronix-codename"
case $MX_RELEASE in
*.99.*) MX_CHANNEL=unstable ;;
*) MX_CHANNEL=stable ;;
esac
}
# userland_stage DEVDIR-BASENAME OUT: the NetBSD userland of mobile/userland
# for the image (docs/NETBSD-USERLAND.md, A7), staged in OUT as a tree that
# assemble-rootfs.sh lays over the Void base:
# - the ports' destdir (build/userland/<dev>/destdir, make userland)
# without its METALOG; a program whose port's AGENTS.md does not say
# "state: done" is left out -- a half-port never reaches the phone;
# - libstdc++ and libgcc_s of the tooldir's gcc 14.4 (make targetlibs),
# stripped: the ports and every C++ program built here need them, and
# libstdc++.so.6.0.33 is backward compatible with Void's 6.0.32.
# Prints nothing and makes an empty OUT when no userland is built (an image
# of the Void base alone).
userland_stage() {
_dev=$1 _out=$2
_dd=$ROOT/build/userland/$_dev/destdir
mkdir -p "$_out"
_out=$(cd "$_out" && pwd)
_ex=$_out.exclude
: > "$_ex"
[ -d "$_dd/usr" ] || { rm -f "$_ex"; return 0; }
for _b in "$_dd"/usr/bin/* "$_dd"/usr/libexec/*; do
[ -f "$_b" ] || continue
_p=$(basename "$_b")
_a=$ROOT/userland/$_p/AGENTS.md
if [ ! -f "$_a" ]; then
# a LINKS name ("[" of test): a hard link of a port's program
for _o in "$_dd"/usr/bin/* "$_dd"/usr/libexec/*; do
[ "$_o" != "$_b" ] && [ "$_b" -ef "$_o" ] &&
[ -f "$ROOT/userland/$(basename "$_o")/AGENTS.md" ] &&
{ _a=$ROOT/userland/$(basename "$_o")/AGENTS.md; break; }
done
fi
if [ ! -f "$_a" ] ||
[ "$(sed -n 's/^state: *//p' "$_a" | head -n 1)" != done ]; then
# a port at work (another agent may be building it):
# left out of the image, the rest goes in
echo "userland_stage: ${_b#$_dd/} left out, its port is not done" >&2
echo "./${_b#$_dd/}" >> "$_ex"
fi
done
(cd "$_dd" && tar --exclude ./METALOG -X "$_ex" -cf - .) |
(cd "$_out" && tar -xf -) || die "cannot stage $_dd"
rm -f "$_ex"
_td=$(ls -d "$ROOT"/build/tooldir.*-aarch64-linux-gnu 2>/dev/null | head -n 1)
_tl=$_td/aarch64-linux-gnu/lib64
[ -f "$_tl/libstdc++.so.6.0.33" ] && [ -f "$_tl/libgcc_s.so.1" ] ||
die "no libstdc++/libgcc_s in $_tl (make targetlibs)"
mkdir -p "$_out/usr/lib"
for _l in libstdc++.so.6.0.33 libgcc_s.so.1; do
"$_td/bin/aarch64-linux-gnu-strip" --strip-unneeded \
-o "$_out/usr/lib/$_l" "$_tl/$_l" || die "cannot strip $_l"
done
ln -sf libstdc++.so.6.0.33 "$_out/usr/lib/libstdc++.so.6"
}
# kernel_make TREE TARGET...: make in a kernel tree with the kernel's name in
# Makronix's versioning (makronix_version): uname -r is <linux>-makronix, with
# -unstable on the unstable channel, and /proc/version names
# makronix@<codename>. Every make in the tree goes through here: kbuild
# writes include/config/kernel.release again on each call, so a
# modules_install without the same LOCALVERSION would install under another
# name. kernel.config sets no CONFIG_LOCALVERSION and no LOCALVERSION_AUTO; a
# LOCALVERSION on the command line also keeps setlocalversion from appending
# "+" for the patched tree.
kernel_make() {
_tree=$1
shift
makronix_version
case $MX_CHANNEL in
unstable) _lv=-makronix-unstable ;;
*) _lv=-makronix ;;
esac
make -C "$_tree" ARCH="$ARCH" CROSS_COMPILE="$CROSS_COMPILE" \
LOCALVERSION="$_lv" KBUILD_BUILD_USER=makronix \
KBUILD_BUILD_HOST="$(echo "$MX_CODENAME" | tr '[:upper:]' '[:lower:]')" "$@"
}
# The running phone over the USB network. phone_addr prints the address
# where ssh answers: PHONE_IP (172.16.42.1) if it does, else the IPv6
# link-local neighbour on the host's cdc_ncm interface -- the host loses its
# IPv4 when it misses the phone's DHCP (docs/BOOT.md) -- or nothing.
# phone_ssh ADDR CMD runs CMD as root (PHONE_PASS, default makronix).
phone_ssh() {
sshpass -p "${PHONE_PASS:-makronix}" ssh -o ConnectTimeout=3 \
-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
-o LogLevel=error "root@$1" "$2"
}
phone_addr() {
command -v sshpass >/dev/null || die "needs sshpass"
if phone_ssh "${PHONE_IP:-172.16.42.1}" true 2>/dev/null; then
echo "${PHONE_IP:-172.16.42.1}"
return 0
fi
for _d in /sys/class/net/*; do
[ "$(basename "$(readlink -f "$_d/device/driver" 2>/dev/null)")" = cdc_ncm ] ||
continue
_i=${_d##*/}
_own=$(ip -6 -o addr show dev "$_i" scope link 2>/dev/null |
awk '{sub("/.*", "", $4); print $4}')
for _a in $(ping -6 -c 2 -W 2 "ff02::1%$_i" 2>/dev/null |
awk '/bytes from/ {sub("%.*", "", $4); print $4}' | sort -u); do
[ "$_a" = "$_own" ] && continue
if phone_ssh "$_a%$_i" true 2>/dev/null; then
echo "$_a%$_i"
return 0
fi
done
done
return 1
}
# fastboot_ready: a phone in fastboot that answers, or die. When none is
# there but the phone runs Linux, reboot it into fastboot (reboot-mode
# bootloader, which needs the PON's mode-bootloader in the DT); otherwise
# wait, the phone may be between two USB identities. Gives up after 120 s
# without a fastboot device. Never resets the device (fastboot_answers).
fastboot_answers() {
[ -n "$(fastboot devices)" ] || return 1
# Wait long and never cut a command off: after a large flash (userdata)
# ABL stays busy for more than 15 s and does not answer; a host that
# gives up and resets the device then (usbreset) crashes it into the
# Qualcomm crash-dump mode, 05c6:900e (measured 2026-11-08: two resets
# 30 s apart, then error -71 and the phone dark).
timeout 120 fastboot getvar product >/dev/null 2>&1
}
fastboot_ready() {
_t=0 _asked=
while [ "$_t" -lt 120 ]; do
if [ -n "$(fastboot devices)" ]; then
fastboot_answers && return 0
die "the phone is in fastboot but does not answer"
fi
if [ -z "$_asked" ] && _a=$(phone_addr); then
echo "$0: rebooting the phone at $_a into fastboot" >&2
phone_ssh "$_a" "reboot-mode bootloader" >/dev/null 2>&1 || true
_asked=1
fi
sleep 2
_t=$((_t + 2))
done
die "no phone in fastboot within 120 s"
}