loader/fs.mitm: Support override config changes at runtime

This commit is contained in:
Michael Scire 2018-10-22 22:53:40 -07:00
parent e61bb00d7a
commit f96e1d5074
4 changed files with 34 additions and 33 deletions

View file

@ -34,6 +34,9 @@ static std::atomic_bool g_has_hid_session = false;
static u64 g_override_key_combination = KEY_R; static u64 g_override_key_combination = KEY_R;
static bool g_override_by_default = true; static bool g_override_by_default = true;
/* Static buffer for loader.ini contents at runtime. */
static char g_config_ini_data[0x800];
static bool IsHexadecimal(const char *str) { static bool IsHexadecimal(const char *str) {
while (*str) { while (*str) {
if (isxdigit(*str)) { if (isxdigit(*str)) {
@ -93,12 +96,7 @@ void Utils::InitializeSdThreadFunc(void *args) {
fsDirClose(&titles_dir); fsDirClose(&titles_dir);
} }
FsFile config_file; Utils::RefreshConfiguration();
if (R_SUCCEEDED(fsFsOpenFile(&g_sd_filesystem, "/atmosphere/loader.ini", FS_OPEN_READ, &config_file))) {
Utils::LoadConfiguration(&config_file);
fsFileClose(&config_file);
}
g_has_initialized = true; g_has_initialized = true;
@ -251,6 +249,9 @@ bool Utils::HasOverrideButton(u64 tid) {
return true; return true;
} }
/* Unconditionally refresh loader.ini contents. */
RefreshConfiguration();
u64 kDown = 0; u64 kDown = 0;
bool keys_triggered = (R_SUCCEEDED(GetKeysDown(&kDown)) && ((kDown & g_override_key_combination) != 0)); bool keys_triggered = (R_SUCCEEDED(GetKeysDown(&kDown)) && ((kDown & g_override_key_combination) != 0));
return IsSdInitialized() && (g_override_by_default ^ keys_triggered); return IsSdInitialized() && (g_override_by_default ^ keys_triggered);
@ -314,27 +315,26 @@ static int FsMitMIniHandler(void *user, const char *section, const char *name, c
} }
void Utils::LoadConfiguration(FsFile *f) { void Utils::RefreshConfiguration() {
if (f == NULL) { FsFile config_file;
if (R_FAILED(fsFsOpenFile(&g_sd_filesystem, "/atmosphere/loader.ini", FS_OPEN_READ, &config_file))) {
return; return;
} }
u64 size; u64 size;
if (R_FAILED(fsFileGetSize(f, &size))) { if (R_FAILED(fsFileGetSize(&config_file, &size))) {
return; return;
} }
size = std::min(size, (decltype(size))0x7FF); size = std::min(size, (decltype(size))0x7FF);
char *config_str = new char[0x800]; /* Read in string. */
if (config_str != NULL) { std::fill(g_config_ini_data, g_config_ini_data + 0x800, 0);
/* Read in string. */ size_t r_s;
std::fill(config_str, config_str + 0x800, 0); fsFileRead(&config_file, 0, g_config_ini_data, size, &r_s);
size_t r_s;
fsFileRead(f, 0, config_str, size, &r_s); ini_parse_string(g_config_ini_data, FsMitMIniHandler, NULL);
ini_parse_string(config_str, FsMitMIniHandler, NULL);
fsFileClose(&config_file);
delete[] config_str;
}
} }

View file

@ -45,5 +45,5 @@ class Utils {
static Result GetKeysDown(u64 *keys); static Result GetKeysDown(u64 *keys);
static bool HasOverrideButton(u64 tid); static bool HasOverrideButton(u64 tid);
private: private:
static void LoadConfiguration(FsFile *f); static void RefreshConfiguration();
}; };

View file

@ -40,6 +40,9 @@ static u64 g_override_key_combination = KEY_R;
static bool g_override_by_default = true; static bool g_override_by_default = true;
static u64 g_override_hbl_tid = 0x010000000000100D; static u64 g_override_hbl_tid = 0x010000000000100D;
/* Static buffer for loader.ini contents at runtime. */
static char g_config_ini_data[0x800];
Result ContentManagement::MountCode(u64 tid, FsStorageId sid) { Result ContentManagement::MountCode(u64 tid, FsStorageId sid) {
char path[FS_MAX_PATH] = {0}; char path[FS_MAX_PATH] = {0};
Result rc; Result rc;
@ -49,6 +52,10 @@ Result ContentManagement::MountCode(u64 tid, FsStorageId sid) {
TryMountSdCard(); TryMountSdCard();
} }
if (g_has_initialized_fs_dev) {
RefreshConfigurationData();
}
if (ShouldOverrideContents() && R_SUCCEEDED(MountCodeNspOnSd(tid))) { if (ShouldOverrideContents() && R_SUCCEEDED(MountCodeNspOnSd(tid))) {
return 0x0; return 0x0;
} }
@ -259,23 +266,17 @@ static int LoaderIniHandler(void *user, const char *section, const char *name, c
return 1; return 1;
} }
void ContentManagement::LoadConfiguration(FILE *config) { void ContentManagement::RefreshConfigurationData() {
FILE *config = fopen("sdmc:/atmosphere/loader.ini", "r");
if (config == NULL) { if (config == NULL) {
return; return;
} }
char *config_str = new char[0x800]; std::fill(g_config_ini_data, g_config_ini_data + 0x800, 0);
if (config_str != NULL) { fread(g_config_ini_data, 1, 0x7FF, config);
/* Read in string. */
std::fill(config_str, config_str + 0x800, 0);
fread(config_str, 1, 0x7FF, config);
ini_parse_string(config_str, LoaderIniHandler, NULL);
delete[] config_str;
}
fclose(config); fclose(config);
ini_parse_string(g_config_ini_data, LoaderIniHandler, NULL);
} }
void ContentManagement::TryMountSdCard() { void ContentManagement::TryMountSdCard() {
@ -292,7 +293,6 @@ void ContentManagement::TryMountSdCard() {
} }
if (R_SUCCEEDED(fsdevMountSdmc())) { if (R_SUCCEEDED(fsdevMountSdmc())) {
ContentManagement::LoadConfiguration(fopen("sdmc:/atmosphere/loader.ini", "r"));
g_has_initialized_fs_dev = true; g_has_initialized_fs_dev = true;
} }
} }

View file

@ -34,6 +34,7 @@ class ContentManagement {
static bool HasCreatedTitle(u64 tid); static bool HasCreatedTitle(u64 tid);
static void SetCreatedTitle(u64 tid); static void SetCreatedTitle(u64 tid);
static void RefreshConfigurationData();
static void LoadConfiguration(FILE *config); static void LoadConfiguration(FILE *config);
static void TryMountSdCard(); static void TryMountSdCard();