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);
@ -118,7 +119,7 @@ void InstallHBL() {
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) {

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);
@ -81,7 +79,7 @@ bool getQuickBoot() {
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);
@ -104,39 +102,19 @@ bool getQuickBoot() {
return false; return false;
} }
static void initExternalStorage() {
struct WUT_PACKED VolumeInfo nn::spm::Initialize();
{
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",
}; };
@ -145,37 +123,38 @@ static void initExternalStorage(void) {
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) {
nn::spm::StorageIndex storageIndex = 0;
if (nn::spm::FindStorageByVolumeId(&storageIndex, (nn::spm::VolumeId *) volumeInfo.volumeId)) {
DEBUG_FUNCTION_LINE("Set DefaultExtendedStorage to %s", volumeInfo.volumeId); DEBUG_FUNCTION_LINE("Set DefaultExtendedStorage to %s", volumeInfo.volumeId);
SetDefaultExtendedStorageVolumeId__Q2_2nn3spmFRCQ3_2nn3spm8VolumeId(volumeInfo.volumeId); nn::spm::SetDefaultExtendedStorageVolumeId((nn::spm::VolumeId *) volumeInfo.volumeId);
uint64_t storageIndex = 0; nn::spm::SetExtendedStorage(&storageIndex);
FindStorageByVolumeId__Q2_2nn3spmFPQ3_2nn3spm12StorageIndexRCQ3_2nn3spm8VolumeId(&storageIndex, volumeInfo.volumeId); ACPMountExternalStorage();
SetExtendedStorage__Q2_2nn3spmFQ3_2nn3spm12StorageIndex(&storageIndex);
found = true; found = true;
break; 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);
uint64_t storageIndex = 0; nn::spm::StorageIndex storageIndex = 0;
SetExtendedStorage__Q2_2nn3spmFQ3_2nn3spm12StorageIndex(&storageIndex); nn::spm::SetExtendedStorage(&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();