ams_mitm: begin skeleton refactor

This commit is contained in:
Michael Scire
2019-11-20 23:58:18 -08:00
committed by SciresM
parent 02d4c97c6d
commit 393596ef9a
105 changed files with 1541 additions and 352 deletions

View File

@@ -1,105 +0,0 @@
/*
* 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 <mutex>
#include <algorithm>
#include <switch.h>
#include "set_mitm_service.hpp"
void SetMitmService::PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx) {
/* No commands need postprocessing. */
}
bool SetMitmService::IsValidLanguageCode(u64 lang_code) {
static constexpr u64 s_valid_language_codes[] = {
LanguageCode_Japanese,
LanguageCode_AmericanEnglish,
LanguageCode_French,
LanguageCode_German,
LanguageCode_Italian,
LanguageCode_Spanish,
LanguageCode_Chinese,
LanguageCode_Korean,
LanguageCode_Dutch,
LanguageCode_Portuguese,
LanguageCode_Russian,
LanguageCode_Taiwanese,
LanguageCode_BritishEnglish,
LanguageCode_CanadianFrench,
LanguageCode_LatinAmericanSpanish,
LanguageCode_SimplifiedChinese,
LanguageCode_TraditionalChinese,
};
size_t num_language_codes = ams::util::size(s_valid_language_codes);
if (GetRuntimeFirmwareVersion() < FirmwareVersion_400) {
/* 4.0.0 added simplified and traditional chinese. */
num_language_codes -= 2;
}
for (size_t i = 0; i < num_language_codes; i++) {
if (lang_code == s_valid_language_codes[i]) {
return true;
}
}
return false;
}
bool SetMitmService::IsValidRegionCode(u32 region_code) {
return region_code < RegionCode_Max;
}
Result SetMitmService::EnsureLocale() {
std::scoped_lock lk(this->lock);
if (!this->got_locale) {
std::memset(&this->locale, 0xCC, sizeof(this->locale));
if (this->title_id == ams::ncm::TitleId::Ns) {
u64 app_pid = 0;
u64 app_tid = 0;
R_TRY(pmdmntGetApplicationPid(&app_pid));
R_TRY(pminfoGetTitleId(&app_tid, app_pid));
this->locale = Utils::GetTitleOverrideLocale(app_tid);
} else {
this->locale = Utils::GetTitleOverrideLocale(static_cast<u64>(this->title_id));
this->got_locale = true;
}
}
return ResultSuccess();
}
Result SetMitmService::GetLanguageCode(Out<u64> out_lang_code) {
this->EnsureLocale();
if (!IsValidLanguageCode(this->locale.language_code)) {
return ResultAtmosphereMitmShouldForwardToSession;
}
out_lang_code.SetValue(this->locale.language_code);
return ResultSuccess();
}
Result SetMitmService::GetRegionCode(Out<u32> out_region_code) {
this->EnsureLocale();
if (!IsValidRegionCode(this->locale.region_code)) {
return ResultAtmosphereMitmShouldForwardToSession;
}
out_region_code.SetValue(this->locale.region_code);
return ResultSuccess();
}

View File

