summaryrefslogtreecommitdiff
path: root/src/core/loader/ncch.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/loader/ncch.cpp')
-rw-r--r--src/core/loader/ncch.cpp312
1 files changed, 312 insertions, 0 deletions
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp
new file mode 100644
index 000000000..60505bdfa
--- /dev/null
+++ b/src/core/loader/ncch.cpp
@@ -0,0 +1,312 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2
3// Refer to the license.txt file included.
4
5#include <memory>
6
7#include "common/file_util.h"
8
9#include "core/loader/ncch.h"
10#include "core/hle/kernel/kernel.h"
11#include "core/mem_map.h"
12
13////////////////////////////////////////////////////////////////////////////////////////////////////
14// Loader namespace
15
16namespace Loader {
17
18static const int kMaxSections = 8; ///< Maximum number of sections (files) in an ExeFs
19static const int kBlockSize = 0x200; ///< Size of ExeFS blocks (in bytes)
20
21/**
22 * Get the decompressed size of an LZSS compressed ExeFS file
23 * @param buffer Buffer of compressed file
24 * @param size Size of compressed buffer
25 * @return Size of decompressed buffer
26 */
27u32 LZSS_GetDecompressedSize(u8* buffer, u32 size) {
28 u32 offset_size = *(u32*)(buffer + size - 4);
29 return offset_size + size;
30}
31
32/**
33 * Decompress ExeFS file (compressed with LZSS)
34 * @param compressed Compressed buffer
35 * @param compressed_size Size of compressed buffer
36 * @param decompressed Decompressed buffer
37 * @param decompressed_size Size of decompressed buffer
38 * @return True on success, otherwise false
39 */
40bool LZSS_Decompress(u8* compressed, u32 compressed_size, u8* decompressed, u32 decompressed_size) {
41 u8* footer = compressed + compressed_size - 8;
42 u32 buffer_top_and_bottom = *(u32*)footer;
43 u32 i, j;
44 u32 out = decompressed_size;
45 u32 index = compressed_size - ((buffer_top_and_bottom >> 24) & 0xFF);
46 u8 control;
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 control = compressed[--index];
54
55 for(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 }
68 index -= 2;
69
70 u32 segment_offset = compressed[index] | (compressed[index + 1] << 8);
71 u32 segment_size = ((segment_offset >> 12) & 15) + 3;
72 segment_offset &= 0x0FFF;
73 segment_offset += 2;
74
75 // Check if compression is out of bounds
76 if(out < segment_size) {
77 return false;
78 }
79 for(j = 0; j < segment_size; j++) {
80 u8 data;
81 // Check if compression is out of bounds
82 if(out + segment_offset >= decompressed_size) {
83 return false;
84 }
85 data = decompressed[out + segment_offset];
86 decompressed[--out] = data;
87 }
88 } else {
89 // Check if compression is out of bounds
90 if(out < 1) {
91 return false;
92 }
93 decompressed[--out] = compressed[--index];
94 }
95 control <<= 1;
96 }
97 }
98 return true;
99}
100
101////////////////////////////////////////////////////////////////////////////////////////////////////
102// AppLoader_NCCH class
103
104/// AppLoader_NCCH constructor
105AppLoader_NCCH::AppLoader_NCCH(const std::string& filename) {
106 this->filename = filename;
107 is_loaded = false;
108 is_compressed = false;
109 entry_point = 0;
110 ncch_offset = 0;
111 exefs_offset = 0;
112}
113
114/// AppLoader_NCCH destructor
115AppLoader_NCCH::~AppLoader_NCCH() {
116 if (file.IsOpen())
117 file.Close();
118}
119
120/**
121 * Loads .code section into memory for booting
122 * @return ResultStatus result of function
123 */
124ResultStatus AppLoader_NCCH::LoadExec() {
125 if (!is_loaded)
126 return ResultStatus::ErrorNotLoaded;
127
128 ResultStatus res;
129 code = ReadCode(res);
130
131 if (ResultStatus::Success == res) {
132 Memory::WriteBlock(entry_point, &code[0], code.size());
133 Kernel::LoadExec(entry_point);
134 }
135 return res;
136}
137
138/**
139 * Reads an application ExeFS section of an NCCH file into AppLoader (e.g. .code, .logo, etc.)
140 * @param name Name of section to read out of NCCH file
141 * @param buffer Vector to read data into
142 * @param error ResultStatus result of function
143 * @return Reference to buffer of data that was read
144 */
145const std::vector<u8>& AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer,
146 ResultStatus& error) {
147 // Iterate through the ExeFs archive until we find the .code file...
148 for (int i = 0; i < kMaxSections; i++) {
149 // Load the specified section...
150 if (strcmp((const char*)exefs_header.section[i].name, name) == 0) {
151 INFO_LOG(LOADER, "ExeFS section %d:", i);
152 INFO_LOG(LOADER, " name: %s", exefs_header.section[i].name);
153 INFO_LOG(LOADER, " offset: 0x%08X", exefs_header.section[i].offset);
154 INFO_LOG(LOADER, " size: 0x%08X", exefs_header.section[i].size);
155
156 s64 section_offset = (exefs_header.section[i].offset + exefs_offset +
157 sizeof(ExeFs_Header) + ncch_offset);
158 file.Seek(section_offset, 0);
159
160 // Section is compressed...
161 if (i == 0 && is_compressed) {
162 // Read compressed .code section...
163 std::unique_ptr<u8[]> temp_buffer(new u8[exefs_header.section[i].size]);
164 file.ReadBytes(&temp_buffer[0], exefs_header.section[i].size);
165
166 // Decompress .code section...
167 u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0],
168 exefs_header.section[i].size);
169 buffer.resize(decompressed_size);
170 if (!LZSS_Decompress(&temp_buffer[0], exefs_header.section[i].size, &buffer[0],
171 decompressed_size)) {
172 error = ResultStatus::ErrorInvalidFormat;
173 return buffer;
174 }
175 // Section is uncompressed...
176 } else {
177 buffer.resize(exefs_header.section[i].size);
178 file.ReadBytes(&buffer[0], exefs_header.section[i].size);
179 }
180 error = ResultStatus::Success;
181 return buffer;
182 }
183 }
184 error = ResultStatus::ErrorNotUsed;
185 return buffer;
186}
187
188/**
189 * Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI)
190 * @param error_string Pointer to string to put error message if an error has occurred
191 * @todo Move NCSD parsing out of here and create a separate function for loading these
192 * @return True on success, otherwise false
193 */
194ResultStatus AppLoader_NCCH::Load() {
195 INFO_LOG(LOADER, "Loading NCCH file %s...", filename.c_str());
196
197 if (is_loaded)
198 return ResultStatus::ErrorAlreadyLoaded;
199
200 file = File::IOFile(filename, "rb");
201
202 if (file.IsOpen()) {
203 file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
204
205 // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)...
206 if (0 == memcmp(&ncch_header.magic, "NCSD", 4)) {
207 WARN_LOG(LOADER, "Only loading the first (bootable) NCCH within the NCSD file!");
208 ncch_offset = 0x4000;
209 file.Seek(ncch_offset, 0);
210 file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
211 }
212
213 // Verify we are loading the correct file type...
214 if (0 != memcmp(&ncch_header.magic, "NCCH", 4))
215 return ResultStatus::ErrorInvalidFormat;
216
217 // Read ExHeader...
218
219 file.ReadBytes(&exheader_header, sizeof(ExHeader_Header));
220
221 is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1;
222 entry_point = exheader_header.codeset_info.text.address;
223
224 INFO_LOG(LOADER, "Name: %s", exheader_header.codeset_info.name);
225 INFO_LOG(LOADER, "Code compressed: %s", is_compressed ? "yes" : "no");
226 INFO_LOG(LOADER, "Entry point: 0x%08X", entry_point);
227
228 // Read ExeFS...
229
230 exefs_offset = ncch_header.exefs_offset * kBlockSize;
231 u32 exefs_size = ncch_header.exefs_size * kBlockSize;
232
233 INFO_LOG(LOADER, "ExeFS offset: 0x%08X", exefs_offset);
234 INFO_LOG(LOADER, "ExeFS size: 0x%08X", exefs_size);
235
236 file.Seek(exefs_offset + ncch_offset, 0);
237 file.ReadBytes(&exefs_header, sizeof(ExeFs_Header));
238
239 is_loaded = true; // Set state to loaded
240
241 LoadExec(); // Load the executable into memory for booting
242
243 return ResultStatus::Success;
244 }
245 return ResultStatus::Error;
246}
247
248/**
249 * Get the code (typically .code section) of the application
250 * @param error ResultStatus result of function
251 * @return Reference to code buffer
252 */
253const std::vector<u8>& AppLoader_NCCH::ReadCode(ResultStatus& error) {
254 return LoadSectionExeFS(".code", code, error);
255}
256
257/**
258 * Get the icon (typically icon section) of the application
259 * @param error ResultStatus result of function
260 * @return Reference to icon buffer
261 */
262const std::vector<u8>& AppLoader_NCCH::ReadIcon(ResultStatus& error) {
263 return LoadSectionExeFS("icon", icon, error);
264}
265
266/**
267 * Get the banner (typically banner section) of the application
268 * @param error ResultStatus result of function
269 * @return Reference to banner buffer
270 */
271const std::vector<u8>& AppLoader_NCCH::ReadBanner(ResultStatus& error) {
272 return LoadSectionExeFS("banner", banner, error);
273}
274
275/**
276 * Get the logo (typically logo section) of the application
277 * @param error ResultStatus result of function
278 * @return Reference to logo buffer
279 */
280const std::vector<u8>& AppLoader_NCCH::ReadLogo(ResultStatus& error) {
281 return LoadSectionExeFS("logo", logo, error);
282}
283
284/**
285 * Get the RomFs archive of the application
286 * @param error ResultStatus result of function
287 * @return Reference to RomFs archive buffer
288 */
289const std::vector<u8>& AppLoader_NCCH::ReadRomFS(ResultStatus& error) {
290 // Check if the NCCH has a RomFS...
291 if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) {
292 u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000;
293 u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000;
294
295 INFO_LOG(LOADER, "RomFS offset: 0x%08X", romfs_offset);
296 INFO_LOG(LOADER, "RomFS size: 0x%08X", romfs_size);
297
298 romfs.resize(romfs_size);
299
300 file.Seek(romfs_offset, 0);
301 file.ReadBytes(&romfs[0], romfs_size);
302
303 error = ResultStatus::Success;
304 return romfs;
305 } else {
306 NOTICE_LOG(LOADER, "RomFS unused");
307 }
308 error = ResultStatus::ErrorNotUsed;
309 return romfs;
310}
311
312} // namespace Loader