diff --git a/Makefile b/Makefile index 90ed937..fa648c0 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ SRC = $(shell pwd) DEP = $(SRC)/dep_root CC = cc -CFLAGS += -mmacosx-version-min=10.8 -I$(DEP)/include -I/opt/procursus/include -I$(SRC)/include -I$(SRC) -Os -Wall -Wextra +CFLAGS += -mmacosx-version-min=10.8 -I$(DEP)/include -I/opt/procursus/include -I$(SRC)/include -I$(SRC) -O0 -g -Wall -Wextra LIBS = $(DEP)/lib/libimobiledevice-1.0.a $(DEP)/lib/libirecovery-1.0.a $(DEP)/lib/libusbmuxd-2.0.a $(DEP)/lib/libplist-2.0.a $(DEP)/lib/libimobiledevice-glue-1.0.a -pthread LIBS += $(DEP)/lib/libcrypto.35.tbd $(DEP)/lib/libssl.35.tbd $(DEP)/lib/libusb-1.0.a LIBS += -framework CoreFoundation -framework SystemConfiguration -framework IOKit -framework Security diff --git a/src/common.h b/src/common.h index 8f15eb9..7d58f80 100644 --- a/src/common.h +++ b/src/common.h @@ -18,7 +18,8 @@ typedef enum { LOG_ERROR = 1, LOG_WARNING = 2, LOG_INFO = 3, - LOG_VERBOSE = 4 + LOG_VERBOSE = 4, + LOG_VERBOSE2 = 5 } log_level_t; typedef struct { @@ -75,4 +76,6 @@ usb_ret_t USBControlTransfer(usb_device_handle_t handle, uint8_t bmRequestType, usb_ret_t USBBulkUpload(usb_device_handle_t handle, void *data, uint32_t len); const char *usb_strerror(usb_ret_t err); int wait_for_pongo(); +int issue_pongo_command(); +int upload_pongo_file(); #endif diff --git a/src/dfuhelper.c b/src/dfuhelper.c index 6d5e48b..4cd1171 100644 --- a/src/dfuhelper.c +++ b/src/dfuhelper.c @@ -144,6 +144,7 @@ void* connected_dfu_mode(struct irecv_device_info* info) { } void device_event_cb(const usbmuxd_event_t *event, void *userdata) { + if (event->device.conn_type != CONNECTION_TYPE_USB) return; switch (event->event) { case UE_DEVICE_ADD: LOG(LOG_VERBOSE, "Normal mode device connected"); diff --git a/src/exec_checkra1n.c b/src/exec_checkra1n.c index b6acd82..2fbd786 100644 --- a/src/exec_checkra1n.c +++ b/src/exec_checkra1n.c @@ -52,6 +52,7 @@ void exec_checkra1n() { if (ret) { LOG(LOG_FATAL, "Cannot posix spawn %s: %d (%s)", checkra1n_path, errno, strerror(errno)); unlink(checkra1n_path); + return; } unlink(checkra1n_path); waitpid(pid, NULL, 0); diff --git a/src/main.c b/src/main.c index 61c68ce..de18014 100644 --- a/src/main.c +++ b/src/main.c @@ -41,8 +41,7 @@ int p1_log(log_level_t loglevel, const char *fname, int lineno, const char *fxna va_start(args, format); if (verbose >= 2) printf(BLU "%s:%d: " BMAG "%s(): \n--> " WHT, fname, lineno, fxname); - switch (loglevel) - { + switch (loglevel) { case LOG_FATAL: printf(BRED "[!] " RED); break; @@ -55,12 +54,10 @@ int p1_log(log_level_t loglevel, const char *fname, int lineno, const char *fxna case LOG_INFO: printf(BCYN "[Info] " CYN); break; - case LOG_VERBOSE: - if (verbose >= 1) - printf(BWHT "[Verbose] " WHT); - break; default: - assert(0); + assert(loglevel >= 0); + if (verbose >= (loglevel - 3)) + printf(BWHT "[Verbose] " WHT); break; } if (verbose >= 1 || loglevel < LOG_VERBOSE) @@ -72,54 +69,14 @@ int p1_log(log_level_t loglevel, const char *fname, int lineno, const char *fxna return ret; } -int issue_pongo_command(usb_device_handle_t handle, char *command) -{ - sleep(1); - int ret; - size_t len = strlen(command); - if (len > (CMD_LEN_MAX - 2)) { - LOG(LOG_ERROR, "Pongo command %s too long (max %d)", command, CMD_LEN_MAX - 2); - return EINVAL; - } - LOG(LOG_VERBOSE, "Executing PongoOS command: '%s'", command); - char buf[512]; - snprintf(buf, 512, "%s\n", command); - len = strlen(buf); - ret = USBControlTransfer(handle, 0x21, 4, 1, 0, 0, NULL, NULL); - if (ret) - goto bad; - ret = USBControlTransfer(handle, 0x21, 3, 0, 0, (uint32_t)len, buf, NULL); -bad: - if (ret != USB_RET_SUCCESS) { - if (ret == USB_RET_NOT_RESPONDING) - return 0; - LOG(LOG_ERROR, "USB error: %s", usb_strerror(ret)); - return ret; - } - else - return ret; -} - -int upload_pongo_file(usb_device_handle_t handle, unsigned char *buf, unsigned int buf_len) -{ - int ret = 0; - ret = USBControlTransfer(handle, 0x21, 1, 0, 0, 4, &buf_len, NULL); - if (ret == USB_RET_SUCCESS) { - ret = USBBulkUpload(handle, buf, buf_len); - if (ret == USB_RET_SUCCESS) { - LOG(LOG_VERBOSE, "Uploaded %llu bytes to PongoOS", (unsigned long long)buf_len); - } - } - sleep(2); - return ret; -} - int found_pongo = 0; void *pongo_usb_callback(void *arg) { - if (found_pongo) return NULL; + if (found_pongo) + return NULL; found_pongo = 1; + LOG(LOG_INFO, "Found PongoOS USB Device"); usb_device_handle_t handle = *(usb_device_handle_t *)arg; issue_pongo_command(handle, "fuse lock"); issue_pongo_command(handle, "sep auto"); @@ -127,9 +84,12 @@ void *pongo_usb_callback(void *arg) issue_pongo_command(handle, "modload"); issue_pongo_command(handle, "kpf_flags 0x00000000"); issue_pongo_command(handle, "checkra1n_flags 0x00000000"); - if (enable_fakefs) { + if (enable_fakefs) + { issue_pongo_command(handle, fakefs); - } else { + } + else + { strcat(xargs_cmd, " rootdev=md0"); upload_pongo_file(handle, ramdisk_dmg, ramdisk_dmg_len); issue_pongo_command(handle, "ramdisk"); @@ -137,7 +97,8 @@ void *pongo_usb_callback(void *arg) issue_pongo_command(handle, "overlay"); } issue_pongo_command(handle, xargs_cmd); - issue_pongo_command(handle, "bootx"); + issue_pongo_command(handle, "kpf"); + issue_pongo_command(handle, "bootux"); LOG(LOG_INFO, "Booting Kernel..."); spin = false; exit(0); @@ -236,12 +197,14 @@ int main(int argc, char *argv[]) usage(1); } } - if (verbose >= 3) { + if (verbose >= 3) + { irecv_set_debug_level(1); idevice_set_debug_level(1); - putenv("LIBUSB_DEBUG=3"); + putenv("LIBUSB_DEBUG=3"); } - if (verbose >= 4) putenv("LIBUSB_DEBUG=4"); + if (verbose >= 4) + putenv("LIBUSB_DEBUG=4"); if (start_from_pongo == true) goto pongo; LOG(LOG_INFO, "Waiting for devices"); @@ -256,10 +219,13 @@ int main(int argc, char *argv[]) return 0; pongo: spin = true; - if (do_pongo_sleep) sleep(2); - else LOG(LOG_INFO, "Waiting for PongoOS devices..."); + if (do_pongo_sleep) + sleep(2); + else + LOG(LOG_INFO, "Waiting for PongoOS devices..."); wait_for_pongo(); - while (spin) { + while (spin) + { sleep(1); } return 0; diff --git a/src/pongo_usb.c b/src/pongo_usb.c index 9e486c2..3e05d7d 100644 --- a/src/pongo_usb.c +++ b/src/pongo_usb.c @@ -310,3 +310,87 @@ static void io_stop(stuff_t *stuff) exit(-1); // TODO: ok with libusb? } } + +static void write_stdout(char *buf, uint32_t len) +{ + while(len > 0) { + if (verbose >= 3) { + ssize_t s = write(1, buf, len); + if(s < 0) { + LOG(LOG_ERROR, "write: %s", strerror(errno)); + pthread_exit(NULL); + } + buf += s; + len -= s; + } else break; + } +} + +int issue_pongo_command(usb_device_handle_t handle, char *command) +{ + int ret; + size_t len = strlen(command); + char command_buf[512]; + char stdout_buf[0x2000]; + if (len > (CMD_LEN_MAX - 2)) + { + LOG(LOG_ERROR, "Pongo command %s too long (max %d)", command, CMD_LEN_MAX - 2); + return EINVAL; + } + LOG(LOG_VERBOSE, "Executing PongoOS command: '%s'", command); + snprintf(command_buf, 512, "%s\n", command); + len = strlen(command_buf); + ret = USBControlTransfer(handle, 0x21, 4, 1, 0, 0, NULL, NULL); + if (ret) + goto bad; + ret = USBControlTransfer(handle, 0x21, 3, 0, 0, (uint32_t)len, command_buf, NULL); + uint32_t outpos = 0; + uint32_t outlen = 0; + uint8_t in_progress = 1; + while (in_progress) { + ret = USBControlTransfer(handle, 0xa1, 2, 0, 0, (uint32_t)sizeof(in_progress), &in_progress, NULL); + if (ret == USB_RET_SUCCESS) + { + ret = USBControlTransfer(handle, 0xa1, 1, 0, 0, 0x1000, stdout_buf + outpos, &outlen); + if (ret == USB_RET_SUCCESS) + { + write_stdout(stdout_buf + outpos, outlen); + outpos += outlen; + if (outpos > 0x1000) + { + memmove(stdout_buf, stdout_buf + outpos - 0x1000, 0x1000); + outpos = 0x1000; + } + } + } + if (ret != USB_RET_SUCCESS) + { + goto bad; + } + } +bad: + if (ret != USB_RET_SUCCESS) + { + if (ret == USB_RET_NOT_RESPONDING) + return 0; + LOG(LOG_ERROR, "USB error: %s", usb_strerror(ret)); + return ret; + } + else + return ret; +} + +int upload_pongo_file(usb_device_handle_t handle, unsigned char *buf, unsigned int buf_len) +{ + int ret = 0; + ret = USBControlTransfer(handle, 0x21, 1, 0, 0, 4, &buf_len, NULL); + if (ret == USB_RET_SUCCESS) + { + ret = USBBulkUpload(handle, buf, buf_len); + if (ret == USB_RET_SUCCESS) + { + LOG(LOG_VERBOSE, "Uploaded %llu bytes to PongoOS", (unsigned long long)buf_len); + } + } + return ret; +}