Atmosphere/stratosphere/fatal/source/fatal_repair.cpp

107 lines
3.2 KiB
C++
Raw Normal View History

/*
2019-04-08 04:00:49 +02:00
* Copyright (c) 2018-2019 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 <http://www.gnu.org/licenses/>.
*/
2019-06-18 01:41:03 +02:00
#include <switch.h>
#include <stratosphere.hpp>
#include "fatal_types.hpp"
#include "fatal_repair.hpp"
#include "fatal_throw.hpp"
static bool InRepairWithoutVolHeld() {
if (GetRuntimeFirmwareVersion() < FirmwareVersion_300) {
return false;
}
2019-06-18 01:41:03 +02:00
bool in_repair;
if (R_FAILED(setsysGetFlag(SetSysFlag_InRepairProcessEnable, &in_repair)) || !in_repair) {
return false;
}
2019-06-18 01:41:03 +02:00
{
GpioPadSession vol_btn;
2018-11-10 22:38:17 +01:00
if (R_FAILED(gpioOpenSession(&vol_btn, GpioPadName_ButtonVolUp))) {
return true;
}
2019-06-18 01:41:03 +02:00
2018-11-10 22:38:17 +01:00
/* Ensure we close even on early return. */
ON_SCOPE_EXIT { gpioPadClose(&vol_btn); };
2019-06-18 01:41:03 +02:00
2018-11-10 22:38:17 +01:00
/* Set direction input. */
gpioPadSetDirection(&vol_btn, GpioDirection_Input);
/* Ensure that we're holding the volume button for a full second. */
TimeoutHelper timeout_helper(1000000000UL);
while (!timeout_helper.TimedOut()) {
GpioValue val;
if (R_FAILED(gpioPadGetValue(&vol_btn, &val)) || val != GpioValue_Low) {
return true;
}
2019-06-18 01:41:03 +02:00
2018-11-10 22:38:17 +01:00
/* Sleep for 100 ms. */
svcSleepThread(100000000UL);
}
}
2019-06-18 01:41:03 +02:00
return false;
}
static bool InRepairWithoutTimeReviserCartridge() {
if (GetRuntimeFirmwareVersion() < FirmwareVersion_500) {
return false;
}
2019-06-18 01:41:03 +02:00
bool requires_time_reviser;
if (R_FAILED(setsysGetFlag(SetSysFlag_RequiresRunRepairTimeReviser, &requires_time_reviser)) || !requires_time_reviser) {
return false;
}
2019-06-18 01:41:03 +02:00
2018-11-10 22:38:17 +01:00
FsGameCardHandle gc_hnd;
u8 gc_attr;
{
FsDeviceOperator devop;
if (R_FAILED(fsOpenDeviceOperator(&devop))) {
return true;
}
2019-06-18 01:41:03 +02:00
2018-11-10 22:38:17 +01:00
/* Ensure we close even on early return. */
ON_SCOPE_EXIT { fsDeviceOperatorClose(&devop); };
2019-06-18 01:41:03 +02:00
2018-11-10 22:38:17 +01:00
/* Check that a gamecard is inserted. */
bool inserted;
if (R_FAILED(fsDeviceOperatorIsGameCardInserted(&devop, &inserted)) || !inserted) {
return true;
}
2019-06-18 01:41:03 +02:00
2018-11-10 22:38:17 +01:00
/* 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 true;
}
}
2019-06-18 01:41:03 +02:00
2018-11-10 22:38:17 +01:00
/* Check that the gamecard is a repair tool. */
return (gc_attr & FsGameCardAttribute_Repair) == FsGameCardAttribute_Repair;
}
void CheckRepairStatus() {
if (InRepairWithoutVolHeld()) {
2019-03-28 23:37:50 +01:00
ThrowFatalForSelf(ResultFatalInRepairWithoutVolHeld);
}
2019-06-18 01:41:03 +02:00
if (InRepairWithoutTimeReviserCartridge()) {
2019-03-28 23:37:50 +01:00
ThrowFatalForSelf(ResultFatalInRepairWithoutTimeReviserCartridge);
}
}