2018-09-07 16:00:13 +01:00
|
|
|
/*
|
2019-04-08 03:00:49 +01:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2018-09-07 16:00:13 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-06-10 18:11:05 +01:00
|
|
|
#include <switch.h>
|
|
|
|
#include <stratosphere.hpp>
|
2019-08-06 03:34:59 +01:00
|
|
|
#include <stratosphere/spl.hpp>
|
2018-06-10 18:11:05 +01:00
|
|
|
#include <atomic>
|
2018-07-28 20:29:54 +01:00
|
|
|
#include <algorithm>
|
2018-10-17 03:01:41 +01:00
|
|
|
#include <strings.h>
|
2018-06-10 18:11:05 +01:00
|
|
|
|
2018-06-15 00:50:01 +01:00
|
|
|
#include "debug.hpp"
|
2019-01-29 10:29:29 +00:00
|
|
|
#include "utils.hpp"
|
2018-10-17 03:01:41 +01:00
|
|
|
#include "ini.h"
|
2018-06-10 18:11:05 +01:00
|
|
|
|
2019-02-05 09:51:05 +00:00
|
|
|
#include "set_mitm/setsys_settings_items.hpp"
|
2019-04-12 23:28:46 +01:00
|
|
|
#include "bpc_mitm/bpcmitm_reboot_manager.hpp"
|
2019-02-05 09:51:05 +00:00
|
|
|
|
2018-06-15 00:50:01 +01:00
|
|
|
static FsFileSystem g_sd_filesystem = {0};
|
2019-09-24 11:15:36 +01:00
|
|
|
|
|
|
|
/* Non-autoclear events for SD/HID init. */
|
2019-10-24 10:30:10 +01:00
|
|
|
static ams::os::Event g_sd_event(false), g_hid_event(false);
|
2019-01-29 10:29:29 +00:00
|
|
|
|
2018-07-28 20:29:54 +01:00
|
|
|
static std::vector<u64> g_mitm_flagged_tids;
|
2018-10-17 04:41:19 +01:00
|
|
|
static std::vector<u64> g_disable_mitm_flagged_tids;
|
2018-07-28 20:29:54 +01:00
|
|
|
static std::atomic_bool g_has_initialized = false;
|
2018-10-17 03:01:41 +01:00
|
|
|
static std::atomic_bool g_has_hid_session = false;
|
|
|
|
|
2019-02-23 15:39:40 +00:00
|
|
|
/* Content override support variables/types */
|
|
|
|
static OverrideKey g_default_override_key = {
|
2019-06-26 23:46:19 +01:00
|
|
|
.key_combination = KEY_L,
|
2019-02-23 15:39:40 +00:00
|
|
|
.override_by_default = true
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HblOverrideConfig {
|
|
|
|
OverrideKey override_key;
|
2019-11-21 07:58:18 +00:00
|
|
|
u64 program_id;
|
2019-02-23 15:39:40 +00:00
|
|
|
bool override_any_app;
|
|
|
|
};
|
|
|
|
|
|
|
|
static HblOverrideConfig g_hbl_override_config = {
|
|
|
|
.override_key = {
|
2019-06-26 23:46:19 +01:00
|
|
|
.key_combination = KEY_R,
|
|
|
|
.override_by_default = false
|
2019-02-23 15:39:40 +00:00
|
|
|
},
|
2019-11-21 07:58:18 +00:00
|
|
|
.program_id = static_cast<u64>(ams::ncm::ProgramId::AppletPhotoViewer),
|
2019-06-26 23:46:19 +01:00
|
|
|
.override_any_app = true
|
2019-02-23 15:39:40 +00:00
|
|
|
};
|
2018-06-10 18:11:05 +01:00
|
|
|
|
2018-10-23 06:53:40 +01:00
|
|
|
/* Static buffer for loader.ini contents at runtime. */
|
|
|
|
static char g_config_ini_data[0x800];
|
|
|
|
|
2018-11-15 23:27:01 +00:00
|
|
|
/* Backup file for CAL0 partition. */
|
|
|
|
static constexpr size_t ProdinfoSize = 0x8000;
|
2019-08-06 03:34:59 +01:00
|
|
|
static FsFile g_cal0_file = {};
|
|
|
|
static FsFile g_bis_key_file = {};
|
2018-11-16 01:23:05 +00:00
|
|
|
static u8 g_cal0_storage_backup[ProdinfoSize];
|
2018-11-15 23:27:01 +00:00
|
|
|
static u8 g_cal0_backup[ProdinfoSize];
|
|
|
|
|
2019-06-15 05:20:53 +01:00
|
|
|
/* Emummc-related file. */
|
|
|
|
static FsFile g_emummc_file = {0};
|
|
|
|
|
2018-07-28 20:29:54 +01:00
|
|
|
static bool IsHexadecimal(const char *str) {
|
|
|
|
while (*str) {
|
2019-06-30 18:48:16 +01:00
|
|
|
if (isxdigit((unsigned char)*str)) {
|
2018-07-30 01:27:30 +01:00
|
|
|
str++;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-10 18:11:05 +01:00
|
|
|
}
|
2018-07-28 20:29:54 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-29 10:29:29 +00:00
|
|
|
void Utils::InitializeThreadFunc(void *args) {
|
2018-07-28 20:29:54 +01:00
|
|
|
/* Get required services. */
|
2019-04-22 20:40:53 +01:00
|
|
|
DoWithSmSession([&]() {
|
|
|
|
Handle tmp_hnd = 0;
|
2019-09-12 09:18:58 +01:00
|
|
|
static const char * const required_active_services[] = {"pcv", "gpio", "pinmux", "psc:m"};
|
2019-10-24 10:30:10 +01:00
|
|
|
for (unsigned int i = 0; i < ams::util::size(required_active_services); i++) {
|
2019-06-20 12:04:33 +01:00
|
|
|
R_ASSERT(smGetServiceOriginal(&tmp_hnd, smEncodeName(required_active_services[i])));
|
|
|
|
svcCloseHandle(tmp_hnd);
|
2018-06-12 23:00:09 +01:00
|
|
|
}
|
2019-04-22 20:40:53 +01:00
|
|
|
});
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-07-28 20:29:54 +01:00
|
|
|
/* Mount SD. */
|
|
|
|
while (R_FAILED(fsMountSdcard(&g_sd_filesystem))) {
|
2018-12-02 03:07:25 +00:00
|
|
|
svcSleepThread(1000000ULL);
|
2018-06-10 18:11:05 +01:00
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-11-15 12:44:13 +00:00
|
|
|
/* Back up CAL0, if it's not backed up already. */
|
|
|
|
fsFsCreateDirectory(&g_sd_filesystem, "/atmosphere/automatic_backups");
|
|
|
|
{
|
|
|
|
FsStorage cal0_storage;
|
2019-06-20 12:04:33 +01:00
|
|
|
R_ASSERT(fsOpenBisStorage(&cal0_storage, FsBisStorageId_CalibrationBinary));
|
|
|
|
R_ASSERT(fsStorageRead(&cal0_storage, 0, g_cal0_storage_backup, ProdinfoSize));
|
2018-11-16 01:23:05 +00:00
|
|
|
fsStorageClose(&cal0_storage);
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-11-15 12:52:55 +00:00
|
|
|
char serial_number[0x40] = {0};
|
2018-11-16 01:23:05 +00:00
|
|
|
memcpy(serial_number, g_cal0_storage_backup + 0x250, 0x18);
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-08-06 03:34:59 +01:00
|
|
|
/* Automatically backup PRODINFO. */
|
|
|
|
{
|
|
|
|
char prodinfo_backup_path[FS_MAX_PATH] = {0};
|
|
|
|
if (strlen(serial_number) > 0) {
|
|
|
|
snprintf(prodinfo_backup_path, sizeof(prodinfo_backup_path) - 1, "/atmosphere/automatic_backups/%s_PRODINFO.bin", serial_number);
|
|
|
|
} else {
|
|
|
|
snprintf(prodinfo_backup_path, sizeof(prodinfo_backup_path) - 1, "/atmosphere/automatic_backups/PRODINFO.bin");
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-08-06 03:34:59 +01:00
|
|
|
fsFsCreateFile(&g_sd_filesystem, prodinfo_backup_path, ProdinfoSize, 0);
|
|
|
|
if (R_SUCCEEDED(fsFsOpenFile(&g_sd_filesystem, prodinfo_backup_path, FS_OPEN_READ | FS_OPEN_WRITE, &g_cal0_file))) {
|
|
|
|
bool has_auto_backup = false;
|
|
|
|
size_t read = 0;
|
|
|
|
if (R_SUCCEEDED(fsFileRead(&g_cal0_file, 0, g_cal0_backup, sizeof(g_cal0_backup), FS_READOPTION_NONE, &read)) && read == sizeof(g_cal0_backup)) {
|
|
|
|
bool is_cal0_valid = true;
|
|
|
|
is_cal0_valid &= memcmp(g_cal0_backup, "CAL0", 4) == 0;
|
|
|
|
is_cal0_valid &= memcmp(g_cal0_backup + 0x250, serial_number, 0x18) == 0;
|
|
|
|
u32 cal0_size = ((u32 *)g_cal0_backup)[2];
|
|
|
|
is_cal0_valid &= cal0_size + 0x40 <= ProdinfoSize;
|
|
|
|
if (is_cal0_valid) {
|
|
|
|
u8 calc_hash[0x20];
|
|
|
|
sha256CalculateHash(calc_hash, g_cal0_backup + 0x40, cal0_size);
|
|
|
|
is_cal0_valid &= memcmp(calc_hash, g_cal0_backup + 0x20, sizeof(calc_hash)) == 0;
|
|
|
|
}
|
|
|
|
has_auto_backup = is_cal0_valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_auto_backup) {
|
|
|
|
fsFileSetSize(&g_cal0_file, ProdinfoSize);
|
|
|
|
fsFileWrite(&g_cal0_file, 0, g_cal0_storage_backup, ProdinfoSize, FS_WRITEOPTION_FLUSH);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: g_cal0_file is intentionally not closed here. This prevents any other process from opening it. */
|
|
|
|
memset(g_cal0_storage_backup, 0, sizeof(g_cal0_storage_backup));
|
|
|
|
memset(g_cal0_backup, 0, sizeof(g_cal0_backup));
|
|
|
|
}
|
2018-11-15 12:52:55 +00:00
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-08-06 03:34:59 +01:00
|
|
|
/* Automatically backup BIS keys. */
|
|
|
|
{
|
2019-08-29 08:14:23 +01:00
|
|
|
u64 key_generation = 0;
|
|
|
|
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_500) {
|
|
|
|
R_ASSERT(splGetConfig(SplConfigItem_NewKeyGeneration, &key_generation));
|
|
|
|
}
|
|
|
|
|
2019-08-06 03:34:59 +01:00
|
|
|
static constexpr u8 const BisKeySources[4][2][0x10] = {
|
|
|
|
{
|
|
|
|
{0xF8, 0x3F, 0x38, 0x6E, 0x2C, 0xD2, 0xCA, 0x32, 0xA8, 0x9A, 0xB9, 0xAA, 0x29, 0xBF, 0xC7, 0x48},
|
|
|
|
{0x7D, 0x92, 0xB0, 0x3A, 0xA8, 0xBF, 0xDE, 0xE1, 0xA7, 0x4C, 0x3B, 0x6E, 0x35, 0xCB, 0x71, 0x06}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{0x41, 0x00, 0x30, 0x49, 0xDD, 0xCC, 0xC0, 0x65, 0x64, 0x7A, 0x7E, 0xB4, 0x1E, 0xED, 0x9C, 0x5F},
|
|
|
|
{0x44, 0x42, 0x4E, 0xDA, 0xB4, 0x9D, 0xFC, 0xD9, 0x87, 0x77, 0x24, 0x9A, 0xDC, 0x9F, 0x7C, 0xA4}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{0x52, 0xC2, 0xE9, 0xEB, 0x09, 0xE3, 0xEE, 0x29, 0x32, 0xA1, 0x0C, 0x1F, 0xB6, 0xA0, 0x92, 0x6C},
|
|
|
|
{0x4D, 0x12, 0xE1, 0x4B, 0x2A, 0x47, 0x4C, 0x1C, 0x09, 0xCB, 0x03, 0x59, 0xF0, 0x15, 0xF4, 0xE4}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{0x52, 0xC2, 0xE9, 0xEB, 0x09, 0xE3, 0xEE, 0x29, 0x32, 0xA1, 0x0C, 0x1F, 0xB6, 0xA0, 0x92, 0x6C},
|
|
|
|
{0x4D, 0x12, 0xE1, 0x4B, 0x2A, 0x47, 0x4C, 0x1C, 0x09, 0xCB, 0x03, 0x59, 0xF0, 0x15, 0xF4, 0xE4}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
u8 bis_keys[4][2][0x10] = {};
|
|
|
|
|
|
|
|
/* TODO: Clean this up in ams_mitm refactor. */
|
|
|
|
for (size_t partition = 0; partition < 4; partition++) {
|
|
|
|
if (partition == 0) {
|
|
|
|
for (size_t i = 0; i < 2; i++) {
|
2019-08-29 08:14:23 +01:00
|
|
|
R_ASSERT(splFsGenerateSpecificAesKey(BisKeySources[partition][i], key_generation, i, bis_keys[partition][i]));
|
2019-08-06 03:34:59 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
static constexpr u8 const BisKekSource[0x10] = {
|
|
|
|
0x34, 0xC1, 0xA0, 0xC4, 0x82, 0x58, 0xF8, 0xB4, 0xFA, 0x9E, 0x5E, 0x6A, 0xDA, 0xFC, 0x7E, 0x4F,
|
|
|
|
};
|
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
const u32 option = (partition == 3 && ams::spl::IsRecoveryBoot()) ? 0x4 : 0x1;
|
2019-08-06 03:34:59 +01:00
|
|
|
|
|
|
|
u8 access_key[0x10];
|
2019-08-29 08:14:23 +01:00
|
|
|
R_ASSERT(splCryptoGenerateAesKek(BisKekSource, key_generation, option, access_key));
|
2019-08-06 03:34:59 +01:00
|
|
|
for (size_t i = 0; i < 2; i++) {
|
|
|
|
R_ASSERT(splCryptoGenerateAesKey(access_key, BisKeySources[partition][i], bis_keys[partition][i]));
|
|
|
|
}
|
2018-11-15 23:27:01 +00:00
|
|
|
}
|
2018-11-15 12:44:13 +00:00
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-08-06 03:34:59 +01:00
|
|
|
char bis_key_backup_path[FS_MAX_PATH] = {0};
|
|
|
|
if (strlen(serial_number) > 0) {
|
|
|
|
snprintf(bis_key_backup_path, sizeof(bis_key_backup_path) - 1, "/atmosphere/automatic_backups/%s_BISKEYS.bin", serial_number);
|
|
|
|
} else {
|
|
|
|
snprintf(bis_key_backup_path, sizeof(bis_key_backup_path) - 1, "/atmosphere/automatic_backups/BISKEYS.bin");
|
2018-11-15 12:44:13 +00:00
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-08-06 03:34:59 +01:00
|
|
|
fsFsCreateFile(&g_sd_filesystem, bis_key_backup_path, sizeof(bis_keys), 0);
|
|
|
|
if (R_SUCCEEDED(fsFsOpenFile(&g_sd_filesystem, bis_key_backup_path, FS_OPEN_READ | FS_OPEN_WRITE, &g_bis_key_file))) {
|
|
|
|
fsFileSetSize(&g_bis_key_file, sizeof(bis_keys));
|
|
|
|
fsFileWrite(&g_bis_key_file, 0, bis_keys, sizeof(bis_keys), FS_WRITEOPTION_FLUSH);
|
|
|
|
/* NOTE: g_bis_key_file is intentionally not closed here. This prevents any other process from opening it. */
|
|
|
|
}
|
2018-11-15 12:44:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-07-28 20:29:54 +01:00
|
|
|
/* Check for MitM flags. */
|
|
|
|
FsDir titles_dir;
|
|
|
|
if (R_SUCCEEDED(fsFsOpenDirectory(&g_sd_filesystem, "/atmosphere/titles", FS_DIROPEN_DIRECTORY, &titles_dir))) {
|
|
|
|
FsDirectoryEntry dir_entry;
|
|
|
|
FsFile f;
|
|
|
|
u64 read_entries;
|
|
|
|
while (R_SUCCEEDED((fsDirRead(&titles_dir, 0, &read_entries, 1, &dir_entry))) && read_entries == 1) {
|
|
|
|
if (strlen(dir_entry.name) == 0x10 && IsHexadecimal(dir_entry.name)) {
|
2019-11-21 07:58:18 +00:00
|
|
|
u64 program_id = strtoul(dir_entry.name, NULL, 16);
|
2018-07-28 20:29:54 +01:00
|
|
|
char title_path[FS_MAX_PATH] = {0};
|
2018-07-29 19:21:42 +01:00
|
|
|
strcpy(title_path, "/atmosphere/titles/");
|
2018-07-28 20:29:54 +01:00
|
|
|
strcat(title_path, dir_entry.name);
|
2018-11-15 12:26:40 +00:00
|
|
|
strcat(title_path, "/flags/fsmitm.flag");
|
2018-07-28 20:29:54 +01:00
|
|
|
if (R_SUCCEEDED(fsFsOpenFile(&g_sd_filesystem, title_path, FS_OPEN_READ, &f))) {
|
2019-11-21 07:58:18 +00:00
|
|
|
g_mitm_flagged_tids.push_back(program_id);
|
2018-07-28 20:29:54 +01:00
|
|
|
fsFileClose(&f);
|
2018-11-15 12:26:40 +00:00
|
|
|
} else {
|
|
|
|
/* TODO: Deprecate. */
|
|
|
|
memset(title_path, 0, sizeof(title_path));
|
|
|
|
strcpy(title_path, "/atmosphere/titles/");
|
|
|
|
strcat(title_path, dir_entry.name);
|
|
|
|
strcat(title_path, "/fsmitm.flag");
|
|
|
|
if (R_SUCCEEDED(fsFsOpenFile(&g_sd_filesystem, title_path, FS_OPEN_READ, &f))) {
|
2019-11-21 07:58:18 +00:00
|
|
|
g_mitm_flagged_tids.push_back(program_id);
|
2018-11-15 12:26:40 +00:00
|
|
|
fsFileClose(&f);
|
|
|
|
}
|
2018-07-28 20:29:54 +01:00
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-10-17 04:41:19 +01:00
|
|
|
memset(title_path, 0, sizeof(title_path));
|
|
|
|
strcpy(title_path, "/atmosphere/titles/");
|
|
|
|
strcat(title_path, dir_entry.name);
|
2018-11-15 12:26:40 +00:00
|
|
|
strcat(title_path, "/flags/fsmitm_disable.flag");
|
2018-10-17 04:41:19 +01:00
|
|
|
if (R_SUCCEEDED(fsFsOpenFile(&g_sd_filesystem, title_path, FS_OPEN_READ, &f))) {
|
2019-11-21 07:58:18 +00:00
|
|
|
g_disable_mitm_flagged_tids.push_back(program_id);
|
2018-10-17 04:41:19 +01:00
|
|
|
fsFileClose(&f);
|
2018-11-15 12:26:40 +00:00
|
|
|
} else {
|
|
|
|
/* TODO: Deprecate. */
|
|
|
|
memset(title_path, 0, sizeof(title_path));
|
|
|
|
strcpy(title_path, "/atmosphere/titles/");
|
|
|
|
strcat(title_path, dir_entry.name);
|
|
|
|
strcat(title_path, "/fsmitm_disable.flag");
|
|
|
|
if (R_SUCCEEDED(fsFsOpenFile(&g_sd_filesystem, title_path, FS_OPEN_READ, &f))) {
|
2019-11-21 07:58:18 +00:00
|
|
|
g_disable_mitm_flagged_tids.push_back(program_id);
|
2018-11-15 12:26:40 +00:00
|
|
|
fsFileClose(&f);
|
|
|
|
}
|
2018-10-17 04:41:19 +01:00
|
|
|
}
|
2018-07-28 20:29:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fsDirClose(&titles_dir);
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-10-23 06:53:40 +01:00
|
|
|
Utils::RefreshConfiguration();
|
2019-06-20 12:04:33 +01:00
|
|
|
|
2019-06-15 05:20:53 +01:00
|
|
|
/* If we're emummc, persist a write handle to prevent other processes from touching the image. */
|
|
|
|
if (IsEmummc()) {
|
|
|
|
const char *emummc_file_path = GetEmummcFilePath();
|
|
|
|
if (emummc_file_path != nullptr) {
|
2019-06-15 15:57:50 +01:00
|
|
|
char emummc_path[0x100] = {0};
|
|
|
|
std::strncpy(emummc_path, emummc_file_path, 0x80);
|
|
|
|
std::strcat(emummc_path, "/eMMC");
|
2019-06-15 05:20:53 +01:00
|
|
|
fsFsOpenFile(&g_sd_filesystem, emummc_file_path, FS_OPEN_READ | FS_OPEN_WRITE, &g_emummc_file);
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-01-29 10:29:29 +00:00
|
|
|
/* Initialize set:sys. */
|
2019-04-22 20:40:53 +01:00
|
|
|
DoWithSmSession([&]() {
|
2019-06-20 12:04:33 +01:00
|
|
|
R_ASSERT(setsysInitialize());
|
2019-04-22 20:40:53 +01:00
|
|
|
});
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-01-29 10:29:29 +00:00
|
|
|
/* Signal SD is initialized. */
|
2018-07-28 20:29:54 +01:00
|
|
|
g_has_initialized = true;
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-02-05 09:51:05 +00:00
|
|
|
/* Load custom settings configuration. */
|
|
|
|
SettingsItemManager::LoadConfiguration();
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-02-05 09:51:05 +00:00
|
|
|
/* Signal to waiters that we are ready. */
|
2019-09-24 11:15:36 +01:00
|
|
|
g_sd_event.Signal();
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-01-29 10:29:29 +00:00
|
|
|
/* Initialize HID. */
|
2019-04-22 20:40:53 +01:00
|
|
|
while (!g_has_hid_session) {
|
|
|
|
DoWithSmSession([&]() {
|
|
|
|
if (R_SUCCEEDED(hidInitialize())) {
|
|
|
|
g_has_hid_session = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!g_has_hid_session) {
|
2019-01-29 10:29:29 +00:00
|
|
|
svcSleepThread(1000000ULL);
|
|
|
|
}
|
2018-10-17 03:01:41 +01:00
|
|
|
}
|
2019-09-12 17:01:36 +01:00
|
|
|
|
|
|
|
/* Signal to waiters that we are ready. */
|
2019-09-24 11:15:36 +01:00
|
|
|
g_hid_event.Signal();
|
2018-10-17 03:01:41 +01:00
|
|
|
}
|
|
|
|
|
2018-06-15 00:50:01 +01:00
|
|
|
bool Utils::IsSdInitialized() {
|
2018-07-28 20:29:54 +01:00
|
|
|
return g_has_initialized;
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
|
|
|
|
2019-01-29 10:29:29 +00:00
|
|
|
void Utils::WaitSdInitialized() {
|
2019-09-24 11:15:36 +01:00
|
|
|
g_sd_event.Wait();
|
2019-01-29 10:29:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 03:30:59 +00:00
|
|
|
bool Utils::IsHidAvailable() {
|
2018-10-17 03:01:41 +01:00
|
|
|
return g_has_hid_session;
|
|
|
|
}
|
|
|
|
|
2019-09-12 17:01:36 +01:00
|
|
|
void Utils::WaitHidAvailable() {
|
2019-09-24 11:15:36 +01:00
|
|
|
g_hid_event.Wait();
|
2019-09-12 17:01:36 +01:00
|
|
|
}
|
|
|
|
|
2018-06-10 18:11:05 +01:00
|
|
|
Result Utils::OpenSdFile(const char *fn, int flags, FsFile *out) {
|
2018-07-28 20:29:54 +01:00
|
|
|
if (!IsSdInitialized()) {
|
2019-03-29 02:45:41 +00:00
|
|
|
return ResultFsSdCardNotPresent;
|
2018-06-10 18:11:05 +01:00
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-06-15 00:50:01 +01:00
|
|
|
return fsFsOpenFile(&g_sd_filesystem, fn, flags, out);
|
|
|
|
}
|
|
|
|
|
2019-11-21 07:58:18 +00:00
|
|
|
Result Utils::OpenSdFileForAtmosphere(u64 program_id, const char *fn, int flags, FsFile *out) {
|
2018-07-28 20:29:54 +01:00
|
|
|
if (!IsSdInitialized()) {
|
2019-03-29 02:45:41 +00:00
|
|
|
return ResultFsSdCardNotPresent;
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-06-10 18:11:05 +01:00
|
|
|
char path[FS_MAX_PATH];
|
2018-06-15 00:50:01 +01:00
|
|
|
if (*fn == '/') {
|
2019-11-21 07:58:18 +00:00
|
|
|
snprintf(path, sizeof(path), "/atmosphere/titles/%016lx%s", program_id, fn);
|
2018-06-15 00:50:01 +01:00
|
|
|
} else {
|
2019-11-21 07:58:18 +00:00
|
|
|
snprintf(path, sizeof(path), "/atmosphere/titles/%016lx/%s", program_id, fn);
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
2018-06-10 18:11:05 +01:00
|
|
|
return fsFsOpenFile(&g_sd_filesystem, path, flags, out);
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 07:58:18 +00:00
|
|
|
Result Utils::OpenRomFSSdFile(u64 program_id, const char *fn, int flags, FsFile *out) {
|
2018-07-28 20:29:54 +01:00
|
|
|
if (!IsSdInitialized()) {
|
2019-03-29 02:45:41 +00:00
|
|
|
return ResultFsSdCardNotPresent;
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-11-21 07:58:18 +00:00
|
|
|
return OpenRomFSFile(&g_sd_filesystem, program_id, fn, flags, out);
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result Utils::OpenSdDir(const char *path, FsDir *out) {
|
2018-07-28 20:29:54 +01:00
|
|
|
if (!IsSdInitialized()) {
|
2019-03-29 02:45:41 +00:00
|
|
|
return ResultFsSdCardNotPresent;
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-06-15 00:50:01 +01:00
|
|
|
return fsFsOpenDirectory(&g_sd_filesystem, path, FS_DIROPEN_DIRECTORY | FS_DIROPEN_FILE, out);
|
|
|
|
}
|
|
|
|
|
2019-11-21 07:58:18 +00:00
|
|
|
Result Utils::OpenSdDirForAtmosphere(u64 program_id, const char *path, FsDir *out) {
|
2018-07-28 20:29:54 +01:00
|
|
|
if (!IsSdInitialized()) {
|
2019-03-29 02:45:41 +00:00
|
|
|
return ResultFsSdCardNotPresent;
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-06-15 00:50:01 +01:00
|
|
|
char safe_path[FS_MAX_PATH];
|
|
|
|
if (*path == '/') {
|
2019-11-21 07:58:18 +00:00
|
|
|
snprintf(safe_path, sizeof(safe_path), "/atmosphere/titles/%016lx%s", program_id, path);
|
2018-06-15 00:50:01 +01:00
|
|
|
} else {
|
2019-11-21 07:58:18 +00:00
|
|
|
snprintf(safe_path, sizeof(safe_path), "/atmosphere/titles/%016lx/%s", program_id, path);
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
|
|
|
return fsFsOpenDirectory(&g_sd_filesystem, safe_path, FS_DIROPEN_DIRECTORY | FS_DIROPEN_FILE, out);
|
|
|
|
}
|
|
|
|
|
2019-11-21 07:58:18 +00:00
|
|
|
Result Utils::OpenRomFSSdDir(u64 program_id, const char *path, FsDir *out) {
|
2018-07-28 20:29:54 +01:00
|
|
|
if (!IsSdInitialized()) {
|
2019-03-29 02:45:41 +00:00
|
|
|
return ResultFsSdCardNotPresent;
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-11-21 07:58:18 +00:00
|
|
|
return OpenRomFSDir(&g_sd_filesystem, program_id, path, out);
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-21 07:58:18 +00:00
|
|
|
Result Utils::OpenRomFSFile(FsFileSystem *fs, u64 program_id, const char *fn, int flags, FsFile *out) {
|
2018-06-15 00:50:01 +01:00
|
|
|
char path[FS_MAX_PATH];
|
|
|
|
if (*fn == '/') {
|
2019-11-21 07:58:18 +00:00
|
|
|
snprintf(path, sizeof(path), "/atmosphere/titles/%016lx/romfs%s", program_id, fn);
|
2018-06-15 00:50:01 +01:00
|
|
|
} else {
|
2019-11-21 07:58:18 +00:00
|
|
|
snprintf(path, sizeof(path), "/atmosphere/titles/%016lx/romfs/%s", program_id, fn);
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
|
|
|
return fsFsOpenFile(fs, path, flags, out);
|
|
|
|
}
|
|
|
|
|
2019-11-21 07:58:18 +00:00
|
|
|
Result Utils::OpenRomFSDir(FsFileSystem *fs, u64 program_id, const char *path, FsDir *out) {
|
2018-06-15 00:50:01 +01:00
|
|
|
char safe_path[FS_MAX_PATH];
|
|
|
|
if (*path == '/') {
|
2019-11-21 07:58:18 +00:00
|
|
|
snprintf(safe_path, sizeof(safe_path), "/atmosphere/titles/%016lx/romfs%s", program_id, path);
|
2018-06-15 00:50:01 +01:00
|
|
|
} else {
|
2019-11-21 07:58:18 +00:00
|
|
|
snprintf(safe_path, sizeof(safe_path), "/atmosphere/titles/%016lx/romfs/%s", program_id, path);
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
|
|
|
return fsFsOpenDirectory(fs, safe_path, FS_DIROPEN_DIRECTORY | FS_DIROPEN_FILE, out);
|
|
|
|
}
|
2018-07-28 04:15:06 +01:00
|
|
|
|
2019-11-21 07:58:18 +00:00
|
|
|
bool Utils::HasSdRomfsContent(u64 program_id) {
|
2018-11-04 20:48:14 +00:00
|
|
|
/* Check for romfs.bin. */
|
|
|
|
FsFile data_file;
|
2019-11-21 07:58:18 +00:00
|
|
|
if (R_SUCCEEDED(Utils::OpenSdFileForAtmosphere(program_id, "romfs.bin", FS_OPEN_READ, &data_file))) {
|
2018-11-04 20:48:14 +00:00
|
|
|
fsFileClose(&data_file);
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-11-04 20:48:14 +00:00
|
|
|
/* Check for romfs folder with non-zero content. */
|
2018-07-28 04:15:06 +01:00
|
|
|
FsDir dir;
|
2019-11-21 07:58:18 +00:00
|
|
|
if (R_FAILED(Utils::OpenRomFSSdDir(program_id, "", &dir))) {
|
2018-11-04 20:45:29 +00:00
|
|
|
return false;
|
2018-07-28 04:15:06 +01:00
|
|
|
}
|
2018-11-04 20:45:29 +00:00
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
fsDirClose(&dir);
|
|
|
|
};
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-07-28 04:15:06 +01:00
|
|
|
FsDirectoryEntry dir_entry;
|
|
|
|
u64 read_entries;
|
2018-11-04 20:45:29 +00:00
|
|
|
return R_SUCCEEDED(fsDirRead(&dir, 0, &read_entries, 1, &dir_entry)) && read_entries == 1;
|
2018-07-28 20:29:54 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 07:58:18 +00:00
|
|
|
Result Utils::SaveSdFileForAtmosphere(u64 program_id, const char *fn, void *data, size_t size) {
|
2018-11-04 21:56:07 +00:00
|
|
|
if (!IsSdInitialized()) {
|
2019-03-29 02:45:41 +00:00
|
|
|
return ResultFsSdCardNotPresent;
|
2018-11-04 21:56:07 +00:00
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-11-04 21:56:07 +00:00
|
|
|
char path[FS_MAX_PATH];
|
|
|
|
if (*fn == '/') {
|
2019-11-21 07:58:18 +00:00
|
|
|
snprintf(path, sizeof(path), "/atmosphere/titles/%016lx%s", program_id, fn);
|
2018-11-04 21:56:07 +00:00
|
|
|
} else {
|
2019-11-21 07:58:18 +00:00
|
|
|
snprintf(path, sizeof(path), "/atmosphere/titles/%016lx/%s", program_id, fn);
|
2018-11-04 21:56:07 +00:00
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-11-04 21:56:07 +00:00
|
|
|
/* Unconditionally create. */
|
|
|
|
FsFile f;
|
|
|
|
fsFsCreateFile(&g_sd_filesystem, path, size, 0);
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-11-04 21:56:07 +00:00
|
|
|
/* Try to open. */
|
2019-06-20 12:04:33 +01:00
|
|
|
R_TRY(fsFsOpenFile(&g_sd_filesystem, path, FS_OPEN_READ | FS_OPEN_WRITE, &f));
|
|
|
|
ON_SCOPE_EXIT { fsFileClose(&f); };
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-11-04 21:56:07 +00:00
|
|
|
/* Try to make it big enough. */
|
2019-06-20 12:04:33 +01:00
|
|
|
R_TRY(fsFileSetSize(&f, size));
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-11-04 21:56:07 +00:00
|
|
|
/* Try to write the data. */
|
2019-06-20 12:04:33 +01:00
|
|
|
R_TRY(fsFileWrite(&f, 0, data, size, FS_WRITEOPTION_FLUSH));
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2018-11-04 21:56:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 04:54:16 +01:00
|
|
|
bool Utils::IsHblTid(u64 _tid) {
|
2019-11-21 07:58:18 +00:00
|
|
|
const ams::ncm::ProgramId tid{_tid};
|
|
|
|
return (g_hbl_override_config.override_any_app && ams::ncm::IsApplicationProgramId(tid)) || (_tid == g_hbl_override_config.program_id);
|
2019-01-09 20:50:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 04:54:16 +01:00
|
|
|
bool Utils::IsWebAppletTid(u64 _tid) {
|
2019-11-21 07:58:18 +00:00
|
|
|
const ams::ncm::ProgramId tid{_tid};
|
|
|
|
return tid == ams::ncm::ProgramId::AppletWeb || tid == ams::ncm::ProgramId::AppletOfflineWeb || tid == ams::ncm::ProgramId::AppletLoginShare || tid == ams::ncm::ProgramId::AppletWifiWebAuth;
|
2019-03-26 16:05:19 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 20:30:32 +00:00
|
|
|
bool Utils::HasTitleFlag(u64 tid, const char *flag) {
|
2018-11-15 12:26:40 +00:00
|
|
|
if (IsSdInitialized()) {
|
|
|
|
FsFile f;
|
|
|
|
char flag_path[FS_MAX_PATH];
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-11-15 12:26:40 +00:00
|
|
|
memset(flag_path, 0, sizeof(flag_path));
|
|
|
|
snprintf(flag_path, sizeof(flag_path) - 1, "flags/%s.flag", flag);
|
2019-03-08 03:53:21 +00:00
|
|
|
if (R_SUCCEEDED(OpenSdFileForAtmosphere(tid, flag_path, FS_OPEN_READ, &f))) {
|
2018-11-15 12:26:40 +00:00
|
|
|
fsFileClose(&f);
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-11-15 12:26:40 +00:00
|
|
|
/* TODO: Deprecate. */
|
|
|
|
snprintf(flag_path, sizeof(flag_path) - 1, "%s.flag", flag);
|
2019-03-08 03:53:21 +00:00
|
|
|
if (R_SUCCEEDED(OpenSdFileForAtmosphere(tid, flag_path, FS_OPEN_READ, &f))) {
|
2018-11-15 12:26:40 +00:00
|
|
|
fsFileClose(&f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-15 12:44:13 +00:00
|
|
|
bool Utils::HasGlobalFlag(const char *flag) {
|
2018-11-15 12:26:40 +00:00
|
|
|
if (IsSdInitialized()) {
|
|
|
|
FsFile f;
|
|
|
|
char flag_path[FS_MAX_PATH] = {0};
|
|
|
|
snprintf(flag_path, sizeof(flag_path), "/atmosphere/flags/%s.flag", flag);
|
2019-03-08 03:53:21 +00:00
|
|
|
if (R_SUCCEEDED(fsFsOpenFile(&g_sd_filesystem, flag_path, FS_OPEN_READ, &f))) {
|
2018-11-15 12:26:40 +00:00
|
|
|
fsFileClose(&f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-29 20:30:32 +00:00
|
|
|
bool Utils::HasHblFlag(const char *flag) {
|
|
|
|
char hbl_flag[FS_MAX_PATH] = {0};
|
|
|
|
snprintf(hbl_flag, sizeof(hbl_flag), "hbl_%s", flag);
|
|
|
|
return HasGlobalFlag(hbl_flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Utils::HasFlag(u64 tid, const char *flag) {
|
2019-01-09 20:50:01 +00:00
|
|
|
return HasTitleFlag(tid, flag) || (IsHblTid(tid) && HasHblFlag(flag));
|
2018-11-29 20:30:32 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 20:29:54 +01:00
|
|
|
bool Utils::HasSdMitMFlag(u64 tid) {
|
2019-01-09 20:50:01 +00:00
|
|
|
if (IsHblTid(tid)) {
|
2018-11-15 12:26:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-07-28 20:29:54 +01:00
|
|
|
if (IsSdInitialized()) {
|
|
|
|
return std::find(g_mitm_flagged_tids.begin(), g_mitm_flagged_tids.end(), tid) != g_mitm_flagged_tids.end();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-17 03:01:41 +01:00
|
|
|
|
2018-10-17 04:41:19 +01:00
|
|
|
bool Utils::HasSdDisableMitMFlag(u64 tid) {
|
|
|
|
if (IsSdInitialized()) {
|
|
|
|
return std::find(g_disable_mitm_flagged_tids.begin(), g_disable_mitm_flagged_tids.end(), tid) != g_disable_mitm_flagged_tids.end();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-16 04:28:38 +00:00
|
|
|
Result Utils::GetKeysHeld(u64 *keys) {
|
2019-03-16 01:34:55 +00:00
|
|
|
if (!Utils::IsHidAvailable()) {
|
2018-10-17 03:01:41 +01:00
|
|
|
return MAKERESULT(Module_Libnx, LibnxError_InitFail_HID);
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-10-17 03:01:41 +01:00
|
|
|
hidScanInput();
|
2019-05-12 14:04:58 +01:00
|
|
|
*keys = 0;
|
|
|
|
|
|
|
|
for (int controller = 0; controller < 10; controller++) {
|
|
|
|
*keys |= hidKeysHeld((HidControllerID) controller);
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2018-10-17 03:01:41 +01:00
|
|
|
}
|
|
|
|
|
2019-02-23 15:39:40 +00:00
|
|
|
static bool HasOverrideKey(OverrideKey *cfg) {
|
|
|
|
u64 kDown = 0;
|
2019-03-16 04:28:38 +00:00
|
|
|
bool keys_triggered = (R_SUCCEEDED(Utils::GetKeysHeld(&kDown)) && ((kDown & cfg->key_combination) != 0));
|
2019-02-23 15:39:40 +00:00
|
|
|
return Utils::IsSdInitialized() && (cfg->override_by_default ^ keys_triggered);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-17 03:01:41 +01:00
|
|
|
bool Utils::HasOverrideButton(u64 tid) {
|
2019-11-21 07:58:18 +00:00
|
|
|
if ((!ams::ncm::IsApplicationProgramId(ams::ncm::ProgramId{tid})) || (!IsSdInitialized())) {
|
2018-10-17 03:01:41 +01:00
|
|
|
/* Disable button override disable for non-applications. */
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-10-23 06:53:40 +01:00
|
|
|
/* Unconditionally refresh loader.ini contents. */
|
|
|
|
RefreshConfiguration();
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-02-23 15:39:40 +00:00
|
|
|
if (IsHblTid(tid) && HasOverrideKey(&g_hbl_override_config.override_key)) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-02-23 15:39:40 +00:00
|
|
|
OverrideKey title_cfg = GetTitleOverrideKey(tid);
|
|
|
|
return HasOverrideKey(&title_cfg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OverrideKey ParseOverrideKey(const char *value) {
|
|
|
|
OverrideKey cfg;
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-02-23 15:39:40 +00:00
|
|
|
/* Parse on by default. */
|
|
|
|
if (value[0] == '!') {
|
|
|
|
cfg.override_by_default = true;
|
|
|
|
value++;
|
|
|
|
} else {
|
|
|
|
cfg.override_by_default = false;
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-02-23 15:39:40 +00:00
|
|
|
/* Parse key combination. */
|
|
|
|
if (strcasecmp(value, "A") == 0) {
|
|
|
|
cfg.key_combination = KEY_A;
|
|
|
|
} else if (strcasecmp(value, "B") == 0) {
|
|
|
|
cfg.key_combination = KEY_B;
|
|
|
|
} else if (strcasecmp(value, "X") == 0) {
|
|
|
|
cfg.key_combination = KEY_X;
|
|
|
|
} else if (strcasecmp(value, "Y") == 0) {
|
|
|
|
cfg.key_combination = KEY_Y;
|
|
|
|
} else if (strcasecmp(value, "LS") == 0) {
|
|
|
|
cfg.key_combination = KEY_LSTICK;
|
|
|
|
} else if (strcasecmp(value, "RS") == 0) {
|
|
|
|
cfg.key_combination = KEY_RSTICK;
|
|
|
|
} else if (strcasecmp(value, "L") == 0) {
|
|
|
|
cfg.key_combination = KEY_L;
|
|
|
|
} else if (strcasecmp(value, "R") == 0) {
|
|
|
|
cfg.key_combination = KEY_R;
|
|
|
|
} else if (strcasecmp(value, "ZL") == 0) {
|
|
|
|
cfg.key_combination = KEY_ZL;
|
|
|
|
} else if (strcasecmp(value, "ZR") == 0) {
|
|
|
|
cfg.key_combination = KEY_ZR;
|
|
|
|
} else if (strcasecmp(value, "PLUS") == 0) {
|
|
|
|
cfg.key_combination = KEY_PLUS;
|
|
|
|
} else if (strcasecmp(value, "MINUS") == 0) {
|
|
|
|
cfg.key_combination = KEY_MINUS;
|
|
|
|
} else if (strcasecmp(value, "DLEFT") == 0) {
|
|
|
|
cfg.key_combination = KEY_DLEFT;
|
|
|
|
} else if (strcasecmp(value, "DUP") == 0) {
|
|
|
|
cfg.key_combination = KEY_DUP;
|
|
|
|
} else if (strcasecmp(value, "DRIGHT") == 0) {
|
|
|
|
cfg.key_combination = KEY_DRIGHT;
|
|
|
|
} else if (strcasecmp(value, "DDOWN") == 0) {
|
|
|
|
cfg.key_combination = KEY_DDOWN;
|
|
|
|
} else if (strcasecmp(value, "SL") == 0) {
|
|
|
|
cfg.key_combination = KEY_SL;
|
|
|
|
} else if (strcasecmp(value, "SR") == 0) {
|
|
|
|
cfg.key_combination = KEY_SR;
|
|
|
|
} else {
|
|
|
|
cfg.key_combination = 0;
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-02-23 15:39:40 +00:00
|
|
|
return cfg;
|
2018-10-17 03:01:41 +01:00
|
|
|
}
|
|
|
|
|
2019-02-23 15:39:40 +00:00
|
|
|
static int FsMitmIniHandler(void *user, const char *section, const char *name, const char *value) {
|
2018-10-17 03:01:41 +01:00
|
|
|
/* Taken and modified, with love, from Rajkosto's implementation. */
|
2019-02-23 15:39:40 +00:00
|
|
|
if (strcasecmp(section, "hbl_config") == 0) {
|
2019-11-21 07:58:18 +00:00
|
|
|
if (strcasecmp(name, "program_id") == 0) {
|
2019-01-09 20:50:01 +00:00
|
|
|
if (strcasecmp(value, "app") == 0) {
|
2019-03-22 13:59:28 +00:00
|
|
|
/* DEPRECATED */
|
2019-02-23 15:39:40 +00:00
|
|
|
g_hbl_override_config.override_any_app = true;
|
2019-11-21 07:58:18 +00:00
|
|
|
g_hbl_override_config.program_id = 0;
|
2019-03-22 14:13:49 +00:00
|
|
|
} else {
|
2019-01-09 20:50:01 +00:00
|
|
|
u64 override_tid = strtoul(value, NULL, 16);
|
|
|
|
if (override_tid != 0) {
|
2019-11-21 07:58:18 +00:00
|
|
|
g_hbl_override_config.program_id = override_tid;
|
2019-01-09 20:50:01 +00:00
|
|
|
}
|
2018-11-15 12:26:40 +00:00
|
|
|
}
|
|
|
|
} else if (strcasecmp(name, "override_key") == 0) {
|
2019-02-23 15:39:40 +00:00
|
|
|
g_hbl_override_config.override_key = ParseOverrideKey(value);
|
2019-03-22 13:59:28 +00:00
|
|
|
} else if (strcasecmp(name, "override_any_app") == 0) {
|
|
|
|
if (strcasecmp(value, "true") == 0 || strcasecmp(value, "1") == 0) {
|
|
|
|
g_hbl_override_config.override_any_app = true;
|
|
|
|
} else if (strcasecmp(value, "false") == 0 || strcasecmp(value, "0") == 0) {
|
|
|
|
g_hbl_override_config.override_any_app = false;
|
|
|
|
} else {
|
|
|
|
/* I guess we default to not changing the value? */
|
|
|
|
}
|
2019-02-23 15:39:40 +00:00
|
|
|
}
|
|
|
|
} else if (strcasecmp(section, "default_config") == 0) {
|
|
|
|
if (strcasecmp(name, "override_key") == 0) {
|
|
|
|
g_default_override_key = ParseOverrideKey(value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-11-21 07:58:18 +00:00
|
|
|
static int FsMitmContentSpecificIniHandler(void *user, const char *section, const char *name, const char *value) {
|
2019-02-23 15:39:40 +00:00
|
|
|
/* We'll output an override key when relevant. */
|
|
|
|
OverrideKey *user_cfg = reinterpret_cast<OverrideKey *>(user);
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-02-23 15:39:40 +00:00
|
|
|
if (strcasecmp(section, "override_config") == 0) {
|
|
|
|
if (strcasecmp(name, "override_key") == 0) {
|
|
|
|
*user_cfg = ParseOverrideKey(value);
|
2018-10-17 03:01:41 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-02-23 15:39:40 +00:00
|
|
|
OverrideKey Utils::GetTitleOverrideKey(u64 tid) {
|
|
|
|
OverrideKey cfg = g_default_override_key;
|
|
|
|
char path[FS_MAX_PATH+1] = {0};
|
2019-05-22 20:11:40 +01:00
|
|
|
snprintf(path, FS_MAX_PATH, "/atmosphere/titles/%016lx/config.ini", tid);
|
2019-02-23 15:39:40 +00:00
|
|
|
FsFile cfg_file;
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-02-24 06:11:38 +00:00
|
|
|
if (R_SUCCEEDED(fsFsOpenFile(&g_sd_filesystem, path, FS_OPEN_READ, &cfg_file))) {
|
2019-02-23 15:39:40 +00:00
|
|
|
ON_SCOPE_EXIT { fsFileClose(&cfg_file); };
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-02-23 15:39:40 +00:00
|
|
|
size_t config_file_size = 0x20000;
|
|
|
|
fsFileGetSize(&cfg_file, &config_file_size);
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-02-24 06:11:38 +00:00
|
|
|
char *config_buf = reinterpret_cast<char *>(calloc(1, config_file_size + 1));
|
2019-02-23 15:39:40 +00:00
|
|
|
if (config_buf != NULL) {
|
|
|
|
ON_SCOPE_EXIT { free(config_buf); };
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-02-23 15:39:40 +00:00
|
|
|
/* Read title ini contents. */
|
2019-06-03 20:15:39 +01:00
|
|
|
fsFileRead(&cfg_file, 0, config_buf, config_file_size, FS_READOPTION_NONE, &config_file_size);
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-02-23 15:39:40 +00:00
|
|
|
/* Parse title ini. */
|
2019-11-21 07:58:18 +00:00
|
|
|
ini_parse_string(config_buf, FsMitmContentSpecificIniHandler, &cfg);
|
2019-02-23 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-02-23 15:39:40 +00:00
|
|
|
return cfg;
|
|
|
|
}
|
2018-10-17 03:01:41 +01:00
|
|
|
|
2019-11-21 07:58:18 +00:00
|
|
|
static int FsMitmContentSpecificLocaleIniHandler(void *user, const char *section, const char *name, const char *value) {
|
2019-05-22 20:11:40 +01:00
|
|
|
/* We'll output an override locale when relevant. */
|
|
|
|
OverrideLocale *user_locale = reinterpret_cast<OverrideLocale *>(user);
|
|
|
|
|
|
|
|
if (strcasecmp(section, "override_config") == 0) {
|
|
|
|
if (strcasecmp(name, "override_language") == 0) {
|
|
|
|
user_locale->language_code = EncodeLanguageCode(value);
|
|
|
|
} else if (strcasecmp(name, "override_region") == 0) {
|
|
|
|
if (strcasecmp(value, "jpn") == 0) {
|
|
|
|
user_locale->region_code = RegionCode_Japan;
|
|
|
|
} else if (strcasecmp(value, "usa") == 0) {
|
|
|
|
user_locale->region_code = RegionCode_America;
|
|
|
|
} else if (strcasecmp(value, "eur") == 0) {
|
|
|
|
user_locale->region_code = RegionCode_Europe;
|
|
|
|
} else if (strcasecmp(value, "aus") == 0) {
|
|
|
|
user_locale->region_code = RegionCode_Australia;
|
|
|
|
} else if (strcasecmp(value, "chn") == 0) {
|
|
|
|
user_locale->region_code = RegionCode_China;
|
|
|
|
} else if (strcasecmp(value, "kor") == 0) {
|
|
|
|
user_locale->region_code = RegionCode_Korea;
|
|
|
|
} else if (strcasecmp(value, "twn") == 0) {
|
|
|
|
user_locale->region_code = RegionCode_Taiwan;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
OverrideLocale Utils::GetTitleOverrideLocale(u64 tid) {
|
|
|
|
OverrideLocale locale;
|
|
|
|
std::memset(&locale, 0xCC, sizeof(locale));
|
|
|
|
char path[FS_MAX_PATH+1] = {0};
|
|
|
|
snprintf(path, FS_MAX_PATH, "/atmosphere/titles/%016lx/config.ini", tid);
|
|
|
|
FsFile cfg_file;
|
|
|
|
|
|
|
|
if (R_SUCCEEDED(fsFsOpenFile(&g_sd_filesystem, path, FS_OPEN_READ, &cfg_file))) {
|
|
|
|
ON_SCOPE_EXIT { fsFileClose(&cfg_file); };
|
|
|
|
|
|
|
|
size_t config_file_size = 0x20000;
|
|
|
|
fsFileGetSize(&cfg_file, &config_file_size);
|
|
|
|
|
|
|
|
char *config_buf = reinterpret_cast<char *>(calloc(1, config_file_size + 1));
|
|
|
|
if (config_buf != NULL) {
|
|
|
|
ON_SCOPE_EXIT { free(config_buf); };
|
|
|
|
|
|
|
|
/* Read title ini contents. */
|
2019-06-03 20:15:39 +01:00
|
|
|
fsFileRead(&cfg_file, 0, config_buf, config_file_size, FS_READOPTION_NONE, &config_file_size);
|
2019-05-22 20:11:40 +01:00
|
|
|
|
|
|
|
/* Parse title ini. */
|
2019-11-21 07:58:18 +00:00
|
|
|
ini_parse_string(config_buf, FsMitmContentSpecificLocaleIniHandler, &locale);
|
2019-05-22 20:11:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return locale;
|
|
|
|
}
|
|
|
|
|
2018-10-23 06:53:40 +01:00
|
|
|
void Utils::RefreshConfiguration() {
|
|
|
|
FsFile config_file;
|
|
|
|
if (R_FAILED(fsFsOpenFile(&g_sd_filesystem, "/atmosphere/loader.ini", FS_OPEN_READ, &config_file))) {
|
2018-10-17 03:01:41 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-10-17 03:01:41 +01:00
|
|
|
u64 size;
|
2018-10-23 06:53:40 +01:00
|
|
|
if (R_FAILED(fsFileGetSize(&config_file, &size))) {
|
2018-10-17 03:01:41 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-10-17 03:01:41 +01:00
|
|
|
size = std::min(size, (decltype(size))0x7FF);
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2018-10-23 06:53:40 +01:00
|
|
|
/* Read in string. */
|
|
|
|
std::fill(g_config_ini_data, g_config_ini_data + 0x800, 0);
|
|
|
|
size_t r_s;
|
2019-06-03 20:15:39 +01:00
|
|
|
fsFileRead(&config_file, 0, g_config_ini_data, size, FS_READOPTION_NONE, &r_s);
|
2018-10-23 07:00:22 +01:00
|
|
|
fsFileClose(&config_file);
|
2019-05-22 20:11:40 +01:00
|
|
|
|
2019-02-23 15:39:40 +00:00
|
|
|
ini_parse_string(g_config_ini_data, FsMitmIniHandler, NULL);
|
2019-02-05 09:51:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result Utils::GetSettingsItemValueSize(const char *name, const char *key, u64 *out_size) {
|
|
|
|
return SettingsItemManager::GetValueSize(name, key, out_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result Utils::GetSettingsItemValue(const char *name, const char *key, void *out, size_t max_size, u64 *out_size) {
|
|
|
|
return SettingsItemManager::GetValue(name, key, out, max_size, out_size);
|
|
|
|
}
|
2019-04-05 21:36:38 +01:00
|
|
|
|
|
|
|
Result Utils::GetSettingsItemBooleanValue(const char *name, const char *key, bool *out) {
|
|
|
|
u8 val = 0;
|
|
|
|
u64 out_size;
|
2019-06-20 12:04:33 +01:00
|
|
|
R_TRY(Utils::GetSettingsItemValue(name, key, &val, sizeof(val), &out_size));
|
|
|
|
|
|
|
|
if (out) {
|
|
|
|
*out = val != 0;
|
2019-04-05 21:36:38 +01:00
|
|
|
}
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-04-05 21:36:38 +01:00
|
|
|
}
|
2019-04-12 23:28:46 +01:00
|
|
|
|
|
|
|
void Utils::RebootToFatalError(AtmosphereFatalErrorContext *ctx) {
|
|
|
|
BpcRebootManager::RebootForFatalError(ctx);
|
|
|
|
}
|