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-22 20:47:52 +08:00
|
|
|
#include <spawn.h>
|
2023-01-18 17:58:20 +08:00
|
|
|
#include <sys/mman.h>
|
2023-01-24 03:32:29 +08:00
|
|
|
#include <time.h>
|
2023-01-16 16:17:41 +08:00
|
|
|
|
2023-01-25 16:46:32 +08:00
|
|
|
#include <libusb-1.0/libusb.h>
|
2023-01-17 19:26:37 +08:00
|
|
|
#include <libimobiledevice/libimobiledevice.h>
|
|
|
|
|
|
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-25 15:42:40 +08:00
|
|
|
unsigned int verbose = 0U;
|
2023-01-22 17:38:47 +08:00
|
|
|
int enable_rootful = 0;
|
2023-01-17 19:26:37 +08:00
|
|
|
int do_pongo_sleep = 0;
|
2023-01-25 15:42:40 +08:00
|
|
|
int device_has_booted = 0;
|
2023-01-17 21:40:32 +08:00
|
|
|
int demote = 0;
|
2023-01-25 15:42:40 +08:00
|
|
|
int pongo_thr_running = 0, dfuhelper_thr_running = 0;
|
2023-01-22 20:47:52 +08:00
|
|
|
bool ohio = true;
|
2023-01-22 23:42:22 +08:00
|
|
|
char xargs_cmd[0x270] = "xargs wdt=-1";
|
2023-01-18 16:02:43 +08:00
|
|
|
char checkrain_flags_cmd[0x20] = "checkra1n_flags 0x0";
|
2023-01-22 23:20:46 +08:00
|
|
|
char palerain_flags_cmd[0x20] = "palera1n_flags 0x0";
|
|
|
|
|
char kpf_flags_cmd[0x20] = "kpf_flags 0x0";
|
2023-01-22 17:38:47 +08:00
|
|
|
char dtpatch_cmd[0x20] = "dtpatch md0";
|
|
|
|
|
char rootfs_cmd[512];
|
2023-01-22 20:47:52 +08:00
|
|
|
extern char** environ;
|
2023-01-17 13:46:42 +08:00
|
|
|
|
2023-01-25 15:42:40 +08:00
|
|
|
bool dfuhelper_only = false;
|
|
|
|
|
bool pongo_exit = false;
|
|
|
|
|
bool palerain_version = false;
|
|
|
|
|
|
2023-01-23 01:24:22 +08:00
|
|
|
|
|
|
|
|
niarelap_file_t* kpf_to_upload_1 = &checkra1n_kpf_pongo;
|
|
|
|
|
niarelap_file_t* ramdisk_to_upload_1 = &ramdisk_dmg;
|
2023-01-22 14:02:20 -05:00
|
|
|
niarelap_file_t* overlay_to_upload_1 = &binpack_dmg;
|
2023-01-23 01:24:22 +08:00
|
|
|
|
|
|
|
|
niarelap_file_t** kpf_to_upload = &kpf_to_upload_1;
|
|
|
|
|
niarelap_file_t** ramdisk_to_upload = &ramdisk_to_upload_1;
|
|
|
|
|
niarelap_file_t** overlay_to_upload = &overlay_to_upload_1;
|
|
|
|
|
|
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-22 17:38:47 +08:00
|
|
|
uint32_t palerain_flags = 0;
|
2023-01-22 23:20:46 +08:00
|
|
|
uint32_t kpf_flags = 0;
|
2023-01-18 16:02:43 +08:00
|
|
|
|
2023-01-25 15:42:40 +08:00
|
|
|
pthread_t dfuhelper_thread, pongo_thread;
|
|
|
|
|
pthread_mutex_t log_mutex;
|
2023-01-16 16:17:41 +08:00
|
|
|
|
2023-01-18 22:35:22 +08:00
|
|
|
int build_checks() {
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
struct mach_header_64* c1_header = (struct mach_header_64*)&checkra1n[0];
|
|
|
|
|
if (c1_header->magic != MH_MAGIC_64 && c1_header->magic != MH_CIGAM_64) {
|
|
|
|
|
LOG(LOG_FATAL, "Broken build: checkra1n is not a thin Mach-O");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (c1_header->cputype != _mh_execute_header.cputype) {
|
|
|
|
|
LOG(LOG_FATAL, "Broken build: checkra1n CPU type is not the same as %s CPU type", getprogname());
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
struct mach_header_64 *kpf_hdr = (struct mach_header_64 *)checkra1n_kpf_pongo;
|
|
|
|
|
if (kpf_hdr->magic != MH_MAGIC_64 && kpf_hdr->magic != MH_CIGAM_64) {
|
|
|
|
|
LOG(LOG_FATAL, "Broken build: Invalid kernel patchfinder: Not thin 64-bit Mach-O");
|
|
|
|
|
return -1;
|
|
|
|
|
} else if (kpf_hdr->filetype != MH_KEXT_BUNDLE) {
|
|
|
|
|
LOG(LOG_FATAL, "Broken build: Invalid kernel patchfinder: Not a kext bundle");
|
|
|
|
|
return -1;
|
|
|
|
|
} else if (kpf_hdr->cputype != CPU_TYPE_ARM64) {
|
|
|
|
|
LOG(LOG_FATAL, "Broken build: Invalid kernel patchfinder: CPU type is not arm64");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-25 15:42:40 +08:00
|
|
|
void thr_cleanup(void* ptr) {
|
|
|
|
|
*(int*)ptr = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-01-17 13:46:42 +08:00
|
|
|
|
2023-01-25 15:42:40 +08:00
|
|
|
int palera1n(int argc, char *argv[]) {
|
2023-01-25 16:46:32 +08:00
|
|
|
int ret = 0;
|
2023-01-25 15:42:40 +08:00
|
|
|
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;
|
|
|
|
|
}
|
2023-01-18 22:35:22 +08:00
|
|
|
if (build_checks()) return -1;
|
2023-01-25 16:46:32 +08:00
|
|
|
if ((ret = optparse(argc, argv))) goto cleanup;
|
2023-01-17 21:40:32 +08:00
|
|
|
LOG(LOG_INFO, "Waiting for devices");
|
2023-01-25 15:42:40 +08:00
|
|
|
|
2023-01-17 19:26:37 +08:00
|
|
|
do_pongo_sleep = 1;
|
2023-01-16 16:17:41 +08:00
|
|
|
pthread_create(&dfuhelper_thread, NULL, dfuhelper, NULL);
|
2023-01-25 15:42:40 +08:00
|
|
|
pthread_create(&pongo_thread, NULL, pongo_helper, NULL);
|
2023-01-16 16:17:41 +08:00
|
|
|
pthread_join(dfuhelper_thread, NULL);
|
2023-01-25 15:42:40 +08:00
|
|
|
if (device_has_booted) goto done;
|
2023-01-17 13:46:42 +08:00
|
|
|
if (dfuhelper_only)
|
|
|
|
|
return 0;
|
2023-01-25 15:42:40 +08:00
|
|
|
if (pongo_thr_running) {
|
|
|
|
|
pthread_cancel(pongo_thread);
|
|
|
|
|
}
|
2023-01-16 17:33:12 +08:00
|
|
|
exec_checkra1n();
|
2023-01-17 13:46:42 +08:00
|
|
|
if (pongo_exit)
|
|
|
|
|
return 0;
|
2023-01-18 00:44:47 +08:00
|
|
|
else
|
|
|
|
|
LOG(LOG_INFO, "Waiting for PongoOS devices...");
|
2023-01-25 15:42:40 +08:00
|
|
|
spin = true;
|
|
|
|
|
sleep(2);
|
|
|
|
|
pthread_create(&pongo_thread, NULL, pongo_helper, NULL);
|
|
|
|
|
pthread_join(pongo_thread, NULL);
|
2023-01-18 00:44:47 +08:00
|
|
|
while (spin)
|
|
|
|
|
{
|
2023-01-17 13:46:42 +08:00
|
|
|
sleep(1);
|
|
|
|
|
}
|
2023-01-25 15:42:40 +08:00
|
|
|
done:
|
2023-01-22 20:47:52 +08:00
|
|
|
if (access("/usr/bin/curl", F_OK) == 0 && ohio) {
|
|
|
|
|
LOG(LOG_VERBOSE4, "Ohio");
|
|
|
|
|
char* ohio_argv[] = {
|
|
|
|
|
"/usr/bin/curl",
|
|
|
|
|
"-sX",
|
|
|
|
|
"POST",
|
|
|
|
|
"-d",
|
|
|
|
|
"{\"app_name\": \"palera1n_c-rewrite\"}",
|
|
|
|
|
"-H",
|
|
|
|
|
"Content-Type: application/json",
|
|
|
|
|
"-H",
|
|
|
|
|
"User-Agent: python-requests/99 palera1n-c-rewrite/0",
|
2023-01-23 01:24:22 +08:00
|
|
|
"-o",
|
|
|
|
|
"/dev/null",
|
2023-01-22 20:47:52 +08:00
|
|
|
"https://ohio.itsnebula.net/hit",
|
|
|
|
|
NULL
|
|
|
|
|
};
|
|
|
|
|
pid_t pid;
|
|
|
|
|
posix_spawn(&pid, ohio_argv[0], NULL, NULL, ohio_argv, environ);
|
|
|
|
|
}
|
|
|
|
|
|
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-25 15:42:40 +08:00
|
|
|
pthread_mutex_destroy(&log_mutex);
|
2023-01-25 16:46:32 +08:00
|
|
|
return ret;
|
2023-01-16 16:17:41 +08:00
|
|
|
}
|
2023-01-25 15:42:40 +08:00
|
|
|
|
|
|
|
|
int main (int argc, char* argv[]) {
|
|
|
|
|
return palera1n(argc, argv);
|
|
|
|
|
}
|