2018-11-10 20:56:43 +00:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2018-11-10 20:56:43 +00: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>
|
2018-11-10 20:56:43 +00:00
|
|
|
#include "fatal_repair.hpp"
|
2019-07-19 03:09:35 +01:00
|
|
|
#include "fatal_service_for_self.hpp"
|
2018-11-10 20:56:43 +00:00
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
namespace ams::fatal::srv {
|
2019-06-18 00:41:03 +01:00
|
|
|
|
2019-07-19 03:09:35 +01:00
|
|
|
namespace {
|
2019-06-18 00:41:03 +01:00
|
|
|
|
2019-07-19 03:09:35 +01:00
|
|
|
bool IsInRepair() {
|
|
|
|
/* Before firmware 3.0.0, this wasn't implemented. */
|
2020-04-14 06:19:44 +01:00
|
|
|
if (hos::GetVersion() < hos::Version_3_0_0) {
|
2019-07-19 03:09:35 +01:00
|
|
|
return false;
|
2018-11-10 21:38:17 +00:00
|
|
|
}
|
2019-06-18 00:41:03 +01:00
|
|
|
|
2019-07-19 03:09:35 +01:00
|
|
|
bool in_repair;
|
2019-10-18 18:37:03 +01:00
|
|
|
return R_SUCCEEDED(setsysGetInRepairProcessEnableFlag(&in_repair)) && in_repair;
|
2018-11-30 13:33:35 +00:00
|
|
|
}
|
2019-06-18 00:41:03 +01:00
|
|
|
|
2019-07-19 03:09:35 +01:00
|
|
|
bool IsInRepairWithoutVolHeld() {
|
|
|
|
if (IsInRepair()) {
|
2020-10-30 18:54:30 +00:00
|
|
|
gpio::GpioPadSession vol_btn;
|
|
|
|
if (R_FAILED(gpio::OpenSession(std::addressof(vol_btn), gpio::DeviceCode_ButtonVolUp))) {
|
2019-07-19 03:09:35 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure we close even on early return. */
|
2020-10-30 18:54:30 +00:00
|
|
|
ON_SCOPE_EXIT { gpio::CloseSession(std::addressof(vol_btn)); };
|
2019-07-19 03:09:35 +01:00
|
|
|
|
|
|
|
/* Set direction input. */
|
2020-10-30 18:54:30 +00:00
|
|
|
gpio::SetDirection(std::addressof(vol_btn), gpio::Direction_Input);
|
2019-07-19 03:09:35 +01:00
|
|
|
|
|
|
|
/* Ensure that we're holding the volume button for a full second. */
|
2020-04-08 10:21:35 +01:00
|
|
|
auto start = os::GetSystemTick();
|
|
|
|
do {
|
2020-10-30 18:54:30 +00:00
|
|
|
if (gpio::GetValue(std::addressof(vol_btn)) != gpio::GpioValue_Low) {
|
2019-07-19 03:09:35 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sleep for 100 ms. */
|
2020-04-08 10:21:35 +01:00
|
|
|
os::SleepThread(TimeSpan::FromMilliSeconds(100));
|
|
|
|
} while (os::ConvertToTimeSpan(os::GetSystemTick() - start) < TimeSpan::FromSeconds(1));
|
2019-07-19 03:09:35 +01:00
|
|
|
}
|
2018-11-10 21:15:48 +00:00
|
|
|
|
2019-07-19 03:09:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-06-18 00:41:03 +01:00
|
|
|
|
2019-07-19 03:09:35 +01:00
|
|
|
bool NeedsRunTimeReviser() {
|
|
|
|
/* Before firmware 5.0.0, this wasn't implemented. */
|
2020-04-14 06:19:44 +01:00
|
|
|
if (hos::GetVersion() < hos::Version_5_0_0) {
|
2019-07-19 03:09:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-06-18 00:41:03 +01:00
|
|
|
|
2019-07-19 03:09:35 +01:00
|
|
|
bool requires_time_reviser;
|
2019-10-18 18:37:03 +01:00
|
|
|
return R_SUCCEEDED(setsysGetRequiresRunRepairTimeReviser(&requires_time_reviser)) && requires_time_reviser;
|
2018-11-10 21:38:17 +00:00
|
|
|
}
|
2019-06-18 00:41:03 +01:00
|
|
|
|
2019-07-19 03:09:35 +01:00
|
|
|
bool IsTimeReviserCartridgeInserted() {
|
|
|
|
FsGameCardHandle gc_hnd;
|
|
|
|
u8 gc_attr;
|
|
|
|
{
|
|
|
|
FsDeviceOperator devop;
|
|
|
|
if (R_FAILED(fsOpenDeviceOperator(&devop))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure we close even on early return. */
|
|
|
|
ON_SCOPE_EXIT { fsDeviceOperatorClose(&devop); };
|
|
|
|
|
|
|
|
/* Check that a gamecard is inserted. */
|
|
|
|
bool inserted;
|
|
|
|
if (R_FAILED(fsDeviceOperatorIsGameCardInserted(&devop, &inserted)) || !inserted) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that we can retrieve the gamecard's attributes. */
|
|
|
|
if (R_FAILED(fsDeviceOperatorGetGameCardHandle(&devop, &gc_hnd)) || R_FAILED(fsDeviceOperatorGetGameCardAttribute(&devop, &gc_hnd, &gc_attr))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-06-18 00:41:03 +01:00
|
|
|
|
2019-07-19 03:09:35 +01:00
|
|
|
/* Check that the gamecard is a repair tool. */
|
2019-11-21 08:28:32 +00:00
|
|
|
return (gc_attr & FsGameCardAttribute_RepairToolFlag);
|
2018-11-10 21:38:17 +00:00
|
|
|
}
|
2019-06-18 00:41:03 +01:00
|
|
|
|
2019-07-19 03:09:35 +01:00
|
|
|
bool IsInRepairWithoutTimeReviserCartridge() {
|
|
|
|
return NeedsRunTimeReviser() && IsTimeReviserCartridgeInserted();
|
2018-11-10 21:38:17 +00:00
|
|
|
}
|
2019-07-19 03:09:35 +01:00
|
|
|
|
2018-11-10 21:38:17 +00:00
|
|
|
}
|
2019-06-18 00:41:03 +01:00
|
|
|
|
2019-07-19 03:09:35 +01:00
|
|
|
void CheckRepairStatus() {
|
|
|
|
if (IsInRepairWithoutVolHeld()) {
|
2019-10-24 09:40:44 +01:00
|
|
|
ThrowFatalForSelf(ResultInRepairWithoutVolHeld());
|
2019-07-19 03:09:35 +01:00
|
|
|
}
|
2018-11-10 21:15:48 +00:00
|
|
|
|
2019-07-19 03:09:35 +01:00
|
|
|
if (IsInRepairWithoutTimeReviserCartridge()) {
|
2019-10-24 09:40:44 +01:00
|
|
|
ThrowFatalForSelf(ResultInRepairWithoutTimeReviserCartridge());
|
2019-07-19 03:09:35 +01:00
|
|
|
}
|
2018-11-10 21:15:48 +00:00
|
|
|
}
|
2019-06-18 00:41:03 +01:00
|
|
|
|
2018-11-10 20:56:43 +00:00
|
|
|
}
|