Files
mi8-platina-linux/scripts/complete_platina_bringup.py
shenlei 530a3690da
Offline project checks / check (push) Canceled after 0s
feat: add WCN3990 Wi-Fi bringup service, battery protection, and bringup automation
- Add phone-dsp.openrc to initialize Hexagon DSP, rmtfs, pd-mapper, and ath10k_snoc
- Update phone-battery-guard threshold to 80% with power supply node fallbacks
- Enhance phone-cellular-setup with battery gate checks, RF locks, and unit tests
- Include bringup, flashing, and inspection automation scripts for platina
- Update candidate IPA v2.6 patches and package locks
2026-11-12 14:54:23 +09:00

253 lines
9.5 KiB
Python

#!/usr/bin/env python3
"""
Complete Platina bringup:
1. Wait for Fastboot mode
2. Boot patched debug image to initramfs (no beebzzr, no UDC glitch)
3. Apply foolproof modprobe disarm (install ... /bin/true)
4. Setup persistent OpenRC USB gadget service (replacing broken usb-moded)
5. Deploy updated platina-cellular with band lock and battery gate
6. Continue boot into OpenRC userspace
7. Connect via SSH and execute cellular data bringup + EasyTier mesh
"""
import http.server
import os
import re
import socket
import socketserver
import subprocess
import sys
import threading
import time
TARGET_IP = "172.16.42.1"
HOST_IP = "172.16.42.2"
HTTP_PORT = 9878
SSH_KEY = "/Users/shenlei/Work/sshKey/oracle_tokyo_ARM.key"
DEBUG_BOOT_IMG = "artifacts/candidate-20261110-r17-cellular/boot-patched-r17.img"
def log(msg):
print(f"[{time.strftime('%H:%M:%S')}] {msg}", flush=True)
class QuietHandler(http.server.SimpleHTTPRequestHandler):
def log_message(self, format, *args):
pass
def start_http_server():
server = socketserver.TCPServer(("0.0.0.0", HTTP_PORT), QuietHandler)
t = threading.Thread(target=server.serve_forever, daemon=True)
t.start()
return server
def find_usb_net_interface():
res = subprocess.run(["ifconfig"], capture_output=True, text=True)
for block in res.stdout.split("\n\n"):
if "172.16.42." in block and "status: active" in block:
m = re.match(r"^([a-z0-9]+):", block)
if m:
return m.group(1)
return None
def telnet_read_until_prompt(s, timeout=15):
s.settimeout(timeout)
buf = ""
start = time.time()
while time.time() - start < timeout:
try:
chunk = s.recv(4096).decode("utf-8", errors="replace")
if not chunk:
break
buf += chunk
if "~ # " in buf or "/ # " in buf:
return buf
except socket.timeout:
break
return buf
def telnet_cmd(s, cmd, timeout=15):
log(f"Telnet -> {cmd}")
s.sendall((cmd + "\n").encode("utf-8"))
out = telnet_read_until_prompt(s, timeout)
log(f"Telnet <- {out.strip()}")
return out
def run_ssh(cmd, timeout=30):
ssh_cmd = [
"ssh",
"-o", "StrictHostKeyChecking=no",
"-o", "UserKnownHostsFile=/dev/null",
"-o", "ConnectTimeout=5",
"-i", SSH_KEY,
f"server@{TARGET_IP}",
cmd
]
res = subprocess.run(ssh_cmd, capture_output=True, text=True, timeout=timeout)
return res.returncode, res.stdout, res.stderr
def wait_for_fastboot():
log("Waiting for Fastboot device (63c36adb)...")
while True:
res = subprocess.run(["fastboot", "devices"], capture_output=True, text=True)
for line in res.stdout.strip().splitlines():
if "fastboot" in line:
serial = line.split()[0]
log(f"Found Fastboot device: {serial}")
return serial
time.sleep(1)
def main():
serial = wait_for_fastboot()
res = subprocess.run(["fastboot", "-s", serial, "getvar", "battery-voltage"], capture_output=True, text=True)
log(f"Battery Voltage: {res.stderr.strip() or res.stdout.strip()}")
log(f"Booting {DEBUG_BOOT_IMG} (patched: no beebzzr, no UDC glitch)...")
res = subprocess.run(["fastboot", "-s", serial, "boot", DEBUG_BOOT_IMG], capture_output=True, text=True)
if res.returncode != 0:
log(f"Boot failed: {res.stderr}")
sys.exit(1)
log("Debug kernel booted! Waiting for initramfs USB network and telnet port 23...")
# Wait for USB network interface
start = time.time()
iface = None
while time.time() - start < 45:
iface = find_usb_net_interface()
if iface:
log(f"Host interface {iface} is ACTIVE with 172.16.42.x!")
break
time.sleep(1)
# Wait for Telnet
start = time.time()
telnet_ready = False
while time.time() - start < 45:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1.0)
try:
s.connect((TARGET_IP, 23))
s.close()
log("SUCCESS: Telnet port 23 is OPEN!")
telnet_ready = True
break
except Exception:
pass
time.sleep(1)
if not telnet_ready:
log("ERROR: Timed out waiting for Telnet port 23.")
sys.exit(1)
http_srv = start_http_server()
log(f"HTTP server started on port {HTTP_PORT}")
# Connect interactive telnet session
ts = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ts.settimeout(10.0)
ts.connect((TARGET_IP, 23))
telnet_read_until_prompt(ts, timeout=5)
# Check battery voltage from kernel
telnet_cmd(ts, "cat /sys/class/power_supply/qcom-battery/voltage_now || true")
# Mount /sysroot cleanly
telnet_cmd(ts, "mount_root_partition 2>/dev/null || mount /dev/mmcblk1p79 /sysroot 2>/dev/null")
# 1. Foolproof modprobe install /bin/true disarm
telnet_cmd(ts, "mkdir -p /sysroot/etc/modprobe.d")
telnet_cmd(ts, "echo 'install qcom_q6v5_mss /bin/true' > /sysroot/etc/modprobe.d/modem-blacklist.conf")
telnet_cmd(ts, "echo 'install ipa /bin/true' >> /sysroot/etc/modprobe.d/modem-blacklist.conf")
telnet_cmd(ts, "echo 'blacklist qcom_q6v5_mss' >> /sysroot/etc/modprobe.d/modem-blacklist.conf")
telnet_cmd(ts, "echo 'blacklist ipa' >> /sysroot/etc/modprobe.d/modem-blacklist.conf")
telnet_cmd(ts, "sed -i '/qcom_q6v5_mss/d' /sysroot/etc/modules-load.d/modem.conf 2>/dev/null || true")
telnet_cmd(ts, "sed -i '/ipa/d' /sysroot/etc/modules-load.d/modem.conf 2>/dev/null || true")
# 2. Disable ModemManager auto-activation
telnet_cmd(ts, "mv /sysroot/usr/share/dbus-1/system-services/org.freedesktop.ModemManager1.service /sysroot/usr/share/dbus-1/system-services/org.freedesktop.ModemManager1.service.disabled 2>/dev/null || true")
telnet_cmd(ts, "rm -f /sysroot/etc/runlevels/default/modemmanager 2>/dev/null || true")
# 3. Setup robust USB gadget service for OpenRC (replaces broken usb-moded)
telnet_cmd(ts, f"wget -O /tmp/setup_gadget.sh http://{HOST_IP}:{HTTP_PORT}/scripts/setup_usb_gadget_service.sh")
telnet_cmd(ts, "sh /tmp/setup_gadget.sh")
# 4. Deploy updated cellular script
telnet_cmd(ts, f"wget -O /sysroot/usr/local/sbin/platina-cellular http://{HOST_IP}:{HTTP_PORT}/scripts/phone-cellular-setup.py")
telnet_cmd(ts, "chmod +x /sysroot/usr/local/sbin/platina-cellular")
# 5. Clean sync, unmount and continue boot
telnet_cmd(ts, "sync")
telnet_cmd(ts, "umount /sysroot 2>/dev/null || true")
log("Triggering /sbin/pmos_continue_boot...")
ts.sendall(b"/sbin/pmos_continue_boot\n")
time.sleep(1)
ts.close()
http_srv.shutdown()
log("Continue boot triggered! Shut down HTTP server.")
# Wait for OpenRC userspace SSH
log("Waiting for OpenRC userspace boot (SSH port 22)...")
start = time.time()
ssh_ready = False
while time.time() - start < 60:
if find_usb_net_interface():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1.0)
try:
s.connect((TARGET_IP, 22))
s.close()
log(f"SUCCESS: SSH port 22 is OPEN after {int(time.time() - start)}s!")
ssh_ready = True
break
except Exception:
pass
time.sleep(1)
if not ssh_ready:
log("ERROR: Timed out waiting for SSH port 22.")
sys.exit(1)
time.sleep(3)
log("Testing SSH login to server@172.16.42.1...")
code, out, err = run_ssh("uname -a; uptime; cat /sys/class/power_supply/qcom-battery/voltage_now")
log(f"SSH test (code={code}):\n{out.strip()}\n{err.strip()}")
# Verify blacklisted modules are NOT loaded
code, out, err = run_ssh("lsmod | grep -E 'qcom_q6v5_mss|ipa'")
log(f"Check blacklisted modules (expect empty):\n{out.strip()}")
# Controlled bringup
log("\n--- Step 1: Check battery gate ---")
code, out, err = run_ssh("sudo /usr/local/sbin/platina-cellular --battery-check")
log(f"Battery gate:\n{out.strip()}")
log("\n--- Step 2: Start rmtfs and load modules safely ---")
code, out, err = run_ssh("sudo rc-service rmtfs start; sudo /usr/local/sbin/platina-cellular --load-modules")
log(f"Load modules:\n{out.strip()}")
time.sleep(3)
log("\n--- Step 3: Check IPA sysfs endpoints ---")
code, out, err = run_ssh("cat /sys/devices/platform/soc@0/14780000.ipa/modem/tx_endpoint_id 2>/dev/null; echo '---'; cat /sys/devices/platform/soc@0/14780000.ipa/modem/rx_endpoint_id 2>/dev/null")
log(f"IPA endpoints:\n{out.strip()}")
log("\n--- Step 4: Lock RF bands (LTE B1,B3) and bring online ---")
code, out, err = run_ssh("sudo /usr/local/sbin/platina-cellular --optimize-rf --rat lte --bands 1,3; sudo /usr/local/sbin/platina-cellular --online")
log(f"RF lock and online:\n{out.strip()}")
time.sleep(3)
log("\n--- Step 5: Activate cellular data ---")
code, out, err = run_ssh("sudo /usr/local/sbin/platina-cellular --auto --once")
log(f"Cellular data setup:\n{out.strip()}")
log("\n--- Step 6: Check ip route and ping ---")
code, out, err = run_ssh("ip route; ping -c 3 -I rmnet_ipa0 1.1.1.1 2>&1 || ping -c 3 1.1.1.1 2>&1")
log(f"Connectivity test:\n{out.strip()}")
log("\n--- Step 7: Check EasyTier mesh status ---")
code, out, err = run_ssh("sudo /usr/local/sbin/platina-mesh --status 2>&1")
log(f"EasyTier mesh status:\n{out.strip()}")
log("\n=== Controlled bringup completed successfully! ===")
if __name__ == "__main__":
main()