From eed20e78a5d784b4ececa2e29169f57a2e8dc12f Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 19 Apr 2024 13:11:28 +0200 Subject: [PATCH] Allow early init of the Module System, check EnvrionmentLoader version --- source/kernel.cpp | 4 ++++ source/main.cpp | 5 +++++ wumsloader/Makefile | 2 +- wumsloader/src/entry.cpp | 4 ++++ wumsloader/src/globals.h | 24 +++++++++++++++--------- wumsloader/src/utils/imports.h | 2 ++ 6 files changed, 31 insertions(+), 10 deletions(-) diff --git a/source/kernel.cpp b/source/kernel.cpp index d960542..5e6b44c 100644 --- a/source/kernel.cpp +++ b/source/kernel.cpp @@ -1,4 +1,5 @@ #include "kernel.h" +#include "../wumsloader/src/globals.h" #include "ElfUtils.h" #include "wumsloader_elf.h" #include @@ -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) { diff --git a/source/main.cpp b/source/main.cpp index aaea277..9a96cdf 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -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" diff --git a/wumsloader/Makefile b/wumsloader/Makefile index 4e5025b..3617d8b 100644 --- a/wumsloader/Makefile +++ b/wumsloader/Makefile @@ -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 diff --git a/wumsloader/src/entry.cpp b/wumsloader/src/entry.cpp index 72aa96a..2b53e46 100644 --- a/wumsloader/src/entry.cpp +++ b/wumsloader/src/entry.cpp @@ -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); } diff --git a/wumsloader/src/globals.h b/wumsloader/src/globals.h index f0326be..c973cbe 100644 --- a/wumsloader/src/globals.h +++ b/wumsloader/src/globals.h @@ -14,15 +14,21 @@ extern std::unique_ptr gModuleDataInfo; extern std::map gUsedRPLs; extern std::vector 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 diff --git a/wumsloader/src/utils/imports.h b/wumsloader/src/utils/imports.h index ae4bab3..bed4504 100644 --- a/wumsloader/src/utils/imports.h +++ b/wumsloader/src/utils/imports.h @@ -38,6 +38,8 @@ IMPORT(__KernelGetInfo); IMPORT(OSCheckActiveThreads); IMPORT(OSGetCurrentThread); IMPORT(OSSetThreadCleanupCallback); +IMPORT(OSIsDebuggerPresent); +IMPORT(__os_snprintf); IMPORT(FSTimeToCalendarTime); IMPORT(FSInit);