From 80999988d40bd3e9278d8989950e12b77e737bd3 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Fri, 27 Aug 2021 17:35:57 -0700 Subject: [PATCH] fusee_cpp: skeleton the remaining code flow --- fusee_cpp/program/program_ovl.ld | 2 + fusee_cpp/program/source/fusee_main.cpp | 32 ++++++++++++-- .../program/source/fusee_secmon_sync.cpp | 44 +++++++++++++++++++ .../program/source/fusee_secmon_sync.hpp | 24 ++++++++++ .../program/source/fusee_setup_horizon.cpp | 27 ++++++++++++ .../program/source/fusee_setup_horizon.hpp | 23 ++++++++++ .../program/source/sdram/fusee_sdram.cpp | 3 ++ 7 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 fusee_cpp/program/source/fusee_secmon_sync.cpp create mode 100644 fusee_cpp/program/source/fusee_secmon_sync.hpp create mode 100644 fusee_cpp/program/source/fusee_setup_horizon.cpp create mode 100644 fusee_cpp/program/source/fusee_setup_horizon.hpp diff --git a/fusee_cpp/program/program_ovl.ld b/fusee_cpp/program/program_ovl.ld index b65eb2ca4..9dfe1ce09 100644 --- a/fusee_cpp/program/program_ovl.ld +++ b/fusee_cpp/program/program_ovl.ld @@ -23,6 +23,7 @@ SECTIONS { BYTE(0x00); } .ovl_mtc_erista { + KEEP(*(.text._ZN3ams6nxboot22DoMemoryTrainingEristaEv)) fusee_mtc_erista.o(.text*); fusee_mtc_erista.o(.rodata*); fusee_mtc_erista.o(.data*); @@ -32,6 +33,7 @@ SECTIONS { BYTE(0x00); } .ovl_mtc_mariko { + KEEP(*(.text._ZN3ams6nxboot22DoMemoryTrainingMarikoEv)) fusee_mtc_mariko.o(.text*); fusee_mtc_mariko.o(.rodata*); fusee_mtc_mariko.o(.data*); diff --git a/fusee_cpp/program/source/fusee_main.cpp b/fusee_cpp/program/source/fusee_main.cpp index e18a04462..c0fce8e6a 100644 --- a/fusee_cpp/program/source/fusee_main.cpp +++ b/fusee_cpp/program/source/fusee_main.cpp @@ -23,6 +23,8 @@ #include "fusee_sd_card.hpp" #include "fusee_fatal.hpp" #include "fusee_secondary_archive.hpp" +#include "fusee_setup_horizon.hpp" +#include "fusee_secmon_sync.hpp" namespace ams::nxboot { @@ -61,6 +63,10 @@ namespace ams::nxboot { } } + void CloseSecondaryArchive() { + fs::CloseFile(g_archive_file); + } + } void Main() { @@ -108,11 +114,29 @@ namespace ams::nxboot { InitializeDisplay(); ShowDisplay(); - /* TODO */ - WaitForReboot(); + /* Close the secondary archive. */ + CloseSecondaryArchive(); - /* TODO */ - AMS_INFINITE_LOOP(); + /* Perform rest of the boot process. */ + SetupAndStartHorizon(); + + /* Finalize display. */ + FinalizeDisplay(); + + /* 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)); + } } } diff --git a/fusee_cpp/program/source/fusee_secmon_sync.cpp b/fusee_cpp/program/source/fusee_secmon_sync.cpp new file mode 100644 index 000000000..d0a8d0219 --- /dev/null +++ b/fusee_cpp/program/source/fusee_secmon_sync.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * 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 . + */ +#include +#include "fusee_secmon_sync.hpp" + +namespace ams::nxboot { + + namespace { + + ALWAYS_INLINE pkg1::SecureMonitorParameters &GetSecureMonitorParameters() { + return *secmon::MemoryRegionPhysicalDeviceBootloaderParams.GetPointer(); + } + + } + + void WaitSecureMonitorState(pkg1::SecureMonitorState state) { + auto &secmon_params = GetSecureMonitorParameters(); + + while (secmon_params.secmon_state != state) { + hw::InvalidateDataCache(std::addressof(secmon_params.secmon_state), sizeof(secmon_params.secmon_state)); + util::WaitMicroSeconds(1); + } + } + + void SetBootloaderState(pkg1::BootloaderState state) { + auto &secmon_params = GetSecureMonitorParameters(); + secmon_params.bootloader_state = state; + hw::FlushDataCache(std::addressof(secmon_params.bootloader_state), sizeof(secmon_params.bootloader_state)); + } + +} \ No newline at end of file diff --git a/fusee_cpp/program/source/fusee_secmon_sync.hpp b/fusee_cpp/program/source/fusee_secmon_sync.hpp new file mode 100644 index 000000000..570e0ee39 --- /dev/null +++ b/fusee_cpp/program/source/fusee_secmon_sync.hpp @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * 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 . + */ +#include +#pragma once + +namespace ams::nxboot { + + void WaitSecureMonitorState(pkg1::SecureMonitorState state); + void SetBootloaderState(pkg1::BootloaderState state); + +} \ No newline at end of file diff --git a/fusee_cpp/program/source/fusee_setup_horizon.cpp b/fusee_cpp/program/source/fusee_setup_horizon.cpp new file mode 100644 index 000000000..c34a983a9 --- /dev/null +++ b/fusee_cpp/program/source/fusee_setup_horizon.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * 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 . + */ +#include +#include "fusee_setup_horizon.hpp" +#include "fusee_fatal.hpp" + +namespace ams::nxboot { + + void SetupAndStartHorizon() { + /* TODO */ + ShowFatalError("SetupAndStartHorizon not fully implemented\n"); + } + +} \ No newline at end of file diff --git a/fusee_cpp/program/source/fusee_setup_horizon.hpp b/fusee_cpp/program/source/fusee_setup_horizon.hpp new file mode 100644 index 000000000..70cddc3b1 --- /dev/null +++ b/fusee_cpp/program/source/fusee_setup_horizon.hpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * 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 . + */ +#include +#pragma once + +namespace ams::nxboot { + + void SetupAndStartHorizon(); + +} \ No newline at end of file diff --git a/fusee_cpp/program/source/sdram/fusee_sdram.cpp b/fusee_cpp/program/source/sdram/fusee_sdram.cpp index 79617f779..e124eb3ae 100644 --- a/fusee_cpp/program/source/sdram/fusee_sdram.cpp +++ b/fusee_cpp/program/source/sdram/fusee_sdram.cpp @@ -1099,6 +1099,9 @@ namespace ams::nxboot { } else /* if (soc_type == fuse::SocType_Mariko) */ { InitializeSdram(sdram_params); } + + /* Lock DRAM scratch. */ + pmc::LockSecureRegister(pmc::SecureRegister_DramParameters); } }