@@ -13,47 +13,29 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
#include "../utils.hpp"
namespace ams::mitm::set {
class SetMitmService : public IMitmServiceObject {
private:
enum class CommandId {
GetLanguageCode = 0,
GetRegionCode = 4,
};
private:
ams::os::Mutex lock;
OverrideLocale locale;
bool got_locale;
public:
SetMitmService(std::shared_ptr<Service> s, u64 pid, ams::ncm::TitleId tid) : IMitmServiceObject(s, pid, tid) {
this->got_locale = false;
}
class SetMitmService : public sf::IMitmServiceObject {
private:
enum class CommandId {
/* TODO */
};
public:
static bool ShouldMitm(os::ProcessId process_id, ncm::ProgramId program_id) {
/* TODO */
return false;
}
public:
SF_MITM_SERVICE_OBJECT_CTOR(SetMitmService) { /* ... */ }
protected:
/* TODO */
public:
DEFINE_SERVICE_DISPATCH_TABLE {
/* TODO */
};
};
static bool ShouldMitm(u64 pid, ams::ncm::TitleId tid) {
/* Mitm all applications. */
return tid == ams::ncm::TitleId::Ns || ams::ncm::IsApplicationTitleId(tid);
}
static void PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx);
protected:
static bool IsValidLanguageCode(u64 lang_code);
static bool IsValidRegionCode(u32 region_code);
Result EnsureLocale();
protected:
/* Overridden commands. */
Result GetLanguageCode(Out<u64> out_lang_code);
Result GetRegionCode(Out<u32> out_region_code);
public:
DEFINE_SERVICE_DISPATCH_TABLE {
MAKE_SERVICE_COMMAND_META(SetMitmService, GetLanguageCode),
MAKE_SERVICE_COMMAND_META(SetMitmService, GetRegionCode),
};
};
}

View File

@@ -1,62 +0,0 @@
/*
* 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 <cstdlib>
#include <cstdint>
#include <cstring>
#include <malloc.h>
#include <switch.h>
#include <atmosphere.h>
#include <stratosphere.hpp>
#include "setmitm_main.hpp"
#include "setsys_mitm_service.hpp"
#include "setsys_settings_items.hpp"
#include "setsys_firmware_version.hpp"
#include "set_mitm_service.hpp"
#include "../utils.hpp"
struct SetSysManagerOptions {
static const size_t PointerBufferSize = 0x100;
static const size_t MaxDomains = 4;
static const size_t MaxDomainObjects = 0x100;
};
using SetMitmManager = WaitableManager<SetSysManagerOptions>;
void SetMitmMain(void *arg) {
/* Wait for SD to initialize. */
Utils::WaitSdInitialized();
/* Initialize version manager. */
VersionManager::Initialize();
/* Create server manager */
static auto s_server_manager = SetMitmManager(4);
/* Create set:sys mitm. */
AddMitmServerToManager<SetSysMitmService>(&s_server_manager, "set:sys", 60);
/* Create set mitm. */
AddMitmServerToManager<SetMitmService>(&s_server_manager, "set", 60);
/* Loop forever, servicing our services. */
s_server_manager.Process();
}

View File

@@ -1,29 +0,0 @@
/*
* 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 <cstdlib>
#include <cstdint>
#include <cstring>
#include <malloc.h>
#include <switch.h>
#include <atmosphere.h>
#include <stratosphere.hpp>
constexpr u32 SetMitmPriority = 43;
constexpr u32 SetMitmStackSize = 0x8000;
void SetMitmMain(void *arg);

View File

@@ -0,0 +1,48 @@
/*
* 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 "setmitm_module.hpp"
#include "set_mitm_service.hpp"
#include "setsys_mitm_service.hpp"
namespace ams::mitm::set {
namespace {
constexpr sm::ServiceName SetMitmServiceName = sm::ServiceName::Encode("set");
constexpr sm::ServiceName SetSysMitmServiceName = sm::ServiceName::Encode("set:sys");
struct ServerOptions {
static constexpr size_t PointerBufferSize = 0x100;
static constexpr size_t MaxDomains = 0;
static constexpr size_t MaxDomainObjects = 0;
};
constexpr size_t MaxServers = 2;
constexpr size_t MaxSessions = 60;
sf::hipc::ServerManager<MaxServers, ServerOptions, MaxSessions> g_server_manager;
}
void MitmModule::ThreadFunction(void *arg) {
/* Create mitm servers. */
R_ASSERT(g_server_manager.RegisterMitmServer<SetMitmService>(SetMitmServiceName));
R_ASSERT(g_server_manager.RegisterMitmServer<SetSysMitmService>(SetSysMitmServiceName));
/* Loop forever, servicing our services. */
g_server_manager.LoopProcess();
}
}

View File

