yuzu-emu
/
yuzu
Archived
1
0
Fork 0

file_sys/ncch_container: add RomFS, ExeFS override to allow for backward compatibility with existing .romfs system archive dumps

This commit is contained in:
shinyquagsire23 2017-10-01 10:30:47 -06:00
parent c93e5ecfe6
commit e21f2348e7
2 changed files with 211 additions and 74 deletions

View File

@ -116,6 +116,7 @@ Loader::ResultStatus NCCHContainer::Load() {
if (is_loaded) if (is_loaded)
return Loader::ResultStatus::Success; return Loader::ResultStatus::Success;
if (file.IsOpen()) {
// Reset read pointer in case this file has been read before. // Reset read pointer in case this file has been read before.
file.Seek(0, SEEK_SET); file.Seek(0, SEEK_SET);
@ -134,9 +135,12 @@ Loader::ResultStatus NCCHContainer::Load() {
if (Loader::MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic) if (Loader::MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic)
return Loader::ResultStatus::ErrorInvalidFormat; return Loader::ResultStatus::ErrorInvalidFormat;
has_header = true;
// System archives and DLC don't have an extended header but have RomFS // System archives and DLC don't have an extended header but have RomFS
if (ncch_header.extended_header_size) { if (ncch_header.extended_header_size) {
if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header)) if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) !=
sizeof(ExHeader_Header))
return Loader::ResultStatus::Error; return Loader::ResultStatus::Error;
is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1; is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1;
@ -149,8 +153,10 @@ Loader::ResultStatus NCCHContainer::Load() {
u8 resource_limit_category = u8 resource_limit_category =
exheader_header.arm11_system_local_caps.resource_limit_category; exheader_header.arm11_system_local_caps.resource_limit_category;
LOG_DEBUG(Service_FS, "Name: %s", exheader_header.codeset_info.name); LOG_DEBUG(Service_FS, "Name: %s",
LOG_DEBUG(Service_FS, "Program ID: %016" PRIX64, ncch_header.program_id); exheader_header.codeset_info.name);
LOG_DEBUG(Service_FS, "Program ID: %016" PRIX64,
ncch_header.program_id);
LOG_DEBUG(Service_FS, "Code compressed: %s", is_compressed ? "yes" : "no"); LOG_DEBUG(Service_FS, "Code compressed: %s", is_compressed ? "yes" : "no");
LOG_DEBUG(Service_FS, "Entry point: 0x%08X", entry_point); LOG_DEBUG(Service_FS, "Entry point: 0x%08X", entry_point);
LOG_DEBUG(Service_FS, "Code size: 0x%08X", code_size); LOG_DEBUG(Service_FS, "Code size: 0x%08X", code_size);
@ -163,7 +169,8 @@ Loader::ResultStatus NCCHContainer::Load() {
static_cast<int>(exheader_header.arm11_system_local_caps.system_mode)); static_cast<int>(exheader_header.arm11_system_local_caps.system_mode));
if (exheader_header.system_info.jump_id != ncch_header.program_id) { if (exheader_header.system_info.jump_id != ncch_header.program_id) {
LOG_ERROR(Service_FS, "ExHeader Program ID mismatch: the ROM is probably encrypted."); LOG_ERROR(Service_FS,
"ExHeader Program ID mismatch: the ROM is probably encrypted.");
return Loader::ResultStatus::ErrorEncrypted; return Loader::ResultStatus::ErrorEncrypted;
} }
@ -182,26 +189,70 @@ Loader::ResultStatus NCCHContainer::Load() {
if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header)) if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header))
return Loader::ResultStatus::Error; return Loader::ResultStatus::Error;
exefs_file = FileUtil::IOFile(filepath, "rb");
has_exefs = true; has_exefs = true;
} }
if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0)
has_romfs = true; has_romfs = true;
}
LoadOverrides();
// We need at least one of these or overrides, practically
if (!(has_exefs || has_romfs || is_tainted))
return Loader::ResultStatus::Error;
is_loaded = true; is_loaded = true;
return Loader::ResultStatus::Success; return Loader::ResultStatus::Success;
} }
Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) { Loader::ResultStatus NCCHContainer::LoadOverrides() {
if (!file.IsOpen()) // Check for split-off files, mark the archive as tainted if we will use them
return Loader::ResultStatus::Error; std::string romfs_override = filepath + ".romfs";
if (FileUtil::Exists(romfs_override)) {
is_tainted = true;
}
// If we have a split-off exefs file/folder, it takes priority
std::string exefs_override = filepath + ".exefs";
std::string exefsdir_override = filepath + ".exefsdir/";
if (FileUtil::Exists(exefs_override)) {
exefs_file = FileUtil::IOFile(exefs_override, "rb");
if (exefs_file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) == sizeof(ExeFs_Header)) {
LOG_DEBUG(Service_FS, "Loading ExeFS section from %s", exefs_override.c_str());
exefs_offset = 0;
is_tainted = true;
has_exefs = true;
} else {
exefs_file = FileUtil::IOFile(filepath, "rb");
}
} else if (FileUtil::Exists(exefsdir_override) && FileUtil::IsDirectory(exefsdir_override)) {
is_tainted = true;
}
if (is_tainted)
LOG_WARNING(Service_FS,
"Loaded NCCH %s is tainted, application behavior may not be as expected!",
filepath.c_str());
return Loader::ResultStatus::Success;
}
Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) {
Loader::ResultStatus result = Load(); Loader::ResultStatus result = Load();
if (result != Loader::ResultStatus::Success) if (result != Loader::ResultStatus::Success)
return result; return result;
if (!has_exefs) // Check if we have files that can drop-in and replace
return Loader::ResultStatus::ErrorNotUsed; result = LoadOverrideExeFSSection(name, buffer);
if (result == Loader::ResultStatus::Success || !has_exefs)
return result;
// If we don't have any separate files, we'll need a full ExeFS
if (!exefs_file.IsOpen())
return Loader::ResultStatus::Error;
LOG_DEBUG(Service_FS, "%d sections:", kMaxSections); LOG_DEBUG(Service_FS, "%d sections:", kMaxSections);
// Iterate through the ExeFs archive until we find a section with the specified name... // 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
s64 section_offset = s64 section_offset =
(section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset); (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset);
file.Seek(section_offset, SEEK_SET); exefs_file.Seek(section_offset, SEEK_SET);
if (strcmp(section.name, ".code") == 0 && is_compressed) { if (strcmp(section.name, ".code") == 0 && is_compressed) {
// Section is compressed, read compressed .code section... // Section is compressed, read compressed .code section...
@ -226,7 +277,7 @@ Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vect
return Loader::ResultStatus::ErrorMemoryAllocationFailed; return Loader::ResultStatus::ErrorMemoryAllocationFailed;
} }
if (file.ReadBytes(&temp_buffer[0], section.size) != section.size) if (exefs_file.ReadBytes(&temp_buffer[0], section.size) != section.size)
return Loader::ResultStatus::Error; return Loader::ResultStatus::Error;
// Decompress .code section... // Decompress .code section...
@ -237,7 +288,7 @@ Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vect
} else { } else {
// Section is uncompressed... // Section is uncompressed...
buffer.resize(section.size); buffer.resize(section.size);
if (file.ReadBytes(&buffer[0], section.size) != section.size) if (exefs_file.ReadBytes(&buffer[0], section.size) != section.size)
return Loader::ResultStatus::Error; return Loader::ResultStatus::Error;
} }
return Loader::ResultStatus::Success; return Loader::ResultStatus::Success;
@ -246,20 +297,56 @@ Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vect
return Loader::ResultStatus::ErrorNotUsed; return Loader::ResultStatus::ErrorNotUsed;
} }
Loader::ResultStatus NCCHContainer::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, Loader::ResultStatus NCCHContainer::LoadOverrideExeFSSection(const char* name,
u64& offset, u64& size) { std::vector<u8>& buffer) {
if (!file.IsOpen()) std::string override_name;
// Map our section name to the extracted equivalent
if (!strcmp(name, ".code"))
override_name = "code.bin";
else if (!strcmp(name, "icon"))
override_name = "code.bin";
else if (!strcmp(name, "banner"))
override_name = "banner.bnr";
else if (!strcmp(name, "logo"))
override_name = "logo.bcma.lz";
else
return Loader::ResultStatus::Error; return Loader::ResultStatus::Error;
std::string section_override = filepath + ".exefsdir/" + override_name;
FileUtil::IOFile section_file(section_override, "rb");
if (section_file.IsOpen()) {
auto section_size = section_file.GetSize();
buffer.resize(section_size);
section_file.Seek(0, SEEK_SET);
if (section_file.ReadBytes(&buffer[0], section_size) == section_size) {
LOG_WARNING(Service_FS, "File %s overriding built-in ExeFS file",
section_override.c_str());
return Loader::ResultStatus::Success;
}
}
return Loader::ResultStatus::ErrorNotUsed;
}
Loader::ResultStatus NCCHContainer::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
u64& offset, u64& size) {
Loader::ResultStatus result = Load(); Loader::ResultStatus result = Load();
if (result != Loader::ResultStatus::Success) if (result != Loader::ResultStatus::Success)
return result; return result;
if (ReadOverrideRomFS(romfs_file, offset, size) == Loader::ResultStatus::Success)
return Loader::ResultStatus::Success;
if (!has_romfs) { if (!has_romfs) {
LOG_DEBUG(Service_FS, "RomFS requested from NCCH which has no RomFS"); LOG_DEBUG(Service_FS, "RomFS requested from NCCH which has no RomFS");
return Loader::ResultStatus::ErrorNotUsed; return Loader::ResultStatus::ErrorNotUsed;
} }
if (!file.IsOpen())
return Loader::ResultStatus::Error;
u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000;
u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000;
@ -280,11 +367,31 @@ Loader::ResultStatus NCCHContainer::ReadRomFS(std::shared_ptr<FileUtil::IOFile>&
return Loader::ResultStatus::Success; return Loader::ResultStatus::Success;
} }
Loader::ResultStatus NCCHContainer::ReadOverrideRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
u64& offset, u64& size) {
// Check for RomFS overrides
std::string split_filepath = filepath + ".romfs";
if (FileUtil::Exists(split_filepath)) {
romfs_file = std::make_shared<FileUtil::IOFile>(split_filepath, "rb");
if (romfs_file->IsOpen()) {
LOG_WARNING(Service_FS, "File %s overriding built-in RomFS", split_filepath.c_str());
offset = 0;
size = romfs_file->GetSize();
return Loader::ResultStatus::Success;
}
}
return Loader::ResultStatus::ErrorNotUsed;
}
Loader::ResultStatus NCCHContainer::ReadProgramId(u64_le& program_id) { Loader::ResultStatus NCCHContainer::ReadProgramId(u64_le& program_id) {
Loader::ResultStatus result = Load(); Loader::ResultStatus result = Load();
if (result != Loader::ResultStatus::Success) if (result != Loader::ResultStatus::Success)
return result; return result;
if (!has_header)
return Loader::ResultStatus::ErrorNotUsed;
program_id = ncch_header.program_id; program_id = ncch_header.program_id;
return Loader::ResultStatus::Success; return Loader::ResultStatus::Success;
} }

