fs.mitm: Fix c++ modernization breaking changes

This commit is contained in:
Michael Scire 2018-06-27 23:02:06 -06:00
parent ed718e007f
commit 8da27723fc
7 changed files with 161 additions and 151 deletions

View file

@ -9,7 +9,12 @@
LayeredRomFS::LayeredRomFS(std::shared_ptr<RomInterfaceStorage> s_r, std::shared_ptr<RomFileStorage> f_r, u64 tid) : storage_romfs(s_r), file_romfs(f_r), title_id(tid) { LayeredRomFS::LayeredRomFS(std::shared_ptr<RomInterfaceStorage> s_r, std::shared_ptr<RomFileStorage> f_r, u64 tid) : storage_romfs(s_r), file_romfs(f_r), title_id(tid) {
/* Start building the new virtual romfs. */ /* Start building the new virtual romfs. */
RomFSBuildContext build_ctx(this->title_id); RomFSBuildContext build_ctx(this->title_id);
this->p_source_infos = std::make_shared<std::vector<RomFSSourceInfo>>(); this->p_source_infos = std::shared_ptr<std::vector<RomFSSourceInfo>>(new std::vector<RomFSSourceInfo>(), [](std::vector<RomFSSourceInfo> *to_delete) {
for (unsigned int i = 0; i < to_delete->size(); i++) {
(*to_delete)[i].Cleanup();
}
delete to_delete;
});
if (Utils::IsSdInitialized()) { if (Utils::IsSdInitialized()) {
build_ctx.MergeSdFiles(); build_ctx.MergeSdFiles();
} }
@ -51,6 +56,7 @@ Result LayeredRomFS::Read(void *buffer, size_t size, u64 offset) {
} }
} }
Result rc;
size_t read_so_far = 0; size_t read_so_far = 0;
while (read_so_far < size) { while (read_so_far < size) {
RomFSSourceInfo *cur_source = &((*this->p_source_infos)[cur_source_ind]); RomFSSourceInfo *cur_source = &((*this->p_source_infos)[cur_source_ind]);
@ -59,11 +65,11 @@ Result LayeredRomFS::Read(void *buffer, size_t size, u64 offset) {
if (cur_read_size > cur_source->size - (offset - cur_source->virtual_offset)) { if (cur_read_size > cur_source->size - (offset - cur_source->virtual_offset)) {
cur_read_size = cur_source->size - (offset - cur_source->virtual_offset); cur_read_size = cur_source->size - (offset - cur_source->virtual_offset);
} }
auto source_info_visitor = [&](auto& info) -> Result { switch (cur_source->type) {
Result rc = 0; case RomFSDataSource::LooseFile:
if constexpr (std::is_same_v<decltype(info), RomFSBaseSourceInfo>) { {
FsFile file; FsFile file;
if (R_FAILED((rc = Utils::OpenRomFSSdFile(this->title_id, info.path, FS_OPEN_READ, &file)))) { if (R_FAILED((rc = Utils::OpenRomFSSdFile(this->title_id, cur_source->loose_source_info.path, FS_OPEN_READ, &file)))) {
fatalSimple(rc); fatalSimple(rc);
} }
size_t out_read; size_t out_read;
@ -74,23 +80,32 @@ Result LayeredRomFS::Read(void *buffer, size_t size, u64 offset) {
Reboot(); Reboot();
} }
fsFileClose(&file); fsFileClose(&file);
} else if constexpr (std::is_same_v<decltype(info), RomFSFileSourceInfo>) { }
memcpy((void *)((uintptr_t)buffer + read_so_far), info.data + (offset - cur_source->virtual_offset), cur_read_size); break;
} else if constexpr (std::is_same_v<decltype(info), RomFSLooseSourceInfo>) { case RomFSDataSource::Memory:
if (R_FAILED((rc = this->storage_romfs->Read((void *)((uintptr_t)buffer + read_so_far), cur_read_size, info.offset + (offset - cur_source->virtual_offset))))) { {
memcpy((void *)((uintptr_t)buffer + read_so_far), cur_source->memory_source_info.data + (offset - cur_source->virtual_offset), cur_read_size);
}
break;
case RomFSDataSource::BaseRomFS:
{
if (R_FAILED((rc = this->storage_romfs->Read((void *)((uintptr_t)buffer + read_so_far), cur_read_size, cur_source->base_source_info.offset + (offset - cur_source->virtual_offset))))) {
/* TODO: Can this ever happen? */ /* TODO: Can this ever happen? */
/* fatalSimple(rc); */ /* fatalSimple(rc); */
return rc; return rc;
} }
} else if constexpr (std::is_same_v<decltype(info), RomFSMemorySourceInfo>) { }
if (R_FAILED((rc = this->file_romfs->Read((void *)((uintptr_t)buffer + read_so_far), cur_read_size, info.offset + (offset - cur_source->virtual_offset))))) { break;
case RomFSDataSource::FileRomFS:
{
if (R_FAILED((rc = this->file_romfs->Read((void *)((uintptr_t)buffer + read_so_far), cur_read_size, cur_source->base_source_info.offset + (offset - cur_source->virtual_offset))))) {
fatalSimple(rc); fatalSimple(rc);
} }
} }
return rc; break;
}; default:
Result rc = std::visit(source_info_visitor, cur_source->info); fatalSimple(0xF601);
}
read_so_far += cur_read_size; read_so_far += cur_read_size;
} else { } else {
/* Handle padding explicitly. */ /* Handle padding explicitly. */

View file

@ -357,7 +357,7 @@ void RomFSBuildContext::Build(std::vector<RomFSSourceInfo> *out_infos) {
case RomFSDataSource::BaseRomFS: case RomFSDataSource::BaseRomFS:
case RomFSDataSource::FileRomFS: case RomFSDataSource::FileRomFS:
/* Try to compact, if possible. */ /* Try to compact, if possible. */
if (out_infos->back().GetType() == cur_file->source) { if (out_infos->back().type == cur_file->source) {
out_infos->back().size = cur_file->offset + ROMFS_FILEPARTITION_OFS + cur_file->size - out_infos->back().virtual_offset; out_infos->back().size = cur_file->offset + ROMFS_FILEPARTITION_OFS + cur_file->size - out_infos->back().virtual_offset;
} else { } else {
out_infos->emplace_back(cur_file->offset + ROMFS_FILEPARTITION_OFS, cur_file->size, cur_file->orig_offset + ROMFS_FILEPARTITION_OFS, cur_file->source); out_infos->emplace_back(cur_file->offset + ROMFS_FILEPARTITION_OFS, cur_file->size, cur_file->orig_offset + ROMFS_FILEPARTITION_OFS, cur_file->source);

View file

@ -4,6 +4,8 @@
#include "fsmitm_romstorage.hpp" #include "fsmitm_romstorage.hpp"
#include "debug.hpp"
#define ROMFS_ENTRY_EMPTY 0xFFFFFFFF #define ROMFS_ENTRY_EMPTY 0xFFFFFFFF
#define ROMFS_FILEPARTITION_OFS 0x200 #define ROMFS_FILEPARTITION_OFS 0x200
@ -31,92 +33,66 @@ struct RomFSMemorySourceInfo {
const u8 *data; const u8 *data;
}; };
class RomFSSourceInfo { struct RomFSSourceInfo {
using InfoVariant = std::variant<RomFSBaseSourceInfo, RomFSFileSourceInfo, RomFSLooseSourceInfo, RomFSMemorySourceInfo>;
static InfoVariant MakeInfoVariantFromOffset(u64 offset, RomFSDataSource t) {
switch(t) {
case RomFSDataSource::BaseRomFS:
return RomFSBaseSourceInfo { offset };
case RomFSDataSource::FileRomFS:
return RomFSFileSourceInfo { offset };
default:
fatalSimple(0xF601);
}
}
static InfoVariant MakeInfoVariantFromPointer(const void *arg, RomFSDataSource t) {
switch(t) {
case RomFSDataSource::LooseFile:
return RomFSLooseSourceInfo { (decltype(RomFSLooseSourceInfo::path))arg };
case RomFSDataSource::Memory:
return RomFSMemorySourceInfo { (decltype(RomFSMemorySourceInfo::data))arg };
default:
fatalSimple(0xF601);
}
}
struct InfoCleanupHelper {
void operator()(RomFSBaseSourceInfo& info) {
}
void operator()(RomFSFileSourceInfo& info) {
}
void operator()(RomFSLooseSourceInfo& info) {
delete info.path;
}
void operator()(RomFSMemorySourceInfo& info) {
delete info.data;
}
};
struct GetTypeHelper {
RomFSDataSource operator()(const RomFSBaseSourceInfo& info) const {
return RomFSDataSource::BaseRomFS;
}
RomFSDataSource operator()(const RomFSFileSourceInfo& info) const {
return RomFSDataSource::FileRomFS;
}
RomFSDataSource operator()(const RomFSLooseSourceInfo& info) const {
return RomFSDataSource::LooseFile;
}
RomFSDataSource operator()(const RomFSMemorySourceInfo& info) const {
return RomFSDataSource::Memory;
}
};
public:
u64 virtual_offset; u64 virtual_offset;
u64 size; u64 size;
union {
RomFSBaseSourceInfo base_source_info;
RomFSFileSourceInfo file_source_info;
RomFSLooseSourceInfo loose_source_info;
RomFSMemorySourceInfo memory_source_info;
};
RomFSDataSource type;
InfoVariant info; RomFSSourceInfo(u64 v_o, u64 s, u64 offset, RomFSDataSource t) : virtual_offset(v_o), size(s), type(t) {
switch (this->type) {
RomFSSourceInfo(u64 v_o, u64 s, u64 offset, RomFSDataSource t) : virtual_offset(v_o), size(s), info(MakeInfoVariantFromOffset(offset, t)) { case RomFSDataSource::BaseRomFS:
this->base_source_info.offset = offset;
break;
case RomFSDataSource::FileRomFS:
this->file_source_info.offset = offset;
break;
case RomFSDataSource::LooseFile:
case RomFSDataSource::Memory:
default:
fatalSimple(0xF601);
}
} }
RomFSSourceInfo(u64 v_o, u64 s, const void *arg, RomFSDataSource t) : virtual_offset(v_o), size(s), info(MakeInfoVariantFromPointer(arg, t)) { RomFSSourceInfo(u64 v_o, u64 s, const void *arg, RomFSDataSource t) : virtual_offset(v_o), size(s), type(t) {
switch (this->type) {
case RomFSDataSource::LooseFile:
this->loose_source_info.path = (decltype(this->loose_source_info.path))arg;
break;
case RomFSDataSource::Memory:
this->memory_source_info.data = (decltype(this->memory_source_info.data))arg;
break;
case RomFSDataSource::BaseRomFS:
case RomFSDataSource::FileRomFS:
default:
fatalSimple(0xF601);
}
} }
~RomFSSourceInfo() { void Cleanup() {
std::visit(InfoCleanupHelper{}, info); switch (this->type) {
case RomFSDataSource::BaseRomFS:
case RomFSDataSource::FileRomFS:
break;
case RomFSDataSource::LooseFile:
delete this->loose_source_info.path;
break;
case RomFSDataSource::Memory:
delete this->memory_source_info.data;
break;
default:
fatalSimple(0xF601);
}
} }
static bool Compare(RomFSSourceInfo *a, RomFSSourceInfo *b) { static bool Compare(RomFSSourceInfo *a, RomFSSourceInfo *b) {
return (a->virtual_offset < b->virtual_offset); return (a->virtual_offset < b->virtual_offset);
} }
RomFSDataSource GetType() const {
return std::visit(GetTypeHelper{}, info);
}
}; };
/* Types for building a RomFS. */ /* Types for building a RomFS. */
@ -165,26 +141,26 @@ struct RomFSBuildDirectoryContext {
char path[FS_MAX_PATH]; char path[FS_MAX_PATH];
u32 cur_path_ofs; u32 cur_path_ofs;
u32 path_len; u32 path_len;
u32 entry_offset; u32 entry_offset = 0;
RomFSBuildDirectoryContext *parent; RomFSBuildDirectoryContext *parent = NULL;
RomFSBuildDirectoryContext *child; RomFSBuildDirectoryContext *child = NULL;
RomFSBuildDirectoryContext *sibling; RomFSBuildDirectoryContext *sibling = NULL;
RomFSBuildFileContext *file; RomFSBuildFileContext *file = NULL;
RomFSBuildDirectoryContext *next; RomFSBuildDirectoryContext *next = NULL;
}; };
struct RomFSBuildFileContext { struct RomFSBuildFileContext {
char path[FS_MAX_PATH]; char path[FS_MAX_PATH];
u32 cur_path_ofs; u32 cur_path_ofs;
u32 path_len; u32 path_len;
u32 entry_offset; u32 entry_offset = 0;
u64 offset; u64 offset = 0;
u64 size; u64 size = 0;
RomFSBuildDirectoryContext *parent; RomFSBuildDirectoryContext *parent = NULL;
RomFSBuildFileContext *sibling; RomFSBuildFileContext *sibling = NULL;
RomFSBuildFileContext *next; RomFSBuildFileContext *next = NULL;
RomFSDataSource source; RomFSDataSource source{0};
u64 orig_offset; u64 orig_offset = 0;
}; };
class RomFSBuildContext { class RomFSBuildContext {

View file

@ -70,10 +70,23 @@ Result FsMitMService::handle_deferred() {
/* Add redirection for RomFS to the SD card. */ /* Add redirection for RomFS to the SD card. */
std::tuple<Result, OutSession<IStorageInterface>> FsMitMService::open_data_storage_by_current_process() { std::tuple<Result, OutSession<IStorageInterface>> FsMitMService::open_data_storage_by_current_process() {
IPCSession<IStorageInterface> *out_session = NULL; IPCSession<IStorageInterface> *out_session = NULL;
FsStorage data_storage; std::shared_ptr<IStorageInterface> out_storage = nullptr;
FsFile data_file;
u32 out_domain_id = 0; u32 out_domain_id = 0;
Result rc; Result rc;
if (this->romfs_storage != nullptr) {
if (this->get_owner() != NULL) {
rc = fsOpenDataStorageByCurrentProcessFromDomainFwd(this->forward_service, &out_domain_id);
} else {
rc = 0;
}
if (R_SUCCEEDED(rc)) {
out_storage = this->romfs_storage;
out_session = new IPCSession<IStorageInterface>(out_storage);
}
} else {
FsStorage data_storage;
FsFile data_file;
if (this->get_owner() == NULL) { if (this->get_owner() == NULL) {
rc = fsOpenDataStorageByCurrentProcessFwd(this->forward_service, &data_storage); rc = fsOpenDataStorageByCurrentProcessFwd(this->forward_service, &data_storage);
} else { } else {
@ -86,14 +99,17 @@ std::tuple<Result, OutSession<IStorageInterface>> FsMitMService::open_data_stora
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
/* TODO: Is there a sensible path that ends in ".romfs" we can use?" */ /* TODO: Is there a sensible path that ends in ".romfs" we can use?" */
if (R_SUCCEEDED(Utils::OpenSdFileForAtmosphere(this->title_id, "romfs.bin", FS_OPEN_READ, &data_file))) { if (R_SUCCEEDED(Utils::OpenSdFileForAtmosphere(this->title_id, "romfs.bin", FS_OPEN_READ, &data_file))) {
out_session = new IPCSession<IStorageInterface>(std::make_shared<IStorageInterface>(new LayeredRomFS(std::make_shared<RomInterfaceStorage>(data_storage), std::make_shared<RomFileStorage>(data_file), this->title_id))); out_storage = std::make_shared<IStorageInterface>(new LayeredRomFS(std::make_shared<RomInterfaceStorage>(data_storage), std::make_shared<RomFileStorage>(data_file), this->title_id));
} else { } else {
out_session = new IPCSession<IStorageInterface>(std::make_shared<IStorageInterface>(new LayeredRomFS(std::make_shared<RomInterfaceStorage>(data_storage), nullptr, this->title_id))); out_storage = std::make_shared<IStorageInterface>(new LayeredRomFS(std::make_shared<RomInterfaceStorage>(data_storage), nullptr, this->title_id));
} }
this->romfs_storage = out_storage;
out_session = new IPCSession<IStorageInterface>(out_storage);
if (this->get_owner() == NULL) { if (this->get_owner() == NULL) {
FsMitMWorker::AddWaitable(out_session); FsMitMWorker::AddWaitable(out_session);
} }
} }
}
OutSession out_s = OutSession(out_session); OutSession out_s = OutSession(out_session);
out_s.domain_id = out_domain_id; out_s.domain_id = out_domain_id;

View file

@ -14,6 +14,7 @@ class FsMitMService : public IMitMServiceObject {
private: private:
bool has_initialized; bool has_initialized;
u64 init_pid; u64 init_pid;
std::shared_ptr<IStorageInterface> romfs_storage;
public: public:
FsMitMService(Service *s) : IMitMServiceObject(s), has_initialized(false), init_pid(0) { FsMitMService(Service *s) : IMitMServiceObject(s), has_initialized(false), init_pid(0) {
/* ... */ /* ... */

View file

@ -130,6 +130,8 @@ class MitMSession final : public ISession<T> {
MitMSession<T> *new_sess = new MitMSession<T>((MitMServer<T> *)this->server, s_h, c_h, cur_out_r.Handles[0]); MitMSession<T> *new_sess = new MitMSession<T>((MitMServer<T> *)this->server, s_h, c_h, cur_out_r.Handles[0]);
new_sess->service_object = this->service_object; new_sess->service_object = this->service_object;
IServiceObject *obj = this->service_object.get();
if (this->is_domain) { if (this->is_domain) {
new_sess->is_domain = true; new_sess->is_domain = true;
new_sess->domain = this->domain; new_sess->domain = this->domain;
@ -188,10 +190,6 @@ class MitMSession final : public ISession<T> {
//Reboot(); //Reboot();
} }
if (retval == 0xA08) {
Reboot();
}
return retval; return retval;
} }

View file

@ -14,7 +14,11 @@ class DomainOwner {
private: private:
std::array<std::shared_ptr<IServiceObject>, DOMAIN_ID_MAX> domain_objects; std::array<std::shared_ptr<IServiceObject>, DOMAIN_ID_MAX> domain_objects;
public: public:
DomainOwner() = default; DomainOwner() {
for (unsigned int i = 0; i < DOMAIN_ID_MAX; i++) {
this->domain_objects[i].reset();
}
}
/* Shared ptrs should auto delete here. */ /* Shared ptrs should auto delete here. */
virtual ~DomainOwner() = default; virtual ~DomainOwner() = default;
@ -33,14 +37,14 @@ class DomainOwner {
} }
*out_i = std::distance(domain_objects.begin(), object_it); *out_i = std::distance(domain_objects.begin(), object_it);
*object_it = std::move(object); *object_it = object;
(*object_it)->set_owner(this); object->set_owner(this);
return 0; return 0;
} }
Result set_object(std::shared_ptr<IServiceObject> object, unsigned int i) { Result set_object(std::shared_ptr<IServiceObject> object, unsigned int i) {
if (domain_objects[i] == NULL) { if (domain_objects[i] == nullptr) {
domain_objects[i] = std::move(object); domain_objects[i] = object;
object->set_owner(this); object->set_owner(this);
return 0; return 0;
} }