2019-06-22 09:10:21 +02:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-06-22 09:10:21 +02: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/>.
|
|
|
|
*/
|
2020-05-12 00:02:10 +02:00
|
|
|
#include <stratosphere.hpp>
|
2019-06-22 09:10:21 +02:00
|
|
|
#include "boot_power_utils.hpp"
|
2020-11-08 13:16:50 +01:00
|
|
|
#include "boot_pmic_driver.hpp"
|
2021-09-06 00:45:40 +02:00
|
|
|
#include "fusee_bin.h"
|
2019-06-22 09:10:21 +02:00
|
|
|
|
2019-10-24 11:30:10 +02:00
|
|
|
namespace ams::boot {
|
2019-06-22 09:10:21 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Convenience definitions. */
|
|
|
|
constexpr uintptr_t IramBase = 0x40000000ull;
|
|
|
|
constexpr uintptr_t IramPayloadBase = 0x40010000ull;
|
|
|
|
constexpr size_t IramSize = 0x40000;
|
2020-11-15 10:36:50 +01:00
|
|
|
constexpr size_t IramPayloadMaxSize = 0x24000;
|
|
|
|
constexpr size_t IramFatalErrorContextOffset = 0x2E000;
|
2019-06-22 09:10:21 +02:00
|
|
|
|
|
|
|
/* Globals. */
|
2019-12-06 08:41:33 +01:00
|
|
|
alignas(os::MemoryPageSize) u8 g_work_page[os::MemoryPageSize];
|
2019-06-22 09:10:21 +02:00
|
|
|
|
|
|
|
/* Helpers. */
|
|
|
|
void ClearIram() {
|
|
|
|
/* Make page FFs. */
|
2019-10-20 02:42:53 +02:00
|
|
|
std::memset(g_work_page, 0xFF, sizeof(g_work_page));
|
2019-06-22 09:10:21 +02:00
|
|
|
|
|
|
|
/* Overwrite all of IRAM with FFs. */
|
|
|
|
for (size_t ofs = 0; ofs < IramSize; ofs += sizeof(g_work_page)) {
|
2019-10-24 11:30:10 +02:00
|
|
|
exosphere::CopyToIram(IramBase + ofs, g_work_page, sizeof(g_work_page));
|
2019-06-22 09:10:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:58:13 +01:00
|
|
|
void DoRebootToPayload() {
|
|
|
|
/* Ensure clean IRAM state. */
|
|
|
|
ClearIram();
|
|
|
|
|
|
|
|
/* Copy in payload. */
|
2021-09-06 00:45:40 +02:00
|
|
|
for (size_t ofs = 0; ofs < fusee_bin_size; ofs += sizeof(g_work_page)) {
|
2021-10-09 21:45:31 +02:00
|
|
|
std::memcpy(g_work_page, fusee_bin + ofs, std::min(static_cast<size_t>(fusee_bin_size - ofs), sizeof(g_work_page)));
|
2020-11-15 21:58:13 +01:00
|
|
|
exosphere::CopyToIram(IramPayloadBase + ofs, g_work_page, sizeof(g_work_page));
|
|
|
|
}
|
|
|
|
|
|
|
|
exosphere::ForceRebootToIramPayload();
|
|
|
|
}
|
|
|
|
|
2020-11-22 03:27:48 +01:00
|
|
|
void DoRebootToFatalError(const ams::FatalErrorContext *ctx) {
|
2019-06-22 09:10:21 +02:00
|
|
|
/* Ensure clean IRAM state. */
|
|
|
|
ClearIram();
|
|
|
|
|
|
|
|
/* Copy in payload. */
|
2021-09-06 00:45:40 +02:00
|
|
|
for (size_t ofs = 0; ofs < fusee_bin_size; ofs += sizeof(g_work_page)) {
|
2021-10-09 21:45:31 +02:00
|
|
|
std::memcpy(g_work_page, fusee_bin + ofs, std::min(static_cast<size_t>(fusee_bin_size - ofs), sizeof(g_work_page)));
|
2019-12-06 08:41:33 +01:00
|
|
|
exosphere::CopyToIram(IramPayloadBase + ofs, g_work_page, sizeof(g_work_page));
|
2019-06-22 09:10:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Copy in fatal error context, if relevant. */
|
|
|
|
if (ctx != nullptr) {
|
|
|
|
std::memset(g_work_page, 0xCC, sizeof(g_work_page));
|
|
|
|
std::memcpy(g_work_page, ctx, sizeof(*ctx));
|
2020-11-15 10:36:50 +01:00
|
|
|
exosphere::CopyToIram(IramPayloadBase + IramFatalErrorContextOffset, g_work_page, sizeof(g_work_page));
|
2019-06-22 09:10:21 +02:00
|
|
|
}
|
|
|
|
|
2020-11-15 21:58:13 +01:00
|
|
|
exosphere::ForceRebootToFatalError();
|
2019-06-22 09:10:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void RebootSystem() {
|
2020-11-08 13:16:50 +01:00
|
|
|
if (spl::GetSocType() == spl::SocType_Erista) {
|
2020-11-15 21:58:13 +01:00
|
|
|
DoRebootToPayload();
|
2020-11-08 13:16:50 +01:00
|
|
|
} else {
|
|
|
|
/* On Mariko, we can't reboot to payload, so we should just do a reboot. */
|
|
|
|
PmicDriver().RebootSystem();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShutdownSystem() {
|
|
|
|
PmicDriver().ShutdownSystem();
|
2019-06-22 09:10:21 +02:00
|
|
|
}
|
|
|
|
|
2020-04-23 01:22:14 +02:00
|
|
|
void SetInitialRebootPayload() {
|
2021-09-06 00:45:40 +02:00
|
|
|
::ams::SetInitialRebootPayload(fusee_bin, fusee_bin_size);
|
2020-04-23 01:22:14 +02:00
|
|
|
}
|
|
|
|
|
2019-10-20 02:42:53 +02:00
|
|
|
void RebootForFatalError(ams::FatalErrorContext *ctx) {
|
2020-11-15 21:58:13 +01:00
|
|
|
DoRebootToFatalError(ctx);
|
2019-06-22 09:10:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|