2018-09-20 04:04:38 +02:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adapted by DarkLordZach for use/interaction with yuzu
|
|
|
|
*
|
|
|
|
* Modifications Copyright 2018 yuzu emulator team
|
|
|
|
* Licensed under GPLv2 or any later version
|
|
|
|
* Refer to the license.txt file included.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cstring>
|
2019-04-05 21:11:12 +02:00
|
|
|
#include <string_view>
|
2018-09-26 23:00:41 +02:00
|
|
|
#include "common/alignment.h"
|
2018-09-20 04:04:38 +02:00
|
|
|
#include "common/assert.h"
|
2018-09-24 03:50:16 +02:00
|
|
|
#include "core/file_sys/fsmitm_romfsbuild.h"
|
2018-10-01 15:12:14 +02:00
|
|
|
#include "core/file_sys/ips_layer.h"
|
2018-09-24 03:50:16 +02:00
|
|
|
#include "core/file_sys/vfs.h"
|
|
|
|
#include "core/file_sys/vfs_vector.h"
|
2018-09-20 04:04:38 +02:00
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
|
|
|
constexpr u64 FS_MAX_PATH = 0x301;
|
|
|
|
|
|
|
|
constexpr u32 ROMFS_ENTRY_EMPTY = 0xFFFFFFFF;
|
|
|
|
constexpr u32 ROMFS_FILEPARTITION_OFS = 0x200;
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
// Types for building a RomFS.
|
2018-09-20 04:04:38 +02:00
|
|
|
struct RomFSHeader {
|
|
|
|
u64 header_size;
|
|
|
|
u64 dir_hash_table_ofs;
|
|
|
|
u64 dir_hash_table_size;
|
|
|
|
u64 dir_table_ofs;
|
|
|
|
u64 dir_table_size;
|
|
|
|
u64 file_hash_table_ofs;
|
|
|
|
u64 file_hash_table_size;
|
|
|
|
u64 file_table_ofs;
|
|
|
|
u64 file_table_size;
|
|
|
|
u64 file_partition_ofs;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(RomFSHeader) == 0x50, "RomFSHeader has incorrect size.");
|
|
|
|
|
|
|
|
struct RomFSDirectoryEntry {
|
|
|
|
u32 parent;
|
|
|
|
u32 sibling;
|
|
|
|
u32 child;
|
|
|
|
u32 file;
|
|
|
|
u32 hash;
|
|
|
|
u32 name_size;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(RomFSDirectoryEntry) == 0x18, "RomFSDirectoryEntry has incorrect size.");
|
|
|
|
|
|
|
|
struct RomFSFileEntry {
|
|
|
|
u32 parent;
|
|
|
|
u32 sibling;
|
|
|
|
u64 offset;
|
|
|
|
u64 size;
|
|
|
|
u32 hash;
|
|
|
|
u32 name_size;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(RomFSFileEntry) == 0x20, "RomFSFileEntry has incorrect size.");
|
|
|
|
|
|
|
|
struct RomFSBuildFileContext;
|
|
|
|
|
|
|
|
struct RomFSBuildDirectoryContext {
|
2018-09-26 00:17:42 +02:00
|
|
|
std::string path;
|
2018-09-24 03:50:16 +02:00
|
|
|
u32 cur_path_ofs = 0;
|
|
|
|
u32 path_len = 0;
|
2018-09-20 04:04:38 +02:00
|
|
|
u32 entry_offset = 0;
|
2018-09-24 03:50:16 +02:00
|
|
|
std::shared_ptr<RomFSBuildDirectoryContext> parent;
|
|
|
|
std::shared_ptr<RomFSBuildDirectoryContext> child;
|
|
|
|
std::shared_ptr<RomFSBuildDirectoryContext> sibling;
|
|
|
|
std::shared_ptr<RomFSBuildFileContext> file;
|
2018-09-20 04:04:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct RomFSBuildFileContext {
|
2018-09-26 00:17:42 +02:00
|
|
|
std::string path;
|
2018-09-24 03:50:16 +02:00
|
|
|
u32 cur_path_ofs = 0;
|
|
|
|
u32 path_len = 0;
|
2018-09-20 04:04:38 +02:00
|
|
|
u32 entry_offset = 0;
|
|
|
|
u64 offset = 0;
|
|
|
|
u64 size = 0;
|
2018-09-24 03:50:16 +02:00
|
|
|
std::shared_ptr<RomFSBuildDirectoryContext> parent;
|
|
|
|
std::shared_ptr<RomFSBuildFileContext> sibling;
|
2018-09-26 00:17:42 +02:00
|
|
|
VirtualFile source;
|
2018-09-20 04:04:38 +02:00
|
|
|
};
|
|
|
|
|
2019-04-05 21:11:12 +02:00
|
|
|
static u32 romfs_calc_path_hash(u32 parent, std::string_view path, u32 start,
|
|
|
|
std::size_t path_len) {
|
2018-09-24 03:50:16 +02:00
|
|
|
u32 hash = parent ^ 123456789;
|
|
|
|
for (u32 i = 0; i < path_len; i++) {
|
|
|
|
hash = (hash >> 5) | (hash << 27);
|
2020-10-21 04:07:39 +02:00
|
|
|
hash ^= path[start + i];
|
2018-09-24 03:50:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2018-09-26 00:25:10 +02:00
|
|
|
static u64 romfs_get_hash_table_count(u64 num_entries) {
|
2018-09-24 03:50:16 +02:00
|
|
|
if (num_entries < 3) {
|
|
|
|
return 3;
|
2018-09-26 00:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (num_entries < 19) {
|
2018-09-24 03:50:16 +02:00
|
|
|
return num_entries | 1;
|
|
|
|
}
|
2018-09-26 00:25:10 +02:00
|
|
|
|
|
|
|
u64 count = num_entries;
|
2018-09-24 03:50:16 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-02 14:56:56 +02:00
|
|
|
void RomFSBuildContext::VisitDirectory(VirtualDir root_romfs, VirtualDir ext,
|
2018-09-24 03:50:16 +02:00
|
|
|
std::shared_ptr<RomFSBuildDirectoryContext> parent) {
|
|
|
|
std::vector<std::shared_ptr<RomFSBuildDirectoryContext>> child_dirs;
|
2018-09-20 04:04:38 +02:00
|
|
|
|
|
|
|
VirtualDir dir;
|
|
|
|
|
|
|
|
if (parent->path_len == 0)
|
|
|
|
dir = root_romfs;
|
|
|
|
else
|
|
|
|
dir = root_romfs->GetDirectoryRelative(parent->path);
|
|
|
|
|
|
|
|
const auto entries = dir->GetEntries();
|
|
|
|
|
|
|
|
for (const auto& kv : entries) {
|
|
|
|
if (kv.second == VfsEntryType::Directory) {
|
2018-09-24 03:50:16 +02:00
|
|
|
const auto child = std::make_shared<RomFSBuildDirectoryContext>();
|
|
|
|
// Set child's path.
|
2018-09-20 04:04:38 +02:00
|
|
|
child->cur_path_ofs = parent->path_len + 1;
|
2018-09-26 00:25:10 +02:00
|
|
|
child->path_len = child->cur_path_ofs + static_cast<u32>(kv.first.size());
|
2018-09-24 03:50:16 +02:00
|
|
|
child->path = parent->path + "/" + kv.first;
|
|
|
|
|
2018-10-02 14:56:56 +02:00
|
|
|
if (ext != nullptr && ext->GetFileRelative(child->path + ".stub") != nullptr)
|
|
|
|
continue;
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
// Sanity check on path_len
|
2018-09-20 04:04:38 +02:00
|
|
|
ASSERT(child->path_len < FS_MAX_PATH);
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
if (AddDirectory(parent, child)) {
|
2018-09-20 04:04:38 +02:00
|
|
|
child_dirs.push_back(child);
|
|
|
|
}
|
|
|
|
} else {
|
2018-09-24 03:50:16 +02:00
|
|
|
const auto child = std::make_shared<RomFSBuildFileContext>();
|
|
|
|
// Set child's path.
|
2018-09-20 04:04:38 +02:00
|
|
|
child->cur_path_ofs = parent->path_len + 1;
|
2018-09-26 00:25:10 +02:00
|
|
|
child->path_len = child->cur_path_ofs + static_cast<u32>(kv.first.size());
|
2018-09-24 03:50:16 +02:00
|
|
|
child->path = parent->path + "/" + kv.first;
|
|
|
|
|
2018-10-02 14:56:56 +02:00
|
|
|
if (ext != nullptr && ext->GetFileRelative(child->path + ".stub") != nullptr)
|
|
|
|
continue;
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
// Sanity check on path_len
|
2018-09-20 04:04:38 +02:00
|
|
|
ASSERT(child->path_len < FS_MAX_PATH);
|
|
|
|
|
|
|
|
child->source = root_romfs->GetFileRelative(child->path);
|
|
|
|
|
2018-10-02 14:56:56 +02:00
|
|
|
if (ext != nullptr) {
|
|
|
|
const auto ips = ext->GetFileRelative(child->path + ".ips");
|
|
|
|
|
|
|
|
if (ips != nullptr) {
|
|
|
|
auto patched = PatchIPS(child->source, ips);
|
|
|
|
if (patched != nullptr)
|
|
|
|
child->source = std::move(patched);
|
|
|
|
}
|
2018-10-01 15:12:14 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 04:04:38 +02:00
|
|
|
child->size = child->source->GetSize();
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
AddFile(parent, child);
|
2018-09-20 04:04:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& child : child_dirs) {
|
2018-10-02 14:56:56 +02:00
|
|
|
this->VisitDirectory(root_romfs, ext, child);
|
2018-09-20 04:04:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
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())
|
2018-09-20 04:04:38 +02:00
|
|
|
return false;
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
// Add a new directory.
|
|
|
|
num_dirs++;
|
|
|
|
dir_table_size +=
|
2018-09-26 23:00:41 +02:00
|
|
|
sizeof(RomFSDirectoryEntry) + Common::AlignUp(dir_ctx->path_len - dir_ctx->cur_path_ofs, 4);
|
2018-09-20 04:04:38 +02:00
|
|
|
dir_ctx->parent = parent_dir_ctx;
|
2018-09-24 03:50:16 +02:00
|
|
|
directories.emplace(dir_ctx->path, dir_ctx);
|
2018-09-20 04:04:38 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
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()) {
|
2018-09-20 04:04:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
// Add a new file.
|
|
|
|
num_files++;
|
|
|
|
file_table_size +=
|
2018-09-26 23:00:41 +02:00
|
|
|
sizeof(RomFSFileEntry) + Common::AlignUp(file_ctx->path_len - file_ctx->cur_path_ofs, 4);
|
2018-09-20 04:04:38 +02:00
|
|
|
file_ctx->parent = parent_dir_ctx;
|
2018-09-24 03:50:16 +02:00
|
|
|
files.emplace(file_ctx->path, file_ctx);
|
2018-09-20 04:04:38 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-02 14:56:56 +02:00
|
|
|
RomFSBuildContext::RomFSBuildContext(VirtualDir base_, VirtualDir ext_)
|
|
|
|
: base(std::move(base_)), ext(std::move(ext_)) {
|
2018-09-24 03:50:16 +02:00
|
|
|
root = std::make_shared<RomFSBuildDirectoryContext>();
|
|
|
|
root->path = "\0";
|
|
|
|
directories.emplace(root->path, root);
|
|
|
|
num_dirs = 1;
|
|
|
|
dir_table_size = 0x18;
|
|
|
|
|
2018-10-02 14:56:56 +02:00
|
|
|
VisitDirectory(base, ext, root);
|
2018-09-20 04:04:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
RomFSBuildContext::~RomFSBuildContext() = default;
|
|
|
|
|
2020-07-12 10:31:01 +02:00
|
|
|
std::multimap<u64, VirtualFile> RomFSBuildContext::Build() {
|
2018-09-26 00:25:10 +02:00
|
|
|
const u64 dir_hash_table_entry_count = romfs_get_hash_table_count(num_dirs);
|
|
|
|
const u64 file_hash_table_entry_count = romfs_get_hash_table_count(num_files);
|
2018-09-24 03:50:16 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
std::shared_ptr<RomFSBuildFileContext> cur_file;
|
2018-09-20 04:04:38 +02:00
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
// Determine file offsets.
|
2018-09-20 04:04:38 +02:00
|
|
|
u32 entry_offset = 0;
|
2018-09-24 03:50:16 +02:00
|
|
|
std::shared_ptr<RomFSBuildFileContext> prev_file = nullptr;
|
|
|
|
for (const auto& it : files) {
|
2018-09-20 04:04:38 +02:00
|
|
|
cur_file = it.second;
|
2018-09-26 23:00:41 +02:00
|
|
|
file_partition_size = Common::AlignUp(file_partition_size, 16);
|
2018-09-24 03:50:16 +02:00
|
|
|
cur_file->offset = file_partition_size;
|
|
|
|
file_partition_size += cur_file->size;
|
2018-09-20 04:04:38 +02:00
|
|
|
cur_file->entry_offset = entry_offset;
|
2020-10-13 14:10:50 +02:00
|
|
|
entry_offset +=
|
|
|
|
static_cast<u32>(sizeof(RomFSFileEntry) +
|
|
|
|
Common::AlignUp(cur_file->path_len - cur_file->cur_path_ofs, 4));
|
2018-09-20 04:04:38 +02:00
|
|
|
prev_file = cur_file;
|
|
|
|
}
|
2018-09-24 03:50:16 +02:00
|
|
|
// Assign deferred parent/sibling ownership.
|
|
|
|
for (auto it = files.rbegin(); it != files.rend(); ++it) {
|
2018-09-20 04:04:38 +02:00
|
|
|
cur_file = it->second;
|
|
|
|
cur_file->sibling = cur_file->parent->file;
|
|
|
|
cur_file->parent->file = cur_file;
|
|
|
|
}
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
std::shared_ptr<RomFSBuildDirectoryContext> cur_dir;
|
|
|
|
|
|
|
|
// Determine directory offsets.
|
2018-09-20 04:04:38 +02:00
|
|
|
entry_offset = 0;
|
2018-09-24 03:50:16 +02:00
|
|
|
for (const auto& it : directories) {
|
2018-09-20 04:04:38 +02:00
|
|
|
cur_dir = it.second;
|
|
|
|
cur_dir->entry_offset = entry_offset;
|
2020-10-13 14:10:50 +02:00
|
|
|
entry_offset +=
|
|
|
|
static_cast<u32>(sizeof(RomFSDirectoryEntry) +
|
|
|
|
Common::AlignUp(cur_dir->path_len - cur_dir->cur_path_ofs, 4));
|
2018-09-20 04:04:38 +02:00
|
|
|
}
|
2018-09-24 03:50:16 +02:00
|
|
|
// Assign deferred parent/sibling ownership.
|
|
|
|
for (auto it = directories.rbegin(); it->second != root; ++it) {
|
2018-09-20 04:04:38 +02:00
|
|
|
cur_dir = it->second;
|
|
|
|
cur_dir->sibling = cur_dir->parent->child;
|
|
|
|
cur_dir->parent->child = cur_dir;
|
|
|
|
}
|
|
|
|
|
2020-07-12 10:31:01 +02:00
|
|
|
std::multimap<u64, VirtualFile> out;
|
2018-09-24 03:50:16 +02:00
|
|
|
|
|
|
|
// Populate file tables.
|
|
|
|
for (const auto& it : files) {
|
2018-09-20 04:04:38 +02:00
|
|
|
cur_file = it.second;
|
2018-09-24 03:50:16 +02:00
|
|
|
RomFSFileEntry cur_entry{};
|
2018-09-20 04:04:38 +02:00
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
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;
|
2018-09-20 04:04:38 +02:00
|
|
|
|
|
|
|
const auto name_size = cur_file->path_len - cur_file->cur_path_ofs;
|
2018-09-24 03:50:16 +02:00
|
|
|
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];
|
2018-09-20 04:04:38 +02:00
|
|
|
file_hash_table[hash % file_hash_table_entry_count] = cur_file->entry_offset;
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
cur_entry.name_size = name_size;
|
2018-09-20 04:04:38 +02:00
|
|
|
|
|
|
|
out.emplace(cur_file->offset + ROMFS_FILEPARTITION_OFS, cur_file->source);
|
2018-09-24 03:50:16 +02:00
|
|
|
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,
|
2018-09-26 23:00:41 +02:00
|
|
|
Common::AlignUp(cur_entry.name_size, 4));
|
2018-09-24 03:50:16 +02:00
|
|
|
std::memcpy(file_table.data() + cur_file->entry_offset + sizeof(RomFSFileEntry),
|
|
|
|
cur_file->path.data() + cur_file->cur_path_ofs, name_size);
|
2018-09-20 04:04:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
// Populate dir tables.
|
|
|
|
for (const auto& it : directories) {
|
2018-09-20 04:04:38 +02:00
|
|
|
cur_dir = it.second;
|
2018-09-24 03:50:16 +02:00
|
|
|
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];
|
2018-09-20 04:04:38 +02:00
|
|
|
dir_hash_table[hash % dir_hash_table_entry_count] = cur_dir->entry_offset;
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
cur_entry.name_size = name_size;
|
2018-09-20 04:04:38 +02:00
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
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,
|
2018-09-26 23:00:41 +02:00
|
|
|
Common::AlignUp(cur_entry.name_size, 4));
|
2018-09-24 03:50:16 +02:00
|
|
|
std::memcpy(dir_table.data() + cur_dir->entry_offset + sizeof(RomFSDirectoryEntry),
|
|
|
|
cur_dir->path.data() + cur_dir->cur_path_ofs, name_size);
|
2018-09-20 04:04:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
// 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;
|
2018-09-26 23:00:41 +02:00
|
|
|
header.dir_hash_table_ofs = Common::AlignUp(header.file_partition_ofs + file_partition_size, 4);
|
2018-09-24 03:50:16 +02:00
|
|
|
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;
|
2018-09-20 04:04:38 +02:00
|
|
|
|
|
|
|
std::vector<u8> header_data(sizeof(RomFSHeader));
|
2018-09-24 03:50:16 +02:00
|
|
|
std::memcpy(header_data.data(), &header, header_data.size());
|
2018-09-26 23:25:14 +02:00
|
|
|
out.emplace(0, std::make_shared<VectorVfsFile>(std::move(header_data)));
|
2018-09-20 04:04:38 +02:00
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
std::vector<u8> metadata(file_hash_table_size + file_table_size + dir_hash_table_size +
|
|
|
|
dir_table_size);
|
2018-09-26 00:10:47 +02:00
|
|
|
std::size_t index = 0;
|
2018-09-24 03:50:16 +02:00
|
|
|
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());
|
2018-09-26 23:25:14 +02:00
|
|
|
out.emplace(header.dir_hash_table_ofs, std::make_shared<VectorVfsFile>(std::move(metadata)));
|
2018-09-20 04:04:38 +02:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace FileSys
|