Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ Press Y on the autoboot menu to set autobooting to this titles. To revert it, fo
- Boot into Wii U Menu, Homebrew Channel, vWii System Menu or vWii Homebrew Channel.
- Full support of Quick Boot Menu of the Gamepad when coldbooting
- Set a autoboot title
- Add your own vWii or Wii U titles to the menu

## Custom boot entries
Create `bootOptions.cfg` next to `autoboot.cfg` (in the `[ENVIRONMENT]` folder) with one entry per line: a name, a comma, and the title's full 64-bit title id.

```
# vWii titles start with 00010001, Wii U titles with 00050000
My vWii Channel,0x0001000152494654
Mii Maker,0x000500101004A200
```

The title id decides how the entry is launched: vWii titles through the vWii, Wii U titles directly. Entries whose title is not installed are left out of the menu. Up to 4 entries are shown, as many as fit: the menu has room for 7 rows in all. System applets (title ids starting with 00050030, like the Internet Browser) can't be custom entries. A custom entry can be set as the autoboot title like any other.

## Buildflags

Expand Down
63 changes: 50 additions & 13 deletions source/BootUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "logger.h"
#include <codecvt>
#include <coreinit/filesystem_fsa.h>
#include <cstdio>
#include <filesystem>
#include <locale>
#include <malloc.h>
Expand Down Expand Up @@ -116,26 +117,29 @@ void bootvWiiMenu() {
launchvWiiTitle(0);
}

