2019-07-18 04:04:00 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <switch.h>
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
#include <stratosphere/cfg.hpp>
|
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
namespace ams::cfg {
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Convenience definitions. */
|
2019-10-11 07:49:28 +01:00
|
|
|
constexpr os::ProcessId InitialProcessIdMinDeprecated = {0x00};
|
|
|
|
constexpr os::ProcessId InitialProcessIdMaxDeprecated = {0x50};
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
/* Privileged process globals. */
|
2019-09-24 11:15:36 +01:00
|
|
|
os::Mutex g_lock;
|
2019-07-18 04:04:00 +01:00
|
|
|
bool g_got_privileged_process_status = false;
|
2019-10-11 07:49:28 +01:00
|
|
|
os::ProcessId g_min_initial_process_id = os::InvalidProcessId, g_max_initial_process_id = os::InvalidProcessId;
|
|
|
|
os::ProcessId g_cur_process_id = os::InvalidProcessId;
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
/* SD card helpers. */
|
2019-10-11 07:49:28 +01:00
|
|
|
void GetPrivilegedProcessIdRange(os::ProcessId *out_min, os::ProcessId *out_max) {
|
|
|
|
os::ProcessId min = os::InvalidProcessId, max = os::InvalidProcessId;
|
|
|
|
if (hos::GetVersion() >= hos::Version_500) {
|
2019-07-18 04:04:00 +01:00
|
|
|
/* On 5.0.0+, we can get precise limits from svcGetSystemInfo. */
|
2019-10-11 07:49:28 +01:00
|
|
|
R_ASSERT(svcGetSystemInfo(reinterpret_cast<u64 *>(&min), SystemInfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Minimum));
|
|
|
|
R_ASSERT(svcGetSystemInfo(reinterpret_cast<u64 *>(&max), SystemInfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Maximum));
|
|
|
|
} else if (hos::GetVersion() >= hos::Version_400) {
|
2019-07-18 04:04:00 +01:00
|
|
|
/* On 4.0.0-4.1.0, we can get the precise limits from normal svcGetInfo. */
|
2019-10-11 07:49:28 +01:00
|
|
|
R_ASSERT(svcGetInfo(reinterpret_cast<u64 *>(&min), InfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Minimum));
|
|
|
|
R_ASSERT(svcGetInfo(reinterpret_cast<u64 *>(&max), InfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Maximum));
|
2019-07-18 04:04:00 +01:00
|
|
|
} else {
|
|
|
|
/* On < 4.0.0, we just use hardcoded extents. */
|
|
|
|
min = InitialProcessIdMinDeprecated;
|
|
|
|
max = InitialProcessIdMaxDeprecated;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_min = min;
|
|
|
|
*out_max = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetPrivilegedProcessStatus() {
|
|
|
|
GetPrivilegedProcessIdRange(&g_min_initial_process_id, &g_max_initial_process_id);
|
2019-10-11 07:49:28 +01:00
|
|
|
g_cur_process_id = os::GetCurrentProcessId();
|
2019-07-18 04:04:00 +01:00
|
|
|
g_got_privileged_process_status = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Privileged Process utilities. */
|
|
|
|
bool IsInitialProcess() {
|
2019-09-24 11:15:36 +01:00
|
|
|
std::scoped_lock lk(g_lock);
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
/* If we've not detected, do detection. */
|
|
|
|
if (!g_got_privileged_process_status) {
|
|
|
|
GetPrivilegedProcessStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine if we're privileged, and return. */
|
|
|
|
return g_min_initial_process_id <= g_cur_process_id && g_cur_process_id <= g_max_initial_process_id;
|
|
|
|
}
|
|
|
|
|
2019-10-11 07:49:28 +01:00
|
|
|
void GetInitialProcessRange(os::ProcessId *out_min, os::ProcessId *out_max) {
|
2019-09-24 11:15:36 +01:00
|
|
|
std::scoped_lock lk(g_lock);
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
/* If we've not detected, do detection. */
|
|
|
|
if (!g_got_privileged_process_status) {
|
|
|
|
GetPrivilegedProcessStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_min = g_min_initial_process_id;
|
|
|
|
*out_max = g_max_initial_process_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|