|
@@ -4,8 +4,6 @@
|
|
|
|
|
|
|
|
#include <memory>
|
|
#include <memory>
|
|
|
|
|
|
|
|
-#include "common/file_util.h"
|
|
|
|
|
-
|
|
|
|
|
#include "core/loader/ncch.h"
|
|
#include "core/loader/ncch.h"
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
#include "core/mem_map.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 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 {
|
|
ResultStatus AppLoader_NCCH::LoadExec() const {
|
|
|
if (!is_loaded)
|
|
if (!is_loaded)
|
|
|
return ResultStatus::ErrorNotLoaded;
|
|
return ResultStatus::ErrorNotLoaded;
|
|
@@ -123,108 +112,100 @@ ResultStatus AppLoader_NCCH::LoadExec() const {
|
|
|
|
|
|
|
|
ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const {
|
|
ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const {
|
|
|
// Iterate through the ExeFs archive until we find the .code file...
|
|
// 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<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...
|
|
|
|
|
|
|
+ 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<u8[]> 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;
|
|
return ResultStatus::ErrorNotUsed;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
ResultStatus AppLoader_NCCH::Load() {
|
|
ResultStatus AppLoader_NCCH::Load() {
|
|
|
- LOG_INFO(Loader, "Loading NCCH file %s...", filename.c_str());
|
|
|
|
|
-
|
|
|
|
|
if (is_loaded)
|
|
if (is_loaded)
|
|
|
return ResultStatus::ErrorAlreadyLoaded;
|
|
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<u8>& buffer) const {
|
|
ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) const {
|
|
@@ -244,29 +225,26 @@ ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) const {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const {
|
|
ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& 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 {
|
|
u64 AppLoader_NCCH::GetProgramId() const {
|