From 5eb96cbece753f0e53b430a7e34061123db4faa6 Mon Sep 17 00:00:00 2001 From: Pablo Curiel Date: Wed, 24 Feb 2021 23:36:53 -0400 Subject: [PATCH] services: completely ditch smRegisterService() and just use the AtmosphereHasService SM API extension. This extension was already being used - however, smRegisterService() was also being used as a fallback method. More than a year and a half has already passed since this feature was introduced, and both SX OS and ReiNX apparently support it now, so it's a justified change. --- source/mem.c | 2 +- source/services.c | 17 ++++++++++------- source/services.h | 6 +++--- source/utils.c | 35 ++++++----------------------------- 4 files changed, 20 insertions(+), 40 deletions(-) diff --git a/source/mem.c b/source/mem.c index f83012b..a45d788 100644 --- a/source/mem.c +++ b/source/mem.c @@ -97,7 +97,7 @@ static bool memRetrieveProgramMemory(MemoryLocation *location, bool is_segment) if (is_segment && location->program_id == FS_SYSMODULE_TID) { - /* If dealing with FS, locate the "real" .text segment, since Atmosphere emuMMC has two. */ + /* If dealing with FS, locate the "real" .text segment, since Atmosphère emuMMC has two. */ do { rc = svcQueryDebugProcessMemory(&mem_info, &page_info, debug_handle, addr); if (R_FAILED(rc)) diff --git a/source/services.c b/source/services.c index ec682c4..824986f 100644 --- a/source/services.c +++ b/source/services.c @@ -38,7 +38,7 @@ typedef struct { /* Function prototypes. */ -static Result smHasService(bool *out_has_service, SmServiceName name); +static Result smAtmosphereHasService(bool *out, SmServiceName name); static Result servicesNifmUserInitialize(void); static bool servicesClkGetServiceType(void *arg); @@ -190,27 +190,30 @@ void servicesChangeHardwareClockRates(u32 cpu_rate, u32 mem_rate) mutexUnlock(&g_servicesMutex); } -Result servicesAtmosphereHasService(bool *out_has_service, const char *name) +Result servicesHasService(bool *out, const char *name) { - if (!out_has_service || !name || !*name) + if (!out || !name || !*name) { LOGFILE("Invalid parameters!"); return MAKERESULT(Module_Libnx, LibnxError_IoError); } + *out = false; + Result rc = 0; SmServiceName service_name = smEncodeName(name); - Result rc = smHasService(out_has_service, service_name); - if (R_FAILED(rc)) LOGFILE("smHasService failed for \"%s\"! (0x%08X).", name, rc); + rc = smAtmosphereHasService(out, service_name); + if (R_FAILED(rc)) LOGFILE("smAtmosphereHasService failed for \"%s\"! (0x%08X).", name, rc); return rc; } -static Result smHasService(bool *out_has_service, SmServiceName name) +/* SM API extension available in Atmosphère and Atmosphère-based CFWs. */ +static Result smAtmosphereHasService(bool *out, SmServiceName name) { u8 tmp = 0; Result rc = serviceDispatchInOut(smGetServiceSession(), 65100, name, tmp); - if (R_SUCCEEDED(rc) && out_has_service) *out_has_service = (tmp & 1); + if (R_SUCCEEDED(rc) && out) *out = tmp; return rc; } diff --git a/source/services.h b/source/services.h index 305dcf3..4c5d146 100644 --- a/source/services.h +++ b/source/services.h @@ -45,8 +45,8 @@ bool servicesCheckInitializedServiceByName(const char *name); /// Changes CPU/MEM clock rates at runtime. void servicesChangeHardwareClockRates(u32 cpu_rate, u32 mem_rate); -/// Wrapper for the Atmosphere-only SM API "HasService" extension. -/// Perfectly safe under development units. Not available in older Atmosphere releases. -Result servicesAtmosphereHasService(bool *out_has_service, const char *name); +/// Wrapper for the "AtmosphereHasService" SM API extension from Atmosphère and Atmosphère-based CFWs. +/// Perfectly safe under development units. Not available in older Atmosphère releases. +Result servicesHasService(bool *out, const char *name); #endif /* __SERVICES_H__ */ diff --git a/source/utils.c b/source/utils.c index 757ed52..9eef030 100644 --- a/source/utils.c +++ b/source/utils.c @@ -66,7 +66,7 @@ static const char *g_illegalFileSystemChars = "\\/:*?\"<>|^"; /* Function prototypes. */ -static bool _utilsGetCustomFirmwareType(void); +static void _utilsGetCustomFirmwareType(void); static bool _utilsIsDevelopmentUnit(void); @@ -98,7 +98,7 @@ bool utilsInitializeResources(void) /* Retrieve custom firmware type. */ if (!_utilsGetCustomFirmwareType()) goto end; - LOGFILE("Detected %s CFW.", (g_customFirmwareType == UtilsCustomFirmwareType_Atmosphere ? "Atmosphere" : (g_customFirmwareType == UtilsCustomFirmwareType_SXOS ? "SX OS" : "ReiNX"))); + LOGFILE("Detected %s CFW.", (g_customFirmwareType == UtilsCustomFirmwareType_Atmosphere ? "Atmosphère" : (g_customFirmwareType == UtilsCustomFirmwareType_SXOS ? "SX OS" : "ReiNX"))); /* Initialize needed services. */ if (!servicesInitialize()) @@ -751,37 +751,14 @@ void utilsOverclockSystem(bool overclock) servicesChangeHardwareClockRates(cpuClkRate, memClkRate); } -static bool _utilsGetCustomFirmwareType(void) +static void _utilsGetCustomFirmwareType(void) { - bool has_service = false, tx_srv = false, rnx_srv = false; + bool has_srv = false, tx_srv = false, rnx_srv = false; - /* First, check if we're running under Atmosphere or an Atmosphere-based CFW by using a SM API extension that's only provided by it. */ - if (R_SUCCEEDED(servicesAtmosphereHasService(&has_service, "ncm"))) - { - /* We're running under Atmosphere or an Atmosphere-based CFW. Time to check which one is it. */ - tx_srv = (R_SUCCEEDED(servicesAtmosphereHasService(&has_service, "tx")) && has_service); - rnx_srv = (R_SUCCEEDED(servicesAtmosphereHasService(&has_service, "rnx")) && has_service); - } else { - /* Odds are we're not running under Atmosphere, or maybe we're running under an old Atmosphere version without SM API extensions. */ - /* We'll use the smRegisterService() trick to check for running services. */ - /* But first, we need to re-initialize SM in order to avoid 0xF601 (port remote dead) errors. */ - smExit(); - - Result rc = smInitialize(); - if (R_FAILED(rc)) - { - LOGFILE("smInitialize failed! (0x%08X).", rc); - return false; - } - - tx_srv = servicesCheckRunningServiceByName("tx"); - rnx_srv = servicesCheckRunningServiceByName("rnx"); - } + tx_srv = (R_SUCCEEDED(servicesHasService(&has_srv, "tx")) && has_srv); + rnx_srv = (R_SUCCEEDED(servicesHasService(&has_srv, "rnx")) && has_srv); - /* Finally, determine the CFW type. */ g_customFirmwareType = (rnx_srv ? UtilsCustomFirmwareType_ReiNX : (tx_srv ? UtilsCustomFirmwareType_SXOS : UtilsCustomFirmwareType_Atmosphere)); - - return true; } static bool _utilsIsDevelopmentUnit(void)