AutobootModule/source/main.cpp

420 lines
12 KiB
C++
Raw Permalink Normal View History

2021-12-28 20:17:21 +01:00
#include <malloc.h>
2021-12-28 23:16:34 +01:00
#include <cstdio>
#include <cstring>
#include <string>
2021-12-28 20:17:21 +01:00
#include <coreinit/screen.h>
#include <coreinit/filesystem.h>
#include <coreinit/memdefaultheap.h>
#include <coreinit/title.h>
2021-12-28 20:17:21 +01:00
#include <vpad/input.h>
#include <sysapp/launch.h>
#include <sysapp/title.h>
#include <padscore/kpad.h>
#include <nn/acp.h>
#include <nn/act.h>
2021-12-29 15:09:19 +01:00
#include <nn/cmpt.h>
2021-12-28 20:17:21 +01:00
#include <nn/ccr/sys_caffeine.h>
2021-12-29 15:09:19 +01:00
#include <nn/sl.h>
2021-12-28 20:17:21 +01:00
#include <nn/spm.h>
#include <whb/log_module.h>
#include <whb/log_udp.h>
#include <whb/log_cafe.h>
#include "DrawUtils.h"
#include "logger.h"
2021-12-29 17:51:30 +01:00
#include "icon_png.h"
2021-12-28 20:17:21 +01:00
#define COLOR_WHITE Color(0xffffffff)
#define COLOR_BLACK Color(0, 0, 0, 255)
#define COLOR_BACKGROUND COLOR_BLACK
#define COLOR_TEXT COLOR_WHITE
#define COLOR_TEXT2 Color(0xB3ffffff)
#define COLOR_AUTOBOOT Color(0xaeea00ff)
#define COLOR_BORDER Color(204, 204, 204, 255)
#define COLOR_BORDER_HIGHLIGHTED Color(0x3478e4ff)
2021-12-28 23:16:34 +01:00
enum {
BOOT_OPTION_WII_U_MENU,
BOOT_OPTION_HOMEBREW_LAUNCHER,
BOOT_OPTION_VWII_SYSTEM_MENU,
BOOT_OPTION_VWII_HOMEBREW_CHANNEL,
};
2021-12-29 17:02:08 +01:00
static const char *menu_options[] = {
"Wii U Menu",
"Homebrew Launcher",
"vWii System Menu",
"vWii Homebrew Channel",
2021-12-28 20:17:21 +01:00
};
2021-12-29 17:02:08 +01:00
static const char *autoboot_config_strings[] = {
"wiiu_menu",
"homebrew_launcher",
"vwii_system_menu",
"vwii_homebrew_channel",
2021-12-28 23:16:34 +01:00
};
std::string configPath;
2021-12-28 23:16:34 +01:00
2021-12-29 17:02:08 +01:00
int32_t readAutobootOption() {
FILE *f = fopen(configPath.c_str(), "r");
2021-12-28 23:16:34 +01:00
if (f) {
char buf[128]{};
fgets(buf, sizeof(buf), f);
fclose(f);
2021-12-29 17:02:08 +01:00
for (uint32_t i = 0; i < sizeof(autoboot_config_strings) / sizeof(char *); i++) {
2021-12-28 23:16:34 +01:00
if (strncmp(autoboot_config_strings[i], buf, strlen(autoboot_config_strings[i])) == 0) {
2021-12-29 17:02:08 +01:00
return i;
2021-12-28 23:16:34 +01:00
}
}
}
2021-12-29 17:02:08 +01:00
return -1;
2021-12-28 23:16:34 +01:00
}
2021-12-29 17:02:08 +01:00
void writeAutobootOption(int32_t autobootOption) {
FILE *f = fopen(configPath.c_str(), "w");
2021-12-28 23:16:34 +01:00
if (f) {
if (autobootOption >= 0) {
fputs(autoboot_config_strings[autobootOption], f);
2021-12-29 17:02:08 +01:00
} else {
2021-12-28 23:16:34 +01:00
fputs("none", f);
}
fclose(f);
}
}
2021-12-28 20:17:21 +01:00
bool getQuickBoot() {
auto bootCheck = CCRSysCaffeineBootCheck();
if (bootCheck == 0) {
nn::sl::Initialize(MEMAllocFromDefaultHeapEx, MEMFreeToDefaultHeap);
char path[0x80];
2021-12-29 15:09:19 +01:00
nn::sl::GetDefaultDatabasePath(path, 0x80, 0x0005001010066000); // ECO process
2021-12-28 20:17:21 +01:00
FSCmdBlock cmdBlock;
FSInitCmdBlock(&cmdBlock);
auto fileStream = new nn::sl::FileStream;
auto *fsClient = (FSClient *) memalign(0x40, sizeof(FSClient));
memset(fsClient, 0, sizeof(*fsClient));
FSAddClient(fsClient, FS_ERROR_FLAG_NONE);
fileStream->Initialize(fsClient, &cmdBlock, path, "r");
auto database = new nn::sl::LaunchInfoDatabase;
database->Load(fileStream, nn::sl::REGION_EUR);
CCRAppLaunchParam data; // load sys caffeine data
// load app launch param
CCRSysCaffeineGetAppLaunchParam(&data);
// get launch info for id
nn::sl::LaunchInfo info;
auto result = database->GetLaunchInfoById(&info, data.titleId);
delete database;
delete fileStream;
FSDelClient(fsClient, FS_ERROR_FLAG_NONE);
nn::sl::Finalize();
if (!result.IsSuccess()) {
DEBUG_FUNCTION_LINE("GetLaunchInfoById failed.");
return false;
}
if (info.titleId == 0x0005001010040000L ||
info.titleId == 0x0005001010040100L ||
info.titleId == 0x0005001010040200L) {
DEBUG_FUNCTION_LINE("Skip quick booting into the Wii U Menu");
return false;
}
if (!SYSCheckTitleExists(info.titleId)) {
DEBUG_FUNCTION_LINE("Title %016llX doesn't exist", info.titleId);
return false;
}
MCPTitleListType titleInfo;
int32_t handle = MCP_Open();
auto err = MCP_GetTitleInfo(handle, info.titleId, &titleInfo);
MCP_Close(handle);
if (err == 0) {
nn::act::Initialize();
2021-12-29 17:02:08 +01:00
for (int32_t i = 0; i < 13; i++) {
2021-12-28 20:17:21 +01:00
char uuid[16];
result = nn::act::GetUuidEx(uuid, i);
if (result.IsSuccess()) {
if (memcmp(uuid, data.uuid, 8) == 0) {
DEBUG_FUNCTION_LINE("Load Console account %d", i);
nn::act::LoadConsoleAccount(i, 0, 0, 0);
break;
}
}
}
nn::act::Finalize();
ACPAssignTitlePatch(&titleInfo);
_SYSLaunchTitleWithStdArgsInNoSplash(info.titleId, nullptr);
return true;
}
return false;
} else {
DEBUG_FUNCTION_LINE("No quick boot");
}
return false;
}
2021-12-29 17:02:08 +01:00
static void initExternalStorage() {
if (OSGetTitleID() == _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_MII_MAKER)) {
// nn::spm functions always call OSFatal when they fail, so we make sure have permission to use
// the lib before actually using it.
return;
}
2021-12-28 20:17:21 +01:00
nn::spm::Initialize();
nn::spm::StorageListItem items[0x20];
int32_t numItems = nn::spm::GetStorageList(items, 0x20);
bool found = false;
2021-12-29 17:02:08 +01:00
for (int32_t i = 0; i < numItems; i++) {
2021-12-28 20:17:21 +01:00
if (items[i].type == nn::spm::STORAGE_TYPE_WFS) {
nn::spm::StorageInfo info{};
if (nn::spm::GetStorageInfo(&info, &items[i].index) == 0) {
DEBUG_FUNCTION_LINE("Using %s for extended storage", info.path);
nn::spm::SetExtendedStorage(&items[i].index);
ACPMountExternalStorage();
nn::spm::SetDefaultExtendedStorageVolumeId(info.volumeId);
found = true;
break;
}
}
}
if (!found) {
DEBUG_FUNCTION_LINE("Fallback to empty ExtendedStorage");
nn::spm::VolumeId empty{};
nn::spm::SetDefaultExtendedStorageVolumeId(empty);
2021-12-29 17:02:08 +01:00
2021-12-28 20:17:21 +01:00
nn::spm::StorageIndex storageIndex = 0;
nn::spm::SetExtendedStorage(&storageIndex);
}
nn::spm::Finalize();
}
2021-12-29 17:02:08 +01:00
void bootWiiUMenu() {
2021-12-28 20:17:21 +01:00
nn::act::Initialize();
nn::act::SlotNo slot = nn::act::GetSlotNo();
nn::act::SlotNo defaultSlot = nn::act::GetDefaultAccount();
nn::act::Finalize();
if (defaultSlot) { //normal menu boot
SYSLaunchMenu();
} else { //show mii select
_SYSLaunchMenuWithCheckingAccount(slot);
}
}
2021-12-29 17:02:08 +01:00
void bootHomebrewLauncher() {
2021-12-28 20:17:21 +01:00
uint64_t titleId = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_MII_MAKER);
_SYSLaunchTitleWithStdArgsInNoSplash(titleId, nullptr);
}
2021-12-29 17:02:08 +01:00
static void launchvWiiTitle(uint32_t titleId_low, uint32_t titleId_high) {
2021-12-28 20:17:21 +01:00
// we need to init kpad for cmpt
KPADInit();
// Try to find a screen type that works
2021-12-29 15:09:19 +01:00
CMPTAcctSetScreenType(CMPT_SCREEN_TYPE_BOTH);
2021-12-28 20:17:21 +01:00
if (CMPTCheckScreenState() < 0) {
2021-12-29 15:09:19 +01:00
CMPTAcctSetScreenType(CMPT_SCREEN_TYPE_DRC);
2021-12-28 20:17:21 +01:00
if (CMPTCheckScreenState() < 0) {
2021-12-29 15:09:19 +01:00
CMPTAcctSetScreenType(CMPT_SCREEN_TYPE_TV);
2021-12-28 20:17:21 +01:00
}
}
uint32_t dataSize = 0;
CMPTGetDataSize(&dataSize);
2021-12-29 17:02:08 +01:00
void *dataBuffer = memalign(0x40, dataSize);
if (titleId_low == 0 && titleId_high == 0) {
CMPTLaunchMenu(dataBuffer, dataSize);
2021-12-29 17:02:08 +01:00
} else {
CMPTLaunchTitle(dataBuffer, dataSize, titleId_low, titleId_high);
}
2021-12-28 20:17:21 +01:00
free(dataBuffer);
}
2021-12-29 17:02:08 +01:00
void bootvWiiMenu() {
launchvWiiTitle(0, 0);
2021-12-28 20:17:21 +01:00
}
2021-12-29 17:02:08 +01:00
void bootHomebrewChannel() {
2021-12-28 23:16:34 +01:00
launchvWiiTitle(0x00010001, 0x4f484243); // 'OHBC'
2021-12-28 20:17:21 +01:00
}
int32_t handleMenuScreen(int32_t autobootOptionInput) {
2021-12-28 20:17:21 +01:00
OSScreenInit();
uint32_t tvBufferSize = OSScreenGetBufferSizeEx(SCREEN_TV);
uint32_t drcBufferSize = OSScreenGetBufferSizeEx(SCREEN_DRC);
auto *screenBuffer = (uint8_t *) memalign(0x100, tvBufferSize + drcBufferSize);
2021-12-28 20:17:21 +01:00
OSScreenSetBufferEx(SCREEN_TV, screenBuffer);
OSScreenSetBufferEx(SCREEN_DRC, screenBuffer + tvBufferSize);
OSScreenEnableEx(SCREEN_TV, TRUE);
OSScreenEnableEx(SCREEN_DRC, TRUE);
DrawUtils::initBuffers(screenBuffer, tvBufferSize, screenBuffer + tvBufferSize, drcBufferSize);
DrawUtils::initFont();
2021-12-29 17:12:47 +01:00
uint32_t selected = autobootOptionInput > 0 ? autobootOptionInput : 0;
int autoboot = autobootOptionInput;
2021-12-28 20:17:21 +01:00
bool redraw = true;
while (true) {
VPADStatus vpad{};
2021-12-29 15:09:19 +01:00
VPADRead(VPAD_CHAN_0, &vpad, 1, nullptr);
2021-12-28 20:17:21 +01:00
if (vpad.trigger & VPAD_BUTTON_UP) {
if (selected > 0) {
selected--;
redraw = true;
}
2021-12-29 17:02:08 +01:00
} else if (vpad.trigger & VPAD_BUTTON_DOWN) {
if (selected < sizeof(menu_options) / sizeof(char *) - 1) {
2021-12-28 20:17:21 +01:00
selected++;
redraw = true;
}
2021-12-29 17:02:08 +01:00
} else if (vpad.trigger & VPAD_BUTTON_A) {
2021-12-28 20:17:21 +01:00
break;
2021-12-29 17:02:08 +01:00
} else if (vpad.trigger & VPAD_BUTTON_X) {
autoboot = -1;
2021-12-28 20:17:21 +01:00
redraw = true;
2021-12-29 17:02:08 +01:00
} else if (vpad.trigger & VPAD_BUTTON_Y) {
autoboot = selected;
2021-12-28 20:17:21 +01:00
redraw = true;
}
if (redraw) {
DrawUtils::beginDraw();
DrawUtils::clear(COLOR_BACKGROUND);
// draw buttons
uint32_t index = 8 + 24 + 8 + 4;
2021-12-29 17:02:08 +01:00
for (uint32_t i = 0; i < sizeof(menu_options) / sizeof(char *); i++) {
2021-12-28 20:17:21 +01:00
if (i == selected) {
2021-12-29 17:02:08 +01:00
DrawUtils::drawRect(16, index, SCREEN_WIDTH - 16 * 2, 44, 4, COLOR_BORDER_HIGHLIGHTED);
2021-12-28 20:17:21 +01:00
} else {
DrawUtils::drawRect(16, index, SCREEN_WIDTH - 16 * 2, 44, 2, (i == (uint32_t) autoboot) ? COLOR_AUTOBOOT : COLOR_BORDER);
2021-12-28 20:17:21 +01:00
}
DrawUtils::setFontSize(24);
DrawUtils::setFontColor((i == (uint32_t) autoboot) ? COLOR_AUTOBOOT : COLOR_TEXT);
2021-12-29 17:02:08 +01:00
DrawUtils::print(16 * 2, index + 8 + 24, menu_options[i]);
2021-12-28 20:17:21 +01:00
index += 42 + 8;
}
DrawUtils::setFontColor(COLOR_TEXT);
// draw top bar
DrawUtils::setFontSize(24);
2021-12-29 17:51:30 +01:00
DrawUtils::drawPNG(16, 2, icon_png);
DrawUtils::print(64 + 2, 6 + 24, "Tiramisu Boot Selector");
2021-12-28 20:17:21 +01:00
DrawUtils::drawRectFilled(8, 8 + 24 + 4, SCREEN_WIDTH - 8 * 2, 3, COLOR_WHITE);
// draw bottom bar
2021-12-29 17:02:08 +01:00
DrawUtils::drawRectFilled(8, SCREEN_HEIGHT - 24 - 8 - 4, SCREEN_WIDTH - 8 * 2, 3, COLOR_WHITE);
2021-12-28 20:17:21 +01:00
DrawUtils::setFontSize(18);
DrawUtils::print(16, SCREEN_HEIGHT - 8, "\ue07d Navigate ");
DrawUtils::print(SCREEN_WIDTH - 16, SCREEN_HEIGHT - 8, "\ue000 Choose", true);
2021-12-29 17:51:30 +01:00
const char *autobootHints = "\ue002 Clear Autoboot / \ue003 Select Autoboot";
2021-12-28 20:17:21 +01:00
DrawUtils::print(SCREEN_WIDTH / 2 + DrawUtils::getTextWidth(autobootHints) / 2, SCREEN_HEIGHT - 8, autobootHints, true);
DrawUtils::endDraw();
redraw = false;
}
}
2021-12-29 17:51:30 +01:00
DrawUtils::beginDraw();
DrawUtils::clear(COLOR_BLACK);
DrawUtils::endDraw();
2021-12-28 20:17:21 +01:00
DrawUtils::deinitFont();
2021-12-30 14:46:42 +01:00
OSScreenShutdown();
2021-12-28 20:17:21 +01:00
free(screenBuffer);
2021-12-28 23:16:34 +01:00
2021-12-30 14:46:42 +01:00
if (autoboot != autobootOptionInput) {
writeAutobootOption(autoboot);
}
2021-12-28 23:16:34 +01:00
return selected;
2021-12-28 20:17:21 +01:00
}
2021-12-29 17:02:08 +01:00
int32_t main(int32_t argc, char **argv) {
#ifdef DEBUG
2021-12-28 20:17:21 +01:00
if (!WHBLogModuleInit()) {
WHBLogCafeInit();
WHBLogUdpInit();
}
#endif // DEBUG
2021-12-28 20:17:21 +01:00
DEBUG_FUNCTION_LINE("Hello from Autoboot");
initExternalStorage();
if (getQuickBoot()) {
return 0;
}
configPath = "fs:/vol/exernal01/wiiu/autoboot.cfg";
if (argc >= 1) {
configPath = std::string(argv[0]) + "/autoboot.cfg";
}
2021-12-29 17:02:08 +01:00
int32_t bootSelection = readAutobootOption();
2021-12-28 23:16:34 +01:00
2021-12-28 20:17:21 +01:00
VPADStatus vpad{};
VPADRead(VPAD_CHAN_0, &vpad, 1, nullptr);
2021-12-28 20:17:21 +01:00
if ((bootSelection == -1) || (vpad.hold & VPAD_BUTTON_PLUS)) {
2021-12-29 17:02:08 +01:00
bootSelection = handleMenuScreen(bootSelection);
2021-12-28 20:17:21 +01:00
}
2021-12-28 23:16:34 +01:00
if (bootSelection >= 0) {
switch (bootSelection) {
2021-12-29 17:02:08 +01:00
case BOOT_OPTION_WII_U_MENU:
bootWiiUMenu();
break;
case BOOT_OPTION_HOMEBREW_LAUNCHER:
bootHomebrewLauncher();
break;
case BOOT_OPTION_VWII_SYSTEM_MENU:
bootvWiiMenu();
break;
case BOOT_OPTION_VWII_HOMEBREW_CHANNEL:
bootHomebrewChannel();
break;
default:
bootWiiUMenu();
break;
2021-12-28 23:16:34 +01:00
}
2021-12-29 17:02:08 +01:00
} else {
bootWiiUMenu();
2021-12-28 23:16:34 +01:00
}
2021-12-28 20:17:21 +01:00
return 0;
}