HBLInstallerWrapper/source/main.cpp

185 lines
5.5 KiB
C++
Raw Normal View History

2021-12-27 23:31:54 +01:00
#include <cstdint>
2021-12-27 16:53:04 +01:00
#include <malloc.h>
#include <cstring>
#include <proc_ui/procui.h>
2021-12-26 09:13:58 +01:00
#include <sysapp/launch.h>
2021-12-27 16:53:04 +01:00
#include <sysapp/title.h>
#include <coreinit/mcp.h>
#include <coreinit/debug.h>
2021-12-27 23:31:54 +01:00
#include <nn/acp.h>
#include <nn/spm.h>
#include <nn/sl.h>
2021-12-27 16:53:04 +01:00
#include <nn/ccr/sys_caffeine.h>
2021-12-27 22:36:23 +01:00
#include <coreinit/dynload.h>
2021-12-27 16:53:04 +01:00
#include <nn/act/client_cpp.h>
#include "hbl_install.h"
#include "utils.h"
#include "logger.h"
2021-12-26 09:13:58 +01:00
2021-12-27 16:53:04 +01:00
#include <whb/log_module.h>
#include <whb/log_udp.h>
#include <whb/log_cafe.h>
2021-12-26 09:13:58 +01:00
2021-12-27 16:53:04 +01:00
bool getQuickBoot() {
auto bootCheck = CCRSysCaffeineBootCheck();
if (bootCheck == 0) {
nn::sl::Initialize(MEMAllocFromDefaultHeapEx, MEMFreeToDefaultHeap);
char path[0x80];
2021-12-27 23:31:54 +01:00
nn::sl::GetDefaultDatabasePath(path, 0x80, 0x0005001010066000L); // ECO process
2021-12-27 16:53:04 +01:00
FSCmdBlock cmdBlock;
FSInitCmdBlock(&cmdBlock);
2021-12-26 09:13:58 +01:00
2021-12-27 16:53:04 +01:00
auto fileStream = new nn::sl::FileStream;
auto *fsClient = (FSClient *) memalign(0x40, sizeof(FSClient));
memset(fsClient, 0, sizeof(*fsClient));
FSAddClient(fsClient, FS_ERROR_FLAG_NONE);
2021-12-26 09:13:58 +01:00
2021-12-27 16:53:04 +01:00
fileStream->Initialize(fsClient, &cmdBlock, path, "r");
2021-12-26 09:13:58 +01:00
2021-12-27 16:53:04 +01:00
auto database = new nn::sl::LaunchInfoDatabase;
database->Load(fileStream, nn::sl::REGION_EUR);
2021-12-27 23:31:54 +01:00
2021-12-27 16:53:04 +01:00
CCRAppLaunchParam data; // load sys caffeine data
// load app launch param
CCRSysCaffeineGetAppLaunchParam(&data);
2021-12-26 09:13:58 +01:00
2021-12-27 16:53:04 +01:00
// get launch info for id
nn::sl::LaunchInfo info;
auto result = database->GetLaunchInfoById(&info, data.titleId);
2021-12-27 22:36:23 +01:00
2021-12-27 16:53:04 +01:00
delete database;
delete fileStream;
2021-12-26 09:13:58 +01:00
2021-12-27 16:53:04 +01:00
FSDelClient(fsClient, FS_ERROR_FLAG_NONE);
2021-12-26 09:13:58 +01:00
2021-12-27 16:53:04 +01:00
nn::sl::Finalize();
2021-12-27 23:31:54 +01:00
if (!result.IsSuccess()) {
DEBUG_FUNCTION_LINE("GetLaunchInfoById failed.");
return false;
}
2021-12-27 23:31:54 +01:00
if (info.titleId == 0x0005001010040000L ||
info.titleId == 0x0005001010040100L ||
info.titleId == 0x0005001010040200L) {
DEBUG_FUNCTION_LINE("Skip quick booting into the Wii U Menu");
return false;
}
2021-12-27 23:31:54 +01:00
if (!SYSCheckTitleExists(info.titleId)) {
2021-12-27 22:36:23 +01:00
DEBUG_FUNCTION_LINE("Title %016llX doesn't exist", info.titleId);
return false;
}
2021-12-27 23:31:54 +01:00
2021-12-27 16:53:04 +01:00
MCPTitleListType titleInfo;
int32_t handle = MCP_Open();
auto err = MCP_GetTitleInfo(handle, info.titleId, &titleInfo);
2021-12-27 16:53:04 +01:00
MCP_Close(handle);
2021-12-27 23:31:54 +01:00
if (err == 0) {
nn::act::Initialize();
for (int i = 0; i < 13; i++) {
char uuid[16];
2021-12-27 23:31:54 +01:00
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;
}
}
2021-12-27 23:31:54 +01:00
}
nn::act::Finalize();
2021-12-27 23:31:54 +01:00
ACPAssignTitlePatch(&titleInfo);
_SYSLaunchTitleWithStdArgsInNoSplash(info.titleId, nullptr);
return true;
}
2021-12-27 23:31:54 +01:00
return false;
2021-12-27 16:53:04 +01:00
} else {
DEBUG_FUNCTION_LINE("No quick boot");
2021-12-26 09:13:58 +01:00
}
2021-12-27 16:53:04 +01:00
return false;
}
2021-12-26 09:13:58 +01:00
2021-12-27 23:31:54 +01:00
static void initExternalStorage() {
nn::spm::Initialize();
2021-12-27 22:36:23 +01:00
FSCmdBlock cmdBlock;
FSInitCmdBlock(&cmdBlock);
2021-12-27 23:31:54 +01:00
FSVolumeInfo volumeInfo;
2021-12-27 16:53:04 +01:00
2021-12-27 22:36:23 +01:00
auto *fsClient = (FSClient *) memalign(0x40, sizeof(FSClient));
memset(fsClient, 0, sizeof(*fsClient));
FSAddClient(fsClient, FS_ERROR_FLAG_NONE);
2021-12-27 23:31:54 +01:00
char volumePaths[][19] = {"/vol/storage_usb01",
"/vol/storage_usb02",
"/vol/storage_usb03",
2021-12-27 22:36:23 +01:00
};
2021-12-27 23:31:54 +01:00
2021-12-27 22:36:23 +01:00
bool found = false;
2021-12-27 23:31:54 +01:00
for (auto path: volumePaths) {
2021-12-27 22:36:23 +01:00
DEBUG_FUNCTION_LINE("Check if %s is connected", path);
2021-12-27 23:31:54 +01:00
if (FSGetVolumeInfo(fsClient, &cmdBlock, path, &volumeInfo, FS_ERROR_FLAG_ALL) == 0) {
nn::spm::StorageIndex storageIndex = 0;
if (nn::spm::FindStorageByVolumeId(&storageIndex, (nn::spm::VolumeId *) volumeInfo.volumeId)) {
DEBUG_FUNCTION_LINE("Set DefaultExtendedStorage to %s", volumeInfo.volumeId);
nn::spm::SetDefaultExtendedStorageVolumeId((nn::spm::VolumeId *) volumeInfo.volumeId);
nn::spm::SetExtendedStorage(&storageIndex);
ACPMountExternalStorage();
found = true;
break;
} else {
DEBUG_FUNCTION_LINE("Failed to find Storage by VolumeId(%s)", volumeInfo.volumeId);
}
2021-12-27 22:36:23 +01:00
}
}
if (!found) {
DEBUG_FUNCTION_LINE("Fallback to empty ExtendedStorage");
2021-12-27 23:31:54 +01:00
nn::spm::VolumeId empty{};
nn::spm::SetDefaultExtendedStorageVolumeId(&empty);
nn::spm::StorageIndex storageIndex = 0;
nn::spm::SetExtendedStorage(&storageIndex);
2021-12-27 22:36:23 +01:00
}
2021-12-26 09:13:58 +01:00
2021-12-27 23:31:54 +01:00
FSDelClient(fsClient, FS_ERROR_FLAG_ALL);
2021-12-27 22:36:23 +01:00
2021-12-27 23:31:54 +01:00
nn::spm::Finalize();
}
void bootHomebrewLauncher() {
2021-12-27 16:53:04 +01:00
uint64_t titleId = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_MII_MAKER);
_SYSLaunchTitleWithStdArgsInNoSplash(titleId, nullptr);
}
2021-12-26 09:13:58 +01:00
int main(int argc, char **argv) {
2021-12-27 23:31:54 +01:00
if (!WHBLogModuleInit()) {
2021-12-27 16:53:04 +01:00
WHBLogCafeInit();
WHBLogUdpInit();
2021-12-26 09:13:58 +01:00
}
2021-12-27 23:31:54 +01:00
2021-12-27 16:53:04 +01:00
initExternalStorage();
2021-12-27 23:31:54 +01:00
2021-12-27 16:53:04 +01:00
InstallHBL();
2021-12-27 23:31:54 +01:00
if (getQuickBoot()) {
return 0;
}
2021-12-27 23:31:54 +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-27 16:53:04 +01:00
}
2021-12-26 09:13:58 +01:00
return 0;
}