fix: USB re-enum on retry, Path B iRecovery rewrite, DFU diagnostic, tests

Path A (checkm8):
- Re-enumerate USB handle between exploit retries; prior code reused the
  stale handle after the device reset on a failed attempt, causing
  LIBUSB_ERROR_NO_DEVICE on attempts 2 and 3
- Add USB_RECONNECT_DELAY_USEC (2 s) constant; allocate exploit_ctx_t
  fresh per attempt so cleanup is symmetric with init

Path B (A12+ bypass):
- Replace broken SET_DESCRIPTOR serial write with libirecovery setenv;
  Apple A12+ BootROM always STALLs SET_DESCRIPTOR (LIBUSB_ERROR_PIPE)
- Add step_reboot_to_recovery(): DFU_ABORT -> poll for PID 0x1281
- Add step_reboot_to_normal(): irecv reboot -> poll idevice_get_device_list
- step_session_handshake() now runs after normal-mode reconnect so
  dev->handle is valid; previously called with NULL handle (DFU mode)
- Expand path_b_execute() from 9 to 10 steps

Device / DFU:
- usb_dfu_read_info(): emit specific diagnostic when serial is the bare
  Apple DFU string (CPID=0); includes per-model button-sequence guidance

start.sh:
- DFU branch: derive support status from binary output instead of
  hardcoding SUPPORTED; gate_support() now reachable from DFU path
- parse_device_info(): guard against empty output (binary crash / no
  lockdownd); print actionable advice, set DEV_STATUS=UNSUPPORTED
- WSL: detect missing usbipd and print passthrough guidance after first
  poll timeout
- Arch Linux: replace exit-1 with AUR install instructions

Tests / build:
- Add tests/test_sections.c: DFU serial parsing, chip DB lookup,
  bypass probe logic, retry/reconnect constants (26 tests, no hardware)
- Add `make test` target; test build excludes USB/lockdown modules

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
tr4m0ryp 2026-04-18 16:24:49 +02:00
parent ee4e7c56fd
commit 5b758931ac
13 changed files with 1388 additions and 178 deletions

View file

@ -139,9 +139,7 @@ int checkm8_verify_pwned(device_info_t *dev)
int checkm8_exploit(device_info_t *dev)
{
exploit_ctx_t ctx;
int attempt;
int pwned;
if (!dev) {
log_error("checkm8_exploit: NULL device");
@ -150,42 +148,56 @@ int checkm8_exploit(device_info_t *dev)
log_info("checkm8_exploit: starting exploit on CPID 0x%04X", dev->cpid);
if (exploit_init(&ctx, dev) != 0)
return -1;
for (attempt = 1; attempt <= MAX_EXPLOIT_TRIES; attempt++) {
log_info("checkm8_exploit: attempt %d/%d", attempt,
MAX_EXPLOIT_TRIES);
exploit_ctx_t ctx;
int pwned;
if (exploit_deliver(&ctx) != 0) {
log_warn("checkm8_exploit: attempt %d failed, retrying...",
attempt);
usleep(STAGE_DELAY_USEC * 10);
/* On retries, the device USB-resets after a failed attempt.
* Close the stale handle and re-enumerate before trying again. */
if (attempt > 1) {
if (dev->usb) {
usb_dfu_close(dev->usb);
dev->usb = NULL;
}
log_info("checkm8_exploit: waiting for device reset...");
usleep(USB_RECONNECT_DELAY_USEC);
if (usb_dfu_find(&dev->usb) != 0) {
log_error("checkm8_exploit: DFU device lost after attempt %d, cannot retry",
attempt - 1);
return -1;
}
log_info("checkm8_exploit: USB handle re-acquired for retry %d", attempt);
}
log_info("checkm8_exploit: attempt %d/%d", attempt, MAX_EXPLOIT_TRIES);
if (exploit_init(&ctx, dev) != 0) {
log_warn("checkm8_exploit: init failed on attempt %d", attempt);
exploit_cleanup(&ctx);
continue;
}
if (exploit_deliver(&ctx) != 0) {
log_warn("checkm8_exploit: attempt %d failed, retrying...", attempt);
exploit_cleanup(&ctx);
continue;
}
/* Verify the exploit worked */
pwned = checkm8_verify_pwned(dev);
exploit_cleanup(&ctx);
if (pwned == 1) {
log_info("checkm8_exploit: success on attempt %d", attempt);
ctx.phase = EXPLOIT_PHASE_DONE;
exploit_cleanup(&ctx);
return 0;
}
if (pwned < 0) {
log_error("checkm8_exploit: verification error on attempt %d",
attempt);
} else {
log_warn("checkm8_exploit: not pwned after attempt %d",
attempt);
}
usleep(STAGE_DELAY_USEC * 10);
if (pwned < 0)
log_error("checkm8_exploit: verification error on attempt %d", attempt);
else
log_warn("checkm8_exploit: not pwned after attempt %d", attempt);
}
log_error("checkm8_exploit: failed after %d attempts",
MAX_EXPLOIT_TRIES);
exploit_cleanup(&ctx);
log_error("checkm8_exploit: failed after %d attempts", MAX_EXPLOIT_TRIES);
return -1;
}