summaryrefslogtreecommitdiff
path: root/src/core/loader/nca.cpp
diff options
context:
space:
mode:
authorGravatar Zach Hilman2018-07-18 21:07:11 -0400
committerGravatar bunnei2018-07-18 18:07:11 -0700
commit29aff8d5ab46c8d0199aa4bfa7eeff5d4fa2d7ef (patch)
tree3202e2ce55ab6387a4ca366a509eccdd963434c3 /src/core/loader/nca.cpp
parentMerge pull request #683 from DarkLordZach/touch (diff)
downloadyuzu-29aff8d5ab46c8d0199aa4bfa7eeff5d4fa2d7ef.tar.gz
yuzu-29aff8d5ab46c8d0199aa4bfa7eeff5d4fa2d7ef.tar.xz
yuzu-29aff8d5ab46c8d0199aa4bfa7eeff5d4fa2d7ef.zip
Virtual Filesystem 2: Electric Boogaloo (#676)
* Virtual Filesystem * Fix delete bug and documentate * Review fixes + other stuff * Fix puyo regression
Diffstat (limited to 'src/core/loader/nca.cpp')
-rw-r--r--src/core/loader/nca.cpp248
1 files changed, 25 insertions, 223 deletions
diff --git a/src/core/loader/nca.cpp b/src/core/loader/nca.cpp
index b463f369c..e73b253b2 100644
--- a/src/core/loader/nca.cpp
+++ b/src/core/loader/nca.cpp
@@ -4,14 +4,13 @@
4 4
5#include <vector> 5#include <vector>
6 6
7#include "common/common_funcs.h"
8#include "common/file_util.h" 7#include "common/file_util.h"
9#include "common/logging/log.h" 8#include "common/logging/log.h"
10#include "common/string_util.h" 9#include "common/string_util.h"
11#include "common/swap.h" 10#include "common/swap.h"
12#include "core/core.h" 11#include "core/core.h"
12#include "core/file_sys/content_archive.h"
13#include "core/file_sys/program_metadata.h" 13#include "core/file_sys/program_metadata.h"
14#include "core/file_sys/romfs_factory.h"
15#include "core/gdbstub/gdbstub.h" 14#include "core/gdbstub/gdbstub.h"
16#include "core/hle/kernel/process.h" 15#include "core/hle/kernel/process.h"
17#include "core/hle/kernel/resource_limit.h" 16#include "core/hle/kernel/resource_limit.h"
@@ -22,208 +21,15 @@
22 21
23namespace Loader { 22namespace Loader {
24 23
25// Media offsets in headers are stored divided by 512. Mult. by this to get real offset. 24AppLoader_NCA::AppLoader_NCA(FileSys::VirtualFile file) : AppLoader(file) {}
26constexpr u64 MEDIA_OFFSET_MULTIPLIER = 0x200;
27
28constexpr u64 SECTION_HEADER_SIZE = 0x200;
29constexpr u64 SECTION_HEADER_OFFSET = 0x400;
30
31enum class NcaContentType : u8 { Program = 0, Meta = 1, Control = 2, Manual = 3, Data = 4 };
32
33enum class NcaSectionFilesystemType : u8 { PFS0 = 0x2, ROMFS = 0x3 };
34
35struct NcaSectionTableEntry {
36 u32_le media_offset;
37 u32_le media_end_offset;
38 INSERT_PADDING_BYTES(0x8);
39};
40static_assert(sizeof(NcaSectionTableEntry) == 0x10, "NcaSectionTableEntry has incorrect size.");
41
42struct NcaHeader {
43 std::array<u8, 0x100> rsa_signature_1;
44 std::array<u8, 0x100> rsa_signature_2;
45 u32_le magic;
46 u8 is_system;
47 NcaContentType content_type;
48 u8 crypto_type;
49 u8 key_index;
50 u64_le size;
51 u64_le title_id;
52 INSERT_PADDING_BYTES(0x4);
53 u32_le sdk_version;
54 u8 crypto_type_2;
55 INSERT_PADDING_BYTES(15);
56 std::array<u8, 0x10> rights_id;
57 std::array<NcaSectionTableEntry, 0x4> section_tables;
58 std::array<std::array<u8, 0x20>, 0x4> hash_tables;
59 std::array<std::array<u8, 0x10>, 0x4> key_area;
60 INSERT_PADDING_BYTES(0xC0);
61};
62static_assert(sizeof(NcaHeader) == 0x400, "NcaHeader has incorrect size.");
63
64struct NcaSectionHeaderBlock {
65 INSERT_PADDING_BYTES(3);
66 NcaSectionFilesystemType filesystem_type;
67 u8 crypto_type;
68 INSERT_PADDING_BYTES(3);
69};
70static_assert(sizeof(NcaSectionHeaderBlock) == 0x8, "NcaSectionHeaderBlock has incorrect size.");
71
72struct Pfs0Superblock {
73 NcaSectionHeaderBlock header_block;
74 std::array<u8, 0x20> hash;
75 u32_le size;
76 INSERT_PADDING_BYTES(4);
77 u64_le hash_table_offset;
78 u64_le hash_table_size;
79 u64_le pfs0_header_offset;
80 u64_le pfs0_size;
81 INSERT_PADDING_BYTES(432);
82};
83static_assert(sizeof(Pfs0Superblock) == 0x200, "Pfs0Superblock has incorrect size.");
84
85static bool IsValidNca(const NcaHeader& header) {
86 return header.magic == Common::MakeMagic('N', 'C', 'A', '2') ||
87 header.magic == Common::MakeMagic('N', 'C', 'A', '3');
88}
89
90// TODO(DarkLordZach): Add support for encrypted.
91class Nca final {
92 std::vector<FileSys::PartitionFilesystem> pfs;
93 std::vector<u64> pfs_offset;
94
95 u64 romfs_offset = 0;
96 u64 romfs_size = 0;
97
98 boost::optional<u8> exefs_id = boost::none;
99
100 FileUtil::IOFile file;
101 std::string path;
102
103 u64 GetExeFsFileOffset(const std::string& file_name) const;
104 u64 GetExeFsFileSize(const std::string& file_name) const;
105
106public:
107 ResultStatus Load(FileUtil::IOFile&& file, std::string path);
108
109 FileSys::PartitionFilesystem GetPfs(u8 id) const;
110
111 u64 GetRomFsOffset() const;
112 u64 GetRomFsSize() const;
113
114 std::vector<u8> GetExeFsFile(const std::string& file_name);
115};
116
117static bool IsPfsExeFs(const FileSys::PartitionFilesystem& pfs) {
118 // According to switchbrew, an exefs must only contain these two files:
119 return pfs.GetFileSize("main") > 0 && pfs.GetFileSize("main.npdm") > 0;
120}
121
122ResultStatus Nca::Load(FileUtil::IOFile&& in_file, std::string in_path) {
123 file = std::move(in_file);
124 path = in_path;
125 file.Seek(0, SEEK_SET);
126 std::array<u8, sizeof(NcaHeader)> header_array{};
127 if (sizeof(NcaHeader) != file.ReadBytes(header_array.data(), sizeof(NcaHeader)))
128 LOG_CRITICAL(Loader, "File reader errored out during header read.");
129
130 NcaHeader header{};
131 std::memcpy(&header, header_array.data(), sizeof(NcaHeader));
132 if (!IsValidNca(header))
133 return ResultStatus::ErrorInvalidFormat;
134
135 int number_sections =
136 std::count_if(std::begin(header.section_tables), std::end(header.section_tables),
137 [](NcaSectionTableEntry entry) { return entry.media_offset > 0; });
138
139 for (int i = 0; i < number_sections; ++i) {
140 // Seek to beginning of this section.
141 file.Seek(SECTION_HEADER_OFFSET + i * SECTION_HEADER_SIZE, SEEK_SET);
142 std::array<u8, sizeof(NcaSectionHeaderBlock)> array{};
143 if (sizeof(NcaSectionHeaderBlock) !=
144 file.ReadBytes(array.data(), sizeof(NcaSectionHeaderBlock)))
145 LOG_CRITICAL(Loader, "File reader errored out during header read.");
146
147 NcaSectionHeaderBlock block{};
148 std::memcpy(&block, array.data(), sizeof(NcaSectionHeaderBlock));
149
150 if (block.filesystem_type == NcaSectionFilesystemType::ROMFS) {
151 romfs_offset = header.section_tables[i].media_offset * MEDIA_OFFSET_MULTIPLIER;
152 romfs_size =
153 header.section_tables[i].media_end_offset * MEDIA_OFFSET_MULTIPLIER - romfs_offset;
154 } else if (block.filesystem_type == NcaSectionFilesystemType::PFS0) {
155 Pfs0Superblock sb{};
156 // Seek back to beginning of this section.
157 file.Seek(SECTION_HEADER_OFFSET + i * SECTION_HEADER_SIZE, SEEK_SET);
158 if (sizeof(Pfs0Superblock) != file.ReadBytes(&sb, sizeof(Pfs0Superblock)))
159 LOG_CRITICAL(Loader, "File reader errored out during header read.");
160
161 u64 offset = (static_cast<u64>(header.section_tables[i].media_offset) *
162 MEDIA_OFFSET_MULTIPLIER) +
163 sb.pfs0_header_offset;
164 FileSys::PartitionFilesystem npfs{};
165 ResultStatus status = npfs.Load(path, offset);
166
167 if (status == ResultStatus::Success) {
168 pfs.emplace_back(std::move(npfs));
169 pfs_offset.emplace_back(offset);
170 }
171 }
172 }
173
174 for (size_t i = 0; i < pfs.size(); ++i) {
175 if (IsPfsExeFs(pfs[i]))
176 exefs_id = i;
177 }
178
179 return ResultStatus::Success;
180}
181
182FileSys::PartitionFilesystem Nca::GetPfs(u8 id) const {
183 return pfs[id];
184}
185
186u64 Nca::GetExeFsFileOffset(const std::string& file_name) const {
187 if (exefs_id == boost::none)
188 return 0;
189 return pfs[*exefs_id].GetFileOffset(file_name) + pfs_offset[*exefs_id];
190}
191
192u64 Nca::GetExeFsFileSize(const std::string& file_name) const {
193 if (exefs_id == boost::none)
194 return 0;
195 return pfs[*exefs_id].GetFileSize(file_name);
196}
197
198u64 Nca::GetRomFsOffset() const {
199 return romfs_offset;
200}
201
202u64 Nca::GetRomFsSize() const {
203 return romfs_size;
204}
205
206std::vector<u8> Nca::GetExeFsFile(const std::string& file_name) {
207 std::vector<u8> out(GetExeFsFileSize(file_name));
208 file.Seek(GetExeFsFileOffset(file_name), SEEK_SET);
209 file.ReadBytes(out.data(), GetExeFsFileSize(file_name));
210 return out;
211}
212
213AppLoader_NCA::AppLoader_NCA(FileUtil::IOFile&& file, std::string filepath)
214 : AppLoader(std::move(file)), filepath(std::move(filepath)) {}
215
216FileType AppLoader_NCA::IdentifyType(FileUtil::IOFile& file, const std::string&) {
217 file.Seek(0, SEEK_SET);
218 std::array<u8, 0x400> header_enc_array{};
219 if (0x400 != file.ReadBytes(header_enc_array.data(), 0x400))
220 return FileType::Error;
221 25
26FileType AppLoader_NCA::IdentifyType(const FileSys::VirtualFile& file) {
222 // TODO(DarkLordZach): Assuming everything is decrypted. Add crypto support. 27 // TODO(DarkLordZach): Assuming everything is decrypted. Add crypto support.
223 NcaHeader header{}; 28 FileSys::NCAHeader header{};
224 std::memcpy(&header, header_enc_array.data(), sizeof(NcaHeader)); 29 if (sizeof(FileSys::NCAHeader) != file->ReadObject(&header))
30 return FileType::Error;
225 31
226 if (IsValidNca(header) && header.content_type == NcaContentType::Program) 32 if (IsValidNCA(header) && header.content_type == FileSys::NCAContentType::Program)
227 return FileType::NCA; 33 return FileType::NCA;
228 34
229 return FileType::Error; 35 return FileType::Error;
@@ -233,17 +39,22 @@ ResultStatus AppLoader_NCA::Load(Kernel::SharedPtr<Kernel::Process>& process) {
233 if (is_loaded) { 39 if (is_loaded) {
234 return ResultStatus::ErrorAlreadyLoaded; 40 return ResultStatus::ErrorAlreadyLoaded;
235 } 41 }
236 if (!file.IsOpen()) {
237 return ResultStatus::Error;
238 }
239 42
240 nca = std::make_unique<Nca>(); 43 nca = std::make_unique<FileSys::NCA>(file);
241 ResultStatus result = nca->Load(std::move(file), filepath); 44 ResultStatus result = nca->GetStatus();
242 if (result != ResultStatus::Success) { 45 if (result != ResultStatus::Success) {
243 return result; 46 return result;
244 } 47 }
245 48
246 result = metadata.Load(nca->GetExeFsFile("main.npdm")); 49 if (nca->GetType() != FileSys::NCAContentType::Program)
50 return ResultStatus::ErrorInvalidFormat;
51
52 auto exefs = nca->GetExeFS();
53
54 if (exefs == nullptr)
55 return ResultStatus::ErrorInvalidFormat;
56
57 result = metadata.Load(exefs->GetFile("main.npdm"));
247 if (result != ResultStatus::Success) { 58 if (result != ResultStatus::Success) {
248 return result; 59 return result;
249 } 60 }
@@ -258,7 +69,8 @@ ResultStatus AppLoader_NCA::Load(Kernel::SharedPtr<Kernel::Process>& process) {
258 for (const auto& module : {"rtld", "main", "subsdk0", "subsdk1", "subsdk2", "subsdk3", 69 for (const auto& module : {"rtld", "main", "subsdk0", "subsdk1", "subsdk2", "subsdk3",
259 "subsdk4", "subsdk5", "subsdk6", "subsdk7", "sdk"}) { 70 "subsdk4", "subsdk5", "subsdk6", "subsdk7", "sdk"}) {
260 const VAddr load_addr = next_load_addr; 71 const VAddr load_addr = next_load_addr;
261 next_load_addr = AppLoader_NSO::LoadModule(module, nca->GetExeFsFile(module), load_addr); 72
73 next_load_addr = AppLoader_NSO::LoadModule(exefs->GetFile(module), load_addr);
262 if (next_load_addr) { 74 if (next_load_addr) {
263 LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", module, load_addr); 75 LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", module, load_addr);
264 // Register module with GDBStub 76 // Register module with GDBStub
@@ -276,28 +88,18 @@ ResultStatus AppLoader_NCA::Load(Kernel::SharedPtr<Kernel::Process>& process) {
276 process->Run(Memory::PROCESS_IMAGE_VADDR, metadata.GetMainThreadPriority(), 88 process->Run(Memory::PROCESS_IMAGE_VADDR, metadata.GetMainThreadPriority(),
277 metadata.GetMainThreadStackSize()); 89 metadata.GetMainThreadStackSize());
278 90
279 if (nca->GetRomFsSize() > 0) 91 if (nca->GetRomFS() != nullptr && nca->GetRomFS()->GetSize() > 0)
280 Service::FileSystem::RegisterRomFS(std::make_unique<FileSys::RomFSFactory>(*this)); 92 Service::FileSystem::RegisterRomFS(std::make_unique<FileSys::RomFSFactory>(*this));
281 93
282 is_loaded = true; 94 is_loaded = true;
95
283 return ResultStatus::Success; 96 return ResultStatus::Success;
284} 97}
285 98
286ResultStatus AppLoader_NCA::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, 99ResultStatus AppLoader_NCA::ReadRomFS(FileSys::VirtualFile& dir) {
287 u64& size) { 100 if (nca == nullptr || nca->GetRomFS() == nullptr || nca->GetRomFS()->GetSize() == 0)
288 if (nca->GetRomFsSize() == 0) {
289 LOG_DEBUG(Loader, "No RomFS available");
290 return ResultStatus::ErrorNotUsed; 101 return ResultStatus::ErrorNotUsed;
291 } 102 dir = nca->GetRomFS();
292
293 romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb");
294
295 offset = nca->GetRomFsOffset();
296 size = nca->GetRomFsSize();
297
298 LOG_DEBUG(Loader, "RomFS offset: 0x{:016X}", offset);
299 LOG_DEBUG(Loader, "RomFS size: 0x{:016X}", size);
300
301 return ResultStatus::Success; 103 return ResultStatus::Success;
302} 104}
303 105