2014-06-16 22:57:09 -04:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 00:38:14 -05:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-06-16 22:57:09 -04:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2014-06-18 18:58:09 -04:00
|
|
|
#include <memory>
|
|
|
|
|
2015-05-06 03:06:12 -04:00
|
|
|
#include "common/logging/log.h"
|
2015-05-03 23:01:16 -04:00
|
|
|
#include "common/make_unique.h"
|
|
|
|
#include "common/string_util.h"
|
|
|
|
#include "common/swap.h"
|
2015-05-06 03:06:12 -04:00
|
|
|
|
2014-06-16 22:57:09 -04:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
2015-05-03 23:01:16 -04:00
|
|
|
#include "core/loader/ncch.h"
|
2014-06-16 22:57:09 -04:00
|
|
|
#include "core/mem_map.h"
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Loader namespace
|
|
|
|
|
|
|
|
namespace Loader {
|
|
|
|
|
2015-01-06 20:01:19 -05: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-16 22: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
|
|
|
|
*/
|
2015-01-06 20:01:19 -05:00
|
|
|
static u32 LZSS_GetDecompressedSize(const u8* buffer, u32 size) {
|
2014-06-16 22:57:09 -04: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
|
|
|
|
*/
|
2015-01-06 20:01:19 -05:00
|
|
|
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<const u32*>(footer);
|
2014-06-16 22:57:09 -04:00
|
|
|
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);
|
|
|
|
|
2015-01-06 20:01:19 -05:00
|
|
|
while (index > stop_index) {
|
2014-09-06 13:36:49 -04:00
|
|
|
u8 control = compressed[--index];
|
2014-06-16 22:57:09 -04:00
|
|
|
|
2015-01-06 20:01:19 -05:00
|
|
|
for (unsigned i = 0; i < 8; i++) {
|
|
|
|
if (index <= stop_index)
|
2014-06-16 22:57:09 -04:00
|
|
|
break;
|
2015-01-06 20:01:19 -05:00
|
|
|
if (index <= 0)
|
2014-06-16 22:57:09 -04:00
|
|
|
break;
|
2015-01-06 20:01:19 -05:00
|
|
|
if (out <= 0)
|
2014-06-16 22:57:09 -04:00
|
|
|
break;
|
|
|
|
|
2015-01-06 20:01:19 -05:00
|
|
|
if (control & 0x80) {
|
2014-06-18 18:58:09 -04:00
|
|
|
// Check if compression is out of bounds
|
2015-01-06 20:01:19 -05:00
|
|
|
if (index < 2)
|
2014-06-16 22:57:09 -04:00
|
|
|
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 18:58:09 -04:00
|
|
|
// Check if compression is out of bounds
|
2015-01-06 20:01:19 -05:00
|
|
|
if (out < segment_size)
|
2014-06-16 22:57:09 -04:00
|
|
|
return false;
|
2015-01-06 20:01:19 -05:00
|
|
|
|
|
|
|
for (unsigned j = 0; j < segment_size; j++) {
|
2014-06-18 18:58:09 -04:00
|
|
|
// Check if compression is out of bounds
|
2015-01-06 20:01:19 -05:00
|
|
|
if (out + segment_offset >= decompressed_size)
|
2014-06-16 22:57:09 -04:00
|
|
|
return false;
|
2014-09-06 13:36:49 -04:00
|
|
|
|
2015-01-06 20:01:19 -05:00
|
|
|
u8 data = decompressed[out + segment_offset];
|
2014-06-16 22:57:09 -04:00
|
|
|
decompressed[--out] = data;
|
|
|
|
}
|
|
|
|
} else {
|
2014-06-18 18:58:09 -04:00
|
|
|
// Check if compression is out of bounds
|
2015-01-06 20:01:19 -05:00
|
|
|
if (out < 1)
|
2014-06-16 22:57:09 -04:00
|
|
|
return false;
|
|
|
|
decompressed[--out] = compressed[--index];
|
|
|
|
}
|
|
|
|
control <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-18 18:58:09 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// AppLoader_NCCH class
|
|
|
|
|
2015-01-06 18:10:13 -05:00
|
|
|
FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) {
|
|
|
|
u32 magic;
|
|
|
|
file.Seek(0x100, SEEK_SET);
|
|
|
|
if (1 != file.ReadArray<u32>(&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;
|
|
|
|
}
|
|
|
|
|
2014-07-04 13:11:09 -04:00
|
|
|
ResultStatus AppLoader_NCCH::LoadExec() const {
|
2014-11-19 03:49:13 -05:00
|
|
|
if (!is_loaded)
|
2014-06-18 18:58:09 -04:00
|
|
|
return ResultStatus::ErrorNotLoaded;
|
2014-06-16 22:57:09 -04:00
|
|
|
|
2014-06-27 15:33:23 -04:00
|
|
|
std::vector<u8> code;
|
|
|
|
if (ResultStatus::Success == ReadCode(code)) {
|
2015-05-03 23:01:16 -04:00
|
|
|
std::string process_name = Common::StringFromFixedZeroTerminatedBuffer(
|
|
|
|
(const char*)exheader_header.codeset_info.name, 8);
|
|
|
|
u64 program_id = *reinterpret_cast<u64_le const*>(&ncch_header.program_id[0]);
|
|
|
|
Kernel::g_current_process = Kernel::Process::Create(process_name, program_id);
|
|
|
|
|
|
|
|
// Copy data while converting endianess
|
|
|
|
std::array<u32, ARRAY_SIZE(exheader_header.arm11_kernel_caps.descriptors)> kernel_caps;
|
|
|
|
std::copy_n(exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(), begin(kernel_caps));
|
|
|
|
Kernel::g_current_process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size());
|
|
|
|
|
2014-06-22 15:40:21 -04:00
|
|
|
Memory::WriteBlock(entry_point, &code[0], code.size());
|
2015-05-03 23:01:16 -04:00
|
|
|
|
|
|
|
s32 priority = exheader_header.arm11_system_local_caps.priority;
|
|
|
|
u32 stack_size = exheader_header.codeset_info.stack_size;
|
|
|
|
Kernel::g_current_process->Run(entry_point, priority, stack_size);
|
2014-06-27 15:33:23 -04:00
|
|
|
return ResultStatus::Success;
|
2014-06-16 22:57:09 -04:00
|
|
|
}
|
2014-06-27 15:33:23 -04:00
|
|
|
return ResultStatus::Error;
|
2014-06-16 22:57:09 -04:00
|
|
|
}
|
|
|
|
|
2014-07-04 13:11:09 -04:00
|
|
|
ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const {
|
2015-01-06 16:30:40 -05:00
|
|
|
if (!file->IsOpen())
|
|
|
|
return ResultStatus::Error;
|
|
|
|
|
|
|
|
LOG_DEBUG(Loader, "%d sections:", kMaxSections);
|
2015-01-06 20:01:19 -05:00
|
|
|
// 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];
|
|
|
|
|
2015-01-06 16:30:40 -05:00
|
|
|
// Load the specified section...
|
2015-01-06 20:01:19 -05:00
|
|
|
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);
|
2015-01-06 16:30:40 -05:00
|
|
|
|
2015-01-06 20:01:19 -05:00
|
|
|
s64 section_offset = (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset);
|
2015-01-06 17:47:43 -05:00
|
|
|
file->Seek(section_offset, SEEK_SET);
|
2015-01-06 16:30:40 -05:00
|
|
|
|
2015-01-06 20:01:19 -05:00
|
|
|
if (is_compressed) {
|
|
|
|
// Section is compressed, read compressed .code section...
|
2015-01-06 16:30:40 -05:00
|
|
|
std::unique_ptr<u8[]> temp_buffer;
|
|
|
|
try {
|
2015-01-06 20:01:19 -05:00
|
|
|
temp_buffer.reset(new u8[section.size]);
|
2015-01-06 16:30:40 -05:00
|
|
|
} catch (std::bad_alloc&) {
|
|
|
|
return ResultStatus::ErrorMemoryAllocationFailed;
|
2014-06-18 18:58:09 -04:00
|
|
|
}
|
2015-01-06 20:01:19 -05:00
|
|
|
|
|
|
|
if (file->ReadBytes(&temp_buffer[0], section.size) != section.size)
|
|
|
|
return ResultStatus::Error;
|
2015-01-06 16:30:40 -05:00
|
|
|
|
|
|
|
// Decompress .code section...
|
2015-01-06 20:01:19 -05:00
|
|
|
u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], section.size);
|
2015-01-06 16:30:40 -05:00
|
|
|
buffer.resize(decompressed_size);
|
2015-01-06 20:01:19 -05:00
|
|
|
if (!LZSS_Decompress(&temp_buffer[0], section.size, &buffer[0], decompressed_size))
|
2015-01-06 16:30:40 -05:00
|
|
|
return ResultStatus::ErrorInvalidFormat;
|
2015-01-06 20:01:19 -05:00
|
|
|
} else {
|
2015-01-06 16:30:40 -05:00
|
|
|
// Section is uncompressed...
|
2015-01-06 20:01:19 -05:00
|
|
|
buffer.resize(section.size);
|
|
|
|
if (file->ReadBytes(&buffer[0], section.size) != section.size)
|
|
|
|
return ResultStatus::Error;
|
2015-01-06 16:30:40 -05:00
|
|
|
}
|
|
|
|
return ResultStatus::Success;
|
2014-06-18 18:58:09 -04:00
|
|
|
}
|
|
|
|
}
|
2014-06-27 15:33:23 -04:00
|
|
|
return ResultStatus::ErrorNotUsed;
|
2014-11-19 03:49:13 -05:00
|
|
|
}
|
2014-06-18 18:58:09 -04:00
|
|
|
|
2014-06-19 17:46:05 -04:00
|
|
|
ResultStatus AppLoader_NCCH::Load() {
|
2014-06-18 18:58:09 -04:00
|
|
|
if (is_loaded)
|
|
|
|
return ResultStatus::ErrorAlreadyLoaded;
|
|
|
|
|
2015-01-06 16:30:40 -05:00
|
|
|
if (!file->IsOpen())
|
|
|
|
return ResultStatus::Error;
|
2014-06-16 22:57:09 -04:00
|
|
|
|
2015-01-06 17:47:43 -05:00
|
|
|
// Reset read pointer in case this file has been read before.
|
|
|
|
file->Seek(0, SEEK_SET);
|
|
|
|
|
2015-01-06 20:01:19 -05:00
|
|
|
if (file->ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header))
|
|
|
|
return ResultStatus::Error;
|
2014-11-19 03:49:13 -05:00
|
|
|
|
2015-01-06 16:30:40 -05:00
|
|
|
// Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)...
|
2015-01-06 18:10:13 -05:00
|
|
|
if (MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) {
|
2015-01-06 16:30:40 -05:00
|
|
|
LOG_WARNING(Loader, "Only loading the first (bootable) NCCH within the NCSD file!");
|
|
|
|
ncch_offset = 0x4000;
|
2015-01-06 17:47:43 -05:00
|
|
|
file->Seek(ncch_offset, SEEK_SET);
|
2015-01-06 16:30:40 -05:00
|
|
|
file->ReadBytes(&ncch_header, sizeof(NCCH_Header));
|
|
|
|
}
|
2014-06-18 18:58:09 -04:00
|
|
|
|
2015-01-06 16:30:40 -05:00
|
|
|
// Verify we are loading the correct file type...
|
2015-01-06 18:10:13 -05:00
|
|
|
if (MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic)
|
2015-01-06 16:30:40 -05:00
|
|
|
return ResultStatus::ErrorInvalidFormat;
|
2014-06-18 23:53:22 -04:00
|
|
|
|
2015-01-06 16:30:40 -05:00
|
|
|
// Read ExHeader...
|
2014-06-16 22:57:09 -04:00
|
|
|
|
2015-01-06 20:01:19 -05:00
|
|
|
if (file->ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header))
|
|
|
|
return ResultStatus::Error;
|
2014-06-16 22:57:09 -04:00
|
|
|
|
2015-04-09 03:11:03 -04:00
|
|
|
is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1;
|
|
|
|
entry_point = exheader_header.codeset_info.text.address;
|
|
|
|
code_size = exheader_header.codeset_info.text.code_size;
|
|
|
|
stack_size = exheader_header.codeset_info.stack_size;
|
|
|
|
bss_size = exheader_header.codeset_info.bss_size;
|
|
|
|
core_version = exheader_header.arm11_system_local_caps.core_version;
|
|
|
|
priority = exheader_header.arm11_system_local_caps.priority;
|
|
|
|
resource_limit_category = exheader_header.arm11_system_local_caps.resource_limit_category;
|
|
|
|
|
|
|
|
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);
|
|
|
|
LOG_DEBUG(Loader, "Code size: 0x%08X", code_size);
|
|
|
|
LOG_DEBUG(Loader, "Stack size: 0x%08X", stack_size);
|
|
|
|
LOG_DEBUG(Loader, "Bss size: 0x%08X", bss_size);
|
|
|
|
LOG_DEBUG(Loader, "Core version: %d" , core_version);
|
|
|
|
LOG_DEBUG(Loader, "Thread priority: 0x%X" , priority);
|
|
|
|
LOG_DEBUG(Loader, "Resource limit descriptor: 0x%08X", exheader_header.arm11_system_local_caps.resource_limit_descriptor);
|
|
|
|
LOG_DEBUG(Loader, "Resource limit category: %d" , resource_limit_category);
|
2014-06-18 23:53:22 -04:00
|
|
|
|
2015-01-06 16:30:40 -05:00
|
|
|
// Read ExeFS...
|
2014-06-16 22:57:09 -04:00
|
|
|
|
2015-01-06 16:30:40 -05:00
|
|
|
exefs_offset = ncch_header.exefs_offset * kBlockSize;
|
|
|
|
u32 exefs_size = ncch_header.exefs_size * kBlockSize;
|
2014-06-16 22:57:09 -04:00
|
|
|
|
2015-04-09 03:11:03 -04:00
|
|
|
LOG_DEBUG(Loader, "ExeFS offset: 0x%08X", exefs_offset);
|
|
|
|
LOG_DEBUG(Loader, "ExeFS size: 0x%08X", exefs_size);
|
2014-06-16 22:57:09 -04:00
|
|
|
|
2015-01-06 17:47:43 -05:00
|
|
|
file->Seek(exefs_offset + ncch_offset, SEEK_SET);
|
2015-01-06 20:01:19 -05:00
|
|
|
if (file->ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header))
|
|
|
|
return ResultStatus::Error;
|
2015-01-06 14:49:25 -05:00
|
|
|
|
2015-01-06 16:30:40 -05:00
|
|
|
is_loaded = true; // Set state to loaded
|
|
|
|
|
2015-01-06 20:01:19 -05:00
|
|
|
return LoadExec(); // Load the executable into memory for booting
|
2014-06-16 22:57:09 -04:00
|
|
|
}
|
|
|
|
|
2014-07-04 13:11:09 -04:00
|
|
|
ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) const {
|
2014-06-27 15:33:23 -04:00
|
|
|
return LoadSectionExeFS(".code", buffer);
|
2014-06-22 15:40:21 -04:00
|
|
|
}
|
|
|
|
|
2014-07-04 13:11:09 -04:00
|
|
|
ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) const {
|
2014-06-27 15:33:23 -04:00
|
|
|
return LoadSectionExeFS("icon", buffer);
|
2014-06-22 15:40:21 -04:00
|
|
|
}
|
|
|
|
|
2014-07-04 13:11:09 -04:00
|
|
|
ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) const {
|
2014-06-27 15:33:23 -04:00
|
|
|
return LoadSectionExeFS("banner", buffer);
|
2014-06-22 15:40:21 -04:00
|
|
|
}
|
|
|
|
|
2014-07-04 13:11:09 -04:00
|
|
|
ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) const {
|
2014-06-27 15:33:23 -04:00
|
|
|
return LoadSectionExeFS("logo", buffer);
|
2014-06-22 15:40:21 -04:00
|
|
|
}
|
|
|
|
|
2014-07-04 13:11:09 -04:00
|
|
|
ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const {
|
2015-01-06 16:30:40 -05:00
|
|
|
if (!file->IsOpen())
|
|
|
|
return ResultStatus::Error;
|
2014-06-22 15:40:21 -04:00
|
|
|
|
2015-01-06 16:30:40 -05: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 15:40:21 -04:00
|
|
|
|
2015-04-09 03:11:03 -04:00
|
|
|
LOG_DEBUG(Loader, "RomFS offset: 0x%08X", romfs_offset);
|
|
|
|
LOG_DEBUG(Loader, "RomFS size: 0x%08X", romfs_size);
|
2014-06-22 15:40:21 -04:00
|
|
|
|
2015-01-06 16:30:40 -05:00
|
|
|
buffer.resize(romfs_size);
|
2014-06-22 15:40:21 -04:00
|
|
|
|
2015-01-06 17:47:43 -05:00
|
|
|
file->Seek(romfs_offset, SEEK_SET);
|
2015-01-06 20:01:19 -05:00
|
|
|
if (file->ReadBytes(&buffer[0], romfs_size) != romfs_size)
|
|
|
|
return ResultStatus::Error;
|
2015-01-06 16:30:40 -05:00
|
|
|
|
|
|
|
return ResultStatus::Success;
|
2014-06-22 15:40:21 -04:00
|
|
|
}
|
2015-01-06 16:30:40 -05:00
|
|
|
LOG_DEBUG(Loader, "NCCH has no RomFS");
|
|
|
|
return ResultStatus::ErrorNotUsed;
|
2014-06-22 15:40:21 -04:00
|
|
|
}
|
|
|
|
|
2014-06-16 22:57:09 -04:00
|
|
|
} // namespace Loader
|