summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar shinyquagsire232018-04-16 04:29:15 -0600
committerGravatar shinyquagsire232018-04-16 04:36:25 -0600
commitc03795300a2d9fa4539fb840dca5b2579984bc7f (patch)
tree94f9d66d4d98edd9e770e030d291a144972abcf8 /src
parentMerge pull request #338 from bunnei/unrequire-shared-font (diff)
downloadyuzu-c03795300a2d9fa4539fb840dca5b2579984bc7f.tar.gz
yuzu-c03795300a2d9fa4539fb840dca5b2579984bc7f.tar.xz
yuzu-c03795300a2d9fa4539fb840dca5b2579984bc7f.zip
file_sys: Add HFS/PFS helper component
Diffstat (limited to 'src')
-rw-r--r--src/core/CMakeLists.txt2
-rw-r--r--src/core/file_sys/partition_filesystem.cpp124
-rw-r--r--src/core/file_sys/partition_filesystem.h87
3 files changed, 213 insertions, 0 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 9877b83fe..c1a645460 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -12,6 +12,8 @@ add_library(core STATIC
12 file_sys/errors.h 12 file_sys/errors.h
13 file_sys/filesystem.cpp 13 file_sys/filesystem.cpp
14 file_sys/filesystem.h 14 file_sys/filesystem.h
15 file_sys/partition_filesystem.cpp
16 file_sys/partition_filesystem.h
15 file_sys/path_parser.cpp 17 file_sys/path_parser.cpp
16 file_sys/path_parser.h 18 file_sys/path_parser.h
17 file_sys/program_metadata.cpp 19 file_sys/program_metadata.cpp
diff --git a/src/core/file_sys/partition_filesystem.cpp b/src/core/file_sys/partition_filesystem.cpp
new file mode 100644
index 000000000..f344e7970
--- /dev/null
+++ b/src/core/file_sys/partition_filesystem.cpp
@@ -0,0 +1,124 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cinttypes>
6#include "common/file_util.h"
7#include "common/logging/log.h"
8#include "core/file_sys/partition_filesystem.h"
9#include "core/loader/loader.h"
10
11namespace FileSys {
12
13Loader::ResultStatus PartitionFilesystem::Load(const std::string& file_path, size_t offset) {
14 FileUtil::IOFile file(file_path, "rb");
15 if (!file.IsOpen())
16 return Loader::ResultStatus::Error;
17
18 // At least be as large as the header
19 if (file.GetSize() < sizeof(Header))
20 return Loader::ResultStatus::Error;
21
22 // For cartridges, HFSs can get very large, so we need to calculate the size up to
23 // the actual content itself instead of just blindly reading in the entire file.
24 Header pfs_header;
25 if (!file.ReadBytes(&pfs_header, sizeof(Header)))
26 return Loader::ResultStatus::Error;
27
28 bool is_hfs = (memcmp(pfs_header.magic.data(), "HFS", 3) == 0);
29 size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry);
30 size_t metadata_size =
31 sizeof(Header) + (pfs_header.num_entries * entry_size) + pfs_header.strtab_size;
32
33 // Actually read in now...
34 file.Seek(offset, SEEK_SET);
35 std::vector<u8> file_data(metadata_size);
36
37 if (!file.ReadBytes(file_data.data(), metadata_size))
38 return Loader::ResultStatus::Error;
39
40 Loader::ResultStatus result = Load(file_data);
41 if (result != Loader::ResultStatus::Success)
42 LOG_ERROR(Service_FS, "Failed to load PFS from file %s!", file_path.c_str());
43
44 return result;
45}
46
47Loader::ResultStatus PartitionFilesystem::Load(const std::vector<u8> file_data, size_t offset) {
48 size_t total_size = static_cast<size_t>(file_data.size() - offset);
49 if (total_size < sizeof(Header))
50 return Loader::ResultStatus::Error;
51
52 memcpy(&pfs_header, &file_data[offset], sizeof(Header));
53 is_hfs = (memcmp(pfs_header.magic.data(), "HFS", 3) == 0);
54
55 size_t entries_offset = offset + sizeof(Header);
56 size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry);
57 size_t strtab_offset = entries_offset + (pfs_header.num_entries * entry_size);
58 for (u16 i = 0; i < pfs_header.num_entries; i++) {
59 FileEntry entry;
60
61 memcpy(&entry.fs_entry, &file_data[entries_offset + (i * entry_size)], sizeof(FSEntry));
62 entry.name = std::string(reinterpret_cast<const char*>(
63 &file_data[strtab_offset + entry.fs_entry.strtab_offset]));
64 pfs_entries.push_back(entry);
65 }
66
67 content_offset = strtab_offset + pfs_header.strtab_size;
68
69 return Loader::ResultStatus::Success;
70}
71
72u32 PartitionFilesystem::GetNumEntries(void) const {
73 return pfs_header.num_entries;
74}
75
76u64 PartitionFilesystem::GetEntryOffset(int index) const {
77 if (index > GetNumEntries())
78 return 0;
79
80 return content_offset + pfs_entries[index].fs_entry.offset;
81}
82
83u64 PartitionFilesystem::GetEntrySize(int index) const {
84 if (index > GetNumEntries())
85 return 0;
86
87 return pfs_entries[index].fs_entry.size;
88}
89
90std::string PartitionFilesystem::GetEntryName(int index) const {
91 if (index > GetNumEntries())
92 return "";
93
94 return pfs_entries[index].name;
95}
96
97u64 PartitionFilesystem::GetFileOffset(const std::string& name) const {
98 for (u32 i = 0; i < pfs_header.num_entries; i++) {
99 if (pfs_entries[i].name == name)
100 return content_offset + pfs_entries[i].fs_entry.offset;
101 }
102
103 return 0;
104}
105
106u64 PartitionFilesystem::GetFileSize(const std::string& name) const {
107 for (u32 i = 0; i < pfs_header.num_entries; i++) {
108 if (pfs_entries[i].name == name)
109 return pfs_entries[i].fs_entry.size;
110 }
111
112 return 0;
113}
114
115void PartitionFilesystem::Print() const {
116 LOG_DEBUG(Service_FS, "Magic: %.4s", pfs_header.magic.data());
117 LOG_DEBUG(Service_FS, "Files: %u", pfs_header.num_entries);
118 for (u32 i = 0; i < pfs_header.num_entries; i++) {
119 LOG_DEBUG(Service_FS, " > File %u: %s (0x%" PRIX64 " bytes, at 0x%" PRIX64 ")",
120 i, pfs_entries[i].name.c_str(), pfs_entries[i].fs_entry.size,
121 GetFileOffset(pfs_entries[i].name));
122 }
123}
124} // namespace FileSys
diff --git a/src/core/file_sys/partition_filesystem.h b/src/core/file_sys/partition_filesystem.h
new file mode 100644
index 000000000..4cc534f50
--- /dev/null
+++ b/src/core/file_sys/partition_filesystem.h
@@ -0,0 +1,87 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include <string>
9#include <vector>
10#include "common/common_funcs.h"
11#include "common/common_types.h"
12#include "common/swap.h"
13
14namespace Loader {
15enum class ResultStatus;
16}
17
18namespace FileSys {
19
20/**
21 * Helper which implements an interface to parse PFS/HFS filesystems.
22 * Data can either be loaded from a file path or data with an offset into it.
23 */
24class PartitionFilesystem {
25public:
26 Loader::ResultStatus Load(const std::string& file_path, size_t offset = 0);
27 Loader::ResultStatus Load(const std::vector<u8> file_data, size_t offset = 0);
28
29 u32 GetNumEntries(void) const;
30 u64 GetEntryOffset(int index) const;
31 u64 GetEntrySize(int index) const;
32 std::string GetEntryName(int index) const;
33 u64 GetFileOffset(const std::string& name) const;
34 u64 GetFileSize(const std::string& name) const;
35
36 void Print() const;
37
38private:
39 struct Header {
40 std::array<char, 4> magic;
41 u32_le num_entries;
42 u32_le strtab_size;
43 INSERT_PADDING_BYTES(0x4);
44 };
45
46 static_assert(sizeof(Header) == 0x10, "PFS/HFS header structure size is wrong");
47
48#pragma pack(push, 1)
49 struct FSEntry {
50 u64_le offset;
51 u64_le size;
52 u32_le strtab_offset;
53 };
54
55 static_assert(sizeof(FSEntry) == 0x14, "FS entry structure size is wrong");
56
57 struct PFSEntry {
58 FSEntry fs_entry;
59 INSERT_PADDING_BYTES(0x4);
60 };
61
62 static_assert(sizeof(PFSEntry) == 0x18, "PFS entry structure size is wrong");
63
64 struct HFSEntry {
65 FSEntry fs_entry;
66 u32_le hash_region_size;
67 INSERT_PADDING_BYTES(0x8);
68 std::array<char, 0x20> hash;
69 };
70
71 static_assert(sizeof(HFSEntry) == 0x40, "HFS entry structure size is wrong");
72
73#pragma pack(pop)
74
75 struct FileEntry {
76 FSEntry fs_entry;
77 std::string name;
78 };
79
80 Header pfs_header;
81 bool is_hfs;
82 size_t content_offset;
83
84 std::vector<FileEntry> pfs_entries;
85};
86
87} // namespace FileSys