summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar shinyquagsire232017-10-01 10:30:47 -0600
committerGravatar shinyquagsire232017-10-01 10:53:18 -0600
commite21f2348e7da4ba2de9fe287276e8c215bcfe9d0 (patch)
treed578c13b23027087ec7dae8cfb6f2b441cebb893
parentfile_sys/archive_ncch: use NCCHContainer instead of loading .romfs files (diff)
downloadyuzu-e21f2348e7da4ba2de9fe287276e8c215bcfe9d0.tar.gz
yuzu-e21f2348e7da4ba2de9fe287276e8c215bcfe9d0.tar.xz
yuzu-e21f2348e7da4ba2de9fe287276e8c215bcfe9d0.zip
file_sys/ncch_container: add RomFS, ExeFS override to allow for backward compatibility with existing .romfs system archive dumps
-rw-r--r--src/core/file_sys/ncch_container.cpp245
-rw-r--r--src/core/file_sys/ncch_container.h30
2 files changed, 206 insertions, 69 deletions
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