#!/usr/bin/env python3
"""Read SMART from a USB-SATA drive on macOS without a kernel extension.

Why this exists: macOS exposes no SMART/ATA pass-through for USB disks, so
smartctl fails with "Operation not supported by device". This script bypasses
the macOS storage driver and talks to the enclosure's bridge chip directly.

How it works (as proven on a Seagate Expansion 0bc2:231a, macOS 27, Apple Silicon):
 1. Unmount the volume, then as root call libusb_detach_kernel_driver. On macOS
    this "captures" the device (USBDeviceReEnumerate with the capture flag):
    the Apple mass-storage driver lets go, but the device is NOT reset and
    stays in the UAS alternate setting the kernel had selected.
 2. Stay in UAS (USB Attached SCSI, interface protocol 0x62). Bulk-Only mode
    (protocol 0x50) never accepted a command on this bridge under macOS.
 3. Allocate USB 3 streams on the status / data-in / data-out pipes
    (libusb_alloc_streams), then for each command: queue async stream reads
    for the status and data pipes, and write a 32-byte UAS Command IU to the
    command pipe. pyusb has no stream API, so that part is raw ctypes.
 4. The SCSI command carried is SAT ATA PASS-THROUGH(16) (opcode 0x85), which
    the bridge translates to ATA: IDENTIFY, SMART READ DATA / THRESHOLDS /
    RETURN STATUS, and log reads. Read-only commands only.
 5. libusb_attach_kernel_driver hands the device back and macOS remounts it.

Setup:  brew install libusb && uv venv venv && uv pip install --python venv/bin/python pyusb
Usage:  sudo venv/bin/python usb_smart.py [--probe]
        (run in a real terminal: sudo needs a TTY for the password)
Edit VID, PID and MOUNT below for another drive. Raw logs go to smart_raw.json."""
import struct, subprocess, sys, os, time, traceback
import ctypes as C

LIBPATH = "/opt/homebrew/lib/libusb-1.0.dylib"
def die(msg): print(msg, flush=True); sys.exit(1)
LOG = os.path.join(os.path.dirname(os.path.abspath(__file__)), "usb_smart_debug.log")
ROOT = os.geteuid() == 0
if ROOT and "--probe" not in sys.argv:
    os.environ["LIBUSB_DEBUG"] = "4"
    sys.stdout.reconfigure(line_buffering=True)
    os.dup2(os.open(LOG, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o644), 2)
def note(s): os.write(2, f"\n##### {s}\n".encode())
import usb.core, usb.util, usb.backend.libusb1
USBError = usb.core.USBError

VID, PID, MOUNT = 0x0BC2, 0x231A, "/Volumes/MyDrive"
NAMES = {1:"Raw_Read_Error_Rate",3:"Spin_Up_Time",4:"Start_Stop_Count",5:"Reallocated_Sector_Ct",
7:"Seek_Error_Rate",9:"Power_On_Hours",10:"Spin_Retry_Count",12:"Power_Cycle_Count",
183:"Runtime_Bad_Block",184:"End-to-End_Error",187:"Reported_Uncorrect",188:"Command_Timeout",
189:"High_Fly_Writes",190:"Airflow_Temperature",191:"G-Sense_Error_Rate",192:"Power-Off_Retract_Ct",
193:"Load_Cycle_Count",194:"Temperature_Celsius",195:"Hardware_ECC_Recovered",
197:"Current_Pending_Sector",198:"Offline_Uncorrectable",199:"UDMA_CRC_Error_Count",
240:"Head_Flying_Hours",241:"Total_LBAs_Written",242:"Total_LBAs_Read"}

be = usb.backend.libusb1.get_backend(find_library=lambda x: LIBPATH)
dev = usb.core.find(idVendor=VID, idProduct=PID, backend=be)
if dev is None: die("drive not found on USB")
cfg = dev[0]
bot = uas = None
for intf in cfg:
    print(f"interface {intf.bInterfaceNumber} alt {intf.bAlternateSetting} proto {intf.bInterfaceProtocol:#x}")
    if intf.bInterfaceClass == 8 and intf.bInterfaceProtocol == 0x50: bot = intf
    if intf.bInterfaceClass == 8 and intf.bInterfaceProtocol == 0x62: uas = intf
