2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-07-27 18:14:03 -04:00
|
|
|
|
2019-11-27 05:28:19 -05:00
|
|
|
#include <memory>
|
|
|
|
|
2023-12-01 23:39:48 -05:00
|
|
|
#include "common/assert.h"
|
2018-07-27 18:14:03 -04:00
|
|
|
#include "common/common_types.h"
|
2020-04-06 20:03:32 -04:00
|
|
|
#include "common/string_util.h"
|
2018-07-27 18:14:03 -04:00
|
|
|
#include "common/swap.h"
|
2018-09-23 21:50:16 -04:00
|
|
|
#include "core/file_sys/fsmitm_romfsbuild.h"
|
2018-07-27 18:14:03 -04:00
|
|
|
#include "core/file_sys/romfs.h"
|
2024-01-16 00:23:01 -05:00
|
|
|
#include "core/file_sys/vfs/vfs.h"
|
|
|
|
#include "core/file_sys/vfs/vfs_cached.h"
|
|
|
|
#include "core/file_sys/vfs/vfs_concat.h"
|
|
|
|
#include "core/file_sys/vfs/vfs_offset.h"
|
|
|
|
#include "core/file_sys/vfs/vfs_vector.h"
|
2018-07-27 18:14:03 -04:00
|
|
|
|
|
|
|
namespace FileSys {
|
2019-11-27 05:26:31 -05:00
|
|
|
namespace {
|
2018-07-27 18:14:03 -04:00
|
|
|
constexpr u32 ROMFS_ENTRY_EMPTY = 0xFFFFFFFF;
|
|
|
|
|
|
|
|
struct TableLocation {
|
|
|
|
u64_le offset;
|
|
|
|
u64_le size;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(TableLocation) == 0x10, "TableLocation has incorrect size.");
|
|
|
|
|
|
|
|
struct RomFSHeader {
|
|
|
|
u64_le header_size;
|
|
|
|
TableLocation directory_hash;
|
|
|
|
TableLocation directory_meta;
|
|
|
|
TableLocation file_hash;
|
|
|
|
TableLocation file_meta;
|
|
|
|
u64_le data_offset;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(RomFSHeader) == 0x50, "RomFSHeader has incorrect size.");
|
|
|
|
|
|
|
|
struct DirectoryEntry {
|
2023-10-31 20:11:14 -04:00
|
|
|
u32_le parent;
|
2018-07-27 18:14:03 -04:00
|
|
|
u32_le sibling;
|
|
|
|
u32_le child_dir;
|
|
|
|
u32_le child_file;
|
|
|
|
u32_le hash;
|
|
|
|
u32_le name_length;
|
|
|
|
};
|
2023-10-31 20:11:14 -04:00
|
|
|
static_assert(sizeof(DirectoryEntry) == 0x18, "DirectoryEntry has incorrect size.");
|
2018-07-27 18:14:03 -04:00
|
|
|
|
|
|
|
struct FileEntry {
|
|
|
|
u32_le parent;
|
|
|
|
u32_le sibling;
|
|
|
|
u64_le offset;
|
|
|
|
u64_le size;
|
|
|
|
u32_le hash;
|
|
|
|
u32_le name_length;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(FileEntry) == 0x20, "FileEntry has incorrect size.");
|
|
|
|
|
2023-11-27 20:30:00 -05:00
|
|
|
struct RomFSTraversalContext {
|
|
|
|
RomFSHeader header;
|
|
|
|
VirtualFile file;
|
|
|
|
std::vector<u8> directory_meta;
|
|
|
|
std::vector<u8> file_meta;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename EntryType, auto Member>
|
|
|
|
std::pair<EntryType, std::string> GetEntry(const RomFSTraversalContext& ctx, size_t offset) {
|
|
|
|
const size_t entry_end = offset + sizeof(EntryType);
|
|
|
|
const std::vector<u8>& vec = ctx.*Member;
|
|
|
|
const size_t size = vec.size();
|
|
|
|
const u8* data = vec.data();
|
|
|
|
EntryType entry{};
|
|
|
|
|
|
|
|
if (entry_end > size) {
|
2018-07-27 18:14:03 -04:00
|
|
|
return {};
|
2023-11-27 20:30:00 -05:00
|
|
|
}
|
|
|
|
std::memcpy(&entry, data + offset, sizeof(EntryType));
|
|
|
|
|
|
|
|
const size_t name_length = std::min(entry_end + entry.name_length, size) - entry_end;
|
|
|
|
std::string name(reinterpret_cast<const char*>(data + entry_end), name_length);
|
|
|
|
|
|
|
|
return {entry, std::move(name)};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<DirectoryEntry, std::string> GetDirectoryEntry(const RomFSTraversalContext& ctx,
|
|
|
|
size_t directory_offset) {
|
|
|
|
return GetEntry<DirectoryEntry, &RomFSTraversalContext::directory_meta>(ctx, directory_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<FileEntry, std::string> GetFileEntry(const RomFSTraversalContext& ctx,
|
|
|
|
size_t file_offset) {
|
|
|
|
return GetEntry<FileEntry, &RomFSTraversalContext::file_meta>(ctx, file_offset);
|
2018-07-27 18:14:03 -04:00
|
|
|
}
|
|
|
|
|
2023-11-27 20:30:00 -05:00
|
|
|
void ProcessFile(const RomFSTraversalContext& ctx, u32 this_file_offset,
|
|
|
|
std::shared_ptr<VectorVfsDirectory>& parent) {
|
2023-10-31 20:11:14 -04:00
|
|
|
while (this_file_offset != ROMFS_ENTRY_EMPTY) {
|
2023-11-27 20:30:00 -05:00
|
|
|
auto entry = GetFileEntry(ctx, this_file_offset);
|
2018-07-27 18:14:03 -04:00
|
|
|
|
2023-11-27 20:30:00 -05:00
|
|
|
parent->AddFile(std::make_shared<OffsetVfsFile>(ctx.file, entry.first.size,
|
|
|
|
entry.first.offset + ctx.header.data_offset,
|
|
|
|
std::move(entry.second)));
|
2018-07-27 18:14:03 -04:00
|
|
|
|
|
|
|
this_file_offset = entry.first.sibling;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 20:30:00 -05:00
|
|
|
void ProcessDirectory(const RomFSTraversalContext& ctx, u32 this_dir_offset,
|
2023-10-31 20:11:14 -04:00
|
|
|
std::shared_ptr<VectorVfsDirectory>& parent) {
|
|
|
|
while (this_dir_offset != ROMFS_ENTRY_EMPTY) {
|
2023-11-27 20:30:00 -05:00
|
|
|
auto entry = GetDirectoryEntry(ctx, this_dir_offset);
|
2018-07-27 18:14:03 -04:00
|
|
|
auto current = std::make_shared<VectorVfsDirectory>(
|
2018-08-11 22:44:50 -04:00
|
|
|
std::vector<VirtualFile>{}, std::vector<VirtualDir>{}, entry.second);
|
2018-07-27 18:14:03 -04:00
|
|
|
|
|
|
|
if (entry.first.child_file != ROMFS_ENTRY_EMPTY) {
|
2023-11-27 20:30:00 -05:00
|
|
|
ProcessFile(ctx, entry.first.child_file, current);
|
2018-07-27 18:14:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entry.first.child_dir != ROMFS_ENTRY_EMPTY) {
|
2023-11-27 20:30:00 -05:00
|
|
|
ProcessDirectory(ctx, entry.first.child_dir, current);
|
2018-07-27 18:14:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
parent->AddDirectory(current);
|
|
|
|
this_dir_offset = entry.first.sibling;
|
|
|
|
}
|
|
|
|
}
|
2019-11-27 05:26:31 -05:00
|
|
|
} // Anonymous namespace
|
2018-07-27 18:14:03 -04:00
|
|
|
|
2023-10-31 20:11:14 -04:00
|
|
|
VirtualDir ExtractRomFS(VirtualFile file) {
|
2023-12-01 23:39:48 -05:00
|
|
|
auto root_container = std::make_shared<VectorVfsDirectory>();
|
|
|
|
if (!file) {
|
|
|
|
return root_container;
|
|
|
|
}
|
|
|
|
|
2023-11-27 20:30:00 -05:00
|
|
|
RomFSTraversalContext ctx{};
|
|
|
|
|
|
|
|
if (file->ReadObject(&ctx.header) != sizeof(RomFSHeader)) {
|
|
|
|
return nullptr;
|
2023-12-01 23:39:48 -05:00
|
|
|
}
|
2018-07-27 18:14:03 -04:00
|
|
|
|
2023-11-27 20:30:00 -05:00
|
|
|
if (ctx.header.header_size != sizeof(RomFSHeader)) {
|
|
|
|
return nullptr;
|
2023-12-01 23:39:48 -05:00
|
|
|
}
|
2018-07-27 18:14:03 -04:00
|
|
|
|
2023-11-27 20:30:00 -05:00
|
|
|
ctx.file = file;
|
|
|
|
ctx.directory_meta =
|
|
|
|
file->ReadBytes(ctx.header.directory_meta.size, ctx.header.directory_meta.offset);
|
|
|
|
ctx.file_meta = file->ReadBytes(ctx.header.file_meta.size, ctx.header.file_meta.offset);
|
2018-07-27 18:14:03 -04:00
|
|
|
|
2023-11-27 20:30:00 -05:00
|
|
|
ProcessDirectory(ctx, 0, root_container);
|
2018-12-24 16:18:28 -05:00
|
|
|
|
2023-10-31 20:11:14 -04:00
|
|
|
if (auto root = root_container->GetSubdirectory(""); root) {
|
2023-11-27 20:30:00 -05:00
|
|
|
return root;
|
2018-09-19 22:04:51 -04:00
|
|
|
}
|
2018-07-27 18:14:03 -04:00
|
|
|
|
2023-12-01 23:39:48 -05:00
|
|
|
ASSERT(false);
|
2023-10-31 20:11:14 -04:00
|
|
|
return nullptr;
|
2018-07-27 18:14:03 -04:00
|
|
|
}
|
2018-09-19 22:04:51 -04:00
|
|
|
|
2018-10-02 08:56:56 -04:00
|
|
|
VirtualFile CreateRomFS(VirtualDir dir, VirtualDir ext) {
|
2018-09-19 22:04:51 -04:00
|
|
|
if (dir == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
2018-10-02 08:56:56 -04:00
|
|
|
RomFSBuildContext ctx{dir, ext};
|
2023-10-13 13:34:41 -04:00
|
|
|
return ConcatenatedVfsFile::MakeConcatenatedFile(0, dir->GetName(), ctx.Build());
|
2018-09-19 22:04:51 -04:00
|
|
|
}
|
|
|
|
|
2018-07-27 18:14:03 -04:00
|
|
|
} // namespace FileSys
|