libstratosphere/fs.mitm: Push WIP support for Domains. Not yet fully working.
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
#include <stratosphere.hpp>
|
||||
#include "fs_shim.h"
|
||||
|
||||
#include "debug.hpp"
|
||||
|
||||
enum FsIStorageCmd {
|
||||
FsIStorage_Cmd_Read = 0,
|
||||
FsIStorage_Cmd_Write = 1,
|
||||
@@ -17,6 +19,9 @@ class IStorage {
|
||||
virtual ~IStorage() {
|
||||
|
||||
}
|
||||
|
||||
virtual IStorage *Clone() = 0;
|
||||
|
||||
virtual Result Read(void *buffer, size_t size, u64 offset, u64 *out_read_size) = 0;
|
||||
virtual Result Write(void *buffer, size_t size, u64 offset) = 0;
|
||||
virtual Result Flush() = 0;
|
||||
@@ -33,6 +38,10 @@ class IStorageInterface : public IServiceObject {
|
||||
/* ... */
|
||||
};
|
||||
|
||||
IStorageInterface *clone() override {
|
||||
return new IStorageInterface(this->base_storage->Clone());
|
||||
}
|
||||
|
||||
~IStorageInterface() {
|
||||
delete base_storage;
|
||||
};
|
||||
|
||||
@@ -1,6 +1,47 @@
|
||||
#include <switch.h>
|
||||
#include "fs_shim.h"
|
||||
|
||||
/* Necessary evil. */
|
||||
Result ipcCopyFromDomain(Handle session, u32 object_id, Service *out) {
|
||||
u32* buf = (u32*)armGetTls();
|
||||
|
||||
IpcCommand c;
|
||||
ipcInitialize(&c);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 cmd_id;
|
||||
u32 object_id;
|
||||
} *raw;
|
||||
|
||||
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||
buf[0] = IpcCommandType_Control;
|
||||
raw->magic = SFCI_MAGIC;
|
||||
raw->cmd_id = 1;
|
||||
raw->object_id = object_id;
|
||||
|
||||
Result rc = ipcDispatch(session);
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
IpcParsedCommand r;
|
||||
ipcParse(&r);
|
||||
|
||||
struct ipcCopyFromDomainResponse {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
} *raw = (struct ipcCopyFromDomainResponse*)r.Raw;
|
||||
|
||||
rc = raw->result;
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
serviceCreate(out, r.Handles[0]);
|
||||
}
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/* Missing fsp-srv commands. */
|
||||
Result fsOpenDataStorageByDataId(Service* s, FsStorageId storage_id, u64 data_id, FsStorage* out) {
|
||||
IpcCommand c;
|
||||
@@ -41,6 +82,47 @@ Result fsOpenDataStorageByDataId(Service* s, FsStorageId storage_id, u64 data_id
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
Result fsOpenDataStorageByDataIdFromDomain(Service* s, FsStorageId storage_id, u64 data_id, u32 *out_object_id) {
|
||||
IpcCommand c;
|
||||
ipcInitialize(&c);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 cmd_id;
|
||||
FsStorageId storage_id;
|
||||
u64 data_id;
|
||||
} *raw;
|
||||
|
||||
raw = ipcPrepareHeaderForDomain(&c, sizeof(*raw), s->object_id);
|
||||
|
||||
raw->magic = SFCI_MAGIC;
|
||||
raw->cmd_id = 202;
|
||||
raw->storage_id = storage_id;
|
||||
raw->data_id = data_id;
|
||||
|
||||
Result rc = serviceIpcDispatch(s);
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
IpcParsedCommand r;
|
||||
ipcParseForDomain(&r);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
u32 object_id;
|
||||
} *resp = r.Raw;
|
||||
|
||||
rc = resp->result;
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
*out_object_id = resp->object_id;
|
||||
}
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Missing FS File commands. */
|
||||
Result fsFileOperateRange(FsFile* f, u32 op_id, u64 off, u64 len, FsRangeInfo *out) {
|
||||
IpcCommand c;
|
||||
|
||||
@@ -16,8 +16,12 @@ typedef struct {
|
||||
u32 flags[0x40/sizeof(u32)];
|
||||
} FsRangeInfo;
|
||||
|
||||
/* Necessary evils. */
|
||||
Result ipcCopyFromDomain(Handle session, u32 object_id, Service *out);
|
||||
|
||||
/* Missing fsp-srv commands. */
|
||||
Result fsOpenDataStorageByDataId(Service* s, FsStorageId storage_id, u64 data_id, FsStorage* out);
|
||||
Result fsOpenDataStorageByDataIdFromDomain(Service* s, FsStorageId storage_id, u64 data_id, u32 *out_object_id);
|
||||
|
||||
/* Missing FS File commands. */
|
||||
Result fsFileOperateRange(FsFile* f, u32 op_id, u64 off, u64 len, FsRangeInfo *out);
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
#include "fsmitm_service.hpp"
|
||||
#include "fsmitm_worker.hpp"
|
||||
|
||||
#include "mitm_service.hpp"
|
||||
|
||||
extern "C" {
|
||||
extern u32 __start__;
|
||||
|
||||
@@ -62,6 +64,11 @@ void __appInit(void) {
|
||||
fatalSimple(0xCAFE << 4 | 3);
|
||||
}
|
||||
|
||||
rc = pminfoInitialize();
|
||||
if (R_FAILED(rc)) {
|
||||
fatalSimple(0xCAFE << 4 | 4);
|
||||
}
|
||||
|
||||
/* Check for exosphere API compatibility. */
|
||||
u64 exosphere_cfg;
|
||||
if (R_SUCCEEDED(splGetConfig((SplConfigItem)65000, &exosphere_cfg))) {
|
||||
@@ -74,7 +81,7 @@ void __appInit(void) {
|
||||
fatalSimple(0xCAFE << 4 | 0xFF);
|
||||
}
|
||||
|
||||
splExit();
|
||||
//splExit();
|
||||
}
|
||||
|
||||
void __appExit(void) {
|
||||
@@ -100,7 +107,8 @@ int main(int argc, char **argv)
|
||||
WaitableManager *server_manager = new WaitableManager(U64_MAX);
|
||||
|
||||
/* Create fsp-srv mitm. */
|
||||
server_manager->add_waitable(new MitMServer<FsMitMService>("fsp-srv", 61));
|
||||
//server_manager->add_waitable(new MitMServer<FsMitMService>("fsp-srv", 61));
|
||||
server_manager->add_waitable(new MitMServer<GenericMitMService>("fsp-srv", 61));
|
||||
|
||||
/* Loop forever, servicing our services. */
|
||||
server_manager->process();
|
||||
|
||||
@@ -19,6 +19,10 @@ class RomFileStorage : public IROStorage {
|
||||
fsFileClose(base_file);
|
||||
delete base_file;
|
||||
};
|
||||
|
||||
RomFileStorage *Clone() override {
|
||||
return new RomFileStorage(this->base_file);
|
||||
};
|
||||
protected:
|
||||
Result Read(void *buffer, size_t size, u64 offset, u64 *out_read_size) override {
|
||||
size_t out_sz = 0;
|
||||
@@ -52,6 +56,10 @@ class RomInterfaceStorage : public IROStorage {
|
||||
fsStorageClose(base_storage);
|
||||
delete base_storage;
|
||||
};
|
||||
|
||||
RomInterfaceStorage *Clone() override {
|
||||
return new RomInterfaceStorage(this->base_storage);
|
||||
};
|
||||
protected:
|
||||
Result Read(void *buffer, size_t size, u64 offset, u64 *out_read_size) override {
|
||||
Result rc = fsStorageRead(this->base_storage, offset, buffer, size);
|
||||
|
||||
@@ -17,32 +17,51 @@ Result FsMitMService::dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_i
|
||||
this->process_id = r.Pid;
|
||||
}
|
||||
break;
|
||||
case FspSrv_Cmd_OpenDataStorageByDataId:
|
||||
/*case FspSrv_Cmd_OpenDataStorageByDataId:
|
||||
rc = WrapIpcCommandImpl<&FsMitMService::open_data_storage_by_data_id>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
||||
break;
|
||||
break; */
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
Result FsMitMService::postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) {
|
||||
void FsMitMService::postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) {
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
} *resp = (decltype(resp))r.Raw;
|
||||
|
||||
u64 *tls = (u64 *)armGetTls();
|
||||
u64 backup_tls[0x100/sizeof(u64)];
|
||||
for (unsigned int i = 0; i < sizeof(backup_tls)/sizeof(u64); i++) {
|
||||
backup_tls[i] = tls[i];
|
||||
}
|
||||
|
||||
Result rc = (Result)resp->result;
|
||||
switch (cmd_id) {
|
||||
case FspSrv_Cmd_SetCurrentProcess:
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
this->has_initialized = true;
|
||||
if (R_FAILED(pminfoInitialize()) || R_FAILED(pminfoGetTitleId(&this->title_id, this->process_id))) {
|
||||
fatalSimple(0xCAFE << 8 | 0xFD);
|
||||
rc = pminfoGetTitleId(&this->title_id, this->process_id);
|
||||
if (R_FAILED(rc)) {
|
||||
if (rc == 0x20F) {
|
||||
this->title_id = this->process_id;
|
||||
rc = 0x0;
|
||||
} else {
|
||||
fatalSimple(rc);
|
||||
}
|
||||
}
|
||||
pminfoExit();
|
||||
}
|
||||
Log(&this->process_id, 8);
|
||||
Log(&this->title_id, 8);
|
||||
for (unsigned int i = 0; i < sizeof(backup_tls)/sizeof(u64); i++) {
|
||||
tls[i] = backup_tls[i];
|
||||
}
|
||||
if (this->title_id >= 0x0100000000001000) {
|
||||
Reboot();
|
||||
}
|
||||
break;
|
||||
}
|
||||
return rc;
|
||||
resp->result = rc;
|
||||
}
|
||||
|
||||
Result FsMitMService::handle_deferred() {
|
||||
@@ -51,25 +70,37 @@ Result FsMitMService::handle_deferred() {
|
||||
}
|
||||
|
||||
/* Add redirection for System Data Archives to the SD card. */
|
||||
std::tuple<Result, MovedHandle> FsMitMService::open_data_storage_by_data_id(FsStorageId storage_id, u64 data_id) {
|
||||
Handle out_h = 0;
|
||||
std::tuple<Result, OutSession<IStorageInterface>> FsMitMService::open_data_storage_by_data_id(u64 sid, u64 data_id) {
|
||||
FsStorageId storage_id = (FsStorageId)sid;
|
||||
IPCSession<IStorageInterface> *out_session = NULL;
|
||||
FsStorage data_storage;
|
||||
FsFile data_file;
|
||||
Result rc = fsOpenDataStorageByDataId(this->forward_service, storage_id, data_id, &data_storage);
|
||||
u32 out_domain_id;
|
||||
Result rc;
|
||||
if (this->get_owner() == NULL) {
|
||||
rc = fsOpenDataStorageByDataId(this->forward_service, storage_id, data_id, &data_storage);
|
||||
} else {
|
||||
rc = fsOpenDataStorageByDataIdFromDomain(this->forward_service, storage_id, data_id, &out_domain_id);
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
rc = ipcCopyFromDomain(this->forward_service->handle, out_domain_id, &data_storage.s);
|
||||
}
|
||||
}
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
IPCSession<IStorageInterface> *out_session = NULL;
|
||||
char path[FS_MAX_PATH] = {0};
|
||||
/* TODO: Is there a sensible path that ends in ".romfs" we can use?" */
|
||||
snprintf(path, sizeof(path), "/atmosphere/titles/%016lx/romfs.bin", data_id);
|
||||
if (R_SUCCEEDED(Utils::OpenSdFile(path, FS_OPEN_READ, &data_file))) {
|
||||
fsStorageClose(&data_storage);
|
||||
out_session = new IPCSession<IStorageInterface>(new IStorageInterface(new RomFileStorage(data_file)));
|
||||
} else {
|
||||
|
||||
} else {
|
||||
out_session = new IPCSession<IStorageInterface>(new IStorageInterface(new RomInterfaceStorage(data_storage)));
|
||||
}
|
||||
FsMitmWorker::AddWaitable(out_session);
|
||||
out_h = out_session->get_client_handle();
|
||||
if (this->get_owner() == NULL) {
|
||||
FsMitmWorker::AddWaitable(out_session);
|
||||
}
|
||||
}
|
||||
return {rc, out_h};
|
||||
|
||||
OutSession out_s = OutSession(out_session);
|
||||
out_s.domain_id = out_domain_id;
|
||||
return {rc, out_s};
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <switch.h>
|
||||
#include <stratosphere/iserviceobject.hpp>
|
||||
#include "imitmserviceobject.hpp"
|
||||
#include "fs_istorage.hpp"
|
||||
|
||||
enum FspSrvCmd {
|
||||
FspSrv_Cmd_SetCurrentProcess = 1,
|
||||
@@ -17,11 +18,25 @@ class FsMitMService : public IMitMServiceObject {
|
||||
FsMitMService(Service *s) : IMitMServiceObject(s), has_initialized(false), process_id(0), title_id(0) {
|
||||
/* ... */
|
||||
}
|
||||
|
||||
FsMitMService *clone() override {
|
||||
auto new_srv = new FsMitMService((Service *)&this->forward_service);
|
||||
this->clone_to(new_srv);
|
||||
return new_srv;
|
||||
}
|
||||
|
||||
void clone_to(void *o) override {
|
||||
FsMitMService *other = (FsMitMService *)o;
|
||||
other->has_initialized = has_initialized;
|
||||
other->process_id = process_id;
|
||||
other->title_id = title_id;
|
||||
}
|
||||
|
||||
virtual Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size);
|
||||
virtual Result postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size);
|
||||
virtual void postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size);
|
||||
virtual Result handle_deferred();
|
||||
|
||||
protected:
|
||||
/* Overridden commands. */
|
||||
std::tuple<Result, MovedHandle> open_data_storage_by_data_id(FsStorageId storage_id, u64 data_id);
|
||||
std::tuple<Result, OutSession<IStorageInterface>> open_data_storage_by_data_id(u64 storage_id, u64 data_id);
|
||||
};
|
||||
@@ -2,6 +2,8 @@
|
||||
#include <stratosphere.hpp>
|
||||
#include <atomic>
|
||||
|
||||
#include "sm_mitm.h"
|
||||
|
||||
#include "fsmitm_utils.hpp"
|
||||
|
||||
static FsFileSystem g_sd_filesystem;
|
||||
@@ -11,6 +13,17 @@ static Result EnsureInitialized() {
|
||||
if (g_has_initialized) {
|
||||
return 0x0;
|
||||
}
|
||||
|
||||
static const char * const required_active_services[] = {"pcv", "gpio", "pinmux", "psc:c"};
|
||||
for (unsigned int i = 0; i < sizeof(required_active_services) / sizeof(required_active_services[0]); i++) {
|
||||
Result rc = smMitMUninstall(required_active_services[i]);
|
||||
if (rc == 0xE15) {
|
||||
return rc;
|
||||
} else if (rc != 0x1015) {
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
Result rc = fsMountSdcard(&g_sd_filesystem);
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
g_has_initialized = true;
|
||||
|
||||
@@ -3,35 +3,36 @@
|
||||
#include "fsmitm_worker.hpp"
|
||||
|
||||
static SystemEvent *g_new_waitable_event = NULL;
|
||||
static ChildWaitableHolder *g_child_holder = NULL;
|
||||
|
||||
static HosMutex g_new_waitable_mutex;
|
||||
static IWaitable *g_new_waitable = NULL;
|
||||
static HosSemaphore g_sema_new_waitable_finish;
|
||||
|
||||
Result FsMitmWorker::AddWaitableInternal(Handle *handles, size_t num_handles, u64 timeout) {
|
||||
static WaitableManager *g_worker_waiter = NULL;
|
||||
|
||||
Result FsMitmWorker::AddWaitableCallback(Handle *handles, size_t num_handles, u64 timeout) {
|
||||
svcClearEvent(handles[0]);
|
||||
g_child_holder->add_child(g_new_waitable);
|
||||
g_sema_new_waitable_finish.Signal();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void FsMitmWorker::AddWaitable(IWaitable *waitable) {
|
||||
g_worker_waiter->add_waitable(waitable);
|
||||
g_new_waitable_mutex.Lock();
|
||||
g_new_waitable = waitable;
|
||||
g_new_waitable_event->signal_event();
|
||||
g_sema_new_waitable_finish.Wait();
|
||||
g_new_waitable_mutex.Unlock();
|
||||
}
|
||||
|
||||
void FsMitmWorker::Main(void *arg) {
|
||||
/* Initialize waitable event. */
|
||||
g_new_waitable_event = new SystemEvent(&FsMitmWorker::AddWaitableInternal);
|
||||
g_child_holder = new ChildWaitableHolder();
|
||||
g_new_waitable_event = new SystemEvent(&FsMitmWorker::AddWaitableCallback);
|
||||
|
||||
/* Make a new waitable manager. */
|
||||
WaitableManager *worker_waiter = new WaitableManager(U64_MAX);
|
||||
worker_waiter->add_waitable(g_new_waitable_event);
|
||||
g_worker_waiter = new WaitableManager(U64_MAX);
|
||||
g_worker_waiter->add_waitable(g_new_waitable_event);
|
||||
|
||||
/* Service processes. */
|
||||
worker_waiter->process();
|
||||
g_worker_waiter->process();
|
||||
|
||||
delete worker_waiter;
|
||||
delete g_worker_waiter;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
class FsMitmWorker {
|
||||
private:
|
||||
static Result AddWaitableInternal(Handle *handles, size_t num_handles, u64 timeout);
|
||||
static Result AddWaitableCallback(Handle *handles, size_t num_handles, u64 timeout);
|
||||
public:
|
||||
static void Main(void *arg);
|
||||
static void AddWaitable(IWaitable *waitable);
|
||||
|
||||
@@ -10,9 +10,11 @@ class IMitMServiceObject : public IServiceObject {
|
||||
IMitMServiceObject(Service *s) : forward_service(s) {
|
||||
|
||||
}
|
||||
|
||||
virtual void clone_to(void *o) = 0;
|
||||
protected:
|
||||
virtual ~IMitMServiceObject() { }
|
||||
virtual Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) = 0;
|
||||
virtual Result postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) = 0;
|
||||
virtual void postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) = 0;
|
||||
virtual Result handle_deferred() = 0;
|
||||
};
|
||||
|
||||
@@ -11,116 +11,39 @@ template <typename T>
|
||||
class MitMSession;
|
||||
|
||||
template <typename T>
|
||||
class MitMServer final : public IWaitable {
|
||||
class MitMServer final : public IServer<T> {
|
||||
static_assert(std::is_base_of<IServiceObject, T>::value, "Service Objects must derive from IServiceObject");
|
||||
protected:
|
||||
Handle port_handle;
|
||||
unsigned int max_sessions;
|
||||
unsigned int num_sessions;
|
||||
MitMSession<T> **sessions;
|
||||
private:
|
||||
char mitm_name[9];
|
||||
|
||||
public:
|
||||
MitMServer(const char *service_name, unsigned int max_s) : max_sessions(max_s) {
|
||||
this->sessions = new MitMSession<T> *[this->max_sessions];
|
||||
for (unsigned int i = 0; i < this->max_sessions; i++) {
|
||||
this->sessions[i] = NULL;
|
||||
MitMServer(const char *service_name, unsigned int max_s, bool s_d = false) : IServer<T>(service_name, max_s, s_d) {
|
||||
Handle tmp_hnd;
|
||||
Result rc;
|
||||
|
||||
if (R_SUCCEEDED((rc = smGetServiceOriginal(&tmp_hnd, smEncodeName(service_name))))) {
|
||||
svcCloseHandle(tmp_hnd);
|
||||
} else {
|
||||
fatalSimple(rc);
|
||||
}
|
||||
this->num_sessions = 0;
|
||||
strncpy(mitm_name, service_name, 8);
|
||||
mitm_name[8] = '\x00';
|
||||
Result rc;
|
||||
if (R_FAILED((rc = smMitMInstall(&this->port_handle, mitm_name)))) {
|
||||
/* TODO: Panic. */
|
||||
fatalSimple(rc);
|
||||
}
|
||||
}
|
||||
|
||||
virtual ~MitMServer() {
|
||||
for (unsigned int i = 0; i < this->max_sessions; i++) {
|
||||
if (this->sessions[i]) {
|
||||
delete this->sessions[i];
|
||||
}
|
||||
|
||||
delete this->sessions;
|
||||
}
|
||||
|
||||
if (port_handle) {
|
||||
if (R_FAILED(smMitMUninstall(mitm_name))) {
|
||||
if (this->port_handle) {
|
||||
if (R_FAILED(smMitMUninstall(this->mitm_name))) {
|
||||
/* TODO: Panic. */
|
||||
}
|
||||
svcCloseHandle(port_handle);
|
||||
}
|
||||
}
|
||||
|
||||
/* IWaitable */
|
||||
virtual unsigned int get_num_waitables() {
|
||||
unsigned int n = 1;
|
||||
for (unsigned int i = 0; i < this->max_sessions; i++) {
|
||||
if (this->sessions[i]) {
|
||||
n += this->sessions[i]->get_num_waitables();
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
virtual void get_waitables(IWaitable **dst) {
|
||||
dst[0] = this;
|
||||
unsigned int n = 0;
|
||||
for (unsigned int i = 0; i < this->max_sessions; i++) {
|
||||
if (this->sessions[i]) {
|
||||
this->sessions[i]->get_waitables(&dst[1 + n]);
|
||||
n += this->sessions[i]->get_num_waitables();
|
||||
}
|
||||
/* svcCloseHandle(port_handle); was called by ~IServer. */
|
||||
}
|
||||
}
|
||||
|
||||
virtual void delete_child(IWaitable *child) {
|
||||
unsigned int i;
|
||||
for (i = 0; i < this->max_sessions; i++) {
|
||||
if (this->sessions[i] == child) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == this->max_sessions) {
|
||||
/* TODO: Panic, because this isn't our child. */
|
||||
} else {
|
||||
delete this->sessions[i];
|
||||
this->sessions[i] = NULL;
|
||||
this->num_sessions--;
|
||||
}
|
||||
}
|
||||
|
||||
virtual Handle get_handle() {
|
||||
return this->port_handle;
|
||||
}
|
||||
|
||||
|
||||
virtual void handle_deferred() {
|
||||
/* TODO: Panic, because we can never defer a server. */
|
||||
}
|
||||
|
||||
virtual Result handle_signaled(u64 timeout) {
|
||||
/* If this server's port was signaled, accept a new session. */
|
||||
Handle session_h;
|
||||
svcAcceptSession(&session_h, this->port_handle);
|
||||
|
||||
if (this->num_sessions >= this->max_sessions) {
|
||||
svcCloseHandle(session_h);
|
||||
return 0x10601;
|
||||
}
|
||||
|
||||
unsigned int i;
|
||||
for (i = 0; i < this->max_sessions; i++) {
|
||||
if (this->sessions[i] == NULL) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this->sessions[i] = new MitMSession<T>(this, session_h, 0, mitm_name);
|
||||
this->sessions[i]->set_parent(this);
|
||||
this->num_sessions++;
|
||||
return 0;
|
||||
ISession<T> *get_new_session(Handle session_h) override {
|
||||
return new MitMSession<T>(this, session_h, 0, mitm_name);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
16
stratosphere/fs_mitm/source/mitm_service.cpp
Normal file
16
stratosphere/fs_mitm/source/mitm_service.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <switch.h>
|
||||
#include "mitm_service.hpp"
|
||||
|
||||
#include "debug.hpp"
|
||||
|
||||
Result GenericMitMService::dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) {
|
||||
return 0xF601;
|
||||
}
|
||||
|
||||
void GenericMitMService::postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) {
|
||||
}
|
||||
|
||||
Result GenericMitMService::handle_deferred() {
|
||||
/* This service is never deferrable. */
|
||||
return 0;
|
||||
}
|
||||
27
stratosphere/fs_mitm/source/mitm_service.hpp
Normal file
27
stratosphere/fs_mitm/source/mitm_service.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#include <switch.h>
|
||||
#include <stratosphere/iserviceobject.hpp>
|
||||
#include "imitmserviceobject.hpp"
|
||||
#include "fs_istorage.hpp"
|
||||
|
||||
|
||||
class GenericMitMService : public IMitMServiceObject {
|
||||
public:
|
||||
GenericMitMService(Service *s) : IMitMServiceObject(s) {
|
||||
/* ... */
|
||||
}
|
||||
|
||||
GenericMitMService *clone() override {
|
||||
auto new_srv = new GenericMitMService((Service *)&this->forward_service);
|
||||
this->clone_to(new_srv);
|
||||
return new_srv;
|
||||
}
|
||||
|
||||
void clone_to(void *o) override {
|
||||
/* ... */
|
||||
}
|
||||
|
||||
virtual Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size);
|
||||
virtual void postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size);
|
||||
virtual Result handle_deferred();
|
||||
};
|
||||
@@ -12,154 +12,208 @@ template <typename T>
|
||||
class MitMServer;
|
||||
|
||||
template <typename T>
|
||||
class MitMSession final : public IWaitable {
|
||||
class MitMSession final : public ISession<T> {
|
||||
static_assert(std::is_base_of<IMitMServiceObject, T>::value, "MitM Service Objects must derive from IMitMServiceObject");
|
||||
|
||||
T *service_object;
|
||||
MitMServer<T> *server;
|
||||
Handle server_handle;
|
||||
Handle client_handle;
|
||||
/* This will be for the actual session. */
|
||||
Service forward_service;
|
||||
|
||||
char *pointer_buffer;
|
||||
size_t pointer_buffer_size;
|
||||
|
||||
static_assert(sizeof(pointer_buffer) <= POINTER_BUFFER_SIZE_MAX, "Incorrect Size for PointerBuffer!");
|
||||
IpcParsedCommand cur_out_r;
|
||||
u32 mitm_domain_id;
|
||||
|
||||
public:
|
||||
MitMSession<T>(MitMServer<T> *s, Handle s_h, Handle c_h, const char *srv) : server(s), server_handle(s_h), client_handle(c_h) {
|
||||
MitMSession<T>(MitMServer<T> *s, Handle s_h, Handle c_h, const char *srv) : ISession<T>(s, s_h, c_h, NULL, 0), mitm_domain_id(0) {
|
||||
this->server = s;
|
||||
this->server_handle = s_h;
|
||||
this->client_handle = c_h;
|
||||
if (R_FAILED(smMitMGetService(&forward_service, srv))) {
|
||||
/* TODO: Panic. */
|
||||
}
|
||||
if (R_FAILED(ipcQueryPointerBufferSize(forward_service.handle, &pointer_buffer_size))) {
|
||||
if (R_FAILED(ipcQueryPointerBufferSize(forward_service.handle, &this->pointer_buffer_size))) {
|
||||
/* TODO: Panic. */
|
||||
}
|
||||
this->service_object = new T(&forward_service);
|
||||
this->pointer_buffer = new char[pointer_buffer_size];
|
||||
this->pointer_buffer = new char[this->pointer_buffer_size];
|
||||
}
|
||||
MitMSession<T>(MitMServer<T> *s, Handle s_h, Handle c_h, Handle f_h, size_t pbs) : ISession<T>(s, s_h, c_h, NULL, 0), mitm_domain_id(0) {
|
||||
this->server = s;
|
||||
this->server_handle = s_h;
|
||||
this->client_handle = c_h;
|
||||
this->pointer_buffer_size = pbs;
|
||||
this->forward_service = {.handle = f_h};
|
||||
this->service_object = new T(&forward_service);
|
||||
this->pointer_buffer = new char[this->pointer_buffer_size];
|
||||
}
|
||||
|
||||
~MitMSession() override {
|
||||
delete this->service_object;
|
||||
delete this->pointer_buffer;
|
||||
virtual ~MitMSession() {
|
||||
serviceClose(&forward_service);
|
||||
if (server_handle) {
|
||||
svcCloseHandle(server_handle);
|
||||
}
|
||||
if (client_handle) {
|
||||
svcCloseHandle(client_handle);
|
||||
}
|
||||
}
|
||||
|
||||
T *get_service_object() { return this->service_object; }
|
||||
Handle get_server_handle() { return this->server_handle; }
|
||||
Handle get_client_handle() { return this->client_handle; }
|
||||
|
||||
/* IWaitable */
|
||||
unsigned int get_num_waitables() override {
|
||||
return 1;
|
||||
}
|
||||
|
||||
void get_waitables(IWaitable **dst) override {
|
||||
dst[0] = this;
|
||||
}
|
||||
|
||||
void delete_child(IWaitable *child) override {
|
||||
/* TODO: Panic, because we can never have any children. */
|
||||
}
|
||||
|
||||
Handle get_handle() override {
|
||||
return this->server_handle;
|
||||
}
|
||||
|
||||
void handle_deferred() override {
|
||||
/* TODO: Panic, because we can never be deferred. */
|
||||
}
|
||||
|
||||
Result handle_signaled(u64 timeout) override {
|
||||
Result rc;
|
||||
int handle_index;
|
||||
|
||||
/* Prepare pointer buffer... */
|
||||
IpcCommand c_for_reply;
|
||||
ipcInitialize(&c_for_reply);
|
||||
ipcAddRecvStatic(&c_for_reply, this->pointer_buffer, this->pointer_buffer_size, 0);
|
||||
Result handle_message(IpcParsedCommand &r) override {
|
||||
IpcCommand c;
|
||||
ipcInitialize(&c);
|
||||
u64 cmd_id = ((u32 *)r.Raw)[2];
|
||||
Result retval = 0xF601;
|
||||
|
||||
cur_out_r.NumHandles = 0;
|
||||
|
||||
Log(armGetTls(), 0x100);
|
||||
|
||||
u32 *cmdbuf = (u32 *)armGetTls();
|
||||
ipcPrepareHeader(&c_for_reply, 0);
|
||||
|
||||
|
||||
if (R_SUCCEEDED(rc = svcReplyAndReceive(&handle_index, &this->server_handle, 1, 0, timeout))) {
|
||||
if (handle_index != 0) {
|
||||
/* TODO: Panic? */
|
||||
}
|
||||
Log(armGetTls(), 0x100);
|
||||
Result retval = 0;
|
||||
u32 *rawdata_start = cmdbuf;
|
||||
|
||||
IpcParsedCommand r;
|
||||
IpcCommand c;
|
||||
IpcParsedCommand out_r;
|
||||
out_r.NumHandles = 0;
|
||||
|
||||
ipcInitialize(&c);
|
||||
|
||||
retval = ipcParse(&r);
|
||||
|
||||
u64 cmd_id = U64_MAX;
|
||||
|
||||
/* TODO: Close input copy handles that we don't need. */
|
||||
|
||||
if (R_SUCCEEDED(retval)) {
|
||||
rawdata_start = (u32 *)r.Raw;
|
||||
cmd_id = rawdata_start[2];
|
||||
retval = 0xF601;
|
||||
if (r.CommandType == IpcCommandType_Request || r.CommandType == IpcCommandType_RequestWithContext) {
|
||||
retval = this->service_object->dispatch(r, c, cmd_id, (u8 *)this->pointer_buffer, this->pointer_buffer_size);
|
||||
if (R_SUCCEEDED(retval)) {
|
||||
ipcParse(&out_r);
|
||||
}
|
||||
}
|
||||
|
||||
/* 0xF601 --> Dispatch onwards. */
|
||||
if (retval == 0xF601) {
|
||||
/* Patch PID Descriptor, if relevant. */
|
||||
if (r.HasPid) {
|
||||
/* [ctrl 0] [ctrl 1] [handle desc 0] [pid low] [pid high] */
|
||||
cmdbuf[4] = 0xFFFE0000UL | (cmdbuf[4] & 0xFFFFUL);
|
||||
}
|
||||
Log(armGetTls(), 0x100);
|
||||
retval = serviceIpcDispatch(&forward_service);
|
||||
if (R_SUCCEEDED(retval)) {
|
||||
ipcParse(&out_r);
|
||||
if (r.CommandType == IpcCommandType_Request || r.CommandType == IpcCommandType_RequestWithContext) {
|
||||
IServiceObject *obj;
|
||||
if (r.IsDomainMessage) {
|
||||
obj = this->get_domain_object(r.ThisObjectId);
|
||||
if (obj && r.MessageType == DomainMessageType_Close) {
|
||||
this->delete_object(r.ThisObjectId);
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
} *o_resp;
|
||||
|
||||
o_resp = (decltype(o_resp)) ipcPrepareHeaderForDomain(&c, sizeof(*o_resp), 0);
|
||||
*(DomainMessageHeader *)((uintptr_t)o_resp - sizeof(DomainMessageHeader)) = {0};
|
||||
o_resp->magic = SFCO_MAGIC;
|
||||
o_resp->result = 0x0;
|
||||
Log(armGetTls(), 0x100);
|
||||
Reboot();
|
||||
return o_resp->result;
|
||||
}
|
||||
} else {
|
||||
obj = this->service_object;
|
||||
}
|
||||
if (obj) {
|
||||
retval = obj->dispatch(r, c, cmd_id, (u8 *)this->pointer_buffer, this->pointer_buffer_size);
|
||||
if (R_SUCCEEDED(retval)) {
|
||||
if (r.IsDomainMessage) {
|
||||
ipcParseForDomain(&cur_out_r);
|
||||
} else {
|
||||
ipcParse(&cur_out_r);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
}
|
||||
} else if (r.CommandType == IpcCommandType_Control || r.CommandType == IpcCommandType_ControlWithContext) {
|
||||
/* Ipc Clone Current Object. */
|
||||
retval = serviceIpcDispatch(&forward_service);
|
||||
Log(armGetTls(), 0x100);
|
||||
if (R_SUCCEEDED(retval)) {
|
||||
ipcParse(&cur_out_r);
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
} *resp = (decltype(resp))cur_out_r.Raw;
|
||||
retval = resp->result;
|
||||
if (false && (cmd_id == IpcCtrl_Cmd_CloneCurrentObject || cmd_id == IpcCtrl_Cmd_CloneCurrentObjectEx)) {
|
||||
if (R_SUCCEEDED(retval)) {
|
||||
Handle s_h;
|
||||
Handle c_h;
|
||||
Result rc;
|
||||
if (R_FAILED((rc = svcCreateSession(&s_h, &c_h, 0, 0)))) {
|
||||
fatalSimple(rc);
|
||||
}
|
||||
|
||||
MitMSession<T> *new_sess = new MitMSession<T>((MitMServer<T> *)this->server, s_h, c_h, cur_out_r.Handles[0], this->pointer_buffer_size);
|
||||
this->get_service_object()->clone_to(new_sess->get_service_object());
|
||||
if (this->is_domain) {
|
||||
new_sess->is_domain = true;
|
||||
new_sess->mitm_domain_id = this->mitm_domain_id;
|
||||
new_sess->forward_service.type = this->forward_service.type;
|
||||
new_sess->forward_service.object_id = this->forward_service.object_id;
|
||||
new_sess->set_object(new_sess->get_service_object(), new_sess->mitm_domain_id);
|
||||
for (unsigned int i = 0; i < DOMAIN_ID_MAX; i++) {
|
||||
if (i != new_sess->mitm_domain_id) {
|
||||
IServiceObject *obj = this->get_domain_object(i);
|
||||
if (obj) {
|
||||
new_sess->set_object(obj->clone(), i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
this->get_manager()->add_waitable(new_sess);
|
||||
ipcSendHandleMove(&c, c_h);
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
} *resp = (decltype(resp))out_r.Raw;
|
||||
} *o_resp;
|
||||
|
||||
retval = resp->result;
|
||||
o_resp = (decltype(o_resp)) ipcPrepareHeader(&c, sizeof(*o_resp));
|
||||
o_resp->magic = SFCO_MAGIC;
|
||||
o_resp->result = 0x0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Log(armGetTls(), 0x100);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/* 0xF601 --> Dispatch onwards. */
|
||||
if (retval == 0xF601) {
|
||||
/* Patch PID Descriptor, if relevant. */
|
||||
if (r.HasPid) {
|
||||
/* [ctrl 0] [ctrl 1] [handle desc 0] [pid low] [pid high] */
|
||||
cmdbuf[4] = 0xFFFE0000UL | (cmdbuf[4] & 0xFFFFUL);
|
||||
}
|
||||
|
||||
if (retval == 0xF601) {
|
||||
/* Session close. */
|
||||
rc = retval;
|
||||
} else {
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
ipcInitialize(&c);
|
||||
retval = this->service_object->postprocess(r, c, cmd_id, (u8 *)this->pointer_buffer, this->pointer_buffer_size);
|
||||
}
|
||||
Log(armGetTls(), 0x100);
|
||||
rc = svcReplyAndReceive(&handle_index, &this->server_handle, 0, this->server_handle, 0);
|
||||
/* Clean up copy handles. */
|
||||
for (unsigned int i = 0; i < out_r.NumHandles; i++) {
|
||||
if (out_r.WasHandleCopied[i]) {
|
||||
svcCloseHandle(out_r.Handles[i]);
|
||||
}
|
||||
Log(armGetTls(), 0x100);
|
||||
retval = serviceIpcDispatch(&forward_service);
|
||||
if (R_SUCCEEDED(retval)) {
|
||||
if (r.IsDomainMessage) {
|
||||
ipcParseForDomain(&cur_out_r);
|
||||
} else {
|
||||
ipcParse(&cur_out_r);
|
||||
}
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
} *resp = (decltype(resp))cur_out_r.Raw;
|
||||
|
||||
retval = resp->result;
|
||||
}
|
||||
}
|
||||
|
||||
Log(armGetTls(), 0x100);
|
||||
Log(&cmd_id, sizeof(u64));
|
||||
u64 retval_for_log = retval;
|
||||
Log(&retval_for_log, sizeof(u64));
|
||||
if (R_FAILED(retval)) {
|
||||
// Reboot();
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
void postprocess(IpcParsedCommand &r, u64 cmd_id) override {
|
||||
if (this->active_object == this->service_object && (r.CommandType == IpcCommandType_Request || r.CommandType == IpcCommandType_RequestWithContext)) {
|
||||
IpcCommand c;
|
||||
ipcInitialize(&c);
|
||||
this->service_object->postprocess(cur_out_r, c, cmd_id, (u8 *)this->pointer_buffer, this->pointer_buffer_size);
|
||||
} else if (r.CommandType == IpcCommandType_Control || r.CommandType == IpcCommandType_ControlWithContext) {
|
||||
if (cmd_id == IpcCtrl_Cmd_ConvertCurrentObjectToDomain) {
|
||||
return;
|
||||
this->is_domain = true;
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
u32 domain_id;
|
||||
} *resp = (decltype(resp))cur_out_r.Raw;
|
||||
Result rc;
|
||||
if (R_FAILED((rc = this->set_object(this->service_object, resp->domain_id)))) {
|
||||
fatalSimple(rc);
|
||||
}
|
||||
this->mitm_domain_id = resp->domain_id;
|
||||
this->forward_service.type = ServiceType_Domain;
|
||||
this->forward_service.object_id = resp->domain_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void cleanup() override {
|
||||
/* Clean up copy handles. */
|
||||
for (unsigned int i = 0; i < cur_out_r.NumHandles; i++) {
|
||||
if (cur_out_r.WasHandleCopied[i]) {
|
||||
svcCloseHandle(cur_out_r.Handles[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user