summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/CMakeLists.txt1
-rw-r--r--src/core/file_sys/archive_ncch.cpp29
-rw-r--r--src/core/file_sys/ncch_container.cpp245
-rw-r--r--src/core/file_sys/ncch_container.h30
-rw-r--r--src/core/file_sys/title_metadata.cpp212
-rw-r--r--src/core/file_sys/title_metadata.h125
-rw-r--r--src/core/loader/ncch.cpp17
7 files changed, 581 insertions, 78 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 3ed619991..2618da18c 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -29,6 +29,7 @@ set(SRCS
29 file_sys/ncch_container.cpp 29 file_sys/ncch_container.cpp
30 file_sys/path_parser.cpp 30 file_sys/path_parser.cpp
31 file_sys/savedata_archive.cpp 31 file_sys/savedata_archive.cpp
32 file_sys/title_metadata.cpp
32 frontend/camera/blank_camera.cpp 33 frontend/camera/blank_camera.cpp
33 frontend/camera/factory.cpp 34 frontend/camera/factory.cpp
34 frontend/camera/interface.cpp 35 frontend/camera/interface.cpp
diff --git a/src/core/file_sys/archive_ncch.cpp b/src/core/file_sys/archive_ncch.cpp
index 6d9007731..e8c5be983 100644
--- a/src/core/file_sys/archive_ncch.cpp
+++ b/src/core/file_sys/archive_ncch.cpp
@@ -13,7 +13,10 @@
13#include "core/file_sys/archive_ncch.h" 13#include "core/file_sys/archive_ncch.h"
14#include "core/file_sys/errors.h" 14#include "core/file_sys/errors.h"
15#include "core/file_sys/ivfc_archive.h" 15#include "core/file_sys/ivfc_archive.h"
16#include "core/file_sys/ncch_container.h"
17#include "core/file_sys/title_metadata.h"
16#include "core/hle/service/fs/archive.h" 18#include "core/hle/service/fs/archive.h"
19#include "core/loader/loader.h"
17 20
18//////////////////////////////////////////////////////////////////////////////////////////////////// 21////////////////////////////////////////////////////////////////////////////////////////////////////
19// FileSys namespace 22// FileSys namespace
@@ -25,8 +28,18 @@ static std::string GetNCCHContainerPath(const std::string& nand_directory) {
25} 28}
26 29
27static std::string GetNCCHPath(const std::string& mount_point, u32 high, u32 low) { 30static std::string GetNCCHPath(const std::string& mount_point, u32 high, u32 low) {
28 return Common::StringFromFormat("%s%08x/%08x/content/00000000.app.romfs", mount_point.c_str(), 31 u32 content_id = 0;
29 high, low); 32
33 // TODO(shinyquagsire23): Title database should be doing this path lookup
34 std::string content_path =
35 Common::StringFromFormat("%s%08x/%08x/content/", mount_point.c_str(), high, low);
36 std::string tmd_path = content_path + "00000000.tmd";
37 TitleMetadata tmd(tmd_path);
38 if (tmd.Load() == Loader::ResultStatus::Success) {
39 content_id = tmd.GetBootContentID();
40 }
41
42 return Common::StringFromFormat("%s%08x.app", content_path.c_str(), content_id);
30} 43}
31 44
32ArchiveFactory_NCCH::ArchiveFactory_NCCH(const std::string& nand_directory) 45ArchiveFactory_NCCH::ArchiveFactory_NCCH(const std::string& nand_directory)
@@ -38,9 +51,14 @@ ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path&
38 u32 high = data[1]; 51 u32 high = data[1];
39 u32 low = data[0]; 52 u32 low = data[0];
40 std::string file_path = GetNCCHPath(mount_point, high, low); 53 std::string file_path = GetNCCHPath(mount_point, high, low);
41 auto file = std::make_shared<FileUtil::IOFile>(file_path, "rb");
42 54
43 if (!file->IsOpen()) { 55 std::shared_ptr<FileUtil::IOFile> romfs_file;
56 u64 romfs_offset = 0;
57 u64 romfs_size = 0;
58 auto ncch_container = NCCHContainer(file_path);
59
60 if (ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size) !=
61 Loader::ResultStatus::Success) {
44 // High Title ID of the archive: The category (https://3dbrew.org/wiki/Title_list). 62 // High Title ID of the archive: The category (https://3dbrew.org/wiki/Title_list).
45 constexpr u32 shared_data_archive = 0x0004009B; 63 constexpr u32 shared_data_archive = 0x0004009B;
46 constexpr u32 system_data_archive = 0x000400DB; 64 constexpr u32 system_data_archive = 0x000400DB;
@@ -74,9 +92,8 @@ ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path&
74 } 92 }
75 return ERROR_NOT_FOUND; 93 return ERROR_NOT_FOUND;
76 } 94 }
77 auto size = file->GetSize();
78 95
79 auto archive = std::make_unique<IVFCArchive>(file, 0, size); 96 auto archive = std::make_unique<IVFCArchive>(romfs_file, romfs_offset, romfs_size);
80 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); 97 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
81} 98}
82 99
diff --git a/src/core/file_sys/ncch_container.cpp b/src/core/file_sys/ncch_container.cpp
index 59c72f3e9..b9fb940c7 100644
--- a/src/core/file_sys/ncch_container.cpp
+++ b/src/core/file_sys/ncch_container.cpp
@@ -116,92 +116,143 @@ Loader::ResultStatus NCCHContainer::Load() {
116 if (is_loaded) 116 if (is_loaded)
117 return Loader::ResultStatus::Success; 117 return Loader::ResultStatus::Success;
118 118
119 // Reset read pointer in case this file has been read before. 119 if (file.IsOpen()) {
120 file.Seek(0, SEEK_SET); 120 // Reset read pointer in case this file has been read before.
121 file.Seek(0, SEEK_SET);
121 122
122 if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header)) 123 if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header))
123 return Loader::ResultStatus::Error; 124 return Loader::ResultStatus::Error;
124 125
125 // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)... 126 // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)...
126 if (Loader::MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) { 127 if (Loader::MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) {
127 LOG_DEBUG(Service_FS, "Only loading the first (bootable) NCCH within the NCSD file!"); 128 LOG_DEBUG(Service_FS, "Only loading the first (bootable) NCCH within the NCSD file!");
128 ncch_offset = 0x4000; 129 ncch_offset = 0x4000;
129 file.Seek(ncch_offset, SEEK_SET); 130 file.Seek(ncch_offset, SEEK_SET);
130 file.ReadBytes(&ncch_header, sizeof(NCCH_Header)); 131 file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
131 } 132 }
132 133
133 // Verify we are loading the correct file type... 134 // Verify we are loading the correct file type...
134 if (Loader::MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic) 135 if (Loader::MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic)
135 return Loader::ResultStatus::ErrorInvalidFormat; 136 return Loader::ResultStatus::ErrorInvalidFormat;
137
138 has_header = true;
139
140 // System archives and DLC don't have an extended header but have RomFS
141 if (ncch_header.extended_header_size) {
142 if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) !=
143 sizeof(ExHeader_Header))
144 return Loader::ResultStatus::Error;
145
146 is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1;
147 u32 entry_point = exheader_header.codeset_info.text.address;
148 u32 code_size = exheader_header.codeset_info.text.code_size;
149 u32 stack_size = exheader_header.codeset_info.stack_size;
150 u32 bss_size = exheader_header.codeset_info.bss_size;
151 u32 core_version = exheader_header.arm11_system_local_caps.core_version;
152 u8 priority = exheader_header.arm11_system_local_caps.priority;
153 u8 resource_limit_category =
154 exheader_header.arm11_system_local_caps.resource_limit_category;
155
156 LOG_DEBUG(Service_FS, "Name: %s",
157 exheader_header.codeset_info.name);
158 LOG_DEBUG(Service_FS, "Program ID: %016" PRIX64,
159 ncch_header.program_id);
160 LOG_DEBUG(Service_FS, "Code compressed: %s", is_compressed ? "yes" : "no");
161 LOG_DEBUG(Service_FS, "Entry point: 0x%08X", entry_point);
162 LOG_DEBUG(Service_FS, "Code size: 0x%08X", code_size);
163 LOG_DEBUG(Service_FS, "Stack size: 0x%08X", stack_size);
164 LOG_DEBUG(Service_FS, "Bss size: 0x%08X", bss_size);
165 LOG_DEBUG(Service_FS, "Core version: %d", core_version);
166 LOG_DEBUG(Service_FS, "Thread priority: 0x%X", priority);
167 LOG_DEBUG(Service_FS, "Resource limit category: %d", resource_limit_category);
168 LOG_DEBUG(Service_FS, "System Mode: %d",
169 static_cast<int>(exheader_header.arm11_system_local_caps.system_mode));
170
171 if (exheader_header.system_info.jump_id != ncch_header.program_id) {
172 LOG_ERROR(Service_FS,
173 "ExHeader Program ID mismatch: the ROM is probably encrypted.");
174 return Loader::ResultStatus::ErrorEncrypted;
175 }
136 176
137 // System archives and DLC don't have an extended header but have RomFS 177 has_exheader = true;
138 if (ncch_header.extended_header_size) { 178 }
139 if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header))
140 return Loader::ResultStatus::Error;
141 179
142 is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1; 180 // DLC can have an ExeFS and a RomFS but no extended header
143 u32 entry_point = exheader_header.codeset_info.text.address; 181 if (ncch_header.exefs_size) {
144 u32 code_size = exheader_header.codeset_info.text.code_size; 182 exefs_offset = ncch_header.exefs_offset * kBlockSize;
145 u32 stack_size = exheader_header.codeset_info.stack_size; 183 u32 exefs_size = ncch_header.exefs_size * kBlockSize;
146 u32 bss_size = exheader_header.codeset_info.bss_size; 184
147 u32 core_version = exheader_header.arm11_system_local_caps.core_version; 185 LOG_DEBUG(Service_FS, "ExeFS offset: 0x%08X", exefs_offset);
148 u8 priority = exheader_header.arm11_system_local_caps.priority; 186 LOG_DEBUG(Service_FS, "ExeFS size: 0x%08X", exefs_size);
149 u8 resource_limit_category = 187
150 exheader_header.arm11_system_local_caps.resource_limit_category; 188 file.Seek(exefs_offset + ncch_offset, SEEK_SET);
151 189 if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header))
152 LOG_DEBUG(Service_FS, "Name: %s", exheader_header.codeset_info.name); 190 return Loader::ResultStatus::Error;
153 LOG_DEBUG(Service_FS, "Program ID: %016" PRIX64, ncch_header.program_id); 191
154 LOG_DEBUG(Service_FS, "Code compressed: %s", is_compressed ? "yes" : "no"); 192 exefs_file = FileUtil::IOFile(filepath, "rb");
155 LOG_DEBUG(Service_FS, "Entry point: 0x%08X", entry_point); 193 has_exefs = true;
156 LOG_DEBUG(Service_FS, "Code size: 0x%08X", code_size);
157 LOG_DEBUG(Service_FS, "Stack size: 0x%08X", stack_size);
158 LOG_DEBUG(Service_FS, "Bss size: 0x%08X", bss_size);
159 LOG_DEBUG(Service_FS, "Core version: %d", core_version);
160 LOG_DEBUG(Service_FS, "Thread priority: 0x%X", priority);
161 LOG_DEBUG(Service_FS, "Resource limit category: %d", resource_limit_category);
162 LOG_DEBUG(Service_FS, "System Mode: %d",
163 static_cast<int>(exheader_header.arm11_system_local_caps.system_mode));
164
165 if (exheader_header.system_info.jump_id != ncch_header.program_id) {
166 LOG_ERROR(Service_FS, "ExHeader Program ID mismatch: the ROM is probably encrypted.");
167 return Loader::ResultStatus::ErrorEncrypted;
168 } 194 }
169 195
170 has_exheader = true; 196 if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0)
197 has_romfs = true;
171 } 198 }
172 199
173 // DLC can have an ExeFS and a RomFS but no extended header 200 LoadOverrides();
174 if (ncch_header.exefs_size) {
175 exefs_offset = ncch_header.exefs_offset * kBlockSize;
176 u32 exefs_size = ncch_header.exefs_size * kBlockSize;
177 201
178 LOG_DEBUG(Service_FS, "ExeFS offset: 0x%08X", exefs_offset); 202 // We need at least one of these or overrides, practically
179 LOG_DEBUG(Service_FS, "ExeFS size: 0x%08X", exefs_size); 203 if (!(has_exefs || has_romfs || is_tainted))
204 return Loader::ResultStatus::Error;
180 205
181 file.Seek(exefs_offset + ncch_offset, SEEK_SET); 206 is_loaded = true;
182 if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header)) 207 return Loader::ResultStatus::Success;
183 return Loader::ResultStatus::Error; 208}
184 209
185 has_exefs = true; 210Loader::ResultStatus NCCHContainer::LoadOverrides() {
211 // Check for split-off files, mark the archive as tainted if we will use them
212 std::string romfs_override = filepath + ".romfs";
213 if (FileUtil::Exists(romfs_override)) {
214 is_tainted = true;
186 } 215 }
187 216
188 if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) 217 // If we have a split-off exefs file/folder, it takes priority
189 has_romfs = true; 218 std::string exefs_override = filepath + ".exefs";
219 std::string exefsdir_override = filepath + ".exefsdir/";
220 if (FileUtil::Exists(exefs_override)) {
221 exefs_file = FileUtil::IOFile(exefs_override, "rb");
222
223 if (exefs_file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) == sizeof(ExeFs_Header)) {
224 LOG_DEBUG(Service_FS, "Loading ExeFS section from %s", exefs_override.c_str());
225 exefs_offset = 0;
226 is_tainted = true;
227 has_exefs = true;
228 } else {
229 exefs_file = FileUtil::IOFile(filepath, "rb");
230 }
231 } else if (FileUtil::Exists(exefsdir_override) && FileUtil::IsDirectory(exefsdir_override)) {
232 is_tainted = true;
233 }
234
235 if (is_tainted)
236 LOG_WARNING(Service_FS,
237 "Loaded NCCH %s is tainted, application behavior may not be as expected!",
238 filepath.c_str());
190 239
191 is_loaded = true;
192 return Loader::ResultStatus::Success; 240 return Loader::ResultStatus::Success;
193} 241}
194 242
195Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) { 243Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) {
196 if (!file.IsOpen())
197 return Loader::ResultStatus::Error;
198
199 Loader::ResultStatus result = Load(); 244 Loader::ResultStatus result = Load();
200 if (result != Loader::ResultStatus::Success) 245 if (result != Loader::ResultStatus::Success)
201 return result; 246 return result;
202 247
203 if (!has_exefs) 248 // Check if we have files that can drop-in and replace
204 return Loader::ResultStatus::ErrorNotUsed; 249 result = LoadOverrideExeFSSection(name, buffer);
250 if (result == Loader::ResultStatus::Success || !has_exefs)
251 return result;
252
253 // If we don't have any separate files, we'll need a full ExeFS
254 if (!exefs_file.IsOpen())
255 return Loader::ResultStatus::Error;
205 256
206 LOG_DEBUG(Service_FS, "%d sections:", kMaxSections); 257 LOG_DEBUG(Service_FS, "%d sections:", kMaxSections);
207 // Iterate through the ExeFs archive until we find a section with the specified name... 258 // Iterate through the ExeFs archive until we find a section with the specified name...
@@ -215,7 +266,7 @@ Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vect
215 266
216 s64 section_offset = 267 s64 section_offset =
217 (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset); 268 (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset);
218 file.Seek(section_offset, SEEK_SET); 269 exefs_file.Seek(section_offset, SEEK_SET);
219 270
220 if (strcmp(section.name, ".code") == 0 && is_compressed) { 271 if (strcmp(section.name, ".code") == 0 && is_compressed) {
221 // Section is compressed, read compressed .code section... 272 // Section is compressed, read compressed .code section...
@@ -226,7 +277,7 @@ Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vect
226 return Loader::ResultStatus::ErrorMemoryAllocationFailed; 277 return Loader::ResultStatus::ErrorMemoryAllocationFailed;
227 } 278 }
228 279
229 if (file.ReadBytes(&temp_buffer[0], section.size) != section.size) 280 if (exefs_file.ReadBytes(&temp_buffer[0], section.size) != section.size)
230 return Loader::ResultStatus::Error; 281 return Loader::ResultStatus::Error;
231 282
232 // Decompress .code section... 283 // Decompress .code section...
@@ -237,7 +288,7 @@ Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vect
237 } else { 288 } else {
238 // Section is uncompressed... 289 // Section is uncompressed...
239 buffer.resize(section.size); 290 buffer.resize(section.size);
240 if (file.ReadBytes(&buffer[0], section.size) != section.size) 291 if (exefs_file.ReadBytes(&buffer[0], section.size) != section.size)
241 return Loader::ResultStatus::Error; 292 return Loader::ResultStatus::Error;
242 } 293 }
243 return Loader::ResultStatus::Success; 294 return Loader::ResultStatus::Success;
@@ -246,20 +297,56 @@ Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vect
246 return Loader::ResultStatus::ErrorNotUsed; 297 return Loader::ResultStatus::ErrorNotUsed;
247} 298}
248 299
249Loader::ResultStatus NCCHContainer::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, 300Loader::ResultStatus NCCHContainer::LoadOverrideExeFSSection(const char* name,
250 u64& offset, u64& size) { 301 std::vector<u8>& buffer) {
251 if (!file.IsOpen()) 302 std::string override_name;
303
304 // Map our section name to the extracted equivalent
305 if (!strcmp(name, ".code"))
306 override_name = "code.bin";
307 else if (!strcmp(name, "icon"))
308 override_name = "code.bin";
309 else if (!strcmp(name, "banner"))
310 override_name = "banner.bnr";
311 else if (!strcmp(name, "logo"))
312 override_name = "logo.bcma.lz";
313 else
252 return Loader::ResultStatus::Error; 314 return Loader::ResultStatus::Error;
253 315
316 std::string section_override = filepath + ".exefsdir/" + override_name;
317 FileUtil::IOFile section_file(section_override, "rb");
318
319 if (section_file.IsOpen()) {
320 auto section_size = section_file.GetSize();
321 buffer.resize(section_size);
322
323 section_file.Seek(0, SEEK_SET);
324 if (section_file.ReadBytes(&buffer[0], section_size) == section_size) {
325 LOG_WARNING(Service_FS, "File %s overriding built-in ExeFS file",
326 section_override.c_str());
327 return Loader::ResultStatus::Success;
328 }
329 }
330 return Loader::ResultStatus::ErrorNotUsed;
331}
332
333Loader::ResultStatus NCCHContainer::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
334 u64& offset, u64& size) {
254 Loader::ResultStatus result = Load(); 335 Loader::ResultStatus result = Load();
255 if (result != Loader::ResultStatus::Success) 336 if (result != Loader::ResultStatus::Success)
256 return result; 337 return result;
257 338
339 if (ReadOverrideRomFS(romfs_file, offset, size) == Loader::ResultStatus::Success)
340 return Loader::ResultStatus::Success;
341
258 if (!has_romfs) { 342 if (!has_romfs) {
259 LOG_DEBUG(Service_FS, "RomFS requested from NCCH which has no RomFS"); 343 LOG_DEBUG(Service_FS, "RomFS requested from NCCH which has no RomFS");
260 return Loader::ResultStatus::ErrorNotUsed; 344 return Loader::ResultStatus::ErrorNotUsed;
261 } 345 }
262 346
347 if (!file.IsOpen())
348 return Loader::ResultStatus::Error;
349
263 u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; 350 u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000;
264 u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; 351 u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000;
265 352
@@ -280,11 +367,31 @@ Loader::ResultStatus NCCHContainer::ReadRomFS(std::shared_ptr<FileUtil::IOFile>&
280 return Loader::ResultStatus::Success; 367 return Loader::ResultStatus::Success;
281} 368}
282 369
370Loader::ResultStatus NCCHContainer::ReadOverrideRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
371 u64& offset, u64& size) {
372 // Check for RomFS overrides
373 std::string split_filepath = filepath + ".romfs";
374 if (FileUtil::Exists(split_filepath)) {
375 romfs_file = std::make_shared<FileUtil::IOFile>(split_filepath, "rb");
376 if (romfs_file->IsOpen()) {
377 LOG_WARNING(Service_FS, "File %s overriding built-in RomFS", split_filepath.c_str());
378 offset = 0;
379 size = romfs_file->GetSize();
380 return Loader::ResultStatus::Success;
381 }
382 }
383
384 return Loader::ResultStatus::ErrorNotUsed;
385}
386
283Loader::ResultStatus NCCHContainer::ReadProgramId(u64_le& program_id) { 387Loader::ResultStatus NCCHContainer::ReadProgramId(u64_le& program_id) {
284 Loader::ResultStatus result = Load(); 388 Loader::ResultStatus result = Load();
285 if (result != Loader::ResultStatus::Success) 389 if (result != Loader::ResultStatus::Success)
286 return result; 390 return result;
287 391
392 if (!has_header)
393 return Loader::ResultStatus::ErrorNotUsed;
394
288 program_id = ncch_header.program_id; 395 program_id = ncch_header.program_id;
289 return Loader::ResultStatus::Success; 396 return Loader::ResultStatus::Success;
290} 397}
diff --git a/src/core/file_sys/ncch_container.h b/src/core/file_sys/ncch_container.h
index 8af9032b4..2cc9d13dc 100644
--- a/src/core/file_sys/ncch_container.h
+++ b/src/core/file_sys/ncch_container.h
@@ -180,6 +180,13 @@ public:
180 Loader::ResultStatus Load(); 180 Loader::ResultStatus Load();
181 181
182 /** 182 /**
183 * Attempt to find overridden sections for the NCCH and mark the container as tainted
184 * if any are found.
185 * @return ResultStatus result of function
186 */
187 Loader::ResultStatus LoadOverrides();
188
189 /**
183 * Reads an application ExeFS section of an NCCH file (e.g. .code, .logo, etc.) 190 * Reads an application ExeFS section of an NCCH file (e.g. .code, .logo, etc.)
184 * @param name Name of section to read out of NCCH file 191 * @param name Name of section to read out of NCCH file
185 * @param buffer Vector to read data into 192 * @param buffer Vector to read data into
@@ -188,6 +195,15 @@ public:
188 Loader::ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer); 195 Loader::ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer);
189 196
190 /** 197 /**
198 * Reads an application ExeFS section from external files instead of an NCCH file,
199 * (e.g. code.bin, logo.bcma.lz, icon.icn, banner.bnr)
200 * @param name Name of section to read from external files
201 * @param buffer Vector to read data into
202 * @return ResultStatus result of function
203 */
204 Loader::ResultStatus LoadOverrideExeFSSection(const char* name, std::vector<u8>& buffer);
205
206 /**
191 * Get the RomFS of the NCCH container 207 * Get the RomFS of the NCCH container
192 * Since the RomFS can be huge, we return a file reference instead of copying to a buffer 208 * Since the RomFS can be huge, we return a file reference instead of copying to a buffer
193 * @param romfs_file The file containing the RomFS 209 * @param romfs_file The file containing the RomFS
@@ -199,6 +215,17 @@ public:
199 u64& size); 215 u64& size);
200 216
201 /** 217 /**
218 * Get the override RomFS of the NCCH container
219 * Since the RomFS can be huge, we return a file reference instead of copying to a buffer
220 * @param romfs_file The file containing the RomFS
221 * @param offset The offset the romfs begins on
222 * @param size The size of the romfs
223 * @return ResultStatus result of function
224 */
225 Loader::ResultStatus ReadOverrideRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
226 u64& offset, u64& size);
227
228 /**
202 * Get the Program ID of the NCCH container 229 * Get the Program ID of the NCCH container
203 * @return ResultStatus result of function 230 * @return ResultStatus result of function
204 */ 231 */
@@ -227,10 +254,12 @@ public:
227 ExHeader_Header exheader_header; 254 ExHeader_Header exheader_header;
228 255
229private: 256private:
257 bool has_header = false;
230 bool has_exheader = false; 258 bool has_exheader = false;
231 bool has_exefs = false; 259 bool has_exefs = false;
232 bool has_romfs = false; 260 bool has_romfs = false;
233 261
262 bool is_tainted = false; // Are there parts of this container being overridden?
234 bool is_loaded = false; 263 bool is_loaded = false;
235 bool is_compressed = false; 264 bool is_compressed = false;
236 265
@@ -239,6 +268,7 @@ private:
239 268
240 std::string filepath; 269 std::string filepath;
241 FileUtil::IOFile file; 270 FileUtil::IOFile file;
271 FileUtil::IOFile exefs_file;
242}; 272};
243 273
244} // namespace FileSys 274} // namespace FileSys
diff --git a/src/core/file_sys/title_metadata.cpp b/src/core/file_sys/title_metadata.cpp
new file mode 100644
index 000000000..1ef8840a0
--- /dev/null
+++ b/src/core/file_sys/title_metadata.cpp
@@ -0,0 +1,212 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cinttypes>
6#include <cryptopp/sha.h>
7#include "common/alignment.h"
8#include "common/file_util.h"
9#include "common/logging/log.h"
10#include "core/file_sys/title_metadata.h"
11#include "core/loader/loader.h"
12
13////////////////////////////////////////////////////////////////////////////////////////////////////
14// FileSys namespace
15
16namespace FileSys {
17
18static u32 GetSignatureSize(u32 signature_type) {
19 switch (signature_type) {
20 case Rsa4096Sha1:
21 case Rsa4096Sha256:
22 return 0x200;
23
24 case Rsa2048Sha1:
25 case Rsa2048Sha256:
26 return 0x100;
27
28 case EllipticSha1:
29 case EcdsaSha256:
30 return 0x3C;
31 }
32}
33
34Loader::ResultStatus TitleMetadata::Load() {
35 FileUtil::IOFile file(filepath, "rb");
36 if (!file.IsOpen())
37 return Loader::ResultStatus::Error;
38
39 if (!file.ReadBytes(&signature_type, sizeof(u32_be)))
40 return Loader::ResultStatus::Error;
41
42 // Signature lengths are variable, and the body follows the signature
43 u32 signature_size = GetSignatureSize(signature_type);
44
45 tmd_signature.resize(signature_size);
46 if (!file.ReadBytes(&tmd_signature[0], signature_size))
47 return Loader::ResultStatus::Error;
48
49 // The TMD body start position is rounded to the nearest 0x40 after the signature
50 size_t body_start = Common::AlignUp(signature_size + sizeof(u32), 0x40);
51 file.Seek(body_start, SEEK_SET);
52
53 // Read our TMD body, then load the amount of ContentChunks specified
54 if (file.ReadBytes(&tmd_body, sizeof(TitleMetadata::Body)) != sizeof(TitleMetadata::Body))
55 return Loader::ResultStatus::Error;
56
57 for (u16 i = 0; i < tmd_body.content_count; i++) {
58 ContentChunk chunk;
59 if (file.ReadBytes(&chunk, sizeof(ContentChunk)) == sizeof(ContentChunk)) {
60 tmd_chunks.push_back(chunk);
61 } else {
62 LOG_ERROR(Service_FS, "Malformed TMD %s, failed to load content chunk index %u!",
63 filepath.c_str(), i);
64 return Loader::ResultStatus::ErrorInvalidFormat;
65 }
66 }
67
68 return Loader::ResultStatus::Success;
69}
70
71Loader::ResultStatus TitleMetadata::Save() {
72 FileUtil::IOFile file(filepath, "wb");
73 if (!file.IsOpen())
74 return Loader::ResultStatus::Error;
75
76 if (!file.WriteBytes(&signature_type, sizeof(u32_be)))
77 return Loader::ResultStatus::Error;
78
79 // Signature lengths are variable, and the body follows the signature
80 u32 signature_size = GetSignatureSize(signature_type);
81
82 if (!file.WriteBytes(tmd_signature.data(), signature_size))
83 return Loader::ResultStatus::Error;
84
85 // The TMD body start position is rounded to the nearest 0x40 after the signature
86 size_t body_start = Common::AlignUp(signature_size + sizeof(u32), 0x40);
87 file.Seek(body_start, SEEK_SET);
88
89 // Update our TMD body values and hashes
90 tmd_body.content_count = static_cast<u16>(tmd_chunks.size());
91
92 // TODO(shinyquagsire23): Do TMDs with more than one contentinfo exist?
93 // For now we'll just adjust the first index to hold all content chunks
94 // and ensure that no further content info data exists.
95 tmd_body.contentinfo = {};
96 tmd_body.contentinfo[0].index = 0;
97 tmd_body.contentinfo[0].command_count = static_cast<u16>(tmd_chunks.size());
98
99 CryptoPP::SHA256 chunk_hash;
100 for (u16 i = 0; i < tmd_body.content_count; i++) {
101 chunk_hash.Update(reinterpret_cast<u8*>(&tmd_chunks[i]), sizeof(ContentChunk));
102 }
103 chunk_hash.Final(tmd_body.contentinfo[0].hash.data());
104
105 CryptoPP::SHA256 contentinfo_hash;
106 for (size_t i = 0; i < tmd_body.contentinfo.size(); i++) {
107 chunk_hash.Update(reinterpret_cast<u8*>(&tmd_body.contentinfo[i]), sizeof(ContentInfo));
108 }
109 chunk_hash.Final(tmd_body.contentinfo_hash.data());
110
111 // Write our TMD body, then write each of our ContentChunks
112 if (file.WriteBytes(&tmd_body, sizeof(TitleMetadata::Body)) != sizeof(TitleMetadata::Body))
113 return Loader::ResultStatus::Error;
114
115 for (u16 i = 0; i < tmd_body.content_count; i++) {
116 ContentChunk chunk = tmd_chunks[i];
117 if (file.WriteBytes(&chunk, sizeof(ContentChunk)) != sizeof(ContentChunk))
118 return Loader::ResultStatus::Error;
119 }
120
121 return Loader::ResultStatus::Success;
122}
123
124u64 TitleMetadata::GetTitleID() const {
125 return tmd_body.title_id;
126}
127
128u32 TitleMetadata::GetTitleType() const {
129 return tmd_body.title_type;
130}
131
132u16 TitleMetadata::GetTitleVersion() const {
133 return tmd_body.title_version;
134}
135
136u64 TitleMetadata::GetSystemVersion() const {
137 return tmd_body.system_version;
138}
139
140size_t TitleMetadata::GetContentCount() const {
141 return tmd_chunks.size();
142}
143
144u32 TitleMetadata::GetBootContentID() const {
145 return tmd_chunks[TMDContentIndex::Main].id;
146}
147
148u32 TitleMetadata::GetManualContentID() const {
149 return tmd_chunks[TMDContentIndex::Manual].id;
150}
151
152u32 TitleMetadata::GetDLPContentID() const {
153 return tmd_chunks[TMDContentIndex::DLP].id;
154}
155
156void TitleMetadata::SetTitleID(u64 title_id) {
157 tmd_body.title_id = title_id;
158}
159
160void TitleMetadata::SetTitleType(u32 type) {
161 tmd_body.title_type = type;
162}
163
164void TitleMetadata::SetTitleVersion(u16 version) {
165 tmd_body.title_version = version;
166}
167
168void TitleMetadata::SetSystemVersion(u64 version) {
169 tmd_body.system_version = version;
170}
171
172void TitleMetadata::AddContentChunk(const ContentChunk& chunk) {
173 tmd_chunks.push_back(chunk);
174}
175
176void TitleMetadata::Print() const {
177 LOG_DEBUG(Service_FS, "%s - %u chunks", filepath.c_str(),
178 static_cast<u32>(tmd_body.content_count));
179
180 // Content info describes ranges of content chunks
181 LOG_DEBUG(Service_FS, "Content info:");
182 for (size_t i = 0; i < tmd_body.contentinfo.size(); i++) {
183 if (tmd_body.contentinfo[i].command_count == 0)
184 break;
185
186 LOG_DEBUG(Service_FS, " Index %04X, Command Count %04X",
187 static_cast<u32>(tmd_body.contentinfo[i].index),
188 static_cast<u32>(tmd_body.contentinfo[i].command_count));
189 }
190
191 // For each content info, print their content chunk range
192 for (size_t i = 0; i < tmd_body.contentinfo.size(); i++) {
193 u16 index = static_cast<u16>(tmd_body.contentinfo[i].index);
194 u16 count = static_cast<u16>(tmd_body.contentinfo[i].command_count);
195
196 if (count == 0)
197 continue;
198
199 LOG_DEBUG(Service_FS, "Content chunks for content info index %zu:", i);
200 for (u16 j = index; j < index + count; j++) {
201 // Don't attempt to print content we don't have
202 if (j > tmd_body.content_count)
203 break;
204
205 const ContentChunk& chunk = tmd_chunks[j];
206 LOG_DEBUG(Service_FS, " ID %08X, Index %04X, Type %04x, Size %016" PRIX64,
207 static_cast<u32>(chunk.id), static_cast<u32>(chunk.index),
208 static_cast<u32>(chunk.type), static_cast<u64>(chunk.size));
209 }
210 }
211}
212} // namespace FileSys
diff --git a/src/core/file_sys/title_metadata.h b/src/core/file_sys/title_metadata.h
new file mode 100644
index 000000000..1fc157bf3
--- /dev/null
+++ b/src/core/file_sys/title_metadata.h
@@ -0,0 +1,125 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <string>
8#include <vector>
9#include "common/common_types.h"
10#include "common/swap.h"
11
12namespace Loader {
13enum class ResultStatus;
14}
15
16////////////////////////////////////////////////////////////////////////////////////////////////////
17// FileSys namespace
18
19namespace FileSys {
20
21enum TMDSignatureType : u32 {
22 Rsa4096Sha1 = 0x10000,
23 Rsa2048Sha1 = 0x10001,
24 EllipticSha1 = 0x10002,
25 Rsa4096Sha256 = 0x10003,
26 Rsa2048Sha256 = 0x10004,
27 EcdsaSha256 = 0x10005
28};
29
30enum TMDContentTypeFlag : u16 {
31 Encrypted = 1 << 1,
32 Disc = 1 << 2,
33 CFM = 1 << 3,
34 Optional = 1 << 14,
35 Shared = 1 << 15
36};
37
38/**
39 * Helper which implements an interface to read and write Title Metadata (TMD) files.
40 * If a file path is provided and the file exists, it can be parsed and used, otherwise
41 * it must be created. The TMD file can then be interpreted, modified and/or saved.
42 */
43class TitleMetadata {
44public:
45 struct ContentChunk {
46 u32_be id;
47 u16_be index;
48 u16_be type;
49 u64_be size;
50 std::array<u8, 0x20> hash;
51 };
52
53 static_assert(sizeof(ContentChunk) == 0x30, "TMD ContentChunk structure size is wrong");
54
55 struct ContentInfo {
56 u16_be index;
57 u16_be command_count;
58 std::array<u8, 0x20> hash;
59 };
60
61 static_assert(sizeof(ContentInfo) == 0x24, "TMD ContentInfo structure size is wrong");
62
63#pragma pack(push, 1)
64
65 struct Body {
66 std::array<u8, 0x40> issuer;
67 u8 version;
68 u8 ca_crl_version;
69 u8 signer_crl_version;
70 u8 reserved;
71 u64_be system_version;
72 u64_be title_id;
73 u32_be title_type;
74 u16_be group_id;
75 u32_be savedata_size;
76 u32_be srl_private_savedata_size;
77 std::array<u8, 4> reserved_2;
78 u8 srl_flag;
79 std::array<u8, 0x31> reserved_3;
80 u32_be access_rights;
81 u16_be title_version;
82 u16_be content_count;
83 u16_be boot_content;
84 std::array<u8, 2> reserved_4;
85 std::array<u8, 0x20> contentinfo_hash;
86 std::array<ContentInfo, 64> contentinfo;
87 };
88
89 static_assert(sizeof(Body) == 0x9C4, "TMD body structure size is wrong");
90
91#pragma pack(pop)
92
93 explicit TitleMetadata(std::string& path) : filepath(std::move(path)) {}
94 Loader::ResultStatus Load();
95 Loader::ResultStatus Save();
96
97 u64 GetTitleID() const;
98 u32 GetTitleType() const;
99 u16 GetTitleVersion() const;
100 u64 GetSystemVersion() const;
101 size_t GetContentCount() const;
102 u32 GetBootContentID() const;
103 u32 GetManualContentID() const;
104 u32 GetDLPContentID() const;
105
106 void SetTitleID(u64 title_id);
107 void SetTitleType(u32 type);
108 void SetTitleVersion(u16 version);
109 void SetSystemVersion(u64 version);
110 void AddContentChunk(const ContentChunk& chunk);
111
112 void Print() const;
113
114private:
115 enum TMDContentIndex { Main = 0, Manual = 1, DLP = 2 };
116
117 Body tmd_body;
118 u32_be signature_type;
119 std::vector<u8> tmd_signature;
120 std::vector<ContentChunk> tmd_chunks;
121
122 std::string filepath;
123};
124
125} // namespace FileSys
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp
index 66bc5823d..52686e364 100644
--- a/src/core/loader/ncch.cpp
+++ b/src/core/loader/ncch.cpp
@@ -14,6 +14,7 @@
14#include "core/core.h" 14#include "core/core.h"
15#include "core/file_sys/archive_selfncch.h" 15#include "core/file_sys/archive_selfncch.h"
16#include "core/file_sys/ncch_container.h" 16#include "core/file_sys/ncch_container.h"
17#include "core/file_sys/title_metadata.h"
17#include "core/hle/kernel/process.h" 18#include "core/hle/kernel/process.h"
18#include "core/hle/kernel/resource_limit.h" 19#include "core/hle/kernel/resource_limit.h"
19#include "core/hle/service/cfg/cfg.h" 20#include "core/hle/service/cfg/cfg.h"
@@ -49,9 +50,19 @@ static std::string GetUpdateNCCHPath(u64_le program_id) {
49 u32 high = static_cast<u32>((program_id | UPDATE_MASK) >> 32); 50 u32 high = static_cast<u32>((program_id | UPDATE_MASK) >> 32);
50 u32 low = static_cast<u32>((program_id | UPDATE_MASK) & 0xFFFFFFFF); 51 u32 low = static_cast<u32>((program_id | UPDATE_MASK) & 0xFFFFFFFF);
51 52
52 return Common::StringFromFormat("%sNintendo 3DS/%s/%s/title/%08x/%08x/content/00000000.app", 53 // TODO(shinyquagsire23): Title database should be doing this path lookup
53 FileUtil::GetUserPath(D_SDMC_IDX).c_str(), SYSTEM_ID, SDCARD_ID, 54 std::string content_path = Common::StringFromFormat(
54 high, low); 55 "%sNintendo 3DS/%s/%s/title/%08x/%08x/content/", FileUtil::GetUserPath(D_SDMC_IDX).c_str(),
56 SYSTEM_ID, SDCARD_ID, high, low);
57 std::string tmd_path = content_path + "00000000.tmd";
58
59 u32 content_id = 0;
60 FileSys::TitleMetadata tmd(tmd_path);
61 if (tmd.Load() == ResultStatus::Success) {
62 content_id = tmd.GetBootContentID();
63 }
64
65 return Common::StringFromFormat("%s%08x.app", content_path.c_str(), content_id);
55} 66}
56 67
57std::pair<boost::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() { 68std::pair<boost::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() {