2020-12-07 04:26:59 +00:00
|
|
|
/*
|
|
|
|
* ums.c
|
|
|
|
*
|
2020-12-23 17:48:57 +00:00
|
|
|
* Copyright (c) 2020-2021, DarkMatterCore <pabloacurielz@gmail.com>.
|
2020-12-07 04:26:59 +00:00
|
|
|
*
|
|
|
|
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
|
|
|
|
*
|
2021-03-25 19:26:58 +00:00
|
|
|
* nxdumptool is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2020-12-07 04:26:59 +00:00
|
|
|
*
|
2021-03-25 19:26:58 +00:00
|
|
|
* nxdumptool is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2020-12-07 04:26:59 +00:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-03-25 19:26:58 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-12-07 04:26:59 +00:00
|
|
|
*/
|
|
|
|
|
2021-03-26 04:35:14 +00:00
|
|
|
#include "nxdt_utils.h"
|
2020-12-07 04:26:59 +00:00
|
|
|
|
|
|
|
/* Global variables. */
|
|
|
|
|
|
|
|
static Mutex g_umsMutex = 0;
|
|
|
|
static bool g_umsInterfaceInit = false;
|
|
|
|
|
|
|
|
static Thread g_umsDetectionThread = {0};
|
|
|
|
static UEvent *g_umsStatusChangeEvent = NULL, g_umsDetectionThreadExitEvent = {0};
|
|
|
|
static bool g_umsDetectionThreadCreated = false, g_umsDeviceInfoUpdated = false;
|
|
|
|
|
|
|
|
static u32 g_umsDeviceCount = 0;
|
|
|
|
static UsbHsFsDevice *g_umsDevices = NULL;
|
|
|
|
|
|
|
|
/* Function prototypes. */
|
|
|
|
|
|
|
|
static bool umsCreateDetectionThread(void);
|
|
|
|
static void umsDestroyDetectionThread(void);
|
|
|
|
static void umsDetectionThreadFunc(void *arg);
|
|
|
|
|
|
|
|
static void umsFreeDeviceData(void);
|
|
|
|
|
|
|
|
bool umsInitialize(void)
|
|
|
|
{
|
|
|
|
mutexLock(&g_umsMutex);
|
|
|
|
|
|
|
|
Result rc = 0;
|
|
|
|
|
|
|
|
bool ret = g_umsInterfaceInit;
|
|
|
|
if (ret) goto end;
|
|
|
|
|
|
|
|
/* Initialize USB Mass Storage Host interface. */
|
|
|
|
rc = usbHsFsInitialize(0);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("usbHsFsInitialize failed! (0x%08X).", rc);
|
2020-12-07 04:26:59 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get USB Mass Storage status change event. */
|
|
|
|
g_umsStatusChangeEvent = usbHsFsGetStatusChangeUserEvent();
|
|
|
|
|
|
|
|
/* Create user-mode exit event. */
|
|
|
|
ueventCreate(&g_umsDetectionThreadExitEvent, true);
|
|
|
|
|
|
|
|
/* Create USB Mass Storage detection thread. */
|
|
|
|
if (!(g_umsDetectionThreadCreated = umsCreateDetectionThread())) goto end;
|
|
|
|
|
|
|
|
ret = g_umsInterfaceInit = true;
|
|
|
|
|
|
|
|
end:
|
|
|
|
mutexUnlock(&g_umsMutex);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void umsExit(void)
|
|
|
|
{
|
|
|
|
mutexLock(&g_umsMutex);
|
|
|
|
|
|
|
|
if (!g_umsInterfaceInit) goto end;
|
|
|
|
|
|
|
|
/* Destroy USB Mass Storage detection thread. */
|
|
|
|
if (g_umsDetectionThreadCreated)
|
|
|
|
{
|
|
|
|
umsDestroyDetectionThread();
|
|
|
|
g_umsDetectionThreadCreated = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close USB Mass Storage Host interface. */
|
|
|
|
usbHsFsExit();
|
|
|
|
|
|
|
|
g_umsInterfaceInit = false;
|
|
|
|
|
|
|
|
end:
|
|
|
|
mutexUnlock(&g_umsMutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool umsIsDeviceInfoUpdated(void)
|
|
|
|
{
|
|
|
|
mutexLock(&g_umsMutex);
|
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
if (g_umsInterfaceInit && g_umsDeviceInfoUpdated)
|
|
|
|
{
|
|
|
|
ret = true;
|
|
|
|
g_umsDeviceInfoUpdated = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutexUnlock(&g_umsMutex);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-12-23 18:43:12 +00:00
|
|
|
UsbHsFsDevice *umsGetDevices(u32 *out_count)
|
2020-12-07 04:26:59 +00:00
|
|
|
{
|
|
|
|
mutexLock(&g_umsMutex);
|
|
|
|
|
2020-12-23 18:43:12 +00:00
|
|
|
UsbHsFsDevice *devices = NULL;
|
2020-12-07 04:26:59 +00:00
|
|
|
|
2020-12-24 08:49:38 +00:00
|
|
|
if (!g_umsInterfaceInit || !out_count)
|
2020-12-07 04:26:59 +00:00
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Invalid parameters!");
|
2020-12-07 04:26:59 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2020-12-24 08:49:38 +00:00
|
|
|
if (g_umsDeviceCount && g_umsDevices)
|
2020-12-23 18:43:12 +00:00
|
|
|
{
|
2020-12-24 08:49:38 +00:00
|
|
|
/* Allocate memory for the output devices. */
|
|
|
|
devices = calloc(g_umsDeviceCount, sizeof(UsbHsFsDevice));
|
|
|
|
if (!devices)
|
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Failed to allocate memory for %u devices!", g_umsDeviceCount);
|
2020-12-24 08:49:38 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy device data. */
|
|
|
|
memcpy(devices, g_umsDevices, g_umsDeviceCount * sizeof(UsbHsFsDevice));
|
2020-12-23 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update output device count. */
|
2020-12-24 08:49:38 +00:00
|
|
|
*out_count = ((g_umsDeviceCount && g_umsDevices) ? g_umsDeviceCount : 0);
|
2020-12-07 04:26:59 +00:00
|
|
|
|
|
|
|
end:
|
|
|
|
mutexUnlock(&g_umsMutex);
|
|
|
|
|
2020-12-23 18:43:12 +00:00
|
|
|
return devices;
|
2020-12-07 04:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool umsCreateDetectionThread(void)
|
|
|
|
{
|
|
|
|
if (!utilsCreateThread(&g_umsDetectionThread, umsDetectionThreadFunc, NULL, 1))
|
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Failed to create USB Mass Storage detection thread!");
|
2020-12-07 04:26:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void umsDestroyDetectionThread(void)
|
|
|
|
{
|
|
|
|
/* Signal the exit event to terminate the USB Mass Storage detection thread. */
|
|
|
|
ueventSignal(&g_umsDetectionThreadExitEvent);
|
|
|
|
|
|
|
|
/* Wait for the USB Mass Storage detection thread to exit. */
|
|
|
|
utilsJoinThread(&g_umsDetectionThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void umsDetectionThreadFunc(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
Result rc = 0;
|
|
|
|
int idx = 0;
|
|
|
|
u32 listed_device_count = 0;
|
|
|
|
|
|
|
|
Waiter status_change_event_waiter = waiterForUEvent(g_umsStatusChangeEvent);
|
|
|
|
Waiter exit_event_waiter = waiterForUEvent(&g_umsDetectionThreadExitEvent);
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
/* Wait until an event is triggered. */
|
|
|
|
rc = waitMulti(&idx, -1, status_change_event_waiter, exit_event_waiter);
|
|
|
|
if (R_FAILED(rc)) continue;
|
|
|
|
|
|
|
|
/* Exit event triggered. */
|
|
|
|
if (idx == 1) break;
|
|
|
|
|
|
|
|
mutexLock(&g_umsMutex);
|
|
|
|
|
|
|
|
/* Free USB Mass Storage device data. */
|
|
|
|
umsFreeDeviceData();
|
|
|
|
|
|
|
|
/* Get mounted device count. */
|
|
|
|
g_umsDeviceCount = usbHsFsGetMountedDeviceCount();
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("USB Mass Storage status change event triggered! Mounted USB Mass Storage device count: %u.", g_umsDeviceCount);
|
2020-12-07 04:26:59 +00:00
|
|
|
|
|
|
|
if (g_umsDeviceCount)
|
|
|
|
{
|
|
|
|
bool fail = false;
|
|
|
|
|
|
|
|
/* Allocate mounted devices buffer. */
|
|
|
|
g_umsDevices = calloc(g_umsDeviceCount, sizeof(UsbHsFsDevice));
|
|
|
|
if (g_umsDevices)
|
|
|
|
{
|
|
|
|
/* List mounted devices. */
|
2020-12-24 08:49:38 +00:00
|
|
|
listed_device_count = usbHsFsListMountedDevices(g_umsDevices, g_umsDeviceCount);
|
|
|
|
if (listed_device_count)
|
2020-12-07 04:26:59 +00:00
|
|
|
{
|
|
|
|
/* Check if we got as many devices as we expected. */
|
|
|
|
if (listed_device_count == g_umsDeviceCount)
|
|
|
|
{
|
|
|
|
/* Update USB Mass Storage device info updated flag. */
|
|
|
|
g_umsDeviceInfoUpdated = true;
|
|
|
|
} else {
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("USB Mass Storage device count mismatch! (%u != %u).", listed_device_count, g_umsDeviceCount);
|
2020-12-07 04:26:59 +00:00
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Failed to list mounted USB Mass Storage devices!");
|
2020-12-07 04:26:59 +00:00
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Failed to allocate memory for mounted USB Mass Storage devices buffer!");
|
2020-12-07 04:26:59 +00:00
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free USB Mass Storage device data if something went wrong. */
|
|
|
|
if (fail) umsFreeDeviceData();
|
2020-12-24 08:49:38 +00:00
|
|
|
} else {
|
|
|
|
/* Update USB Mass Storage device info updated flag. */
|
|
|
|
g_umsDeviceInfoUpdated = true;
|
2020-12-07 04:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mutexUnlock(&g_umsMutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free USB Mass Storage device data. */
|
|
|
|
umsFreeDeviceData();
|
|
|
|
|
|
|
|
threadExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void umsFreeDeviceData(void)
|
|
|
|
{
|
|
|
|
/* Free devices buffer. */
|
|
|
|
if (g_umsDevices)
|
|
|
|
{
|
|
|
|
free(g_umsDevices);
|
|
|
|
g_umsDevices = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset device count. */
|
|
|
|
g_umsDeviceCount = 0;
|
|
|
|
}
|