2021-08-21 19:10:13 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2021-08-21 19:10: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/>.
|
|
|
|
*/
|
|
|
|
#include <exosphere.hpp>
|
2021-08-23 19:57:39 +01:00
|
|
|
#include "fusee_display.hpp"
|
2021-08-23 22:18:59 +01:00
|
|
|
#include "sein/fusee_secure_initialize.hpp"
|
|
|
|
#include "sdram/fusee_sdram.hpp"
|
2021-08-24 05:15:51 +01:00
|
|
|
#include "mtc/fusee_mtc.hpp"
|
2021-08-23 22:18:59 +01:00
|
|
|
#include "fs/fusee_fs_api.hpp"
|
2021-08-24 05:15:51 +01:00
|
|
|
#include "fusee_overlay_manager.hpp"
|
2021-08-23 15:11:28 +01:00
|
|
|
#include "fusee_sd_card.hpp"
|
2021-08-23 22:18:59 +01:00
|
|
|
#include "fusee_fatal.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"
|
|
|
|
#include "fusee_secmon_sync.hpp"
|
2021-08-21 19:10:13 +01:00
|
|
|
|
|
|
|
namespace ams::nxboot {
|
|
|
|
|
2021-08-23 22:18:59 +01:00
|
|
|
namespace {
|
|
|
|
|
2021-09-05 23:45:40 +01:00
|
|
|
constexpr const char ExternalPackageFilePath[] = "sdmc:/atmosphere/package3";
|
2021-08-23 22:18:59 +01:00
|
|
|
|
2021-09-05 23:45:40 +01:00
|
|
|
constinit fs::FileHandle g_package_file;
|
2021-08-24 05:15:51 +01:00
|
|
|
|
2021-09-05 23:45:40 +01:00
|
|
|
void OpenExternalPackage() {
|
2021-08-23 22:18:59 +01:00
|
|
|
Result result;
|
|
|
|
|
2021-09-05 23:45:40 +01:00
|
|
|
/* Open external package. */
|
|
|
|
if (R_FAILED((result = fs::OpenFile(std::addressof(g_package_file), ExternalPackageFilePath, fs::OpenMode_Read)))) {
|
|
|
|
ShowFatalError("Failed to open %s!\n", ExternalPackageFilePath);
|
2021-08-23 22:18:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get file size. */
|
|
|
|
s64 file_size;
|
2021-09-05 23:45:40 +01:00
|
|
|
if (R_FAILED((result = fs::GetFileSize(std::addressof(file_size), g_package_file)))) {
|
|
|
|
ShowFatalError("Failed to get package3 size: 0x%08" PRIx32 "\n", result.GetValue());
|
2021-08-23 22:18:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check file size. */
|
2021-09-05 23:45:40 +01:00
|
|
|
if (static_cast<size_t>(file_size) != ExternalPackageSize) {
|
|
|
|
ShowFatalError("package3 seems corrupted (size 0x%zx != 0x%zx)", static_cast<size_t>(file_size), ExternalPackageSize);
|
2021-08-23 22:18:59 +01:00
|
|
|
}
|
2021-08-24 05:15:51 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 23:45:40 +01:00
|
|
|
void ReadFullExternalPackage() {
|
2021-08-24 05:15:51 +01:00
|
|
|
Result result;
|
2021-08-23 22:18:59 +01:00
|
|
|
|
2021-09-05 23:45:40 +01:00
|
|
|
if (R_FAILED((result = fs::ReadFile(g_package_file, 0, const_cast<void *>(static_cast<const void *>(std::addressof(GetExternalPackage()))), ExternalPackageSize)))) {
|
|
|
|
ShowFatalError("Failed to read %s!\n", ExternalPackageFilePath);
|
2021-08-23 22:18:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 23:45:40 +01:00
|
|
|
void CloseExternalPackage() {
|
|
|
|
fs::CloseFile(g_package_file);
|
2021-08-28 01:35:57 +01:00
|
|
|
}
|
|
|
|
|
2021-08-23 22:18:59 +01:00
|
|
|
}
|
|
|
|
|
2021-08-21 19:10:13 +01:00
|
|
|
void Main() {
|
2021-08-21 23:49:36 +01:00
|
|
|
/* Perform secure hardware initialization. */
|
2021-08-22 00:32:05 +01:00
|
|
|
SecureInitialize(true);
|
2021-08-21 23:49:36 +01:00
|
|
|
|
2021-08-23 23:10:24 +01:00
|
|
|
/* Overclock the bpmp. */
|
|
|
|
clkrst::SetBpmpClockRate(fuse::GetSocType() == fuse::SocType_Mariko ? clkrst::BpmpClockRate_589MHz : clkrst::BpmpClockRate_576MHz);
|
|
|
|
|
2021-08-22 10:32:05 +01:00
|
|
|
/* Initialize Sdram. */
|
|
|
|
InitializeSdram();
|
|
|
|
|
2021-08-23 17:13:26 +01:00
|
|
|
/* Initialize cache. */
|
|
|
|
hw::InitializeDataCache();
|
|
|
|
|
2021-08-23 15:11:28 +01:00
|
|
|
/* Initialize SD card. */
|
|
|
|
{
|
2021-08-23 22:18:59 +01:00
|
|
|
Result result = InitializeSdCard();
|
|
|
|
if (R_FAILED(result)) {
|
|
|
|
ShowFatalError("Failed to initialize the SD card: 0x%08" PRIx32 "\n", result.GetValue());
|
2021-08-23 15:11:28 +01:00
|
|
|
}
|
2021-08-23 22:18:59 +01:00
|
|
|
}
|
2021-08-23 18:13:46 +01:00
|
|
|
|
2021-08-23 22:18:59 +01:00
|
|
|
/* Mount SD card. */
|
|
|
|
if (!fs::MountSdCard()) {
|
|
|
|
ShowFatalError("Failed to mount the SD card.");
|
2021-08-23 15:11:28 +01:00
|
|
|
}
|
|
|
|
|
2021-08-23 22:18:59 +01:00
|
|
|
/* If we have a fatal error, save and display it. */
|
|
|
|
SaveAndShowFatalError();
|
|
|
|
|
2021-09-05 23:45:40 +01:00
|
|
|
/* Open the external package. */
|
|
|
|
OpenExternalPackage();
|
2021-08-24 05:15:51 +01:00
|
|
|
|
|
|
|
/* Load the memory training overlay. */
|
2021-09-05 23:45:40 +01:00
|
|
|
LoadOverlay(g_package_file, OverlayId_MemoryTraining);
|
2021-08-24 05:15:51 +01:00
|
|
|
|
|
|
|
/* Do memory training. */
|
|
|
|
DoMemoryTraining();
|
|
|
|
|
|
|
|
/* Read the rest of the archive file. */
|
2021-09-05 23:45:40 +01:00
|
|
|
ReadFullExternalPackage();
|
2021-08-23 22:18:59 +01:00
|
|
|
|
2021-09-03 08:40:26 +01:00
|
|
|
/* Save the memory training overlay. */
|
|
|
|
SaveMemoryTrainingOverlay();
|
|
|
|
|
2021-08-23 23:10:24 +01:00
|
|
|
/* Initialize display (splash screen will be visible from this point onwards). */
|
|
|
|
InitializeDisplay();
|
|
|
|
ShowDisplay();
|
|
|
|
|
2021-09-05 23:45:40 +01:00
|
|
|
/* Close the external package. */
|
|
|
|
CloseExternalPackage();
|
2021-08-28 01:35:57 +01:00
|
|
|
|
|
|
|
/* Perform rest of the boot process. */
|
|
|
|
SetupAndStartHorizon();
|
|
|
|
|
2021-09-03 08:40:26 +01:00
|
|
|
/* Restore the memory training overlay. */
|
|
|
|
RestoreMemoryTrainingOverlay();
|
|
|
|
|
|
|
|
/* Restore memory clock rate. */
|
|
|
|
RestoreMemoryClockRate();
|
|
|
|
|
2021-09-04 02:34:20 +01:00
|
|
|
/* Restore secure monitor code. */
|
|
|
|
RestoreSecureMonitorOverlay();
|
|
|
|
|
2021-08-28 01:35:57 +01:00
|
|
|
/* Finalize display. */
|
|
|
|
FinalizeDisplay();
|
2021-08-23 22:18:59 +01:00
|
|
|
|
2021-09-05 05:43:19 +01:00
|
|
|
/* Finalize sd card. */
|
|
|
|
FinalizeSdCard();
|
|
|
|
|
2021-08-28 01:35:57 +01:00
|
|
|
/* Finalize the data cache. */
|
|
|
|
hw::FinalizeDataCache();
|
|
|
|
|
|
|
|
/* Downclock the bpmp. */
|
|
|
|
clkrst::SetBpmpClockRate(clkrst::BpmpClockRate_408MHz);
|
|
|
|
|
|
|
|
/* Signal to the secure monitor that we're done. */
|
|
|
|
SetBootloaderState(pkg1::BootloaderState_Done);
|
|
|
|
|
|
|
|
/* Halt ourselves. */
|
|
|
|
while (true) {
|
|
|
|
reg::Write(secmon::MemoryRegionPhysicalDeviceFlowController.GetAddress() + FLOW_CTLR_HALT_COP_EVENTS, FLOW_REG_BITS_ENUM(HALT_COP_EVENTS_MODE, FLOW_MODE_STOP),
|
|
|
|
FLOW_REG_BITS_ENUM(HALT_COP_EVENTS_JTAG, ENABLED));
|
|
|
|
}
|
2021-08-21 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|