From 5ebee36b7f6df03b8d8977372c40b4352b8dc6cf Mon Sep 17 00:00:00 2001 From: Maschell Date: Wed, 24 Aug 2022 14:36:20 +0200 Subject: [PATCH] Use libmocha to load .rpx --- Makefile | 2 +- src/RPXLoading.cpp | 30 ++++++++++++++---------------- src/main.cpp | 9 +++++++++ src/utils/logger.h | 5 ++++- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 17fcbb3..59dcf59 100644 --- a/Makefile +++ b/Makefile @@ -49,7 +49,7 @@ CXXFLAGS += -DDEBUG -DVERBOSE_DEBUG -g CFLAGS += -DDEBUG -DVERBOSE_DEBUG -g endif -LIBS := -lwums -lwut -lfunctionpatcher -lcontentredirection -lwuhbutils -lromfs +LIBS := -lwums -lwut -lfunctionpatcher -lcontentredirection -lwuhbutils -lmocha -lromfs #------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level diff --git a/src/RPXLoading.cpp b/src/RPXLoading.cpp index aba625f..4ef2811 100644 --- a/src/RPXLoading.cpp +++ b/src/RPXLoading.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -117,13 +118,12 @@ static int parseINIhandler(void *user, const char *section, const char *name, } bool RL_LoadFromSDOnNextLaunch(const char *bundle_path) { - LOAD_REQUEST request; + MochaRPXLoadInfo request; memset(&request, 0, sizeof(request)); - request.command = 0xFC; // IPC_CUSTOM_LOAD_CUSTOM_RPX; - request.target = 0; // LOAD_FILE_TARGET_SD_CARD - request.filesize = 0; // unknown filesize - request.fileoffset = 0; // + request.target = LOAD_RPX_TARGET_SD_CARD; // LOAD_FILE_TARGET_SD_CARD + request.filesize = 0; // unknown filesize + request.fileoffset = 0; // WUHBRPXInfo fileInfo; @@ -193,20 +193,18 @@ bool RL_LoadFromSDOnNextLaunch(const char *bundle_path) { strncat(request.path, bundle_path, sizeof(request.path) - 1); OSMemoryBarrier(); - - int success = false; - int mcpFd = IOS_Open("/dev/mcp", (IOSOpenMode) 0); - if (mcpFd >= 0) { - if (IOS_Ioctl(mcpFd, 100, &request, sizeof(request), nullptr, 0) == IOS_ERROR_OK) { - success = true; - } - - IOS_Close(mcpFd); + bool success = false; + MochaUtilsStatus res; + if ((res = Mocha_PrepareRPXLaunch(&request)) == MOCHA_RESULT_SUCCESS) { + success = true; + } else { + DEBUG_FUNCTION_LINE_ERR("Failed to prepare rpx launch: %s", Mocha_GetStatusStr(res)); } - OSMemoryBarrier(); - if (!success) { + request.target = LOAD_RPX_TARGET_EXTRA_REVERT_PREPARE; + Mocha_PrepareRPXLaunch(&request); + gReplacementInfo.rpxReplacementInfo.willRPXBeReplaced = false; DEBUG_FUNCTION_LINE_ERR("Failed to load %s on next restart", request.path); return false; diff --git a/src/main.cpp b/src/main.cpp index 8c4dce6..5d0dde4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -51,6 +52,14 @@ WUMS_INITIALIZE() { OSFatal("Failed to init WUHBUtils."); } + + // Init libmocha + MochaUtilsStatus error3; + if ((error3 = Mocha_InitLibrary()) != MOCHA_RESULT_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to init libmocha. Error %s", Mocha_GetStatusStr(error3)); + OSFatal("Failed to init libmocha. Make sure to use the latest version of MochaPayload"); + } + deinitLogging(); } diff --git a/src/utils/logger.h b/src/utils/logger.h index efda62b..e1dff38 100644 --- a/src/utils/logger.h +++ b/src/utils/logger.h @@ -21,7 +21,6 @@ extern "C" { LOG_FUNC("[(%s)%18s][%23s]%30s@L%04d: " LOG_LEVEL "" FMT "" LINE_END, LOG_APP_TYPE, LOG_APP_NAME, __FILENAME__, __FUNCTION__, __LINE__, ##ARGS); \ } while (0) - #ifdef DEBUG #ifdef VERBOSE_DEBUG @@ -36,6 +35,8 @@ extern "C" { #define DEBUG_FUNCTION_LINE_ERR(FMT, ARGS...) LOG_EX(WHBLogPrintf, "##ERROR## ", "", FMT, ##ARGS) +#define DEBUG_FUNCTION_LINE_WARN(FMT, ARGS...) LOG_EX(WHBLogPrintf, "##WARN ## ", "", FMT, ##ARGS) + #else #define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while (0) @@ -46,6 +47,8 @@ extern "C" { #define DEBUG_FUNCTION_LINE_ERR(FMT, ARGS...) LOG_EX(OSReport, "##ERROR## ", "\n", FMT, ##ARGS) +#define DEBUG_FUNCTION_LINE_WARN(FMT, ARGS...) LOG_EX(OSReport, "##WARN ## ", "\n", FMT, ##ARGS) + #endif void initLogging();