summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorGravatar bunnei2017-10-15 00:11:38 -0400
committerGravatar bunnei2017-10-15 00:11:38 -0400
commit746c2a3ae769c6172700e4f9e10ba01fa0df4ccb (patch)
treefc17369ecf7eae3ba8567f3098223a40ab860ff4 /src/core
parenthle: Add service stubs for apm and appletOE. (diff)
downloadyuzu-746c2a3ae769c6172700e4f9e10ba01fa0df4ccb.tar.gz
yuzu-746c2a3ae769c6172700e4f9e10ba01fa0df4ccb.tar.xz
yuzu-746c2a3ae769c6172700e4f9e10ba01fa0df4ccb.zip
core: Refactor MakeMagic usage and remove dead code.
Diffstat (limited to 'src/core')
-rw-r--r--src/core/CMakeLists.txt3
-rw-r--r--src/core/file_sys/ncch_container.cpp423
-rw-r--r--src/core/file_sys/ncch_container.h274
-rw-r--r--src/core/loader/elf.cpp3
-rw-r--r--src/core/loader/loader.h4
-rw-r--r--src/core/loader/nro.cpp7
-rw-r--r--src/core/loader/nso.cpp7
-rw-r--r--src/core/loader/smdh.cpp51
-rw-r--r--src/core/loader/smdh.h81
9 files changed, 10 insertions, 843 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 4e9570424..95081f260 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -19,7 +19,6 @@ set(SRCS
19 file_sys/archive_backend.cpp 19 file_sys/archive_backend.cpp
20 file_sys/disk_archive.cpp 20 file_sys/disk_archive.cpp
21 file_sys/ivfc_archive.cpp 21 file_sys/ivfc_archive.cpp
22 file_sys/ncch_container.cpp
23 file_sys/path_parser.cpp 22 file_sys/path_parser.cpp
24 file_sys/savedata_archive.cpp 23 file_sys/savedata_archive.cpp
25 file_sys/title_metadata.cpp 24 file_sys/title_metadata.cpp
@@ -71,7 +70,6 @@ set(SRCS
71 loader/loader.cpp 70 loader/loader.cpp
72 loader/nro.cpp 71 loader/nro.cpp
73 loader/nso.cpp 72 loader/nso.cpp
74 loader/smdh.cpp
75 tracer/recorder.cpp 73 tracer/recorder.cpp
76 memory.cpp 74 memory.cpp
77 perf_stats.cpp 75 perf_stats.cpp
@@ -163,7 +161,6 @@ set(HEADERS
163 loader/loader.h 161 loader/loader.h
164 loader/nro.h 162 loader/nro.h
165 loader/nso.h 163 loader/nso.h
166 loader/smdh.h
167 tracer/recorder.h 164 tracer/recorder.h
168 tracer/citrace.h 165 tracer/citrace.h
169 memory.h 166 memory.h
diff --git a/src/core/file_sys/ncch_container.cpp b/src/core/file_sys/ncch_container.cpp
deleted file mode 100644
index b9fb940c7..000000000
--- a/src/core/file_sys/ncch_container.cpp
+++ /dev/null
@@ -1,423 +0,0 @@
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 <cstring>
7#include <memory>
8#include "common/common_types.h"
9#include "common/logging/log.h"
10#include "core/core.h"
11#include "core/file_sys/ncch_container.h"
12#include "core/loader/loader.h"
13
14////////////////////////////////////////////////////////////////////////////////////////////////////
15// FileSys namespace
16
17namespace FileSys {
18
19static const int kMaxSections = 8; ///< Maximum number of sections (files) in an ExeFs
20static const int kBlockSize = 0x200; ///< Size of ExeFS blocks (in bytes)
21
22/**
23 * Get the decompressed size of an LZSS compressed ExeFS file
24 * @param buffer Buffer of compressed file
25 * @param size Size of compressed buffer
26 * @return Size of decompressed buffer
27 */
28static u32 LZSS_GetDecompressedSize(const u8* buffer, u32 size) {
29 u32 offset_size = *(u32*)(buffer + size - 4);
30 return offset_size + size;
31}
32
33/**
34 * Decompress ExeFS file (compressed with LZSS)
35 * @param compressed Compressed buffer
36 * @param compressed_size Size of compressed buffer
37 * @param decompressed Decompressed buffer
38 * @param decompressed_size Size of decompressed buffer
39 * @return True on success, otherwise false
40 */
41static bool LZSS_Decompress(const u8* compressed, u32 compressed_size, u8* decompressed,
42 u32 decompressed_size) {
43 const u8* footer = compressed + compressed_size - 8;
44 u32 buffer_top_and_bottom = *reinterpret_cast<const u32*>(footer);
45 u32 out = decompressed_size;
46 u32 index = compressed_size - ((buffer_top_and_bottom >> 24) & 0xFF);
47 u32 stop_index = compressed_size - (buffer_top_and_bottom & 0xFFFFFF);
48
49 memset(decompressed, 0, decompressed_size);
50 memcpy(decompressed, compressed, compressed_size);
51
52 while (index > stop_index) {
53 u8 control = compressed[--index];
54
55 for (unsigned i = 0; i < 8; i++) {
56 if (index <= stop_index)
57 break;
58 if (index <= 0)
59 break;
60 if (out <= 0)
61 break;
62
63 if (control & 0x80) {
64 // Check if compression is out of bounds
65 if (index < 2)
66 return false;
67 index -= 2;
68
69 u32 segment_offset = compressed[index] | (compressed[index + 1] << 8);
70 u32 segment_size = ((segment_offset >> 12) & 15) + 3;
71 segment_offset &= 0x0FFF;
72 segment_offset += 2;
73
74 // Check if compression is out of bounds
75 if (out < segment_size)
76 return false;
77
78 for (unsigned j = 0; j < segment_size; j++) {
79 // Check if compression is out of bounds
80 if (out + segment_offset >= decompressed_size)
81 return false;
82
83 u8 data = decompressed[out + segment_offset];
84 decompressed[--out] = data;
85 }
86 } else {
87 // Check if compression is out of bounds
88 if (out < 1)
89 return false;
90 decompressed[--out] = compressed[--index];
91 }
92 control <<= 1;
93 }
94 }
95 return true;
96}
97
98NCCHContainer::NCCHContainer(const std::string& filepath) : filepath(filepath) {
99 file = FileUtil::IOFile(filepath, "rb");
100}
101
102Loader::ResultStatus NCCHContainer::OpenFile(const std::string& filepath) {
103 this->filepath = filepath;
104 file = FileUtil::IOFile(filepath, "rb");
105
106 if (!file.IsOpen()) {
107 LOG_WARNING(Service_FS, "Failed to open %s", filepath.c_str());
108 return Loader::ResultStatus::Error;
109 }
110
111 LOG_DEBUG(Service_FS, "Opened %s", filepath.c_str());
112 return Loader::ResultStatus::Success;
113}
114
115Loader::ResultStatus NCCHContainer::Load() {
116 if (is_loaded)
117 return Loader::ResultStatus::Success;
118
119 if (file.IsOpen()) {
120 // Reset read pointer in case this file has been read before.
121 file.Seek(0, SEEK_SET);
122
123 if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header))
124 return Loader::ResultStatus::Error;
125
126 // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)...
127 if (Loader::MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) {
128 LOG_DEBUG(Service_FS, "Only loading the first (bootable) NCCH within the NCSD file!");
129 ncch_offset = 0x4000;
130 file.Seek(ncch_offset, SEEK_SET);
131 file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
132 }
133
134 // Verify we are loading the correct file type...
135 if (Loader::MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic)
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 }
176
177 has_exheader = true;
178 }
179
180 // DLC can have an ExeFS and a RomFS but no extended header
181 if (ncch_header.exefs_size) {
182 exefs_offset = ncch_header.exefs_offset * kBlockSize;
183 u32 exefs_size = ncch_header.exefs_size * kBlockSize;
184
185 LOG_DEBUG(Service_FS, "ExeFS offset: 0x%08X", exefs_offset);
186 LOG_DEBUG(Service_FS, "ExeFS size: 0x%08X", exefs_size);
187
188 file.Seek(exefs_offset + ncch_offset, SEEK_SET);
189 if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header))
190 return Loader::ResultStatus::Error;
191
192 exefs_file = FileUtil::IOFile(filepath, "rb");
193 has_exefs = true;
194 }
195
196 if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0)
197 has_romfs = true;
198 }
199
200 LoadOverrides();
201
202 // We need at least one of these or overrides, practically
203 if (!(has_exefs || has_romfs || is_tainted))
204 return Loader::ResultStatus::Error;
205
206 is_loaded = true;
207 return Loader::ResultStatus::Success;
208}
209
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;
215 }
216
217 // If we have a split-off exefs file/folder, it takes priority
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());
239
240 return Loader::ResultStatus::Success;
241}
242
243Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) {
244 Loader::ResultStatus result = Load();
245 if (result != Loader::ResultStatus::Success)
246 return result;
247
248 // Check if we have files that can drop-in and replace
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;
256
257 LOG_DEBUG(Service_FS, "%d sections:", kMaxSections);
258 // Iterate through the ExeFs archive until we find a section with the specified name...
259 for (unsigned section_number = 0; section_number < kMaxSections; section_number++) {
260 const auto& section = exefs_header.section[section_number];
261
262 // Load the specified section...
263 if (strcmp(section.name, name) == 0) {
264 LOG_DEBUG(Service_FS, "%d - offset: 0x%08X, size: 0x%08X, name: %s", section_number,
265 section.offset, section.size, section.name);
266
267 s64 section_offset =
268 (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset);
269 exefs_file.Seek(section_offset, SEEK_SET);
270
271 if (strcmp(section.name, ".code") == 0 && is_compressed) {
272 // Section is compressed, read compressed .code section...
273 std::unique_ptr<u8[]> temp_buffer;
274 try {
275 temp_buffer.reset(new u8[section.size]);
276 } catch (std::bad_alloc&) {
277 return Loader::ResultStatus::ErrorMemoryAllocationFailed;
278 }
279
280 if (exefs_file.ReadBytes(&temp_buffer[0], section.size) != section.size)
281 return Loader::ResultStatus::Error;
282
283 // Decompress .code section...
284 u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], section.size);
285 buffer.resize(decompressed_size);
286 if (!LZSS_Decompress(&temp_buffer[0], section.size, &buffer[0], decompressed_size))
287 return Loader::ResultStatus::ErrorInvalidFormat;
288 } else {
289 // Section is uncompressed...
290 buffer.resize(section.size);
291 if (exefs_file.ReadBytes(&buffer[0], section.size) != section.size)
292 return Loader::ResultStatus::Error;
293 }
294 return Loader::ResultStatus::Success;
295 }
296 }
297 return Loader::ResultStatus::ErrorNotUsed;
298}
299
300Loader::ResultStatus NCCHContainer::LoadOverrideExeFSSection(const char* name,
301 std::vector<u8>& buffer) {
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
314 return Loader::ResultStatus::Error;
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) {
335 Loader::ResultStatus result = Load();
336 if (result != Loader::ResultStatus::Success)
337 return result;
338
339 if (ReadOverrideRomFS(romfs_file, offset, size) == Loader::ResultStatus::Success)
340 return Loader::ResultStatus::Success;
341
342 if (!has_romfs) {
343 LOG_DEBUG(Service_FS, "RomFS requested from NCCH which has no RomFS");
344 return Loader::ResultStatus::ErrorNotUsed;
345 }
346
347 if (!file.IsOpen())
348 return Loader::ResultStatus::Error;
349
350 u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000;
351 u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000;
352
353 LOG_DEBUG(Service_FS, "RomFS offset: 0x%08X", romfs_offset);
354 LOG_DEBUG(Service_FS, "RomFS size: 0x%08X", romfs_size);
355
356 if (file.GetSize() < romfs_offset + romfs_size)
357 return Loader::ResultStatus::Error;
358
359 // We reopen the file, to allow its position to be independent from file's
360 romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb");
361 if (!romfs_file->IsOpen())
362 return Loader::ResultStatus::Error;
363
364 offset = romfs_offset;
365 size = romfs_size;
366
367 return Loader::ResultStatus::Success;
368}
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
387Loader::ResultStatus NCCHContainer::ReadProgramId(u64_le& program_id) {
388 Loader::ResultStatus result = Load();
389 if (result != Loader::ResultStatus::Success)
390 return result;
391
392 if (!has_header)
393 return Loader::ResultStatus::ErrorNotUsed;
394
395 program_id = ncch_header.program_id;
396 return Loader::ResultStatus::Success;
397}
398
399bool NCCHContainer::HasExeFS() {
400 Loader::ResultStatus result = Load();
401 if (result != Loader::ResultStatus::Success)
402 return false;
403
404 return has_exefs;
405}
406
407bool NCCHContainer::HasRomFS() {
408 Loader::ResultStatus result = Load();
409 if (result != Loader::ResultStatus::Success)
410 return false;
411
412 return has_romfs;
413}
414
415bool NCCHContainer::HasExHeader() {
416 Loader::ResultStatus result = Load();
417 if (result != Loader::ResultStatus::Success)
418 return false;
419
420 return has_exheader;
421}
422
423} // namespace FileSys
diff --git a/src/core/file_sys/ncch_container.h b/src/core/file_sys/ncch_container.h
deleted file mode 100644
index 2cc9d13dc..000000000
--- a/src/core/file_sys/ncch_container.h
+++ /dev/null
@@ -1,274 +0,0 @@
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 <cstddef>
8#include <memory>
9#include <string>
10#include <vector>
11#include "common/bit_field.h"
12#include "common/common_types.h"
13#include "common/file_util.h"
14#include "common/swap.h"
15#include "core/core.h"
16
17////////////////////////////////////////////////////////////////////////////////////////////////////
18/// NCCH header (Note: "NCCH" appears to be a publicly unknown acronym)
19
20struct NCCH_Header {
21 u8 signature[0x100];
22 u32_le magic;
23 u32_le content_size;
24 u8 partition_id[8];
25 u16_le maker_code;
26 u16_le version;
27 u8 reserved_0[4];
28 u64_le program_id;
29 u8 reserved_1[0x10];
30 u8 logo_region_hash[0x20];
31 u8 product_code[0x10];
32 u8 extended_header_hash[0x20];
33 u32_le extended_header_size;
34 u8 reserved_2[4];
35 u8 flags[8];
36 u32_le plain_region_offset;
37 u32_le plain_region_size;
38 u32_le logo_region_offset;
39 u32_le logo_region_size;
40 u32_le exefs_offset;
41 u32_le exefs_size;
42 u32_le exefs_hash_region_size;
43 u8 reserved_3[4];
44 u32_le romfs_offset;
45 u32_le romfs_size;
46 u32_le romfs_hash_region_size;
47 u8 reserved_4[4];
48 u8 exefs_super_block_hash[0x20];
49 u8 romfs_super_block_hash[0x20];
50};
51
52static_assert(sizeof(NCCH_Header) == 0x200, "NCCH header structure size is wrong");
53
54////////////////////////////////////////////////////////////////////////////////////////////////////
55// ExeFS (executable file system) headers
56
57struct ExeFs_SectionHeader {
58 char name[8];
59 u32 offset;
60 u32 size;
61};
62
63struct ExeFs_Header {
64 ExeFs_SectionHeader section[8];
65 u8 reserved[0x80];
66 u8 hashes[8][0x20];
67};
68
69////////////////////////////////////////////////////////////////////////////////////////////////////
70// ExHeader (executable file system header) headers
71
72struct ExHeader_SystemInfoFlags {
73 u8 reserved[5];
74 u8 flag;
75 u8 remaster_version[2];
76};
77
78struct ExHeader_CodeSegmentInfo {
79 u32 address;
80 u32 num_max_pages;
81 u32 code_size;
82};
83
84struct ExHeader_CodeSetInfo {
85 u8 name[8];
86 ExHeader_SystemInfoFlags flags;
87 ExHeader_CodeSegmentInfo text;
88 u32 stack_size;
89 ExHeader_CodeSegmentInfo ro;
90 u8 reserved[4];
91 ExHeader_CodeSegmentInfo data;
92 u32 bss_size;
93};
94
95struct ExHeader_DependencyList {
96 u8 program_id[0x30][8];
97};
98
99struct ExHeader_SystemInfo {
100 u64 save_data_size;
101 u64_le jump_id;
102 u8 reserved_2[0x30];
103};
104
105struct ExHeader_StorageInfo {
106 u8 ext_save_data_id[8];
107 u8 system_save_data_id[8];
108 u8 reserved[8];
109 u8 access_info[7];
110 u8 other_attributes;
111};
112
113struct ExHeader_ARM11_SystemLocalCaps {
114 u64_le program_id;
115 u32_le core_version;
116 u8 reserved_flags[2];
117 union {
118 u8 flags0;
119 BitField<0, 2, u8> ideal_processor;
120 BitField<2, 2, u8> affinity_mask;
121 BitField<4, 4, u8> system_mode;
122 };
123 u8 priority;
124 u8 resource_limit_descriptor[0x10][2];
125 ExHeader_StorageInfo storage_info;
126 u8 service_access_control[0x20][8];
127 u8 ex_service_access_control[0x2][8];
128 u8 reserved[0xf];
129 u8 resource_limit_category;
130};
131
132struct ExHeader_ARM11_KernelCaps {
133 u32_le descriptors[28];
134 u8 reserved[0x10];
135};
136
137struct ExHeader_ARM9_AccessControl {
138 u8 descriptors[15];
139 u8 descversion;
140};
141
142struct ExHeader_Header {
143 ExHeader_CodeSetInfo codeset_info;
144 ExHeader_DependencyList dependency_list;
145 ExHeader_SystemInfo system_info;
146 ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps;
147 ExHeader_ARM11_KernelCaps arm11_kernel_caps;
148 ExHeader_ARM9_AccessControl arm9_access_control;
149 struct {
150 u8 signature[0x100];
151 u8 ncch_public_key_modulus[0x100];
152 ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps;
153 ExHeader_ARM11_KernelCaps arm11_kernel_caps;
154 ExHeader_ARM9_AccessControl arm9_access_control;
155 } access_desc;
156};
157
158static_assert(sizeof(ExHeader_Header) == 0x800, "ExHeader structure size is wrong");
159
160////////////////////////////////////////////////////////////////////////////////////////////////////
161// FileSys namespace
162
163namespace FileSys {
164
165/**
166 * Helper which implements an interface to deal with NCCH containers which can
167 * contain ExeFS archives or RomFS archives for games or other applications.
168 */
169class NCCHContainer {
170public:
171 NCCHContainer(const std::string& filepath);
172 NCCHContainer() {}
173
174 Loader::ResultStatus OpenFile(const std::string& filepath);
175
176 /**
177 * Ensure ExeFS and exheader is loaded and ready for reading sections
178 * @return ResultStatus result of function
179 */
180 Loader::ResultStatus Load();
181
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 /**
190 * Reads an application ExeFS section of an NCCH file (e.g. .code, .logo, etc.)
191 * @param name Name of section to read out of NCCH file
192 * @param buffer Vector to read data into
193 * @return ResultStatus result of function
194 */
195 Loader::ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer);
196
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 /**
207 * Get the RomFS of the NCCH container
208 * Since the RomFS can be huge, we return a file reference instead of copying to a buffer
209 * @param romfs_file The file containing the RomFS
210 * @param offset The offset the romfs begins on
211 * @param size The size of the romfs
212 * @return ResultStatus result of function
213 */
214 Loader::ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset,
215 u64& size);
216
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 /**
229 * Get the Program ID of the NCCH container
230 * @return ResultStatus result of function
231 */
232 Loader::ResultStatus ReadProgramId(u64_le& program_id);
233
234 /**
235 * Checks whether the NCCH container contains an ExeFS
236 * @return bool check result
237 */
238 bool HasExeFS();
239
240 /**
241 * Checks whether the NCCH container contains a RomFS
242 * @return bool check result
243 */
244 bool HasRomFS();
245
246 /**
247 * Checks whether the NCCH container contains an ExHeader
248 * @return bool check result
249 */
250 bool HasExHeader();
251
252 NCCH_Header ncch_header;
253 ExeFs_Header exefs_header;
254 ExHeader_Header exheader_header;
255
256private:
257 bool has_header = false;
258 bool has_exheader = false;
259 bool has_exefs = false;
260 bool has_romfs = false;
261
262 bool is_tainted = false; // Are there parts of this container being overridden?
263 bool is_loaded = false;
264 bool is_compressed = false;
265
266 u32 ncch_offset = 0; // Offset to NCCH header, can be 0 or after NCSD header
267 u32 exefs_offset = 0;
268
269 std::string filepath;
270 FileUtil::IOFile file;
271 FileUtil::IOFile exefs_file;
272};
273
274} // namespace FileSys
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp
index 9969a8c39..9ba913dbe 100644
--- a/src/core/loader/elf.cpp
+++ b/src/core/loader/elf.cpp
@@ -5,6 +5,7 @@
5#include <cstring> 5#include <cstring>
6#include <memory> 6#include <memory>
7#include <string> 7#include <string>
8#include "common/common_funcs.h"
8#include "common/common_types.h" 9#include "common/common_types.h"
9#include "common/file_util.h" 10#include "common/file_util.h"
10#include "common/logging/log.h" 11#include "common/logging/log.h"
@@ -376,7 +377,7 @@ FileType AppLoader_ELF::IdentifyType(FileUtil::IOFile& file) {
376 if (1 != file.ReadArray<u16>(&machine, 1)) 377 if (1 != file.ReadArray<u16>(&machine, 1))
377 return FileType::Error; 378 return FileType::Error;
378 379
379 if (MakeMagic('\x7f', 'E', 'L', 'F') == magic && ELF_MACHINE_ARM == machine) 380 if (Common::MakeMagic('\x7f', 'E', 'L', 'F') == magic && ELF_MACHINE_ARM == machine)
380 return FileType::ELF; 381 return FileType::ELF;
381 382
382 return FileType::Error; 383 return FileType::Error;
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h
index ac4e7acc2..dd6bb4e64 100644
--- a/src/core/loader/loader.h
+++ b/src/core/loader/loader.h
@@ -75,10 +75,6 @@ enum class ResultStatus {
75 ErrorEncrypted, 75 ErrorEncrypted,
76}; 76};
77 77
78constexpr u32 MakeMagic(char a, char b, char c, char d) {
79 return a | b << 8 | c << 16 | d << 24;
80}
81
82/// Interface for loading an application 78/// Interface for loading an application
83class AppLoader : NonCopyable { 79class AppLoader : NonCopyable {
84public: 80public:
diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp
index 24c2c55a9..b37c3a092 100644
--- a/src/core/loader/nro.cpp
+++ b/src/core/loader/nro.cpp
@@ -4,6 +4,7 @@
4 4
5#include <vector> 5#include <vector>
6 6
7#include "common/common_funcs.h"
7#include "common/logging/log.h" 8#include "common/logging/log.h"
8#include "common/swap.h" 9#include "common/swap.h"
9#include "core/hle/kernel/process.h" 10#include "core/hle/kernel/process.h"
@@ -51,7 +52,7 @@ FileType AppLoader_NRO::IdentifyType(FileUtil::IOFile& file) {
51 if (sizeof(NroHeader) != file.ReadBytes(&nro_header, sizeof(NroHeader))) { 52 if (sizeof(NroHeader) != file.ReadBytes(&nro_header, sizeof(NroHeader))) {
52 return FileType::Error; 53 return FileType::Error;
53 } 54 }
54 if (nro_header.magic == MakeMagic('N', 'R', 'O', '0')) { 55 if (nro_header.magic == Common::MakeMagic('N', 'R', 'O', '0')) {
55 return FileType::NRO; 56 return FileType::NRO;
56 } 57 }
57 return FileType::Error; 58 return FileType::Error;
@@ -87,7 +88,7 @@ bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) {
87 if (sizeof(NroHeader) != file.ReadBytes(&nro_header, sizeof(NroHeader))) { 88 if (sizeof(NroHeader) != file.ReadBytes(&nro_header, sizeof(NroHeader))) {
88 return {}; 89 return {};
89 } 90 }
90 if (nro_header.magic != MakeMagic('N', 'R', 'O', '0')) { 91 if (nro_header.magic != Common::MakeMagic('N', 'R', 'O', '0')) {
91 return {}; 92 return {};
92 } 93 }
93 94
@@ -109,7 +110,7 @@ bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) {
109 u32 bss_size{Memory::PAGE_SIZE}; // Default .bss to page size if MOD0 section doesn't exist 110 u32 bss_size{Memory::PAGE_SIZE}; // Default .bss to page size if MOD0 section doesn't exist
110 std::memcpy(&mod_header, program_image.data() + nro_header.module_header_offset, 111 std::memcpy(&mod_header, program_image.data() + nro_header.module_header_offset,
111 sizeof(ModHeader)); 112 sizeof(ModHeader));
112 const bool has_mod_header{mod_header.magic == MakeMagic('M', 'O', 'D', '0')}; 113 const bool has_mod_header{mod_header.magic == Common::MakeMagic('M', 'O', 'D', '0')};
113 if (has_mod_header) { 114 if (has_mod_header) {
114 // Resize program image to include .bss section and page align each section 115 // Resize program image to include .bss section and page align each section
115 bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset); 116 bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset);
diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp
index 5ebbde19a..0d16d4b8c 100644
--- a/src/core/loader/nso.cpp
+++ b/src/core/loader/nso.cpp
@@ -5,6 +5,7 @@
5#include <vector> 5#include <vector>
6#include <lz4.h> 6#include <lz4.h>
7 7
8#include "common/common_funcs.h"
8#include "common/logging/log.h" 9#include "common/logging/log.h"
9#include "common/swap.h" 10#include "common/swap.h"
10#include "core/hle/kernel/process.h" 11#include "core/hle/kernel/process.h"
@@ -50,7 +51,7 @@ FileType AppLoader_NSO::IdentifyType(FileUtil::IOFile& file) {
50 return FileType::Error; 51 return FileType::Error;
51 } 52 }
52 53
53 if (MakeMagic('N', 'S', 'O', '0') == magic) { 54 if (Common::MakeMagic('N', 'S', 'O', '0') == magic) {
54 return FileType::NSO; 55 return FileType::NSO;
55 } 56 }
56 57
@@ -96,7 +97,7 @@ VAddr AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base, bool relo
96 if (sizeof(NsoHeader) != file.ReadBytes(&nso_header, sizeof(NsoHeader))) { 97 if (sizeof(NsoHeader) != file.ReadBytes(&nso_header, sizeof(NsoHeader))) {
97 return {}; 98 return {};
98 } 99 }
99 if (nso_header.magic != MakeMagic('N', 'S', 'O', '0')) { 100 if (nso_header.magic != Common::MakeMagic('N', 'S', 'O', '0')) {
100 return {}; 101 return {};
101 } 102 }
102 103
@@ -121,7 +122,7 @@ VAddr AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base, bool relo
121 ModHeader mod_header{}; 122 ModHeader mod_header{};
122 u32 bss_size{Memory::PAGE_SIZE}; // Default .bss to page size if MOD0 section doesn't exist 123 u32 bss_size{Memory::PAGE_SIZE}; // Default .bss to page size if MOD0 section doesn't exist
123 std::memcpy(&mod_header, program_image.data() + module_offset, sizeof(ModHeader)); 124 std::memcpy(&mod_header, program_image.data() + module_offset, sizeof(ModHeader));
124 const bool has_mod_header{mod_header.magic == MakeMagic('M', 'O', 'D', '0')}; 125 const bool has_mod_header{mod_header.magic == Common::MakeMagic('M', 'O', 'D', '0')};
125 if (has_mod_header) { 126 if (has_mod_header) {
126 // Resize program image to include .bss section and page align each section 127 // Resize program image to include .bss section and page align each section
127 bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset); 128 bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset);
diff --git a/src/core/loader/smdh.cpp b/src/core/loader/smdh.cpp
deleted file mode 100644
index ccbeb7961..000000000
--- a/src/core/loader/smdh.cpp
+++ /dev/null
@@ -1,51 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cstring>
6#include <vector>
7#include "common/common_types.h"
8#include "core/loader/loader.h"
9#include "core/loader/smdh.h"
10#include "video_core/utils.h"
11
12namespace Loader {
13
14bool IsValidSMDH(const std::vector<u8>& smdh_data) {
15 if (smdh_data.size() < sizeof(Loader::SMDH))
16 return false;
17
18 u32 magic;
19 memcpy(&magic, smdh_data.data(), sizeof(u32));
20
21 return Loader::MakeMagic('S', 'M', 'D', 'H') == magic;
22}
23
24std::vector<u16> SMDH::GetIcon(bool large) const {
25 u32 size;
26 const u8* icon_data;
27
28 if (large) {
29 size = 48;
30 icon_data = large_icon.data();
31 } else {
32 size = 24;
33 icon_data = small_icon.data();
34 }
35
36 std::vector<u16> icon(size * size);
37 for (u32 x = 0; x < size; ++x) {
38 for (u32 y = 0; y < size; ++y) {
39 u32 coarse_y = y & ~7;
40 const u8* pixel = icon_data + VideoCore::GetMortonOffset(x, y, 2) + coarse_y * size * 2;
41 icon[x + size * y] = (pixel[1] << 8) + pixel[0];
42 }
43 }
44 return icon;
45}
46
47std::array<u16, 0x40> SMDH::GetShortTitle(Loader::SMDH::TitleLanguage language) const {
48 return titles[static_cast<int>(language)].short_title;
49}
50
51} // namespace
diff --git a/src/core/loader/smdh.h b/src/core/loader/smdh.h
deleted file mode 100644
index ac7726c8f..000000000
--- a/src/core/loader/smdh.h
+++ /dev/null
@@ -1,81 +0,0 @@
1// Copyright 2016 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 <array>
8#include <vector>
9#include "common/common_funcs.h"
10#include "common/common_types.h"
11#include "common/swap.h"
12
13namespace Loader {
14
15/**
16 * Tests if data is a valid SMDH by its length and magic number.
17 * @param smdh_data data buffer to test
18 * @return bool test result
19 */
20bool IsValidSMDH(const std::vector<u8>& smdh_data);
21
22/// SMDH data structure that contains titles, icons etc. See https://www.3dbrew.org/wiki/SMDH
23struct SMDH {
24 u32_le magic;
25 u16_le version;
26 INSERT_PADDING_BYTES(2);
27
28 struct Title {
29 std::array<u16, 0x40> short_title;
30 std::array<u16, 0x80> long_title;
31 std::array<u16, 0x40> publisher;
32 };
33 std::array<Title, 16> titles;
34
35 std::array<u8, 16> ratings;
36 u32_le region_lockout;
37 u32_le match_maker_id;
38 u64_le match_maker_bit_id;
39 u32_le flags;
40 u16_le eula_version;
41 INSERT_PADDING_BYTES(2);
42 float_le banner_animation_frame;
43 u32_le cec_id;
44 INSERT_PADDING_BYTES(8);
45
46 std::array<u8, 0x480> small_icon;
47 std::array<u8, 0x1200> large_icon;
48
49 /// indicates the language used for each title entry
50 enum class TitleLanguage {
51 Japanese = 0,
52 English = 1,
53 French = 2,
54 German = 3,
55 Italian = 4,
56 Spanish = 5,
57 SimplifiedChinese = 6,
58 Korean = 7,
59 Dutch = 8,
60 Portuguese = 9,
61 Russian = 10,
62 TraditionalChinese = 11
63 };
64
65 /**
66 * Gets game icon from SMDH
67 * @param large If true, returns large icon (48x48), otherwise returns small icon (24x24)
68 * @return vector of RGB565 data
69 */
70 std::vector<u16> GetIcon(bool large) const;
71
72 /**
73 * Gets the short game title from SMDH
74 * @param language title language
75 * @return UTF-16 array of the short title
76 */
77 std::array<u16, 0x40> GetShortTitle(Loader::SMDH::TitleLanguage language) const;
78};
79static_assert(sizeof(SMDH) == 0x36C0, "SMDH structure size is wrong");
80
81} // namespace