summaryrefslogtreecommitdiffstats
path: root/src/core/file_sys/fsmitm_romfsbuild.cpp
diff options
context:
space:
mode:
authorZach Hilman <zachhilman@gmail.com>2018-09-24 03:50:16 +0200
committerZach Hilman <zachhilman@gmail.com>2018-09-24 03:50:20 +0200
commitb3c2ec362bbbdd89da9c0aa84b425717f5e3d351 (patch)
treed3f4e621532f1f280f94bac4e6d071707aabbd35 /src/core/file_sys/fsmitm_romfsbuild.cpp
parentqt: Add UI elements for LayeredFS and related tools (diff)
downloadyuzu-b3c2ec362bbbdd89da9c0aa84b425717f5e3d351.tar
yuzu-b3c2ec362bbbdd89da9c0aa84b425717f5e3d351.tar.gz
yuzu-b3c2ec362bbbdd89da9c0aa84b425717f5e3d351.tar.bz2
yuzu-b3c2ec362bbbdd89da9c0aa84b425717f5e3d351.tar.lz
yuzu-b3c2ec362bbbdd89da9c0aa84b425717f5e3d351.tar.xz
yuzu-b3c2ec362bbbdd89da9c0aa84b425717f5e3d351.tar.zst
yuzu-b3c2ec362bbbdd89da9c0aa84b425717f5e3d351.zip
Diffstat (limited to 'src/core/file_sys/fsmitm_romfsbuild.cpp')
-rw-r--r--src/core/file_sys/fsmitm_romfsbuild.cpp358
1 files changed, 181 insertions, 177 deletions
diff --git a/src/core/file_sys/fsmitm_romfsbuild.cpp b/src/core/file_sys/fsmitm_romfsbuild.cpp
index a6fe6719d..21fc3d796 100644
--- a/src/core/file_sys/fsmitm_romfsbuild.cpp
+++ b/src/core/file_sys/fsmitm_romfsbuild.cpp
@@ -24,9 +24,9 @@
#include <cstring>
#include "common/assert.h"
-#include "fsmitm_romfsbuild.hpp"
-#include "vfs.h"
-#include "vfs_vector.h"
+#include "core/file_sys/fsmitm_romfsbuild.h"
+#include "core/file_sys/vfs.h"
+#include "core/file_sys/vfs_vector.h"
namespace FileSys {
@@ -35,7 +35,7 @@ constexpr u64 FS_MAX_PATH = 0x301;
constexpr u32 ROMFS_ENTRY_EMPTY = 0xFFFFFFFF;
constexpr u32 ROMFS_FILEPARTITION_OFS = 0x200;
-/* Types for building a RomFS. */
+// Types for building a RomFS.
struct RomFSHeader {
u64 header_size;
u64 dir_hash_table_ofs;
@@ -57,7 +57,6 @@ struct RomFSDirectoryEntry {
u32 file;
u32 hash;
u32 name_size;
- char name[];
};
static_assert(sizeof(RomFSDirectoryEntry) == 0x18, "RomFSDirectoryEntry has incorrect size.");
@@ -68,37 +67,63 @@ struct RomFSFileEntry {
u64 size;
u32 hash;
u32 name_size;
- char name[];
};
static_assert(sizeof(RomFSFileEntry) == 0x20, "RomFSFileEntry has incorrect size.");
struct RomFSBuildFileContext;
struct RomFSBuildDirectoryContext {
- char* path;
- u32 cur_path_ofs;
- u32 path_len;
+ std::string path = "";
+ u32 cur_path_ofs = 0;
+ u32 path_len = 0;
u32 entry_offset = 0;
- RomFSBuildDirectoryContext* parent = nullptr;
- RomFSBuildDirectoryContext* child = nullptr;
- RomFSBuildDirectoryContext* sibling = nullptr;
- RomFSBuildFileContext* file = nullptr;
+ std::shared_ptr<RomFSBuildDirectoryContext> parent;
+ std::shared_ptr<RomFSBuildDirectoryContext> child;
+ std::shared_ptr<RomFSBuildDirectoryContext> sibling;
+ std::shared_ptr<RomFSBuildFileContext> file;
};
struct RomFSBuildFileContext {
- char* path;
- u32 cur_path_ofs;
- u32 path_len;
+ std::string path = "";
+ u32 cur_path_ofs = 0;
+ u32 path_len = 0;
u32 entry_offset = 0;
u64 offset = 0;
u64 size = 0;
- RomFSBuildDirectoryContext* parent = nullptr;
- RomFSBuildFileContext* sibling = nullptr;
+ std::shared_ptr<RomFSBuildDirectoryContext> parent;
+ std::shared_ptr<RomFSBuildFileContext> sibling;
VirtualFile source = nullptr;
+
+ RomFSBuildFileContext() : path(""), cur_path_ofs(0), path_len(0) {}
};
-void RomFSBuildContext::VisitDirectory(VirtualDir root_romfs, RomFSBuildDirectoryContext* parent) {
- std::vector<RomFSBuildDirectoryContext*> child_dirs;
+static u32 romfs_calc_path_hash(u32 parent, std::string path, u32 start, size_t path_len) {
+ u32 hash = parent ^ 123456789;
+ for (u32 i = 0; i < path_len; i++) {
+ hash = (hash >> 5) | (hash << 27);
+ hash ^= path[start + i];
+ }
+
+ return hash;
+}
+
+static u32 romfs_get_hash_table_count(u32 num_entries) {
+ if (num_entries < 3) {
+ return 3;
+ } else if (num_entries < 19) {
+ return num_entries | 1;
+ }
+ u32 count = num_entries;
+ while (count % 2 == 0 || count % 3 == 0 || count % 5 == 0 || count % 7 == 0 ||
+ count % 11 == 0 || count % 13 == 0 || count % 17 == 0) {
+ count++;
+ }
+ return count;
+}
+
+void RomFSBuildContext::VisitDirectory(VirtualDir root_romfs,
+ std::shared_ptr<RomFSBuildDirectoryContext> parent) {
+ std::vector<std::shared_ptr<RomFSBuildDirectoryContext>> child_dirs;
VirtualDir dir;
@@ -111,41 +136,33 @@ void RomFSBuildContext::VisitDirectory(VirtualDir root_romfs, RomFSBuildDirector
for (const auto& kv : entries) {
if (kv.second == VfsEntryType::Directory) {
- RomFSBuildDirectoryContext* child = new RomFSBuildDirectoryContext({0});
- /* Set child's path. */
+ const auto child = std::make_shared<RomFSBuildDirectoryContext>();
+ // Set child's path.
child->cur_path_ofs = parent->path_len + 1;
child->path_len = child->cur_path_ofs + kv.first.size();
- child->path = new char[child->path_len + 1];
- strcpy(child->path, parent->path);
+ child->path = parent->path + "/" + kv.first;
+
+ // Sanity check on path_len
ASSERT(child->path_len < FS_MAX_PATH);
- strcat(child->path + parent->path_len, "/");
- strcat(child->path + parent->path_len, kv.first.c_str());
- if (!this->AddDirectory(parent, child, nullptr)) {
- delete child->path;
- delete child;
- } else {
+ if (AddDirectory(parent, child)) {
child_dirs.push_back(child);
}
} else {
- RomFSBuildFileContext* child = new RomFSBuildFileContext({0});
- /* Set child's path. */
+ const auto child = std::make_shared<RomFSBuildFileContext>();
+ // Set child's path.
child->cur_path_ofs = parent->path_len + 1;
child->path_len = child->cur_path_ofs + kv.first.size();
- child->path = new char[child->path_len + 1];
- strcpy(child->path, parent->path);
+ child->path = parent->path + "/" + kv.first;
+
+ // Sanity check on path_len
ASSERT(child->path_len < FS_MAX_PATH);
- strcat(child->path + parent->path_len, "/");
- strcat(child->path + parent->path_len, kv.first.c_str());
child->source = root_romfs->GetFileRelative(child->path);
child->size = child->source->GetSize();
- if (!this->AddFile(parent, child)) {
- delete child->path;
- delete child;
- }
+ AddFile(parent, child);
}
}
@@ -154,213 +171,200 @@ void RomFSBuildContext::VisitDirectory(VirtualDir root_romfs, RomFSBuildDirector
}
}
-bool RomFSBuildContext::AddDirectory(RomFSBuildDirectoryContext* parent_dir_ctx,
- RomFSBuildDirectoryContext* dir_ctx,
- RomFSBuildDirectoryContext** out_dir_ctx) {
- /* Check whether it's already in the known directories. */
- auto existing = this->directories.find(dir_ctx->path);
- if (existing != this->directories.end()) {
- if (out_dir_ctx) {
- *out_dir_ctx = existing->second;
- }
+bool RomFSBuildContext::AddDirectory(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
+ std::shared_ptr<RomFSBuildDirectoryContext> dir_ctx) {
+ // Check whether it's already in the known directories.
+ const auto existing = directories.find(dir_ctx->path);
+ if (existing != directories.end())
return false;
- }
- /* Add a new directory. */
- this->num_dirs++;
- this->dir_table_size +=
+ // Add a new directory.
+ num_dirs++;
+ dir_table_size +=
sizeof(RomFSDirectoryEntry) + ((dir_ctx->path_len - dir_ctx->cur_path_ofs + 3) & ~3);
dir_ctx->parent = parent_dir_ctx;
- this->directories.insert({dir_ctx->path, dir_ctx});
+ directories.emplace(dir_ctx->path, dir_ctx);
- if (out_dir_ctx) {
- *out_dir_ctx = dir_ctx;
- }
return true;
}
-bool RomFSBuildContext::AddFile(RomFSBuildDirectoryContext* parent_dir_ctx,
- RomFSBuildFileContext* file_ctx) {
- /* Check whether it's already in the known files. */
- auto existing = this->files.find(file_ctx->path);
- if (existing != this->files.end()) {
+bool RomFSBuildContext::AddFile(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
+ std::shared_ptr<RomFSBuildFileContext> file_ctx) {
+ // Check whether it's already in the known files.
+ const auto existing = files.find(file_ctx->path);
+ if (existing != files.end()) {
return false;
}
- /* Add a new file. */
- this->num_files++;
- this->file_table_size +=
+ // Add a new file.
+ num_files++;
+ file_table_size +=
sizeof(RomFSFileEntry) + ((file_ctx->path_len - file_ctx->cur_path_ofs + 3) & ~3);
file_ctx->parent = parent_dir_ctx;
- this->files.insert({file_ctx->path, file_ctx});
+ files.emplace(file_ctx->path, file_ctx);
return true;
}
RomFSBuildContext::RomFSBuildContext(VirtualDir base_) : base(std::move(base_)) {
- this->root = new RomFSBuildDirectoryContext({0});
- this->root->path = new char[1];
- this->root->path[0] = '\x00';
- this->directories.insert({this->root->path, this->root});
- this->num_dirs = 1;
- this->dir_table_size = 0x18;
-
- VisitDirectory(base, this->root);
+ root = std::make_shared<RomFSBuildDirectoryContext>();
+ root->path = "\0";
+ directories.emplace(root->path, root);
+ num_dirs = 1;
+ dir_table_size = 0x18;
+
+ VisitDirectory(base, root);
}
+RomFSBuildContext::~RomFSBuildContext() = default;
+
std::map<u64, VirtualFile> RomFSBuildContext::Build() {
- std::map<u64, VirtualFile> out;
- RomFSBuildFileContext* cur_file;
- RomFSBuildDirectoryContext* cur_dir;
-
- const auto dir_hash_table_entry_count = romfs_get_hash_table_count(this->num_dirs);
- const auto file_hash_table_entry_count = romfs_get_hash_table_count(this->num_files);
- this->dir_hash_table_size = 4 * dir_hash_table_entry_count;
- this->file_hash_table_size = 4 * file_hash_table_entry_count;
-
- /* Assign metadata pointers */
- RomFSHeader* header = new RomFSHeader({0});
- auto metadata = new u8[this->dir_hash_table_size + this->dir_table_size +
- this->file_hash_table_size + this->file_table_size];
- auto dir_hash_table = reinterpret_cast<u32*>(metadata);
- const auto dir_table = reinterpret_cast<RomFSDirectoryEntry*>(
- reinterpret_cast<uintptr_t>(dir_hash_table) + this->dir_hash_table_size);
- auto file_hash_table =
- reinterpret_cast<u32*>(reinterpret_cast<uintptr_t>(dir_table) + this->dir_table_size);
- const auto file_table = reinterpret_cast<RomFSFileEntry*>(
- reinterpret_cast<uintptr_t>(file_hash_table) + this->file_hash_table_size);
-
- /* Clear out hash tables. */
+ const auto dir_hash_table_entry_count = romfs_get_hash_table_count(num_dirs);
+ const auto file_hash_table_entry_count = romfs_get_hash_table_count(num_files);
+ dir_hash_table_size = 4 * dir_hash_table_entry_count;
+ file_hash_table_size = 4 * file_hash_table_entry_count;
+
+ // Assign metadata pointers
+ RomFSHeader header{};
+
+ std::vector<u32> dir_hash_table(dir_hash_table_entry_count, ROMFS_ENTRY_EMPTY);
+ std::vector<u32> file_hash_table(file_hash_table_entry_count, ROMFS_ENTRY_EMPTY);
+
+ std::vector<u8> dir_table(dir_table_size);
+ std::vector<u8> file_table(file_table_size);
+
+ // Clear out hash tables.
for (u32 i = 0; i < dir_hash_table_entry_count; i++)
dir_hash_table[i] = ROMFS_ENTRY_EMPTY;
for (u32 i = 0; i < file_hash_table_entry_count; i++)
file_hash_table[i] = ROMFS_ENTRY_EMPTY;
- out.clear();
+ std::shared_ptr<RomFSBuildFileContext> cur_file;
- /* Determine file offsets. */
+ // Determine file offsets.
u32 entry_offset = 0;
- RomFSBuildFileContext* prev_file = nullptr;
- for (const auto& it : this->files) {
+ std::shared_ptr<RomFSBuildFileContext> prev_file = nullptr;
+ for (const auto& it : files) {
cur_file = it.second;
- this->file_partition_size = (this->file_partition_size + 0xFULL) & ~0xFULL;
- cur_file->offset = this->file_partition_size;
- this->file_partition_size += cur_file->size;
+ file_partition_size = (file_partition_size + 0xFULL) & ~0xFULL;
+ cur_file->offset = file_partition_size;
+ file_partition_size += cur_file->size;
cur_file->entry_offset = entry_offset;
entry_offset +=
sizeof(RomFSFileEntry) + ((cur_file->path_len - cur_file->cur_path_ofs + 3) & ~3);
prev_file = cur_file;
}
- /* Assign deferred parent/sibling ownership. */
- for (auto it = this->files.rbegin(); it != this->files.rend(); it++) {
+ // Assign deferred parent/sibling ownership.
+ for (auto it = files.rbegin(); it != files.rend(); ++it) {
cur_file = it->second;
cur_file->sibling = cur_file->parent->file;
cur_file->parent->file = cur_file;
}
- /* Determine directory offsets. */
+ std::shared_ptr<RomFSBuildDirectoryContext> cur_dir;
+
+ // Determine directory offsets.
entry_offset = 0;
- for (const auto& it : this->directories) {
+ for (const auto& it : directories) {
cur_dir = it.second;
cur_dir->entry_offset = entry_offset;
entry_offset +=
sizeof(RomFSDirectoryEntry) + ((cur_dir->path_len - cur_dir->cur_path_ofs + 3) & ~3);
}
- /* Assign deferred parent/sibling ownership. */
- for (auto it = this->directories.rbegin(); it->second != this->root; it++) {
+ // Assign deferred parent/sibling ownership.
+ for (auto it = directories.rbegin(); it->second != root; ++it) {
cur_dir = it->second;
cur_dir->sibling = cur_dir->parent->child;
cur_dir->parent->child = cur_dir;
}
- /* Populate file tables. */
- for (const auto& it : this->files) {
+ std::map<u64, VirtualFile> out;
+
+ // Populate file tables.
+ for (const auto& it : files) {
cur_file = it.second;
- RomFSFileEntry* cur_entry = romfs_get_fentry(file_table, cur_file->entry_offset);
+ RomFSFileEntry cur_entry{};
- cur_entry->parent = cur_file->parent->entry_offset;
- cur_entry->sibling =
- (cur_file->sibling == nullptr) ? ROMFS_ENTRY_EMPTY : cur_file->sibling->entry_offset;
- cur_entry->offset = cur_file->offset;
- cur_entry->size = cur_file->size;
+ cur_entry.parent = cur_file->parent->entry_offset;
+ cur_entry.sibling =
+ cur_file->sibling == nullptr ? ROMFS_ENTRY_EMPTY : cur_file->sibling->entry_offset;
+ cur_entry.offset = cur_file->offset;
+ cur_entry.size = cur_file->size;
const auto name_size = cur_file->path_len - cur_file->cur_path_ofs;
- const auto hash = romfs_calc_path_hash(cur_file->parent->entry_offset,
- reinterpret_cast<unsigned char*>(cur_file->path) +
- cur_file->cur_path_ofs,
- 0, name_size);
- cur_entry->hash = file_hash_table[hash % file_hash_table_entry_count];
+ const auto hash = romfs_calc_path_hash(cur_file->parent->entry_offset, cur_file->path,
+ cur_file->cur_path_ofs, name_size);
+ cur_entry.hash = file_hash_table[hash % file_hash_table_entry_count];
file_hash_table[hash % file_hash_table_entry_count] = cur_file->entry_offset;
- cur_entry->name_size = name_size;
- memset(cur_entry->name, 0, (cur_entry->name_size + 3) & ~3);
- memcpy(cur_entry->name, cur_file->path + cur_file->cur_path_ofs, name_size);
+ cur_entry.name_size = name_size;
out.emplace(cur_file->offset + ROMFS_FILEPARTITION_OFS, cur_file->source);
+ std::memcpy(file_table.data() + cur_file->entry_offset, &cur_entry, sizeof(RomFSFileEntry));
+ std::memset(file_table.data() + cur_file->entry_offset + sizeof(RomFSFileEntry), 0,
+ (cur_entry.name_size + 3) & ~3);
+ std::memcpy(file_table.data() + cur_file->entry_offset + sizeof(RomFSFileEntry),
+ cur_file->path.data() + cur_file->cur_path_ofs, name_size);
}
- /* Populate dir tables. */
- for (const auto& it : this->directories) {
+ // Populate dir tables.
+ for (const auto& it : directories) {
cur_dir = it.second;
- RomFSDirectoryEntry* cur_entry = romfs_get_direntry(dir_table, cur_dir->entry_offset);
- cur_entry->parent = cur_dir == this->root ? 0 : cur_dir->parent->entry_offset;
- cur_entry->sibling =
- (cur_dir->sibling == nullptr) ? ROMFS_ENTRY_EMPTY : cur_dir->sibling->entry_offset;
- cur_entry->child =
- (cur_dir->child == nullptr) ? ROMFS_ENTRY_EMPTY : cur_dir->child->entry_offset;
- cur_entry->file =
- (cur_dir->file == nullptr) ? ROMFS_ENTRY_EMPTY : cur_dir->file->entry_offset;
-
- u32 name_size = cur_dir->path_len - cur_dir->cur_path_ofs;
- u32 hash = romfs_calc_path_hash(
- cur_dir == this->root ? 0 : cur_dir->parent->entry_offset,
- reinterpret_cast<unsigned char*>(cur_dir->path) + cur_dir->cur_path_ofs, 0, name_size);
- cur_entry->hash = dir_hash_table[hash % dir_hash_table_entry_count];
+ RomFSDirectoryEntry cur_entry{};
+
+ cur_entry.parent = cur_dir == root ? 0 : cur_dir->parent->entry_offset;
+ cur_entry.sibling =
+ cur_dir->sibling == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->sibling->entry_offset;
+ cur_entry.child =
+ cur_dir->child == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->child->entry_offset;
+ cur_entry.file = cur_dir->file == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->file->entry_offset;
+
+ const auto name_size = cur_dir->path_len - cur_dir->cur_path_ofs;
+ const auto hash = romfs_calc_path_hash(cur_dir == root ? 0 : cur_dir->parent->entry_offset,
+ cur_dir->path, cur_dir->cur_path_ofs, name_size);
+ cur_entry.hash = dir_hash_table[hash % dir_hash_table_entry_count];
dir_hash_table[hash % dir_hash_table_entry_count] = cur_dir->entry_offset;
- cur_entry->name_size = name_size;
- memset(cur_entry->name, 0, (cur_entry->name_size + 3) & ~3);
- memcpy(cur_entry->name, cur_dir->path + cur_dir->cur_path_ofs, name_size);
- }
+ cur_entry.name_size = name_size;
- /* Delete directories. */
- for (const auto& it : this->directories) {
- cur_dir = it.second;
- delete cur_dir->path;
- delete cur_dir;
+ std::memcpy(dir_table.data() + cur_dir->entry_offset, &cur_entry,
+ sizeof(RomFSDirectoryEntry));
+ std::memcpy(dir_table.data() + cur_dir->entry_offset, &cur_entry,
+ sizeof(RomFSDirectoryEntry));
+ std::memset(dir_table.data() + cur_dir->entry_offset + sizeof(RomFSDirectoryEntry), 0,
+ (cur_entry.name_size + 3) & ~3);
+ std::memcpy(dir_table.data() + cur_dir->entry_offset + sizeof(RomFSDirectoryEntry),
+ cur_dir->path.data() + cur_dir->cur_path_ofs, name_size);
}
- this->root = nullptr;
- this->directories.clear();
- /* Delete files. */
- for (const auto& it : this->files) {
- cur_file = it.second;
- delete cur_file->path;
- delete cur_file;
- }
- this->files.clear();
-
- /* Set header fields. */
- header->header_size = sizeof(*header);
- header->file_hash_table_size = this->file_hash_table_size;
- header->file_table_size = this->file_table_size;
- header->dir_hash_table_size = this->dir_hash_table_size;
- header->dir_table_size = this->dir_table_size;
- header->file_partition_ofs = ROMFS_FILEPARTITION_OFS;
- header->dir_hash_table_ofs =
- (header->file_partition_ofs + this->file_partition_size + 3ULL) & ~3ULL;
- header->dir_table_ofs = header->dir_hash_table_ofs + header->dir_hash_table_size;
- header->file_hash_table_ofs = header->dir_table_ofs + header->dir_table_size;
- header->file_table_ofs = header->file_hash_table_ofs + header->file_hash_table_size;
+ // Set header fields.
+ header.header_size = sizeof(RomFSHeader);
+ header.file_hash_table_size = file_hash_table_size;
+ header.file_table_size = file_table_size;
+ header.dir_hash_table_size = dir_hash_table_size;
+ header.dir_table_size = dir_table_size;
+ header.file_partition_ofs = ROMFS_FILEPARTITION_OFS;
+ header.dir_hash_table_ofs = (header.file_partition_ofs + file_partition_size + 3ULL) & ~3ULL;
+ header.dir_table_ofs = header.dir_hash_table_ofs + header.dir_hash_table_size;
+ header.file_hash_table_ofs = header.dir_table_ofs + header.dir_table_size;
+ header.file_table_ofs = header.file_hash_table_ofs + header.file_hash_table_size;
std::vector<u8> header_data(sizeof(RomFSHeader));
- std::memcpy(header_data.data(), header, header_data.size());
+ std::memcpy(header_data.data(), &header, header_data.size());
out.emplace(0, std::make_shared<VectorVfsFile>(header_data));
- std::vector<u8> meta_out(this->file_hash_table_size + this->file_table_size +
- this->dir_hash_table_size + this->dir_table_size);
- std::memcpy(meta_out.data(), metadata, meta_out.size());
- out.emplace(header->dir_hash_table_ofs, std::make_shared<VectorVfsFile>(meta_out));
+ std::vector<u8> metadata(file_hash_table_size + file_table_size + dir_hash_table_size +
+ dir_table_size);
+ auto index = 0;
+ std::memcpy(metadata.data(), dir_hash_table.data(), dir_hash_table.size() * sizeof(u32));
+ index += dir_hash_table.size() * sizeof(u32);
+ std::memcpy(metadata.data() + index, dir_table.data(), dir_table.size());
+ index += dir_table.size();
+ std::memcpy(metadata.data() + index, file_hash_table.data(),
+ file_hash_table.size() * sizeof(u32));
+ index += file_hash_table.size() * sizeof(u32);
+ std::memcpy(metadata.data() + index, file_table.data(), file_table.size());
+ out.emplace(header.dir_hash_table_ofs, std::make_shared<VectorVfsFile>(metadata));
return out;
}