A tool for deriving PKG packet encryption keys for ps4 written in c++
| 1 | /* |
| 2 | * ╔═══════════════════════════════════════════════════════════════════════════╗ |
| 3 | * ║ PS4 MOCK RUNTIME LAYER ║ |
| 4 | * ╠═══════════════════════════════════════════════════════════════════════════╣ |
| 5 | * ║ Complete runtime emulation for decompiled PS4 games. ║ |
| 6 | * ║ Includes: Memory, VFS, Syscalls, Threading stubs. ║ |
| 7 | * ╚═══════════════════════════════════════════════════════════════════════════╝ |
| 8 | */ |
| 9 | |
| 10 | #include "runtime.h" |
| 11 | #include "ps4_memory.h" |
| 12 | #include "ps4_tls.h" |
| 13 | #include <cstring> |
| 14 | #include <filesystem> |
| 15 | #include <fstream> |
| 16 | #include <iostream> |
| 17 | #include <map> |
| 18 | |
| 19 | /* |
| 20 | * ┌─────────────────────────────────────────────────────────────────┐ |
| 21 | * │ VFS - VIRTUAL FILE SYSTEM │ |
| 22 | * └─────────────────────────────────────────────────────────────────┘ |
| 23 | * Translates PS4 paths to host filesystem paths. |
| 24 | * |
| 25 | * /app0/ → assets/ (Game installation) |
| 26 | * /data/ → savedata/ (Save game data) |
| 27 | * /host/ → debug/ (Debug output) |
| 28 | * /download0/ → dlc/ (DLC content) |
| 29 | */ |
| 30 | |
| 31 | static std::filesystem::path g_assetsRoot; |
| 32 | |
| 33 | std::string resolve_path(const std::string &ps4_path) { |
| 34 | std::string path = ps4_path; |
| 35 | |
| 36 | // Remove leading slash if present |
| 37 | if (!path.empty() && path[0] == '/') { |
| 38 | path = path.substr(1); |
| 39 | } |
| 40 | |
| 41 | // Mount point translation |
| 42 | if (path.find("app0/") == 0) { |
| 43 | return (g_assetsRoot / path.substr(5)).string(); |
| 44 | } |
| 45 | if (path.find("data/") == 0) { |
| 46 | return (g_assetsRoot.parent_path() / "savedata" / path.substr(5)).string(); |
| 47 | } |
| 48 | if (path.find("host/") == 0) { |
| 49 | return (g_assetsRoot.parent_path() / "debug" / path.substr(5)).string(); |
| 50 | } |
| 51 | if (path.find("download0/") == 0) { |
| 52 | return (g_assetsRoot.parent_path() / "dlc" / path.substr(10)).string(); |
| 53 | } |
| 54 | |
| 55 | // Default: assume it's relative to assets |
| 56 | return (g_assetsRoot / path).string(); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * ┌─────────────────────────────────────────────────────────────────┐ |
| 61 | * │ FILE DESCRIPTOR TABLE │ |
| 62 | * └─────────────────────────────────────────────────────────────────┘ |
| 63 | * Tracks open files for sceKernel* syscalls. |
| 64 | */ |
| 65 | |
| 66 | static std::map<int, std::fstream *> g_openFiles; |
| 67 | static int g_nextFd = 100; // Start at 100 to avoid conflicts |
| 68 | |
| 69 | /* |
| 70 | * ┌─────────────────────────────────────────────────────────────────┐ |
| 71 | * │ RUNTIME INITIALIZATION │ |
| 72 | * └─────────────────────────────────────────────────────────────────┘ |
| 73 | */ |
| 74 | |
| 75 | void runtime_init() { |
| 76 | std::cout << "[RUNTIME] ══════════════════════════════════════════════" |
| 77 | << std::endl; |
| 78 | std::cout << "[RUNTIME] Initializing PS4 Mock Runtime..." << std::endl; |
| 79 | |
| 80 | // Initialize TLS |
| 81 | if (!PS4Emu::InitializeTLS()) { |
| 82 | std::cerr << "[RUNTIME] FATAL: Failed to initialize TLS!" << std::endl; |
| 83 | std::exit(1); |
| 84 | } |
| 85 | |
| 86 | // Create main thread TCB |
| 87 | PS4Emu::Tcb *mainTcb = PS4Emu::CreateTcb(); |
| 88 | PS4Emu::SetTcbBase(mainTcb); |
| 89 | std::cout << "[RUNTIME] Main thread TCB initialized" << std::endl; |
| 90 | |
| 91 | // Initialize memory emulation |
| 92 | if (!PS4Emu::InitializeMemory()) { |
| 93 | std::cerr << "[RUNTIME] FATAL: Failed to initialize memory emulation!" |
| 94 | << std::endl; |
| 95 | std::exit(1); |
| 96 | } |
| 97 | |
| 98 | // Set up VFS root |
| 99 | g_assetsRoot = std::filesystem::current_path() / "assets"; |
| 100 | std::cout << "[RUNTIME] VFS Root: \"" << g_assetsRoot.string() << "\"" |
| 101 | << std::endl; |
| 102 | |
| 103 | // Create required directories |
| 104 | std::filesystem::create_directories(g_assetsRoot); |
| 105 | std::filesystem::create_directories(g_assetsRoot.parent_path() / "savedata"); |
| 106 | std::filesystem::create_directories(g_assetsRoot.parent_path() / "debug"); |
| 107 | |
| 108 | std::cout << "[RUNTIME] ══════════════════════════════════════════════" |
| 109 | << std::endl; |
| 110 | } |
| 111 | |
| 112 | void runtime_shutdown() { |
| 113 | std::cout << "[RUNTIME] Shutting down..." << std::endl; |
| 114 | |
| 115 | // Close all open files |
| 116 | for (auto &[fd, file] : g_openFiles) { |
| 117 | if (file) { |
| 118 | file->close(); |
| 119 | delete file; |
| 120 | } |
| 121 | } |
| 122 | g_openFiles.clear(); |
| 123 | |
| 124 | // Shutdown memory |
| 125 | PS4Emu::ShutdownMemory(); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * ┌─────────────────────────────────────────────────────────────────┐ |
| 130 | * │ PS4 KERNEL SYSCALLS │ |
| 131 | * └─────────────────────────────────────────────────────────────────┘ |
| 132 | * Note: Most syscalls are now implemented in: |
| 133 | * - ps4_kernel.cpp (file, memory, time syscalls) |
| 134 | * - ps4_pthread.cpp (threading primitives) |
| 135 | * - ps4_stubs.cpp (PAD, Audio, Video, User Service, etc.) |
| 136 | */ |
| 137 | |
| 138 | extern "C" { |
| 139 | |
| 140 | // ─────────────────────── MEMORY (ADDITIONAL) ─────────────────────── |
| 141 | |
| 142 | void *sceKernelMmap(void *addr, size_t len, int prot, int flags, int fd, |
| 143 | off_t offset) { |
| 144 | std::cout << "[SYSCALL] sceKernelMmap(len=" << len |
| 145 | << ") -> using PS4Emu::TranslateAddress" << std::endl; |
| 146 | return PS4Emu::TranslateAddress(reinterpret_cast<uint64_t>(addr)); |
| 147 | } |
| 148 | |
| 149 | // ─────────────────────── MODULES ─────────────────────── |
| 150 | |
| 151 | int sceKernelLoadStartModule(const char *path, size_t args, const void *argp, |
| 152 | unsigned int flags, void *option, int *result) { |
| 153 | std::cout << "[SYSCALL] sceKernelLoadStartModule(\"" << path << "\") -> STUB" |
| 154 | << std::endl; |
| 155 | if (result) |
| 156 | *result = 0; |
| 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | } // extern "C" |
| 161 |