ifn = (uas or bot).bInterfaceNumber

# UAS pipe ids from pipe-usage descriptors (0x24): 1 cmd, 2 status, 3 data-in, 4 data-out
upipe = {}
if uas is not None:
    for ep in uas:
        x = list(ep.extra_descriptors); i = 0; pid = None; ss = None
        while i + 1 < len(x) and x[i]:
            if x[i+1] == 0x24: pid = x[i+2]
            if x[i+1] == 0x30: ss = x[i+3] & 0x1F
            i += x[i]
        print(f"  uas ep {ep.bEndpointAddress:#04x} pipe id {pid} max-streams 2^{ss}")
        if pid: upipe[pid] = ep.bEndpointAddress
if bot is not None:
    def _ep(d): return usb.util.find_descriptor(bot, custom_match=lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == d and usb.util.endpoint_type(e.bmAttributes) == usb.util.ENDPOINT_TYPE_BULK).bEndpointAddress
    ep_in, ep_out = _ep(usb.util.ENDPOINT_IN), _ep(usb.util.ENDPOINT_OUT)
    print(f"  bot ep in {ep_in:#04x} out {ep_out:#04x}")
if "--probe" in sys.argv: die("probe ok")
if not ROOT: die("run with sudo")

# ---------- raw libusb bits pyusb does not expose (streams, async) ----------
L = C.CDLL(LIBPATH)
class Xfer(C.Structure): pass
CB = C.CFUNCTYPE(None, C.POINTER(Xfer))
Xfer._fields_ = [("dev_handle", C.c_void_p), ("flags", C.c_uint8), ("endpoint", C.c_uint8), ("type", C.c_uint8),
                 ("timeout", C.c_uint), ("status", C.c_int), ("length", C.c_int), ("actual_length", C.c_int),
                 ("callback", CB), ("user_data", C.c_void_p), ("buffer", C.c_void_p), ("num_iso_packets", C.c_int)]
class Timeval(C.Structure): _fields_ = [("tv_sec", C.c_long), ("tv_usec", C.c_int32)]
L.libusb_alloc_transfer.restype = C.POINTER(Xfer); L.libusb_alloc_transfer.argtypes = [C.c_int]
L.libusb_submit_transfer.argtypes = [C.POINTER(Xfer)]
L.libusb_cancel_transfer.argtypes = [C.POINTER(Xfer)]
L.libusb_free_transfer.argtypes = [C.POINTER(Xfer)]; L.libusb_free_transfer.restype = None
L.libusb_transfer_set_stream_id.argtypes = [C.POINTER(Xfer), C.c_uint32]; L.libusb_transfer_set_stream_id.restype = None
L.libusb_alloc_streams.argtypes = [C.c_void_p, C.c_uint32, C.c_char_p, C.c_int]
L.libusb_free_streams.argtypes = [C.c_void_p, C.c_char_p, C.c_int]
L.libusb_handle_events_timeout.argtypes = [C.c_void_p, C.POINTER(Timeval)]
def hval(): return dev._ctx.handle.handle.value
done = {}
@CB
def cb(tp):
    x = tp.contents; done[x.user_data] = (x.status, x.actual_length)
def pump(cond, secs):
    end = time.time() + secs; tv = Timeval(0, 100000)
    while not cond() and time.time() < end: L.libusb_handle_events_timeout(be.ctx, C.byref(tv))
    return cond()

