Files
palera1n/src/main.c
T

371 lines
10 KiB
C
Raw Normal View History

2023-01-16 16:17:41 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <stddef.h>
#include <unistd.h>
#include <stdarg.h>
#include <ctype.h>
#include <assert.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <pthread.h>
#include <inttypes.h>
2023-01-17 13:46:42 +08:00
#include <getopt.h>
#include <errno.h>
2023-01-18 17:58:20 +08:00
#include <sys/mman.h>
2023-01-16 16:17:41 +08:00
2023-01-17 19:26:37 +08:00
#include <libimobiledevice/libimobiledevice.h>
2023-01-18 17:58:20 +08:00
#if defined(__APPLE__)
#include <mach-o/loader.h>
#else
#define MH_MAGIC 0xfeedface
#define MH_CIGAM 0xcefaedfe
#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
2023-01-16 16:17:41 +08:00
#include "ANSI-color-codes.h"
#include "common.h"
2023-01-17 13:46:42 +08:00
#include "checkra1n.h"
2023-01-18 16:02:43 +08:00
#include "kerninfo.h"
2023-01-16 16:17:41 +08:00
2023-01-17 13:46:42 +08:00
#define CMD_LEN_MAX 512
2023-01-18 17:58:20 +08:00
#define OVERRIDE_MAGIC 0xd803b376
2023-01-17 13:46:42 +08:00
2023-01-18 16:02:43 +08:00
unsigned int verbose = 0;
2023-01-17 19:26:37 +08:00
int enable_fakefs = 0;
int do_pongo_sleep = 0;
int demote = 0;
2023-01-17 13:59:44 +08:00
char xargs_cmd[0x270] = "xargs serial=3 wdt=-1";
2023-01-18 16:02:43 +08:00
char checkrain_flags_cmd[0x20] = "checkra1n_flags 0x0";
2023-01-17 19:26:37 +08:00
char fakefs[512];
2023-01-17 13:46:42 +08:00
2023-01-18 17:58:20 +08:00
override_file_t override_ramdisk;
override_file_t override_kpf;
override_file_t override_overlay;
2023-01-18 16:02:43 +08:00
uint32_t checkrain_flags = 0;
2023-01-17 13:46:42 +08:00
int p1_log(log_level_t loglevel, const char *fname, int lineno, const char *fxname, char *__restrict format, ...)
{
int ret = 0;
2023-01-16 16:17:41 +08:00
va_list args;
va_start(args, format);
2023-01-18 16:06:07 +08:00
if (verbose >= 2 && verbose >= (loglevel - 3))
2023-01-17 13:46:42 +08:00
printf(BLU "%s:%d: " BMAG "%s(): \n--> " WHT, fname, lineno, fxname);
2023-01-18 17:58:20 +08:00
switch (loglevel)
{
2023-01-17 13:46:42 +08:00
case LOG_FATAL:
printf(BRED "[!] " RED);
break;
case LOG_ERROR:
printf(BRED "[Error] " RED);
break;
case LOG_WARNING:
printf(BYEL "[Warning] " YEL);
break;
case LOG_INFO:
printf(BCYN "[Info] " CYN);
break;
default:
2023-01-18 00:44:47 +08:00
assert(loglevel >= 0);
if (verbose >= (loglevel - 3))
printf(BWHT "[Verbose] " WHT);
2023-01-17 13:46:42 +08:00
break;
}
2023-01-18 16:02:43 +08:00
if (verbose >= (loglevel - 3) || loglevel < LOG_VERBOSE)
2023-01-17 13:46:42 +08:00
{
ret = vprintf(format, args);
va_end(args);
printf(CRESET "\n");
2023-01-16 16:17:41 +08:00
}
return ret;
}
2023-01-17 13:46:42 +08:00
int found_pongo = 0;
void *pongo_usb_callback(void *arg)
{
2023-01-18 00:44:47 +08:00
if (found_pongo)
return NULL;
2023-01-17 13:46:42 +08:00
found_pongo = 1;
2023-01-18 00:44:47 +08:00
LOG(LOG_INFO, "Found PongoOS USB Device");
2023-01-17 13:46:42 +08:00
usb_device_handle_t handle = *(usb_device_handle_t *)arg;
issue_pongo_command(handle, "fuse lock");
issue_pongo_command(handle, "sep auto");
upload_pongo_file(handle, checkra1n_kpf_pongo, checkra1n_kpf_pongo_len);
issue_pongo_command(handle, "modload");
issue_pongo_command(handle, "kpf_flags 0x00000000");
2023-01-18 16:02:43 +08:00
issue_pongo_command(handle, checkrain_flags_cmd);
2023-01-18 17:58:20 +08:00
if (enable_fakefs)
{
2023-01-17 19:26:37 +08:00
issue_pongo_command(handle, fakefs);
2023-01-18 17:58:20 +08:00
}
else
{
strcat(xargs_cmd, " rootdev=md0");
upload_pongo_file(handle, ramdisk_dmg, ramdisk_dmg_len);
issue_pongo_command(handle, "ramdisk");
upload_pongo_file(handle, binpack_dmg, binpack_dmg_len);
issue_pongo_command(handle, "overlay");
2023-01-18 16:02:43 +08:00
issue_pongo_command(handle, "dtpatch md0");
2023-01-17 19:26:37 +08:00
}
2023-01-17 13:59:44 +08:00
issue_pongo_command(handle, xargs_cmd);
2023-01-18 00:44:47 +08:00
issue_pongo_command(handle, "kpf");
issue_pongo_command(handle, "bootux");
2023-01-17 13:46:42 +08:00
LOG(LOG_INFO, "Booting Kernel...");
spin = false;
return NULL;
}
static struct option longopts[] = {
{"dfuhelper", no_argument, NULL, 'D'},
{"help", no_argument, NULL, 'h'},
{"pongo-shell", no_argument, NULL, 'p'},
{"start-from-pongo", no_argument, NULL, 'P'},
{"debug-logging", no_argument, NULL, 'V'},
2023-01-17 13:59:44 +08:00
{"boot-args", required_argument, NULL, 'e'},
2023-01-17 20:48:54 +08:00
{"rootfs", required_argument, NULL, 'f'},
2023-01-18 16:02:43 +08:00
{"rootless", 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},
2023-01-18 17:58:20 +08:00
{"override-overlay", required_argument, NULL, 'o'},
{"override-ramdisk", required_argument, NULL, 'r'},
{"override-kpf", required_argument, NULL, 'K'},
2023-01-17 13:46:42 +08:00
{NULL, 0, NULL, 0}};
int usage(int e)
{
fprintf(stderr,
2023-01-18 17:58:20 +08:00
"Usage: %s [-DhpPvlds] [-e boot arguments] [-f root device] [-o overlay file] [-r ramdisk file] [-K KPF file]\n"
2023-01-17 13:46:42 +08:00
"Copyright (C) 2023, palera1n team, All Rights Reserved.\n\n"
"iOS/iPadOS 15+ arm64 jailbreaking tool\n\n"
2023-01-18 16:02:43 +08:00
"\t--version\t\t\t\tPrint version\n"
"\t--force-revert\t\t\t\tRemove jailbreak (on rootless)\n"
2023-01-17 14:06:08 +08:00
"\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-P, --start-from-pongo\t\t\tStart with a PongoOS USB Device attached\n"
2023-01-17 19:26:37 +08:00
"\t-v, --debug-logging\t\t\tEnable debug logging\n"
2023-01-17 14:06:27 +08:00
"\t\tThis option can be repeated for extra verbosity.\n"
2023-01-17 19:26:37 +08:00
"\t-e, --boot-args <boot arguments>\tXNU boot arguments\n"
"\t-f, --rootfs <root device>\t\tBoots rootful setup on <root device>\n"
"\t-l, --rootless\t\t\t\tBoots rootless. This is the default\n"
2023-01-18 16:02:43 +08:00
"\t-s, --safe-mode\t\t\t\tEnter safe mode (on rootless)\n"
2023-01-18 17:58:20 +08:00
"\t-d, --demote\t\t\t\tDemote\n"
"\t-o, --override-overlay <file>\t\tOverride overlay\n"
"\t-r, --override-ramdisk <file>\t\tOverride ramdisk\n"
"\t-K, --override-kpf <file>\t\tOverride kernel patchfinder\n",
2023-01-17 13:46:42 +08:00
getprogname());
exit(e);
}
2023-01-18 17:58:20 +08:00
int override_file(override_file_t *finfo, unsigned char orig[], unsigned int *orig_len, char *filename)
{
int ret = 0;
int fd = open(filename, O_RDONLY);
if (fd == -1)
{
LOG(LOG_ERROR, "Cannot open file %s: %d (%s)\n", filename, errno, strerror(errno));
return errno;
}
struct stat st;
ret = fstat(fd, &st);
if (ret)
{
LOG(LOG_ERROR, "Cannot fstat fd from file %s: %d (%s)\n", filename, errno, strerror(errno));
return errno;
}
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;
}
finfo->magic = OVERRIDE_MAGIC;
finfo->fd = fd;
finfo->len = (unsigned int)st.st_size;;
finfo->ptr = (unsigned char*)addr;
finfo->orig_len = *orig_len;
finfo->orig_ptr = orig;
orig = (unsigned char*)addr;
*orig_len = (unsigned int)st.st_size;
return 0;
}
2023-01-17 13:46:42 +08:00
bool dfuhelper_only = false;
bool pongo_exit = false;
bool start_from_pongo = false;
2023-01-18 16:02:43 +08:00
bool palerain_version = false;
2023-01-17 13:46:42 +08:00
int main(int argc, char *argv[])
{
int opt;
int index;
2023-01-18 17:58:20 +08:00
while ((opt = getopt_long(argc, argv, "DhpPvldse:f:o:r:K:", longopts, NULL)) != -1)
2023-01-17 13:46:42 +08:00
{
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);
assert(0);
2023-01-17 19:26:37 +08:00
case 'v':
2023-01-17 13:46:42 +08:00
verbose++;
break;
2023-01-17 13:59:44 +08:00
case 'e':
snprintf(xargs_cmd, sizeof(xargs_cmd), "xargs %s", optarg);
break;
2023-01-17 19:26:37 +08:00
case 'f':
snprintf(fakefs, sizeof(fakefs), "dtpatch %s", optarg);
enable_fakefs = 1;
break;
case 'l':
enable_fakefs = 0;
break;
case 'd':
demote = 1;
break;
2023-01-18 16:02:43 +08:00
case 's':
checkrain_flags |= checkrain_option_safemode;
break;
2023-01-18 17:58:20 +08:00
case 'o':
if (override_file(&override_overlay, binpack_dmg, &binpack_dmg_len, optarg))
return 1;
break;
case 'r':
if (override_file(&override_ramdisk, ramdisk_dmg, &ramdisk_dmg_len, optarg))
return 1;
break;
case 'K':
if (override_file(&override_kpf, checkra1n_kpf_pongo, &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_ERROR, "Invalid kernel patchfinder: Not thin 64-bit Mach-O");
goto cleanup;
} else if (hdr->filetype != MH_KEXT_BUNDLE) {
LOG(LOG_ERROR, "Invalid kernel patchfinder: Not a kext bundle");
goto cleanup;
} else if (hdr->cputype != CPU_TYPE_ARM64) {
LOG(LOG_ERROR, "Invalid kernel patchfinder: CPU type is not arm64");
goto cleanup;
}
break;
2023-01-18 16:02:43 +08:00
case checkrain_option_force_revert:
checkrain_flags |= checkrain_option_force_revert;
break;
case palerain_option_version:
palerain_version = true;
break;
2023-01-17 13:46:42 +08:00
default:
usage(1);
break;
}
}
2023-01-18 17:58:20 +08:00
if (palerain_version)
{
2023-01-18 16:02:43 +08:00
printf("palera1n %s\n", PALERAIN_VERSION);
return 0;
}
snprintf(checkrain_flags_cmd, 0x20, "checkra1n_flags 0x%x", checkrain_flags);
LOG(LOG_VERBOSE2, "checkrain_flags: %s\n", checkrain_flags_cmd);
2023-01-18 17:58:20 +08:00
LOG(LOG_VERBOSE4, "binpack_dmg @ %p", binpack_dmg);
LOG(LOG_VERBOSE4, "ramdisk_dmg @ %p", ramdisk_dmg);
LOG(LOG_VERBOSE4, "checkra1n_kpf_pongo @ %p", checkra1n_kpf_pongo);
2023-01-17 13:46:42 +08:00
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");
2023-01-17 21:43:46 +08:00
return -2;
2023-01-17 13:46:42 +08:00
}
else
{
2023-01-17 19:26:37 +08:00
fprintf(stderr, "%s: unknown argument: %s\n", getprogname(), argv[index]);
2023-01-17 13:46:42 +08:00
usage(1);
}
}
2023-01-18 00:44:47 +08:00
if (verbose >= 3)
{
2023-01-17 19:26:37 +08:00
irecv_set_debug_level(1);
idevice_set_debug_level(1);
2023-01-18 00:44:47 +08:00
putenv("LIBUSB_DEBUG=3");
2023-01-17 19:26:37 +08:00
}
2023-01-18 00:44:47 +08:00
if (verbose >= 4)
putenv("LIBUSB_DEBUG=4");
2023-01-17 13:46:42 +08:00
if (start_from_pongo == true)
goto pongo;
LOG(LOG_INFO, "Waiting for devices");
2023-01-17 19:26:37 +08:00
do_pongo_sleep = 1;
2023-01-16 16:17:41 +08:00
pthread_t dfuhelper_thread;
pthread_create(&dfuhelper_thread, NULL, dfuhelper, NULL);
pthread_join(dfuhelper_thread, NULL);
2023-01-17 13:46:42 +08:00
if (dfuhelper_only)
return 0;
2023-01-16 17:33:12 +08:00
exec_checkra1n();
2023-01-17 13:46:42 +08:00
if (pongo_exit)
return 0;
pongo:
spin = true;
2023-01-18 00:44:47 +08:00
if (do_pongo_sleep)
sleep(2);
else
LOG(LOG_INFO, "Waiting for PongoOS devices...");
2023-01-17 13:46:42 +08:00
wait_for_pongo();
2023-01-18 00:44:47 +08:00
while (spin)
{
2023-01-17 13:46:42 +08:00
sleep(1);
}
2023-01-18 17:58:20 +08:00
cleanup:
if (override_kpf.magic == OVERRIDE_MAGIC) {
munmap(override_kpf.ptr, (size_t)override_kpf.len);
close(override_kpf.fd);
}
if (override_ramdisk.magic == OVERRIDE_MAGIC) {
munmap(override_ramdisk.ptr, (size_t)override_ramdisk.len);
close(override_ramdisk.fd);
}
if (override_overlay.magic == OVERRIDE_MAGIC) {
munmap(override_overlay.ptr, (size_t)override_overlay.len);
close(override_overlay.fd);
}
2023-01-16 16:17:41 +08:00
return 0;
}