2020-11-29 12:59:58 +01:00
|
|
|
/****************************************************************************
|
2022-03-04 13:23:36 +01:00
|
|
|
* Copyright (C) 2018-2022 Maschell
|
2020-11-29 12:59:58 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
2022-02-03 17:21:38 +01:00
|
|
|
|
2020-11-29 12:59:58 +01:00
|
|
|
#include "dynamic.h"
|
|
|
|
#include "kernel.h"
|
2022-03-04 13:29:02 +01:00
|
|
|
#include "utils/DirList.h"
|
2022-02-03 17:21:38 +01:00
|
|
|
#include "utils/ElfUtils.h"
|
|
|
|
#include "utils/logger.h"
|
2023-04-17 12:14:55 +02:00
|
|
|
#include <coreinit/debug.h>
|
|
|
|
#include <coreinit/dynload.h>
|
2020-11-29 12:59:58 +01:00
|
|
|
#include <coreinit/filesystem.h>
|
2023-04-17 12:14:55 +02:00
|
|
|
#include <coreinit/memexpheap.h>
|
|
|
|
#include <coreinit/screen.h>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <malloc.h>
|
2020-11-29 12:59:58 +01:00
|
|
|
#include <map>
|
2022-02-03 17:21:38 +01:00
|
|
|
#include <string>
|
|
|
|
#include <sys/stat.h>
|
2020-11-29 12:59:58 +01:00
|
|
|
#include <utils/StringTools.h>
|
2022-02-03 17:21:38 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <vpad/input.h>
|
2020-11-29 12:59:58 +01:00
|
|
|
|
|
|
|
std::map<std::string, std::string> get_all_payloads(const char *relativefilepath);
|
|
|
|
|
|
|
|
std::string PayloadSelectionScreen(const std::map<std::string, std::string> &payloads);
|
|
|
|
|
2022-08-08 11:45:02 +02:00
|
|
|
extern "C" void init_wut();
|
|
|
|
extern "C" void fini_wut();
|
2022-03-04 13:23:36 +01:00
|
|
|
MEMExpHeapBlock *memory_start = nullptr;
|
2020-11-29 12:59:58 +01:00
|
|
|
|
|
|
|
extern "C" uint32_t start_wrapper(int argc, char **argv) {
|
|
|
|
doKernelSetup();
|
|
|
|
InitFunctionPointers();
|
|
|
|
|
2022-03-04 13:23:36 +01:00
|
|
|
// Save last entry on mem2 heap to detect leaked memory
|
|
|
|
MEMHeapHandle mem2_heap_handle = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2);
|
|
|
|
auto heap = (MEMExpHeap *) mem2_heap_handle;
|
|
|
|
memory_start = heap->usedList.tail;
|
2020-12-15 23:59:46 +01:00
|
|
|
|
2022-08-08 11:45:02 +02:00
|
|
|
init_wut();
|
2020-11-29 12:59:58 +01:00
|
|
|
|
2023-04-17 12:14:55 +02:00
|
|
|
initLogging();
|
2020-11-29 12:59:58 +01:00
|
|
|
|
2023-04-17 12:14:55 +02:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Hello from payload.elf multiloader");
|
2020-11-29 12:59:58 +01:00
|
|
|
|
|
|
|
VPADReadError err;
|
|
|
|
VPADStatus vpad_data;
|
|
|
|
VPADRead(VPAD_CHAN_0, &vpad_data, 1, &err);
|
|
|
|
|
|
|
|
uint32_t btn = vpad_data.hold | vpad_data.trigger;
|
|
|
|
|
|
|
|
std::map<std::string, std::string> payloads = get_all_payloads("wiiu/payloads");
|
|
|
|
|
|
|
|
uint32_t entryPoint = 0;
|
|
|
|
|
|
|
|
std::string payload_path = "wiiu/payloads/default/payload.elf";
|
|
|
|
if ((btn & VPAD_BUTTON_B) == VPAD_BUTTON_B) {
|
|
|
|
payload_path = PayloadSelectionScreen(payloads);
|
2023-04-17 12:14:55 +02:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Selected %s", payload_path.c_str());
|
2020-11-29 12:59:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
entryPoint = load_loader_elf_from_sd(nullptr, payload_path.c_str());
|
|
|
|
|
|
|
|
if (entryPoint != 0) {
|
2023-04-17 12:14:55 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Loaded payload entrypoint at %08X", entryPoint);
|
2020-11-29 12:59:58 +01:00
|
|
|
} else {
|
2023-04-17 12:14:55 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to load: %s", payload_path.c_str());
|
2020-11-29 12:59:58 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 12:14:55 +02:00
|
|
|
deinitLogging();
|
2020-11-29 12:59:58 +01:00
|
|
|
|
2022-08-08 11:45:02 +02:00
|
|
|
fini_wut();
|
2022-02-03 17:21:38 +01:00
|
|
|
|
2020-11-29 12:59:58 +01:00
|
|
|
revertKernelHook();
|
|
|
|
|
|
|
|
return entryPoint;
|
|
|
|
}
|
2022-03-04 13:29:02 +01:00
|
|
|
|
2020-11-29 12:59:58 +01:00
|
|
|
extern "C" int _start(int argc, char **argv) {
|
|
|
|
uint32_t entryPoint = start_wrapper(argc, argv);
|
2022-02-03 17:21:38 +01:00
|
|
|
|
2022-03-04 13:23:36 +01:00
|
|
|
MEMHeapHandle mem2_heap_handle = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2);
|
|
|
|
auto heap = (MEMExpHeap *) mem2_heap_handle;
|
|
|
|
// free leaked memory
|
|
|
|
if (memory_start) {
|
|
|
|
int leak_count = 0;
|
|
|
|
while (true) {
|
|
|
|
MEMExpHeapBlock *memory_end = heap->usedList.tail;
|
|
|
|
if (memory_end == memory_start) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto mem_ptr = &memory_end[1]; // &memory_end + sizeof(MEMExpHeapBlock);
|
|
|
|
free(mem_ptr);
|
|
|
|
leak_count++;
|
2020-12-15 23:59:46 +01:00
|
|
|
}
|
2023-04-17 12:14:55 +02:00
|
|
|
DEBUG_FUNCTION_LINE_INFO("Freed %d leaked memory blocks\n", leak_count);
|
2020-12-15 23:59:46 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 12:59:58 +01:00
|
|
|
int res = -1;
|
|
|
|
if (entryPoint != 0) {
|
|
|
|
res = ((int (*)(int, char **)) entryPoint)(argc, argv);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, std::string> get_all_payloads(const char *relativefilepath) {
|
|
|
|
std::map<std::string, std::string> result;
|
|
|
|
std::vector<std::string> payload_folders;
|
|
|
|
std::vector<std::string> files_inFolder;
|
|
|
|
|
2022-03-04 13:29:02 +01:00
|
|
|
DirList dirList(std::string("fs:/vol/external01/") + relativefilepath, nullptr, DirList::Dirs, 1);
|
|
|
|
dirList.SortList();
|
2020-11-29 12:59:58 +01:00
|
|
|
|
2022-03-04 13:29:02 +01:00
|
|
|
for (int i = 0; i < dirList.GetFilecount(); i++) {
|
|
|
|
auto curDirName = dirList.GetFilename(i);
|
|
|
|
auto curPath = dirList.GetFilepath(i);
|
|
|
|
DEBUG_FUNCTION_LINE("Reading path %s", curDirName);
|
2020-11-29 12:59:58 +01:00
|
|
|
|
2022-03-04 13:29:02 +01:00
|
|
|
auto payloadPath = std::string(curPath) + "/payload.elf";
|
|
|
|
auto fd = fopen(payloadPath.c_str(), "r");
|
|
|
|
if (fd) {
|
|
|
|
fclose(fd);
|
|
|
|
result[curDirName] = payloadPath;
|
2020-11-29 12:59:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PayloadSelectionScreen(const std::map<std::string, std::string> &payloads) {
|
|
|
|
// Init screen and screen buffers
|
|
|
|
OSScreenInit();
|
2020-12-15 23:59:46 +01:00
|
|
|
uint32_t screen_buf0_size = OSScreenGetBufferSizeEx(SCREEN_TV);
|
|
|
|
uint32_t screen_buf1_size = OSScreenGetBufferSizeEx(SCREEN_DRC);
|
2022-03-04 13:29:02 +01:00
|
|
|
auto *screenBuffer = (uint8_t *) memalign(0x100, screen_buf0_size + screen_buf1_size);
|
2022-02-03 17:21:38 +01:00
|
|
|
OSScreenSetBufferEx(SCREEN_TV, (void *) screenBuffer);
|
|
|
|
OSScreenSetBufferEx(SCREEN_DRC, (void *) (screenBuffer + screen_buf0_size));
|
2020-11-29 12:59:58 +01:00
|
|
|
|
|
|
|
OSScreenEnableEx(SCREEN_TV, 1);
|
|
|
|
OSScreenEnableEx(SCREEN_DRC, 1);
|
|
|
|
|
|
|
|
// Clear screens
|
|
|
|
OSScreenClearBufferEx(SCREEN_TV, 0);
|
|
|
|
OSScreenClearBufferEx(SCREEN_DRC, 0);
|
|
|
|
|
|
|
|
OSScreenFlipBuffersEx(SCREEN_TV);
|
|
|
|
OSScreenFlipBuffersEx(SCREEN_DRC);
|
|
|
|
|
|
|
|
VPADStatus vpad_data;
|
|
|
|
VPADReadError error;
|
2022-02-03 17:21:38 +01:00
|
|
|
int selected = 0;
|
2020-12-05 21:26:53 +01:00
|
|
|
std::string header = "Please choose your payload:";
|
2020-11-29 12:59:58 +01:00
|
|
|
while (true) {
|
|
|
|
// Clear screens
|
|
|
|
OSScreenClearBufferEx(SCREEN_TV, 0);
|
|
|
|
OSScreenClearBufferEx(SCREEN_DRC, 0);
|
|
|
|
|
|
|
|
int pos = 0;
|
2020-12-05 21:26:53 +01:00
|
|
|
|
|
|
|
OSScreenPutFontEx(SCREEN_TV, 0, pos, header.c_str());
|
|
|
|
OSScreenPutFontEx(SCREEN_DRC, 0, pos, header.c_str());
|
|
|
|
|
|
|
|
pos += 2;
|
|
|
|
|
2020-11-29 12:59:58 +01:00
|
|
|
int i = 0;
|
2022-02-03 17:21:38 +01:00
|
|
|
for (auto const &[key, val] : payloads) {
|
2020-11-29 12:59:58 +01:00
|
|
|
std::string text = StringTools::strfmt("%s %s", i == selected ? "> " : " ", key.c_str());
|
|
|
|
OSScreenPutFontEx(SCREEN_TV, 0, pos, text.c_str());
|
|
|
|
OSScreenPutFontEx(SCREEN_DRC, 0, pos, text.c_str());
|
|
|
|
i++;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
VPADRead(VPAD_CHAN_0, &vpad_data, 1, &error);
|
|
|
|
if (vpad_data.trigger == VPAD_BUTTON_A) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vpad_data.trigger == VPAD_BUTTON_UP) {
|
|
|
|
selected--;
|
|
|
|
if (selected < 0) {
|
|
|
|
selected = 0;
|
|
|
|
}
|
|
|
|
} else if (vpad_data.trigger == VPAD_BUTTON_DOWN) {
|
|
|
|
selected++;
|
|
|
|
if ((uint32_t) selected >= payloads.size()) {
|
|
|
|
selected = payloads.size() - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OSScreenFlipBuffersEx(SCREEN_TV);
|
|
|
|
OSScreenFlipBuffersEx(SCREEN_DRC);
|
|
|
|
|
|
|
|
OSSleepTicks(OSMillisecondsToTicks(16));
|
|
|
|
}
|
2022-03-04 13:23:36 +01:00
|
|
|
|
|
|
|
free(screenBuffer);
|
|
|
|
|
2020-11-29 12:59:58 +01:00
|
|
|
int i = 0;
|
2022-02-03 17:21:38 +01:00
|
|
|
for (auto const &[key, val] : payloads) {
|
2020-11-29 12:59:58 +01:00
|
|
|
if (i == selected) {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|