Skip to content

Commit

Permalink
Allow early init of the Module System, check EnvrionmentLoader version
Browse files Browse the repository at this point in the history
  • Loading branch information
Maschell committed Apr 20, 2024
1 parent 40bec54 commit eed20e7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
4 changes: 4 additions & 0 deletions source/kernel.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "kernel.h"
#include "../wumsloader/src/globals.h"
#include "ElfUtils.h"
#include "wumsloader_elf.h"
#include <coreinit/cache.h>
Expand All @@ -20,6 +21,9 @@ void SetupWUMSLoader() {
KernelWriteU32(repl_addr, 0x48000003 | entryPoint);
DCFlushRange((void *) repl_addr, 4);
ICInvalidateRange((void *) (repl_addr), 4);

// We call the mainhook (wumloader) once with a MAGIC number to set up the WUMS Modules but not actually call the original main function
((int (*)(int, char **))(entryPoint))(WUMS_LOADER_SETUP_MAGIC_WORD, nullptr);
}

void KernelWriteU32(uint32_t addr, uint32_t value) {
Expand Down
5 changes: 5 additions & 0 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ int main(int argc, char **argv) {
if (argc >= 1) {
basePath = argv[0];
}
if (argc < 4 || std::string_view("EnvironmentLoader") != argv[1] || (uint32_t) argv[2] < 2 || (uint32_t) argv[3] == 0) {
OSFatal("WUMSLoader: Failed to parse arguments, make sure to use the latest environment loader.\n See https://wiiu.hacks.guide/ for more information.");
}

memcpy(MEMORY_REGION_USABLE_MEM_REGION_END_VALUE_PTR, &argv[3], sizeof(uint32_t));

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
Expand Down
2 changes: 1 addition & 1 deletion wumsloader/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ MAKEFLAGS += --no-print-directory
#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS := -lwums -lwut -lz
LIBS := -lwut -lz

#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
Expand Down
4 changes: 4 additions & 0 deletions wumsloader/src/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ extern "C" int _start(int argc, char **argv) {
:);

OSCheckActiveThreads();
if (argc == WUMS_LOADER_SETUP_MAGIC_WORD) {
DEBUG_FUNCTION_LINE("Skip calling the real main function because we just want to setup WUMS");
return 0;
}
return ((int (*)(int, char **))(*(unsigned int *) 0x1005E040))(argc, argv);
}

Expand Down
24 changes: 15 additions & 9 deletions wumsloader/src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ extern std::unique_ptr<module_information_single_t[]> gModuleDataInfo;
extern std::map<std::string, OSDynLoad_Module> gUsedRPLs;
extern std::vector<void *> gAllocatedAddresses;

#define MEMORY_REGION_START 0x00800000
#define MEMORY_REGION_SIZE 0x00800000
#define MEMORY_REGION_START 0x00800000
#define MEMORY_REGION_SIZE 0x00800000

#define CUSTOM_RPX_LOADER_RETURN_CODE 0x00009000 // We have to skip the first 0x00009000 bytes because it's still used
#define RELOCATOR_SIZE 0x52000 // Maximum size of the wumsloader, needs to match the one defined in link.ld
#define ENVIRONMENT_PATH_LENGTH 0x100 // Length of the EnvironmentPath.
#define CUSTOM_RPX_LOADER_RETURN_CODE 0x00009000 // We have to skip the first 0x00009000 bytes because it's still used
#define RELOCATOR_SIZE 0x52000 // Maximum size of the wumsloader, needs to match the one defined in link.ld
#define ENVIRONMENT_PATH_LENGTH 0x100 // Length of the EnvironmentPath.
#define MEMORY_REGION_USABLE_MEM_REGION_END_LENGTH 0x04 // sizeof(uint32_t)

#define MEMORY_REGION_ENVIRONMENT_STRING_ADRR (MEMORY_REGION_START + CUSTOM_RPX_LOADER_RETURN_CODE + RELOCATOR_SIZE)
#define MEMORY_REGION_USABLE_HEAP_START (MEMORY_REGION_ENVIRONMENT_STRING_ADRR + ENVIRONMENT_PATH_LENGTH)
#define MEMORY_REGION_USABLE_HEAP_END (0x00FFF000) // We need to leave space for the BAT hook
#define MEMORY_REGION_ENVIRONMENT_STRING_ADRR (MEMORY_REGION_START + CUSTOM_RPX_LOADER_RETURN_CODE + RELOCATOR_SIZE)
#define MEMORY_REGION_USABLE_MEM_REGION_END_VALUE_PTR ((uint32_t *) (MEMORY_REGION_ENVIRONMENT_STRING_ADRR + ENVIRONMENT_PATH_LENGTH))
#define MEMORY_REGION_USABLE_MEM_REGION_END_VALUE (*MEMORY_REGION_USABLE_MEM_REGION_END_VALUE_PTR)

#define ENVRIONMENT_STRING ((char *) MEMORY_REGION_ENVIRONMENT_STRING_ADRR)
#define MEMORY_REGION_USABLE_HEAP_START ((uint32_t) MEMORY_REGION_USABLE_MEM_REGION_END_VALUE_PTR + MEMORY_REGION_USABLE_MEM_REGION_END_LENGTH)
#define MEMORY_REGION_USABLE_HEAP_END MEMORY_REGION_USABLE_MEM_REGION_END_VALUE

#define ENVRIONMENT_STRING ((char *) MEMORY_REGION_ENVIRONMENT_STRING_ADRR)

#define WUMS_LOADER_SETUP_MAGIC_WORD 0x13371337
2 changes: 2 additions & 0 deletions wumsloader/src/utils/imports.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ IMPORT(__KernelGetInfo);
IMPORT(OSCheckActiveThreads);
IMPORT(OSGetCurrentThread);
IMPORT(OSSetThreadCleanupCallback);
IMPORT(OSIsDebuggerPresent);
IMPORT(__os_snprintf);

IMPORT(FSTimeToCalendarTime);
IMPORT(FSInit);
Expand Down

0 comments on commit eed20e7

Please sign in to comment.