@@ -13,13 +13,12 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
#include "../amsmitm_module.hpp"
class VersionManager {
public:
static void Initialize();
static Result GetFirmwareVersion(ams::ncm::TitleId title_id, SetSysFirmwareVersion *out);
};
namespace ams::mitm::set {
DEFINE_MITM_MODULE_CLASS(0x8000, 43);
}

View File

@@ -1,79 +0,0 @@
/*
* 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 <mutex>
#include <switch.h>
#include "setsys_firmware_version.hpp"
static ams::os::Mutex g_version_mutex;
static bool g_got_version = false;
static SetSysFirmwareVersion g_ams_fw_version = {0};
static SetSysFirmwareVersion g_fw_version = {0};
void VersionManager::Initialize() {
std::scoped_lock lock(g_version_mutex);
if (g_got_version) {
return;
}
/* Mount firmware version data archive. */
R_ASSERT(romfsMountFromDataArchive(static_cast<u64>(ams::ncm::TitleId::ArchiveSystemVersion), FsStorageId_NandSystem, "sysver"));
{
ON_SCOPE_EXIT { romfsUnmount("sysver"); };
SetSysFirmwareVersion fw_ver;
/* Firmware version file must exist. */
FILE *f = fopen("sysver:/file", "rb");
AMS_ASSERT(f != NULL);
ON_SCOPE_EXIT { fclose(f); };
/* Must be possible to read firmware version from file. */
AMS_ASSERT(fread(&fw_ver, sizeof(fw_ver), 1, f) == 1);
g_fw_version = fw_ver;
g_ams_fw_version = fw_ver;
}
/* Modify the output firmware version. */
{
u32 major, minor, micro;
GetAtmosphereApiVersion(&major, &minor, &micro, nullptr, nullptr);
const char emummc_char = IsEmummc() ? 'E' : 'S';
{
char display_version[sizeof(g_ams_fw_version.display_version)] = {0};
std::snprintf(display_version, sizeof(display_version), "%s|AMS %u.%u.%u|%c", g_ams_fw_version.display_version, major, minor, micro, emummc_char);
std::memcpy(g_ams_fw_version.display_version, display_version, sizeof(g_ams_fw_version.display_version));
}
}
g_got_version = true;
}
Result VersionManager::GetFirmwareVersion(ams::ncm::TitleId title_id, SetSysFirmwareVersion *out) {
VersionManager::Initialize();
/* Report atmosphere string to qlaunch, maintenance and nothing else. */
if (title_id == ams::ncm::TitleId::AppletQlaunch || title_id == ams::ncm::TitleId::AppletMaintenanceMenu) {
*out = g_ams_fw_version;
} else {
*out = g_fw_version;
}
return ResultSuccess();
}

View File

