#!/usr/bin/env python3 """Dump Sahara memory-debug regions from a crashed 2720 over EDL. No stock fastboot is needed. Power-cycle the phone into 05c6:900e first so a fresh Sahara HELLO is available, then run this script. Output goes to backup/crash-/. The memory table lists aboot/TZ crash regions; `strings` on the dumps usually reveals the last error messages. """ import argparse import os import struct import sys import time from pathlib import Path BASE_DIR = Path(__file__).resolve().parent.parent sys.path.insert(0, str(BASE_DIR / ".flash-deps")) import usb.core # noqa: E402 import usb.util # noqa: E402 VID = 0x05C6 PIDS = (0x900E, 0x9008, 0x9006) CMD_HELLO_RSP = 0x2 CMD_SWITCH_MODE = 0xC CMD_MEMORY_READ = 0xA CMD_MEMORY_DEBUG = 0x9 MODE_MEMORY_DEBUG = 0x2 MODE_COMMAND = 0x3 TABLE_ENTRY_SIZE = 3 * 4 + 20 + 20 def find_device(): for pid in PIDS: dev = usb.core.find(idVendor=VID, idProduct=pid) if dev: return dev, pid return None, None def endpoints(dev): try: if dev.is_kernel_driver_active(0): dev.detach_kernel_driver(0) except Exception: pass try: dev.set_configuration() except Exception: pass try: usb.util.claim_interface(dev, 0) except Exception: pass epin = epout = None for intf in dev.get_active_configuration(): for ep in intf: if ep.bmAttributes != 2: continue direction = usb.util.endpoint_direction(ep.bEndpointAddress) if direction == usb.util.ENDPOINT_IN and epin is None: epin = ep if direction == usb.util.ENDPOINT_OUT and epout is None: epout = ep return epin, epout def main(): parser = argparse.ArgumentParser() parser.add_argument("--out", default="") parser.add_argument("--timeout", type=int, default=8) args = parser.parse_args() dev, pid = find_device() if not dev: print("FAIL: no Qualcomm EDL device (05c6:900e/9008/9006)") return 1 print("device", hex(pid)) epin, epout = endpoints(dev) if not epin or not epout: print("FAIL: no bulk endpoints") return 1 def read(n=0x1000, t=None): return bytes(dev.read(epin.bEndpointAddress, n, timeout=t or args.timeout * 1000)) def write(data): try: return dev.write(epout.bEndpointAddress, data, timeout=args.timeout * 1000) except Exception as exc: print("FAIL: USB write failed (%s). Power-cycle the phone to get a " "fresh Sahara HELLO." % exc) raise SystemExit(1) outdir = Path(args.out) if args.out else ( BASE_DIR / "backup" / ("crash-" + time.strftime("%Y%m%d-%H%M%S"))) outdir.mkdir(parents=True, exist_ok=True) # Ask the target to leave memory-debug wait state and re-send HELLO. write(struct.pack("<3I", CMD_SWITCH_MODE, 0xC, MODE_COMMAND)) try: hello = read(0x1000) except Exception as exc: print("FAIL: no fresh Sahara HELLO (%s). Power-cycle the phone." % exc) return 1 if len(hello) < 48: print("FAIL: short HELLO (%d bytes)" % len(hello)) return 1 fields = struct.unpack_from("<12I", hello) print("HELLO cmd=%#x len=%d version=%d mode=%#x" % (fields[0], fields[1], fields[2], fields[5])) (outdir / "hello.bin").write_bytes(hello) if fields[5] == 0: print("Sahara mode 0 = IMAGE_TX_PENDING (plain EDL, waiting for a " "programmer), not memory-debug.") print("No crash dump is available in this state. The phone did not " "enter the crash-dump path; use flash-edl.sh normally.") return 0 if fields[5] != MODE_MEMORY_DEBUG: print("Sahara mode %#x is not memory-debug; no dump available." % fields[5]) return 0 write(struct.pack("<12I", CMD_HELLO_RSP, 0x30, 2, 1, 0, MODE_MEMORY_DEBUG, 0, 0, 0, 0, 0, 0)) try: rsp = read(0x1000) except Exception as exc: print("FAIL: no memory-debug response (%s)" % exc) return 1 if len(rsp) < 16: print("FAIL: short memory-debug response") return 1 cmd, length = struct.unpack_from("<2I", rsp) print("response cmd=%#x len=%d" % (cmd, length)) if cmd != CMD_MEMORY_DEBUG: print("FAIL: target did not enter memory debug (cmd=%#x)" % cmd) return 1 table_addr, table_len = struct.unpack_from("<2I", rsp, 8) print("memory table %#x / %#x bytes" % (table_addr, table_len)) if not table_addr or not table_len or table_len > 0x100000: print("FAIL: implausible memory table") return 1 pos = 0 table = b"" while pos < table_len: n = min(0x800, table_len - pos) write(struct.pack("<4I", CMD_MEMORY_READ, 16, table_addr + pos, n)) try: chunk = read(n) except Exception as exc: print("FAIL reading table at %#x: %s" % (pos, exc)) return 1 table += chunk[:n] pos += len(chunk) (outdir / "memory_table.bin").write_bytes(table) for i in range(0, len(table) - TABLE_ENTRY_SIZE + 1, TABLE_ENTRY_SIZE): save, base, size = struct.unpack_from("<3I", table, i) desc = table[i + 12:i + 32].rstrip(b"\0").decode("utf-8", "replace") name = table[i + 32:i + 52].rstrip(b"\0").decode("utf-8", "replace") idx = i // TABLE_ENTRY_SIZE print("region %d save=%#x base=%#x size=%#x desc=%r file=%r" % (idx, save, base, size, desc, name)) if not base or not size or size > 0x1000000: continue pos = 0 blob = b"" while pos < size: n = min(0x800, size - pos) write(struct.pack("<4I", CMD_MEMORY_READ, 16, base + pos, n)) try: chunk = read(n) except Exception: break blob += chunk[:n] pos += len(chunk) safe = (name or ("region%d" % idx)).replace("/", "_") (outdir / (safe + ".bin")).write_bytes(blob) print(" dumped %s (%d bytes)" % (safe, len(blob))) print("done:", outdir) return 0 if __name__ == "__main__": sys.exit(main())