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