View File

@ -179,6 +179,13 @@ public:
*/ */
Loader::ResultStatus Load(); Loader::ResultStatus Load();
/**
* Attempt to find overridden sections for the NCCH and mark the container as tainted
* if any are found.
* @return ResultStatus result of function
*/
Loader::ResultStatus LoadOverrides();
/** /**
* Reads an application ExeFS section of an NCCH file (e.g. .code, .logo, etc.) * Reads an application ExeFS section of an NCCH file (e.g. .code, .logo, etc.)
* @param name Name of section to read out of NCCH file * @param name Name of section to read out of NCCH file
@ -187,6 +194,15 @@ public:
*/ */
Loader::ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer); Loader::ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer);
/**
* Reads an application ExeFS section from external files instead of an NCCH file,
* (e.g. code.bin, logo.bcma.lz, icon.icn, banner.bnr)
* @param name Name of section to read from external files
* @param buffer Vector to read data into
* @return ResultStatus result of function
*/
Loader::ResultStatus LoadOverrideExeFSSection(const char* name, std::vector<u8>& buffer);
/** /**
* Get the RomFS of the NCCH container * Get the RomFS of the NCCH container
* Since the RomFS can be huge, we return a file reference instead of copying to a buffer * Since the RomFS can be huge, we return a file reference instead of copying to a buffer
@ -198,6 +214,17 @@ public:
Loader::ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, Loader::ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset,
u64& size); u64& size);
/**
* Get the override RomFS of the NCCH container
* Since the RomFS can be huge, we return a file reference instead of copying to a buffer
* @param romfs_file The file containing the RomFS
* @param offset The offset the romfs begins on
* @param size The size of the romfs
* @return ResultStatus result of function
*/
Loader::ResultStatus ReadOverrideRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
u64& offset, u64& size);
/** /**
* Get the Program ID of the NCCH container * Get the Program ID of the NCCH container
* @return ResultStatus result of function * @return ResultStatus result of function
@ -227,10 +254,12 @@ public:
ExHeader_Header exheader_header; ExHeader_Header exheader_header;
private: private:
bool has_header = false;
bool has_exheader = false; bool has_exheader = false;
bool has_exefs = false; bool has_exefs = false;
bool has_romfs = false; bool has_romfs = false;
bool is_tainted = false; // Are there parts of this container being overridden?
bool is_loaded = false; bool is_loaded = false;
bool is_compressed = false; bool is_compressed = false;
@ -239,6 +268,7 @@ private:
std::string filepath; std::string filepath;
FileUtil::IOFile file; FileUtil::IOFile file;
FileUtil::IOFile exefs_file;
}; };
} // namespace FileSys } // namespace FileSys