diff --git a/nx/include/switch/services/ns.h b/nx/include/switch/services/ns.h index b7123787..e21b7315 100644 --- a/nx/include/switch/services/ns.h +++ b/nx/include/switch/services/ns.h @@ -234,6 +234,50 @@ Service* nsGetServiceSession_GetterInterface(void); /// Gets the Service object for IApplicationManagerInterface. Service* nsGetServiceSession_ApplicationManagerInterface(void); +/// Gets the Service object for IFactoryResetInterface via the cmd for that. +Result nsGetFactoryResetInterface(Service* srv_out); + +///@} + +///@name IFactoryResetInterface +///@{ + +/** + * @brief ResetToFactorySettings + * @note Uses \ref nsGetFactoryResetInterface on [3.0.0+], otherwise IApplicationManagerInterface is used. + */ +Result nsResetToFactorySettings(void); + +/** + * @brief ResetToFactorySettingsWithoutUserSaveData + * @note Uses \ref nsGetFactoryResetInterface on [3.0.0+], otherwise IApplicationManagerInterface is used. + */ +Result nsResetToFactorySettingsWithoutUserSaveData(void); + +/** + * @brief ResetToFactorySettingsForRefurbishment + * @note Uses \ref nsGetFactoryResetInterface on [3.0.0+], otherwise IApplicationManagerInterface is used. + * @note Only available on [2.0.0+]. + */ +Result nsResetToFactorySettingsForRefurbishment(void); + +/** + * @brief ResetToFactorySettingsWithPlatformRegion + * @note Only available on [9.1.0+]. + */ +Result nsResetToFactorySettingsWithPlatformRegion(void); + +/** + * @brief ResetToFactorySettingsWithPlatformRegionAuthentication + * @note Only available on [9.1.0+]. + */ +Result nsResetToFactorySettingsWithPlatformRegionAuthentication(void); + +///@} + +///@name IApplicationManagerInterface +///@{ + /** * @brief Gets an listing of \ref NsApplicationRecord. * @param[out] records Output array of \ref NsApplicationRecord. diff --git a/nx/source/services/ns.c b/nx/source/services/ns.c index eb9e3950..bbc430f1 100644 --- a/nx/source/services/ns.c +++ b/nx/source/services/ns.c @@ -46,6 +46,10 @@ Service* nsGetServiceSession_ApplicationManagerInterface(void) { return &g_nsAppManSrv; } +Result nsGetFactoryResetInterface(Service* srv_out) { + return _nsGetSession(&g_nsGetterSrv, srv_out, 7994); +} + static Result _nsGetSession(Service* srv, Service* srv_out, u32 cmd_id) { return serviceDispatch(srv, cmd_id, .out_num_objects = 1, @@ -234,6 +238,81 @@ static Result _nsCheckNifm(void) { return nifmIsAnyInternetRequestAccepted(nifmGetClientId()) ? 0 : MAKERESULT(16, 340); } +// IFactoryResetInterface + +Result nsResetToFactorySettings(void) { + Service srv={0}, *srv_ptr = &srv; + Result rc=0; + if (hosversionAtLeast(3,0,0)) + rc = nsGetFactoryResetInterface(&srv); + else + srv_ptr = &g_nsAppManSrv; + + if (R_SUCCEEDED(rc)) rc = _nsCmdNoIO(srv_ptr, 100); + + serviceClose(&srv); + return rc; +} + +Result nsResetToFactorySettingsWithoutUserSaveData(void) { + Service srv={0}, *srv_ptr = &srv; + Result rc=0; + if (hosversionAtLeast(3,0,0)) + rc = nsGetFactoryResetInterface(&srv); + else + srv_ptr = &g_nsAppManSrv; + + if (R_SUCCEEDED(rc)) rc = _nsCmdNoIO(srv_ptr, 101); + + serviceClose(&srv); + return rc; +} + +Result nsResetToFactorySettingsForRefurbishment(void) { + if (hosversionBefore(2,0,0)) + return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); + + Service srv={0}, *srv_ptr = &srv; + Result rc=0; + if (hosversionAtLeast(3,0,0)) + rc = nsGetFactoryResetInterface(&srv); + else + srv_ptr = &g_nsAppManSrv; + + if (R_SUCCEEDED(rc)) rc = _nsCmdNoIO(srv_ptr, 102); + + serviceClose(&srv); + return rc; +} + +Result nsResetToFactorySettingsWithPlatformRegion(void) { + if (hosversionBefore(9,1,0)) + return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); + + Service srv={0}; + Result rc = nsGetFactoryResetInterface(&srv); + + if (R_SUCCEEDED(rc)) rc = _nsCmdNoIO(&srv, 103); + + serviceClose(&srv); + return rc; +} + +Result nsResetToFactorySettingsWithPlatformRegionAuthentication(void) { + if (hosversionBefore(9,1,0)) + return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); + + Service srv={0}; + Result rc = nsGetFactoryResetInterface(&srv); + + if (R_SUCCEEDED(rc)) rc = _nsCmdNoIO(&srv, 104); + + serviceClose(&srv); + return rc; +} + +// IApplicationManagerInterface + Result nsListApplicationRecord(NsApplicationRecord* records, s32 count, s32 entry_offset, s32* out_entrycount) { return serviceDispatchInOut(&g_nsAppManSrv, 0, entry_offset, *out_entrycount, .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out },