1
0
Fork 0
mirror of https://github.com/DarkMatterCore/nxdumptool.git synced 2024-11-13 22:16:45 +00:00
nxdumptool/source/services.c

273 lines
8.7 KiB
C
Raw Normal View History

2020-04-16 01:06:41 +01:00
/*
* services.c
2020-04-16 01:06:41 +01:00
*
* Copyright (c) 2020, 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
2020-04-16 01:06:41 +01:00
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* nxdumptool is distributed in the hope it will be useful, but WITHOUT
2020-04-16 01:06:41 +01:00
* 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 <http://www.gnu.org/licenses/>.
*/
2020-07-07 16:51:33 +01:00
#include "utils.h"
2020-04-15 21:50:07 +01:00
#include "services.h"
#include "es.h"
#include "fspusb.h"
/* Type definitions. */
typedef bool (*ServiceCondFunction)(void *arg); /* Used to perform a runtime condition check (e.g. system version) before initializing the service. */
typedef Result (*ServiceInitFunction)(void); /* Used to initialize the service. */
typedef void (*ServiceCloseFunction)(void); /* Used to close the service. */
2020-04-15 21:50:07 +01:00
typedef struct ServicesInfoEntry {
bool initialized;
char name[8];
ServiceCondFunction cond_func;
ServiceInitFunction init_func;
ServiceCloseFunction close_func;
} ServicesInfoEntry;
/* Function prototypes. */
2020-05-15 03:59:57 +01:00
static Result servicesPlUserInitialize(void);
2020-04-15 21:50:07 +01:00
static Result servicesNifmUserInitialize(void);
static bool servicesClkGetServiceType(void *arg);
static bool servicesSplCryptoCheckAvailability(void *arg);
static bool servicesFspUsbCheckAvailability(void *arg);
/* Global variables. */
static ServicesInfoEntry g_serviceInfo[] = {
{ false, "ncm", NULL, &ncmInitialize, &ncmExit },
{ false, "ns", NULL, &nsInitialize, &nsExit },
{ false, "csrng", NULL, &csrngInitialize, &csrngExit },
{ false, "spl", NULL, &splInitialize, &splExit },
{ false, "spl:mig", &servicesSplCryptoCheckAvailability, &splCryptoInitialize, &splCryptoExit }, /* Checks if spl:mig is really available (e.g. avoid calling splInitialize twice). */
2020-04-15 21:50:07 +01:00
{ false, "pm:dmnt", NULL, &pmdmntInitialize, &pmdmntExit },
2020-05-15 03:59:57 +01:00
{ false, "pl:u", NULL, &servicesPlUserInitialize, &plExit },
2020-04-15 21:50:07 +01:00
{ false, "psm", NULL, &psmInitialize, &psmExit },
{ false, "nifm:u", NULL, &servicesNifmUserInitialize, &nifmExit },
{ false, "clk", &servicesClkGetServiceType, NULL, NULL }, /* Placeholder for pcv / clkrst. */
{ false, "fsp-usb", &servicesFspUsbCheckAvailability, &fspusbInitialize, &fspusbExit }, /* Checks if fsp-usb is really available. */
2020-04-15 21:50:07 +01:00
{ false, "es", NULL, &esInitialize, &esExit },
{ false, "set:cal", NULL, &setcalInitialize, &setcalExit }
2020-04-15 21:50:07 +01:00
};
static const u32 g_serviceInfoCount = MAX_ELEMENTS(g_serviceInfo);
static bool g_clkSvcUsePcv = false;
static ClkrstSession g_clkrstCpuSession = {0}, g_clkrstMemSession = {0};
2020-05-03 00:40:50 +01:00
static Mutex g_servicesMutex = 0;
2020-04-15 21:50:07 +01:00
bool servicesInitialize(void)
{
2020-05-03 00:40:50 +01:00
mutexLock(&g_servicesMutex);
2020-04-15 21:50:07 +01:00
Result rc = 0;
bool ret = true;
for(u32 i = 0; i < g_serviceInfoCount; i++)
{
/* Check if this service has been already initialized or if it actually has a valid initialize function. */
2020-05-10 17:40:12 +01:00
if (g_serviceInfo[i].initialized || g_serviceInfo[i].init_func == NULL) continue;
2020-04-15 21:50:07 +01:00
/* Check if this service depends on a condition function. */
2020-04-15 21:50:07 +01:00
if (g_serviceInfo[i].cond_func != NULL)
{
/* Run the condition function - it will update the current service member. */
/* Skip this service if the required conditions aren't met. */
2020-04-15 21:50:07 +01:00
if (!g_serviceInfo[i].cond_func(&(g_serviceInfo[i]))) continue;
}
/* Initialize service. */
2020-04-15 21:50:07 +01:00
rc = g_serviceInfo[i].init_func();
if (R_FAILED(rc))
{
LOGFILE("Failed to initialize %s service! (0x%08X).", g_serviceInfo[i].name, rc);
2020-04-15 21:50:07 +01:00
ret = false;
break;
}
/* Update initialized flag. */
2020-04-15 21:50:07 +01:00
g_serviceInfo[i].initialized = true;
}
2020-05-03 00:40:50 +01:00
mutexUnlock(&g_servicesMutex);
2020-04-15 21:50:07 +01:00
return ret;
}
void servicesClose(void)
{
2020-05-03 00:40:50 +01:00
mutexLock(&g_servicesMutex);
2020-04-15 21:50:07 +01:00
for(u32 i = 0; i < g_serviceInfoCount; i++)
{
/* Check if this service has not been initialized, or if it doesn't have a valid close function. */
2020-04-15 21:50:07 +01:00
if (!g_serviceInfo[i].initialized || g_serviceInfo[i].close_func == NULL) continue;
/* Close service. */
2020-04-15 21:50:07 +01:00
g_serviceInfo[i].close_func();
}
2020-05-03 00:40:50 +01:00
mutexUnlock(&g_servicesMutex);
2020-04-15 21:50:07 +01:00
}
bool servicesCheckRunningServiceByName(const char *name)
{
if (!name || !strlen(name)) return false;
2020-04-19 23:44:22 +01:00
Handle handle = INVALID_HANDLE;
2020-04-15 21:50:07 +01:00
SmServiceName service_name = smEncodeName(name);
Result rc = smRegisterService(&handle, service_name, false, 1);
bool running = R_FAILED(rc);
2020-04-19 23:44:22 +01:00
if (handle != INVALID_HANDLE) svcCloseHandle(handle);
2020-04-15 21:50:07 +01:00
if (!running) smUnregisterService(service_name);
return running;
}
bool servicesCheckInitializedServiceByName(const char *name)
{
2020-05-03 00:40:50 +01:00
mutexLock(&g_servicesMutex);
2020-04-15 21:50:07 +01:00
bool ret = false;
2020-05-03 00:40:50 +01:00
if (!name || !strlen(name)) goto end;
2020-05-03 00:40:50 +01:00
2020-04-15 21:50:07 +01:00
size_t name_len = strlen(name);
for(u32 i = 0; i < g_serviceInfoCount; i++)
{
if (strlen(g_serviceInfo[i].name) == name_len && !strcmp(g_serviceInfo[i].name, name))
2020-04-15 21:50:07 +01:00
{
ret = g_serviceInfo[i].initialized;
break;
}
}
end:
2020-05-03 00:40:50 +01:00
mutexUnlock(&g_servicesMutex);
2020-04-15 21:50:07 +01:00
return ret;
}
void servicesChangeHardwareClockRates(u32 cpu_rate, u32 mem_rate)
{
2020-05-03 00:40:50 +01:00
mutexLock(&g_servicesMutex);
2020-04-15 21:50:07 +01:00
if (g_clkSvcUsePcv)
{
pcvSetClockRate(PcvModule_CpuBus, cpu_rate);
pcvSetClockRate(PcvModule_EMC, mem_rate);
} else {
clkrstSetClockRate(&g_clkrstCpuSession, cpu_rate);
clkrstSetClockRate(&g_clkrstMemSession, mem_rate);
}
2020-05-03 00:40:50 +01:00
mutexUnlock(&g_servicesMutex);
2020-04-15 21:50:07 +01:00
}
2020-05-15 03:59:57 +01:00
static Result servicesPlUserInitialize(void)
{
return plInitialize(PlServiceType_User);
}
2020-04-15 21:50:07 +01:00
static Result servicesNifmUserInitialize(void)
{
return nifmInitialize(NifmServiceType_User);
}
static Result servicesClkrstInitialize(void)
{
Result rc = 0;
/* Open clkrst service handle. */
2020-04-15 21:50:07 +01:00
rc = clkrstInitialize();
if (R_FAILED(rc)) return rc;
/* Initialize CPU and MEM clkrst sessions. */
2020-04-15 21:50:07 +01:00
memset(&g_clkrstCpuSession, 0, sizeof(ClkrstSession));
memset(&g_clkrstMemSession, 0, sizeof(ClkrstSession));
rc = clkrstOpenSession(&g_clkrstCpuSession, PcvModuleId_CpuBus, 3);
if (R_FAILED(rc))
{
clkrstExit();
return rc;
}
rc = clkrstOpenSession(&g_clkrstMemSession, PcvModuleId_EMC, 3);
if (R_FAILED(rc))
{
clkrstCloseSession(&g_clkrstCpuSession);
clkrstExit();
}
return rc;
}
static void servicesClkrstExit(void)
{
/* Close CPU and MEM clkrst sessions. */
2020-04-15 21:50:07 +01:00
clkrstCloseSession(&g_clkrstMemSession);
clkrstCloseSession(&g_clkrstCpuSession);
/* Close clkrst service handle. */
2020-04-15 21:50:07 +01:00
clkrstExit();
}
static bool servicesClkGetServiceType(void *arg)
{
if (!arg) return false;
ServicesInfoEntry *info = (ServicesInfoEntry*)arg;
if (strlen(info->name) != 3 || strcmp(info->name, "clk") != 0 || info->init_func != NULL || info->close_func != NULL) return false;
2020-04-15 21:50:07 +01:00
/* Determine which service needs to be used to control hardware clock rates, depending on the system version. */
/* This may either be pcv (sysver lower than 8.0.0) or clkrst (sysver equal to or greater than 8.0.0). */
2020-04-15 21:50:07 +01:00
g_clkSvcUsePcv = hosversionBefore(8, 0, 0);
/* Fill service info. */
2020-04-15 21:50:07 +01:00
sprintf(info->name, "%s", (g_clkSvcUsePcv ? "pcv" : "clkrst"));
info->init_func = (g_clkSvcUsePcv ? &pcvInitialize : &servicesClkrstInitialize);
info->close_func = (g_clkSvcUsePcv ? &pcvExit : &servicesClkrstExit);
return true;
}
static bool servicesSplCryptoCheckAvailability(void *arg)
{
if (!arg) return false;
ServicesInfoEntry *info = (ServicesInfoEntry*)arg;
if (strlen(info->name) != 7 || strcmp(info->name, "spl:mig") != 0 || info->init_func == NULL || info->close_func == NULL) return false;
2020-04-15 21:50:07 +01:00
/* Check if spl:mig is available (sysver equal to or greater than 4.0.0). */
2020-04-15 21:50:07 +01:00
return !hosversionBefore(4, 0, 0);
}
static bool servicesFspUsbCheckAvailability(void *arg)
{
if (!arg) return false;
ServicesInfoEntry *info = (ServicesInfoEntry*)arg;
if (strlen(info->name) != 7 || strcmp(info->name, "fsp-usb") != 0 || info->init_func == NULL || info->close_func == NULL) return false;
2020-04-15 21:50:07 +01:00
/* Check if fsp-usb is actually running in the background. */
2020-04-15 21:50:07 +01:00
return servicesCheckRunningServiceByName("fsp-usb");
}