2014-06-17 06:57:09 +04:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2014-06-19 02:58:09 +04:00
|
|
|
#include <memory>
|
|
|
|
|
2014-06-17 06:57:09 +04:00
|
|
|
#include "common/file_util.h"
|
|
|
|
|
|
|
|
#include "core/loader/ncch.h"
|
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/mem_map.h"
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Loader namespace
|
|
|
|
|
|
|
|
namespace Loader {
|
|
|
|
|
2014-06-19 07:53:22 +04: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 06:57:09 +04: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
|
|
|
|
*/
|
|
|
|
u32 LZSS_GetDecompressedSize(u8* buffer, u32 size) {
|
|
|
|
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-06-19 02:58:09 +04:00
|
|
|
bool LZSS_Decompress(u8* compressed, u32 compressed_size, u8* decompressed, u32 decompressed_size) {
|
2014-06-17 06:57:09 +04:00
|
|
|
u8* footer = compressed + compressed_size - 8;
|
|
|
|
u32 buffer_top_and_bottom = *(u32*)footer;
|
|
|
|
u32 i, j;
|
|
|
|
u32 out = decompressed_size;
|
|
|
|
u32 index = compressed_size - ((buffer_top_and_bottom >> 24) & 0xFF);
|
|
|
|
u8 control;
|
|
|
|
u32 stop_index = compressed_size - (buffer_top_and_bottom & 0xFFFFFF);
|
|
|
|
|
|
|
|
memset(decompressed, 0, decompressed_size);
|
|
|
|
memcpy(decompressed, compressed, compressed_size);
|
|
|
|
|
|
|
|
while(index > stop_index) {
|
|
|
|
control = compressed[--index];
|
|
|
|
|
|
|
|
for(i = 0; i < 8; i++) {
|
|
|
|
if(index <= stop_index)
|
|
|
|
break;
|
|
|
|
if(index <= 0)
|
|
|
|
break;
|
|
|
|
if(out <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(control & 0x80) {
|
2014-06-19 02:58:09 +04:00
|
|
|
// Check if compression is out of bounds
|
2014-06-17 06:57:09 +04: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-19 02:58:09 +04:00
|
|
|
// Check if compression is out of bounds
|
2014-06-17 06:57:09 +04:00
|
|
|
if(out < segment_size) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for(j = 0; j < segment_size; j++) {
|
|
|
|
u8 data;
|
2014-06-19 02:58:09 +04:00
|
|
|
// Check if compression is out of bounds
|
2014-06-17 06:57:09 +04:00
|
|
|
if(out + segment_offset >= decompressed_size) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
data = decompressed[out + segment_offset];
|
|
|
|
decompressed[--out] = data;
|
|
|
|
}
|
|
|
|
} else {
|
2014-06-19 02:58:09 +04:00
|
|
|
// Check if compression is out of bounds
|
2014-06-17 06:57:09 +04:00
|
|
|
if(out < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
decompressed[--out] = compressed[--index];
|
|
|
|
}
|
|
|
|
control <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-19 02:58:09 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// AppLoader_NCCH class
|
|
|
|
|
|
|
|
/// AppLoader_NCCH constructor
|
2014-06-20 01:46:05 +04:00
|
|
|
AppLoader_NCCH::AppLoader_NCCH(const std::string& filename) {
|
2014-06-19 02:58:09 +04:00
|
|
|
this->filename = filename;
|
|
|
|
is_loaded = false;
|
|
|
|
is_compressed = false;
|
|
|
|
entry_point = 0;
|
|
|
|
ncch_offset = 0;
|
|
|
|
exefs_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AppLoader_NCCH destructor
|
|
|
|
AppLoader_NCCH::~AppLoader_NCCH() {
|
2014-06-22 23:40:21 +04:00
|
|
|
if (file.IsOpen())
|
|
|
|
file.Close();
|
2014-06-19 02:58:09 +04:00
|
|
|
}
|
|
|
|
|
2014-06-17 06:57:09 +04:00
|
|
|
/**
|
2014-06-19 02:58:09 +04:00
|
|
|
* Loads .code section into memory for booting
|
|
|
|
* @return ResultStatus result of function
|
2014-06-17 06:57:09 +04:00
|
|
|
*/
|
2014-06-22 23:40:21 +04:00
|
|
|
ResultStatus AppLoader_NCCH::LoadExec() {
|
2014-06-19 02:58:09 +04:00
|
|
|
if (!is_loaded)
|
|
|
|
return ResultStatus::ErrorNotLoaded;
|
2014-06-17 06:57:09 +04:00
|
|
|
|
2014-06-27 23:33:23 +04:00
|
|
|
std::vector<u8> code;
|
|
|
|
if (ResultStatus::Success == ReadCode(code)) {
|
2014-06-22 23:40:21 +04:00
|
|
|
Memory::WriteBlock(entry_point, &code[0], code.size());
|
|
|
|
Kernel::LoadExec(entry_point);
|
2014-06-27 23:33:23 +04:00
|
|
|
return ResultStatus::Success;
|
2014-06-17 06:57:09 +04:00
|
|
|
}
|
2014-06-27 23:33:23 +04:00
|
|
|
return ResultStatus::Error;
|
2014-06-17 06:57:09 +04:00
|
|
|
}
|
|
|
|
|
2014-06-19 02:58:09 +04:00
|
|
|
/**
|
2014-06-19 07:53:22 +04:00
|
|
|
* Reads an application ExeFS section of an NCCH file into AppLoader (e.g. .code, .logo, etc.)
|
2014-06-19 02:58:09 +04:00
|
|
|
* @param name Name of section to read out of NCCH file
|
2014-06-22 23:40:21 +04:00
|
|
|
* @param buffer Vector to read data into
|
2014-06-27 23:33:23 +04:00
|
|
|
* @return ResultStatus result of function
|
2014-06-19 02:58:09 +04:00
|
|
|
*/
|
2014-06-27 23:33:23 +04:00
|
|
|
ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) {
|
2014-06-19 02:58:09 +04:00
|
|
|
// Iterate through the ExeFs archive until we find the .code file...
|
2014-06-19 07:53:22 +04:00
|
|
|
for (int i = 0; i < kMaxSections; i++) {
|
|
|
|
// Load the specified section...
|
2014-06-19 02:58:09 +04:00
|
|
|
if (strcmp((const char*)exefs_header.section[i].name, name) == 0) {
|
2014-06-19 07:53:22 +04:00
|
|
|
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);
|
|
|
|
|
2014-06-19 02:58:09 +04:00
|
|
|
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...
|
2014-06-22 23:40:21 +04:00
|
|
|
u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0],
|
|
|
|
exefs_header.section[i].size);
|
2014-06-19 02:58:09 +04:00
|
|
|
buffer.resize(decompressed_size);
|
2014-06-22 23:40:21 +04:00
|
|
|
if (!LZSS_Decompress(&temp_buffer[0], exefs_header.section[i].size, &buffer[0],
|
2014-06-19 02:58:09 +04:00
|
|
|
decompressed_size)) {
|
2014-06-27 23:33:23 +04:00
|
|
|
return ResultStatus::ErrorInvalidFormat;
|
2014-06-19 02:58:09 +04:00
|
|
|
}
|
|
|
|
// Section is uncompressed...
|
|
|
|
} else {
|
|
|
|
buffer.resize(exefs_header.section[i].size);
|
|
|
|
file.ReadBytes(&buffer[0], exefs_header.section[i].size);
|
|
|
|
}
|
2014-06-27 23:33:23 +04:00
|
|
|
return ResultStatus::Success;
|
2014-06-19 02:58:09 +04:00
|
|
|
}
|
|
|
|
}
|
2014-06-27 23:33:23 +04:00
|
|
|
return ResultStatus::ErrorNotUsed;
|
2014-06-19 02:58:09 +04:00
|
|
|
}
|
|
|
|
|
2014-06-17 06:57:09 +04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2014-06-20 01:46:05 +04:00
|
|
|
ResultStatus AppLoader_NCCH::Load() {
|
2014-06-17 06:57:09 +04:00
|
|
|
INFO_LOG(LOADER, "Loading NCCH file %s...", filename.c_str());
|
|
|
|
|
2014-06-19 02:58:09 +04:00
|
|
|
if (is_loaded)
|
|
|
|
return ResultStatus::ErrorAlreadyLoaded;
|
|
|
|
|
2014-06-22 23:40:21 +04:00
|
|
|
file = File::IOFile(filename, "rb");
|
2014-06-17 06:57:09 +04:00
|
|
|
|
|
|
|
if (file.IsOpen()) {
|
|
|
|
file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
|
|
|
|
|
|
|
|
// Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)...
|
2014-06-19 02:58:09 +04:00
|
|
|
if (0 == memcmp(&ncch_header.magic, "NCSD", 4)) {
|
2014-06-17 06:57:09 +04:00
|
|
|
WARN_LOG(LOADER, "Only loading the first (bootable) NCCH within the NCSD file!");
|
2014-06-19 02:58:09 +04:00
|
|
|
ncch_offset = 0x4000;
|
|
|
|
file.Seek(ncch_offset, 0);
|
2014-06-17 06:57:09 +04:00
|
|
|
file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
|
|
|
|
}
|
2014-06-19 02:58:09 +04:00
|
|
|
|
2014-06-17 06:57:09 +04:00
|
|
|
// Verify we are loading the correct file type...
|
2014-06-19 02:58:09 +04:00
|
|
|
if (0 != memcmp(&ncch_header.magic, "NCCH", 4))
|
|
|
|
return ResultStatus::ErrorInvalidFormat;
|
|
|
|
|
2014-06-19 07:53:22 +04:00
|
|
|
// Read ExHeader...
|
|
|
|
|
2014-06-17 06:57:09 +04:00
|
|
|
file.ReadBytes(&exheader_header, sizeof(ExHeader_Header));
|
|
|
|
|
2014-06-19 02:58:09 +04:00
|
|
|
is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1;
|
|
|
|
entry_point = exheader_header.codeset_info.text.address;
|
2014-06-17 06:57:09 +04:00
|
|
|
|
|
|
|
INFO_LOG(LOADER, "Name: %s", exheader_header.codeset_info.name);
|
|
|
|
INFO_LOG(LOADER, "Code compressed: %s", is_compressed ? "yes" : "no");
|
2014-06-19 02:58:09 +04:00
|
|
|
INFO_LOG(LOADER, "Entry point: 0x%08X", entry_point);
|
2014-06-17 06:57:09 +04:00
|
|
|
|
2014-06-19 07:53:22 +04:00
|
|
|
// Read ExeFS...
|
|
|
|
|
|
|
|
exefs_offset = ncch_header.exefs_offset * kBlockSize;
|
|
|
|
u32 exefs_size = ncch_header.exefs_size * kBlockSize;
|
2014-06-17 06:57:09 +04:00
|
|
|
|
|
|
|
INFO_LOG(LOADER, "ExeFS offset: 0x%08X", exefs_offset);
|
|
|
|
INFO_LOG(LOADER, "ExeFS size: 0x%08X", exefs_size);
|
|
|
|
|
2014-06-19 02:58:09 +04:00
|
|
|
file.Seek(exefs_offset + ncch_offset, 0);
|
2014-06-17 06:57:09 +04:00
|
|
|
file.ReadBytes(&exefs_header, sizeof(ExeFs_Header));
|
|
|
|
|
2014-06-19 02:58:09 +04:00
|
|
|
is_loaded = true; // Set state to loaded
|
2014-06-17 06:57:09 +04:00
|
|
|
|
2014-06-19 02:58:09 +04:00
|
|
|
LoadExec(); // Load the executable into memory for booting
|
|
|
|
|
|
|
|
return ResultStatus::Success;
|
2014-06-17 06:57:09 +04:00
|
|
|
}
|
2014-06-19 02:58:09 +04:00
|
|
|
return ResultStatus::Error;
|
2014-06-17 06:57:09 +04:00
|
|
|
}
|
|
|
|
|
2014-06-22 23:40:21 +04:00
|
|
|
/**
|
|
|
|
* Get the code (typically .code section) of the application
|
2014-06-27 23:33:23 +04:00
|
|
|
* @param buffer Reference to buffer to store data
|
|
|
|
* @return ResultStatus result of function
|
2014-06-22 23:40:21 +04:00
|
|
|
*/
|
2014-06-27 23:33:23 +04:00
|
|
|
ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) {
|
|
|
|
return LoadSectionExeFS(".code", buffer);
|
2014-06-22 23:40:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the icon (typically icon section) of the application
|
2014-06-27 23:33:23 +04:00
|
|
|
* @param buffer Reference to buffer to store data
|
|
|
|
* @return ResultStatus result of function
|
2014-06-22 23:40:21 +04:00
|
|
|
*/
|
2014-06-27 23:33:23 +04:00
|
|
|
ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) {
|
|
|
|
return LoadSectionExeFS("icon", buffer);
|
2014-06-22 23:40:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the banner (typically banner section) of the application
|
2014-06-27 23:33:23 +04:00
|
|
|
* @param buffer Reference to buffer to store data
|
|
|
|
* @return ResultStatus result of function
|
2014-06-22 23:40:21 +04:00
|
|
|
*/
|
2014-06-27 23:33:23 +04:00
|
|
|
ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) {
|
|
|
|
return LoadSectionExeFS("banner", buffer);
|
2014-06-22 23:40:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the logo (typically logo section) of the application
|
2014-06-27 23:33:23 +04:00
|
|
|
* @param buffer Reference to buffer to store data
|
|
|
|
* @return ResultStatus result of function
|
2014-06-22 23:40:21 +04:00
|
|
|
*/
|
2014-06-27 23:33:23 +04:00
|
|
|
ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) {
|
|
|
|
return LoadSectionExeFS("logo", buffer);
|
2014-06-22 23:40:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-27 23:33:23 +04:00
|
|
|
* Get the RomFS of the application
|
|
|
|
* @param buffer Reference to buffer to store data
|
|
|
|
* @return ResultStatus result of function
|
2014-06-22 23:40:21 +04:00
|
|
|
*/
|
2014-06-27 23:33:23 +04:00
|
|
|
ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) {
|
2014-06-22 23:40:21 +04: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;
|
|
|
|
|
|
|
|
INFO_LOG(LOADER, "RomFS offset: 0x%08X", romfs_offset);
|
|
|
|
INFO_LOG(LOADER, "RomFS size: 0x%08X", romfs_size);
|
|
|
|
|
2014-06-27 23:33:23 +04:00
|
|
|
buffer.resize(romfs_size);
|
2014-06-22 23:40:21 +04:00
|
|
|
|
|
|
|
file.Seek(romfs_offset, 0);
|
2014-06-27 23:33:23 +04:00
|
|
|
file.ReadBytes(&buffer[0], romfs_size);
|
2014-06-22 23:40:21 +04:00
|
|
|
|
2014-06-27 23:33:23 +04:00
|
|
|
return ResultStatus::Success;
|
2014-06-22 23:40:21 +04:00
|
|
|
}
|
2014-06-27 23:33:23 +04:00
|
|
|
NOTICE_LOG(LOADER, "RomFS unused");
|
|
|
|
return ResultStatus::ErrorNotUsed;
|
2014-06-22 23:40:21 +04:00
|
|
|
}
|
|
|
|
|
2014-06-17 06:57:09 +04:00
|
|
|
} // namespace Loader
|