From 999964611a353cec6ba2d33a0535a25dfe63a4c9 Mon Sep 17 00:00:00 2001 From: Nick Chan Date: Wed, 25 Jan 2023 16:46:32 +0800 Subject: [PATCH] code cleanup --- src/Makefile | 3 +- src/common.h | 74 ++++++++-- src/main.c | 320 +------------------------------------------- src/optparse.c | 212 +++++++++++++++++++++++++++++ src/override_file.c | 48 +++++++ src/pongo_helper.c | 62 +++++++++ 6 files changed, 391 insertions(+), 328 deletions(-) create mode 100644 src/optparse.c create mode 100644 src/override_file.c create mode 100644 src/pongo_helper.c diff --git a/src/Makefile b/src/Makefile index 68d0641..530d621 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,7 @@ all: palera1n -OBJECTS = main.o dfuhelper.o devhelper.o lockdown_helper.o exec_checkra1n.o pongo_usb.o checkra1n.o log.o +OBJECTS = main.o dfuhelper.o devhelper.o lockdown_helper.o exec_checkra1n.o +OBJECTS += pongo_usb.o checkra1n.o log.o optparse.o override_file.o pongo_helper.o CHECKRA1N_VERSION ?= 0.1337.1 LIPO = lipo LDID = ldid diff --git a/src/common.h b/src/common.h index 608f2e8..1f5d8f3 100644 --- a/src/common.h +++ b/src/common.h @@ -2,17 +2,52 @@ #define COMMON_H #include "lockdown_helper.h" +#include "checkra1n.h" +#include "kerninfo.h" #include #include #include #include #include #include +#include #include #define LOG(loglevel, ...) p1_log(loglevel, __FILE__, __LINE__, __func__, __VA_ARGS__) #define CLEAR() printf("\33[2K\r"); #define palerain_option_version 129 +#define CMD_LEN_MAX 512 +#define OVERRIDE_MAGIC 0xd803b376 +#define USB_RET_SUCCESS LIBUSB_SUCCESS +#define USB_RET_NOT_RESPONDING LIBUSB_ERROR_OTHER +#define USB_RET_IO LIBUSB_ERROR_IO +#if defined(__APPLE__) +#include +#include +#else +#define MH_MAGIC_64 0xfeedfacf +#define MH_CIGAM_64 0xcffaedfe +#define MH_KEXT_BUNDLE 0xb + +typedef int cpu_type_t; +typedef int cpu_subtype_t; + +#define CPU_ARCH_ABI64 0x01000000 +#define CPU_TYPE_ARM ((cpu_type_t) 12) +#define CPU_TYPE_ARM64 (CPU_TYPE_ARM | CPU_ARCH_ABI64) + +struct mach_header_64 +{ + uint32_t magic; /* mach magic number identifier */ + cpu_type_t cputype; /* cpu specifier */ + cpu_subtype_t cpusubtype; /* machine specifier */ + uint32_t filetype; /* type of file */ + uint32_t ncmds; /* number of load commands */ + uint32_t sizeofcmds; /* the size of all the load commands */ + uint32_t flags; /* flags */ + uint32_t reserved; /* reserved */ +}; +#endif typedef enum { LOG_FATAL = 0, @@ -52,10 +87,13 @@ typedef struct { int fd; /* file descriptor of the override file */ } override_file_t; -// set this value to 0 gracefully exit -extern int spin; +typedef unsigned char niarelap_file_t[]; +typedef int usb_ret_t; +typedef libusb_device_handle *usb_device_handle_t; + extern unsigned int verbose; -extern int demote; +extern int spin, demote; + extern char* pongo_path; extern bool dfuhelper_only; @@ -64,9 +102,26 @@ extern bool pongo_exit; extern pthread_t dfuhelper_thread, pongo_thread; extern int pongo_thr_running, dfuhelper_thr_running; -extern pthread_mutex_t log_mutex; -void thr_cleanup(void* ptr); +extern niarelap_file_t *kpf_to_upload_1, *ramdisk_to_upload_1, *overlay_to_upload_1; +extern niarelap_file_t **kpf_to_upload, **ramdisk_to_upload, **overlay_to_upload; +extern override_file_t override_ramdisk, override_kpf, override_overlay; +extern uint32_t checkrain_flags, palerain_flags, kpf_flags; +extern pthread_mutex_t log_mutex; + +extern int enable_rootful, do_pongo_sleep, device_has_booted, demote, found_pongo; +extern int pongo_thr_running, dfuhelper_thr_running; +extern bool ohio; +extern char xargs_cmd[0x270]; +extern char checkrain_flags_cmd[0x20]; +extern char palerain_flags_cmd[0x20]; +extern char kpf_flags_cmd[0x20]; +extern char dtpatch_cmd[0x20]; +extern char rootfs_cmd[512]; + +extern bool palerain_version; + +void thr_cleanup(void* ptr); void* dfuhelper(void* ptr); int p1_log(log_level_t loglevel, const char *fname, int lineno, const char *fxname, char* __restrict format, ...); /* devhelper helpers */ @@ -84,13 +139,9 @@ int autoboot_cmd(const uint64_t ecid); int exitrecv_cmd(const uint64_t ecid); void exec_checkra1n(); +int override_file(override_file_t *finfo, niarelap_file_t** orig, unsigned int *orig_len, char *filename); +void* pongo_helper(void* ptr); -typedef int usb_ret_t; -typedef libusb_device_handle *usb_device_handle_t; - -#define USB_RET_SUCCESS LIBUSB_SUCCESS -#define USB_RET_NOT_RESPONDING LIBUSB_ERROR_OTHER -#define USB_RET_IO LIBUSB_ERROR_IO extern void* pongo_usb_callback(void* arg); usb_ret_t USBControlTransfer(usb_device_handle_t handle, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t wLength, void *data, uint32_t *wLenDone); @@ -99,4 +150,5 @@ const char *usb_strerror(usb_ret_t err); int wait_for_pongo(); int issue_pongo_command(); int upload_pongo_file(); +int optparse(int argc, char* argv[]); #endif diff --git a/src/main.c b/src/main.c index 8a23a37..bb582ea 100644 --- a/src/main.c +++ b/src/main.c @@ -22,45 +22,14 @@ #include #include +#include #include -#if defined(__APPLE__) -#include -#include -#else -#define MH_MAGIC_64 0xfeedfacf -#define MH_CIGAM_64 0xcffaedfe -#define MH_KEXT_BUNDLE 0xb - -typedef int cpu_type_t; -typedef int cpu_subtype_t; - -#define CPU_ARCH_ABI64 0x01000000 -#define CPU_TYPE_ARM ((cpu_type_t) 12) -#define CPU_TYPE_ARM64 (CPU_TYPE_ARM | CPU_ARCH_ABI64) - -struct mach_header_64 -{ - uint32_t magic; /* mach magic number identifier */ - cpu_type_t cputype; /* cpu specifier */ - cpu_subtype_t cpusubtype; /* machine specifier */ - uint32_t filetype; /* type of file */ - uint32_t ncmds; /* number of load commands */ - uint32_t sizeofcmds; /* the size of all the load commands */ - uint32_t flags; /* flags */ - uint32_t reserved; /* reserved */ -}; - -#endif - #include "ANSI-color-codes.h" #include "common.h" #include "checkra1n.h" #include "kerninfo.h" -#define CMD_LEN_MAX 512 -#define OVERRIDE_MAGIC 0xd803b376 - unsigned int verbose = 0U; int enable_rootful = 0; int do_pongo_sleep = 0; @@ -78,10 +47,8 @@ extern char** environ; bool dfuhelper_only = false; bool pongo_exit = false; -bool start_from_pongo = false; bool palerain_version = false; -typedef unsigned char niarelap_file_t[]; niarelap_file_t* kpf_to_upload_1 = &checkra1n_kpf_pongo; niarelap_file_t* ramdisk_to_upload_1 = &ramdisk_dmg; @@ -102,128 +69,6 @@ uint32_t kpf_flags = 0; pthread_t dfuhelper_thread, pongo_thread; pthread_mutex_t log_mutex; -int found_pongo = 0; - -void *pongo_usb_callback(void *arg) { - 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, NULL); - issue_pongo_command(handle, "fuse lock"); - issue_pongo_command(handle, "sep auto"); - upload_pongo_file(handle, **kpf_to_upload, checkra1n_kpf_pongo_len); - issue_pongo_command(handle, "modload"); - issue_pongo_command(handle, kpf_flags_cmd); - issue_pongo_command(handle, checkrain_flags_cmd); - issue_pongo_command(handle, palerain_flags_cmd); - if (enable_rootful) - { - issue_pongo_command(handle, rootfs_cmd); - issue_pongo_command(handle, dtpatch_cmd); - } - strcat(xargs_cmd, " rootdev=md0"); - upload_pongo_file(handle, **ramdisk_to_upload, ramdisk_dmg_len); - issue_pongo_command(handle, "ramdisk"); - upload_pongo_file(handle, **overlay_to_upload, binpack_dmg_len); - issue_pongo_command(handle, "overlay"); - issue_pongo_command(handle, xargs_cmd); - issue_pongo_command(handle, "kpf"); - issue_pongo_command(handle, "bootux"); - LOG(LOG_INFO, "Booting Kernel..."); - device_has_booted = 1; - if (dfuhelper_thr_running) { - pthread_cancel(dfuhelper_thread); - } - spin = false; - return NULL; -} - -static struct option longopts[] = { - {"dfuhelper", no_argument, NULL, 'D'}, - {"help", no_argument, NULL, 'h'}, - {"pongo-shell", no_argument, NULL, 'p'}, - {"debug-logging", no_argument, NULL, 'v'}, - {"verbose-boot", no_argument, NULL, 'V'}, - {"boot-args", required_argument, NULL, 'e'}, - {"rootfs", required_argument, NULL, 'f'}, - {"rootless", no_argument, NULL, 'l'}, - {"jbinit-log-to-file", no_argument, NULL, 'L'}, - {"demote", no_argument, NULL, 'd'}, - {"force-revert", no_argument, NULL, checkrain_option_force_revert}, - {"safe-mode", no_argument, NULL, 's'}, - {"version", no_argument, NULL, palerain_option_version}, - {"override-pongo", required_argument, NULL, 'k'}, - {"override-overlay", required_argument, NULL, 'o'}, - {"override-ramdisk", required_argument, NULL, 'r'}, - {"override-kpf", required_argument, NULL, 'K'}, - {"disable-ohio", no_argument, NULL, 'O'}, - {NULL, 0, NULL, 0} -}; - -int usage(int e, char* prog_name) { - fprintf(stderr, - "Usage: %s [-DhpPvVldsOL] [-e boot arguments] [-f root device] [-k Pongo image] [-o overlay file] [-r ramdisk file] [-K KPF file]\n" - "Copyright (C) 2023, palera1n team, All Rights Reserved.\n\n" - "iOS/iPadOS 15+ arm64 jailbreaking tool\n\n" - "\t--version\t\t\t\tPrint version\n" - "\t--force-revert\t\t\t\tRemove jailbreak (on rootless)\n" - "\t-D, --dfuhelper-only\t\t\tExit after entering DFU\n" - "\t-h, --help\t\t\t\tShow this help\n" - "\t-p, --pongo-shell\t\t\tBoots to PongoOS shell\n" - "\t-v, --debug-logging\t\t\tEnable debug logging\n" - "\t\tThis option can be repeated for extra verbosity.\n" - "\t-V, --verbose-boot\t\t\tVerbose boot\n" - "\t-L, --jbinit-log-to-file\t\tMake jbinit log to /cores/jbinit.log (can be read from sandbox while jailbroken)\n" - "\t-e, --boot-args \tXNU boot arguments\n" - "\t-f, --rootfs \t\tBoots rootful setup on \n" - "\t-l, --rootless\t\t\t\tBoots rootless. This is the default\n" - "\t-s, --safe-mode\t\t\t\tEnter safe mode\n" - "\t-d, --demote\t\t\t\tDemote\n" - "\t-k, --override-pongo \t\tOverride Pongo image\n" - "\t-o, --override-overlay \t\tOverride overlay\n" - "\t-r, --override-ramdisk \t\tOverride ramdisk\n" - "\t-K, --override-kpf \t\tOverride kernel patchfinder\n" - "\t-O, --disable-ohio\t\t\tDisable Ohio\n", - prog_name); - exit(e); -}; - -int override_file(override_file_t *finfo, niarelap_file_t** orig, unsigned int *orig_len, char *filename) { - int ret = 0; - int fd = open(filename, O_RDONLY); - LOG(LOG_VERBOSE5, "override_file() called!"); - if (fd == -1) { - LOG(LOG_ERROR, "Cannot open file %s: %d (%s)", filename, errno, strerror(errno)); - return errno; - } - LOG(LOG_VERBOSE5, "override_file: opened %s!", filename); - struct stat st; - ret = fstat(fd, &st); - if (ret) { - LOG(LOG_ERROR, "Cannot fstat fd from file %s: %d (%s)", filename, errno, strerror(errno)); - return errno; - } - LOG(LOG_VERBOSE5, "override_file: fstat fd %d succeeded!", fd); - void *addr = mmap(NULL, st.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0); - if (addr == MAP_FAILED) { - LOG(LOG_ERROR, "Failed to map file %s: %d (%s)", filename, errno, strerror(errno)); - return errno; - } - LOG(LOG_VERBOSE5, "override_file: Override file mapped successfully"); - finfo->magic = OVERRIDE_MAGIC; - finfo->fd = fd; - finfo->len = (unsigned int)st.st_size;; - finfo->ptr = *(niarelap_file_t*)addr; - finfo->orig_len = *orig_len; - finfo->orig_ptr = **orig; - *orig = (niarelap_file_t*)addr; - *orig_len = (unsigned int)st.st_size; - LOG(LOG_VERBOSE5, "override_file() finished!"); - return 0; -}; - int build_checks() { #if defined(__APPLE__) struct mach_header_64* c1_header = (struct mach_header_64*)&checkra1n[0]; @@ -255,172 +100,15 @@ void thr_cleanup(void* ptr) { return; } -void* pongo_helper(void* ptr) { - pongo_thr_running = 1; - pthread_cleanup_push(thr_cleanup, &pongo_thr_running); - wait_for_pongo(); - while (spin) { - sleep(1); - } - pthread_cleanup_pop(1); - return NULL; -} - int palera1n(int argc, char *argv[]) { + int ret = 0; int mutex_err = pthread_mutex_init(&log_mutex, NULL); if (mutex_err) { fprintf(stderr, "palera1n_init: cannot create mutex: %d (%s)\n", errno, strerror(errno)); return -1; } if (build_checks()) return -1; - int opt; - int index; - while ((opt = getopt_long(argc, argv, "DhpvVldsOLe:f:o:r:K:k:", longopts, NULL)) != -1) - { - switch (opt) - { - case 'P': - start_from_pongo = true; - break; - case 'p': - pongo_exit = true; - break; - case 'D': - dfuhelper_only = true; - break; - case 'h': - usage(0, argv[0]); - assert(0); - case 'v': - verbose++; - break; - case 'V': - kpf_flags |= checkrain_option_verbose_boot; - break; - case 'e': - if (strstr(optarg, "rootdev=") != NULL) { - LOG(LOG_FATAL, "The boot arg rootdev= is already used by palera1n and cannot be overriden"); - return -1; - } - snprintf(xargs_cmd, sizeof(xargs_cmd), "xargs %s", optarg); - break; - case 'f': - snprintf(rootfs_cmd, sizeof(rootfs_cmd), "rootfs %s", optarg); - snprintf(dtpatch_cmd, 0x20, "dtpatch %s", optarg); - enable_rootful = 1; - break; - case 'l': - enable_rootful = 0; - break; - case 'L': - palerain_flags |= palerain_option_jbinit_log_to_file; - break; - case 'd': - demote = 1; - break; - case 's': - checkrain_flags |= checkrain_option_safemode; - break; - case 'k': - if (access(optarg, F_OK) != 0) { - LOG(LOG_FATAL, "Cannot access pongo file at %s: %d (%s)", optarg, errno, strerror(errno)); - return -1; - } - pongo_path = malloc(strlen(optarg) + 1); - strcpy(pongo_path, optarg); - break; - case 'o': - if (override_file(&override_overlay, overlay_to_upload, &binpack_dmg_len, optarg)) - return 1; - break; - case 'r': - if (override_file(&override_ramdisk, ramdisk_to_upload, &ramdisk_dmg_len, optarg)) - return 1; - break; - case 'K': - if (override_file(&override_kpf, kpf_to_upload, &checkra1n_kpf_pongo_len, optarg)) - return 1; - struct mach_header_64* hdr = (struct mach_header_64*)override_kpf.ptr; - if (hdr->magic != MH_MAGIC_64 && hdr->magic != MH_CIGAM_64) { - LOG(LOG_FATAL, "Invalid kernel patchfinder: Not thin 64-bit Mach-O"); - goto cleanup; - } else if (hdr->filetype != MH_KEXT_BUNDLE) { - LOG(LOG_FATAL, "Invalid kernel patchfinder: Not a kext bundle"); - goto cleanup; - } else if (hdr->cputype != CPU_TYPE_ARM64) { - LOG(LOG_FATAL, "Invalid kernel patchfinder: CPU type is not arm64"); - goto cleanup; - } - break; - case 'O': - ohio = false; - break; - case checkrain_option_force_revert: - checkrain_flags |= checkrain_option_force_revert; - break; - case palerain_option_version: - palerain_version = true; - break; - default: - usage(1, argv[0]); - break; - } - } - if (palerain_version) { - printf("palera1n %s\n", PALERAIN_VERSION); - return 0; - } - - if (enable_rootful) { - palerain_flags |= palerain_option_rootful; - } - - snprintf(checkrain_flags_cmd, 0x20, "checkra1n_flags 0x%x", checkrain_flags); - snprintf(palerain_flags_cmd, 0x20, "palera1n_flags 0x%x", palerain_flags); - snprintf(kpf_flags_cmd, 0x20, "kpf_flags 0x%x", kpf_flags); - LOG(LOG_VERBOSE3, "checkrain_flags: %s", checkrain_flags_cmd); - LOG(LOG_VERBOSE3, "palerain_flags: %s", palerain_flags_cmd); - LOG(LOG_VERBOSE3, "kpf_flags: %s", kpf_flags_cmd); - if (override_kpf.magic == OVERRIDE_MAGIC) { - LOG(LOG_VERBOSE4, "kpf override length %u -> %u", override_kpf.orig_len, checkra1n_kpf_pongo_len); - LOG(LOG_VERBOSE4, "kpf override ptr %p -> %p", override_kpf.orig_ptr, **kpf_to_upload); - } - if (override_ramdisk.magic == OVERRIDE_MAGIC) { - LOG(LOG_VERBOSE4, "ramdisk override length %u -> %u", override_ramdisk.orig_len, ramdisk_dmg_len); - LOG(LOG_VERBOSE4, "ramdisk override ptr %p -> %p", override_ramdisk.orig_ptr, **ramdisk_to_upload); - } - if (override_overlay.magic == OVERRIDE_MAGIC) { - LOG(LOG_VERBOSE4, "overlay override length %u -> %u", override_overlay.orig_len, binpack_dmg_len); - LOG(LOG_VERBOSE4, "overlay override ptr %p -> %p", override_overlay.orig_ptr, **overlay_to_upload); - } - - for (index = optind; index < argc; index++) - { - if (!strcmp("windows", argv[index])) - { - fprintf(stderr, - "Windows not really using for manipulating OSX images,\n" - "compiled in mingw tool for this working unstable and incorrectly\n"); - return -2; - } - else - { - fprintf(stderr, "%s: unknown argument: %s\n", argv[0], argv[index]); - usage(1, argv[0]); - } - } - if (verbose >= 2) putenv("LIBUSB_DEBUG=1"); - if (verbose >= 3) - { - irecv_set_debug_level(1); - putenv("LIBUSB_DEBUG=2"); - } - if (verbose >= 4) { - idevice_set_debug_level(1); - putenv("LIBUSB_DEBUG=3"); - } - if (verbose >= 5) - putenv("LIBUSB_DEBUG=4"); + if ((ret = optparse(argc, argv))) goto cleanup; LOG(LOG_INFO, "Waiting for devices"); do_pongo_sleep = 1; @@ -482,7 +170,7 @@ cleanup: close(override_overlay.fd); } pthread_mutex_destroy(&log_mutex); - return 0; + return ret; } int main (int argc, char* argv[]) { diff --git a/src/optparse.c b/src/optparse.c new file mode 100644 index 0000000..c84f364 --- /dev/null +++ b/src/optparse.c @@ -0,0 +1,212 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "common.h" + +static struct option longopts[] = { + {"dfuhelper", no_argument, NULL, 'D'}, + {"help", no_argument, NULL, 'h'}, + {"pongo-shell", no_argument, NULL, 'p'}, + {"debug-logging", no_argument, NULL, 'v'}, + {"verbose-boot", no_argument, NULL, 'V'}, + {"boot-args", required_argument, NULL, 'e'}, + {"rootfs", required_argument, NULL, 'f'}, + {"rootless", no_argument, NULL, 'l'}, + {"jbinit-log-to-file", no_argument, NULL, 'L'}, + {"demote", no_argument, NULL, 'd'}, + {"force-revert", no_argument, NULL, checkrain_option_force_revert}, + {"safe-mode", no_argument, NULL, 's'}, + {"version", no_argument, NULL, palerain_option_version}, + {"override-pongo", required_argument, NULL, 'k'}, + {"override-overlay", required_argument, NULL, 'o'}, + {"override-ramdisk", required_argument, NULL, 'r'}, + {"override-kpf", required_argument, NULL, 'K'}, + {"disable-ohio", no_argument, NULL, 'O'}, + {NULL, 0, NULL, 0} +}; + +static int usage(int e, char* prog_name) { + fprintf(stderr, + "Usage: %s [-DhpPvVldsOL] [-e boot arguments] [-f root device] [-k Pongo image] [-o overlay file] [-r ramdisk file] [-K KPF file]\n" + "Copyright (C) 2023, palera1n team, All Rights Reserved.\n\n" + "iOS/iPadOS 15+ arm64 jailbreaking tool\n\n" + "\t--version\t\t\t\tPrint version\n" + "\t--force-revert\t\t\t\tRemove jailbreak (on rootless)\n" + "\t-D, --dfuhelper-only\t\t\tExit after entering DFU\n" + "\t-h, --help\t\t\t\tShow this help\n" + "\t-p, --pongo-shell\t\t\tBoots to PongoOS shell\n" + "\t-v, --debug-logging\t\t\tEnable debug logging\n" + "\t\tThis option can be repeated for extra verbosity.\n" + "\t-V, --verbose-boot\t\t\tVerbose boot\n" + "\t-L, --jbinit-log-to-file\t\tMake jbinit log to /cores/jbinit.log (can be read from sandbox while jailbroken)\n" + "\t-e, --boot-args \tXNU boot arguments\n" + "\t-f, --rootfs \t\tBoots rootful setup on \n" + "\t-l, --rootless\t\t\t\tBoots rootless. This is the default\n" + "\t-s, --safe-mode\t\t\t\tEnter safe mode\n" + "\t-d, --demote\t\t\t\tDemote\n" + "\t-k, --override-pongo \t\tOverride Pongo image\n" + "\t-o, --override-overlay \t\tOverride overlay\n" + "\t-r, --override-ramdisk \t\tOverride ramdisk\n" + "\t-K, --override-kpf \t\tOverride kernel patchfinder\n" + "\t-O, --disable-ohio\t\t\tDisable Ohio\n", + prog_name); + exit(e); +}; + +int optparse(int argc, char* argv[]) { + int opt; + int index; + while ((opt = getopt_long(argc, argv, "DhpvVldsOLe:f:o:r:K:k:", longopts, NULL)) != -1) + { + switch (opt) { + case 'p': + pongo_exit = true; + break; + case 'D': + dfuhelper_only = true; + break; + case 'h': + usage(0, argv[0]); + assert(0); + case 'v': + verbose++; + break; + case 'V': + kpf_flags |= checkrain_option_verbose_boot; + break; + case 'e': + if (strstr(optarg, "rootdev=") != NULL) { + LOG(LOG_FATAL, "The boot arg rootdev= is already used by palera1n and cannot be overriden"); + return -1; + } + snprintf(xargs_cmd, sizeof(xargs_cmd), "xargs %s", optarg); + break; + case 'f': + snprintf(rootfs_cmd, sizeof(rootfs_cmd), "rootfs %s", optarg); + snprintf(dtpatch_cmd, 0x20, "dtpatch %s", optarg); + enable_rootful = 1; + break; + case 'l': + enable_rootful = 0; + break; + case 'L': + palerain_flags |= palerain_option_jbinit_log_to_file; + break; + case 'd': + demote = 1; + break; + case 's': + checkrain_flags |= checkrain_option_safemode; + break; + case 'k': + if (access(optarg, F_OK) != 0) { + LOG(LOG_FATAL, "Cannot access pongo file at %s: %d (%s)", optarg, errno, strerror(errno)); + return -1; + } + pongo_path = malloc(strlen(optarg) + 1); + strcpy(pongo_path, optarg); + break; + case 'o': + if (override_file(&override_overlay, overlay_to_upload, &binpack_dmg_len, optarg)) + return 1; + break; + case 'r': + if (override_file(&override_ramdisk, ramdisk_to_upload, &ramdisk_dmg_len, optarg)) + return 1; + break; + case 'K': + if (override_file(&override_kpf, kpf_to_upload, &checkra1n_kpf_pongo_len, optarg)) + return 1; + struct mach_header_64* hdr = (struct mach_header_64*)override_kpf.ptr; + if (hdr->magic != MH_MAGIC_64 && hdr->magic != MH_CIGAM_64) { + LOG(LOG_FATAL, "Invalid kernel patchfinder: Not thin 64-bit Mach-O"); + return -1; + } else if (hdr->filetype != MH_KEXT_BUNDLE) { + LOG(LOG_FATAL, "Invalid kernel patchfinder: Not a kext bundle"); + return -1; + } else if (hdr->cputype != CPU_TYPE_ARM64) { + LOG(LOG_FATAL, "Invalid kernel patchfinder: CPU type is not arm64"); + return -1; + } + break; + case 'O': + ohio = false; + break; + case checkrain_option_force_revert: + checkrain_flags |= checkrain_option_force_revert; + break; + case palerain_option_version: + palerain_version = true; + break; + default: + usage(1, argv[0]); + break; + } + } + if (palerain_version) { + printf("palera1n %s\n", PALERAIN_VERSION); + return 0; + } + + if (enable_rootful) { + palerain_flags |= palerain_option_rootful; + } + + snprintf(checkrain_flags_cmd, 0x20, "checkra1n_flags 0x%x", checkrain_flags); + snprintf(palerain_flags_cmd, 0x20, "palera1n_flags 0x%x", palerain_flags); + snprintf(kpf_flags_cmd, 0x20, "kpf_flags 0x%x", kpf_flags); + LOG(LOG_VERBOSE3, "checkrain_flags: %s", checkrain_flags_cmd); + LOG(LOG_VERBOSE3, "palerain_flags: %s", palerain_flags_cmd); + LOG(LOG_VERBOSE3, "kpf_flags: %s", kpf_flags_cmd); + if (override_kpf.magic == OVERRIDE_MAGIC) { + LOG(LOG_VERBOSE4, "kpf override length %u -> %u", override_kpf.orig_len, checkra1n_kpf_pongo_len); + LOG(LOG_VERBOSE4, "kpf override ptr %p -> %p", override_kpf.orig_ptr, **kpf_to_upload); + } + if (override_ramdisk.magic == OVERRIDE_MAGIC) { + LOG(LOG_VERBOSE4, "ramdisk override length %u -> %u", override_ramdisk.orig_len, ramdisk_dmg_len); + LOG(LOG_VERBOSE4, "ramdisk override ptr %p -> %p", override_ramdisk.orig_ptr, **ramdisk_to_upload); + } + if (override_overlay.magic == OVERRIDE_MAGIC) { + LOG(LOG_VERBOSE4, "overlay override length %u -> %u", override_overlay.orig_len, binpack_dmg_len); + LOG(LOG_VERBOSE4, "overlay override ptr %p -> %p", override_overlay.orig_ptr, **overlay_to_upload); + } + + for (index = optind; index < argc; index++) + { + if (!strcmp("windows", argv[index])) + { + fprintf(stderr, + "Windows not really using for manipulating OSX images,\n" + "compiled in mingw tool for this working unstable and incorrectly\n"); + return -2; + } + else + { + fprintf(stderr, "%s: unknown argument: %s\n", argv[0], argv[index]); + usage(1, argv[0]); + } + } + if (verbose >= 2) putenv("LIBUSB_DEBUG=1"); + if (verbose >= 3) + { + irecv_set_debug_level(1); + putenv("LIBUSB_DEBUG=2"); + } + if (verbose >= 4) { + idevice_set_debug_level(1); + putenv("LIBUSB_DEBUG=3"); + } + if (verbose >= 5) + putenv("LIBUSB_DEBUG=4"); + return 0; +} + diff --git a/src/override_file.c b/src/override_file.c new file mode 100644 index 0000000..5c9943e --- /dev/null +++ b/src/override_file.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "common.h" + +int override_file(override_file_t *finfo, niarelap_file_t** orig, unsigned int *orig_len, char *filename) { + int ret = 0; + int fd = open(filename, O_RDONLY); + LOG(LOG_VERBOSE5, "override_file() called!"); + if (fd == -1) { + LOG(LOG_ERROR, "Cannot open file %s: %d (%s)", filename, errno, strerror(errno)); + return errno; + } + LOG(LOG_VERBOSE5, "override_file: opened %s!", filename); + struct stat st; + ret = fstat(fd, &st); + if (ret) { + LOG(LOG_ERROR, "Cannot fstat fd from file %s: %d (%s)", filename, errno, strerror(errno)); + return errno; + } + LOG(LOG_VERBOSE5, "override_file: fstat fd %d succeeded!", fd); + void *addr = mmap(NULL, st.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0); + if (addr == MAP_FAILED) { + LOG(LOG_ERROR, "Failed to map file %s: %d (%s)", filename, errno, strerror(errno)); + return errno; + } + LOG(LOG_VERBOSE5, "override_file: Override file mapped successfully"); + finfo->magic = OVERRIDE_MAGIC; + finfo->fd = fd; + finfo->len = (unsigned int)st.st_size;; + finfo->ptr = *(niarelap_file_t*)addr; + finfo->orig_len = *orig_len; + finfo->orig_ptr = **orig; + *orig = (niarelap_file_t*)addr; + *orig_len = (unsigned int)st.st_size; + LOG(LOG_VERBOSE5, "override_file() finished!"); + return 0; +}; diff --git a/src/pongo_helper.c b/src/pongo_helper.c new file mode 100644 index 0000000..ab1326c --- /dev/null +++ b/src/pongo_helper.c @@ -0,0 +1,62 @@ +#include +#include // open +#include +#include +#include +#include +#include // exit, strtoull +#include // strlen, strerror, memcpy, memmove +#include // close +#include // mmap, munmap +#include // fstst + +#include "common.h" + +int found_pongo = 0; + +void* pongo_helper(void* ptr) { + pongo_thr_running = 1; + pthread_cleanup_push(thr_cleanup, &pongo_thr_running); + wait_for_pongo(); + while (spin) { + sleep(1); + } + pthread_cleanup_pop(1); + return NULL; +} + +void *pongo_usb_callback(void *arg) { + 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, NULL); + issue_pongo_command(handle, "fuse lock"); + issue_pongo_command(handle, "sep auto"); + upload_pongo_file(handle, **kpf_to_upload, checkra1n_kpf_pongo_len); + issue_pongo_command(handle, "modload"); + issue_pongo_command(handle, kpf_flags_cmd); + issue_pongo_command(handle, checkrain_flags_cmd); + issue_pongo_command(handle, palerain_flags_cmd); + if (enable_rootful) + { + issue_pongo_command(handle, rootfs_cmd); + issue_pongo_command(handle, dtpatch_cmd); + } + strcat(xargs_cmd, " rootdev=md0"); + upload_pongo_file(handle, **ramdisk_to_upload, ramdisk_dmg_len); + issue_pongo_command(handle, "ramdisk"); + upload_pongo_file(handle, **overlay_to_upload, binpack_dmg_len); + issue_pongo_command(handle, "overlay"); + issue_pongo_command(handle, xargs_cmd); + issue_pongo_command(handle, "kpf"); + issue_pongo_command(handle, "bootux"); + LOG(LOG_INFO, "Booting Kernel..."); + device_has_booted = 1; + if (dfuhelper_thr_running) { + pthread_cancel(dfuhelper_thread); + } + spin = false; + return NULL; +}