@@ -1,100 +0,0 @@
/*
* 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 <mutex>
#include <algorithm>
#include <switch.h>
#include "setsys_mitm_service.hpp"
#include "setsys_firmware_version.hpp"
#include "setsys_settings_items.hpp"
void SetSysMitmService::PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx) {
/* No commands need postprocessing. */
}
Result SetSysMitmService::GetFirmwareVersion(OutPointerWithServerSize<SetSysFirmwareVersion, 0x1> out) {
/* Get firmware version from manager. */
R_TRY(VersionManager::GetFirmwareVersion(this->title_id, out.pointer));
/* GetFirmwareVersion sanitizes these fields. */
out.pointer->revision_major = 0;
out.pointer->revision_minor = 0;
return ResultSuccess();
}
Result SetSysMitmService::GetFirmwareVersion2(OutPointerWithServerSize<SetSysFirmwareVersion, 0x1> out) {
return VersionManager::GetFirmwareVersion(this->title_id, out.pointer);
}
Result SetSysMitmService::GetSettingsItemValueSize(Out<u64> out_size, InPointer<char> in_name, InPointer<char> in_key) {
char name[SET_MAX_NAME_SIZE] = {0};
char key[SET_MAX_NAME_SIZE] = {0};
/* Validate name and key. */
R_TRY(SettingsItemManager::ValidateName(in_name.pointer));
R_TRY(SettingsItemManager::ValidateKey(in_key.pointer));
if (in_name.num_elements < SET_MAX_NAME_SIZE) {
strncpy(name, in_name.pointer, in_name.num_elements);
} else {
strncpy(name, in_name.pointer, SET_MAX_NAME_SIZE-1);
}
if (in_key.num_elements < SET_MAX_NAME_SIZE) {
strncpy(key, in_key.pointer, in_key.num_elements);
} else {
strncpy(key, in_key.pointer, SET_MAX_NAME_SIZE-1);
}
/* Try to get override setting, fall back to real setting. */
if (R_FAILED(SettingsItemManager::GetValueSize(name, key, out_size.GetPointer()))) {
R_TRY(setsysGetSettingsItemValueSize(name, key, out_size.GetPointer()));
}
return ResultSuccess();
}
Result SetSysMitmService::GetSettingsItemValue(Out<u64> out_size, OutBuffer<u8> out_value, InPointer<char> in_name, InPointer<char> in_key) {
char name[SET_MAX_NAME_SIZE] = {0};
char key[SET_MAX_NAME_SIZE] = {0};
/* Validate name and key. */
R_TRY(SettingsItemManager::ValidateName(in_name.pointer));
R_TRY(SettingsItemManager::ValidateKey(in_key.pointer));
if (out_value.buffer == nullptr) {
return ResultSettingsItemValueBufferNull;
}
if (in_name.num_elements < SET_MAX_NAME_SIZE) {
strncpy(name, in_name.pointer, in_name.num_elements);
} else {
strncpy(name, in_name.pointer, SET_MAX_NAME_SIZE-1);
}
if (in_key.num_elements < SET_MAX_NAME_SIZE) {
strncpy(key, in_key.pointer, in_key.num_elements);
} else {
strncpy(key, in_key.pointer, SET_MAX_NAME_SIZE-1);
}
/* Try to get override setting, fall back to real setting. */
if (R_FAILED(SettingsItemManager::GetValue(name, key, out_value.buffer, out_value.num_elements, out_size.GetPointer()))) {
R_TRY(setsysGetSettingsItemValueFwd(this->forward_service.get(), name, key, out_value.buffer, out_value.num_elements, out_size.GetPointer()));
}
return ResultSuccess();
}

View File

@@ -13,44 +13,29 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
#include "setsys_shim.h"
namespace ams::mitm::set {
class SetSysMitmService : public IMitmServiceObject {
private:
enum class CommandId {
GetFirmwareVersion = 3,
GetFirmwareVersion2 = 4,
GetSettingsItemValueSize = 37,
GetSettingsItemValue = 38,
};
public:
SetSysMitmService(std::shared_ptr<Service> s, u64 pid, ams::ncm::TitleId tid) : IMitmServiceObject(s, pid, tid) {
/* ... */
}
class SetSysMitmService : public sf::IMitmServiceObject {
private:
enum class CommandId {
/* TODO */
};
public:
static bool ShouldMitm(os::ProcessId process_id, ncm::ProgramId program_id) {
/* TODO */
return false;
}
public:
SF_MITM_SERVICE_OBJECT_CTOR(SetSysMitmService) { /* ... */ }
protected:
/* TODO */
public:
DEFINE_SERVICE_DISPATCH_TABLE {
/* TODO */
};
};
static bool ShouldMitm(u64 pid, ams::ncm::TitleId tid) {
/* Mitm everything. */
return true;
}
static void PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx);
protected:
/* Overridden commands. */
Result GetFirmwareVersion(OutPointerWithServerSize<SetSysFirmwareVersion, 0x1> out);
Result GetFirmwareVersion2(OutPointerWithServerSize<SetSysFirmwareVersion, 0x1> out);
Result GetSettingsItemValueSize(Out<u64> out_size, InPointer<char> name, InPointer<char> key);
Result GetSettingsItemValue(Out<u64> out_size, OutBuffer<u8> out_value, InPointer<char> name, InPointer<char> key);
public:
DEFINE_SERVICE_DISPATCH_TABLE {
MAKE_SERVICE_COMMAND_META(SetSysMitmService, GetFirmwareVersion),
MAKE_SERVICE_COMMAND_META(SetSysMitmService, GetFirmwareVersion2),
MAKE_SERVICE_COMMAND_META(SetSysMitmService, GetSettingsItemValueSize),
MAKE_SERVICE_COMMAND_META(SetSysMitmService, GetSettingsItemValue),
};
};
}

