From 27418f035cb5cafff487141b4469968284e9975e Mon Sep 17 00:00:00 2001 From: Maschell Date: Mon, 27 Dec 2021 23:31:54 +0100 Subject: [PATCH] Fix USB usage --- source/hbl_install.cpp | 65 +++++++++---------- source/main.cpp | 137 +++++++++++++++++------------------------ 2 files changed, 91 insertions(+), 111 deletions(-) diff --git a/source/hbl_install.cpp b/source/hbl_install.cpp index 1facf6f..3ce054d 100644 --- a/source/hbl_install.cpp +++ b/source/hbl_install.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include #include @@ -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); + static void InstallPatches(); 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 - 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++) { 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') { @@ -92,8 +93,8 @@ void KernelWriteU32(uint32_t addr, uint32_t value) { ICInvalidateRange(&value, 4); DCFlushRange(&value, 4); - uint32_t dst = (uint32_t) OSEffectiveToPhysical((uint32_t) addr); - uint32_t src = (uint32_t) OSEffectiveToPhysical((uint32_t) &value); + auto dst = (uint32_t) OSEffectiveToPhysical((uint32_t) addr); + auto src = (uint32_t) OSEffectiveToPhysical((uint32_t) &value); SC_0x25_KernelCopyData(dst, src, 4); @@ -115,10 +116,10 @@ void InstallHBL() { kern_write((void *) (KERN_SYSCALL_TBL_5 + (0x36 * 4)), (unsigned int) KernelPatches); Syscall_0x36(); - + 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); if (mainEntryPoint == 0) { @@ -144,16 +145,16 @@ static void InstallPatches() { unsigned int bufferU32; /* Pre-setup a few options to defined values */ bufferU32 = 550; - memcpy((void*)&OS_FIRMWARE, &bufferU32, sizeof(bufferU32)); + memcpy((void *) &OS_FIRMWARE, &bufferU32, sizeof(bufferU32)); bufferU32 = 0xDEADC0DE; - memcpy((void*)&MAIN_ENTRY_ADDR, &bufferU32, sizeof(bufferU32)); - memcpy((void*)&ELF_DATA_ADDR, &bufferU32, sizeof(bufferU32)); + memcpy((void *) &MAIN_ENTRY_ADDR, &bufferU32, sizeof(bufferU32)); + memcpy((void *) &ELF_DATA_ADDR, &bufferU32, sizeof(bufferU32)); bufferU32 = 0; - memcpy((void*)&ELF_DATA_SIZE, &bufferU32, sizeof(bufferU32)); - memcpy((void*)&HBL_CHANNEL, &bufferU32, sizeof(bufferU32)); + memcpy((void *) &ELF_DATA_SIZE, &bufferU32, sizeof(bufferU32)); + memcpy((void *) &HBL_CHANNEL, &bufferU32, sizeof(bufferU32)); - osSpecificFunctions.addr_OSDynLoad_Acquire = (unsigned int)OSDynLoad_Acquire; - osSpecificFunctions.addr_OSDynLoad_FindExport = (unsigned int)OSDynLoad_FindExport; + osSpecificFunctions.addr_OSDynLoad_Acquire = (unsigned int) OSDynLoad_Acquire; + osSpecificFunctions.addr_OSDynLoad_FindExport = (unsigned int) OSDynLoad_FindExport; osSpecificFunctions.addr_KernSyscallTbl1 = KERN_SYSCALL_TBL_1; osSpecificFunctions.addr_KernSyscallTbl2 = KERN_SYSCALL_TBL_2; @@ -167,32 +168,32 @@ static void InstallPatches() { osSpecificFunctions.addr_PrepareTitle_hook = address_PrepareTitle_hook; osSpecificFunctions.addr_sgIsLoadingBuffer = address_sgIsLoadingBuffer; 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 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 */ void __attribute__ ((noinline)) kern_write(void *addr, uint32_t value) { asm volatile ( - "li 3,1\n" - "li 4,0\n" - "mr 5,%1\n" - "li 6,0\n" - "li 7,0\n" - "lis 8,1\n" - "mr 9,%0\n" - "mr %1,1\n" - "li 0,0x3500\n" - "sc\n" - "nop\n" - "mr 1,%1\n" - : - : "r"(addr), "r"(value) - : "memory", "ctr", "lr", "0", "3", "4", "5", "6", "7", "8", "9", "10", - "11", "12" + "li 3,1\n" + "li 4,0\n" + "mr 5,%1\n" + "li 6,0\n" + "li 7,0\n" + "lis 8,1\n" + "mr 9,%0\n" + "mr %1,1\n" + "li 0,0x3500\n" + "sc\n" + "nop\n" + "mr 1,%1\n" + : + : "r"(addr), "r"(value) + : "memory", "ctr", "lr", "0", "3", "4", "5", "6", "7", "8", "9", "10", + "11", "12" ); } diff --git a/source/main.cpp b/source/main.cpp index d974996..4c8d911 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -6,12 +6,10 @@ #include #include #include -#include -#include -#include -#include +#include +#include +#include #include - #include #include @@ -28,7 +26,7 @@ bool getQuickBoot() { if (bootCheck == 0) { nn::sl::Initialize(MEMAllocFromDefaultHeapEx, MEMFreeToDefaultHeap); char path[0x80]; - nn::sl::GetDefaultDatabasePath(path, 0x80, 0x00050010, 0x10066000); + nn::sl::GetDefaultDatabasePath(path, 0x80, 0x0005001010066000L); // ECO process FSCmdBlock cmdBlock; FSInitCmdBlock(&cmdBlock); @@ -41,7 +39,7 @@ bool getQuickBoot() { 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); @@ -56,47 +54,47 @@ bool getQuickBoot() { FSDelClient(fsClient, FS_ERROR_FLAG_NONE); nn::sl::Finalize(); - - if(!result.IsSuccess()) { + + if (!result.IsSuccess()) { DEBUG_FUNCTION_LINE("GetLaunchInfoById failed."); return false; } - if( info.titleId == 0x0005001010040000L || + 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)) { + 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(); - for(int i = 0; i < 13; i++){ + if (err == 0) { + nn::act::Initialize(); + for (int i = 0; i < 13; i++) { char uuid[16]; - auto result = nn::act::GetUuidEx(uuid, i); - if(result.IsSuccess()){ - if( memcmp(uuid, data.uuid, 8) == 0) { + 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); + ACPAssignTitlePatch(&titleInfo); _SYSLaunchTitleWithStdArgsInNoSplash(info.titleId, nullptr); return true; } - + return false; } else { DEBUG_FUNCTION_LINE("No quick boot"); @@ -104,92 +102,73 @@ bool getQuickBoot() { 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; FSInitCmdBlock(&cmdBlock); - - VolumeInfo volumeInfo; + + FSVolumeInfo volumeInfo; auto *fsClient = (FSClient *) memalign(0x40, sizeof(FSClient)); memset(fsClient, 0, sizeof(*fsClient)); FSAddClient(fsClient, FS_ERROR_FLAG_NONE); - - char volumePaths[][19] = - { "/vol/storage_usb01", - "/vol/storage_usb02", - "/vol/storage_usb03", + + char volumePaths[][19] = {"/vol/storage_usb01", + "/vol/storage_usb02", + "/vol/storage_usb03", }; - + bool found = false; - for(auto path : volumePaths) { + for (auto path: volumePaths) { DEBUG_FUNCTION_LINE("Check if %s is connected", path); - if(FSGetVolumeInfo(fsClient, &cmdBlock, path, &volumeInfo, FS_ERROR_FLAG_ALL) == 0){ - DEBUG_FUNCTION_LINE("Set DefaultExtendedStorage to %s", volumeInfo.volumeId); - SetDefaultExtendedStorageVolumeId__Q2_2nn3spmFRCQ3_2nn3spm8VolumeId(volumeInfo.volumeId); - uint64_t storageIndex = 0; - FindStorageByVolumeId__Q2_2nn3spmFPQ3_2nn3spm12StorageIndexRCQ3_2nn3spm8VolumeId(&storageIndex, volumeInfo.volumeId); - SetExtendedStorage__Q2_2nn3spmFQ3_2nn3spm12StorageIndex(&storageIndex); - found = true; - break; + 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); + } } } if (!found) { DEBUG_FUNCTION_LINE("Fallback to empty ExtendedStorage"); - char empty[16]; - empty[0] = '\0'; - SetDefaultExtendedStorageVolumeId__Q2_2nn3spmFRCQ3_2nn3spm8VolumeId(empty); - - uint64_t storageIndex = 0; - SetExtendedStorage__Q2_2nn3spmFQ3_2nn3spm12StorageIndex(&storageIndex); + nn::spm::VolumeId empty{}; + nn::spm::SetDefaultExtendedStorageVolumeId(&empty); + + nn::spm::StorageIndex storageIndex = 0; + nn::spm::SetExtendedStorage(&storageIndex); } - - FSDelClient(fsClient, FS_ERROR_FLAG_ALL); + + FSDelClient(fsClient, FS_ERROR_FLAG_ALL); + + nn::spm::Finalize(); } - -void bootHomebrewLauncher(void) { +void bootHomebrewLauncher() { uint64_t titleId = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_MII_MAKER); _SYSLaunchTitleWithStdArgsInNoSplash(titleId, nullptr); } - -extern "C" void _SYSLaunchMenuWithCheckingAccount(nn::act::SlotNo slot); - int main(int argc, char **argv) { - if(!WHBLogModuleInit()){ + if (!WHBLogModuleInit()) { WHBLogCafeInit(); WHBLogUdpInit(); } - + initExternalStorage(); - + InstallHBL(); - - if(getQuickBoot()) { + + if (getQuickBoot()) { return 0; } - + nn::act::Initialize(); nn::act::SlotNo slot = nn::act::GetSlotNo(); nn::act::SlotNo defaultSlot = nn::act::GetDefaultAccount();