summaryrefslogtreecommitdiffstats
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.cpp172
1 files changed, 90 insertions, 82 deletions
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp
index 60505bdfa..ba27eb75a 100644
--- a/src/core/loader/ncch.cpp
+++ b/src/core/loader/ncch.cpp
@@ -113,76 +113,80 @@ AppLoader_NCCH::AppLoader_NCCH(const std::string& filename) {
/// AppLoader_NCCH destructor
AppLoader_NCCH::~AppLoader_NCCH() {
- if (file.IsOpen())
- file.Close();
}
/**
* Loads .code section into memory for booting
* @return ResultStatus result of function
*/
-ResultStatus AppLoader_NCCH::LoadExec() {
+ResultStatus AppLoader_NCCH::LoadExec() const {
if (!is_loaded)
return ResultStatus::ErrorNotLoaded;
- ResultStatus res;
- code = ReadCode(res);
-
- if (ResultStatus::Success == res) {
+ std::vector<u8> code;
+ if (ResultStatus::Success == ReadCode(code)) {
Memory::WriteBlock(entry_point, &code[0], code.size());
Kernel::LoadExec(entry_point);
+ return ResultStatus::Success;
}
- return res;
+ 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
- * @param error ResultStatus result of function
- * @return Reference to buffer of data that was read
+ * @return ResultStatus result of function
*/
-const std::vector<u8>& AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer,
- ResultStatus& error) {
+ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const {
// Iterate through the ExeFs archive until we find the .code file...
- for (int i = 0; i < kMaxSections; i++) {
- // Load the specified section...
- if (strcmp((const char*)exefs_header.section[i].name, name) == 0) {
- INFO_LOG(LOADER, "ExeFS section %d:", i);
- INFO_LOG(LOADER, " name: %s", exefs_header.section[i].name);
- INFO_LOG(LOADER, " offset: 0x%08X", exefs_header.section[i].offset);
- INFO_LOG(LOADER, " size: 0x%08X", exefs_header.section[i].size);
-
- 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<u8[]> temp_buffer(new u8[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)) {
- error = ResultStatus::ErrorInvalidFormat;
- return buffer;
+ File::IOFile file(filename, "rb");
+ if (file.IsOpen()) {
+ for (int i = 0; i < kMaxSections; i++) {
+ // Load the specified section...
+ if (strcmp((const char*)exefs_header.section[i].name, name) == 0) {
+ INFO_LOG(LOADER, "ExeFS section %d:", i);
+ INFO_LOG(LOADER, " name: %s", exefs_header.section[i].name);
+ INFO_LOG(LOADER, " offset: 0x%08X", exefs_header.section[i].offset);
+ INFO_LOG(LOADER, " size: 0x%08X", exefs_header.section[i].size);
+
+ 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<u8[]> 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...
}
- // Section is uncompressed...
- } else {
- buffer.resize(exefs_header.section[i].size);
- file.ReadBytes(&buffer[0], exefs_header.section[i].size);
+ else {
+ buffer.resize(exefs_header.section[i].size);
+ file.ReadBytes(&buffer[0], exefs_header.section[i].size);
+ }
+ return ResultStatus::Success;
}
- error = ResultStatus::Success;
- return buffer;
}
+ } else {
+ ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str());
+ return ResultStatus::Error;
}
- error = ResultStatus::ErrorNotUsed;
- return buffer;
+ return ResultStatus::ErrorNotUsed;
}
/**
@@ -197,8 +201,7 @@ ResultStatus AppLoader_NCCH::Load() {
if (is_loaded)
return ResultStatus::ErrorAlreadyLoaded;
- file = File::IOFile(filename, "rb");
-
+ File::IOFile file(filename, "rb");
if (file.IsOpen()) {
file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
@@ -241,72 +244,77 @@ ResultStatus AppLoader_NCCH::Load() {
LoadExec(); // Load the executable into memory for booting
return ResultStatus::Success;
+ } else {
+ ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str());
}
return ResultStatus::Error;
}
/**
* Get the code (typically .code section) of the application
- * @param error ResultStatus result of function
- * @return Reference to code buffer
+ * @param buffer Reference to buffer to store data
+ * @return ResultStatus result of function
*/
-const std::vector<u8>& AppLoader_NCCH::ReadCode(ResultStatus& error) {
- return LoadSectionExeFS(".code", code, error);
+ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) const {
+ return LoadSectionExeFS(".code", buffer);
}
/**
* Get the icon (typically icon section) of the application
- * @param error ResultStatus result of function
- * @return Reference to icon buffer
+ * @param buffer Reference to buffer to store data
+ * @return ResultStatus result of function
*/
-const std::vector<u8>& AppLoader_NCCH::ReadIcon(ResultStatus& error) {
- return LoadSectionExeFS("icon", icon, error);
+ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) const {
+ return LoadSectionExeFS("icon", buffer);
}
/**
* Get the banner (typically banner section) of the application
- * @param error ResultStatus result of function
- * @return Reference to banner buffer
+ * @param buffer Reference to buffer to store data
+ * @return ResultStatus result of function
*/
-const std::vector<u8>& AppLoader_NCCH::ReadBanner(ResultStatus& error) {
- return LoadSectionExeFS("banner", banner, error);
+ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) const {
+ return LoadSectionExeFS("banner", buffer);
}
/**
* Get the logo (typically logo section) of the application
- * @param error ResultStatus result of function
- * @return Reference to logo buffer
+ * @param buffer Reference to buffer to store data
+ * @return ResultStatus result of function
*/
-const std::vector<u8>& AppLoader_NCCH::ReadLogo(ResultStatus& error) {
- return LoadSectionExeFS("logo", logo, error);
+ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) const {
+ return LoadSectionExeFS("logo", buffer);
}
/**
- * Get the RomFs archive of the application
- * @param error ResultStatus result of function
- * @return Reference to RomFs archive buffer
+ * Get the RomFS of the application
+ * @param buffer Reference to buffer to store data
+ * @return ResultStatus result of function
*/
-const std::vector<u8>& AppLoader_NCCH::ReadRomFS(ResultStatus& error) {
- // 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;
+ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const {
+ File::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;
- INFO_LOG(LOADER, "RomFS offset: 0x%08X", romfs_offset);
- INFO_LOG(LOADER, "RomFS size: 0x%08X", romfs_size);
+ INFO_LOG(LOADER, "RomFS offset: 0x%08X", romfs_offset);
+ INFO_LOG(LOADER, "RomFS size: 0x%08X", romfs_size);
- romfs.resize(romfs_size);
+ buffer.resize(romfs_size);
- file.Seek(romfs_offset, 0);
- file.ReadBytes(&romfs[0], romfs_size);
+ file.Seek(romfs_offset, 0);
+ file.ReadBytes(&buffer[0], romfs_size);
- error = ResultStatus::Success;
- return romfs;
- } else {
+ return ResultStatus::Success;
+ }
NOTICE_LOG(LOADER, "RomFS unused");
+ return ResultStatus::ErrorNotUsed;
+ } else {
+ ERROR_LOG(LOADER, "Unable to read file %s!", filename.c_str());
}
- error = ResultStatus::ErrorNotUsed;
- return romfs;
+ return ResultStatus::Error;
}
} // namespace Loader