wms_worker_station/scripts/set-eth-dhcp.ps1
liufumingren dcbf6e3b1f feat(device): 新增 set-eth-dhcp.ps1,X9R 有线网默认改为 DHCP
根因:本 ROM 的以太网服务把配置存在 /system/etc/ipconfig.txt(不是常见的 /data/misc/ethernet/),该文件位于只读 /system,在安卓设置里改成 DHCP 无法保存,重启后照旧读回出厂静态记录(STATIC 10.10.1.100/24)。

- 新增 scripts/set-eth-dhcp.ps1:adb root/remount 后按 AOSP IpConfigStore v2 格式写入 DHCP 记录;写入前把原文件备份到 /data/local/tmp 与 PC,-Restore 可回滚
- README「机型注意:X9R」新增该条目,排查工具表加入该脚本

实测 W2ACWX7T44:连续重启后 IP assignment: DHCP,租约 192.168.11.101/22
2026-09-30 17:54:22 +08:00

198 lines
7.2 KiB
PowerShell

<#
.SYNOPSIS
Set the default wired-ethernet IP assignment of the X9R / RK3288 tablets to DHCP.
.DESCRIPTION
Why the wired network keeps coming back as STATIC after every reboot:
The ROM's ethernet service (android.net.IEthernetManager) does NOT use the
usual /data/misc/ethernet/ipconfig.txt path. Its EthernetConfigStore is
built with Environment.getRootDirectory() + "/etc/ipconfig.txt", i.e.
/system/etc/ipconfig.txt
(verified by searching the string table of
/data/dalvik-cache/arm/system@framework@ethernet-service.jar@classes.dex)
That file lives on the read-only /system partition, so the Android
Settings UI can never persist a change - it writes, the write fails and
after the next reboot the baked-in STATIC record is read back again.
This script rewrites that file with a DHCP record in the AOSP IpConfigStore
version-2 format (big-endian 2-byte length prefixed UTF-8 strings):
int version = 2
utf "ipAssignment" utf "DHCP"
utf "proxySettings" utf "NONE"
utf "id" int 0
utf "eos"
The original file is backed up on the device (/data/local/tmp/ipconfig.txt.bak)
and on the PC before it is replaced.
.PARAMETER Serial
adb device serial (e.g. SCOPAU6MNZ). Omit when exactly one device is attached.
.PARAMETER NoReboot
Write the file but do not reboot (the new value only takes effect at boot).
.PARAMETER Restore
Put the on-device backup back instead of writing the DHCP record.
.PARAMETER Static
Skip the DHCP rewrite and only show instructions (reserved for future use).
.EXAMPLE
powershell -ExecutionPolicy Bypass -File scripts\set-eth-dhcp.ps1
.EXAMPLE
powershell -ExecutionPolicy Bypass -File scripts\set-eth-dhcp.ps1 -Restore
#>
[CmdletBinding()]
param(
[string]$Serial,
[switch]$NoReboot,
[switch]$Restore
)
$ErrorActionPreference = 'Stop'
$CONFIG_PATH = '/system/etc/ipconfig.txt'
$BACKUP_PATH = '/data/local/tmp/ipconfig.txt.bak'
$LOCAL_BACKUP_DIR = Join-Path $env:TEMP 'eth-ipconfig-backup'
function Find-Adb {
$candidates = @()
if ($env:ADB) { $candidates += $env:ADB }
$sdk = if ($env:ANDROID_HOME) { $env:ANDROID_HOME } elseif ($env:ANDROID_SDK_ROOT) { $env:ANDROID_SDK_ROOT } else { '' }
if ($sdk) { $candidates += (Join-Path $sdk 'platform-tools\adb.exe') }
if ($env:LOCALAPPDATA) { $candidates += (Join-Path $env:LOCALAPPDATA 'Android\Sdk\platform-tools\adb.exe') }
$candidates += 'adb.exe'
foreach ($c in $candidates) {
try {
$out = & $c version 2>$null
if ($LASTEXITCODE -eq 0 -and ($out -join ' ') -match 'Android Debug Bridge') { return $c }
} catch { }
}
throw 'adb.exe not found. Install platform-tools or pass its folder via the PATH / ANDROID_HOME environment variable.'
}
function Invoke-Adb {
param([Parameter(ValueFromRemainingArguments = $true)][string[]]$Args)
$all = @()
if ($Serial) { $all += @('-s', $Serial) }
$all += $Args
& $script:Adb @all
if ($LASTEXITCODE -ne 0) { throw ("adb " + ($all -join ' ') + " failed with exit code $LASTEXITCODE") }
}
function New-DhcpConfigBytes {
$buf = New-Object 'System.Collections.Generic.List[byte]'
$addInt = {
param([int]$v)
foreach ($shift in 24, 16, 8, 0) {
$buf.Add([byte](($v -shr $shift) -band 0xFF))
}
}
$addUtf = {
param([string]$s)
$b = [Text.Encoding]::UTF8.GetBytes($s)
$buf.Add([byte](($b.Length -shr 8) -band 0xFF))
$buf.Add([byte]($b.Length -band 0xFF))
$buf.AddRange($b)
}
& $addInt 2
& $addUtf 'ipAssignment'
& $addUtf 'DHCP'
& $addUtf 'proxySettings'
& $addUtf 'NONE'
& $addUtf 'id'
& $addInt 0
& $addUtf 'eos'
return $buf.ToArray()
}
$script:Adb = Find-Adb
Write-Host "[adb] $script:Adb"
# Resolve the target device
$devices = @()
(& $script:Adb devices) | Select-Object -Skip 1 | ForEach-Object {
if ($_ -match '^(\S+)\s+device') { $devices += $Matches[1] }
}
if ($devices.Count -eq 0) { throw 'No adb device connected.' }
if (-not $Serial) {
if ($devices.Count -gt 1) { throw ("More than one device connected (" + ($devices -join ', ') + '). Use -Serial <serial>.') }
$Serial = $devices[0]
}
Write-Host "[device] $Serial"
if ($Restore) {
Invoke-Adb shell "ls -l $BACKUP_PATH" | Out-Null
Invoke-Adb root | Out-Null
Invoke-Adb wait-for-device | Out-Null
Invoke-Adb remount | Out-Null
Invoke-Adb shell "cp -a $BACKUP_PATH $CONFIG_PATH" | Out-Null
Invoke-Adb shell "sync" | Out-Null
Write-Host "[ok] restored $CONFIG_PATH from $BACKUP_PATH"
} else {
Invoke-Adb root | Out-Null
Invoke-Adb wait-for-device | Out-Null
# Keep a copy of the current file on the PC (never overwrite an existing one)
New-Item -ItemType Directory -Force -Path $LOCAL_BACKUP_DIR | Out-Null
$stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$localBackup = Join-Path $LOCAL_BACKUP_DIR ("ipconfig-$Serial-$stamp.txt")
& $script:Adb -s $Serial pull $CONFIG_PATH $localBackup | Out-Null
if (Test-Path $localBackup) {
Write-Host "[backup] $localBackup ($((Get-Item $localBackup).Length) bytes)"
} else {
Write-Host '[backup] no existing file on device'
}
# Never clobber an earlier backup - it holds the factory STATIC record.
# The existence check runs on the host: shell operators such as "||" inside an
# adb shell one-liner are not reliably preserved by the Windows argument handling.
$probe = (& $script:Adb -s $Serial shell "ls $BACKUP_PATH") -join ''
if ($probe -notmatch 'ipconfig') {
Invoke-Adb shell "cp -a $CONFIG_PATH $BACKUP_PATH" | Out-Null
Write-Host "[backup] $BACKUP_PATH (on device, factory default)"
} else {
Write-Host "[backup] $BACKUP_PATH (on device, already exists - kept)"
}
Invoke-Adb remount | Out-Null
$tmp = Join-Path $env:TEMP 'ipconfig-dhcp.txt'
[IO.File]::WriteAllBytes($tmp, (New-DhcpConfigBytes))
Invoke-Adb push $tmp $CONFIG_PATH | Out-Null
Invoke-Adb shell "chmod 644 $CONFIG_PATH" | Out-Null
Invoke-Adb shell "chown root:root $CONFIG_PATH" | Out-Null
Invoke-Adb shell "chcon u:object_r:system_file:s0 $CONFIG_PATH" | Out-Null
Invoke-Adb shell "sync" | Out-Null
Write-Host "[ok] wrote DHCP record to $CONFIG_PATH"
}
if ($NoReboot) {
Write-Host '[done] reboot skipped - run "adb reboot" or re-run without -NoReboot to apply'
exit 0
}
Invoke-Adb reboot | Out-Null
Write-Host '[reboot] waiting for boot_completed ...'
Invoke-Adb wait-for-device | Out-Null
& $script:Adb -s $Serial shell 'while ! getprop sys.boot_completed | grep -q 1; do sleep 2; done; echo READY' | Out-Null
Write-Host '[reboot] up'
# The PHY needs ~20 s to negotiate the link and DHCP runs right after that,
# so poll before reporting instead of printing a half-initialised state.
$deadline = (Get-Date).AddSeconds(90)
do {
Start-Sleep -Seconds 3
$report = & $script:Adb -s $Serial shell dumpsys ethernet
$text = $report -join "`n"
if ($text -match 'mEthernetCurrentState: CONNECTED') { break }
} while ((Get-Date) -lt $deadline)
$report | Select-String -Pattern 'Current Ethernet state', 'Link state', 'LinkAddresses', 'IP assignment' -Context 0, 1