2020-12-07 04:26:59 +00:00
|
|
|
/*
|
|
|
|
* ums.c
|
|
|
|
*
|
2023-04-08 12:42:22 +01:00
|
|
|
* Copyright (c) 2020-2023, 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;
|
2023-02-24 20:32:58 +00:00
|
|
|
static bool g_umsInterfaceInit = false, g_umsDeviceInfoUpdated = false;
|
2020-12-07 04:26:59 +00:00
|
|
|
|
|
|
|
static u32 g_umsDeviceCount = 0;
|
|
|
|
static UsbHsFsDevice *g_umsDevices = NULL;
|
|
|
|
|
|
|
|
/* Function prototypes. */
|
|
|
|
|
|
|
|
static void umsFreeDeviceData(void);
|
2023-02-24 20:32:58 +00:00
|
|
|
static void umsPopulateCallback(const UsbHsFsDevice *devices, u32 device_count, void *user_data);
|
|
|
|
static bool umsDuplicateDeviceArray(const UsbHsFsDevice *in_devices, u32 in_device_count, UsbHsFsDevice **out_devices, u32 *out_device_count);
|
2020-12-07 04:26:59 +00:00
|
|
|
|
|
|
|
bool umsInitialize(void)
|
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
bool ret = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
SCOPED_LOCK(&g_umsMutex)
|
2020-12-07 04:26:59 +00:00
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
ret = g_umsInterfaceInit;
|
|
|
|
if (ret) break;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
/* Set populate callback function. */
|
|
|
|
usbHsFsSetPopulateCallback(&umsPopulateCallback, NULL);
|
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
/* Initialize USB Mass Storage Host interface. */
|
|
|
|
Result rc = usbHsFsInitialize(0);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("usbHsFsInitialize failed! (0x%X).", rc);
|
2021-05-18 13:32:43 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
/* Update flags. */
|
|
|
|
ret = g_umsInterfaceInit = true;
|
2020-12-07 04:26:59 +00:00
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2020-12-07 04:26:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void umsExit(void)
|
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
SCOPED_LOCK(&g_umsMutex)
|
2020-12-07 04:26:59 +00:00
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
/* Close USB Mass Storage Host interface. */
|
|
|
|
usbHsFsExit();
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
/* Free USB Mass Storage device data. */
|
|
|
|
umsFreeDeviceData();
|
|
|
|
|
|
|
|
/* Update flags. */
|
|
|
|
g_umsInterfaceInit = g_umsDeviceInfoUpdated = false;
|
2020-12-07 04:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool umsIsDeviceInfoUpdated(void)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-09 05:48:17 +01:00
|
|
|
SCOPED_TRY_LOCK(&g_umsMutex)
|
2020-12-07 04:26:59 +00:00
|
|
|
{
|
2023-02-24 20:32:58 +00:00
|
|
|
ret = (g_umsInterfaceInit && g_umsDeviceInfoUpdated);
|
2020-12-07 04:26:59 +00:00
|
|
|
g_umsDeviceInfoUpdated = false;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2020-12-07 04:26:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-12-23 18:43:12 +00:00
|
|
|
UsbHsFsDevice *umsGetDevices(u32 *out_count)
|
2020-12-07 04:26:59 +00:00
|
|
|
{
|
2020-12-23 18:43:12 +00:00
|
|
|
UsbHsFsDevice *devices = NULL;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-18 13:32:43 +01:00
|
|
|
SCOPED_LOCK(&g_umsMutex)
|
2020-12-23 18:43:12 +00:00
|
|
|
{
|
2021-05-18 13:32:43 +01:00
|
|
|
if (!g_umsInterfaceInit || !out_count)
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Invalid parameters!");
|
2021-05-18 13:32:43 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
/* Duplicate device data. */
|
|
|
|
if (!umsDuplicateDeviceArray(g_umsDevices, g_umsDeviceCount, &devices, out_count)) LOG_MSG_ERROR("Failed to duplicate USB Mass Storage device data!");
|
2020-12-23 18:43:12 +00:00
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2020-12-23 18:43:12 +00:00
|
|
|
return devices;
|
2020-12-07 04:26:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
static void umsFreeDeviceData(void)
|
2020-12-07 04:26:59 +00:00
|
|
|
{
|
2023-02-24 20:32:58 +00:00
|
|
|
/* Free devices buffer. */
|
|
|
|
if (g_umsDevices)
|
2020-12-07 04:26:59 +00:00
|
|
|
{
|
2023-02-24 20:32:58 +00:00
|
|
|
free(g_umsDevices);
|
|
|
|
g_umsDevices = NULL;
|
2020-12-07 04:26:59 +00:00
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
/* Reset device count. */
|
|
|
|
g_umsDeviceCount = 0;
|
2020-12-07 04:26:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
static void umsPopulateCallback(const UsbHsFsDevice *devices, u32 device_count, void *user_data)
|
2020-12-07 04:26:59 +00:00
|
|
|
{
|
2023-02-24 20:32:58 +00:00
|
|
|
(void)user_data;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
SCOPED_LOCK(&g_umsMutex)
|
|
|
|
{
|
|
|
|
/* Free USB Mass Storage device data. */
|
|
|
|
umsFreeDeviceData();
|
|
|
|
|
|
|
|
LOG_MSG_INFO("Mounted USB Mass Storage device count: %u.", device_count);
|
|
|
|
|
|
|
|
if (devices && device_count)
|
|
|
|
{
|
|
|
|
/* Duplicate device data. */
|
|
|
|
if (!umsDuplicateDeviceArray(devices, device_count, &g_umsDevices, &g_umsDeviceCount)) LOG_MSG_ERROR("Failed to duplicate USB Mass Storage device data!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update USB Mass Storage device info updated flag. */
|
|
|
|
g_umsDeviceInfoUpdated = true;
|
|
|
|
}
|
2020-12-07 04:26:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
static bool umsDuplicateDeviceArray(const UsbHsFsDevice *in_devices, u32 in_device_count, UsbHsFsDevice **out_devices, u32 *out_device_count)
|
2020-12-07 04:26:59 +00:00
|
|
|
{
|
2023-02-24 20:32:58 +00:00
|
|
|
if (!out_devices || !out_device_count)
|
|
|
|
{
|
|
|
|
LOG_MSG_ERROR("Invalid parameters!");
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
UsbHsFsDevice *tmp_devices = NULL;
|
|
|
|
bool ret = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
/* Clear output. */
|
|
|
|
*out_devices = NULL;
|
|
|
|
*out_device_count = 0;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
/* Short-circuit. */
|
|
|
|
if (!in_devices || !in_device_count)
|
2020-12-07 04:26:59 +00:00
|
|
|
{
|
2023-02-24 20:32:58 +00:00
|
|
|
ret = true;
|
|
|
|
goto end;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
/* Duplicate input array. */
|
|
|
|
tmp_devices = calloc(in_device_count, sizeof(UsbHsFsDevice));
|
|
|
|
if (!tmp_devices)
|
|
|
|
{
|
|
|
|
LOG_MSG_ERROR("Failed to allocate memory for %u devices!", in_device_count);
|
|
|
|
goto end;
|
2020-12-07 04:26:59 +00:00
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
/* Copy device data. */
|
|
|
|
memcpy(tmp_devices, in_devices, in_device_count * sizeof(UsbHsFsDevice));
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
/* Update output. */
|
|
|
|
*out_devices = tmp_devices;
|
|
|
|
*out_device_count = in_device_count;
|
2020-12-07 04:26:59 +00:00
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
/* Update return value. */
|
|
|
|
ret = true;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-02-24 20:32:58 +00:00
|
|
|
end:
|
|
|
|
return ret;
|
2020-12-07 04:26:59 +00:00
|
|
|
}
|