Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions sysmodules/loader/source/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@
#include "ifile.h"
#include "util.h"
#include "hbldr.h"
#include "loader.h"

#define SYSMODULE_CXI_COOKIE_MASK 0xEEEE000000000000ull

// Used by the custom loader command 0x101 (ControlApplicationMemoryModeOverride)
typedef struct ControlApplicationMemoryModeOverrideConfig {
u32 query : 1; //< Only query the current configuration, do not update it.
u32 enable_o3ds : 1; //< Enable o3ds memory mode override
u32 enable_n3ds : 1; //< Enable n3ds memory mode override
u32 o3ds_mode : 3; //< O3ds memory mode
u32 n3ds_mode : 3; //< N3ds memory mode
} ControlApplicationMemoryModeOverrideConfig;

static ControlApplicationMemoryModeOverrideConfig g_memoryOverrideConfig = { 0 };
extern ControlApplicationMemoryModeOverrideConfig g_memoryOverrideConfig;

extern u32 config, multiConfig, bootConfig;
extern bool isN3DS, isSdMode, nextGamePatchDisabled;
Expand Down Expand Up @@ -597,7 +589,20 @@ void loaderHandleCommands(void *ctx)
case 0x101: // ControlApplicationMemoryModeOverride
memcpy(&memModeOverride, &cmdbuf[1], sizeof(ControlApplicationMemoryModeOverrideConfig));
if (!memModeOverride.query)
{
g_memoryOverrideConfig = memModeOverride;
if (g_memoryOverrideConfig.enable_o3ds || g_memoryOverrideConfig.enable_n3ds)
{
IFile file;
FS_ArchiveID archiveId = isSdMode ? ARCHIVE_SDMC : ARCHIVE_NAND_RW;
if (R_SUCCEEDED(IFile_Open(&file, archiveId, fsMakePath(PATH_EMPTY, ""), fsMakePath(PATH_ASCII, "/luma/memtype_override.bin"),
FS_OPEN_CREATE | FS_OPEN_READ | FS_OPEN_WRITE))) {
u64 written;
IFile_Write(&file, &written, &g_memoryOverrideConfig, sizeof(ControlApplicationMemoryModeOverrideConfig), 0);
IFile_Close(&file);
}
}
}
cmdbuf[0] = IPC_MakeHeader(0x101, 2, 0);
cmdbuf[1] = (Result)0;
memcpy(&cmdbuf[2], &g_memoryOverrideConfig, sizeof(ControlApplicationMemoryModeOverrideConfig));
Expand Down
9 changes: 9 additions & 0 deletions sysmodules/loader/source/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@

#include <3ds/types.h>

// Used by the custom loader command 0x101 (ControlApplicationMemoryModeOverride)
typedef struct ControlApplicationMemoryModeOverrideConfig {
u32 query : 1; //< Only query the current configuration, do not update it.
u32 enable_o3ds : 1; //< Enable o3ds memory mode override
u32 enable_n3ds : 1; //< Enable n3ds memory mode override
u32 o3ds_mode : 3; //< O3ds memory mode
u32 n3ds_mode : 3; //< N3ds memory mode
} ControlApplicationMemoryModeOverrideConfig;

void loaderHandleCommands(void *ctx);
24 changes: 24 additions & 0 deletions sysmodules/loader/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "3dsx.h"
#include "hbldr.h"

ControlApplicationMemoryModeOverrideConfig g_memoryOverrideConfig = { 0 };

u32 config, multiConfig, bootConfig;
bool isN3DS, isSdMode, nextGamePatchDisabled, isLumaWithKext;

Expand Down Expand Up @@ -77,6 +79,27 @@ static inline void loadCFWInfo(void)
Luma_SharedConfig->use_hbldr = true;
}

static inline void loadMemOverrideCfg(void)
{
IFile file;
FS_ArchiveID archiveId = isSdMode ? ARCHIVE_SDMC : ARCHIVE_NAND_RW;
if (R_SUCCEEDED(IFile_Open(&file, archiveId, fsMakePath(PATH_EMPTY, ""), fsMakePath(PATH_ASCII, "/luma/memtype_override.bin"), FS_OPEN_READ)))
{
u64 read;
Result res = IFile_Read(&file, &read, &g_memoryOverrideConfig, sizeof(ControlApplicationMemoryModeOverrideConfig));
if (R_FAILED(res) || read != sizeof(ControlApplicationMemoryModeOverrideConfig))
memset(&g_memoryOverrideConfig, 0, sizeof(ControlApplicationMemoryModeOverrideConfig));
IFile_Close(&file);

FS_Archive ar;
if(R_SUCCEEDED(FSUSER_OpenArchive(&ar, archiveId, fsMakePath(PATH_EMPTY, ""))))
{
FSUSER_DeleteFile(ar, fsMakePath(PATH_ASCII, "/luma/memtype_override.bin"));
FSUSER_CloseArchive(ar);
}
}
}

void __ctru_exit(int rc) { (void)rc; } // needed to avoid linking error

// this is called after main exits
Expand Down Expand Up @@ -134,6 +157,7 @@ static_assert(ARGVBUF_SIZE > 2 * PATH_MAX, "Wrong 3DSX argv buffer size");

int main(void)
{
loadMemOverrideCfg();
nextGamePatchDisabled = false;

// Loader doesn't use any input static buffer, so we should be fine
Expand Down