1
0
Fork 0
mirror of https://github.com/DarkMatterCore/nxdumptool.git synced 2024-11-26 20:22:17 +00:00
nxdumptool/source/core/ums.c

205 lines
5.2 KiB
C
Raw Normal View History

/*
* ums.c
*
2023-04-08 12:42:22 +01:00
* Copyright (c) 2020-2023, DarkMatterCore <pabloacurielz@gmail.com>.
*
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-03-26 04:35:14 +00:00
#include "nxdt_utils.h"
/* Global variables. */
static Mutex g_umsMutex = 0;
static bool g_umsInterfaceInit = false, g_umsDeviceInfoUpdated = false;
static u32 g_umsDeviceCount = 0;
static UsbHsFsDevice *g_umsDevices = NULL;
/* Function prototypes. */
static void umsFreeDeviceData(void);
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);
bool umsInitialize(void)
{
bool ret = false;
SCOPED_LOCK(&g_umsMutex)
{
ret = g_umsInterfaceInit;
if (ret) break;
/* Set populate callback function. */
usbHsFsSetPopulateCallback(&umsPopulateCallback, NULL);
/* Initialize USB Mass Storage Host interface. */
Result rc = usbHsFsInitialize(0);
if (R_FAILED(rc))
{
LOG_MSG_ERROR("usbHsFsInitialize failed! (0x%X).", rc);
break;
}
/* Update flags. */
ret = g_umsInterfaceInit = true;
}
return ret;
}
void umsExit(void)
{
SCOPED_LOCK(&g_umsMutex)
{
/* Close USB Mass Storage Host interface. */
usbHsFsExit();
/* Free USB Mass Storage device data. */
umsFreeDeviceData();
/* Update flags. */
g_umsInterfaceInit = g_umsDeviceInfoUpdated = false;
}
}
bool umsIsDeviceInfoUpdated(void)
{
bool ret = false;
SCOPED_TRY_LOCK(&g_umsMutex)
{
ret = (g_umsInterfaceInit && g_umsDeviceInfoUpdated);
g_umsDeviceInfoUpdated = false;
}
return ret;
}
2020-12-23 18:43:12 +00:00
UsbHsFsDevice *umsGetDevices(u32 *out_count)
{
2020-12-23 18:43:12 +00:00
UsbHsFsDevice *devices = NULL;
SCOPED_LOCK(&g_umsMutex)
2020-12-23 18:43:12 +00:00
{
if (!g_umsInterfaceInit || !out_count)
{
LOG_MSG_ERROR("Invalid parameters!");
break;
}
/* 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
}
2020-12-23 18:43:12 +00:00
return devices;
}
bool umsUnmountDevice(const UsbHsFsDevice *device)
{
if (!device)
{
LOG_MSG_ERROR("Invalid parameters!");
return false;
}
/* Unmount USB Mass Storage device. If successful, let libusbhsfs automatically call our populate callback. */
bool ret = usbHsFsUnmountDevice(device, true);
if (!ret) LOG_MSG_ERROR("Failed to unmount UMS device \"%s\"!", device->name);
return ret;
}
static void umsFreeDeviceData(void)
{
/* Free devices buffer. */
if (g_umsDevices)
{
free(g_umsDevices);
g_umsDevices = NULL;
}
/* Reset device count. */
g_umsDeviceCount = 0;
}
static void umsPopulateCallback(const UsbHsFsDevice *devices, u32 device_count, void *user_data)
{
NX_IGNORE_ARG(user_data);
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;
}
}
static bool umsDuplicateDeviceArray(const UsbHsFsDevice *in_devices, u32 in_device_count, UsbHsFsDevice **out_devices, u32 *out_device_count)
{
if (!out_devices || !out_device_count)
{
LOG_MSG_ERROR("Invalid parameters!");
return false;
}
UsbHsFsDevice *tmp_devices = NULL;
bool ret = false;
/* Clear output. */
*out_devices = NULL;
*out_device_count = 0;
/* Short-circuit. */
if (!in_devices || !in_device_count)
{
ret = true;
goto end;
}
/* 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;
}
/* Copy device data. */
memcpy(tmp_devices, in_devices, in_device_count * sizeof(UsbHsFsDevice));
/* Update output. */
*out_devices = tmp_devices;
*out_device_count = in_device_count;
/* Update return value. */
ret = true;
end:
return ret;
}