uint64_t getVWiiHBLTitleId() {
// fall back to booting the vWii system menu if anything fails
uint64_t titleId = 0;
bool isVWiiTitle(uint64_t titleId) {
return (titleId >> 48) == 0x0001;
}

bool isWiiUTitle(uint64_t titleId) {
return (titleId >> 48) == 0x0005;
}

bool vWiiTitleExists(uint64_t titleId) {
bool exists = false;

FSAInit();
auto client = FSAAddClient(nullptr);
if (client > 0) {
if (Mocha_UnlockFSClientEx(client) == MOCHA_RESULT_SUCCESS) {
// mount the slccmpt
if (FSAMount(client, "/dev/slccmpt01", "/vol/storage_abm_slccmpt01", FSA_MOUNT_FLAG_GLOBAL_MOUNT, nullptr, 0) >= 0) {
// every installed title has a TMD; content file names depend on the title
char path[0x80];
snprintf(path, sizeof(path), "/vol/storage_abm_slccmpt01/title/%08x/%08x/content/title.tmd",
(uint32_t) (titleId >> 32), (uint32_t) titleId);
FSStat stat;

// test if the OHBC or HBC is installed
if (FSAGetStat(client, "/vol/storage_abm_slccmpt01/title/00010001/4f484243/content/00000000.app", &stat) >= 0) {
titleId = 0x000100014F484243L; // 'OHBC'
} else if (FSAGetStat(client, "/vol/storage_abm_slccmpt01/title/00010001/4c554c5a/content/00000000.app", &stat) >= 0) {
titleId = 0x000100014C554C5AL; // 'LULZ'
} else {
DEBUG_FUNCTION_LINE("Cannot find HBC");
}
exists = FSAGetStat(client, path, &stat) >= 0;
FSAUnmount(client, "/vol/storage_abm_slccmpt01", FSA_UNMOUNT_FLAG_FORCE);
} else {
DEBUG_FUNCTION_LINE_ERR("Failed to mount slccmpt01");
Expand All @@ -147,11 +151,44 @@ uint64_t getVWiiHBLTitleId() {
} else {
DEBUG_FUNCTION_LINE_ERR("Failed to add FSAClient");
}
return titleId;
return exists;
}

bool titleExists(uint64_t titleId) {
if (isVWiiTitle(titleId)) {
return vWiiTitleExists(titleId);
}
if (isWiiUTitle(titleId)) {
return SYSCheckTitleExists(titleId);
}
return false;
}

uint64_t getVWiiHBLTitleId() {
// test if the OHBC or HBC is installed, fall back to booting the vWii system menu if anything fails
if (vWiiTitleExists(0x000100014F484243L)) {
return 0x000100014F484243L; // 'OHBC'
}
if (vWiiTitleExists(0x000100014C554C5AL)) {
return 0x000100014C554C5AL; // 'LULZ'
}
DEBUG_FUNCTION_LINE("Cannot find HBC");
return 0;
}

void bootHomebrewChannel() {
uint64_t titleId = getVWiiHBLTitleId();
DEBUG_FUNCTION_LINE("Launching vWii title %016llx", titleId);
launchvWiiTitle(titleId);
}

void bootCustomTitle(uint64_t titleId) {
if (isVWiiTitle(titleId)) {
DEBUG_FUNCTION_LINE("Launching vWii title %016llx", titleId);
launchvWiiTitle(titleId);
} else {
handleAccountSelection();
DEBUG_FUNCTION_LINE("Launching Wii U title %016llx", titleId);
_SYSLaunchTitleWithStdArgsInNoSplash(titleId, nullptr);
}
}
14 changes: 13 additions & 1 deletion source/BootUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ void bootvWiiMenu();

void bootHomebrewChannel();

uint64_t getVWiiHBLTitleId();
uint64_t getVWiiHBLTitleId();

// vWii titles start with 0x0001, Wii U titles with 0x0005.
bool isVWiiTitle(uint64_t titleId);

bool isWiiUTitle(uint64_t titleId);

bool vWiiTitleExists(uint64_t titleId);

bool titleExists(uint64_t titleId);

// Launches a vWii or Wii U title; the title id decides which.
void bootCustomTitle(uint64_t titleId);
4 changes: 2 additions & 2 deletions source/DrawUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ void DrawUtils::print(uint32_t x, uint32_t y, const char *string, bool alignRigh
auto *buffer = new wchar_t[strlen(string) + 1];

size_t num = mbstowcs(buffer, string, strlen(string));
if (num > 0) {
if (num > 0 && num != (size_t) -1) { // (size_t) -1: a byte mbstowcs can't convert
buffer[num] = 0;
} else {
wchar_t *tmp = buffer;
Expand Down Expand Up @@ -374,7 +374,7 @@ uint32_t DrawUtils::getTextWidth(const char *string) {
auto *buffer = new wchar_t[strlen(string) + 1];

size_t num = mbstowcs(buffer, string, strlen(string));
if (num > 0) {
if (num > 0 && num != (size_t) -1) {
buffer[num] = 0;
} else {
wchar_t *tmp = buffer;
Expand Down
123 changes: 115 additions & 8 deletions source/MenuUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#include "MenuUtils.h"
#include "ACTAccountInfo.h"
#include "BootUtils.h"
#include "DrawUtils.h"
#include "InputUtils.h"
#include "PairUtils.h"
#include "logger.h"
#include "main.h"
#include "utils.h"
#include "version.h"
#include <cctype>
#include <coreinit/debug.h>
#include <coreinit/filesystem_fsa.h>
#include <coreinit/screen.h>
#include <coreinit/thread.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <gx2/state.h>
#include <malloc.h>
Expand All @@ -30,13 +34,101 @@ const char *autoboot_config_strings[] = {
"vwii_homebrew_channel",
};

int32_t readAutobootOption(std::string &configPath) {
#define CUSTOM_AUTOBOOT_PREFIX "title_"

static void trimInPlace(char *str) {
char *start = str;
while (*start && isspace((unsigned char) *start)) {
start++;
}
char *end = start + strlen(start);
while (end > start && isspace((unsigned char) end[-1])) {
end--;
}
*end = '\0';
memmove(str, start, end - start + 1);
}

std::vector<CustomBootOption> readCustomBootOptions(const std::string &path) {
std::vector<CustomBootOption> result;
FILE *f = fopen(path.c_str(), "r");
if (!f) {
return result;
}

// One entry per line: "Name,0x0001000152494654". The title id decides how it is launched.
char line[256];
while (fgets(line, sizeof(line), f)) {
trimInPlace(line);
if (line[0] == '\0' || line[0] == '#') {
continue;
}

char *comma = strrchr(line, ',');
if (!comma) {
DEBUG_FUNCTION_LINE_WARN("bootOptions.cfg: no comma in \"%s\"", line);
continue;
}
*comma = '\0';

char *idStr = comma + 1;
trimInPlace(idStr);
char *idEnd = nullptr;
uint64_t titleId = strtoull(idStr, &idEnd, 16);
if (idEnd == idStr || *idEnd != '\0' || !(isVWiiTitle(titleId) || isWiiUTitle(titleId))) {
DEBUG_FUNCTION_LINE_WARN("bootOptions.cfg: \"%s\" is not a full vWii or Wii U title id", idStr);
continue;
}

// System applets (the browser, the eShop, ...) need their own launch calls.
if ((titleId >> 32) == 0x00050030) {
DEBUG_FUNCTION_LINE_WARN("bootOptions.cfg: %016llx is a system applet, which can't be a custom entry", titleId);
continue;
}

trimInPlace(line);
if (line[0] == '\0') {
DEBUG_FUNCTION_LINE_WARN("bootOptions.cfg: entry for %016llx has no name", titleId);
continue;
}

bool duplicate = false;
for (auto &option : result) {
duplicate |= option.titleId == titleId;
}
if (duplicate) {
DEBUG_FUNCTION_LINE_WARN("bootOptions.cfg: %016llx is listed twice", titleId);
continue;
}

if (result.size() >= MAX_CUSTOM_BOOT_OPTIONS) {
DEBUG_FUNCTION_LINE_WARN("bootOptions.cfg: only the first %d entries are used", MAX_CUSTOM_BOOT_OPTIONS);
break;
}
result.push_back({line, titleId});
}
fclose(f);
return result;
}

int32_t readAutobootOption(std::string &configPath, const std::vector<CustomBootOption> &customOptions) {
FILE *f = fopen(configPath.c_str(), "r");
if (f) {
char buf[128]{};
fgets(buf, sizeof(buf), f);
fclose(f);

// Custom entries are saved by title id, so renaming one keeps it selected.
if (strncmp(CUSTOM_AUTOBOOT_PREFIX, buf, strlen(CUSTOM_AUTOBOOT_PREFIX)) == 0) {
uint64_t titleId = strtoull(buf + strlen(CUSTOM_AUTOBOOT_PREFIX), nullptr, 16);
for (uint32_t i = 0; i < customOptions.size(); i++) {
if (customOptions[i].titleId == titleId) {
return BOOT_OPTION_CUSTOM_BASE + i;
}
}
return -1;
}

for (uint32_t i = 0; i < sizeof(autoboot_config_strings) / sizeof(char *); i++) {
if (strncmp(autoboot_config_strings[i], buf, strlen(autoboot_config_strings[i])) == 0) {
return i;
Expand All @@ -46,10 +138,19 @@ int32_t readAutobootOption(std::string &configPath) {
return -1;
}

void writeAutobootOption(std::string &configPath, int32_t autobootOption) {
void writeAutobootOption(std::string &configPath, int32_t autobootOption, const std::vector<CustomBootOption> &customOptions) {
FILE *f = fopen(configPath.c_str(), "w");
if (f) {
if (autobootOption >= 0) {
if (autobootOption >= BOOT_OPTION_CUSTOM_BASE) {
uint32_t index = autobootOption - BOOT_OPTION_CUSTOM_BASE;
if (index < customOptions.size()) {
char buf[sizeof(CUSTOM_AUTOBOOT_PREFIX) + 16];
snprintf(buf, sizeof(buf), CUSTOM_AUTOBOOT_PREFIX "%016llx", customOptions[index].titleId);
fputs(buf, f);
} else {
fputs("none", f);
}
} else if (autobootOption >= 0) {
fputs(autoboot_config_strings[autobootOption], f);
} else {
fputs("none", f);
Expand Down Expand Up @@ -108,7 +209,8 @@ void drawMenuScreen(const std::map<uint32_t, std::string> &menu, uint32_t select
DrawUtils::endDraw();
}

int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, const std::map<uint32_t, std::string> &menu) {
int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, const std::map<uint32_t, std::string> &menu,
const std::vector<CustomBootOption> &customOptions) {
auto screenBuffer = DrawUtils::InitOSScreen();
if (!screenBuffer) {
OSFatal("AutobootModule: Failed to alloc memory for screen");
Expand All @@ -124,6 +226,9 @@ int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, c

int32_t selectedIndex = autobootOptionInput > 0 ? autobootOptionInput : 0;
int autobootIndex = autobootOptionInput;
// Set by X or Y: the saved choice is written even if it matches the input, so X can clear
// a saved custom entry whose title isn't installed (the menu gets -1 for it).
bool autobootChanged = false;

// "Convert" id to index
int32_t offset = 0;
Expand Down Expand Up @@ -175,9 +280,11 @@ int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, c
} else if (input.trigger & VPAD_BUTTON_A) {
break;
} else if (input.trigger & (VPAD_BUTTON_X | VPAD_BUTTON_MINUS)) {
autobootIndex = -1;
autobootIndex = -1;
autobootChanged = true;
} else if (input.trigger & (VPAD_BUTTON_Y | VPAD_BUTTON_PLUS)) {
autobootIndex = selectedIndex;
autobootIndex = selectedIndex;
autobootChanged = true;
} else if ((input.hold & (VPAD_BUTTON_PLUS | VPAD_BUTTON_MINUS)) == (VPAD_BUTTON_PLUS | VPAD_BUTTON_MINUS)) {
if (holdUpdateBlockedForFrames++ > 50) {
if (gUpdatesBlocked) {
Expand Down Expand Up @@ -216,8 +323,8 @@ int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, c
autoboot = std::next(menu.begin(), autobootIndex)->first;
}

if (autoboot != autobootOptionInput) {
writeAutobootOption(configPath, autoboot);
if (autobootChanged || autoboot != autobootOptionInput) {
writeAutobootOption(configPath, autoboot, customOptions);
}

return selected;
Expand Down
21 changes: 18 additions & 3 deletions source/MenuUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,26 @@ enum {
BOOT_OPTION_VWII_HOMEBREW_CHANNEL,
};

int32_t readAutobootOption(std::string &configPath);
// Entries from bootOptions.cfg use BOOT_OPTION_CUSTOM_BASE + their index in the file.
#define BOOT_OPTION_CUSTOM_BASE 0x100
// At most this many custom entries, fewer if the menu runs out of rows.
#define MAX_CUSTOM_BOOT_OPTIONS 4
// The menu doesn't scroll, and an eighth row would cover the update status line.
#define MAX_MENU_ROWS 7

struct CustomBootOption {
std::string name;
uint64_t titleId;
};

std::vector<CustomBootOption> readCustomBootOptions(const std::string &path);

int32_t readAutobootOption(std::string &configPath, const std::vector<CustomBootOption> &customOptions);

void writeAutobootOption(std::string &configPath, int32_t autobootOption);
void writeAutobootOption(std::string &configPath, int32_t autobootOption, const std::vector<CustomBootOption> &customOptions);

int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, const std::map<uint32_t, std::string> &menu);
int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, const std::map<uint32_t, std::string> &menu,
const std::vector<CustomBootOption> &customOptions);

nn::act::SlotNo handleAccountSelectScreen(const std::vector<std::shared_ptr<AccountInfo>> &data);

Expand Down
Loading