summaryrefslogtreecommitdiff
path: root/src/core/loader/ncch.cpp
diff options
context:
space:
mode:
authorGravatar Emmanuel Gil Peyrot2015-01-07 01:01:19 +0000
committerGravatar Emmanuel Gil Peyrot2015-01-15 22:23:08 +0100
commit08aaa3350066b5d506368d18dddba3f829c85b7a (patch)
treebb4b188ad8a572bfb5010460abc23a5be0d3ca45 /src/core/loader/ncch.cpp
parentLoader: Display the type of the file being loaded. (diff)
downloadyuzu-08aaa3350066b5d506368d18dddba3f829c85b7a.tar.gz
yuzu-08aaa3350066b5d506368d18dddba3f829c85b7a.tar.xz
yuzu-08aaa3350066b5d506368d18dddba3f829c85b7a.zip
Loader: Clean up the NCCH AppLoader.
Diffstat (limited to 'src/core/loader/ncch.cpp')
-rw-r--r--src/core/loader/ncch.cpp99
1 files changed, 48 insertions, 51 deletions
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp
index d6eb549b7..aaaa4d650 100644
--- a/src/core/loader/ncch.cpp
+++ b/src/core/loader/ncch.cpp
@@ -13,8 +13,8 @@
13 13
14namespace Loader { 14namespace Loader {
15 15
16static 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
17static const int kBlockSize = 0x200; ///< Size of ExeFS blocks (in bytes) 17static const int kBlockSize = 0x200; ///< Size of ExeFS blocks (in bytes)
18 18
19/** 19/**
20 * Get the decompressed size of an LZSS compressed ExeFS file 20 * Get the decompressed size of an LZSS compressed ExeFS file
@@ -22,7 +22,7 @@ static const int kBlockSize = 0x200; ///< Size of ExeFS blocks (in bytes)
22 * @param size Size of compressed buffer 22 * @param size Size of compressed buffer
23 * @return Size of decompressed buffer 23 * @return Size of decompressed buffer
24 */ 24 */
25static u32 LZSS_GetDecompressedSize(u8* buffer, u32 size) { 25static u32 LZSS_GetDecompressedSize(const u8* buffer, u32 size) {
26 u32 offset_size = *(u32*)(buffer + size - 4); 26 u32 offset_size = *(u32*)(buffer + size - 4);
27 return offset_size + size; 27 return offset_size + size;
28} 28}
@@ -35,9 +35,9 @@ static u32 LZSS_GetDecompressedSize(u8* buffer, u32 size) {
35 * @param decompressed_size Size of decompressed buffer 35 * @param decompressed_size Size of decompressed buffer
36 * @return True on success, otherwise false 36 * @return True on success, otherwise false
37 */ 37 */
38static 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) {
39 u8* footer = compressed + compressed_size - 8; 39 const u8* footer = compressed + compressed_size - 8;
40 u32 buffer_top_and_bottom = *(u32*)footer; 40 u32 buffer_top_and_bottom = *reinterpret_cast<const u32*>(footer);
41 u32 out = decompressed_size; 41 u32 out = decompressed_size;
42 u32 index = compressed_size - ((buffer_top_and_bottom >> 24) & 0xFF); 42 u32 index = compressed_size - ((buffer_top_and_bottom >> 24) & 0xFF);
43 u32 stop_index = compressed_size - (buffer_top_and_bottom & 0xFFFFFF); 43 u32 stop_index = compressed_size - (buffer_top_and_bottom & 0xFFFFFF);
@@ -45,22 +45,21 @@ static bool LZSS_Decompress(u8* compressed, u32 compressed_size, u8* decompresse
45 memset(decompressed, 0, decompressed_size); 45 memset(decompressed, 0, decompressed_size);
46 memcpy(decompressed, compressed, compressed_size); 46 memcpy(decompressed, compressed, compressed_size);
47 47
48 while(index > stop_index) { 48 while (index > stop_index) {
49 u8 control = compressed[--index]; 49 u8 control = compressed[--index];
50 50
51 for(u32 i = 0; i < 8; i++) { 51 for (unsigned i = 0; i < 8; i++) {
52 if(index <= stop_index) 52 if (index <= stop_index)
53 break; 53 break;
54 if(index <= 0) 54 if (index <= 0)
55 break; 55 break;
56 if(out <= 0) 56 if (out <= 0)
57 break; 57 break;
58 58
59 if(control & 0x80) { 59 if (control & 0x80) {
60 // Check if compression is out of bounds 60 // Check if compression is out of bounds
61 if(index < 2) { 61 if (index < 2)
62 return false; 62 return false;
63 }
64 index -= 2; 63 index -= 2;
65 64
66 u32 segment_offset = compressed[index] | (compressed[index + 1] << 8); 65 u32 segment_offset = compressed[index] | (compressed[index + 1] << 8);
@@ -69,23 +68,21 @@ static bool LZSS_Decompress(u8* compressed, u32 compressed_size, u8* decompresse
69 segment_offset += 2; 68 segment_offset += 2;
70 69
71 // Check if compression is out of bounds 70 // Check if compression is out of bounds
72 if(out < segment_size) { 71 if (out < segment_size)
73 return false; 72 return false;
74 } 73
75 for(u32 j = 0; j < segment_size; j++) { 74 for (unsigned j = 0; j < segment_size; j++) {
76 // Check if compression is out of bounds 75 // Check if compression is out of bounds
77 if(out + segment_offset >= decompressed_size) { 76 if (out + segment_offset >= decompressed_size)
78 return false; 77 return false;
79 }
80 78
81 u8 data = decompressed[out + segment_offset]; 79 u8 data = decompressed[out + segment_offset];
82 decompressed[--out] = data; 80 decompressed[--out] = data;
83 } 81 }
84 } else { 82 } else {
85 // Check if compression is out of bounds 83 // Check if compression is out of bounds
86 if(out < 1) { 84 if (out < 1)
87 return false; 85 return false;
88 }
89 decompressed[--out] = compressed[--index]; 86 decompressed[--out] = compressed[--index];
90 } 87 }
91 control <<= 1; 88 control <<= 1;
@@ -126,46 +123,44 @@ ResultStatus AppLoader_NCCH::LoadExec() const {
126} 123}
127 124
128ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const { 125ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const {
129 // Iterate through the ExeFs archive until we find the .code file...
130 if (!file->IsOpen()) 126 if (!file->IsOpen())
131 return ResultStatus::Error; 127 return ResultStatus::Error;
132 128
133 LOG_DEBUG(Loader, "%d sections:", kMaxSections); 129 LOG_DEBUG(Loader, "%d sections:", kMaxSections);
134 for (int i = 0; i < kMaxSections; i++) { 130 // Iterate through the ExeFs archive until we find the .code file...
131 for (unsigned section_number = 0; section_number < kMaxSections; section_number++) {
132 const auto& section = exefs_header.section[section_number];
133
135 // Load the specified section... 134 // Load the specified section...
136 if (strcmp((const char*)exefs_header.section[i].name, name) == 0) { 135 if (strcmp(section.name, name) == 0) {
137 LOG_DEBUG(Loader, "%d - offset: 0x%08X, size: 0x%08X, name: %s", i, 136 LOG_DEBUG(Loader, "%d - offset: 0x%08X, size: 0x%08X, name: %s", section_number,
138 exefs_header.section[i].offset, exefs_header.section[i].size, 137 section.offset, section.size, section.name);
139 exefs_header.section[i].name);
140 138
141 s64 section_offset = (exefs_header.section[i].offset + exefs_offset + 139 s64 section_offset = (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset);
142 sizeof(ExeFs_Header)+ncch_offset);
143 file->Seek(section_offset, SEEK_SET); 140 file->Seek(section_offset, SEEK_SET);
144 141
145 // Section is compressed... 142 if (is_compressed) {
146 if (i == 0 && is_compressed) { 143 // Section is compressed, read compressed .code section...
147 // Read compressed .code section...
148 std::unique_ptr<u8[]> temp_buffer; 144 std::unique_ptr<u8[]> temp_buffer;
149 try { 145 try {
150 temp_buffer.reset(new u8[exefs_header.section[i].size]); 146 temp_buffer.reset(new u8[section.size]);
151 } catch (std::bad_alloc&) { 147 } catch (std::bad_alloc&) {
152 return ResultStatus::ErrorMemoryAllocationFailed; 148 return ResultStatus::ErrorMemoryAllocationFailed;
153 } 149 }
154 file->ReadBytes(&temp_buffer[0], exefs_header.section[i].size); 150
151 if (file->ReadBytes(&temp_buffer[0], section.size) != section.size)
152 return ResultStatus::Error;
155 153
156 // Decompress .code section... 154 // Decompress .code section...
157 u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], 155 u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], section.size);
158 exefs_header.section[i].size);
159 buffer.resize(decompressed_size); 156 buffer.resize(decompressed_size);
160 if (!LZSS_Decompress(&temp_buffer[0], exefs_header.section[i].size, &buffer[0], 157 if (!LZSS_Decompress(&temp_buffer[0], section.size, &buffer[0], decompressed_size))
161 decompressed_size)) {
162 return ResultStatus::ErrorInvalidFormat; 158 return ResultStatus::ErrorInvalidFormat;
163 } 159 } else {
164 // Section is uncompressed... 160 // Section is uncompressed...
165 } 161 buffer.resize(section.size);
166 else { 162 if (file->ReadBytes(&buffer[0], section.size) != section.size)
167 buffer.resize(exefs_header.section[i].size); 163 return ResultStatus::Error;
168 file->ReadBytes(&buffer[0], exefs_header.section[i].size);
169 } 164 }
170 return ResultStatus::Success; 165 return ResultStatus::Success;
171 } 166 }
@@ -183,7 +178,8 @@ ResultStatus AppLoader_NCCH::Load() {
183 // Reset read pointer in case this file has been read before. 178 // Reset read pointer in case this file has been read before.
184 file->Seek(0, SEEK_SET); 179 file->Seek(0, SEEK_SET);
185 180
186 file->ReadBytes(&ncch_header, sizeof(NCCH_Header)); 181 if (file->ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header))
182 return ResultStatus::Error;
187 183
188 // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)... 184 // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)...
189 if (MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) { 185 if (MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) {
@@ -199,7 +195,8 @@ ResultStatus AppLoader_NCCH::Load() {
199 195
200 // Read ExHeader... 196 // Read ExHeader...
201 197
202 file->ReadBytes(&exheader_header, sizeof(ExHeader_Header)); 198 if (file->ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header))
199 return ResultStatus::Error;
203 200
204 is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1; 201 is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1;
205 entry_point = exheader_header.codeset_info.text.address; 202 entry_point = exheader_header.codeset_info.text.address;
@@ -217,13 +214,12 @@ ResultStatus AppLoader_NCCH::Load() {
217 LOG_DEBUG(Loader, "ExeFS size: 0x%08X", exefs_size); 214 LOG_DEBUG(Loader, "ExeFS size: 0x%08X", exefs_size);
218 215
219 file->Seek(exefs_offset + ncch_offset, SEEK_SET); 216 file->Seek(exefs_offset + ncch_offset, SEEK_SET);
220 file->ReadBytes(&exefs_header, sizeof(ExeFs_Header)); 217 if (file->ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header))
221 218 return ResultStatus::Error;
222 LoadExec(); // Load the executable into memory for booting
223 219
224 is_loaded = true; // Set state to loaded 220 is_loaded = true; // Set state to loaded
225 221
226 return ResultStatus::Success; 222 return LoadExec(); // Load the executable into memory for booting
227} 223}
228 224
229ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) const { 225ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) const {
@@ -257,7 +253,8 @@ ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const {
257 buffer.resize(romfs_size); 253 buffer.resize(romfs_size);
258 254
259 file->Seek(romfs_offset, SEEK_SET); 255 file->Seek(romfs_offset, SEEK_SET);
260 file->ReadBytes(&buffer[0], romfs_size); 256 if (file->ReadBytes(&buffer[0], romfs_size) != romfs_size)
257 return ResultStatus::Error;
261 258
262 return ResultStatus::Success; 259 return ResultStatus::Success;
263 } 260 }