View File

@@ -1,304 +0,0 @@
/*
* 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 <mutex>
#include <algorithm>
#include <map>
#include <string>
#include <utility>
#include <switch.h>
#include <strings.h>
#include <ctype.h>
#include "setsys_settings_items.hpp"
#include "../utils.hpp"
#include "../ini.h"
struct SettingsItemValue {
size_t size;
u8 *data;
};
std::map<std::string, SettingsItemValue> g_settings_items;
static bool g_threw_fatal = false;
static ams::os::Thread g_fatal_thread;
static void FatalThreadFunc(void *arg) {
svcSleepThread(5000000000ULL);
fatalSimple(static_cast<Result>(reinterpret_cast<uintptr_t>(arg)));
}
static bool IsCorrectFormat(const char *str, size_t len) {
if (len > 0 && str[len - 1] == '.') {
return false;
}
for (size_t i = 0; i < len; i++) {
const char c = *(str++);
if ('a' <= c && c <= 'z') {
continue;
}
if ('0' <= c && c <= '9') {
continue;
}
if (c == '-' || c == '.' || c == '_') {
continue;
}
return false;
}
return true;
}
Result SettingsItemManager::ValidateName(const char *name, size_t max_size) {
if (name == nullptr) {
return ResultSettingsItemNameNull;
}
const size_t name_len = strnlen(name, std::min(max_size, MaxNameLength + 1));
if (name_len == 0) {
return ResultSettingsItemNameEmpty;
} else if (name_len > MaxNameLength) {
return ResultSettingsItemNameTooLong;
}
if (!IsCorrectFormat(name, name_len)) {
return ResultSettingsItemNameInvalidFormat;
}
return ResultSuccess();
}
Result SettingsItemManager::ValidateName(const char *name) {
return ValidateName(name, MaxNameLength + 1);
}
Result SettingsItemManager::ValidateKey(const char *key, size_t max_size) {
if (key == nullptr) {
return ResultSettingsItemKeyNull;
}
const size_t key_len = strnlen(key, std::min(max_size, MaxKeyLength + 1));
if (key_len == 0) {
return ResultSettingsItemKeyEmpty;
} else if (key_len > MaxKeyLength) {
return ResultSettingsItemKeyTooLong;
}
if (!IsCorrectFormat(key, key_len)) {
return ResultSettingsItemKeyInvalidFormat;
}
return ResultSuccess();
}
Result SettingsItemManager::ValidateKey(const char *key) {
return ValidateKey(key, MaxKeyLength + 1);
}
static bool IsHexadecimal(const char *str) {
while (*str) {
if (isxdigit((unsigned char)*str)) {
str++;
} else {
return false;
}
}
return true;
}
static char hextoi(char c) {
if ('a' <= c && c <= 'f') return c - 'a' + 0xA;
if ('A' <= c && c <= 'F') return c - 'A' + 0xA;
if ('0' <= c && c <= '9') return c - '0';
return 0;
}
static Result ParseValue(const char *name, const char *key, const char *val_tup) {
const char *delimiter = strchr(val_tup, '!');
const char *value_str = delimiter + 1;
const char *type = val_tup;
if (delimiter == NULL) {
return ResultSettingsItemValueInvalidFormat;
}
while (isspace((unsigned char)*type) && type != delimiter) {
type++;
}
size_t type_len = delimiter - type;
size_t value_len = strlen(value_str);
if (delimiter == NULL || value_len == 0 || type_len == 0) {
return ResultSettingsItemValueInvalidFormat;
}
std::string kv = std::string(name).append("!").append(key);
SettingsItemValue value;
if (strncasecmp(type, "str", type_len) == 0 || strncasecmp(type, "string", type_len) == 0) {
/* String */
value.size = value_len + 1;
value.data = reinterpret_cast<u8 *>(strdup(value_str));
if (value.data == nullptr) {
return ResultSettingsItemValueAllocationFailed;
}
} else if (strncasecmp(type, "hex", type_len) == 0 || strncasecmp(type, "bytes", type_len) == 0) {
/* hex */
if (value_len % 2 || !IsHexadecimal(value_str)) {
return ResultSettingsItemValueInvalidFormat;
}
value.size = value_len / 2;
u8 *data = reinterpret_cast<u8 *>(malloc(value.size));
if (data == nullptr) {
return ResultSettingsItemValueAllocationFailed;
}
memset(data, 0, value.size);
for (size_t i = 0; i < value_len; i++) {
data[i >> 1] |= hextoi(value_str[i]) << (4 * (i & 1));
}
value.data = data;
} else if (strncasecmp(type, "u8", type_len) == 0) {
/* u8 */
value.size = sizeof(u8);
u8 *data = reinterpret_cast<u8 *>(malloc(value.size));
if (data == nullptr) {
return ResultSettingsItemValueAllocationFailed;
}
*data = (u8)(strtoul(value_str, nullptr, 0));
value.data = reinterpret_cast<u8 *>(data);
} else if (strncasecmp(type, "u16", type_len) == 0) {
/* u16 */
value.size = sizeof(u16);
u16 *data = reinterpret_cast<u16 *>(malloc(value.size));
if (data == nullptr) {
return ResultSettingsItemValueAllocationFailed;
}
*data = (u16)(strtoul(value_str, nullptr, 0));
value.data = reinterpret_cast<u8 *>(data);
} else if (strncasecmp(type, "u32", type_len) == 0) {
/* u32 */
value.size = sizeof(u32);
u32 *data = reinterpret_cast<u32 *>(malloc(value.size));
if (data == nullptr) {
return ResultSettingsItemValueAllocationFailed;
}
*data = (u32)(strtoul(value_str, nullptr, 0));
value.data = reinterpret_cast<u8 *>(data);
} else if (strncasecmp(type, "u64", type_len) == 0) {
/* u64 */
value.size = sizeof(u64);
u64 *data = reinterpret_cast<u64 *>(malloc(value.size));
if (data == nullptr) {
return ResultSettingsItemValueAllocationFailed;
}
*data = (u64)(strtoul(value_str, nullptr, 0));
value.data = reinterpret_cast<u8 *>(data);
} else {
return ResultSettingsItemValueInvalidFormat;
}
g_settings_items[kv] = value;
return ResultSuccess();
}
static Result ParseSettingsItemValue(const char *name, const char *key, const char *value) {
/* Validate name and key, then parse value. */
R_TRY(SettingsItemManager::ValidateName(name));
R_TRY(SettingsItemManager::ValidateKey(name));
R_TRY(ParseValue(name, key, value));
return ResultSuccess();
}
static int SettingsItemIniHandler(void *user, const char *name, const char *key, const char *value) {
Result *user_res = reinterpret_cast<Result *>(user);
/* Stop parsing after we fail to parse a value. */
if (R_FAILED(*user_res)) {
return 0;
}
*user_res = ParseSettingsItemValue(name, key, value);
return R_SUCCEEDED(*user_res) ? 1 : 0;
}
static Result LoadConfigurationImpl() {
/* Open file. */
FsFile config_file;
R_TRY(Utils::OpenSdFile("/atmosphere/system_settings.ini", FS_OPEN_READ, &config_file));
ON_SCOPE_EXIT {
fsFileClose(&config_file);
};
/* Allocate buffer. */
std::string config_buf(0xFFFF, '\0');
/* Read from file. */
size_t actual_size;
R_TRY(fsFileRead(&config_file, 0, config_buf.data(), config_buf.size(), FS_READOPTION_NONE, &actual_size));
/* Parse. */
Result parse_res = ResultSuccess;
ini_parse_string(config_buf.c_str(), SettingsItemIniHandler, &parse_res);
return parse_res;
}
void SettingsItemManager::LoadConfiguration() {
const Result load_res = LoadConfigurationImpl();
if (R_FAILED(load_res) && !g_threw_fatal) {
/* Report error if we encountered one. */
g_threw_fatal = true;
g_fatal_thread.Initialize(&FatalThreadFunc, reinterpret_cast<void *>(load_res), 0x1000, 49);
g_fatal_thread.Start();
}
}
Result SettingsItemManager::GetValueSize(const char *name, const char *key, u64 *out_size) {
const std::string kv = std::string(name).append("!").append(key);
auto it = g_settings_items.find(kv);
if (it == g_settings_items.end()) {
return ResultSettingsItemNotFound;
}
*out_size = it->second.size;
return ResultSuccess();
}
Result SettingsItemManager::GetValue(const char *name, const char *key, void *out, size_t max_size, u64 *out_size) {
const std::string kv = std::string(name).append("!").append(key);
auto it = g_settings_items.find(kv);
if (it == g_settings_items.end()) {
return ResultSettingsItemNotFound;
}
size_t copy_size = it->second.size;
if (max_size < copy_size) {
copy_size = max_size;
}
*out_size = copy_size;
memcpy(out, it->second.data, copy_size);
return ResultSuccess();
}

