A tool for deriving PKG packet encryption keys for ps4 written in c++
| 1 | /* |
| 2 | * ╔═══════════════════════════════════════════════════════════════════════════╗ |
| 3 | * ║ PS4 KERNEL STUBS - HEADER ║ |
| 4 | * ╠═══════════════════════════════════════════════════════════════════════════╣ |
| 5 | * ║ Emulates sceKernel* syscalls for PS4 compatibility. ║ |
| 6 | * ║ Based on shadPS4 implementation patterns. ║ |
| 7 | * ╚═══════════════════════════════════════════════════════════════════════════╝ |
| 8 | */ |
| 9 | |
| 10 | #pragma once |
| 11 | |
| 12 | #include <cstdint> |
| 13 | |
| 14 | /* |
| 15 | * ┌─────────────────────────────────────────────────────────────────┐ |
| 16 | * │ ERROR CODES │ |
| 17 | * │ PS4 uses ORBIS_* error codes (negative values) │ |
| 18 | * └─────────────────────────────────────────────────────────────────┘ |
| 19 | */ |
| 20 | #define ORBIS_OK 0 |
| 21 | #define ORBIS_KERNEL_ERROR_UNKNOWN 0x80020000 |
| 22 | #define ORBIS_KERNEL_ERROR_EPERM 0x80020001 |
| 23 | #define ORBIS_KERNEL_ERROR_ENOENT 0x80020002 |
| 24 | #define ORBIS_KERNEL_ERROR_ESRCH 0x80020003 |
| 25 | #define ORBIS_KERNEL_ERROR_EINTR 0x80020004 |
| 26 | #define ORBIS_KERNEL_ERROR_EIO 0x80020005 |
| 27 | #define ORBIS_KERNEL_ERROR_ENOMEM 0x8002000C |
| 28 | #define ORBIS_KERNEL_ERROR_EACCES 0x8002000D |
| 29 | #define ORBIS_KERNEL_ERROR_EFAULT 0x8002000E |
| 30 | #define ORBIS_KERNEL_ERROR_EBUSY 0x80020010 |
| 31 | #define ORBIS_KERNEL_ERROR_EEXIST 0x80020011 |
| 32 | #define ORBIS_KERNEL_ERROR_EINVAL 0x80020016 |
| 33 | #define ORBIS_KERNEL_ERROR_ENOSPC 0x8002001C |
| 34 | #define ORBIS_KERNEL_ERROR_EAGAIN 0x80020023 |
| 35 | #define ORBIS_KERNEL_ERROR_ETIMEDOUT 0x8002003C |
| 36 | |
| 37 | /* |
| 38 | * ┌─────────────────────────────────────────────────────────────────┐ |
| 39 | * │ MEMORY CONSTANTS │ |
| 40 | * └─────────────────────────────────────────────────────────────────┘ |
| 41 | */ |
| 42 | constexpr uint64_t ORBIS_KERNEL_TOTAL_MEM = 5248ULL * 1024 * 1024; // 5248 MB |
| 43 | |
| 44 | // Memory protection flags |
| 45 | #define ORBIS_KERNEL_PROT_CPU_READ 0x01 |
| 46 | #define ORBIS_KERNEL_PROT_CPU_WRITE 0x02 |
| 47 | #define ORBIS_KERNEL_PROT_CPU_RW 0x02 |
| 48 | #define ORBIS_KERNEL_PROT_GPU_READ 0x10 |
| 49 | #define ORBIS_KERNEL_PROT_GPU_WRITE 0x20 |
| 50 | #define ORBIS_KERNEL_PROT_GPU_RW 0x30 |
| 51 | |
| 52 | // Memory types |
| 53 | #define ORBIS_KERNEL_WB_ONION 0 // Write-back (Onion bus) |
| 54 | #define ORBIS_KERNEL_WC_GARLIC 3 // Write-combining (Garlic bus) |
| 55 | #define ORBIS_KERNEL_WB_GARLIC 10 // Write-back (Garlic bus) |
| 56 | |
| 57 | /* |
| 58 | * ┌─────────────────────────────────────────────────────────────────┐ |
| 59 | * │ STRUCTURES │ |
| 60 | * └─────────────────────────────────────────────────────────────────┘ |
| 61 | */ |
| 62 | |
| 63 | struct OrbisKernelTimespec { |
| 64 | int64_t tv_sec; |
| 65 | int64_t tv_nsec; |
| 66 | }; |
| 67 | |
| 68 | struct OrbisKernelTimeval { |
| 69 | int64_t tv_sec; |
| 70 | int64_t tv_usec; |
| 71 | }; |
| 72 | |
| 73 | struct OrbisKernelStat { |
| 74 | uint32_t st_dev; |
| 75 | uint32_t st_ino; |
| 76 | uint16_t st_mode; |
| 77 | uint16_t st_nlink; |
| 78 | uint32_t st_uid; |
| 79 | uint32_t st_gid; |
| 80 | uint32_t st_rdev; |
| 81 | OrbisKernelTimespec st_atim; |
| 82 | OrbisKernelTimespec st_mtim; |
| 83 | OrbisKernelTimespec st_ctim; |
| 84 | int64_t st_size; |
| 85 | int64_t st_blocks; |
| 86 | uint32_t st_blksize; |
| 87 | uint32_t st_flags; |
| 88 | uint32_t st_gen; |
| 89 | int32_t st_lspare; |
| 90 | OrbisKernelTimespec st_birthtim; |
| 91 | uint8_t padding[8]; |
| 92 | }; |
| 93 | |
| 94 | /* |
| 95 | * ┌─────────────────────────────────────────────────────────────────┐ |
| 96 | * │ SYSCALL DECLARATIONS │ |
| 97 | * └─────────────────────────────────────────────────────────────────┘ |
| 98 | */ |
| 99 | |
| 100 | extern "C" { |
| 101 | |
| 102 | // ═══════════════════════════════════════════════════════════════════ |
| 103 | // MEMORY SYSCALLS |
| 104 | // ═══════════════════════════════════════════════════════════════════ |
| 105 | uint64_t sceKernelGetDirectMemorySize(); |
| 106 | int32_t sceKernelAllocateDirectMemory(int64_t searchStart, int64_t searchEnd, |
| 107 | uint64_t len, uint64_t alignment, |
| 108 | int32_t memoryType, int64_t *physAddrOut); |
| 109 | int32_t sceKernelMapDirectMemory(void **addr, uint64_t len, int32_t prot, |
| 110 | int32_t flags, int64_t physAddr, |
| 111 | uint64_t alignment); |
| 112 | int32_t sceKernelMapFlexibleMemory(void **addrInOut, uint64_t len, int32_t prot, |
| 113 | int32_t flags); |
| 114 | int32_t sceKernelMunmap(void *addr, uint64_t len); |
| 115 | int32_t sceKernelMprotect(const void *addr, uint64_t size, int32_t prot); |
| 116 | |
| 117 | // ═══════════════════════════════════════════════════════════════════ |
| 118 | // FILE SYSCALLS |
| 119 | // ═══════════════════════════════════════════════════════════════════ |
| 120 | int32_t sceKernelOpen(const char *path, int32_t flags, uint16_t mode); |
| 121 | int32_t sceKernelClose(int32_t fd); |
| 122 | int64_t sceKernelRead(int32_t fd, void *buf, uint64_t nbytes); |
| 123 | int64_t sceKernelWrite(int32_t fd, const void *buf, uint64_t nbytes); |
| 124 | int64_t sceKernelLseek(int32_t fd, int64_t offset, int32_t whence); |
| 125 | int32_t sceKernelStat(const char *path, OrbisKernelStat *sb); |
| 126 | int32_t sceKernelFstat(int32_t fd, OrbisKernelStat *sb); |
| 127 | int32_t sceKernelMkdir(const char *path, uint16_t mode); |
| 128 | |
| 129 | // ═══════════════════════════════════════════════════════════════════ |
| 130 | // TIME SYSCALLS |
| 131 | // ═══════════════════════════════════════════════════════════════════ |
| 132 | uint64_t sceKernelGetProcessTime(); |
| 133 | uint64_t sceKernelGetProcessTimeCounter(); |
| 134 | uint64_t sceKernelGetProcessTimeCounterFrequency(); |
| 135 | int32_t sceKernelGettimeofday(OrbisKernelTimeval *tv); |
| 136 | int32_t sceKernelClockGettime(int32_t clockId, OrbisKernelTimespec *tp); |
| 137 | int32_t sceKernelUsleep(uint32_t microseconds); |
| 138 | int32_t sceKernelNanosleep(const OrbisKernelTimespec *rqtp, |
| 139 | OrbisKernelTimespec *rmtp); |
| 140 | |
| 141 | // ═══════════════════════════════════════════════════════════════════ |
| 142 | // PROCESS SYSCALLS |
| 143 | // ═══════════════════════════════════════════════════════════════════ |
| 144 | int32_t sceKernelGetCurrentCpu(); |
| 145 | int32_t sceKernelGetProcessId(); |
| 146 | |
| 147 | // ═══════════════════════════════════════════════════════════════════ |
| 148 | // ERROR HANDLING |
| 149 | // ═══════════════════════════════════════════════════════════════════ |
| 150 | int32_t *__Error(); // Returns pointer to thread-local errno |
| 151 | |
| 152 | } // extern "C" |
| 153 |