Files

230 lines
6.6 KiB
C
Raw Permalink Normal View History

2023-02-06 01:03:29 +08:00
#ifndef PALERAIN_H
#define PALERAIN_H
2023-01-16 16:17:41 +08:00
2023-02-10 16:56:58 +09:00
#if defined(__APPLE__)
2023-03-25 13:35:44 +08:00
#define NSIG __DARWIN_NSIG
2023-02-10 16:56:58 +09:00
#endif
2023-01-28 16:24:22 +08:00
#include "xxd-embedded.h"
2023-05-29 01:56:12 +08:00
#include "paleinfo.h"
2023-01-16 16:17:41 +08:00
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <stdarg.h>
#include <usbmuxd.h>
#include <libirecovery.h>
2023-01-25 21:22:42 +08:00
#include <libimobiledevice/libimobiledevice.h>
2023-01-16 16:17:41 +08:00
#define LOG(loglevel, ...) p1_log(loglevel, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define CLEAR() printf("\33[2K\r");
2023-02-11 01:25:01 +08:00
#define palerain_option_case(x) (512 + x)
2023-01-18 16:02:43 +08:00
#define palerain_option_version 129
2023-01-25 21:22:42 +08:00
#define CMD_LEN_MAX 512
#define OVERRIDE_MAGIC 0xd803b376
2023-02-11 01:25:01 +08:00
#define palerain_option_case_version palerain_option_case(0)
#define palerain_option_case_force_revert palerain_option_case(1)
2023-12-26 16:41:01 +08:00
#define palerain_option_case_libcheckra1nhelper_path palerain_option_case(2)
2024-03-27 22:50:45 -07:00
#define palerain_option_case_cli palerain_option_case(3)
2023-12-26 16:41:01 +08:00
2023-02-11 01:25:01 +08:00
2023-02-08 16:47:04 +08:00
#ifdef USE_LIBUSB
2023-02-08 17:14:00 +08:00
#include <libusb-1.0/libusb.h>
2023-02-08 16:47:04 +08:00
2023-01-25 21:22:42 +08:00
#define USB_RET_SUCCESS LIBUSB_SUCCESS
#define USB_RET_NOT_RESPONDING LIBUSB_ERROR_OTHER
#define USB_RET_IO LIBUSB_ERROR_IO
2023-02-14 01:36:16 +08:00
#define USB_RET_NO_DEVICE LIBUSB_ERROR_NO_DEVICE
2023-10-30 17:31:54 +08:00
typedef int32_t usb_ret_t;
2023-02-08 16:47:04 +08:00
typedef libusb_device_handle *usb_device_handle_t;
typedef struct stuff
{
pthread_t th;
libusb_device *dev;
usb_device_handle_t handle;
} stuff_t;
usb_ret_t USBBulkUpload(usb_device_handle_t handle, void *data, int len);
#else
2023-02-08 17:29:00 +08:00
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpedantic"
2023-02-08 16:47:04 +08:00
#include <IOKit/IOKitLib.h>
#include <IOKit/usb/IOUSBLib.h>
#include <IOKit/IOCFPlugIn.h>
2023-02-08 17:29:00 +08:00
#pragma clang diagnostic pop
2023-02-08 16:47:04 +08:00
#define USB_RET_SUCCESS KERN_SUCCESS
#define USB_RET_NOT_RESPONDING kIOReturnNotResponding
2023-02-14 01:36:16 +08:00
#define USB_RET_IO kIOReturnNotReady
#define USB_RET_NO_DEVICE kIOReturnNoDevice
2023-02-08 16:47:04 +08:00
typedef IOReturn usb_ret_t;
typedef IOUSBInterfaceInterface245 **usb_device_handle_t;
typedef struct
{
pthread_t th;
volatile uint64_t regID;
IOUSBDeviceInterface245 **dev;
usb_device_handle_t handle;
} stuff_t;
usb_ret_t USBBulkUpload(usb_device_handle_t handle, void *data, uint32_t len);
#endif
2023-01-28 20:41:18 +08:00
#ifndef PALERAIN_VERSION
2024-04-06 23:27:13 +08:00
#define PALERAIN_VERSION "2.0"
2023-01-28 20:41:18 +08:00
#endif
2023-01-25 21:22:42 +08:00
#if defined(__APPLE__)
#include <mach-o/loader.h>
#include <mach-o/ldsyms.h>
#else
#define MH_MAGIC_64 0xfeedfacf
#define MH_CIGAM_64 0xcffaedfe
#define MH_KEXT_BUNDLE 0xb
2023-10-30 17:31:54 +08:00
typedef int32_t cpu_type_t;
typedef int32_t cpu_subtype_t;
2023-01-25 21:22:42 +08:00
#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
typedef void *(*pthread_start_t)(void *);
2023-02-08 16:47:04 +08:00
// Keep in sync with Pongo
#define PONGO_USB_VENDOR 0x05ac
#define PONGO_USB_PRODUCT 0x4141
#define CMD_LEN_MAX 512
#define UPLOADSZ_MAX (1024 * 1024 * 128)
2023-01-16 16:17:41 +08:00
typedef enum {
LOG_FATAL = 0,
LOG_ERROR = 1,
LOG_WARNING = 2,
LOG_INFO = 3,
2023-01-18 00:44:47 +08:00
LOG_VERBOSE = 4,
2023-01-18 17:58:20 +08:00
LOG_VERBOSE2 = 5,
LOG_VERBOSE3 = 6,
LOG_VERBOSE4 = 7,
2023-01-22 23:20:46 +08:00
LOG_VERBOSE5 = 8,
2023-01-16 16:17:41 +08:00
} log_level_t;
typedef struct {
uint64_t ecid;
char* productType;
char* productVersion;
char* buildVersion;
char* CPUArchitecture;
const char* displayName;
} devinfo_t;
typedef struct {
int mode;
2023-10-30 17:31:54 +08:00
int cpid;
2023-01-16 16:17:41 +08:00
char product_type[0x20];
char display_name[0x20];
char iboot_ver[0x20];
} recvinfo_t;
2023-01-18 17:58:20 +08:00
typedef struct {
uint32_t magic; /* 0xd803b376*/
unsigned char* ptr; /* pointer to the override file in memory */
2023-10-30 17:31:54 +08:00
uint32_t len; /* length of override file */
2023-01-18 17:58:20 +08:00
unsigned char* orig_ptr; /* pointer to the overriden file */
2023-10-30 17:31:54 +08:00
uint32_t orig_len; /* length of the overriden file */
2023-01-18 17:58:20 +08:00
int fd; /* file descriptor of the override file */
} override_file_t;
2023-01-25 21:22:42 +08:00
typedef unsigned char niarelap_file_t[];
2023-01-18 16:02:43 +08:00
extern unsigned int verbose;
2023-01-25 21:22:42 +08:00
2023-01-22 19:18:35 +08:00
extern char* pongo_path;
2023-03-29 18:46:01 +08:00
#ifdef TUI
2023-02-10 18:08:29 +08:00
extern bool tui_started;
2023-01-29 13:17:30 +08:00
#endif
2023-01-16 16:17:41 +08:00
2023-05-29 01:56:12 +08:00
extern uint64_t palerain_flags;
extern uint64_t* palerain_flags_p;
2023-02-10 18:08:29 +08:00
2023-01-25 21:22:42 +08:00
extern pthread_t dfuhelper_thread, pongo_thread;
extern int pongo_thr_running, dfuhelper_thr_running;
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;
2023-05-29 01:56:12 +08:00
extern uint64_t palerain_flags;
2023-01-25 21:22:42 +08:00
extern pthread_mutex_t log_mutex;
2023-01-30 21:41:15 +08:00
extern pthread_mutex_t spin_mutex, found_pongo_mutex, ecid_dfu_wait_mutex;
2023-01-25 21:22:42 +08:00
extern int pongo_thr_running, dfuhelper_thr_running;
2023-02-10 18:08:29 +08:00
extern bool device_has_booted;
2023-01-25 21:22:42 +08:00
extern char xargs_cmd[0x270];
2023-05-30 15:40:03 +08:00
extern char palerain_flags_cmd[0x30];
2023-02-03 13:22:07 +08:00
extern char* ext_checkra1n;
2023-01-25 21:22:42 +08:00
void thr_cleanup(void* ptr);
2023-01-16 16:17:41 +08:00
void* dfuhelper(void* ptr);
2023-02-15 15:10:04 +08:00
int p1_log(log_level_t loglevel, const char *fname, int lineno, const char *fxname, const char* __restrict format, ...);
2023-01-16 16:17:41 +08:00
/* devhelper helpers */
void devinfo_free(devinfo_t *dev);
bool cpid_is_arm64(unsigned int cpid);
/* devhelper commands */
int subscribe_cmd(usbmuxd_event_cb_t device_event_cb, irecv_device_event_cb_t irecv_event_cb);
int unsubscribe_cmd(void);
2023-01-16 16:17:41 +08:00
int devinfo_cmd(devinfo_t *dev, const char *udid);
int enter_recovery_cmd(const char* udid);
int reboot_cmd(const char* udid);
2023-01-25 22:09:38 +08:00
int passstat_cmd(unsigned char* status, const char* udid);
2023-01-16 16:17:41 +08:00
int recvinfo_cmd(recvinfo_t* info, const uint64_t ecid);
int autoboot_cmd(const uint64_t ecid);
int exitrecv_cmd(const uint64_t ecid);
int exec_checkra1n(void);
2023-01-25 21:22:42 +08:00
int override_file(override_file_t *finfo, niarelap_file_t** orig, unsigned int *orig_len, char *filename);
void* pongo_helper(void* ptr);
const unsigned char *
boyermoore_horspool_memmem(const unsigned char* haystack, size_t hlen,
const unsigned char* needle, size_t nlen);
2023-01-17 13:46:42 +08:00
extern void* pongo_usb_callback(stuff_t* arg);
2023-01-17 13:46:42 +08:00
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);
const char *usb_strerror(usb_ret_t err);
int wait_for_pongo(void);
int upload_pongo_file(usb_device_handle_t, unsigned char*, unsigned int);
int issue_pongo_command(usb_device_handle_t, char*);
int tui(void);
2023-01-25 21:22:42 +08:00
int optparse(int argc, char* argv[]);
2023-01-30 21:41:15 +08:00
bool get_spin(void);
2023-01-30 21:41:15 +08:00
bool set_spin(bool val);
bool get_found_pongo(void);
2023-02-01 20:10:18 +08:00
void* pongo_helper(void* _);
2023-01-30 21:41:15 +08:00
bool set_found_pongo(bool val);
uint64_t get_ecid_wait_for_dfu(void);
2023-01-30 21:41:15 +08:00
uint64_t set_ecid_wait_for_dfu(uint64_t ecid);
2023-02-05 15:04:42 +08:00
2023-02-08 16:47:04 +08:00
void write_stdout(char *buf, uint32_t len);
void io_start(stuff_t *stuff);
void io_stop(stuff_t *stuff);
void print_credits(void);
2023-02-11 19:23:57 +08:00
2023-02-07 16:19:06 +08:00
#endif