# ---------- transports: each returns (data, scsi_status, sense) ----------
TAG = 1
def uas_cmd(cdb, nin, tmo=8000):
    note(f"UAS CDB {cdb.hex()} len {nin}")
    done.clear(); live = {}
    sbuf = C.create_string_buffer(256); dbuf = C.create_string_buffer(max(nin, 1))
    def sub(ep, buf, n, ident):
        t = L.libusb_alloc_transfer(0); x = t.contents
        x.dev_handle = hval(); x.endpoint = ep; x.type = 4; x.timeout = tmo
        x.buffer = C.cast(buf, C.c_void_p); x.length = n; x.callback = cb; x.user_data = ident
        L.libusb_transfer_set_stream_id(t, TAG)
        rc = L.libusb_submit_transfer(t)
        if rc: L.libusb_free_transfer(t); raise USBError(f"submit ep {ep:#x}: libusb rc {rc}")
        live[ident] = t
    try:
        sub(upipe[2], sbuf, 256, 1)
        if nin: sub(upipe[3], dbuf, nin, 2)
        iu = bytes([1, 0, 0, TAG, 0, 0, 0, 0]) + bytes(8) + cdb.ljust(16, b"\0")
        try: dev.write(upipe[1], iu, tmo)
        except USBError as e: raise USBError(f"command IU write: {e}")
        if not pump(lambda: 1 in done, tmo / 1000 + 1): raise USBError("status IU never arrived")
        st, n = done[1]
        if st != 0: raise USBError(f"status transfer failed, libusb status {st}")
        s = sbuf.raw[:n]
        if nin: pump(lambda: 2 in done, 1)
        data = dbuf.raw[:done[2][1]] if nin and 2 in done and done[2][0] == 0 else b""
        if len(s) < 16 or s[0] != 3: raise USBError(f"unexpected IU {s[:24].hex()}")
        return data, s[6], s[16:16 + int.from_bytes(s[14:16], "big")]
    finally:
        for i, t in live.items():
            if i not in done: L.libusb_cancel_transfer(t)
        pump(lambda: all(i in done for i in live), 3)
        for i, t in live.items():
            if i in done: L.libusb_free_transfer(t)

btag = 0
def bot_raw(cdb, nin, tmo):
    global btag; btag += 1
    note(f"BOT CDB {cdb.hex()} len {nin}")
    try: dev.write(ep_out, struct.pack("<IIIBBB", 0x43425355, btag, nin, 0x80, 0, len(cdb)) + cdb.ljust(16, b"\0"), tmo)
    except USBError as e: raise USBError(f"CBW write: {e}")
    data = b""
    if nin:
        try: data = bytes(dev.read(ep_in, nin, tmo))
        except USBError as e: print("  data phase:", e); dev.clear_halt(ep_in)
    try: csw = bytes(dev.read(ep_in, 13, tmo))
    except USBError: dev.clear_halt(ep_in); csw = bytes(dev.read(ep_in, 13, tmo))
    return data, csw[12]
def bot_cmd(cdb, nin, tmo=8000):
    data, st = bot_raw(cdb, nin, tmo); sense = b""
    if st: sense, _ = bot_raw(bytes([0x03, 0, 0, 0, 64, 0]), 64, tmo)
    return data, st, sense

# ---------- SMART over either transport ----------
def s(b): return bytes(b[i ^ 1] for i in range(len(b))).decode("ascii", "replace").strip()
def ata_regs(sense):
    if len(sense) >= 10 and sense[0] & 0x7F in (0x72, 0x73):
        i = 8
        while i + 1 < len(sense):
            if sense[i] == 0x09: return sense[i:i + 14]
            i += sense[i + 1] + 2
    return None
