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.
|
|
|
|
|
2015-05-09 08:05:59 +00:00
|
|
|
#include <algorithm>
|
2017-03-08 15:37:24 +00:00
|
|
|
#include <cinttypes>
|
2015-06-21 13:58:59 +00:00
|
|
|
#include <cstring>
|
2014-06-18 22:58:09 +00:00
|
|
|
#include <memory>
|
2015-05-06 07:06:12 +00:00
|
|
|
#include "common/logging/log.h"
|
2015-05-04 03:01:16 +00:00
|
|
|
#include "common/string_util.h"
|
|
|
|
#include "common/swap.h"
|
2017-05-02 04:10:04 +00:00
|
|
|
#include "core/core.h"
|
2017-02-10 15:15:26 +00:00
|
|
|
#include "core/file_sys/archive_selfncch.h"
|
2015-06-21 13:58:59 +00:00
|
|
|
#include "core/hle/kernel/process.h"
|
2015-05-12 20:25:15 +00:00
|
|
|
#include "core/hle/kernel/resource_limit.h"
|
2016-11-30 09:32:09 +00:00
|
|
|
#include "core/hle/service/cfg/cfg.h"
|
2016-05-17 22:06:33 +00:00
|
|
|
#include "core/hle/service/fs/archive.h"
|
2016-09-21 06:52:38 +00:00
|
|
|
#include "core/loader/ncch.h"
|
2016-11-30 09:32:09 +00:00
|
|
|
#include "core/loader/smdh.h"
|
2015-05-13 01:38:29 +00:00
|
|
|
#include "core/memory.h"
|
2014-06-17 02:57:09 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Loader namespace
|
|
|
|
|
|
|
|
namespace Loader {
|
|
|
|
|
2016-09-18 00:38:01 +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
|
|
|
|
*/
|
2015-01-07 01:01:19 +00:00
|
|
|
static u32 LZSS_GetDecompressedSize(const 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
|
|
|
|
*/
|
2016-09-18 00:38:01 +00:00
|
|
|
static bool LZSS_Decompress(const u8* compressed, u32 compressed_size, u8* decompressed,
|
|
|
|
u32 decompressed_size) {
|
2015-01-07 01:01:19 +00:00
|
|
|
const u8* footer = compressed + compressed_size - 8;
|
|
|
|
u32 buffer_top_and_bottom = *reinterpret_cast<const u32*>(footer);
|
2014-06-17 02:57:09 +00: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-07 01:01:19 +00:00
|
|
|
while (index > stop_index) {
|
2016-09-18 00:38:01 +00:00
|
|
|
u8 control = compressed[--index];
|
2014-06-17 02:57:09 +00:00
|
|
|
|
2015-01-07 01:01:19 +00:00
|
|
|
for (unsigned i = 0; i < 8; i++) {
|
|
|
|
if (index <= stop_index)
|
2014-06-17 02:57:09 +00:00
|
|
|
break;
|
2015-01-07 01:01:19 +00:00
|
|
|
if (index <= 0)
|
2014-06-17 02:57:09 +00:00
|
|
|
break;
|
2015-01-07 01:01:19 +00:00
|
|
|
if (out <= 0)
|
2014-06-17 02:57:09 +00:00
|
|
|
break;
|
|
|
|
|
2015-01-07 01:01:19 +00:00
|
|
|
if (control & 0x80) {
|
2014-06-18 22:58:09 +00:00
|
|
|
// Check if compression is out of bounds
|
2015-01-07 01:01:19 +00:00
|
|
|
if (index < 2)
|
2014-06-17 02:57:09 +00: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 22:58:09 +00:00
|
|
|
// Check if compression is out of bounds
|
2015-01-07 01:01:19 +00:00
|
|
|
if (out < segment_size)
|
2014-06-17 02:57:09 +00:00
|
|
|
return false;
|
2015-01-07 01:01:19 +00:00
|
|
|
|
|
|
|
for (unsigned j = 0; j < segment_size; j++) {
|
2014-06-18 22:58:09 +00:00
|
|
|
// Check if compression is out of bounds
|
2015-01-07 01:01:19 +00:00
|
|
|
if (out + segment_offset >= decompressed_size)
|
2014-06-17 02:57:09 +00:00
|
|
|
return false;
|
2014-09-06 17:36:49 +00:00
|
|
|
|
2015-01-07 01:01:19 +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
|
2015-01-07 01:01:19 +00:00
|
|
|
if (out < 1)
|
2014-06-17 02:57:09 +00:00
|
|
|
return false;
|
|
|
|
decompressed[--out] = compressed[--index];
|
|
|
|
}
|
|
|
|
control <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-18 22:58:09 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// AppLoader_NCCH class
|
|
|
|
|
2015-01-06 23:10:13 +00: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;
|
|
|
|
}
|
|
|
|
|
2016-11-27 04:13:40 +00:00
|
|
|
boost::optional<u32> AppLoader_NCCH::LoadKernelSystemMode() {
|
|
|
|
if (!is_loaded) {
|
|
|
|
if (LoadExeFS() != ResultStatus::Success)
|
|
|
|
return boost::none;
|
|
|
|
}
|
|
|
|
return exheader_header.arm11_system_local_caps.system_mode.Value();
|
2016-11-20 01:40:04 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 22:16:33 +00:00
|
|
|
ResultStatus AppLoader_NCCH::LoadExec() {
|
2015-07-10 01:52:15 +00:00
|
|
|
using Kernel::SharedPtr;
|
|
|
|
using Kernel::CodeSet;
|
|
|
|
|
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)) {
|
2015-05-04 03:01:16 +00:00
|
|
|
std::string process_name = Common::StringFromFixedZeroTerminatedBuffer(
|
2016-09-18 00:38:01 +00:00
|
|
|
(const char*)exheader_header.codeset_info.name, 8);
|
2015-07-10 01:52:15 +00:00
|
|
|
|
2015-10-11 01:51:06 +00:00
|
|
|
SharedPtr<CodeSet> codeset = CodeSet::Create(process_name, ncch_header.program_id);
|
2015-07-10 01:52:15 +00:00
|
|
|
|
|
|
|
codeset->code.offset = 0;
|
|
|
|
codeset->code.addr = exheader_header.codeset_info.text.address;
|
|
|
|
codeset->code.size = exheader_header.codeset_info.text.num_max_pages * Memory::PAGE_SIZE;
|
|
|
|
|
|
|
|
codeset->rodata.offset = codeset->code.offset + codeset->code.size;
|
|
|
|
codeset->rodata.addr = exheader_header.codeset_info.ro.address;
|
|
|
|
codeset->rodata.size = exheader_header.codeset_info.ro.num_max_pages * Memory::PAGE_SIZE;
|
|
|
|
|
|
|
|
// TODO(yuriks): Not sure if the bss size is added to the page-aligned .data size or just
|
|
|
|
// to the regular size. Playing it safe for now.
|
|
|
|
u32 bss_page_size = (exheader_header.codeset_info.bss_size + 0xFFF) & ~0xFFF;
|
|
|
|
code.resize(code.size() + bss_page_size, 0);
|
|
|
|
|
|
|
|
codeset->data.offset = codeset->rodata.offset + codeset->rodata.size;
|
|
|
|
codeset->data.addr = exheader_header.codeset_info.data.address;
|
2016-09-18 00:38:01 +00:00
|
|
|
codeset->data.size =
|
|
|
|
exheader_header.codeset_info.data.num_max_pages * Memory::PAGE_SIZE + bss_page_size;
|
2015-07-10 01:52:15 +00:00
|
|
|
|
|
|
|
codeset->entrypoint = codeset->code.addr;
|
|
|
|
codeset->memory = std::make_shared<std::vector<u8>>(std::move(code));
|
|
|
|
|
|
|
|
Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
|
2015-05-04 03:01:16 +00:00
|
|
|
|
2015-05-12 20:25:15 +00:00
|
|
|
// Attach a resource limit to the process based on the resource limit category
|
2016-09-18 00:38:01 +00:00
|
|
|
Kernel::g_current_process->resource_limit =
|
|
|
|
Kernel::ResourceLimit::GetForCategory(static_cast<Kernel::ResourceLimitCategory>(
|
|
|
|
exheader_header.arm11_system_local_caps.resource_limit_category));
|
2015-05-12 20:25:15 +00:00
|
|
|
|
2016-04-17 19:01:40 +00:00
|
|
|
// Set the default CPU core for this process
|
2016-09-18 00:38:01 +00:00
|
|
|
Kernel::g_current_process->ideal_processor =
|
|
|
|
exheader_header.arm11_system_local_caps.ideal_processor;
|
2016-04-17 19:01:40 +00:00
|
|
|
|
2016-10-20 14:26:59 +00:00
|
|
|
// Copy data while converting endianness
|
2015-05-04 03:01:16 +00:00
|
|
|
std::array<u32, ARRAY_SIZE(exheader_header.arm11_kernel_caps.descriptors)> kernel_caps;
|
2016-09-18 00:38:01 +00:00
|
|
|
std::copy_n(exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(),
|
|
|
|
begin(kernel_caps));
|
2015-05-04 03:01:16 +00:00
|
|
|
Kernel::g_current_process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size());
|
|
|
|
|
|
|
|
s32 priority = exheader_header.arm11_system_local_caps.priority;
|
|
|
|
u32 stack_size = exheader_header.codeset_info.stack_size;
|
2015-07-10 01:52:15 +00:00
|
|
|
Kernel::g_current_process->Run(priority, stack_size);
|
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
|
|
|
}
|
|
|
|
|
2015-07-11 22:16:33 +00:00
|
|
|
ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) {
|
|
|
|
if (!file.IsOpen())
|
2015-01-06 21:30:40 +00:00
|
|
|
return ResultStatus::Error;
|
|
|
|
|
2016-04-13 21:04:05 +00:00
|
|
|
ResultStatus result = LoadExeFS();
|
|
|
|
if (result != ResultStatus::Success)
|
|
|
|
return result;
|
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
LOG_DEBUG(Loader, "%d sections:", kMaxSections);
|
2016-04-14 13:53:51 +00:00
|
|
|
// Iterate through the ExeFs archive until we find a section with the specified name...
|
2015-01-07 01:01:19 +00:00
|
|
|
for (unsigned section_number = 0; section_number < kMaxSections; section_number++) {
|
|
|
|
const auto& section = exefs_header.section[section_number];
|
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
// Load the specified section...
|
2015-01-07 01:01:19 +00: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 21:30:40 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
s64 section_offset =
|
|
|
|
(section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset);
|
2015-07-11 22:16:33 +00:00
|
|
|
file.Seek(section_offset, SEEK_SET);
|
2015-01-06 21:30:40 +00:00
|
|
|
|
2016-04-14 13:53:51 +00:00
|
|
|
if (strcmp(section.name, ".code") == 0 && is_compressed) {
|
2015-01-07 01:01:19 +00:00
|
|
|
// Section is compressed, read compressed .code section...
|
2015-01-06 21:30:40 +00:00
|
|
|
std::unique_ptr<u8[]> temp_buffer;
|
|
|
|
try {
|
2015-01-07 01:01:19 +00:00
|
|
|
temp_buffer.reset(new u8[section.size]);
|
2015-01-06 21:30:40 +00:00
|
|
|
} catch (std::bad_alloc&) {
|
|
|
|
return ResultStatus::ErrorMemoryAllocationFailed;
|
2014-06-18 22:58:09 +00:00
|
|
|
}
|
2015-01-07 01:01:19 +00:00
|
|
|
|
2015-07-11 22:16:33 +00:00
|
|
|
if (file.ReadBytes(&temp_buffer[0], section.size) != section.size)
|
2015-01-07 01:01:19 +00:00
|
|
|
return ResultStatus::Error;
|
2015-01-06 21:30:40 +00:00
|
|
|
|
|
|
|
// Decompress .code section...
|
2015-01-07 01:01:19 +00:00
|
|
|
u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], section.size);
|
2015-01-06 21:30:40 +00:00
|
|
|
buffer.resize(decompressed_size);
|
2015-01-07 01:01:19 +00:00
|
|
|
if (!LZSS_Decompress(&temp_buffer[0], section.size, &buffer[0], decompressed_size))
|
2015-01-06 21:30:40 +00:00
|
|
|
return ResultStatus::ErrorInvalidFormat;
|
2015-01-07 01:01:19 +00:00
|
|
|
} else {
|
2015-01-06 21:30:40 +00:00
|
|
|
// Section is uncompressed...
|
2015-01-07 01:01:19 +00:00
|
|
|
buffer.resize(section.size);
|
2015-07-11 22:16:33 +00:00
|
|
|
if (file.ReadBytes(&buffer[0], section.size) != section.size)
|
2015-01-07 01:01:19 +00:00
|
|
|
return ResultStatus::Error;
|
2015-01-06 21:30:40 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2016-04-13 21:04:05 +00:00
|
|
|
ResultStatus AppLoader_NCCH::LoadExeFS() {
|
|
|
|
if (is_exefs_loaded)
|
|
|
|
return ResultStatus::Success;
|
2014-06-18 22:58:09 +00:00
|
|
|
|
2015-07-11 22:16:33 +00:00
|
|
|
if (!file.IsOpen())
|
2015-01-06 21:30:40 +00:00
|
|
|
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.
|
2015-07-11 22:16:33 +00:00
|
|
|
file.Seek(0, SEEK_SET);
|
2015-01-06 22:47:43 +00:00
|
|
|
|
2015-07-11 22:16:33 +00:00
|
|
|
if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header))
|
2015-01-07 01:01:19 +00:00
|
|
|
return ResultStatus::Error;
|
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)...
|
2015-01-06 23:10:13 +00:00
|
|
|
if (MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) {
|
2017-03-08 15:37:24 +00:00
|
|
|
LOG_DEBUG(Loader, "Only loading the first (bootable) NCCH within the NCSD file!");
|
2015-01-06 21:30:40 +00:00
|
|
|
ncch_offset = 0x4000;
|
2015-07-11 22:16:33 +00:00
|
|
|
file.Seek(ncch_offset, SEEK_SET);
|
|
|
|
file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
|
2015-01-06 21:30:40 +00:00
|
|
|
}
|
2014-06-18 22:58:09 +00:00
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
// Verify we are loading the correct file type...
|
2015-01-06 23:10:13 +00:00
|
|
|
if (MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic)
|
2015-01-06 21:30:40 +00:00
|
|
|
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-07-11 22:16:33 +00:00
|
|
|
if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header))
|
2015-01-07 01:01:19 +00:00
|
|
|
return ResultStatus::Error;
|
2014-06-17 02:57:09 +00:00
|
|
|
|
2016-09-18 00:38:01 +00: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;
|
2015-04-09 07:11:03 +00:00
|
|
|
resource_limit_category = exheader_header.arm11_system_local_caps.resource_limit_category;
|
|
|
|
|
2017-03-08 15:37:24 +00:00
|
|
|
LOG_DEBUG(Loader, "Name: %s", exheader_header.codeset_info.name);
|
|
|
|
LOG_DEBUG(Loader, "Program ID: %016" PRIX64, ncch_header.program_id);
|
2016-09-18 00:38:01 +00:00
|
|
|
LOG_DEBUG(Loader, "Code compressed: %s", is_compressed ? "yes" : "no");
|
2015-04-09 07:11:03 +00:00
|
|
|
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);
|
2016-09-18 00:38:01 +00:00
|
|
|
LOG_DEBUG(Loader, "Core version: %d", core_version);
|
|
|
|
LOG_DEBUG(Loader, "Thread priority: 0x%X", priority);
|
|
|
|
LOG_DEBUG(Loader, "Resource limit category: %d", resource_limit_category);
|
2016-11-27 04:13:40 +00:00
|
|
|
LOG_DEBUG(Loader, "System Mode: %d",
|
2017-01-04 16:48:29 +00:00
|
|
|
static_cast<int>(exheader_header.arm11_system_local_caps.system_mode));
|
2014-06-19 03:53:22 +00:00
|
|
|
|
2015-10-11 01:50:10 +00:00
|
|
|
if (exheader_header.arm11_system_local_caps.program_id != ncch_header.program_id) {
|
|
|
|
LOG_ERROR(Loader, "ExHeader Program ID mismatch: the ROM is probably encrypted.");
|
|
|
|
return ResultStatus::ErrorEncrypted;
|
|
|
|
}
|
|
|
|
|
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-04-09 07:11:03 +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-07-11 22:16:33 +00:00
|
|
|
file.Seek(exefs_offset + ncch_offset, SEEK_SET);
|
|
|
|
if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header))
|
2015-01-07 01:01:19 +00:00
|
|
|
return ResultStatus::Error;
|
2015-01-06 19:49:25 +00:00
|
|
|
|
2016-04-13 21:04:05 +00:00
|
|
|
is_exefs_loaded = true;
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2016-11-30 09:32:09 +00:00
|
|
|
void AppLoader_NCCH::ParseRegionLockoutInfo() {
|
|
|
|
std::vector<u8> smdh_buffer;
|
|
|
|
if (ReadIcon(smdh_buffer) == ResultStatus::Success && smdh_buffer.size() >= sizeof(SMDH)) {
|
|
|
|
SMDH smdh;
|
|
|
|
memcpy(&smdh, smdh_buffer.data(), sizeof(SMDH));
|
|
|
|
u32 region_lockout = smdh.region_lockout;
|
|
|
|
constexpr u32 REGION_COUNT = 7;
|
|
|
|
for (u32 region = 0; region < REGION_COUNT; ++region) {
|
|
|
|
if (region_lockout & 1) {
|
|
|
|
Service::CFG::SetPreferredRegionCode(region);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
region_lockout >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-13 21:04:05 +00:00
|
|
|
ResultStatus AppLoader_NCCH::Load() {
|
|
|
|
if (is_loaded)
|
|
|
|
return ResultStatus::ErrorAlreadyLoaded;
|
|
|
|
|
|
|
|
ResultStatus result = LoadExeFS();
|
|
|
|
if (result != ResultStatus::Success)
|
|
|
|
return result;
|
|
|
|
|
2017-03-08 15:37:24 +00:00
|
|
|
LOG_INFO(Loader, "Program ID: %016" PRIX64, ncch_header.program_id);
|
|
|
|
|
2017-05-02 04:10:04 +00:00
|
|
|
Core::Telemetry().AddField(Telemetry::FieldType::Session, "ProgramId", ncch_header.program_id);
|
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
is_loaded = true; // Set state to loaded
|
|
|
|
|
2016-05-17 22:06:33 +00:00
|
|
|
result = LoadExec(); // Load the executable into memory for booting
|
|
|
|
if (ResultStatus::Success != result)
|
|
|
|
return result;
|
|
|
|
|
2017-02-10 15:15:26 +00:00
|
|
|
Service::FS::RegisterArchiveType(std::make_unique<FileSys::ArchiveFactory_SelfNCCH>(*this),
|
|
|
|
Service::FS::ArchiveIdCode::SelfNCCH);
|
2016-11-30 09:32:09 +00:00
|
|
|
|
|
|
|
ParseRegionLockoutInfo();
|
|
|
|
|
2016-05-17 22:06:33 +00:00
|
|
|
return ResultStatus::Success;
|
2014-06-17 02:57:09 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 22:16:33 +00:00
|
|
|
ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) {
|
2014-06-27 19:33:23 +00:00
|
|
|
return LoadSectionExeFS(".code", buffer);
|
2014-06-22 19:40:21 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 22:16:33 +00:00
|
|
|
ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) {
|
2014-06-27 19:33:23 +00:00
|
|
|
return LoadSectionExeFS("icon", buffer);
|
2014-06-22 19:40:21 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 22:16:33 +00:00
|
|
|
ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) {
|
2014-06-27 19:33:23 +00:00
|
|
|
return LoadSectionExeFS("banner", buffer);
|
2014-06-22 19:40:21 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 22:16:33 +00:00
|
|
|
ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) {
|
2014-06-27 19:33:23 +00:00
|
|
|
return LoadSectionExeFS("logo", buffer);
|
2014-06-22 19:40:21 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 09:54:25 +00:00
|
|
|
ResultStatus AppLoader_NCCH::ReadProgramId(u64& out_program_id) {
|
|
|
|
if (!file.IsOpen())
|
|
|
|
return ResultStatus::Error;
|
|
|
|
|
|
|
|
ResultStatus result = LoadExeFS();
|
|
|
|
if (result != ResultStatus::Success)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
out_program_id = ncch_header.program_id;
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
ResultStatus AppLoader_NCCH::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset,
|
|
|
|
u64& size) {
|
2015-07-11 22:16:33 +00:00
|
|
|
if (!file.IsOpen())
|
2015-01-06 21:30:40 +00:00
|
|
|
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-04-09 07:11:03 +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
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
if (file.GetSize() < romfs_offset + romfs_size)
|
2015-07-09 21:55:23 +00:00
|
|
|
return ResultStatus::Error;
|
2014-06-22 19:40:21 +00:00
|
|
|
|
2015-07-09 21:55:23 +00:00
|
|
|
// We reopen the file, to allow its position to be independent from file's
|
|
|
|
romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb");
|
|
|
|
if (!romfs_file->IsOpen())
|
2015-01-07 01:01:19 +00:00
|
|
|
return ResultStatus::Error;
|
2015-01-06 21:30:40 +00:00
|
|
|
|
2015-07-09 21:55:23 +00:00
|
|
|
offset = romfs_offset;
|
|
|
|
size = romfs_size;
|
|
|
|
|
2015-01-06 21:30:40 +00:00
|
|
|
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-06-17 02:57:09 +00:00
|
|
|
} // namespace Loader
|