mirror of
https://github.com/Atmosphere-NX/Atmosphere
synced 2024-12-22 12:21:18 +00:00
fusee_cpp: implement warmboot firmware load
This commit is contained in:
parent
4355a2b036
commit
1dd0297db3
3 changed files with 182 additions and 2 deletions
|
@ -30,6 +30,7 @@ namespace ams::nxboot {
|
|||
namespace {
|
||||
|
||||
constexpr inline const uintptr_t CLKRST = secmon::MemoryRegionPhysicalDeviceClkRst.GetAddress();
|
||||
constexpr inline const uintptr_t PMC = secmon::MemoryRegionPhysicalDevicePmc.GetAddress();
|
||||
constexpr inline const uintptr_t MC = secmon::MemoryRegionPhysicalDeviceMemoryController.GetAddress();
|
||||
|
||||
constinit secmon::EmummcConfiguration g_emummc_cfg = {};
|
||||
|
@ -367,6 +368,180 @@ namespace ams::nxboot {
|
|||
return package2;
|
||||
}
|
||||
|
||||
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, 0xD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E, 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>();
|
||||
size_t warmboot_size = std::min(sizeof(GetSecondaryArchive().warmboot), secmon::MemoryRegionPhysicalIramWarmbootBin.GetSize());
|
||||
if (soc_type == fuse::SocType_Erista) {
|
||||
/* Copy the ams warmboot binary. */
|
||||
std::memcpy(warmboot_dst, GetSecondaryArchive().warmboot, warmboot_size);
|
||||
|
||||
/* 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));
|
||||
}
|
||||
} 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);
|
||||
void *tmp = AllocateMemory(warmboot_src_size);
|
||||
|
||||
/* 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SetupAndStartHorizon() {
|
||||
|
@ -388,13 +563,13 @@ namespace ams::nxboot {
|
|||
|
||||
/* Get target firmware. */
|
||||
const auto target_firmware = GetTargetFirmware(package1);
|
||||
AMS_UNUSED(target_firmware);
|
||||
|
||||
/* Read/decrypt package2. */
|
||||
u8 * const package2 = LoadBootConfigAndPackage2();
|
||||
AMS_UNUSED(package2);
|
||||
|
||||
/* TODO: Setup warmboot firmware. */
|
||||
/* Setup warmboot firmware. */
|
||||
LoadWarmbootFirmware(soc_type, target_firmware, package1);
|
||||
|
||||
/* TODO: Setup exosphere. */
|
||||
|
||||
|
|
|
@ -113,6 +113,7 @@ namespace ams::fuse {
|
|||
|
||||
SocType GetSocType();
|
||||
int GetExpectedFuseVersion(TargetFirmware target_fw);
|
||||
int GetFuseVersion();
|
||||
bool HasRcmVulnerabilityPatch();
|
||||
|
||||
bool IsOdmProductionMode();
|
||||
|
|
|
@ -394,6 +394,10 @@ namespace ams::fuse {
|
|||
return GetExpectedFuseVersionImpl(target_fw);
|
||||
}
|
||||
|
||||
int GetFuseVersion() {
|
||||
return util::PopCount(GetCommonOdmWord(7));
|
||||
}
|
||||
|
||||
bool HasRcmVulnerabilityPatch() {
|
||||
/* Only check for RCM bug patch once, and cache our result. */
|
||||
if (!g_checked_for_rcm_bug_patch) {
|
||||
|
|
Loading…
Reference in a new issue