fix: 刷机后开机时把 USB 序列号改成芯片号

This commit is contained in:
Nixevol
2026-11-29 19:43:53 +08:00
parent 5e7d9728ca
commit a7e5635fd5
2 changed files with 77 additions and 6 deletions
+74 -5
View File
@@ -86,16 +86,85 @@ impl AdbDevice {
pub fn mark(device: &AdbDevice, _chip_serial: &str, _model_id: &str) -> Result<String, String> { pub fn mark(device: &AdbDevice, _chip_serial: &str, _model_id: &str) -> Result<String, String> {
let text = shell(device, &devinfo_script())?; let text = shell(device, &devinfo_script())?;
let chip_serial = devinfo_chip(&text); let chip_serial = devinfo_chip(&text);
if chip_serial.is_empty() { if chip_serial.len() < 4
|| chip_serial.len() > 16
|| !chip_serial.chars().all(|ch| ch.is_ascii_hexdigit())
{
return Err("devinfo 里没有芯片号".into()); return Err("devinfo 里没有芯片号".into());
} }
let script = format!( // gc 每次开机都把序列号写成 0123456789,管理脚本里的那一行并不会被执行。
"if [ -f /usr/sbin/openstick-gc-manager.sh ]; then sed -i 's|echo \"[^\"]*\" > ${{STRING_DIR}}/serialnumber|echo \"{chip_serial}\" > ${{STRING_DIR}}/serialnumber|' /usr/sbin/openstick-gc-manager.sh; fi\nif [ -f /sys/kernel/config/usb_gadget/g1/strings/0x409/serialnumber ]; then echo '{chip_serial}' > /sys/kernel/config/usb_gadget/g1/strings/0x409/serialnumber || true; fi\nprintf 'CHIP=%s\\n' '{chip_serial}'\n" // 在 gc 完成后再改序列号并重新绑定。不能在当前这条 ADB 里直接改,断开 USB 会把脚本一起杀掉。
); shell(device, &install_serial_service())?;
shell(device, &script)?;
Ok(chip_serial) Ok(chip_serial)
} }
fn install_serial_service() -> String {
r#"
mkdir -p /usr/local/sbin /etc/systemd/system
cat > /usr/local/sbin/flasher-serial.sh << 'EOF'
#!/bin/sh
part=/dev/disk/by-partlabel/devinfo
if [ ! -b "$part" ]; then
exit 0
fi
sz=$(blockdev --getsz "$part" 2>/dev/null || echo 0)
if [ "$sz" -le 0 ]; then
exit 0
fi
chip=$(dd if="$part" bs=512 skip=$((sz - 1)) count=1 status=none 2>/dev/null | tr -d '\000' | sed -n 's/^chip_serial=//p' | head -n 1)
echo "$chip" | grep -Eq '^[0-9A-Fa-f]{4,16}$' || exit 0
gadget=/sys/kernel/config/usb_gadget/g1
strings=$gadget/strings/0x409
i=0
while [ ! -d "$strings" ] && [ "$i" -lt 20 ]; do
sleep 0.5
i=$((i + 1))
done
if [ ! -d "$strings" ]; then
exit 0
fi
current=$(cat "$strings/serialnumber" 2>/dev/null || true)
if [ "$current" = "$chip" ]; then
exit 0
fi
udc=$(cat "$gadget/UDC" 2>/dev/null || true)
rebind() {
if [ -n "$udc" ]; then
now=$(cat "$gadget/UDC" 2>/dev/null || true)
if [ -z "$now" ]; then
echo "$udc" > "$gadget/UDC" || true
fi
fi
}
trap rebind EXIT
if [ -n "$udc" ]; then
echo > "$gadget/UDC" || exit 0
fi
sleep 0.2
if ! echo "$chip" > "$strings/serialnumber"; then
exit 0
fi
exit 0
EOF
chmod 755 /usr/local/sbin/flasher-serial.sh
cat > /etc/systemd/system/flasher-serial.service << 'EOF'
[Unit]
Description=Apply chip serial to USB gadget
After=openstick-gc-startup.service
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/flasher-serial.sh
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable flasher-serial.service
"#
.into()
}
pub fn reboot(device: &AdbDevice) -> Result<(), String> { pub fn reboot(device: &AdbDevice) -> Result<(), String> {
shell(device, "reboot")?; shell(device, "reboot")?;
Ok(()) Ok(())
+3 -1
View File
@@ -1521,7 +1521,9 @@ fn adb_popup(
let device = device.clone(); let device = device.clone();
let adb_refresh = Arc::clone(adb_refresh); let adb_refresh = Arc::clone(adb_refresh);
thread::spawn(move || { thread::spawn(move || {
let _ = adb::mark(&device, &chip, &model); if adb::mark(&device, &chip, &model).is_ok() {
let _ = adb::reboot(&device);
}
adb_refresh.store(true, Ordering::SeqCst); adb_refresh.store(true, Ordering::SeqCst);
}); });
} }