2021-08-28 01:35:57 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2021-08-28 01:35:57 +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/>.
|
|
|
|
*/
|
|
|
|
#include <exosphere.hpp>
|
2021-09-01 22:05:39 +01:00
|
|
|
#include <exosphere/secmon/secmon_monitor_context.hpp>
|
2021-08-28 09:15:15 +01:00
|
|
|
#include "fusee_key_derivation.hpp"
|
2021-09-05 23:45:40 +01:00
|
|
|
#include "fusee_external_package.hpp"
|
2021-08-28 01:35:57 +01:00
|
|
|
#include "fusee_setup_horizon.hpp"
|
2021-08-28 11:05:03 +01:00
|
|
|
#include "fusee_ini.hpp"
|
2021-09-01 06:51:51 +01:00
|
|
|
#include "fusee_emummc.hpp"
|
|
|
|
#include "fusee_mmc.hpp"
|
2021-09-02 02:10:48 +01:00
|
|
|
#include "fusee_cpu.hpp"
|
2021-08-28 01:35:57 +01:00
|
|
|
#include "fusee_fatal.hpp"
|
2021-09-01 08:50:52 +01:00
|
|
|
#include "fusee_package2.hpp"
|
2021-09-01 07:17:40 +01:00
|
|
|
#include "fusee_malloc.hpp"
|
2021-09-02 02:10:48 +01:00
|
|
|
#include "fusee_secmon_sync.hpp"
|
2021-09-03 04:40:15 +01:00
|
|
|
#include "fusee_stratosphere.hpp"
|
2021-09-01 06:51:51 +01:00
|
|
|
#include "fs/fusee_fs_api.hpp"
|
2021-08-28 01:35:57 +01:00
|
|
|
|
|
|
|
namespace ams::nxboot {
|
|
|
|
|
2021-08-28 07:20:46 +01:00
|
|
|
namespace {
|
|
|
|
|
2021-08-28 09:15:15 +01:00
|
|
|
constexpr inline const uintptr_t CLKRST = secmon::MemoryRegionPhysicalDeviceClkRst.GetAddress();
|
2021-09-01 18:37:01 +01:00
|
|
|
constexpr inline const uintptr_t PMC = secmon::MemoryRegionPhysicalDevicePmc.GetAddress();
|
2021-08-28 09:15:15 +01:00
|
|
|
constexpr inline const uintptr_t MC = secmon::MemoryRegionPhysicalDeviceMemoryController.GetAddress();
|
|
|
|
|
2021-08-28 11:05:03 +01:00
|
|
|
constinit secmon::EmummcConfiguration g_emummc_cfg = {};
|
|
|
|
|
2021-08-28 07:20:46 +01:00
|
|
|
void DeriveAllKeys(const fuse::SocType soc_type) {
|
|
|
|
/* If on erista, run the TSEC keygen firmware. */
|
|
|
|
if (soc_type == fuse::SocType_Erista) {
|
|
|
|
clkrst::SetBpmpClockRate(clkrst::BpmpClockRate_408MHz);
|
|
|
|
|
2021-09-05 23:45:40 +01:00
|
|
|
if (!tsec::RunTsecFirmware(GetExternalPackage().tsec_keygen, sizeof(GetExternalPackage().tsec_keygen))) {
|
2021-08-28 07:20:46 +01:00
|
|
|
ShowFatalError("Failed to run tsec_keygen firmware!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
clkrst::SetBpmpClockRate(clkrst::BpmpClockRate_576MHz);
|
|
|
|
}
|
|
|
|
|
2021-08-28 09:15:15 +01:00
|
|
|
/* Derive master/device keys. */
|
|
|
|
if (soc_type == fuse::SocType_Erista) {
|
|
|
|
DeriveKeysErista();
|
|
|
|
} else /* if (soc_type == fuse::SocType_Mariko) */ {
|
|
|
|
DeriveKeysMariko();
|
|
|
|
}
|
2021-08-28 07:20:46 +01:00
|
|
|
}
|
|
|
|
|
2021-08-28 11:05:03 +01:00
|
|
|
bool ParseIniSafe(IniSectionList &out_sections, const char *ini_path) {
|
|
|
|
const auto result = ParseIniFile(out_sections, ini_path);
|
|
|
|
if (result == ParseIniResult_Success) {
|
|
|
|
return true;
|
|
|
|
} else if (result == ParseIniResult_NoFile) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
ShowFatalError("Failed to parse %s!\n", ini_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 ParseHexInteger(const char *s) {
|
|
|
|
u32 x = 0;
|
|
|
|
if (s[0] == '0' && s[1] == 'x') {
|
|
|
|
s += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const char c = *(s++);
|
|
|
|
|
|
|
|
if (c == '\x00') {
|
|
|
|
return x;
|
|
|
|
} else {
|
|
|
|
x <<= 4;
|
|
|
|
|
|
|
|
if ('0' <= c && c <= '9') {
|
2021-09-03 08:40:26 +01:00
|
|
|
x |= (c - '0');
|
2021-08-28 11:05:03 +01:00
|
|
|
} else if ('a' <= c && c <= 'f') {
|
2021-09-03 08:40:26 +01:00
|
|
|
x |= (c - 'a') + 10;
|
2021-08-28 11:05:03 +01:00
|
|
|
} else if ('A' <= c && c <= 'F') {
|
2021-09-03 08:40:26 +01:00
|
|
|
x |= (c - 'A') + 10;
|
2021-08-28 11:05:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 22:05:39 +01:00
|
|
|
u32 ParseDecimalInteger(const char *s) {
|
|
|
|
u32 x = 0;
|
|
|
|
while (true) {
|
|
|
|
const char c = *(s++);
|
|
|
|
|
|
|
|
if (c == '\x00') {
|
|
|
|
return x;
|
|
|
|
} else {
|
|
|
|
x *= 10;
|
|
|
|
|
|
|
|
if ('0' <= c && c <= '9') {
|
|
|
|
x += c - '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 06:51:51 +01:00
|
|
|
bool IsDirectoryExist(const char *path) {
|
|
|
|
fs::DirectoryEntryType entry_type;
|
|
|
|
bool archive;
|
|
|
|
return R_SUCCEEDED(fs::GetEntryType(std::addressof(entry_type), std::addressof(archive), path)) && entry_type == fs::DirectoryEntryType_Directory;
|
|
|
|
}
|
|
|
|
|
2021-10-11 08:54:17 +01:00
|
|
|
bool IsFileExist(const char *path) {
|
2021-09-01 06:51:51 +01:00
|
|
|
fs::DirectoryEntryType entry_type;
|
|
|
|
bool archive;
|
|
|
|
return R_SUCCEEDED(fs::GetEntryType(std::addressof(entry_type), std::addressof(archive), path)) && entry_type == fs::DirectoryEntryType_File;
|
|
|
|
}
|
|
|
|
|
2021-08-28 11:05:03 +01:00
|
|
|
bool ConfigureEmummc() {
|
|
|
|
/* Set magic. */
|
|
|
|
g_emummc_cfg.base_cfg.magic = secmon::EmummcBaseConfiguration::Magic;
|
|
|
|
|
|
|
|
/* Parse ini. */
|
|
|
|
bool enabled = false;
|
|
|
|
u32 id = 0;
|
|
|
|
u32 sector = 0;
|
|
|
|
const char *path = "";
|
|
|
|
const char *n_path = "";
|
|
|
|
{
|
|
|
|
IniSectionList sections;
|
|
|
|
if (ParseIniSafe(sections, "sdmc:/emummc/emummc.ini")) {
|
|
|
|
for (const auto §ion : sections) {
|
|
|
|
/* We only care about the [emummc] section. */
|
|
|
|
if (std::strcmp(section.name, "emummc")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle individual fields. */
|
|
|
|
for (const auto &entry : section.kv_list) {
|
|
|
|
if (std::strcmp(entry.key, "enabled") == 0) {
|
2021-10-16 20:26:17 +01:00
|
|
|
enabled = entry.value[0] != '0';
|
2021-08-28 11:05:03 +01:00
|
|
|
} else if (std::strcmp(entry.key, "id") == 0) {
|
|
|
|
id = ParseHexInteger(entry.value);
|
|
|
|
} else if (std::strcmp(entry.key, "sector") == 0) {
|
|
|
|
sector = ParseHexInteger(entry.value);
|
|
|
|
} else if (std::strcmp(entry.key, "path") == 0) {
|
|
|
|
path = entry.value;
|
|
|
|
} else if (std::strcmp(entry.key, "nintendo_path") == 0) {
|
|
|
|
n_path = entry.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set values parsed from config. */
|
|
|
|
g_emummc_cfg.base_cfg.id = id;
|
|
|
|
std::strncpy(g_emummc_cfg.emu_dir_path.str, n_path, sizeof(g_emummc_cfg.emu_dir_path.str));
|
|
|
|
g_emummc_cfg.emu_dir_path.str[sizeof(g_emummc_cfg.emu_dir_path.str) - 1] = '\x00';
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
if (sector > 0) {
|
|
|
|
g_emummc_cfg.base_cfg.type = secmon::EmummcType_Partition;
|
|
|
|
g_emummc_cfg.partition_cfg.start_sector = sector;
|
2021-09-01 06:51:51 +01:00
|
|
|
} else if (path[0] != '\x00' && IsDirectoryExist(path)) {
|
2021-08-28 11:05:03 +01:00
|
|
|
g_emummc_cfg.base_cfg.type = secmon::EmummcType_File;
|
|
|
|
|
2021-09-01 06:51:51 +01:00
|
|
|
std::strncpy(g_emummc_cfg.file_cfg.path.str, path, sizeof(g_emummc_cfg.file_cfg.path.str));
|
|
|
|
g_emummc_cfg.file_cfg.path.str[sizeof(g_emummc_cfg.file_cfg.path.str) - 1] = '\x00';
|
2021-08-28 11:05:03 +01:00
|
|
|
} else {
|
|
|
|
ShowFatalError("Invalid emummc setting!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
2021-09-01 07:17:40 +01:00
|
|
|
u8 *LoadPackage1(fuse::SocType soc_type) {
|
|
|
|
u8 *package1 = static_cast<u8 *>(AllocateAligned(0x40000, 0x1000));
|
|
|
|
|
|
|
|
const Result result = ReadBoot0(0x100000, package1, 0x40000);
|
|
|
|
if (R_FAILED(result)) {
|
|
|
|
ShowFatalError("Failed to read boot0: 0x%08" PRIx32 "!\n", result.GetValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (soc_type == fuse::SocType_Mariko) {
|
|
|
|
package1 += 0x170;
|
|
|
|
|
2021-09-03 20:43:07 +01:00
|
|
|
se::DecryptAes128Cbc(package1 + 0x20, 0x40000 - (0x20 + 0x170), pkg1::AesKeySlot_MarikoBek, package1 + 0x20, 0x40000 - (0x20 + 0x170), package1 + 0x10, se::AesBlockSize);
|
2021-09-01 07:17:40 +01:00
|
|
|
|
|
|
|
hw::InvalidateDataCache(package1 + 0x20, 0x40000 - (0x20 + 0x170));
|
|
|
|
|
2021-09-02 07:16:40 +01:00
|
|
|
if (std::memcmp(package1, package1 + 0x20, 0x20) != 0) {
|
|
|
|
ShowFatalError("Package1 seems corrupt!\n");
|
|
|
|
}
|
2021-09-01 07:17:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return package1;
|
|
|
|
}
|
|
|
|
|
2021-10-11 08:54:17 +01:00
|
|
|
ams::TargetFirmware GetApproximateTargetFirmware(const u8 *package1) {
|
|
|
|
/* Get an approximation of the target firmware. */
|
2021-09-01 07:17:40 +01:00
|
|
|
switch (package1[0x1F]) {
|
|
|
|
case 0x01:
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_1_0_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
case 0x02:
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_2_0_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
case 0x04:
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_3_0_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
case 0x07:
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_4_0_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
case 0x0B:
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_5_0_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
case 0x0E:
|
|
|
|
if (std::memcmp(package1 + 0x10, "20180802", 8) == 0) {
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_6_0_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20181107", 8) == 0) {
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_6_2_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x0F:
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_7_0_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
case 0x10:
|
|
|
|
if (std::memcmp(package1 + 0x10, "20190314", 8) == 0) {
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_8_0_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20190531", 8) == 0) {
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_8_1_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20190809", 8) == 0) {
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_9_0_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20191021", 8) == 0) {
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_9_1_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20200303", 8) == 0) {
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_10_0_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20201030", 8) == 0) {
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_11_0_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20210129", 8) == 0) {
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_12_0_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20210422", 8) == 0) {
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_12_0_2;
|
2021-09-01 07:17:40 +01:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20210607", 8) == 0) {
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_12_1_0;
|
2021-09-16 00:08:57 +01:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20210805", 8) == 0) {
|
2021-10-11 08:54:17 +01:00
|
|
|
return ams::TargetFirmware_13_0_0;
|
2022-01-20 00:28:32 +00:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20220105", 8) == 0) {
|
|
|
|
return ams::TargetFirmware_13_2_1;
|
2022-03-22 01:37:48 +00:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20220209", 8) == 0) {
|
|
|
|
return ams::TargetFirmware_14_0_0;
|
2022-10-11 05:33:33 +01:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20220801", 8) == 0) {
|
|
|
|
return ams::TargetFirmware_15_0_0;
|
2023-02-21 22:12:27 +00:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20230111", 8) == 0) {
|
|
|
|
return ams::TargetFirmware_16_0_0;
|
2023-10-11 18:55:34 +01:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20230906", 8) == 0) {
|
|
|
|
return ams::TargetFirmware_17_0_0;
|
2024-03-28 01:48:49 +00:00
|
|
|
} else if (std::memcmp(package1 + 0x10, "20240207", 8) == 0) {
|
|
|
|
return ams::TargetFirmware_18_0_0;
|
2021-09-01 07:17:40 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-10-11 08:54:17 +01:00
|
|
|
ShowFatalError("Unable to identify package1!\n");
|
2021-09-01 07:17:40 +01:00
|
|
|
}
|
|
|
|
|
2021-09-01 08:50:52 +01:00
|
|
|
u8 *LoadBootConfigAndPackage2() {
|
|
|
|
Result result;
|
|
|
|
|
|
|
|
/* Load boot config. */
|
|
|
|
if (R_FAILED((result = ReadPackage2(0, secmon::MemoryRegionPhysicalIramBootConfig.GetPointer<void>(), secmon::MemoryRegionPhysicalIramBootConfig.GetSize())))) {
|
|
|
|
ShowFatalError("Failed to read boot config: 0x%08" PRIx32 "!\n", result.GetValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read package2 header. */
|
|
|
|
u8 *package2;
|
|
|
|
size_t package2_size;
|
|
|
|
{
|
2021-10-18 08:17:13 +01:00
|
|
|
constexpr s64 Package2Offset = AMS_OFFSETOF(pkg2::StorageLayout, package2_header);
|
2021-09-01 08:50:52 +01:00
|
|
|
|
|
|
|
pkg2::Package2Header header;
|
|
|
|
if (R_FAILED((result = ReadPackage2(Package2Offset, std::addressof(header), sizeof(header))))) {
|
|
|
|
ShowFatalError("Failed to read package2 header: 0x%08" PRIx32 "!\n", result.GetValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
package2_size = header.meta.GetSize();
|
|
|
|
package2 = static_cast<u8 *>(AllocateAligned(util::AlignUp(package2_size, 0x4000), 0x4000));
|
|
|
|
|
|
|
|
if (R_FAILED((result = ReadPackage2(Package2Offset, package2, util::AlignUp(package2_size, 0x4000))))) {
|
|
|
|
ShowFatalError("Failed to read package2: 0x%08" PRIx32 "!\n", result.GetValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrypt package2. */
|
|
|
|
DecryptPackage2(package2);
|
|
|
|
|
|
|
|
return package2;
|
|
|
|
}
|
|
|
|
|
2021-09-01 18:37:01 +01:00
|
|
|
constexpr inline const u8 PkcModulusErista[0x100] = {
|
|
|
|
0xF7, 0x86, 0x47, 0xAB, 0x71, 0x89, 0x81, 0xB5, 0xCF, 0x0C, 0xB0, 0xE8, 0x48, 0xA7, 0xFD, 0xAD,
|
|
|
|
0xCB, 0x4E, 0x4A, 0x52, 0x0B, 0x1A, 0x8E, 0xDE, 0x41, 0x87, 0x6F, 0xB7, 0x31, 0x05, 0x5F, 0xAA,
|
|
|
|
0xEA, 0x97, 0x76, 0x21, 0x20, 0x2B, 0x40, 0x48, 0x76, 0x55, 0x35, 0x03, 0xFE, 0x7F, 0x67, 0x62,
|
|
|
|
0xFD, 0x4E, 0xE1, 0x22, 0xF8, 0xF0, 0x97, 0x39, 0xEF, 0xEA, 0x47, 0x89, 0x3C, 0xDB, 0xF0, 0x02,
|
|
|
|
0xAD, 0x0C, 0x96, 0xCA, 0x82, 0xAB, 0xB3, 0xCB, 0x98, 0xC8, 0xDC, 0xC6, 0xAC, 0x5C, 0x93, 0x3B,
|
|
|
|
0x84, 0x3D, 0x51, 0x91, 0x9E, 0xC1, 0x29, 0x22, 0x95, 0xF0, 0xA1, 0x51, 0xBA, 0xAF, 0x5D, 0xC3,
|
|
|
|
0xAB, 0x04, 0x1B, 0x43, 0x61, 0x7D, 0xEA, 0x65, 0x95, 0x24, 0x3C, 0x51, 0x3E, 0x8F, 0xDB, 0xDB,
|
|
|
|
0xC1, 0xC4, 0x2D, 0x04, 0x29, 0x5A, 0xD7, 0x34, 0x6B, 0xCC, 0xF1, 0x06, 0xF9, 0xC9, 0xE1, 0xF9,
|
|
|
|
0x61, 0x52, 0xE2, 0x05, 0x51, 0xB1, 0x3D, 0x88, 0xF9, 0xA9, 0x27, 0xA5, 0x6F, 0x4D, 0xE7, 0x22,
|
|
|
|
0x48, 0xA5, 0xF8, 0x12, 0xA2, 0xC2, 0x5A, 0xA0, 0xBF, 0xC8, 0x76, 0x4B, 0x66, 0xFE, 0x1C, 0x73,
|
|
|
|
0x00, 0x29, 0x26, 0xCD, 0x18, 0x4F, 0xC2, 0xB0, 0x51, 0x77, 0x2E, 0x91, 0x09, 0x1B, 0x41, 0x5D,
|
|
|
|
0x89, 0x5E, 0xEE, 0x24, 0x22, 0x47, 0xE5, 0xE5, 0xF1, 0x86, 0x99, 0x67, 0x08, 0x28, 0x42, 0xF0,
|
|
|
|
0x58, 0x62, 0x54, 0xC6, 0x5B, 0xDC, 0xE6, 0x80, 0x85, 0x6F, 0xE2, 0x72, 0xB9, 0x7E, 0x36, 0x64,
|
|
|
|
0x48, 0x85, 0x10, 0xA4, 0x75, 0x38, 0x79, 0x76, 0x8B, 0x51, 0xD5, 0x87, 0xC3, 0x02, 0xC9, 0x1B,
|
|
|
|
0x93, 0x22, 0x49, 0xEA, 0xAB, 0xA0, 0xB5, 0xB1, 0x3C, 0x10, 0xC4, 0x71, 0xF0, 0xF1, 0x81, 0x1A,
|
|
|
|
0x3A, 0x9C, 0xFC, 0x51, 0x61, 0xB1, 0x4B, 0x18, 0xB2, 0x3D, 0xAA, 0xD6, 0xAC, 0x72, 0x26, 0xB7
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr inline const u8 PkcModulusDevelopmentErista[0x100] = {
|
|
|
|
0x37, 0x84, 0x14, 0xB3, 0x78, 0xA4, 0x7F, 0xD8, 0x71, 0x45, 0xCD, 0x90, 0x51, 0x51, 0xBF, 0x2C,
|
|
|
|
0x27, 0x03, 0x30, 0x46, 0xBE, 0x8F, 0x99, 0x3E, 0x9F, 0x36, 0x4D, 0xEB, 0xF7, 0x0E, 0x81, 0x7F,
|
|
|
|
0xE4, 0x6B, 0xA8, 0x42, 0x8A, 0xA5, 0x4F, 0x76, 0xCC, 0xCB, 0xC5, 0x31, 0xA8, 0x5A, 0x70, 0x51,
|
|
|
|
0x34, 0xBF, 0x1E, 0x8D, 0x6E, 0xCF, 0x05, 0x84, 0xCF, 0x8B, 0xE5, 0x9C, 0x3A, 0xA5, 0xCD, 0x1A,
|
|
|
|
0x9C, 0xAC, 0x59, 0x30, 0x09, 0x21, 0x3C, 0xBE, 0x07, 0x5C, 0x8D, 0x1C, 0xD1, 0xA3, 0xC9, 0x8F,
|
|
|
|
0x26, 0xE2, 0x99, 0xB2, 0x3C, 0x28, 0xAD, 0x63, 0x0F, 0xF5, 0xA0, 0x1C, 0xA2, 0x34, 0xC4, 0x0E,
|
|
|
|
0xDB, 0xD7, 0xE1, 0xA9, 0x5E, 0xE9, 0xA5, 0xA8, 0x64, 0x3A, 0xFC, 0x48, 0xB5, 0x97, 0xDF, 0x55,
|
|
|
|
0x7C, 0x9A, 0xD2, 0x8C, 0x32, 0x36, 0x1D, 0xC5, 0xA0, 0xC5, 0x66, 0xDF, 0x8A, 0xAD, 0x76, 0x18,
|
|
|
|
0x46, 0x3E, 0xDF, 0xD8, 0xEF, 0xB9, 0xE5, 0xDC, 0xCD, 0x08, 0x59, 0xBC, 0x36, 0x68, 0xD6, 0xFC,
|
|
|
|
0x3F, 0xFA, 0x11, 0x00, 0x0D, 0x50, 0xE0, 0x69, 0x0F, 0x70, 0x78, 0x7E, 0xD1, 0xA5, 0x85, 0xCD,
|
|
|
|
0x13, 0xBC, 0x42, 0x74, 0x33, 0x0C, 0x11, 0x24, 0x1E, 0x33, 0xD5, 0x31, 0xB7, 0x3E, 0x48, 0x94,
|
|
|
|
0xCC, 0x81, 0x29, 0x1E, 0xB1, 0xCF, 0x4C, 0x36, 0x7F, 0xE1, 0x1C, 0x15, 0xD4, 0x3F, 0xFB, 0x12,
|
|
|
|
0xC2, 0x73, 0x22, 0x16, 0x52, 0xE0, 0x5C, 0x4C, 0x94, 0xE0, 0x87, 0x47, 0xEA, 0xD0, 0x9F, 0x42,
|
|
|
|
0x9B, 0xAC, 0xB6, 0xB5, 0xB6, 0x34, 0xE4, 0x55, 0x49, 0xD7, 0xC0, 0xAE, 0xD4, 0x22, 0xB3, 0x5C,
|
|
|
|
0x87, 0x64, 0x42, 0xEC, 0x11, 0x6D, 0xBC, 0x09, 0xC0, 0x80, 0x07, 0xD0, 0xBD, 0xBA, 0x45, 0xFE,
|
|
|
|
0xD5, 0x52, 0xDA, 0xEC, 0x41, 0xA4, 0xAD, 0x7B, 0x36, 0x86, 0x18, 0xB4, 0x5B, 0xD1, 0x30, 0xBB
|
|
|
|
};
|
|
|
|
|
|
|
|
void LoadWarmbootFirmware(fuse::SocType soc_type, ams::TargetFirmware target_firmware, const u8 *package1) {
|
|
|
|
u8 *warmboot_dst = secmon::MemoryRegionPhysicalIramWarmbootBin.GetPointer<u8>();
|
2021-09-05 23:45:40 +01:00
|
|
|
size_t warmboot_size = std::min(sizeof(GetExternalPackage().warmboot), secmon::MemoryRegionPhysicalIramWarmbootBin.GetSize());
|
2021-09-01 18:37:01 +01:00
|
|
|
if (soc_type == fuse::SocType_Erista) {
|
|
|
|
/* Copy the ams warmboot binary. */
|
2021-09-05 23:45:40 +01:00
|
|
|
std::memcpy(warmboot_dst, GetExternalPackage().warmboot, warmboot_size);
|
2021-09-01 18:37:01 +01:00
|
|
|
|
|
|
|
/* Set the rsa modulus. */
|
|
|
|
if (fuse::GetHardwareState() == fuse::HardwareState_Production) {
|
|
|
|
std::memcpy(warmboot_dst + 0x10, PkcModulusErista, sizeof(PkcModulusErista));
|
|
|
|
} else {
|
|
|
|
std::memcpy(warmboot_dst + 0x10, PkcModulusDevelopmentErista, sizeof(PkcModulusDevelopmentErista));
|
|
|
|
}
|
2021-09-05 05:43:19 +01:00
|
|
|
|
|
|
|
/* Set the target firmware. */
|
|
|
|
std::memcpy(warmboot_dst + 0x248, std::addressof(target_firmware), sizeof(target_firmware));
|
2021-09-01 18:37:01 +01:00
|
|
|
} else /* if (soc_type == fuse::SocType_Mariko) */ {
|
|
|
|
/* Declare path for mariko warmboot files. */
|
|
|
|
char warmboot_path[0x80] = "sdmc:/warmboot_mariko/wb_xx.bin";
|
|
|
|
|
|
|
|
auto UpdateWarmbootPath = [&warmboot_path](u8 fuses) {
|
|
|
|
warmboot_path[0x19] = "0123456789abcdef"[(fuses >> 4) & 0xF];
|
|
|
|
warmboot_path[0x1A] = "0123456789abcdef"[(fuses >> 0) & 0xF];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Get expected/burnt fuse counts. */
|
|
|
|
const u32 expected_fuses = fuse::GetExpectedFuseVersion(target_firmware);
|
|
|
|
const u32 burnt_fuses = fuse::GetFuseVersion();
|
|
|
|
u32 used_fuses = expected_fuses;
|
|
|
|
|
|
|
|
/* Get warmboot from package1. */
|
|
|
|
const u8 *warmboot_src = nullptr;
|
|
|
|
size_t warmboot_src_size = 0;
|
|
|
|
{
|
|
|
|
const u32 *package1_pk11 = reinterpret_cast<const u32 *>(package1 + (target_firmware >= ams::TargetFirmware_6_2_0 ? 0x7000 : 0x4000));
|
|
|
|
if (std::memcmp(package1_pk11, "PK11", 4) != 0) {
|
|
|
|
ShowFatalError("Invalid package1 magic!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
const u32 *package1_pk11_data = reinterpret_cast<const u32 *>(package1_pk11 + (0x20 / sizeof(u32)));
|
|
|
|
for (size_t i = 0; i < 3; ++i) {
|
|
|
|
switch (*package1_pk11_data) {
|
|
|
|
case 0xD5034FDF:
|
|
|
|
package1_pk11_data += package1_pk11[6] / sizeof(u32);
|
|
|
|
break;
|
|
|
|
case 0xE328F0C0:
|
|
|
|
case 0xF0C0A7F0:
|
|
|
|
package1_pk11_data += package1_pk11[4] / sizeof(u32);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
warmboot_src = reinterpret_cast<const u8 *>(package1_pk11_data);
|
|
|
|
i = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
warmboot_src_size = *package1_pk11_data;
|
|
|
|
if (!(0x800 <= warmboot_src_size && warmboot_src_size < 0x1000)) {
|
|
|
|
ShowFatalError("Package1 warmboot firmware seems invalid!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we should, save the current warmboot firmware. */
|
|
|
|
UpdateWarmbootPath(expected_fuses);
|
|
|
|
if (!IsFileExist(warmboot_path)) {
|
|
|
|
fs::CreateDirectory("sdmc:/warmboot_mariko");
|
|
|
|
fs::CreateFile(warmboot_path, warmboot_src_size);
|
|
|
|
|
|
|
|
Result result;
|
|
|
|
fs::FileHandle file;
|
|
|
|
if (R_FAILED((result = fs::OpenFile(std::addressof(file), warmboot_path, fs::OpenMode_ReadWrite)))) {
|
|
|
|
ShowFatalError("Failed to save %s!\n", warmboot_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
ON_SCOPE_EXIT { fs::CloseFile(file); };
|
|
|
|
|
|
|
|
if (R_FAILED((result = fs::WriteFile(file, 0, warmboot_src, warmboot_src_size, fs::WriteOption::Flush)))) {
|
|
|
|
ShowFatalError("Failed to save %s!\n", warmboot_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we need to, find a cached warmboot firmware that we can use. */
|
|
|
|
if (burnt_fuses > expected_fuses) {
|
|
|
|
warmboot_src = nullptr;
|
|
|
|
warmboot_src_size = 0;
|
|
|
|
for (u32 attempt = burnt_fuses; attempt <= 32; ++attempt) {
|
|
|
|
/* Open the current cache file. */
|
|
|
|
UpdateWarmbootPath(attempt);
|
|
|
|
|
|
|
|
fs::FileHandle file;
|
|
|
|
if (R_FAILED(fs::OpenFile(std::addressof(file), warmboot_path, fs::OpenMode_Read))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ON_SCOPE_EXIT { fs::CloseFile(file); };
|
|
|
|
|
|
|
|
/* Get the size. */
|
|
|
|
s64 size;
|
|
|
|
if (R_FAILED(fs::GetFileSize(std::addressof(size), file)) || !(0x800 <= size && size < 0x1000)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate memory. */
|
|
|
|
warmboot_src_size = static_cast<size_t>(size);
|
2021-09-01 22:05:39 +01:00
|
|
|
void *tmp = AllocateAligned(warmboot_src_size, 0x10);
|
2021-09-01 18:37:01 +01:00
|
|
|
|
|
|
|
/* Read the file. */
|
|
|
|
if (R_FAILED(fs::ReadFile(file, 0, tmp, warmboot_src_size))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use the cached file. */
|
|
|
|
used_fuses = attempt;
|
|
|
|
warmboot_src = static_cast<const u8 *>(tmp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that we found a firmware. */
|
|
|
|
if (warmboot_src == nullptr) {
|
|
|
|
ShowFatalError("Failed to locate warmboot firmware!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the warmboot firmware. */
|
|
|
|
std::memcpy(warmboot_dst, warmboot_src, std::min(warmboot_size, warmboot_src_size));
|
|
|
|
|
|
|
|
/* Set the warmboot firmware magic. */
|
|
|
|
switch (used_fuses) {
|
|
|
|
case 7:
|
|
|
|
reg::Write(PMC + APBDEV_PMC_SECURE_SCRATCH32, 0x87);
|
|
|
|
case 8:
|
|
|
|
reg::Write(PMC + APBDEV_PMC_SECURE_SCRATCH32, 0xA8);
|
|
|
|
default:
|
|
|
|
reg::Write(PMC + APBDEV_PMC_SECURE_SCRATCH32, (0x108 + 0x21 * (used_fuses - 8)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
reg::SetBits(PMC + APBDEV_PMC_SEC_DISABLE3, (1 << 16));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 04:40:15 +01:00
|
|
|
void ConfigureExosphere(fuse::SocType soc_type, ams::TargetFirmware target_firmware, bool emummc_enabled, u32 fs_version) {
|
2021-09-01 22:05:39 +01:00
|
|
|
/* Get monitor configuration. */
|
|
|
|
auto &storage_ctx = *secmon::MemoryRegionPhysicalDramMonitorConfiguration.GetPointer<secmon::SecureMonitorStorageConfiguration>();
|
|
|
|
std::memset(std::addressof(storage_ctx), 0, sizeof(storage_ctx));
|
|
|
|
|
|
|
|
/* Set magic. */
|
|
|
|
storage_ctx.magic = secmon::SecureMonitorStorageConfiguration::Magic;
|
|
|
|
|
|
|
|
/* Set some defaults. */
|
|
|
|
storage_ctx.target_firmware = target_firmware;
|
|
|
|
storage_ctx.lcd_vendor = GetDisplayLcdVendor();
|
|
|
|
storage_ctx.emummc_cfg = g_emummc_cfg;
|
|
|
|
storage_ctx.flags[0] = secmon::SecureMonitorConfigurationFlag_Default;
|
|
|
|
storage_ctx.flags[1] = secmon::SecureMonitorConfigurationFlag_None;
|
|
|
|
storage_ctx.log_port = uart::Port_ReservedDebug;
|
|
|
|
storage_ctx.log_baud_rate = 115200;
|
|
|
|
|
2021-09-03 04:40:15 +01:00
|
|
|
/* Set the fs version. */
|
|
|
|
storage_ctx.emummc_cfg.base_cfg.fs_version = fs_version;
|
|
|
|
|
2021-09-01 22:05:39 +01:00
|
|
|
/* Parse fields from exosphere.ini */
|
|
|
|
{
|
|
|
|
IniSectionList sections;
|
|
|
|
if (ParseIniSafe(sections, "sdmc:/exosphere.ini")) {
|
|
|
|
for (const auto §ion : sections) {
|
|
|
|
/* We only care about the [exosphere] section. */
|
|
|
|
if (std::strcmp(section.name, "exosphere")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle individual fields. */
|
|
|
|
for (const auto &entry : section.kv_list) {
|
|
|
|
if (std::strcmp(entry.key, "debugmode") == 0) {
|
|
|
|
if (entry.value[0] == '1') {
|
|
|
|
storage_ctx.flags[0] |= secmon::SecureMonitorConfigurationFlag_IsDevelopmentFunctionEnabledForKernel;
|
|
|
|
} else {
|
|
|
|
storage_ctx.flags[0] &= ~secmon::SecureMonitorConfigurationFlag_IsDevelopmentFunctionEnabledForKernel;
|
|
|
|
}
|
|
|
|
} else if (std::strcmp(entry.key, "debugmode_user") == 0) {
|
|
|
|
if (entry.value[0] == '1') {
|
|
|
|
storage_ctx.flags[0] |= secmon::SecureMonitorConfigurationFlag_IsDevelopmentFunctionEnabledForUser;
|
|
|
|
} else {
|
|
|
|
storage_ctx.flags[0] &= ~secmon::SecureMonitorConfigurationFlag_IsDevelopmentFunctionEnabledForUser;
|
|
|
|
}
|
|
|
|
} else if (std::strcmp(entry.key, "disable_user_exception_handlers") == 0) {
|
|
|
|
if (entry.value[0] == '1') {
|
|
|
|
storage_ctx.flags[0] |= secmon::SecureMonitorConfigurationFlag_DisableUserModeExceptionHandlers;
|
|
|
|
} else {
|
|
|
|
storage_ctx.flags[0] &= ~secmon::SecureMonitorConfigurationFlag_DisableUserModeExceptionHandlers;
|
|
|
|
}
|
|
|
|
} else if (std::strcmp(entry.key, "enable_user_pmu_access") == 0) {
|
|
|
|
if (entry.value[0] == '1') {
|
|
|
|
storage_ctx.flags[0] |= secmon::SecureMonitorConfigurationFlag_EnableUserModePerformanceCounterAccess;
|
|
|
|
} else {
|
|
|
|
storage_ctx.flags[0] &= ~secmon::SecureMonitorConfigurationFlag_EnableUserModePerformanceCounterAccess;
|
|
|
|
}
|
|
|
|
} else if (std::strcmp(entry.key, "blank_prodinfo_sysmmc") == 0) {
|
2021-09-07 12:34:52 +01:00
|
|
|
if (!emummc_enabled) {
|
|
|
|
if (entry.value[0] == '1') {
|
|
|
|
storage_ctx.flags[0] |= secmon::SecureMonitorConfigurationFlag_ShouldUseBlankCalibrationBinary;
|
|
|
|
} else {
|
|
|
|
storage_ctx.flags[0] &= ~secmon::SecureMonitorConfigurationFlag_ShouldUseBlankCalibrationBinary;
|
|
|
|
}
|
2021-09-01 22:05:39 +01:00
|
|
|
}
|
|
|
|
} else if (std::strcmp(entry.key, "blank_prodinfo_emummc") == 0) {
|
2021-09-07 12:34:52 +01:00
|
|
|
if (emummc_enabled) {
|
|
|
|
if (entry.value[0] == '1') {
|
|
|
|
storage_ctx.flags[0] |= secmon::SecureMonitorConfigurationFlag_ShouldUseBlankCalibrationBinary;
|
|
|
|
} else {
|
|
|
|
storage_ctx.flags[0] &= ~secmon::SecureMonitorConfigurationFlag_ShouldUseBlankCalibrationBinary;
|
|
|
|
}
|
2021-09-01 22:05:39 +01:00
|
|
|
}
|
|
|
|
} else if (std::strcmp(entry.key, "allow_writing_to_cal_sysmmc") == 0) {
|
|
|
|
if (entry.value[0] == '1') {
|
|
|
|
storage_ctx.flags[0] |= secmon::SecureMonitorConfigurationFlag_AllowWritingToCalibrationBinarySysmmc;
|
|
|
|
} else {
|
|
|
|
storage_ctx.flags[0] &= ~secmon::SecureMonitorConfigurationFlag_AllowWritingToCalibrationBinarySysmmc;
|
|
|
|
}
|
|
|
|
} else if (std::strcmp(entry.key, "log_port") == 0) {
|
|
|
|
const u32 log_port = ParseDecimalInteger(entry.value);
|
|
|
|
if (0 <= log_port && log_port < 4) {
|
|
|
|
storage_ctx.log_port = log_port;
|
|
|
|
}
|
|
|
|
} else if (std::strcmp(entry.key, "log_baud_rate") == 0) {
|
|
|
|
storage_ctx.log_baud_rate = ParseDecimalInteger(entry.value);
|
|
|
|
} else if (std::strcmp(entry.key, "log_inverted") == 0) {
|
2021-09-08 22:32:15 +01:00
|
|
|
if (entry.value[0] == '1') {
|
2021-09-01 22:05:39 +01:00
|
|
|
storage_ctx.log_flags |= uart::Flag_Inverted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse usb setting from system_settings.ini */
|
|
|
|
{
|
|
|
|
IniSectionList sections;
|
|
|
|
if (ParseIniSafe(sections, "sdmc:/atmosphere/config/system_settings.ini")) {
|
|
|
|
for (const auto §ion : sections) {
|
|
|
|
/* We only care about the [usb] section. */
|
|
|
|
if (std::strcmp(section.name, "usb")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle individual fields. */
|
|
|
|
for (const auto &entry : section.kv_list) {
|
|
|
|
if (std::strcmp(entry.key, "usb30_force_enabled") == 0) {
|
|
|
|
if (std::strcmp(entry.value, "u8!0x1") == 0) {
|
|
|
|
storage_ctx.flags[0] |= secmon::SecureMonitorConfigurationFlag_ForceEnableUsb30;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy exosphere. */
|
|
|
|
void *exosphere_dst = reinterpret_cast<void *>(0x40030000);
|
|
|
|
bool use_sd_exo = false;
|
|
|
|
{
|
|
|
|
/* Try to use an sd card file, if present. */
|
|
|
|
fs::FileHandle exo_file;
|
|
|
|
if (R_SUCCEEDED(fs::OpenFile(std::addressof(exo_file), "sdmc:/atmosphere/exosphere.bin", fs::OpenMode_Read))) {
|
|
|
|
ON_SCOPE_EXIT { fs::CloseFile(exo_file); };
|
|
|
|
|
|
|
|
/* Note that we're using sd_exo. */
|
|
|
|
use_sd_exo = true;
|
|
|
|
|
|
|
|
Result result;
|
|
|
|
|
|
|
|
/* Get the size. */
|
|
|
|
s64 size;
|
2021-09-05 23:45:40 +01:00
|
|
|
if (R_FAILED((result = fs::GetFileSize(std::addressof(size), exo_file))) || size > sizeof(GetExternalPackage().exosphere)) {
|
2021-09-01 22:05:39 +01:00
|
|
|
ShowFatalError("Invalid SD exosphere size: 0x%08" PRIx32 ", %" PRIx64 "!\n", result.GetValue(), static_cast<u64>(size));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the file. */
|
|
|
|
if (R_FAILED((result = fs::ReadFile(exo_file, 0, exosphere_dst, size)))) {
|
|
|
|
ShowFatalError("Failed to read SD exosphere: 0x%08" PRIx32 "!\n", result.GetValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!use_sd_exo) {
|
2021-09-05 23:45:40 +01:00
|
|
|
std::memcpy(exosphere_dst, GetExternalPackage().exosphere, sizeof(GetExternalPackage().exosphere));
|
2021-09-01 22:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy mariko fatal. */
|
|
|
|
if (soc_type == fuse::SocType_Mariko) {
|
|
|
|
u8 *mariko_fatal_dst = secmon::MemoryRegionPhysicalMarikoProgramImage.GetPointer<u8>();
|
|
|
|
bool use_sd_mariko_fatal = false;
|
|
|
|
{
|
|
|
|
/* Try to use an sd card file, if present. */
|
|
|
|
fs::FileHandle mariko_program_file;
|
|
|
|
if (R_SUCCEEDED(fs::OpenFile(std::addressof(mariko_program_file), "sdmc:/atmosphere/mariko_fatal.bin", fs::OpenMode_Read))) {
|
|
|
|
ON_SCOPE_EXIT { fs::CloseFile(mariko_program_file); };
|
|
|
|
|
2021-09-01 22:08:39 +01:00
|
|
|
/* Note that we're using sd mariko fatal. */
|
|
|
|
use_sd_mariko_fatal = true;
|
2021-09-01 22:05:39 +01:00
|
|
|
|
|
|
|
Result result;
|
|
|
|
|
|
|
|
/* Get the size. */
|
|
|
|
s64 size;
|
2021-09-05 23:45:40 +01:00
|
|
|
if (R_FAILED((result = fs::GetFileSize(std::addressof(size), mariko_program_file))) || size > sizeof(GetExternalPackage().mariko_fatal)) {
|
2021-09-01 22:08:39 +01:00
|
|
|
ShowFatalError("Invalid SD mariko_fatal size: 0x%08" PRIx32 ", %" PRIx64 "!\n", result.GetValue(), static_cast<u64>(size));
|
2021-09-01 22:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the file. */
|
|
|
|
if (R_FAILED((result = fs::ReadFile(mariko_program_file, 0, mariko_fatal_dst, size)))) {
|
2021-09-01 22:08:39 +01:00
|
|
|
ShowFatalError("Failed to read SD mariko_fatal: 0x%08" PRIx32 "!\n", result.GetValue());
|
2021-09-01 22:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear the remainder. */
|
2021-09-05 23:45:40 +01:00
|
|
|
std::memset(mariko_fatal_dst + size, 0, sizeof(GetExternalPackage().mariko_fatal) - size);
|
2021-09-01 22:05:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!use_sd_mariko_fatal) {
|
2021-09-05 23:45:40 +01:00
|
|
|
std::memcpy(mariko_fatal_dst, GetExternalPackage().mariko_fatal, sizeof(GetExternalPackage().mariko_fatal));
|
2021-09-01 22:05:39 +01:00
|
|
|
}
|
|
|
|
}
|
2021-09-02 02:10:48 +01:00
|
|
|
|
|
|
|
/* Setup the CPU to boot exosphere. */
|
|
|
|
SetupCpu(reinterpret_cast<uintptr_t>(exosphere_dst));
|
|
|
|
|
2021-09-05 05:43:19 +01:00
|
|
|
/* Initialize bootloader parameters. */
|
|
|
|
InitializeSecureMonitorMailbox();
|
|
|
|
|
2021-09-02 02:10:48 +01:00
|
|
|
/* Set our bootloader state. */
|
|
|
|
SetBootloaderState(pkg1::BootloaderState_LoadedBootConfig);
|
|
|
|
|
|
|
|
/* Ensure that the CPU will see consistent data. */
|
|
|
|
hw::FlushEntireDataCache();
|
2021-09-01 22:05:39 +01:00
|
|
|
}
|
|
|
|
|
2021-09-03 04:40:15 +01:00
|
|
|
bool IsNogcEnabled(ams::TargetFirmware target_firmware) {
|
|
|
|
/* First parse from ini. */
|
|
|
|
{
|
|
|
|
IniSectionList sections;
|
|
|
|
if (ParseIniSafe(sections, "sdmc:/atmosphere/config/stratosphere.ini")) {
|
|
|
|
for (const auto §ion : sections) {
|
|
|
|
/* We only care about the [stratosphere] section. */
|
|
|
|
if (std::strcmp(section.name, "stratosphere")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle individual fields. */
|
|
|
|
for (const auto &entry : section.kv_list) {
|
|
|
|
if (std::strcmp(entry.key, "nogc") == 0) {
|
|
|
|
return entry.value[0] == '1';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* That failed, so try to decide automatically. */
|
|
|
|
const auto fuse_version = fuse::GetFuseVersion();
|
|
|
|
if (target_firmware >= ams::TargetFirmware_12_0_2 && fuse_version < fuse::GetExpectedFuseVersion(ams::TargetFirmware_12_0_2)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (target_firmware >= ams::TargetFirmware_11_0_0 && fuse_version < fuse::GetExpectedFuseVersion(ams::TargetFirmware_11_0_0)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (target_firmware >= ams::TargetFirmware_9_0_0 && fuse_version < fuse::GetExpectedFuseVersion(ams::TargetFirmware_9_0_0)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (target_firmware >= ams::TargetFirmware_4_0_0 && fuse_version < fuse::GetExpectedFuseVersion(ams::TargetFirmware_4_0_0)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-28 07:20:46 +01:00
|
|
|
}
|
|
|
|
|
2021-08-28 01:35:57 +01:00
|
|
|
void SetupAndStartHorizon() {
|
2021-09-01 08:16:53 +01:00
|
|
|
/* Get soc type. */
|
2021-08-28 07:20:46 +01:00
|
|
|
const auto soc_type = fuse::GetSocType();
|
|
|
|
|
|
|
|
/* Derive all keys. */
|
|
|
|
DeriveAllKeys(soc_type);
|
|
|
|
|
2021-08-28 11:05:03 +01:00
|
|
|
/* Determine whether we're using emummc. */
|
|
|
|
const bool emummc_enabled = ConfigureEmummc();
|
2021-09-01 06:51:51 +01:00
|
|
|
|
|
|
|
/* Initialize emummc. */
|
2021-09-01 08:16:53 +01:00
|
|
|
/* NOTE: SYSTEM:/ accessible past this point. */
|
2021-09-01 06:51:51 +01:00
|
|
|
InitializeEmummc(emummc_enabled, g_emummc_cfg);
|
2021-08-28 09:15:15 +01:00
|
|
|
|
2021-09-01 07:17:40 +01:00
|
|
|
/* Read bootloader. */
|
|
|
|
const u8 * const package1 = LoadPackage1(soc_type);
|
|
|
|
|
|
|
|
/* Get target firmware. */
|
2021-10-11 08:54:17 +01:00
|
|
|
const auto target_firmware = GetApproximateTargetFirmware(package1);
|
2021-09-01 07:17:40 +01:00
|
|
|
|
2021-09-01 08:50:52 +01:00
|
|
|
/* Read/decrypt package2. */
|
|
|
|
u8 * const package2 = LoadBootConfigAndPackage2();
|
2021-09-01 08:16:53 +01:00
|
|
|
|
2021-09-01 18:37:01 +01:00
|
|
|
/* Setup warmboot firmware. */
|
|
|
|
LoadWarmbootFirmware(soc_type, target_firmware, package1);
|
2021-09-01 08:16:53 +01:00
|
|
|
|
2021-09-03 04:40:15 +01:00
|
|
|
/* Decide whether to use nogc patches. */
|
|
|
|
const bool nogc_enabled = IsNogcEnabled(target_firmware);
|
|
|
|
|
|
|
|
/* Decide what KIPs/patches we're loading. */
|
|
|
|
const auto fs_version = ConfigureStratosphere(package2, target_firmware, emummc_enabled, nogc_enabled);
|
|
|
|
|
2021-09-01 22:05:39 +01:00
|
|
|
/* Setup exosphere. */
|
2021-09-03 04:40:15 +01:00
|
|
|
ConfigureExosphere(soc_type, target_firmware, emummc_enabled, fs_version);
|
2021-09-01 08:16:53 +01:00
|
|
|
|
2021-09-02 02:10:48 +01:00
|
|
|
/* Start CPU. */
|
|
|
|
StartCpu();
|
|
|
|
|
2021-09-03 04:40:15 +01:00
|
|
|
/* Build modified package2. */
|
|
|
|
RebuildPackage2(target_firmware, emummc_enabled);
|
2021-08-28 11:05:03 +01:00
|
|
|
|
2021-09-04 02:34:20 +01:00
|
|
|
/* Wait for confirmation that exosphere is ready. */
|
|
|
|
WaitSecureMonitorState(pkg1::SecureMonitorState_Initialized);
|
2021-08-28 01:35:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|