Files
Nixevol 6aec54f8ff feat: 面板升级到 V0.9,刷机改为同一次连接里备份并写入
补齐热点、短信、流量和系统设置;9008 芯片不答话时及时停止,避免把引导程序写死。
2026-11-30 09:55:43 +08:00

163 lines
5.5 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/sh
# 开机只枚举一次 USB:Windows 能直接认出的 RNDIS,加上 ADB。
# 网卡地址由这块板子的芯片号算出来,避免每次开机都被电脑当成一块新网卡。
G=/sys/kernel/config/usb_gadget/g1
UDC_NAME=ci_hdrc.0
log() {
logger -t mifipanel-usb "$*" 2>/dev/null || true
}
read_id() {
part=/dev/disk/by-partlabel/devinfo
if [ -b "$part" ]; then
sz=$(blockdev --getsz "$part" 2>/dev/null || echo 0)
if [ "$sz" -gt 0 ]; then
blob=$(dd if="$part" bs=512 skip=$((sz - 1)) count=1 status=none 2>/dev/null | tr -d '\000')
chip=$(printf '%s\n' "$blob" | sed -n 's/^chip_serial=//p' | head -n 1 | tr -d ' \r')
if printf '%s' "$chip" | grep -Eq '^(0[xX])?[0-9A-Fa-f]{4,16}$'; then
printf '%s' "$chip"
return
fi
fi
fi
if [ -r /sys/block/mmcblk0/device/cid ]; then
cid=$(tr -d ' \r\n\000' < /sys/block/mmcblk0/device/cid)
if printf '%s' "$cid" | grep -Eq '^[0-9A-Fa-f]{8,64}$'; then
printf '%s' "$cid"
return
fi
fi
printf '%s' "ufi003"
}
mac_of() {
sum=$(printf '%s' "$1" | sha256sum | awk '{print $1}')
o1=$(printf '%s' "$sum" | cut -c1-2)
o2=$(printf '%s' "$sum" | cut -c3-4)
o3=$(printf '%s' "$sum" | cut -c5-6)
o4=$(printf '%s' "$sum" | cut -c7-8)
o5=$(printf '%s' "$sum" | cut -c9-10)
o6=$(printf '%s' "$sum" | cut -c11-12)
n=$(printf '%d' "0x$o1")
n=$(( (n | 2) & 254 ))
printf '%02x:%s:%s:%s:%s:%s' "$n" "$o2" "$o3" "$o4" "$o5" "$o6"
}
write_text() {
printf '%s' "$2" > "$1" 2>/dev/null || return 1
}
modprobe libcomposite 2>/dev/null || true
if [ -e /sys/class/udc/$UDC_NAME/device/role ]; then
echo gadget > /sys/class/udc/$UDC_NAME/device/role 2>/dev/null || true
fi
ID=$(read_id)
HOST=$(mac_of "usb-host:$ID")
DEV=$(mac_of "usb-dev:$ID")
ready() {
[ -d "$G/functions/rndis.rndis.1" ] || return 1
[ -L "$G/configs/c1.1/adb" ] || return 1
[ -L "$G/configs/c1.1/rndis.1" ] || return 1
cur_id=$(tr -d ' \r\n\000' < "$G/idProduct" 2>/dev/null | tr 'A-F' 'a-f' || true)
cur_class=$(tr -d ' \r\n\000' < "$G/bDeviceClass" 2>/dev/null | tr 'A-F' 'a-f' || true)
cur_host=$(tr -d ' \r\n\000' < "$G/functions/rndis.rndis.1/host_addr" 2>/dev/null | tr 'A-F' 'a-f' || true)
cur_serial=$(tr -d ' \r\n\000' < "$G/strings/0x409/serialnumber" 2>/dev/null || true)
cur_udc=$(tr -d ' \r\n\000' < "$G/UDC" 2>/dev/null || true)
[ "$cur_id" = "0x4ee9" ] || return 1
case "$cur_class" in
0xef|ef|239) ;;
*) return 1 ;;
esac
[ "$cur_host" = "$HOST" ] || return 1
[ "$cur_serial" = "$ID" ] || return 1
[ -n "$cur_udc" ] || return 1
}
bring_up_lan() {
i=0
while [ ! -d /sys/class/net/usb0 ] && [ "$i" -lt 30 ]; do
sleep 0.1
i=$((i + 1))
done
if command -v nmcli >/dev/null 2>&1; then
nmcli -w 8 connection up usb-failsafe >/dev/null 2>&1 || true
fi
}
if ready; then
log "USB 网卡已是固定配置"
bring_up_lan
exit 0
fi
if [ -d "$G" ] && [ -f "$G/UDC" ]; then
printf '\n' > "$G/UDC" 2>/dev/null || true
fi
rm -f "$G/configs/c1.1/rndis.1" "$G/configs/c1.1/adb" "$G/configs/c.1/rndis.1" "$G/configs/c.1/adb" \
"$G/os_desc/c1.1" "$G/os_desc/c.1" 2>/dev/null || true
rmdir "$G/configs/c1.1/strings/0x409" "$G/configs/c1.1/strings" "$G/configs/c1.1" \
"$G/configs/c.1/strings/0x409" "$G/configs/c.1/strings" "$G/configs/c.1" \
"$G/functions/rndis.rndis.1" "$G/functions/ffs.adb" "$G/strings/0x409" "$G" 2>/dev/null || true
mkdir -p "$G/strings/0x409" \
"$G/functions/rndis.rndis.1/os_desc/interface.rndis" \
"$G/functions/ffs.adb" \
"$G/configs/c1.1/strings/0x409" \
"$G/os_desc" || {
log "无法创建 USB gadget"
exit 1
}
write_text "$G/idVendor" "0x18d1" || exit 1
write_text "$G/idProduct" "0x4ee9" || exit 1
write_text "$G/bcdUSB" "0x0200" || true
write_text "$G/bcdDevice" "0x0001" || true
write_text "$G/bDeviceClass" "0xef" || exit 1
write_text "$G/bDeviceSubClass" "0x02" || exit 1
write_text "$G/bDeviceProtocol" "0x01" || exit 1
write_text "$G/strings/0x409/serialnumber" "$ID" || exit 1
write_text "$G/strings/0x409/manufacturer" "HandsomeTech" || true
write_text "$G/strings/0x409/product" "HandsomeMod Device" || true
write_text "$G/functions/rndis.rndis.1/class" "e0" || exit 1
write_text "$G/functions/rndis.rndis.1/subclass" "01" || exit 1
write_text "$G/functions/rndis.rndis.1/protocol" "03" || exit 1
write_text "$G/functions/rndis.rndis.1/host_addr" "$HOST" || exit 1
write_text "$G/functions/rndis.rndis.1/dev_addr" "$DEV" || exit 1
write_text "$G/functions/rndis.rndis.1/os_desc/interface.rndis/compatible_id" "RNDIS" || exit 1
write_text "$G/functions/rndis.rndis.1/os_desc/interface.rndis/sub_compatible_id" "5162001" || exit 1
write_text "$G/configs/c1.1/bmAttributes" "0x80" || true
write_text "$G/configs/c1.1/MaxPower" "500" || true
write_text "$G/configs/c1.1/strings/0x409/configuration" "rndis_adb" || true
ln -s "$G/functions/rndis.rndis.1" "$G/configs/c1.1/rndis.1"
ln -s "$G/functions/ffs.adb" "$G/configs/c1.1/adb"
ln -s "$G/configs/c1.1" "$G/os_desc/c1.1"
write_text "$G/os_desc/use" "1" || exit 1
write_text "$G/os_desc/b_vendor_code" "0xbc" || true
write_text "$G/os_desc/qw_sign" "MSFT100" || true
mkdir -p /dev/usb-ffs/adb
if ! mountpoint -q /dev/usb-ffs/adb 2>/dev/null; then
mount -t functionfs adb /dev/usb-ffs/adb || {
log "ADB 功能文件系统挂载失败"
exit 1
}
fi
systemctl start adbd || log "adbd 没有启动"
i=0
while [ ! -e /dev/usb-ffs/adb/ep0 ] && [ "$i" -lt 50 ]; do
sleep 0.1
i=$((i + 1))
done
write_text "$G/UDC" "$UDC_NAME" || {
log "USB 绑定失败"
exit 1
}
log "USB 网卡已绑定 serial=$ID host=$HOST"
bring_up_lan
exit 0