Fix USB usage

This commit is contained in:
Maschell 2021-12-27 23:31:54 +01:00
parent f05f1a2420
commit 27418f035c
2 changed files with 91 additions and 111 deletions

View File

@ -1,5 +1,5 @@
#include <stdint.h> #include <cstdint>
#include <string.h> #include <cstring>
#include <malloc.h> #include <malloc.h>
#include <coreinit/dynload.h> #include <coreinit/dynload.h>
#include <coreinit/debug.h> #include <coreinit/debug.h>
@ -34,6 +34,7 @@ extern "C" void SC_0x25_KernelCopyData(unsigned int addr, unsigned int src, unsi
void __attribute__ ((noinline)) kern_write(void *addr, uint32_t value); void __attribute__ ((noinline)) kern_write(void *addr, uint32_t value);
static void InstallPatches(); static void InstallPatches();
static unsigned int load_elf_image(const uint8_t *elfstart) { static unsigned int load_elf_image(const uint8_t *elfstart) {
@ -73,7 +74,7 @@ static unsigned int load_elf_image(const uint8_t *elfstart) {
} }
//! clear BSS //! clear BSS
Elf32_Shdr *shdr = (Elf32_Shdr *) (elfstart + ehdr->e_shoff); auto *shdr = (Elf32_Shdr *) (elfstart + ehdr->e_shoff);
for (i = 0; i < ehdr->e_shnum; i++) { for (i = 0; i < ehdr->e_shnum; i++) {
const char *section_name = ((const char *) elfstart) + shdr[ehdr->e_shstrndx].sh_offset + shdr[i].sh_name; const char *section_name = ((const char *) elfstart) + shdr[ehdr->e_shstrndx].sh_offset + shdr[i].sh_name;
if (section_name[0] == '.' && section_name[1] == 'b' && section_name[2] == 's' && section_name[3] == 's') { if (section_name[0] == '.' && section_name[1] == 'b' && section_name[2] == 's' && section_name[3] == 's') {
@ -92,8 +93,8 @@ void KernelWriteU32(uint32_t addr, uint32_t value) {
ICInvalidateRange(&value, 4); ICInvalidateRange(&value, 4);
DCFlushRange(&value, 4); DCFlushRange(&value, 4);
uint32_t dst = (uint32_t) OSEffectiveToPhysical((uint32_t) addr); auto dst = (uint32_t) OSEffectiveToPhysical((uint32_t) addr);
uint32_t src = (uint32_t) OSEffectiveToPhysical((uint32_t) &value); auto src = (uint32_t) OSEffectiveToPhysical((uint32_t) &value);
SC_0x25_KernelCopyData(dst, src, 4); SC_0x25_KernelCopyData(dst, src, 4);
@ -115,10 +116,10 @@ void InstallHBL() {
kern_write((void *) (KERN_SYSCALL_TBL_5 + (0x36 * 4)), (unsigned int) KernelPatches); kern_write((void *) (KERN_SYSCALL_TBL_5 + (0x36 * 4)), (unsigned int) KernelPatches);
Syscall_0x36(); Syscall_0x36();
InstallPatches(); InstallPatches();
unsigned char *pElfBuffer = (unsigned char *) sd_loader_elf; // use this address as temporary to load the elf auto *pElfBuffer = (unsigned char *) sd_loader_elf; // use this address as temporary to load the elf
unsigned int mainEntryPoint = load_elf_image(pElfBuffer); unsigned int mainEntryPoint = load_elf_image(pElfBuffer);
if (mainEntryPoint == 0) { if (mainEntryPoint == 0) {
@ -144,16 +145,16 @@ static void InstallPatches() {
unsigned int bufferU32; unsigned int bufferU32;
/* Pre-setup a few options to defined values */ /* Pre-setup a few options to defined values */
bufferU32 = 550; bufferU32 = 550;
memcpy((void*)&OS_FIRMWARE, &bufferU32, sizeof(bufferU32)); memcpy((void *) &OS_FIRMWARE, &bufferU32, sizeof(bufferU32));
bufferU32 = 0xDEADC0DE; bufferU32 = 0xDEADC0DE;
memcpy((void*)&MAIN_ENTRY_ADDR, &bufferU32, sizeof(bufferU32)); memcpy((void *) &MAIN_ENTRY_ADDR, &bufferU32, sizeof(bufferU32));
memcpy((void*)&ELF_DATA_ADDR, &bufferU32, sizeof(bufferU32)); memcpy((void *) &ELF_DATA_ADDR, &bufferU32, sizeof(bufferU32));
bufferU32 = 0; bufferU32 = 0;
memcpy((void*)&ELF_DATA_SIZE, &bufferU32, sizeof(bufferU32)); memcpy((void *) &ELF_DATA_SIZE, &bufferU32, sizeof(bufferU32));
memcpy((void*)&HBL_CHANNEL, &bufferU32, sizeof(bufferU32)); memcpy((void *) &HBL_CHANNEL, &bufferU32, sizeof(bufferU32));
osSpecificFunctions.addr_OSDynLoad_Acquire = (unsigned int)OSDynLoad_Acquire; osSpecificFunctions.addr_OSDynLoad_Acquire = (unsigned int) OSDynLoad_Acquire;
osSpecificFunctions.addr_OSDynLoad_FindExport = (unsigned int)OSDynLoad_FindExport; osSpecificFunctions.addr_OSDynLoad_FindExport = (unsigned int) OSDynLoad_FindExport;
osSpecificFunctions.addr_KernSyscallTbl1 = KERN_SYSCALL_TBL_1; osSpecificFunctions.addr_KernSyscallTbl1 = KERN_SYSCALL_TBL_1;
osSpecificFunctions.addr_KernSyscallTbl2 = KERN_SYSCALL_TBL_2; osSpecificFunctions.addr_KernSyscallTbl2 = KERN_SYSCALL_TBL_2;
@ -167,32 +168,32 @@ static void InstallPatches() {
osSpecificFunctions.addr_PrepareTitle_hook = address_PrepareTitle_hook; osSpecificFunctions.addr_PrepareTitle_hook = address_PrepareTitle_hook;
osSpecificFunctions.addr_sgIsLoadingBuffer = address_sgIsLoadingBuffer; osSpecificFunctions.addr_sgIsLoadingBuffer = address_sgIsLoadingBuffer;
osSpecificFunctions.addr_gDynloadInitialized = address_gDynloadInitialized; osSpecificFunctions.addr_gDynloadInitialized = address_gDynloadInitialized;
osSpecificFunctions.orig_LiWaitOneChunkInstr = *(unsigned int*)address_LiWaitOneChunk; osSpecificFunctions.orig_LiWaitOneChunkInstr = *(unsigned int *) address_LiWaitOneChunk;
//! pointer to main entry point of a title //! pointer to main entry point of a title
osSpecificFunctions.addr_OSTitle_main_entry = ADDRESS_OSTitle_main_entry_ptr; osSpecificFunctions.addr_OSTitle_main_entry = ADDRESS_OSTitle_main_entry_ptr;
memcpy((void*)OS_SPECIFICS, &osSpecificFunctions, sizeof(OsSpecifics)); memcpy((void *) OS_SPECIFICS, &osSpecificFunctions, sizeof(OsSpecifics));
} }
/* Write a 32-bit word with kernel permissions */ /* Write a 32-bit word with kernel permissions */
void __attribute__ ((noinline)) kern_write(void *addr, uint32_t value) { void __attribute__ ((noinline)) kern_write(void *addr, uint32_t value) {
asm volatile ( asm volatile (
"li 3,1\n" "li 3,1\n"
"li 4,0\n" "li 4,0\n"
"mr 5,%1\n" "mr 5,%1\n"
"li 6,0\n" "li 6,0\n"
"li 7,0\n" "li 7,0\n"
"lis 8,1\n" "lis 8,1\n"
"mr 9,%0\n" "mr 9,%0\n"
"mr %1,1\n" "mr %1,1\n"
"li 0,0x3500\n" "li 0,0x3500\n"
"sc\n" "sc\n"
"nop\n" "nop\n"
"mr 1,%1\n" "mr 1,%1\n"
: :
: "r"(addr), "r"(value) : "r"(addr), "r"(value)
: "memory", "ctr", "lr", "0", "3", "4", "5", "6", "7", "8", "9", "10", : "memory", "ctr", "lr", "0", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12" "11", "12"
); );
} }

View File

@ -1,4 +1,4 @@
#include <stdint.h> #include <cstdint>
#include <malloc.h> #include <malloc.h>
#include <cstring> #include <cstring>
#include <proc_ui/procui.h> #include <proc_ui/procui.h>
@ -6,12 +6,10 @@
#include <sysapp/title.h> #include <sysapp/title.h>
#include <coreinit/mcp.h> #include <coreinit/mcp.h>
#include <coreinit/debug.h> #include <coreinit/debug.h>
#include <nn/acp/title.h> #include <nn/acp.h>
#include <nn/sl/common.h> #include <nn/spm.h>
#include <nn/sl/FileStream.h> #include <nn/sl.h>
#include <nn/sl/LaunchInfoDatabase.h>
#include <nn/ccr/sys_caffeine.h> #include <nn/ccr/sys_caffeine.h>
#include <coreinit/dynload.h> #include <coreinit/dynload.h>
#include <nn/act/client_cpp.h> #include <nn/act/client_cpp.h>
@ -28,7 +26,7 @@ bool getQuickBoot() {
if (bootCheck == 0) { if (bootCheck == 0) {
nn::sl::Initialize(MEMAllocFromDefaultHeapEx, MEMFreeToDefaultHeap); nn::sl::Initialize(MEMAllocFromDefaultHeapEx, MEMFreeToDefaultHeap);
char path[0x80]; char path[0x80];
nn::sl::GetDefaultDatabasePath(path, 0x80, 0x00050010, 0x10066000); nn::sl::GetDefaultDatabasePath(path, 0x80, 0x0005001010066000L); // ECO process
FSCmdBlock cmdBlock; FSCmdBlock cmdBlock;
FSInitCmdBlock(&cmdBlock); FSInitCmdBlock(&cmdBlock);
@ -41,7 +39,7 @@ bool getQuickBoot() {
auto database = new nn::sl::LaunchInfoDatabase; auto database = new nn::sl::LaunchInfoDatabase;
database->Load(fileStream, nn::sl::REGION_EUR); database->Load(fileStream, nn::sl::REGION_EUR);
CCRAppLaunchParam data; // load sys caffeine data CCRAppLaunchParam data; // load sys caffeine data
// load app launch param // load app launch param
CCRSysCaffeineGetAppLaunchParam(&data); CCRSysCaffeineGetAppLaunchParam(&data);
@ -56,47 +54,47 @@ bool getQuickBoot() {
FSDelClient(fsClient, FS_ERROR_FLAG_NONE); FSDelClient(fsClient, FS_ERROR_FLAG_NONE);
nn::sl::Finalize(); nn::sl::Finalize();
if(!result.IsSuccess()) { if (!result.IsSuccess()) {
DEBUG_FUNCTION_LINE("GetLaunchInfoById failed."); DEBUG_FUNCTION_LINE("GetLaunchInfoById failed.");
return false; return false;
} }
if( info.titleId == 0x0005001010040000L || if (info.titleId == 0x0005001010040000L ||
info.titleId == 0x0005001010040100L || info.titleId == 0x0005001010040100L ||
info.titleId == 0x0005001010040200L) { info.titleId == 0x0005001010040200L) {
DEBUG_FUNCTION_LINE("Skip quick booting into the Wii U Menu"); DEBUG_FUNCTION_LINE("Skip quick booting into the Wii U Menu");
return false; return false;
} }
if(!SYSCheckTitleExists(info.titleId)) { if (!SYSCheckTitleExists(info.titleId)) {
DEBUG_FUNCTION_LINE("Title %016llX doesn't exist", info.titleId); DEBUG_FUNCTION_LINE("Title %016llX doesn't exist", info.titleId);
return false; return false;
} }
MCPTitleListType titleInfo; MCPTitleListType titleInfo;
int32_t handle = MCP_Open(); int32_t handle = MCP_Open();
auto err = MCP_GetTitleInfo(handle, info.titleId, &titleInfo); auto err = MCP_GetTitleInfo(handle, info.titleId, &titleInfo);
MCP_Close(handle); MCP_Close(handle);
if(err == 0) { if (err == 0) {
nn::act::Initialize(); nn::act::Initialize();
for(int i = 0; i < 13; i++){ for (int i = 0; i < 13; i++) {
char uuid[16]; char uuid[16];
auto result = nn::act::GetUuidEx(uuid, i); result = nn::act::GetUuidEx(uuid, i);
if(result.IsSuccess()){ if (result.IsSuccess()) {
if( memcmp(uuid, data.uuid, 8) == 0) { if (memcmp(uuid, data.uuid, 8) == 0) {
DEBUG_FUNCTION_LINE("Load Console account %d", i); DEBUG_FUNCTION_LINE("Load Console account %d", i);
nn::act::LoadConsoleAccount(i, 0, 0, 0); nn::act::LoadConsoleAccount(i, 0, 0, 0);
break; break;
} }
} }
} }
nn::act::Finalize(); nn::act::Finalize();
ACPAssignTitlePatch(&titleInfo); ACPAssignTitlePatch(&titleInfo);
_SYSLaunchTitleWithStdArgsInNoSplash(info.titleId, nullptr); _SYSLaunchTitleWithStdArgsInNoSplash(info.titleId, nullptr);
return true; return true;
} }
return false; return false;
} else { } else {
DEBUG_FUNCTION_LINE("No quick boot"); DEBUG_FUNCTION_LINE("No quick boot");
@ -104,92 +102,73 @@ bool getQuickBoot() {
return false; return false;
} }
static void initExternalStorage() {
nn::spm::Initialize();
struct WUT_PACKED VolumeInfo
{
WUT_UNKNOWN_BYTES(0xAC);
char volumeId[16];
WUT_UNKNOWN_BYTES(0x100);
};
WUT_CHECK_OFFSET(VolumeInfo, 0xAC, volumeId);
WUT_CHECK_SIZE(VolumeInfo, 444);
extern "C" uint32_t FSGetVolumeInfo(FSClient*, FSCmdBlock*, const char* path, VolumeInfo* data, FSErrorFlag errorMask);
extern "C" void Initialize__Q2_2nn3spmFv();
extern "C" void SetAutoFatal__Q2_2nn3spmFb(bool);
extern "C" void SetExtendedStorage__Q2_2nn3spmFQ3_2nn3spm12StorageIndex(uint64_t*);
extern "C" void SetDefaultExtendedStorageVolumeId__Q2_2nn3spmFRCQ3_2nn3spm8VolumeId(char *);
extern "C" uint32_t FindStorageByVolumeId__Q2_2nn3spmFPQ3_2nn3spm12StorageIndexRCQ3_2nn3spm8VolumeId(uint64_t *, char *);
static void initExternalStorage(void) {
Initialize__Q2_2nn3spmFv();
FSCmdBlock cmdBlock; FSCmdBlock cmdBlock;
FSInitCmdBlock(&cmdBlock); FSInitCmdBlock(&cmdBlock);
VolumeInfo volumeInfo; FSVolumeInfo volumeInfo;
auto *fsClient = (FSClient *) memalign(0x40, sizeof(FSClient)); auto *fsClient = (FSClient *) memalign(0x40, sizeof(FSClient));
memset(fsClient, 0, sizeof(*fsClient)); memset(fsClient, 0, sizeof(*fsClient));
FSAddClient(fsClient, FS_ERROR_FLAG_NONE); FSAddClient(fsClient, FS_ERROR_FLAG_NONE);
char volumePaths[][19] = char volumePaths[][19] = {"/vol/storage_usb01",
{ "/vol/storage_usb01", "/vol/storage_usb02",
"/vol/storage_usb02", "/vol/storage_usb03",
"/vol/storage_usb03",
}; };
bool found = false; bool found = false;
for(auto path : volumePaths) { for (auto path: volumePaths) {
DEBUG_FUNCTION_LINE("Check if %s is connected", path); DEBUG_FUNCTION_LINE("Check if %s is connected", path);
if(FSGetVolumeInfo(fsClient, &cmdBlock, path, &volumeInfo, FS_ERROR_FLAG_ALL) == 0){ if (FSGetVolumeInfo(fsClient, &cmdBlock, path, &volumeInfo, FS_ERROR_FLAG_ALL) == 0) {
DEBUG_FUNCTION_LINE("Set DefaultExtendedStorage to %s", volumeInfo.volumeId); nn::spm::StorageIndex storageIndex = 0;
SetDefaultExtendedStorageVolumeId__Q2_2nn3spmFRCQ3_2nn3spm8VolumeId(volumeInfo.volumeId); if (nn::spm::FindStorageByVolumeId(&storageIndex, (nn::spm::VolumeId *) volumeInfo.volumeId)) {
uint64_t storageIndex = 0; DEBUG_FUNCTION_LINE("Set DefaultExtendedStorage to %s", volumeInfo.volumeId);
FindStorageByVolumeId__Q2_2nn3spmFPQ3_2nn3spm12StorageIndexRCQ3_2nn3spm8VolumeId(&storageIndex, volumeInfo.volumeId); nn::spm::SetDefaultExtendedStorageVolumeId((nn::spm::VolumeId *) volumeInfo.volumeId);
SetExtendedStorage__Q2_2nn3spmFQ3_2nn3spm12StorageIndex(&storageIndex); nn::spm::SetExtendedStorage(&storageIndex);
found = true; ACPMountExternalStorage();
break; found = true;
break;
} else {
DEBUG_FUNCTION_LINE("Failed to find Storage by VolumeId(%s)", volumeInfo.volumeId);
}
} }
} }
if (!found) { if (!found) {
DEBUG_FUNCTION_LINE("Fallback to empty ExtendedStorage"); DEBUG_FUNCTION_LINE("Fallback to empty ExtendedStorage");
char empty[16]; nn::spm::VolumeId empty{};
empty[0] = '\0'; nn::spm::SetDefaultExtendedStorageVolumeId(&empty);
SetDefaultExtendedStorageVolumeId__Q2_2nn3spmFRCQ3_2nn3spm8VolumeId(empty);
nn::spm::StorageIndex storageIndex = 0;
uint64_t storageIndex = 0; nn::spm::SetExtendedStorage(&storageIndex);
SetExtendedStorage__Q2_2nn3spmFQ3_2nn3spm12StorageIndex(&storageIndex);
} }
FSDelClient(fsClient, FS_ERROR_FLAG_ALL); FSDelClient(fsClient, FS_ERROR_FLAG_ALL);
nn::spm::Finalize();
} }
void bootHomebrewLauncher() {
void bootHomebrewLauncher(void) {
uint64_t titleId = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_MII_MAKER); uint64_t titleId = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_MII_MAKER);
_SYSLaunchTitleWithStdArgsInNoSplash(titleId, nullptr); _SYSLaunchTitleWithStdArgsInNoSplash(titleId, nullptr);
} }
extern "C" void _SYSLaunchMenuWithCheckingAccount(nn::act::SlotNo slot);
int main(int argc, char **argv) { int main(int argc, char **argv) {
if(!WHBLogModuleInit()){ if (!WHBLogModuleInit()) {
WHBLogCafeInit(); WHBLogCafeInit();
WHBLogUdpInit(); WHBLogUdpInit();
} }
initExternalStorage(); initExternalStorage();
InstallHBL(); InstallHBL();
if(getQuickBoot()) { if (getQuickBoot()) {
return 0; return 0;
} }
nn::act::Initialize(); nn::act::Initialize();
nn::act::SlotNo slot = nn::act::GetSlotNo(); nn::act::SlotNo slot = nn::act::GetSlotNo();
nn::act::SlotNo defaultSlot = nn::act::GetDefaultAccount(); nn::act::SlotNo defaultSlot = nn::act::GetDefaultAccount();