2021-07-21 16:04:18 +01:00
|
|
|
/*
|
|
|
|
* config.c
|
|
|
|
*
|
2024-04-12 10:47:36 +01:00
|
|
|
* Copyright (c) 2020-2024, DarkMatterCore <pabloacurielz@gmail.com>.
|
2021-07-21 16:04:18 +01:00
|
|
|
*
|
|
|
|
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
|
|
|
|
*
|
|
|
|
* nxdumptool is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* nxdumptool is distributed in the hope that 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-04-30 22:01:42 +01:00
|
|
|
#include <core/nxdt_utils.h>
|
|
|
|
#include <core/config.h>
|
|
|
|
#include <core/title.h>
|
2021-07-21 16:04:18 +01:00
|
|
|
|
2021-08-07 09:42:03 +01:00
|
|
|
#define CONFIG_VALIDATE_FIELD(type, name, ...) \
|
2021-07-21 16:04:18 +01:00
|
|
|
if (!strcmp(key, #name)) { \
|
2021-08-07 09:42:03 +01:00
|
|
|
if (name##_found || !jsonValidate##type(val, ##__VA_ARGS__)) goto end; \
|
2021-07-21 16:04:18 +01:00
|
|
|
name##_found = true; \
|
|
|
|
continue; \
|
|
|
|
}
|
|
|
|
|
2021-08-07 09:42:03 +01:00
|
|
|
#define CONFIG_VALIDATE_OBJECT(type, name) \
|
2021-07-21 16:04:18 +01:00
|
|
|
if (!strcmp(key, #name)) { \
|
|
|
|
if (name##_found || !configValidateJson##type##Object(val)) goto end; \
|
|
|
|
name##_found = true; \
|
|
|
|
continue; \
|
|
|
|
}
|
|
|
|
|
2021-08-07 09:42:03 +01:00
|
|
|
#define CONFIG_GETTER(functype, vartype, ...) \
|
2021-07-21 16:04:18 +01:00
|
|
|
vartype configGet##functype(const char *path) { \
|
|
|
|
vartype ret = (vartype)0; \
|
|
|
|
SCOPED_LOCK(&g_configMutex) { \
|
2021-07-25 23:23:44 +01:00
|
|
|
if (!g_configInterfaceInit) break; \
|
2021-08-07 09:42:03 +01:00
|
|
|
ret = jsonGet##functype(g_configJson, path); \
|
2021-07-21 16:04:18 +01:00
|
|
|
} \
|
|
|
|
return ret; \
|
|
|
|
}
|
|
|
|
|
2021-08-07 09:42:03 +01:00
|
|
|
#define CONFIG_SETTER(functype, vartype, ...) \
|
2021-07-21 16:04:18 +01:00
|
|
|
void configSet##functype(const char *path, vartype value) { \
|
|
|
|
SCOPED_LOCK(&g_configMutex) { \
|
2021-07-25 23:23:44 +01:00
|
|
|
if (!g_configInterfaceInit) break; \
|
2021-08-07 09:42:03 +01:00
|
|
|
if (jsonSet##functype(g_configJson, path, value)) configWriteConfigJson(); \
|
2021-07-21 16:04:18 +01:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Global variables. */
|
|
|
|
|
|
|
|
static Mutex g_configMutex = 0;
|
|
|
|
static bool g_configInterfaceInit = false;
|
|
|
|
|
2023-05-24 20:05:34 +01:00
|
|
|
static char g_configJsonPath[FS_MAX_PATH] = {0};
|
2021-07-21 16:04:18 +01:00
|
|
|
static struct json_object *g_configJson = NULL;
|
|
|
|
|
|
|
|
/* Function prototypes. */
|
|
|
|
|
|
|
|
static bool configParseConfigJson(void);
|
2023-11-26 21:54:25 +00:00
|
|
|
static bool configResetConfigJson(void);
|
2021-07-21 16:04:18 +01:00
|
|
|
static void configWriteConfigJson(void);
|
|
|
|
static void configFreeConfigJson(void);
|
|
|
|
|
|
|
|
static bool configValidateJsonRootObject(const struct json_object *obj);
|
|
|
|
static bool configValidateJsonGameCardObject(const struct json_object *obj);
|
|
|
|
static bool configValidateJsonNspObject(const struct json_object *obj);
|
|
|
|
static bool configValidateJsonTicketObject(const struct json_object *obj);
|
|
|
|
static bool configValidateJsonNcaFsObject(const struct json_object *obj);
|
|
|
|
|
|
|
|
bool configInitialize(void)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
SCOPED_LOCK(&g_configMutex)
|
|
|
|
{
|
|
|
|
ret = g_configInterfaceInit;
|
|
|
|
if (ret) break;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
/* Parse JSON config. */
|
|
|
|
if (!configParseConfigJson())
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Failed to parse JSON configuration!");
|
2021-07-21 16:04:18 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
/* Update flags. */
|
|
|
|
ret = g_configInterfaceInit = true;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void configExit(void)
|
|
|
|
{
|
|
|
|
SCOPED_LOCK(&g_configMutex)
|
|
|
|
{
|
|
|
|
/* Free JSON object. */
|
|
|
|
/* We don't need to write it back to the SD card - setter functions do that on their own. */
|
|
|
|
configFreeConfigJson();
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
/* Update flag. */
|
|
|
|
g_configInterfaceInit = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-26 21:54:25 +00:00
|
|
|
void configResetSettings(void)
|
|
|
|
{
|
|
|
|
configResetConfigJson();
|
|
|
|
}
|
|
|
|
|
2021-08-07 09:42:03 +01:00
|
|
|
CONFIG_GETTER(Boolean, bool);
|
|
|
|
CONFIG_SETTER(Boolean, bool);
|
2021-07-21 16:04:18 +01:00
|
|
|
|
2021-08-07 09:42:03 +01:00
|
|
|
CONFIG_GETTER(Integer, int);
|
|
|
|
CONFIG_SETTER(Integer, int);
|
2021-07-21 16:04:18 +01:00
|
|
|
|
|
|
|
static bool configParseConfigJson(void)
|
|
|
|
{
|
2023-05-24 20:05:34 +01:00
|
|
|
bool use_default_config = true, use_root = true, ret = false;
|
|
|
|
const char *launch_path = utilsGetLaunchPath();
|
|
|
|
char *ptr1 = NULL, *ptr2 = NULL;
|
|
|
|
|
|
|
|
/* Generate config JSON path. */
|
|
|
|
if (launch_path)
|
|
|
|
{
|
|
|
|
ptr1 = strchr(launch_path, '/');
|
|
|
|
ptr2 = strrchr(launch_path, '/');
|
|
|
|
|
|
|
|
if (ptr1 && ptr2 && ptr1 != ptr2)
|
|
|
|
{
|
|
|
|
/* Use config JSON from the current working directory. */
|
|
|
|
snprintf(g_configJsonPath, sizeof(g_configJsonPath), "%.*s" CONFIG_FILE_NAME, (int)((ptr2 - launch_path) + 1), launch_path);
|
|
|
|
use_root = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use config JSON from the SD card root directory. */
|
|
|
|
if (use_root) sprintf(g_configJsonPath, DEVOPTAB_SDMC_DEVICE "/" CONFIG_FILE_NAME);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
/* Read config JSON. */
|
2023-05-24 20:05:34 +01:00
|
|
|
g_configJson = json_object_from_file(g_configJsonPath);
|
2022-07-12 17:34:49 +01:00
|
|
|
if (g_configJson)
|
2021-07-21 16:04:18 +01:00
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
/* Validate configuration. */
|
|
|
|
ret = configValidateJsonRootObject(g_configJson);
|
|
|
|
use_default_config = !ret;
|
|
|
|
} else {
|
2021-08-07 09:42:03 +01:00
|
|
|
jsonLogLastError();
|
2021-07-21 16:04:18 +01:00
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-11-26 21:54:25 +00:00
|
|
|
/* Try to load the default settings. */
|
|
|
|
if (use_default_config) ret = configResetConfigJson();
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-11-26 21:54:25 +00:00
|
|
|
if (!ret) LOG_MSG_ERROR("Failed to parse both current and default JSON configuration files!");
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-11-26 21:54:25 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-11-26 21:54:25 +00:00
|
|
|
static bool configResetConfigJson(void)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
LOG_MSG_INFO("Loading default configuration.");
|
|
|
|
|
|
|
|
/* Free config JSON. */
|
|
|
|
configFreeConfigJson();
|
|
|
|
|
|
|
|
/* Read default config JSON. */
|
|
|
|
g_configJson = json_object_from_file(DEFAULT_CONFIG_PATH);
|
|
|
|
if (g_configJson)
|
|
|
|
{
|
|
|
|
configWriteConfigJson();
|
|
|
|
ret = true;
|
|
|
|
} else {
|
|
|
|
jsonLogLastError();
|
|
|
|
}
|
2022-07-12 17:34:49 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void configWriteConfigJson(void)
|
|
|
|
{
|
|
|
|
if (!g_configJson) return;
|
2023-05-24 20:05:34 +01:00
|
|
|
if (json_object_to_file_ext(g_configJsonPath, g_configJson, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY) != 0) jsonLogLastError();
|
2021-07-21 16:04:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void configFreeConfigJson(void)
|
|
|
|
{
|
|
|
|
if (!g_configJson) return;
|
|
|
|
json_object_put(g_configJson);
|
|
|
|
g_configJson = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool configValidateJsonRootObject(const struct json_object *obj)
|
|
|
|
{
|
2022-09-12 19:19:10 +01:00
|
|
|
bool ret = false, overclock_found = false, naming_convention_found = false, output_storage_found = false, gamecard_found = false;
|
2021-07-21 16:04:18 +01:00
|
|
|
bool nsp_found = false, ticket_found = false, nca_fs_found = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-08-07 09:42:03 +01:00
|
|
|
if (!jsonValidateObject(obj)) goto end;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
json_object_object_foreach(obj, key, val)
|
|
|
|
{
|
2021-08-07 09:42:03 +01:00
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, overclock);
|
|
|
|
CONFIG_VALIDATE_FIELD(Integer, naming_convention, TitleNamingConvention_Full, TitleNamingConvention_Count - 1);
|
2022-09-12 19:19:10 +01:00
|
|
|
CONFIG_VALIDATE_FIELD(Integer, output_storage, ConfigOutputStorage_SdCard, ConfigOutputStorage_Count - 1);
|
2021-08-07 09:42:03 +01:00
|
|
|
CONFIG_VALIDATE_OBJECT(GameCard, gamecard);
|
|
|
|
CONFIG_VALIDATE_OBJECT(Nsp, nsp);
|
|
|
|
CONFIG_VALIDATE_OBJECT(Ticket, ticket);
|
|
|
|
CONFIG_VALIDATE_OBJECT(NcaFs, nca_fs);
|
2021-07-21 16:04:18 +01:00
|
|
|
goto end;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2022-09-12 19:19:10 +01:00
|
|
|
ret = (overclock_found && naming_convention_found && output_storage_found && gamecard_found && nsp_found && ticket_found && nca_fs_found);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
end:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool configValidateJsonGameCardObject(const struct json_object *obj)
|
|
|
|
{
|
2023-05-24 20:05:34 +01:00
|
|
|
bool ret = false, prepend_key_area_found = false, keep_certificate_found = false, trim_dump_found = false, calculate_checksum_found = false;
|
2024-05-02 14:38:39 +01:00
|
|
|
bool lookup_checksum_found = false, write_raw_hfs_partition_found = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-08-07 09:42:03 +01:00
|
|
|
if (!jsonValidateObject(obj)) goto end;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
json_object_object_foreach(obj, key, val)
|
|
|
|
{
|
2022-09-12 19:19:10 +01:00
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, prepend_key_area);
|
2021-08-07 09:42:03 +01:00
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, keep_certificate);
|
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, trim_dump);
|
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, calculate_checksum);
|
2024-05-02 14:38:39 +01:00
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, lookup_checksum);
|
2023-05-24 20:05:34 +01:00
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, write_raw_hfs_partition);
|
2021-07-21 16:04:18 +01:00
|
|
|
goto end;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2024-05-02 14:38:39 +01:00
|
|
|
ret = (prepend_key_area_found && keep_certificate_found && trim_dump_found && calculate_checksum_found && lookup_checksum_found && write_raw_hfs_partition_found);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
end:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool configValidateJsonNspObject(const struct json_object *obj)
|
|
|
|
{
|
2022-06-26 02:34:31 +01:00
|
|
|
bool ret = false, set_download_distribution_found = false, remove_console_data_found = false, remove_titlekey_crypto_found = false;
|
2023-05-24 20:05:34 +01:00
|
|
|
bool disable_linked_account_requirement_found = false, enable_screenshots_found = false, enable_video_capture_found = false, disable_hdcp_found = false;
|
2023-07-17 00:03:05 +01:00
|
|
|
bool generate_authoringtool_data_found = false, lookup_checksum_found = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-08-07 09:42:03 +01:00
|
|
|
if (!jsonValidateObject(obj)) goto end;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
json_object_object_foreach(obj, key, val)
|
|
|
|
{
|
2021-08-07 09:42:03 +01:00
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, set_download_distribution);
|
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, remove_console_data);
|
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, remove_titlekey_crypto);
|
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, disable_linked_account_requirement);
|
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, enable_screenshots);
|
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, enable_video_capture);
|
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, disable_hdcp);
|
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, lookup_checksum);
|
2023-07-17 00:03:05 +01:00
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, generate_authoringtool_data);
|
2021-07-21 16:04:18 +01:00
|
|
|
goto end;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2022-06-26 02:34:31 +01:00
|
|
|
ret = (set_download_distribution_found && remove_console_data_found && remove_titlekey_crypto_found && disable_linked_account_requirement_found && \
|
2023-07-17 00:03:05 +01:00
|
|
|
enable_screenshots_found && enable_video_capture_found && disable_hdcp_found && generate_authoringtool_data_found && lookup_checksum_found);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
end:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool configValidateJsonTicketObject(const struct json_object *obj)
|
|
|
|
{
|
|
|
|
bool ret = false, remove_console_data_found = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-08-07 09:42:03 +01:00
|
|
|
if (!jsonValidateObject(obj)) goto end;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
json_object_object_foreach(obj, key, val)
|
|
|
|
{
|
2021-08-07 09:42:03 +01:00
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, remove_console_data);
|
2021-07-21 16:04:18 +01:00
|
|
|
goto end;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
ret = remove_console_data_found;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
end:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool configValidateJsonNcaFsObject(const struct json_object *obj)
|
|
|
|
{
|
2023-05-30 00:22:12 +01:00
|
|
|
bool ret = false, write_raw_section_found = false, use_layeredfs_dir_found = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-08-07 09:42:03 +01:00
|
|
|
if (!jsonValidateObject(obj)) goto end;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
json_object_object_foreach(obj, key, val)
|
|
|
|
{
|
2023-05-30 00:22:12 +01:00
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, write_raw_section);
|
2021-08-07 09:42:03 +01:00
|
|
|
CONFIG_VALIDATE_FIELD(Boolean, use_layeredfs_dir);
|
2021-07-21 16:04:18 +01:00
|
|
|
goto end;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2023-05-30 00:22:12 +01:00
|
|
|
ret = (write_raw_section_found && use_layeredfs_dir_found);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-21 16:04:18 +01:00
|
|
|
end:
|
|
|
|
return ret;
|
|
|
|
}
|