fs.mitm: Add HANS-style redirection for System Data Archives.
This commit is contained in:
120
stratosphere/fs_mitm/source/fs_istorage.hpp
Normal file
120
stratosphere/fs_mitm/source/fs_istorage.hpp
Normal file
@@ -0,0 +1,120 @@
|
||||
#pragma once
|
||||
#include <switch.h>
|
||||
#include <stratosphere.hpp>
|
||||
#include "fs_shim.h"
|
||||
|
||||
enum FsIStorageCmd {
|
||||
FsIStorage_Cmd_Read = 0,
|
||||
FsIStorage_Cmd_Write = 1,
|
||||
FsIStorage_Cmd_Flush = 2,
|
||||
FsIStorage_Cmd_SetSize = 3,
|
||||
FsIStorage_Cmd_GetSize = 4,
|
||||
FsIStorage_Cmd_OperateRange = 5,
|
||||
};
|
||||
|
||||
class IStorage {
|
||||
public:
|
||||
virtual ~IStorage() {
|
||||
|
||||
}
|
||||
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;
|
||||
virtual Result SetSize(u64 size) = 0;
|
||||
virtual Result GetSize(u64 *out_size) = 0;
|
||||
virtual Result OperateRange(u32 operation_type, u64 offset, u64 size, FsRangeInfo *out_range_info) = 0;
|
||||
};
|
||||
|
||||
class IStorageInterface : public IServiceObject {
|
||||
private:
|
||||
IStorage *base_storage;
|
||||
public:
|
||||
IStorageInterface(IStorage *s) : base_storage(s) {
|
||||
/* ... */
|
||||
};
|
||||
|
||||
~IStorageInterface() {
|
||||
delete base_storage;
|
||||
};
|
||||
|
||||
Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) final {
|
||||
Result rc = 0xF601;
|
||||
switch ((FsIStorageCmd)cmd_id) {
|
||||
case FsIStorage_Cmd_Read:
|
||||
rc = WrapIpcCommandImpl<&IStorageInterface::read>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
||||
break;
|
||||
case FsIStorage_Cmd_Write:
|
||||
rc = WrapIpcCommandImpl<&IStorageInterface::write>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
||||
break;
|
||||
case FsIStorage_Cmd_Flush:
|
||||
rc = WrapIpcCommandImpl<&IStorageInterface::flush>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
||||
break;
|
||||
case FsIStorage_Cmd_SetSize:
|
||||
rc = WrapIpcCommandImpl<&IStorageInterface::set_size>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
||||
break;
|
||||
case FsIStorage_Cmd_GetSize:
|
||||
rc = WrapIpcCommandImpl<&IStorageInterface::get_size>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
||||
break;
|
||||
case FsIStorage_Cmd_OperateRange:
|
||||
if (kernelAbove400()) {
|
||||
rc = WrapIpcCommandImpl<&IStorageInterface::operate_range>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return rc;
|
||||
};
|
||||
|
||||
Result handle_deferred() final {
|
||||
/* TODO: Panic, we can never defer. */
|
||||
return 0;
|
||||
};
|
||||
private:
|
||||
/* Actual command API. */
|
||||
virtual std::tuple<Result, u64> read(OutBuffer<u8, BufferType_Type1> buffer, u64 offset, u64 size) final {
|
||||
u64 out_size = 0;
|
||||
Result rc = this->base_storage->Read(buffer.buffer, std::min(buffer.num_elements, size), offset , &out_size);
|
||||
return {rc, out_size};
|
||||
};
|
||||
virtual std::tuple<Result> write(InBuffer<u8, BufferType_Type1> buffer, u64 offset, u64 size) final {
|
||||
return {this->base_storage->Write(buffer.buffer, std::min(buffer.num_elements, size), offset)};
|
||||
|
||||
};
|
||||
virtual std::tuple<Result> flush() final {
|
||||
return {this->base_storage->Flush()};
|
||||
};
|
||||
virtual std::tuple<Result> set_size(u64 size) final {
|
||||
return {this->base_storage->SetSize(size)};
|
||||
};
|
||||
virtual std::tuple<Result, u64> get_size() final {
|
||||
u64 out_size = 0;
|
||||
Result rc = this->base_storage->GetSize(&out_size);
|
||||
return {rc, out_size};
|
||||
};
|
||||
virtual std::tuple<Result, FsRangeInfo> operate_range(u32 operation_type, u64 offset, u64 size) final {
|
||||
FsRangeInfo out_range_info = {0};
|
||||
Result rc = this->base_storage->OperateRange(operation_type, offset, size, &out_range_info);
|
||||
return {rc, out_range_info};
|
||||
};
|
||||
};
|
||||
|
||||
class IROStorage : public IStorage {
|
||||
protected:
|
||||
virtual Result Read(void *buffer, size_t size, u64 offset, u64 *out_read_size) = 0;
|
||||
Result Write(void *buffer, size_t size, u64 offset) final {
|
||||
(void)(buffer);
|
||||
(void)(offset);
|
||||
(void)(size);
|
||||
return 0x313802;
|
||||
};
|
||||
Result Flush() final {
|
||||
return 0x0;
|
||||
};
|
||||
Result SetSize(u64 size) final {
|
||||
(void)(size);
|
||||
return 0x313802;
|
||||
};
|
||||
virtual Result GetSize(u64 *out_size) = 0;
|
||||
virtual Result OperateRange(u32 operation_type, u64 offset, u64 size, FsRangeInfo *out_range_info) = 0;
|
||||
};
|
||||
155
stratosphere/fs_mitm/source/fs_shim.c
Normal file
155
stratosphere/fs_mitm/source/fs_shim.c
Normal file
@@ -0,0 +1,155 @@
|
||||
#include <switch.h>
|
||||
#include "fs_shim.h"
|
||||
|
||||
/* Missing fsp-srv commands. */
|
||||
Result fsOpenDataStorageByDataId(Service* s, FsStorageId storage_id, u64 data_id, FsStorage* out) {
|
||||
IpcCommand c;
|
||||
ipcInitialize(&c);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 cmd_id;
|
||||
FsStorageId storage_id;
|
||||
u64 data_id;
|
||||
} *raw;
|
||||
|
||||
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||
|
||||
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;
|
||||
ipcParse(&r);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
} *resp = r.Raw;
|
||||
|
||||
rc = resp->result;
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
serviceCreate(&out->s, r.Handles[0]);
|
||||
}
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Missing FS File commands. */
|
||||
Result fsFileOperateRange(FsFile* f, u32 op_id, u64 off, u64 len, FsRangeInfo *out) {
|
||||
IpcCommand c;
|
||||
ipcInitialize(&c);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 cmd_id;
|
||||
u32 op_id;
|
||||
u64 off;
|
||||
u64 len;
|
||||
} *raw;
|
||||
|
||||
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||
|
||||
raw->magic = SFCI_MAGIC;
|
||||
raw->cmd_id = 5;
|
||||
raw->op_id = op_id;
|
||||
raw->off = off;
|
||||
raw->len = len;
|
||||
|
||||
Result rc = serviceIpcDispatch(&f->s);
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
IpcParsedCommand r;
|
||||
ipcParse(&r);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
FsRangeInfo range_info;
|
||||
} *resp = r.Raw;
|
||||
|
||||
rc = resp->result;
|
||||
if (R_SUCCEEDED(rc) && out) *out = resp->range_info;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Missing FS Storage commands. */
|
||||
Result fsStorageGetSize(FsStorage* s, u64* out) {
|
||||
IpcCommand c;
|
||||
ipcInitialize(&c);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 cmd_id;
|
||||
} *raw;
|
||||
|
||||
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||
|
||||
raw->magic = SFCI_MAGIC;
|
||||
raw->cmd_id = 4;
|
||||
|
||||
Result rc = serviceIpcDispatch(&s->s);
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
IpcParsedCommand r;
|
||||
ipcParse(&r);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
u64 size;
|
||||
} *resp = r.Raw;
|
||||
|
||||
rc = resp->result;
|
||||
if (R_SUCCEEDED(rc) && out) *out = resp->size;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
Result fsStorageOperateRange(FsStorage* s, u32 op_id, u64 off, u64 len, FsRangeInfo *out) {
|
||||
IpcCommand c;
|
||||
ipcInitialize(&c);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 cmd_id;
|
||||
u32 op_id;
|
||||
u64 off;
|
||||
u64 len;
|
||||
} *raw;
|
||||
|
||||
raw = ipcPrepareHeader(&c, sizeof(*raw));
|
||||
|
||||
raw->magic = SFCI_MAGIC;
|
||||
raw->cmd_id = 5;
|
||||
raw->op_id = op_id;
|
||||
raw->off = off;
|
||||
raw->len = len;
|
||||
|
||||
Result rc = serviceIpcDispatch(&s->s);
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
IpcParsedCommand r;
|
||||
ipcParse(&r);
|
||||
|
||||
struct {
|
||||
u64 magic;
|
||||
u64 result;
|
||||
FsRangeInfo range_info;
|
||||
} *resp = r.Raw;
|
||||
|
||||
rc = resp->result;
|
||||
if (R_SUCCEEDED(rc) && out) *out = resp->range_info;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
31
stratosphere/fs_mitm/source/fs_shim.h
Normal file
31
stratosphere/fs_mitm/source/fs_shim.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* @file fs_shim.h
|
||||
* @brief Filesystem Services (fs) IPC wrapper. To be merged into libnx, eventually.
|
||||
* @author SciresM
|
||||
* @copyright libnx Authors
|
||||
*/
|
||||
#pragma once
|
||||
#include <switch.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* TODO: Reverse this more. */
|
||||
typedef struct {
|
||||
u32 flags[0x40/sizeof(u32)];
|
||||
} FsRangeInfo;
|
||||
|
||||
/* Missing fsp-srv commands. */
|
||||
Result fsOpenDataStorageByDataId(Service* s, FsStorageId storage_id, u64 data_id, FsStorage* out);
|
||||
|
||||
/* Missing FS File commands. */
|
||||
Result fsFileOperateRange(FsFile* f, u32 op_id, u64 off, u64 len, FsRangeInfo *out);
|
||||
|
||||
/* Missing FS Storage commands. */
|
||||
Result fsStorageGetSize(FsStorage* s, u64* out);
|
||||
Result fsStorageOperateRange(FsStorage* s, u32 op_id, u64 off, u64 len, FsRangeInfo *out);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
71
stratosphere/fs_mitm/source/fsmitm_romstorage.hpp
Normal file
71
stratosphere/fs_mitm/source/fsmitm_romstorage.hpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
#include <switch.h>
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
#include "fs_istorage.hpp"
|
||||
|
||||
/* Represents a RomFS stored in some file. */
|
||||
class RomFileStorage : public IROStorage {
|
||||
private:
|
||||
FsFile *base_file;
|
||||
public:
|
||||
RomFileStorage(FsFile *f) : base_file(f) {
|
||||
/* ... */
|
||||
};
|
||||
RomFileStorage(FsFile f) {
|
||||
this->base_file = new FsFile(f);
|
||||
};
|
||||
~RomFileStorage() {
|
||||
fsFileClose(base_file);
|
||||
delete base_file;
|
||||
};
|
||||
protected:
|
||||
Result Read(void *buffer, size_t size, u64 offset, u64 *out_read_size) override {
|
||||
size_t out_sz = 0;
|
||||
Result rc = fsFileRead(this->base_file, offset, buffer, size, &out_sz);
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
*out_read_size = out_sz;
|
||||
}
|
||||
return rc;
|
||||
};
|
||||
Result GetSize(u64 *out_size) override {
|
||||
return fsFileGetSize(this->base_file, out_size);
|
||||
};
|
||||
Result OperateRange(u32 operation_type, u64 offset, u64 size, FsRangeInfo *out_range_info) override {
|
||||
/* TODO: Merge into libnx. */
|
||||
return fsFileOperateRange(this->base_file, operation_type, offset, size, out_range_info);
|
||||
};
|
||||
};
|
||||
|
||||
/* Represents a RomFS accessed via some IStorage. */
|
||||
class RomInterfaceStorage : public IROStorage {
|
||||
private:
|
||||
FsStorage *base_storage;
|
||||
public:
|
||||
RomInterfaceStorage(FsStorage *s) : base_storage(s) {
|
||||
/* ... */
|
||||
};
|
||||
RomInterfaceStorage(FsStorage s) {
|
||||
this->base_storage = new FsStorage(s);
|
||||
};
|
||||
~RomInterfaceStorage() {
|
||||
fsStorageClose(base_storage);
|
||||
delete 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);
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
*out_read_size = size;
|
||||
}
|
||||
return rc;
|
||||
};
|
||||
Result GetSize(u64 *out_size) override {
|
||||
/* TODO: Merge into libnx. */
|
||||
return fsStorageGetSize(this->base_storage, out_size);
|
||||
};
|
||||
Result OperateRange(u32 operation_type, u64 offset, u64 size, FsRangeInfo *out_range_info) override {
|
||||
/* TODO: Merge into libnx. */
|
||||
return fsStorageOperateRange(this->base_storage, operation_type, offset, size, out_range_info);
|
||||
};
|
||||
};
|
||||
@@ -1,14 +1,25 @@
|
||||
#include <switch.h>
|
||||
#include "fsmitm_service.hpp"
|
||||
#include "fs_shim.h"
|
||||
|
||||
#include "fsmitm_worker.hpp"
|
||||
#include "fsmitm_utils.hpp"
|
||||
#include "fsmitm_romstorage.hpp"
|
||||
|
||||
#include "debug.hpp"
|
||||
|
||||
Result FsMitMService::dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) {
|
||||
Result rc = 0xF601;
|
||||
|
||||
switch (cmd_id) {
|
||||
case FspSrv_Cmd_SetCurrentProcess:
|
||||
if (!this->has_initialized && r.HasPid) {
|
||||
this->process_id = r.Pid;
|
||||
}
|
||||
break;
|
||||
case FspSrv_Cmd_OpenDataStorageByDataId:
|
||||
rc = WrapIpcCommandImpl<&FsMitMService::open_data_storage_by_data_id>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
||||
break;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
@@ -37,4 +48,28 @@ Result FsMitMService::postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cm
|
||||
Result FsMitMService::handle_deferred() {
|
||||
/* This service is never deferrable. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
FsStorage data_storage;
|
||||
FsFile data_file;
|
||||
Result rc = fsOpenDataStorageByDataId(this->forward_service, storage_id, data_id, &data_storage);
|
||||
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 {
|
||||
|
||||
out_session = new IPCSession<IStorageInterface>(new IStorageInterface(new RomInterfaceStorage(data_storage)));
|
||||
}
|
||||
FsMitmWorker::AddWaitable(out_session);
|
||||
out_h = out_session->get_client_handle();
|
||||
}
|
||||
return {rc, out_h};
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
enum FspSrvCmd {
|
||||
FspSrv_Cmd_SetCurrentProcess = 1,
|
||||
FspSrv_Cmd_OpenDataStorageByDataId = 202,
|
||||
};
|
||||
|
||||
class FsMitMService : public IMitMServiceObject {
|
||||
@@ -13,10 +14,14 @@ class FsMitMService : public IMitMServiceObject {
|
||||
u64 process_id;
|
||||
u64 title_id;
|
||||
public:
|
||||
FsMitMService() : has_initialized(false), process_id(0), title_id(0) {
|
||||
FsMitMService(Service *s) : IMitMServiceObject(s), has_initialized(false), process_id(0), title_id(0) {
|
||||
/* ... */
|
||||
}
|
||||
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 Result handle_deferred();
|
||||
|
||||
protected:
|
||||
/* Overridden commands. */
|
||||
std::tuple<Result, MovedHandle> open_data_storage_by_data_id(FsStorageId storage_id, u64 data_id);
|
||||
};
|
||||
29
stratosphere/fs_mitm/source/fsmitm_utils.cpp
Normal file
29
stratosphere/fs_mitm/source/fsmitm_utils.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <switch.h>
|
||||
#include <stratosphere.hpp>
|
||||
#include <atomic>
|
||||
|
||||
#include "fsmitm_utils.hpp"
|
||||
|
||||
static FsFileSystem g_sd_filesystem;
|
||||
static bool g_has_initialized = false;
|
||||
|
||||
static Result EnsureInitialized() {
|
||||
if (g_has_initialized) {
|
||||
return 0x0;
|
||||
}
|
||||
Result rc = fsMountSdcard(&g_sd_filesystem);
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
g_has_initialized = true;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
Result Utils::OpenSdFile(const char *fn, int flags, FsFile *out) {
|
||||
Result rc;
|
||||
if (R_FAILED((rc = EnsureInitialized()))) {
|
||||
return rc;
|
||||
}
|
||||
char path[FS_MAX_PATH];
|
||||
strcpy(path, fn);
|
||||
return fsFsOpenFile(&g_sd_filesystem, path, flags, out);
|
||||
}
|
||||
8
stratosphere/fs_mitm/source/fsmitm_utils.hpp
Normal file
8
stratosphere/fs_mitm/source/fsmitm_utils.hpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#include <switch.h>
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
class Utils {
|
||||
public:
|
||||
static Result OpenSdFile(const char *fn, int flags, FsFile *out);
|
||||
};
|
||||
@@ -4,6 +4,12 @@
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
class IMitMServiceObject : public IServiceObject {
|
||||
protected:
|
||||
Service *forward_service;
|
||||
public:
|
||||
IMitMServiceObject(Service *s) : forward_service(s) {
|
||||
|
||||
}
|
||||
protected:
|
||||
virtual ~IMitMServiceObject() { }
|
||||
virtual Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) = 0;
|
||||
|
||||
@@ -28,18 +28,19 @@ class MitMSession final : public IWaitable {
|
||||
static_assert(sizeof(pointer_buffer) <= POINTER_BUFFER_SIZE_MAX, "Incorrect Size for PointerBuffer!");
|
||||
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) {
|
||||
this->service_object = new T();
|
||||
if (R_FAILED(smMitMGetService(&forward_service, srv))) {
|
||||
/* TODO: Panic. */
|
||||
}
|
||||
if (R_FAILED(ipcQueryPointerBufferSize(forward_service.handle, &pointer_buffer_size))) {
|
||||
/* TODO: Panic. */
|
||||
}
|
||||
this->service_object = new T(&forward_service);
|
||||
this->pointer_buffer = new char[pointer_buffer_size];
|
||||
}
|
||||
|
||||
~MitMSession() override {
|
||||
delete this->service_object;
|
||||
delete this->pointer_buffer;
|
||||
serviceClose(&forward_service);
|
||||
if (server_handle) {
|
||||
svcCloseHandle(server_handle);
|
||||
|
||||
Reference in New Issue
Block a user