2014-06-17 02:57:09 +00:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 05:38:14 +00:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-06-17 02:57:09 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2014-06-18 22:58:09 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2014-06-17 02:57:09 +00:00
|
|
|
#include "core/loader/ncch.h"
|
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/mem_map.h"
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Loader namespace
|
|
|
|
|
|
|
|
namespace Loader {
|
|
|
|
|
2014-06-19 03:53:22 +00:00
|
|
|
static const int kMaxSections = 8; ///< Maximum number of sections (files) in an ExeFs
|
|
|
|
static const int kBlockSize = 0x200; ///< Size of ExeFS blocks (in bytes)
|
2014-06-17 02:57:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the decompressed size of an LZSS compressed ExeFS file
|
|
|
|
* @param buffer Buffer of compressed file
|
|
|
|
* @param size Size of compressed buffer
|
|
|
|
* @return Size of decompressed buffer
|
|
|
|
*/
|
2014-11-17 03:58:39 +00:00
|
|
|
static u32 LZSS_GetDecompressedSize(u8* buffer, u32 size) {
|
2014-06-17 02:57:09 +00:00
|
|
|
u32 offset_size = *(u32*)(buffer + size - 4);
|
|
|
|
return offset_size + size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decompress ExeFS file (compressed with LZSS)
|
|
|
|
* @param compressed Compressed buffer
|
|
|
|
* @param compressed_size Size of compressed buffer
|
|
|
|
* @param decompressed Decompressed buffer
|
|
|
|
* @param decompressed_size Size of decompressed buffer
|
|
|
|
* @return True on success, otherwise false
|
|
|
|
*/
|
2014-11-17 03:58:39 +00:00
|
|
|
static bool LZSS_Decompress(u8* compressed, u32 compressed_size, u8* decompressed, u32 decompressed_size) {
|
2014-06-17 02:57:09 +00:00
|
|
|
u8* footer = compressed + compressed_size - 8;
|
|
|
|
u32 buffer_top_and_bottom = *(u32*)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);
|
|
|
|
|
|
|
|
memset(decompressed, 0, decompressed_size);
|
|
|
|
memcpy(decompressed, compressed, compressed_size);
|
|
|
|
|
|
|
|
while(index > stop_index) {
|
2014-09-06 17:36:49 +00:00
|
|
|
u8 control = compressed[--index];
|
2014-06-17 02:57:09 +00:00
|
|
|
|
2014-09-06 17:36:49 +00:00
|
|
|
for(u32 i = 0; i < 8; i++) {
|
2014-06-17 02:57:09 +00:00
|
|
|
if(index <= stop_index)
|
|
|
|
break;
|
|
|
|
if(index <= 0)
|
|
|
|
break;
|
|
|
|
if(out <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(control & 0x80) {
|
2014-06-18 22:58:09 +00:00
|
|
|
// Check if compression is out of bounds
|
2014-06-17 02:57:09 +00:00
|
|
|
if(index < 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
index -= 2;
|
|
|
|
|
|
|
|
u32 segment_offset = compressed[index] | (compressed[index + 1] << 8);
|
|
|
|
u32 segment_size = ((segment_offset >> 12) & 15) + 3;
|
|
|
|
segment_offset &= 0x0FFF;
|
|
|
|
segment_offset += 2;
|
|
|
|
|
2014-06-18 22:58:09 +00:00
|
|
|
// Check if compression is out of bounds
|
2014-06-17 02:57:09 +00:00
|
|
|
if(out < segment_size) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-06 17:36:49 +00:00
|
|
|
for(u32 j = 0; j < segment_size; j++) {
|
2014-06-18 22:58:09 +00:00
|
|
|
// Check if compression is out of bounds
|
2014-06-17 02:57:09 +00:00
|
|
|
if(out + segment_offset >= decompressed_size) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-06 17:36:49 +00:00
|
|
|
|
|
|
|
u8 data = decompressed[out + segment_offset];
|
2014-06-17 02:57:09 +00:00
|
|
|
decompressed[--out] = data;
|
|
|
|
}
|
|
|
|
} else {
|
2014-06-18 22:58:09 +00:00
|
|
|
// Check if compression is out of bounds
|
2014-06-17 02:57:09 +00:00
|
|
|
if(out < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
decompressed[--out] = compressed[--index];
|
|
|
|
}
|
|
|
|
control <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-18 22:58:09 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// AppLoader_NCCH class
|
|
|
|
|
2014-07-04 17:11:09 +00:00
|
|
|
ResultStatus AppLoader_NCCH::LoadExec() const {
|
2014-11-19 08:49:13 +00:00
|
|
|
if (!is_loaded)
|
2014-06-18 22:58:09 +00:00
|
|
|
return ResultStatus::ErrorNotLoaded;
|
2014-06-17 02:57:09 +00:00
|
|
|
|
2014-06-27 19:33:23 +00:00
|
|
|
std::vector<u8> code;
|
|
|
|
if (ResultStatus::Success == ReadCode(code)) {
|
2014-06-22 19:40:21 +00:00
|
|
|
Memory::WriteBlock(entry_point, &code[0], code.size());
|
|
|
|
Kernel::LoadExec(entry_point);
|
2014-06-27 19:33:23 +00:00
|
|
|
return ResultStatus::Success;
|
2014-06-17 02:57:09 +00:00
|
|
|
}
|
2014-06-27 19:33:23 +00:00
|
|
|
return ResultStatus::Error;
|
2014-06-17 02:57:09 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 17:11:09 +00:00
|
|
|
ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const {
|
2014-06-18 22:58:09 +00:00
|
|
|
// Iterate through the ExeFs archive until we find the .code file...
|
2015-01-06 21:30:40 +00:00
|
|
|
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);
|
2015-01-06 22:47:43 +00:00
|
|
|
file->Seek(section_offset, SEEK_SET);
|
2015-01-06 21:30:40 +00:00
|
|
|
|
|
|
|
// 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;
|
2014-06-18 22:58:09 +00:00
|
|
|
}
|
2015-01-06 21:30:40 +00:00
|
|
|
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;
|
2014-07-04 17:11:09 +00:00
|
|
|
}
|
2015-01-06 21:30:40 +00:00
|
|
|
// Section is uncompressed...
|
2014-06-18 22:58:09 +00:00
|
|
|
}
|
2015-01-06 21:30:40 +00:00
|
|
|
else {
|
|
|
|
buffer.resize(exefs_header.section[i].size);
|
|
|
|
file->ReadBytes(&buffer[0], exefs_header.section[i].size);
|
|
|
|
}
|
|
|
|
return ResultStatus::Success;
|
2014-06-18 22:58:09 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-27 19:33:23 +00:00
|
|
|
return ResultStatus::ErrorNotUsed;
|
2014-11-19 08:49:13 +00:00
|
|
|
}
|
2014-06-18 22:58:09 +00:00
|
|
|
|
2014-06-19 21:46:05 +00:00
|
|
|
ResultStatus AppLoader_NCCH::Load() {
|
2014-06-18 22:58:09 +00:00
|
|
|
if (is_loaded)
|
|
|
|
return ResultStatus::ErrorAlreadyLoaded;
|
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
if (!file->IsOpen())
|
|
|
|
return ResultStatus::Error;
|
2014-06-17 02:57:09 +00:00
|
|
|
|
2015-01-06 22:47:43 +00:00
|
|
|
// Reset read pointer in case this file has been read before.
|
|
|
|
file->Seek(0, SEEK_SET);
|
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
file->ReadBytes(&ncch_header, sizeof(NCCH_Header));
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
// 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;
|
2015-01-06 22:47:43 +00:00
|
|
|
file->Seek(ncch_offset, SEEK_SET);
|
2015-01-06 21:30:40 +00:00
|
|
|
file->ReadBytes(&ncch_header, sizeof(NCCH_Header));
|
|
|
|
}
|
2014-06-18 22:58:09 +00:00
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
// Verify we are loading the correct file type...
|
|
|
|
if (0 != memcmp(&ncch_header.magic, "NCCH", 4))
|
|
|
|
return ResultStatus::ErrorInvalidFormat;
|
2014-06-19 03:53:22 +00:00
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
// Read ExHeader...
|
2014-06-17 02:57:09 +00:00
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
file->ReadBytes(&exheader_header, sizeof(ExHeader_Header));
|
2014-06-17 02:57:09 +00:00
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1;
|
|
|
|
entry_point = exheader_header.codeset_info.text.address;
|
2014-06-17 02:57:09 +00:00
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
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);
|
2014-06-19 03:53:22 +00:00
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
// Read ExeFS...
|
2014-06-17 02:57:09 +00:00
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
exefs_offset = ncch_header.exefs_offset * kBlockSize;
|
|
|
|
u32 exefs_size = ncch_header.exefs_size * kBlockSize;
|
2014-06-17 02:57:09 +00:00
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
LOG_DEBUG(Loader, "ExeFS offset: 0x%08X", exefs_offset);
|
|
|
|
LOG_DEBUG(Loader, "ExeFS size: 0x%08X", exefs_size);
|
2014-06-17 02:57:09 +00:00
|
|
|
|
2015-01-06 22:47:43 +00:00
|
|
|
file->Seek(exefs_offset + ncch_offset, SEEK_SET);
|
2015-01-06 21:30:40 +00:00
|
|
|
file->ReadBytes(&exefs_header, sizeof(ExeFs_Header));
|
2014-06-18 22:58:09 +00:00
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
LoadExec(); // Load the executable into memory for booting
|
2015-01-06 19:49:25 +00:00
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
is_loaded = true; // Set state to loaded
|
|
|
|
|
|
|
|
return ResultStatus::Success;
|
2014-06-17 02:57:09 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 17:11:09 +00:00
|
|
|
ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) const {
|
2014-06-27 19:33:23 +00:00
|
|
|
return LoadSectionExeFS(".code", buffer);
|
2014-06-22 19:40:21 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 17:11:09 +00:00
|
|
|
ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) const {
|
2014-06-27 19:33:23 +00:00
|
|
|
return LoadSectionExeFS("icon", buffer);
|
2014-06-22 19:40:21 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 17:11:09 +00:00
|
|
|
ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) const {
|
2014-06-27 19:33:23 +00:00
|
|
|
return LoadSectionExeFS("banner", buffer);
|
2014-06-22 19:40:21 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 17:11:09 +00:00
|
|
|
ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) const {
|
2014-06-27 19:33:23 +00:00
|
|
|
return LoadSectionExeFS("logo", buffer);
|
2014-06-22 19:40:21 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 17:11:09 +00:00
|
|
|
ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const {
|
2015-01-06 21:30:40 +00:00
|
|
|
if (!file->IsOpen())
|
|
|
|
return ResultStatus::Error;
|
2014-06-22 19:40:21 +00:00
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
// 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;
|
2014-06-22 19:40:21 +00:00
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
LOG_DEBUG(Loader, "RomFS offset: 0x%08X", romfs_offset);
|
|
|
|
LOG_DEBUG(Loader, "RomFS size: 0x%08X", romfs_size);
|
2014-06-22 19:40:21 +00:00
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
buffer.resize(romfs_size);
|
2014-06-22 19:40:21 +00:00
|
|
|
|
2015-01-06 22:47:43 +00:00
|
|
|
file->Seek(romfs_offset, SEEK_SET);
|
2015-01-06 21:30:40 +00:00
|
|
|
file->ReadBytes(&buffer[0], romfs_size);
|
|
|
|
|
|
|
|
return ResultStatus::Success;
|
2014-06-22 19:40:21 +00:00
|
|
|
}
|
2015-01-06 21:30:40 +00:00
|
|
|
LOG_DEBUG(Loader, "NCCH has no RomFS");
|
|
|
|
return ResultStatus::ErrorNotUsed;
|
2014-06-22 19:40:21 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 05:33:41 +00:00
|
|
|
u64 AppLoader_NCCH::GetProgramId() const {
|
|
|
|
return *reinterpret_cast<u64 const*>(&ncch_header.program_id[0]);
|
|
|
|
}
|
|
|
|
|
2014-06-17 02:57:09 +00:00
|
|
|
} // namespace Loader
|