def do_smart(x):
    def ata_in(feature, cmd, smart=True):
        mid, hi = (0x4F, 0xC2) if smart else (0, 0)
        for name, cdb in (("ATA16", bytes([0x85, 0x08, 0x0E, 0, feature, 0, 1, 0, 0, 0, mid, 0, hi, 0, cmd, 0])),
                          ("ATA12", bytes([0xA1, 0x08, 0x0E, feature, 1, 0, mid, hi, 0, cmd, 0, 0]))):
            d, st, sense = x(cdb, 512)
            if st == 0 and len(d) == 512: return d
            print(f"  {name} cmd {cmd:#x}/{feature:#x}: status {st} len {len(d)} sense {sense.hex()}")
        return None
    d, st, sense = x(bytes([0x12, 0, 0, 0, 36, 0]), 36)
    print(f"  INQUIRY status {st}: {d[8:36].decode('ascii', 'replace') if len(d) >= 36 else d.hex()}")
    if len(d) < 36: return False
    try: print("  START UNIT status", x(bytes([0x1B, 0, 0, 0, 1, 0]), 0, 25000)[1])
    except USBError as e: print("  START UNIT:", e)
    idn = ata_in(0, 0xEC, smart=False)
    if idn: print(f"\nModel: {s(idn[54:94])}\nSerial: {s(idn[20:40])}\nFirmware: {s(idn[46:54])}")
    else: print("  IDENTIFY failed")
    data = ata_in(0xD0, 0xB0)
    if not data: print("  SMART READ DATA failed: bridge refuses ATA passthrough on this transport"); return False
    thr = ata_in(0xD1, 0xB0)
    try:
        _, st, sense = x(bytes([0x85, 0x06, 0x20, 0, 0xDA, 0, 0, 0, 0, 0, 0x4F, 0, 0xC2, 0, 0xB0, 0]), 0)
        r = ata_regs(sense)
        if r: print("Overall health:", {(0x4F, 0xC2): "PASSED", (0xF4, 0x2C): "FAILING"}.get((r[9], r[11]), f"unknown {r.hex()}"))
        else: print("Overall health: registers not returned; sense", sense.hex())
    except USBError as e: print("Overall health:", e)
    th = {thr[2 + i * 12]: thr[3 + i * 12] for i in range(30)} if thr else {}
    print(f"\n{'ID':>3} {'ATTRIBUTE':<24}{'VAL':>4}{'WORST':>6}{'THR':>5}  RAW")
    for i in range(30):
        e = data[2 + i * 12: 14 + i * 12]
        if e[0] == 0: continue
        raw = int.from_bytes(e[5:11], "little")
        shown = raw & 0xFFFFFFFF if e[0] in (9, 240) else raw & 0xFF if e[0] in (190, 194) else raw  # vendors pack extra data in the high bytes
        print(f"{e[0]:>3} {NAMES.get(e[0], '?'):<24}{e[3]:>4}{e[4]:>6}{th.get(e[0], 0):>5}  {shown}  (0x{raw:012x})")
    blobs = {"identify": idn.hex() if idn else "", "smart_data": data.hex(), "smart_thresholds": thr.hex() if thr else ""}
    def grab(name, cdb, n=512):
        d, st, sense = x(cdb, n)
        if st == 0 and len(d) == n: blobs[name] = d.hex(); return True
        print(f"  {name}: not available (status {st}, sense {sense.hex()[:36]})"); return False
    def smart_log(addr): return bytes([0x85, 0x08, 0x0E, 0, 0xD5, 0, 1, 0, addr, 0, 0x4F, 0, 0xC2, 0, 0xB0, 0])
    def gp_log(addr, page): return bytes([0x85, 0x09, 0x0E, 0, 0, 0, 1, 0, addr, 0, page, 0, 0, 0, 0x2F, 0])
    try:
        print("\nextra logs:")
        for name, cdb in ([("smartlog_01_errors", smart_log(0x01)), ("smartlog_06_selftest", smart_log(0x06)),
                           ("smartlog_e0_sct_status", smart_log(0xE0)), ("gpl_00_directory", gp_log(0x00, 0)),
                           ("gpl_03_ext_errors", gp_log(0x03, 0)), ("gpl_07_ext_selftest", gp_log(0x07, 0))]
                          + [(f"gpl_04_devstats_p{p}", gp_log(0x04, p)) for p in (0, 1, 2, 3, 4, 5, 6, 7)]
                          + [(f"gpl_a6_seagate_farm_p{p}", gp_log(0xA6, p)) for p in range(8)]):
            if grab(name, cdb): print(f"  {name}: ok")
    except USBError as e: print("  extra logs stopped:", e)
    import json
    out = os.path.join(os.path.dirname(os.path.abspath(__file__)), "smart_raw.json")
    with open(out, "w") as f: json.dump(blobs, f, indent=1)
    os.chmod(out, 0o644); print(f"  raw data saved to {out} ({len(blobs)} blobs)")
    return True

