From bfdd89218dd4dce007d5e26da8315d22a88079ac Mon Sep 17 00:00:00 2001 From: Edoardo Lolletti Date: Sat, 27 Apr 2024 23:31:07 +0200 Subject: [PATCH] Properly detect case where deviceList was populated with garbage data Assume the device list is valid if it has a valid device that matches the path of the app --- arm9/src/deviceList.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/arm9/src/deviceList.c b/arm9/src/deviceList.c index 498a2cc..f3ec1e8 100644 --- a/arm9/src/deviceList.c +++ b/arm9/src/deviceList.c @@ -4,6 +4,8 @@ #define DEVICE_LIST_SENTINEL *(vu32*)0x02300020 #define DEVICE_LIST_ADDR (vu8*)0x02300024 +static u8 gotDeviceList = 0; + size_t getDeviceNameLenFromAppName(const char (appname)[0x40]) { for(int i = 0; i < 0x40; ++i) @@ -18,39 +20,40 @@ size_t getDeviceNameLenFromAppName(const char (appname)[0x40]) DeviceList* getDeviceList(void) { - static bool gotDeviceList = false; if(!gotDeviceList) { + gotDeviceList = 1; while(!DEVICE_LIST_SENTINEL); DeviceList* list = (DeviceList*)DEVICE_LIST_ADDR; - + size_t deviceNameLen = getDeviceNameLenFromAppName(list->appname); - + if(deviceNameLen == 0) { return 0; } - bool isSd = false; - + for(int i = 0; i < 11; ++i) { Device* device = &list->devices[i]; if(device->deviceName[deviceNameLen] == '\0' && strncmp(device->deviceName, list->appname, deviceNameLen) == 0) { - isSd = device->phisicalDrive == 0; + gotDeviceList = 2; + bool isSd = device->phisicalDrive == 0; + if(isSd) + { + //transform the root path to sd:/ (can be sdmc:/, nand:/, nand2:/, etc) + memmove(list->appname + 2, list->appname + deviceNameLen, sizeof(list->appname) - deviceNameLen); + list->appname[0] = 's'; + list->appname[1] = 'd'; + memset(list->appname + (sizeof(list->appname) - deviceNameLen + 2), 0, deviceNameLen - 2); + } break; } } - - if(isSd) - { - //transform the root path to sd:/ (can be sdmc:/, nand:/, nand2:/, etc) - memmove(list->appname + 2, list->appname + deviceNameLen, sizeof(list->appname) - deviceNameLen); - list->appname[0] = 's'; - list->appname[1] = 'd'; - memset(list->appname + (sizeof(list->appname) - deviceNameLen + 2), 0, deviceNameLen - 2); - } - gotDeviceList = true; } - + + if(gotDeviceList == 1) + return 0; + return (DeviceList*)DEVICE_LIST_ADDR; } \ No newline at end of file