Xbox Cloud Gaming app for PS5 Homebrew
C
fix(ps4): bypass dl_iterate_phdr crash, link native videodec2/random/netctl, and ensure sysmodules
Rafael Abud Ferreira767bd19249f7
diff --git a/CMakeLists.txt b/CMakeLists.txtindex de27842..dc8b86f 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -189,6 +189,7 @@ elseif(XC_TARGET_PS4)xcloud_corexc_ffmpegdatachannel-static+ -lSceVideodec2-lSceVideoOut-lScePad-lSceAudioOut@@ -196,6 +197,8 @@ elseif(XC_TARGET_PS4)-lSceKeyboard-lSceMouse-lSceNet+ -lSceNetCtl+ -lSceRandom-lSceImeDialog-lSceCommonDialog-lSceUserServicediff --git a/src/app/crash_guard.cpp b/src/app/crash_guard.cppindex 7f945eb..e209966 100644--- a/src/app/crash_guard.cpp+++ b/src/app/crash_guard.cpp@@ -31,6 +31,11 @@ struct SceModuleInfoForUnwind {};int sceKernelGetModuleInfoForUnwind(uintptr_t addr, int flags, SceModuleInfoForUnwind* info);}+#elif defined(XC_TARGET_PS4) || defined(__ORBIS__)+// PS4 kernel does not supply auxv to musl libc; dl_iterate_phdr is not supported.+extern "C" {+extern const char __text_start[] __attribute__((weak));+}#else#include <link.h>#endif@@ -49,6 +54,9 @@ void initBase() {#if defined(XCLOUD_PS5)g_ebootBase = reinterpret_cast<uintptr_t>(__text_start);g_ebootEnd = reinterpret_cast<uintptr_t>(__text_end);+#elif defined(XC_TARGET_PS4) || defined(__ORBIS__)+ g_ebootBase = reinterpret_cast<uintptr_t>(__text_start);+ g_ebootEnd = g_ebootBase + 0x20000000;#elsedl_iterate_phdr([](struct dl_phdr_info* info, size_t, void*) -> int {@@ -135,6 +143,12 @@ void appendCodeAddr(char* buf, size_t cap, size_t& len, uintptr_t addr) {appendHex(buf, cap, len, addr - mod.seg0_addr);return;}+#elif defined(XC_TARGET_PS4) || defined(__ORBIS__)+ if (g_ebootBase != 0 && addr >= g_ebootBase && addr < g_ebootEnd) {+ appendStr(buf, cap, len, "eboot+");+ appendHex(buf, cap, len, addr - g_ebootBase);+ return;+ }#elsestruct Lookup {uintptr_t addr;diff --git a/src/app/ps4_main.cpp b/src/app/ps4_main.cppindex a4b67f4..ea9cb9c 100644--- a/src/app/ps4_main.cpp+++ b/src/app/ps4_main.cpp@@ -5,11 +5,13 @@#include "util/log.h"#include <fcntl.h>+#include <sys/stat.h>#include <unistd.h>int main(int argc, char** argv) {(void)argc;(void)argv;+ ::mkdir("/data/psbox", 0777);// Early diagnostic marker: verifies main() was entered before complex subsystemsfor (const char* path : {"/data/psbox/boot.log", "/data/homebrew/PSBOX00001/boot.log"}) {int fd = ::open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);diff --git a/src/media/audio_in_ps4.cpp b/src/media/audio_in_ps4.cppindex c80421b..886d648 100644--- a/src/media/audio_in_ps4.cpp+++ b/src/media/audio_in_ps4.cpp@@ -21,6 +21,7 @@ int sceAudioInInput(int handle, void* dest);int sceAudioInClose(int handle);int sceAudioInGetSilentState(int handle);int sceAudioInSetGain(int handle, int gain);+int sceSysmoduleLoadModuleInternal(uint32_t id);}namespace xc::media {@@ -41,6 +42,8 @@ bool AudioIn::open(AudioCallback cb) {if (running_) return true;callback_ = std::move(cb);+ sceSysmoduleLoadModuleInternal(0x80000002); // AUDIOIN+static bool initialised = false;if (!initialised) {int rc = sceAudioInInit();diff --git a/src/media/audio_out_ps4.cpp b/src/media/audio_out_ps4.cppindex c822120..1823437 100644--- a/src/media/audio_out_ps4.cpp+++ b/src/media/audio_out_ps4.cpp@@ -19,6 +19,7 @@ int sceAudioOutOutput(int handle, const void* ptr);int sceAudioOutClose(int handle);int sceAudioOutSetMixLevelPadSpk(int handle, int mixLevel);int sceAudioOutSetPadSpkVolume(int handle, int volume);+int sceSysmoduleLoadModuleInternal(uint32_t id);}namespace xc::media {@@ -74,6 +75,7 @@ bool audioStart() {if (g_running) return true;static bool initialised = false;if (!initialised) {+ sceSysmoduleLoadModuleInternal(0x80000001); // AUDIOOUTint rc = sceAudioOutInit();if (rc < 0 && rc != static_cast<int>(0x8026000C) && rc != static_cast<int>(0x8026000E)) {XC_LOGE("sceAudioOutInit: 0x%08x", static_cast<unsigned>(rc));diff --git a/src/media/hw_decoder_ps4.cpp b/src/media/hw_decoder_ps4.cppindex e29eb51..8123e90 100644--- a/src/media/hw_decoder_ps4.cpp+++ b/src/media/hw_decoder_ps4.cpp@@ -9,7 +9,6 @@#include <cstring>#include <fcntl.h>#include <unistd.h>-#include <dlfcn.h>extern "C" {int sceSysmoduleLoadModule(uint32_t id);@@ -78,27 +77,15 @@ struct Vdec2Output {uint32_t frameFormat, pitchBytes;};-using FnQueryComputeMem = int (*)(Vdec2ComputeMemory*);-using FnAllocComputeQueue = int (*)(const Vdec2ComputeConfig*, const Vdec2ComputeMemory*, void**);-using FnReleaseComputeQueue = int (*)(void*);-using FnQueryDecoderMem = int (*)(const Vdec2DecoderConfig*, Vdec2DecoderMemory*);-using FnCreateDecoder = int (*)(const Vdec2DecoderConfig*, const Vdec2DecoderMemory*, void**);-using FnDeleteDecoder = int (*)(void*);-using FnDecode = int (*)(void*, Vdec2Input*, Vdec2Frame*, Vdec2Output*);-using FnFlush = int (*)(void*, Vdec2Frame*, Vdec2Output*);--struct Videodec2Api {- FnQueryComputeMem queryComputeMem = nullptr;- FnAllocComputeQueue allocComputeQueue = nullptr;- FnReleaseComputeQueue releaseComputeQueue = nullptr;- FnQueryDecoderMem queryDecoderMem = nullptr;- FnCreateDecoder createDecoder = nullptr;- FnDeleteDecoder deleteDecoder = nullptr;- FnDecode decode = nullptr;- FnFlush flush = nullptr;- bool loaded = false;-};-static Videodec2Api g_vdec;+// PS4 libSceVideodec2 native functions linked via -lSceVideodec2+int sceVideodec2QueryComputeMemoryInfo(Vdec2ComputeMemory* mem);+int sceVideodec2AllocateComputeQueue(const Vdec2ComputeConfig* cfg, const Vdec2ComputeMemory* mem, void** queue);+int sceVideodec2ReleaseComputeQueue(void* queue);+int sceVideodec2QueryDecoderMemoryInfo(const Vdec2DecoderConfig* cfg, Vdec2DecoderMemory* mem);+int sceVideodec2CreateDecoder(const Vdec2DecoderConfig* cfg, const Vdec2DecoderMemory* mem, void** decoder);+int sceVideodec2DeleteDecoder(void* decoder);+int sceVideodec2Decode(void* decoder, Vdec2Input* input, Vdec2Frame* frame, Vdec2Output* output);+int sceVideodec2Flush(void* decoder, Vdec2Frame* frame, Vdec2Output* output);}namespace xc::media {@@ -109,7 +96,6 @@ constexpr uint32_t kVideodecModule = 0x00CF; // SCE_SYSMODULE_VIDEODEC2constexpr int kMemoryTypeOnion = 0; // Coherent CPU+GPUconstexpr int kMemoryTypeGarlic = 3; // GPU Direct Memoryconstexpr int kProtectionRw = 0x33;-constexpr const char* kProbeFile = "/data/psbox/hwdec.probe";inline size_t align16k(size_t n) {return (n + 0x3FFF) & ~static_cast<size_t>(0x3FFF);@@ -134,22 +120,6 @@ void* allocDirect(size_t size, int memoryType, const char* label) {return addr;}-void writeProbe(const char* state) {- int fd = ::open(kProbeFile, O_WRONLY | O_CREAT | O_TRUNC, 0666);- if (fd >= 0) {- (void)::write(fd, state, std::strlen(state));- ::close(fd);- }-}--void removeProbe() {- ::unlink(kProbeFile);-}--bool hasProbe() {- return ::access(kProbeFile, F_OK) == 0;-}-} // namespaceHwDecoder::~HwDecoder() {@@ -158,12 +128,12 @@ HwDecoder::~HwDecoder() {frm.size = sizeof(frm);Vdec2Output out{};out.size = sizeof(out);- if (g_vdec.flush) g_vdec.flush(decoder_, &frm, &out);- if (g_vdec.deleteDecoder) g_vdec.deleteDecoder(decoder_);+ sceVideodec2Flush(decoder_, &frm, &out);+ sceVideodec2DeleteDecoder(decoder_);decoder_ = nullptr;}if (computeQueue_) {- if (g_vdec.releaseComputeQueue) g_vdec.releaseComputeQueue(computeQueue_);+ sceVideodec2ReleaseComputeQueue(computeQueue_);computeQueue_ = nullptr;}frameBuffer_ = nullptr;@@ -171,37 +141,12 @@ HwDecoder::~HwDecoder() {}bool HwDecoder::init(int maxWidth, int maxHeight) {- if (hasProbe()) {- XC_LOGW("hwdec: previous crash probe file found (%s); disabling hardware decoding", kProbeFile);- return false;- }-- writeProbe("starting");-int rc = sceSysmoduleLoadModule(kVideodecModule);XC_LOGI("hwdec: sceSysmoduleLoadModule(VIDEODEC2) -> 0x%08x", static_cast<unsigned>(rc));if (rc < 0 && rc != static_cast<int>(0x80020002) && rc != static_cast<int>(0x8002000e)) {- removeProbe();return false;}- if (!g_vdec.loaded) {- g_vdec.queryComputeMem = reinterpret_cast<FnQueryComputeMem>(dlsym(RTLD_DEFAULT, "sceVideodec2QueryComputeMemoryInfo"));- g_vdec.allocComputeQueue = reinterpret_cast<FnAllocComputeQueue>(dlsym(RTLD_DEFAULT, "sceVideodec2AllocateComputeQueue"));- g_vdec.releaseComputeQueue = reinterpret_cast<FnReleaseComputeQueue>(dlsym(RTLD_DEFAULT, "sceVideodec2ReleaseComputeQueue"));- g_vdec.queryDecoderMem = reinterpret_cast<FnQueryDecoderMem>(dlsym(RTLD_DEFAULT, "sceVideodec2QueryDecoderMemoryInfo"));- g_vdec.createDecoder = reinterpret_cast<FnCreateDecoder>(dlsym(RTLD_DEFAULT, "sceVideodec2CreateDecoder"));- g_vdec.deleteDecoder = reinterpret_cast<FnDeleteDecoder>(dlsym(RTLD_DEFAULT, "sceVideodec2DeleteDecoder"));- g_vdec.decode = reinterpret_cast<FnDecode>(dlsym(RTLD_DEFAULT, "sceVideodec2Decode"));- g_vdec.flush = reinterpret_cast<FnFlush>(dlsym(RTLD_DEFAULT, "sceVideodec2Flush"));- if (!g_vdec.queryComputeMem || !g_vdec.allocComputeQueue || !g_vdec.createDecoder || !g_vdec.decode) {- XC_LOGW("hwdec: could not resolve all libSceVideodec2 symbols via dlsym; falling back to software decoding");- removeProbe();- return false;- }- g_vdec.loaded = true;- }-// Load firmware helper modules (available on retail PS4 firmware)for (const char* sprx : {"/system/common/lib/libSceVdecCore.sprx",@@ -218,18 +163,16 @@ bool HwDecoder::init(int maxWidth, int maxHeight) {// Query and allocate compute queue memoryVdec2ComputeMemory computeMem{};computeMem.size = sizeof(computeMem);- rc = g_vdec.queryComputeMem(&computeMem);+ rc = sceVideodec2QueryComputeMemoryInfo(&computeMem);XC_LOGI("hwdec: compute memory query: rc=0x%08x, %llu bytes", static_cast<unsigned>(rc),static_cast<unsigned long long>(computeMem.cpuGpuSize));if (rc != 0 || computeMem.cpuGpuSize == 0) {- removeProbe();return false;}size_t computeBytes = align16k(computeMem.cpuGpuSize);void* computeBuf = allocDirect(computeBytes, kMemoryTypeOnion, "compute queue");if (!computeBuf) {- removeProbe();return false;}computeMem.cpuGpu = computeBuf;@@ -243,16 +186,12 @@ bool HwDecoder::init(int maxWidth, int maxHeight) {compCfg.queueId = 0;compCfg.checkMemoryType = 1;- writeProbe("compute queue");- rc = g_vdec.allocComputeQueue(&compCfg, &computeMem, &queue);- removeProbe();-+ rc = sceVideodec2AllocateComputeQueue(&compCfg, &computeMem, &queue);XC_LOGI("hwdec: compute queue pipe %u queue 0: rc=0x%08x, queue=%p", pipe, static_cast<unsigned>(rc), queue);if (rc == 0 && queue) break;}if (!queue) {XC_LOGW("hwdec: failed to allocate compute queue on all pipes");- removeProbe();return false;}computeQueue_ = queue;@@ -272,10 +211,9 @@ bool HwDecoder::init(int maxWidth, int maxHeight) {Vdec2DecoderMemory decMem{};decMem.size = sizeof(decMem);- rc = g_vdec.queryDecoderMem(&decCfg, &decMem);+ rc = sceVideodec2QueryDecoderMemoryInfo(&decCfg, &decMem);if (rc != 0) {XC_LOGW("hwdec: sceVideodec2QueryDecoderMemoryInfo failed: 0x%08x", static_cast<unsigned>(rc));- removeProbe();return false;}@@ -285,7 +223,6 @@ bool HwDecoder::init(int maxWidth, int maxHeight) {rc = sceKernelMapNamedFlexibleMemory(&cpuMem, cpuBytes, kProtectionRw, 0, "PSBoxVdecCpu");if (rc != 0 || !cpuMem) {XC_LOGW("hwdec: allocate flexible memory %zu failed: 0x%08x", cpuBytes, static_cast<unsigned>(rc));- removeProbe();return false;}decMem.cpu = cpuMem;@@ -294,25 +231,21 @@ bool HwDecoder::init(int maxWidth, int maxHeight) {if (decMem.gpuSize > 0) {decMem.gpu = allocDirect(decMem.gpuSize, kMemoryTypeGarlic, "decoder gpu");- if (!decMem.gpu) { removeProbe(); return false; }+ if (!decMem.gpu) return false;}if (decMem.cpuGpuSize > 0) {decMem.cpuGpu = allocDirect(decMem.cpuGpuSize, kMemoryTypeOnion, "decoder cpu+gpu");- if (!decMem.cpuGpu) { removeProbe(); return false; }+ if (!decMem.cpuGpu) return false;}frameBufferSize_ = align16k(decMem.maxFrameSize > 0 ? decMem.maxFrameSize : (static_cast<size_t>(maxWidth) * maxHeight * 3 / 2 + 64));frameBuffer_ = static_cast<uint8_t*>(allocDirect(frameBufferSize_, kMemoryTypeGarlic, "frame buffer"));if (!frameBuffer_) {- removeProbe();return false;}- writeProbe("create decoder");- rc = g_vdec.createDecoder(&decCfg, &decMem, &decoder_);- removeProbe();-+ rc = sceVideodec2CreateDecoder(&decCfg, &decMem, &decoder_);XC_LOGI("hwdec: sceVideodec2CreateDecoder -> 0x%08x", static_cast<unsigned>(rc));if (rc != 0 || !decoder_) {decoder_ = nullptr;@@ -340,7 +273,7 @@ bool HwDecoder::decode(const uint8_t* data, size_t len, uint64_t pts, Picture& oVdec2Output output{};output.size = sizeof(output);- int rc = g_vdec.decode ? g_vdec.decode(decoder_, &input, &frame, &output) : -1;+ int rc = sceVideodec2Decode(decoder_, &input, &frame, &output);if (rc != 0) {return false;}diff --git a/src/platform/ps4/keyboard_ps4.cpp b/src/platform/ps4/keyboard_ps4.cppindex a12d200..2c085d3 100644--- a/src/platform/ps4/keyboard_ps4.cpp+++ b/src/platform/ps4/keyboard_ps4.cpp@@ -9,6 +9,8 @@#include <vector>extern "C" {+int sceSysmoduleLoadModule(uint32_t id);+int sceSysmoduleLoadModuleInternal(uint32_t id);int sceCommonDialogInitialize(void);int sceImeDialogInit(const void* param, void* extended);int sceImeDialogGetStatus(void);@@ -109,6 +111,8 @@ void utf16ToUtf8(const char16_t* in, std::string& out) {bool systemKeyboardAvailable() {if (g_available >= 0) return g_available == 1;+ sceSysmoduleLoadModuleInternal(0x80000018); // COMMON_DIALOG+ sceSysmoduleLoadModule(0x0096); // IME_DIALOGint rc = sceCommonDialogInitialize();g_available = (rc >= 0 || rc == static_cast<int>(0x80610005)) ? 1 : 0;return g_available == 1;diff --git a/src/platform/ps4/platform_ps4.cpp b/src/platform/ps4/platform_ps4.cppindex b9883c2..f3ebabe 100644--- a/src/platform/ps4/platform_ps4.cpp+++ b/src/platform/ps4/platform_ps4.cpp@@ -47,6 +47,9 @@ int sceNetSocketClose(int s);int* sceNetErrnoLoc(void);int _ioctl(int fd, unsigned long request, void* arg);int _close(int fd);+int sceSysmoduleLoadModule(uint32_t id);+int sceSysmoduleLoadModuleInternal(uint32_t id);+int sceRandomGetRandomNumber(void* buf, size_t size);int ioctl(int fd, unsigned long request, ...) {void* arg = nullptr;@@ -289,23 +292,8 @@ int close(int fd) {}extern "C" void arc4random_buf(void* buf, size_t n) {- static int s_urandomFd = -1;- if (s_urandomFd < 0) {- s_urandomFd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);- }- size_t done = 0;- if (s_urandomFd >= 0) {- while (done < n) {- ssize_t r = read(s_urandomFd, static_cast<char*>(buf) + done, n - done);- if (r <= 0) break;- done += static_cast<size_t>(r);- }- }- if (done < n) {- uint8_t* p = static_cast<uint8_t*>(buf) + done;- for (size_t i = done; i < n; ++i) {- *p++ = static_cast<uint8_t>(std::rand() ^ (std::rand() >> 8));- }+ if (buf && n > 0) {+ sceRandomGetRandomNumber(buf, n);}}@@ -609,6 +597,10 @@ void migrateLegacy() {} // namespacebool init() {+ sceSysmoduleLoadModuleInternal(0x8000001C); // NET+ sceSysmoduleLoadModuleInternal(0x80000009); // NETCTL+ sceSysmoduleLoadModule(0x00BA); // RANDOM+int errPsbox = 0;int errLegacy = 0;if (writable(kDataDir, errPsbox)) {