Files
mi8-platina-linux/scripts/setup_usb_gadget_service.sh
2026-11-18 01:39:26 +09:00

75 lines
2.3 KiB
Bash

#!/bin/sh
# Setup robust USB gadget network service for postmarketOS userspace
mkdir -p /sysroot/usr/local/sbin
cat << 'EOF' > /sysroot/usr/local/sbin/platina-usb-gadget
#!/bin/sh
# Platina USB gadget NCM bringup (preserves initramfs-bound g1 to avoid macOS NCM re-enumeration drops)
set -e
CONFIGFS=/sys/kernel/config/usb_gadget
if ! mountpoint -q /sys/kernel/config; then
mount -t configfs none /sys/kernel/config 2>/dev/null || true
fi
CURRENT_UDC=$(cat "$CONFIGFS/g1/UDC" 2>/dev/null || true)
if [ -z "$CURRENT_UDC" ]; then
mkdir -p "$CONFIGFS/g1"
echo "0x2717" > "$CONFIGFS/g1/idVendor"
echo "0xff80" > "$CONFIGFS/g1/idProduct"
mkdir -p "$CONFIGFS/g1/strings/0x409"
echo "Xiaomi" > "$CONFIGFS/g1/strings/0x409/manufacturer"
echo "63c36adb" > "$CONFIGFS/g1/strings/0x409/serialnumber"
echo "Xiaomi Mi 8 Lite" > "$CONFIGFS/g1/strings/0x409/product"
mkdir -p "$CONFIGFS/g1/functions/ncm.usb0"
echo "d2:d1:1e:20:5a:dc" > "$CONFIGFS/g1/functions/ncm.usb0/host_addr" 2>/dev/null || true
echo "0e:f1:f5:d7:ba:a9" > "$CONFIGFS/g1/functions/ncm.usb0/dev_addr" 2>/dev/null || true
mkdir -p "$CONFIGFS/g1/configs/c.1/strings/0x409"
echo "USB Network" > "$CONFIGFS/g1/configs/c.1/strings/0x409/configuration"
if [ ! -L "$CONFIGFS/g1/configs/c.1/ncm.usb0" ]; then
ln -s "$CONFIGFS/g1/functions/ncm.usb0" "$CONFIGFS/g1/configs/c.1/"
fi
UDC=$(ls /sys/class/udc | head -1)
if [ -n "$UDC" ]; then
echo "$UDC" > "$CONFIGFS/g1/UDC"
fi
fi
ip link set usb0 up 2>/dev/null || true
ip addr replace 172.16.42.1/24 dev usb0 2>/dev/null || true
EOF
chmod +x /sysroot/usr/local/sbin/platina-usb-gadget
# OpenRC init script
cat << 'EOF' > /sysroot/etc/init.d/platina-usb-gadget
#!/sbin/openrc-run
description="Platina USB Gadget Network"
depend() {
before networkmanager
}
start() {
ebegin "Starting Platina USB gadget network"
/usr/local/sbin/platina-usb-gadget
eend $?
}
stop() {
ebegin "Stopping Platina USB gadget network"
echo "" > /sys/kernel/config/usb_gadget/g1/UDC 2>/dev/null || true
eend $?
}
EOF
chmod +x /sysroot/etc/init.d/platina-usb-gadget
# Enable in boot runlevel before networkmanager
ln -sf /etc/init.d/platina-usb-gadget /sysroot/etc/runlevels/boot/platina-usb-gadget
# Remove broken usb-moded
rm -f /sysroot/etc/runlevels/default/usb-moded