View File

@@ -1,36 +0,0 @@
/*
* 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/>.
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
class SettingsItemManager {
public:
static constexpr size_t MaxNameLength = 64;
static constexpr size_t MaxKeyLength = 64;
public:
static Result ValidateName(const char *name, size_t max_size);
static Result ValidateName(const char *name);
static Result ValidateKey(const char *key, size_t max_size);
static Result ValidateKey(const char *key);
static void LoadConfiguration();
static Result GetValueSize(const char *name, const char *key, u64 *out_size);
static Result GetValue(const char *name, const char *key, void *out, size_t max_size, u64 *out_size);
};

View File

@@ -1,67 +0,0 @@
/*
* 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 <string.h>
#include <switch.h>
#include "setsys_shim.h"
/* Command forwarders. */
Result setsysGetSettingsItemValueFwd(Service *s, const char *name, const char *item_key, void *value_out, size_t value_out_size, u64 *size_out) {
char send_name[SET_MAX_NAME_SIZE];
char send_item_key[SET_MAX_NAME_SIZE];
memset(send_name, 0, SET_MAX_NAME_SIZE);
memset(send_item_key, 0, SET_MAX_NAME_SIZE);
strncpy(send_name, name, SET_MAX_NAME_SIZE-1);
strncpy(send_item_key, item_key, SET_MAX_NAME_SIZE-1);
IpcCommand c;
ipcInitialize(&c);
ipcAddSendStatic(&c, send_name, SET_MAX_NAME_SIZE, 0);
ipcAddSendStatic(&c, send_item_key, SET_MAX_NAME_SIZE, 0);
ipcAddRecvBuffer(&c, value_out, value_out_size, 0);
struct {
u64 magic;
u64 cmd_id;
} *raw;
raw = serviceIpcPrepareHeader(s, &c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 38;
Result rc = serviceIpcDispatch(s);
if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
struct {
u64 magic;
u64 result;
u64 size_out;
} *resp;
serviceIpcParse(s, &r, sizeof(*resp));
resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc)) {
*size_out = resp->size_out;
}
}
return rc;
}

View File

@@ -1,19 +0,0 @@
/**
* @file setsys_shim.h
* @brief System Settings Services (set:sys) IPC wrapper. To be merged into libnx, eventually.
* @author SciresM
* @copyright libnx Authors
*/
#pragma once
#include <switch.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Command forwarders. */
Result setsysGetSettingsItemValueFwd(Service* s, const char *name, const char *item_key, void *value_out, size_t value_out_size, u64 *size_out);
#ifdef __cplusplus
}
#endif