2020-04-28 14:43:07 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include <elfio/elfio.hpp>
|
|
|
|
#include <proc_ui/procui.h>
|
|
|
|
#include <sysapp/launch.h>
|
|
|
|
#include <coreinit/foreground.h>
|
|
|
|
#include <coreinit/cache.h>
|
|
|
|
#include <coreinit/cache.h>
|
|
|
|
#include <coreinit/memorymap.h>
|
|
|
|
#include <coreinit/dynload.h>
|
|
|
|
#include <whb/log.h>
|
|
|
|
#include <whb/log_udp.h>
|
2020-05-17 13:11:52 +02:00
|
|
|
#include <vector>
|
2020-04-28 14:43:07 +02:00
|
|
|
|
|
|
|
#include "fs/DirList.h"
|
|
|
|
#include "utils/logger.h"
|
|
|
|
#include "utils/utils.h"
|
|
|
|
#include "module/ModuleDataPersistence.h"
|
|
|
|
#include "module/ModuleDataFactory.h"
|
|
|
|
#include "ElfUtils.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
|
|
|
|
#include "common/module_defines.h"
|
|
|
|
|
|
|
|
bool CheckRunning() {
|
|
|
|
|
2020-05-17 19:05:51 +02:00
|
|
|
switch (ProcUIProcessMessages(true)) {
|
|
|
|
case PROCUI_STATUS_EXITING: {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
case PROCUI_STATUS_RELEASE_FOREGROUND: {
|
|
|
|
ProcUIDrawDoneRelease();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PROCUI_STATUS_IN_FOREGROUND: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PROCUI_STATUS_IN_BACKGROUND:
|
|
|
|
default:
|
|
|
|
break;
|
2020-04-28 14:43:07 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-29 11:43:45 +02:00
|
|
|
#define gModuleData ((module_information_t *) (0x00880000))
|
2020-04-28 14:43:07 +02:00
|
|
|
static_assert(sizeof(module_information_t) <= 0x80000);
|
|
|
|
|
|
|
|
extern "C" uint32_t textStart();
|
|
|
|
|
2020-05-17 19:05:51 +02:00
|
|
|
bool doRelocation(std::vector<RelocationData> &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length) {
|
|
|
|
for (auto const &curReloc : relocData) {
|
2020-05-17 13:11:52 +02:00
|
|
|
std::string functionName = curReloc.getName();
|
|
|
|
std::string rplName = curReloc.getImportRPLInformation().getName();
|
|
|
|
int32_t isData = curReloc.getImportRPLInformation().isData();
|
2020-04-28 14:43:07 +02:00
|
|
|
OSDynLoad_Module rplHandle = 0;
|
|
|
|
OSDynLoad_Acquire(rplName.c_str(), &rplHandle);
|
|
|
|
|
|
|
|
uint32_t functionAddress = 0;
|
2020-05-17 19:05:51 +02:00
|
|
|
OSDynLoad_FindExport(rplHandle, isData, functionName.c_str(), (void **) &functionAddress);
|
|
|
|
if (functionAddress == 0) {
|
2020-04-28 14:43:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-17 19:05:51 +02:00
|
|
|
if (!ElfUtils::elfLinkOne(curReloc.getType(), curReloc.getOffset(), curReloc.getAddend(), (uint32_t) curReloc.getDestination(), functionAddress, tramp_data, tramp_length, RELOC_TYPE_IMPORT)) {
|
2020-04-28 14:43:07 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Relocation failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DCFlushRange(tramp_data, tramp_length * sizeof(relocation_trampolin_entry_t));
|
|
|
|
ICInvalidateRange(tramp_data, tramp_length * sizeof(relocation_trampolin_entry_t));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-17 19:05:51 +02:00
|
|
|
int main(int argc, char **argv) {
|
2020-04-28 14:43:07 +02:00
|
|
|
WHBLogUdpInit();
|
|
|
|
|
|
|
|
// 0x100 because before the .text section is a .init section
|
|
|
|
// Currently the size of the .init is ~ 0x24 bytes. We substract 0x100 to be safe.
|
|
|
|
uint32_t textSectionStart = textStart() - 0x1000;
|
|
|
|
|
|
|
|
DirList setupModules("fs:/vol/external01/wiiu/modules/setup", ".rpx", DirList::Files, 1);
|
|
|
|
setupModules.SortList();
|
|
|
|
|
2020-05-17 19:05:51 +02:00
|
|
|
for (int i = 0; i < setupModules.GetFilecount(); i++) {
|
|
|
|
memset((void *) gModuleData, 0, sizeof(module_information_t));
|
|
|
|
DEBUG_FUNCTION_LINE("Trying to run %s", setupModules.GetFilepath(i));
|
2020-05-28 21:45:44 +02:00
|
|
|
uint32_t destination_address = 0x00900000;
|
|
|
|
std::optional<ModuleData> moduleData = ModuleDataFactory::load(setupModules.GetFilepath(i), &destination_address, 0x01000000 - textSectionStart, gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH);
|
2020-05-17 19:05:51 +02:00
|
|
|
if (!moduleData) {
|
2020-04-28 14:43:07 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Failed to load %s", setupModules.GetFilepath(i));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DEBUG_FUNCTION_LINE("Loaded module data");
|
2020-05-17 13:11:52 +02:00
|
|
|
std::vector<RelocationData> relocData = moduleData->getRelocationDataList();
|
2020-05-17 19:05:51 +02:00
|
|
|
if (!doRelocation(relocData, gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH)) {
|
2020-04-28 14:43:07 +02:00
|
|
|
DEBUG_FUNCTION_LINE("relocations failed\n");
|
|
|
|
}
|
2020-05-17 19:05:51 +02:00
|
|
|
if (moduleData->getBSSAddr() != 0) {
|
2020-04-28 14:43:07 +02:00
|
|
|
DEBUG_FUNCTION_LINE("memset .bss %08X (%d)", moduleData->getBSSAddr(), moduleData->getBSSSize());
|
2020-05-17 19:05:51 +02:00
|
|
|
memset((void *) moduleData->getBSSAddr(), 0, moduleData->getBSSSize());
|
2020-04-28 14:43:07 +02:00
|
|
|
}
|
2020-05-17 19:05:51 +02:00
|
|
|
if (moduleData->getSBSSAddr() != 0) {
|
2020-04-28 14:43:07 +02:00
|
|
|
DEBUG_FUNCTION_LINE("memset .sbss %08X (%d)", moduleData->getSBSSAddr(), moduleData->getSBSSSize());
|
2020-05-17 19:05:51 +02:00
|
|
|
memset((void *) moduleData->getSBSSAddr(), 0, moduleData->getSBSSSize());
|
2020-04-28 14:43:07 +02:00
|
|
|
}
|
2020-05-17 19:05:51 +02:00
|
|
|
DCFlushRange((void *) 0x00800000, 0x00800000);
|
|
|
|
ICInvalidateRange((void *) 0x00800000, 0x00800000);
|
2020-04-28 14:43:07 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Calling %08X", moduleData->getEntrypoint());
|
2020-05-17 19:05:51 +02:00
|
|
|
((int (*)(int, char **)) moduleData->getEntrypoint())(argc, argv);
|
2020-04-28 14:43:07 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Back from module");
|
|
|
|
}
|
|
|
|
|
2020-05-17 19:05:51 +02:00
|
|
|
memset((void *) gModuleData, 0, sizeof(module_information_t));
|
2020-04-29 11:43:45 +02:00
|
|
|
|
2020-05-17 21:14:27 +02:00
|
|
|
DirList modules("fs:/vol/external01/wiiu/modules", ".wms", DirList::Files, 1);
|
2020-04-28 14:43:07 +02:00
|
|
|
modules.SortList();
|
2020-05-28 21:45:44 +02:00
|
|
|
uint32_t destination_address = 0x00900000;
|
2020-05-29 17:36:10 +02:00
|
|
|
|
2020-05-17 19:05:51 +02:00
|
|
|
for (int i = 0; i < modules.GetFilecount(); i++) {
|
|
|
|
DEBUG_FUNCTION_LINE("Loading module %s", modules.GetFilepath(i));
|
2020-04-28 14:43:07 +02:00
|
|
|
|
2020-05-28 21:45:44 +02:00
|
|
|
std::optional<ModuleData> moduleData = ModuleDataFactory::load(modules.GetFilepath(i), &destination_address, 0x01000000 - textSectionStart, gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH);
|
2020-04-28 14:43:07 +02:00
|
|
|
|
2020-05-17 19:05:51 +02:00
|
|
|
if (moduleData) {
|
2020-04-29 12:06:47 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Successfully loaded %s", modules.GetFilepath(i));
|
2020-05-17 13:11:52 +02:00
|
|
|
ModuleDataPersistence::saveModuleData(gModuleData, moduleData.value());
|
2020-05-03 00:06:11 +02:00
|
|
|
} else {
|
2020-04-29 12:06:47 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Failed to load %s", modules.GetFilepath(i));
|
2020-04-28 14:43:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetupRelocator();
|
|
|
|
|
|
|
|
WHBLogUdpDeinit();
|
|
|
|
|
|
|
|
ProcUIInit(OSSavesDone_ReadyToRelease);
|
|
|
|
SYSLaunchMenu();
|
2020-05-17 19:05:51 +02:00
|
|
|
while (CheckRunning()) {
|
2020-04-28 14:43:07 +02:00
|
|
|
// wait.
|
|
|
|
OSSleepTicks(OSMillisecondsToTicks(100));
|
|
|
|
}
|
|
|
|
ProcUIShutdown();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|