Rename Mocha_LoadRPXOnNextLaunch and add Mocha_LaunchRPX / Mocha_LaunchHomebrewWrapper

This commit is contained in:
Maschell 2022-07-22 14:13:11 +02:00
parent 41fba4fc64
commit e16878d7d7
3 changed files with 89 additions and 4 deletions

View File

@ -14,7 +14,8 @@ extern "C" {
#define IPC_CUSTOM_GET_MOCHA_API_VERSION 0xF8
typedef enum LoadRPXTargetEnum {
LOAD_RPX_TARGET_SD_CARD = 0,
LOAD_RPX_TARGET_SD_CARD = 0,
LOAD_RPX_TARGET_EXTRA_REVERT_PREPARE = 0x42424242,
} LoadRPXTargetEnum;
typedef struct __attribute((packed)) {

View File

@ -86,7 +86,54 @@ MochaUtilsStatus Mocha_UnlockFSClient(FSClient *client);
*/
MochaUtilsStatus Mocha_UnlockFSClientEx(int clientHandle);
MochaUtilsStatus Mocha_LoadRPXOnNextLaunch(MochaRPXLoadInfo *loadInfo);
/**
* Set the .rpx that will be loaded the next time the homebrew wrapper application is launched (e.g. Health & Safety or Daily Log).
* <br>
* Loading a .rpx from within a file (archive e.g. a WUHB) is supported. <br>
* To achieve this, the fileoffset (offset inside file specified via path) and filesize (size of the .rpx) need to be set. <br>
* If filesize is set to 0, the whole file (starting at fileoffset) will be loaded as .rpx <br>
* <br>
* The path is **relative** to the root of the given target device.<br>
* The target LOAD_RPX_TARGET_EXTRA_REVERT_PREPARE will revert a prepare call. <br>
* <br>
* To launch the prepared RPX call Mocha_LaunchHomebrewWrapper if this call was successful.
*
* @param loadInfo Information about the .rpx replacement.
* @return MOCHA_RESULT_SUCCESS: Loading the next RPX will be redirected. <br>
* MOCHA_RESULT_INVALID_ARGUMENT: The given loadInfo was NULL <br>
* MOCHA_RESULT_LIB_UNINITIALIZED: Library was not initialized. Call Mocha_InitLibrary() before using this function.<br>
* MOCHA_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded mocha version.<br>
* MOCHA_RESULT_UNKNOWN_ERROR: Failed to setup a redirect of RPX.
*/
MochaUtilsStatus Mocha_PrepareRPXLaunch(MochaRPXLoadInfo *loadInfo);
/**
* Launches the wrapper app for launching .rpx <br>
* To launch a RPX call `Mocha_PrepareRPXLaunch` before this function. <br>
* <br>
* see: `Mocha_LaunchRPX` to prepare and launch a RPX in one command.
*
* @return MOCHA_RESULT_SUCCESS: App is launching<br>
* MOCHA_RESULT_LIB_UNINITIALIZED: Library was not initialized. Call Mocha_InitLibrary() before using this function.<br>
* MOCHA_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded mocha version.<br>
* MOCHA_RESULT_NOT_FOUND: Not application that can be used as homebrew wrapper found.
*/
MochaUtilsStatus Mocha_LaunchHomebrewWrapper();
/**
* Launches a given RPX by launching a wrapper application and replacing the RPX on the fly. <br>
* See Mocha_PrepareRPXLaunch for more information. <br>
*
* Note: Combines Mocha_PrepareRPXLaunch and Mocha_LaunchHomebrewWrapper.
* @param loadInfo
* @return MOCHA_RESULT_SUCCESS: Requested RPX will be launched<br>
* MOCHA_RESULT_LIB_UNINITIALIZED: Library was not initialized. Call Mocha_InitLibrary() before using this function.<br>
* MOCHA_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded mocha version.<br>
* MOCHA_RESULT_INVALID_ARGUMENT: The given loadInfo was NULL <br>
* MOCHA_RESULT_NOT_FOUND: Not application that can be used as homebrew wrapper found.
* MOCHA_RESULT_UNKNOWN_ERROR: Failed to setup a redirect of RPX.
*/
MochaUtilsStatus Mocha_LaunchRPX(MochaRPXLoadInfo *loadInfo);
typedef struct WUDDiscKey {
uint8_t key[0x10];

View File

@ -4,6 +4,8 @@
#include <coreinit/ios.h>
#include <cstring>
#include <stdint.h>
#include <sysapp/launch.h>
#include <sysapp/title.h>
int mochaInitDone = 0;
uint32_t mochaApiVersion = 0;
@ -131,13 +133,16 @@ MochaUtilsStatus Mocha_UnlockFSClientEx(int clientHandle) {
return MOCHA_RESULT_UNKNOWN_ERROR;
}
MochaUtilsStatus Mocha_LoadRPXOnNextLaunch(MochaRPXLoadInfo *loadInfo) {
MochaUtilsStatus Mocha_PrepareRPXLaunch(MochaRPXLoadInfo *loadInfo) {
if (!mochaInitDone) {
return MOCHA_RESULT_LIB_UNINITIALIZED;
}
if (mochaApiVersion < 1) {
return MOCHA_RESULT_UNSUPPORTED_COMMAND;
}
if (!loadInfo) {
return MOCHA_RESULT_INVALID_ARGUMENT;
}
MochaUtilsStatus res = MOCHA_RESULT_UNKNOWN_ERROR;
int mcpFd = IOS_Open("/dev/mcp", (IOSOpenMode) 0);
if (mcpFd >= 0) {
@ -148,13 +153,45 @@ MochaUtilsStatus Mocha_LoadRPXOnNextLaunch(MochaRPXLoadInfo *loadInfo) {
if (IOS_Ioctl(mcpFd, 100, io_buffer, sizeof(MochaRPXLoadInfo) + 4, io_buffer, 0x4) == IOS_ERROR_OK) {
res = MOCHA_RESULT_SUCCESS;
}
IOS_Close(mcpFd);
}
return res;
}
MochaUtilsStatus Mocha_LaunchRPX(MochaRPXLoadInfo *loadInfo) {
auto res = Mocha_PrepareRPXLaunch(loadInfo);
if (res == MOCHA_RESULT_SUCCESS) {
res = Mocha_LaunchHomebrewWrapper();
if (res != MOCHA_RESULT_SUCCESS) {
MochaRPXLoadInfo loadInfoRevert;
loadInfoRevert.target = LOAD_RPX_TARGET_EXTRA_REVERT_PREPARE;
Mocha_PrepareRPXLaunch(&loadInfoRevert);
}
}
return res;
}
MochaUtilsStatus Mocha_LaunchHomebrewWrapper() {
if (!mochaInitDone) {
return MOCHA_RESULT_LIB_UNINITIALIZED;
}
if (mochaApiVersion < 1) {
return MOCHA_RESULT_UNSUPPORTED_COMMAND;
}
uint64_t titleID = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_HEALTH_AND_SAFETY);
if (!SYSCheckTitleExists(titleID)) { // Fallback to daily log app if H&S doesn't exist.
titleID = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_DAILY_LOG);
}
if (!SYSCheckTitleExists(titleID)) {
return MOCHA_RESULT_NOT_FOUND;
}
_SYSLaunchTitleWithStdArgsInNoSplash(titleID, nullptr);
return MOCHA_RESULT_SUCCESS;
}
MochaUtilsStatus Mocha_ODMGetDiscKey(WUDDiscKey *discKey) {
if (!mochaInitDone) {
return MOCHA_RESULT_LIB_UNINITIALIZED;