def setup(alt, port_reset=False, set_cfg=False):
    note(f"setup alt={alt.bAlternateSetting} port_reset={port_reset} set_cfg={set_cfg}")
    try: usb.util.release_interface(dev, ifn)
    except Exception: pass
    steps = []
    if port_reset: steps.append(("port reset", lambda: (dev.reset(), time.sleep(4))))
    if set_cfg: steps.append(("set_configuration", lambda: dev.set_configuration(cfg.bConfigurationValue)))
    steps += [("claim", lambda: usb.util.claim_interface(dev, ifn)),
              ("altsetting", lambda: dev.set_interface_altsetting(interface=ifn, alternate_setting=alt.bAlternateSetting))]
    for name, fn in steps:
        try: fn()
        except Exception as e: print(f"  {name}: {e}")
    time.sleep(1)
    try: print("  device reports alt setting", bytes(dev.ctrl_transfer(0x81, 0x0A, 0, ifn, 1, 3000)).hex())
    except Exception as e: print("  get_interface:", e)

def try_uas(port_reset):
    setup(uas, port_reset=port_reset, set_cfg=port_reset)
    eps = bytes([upipe[2], upipe[3], upipe[4]])
    n = L.libusb_alloc_streams(hval(), 4, eps, len(eps))
    print("  streams allocated:", n)
    if n < 1: return False
    try: return do_smart(uas_cmd)
    finally: L.libusb_free_streams(hval(), eps, len(eps))
def try_bot(port_reset):
    setup(bot, port_reset=port_reset, set_cfg=True)
    try: dev.ctrl_transfer(0x21, 0xFF, 0, ifn, None, 5000)
    except Exception as e: print("  bot reset:", e)
    for ep in (ep_in, ep_out):
        try: print(f"  ep {ep:#04x} status", bytes(dev.ctrl_transfer(0x82, 0, 0, ep, 2, 3000)).hex())
        except Exception as e: print(f"  ep {ep:#04x} status:", e)
    return do_smart(lambda c, n, t=5000: bot_cmd(c, n, t))

if os.path.ismount(MOUNT):
    r = subprocess.run(["diskutil", "unmount", MOUNT], capture_output=True, text=True)
    print(r.stdout.strip() or r.stderr.strip())
    if r.returncode: die("could not unmount; close files on the drive and retry")
ok = False
try:
    active = dev.is_kernel_driver_active(ifn); print("kernel driver active:", active)
    if active: dev.detach_kernel_driver(ifn); print("detached")
    time.sleep(2)
    plans = []
    if uas is not None and len(upipe) == 4: plans += [("UAS, as the kernel left it", lambda: try_uas(False)), ("UAS after port reset", lambda: try_uas(True))]
    if bot is not None: plans += [("Bulk-Only after port reset", lambda: try_bot(True))]
    for name, fn in plans:
        print(f"\n=== {name}")
        try:
            if fn(): ok = True; break
        except Exception as e:
            print(f"  failed: {e}"); traceback.print_exc(file=sys.stderr)
finally:
    try:
        try: usb.util.release_interface(dev, ifn)
        except Exception: pass
        dev.attach_kernel_driver(ifn)
        print("\nkernel driver reattached; the disk should remount (unplug and replug if it does not)")
    except Exception as e:
        print("\nreattach:", e, "- unplug and replug the drive to remount it")
print("SMART read OK" if ok else "no SMART data; debug log: " + LOG)
