ams.mitm: add bpc handler, for reboot power button stuff
This commit is contained in:
34
stratosphere/ams_mitm/source/bpc_mitm/bpc_mitm_service.cpp
Normal file
34
stratosphere/ams_mitm/source/bpc_mitm/bpc_mitm_service.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) 2018 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/>.
|
||||
*/
|
||||
|
||||
#include <mutex>
|
||||
#include <switch.h>
|
||||
#include <stratosphere.hpp>
|
||||
#include "bpc_mitm_service.hpp"
|
||||
#include "bpcmitm_reboot_manager.hpp"
|
||||
|
||||
void BpcMitmService::PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx) {
|
||||
/* Nothing to do here */
|
||||
}
|
||||
|
||||
Result BpcMitmService::ShutdownSystem() {
|
||||
/* TODO: Use exosphere + reboot to perform real shutdown, instead of fake shutdown. */
|
||||
return RESULT_FORWARD_TO_SESSION;
|
||||
}
|
||||
|
||||
Result BpcMitmService::RebootSystem() {
|
||||
return BpcRebootManager::PerformReboot();
|
||||
}
|
||||
52
stratosphere/ams_mitm/source/bpc_mitm/bpc_mitm_service.hpp
Normal file
52
stratosphere/ams_mitm/source/bpc_mitm/bpc_mitm_service.hpp
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2018 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <switch.h>
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
enum BpcCmd : u32 {
|
||||
BpcCmd_ShutdownSystem = 0,
|
||||
BpcCmd_RebootSystem = 1,
|
||||
};
|
||||
|
||||
class BpcMitmService : public IMitmServiceObject {
|
||||
public:
|
||||
BpcMitmService(std::shared_ptr<Service> s, u64 pid) : IMitmServiceObject(s, pid) {
|
||||
/* ... */
|
||||
}
|
||||
|
||||
static bool ShouldMitm(u64 pid, u64 tid) {
|
||||
/* We will mitm:
|
||||
* - am, to intercept the Reboot/Power buttons in the overlay menu.
|
||||
* - fatal, to simplify payload reboot logic significantly
|
||||
* - applications, to allow homebrew to take advantage of the feature.
|
||||
*/
|
||||
return tid == 0x0100000000000023ull || tid == 0x0100000000000034ull || tid >= 0x0100000000010000ull;
|
||||
}
|
||||
|
||||
static void PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx);
|
||||
|
||||
protected:
|
||||
/* Overridden commands. */
|
||||
Result ShutdownSystem();
|
||||
Result RebootSystem();
|
||||
public:
|
||||
DEFINE_SERVICE_DISPATCH_TABLE {
|
||||
MakeServiceCommandMeta<BpcCmd_ShutdownSystem, &BpcMitmService::ShutdownSystem>(),
|
||||
MakeServiceCommandMeta<BpcCmd_RebootSystem, &BpcMitmService::RebootSystem>(),
|
||||
};
|
||||
};
|
||||
55
stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_main.cpp
Normal file
55
stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_main.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 2018 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/>.
|
||||
*/
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <malloc.h>
|
||||
|
||||
#include <switch.h>
|
||||
#include <atmosphere.h>
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
#include "bpcmitm_main.hpp"
|
||||
#include "bpc_mitm_service.hpp"
|
||||
#include "bpcmitm_reboot_manager.hpp"
|
||||
|
||||
#include "../utils.hpp"
|
||||
|
||||
void BpcMitmMain(void *arg) {
|
||||
/* Wait for initialization to occur */
|
||||
Utils::WaitSdInitialized();
|
||||
|
||||
/* Load a payload off of the SD */
|
||||
BpcRebootManager::Initialize();
|
||||
|
||||
/* Create server manager */
|
||||
auto server_manager = new WaitableManager(2);
|
||||
|
||||
/* Create bpc mitm. */
|
||||
const char *service_name = "bpc";
|
||||
if (GetRuntimeFirmwareVersion() < FirmwareVersion_200) {
|
||||
service_name = "bpc:c";
|
||||
}
|
||||
AddMitmServerToManager<BpcMitmService>(server_manager, service_name, 13);
|
||||
|
||||
/* Loop forever, servicing our services. */
|
||||
server_manager->Process();
|
||||
|
||||
delete server_manager;
|
||||
|
||||
}
|
||||
|
||||
29
stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_main.hpp
Normal file
29
stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_main.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (c) 2018 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/>.
|
||||
*/
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <malloc.h>
|
||||
|
||||
#include <switch.h>
|
||||
#include <atmosphere.h>
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
constexpr u32 BpcMitmPriority = 32;
|
||||
constexpr u32 BpcMitmStackSize = 0x8000;
|
||||
|
||||
void BpcMitmMain(void *arg);
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (c) 2018 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/>.
|
||||
*/
|
||||
|
||||
#include <switch.h>
|
||||
#include <stratosphere.hpp>
|
||||
#include <string.h>
|
||||
#include "bpcmitm_reboot_manager.hpp"
|
||||
#include "../utils.hpp"
|
||||
|
||||
/* TODO: Find a way to pre-populate this with the contents of fusee-primary. */
|
||||
static u8 g_reboot_payload[IRAM_PAYLOAD_MAX_SIZE] __attribute__ ((aligned (0x1000)));
|
||||
static u8 g_work_page[0x1000] __attribute__ ((aligned (0x1000)));
|
||||
static bool g_payload_loaded = false;
|
||||
static BpcRebootType g_reboot_type = BpcRebootType::ToPayload;
|
||||
|
||||
void BpcRebootManager::Initialize() {
|
||||
/* Open payload file. */
|
||||
FsFile payload_file;
|
||||
if (R_FAILED(Utils::OpenSdFile("/atmosphere/reboot_payload.bin", FS_OPEN_READ, &payload_file))) {
|
||||
return;
|
||||
}
|
||||
ON_SCOPE_EXIT { fsFileClose(&payload_file); };
|
||||
|
||||
/* Clear payload buffer */
|
||||
std::memset(g_reboot_payload, 0xFF, sizeof(g_reboot_payload));
|
||||
|
||||
/* Read payload file. */
|
||||
size_t actual_size;
|
||||
fsFileRead(&payload_file, 0, g_reboot_payload, IRAM_PAYLOAD_MAX_SIZE, &actual_size);
|
||||
|
||||
g_payload_loaded = true;
|
||||
|
||||
/* TODO: Figure out what kind of reboot we're gonna be doing. */
|
||||
}
|
||||
|
||||
static void ClearIram() {
|
||||
/* Make page FFs. */
|
||||
memset(g_work_page, 0xFF, sizeof(g_work_page));
|
||||
|
||||
/* Overwrite all of IRAM with FFs. */
|
||||
for (size_t ofs = 0; ofs < IRAM_PAYLOAD_MAX_SIZE; ofs += sizeof(g_work_page)) {
|
||||
CopyToIram(IRAM_PAYLOAD_BASE + ofs, g_work_page, sizeof(g_work_page));
|
||||
}
|
||||
}
|
||||
|
||||
static void DoRebootToPayload() {
|
||||
/* If we don't actually have a payload loaded, just go to RCM. */
|
||||
if (!g_payload_loaded) {
|
||||
RebootToRcm();
|
||||
}
|
||||
|
||||
/* Ensure clean IRAM state. */
|
||||
ClearIram();
|
||||
|
||||
/* Copy in payload. */
|
||||
for (size_t ofs = 0; ofs < sizeof(g_reboot_payload); ofs += 0x1000) {
|
||||
CopyToIram(IRAM_PAYLOAD_BASE + ofs, &g_reboot_payload[ofs], 0x1000);
|
||||
}
|
||||
|
||||
RebootToIramPayload();
|
||||
}
|
||||
|
||||
Result BpcRebootManager::PerformReboot() {
|
||||
switch (g_reboot_type) {
|
||||
case BpcRebootType::Standard:
|
||||
return RESULT_FORWARD_TO_SESSION;
|
||||
case BpcRebootType::ToRcm:
|
||||
RebootToRcm();
|
||||
return 0;
|
||||
case BpcRebootType::ToPayload:
|
||||
default:
|
||||
DoRebootToPayload();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) 2018 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <switch.h>
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
#define IRAM_PAYLOAD_MAX_SIZE 0x2F000
|
||||
#define IRAM_PAYLOAD_BASE 0x40010000ull
|
||||
|
||||
enum class BpcRebootType : u32 {
|
||||
Standard,
|
||||
ToRcm,
|
||||
ToPayload,
|
||||
};
|
||||
|
||||
class BpcRebootManager {
|
||||
public:
|
||||
static void Initialize();
|
||||
static Result PerformReboot();
|
||||
};
|
||||
Reference in New Issue
Block a user