A tool for deriving PKG packet encryption keys for ps4 written in c++
| 1 | /* |
| 2 | * ╔═══════════════════════════════════════════════════════════════════════════╗ |
| 3 | * ║ PS4 MEMORY EMULATION LAYER ║ |
| 4 | * ╠═══════════════════════════════════════════════════════════════════════════╣ |
| 5 | * ║ Provides emulation of PS4 global memory space on host platforms. ║ |
| 6 | * ║ The PS4 binary has hardcoded addresses that we need to make accessible. ║ |
| 7 | * ╚═══════════════════════════════════════════════════════════════════════════╝ |
| 8 | */ |
| 9 | |
| 10 | #pragma once |
| 11 | |
| 12 | #include <cstddef> |
| 13 | #include <cstdint> |
| 14 | |
| 15 | namespace PS4Emu { |
| 16 | |
| 17 | /* |
| 18 | * ┌─────────────────────────────────────────────────────────────────┐ |
| 19 | * │ MEMORY LAYOUT CONSTANTS │ |
| 20 | * └─────────────────────────────────────────────────────────────────┘ |
| 21 | * PS4 typical memory regions: |
| 22 | * 0x00000000 - 0x00FFFFFF : Low memory (globals, TLS) |
| 23 | * 0x00400000 - 0x00FFFFFF : Executable base (eboot.bin) |
| 24 | * 0x01000000+ : Heap, libraries |
| 25 | */ |
| 26 | |
| 27 | // Base address where we'll map PS4 global space |
| 28 | // We use a high address to avoid conflicts with host allocations |
| 29 | constexpr uint64_t PS4_GLOBAL_BASE = 0x100000000ULL; |
| 30 | |
| 31 | // Size of the global memory region (16MB should cover most globals) |
| 32 | constexpr size_t PS4_GLOBAL_SIZE = 16 * 1024 * 1024; |
| 33 | |
| 34 | // Maximum address we'll redirect (anything above this is likely heap) |
| 35 | constexpr uint64_t PS4_GLOBAL_MAX = 0x01000000ULL; |
| 36 | |
| 37 | /* |
| 38 | * ┌─────────────────────────────────────────────────────────────────┐ |
| 39 | * │ INITIALIZATION API │ |
| 40 | * └─────────────────────────────────────────────────────────────────┘ |
| 41 | */ |
| 42 | |
| 43 | // Initialize the PS4 memory emulation layer |
| 44 | // Must be called before any game code runs |
| 45 | bool InitializeMemory(); |
| 46 | |
| 47 | // Cleanup the memory layer |
| 48 | void ShutdownMemory(); |
| 49 | |
| 50 | // Get the host pointer for a PS4 global address |
| 51 | // Returns nullptr if address is out of range |
| 52 | void *TranslateAddress(uint64_t ps4_addr); |
| 53 | |
| 54 | // Debug: Frame counter functions to verify main loop execution |
| 55 | uint64_t GetFrameCounter(); |
| 56 | void IncrementFrameCounter(); |
| 57 | void *GetGlobalMemoryBase(); |
| 58 | |
| 59 | /* |
| 60 | * ┌─────────────────────────────────────────────────────────────────┐ |
| 61 | * │ ACCESS MACROS │ |
| 62 | * └─────────────────────────────────────────────────────────────────┘ |
| 63 | * These macros provide safe access to PS4 global memory. |
| 64 | * Example: instead of *(int64_t*)0x9d9e28 |
| 65 | * use PS4_GLOBAL_I64(0x9d9e28) |
| 66 | */ |
| 67 | |
| 68 | #define PS4_GLOBAL_PTR(addr) (PS4Emu::TranslateAddress(addr)) |
| 69 | |
| 70 | #define PS4_GLOBAL_I8(addr) (*reinterpret_cast<int8_t *>(PS4_GLOBAL_PTR(addr))) |
| 71 | |
| 72 | #define PS4_GLOBAL_I16(addr) \ |
| 73 | (*reinterpret_cast<int16_t *>(PS4_GLOBAL_PTR(addr))) |
| 74 | |
| 75 | #define PS4_GLOBAL_I32(addr) \ |
| 76 | (*reinterpret_cast<int32_t *>(PS4_GLOBAL_PTR(addr))) |
| 77 | |
| 78 | #define PS4_GLOBAL_I64(addr) \ |
| 79 | (*reinterpret_cast<int64_t *>(PS4_GLOBAL_PTR(addr))) |
| 80 | |
| 81 | #define PS4_GLOBAL_U8(addr) (*reinterpret_cast<uint8_t *>(PS4_GLOBAL_PTR(addr))) |
| 82 | |
| 83 | #define PS4_GLOBAL_U16(addr) \ |
| 84 | (*reinterpret_cast<uint16_t *>(PS4_GLOBAL_PTR(addr))) |
| 85 | |
| 86 | #define PS4_GLOBAL_U32(addr) \ |
| 87 | (*reinterpret_cast<uint32_t *>(PS4_GLOBAL_PTR(addr))) |
| 88 | |
| 89 | #define PS4_GLOBAL_U64(addr) \ |
| 90 | (*reinterpret_cast<uint64_t *>(PS4_GLOBAL_PTR(addr))) |
| 91 | |
| 92 | } // namespace PS4Emu |
| 93 |