2019-05-03 03:33:12 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-05-03 03:33:12 +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/>.
|
|
|
|
*/
|
2020-05-11 23:02:10 +01:00
|
|
|
#include <stratosphere.hpp>
|
2019-06-22 08:10:21 +01:00
|
|
|
#include "boot_boot_reason.hpp"
|
2019-05-03 03:33:12 +01:00
|
|
|
#include "boot_pmic_driver.hpp"
|
|
|
|
#include "boot_rtc_driver.hpp"
|
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
namespace ams::boot {
|
2019-05-03 03:33:12 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Globals. */
|
2020-11-08 12:16:50 +00:00
|
|
|
constinit spl::BootReason g_boot_reason = spl::BootReason_Unknown;
|
|
|
|
constinit bool g_detected_boot_reason = false;
|
2019-06-22 08:10:21 +01:00
|
|
|
|
|
|
|
/* Helpers. */
|
2020-11-08 12:16:50 +00:00
|
|
|
spl::BootReason DetectBootReason(u32 power_intr, u8 rtc_intr, u8 nv_erc, bool ac_ok) {
|
2019-06-22 08:10:21 +01:00
|
|
|
if (power_intr & 0x08) {
|
2020-11-08 12:16:50 +00:00
|
|
|
return spl::BootReason_OnKey;
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
|
|
|
if (rtc_intr & 0x02) {
|
2020-11-08 12:16:50 +00:00
|
|
|
return spl::BootReason_RtcAlarm1;
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
|
|
|
if (power_intr & 0x80) {
|
2020-11-08 12:16:50 +00:00
|
|
|
return spl::BootReason_AcOk;
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
|
|
|
if (rtc_intr & 0x04) {
|
2019-06-22 19:34:18 +01:00
|
|
|
if (nv_erc != 0x80 && !spl::IsRecoveryBoot()) {
|
2020-11-08 12:16:50 +00:00
|
|
|
return spl::BootReason_RtcAlarm2;
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((nv_erc & 0x40) && ac_ok) {
|
2020-11-08 12:16:50 +00:00
|
|
|
return spl::BootReason_AcOk;
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
2020-11-08 12:16:50 +00:00
|
|
|
return spl::BootReason_Unknown;
|
2019-05-03 03:33:12 +01:00
|
|
|
}
|
2019-06-22 08:10:21 +01:00
|
|
|
|
2019-05-03 03:33:12 +01:00
|
|
|
}
|
2019-06-22 08:10:21 +01:00
|
|
|
|
|
|
|
void DetectBootReason() {
|
|
|
|
u8 power_intr;
|
|
|
|
u8 rtc_intr;
|
|
|
|
u8 rtc_intr_m;
|
|
|
|
u8 nv_erc;
|
|
|
|
bool ac_ok;
|
|
|
|
|
|
|
|
/* Get values from PMIC. */
|
|
|
|
{
|
|
|
|
PmicDriver pmic_driver;
|
2020-11-08 12:16:50 +00:00
|
|
|
R_ABORT_UNLESS(pmic_driver.GetOnOffIrq(std::addressof(power_intr)));
|
|
|
|
R_ABORT_UNLESS(pmic_driver.GetNvErc(std::addressof(nv_erc)));
|
|
|
|
R_ABORT_UNLESS(pmic_driver.GetAcOk(std::addressof(ac_ok)));
|
2019-05-03 03:33:12 +01:00
|
|
|
}
|
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Get values from RTC. */
|
|
|
|
{
|
|
|
|
RtcDriver rtc_driver;
|
2020-11-08 12:16:50 +00:00
|
|
|
R_ABORT_UNLESS(rtc_driver.GetRtcIntr(std::addressof(rtc_intr)));
|
|
|
|
R_ABORT_UNLESS(rtc_driver.GetRtcIntrM(std::addressof(rtc_intr_m)));
|
2019-05-03 03:33:12 +01:00
|
|
|
}
|
2019-06-22 08:10:21 +01:00
|
|
|
|
|
|
|
/* Set global derived boot reason. */
|
2020-11-08 12:16:50 +00:00
|
|
|
g_boot_reason = DetectBootReason(power_intr, rtc_intr & ~rtc_intr_m, nv_erc, ac_ok);
|
2019-06-22 08:10:21 +01:00
|
|
|
|
|
|
|
/* Set boot reason for SPL. */
|
2020-04-14 06:19:44 +01:00
|
|
|
if (hos::GetVersion() >= hos::Version_3_0_0) {
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Create the boot reason value. */
|
2020-05-11 23:04:51 +01:00
|
|
|
spl::BootReasonValue boot_reason_value = {};
|
|
|
|
boot_reason_value.power_intr = power_intr;
|
|
|
|
boot_reason_value.rtc_intr = rtc_intr & ~rtc_intr_m;
|
|
|
|
boot_reason_value.nv_erc = nv_erc;
|
2019-06-22 08:10:21 +01:00
|
|
|
boot_reason_value.boot_reason = g_boot_reason;
|
2020-05-11 23:04:51 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Set the boot reason value. */
|
2020-05-11 23:04:51 +01:00
|
|
|
R_ABORT_UNLESS(spl::SetBootReason(boot_reason_value));
|
2019-05-03 03:33:12 +01:00
|
|
|
}
|
2019-06-22 08:10:21 +01:00
|
|
|
|
|
|
|
g_detected_boot_reason = true;
|
2019-05-03 03:33:12 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
spl::BootReason GetBootReason() {
|
2020-02-23 07:05:14 +00:00
|
|
|
AMS_ABORT_UNLESS(g_detected_boot_reason);
|
2019-06-22 08:10:21 +01:00
|
|
|
return g_boot_reason;
|
2019-05-03 03:33:12 +01:00
|
|
|
}
|
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|