From bc2212106f755f85e55d039ffc783116b15e5df6 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Mon, 5 Jan 2015 20:09:35 +0000 Subject: Loader: Fix indentation, whitespace, and a few other such cosmetic stuff. --- src/core/loader/ncch.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/loader/ncch.cpp') diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index 0dc21699e..e246cebc4 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -149,7 +149,7 @@ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector& exefs_header.section[i].name); s64 section_offset = (exefs_header.section[i].offset + exefs_offset + - sizeof(ExeFs_Header)+ncch_offset); + sizeof(ExeFs_Header)+ncch_offset); file.Seek(section_offset, 0); // Section is compressed... -- cgit v1.2.3 From 43e699d849ac5dd7a29ff0eeb5821e2a824c091e Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 6 Jan 2015 23:36:48 +0000 Subject: =?UTF-8?q?Loader:=20Don=E2=80=99t=20duplicate=20the=20docstring?= =?UTF-8?q?=20into=20the=20cpp=20file.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/loader/ncch.cpp | 41 ----------------------------------------- 1 file changed, 41 deletions(-) (limited to 'src/core/loader/ncch.cpp') diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index e246cebc4..f1d01252e 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -113,10 +113,6 @@ AppLoader_NCCH::AppLoader_NCCH(const std::string& filename) { AppLoader_NCCH::~AppLoader_NCCH() { } -/** - * Loads .code section into memory for booting - * @return ResultStatus result of function - */ ResultStatus AppLoader_NCCH::LoadExec() const { if (!is_loaded) return ResultStatus::ErrorNotLoaded; @@ -130,12 +126,6 @@ ResultStatus AppLoader_NCCH::LoadExec() const { return ResultStatus::Error; } -/** - * Reads an application ExeFS section of an NCCH file into AppLoader (e.g. .code, .logo, etc.) - * @param name Name of section to read out of NCCH file - * @param buffer Vector to read data into - * @return ResultStatus result of function - */ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector& buffer) const { // Iterate through the ExeFs archive until we find the .code file... FileUtil::IOFile file(filename, "rb"); @@ -187,12 +177,6 @@ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector& return ResultStatus::ErrorNotUsed; } -/** - * Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI) - * @param error_string Pointer to string to put error message if an error has occurred - * @todo Move NCSD parsing out of here and create a separate function for loading these - * @return True on success, otherwise false - */ ResultStatus AppLoader_NCCH::Load() { LOG_INFO(Loader, "Loading NCCH file %s...", filename.c_str()); @@ -248,47 +232,22 @@ ResultStatus AppLoader_NCCH::Load() { return ResultStatus::Error; } -/** - * Get the code (typically .code section) of the application - * @param buffer Reference to buffer to store data - * @return ResultStatus result of function - */ ResultStatus AppLoader_NCCH::ReadCode(std::vector& buffer) const { return LoadSectionExeFS(".code", buffer); } -/** - * Get the icon (typically icon section) of the application - * @param buffer Reference to buffer to store data - * @return ResultStatus result of function - */ ResultStatus AppLoader_NCCH::ReadIcon(std::vector& buffer) const { return LoadSectionExeFS("icon", buffer); } -/** - * Get the banner (typically banner section) of the application - * @param buffer Reference to buffer to store data - * @return ResultStatus result of function - */ ResultStatus AppLoader_NCCH::ReadBanner(std::vector& buffer) const { return LoadSectionExeFS("banner", buffer); } -/** - * Get the logo (typically logo section) of the application - * @param buffer Reference to buffer to store data - * @return ResultStatus result of function - */ ResultStatus AppLoader_NCCH::ReadLogo(std::vector& buffer) const { return LoadSectionExeFS("logo", buffer); } -/** - * Get the RomFS of the application - * @param buffer Reference to buffer to store data - * @return ResultStatus result of function - */ ResultStatus AppLoader_NCCH::ReadRomFS(std::vector& buffer) const { FileUtil::IOFile file(filename, "rb"); if (file.IsOpen()) { -- cgit v1.2.3 From 85030c6e6bfe83b6671de45b25535fe3ef713319 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 6 Jan 2015 19:49:25 +0000 Subject: Loader: Never forget to change is_loaded. --- src/core/loader/ncch.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/core/loader/ncch.cpp') diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index f1d01252e..0793cc7cd 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -102,7 +102,6 @@ static bool LZSS_Decompress(u8* compressed, u32 compressed_size, u8* decompresse /// AppLoader_NCCH constructor AppLoader_NCCH::AppLoader_NCCH(const std::string& filename) { this->filename = filename; - is_loaded = false; is_compressed = false; entry_point = 0; ncch_offset = 0; @@ -221,10 +220,10 @@ ResultStatus AppLoader_NCCH::Load() { file.Seek(exefs_offset + ncch_offset, 0); file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)); - is_loaded = true; // Set state to loaded - LoadExec(); // Load the executable into memory for booting + is_loaded = true; // Set state to loaded + return ResultStatus::Success; } else { LOG_ERROR(Loader, "Unable to read file %s!", filename.c_str()); -- cgit v1.2.3 From 2d63df90a9d64d3961be07ef0a959bc48b42c73b Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 6 Jan 2015 21:10:12 +0000 Subject: Loader: Initialize the default NCCH values in the class declaration, not in the constructor. --- src/core/loader/ncch.cpp | 4 ---- 1 file changed, 4 deletions(-) (limited to 'src/core/loader/ncch.cpp') diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index 0793cc7cd..883a0f753 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -102,10 +102,6 @@ static bool LZSS_Decompress(u8* compressed, u32 compressed_size, u8* decompresse /// AppLoader_NCCH constructor AppLoader_NCCH::AppLoader_NCCH(const std::string& filename) { this->filename = filename; - is_compressed = false; - entry_point = 0; - ncch_offset = 0; - exefs_offset = 0; } /// AppLoader_NCCH destructor -- cgit v1.2.3 From b5237e885df72f6c37532fc8af9573966e7b07e5 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 6 Jan 2015 21:30:40 +0000 Subject: Loader: Keep a reference to the file and pass it to the correct AppLoader, instead of loading it multiple times. --- src/core/loader/ncch.cpp | 190 +++++++++++++++++++++-------------------------- 1 file changed, 84 insertions(+), 106 deletions(-) (limited to 'src/core/loader/ncch.cpp') diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index 883a0f753..eca57d14b 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -4,8 +4,6 @@ #include -#include "common/file_util.h" - #include "core/loader/ncch.h" #include "core/hle/kernel/kernel.h" #include "core/mem_map.h" @@ -99,15 +97,6 @@ static bool LZSS_Decompress(u8* compressed, u32 compressed_size, u8* decompresse //////////////////////////////////////////////////////////////////////////////////////////////////// // AppLoader_NCCH class -/// AppLoader_NCCH constructor -AppLoader_NCCH::AppLoader_NCCH(const std::string& filename) { - this->filename = filename; -} - -/// AppLoader_NCCH destructor -AppLoader_NCCH::~AppLoader_NCCH() { -} - ResultStatus AppLoader_NCCH::LoadExec() const { if (!is_loaded) return ResultStatus::ErrorNotLoaded; @@ -123,108 +112,100 @@ ResultStatus AppLoader_NCCH::LoadExec() const { ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector& buffer) const { // Iterate through the ExeFs archive until we find the .code file... - FileUtil::IOFile file(filename, "rb"); - if (file.IsOpen()) { - LOG_DEBUG(Loader, "%d sections:", kMaxSections); - for (int i = 0; i < kMaxSections; i++) { - // Load the specified section... - if (strcmp((const char*)exefs_header.section[i].name, name) == 0) { - LOG_DEBUG(Loader, "%d - offset: 0x%08X, size: 0x%08X, name: %s", i, - exefs_header.section[i].offset, exefs_header.section[i].size, - exefs_header.section[i].name); - - s64 section_offset = (exefs_header.section[i].offset + exefs_offset + - sizeof(ExeFs_Header)+ncch_offset); - file.Seek(section_offset, 0); - - // Section is compressed... - if (i == 0 && is_compressed) { - // Read compressed .code section... - std::unique_ptr temp_buffer; - try { - temp_buffer.reset(new u8[exefs_header.section[i].size]); - } catch (std::bad_alloc&) { - return ResultStatus::ErrorMemoryAllocationFailed; - } - file.ReadBytes(&temp_buffer[0], exefs_header.section[i].size); - - // Decompress .code section... - u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], - exefs_header.section[i].size); - buffer.resize(decompressed_size); - if (!LZSS_Decompress(&temp_buffer[0], exefs_header.section[i].size, &buffer[0], - decompressed_size)) { - return ResultStatus::ErrorInvalidFormat; - } - // Section is uncompressed... + if (!file->IsOpen()) + return ResultStatus::Error; + + LOG_DEBUG(Loader, "%d sections:", kMaxSections); + for (int i = 0; i < kMaxSections; i++) { + // Load the specified section... + if (strcmp((const char*)exefs_header.section[i].name, name) == 0) { + LOG_DEBUG(Loader, "%d - offset: 0x%08X, size: 0x%08X, name: %s", i, + exefs_header.section[i].offset, exefs_header.section[i].size, + exefs_header.section[i].name); + + s64 section_offset = (exefs_header.section[i].offset + exefs_offset + + sizeof(ExeFs_Header)+ncch_offset); + file->Seek(section_offset, 0); + + // Section is compressed... + if (i == 0 && is_compressed) { + // Read compressed .code section... + std::unique_ptr temp_buffer; + try { + temp_buffer.reset(new u8[exefs_header.section[i].size]); + } catch (std::bad_alloc&) { + return ResultStatus::ErrorMemoryAllocationFailed; } - else { - buffer.resize(exefs_header.section[i].size); - file.ReadBytes(&buffer[0], exefs_header.section[i].size); + file->ReadBytes(&temp_buffer[0], exefs_header.section[i].size); + + // Decompress .code section... + u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], + exefs_header.section[i].size); + buffer.resize(decompressed_size); + if (!LZSS_Decompress(&temp_buffer[0], exefs_header.section[i].size, &buffer[0], + decompressed_size)) { + return ResultStatus::ErrorInvalidFormat; } - return ResultStatus::Success; + // Section is uncompressed... } + else { + buffer.resize(exefs_header.section[i].size); + file->ReadBytes(&buffer[0], exefs_header.section[i].size); + } + return ResultStatus::Success; } - } else { - LOG_ERROR(Loader, "Unable to read file %s!", filename.c_str()); - return ResultStatus::Error; } return ResultStatus::ErrorNotUsed; } ResultStatus AppLoader_NCCH::Load() { - LOG_INFO(Loader, "Loading NCCH file %s...", filename.c_str()); - if (is_loaded) return ResultStatus::ErrorAlreadyLoaded; - FileUtil::IOFile file(filename, "rb"); - if (file.IsOpen()) { - file.ReadBytes(&ncch_header, sizeof(NCCH_Header)); + if (!file->IsOpen()) + return ResultStatus::Error; - // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)... - if (0 == memcmp(&ncch_header.magic, "NCSD", 4)) { - LOG_WARNING(Loader, "Only loading the first (bootable) NCCH within the NCSD file!"); - ncch_offset = 0x4000; - file.Seek(ncch_offset, 0); - file.ReadBytes(&ncch_header, sizeof(NCCH_Header)); - } + file->ReadBytes(&ncch_header, sizeof(NCCH_Header)); - // Verify we are loading the correct file type... - if (0 != memcmp(&ncch_header.magic, "NCCH", 4)) - return ResultStatus::ErrorInvalidFormat; + // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)... + if (0 == memcmp(&ncch_header.magic, "NCSD", 4)) { + LOG_WARNING(Loader, "Only loading the first (bootable) NCCH within the NCSD file!"); + ncch_offset = 0x4000; + file->Seek(ncch_offset, 0); + file->ReadBytes(&ncch_header, sizeof(NCCH_Header)); + } - // Read ExHeader... + // Verify we are loading the correct file type... + if (0 != memcmp(&ncch_header.magic, "NCCH", 4)) + return ResultStatus::ErrorInvalidFormat; - file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)); + // Read ExHeader... - is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1; - entry_point = exheader_header.codeset_info.text.address; + file->ReadBytes(&exheader_header, sizeof(ExHeader_Header)); - LOG_INFO(Loader, "Name: %s", exheader_header.codeset_info.name); - LOG_DEBUG(Loader, "Code compressed: %s", is_compressed ? "yes" : "no"); - LOG_DEBUG(Loader, "Entry point: 0x%08X", entry_point); + is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1; + entry_point = exheader_header.codeset_info.text.address; - // Read ExeFS... + LOG_INFO(Loader, "Name: %s", exheader_header.codeset_info.name); + LOG_DEBUG(Loader, "Code compressed: %s", is_compressed ? "yes" : "no"); + LOG_DEBUG(Loader, "Entry point: 0x%08X", entry_point); - exefs_offset = ncch_header.exefs_offset * kBlockSize; - u32 exefs_size = ncch_header.exefs_size * kBlockSize; + // Read ExeFS... - LOG_DEBUG(Loader, "ExeFS offset: 0x%08X", exefs_offset); - LOG_DEBUG(Loader, "ExeFS size: 0x%08X", exefs_size); + exefs_offset = ncch_header.exefs_offset * kBlockSize; + u32 exefs_size = ncch_header.exefs_size * kBlockSize; - file.Seek(exefs_offset + ncch_offset, 0); - file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)); + LOG_DEBUG(Loader, "ExeFS offset: 0x%08X", exefs_offset); + LOG_DEBUG(Loader, "ExeFS size: 0x%08X", exefs_size); - LoadExec(); // Load the executable into memory for booting + file->Seek(exefs_offset + ncch_offset, 0); + file->ReadBytes(&exefs_header, sizeof(ExeFs_Header)); - is_loaded = true; // Set state to loaded + LoadExec(); // Load the executable into memory for booting - return ResultStatus::Success; - } else { - LOG_ERROR(Loader, "Unable to read file %s!", filename.c_str()); - } - return ResultStatus::Error; + is_loaded = true; // Set state to loaded + + return ResultStatus::Success; } ResultStatus AppLoader_NCCH::ReadCode(std::vector& buffer) const { @@ -244,29 +225,26 @@ ResultStatus AppLoader_NCCH::ReadLogo(std::vector& buffer) const { } ResultStatus AppLoader_NCCH::ReadRomFS(std::vector& buffer) const { - FileUtil::IOFile file(filename, "rb"); - if (file.IsOpen()) { - // Check if the NCCH has a RomFS... - if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) { - u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; - u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; + if (!file->IsOpen()) + return ResultStatus::Error; - LOG_DEBUG(Loader, "RomFS offset: 0x%08X", romfs_offset); - LOG_DEBUG(Loader, "RomFS size: 0x%08X", romfs_size); + // Check if the NCCH has a RomFS... + if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) { + u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; + u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; - buffer.resize(romfs_size); + LOG_DEBUG(Loader, "RomFS offset: 0x%08X", romfs_offset); + LOG_DEBUG(Loader, "RomFS size: 0x%08X", romfs_size); - file.Seek(romfs_offset, 0); - file.ReadBytes(&buffer[0], romfs_size); + buffer.resize(romfs_size); - return ResultStatus::Success; - } - LOG_DEBUG(Loader, "NCCH has no RomFS"); - return ResultStatus::ErrorNotUsed; - } else { - LOG_ERROR(Loader, "Unable to read file %s!", filename.c_str()); + file->Seek(romfs_offset, 0); + file->ReadBytes(&buffer[0], romfs_size); + + return ResultStatus::Success; } - return ResultStatus::Error; + LOG_DEBUG(Loader, "NCCH has no RomFS"); + return ResultStatus::ErrorNotUsed; } u64 AppLoader_NCCH::GetProgramId() const { -- cgit v1.2.3 From 04622a859cc748745cbbeb0b332f930085438077 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 6 Jan 2015 22:47:43 +0000 Subject: =?UTF-8?q?Loader:=20Don=E2=80=99t=20assume=20the=20file=20hasn?= =?UTF-8?q?=E2=80=99t=20been=20read=20before.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/loader/ncch.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/core/loader/ncch.cpp') diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index eca57d14b..edf53c2c0 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -125,7 +125,7 @@ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector& s64 section_offset = (exefs_header.section[i].offset + exefs_offset + sizeof(ExeFs_Header)+ncch_offset); - file->Seek(section_offset, 0); + file->Seek(section_offset, SEEK_SET); // Section is compressed... if (i == 0 && is_compressed) { @@ -165,13 +165,16 @@ ResultStatus AppLoader_NCCH::Load() { if (!file->IsOpen()) return ResultStatus::Error; + // Reset read pointer in case this file has been read before. + file->Seek(0, SEEK_SET); + file->ReadBytes(&ncch_header, sizeof(NCCH_Header)); // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)... if (0 == memcmp(&ncch_header.magic, "NCSD", 4)) { LOG_WARNING(Loader, "Only loading the first (bootable) NCCH within the NCSD file!"); ncch_offset = 0x4000; - file->Seek(ncch_offset, 0); + file->Seek(ncch_offset, SEEK_SET); file->ReadBytes(&ncch_header, sizeof(NCCH_Header)); } @@ -198,7 +201,7 @@ ResultStatus AppLoader_NCCH::Load() { LOG_DEBUG(Loader, "ExeFS offset: 0x%08X", exefs_offset); LOG_DEBUG(Loader, "ExeFS size: 0x%08X", exefs_size); - file->Seek(exefs_offset + ncch_offset, 0); + file->Seek(exefs_offset + ncch_offset, SEEK_SET); file->ReadBytes(&exefs_header, sizeof(ExeFs_Header)); LoadExec(); // Load the executable into memory for booting @@ -238,7 +241,7 @@ ResultStatus AppLoader_NCCH::ReadRomFS(std::vector& buffer) const { buffer.resize(romfs_size); - file->Seek(romfs_offset, 0); + file->Seek(romfs_offset, SEEK_SET); file->ReadBytes(&buffer[0], romfs_size); return ResultStatus::Success; -- cgit v1.2.3 From 82ec17db7df53ed1c376d1cdaa9a6587719a546d Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 6 Jan 2015 23:10:13 +0000 Subject: Loader: Guess filetype from the magic, or fallback to the extension. --- src/core/loader/ncch.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'src/core/loader/ncch.cpp') diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index edf53c2c0..d6eb549b7 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp @@ -97,6 +97,21 @@ static bool LZSS_Decompress(u8* compressed, u32 compressed_size, u8* decompresse //////////////////////////////////////////////////////////////////////////////////////////////////// // AppLoader_NCCH class +FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) { + u32 magic; + file.Seek(0x100, SEEK_SET); + if (1 != file.ReadArray(&magic, 1)) + return FileType::Error; + + if (MakeMagic('N', 'C', 'S', 'D') == magic) + return FileType::CCI; + + if (MakeMagic('N', 'C', 'C', 'H') == magic) + return FileType::CXI; + + return FileType::Error; +} + ResultStatus AppLoader_NCCH::LoadExec() const { if (!is_loaded) return ResultStatus::ErrorNotLoaded; @@ -171,7 +186,7 @@ ResultStatus AppLoader_NCCH::Load() { file->ReadBytes(&ncch_header, sizeof(NCCH_Header)); // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)... - if (0 == memcmp(&ncch_header.magic, "NCSD", 4)) { + if (MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) { LOG_WARNING(Loader, "Only loading the first (bootable) NCCH within the NCSD file!"); ncch_offset = 0x4000; file->Seek(ncch_offset, SEEK_SET); @@ -179,7 +194,7 @@ ResultStatus AppLoader_NCCH::Load() { } // Verify we are loading the correct file type... - if (0 != memcmp(&ncch_header.magic, "NCCH", 4)) + if (MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic) return ResultStatus::ErrorInvalidFormat; // Read ExHeader... -- cgit v1.2.3 From 08aaa3350066b5d506368d18dddba3f829c85b7a Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 7 Jan 2015 01:01:19 +0000 Subject: Loader: Clean up the NCCH AppLoader. --- src/core/loader/ncch.cpp | 99 +++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 51 deletions(-) (limited to 'src/core/loader/ncch.cpp') 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 @@ namespace Loader { -static const int kMaxSections = 8; ///< Maximum number of sections (files) in an ExeFs -static const int kBlockSize = 0x200; ///< Size of ExeFS blocks (in bytes) +static const int kMaxSections = 8; ///< Maximum number of sections (files) in an ExeFs +static const int kBlockSize = 0x200; ///< Size of ExeFS blocks (in bytes) /** * 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) * @param size Size of compressed buffer * @return Size of decompressed buffer */ -static u32 LZSS_GetDecompressedSize(u8* buffer, u32 size) { +static u32 LZSS_GetDecompressedSize(const u8* buffer, u32 size) { u32 offset_size = *(u32*)(buffer + size - 4); return offset_size + size; } @@ -35,9 +35,9 @@ static u32 LZSS_GetDecompressedSize(u8* buffer, u32 size) { * @param decompressed_size Size of decompressed buffer * @return True on success, otherwise false */ -static bool LZSS_Decompress(u8* compressed, u32 compressed_size, u8* decompressed, u32 decompressed_size) { - u8* footer = compressed + compressed_size - 8; - u32 buffer_top_and_bottom = *(u32*)footer; +static bool LZSS_Decompress(const u8* compressed, u32 compressed_size, u8* decompressed, u32 decompressed_size) { + const u8* footer = compressed + compressed_size - 8; + u32 buffer_top_and_bottom = *reinterpret_cast(footer); u32 out = decompressed_size; u32 index = compressed_size - ((buffer_top_and_bottom >> 24) & 0xFF); 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 memset(decompressed, 0, decompressed_size); memcpy(decompressed, compressed, compressed_size); - while(index > stop_index) { + while (index > stop_index) { u8 control = compressed[--index]; - for(u32 i = 0; i < 8; i++) { - if(index <= stop_index) + for (unsigned i = 0; i < 8; i++) { + if (index <= stop_index) break; - if(index <= 0) + if (index <= 0) break; - if(out <= 0) + if (out <= 0) break; - if(control & 0x80) { + if (control & 0x80) { // Check if compression is out of bounds - if(index < 2) { + if (index < 2) return false; - } index -= 2; u32 segment_offset = compressed[index] | (compressed[index + 1] << 8); @@ -69,23 +68,21 @@ static bool LZSS_Decompress(u8* compressed, u32 compressed_size, u8* decompresse segment_offset += 2; // Check if compression is out of bounds - if(out < segment_size) { + if (out < segment_size) return false; - } - for(u32 j = 0; j < segment_size; j++) { + + for (unsigned j = 0; j < segment_size; j++) { // Check if compression is out of bounds - if(out + segment_offset >= decompressed_size) { + if (out + segment_offset >= decompressed_size) return false; - } - u8 data = decompressed[out + segment_offset]; + u8 data = decompressed[out + segment_offset]; decompressed[--out] = data; } } else { // Check if compression is out of bounds - if(out < 1) { + if (out < 1) return false; - } decompressed[--out] = compressed[--index]; } control <<= 1; @@ -126,46 +123,44 @@ ResultStatus AppLoader_NCCH::LoadExec() const { } ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector& buffer) const { - // Iterate through the ExeFs archive until we find the .code file... if (!file->IsOpen()) return ResultStatus::Error; LOG_DEBUG(Loader, "%d sections:", kMaxSections); - for (int i = 0; i < kMaxSections; i++) { + // Iterate through the ExeFs archive until we find the .code file... + for (unsigned section_number = 0; section_number < kMaxSections; section_number++) { + const auto& section = exefs_header.section[section_number]; + // Load the specified section... - if (strcmp((const char*)exefs_header.section[i].name, name) == 0) { - LOG_DEBUG(Loader, "%d - offset: 0x%08X, size: 0x%08X, name: %s", i, - exefs_header.section[i].offset, exefs_header.section[i].size, - exefs_header.section[i].name); + if (strcmp(section.name, name) == 0) { + LOG_DEBUG(Loader, "%d - offset: 0x%08X, size: 0x%08X, name: %s", section_number, + section.offset, section.size, section.name); - s64 section_offset = (exefs_header.section[i].offset + exefs_offset + - sizeof(ExeFs_Header)+ncch_offset); + s64 section_offset = (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset); file->Seek(section_offset, SEEK_SET); - // Section is compressed... - if (i == 0 && is_compressed) { - // Read compressed .code section... + if (is_compressed) { + // Section is compressed, read compressed .code section... std::unique_ptr temp_buffer; try { - temp_buffer.reset(new u8[exefs_header.section[i].size]); + temp_buffer.reset(new u8[section.size]); } catch (std::bad_alloc&) { return ResultStatus::ErrorMemoryAllocationFailed; } - file->ReadBytes(&temp_buffer[0], exefs_header.section[i].size); + + if (file->ReadBytes(&temp_buffer[0], section.size) != section.size) + return ResultStatus::Error; // Decompress .code section... - u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], - exefs_header.section[i].size); + u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], section.size); buffer.resize(decompressed_size); - if (!LZSS_Decompress(&temp_buffer[0], exefs_header.section[i].size, &buffer[0], - decompressed_size)) { + if (!LZSS_Decompress(&temp_buffer[0], section.size, &buffer[0], decompressed_size)) return ResultStatus::ErrorInvalidFormat; - } + } else { // Section is uncompressed... - } - else { - buffer.resize(exefs_header.section[i].size); - file->ReadBytes(&buffer[0], exefs_header.section[i].size); + buffer.resize(section.size); + if (file->ReadBytes(&buffer[0], section.size) != section.size) + return ResultStatus::Error; } return ResultStatus::Success; } @@ -183,7 +178,8 @@ ResultStatus AppLoader_NCCH::Load() { // Reset read pointer in case this file has been read before. file->Seek(0, SEEK_SET); - file->ReadBytes(&ncch_header, sizeof(NCCH_Header)); + if (file->ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header)) + return ResultStatus::Error; // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)... if (MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) { @@ -199,7 +195,8 @@ ResultStatus AppLoader_NCCH::Load() { // Read ExHeader... - file->ReadBytes(&exheader_header, sizeof(ExHeader_Header)); + if (file->ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header)) + return ResultStatus::Error; is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1; entry_point = exheader_header.codeset_info.text.address; @@ -217,13 +214,12 @@ ResultStatus AppLoader_NCCH::Load() { LOG_DEBUG(Loader, "ExeFS size: 0x%08X", exefs_size); file->Seek(exefs_offset + ncch_offset, SEEK_SET); - file->ReadBytes(&exefs_header, sizeof(ExeFs_Header)); - - LoadExec(); // Load the executable into memory for booting + if (file->ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header)) + return ResultStatus::Error; is_loaded = true; // Set state to loaded - return ResultStatus::Success; + return LoadExec(); // Load the executable into memory for booting } ResultStatus AppLoader_NCCH::ReadCode(std::vector& buffer) const { @@ -257,7 +253,8 @@ ResultStatus AppLoader_NCCH::ReadRomFS(std::vector& buffer) const { buffer.resize(romfs_size); file->Seek(romfs_offset, SEEK_SET); - file->ReadBytes(&buffer[0], romfs_size); + if (file->ReadBytes(&buffer[0], romfs_size) != romfs_size) + return ResultStatus::Error; return ResultStatus::Success; } -- cgit v1.2.3