2018-05-09 02:34:44 +02:00
|
|
|
/* Patch for MCP_LoadFile (ioctl 0x53).
|
2018-06-01 13:09:51 +02:00
|
|
|
* Also adds a sibling ioctl, 0x64, that allows setting a custom path to a main RPX
|
2018-05-09 02:34:44 +02:00
|
|
|
*
|
|
|
|
* Reference for most of the types and whatever:
|
|
|
|
* https://github.com/exjam/decaf-emu/tree/ios/src/libdecaf/src/ios/mcp
|
|
|
|
*
|
|
|
|
* This is a proof of concept, and shouldn't be used until it's cleaned up a bit.
|
2018-05-10 13:20:53 +02:00
|
|
|
* Co-authored by exjam, Maschell and QuarkTheAwesome
|
2018-05-09 02:34:44 +02:00
|
|
|
*
|
|
|
|
* Flow of calls:
|
|
|
|
* - kernel loads system libraries, app rpx or user calls OSDynLoad
|
|
|
|
* - goes to loader.elf, which will call ioctl 0x53
|
|
|
|
* - with fileType = LOAD_FILE_CAFE_OS
|
|
|
|
* - on failure, again with LOAD_FILE_PROCESS_CODE
|
|
|
|
* - on failure, again with LOAD_FILE_SYS_DATA_CODE
|
|
|
|
* - each request routes here where we can do whatever
|
|
|
|
*/
|
|
|
|
|
2018-05-10 13:20:53 +02:00
|
|
|
#include "logger.h"
|
2018-05-09 02:34:44 +02:00
|
|
|
#include "ipc_types.h"
|
2018-06-01 13:09:51 +02:00
|
|
|
#include <string.h>
|
2018-05-09 02:34:44 +02:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
//Load from the process's code directory (process title id)/code/%s
|
|
|
|
LOAD_FILE_PROCESS_CODE = 0,
|
|
|
|
//Load from the CafeOS directory (00050010-1000400A)/code/%s
|
|
|
|
LOAD_FILE_CAFE_OS = 1,
|
|
|
|
//Load from a system data title's content directory (0005001B-x)/content/%s
|
|
|
|
LOAD_FILE_SYS_DATA_CONTENT = 2,
|
|
|
|
//Load from a system data title's code directory (0005001B-x)/content/%s
|
|
|
|
LOAD_FILE_SYS_DATA_CODE = 3,
|
2018-05-11 08:01:06 +02:00
|
|
|
|
|
|
|
LOAD_FILE_FORCE_SIZE = 0xFFFFFFFF,
|
2018-05-09 02:34:44 +02:00
|
|
|
} MCPFileType;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned char unk[0x10];
|
|
|
|
|
|
|
|
unsigned int pos;
|
|
|
|
MCPFileType type;
|
|
|
|
unsigned int cafe_pid;
|
|
|
|
|
|
|
|
unsigned char unk2[0xC];
|
|
|
|
|
|
|
|
char name[0x40];
|
|
|
|
|
|
|
|
unsigned char unk3[0x12D8 - 0x68];
|
2018-05-10 13:20:53 +02:00
|
|
|
} MCPLoadFileRequest;
|
2018-05-09 02:34:44 +02:00
|
|
|
//sizeof(MCPLoadFileRequest) = 0x12D8
|
|
|
|
|
|
|
|
int (*const real_MCP_LoadFile)(ipcmessage* msg) = (void*)0x0501CAA8 + 1; //+1 for thumb
|
2018-05-10 13:20:53 +02:00
|
|
|
int (*const MCP_DoLoadFile)(const char* path, const char* path2, void* outputBuffer, u32 outLength, u32 pos, int* bytesRead, u32 unk) = (void*)0x05017248 + 1;
|
|
|
|
int (*const MCP_UnknownStuff)(const char* path, u32 pos, void* outputBuffer, u32 outLength, u32 outLength2, u32 unk) = (void*)0x05014CAC + 1;
|
2018-05-09 02:34:44 +02:00
|
|
|
|
2018-06-01 13:09:51 +02:00
|
|
|
static bool replacerpx = false;
|
|
|
|
static bool didrpxfirstchunk = false;
|
|
|
|
static char rpxpath[0x280];
|
|
|
|
|
2018-05-09 02:34:44 +02:00
|
|
|
int _MCP_LoadFile_patch(ipcmessage* msg) {
|
2018-05-10 13:20:53 +02:00
|
|
|
if (!msg->ioctl.buffer_in) {
|
|
|
|
log_printf("MCP_LoadFile: !msg->ioctl.buffer_in\n");
|
|
|
|
return -29;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->ioctl.length_in != 0x12D8) {
|
|
|
|
log_printf("MCP_LoadFile: Unexpected msg->ioctl.length_in = %u\n", msg->ioctl.length_in);
|
|
|
|
return -29;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!msg->ioctl.buffer_io) {
|
|
|
|
log_printf("MCP_LoadFile: !msg->ioctl.buffer_io\n");
|
|
|
|
return -29;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!msg->ioctl.length_io) {
|
|
|
|
log_printf("MCP_LoadFile: !msg->ioctl.length_io\n");
|
|
|
|
return -29;
|
2018-05-09 02:34:44 +02:00
|
|
|
}
|
2018-06-01 13:09:51 +02:00
|
|
|
|
2018-05-10 13:20:53 +02:00
|
|
|
MCPLoadFileRequest* request = (MCPLoadFileRequest*)msg->ioctl.buffer_in;
|
|
|
|
log_printf("MCP_LoadFile: msg->ioctl.buffer_io = %p, msg->ioctl.length_io = 0x%X\n", msg->ioctl.buffer_io, msg->ioctl.length_io);
|
|
|
|
log_printf("MCP_LoadFile: request->type = %d, request->pos = %d, request->name = \"%s\"\n", request->type, request->pos, request->name);
|
|
|
|
|
2018-05-11 08:01:06 +02:00
|
|
|
if (request->type == LOAD_FILE_CAFE_OS &&
|
|
|
|
request->name[0] == '*') {
|
2018-05-10 13:20:53 +02:00
|
|
|
char path[0x40];
|
|
|
|
|
2018-05-11 08:01:06 +02:00
|
|
|
/* Translate request->name to a path by replacing * with / */
|
2018-05-10 13:20:53 +02:00
|
|
|
for (int i = 0; i < 0x40; ++i) {
|
|
|
|
if (request->name[i] == '*') {
|
|
|
|
path[i] = '/';
|
|
|
|
} else {
|
|
|
|
path[i] = request->name[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log_printf("MCP_LoadFile: Load custom path \"%s\"\n", path);
|
|
|
|
|
|
|
|
/* TODO: If this fails, try last argument as 1 */
|
|
|
|
int bytesRead = 0;
|
|
|
|
int result = MCP_DoLoadFile(path, NULL, msg->ioctl.buffer_io, msg->ioctl.length_io, request->pos, &bytesRead, 0);
|
|
|
|
log_printf("MCP_LoadFile: MCP_DoLoadFile returned %d, bytesRead = %d\n", result, bytesRead);
|
|
|
|
|
|
|
|
if (result >= 0) {
|
|
|
|
if (!bytesRead) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: If this fails, try last argument as 1 */
|
|
|
|
result = MCP_UnknownStuff(path, request->pos, msg->ioctl.buffer_io, msg->ioctl.length_io, msg->ioctl.length_io, 0);
|
|
|
|
log_printf("MCP_LoadFile: MCP_UnknownStuff returned %d\n", result);
|
|
|
|
|
|
|
|
if (result < 0) {
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return bytesRead;
|
|
|
|
}
|
|
|
|
}
|
2018-06-01 13:09:51 +02:00
|
|
|
/* RPX replacement!
|
|
|
|
Only replace this chunk if:
|
|
|
|
- replacerpx is true (replace the next rpx to be loaded)
|
|
|
|
- this file is an rpx
|
|
|
|
and either of the following:
|
|
|
|
- we haven't read the first chunk yet
|
|
|
|
- this is not the first chunk
|
|
|
|
|
|
|
|
This set of conditions means that replacement will only occur the first time an RPX is read in.
|
|
|
|
If the first chunk is read a second time, this means that the first read has already finished.
|
|
|
|
We only want to replace the first read. */
|
|
|
|
} else if (replacerpx) {
|
|
|
|
char* extension = request->name + strlen(request->name) - 3;
|
|
|
|
if (extension[0] == 'r' &&
|
|
|
|
extension[1] == 'p' &&
|
|
|
|
extension[2] == 'x') {
|
|
|
|
if (!didrpxfirstchunk || request->pos > 0) {
|
|
|
|
log_printf("MCP_LoadFile: Custom RPX path \"%s\"\n", rpxpath);
|
|
|
|
|
|
|
|
int bytesRead = 0;
|
|
|
|
int result = MCP_DoLoadFile(rpxpath, NULL, msg->ioctl.buffer_io, msg->ioctl.length_io, request->pos, &bytesRead, 0);
|
|
|
|
log_printf("MCP_LoadFile: MCP_DoLoadFile returned %d, bytesRead = %d\n", result, bytesRead);
|
|
|
|
|
|
|
|
if (result >= 0) {
|
|
|
|
if (!bytesRead) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
result = MCP_UnknownStuff(rpxpath, request->pos, msg->ioctl.buffer_io, msg->ioctl.length_io, msg->ioctl.length_io, 0);
|
|
|
|
log_printf("MCP_LoadFile: MCP_UnknownStuff returned %d\n", result);
|
|
|
|
|
|
|
|
if (result < 0) {
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
if (request->pos == 0) {
|
|
|
|
/* Successfully read in first RPX chunk, set flag */
|
|
|
|
didrpxfirstchunk = true;
|
|
|
|
}
|
|
|
|
return bytesRead;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* This is the second time reading the first chunk of an rpx.
|
|
|
|
Therefore we have already replaced the rpx we were asked to. */
|
|
|
|
replacerpx = false;
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 13:20:53 +02:00
|
|
|
}
|
|
|
|
|
2018-05-09 02:34:44 +02:00
|
|
|
return real_MCP_LoadFile(msg);
|
2018-05-08 12:17:40 +02:00
|
|
|
}
|
2018-05-31 13:11:20 +02:00
|
|
|
|
2018-06-01 13:09:51 +02:00
|
|
|
/* RPX replacement! Call this ioctl to replace the next loaded RPX with an arbitrary path.
|
|
|
|
DO NOT RETURN 0, this affects the codepaths back in the IOSU code */
|
2018-05-31 13:11:20 +02:00
|
|
|
int _MCP_ioctl64_patch(ipcmessage* msg) {
|
2018-06-01 13:09:51 +02:00
|
|
|
if (!msg->ioctl.buffer_in) {
|
|
|
|
log_printf("MCP_ioctl64: !msg->ioctl.buffer_in\n");
|
|
|
|
return -29;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!msg->ioctl.length_in) {
|
|
|
|
log_printf("MCP_ioctl64: !msg->ioctl.length_in");
|
|
|
|
return -29;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->ioctl.length_in > sizeof(rpxpath) - 1) {
|
|
|
|
log_printf("MCP_ioctl64: ioctl.length_in: %X > %X!", msg->ioctl.length_in, sizeof(rpxpath) - 1);
|
|
|
|
return -29;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(rpxpath, (const char*)msg->ioctl.buffer_in, sizeof(rpxpath) - 1);
|
|
|
|
rpxpath[sizeof(rpxpath) - 1] = '\0';
|
|
|
|
|
|
|
|
replacerpx = true;
|
|
|
|
didrpxfirstchunk = false;
|
2018-05-31 13:11:20 +02:00
|
|
|
|
2018-06-01 13:09:51 +02:00
|
|
|
log_printf("MCP_ioctl64: Will load %s for next title\n", rpxpath);
|
2018-05-31 13:11:20 +02:00
|
|
|
return 1;
|
|
|
|
}
|