fix: 刷机后开机时把 USB 序列号改成芯片号
This commit is contained in:
+74
-5
@@ -86,16 +86,85 @@ impl AdbDevice {
|
||||
pub fn mark(device: &AdbDevice, _chip_serial: &str, _model_id: &str) -> Result<String, String> {
|
||||
let text = shell(device, &devinfo_script())?;
|
||||
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());
|
||||
}
|
||||
let script = format!(
|
||||
"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"
|
||||
);
|
||||
shell(device, &script)?;
|
||||
// gc 每次开机都把序列号写成 0123456789,管理脚本里的那一行并不会被执行。
|
||||
// 在 gc 完成后再改序列号并重新绑定。不能在当前这条 ADB 里直接改,断开 USB 会把脚本一起杀掉。
|
||||
shell(device, &install_serial_service())?;
|
||||
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> {
|
||||
shell(device, "reboot")?;
|
||||
Ok(())
|
||||
|
||||
+3
-1
@@ -1521,7 +1521,9 @@ fn adb_popup(
|
||||
let device = device.clone();
|
||||
let adb_refresh = Arc::clone(adb_refresh);
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user