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.
|
|
|
|
|
2015-05-09 04:05:59 -04:00
|
|
|
#include <algorithm>
|
2015-06-21 09:58:59 -04:00
|
|
|
#include <cstring>
|
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/string_util.h"
|
|
|
|
#include "common/swap.h"
|
2016-05-17 18:06:33 -04:00
|
|
|
#include "core/file_sys/archive_romfs.h"
|
2015-06-21 09:58:59 -04:00
|
|
|
#include "core/hle/kernel/process.h"
|
2015-05-12 16:25:15 -04:00
|
|
|
#include "core/hle/kernel/resource_limit.h"
|
2016-11-30 04:32:09 -05:00
|
|
|
#include "core/hle/service/cfg/cfg.h"
|
2016-05-17 18:06:33 -04:00
|
|
|
#include "core/hle/service/fs/archive.h"
|
2016-09-21 02:52:38 -04:00
|
|
|
#include "core/loader/ncch.h"
|
2016-11-30 04:32:09 -05:00
|
|
|
#include "core/loader/smdh.h"
|
2015-05-12 21:38:29 -04:00
|
|
|
#include "core/memory.h"
|
2014-06-16 22:57:09 -04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Loader namespace
|
|
|
|
|
|
|
|
namespace Loader {
|
|
|
|
|
2016-09-17 20:38:01 -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-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
|
|
|
|
*/
|
2016-09-17 20:38:01 -04:00
|
|
|
static bool LZSS_Decompress(const u8* compressed, u32 compressed_size, u8* decompressed,
|
|
|
|
u32 decompressed_size) {
|
2015-01-06 20:01:19 -05:00
|
|
|
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) {
|
2016-09-17 20:38:01 -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;
|
|
|
|
}
|
|
|
|
|
2016-11-26 23:13:40 -05: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-19 20:40:04 -05:00
|
|
|
}
|
|
|
|
|
2015-07-11 18:16:33 -04:00
|
|
|
ResultStatus AppLoader_NCCH::LoadExec() {
|
2015-07-09 21:52:15 -04:00
|
|
|
using Kernel::SharedPtr;
|
|
|
|
using Kernel::CodeSet;
|
|
|
|
|
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(
|
2016-09-17 20:38:01 -04:00
|
|
|
(const char*)exheader_header.codeset_info.name, 8);
|
2015-07-09 21:52:15 -04:00
|
|
|
|
2015-10-10 21:51:06 -04:00
|
|
|
SharedPtr<CodeSet> codeset = CodeSet::Create(process_name, ncch_header.program_id);
|
2015-07-09 21:52:15 -04: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-17 20:38:01 -04:00
|
|
|
codeset->data.size =
|
|
|
|
exheader_header.codeset_info.data.num_max_pages * Memory::PAGE_SIZE + bss_page_size;
|
2015-07-09 21:52:15 -04: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-03 23:01:16 -04:00
|
|
|
|
2015-05-12 16:25:15 -04:00
|
|
|
// Attach a resource limit to the process based on the resource limit category
|
2016-09-17 20:38:01 -04: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 16:25:15 -04:00
|
|
|
|
2016-04-17 15:01:40 -04:00
|
|
|
// Set the default CPU core for this process
|
2016-09-17 20:38:01 -04:00
|
|
|
Kernel::g_current_process->ideal_processor =
|
|
|
|
exheader_header.arm11_system_local_caps.ideal_processor;
|
2016-04-17 15:01:40 -04:00
|
|
|
|
2016-10-20 10:26:59 -04:00
|
|
|
// Copy data while converting endianness
|
2015-05-03 23:01:16 -04:00
|
|
|
std::array<u32, ARRAY_SIZE(exheader_header.arm11_kernel_caps.descriptors)> kernel_caps;
|
2016-09-17 20:38:01 -04:00
|
|
|
std::copy_n(exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(),
|
|
|
|
begin(kernel_caps));
|
2015-05-03 23:01:16 -04: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-09 21:52:15 -04:00
|
|
|
Kernel::g_current_process->Run(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
|
|
|
}
|
|
|
|
|
2015-07-11 18:16:33 -04:00
|
|
|
ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) {
|
|
|
|
if (!file.IsOpen())
|
2015-01-06 16:30:40 -05:00
|
|
|
return ResultStatus::Error;
|
|
|
|
|
2016-04-13 17:04:05 -04:00
|
|
|
ResultStatus result = LoadExeFS();
|
|
|
|
if (result != ResultStatus::Success)
|
|
|
|
return result;
|
|
|
|
|
2015-01-06 16:30:40 -05:00
|
|
|
LOG_DEBUG(Loader, "%d sections:", kMaxSections);
|
2016-04-14 09:53:51 -04:00
|
|
|
// Iterate through the ExeFs archive until we find a section with the specified name...
|
2015-01-06 20:01:19 -05:00
|
|
|
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
|
|
|
|
2016-09-17 20:38:01 -04:00
|
|
|
s64 section_offset =
|
|
|
|
(section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset);
|
2015-07-11 18:16:33 -04:00
|
|
|
file.Seek(section_offset, SEEK_SET);
|
2015-01-06 16:30:40 -05:00
|
|
|
|
2016-04-14 09:53:51 -04:00
|
|
|
if (strcmp(section.name, ".code") == 0 && is_compressed) {
|
2015-01-06 20:01:19 -05:00
|
|
|
// 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
|
|
|
|
2015-07-11 18:16:33 -04:00
|
|
|
if (file.ReadBytes(&temp_buffer[0], section.size) != section.size)
|
2015-01-06 20:01:19 -05:00
|
|
|
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);
|
2015-07-11 18:16:33 -04:00
|
|
|
if (file.ReadBytes(&buffer[0], section.size) != section.size)
|
2015-01-06 20:01:19 -05:00
|
|
|
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
|
|
|
|
2016-04-13 17:04:05 -04:00
|
|
|
ResultStatus AppLoader_NCCH::LoadExeFS() {
|
|
|
|
if (is_exefs_loaded)
|
|
|
|
return ResultStatus::Success;
|
2014-06-18 18:58:09 -04:00
|
|
|
|
2015-07-11 18:16:33 -04:00
|
|
|
if (!file.IsOpen())
|
2015-01-06 16:30:40 -05:00
|
|
|
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.
|
2015-07-11 18:16:33 -04:00
|
|
|
file.Seek(0, SEEK_SET);
|
2015-01-06 17:47:43 -05:00
|
|
|
|
2015-07-11 18:16:33 -04:00
|
|
|
if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header))
|
2015-01-06 20:01:19 -05:00
|
|
|
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-07-11 18:16:33 -04:00
|
|
|
file.Seek(ncch_offset, SEEK_SET);
|
|
|
|
file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
|
2015-01-06 16:30:40 -05:00
|
|
|
}
|
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-07-11 18:16:33 -04:00
|
|
|
if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header))
|
2015-01-06 20:01:19 -05:00
|
|
|
return ResultStatus::Error;
|
2014-06-16 22:57:09 -04:00
|
|
|
|
2016-09-17 20:38:01 -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;
|
2015-04-09 03:11:03 -04:00
|
|
|
resource_limit_category = exheader_header.arm11_system_local_caps.resource_limit_category;
|
|
|
|
|
2016-09-17 20:38:01 -04:00
|
|
|
LOG_INFO(Loader, "Name: %s", exheader_header.codeset_info.name);
|
|
|
|
LOG_INFO(Loader, "Program ID: %016llX", ncch_header.program_id);
|
|
|
|
LOG_DEBUG(Loader, "Code compressed: %s", is_compressed ? "yes" : "no");
|
2015-04-09 03:11:03 -04: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-17 20:38:01 -04: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-26 23:13:40 -05:00
|
|
|
LOG_DEBUG(Loader, "System Mode: %d",
|
2017-01-04 11:48:29 -05:00
|
|
|
static_cast<int>(exheader_header.arm11_system_local_caps.system_mode));
|
2014-06-18 23:53:22 -04:00
|
|
|
|
2015-10-10 21:50:10 -04: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 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-07-11 18:16:33 -04:00
|
|
|
file.Seek(exefs_offset + ncch_offset, SEEK_SET);
|
|
|
|
if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header))
|
2015-01-06 20:01:19 -05:00
|
|
|
return ResultStatus::Error;
|
2015-01-06 14:49:25 -05:00
|
|
|
|
2016-04-13 17:04:05 -04:00
|
|
|
is_exefs_loaded = true;
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2016-11-30 04:32:09 -05: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 17:04:05 -04:00
|
|
|
ResultStatus AppLoader_NCCH::Load() {
|
|
|
|
if (is_loaded)
|
|
|
|
return ResultStatus::ErrorAlreadyLoaded;
|
|
|
|
|
|
|
|
ResultStatus result = LoadExeFS();
|
|
|
|
if (result != ResultStatus::Success)
|
|
|
|
return result;
|
|
|
|
|
2015-01-06 16:30:40 -05:00
|
|
|
is_loaded = true; // Set state to loaded
|
|
|
|
|
2016-05-17 18:06:33 -04:00
|
|
|
result = LoadExec(); // Load the executable into memory for booting
|
|
|
|
if (ResultStatus::Success != result)
|
|
|
|
return result;
|
|
|
|
|
2016-09-17 20:38:01 -04:00
|
|
|
Service::FS::RegisterArchiveType(std::make_unique<FileSys::ArchiveFactory_RomFS>(*this),
|
|
|
|
Service::FS::ArchiveIdCode::RomFS);
|
2016-11-30 04:32:09 -05:00
|
|
|
|
|
|
|
ParseRegionLockoutInfo();
|
|
|
|
|
2016-05-17 18:06:33 -04:00
|
|
|
return ResultStatus::Success;
|
2014-06-16 22:57:09 -04:00
|
|
|
}
|
|
|
|
|
2015-07-11 18:16:33 -04:00
|
|
|
ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) {
|
2014-06-27 15:33:23 -04:00
|
|
|
return LoadSectionExeFS(".code", buffer);
|
2014-06-22 15:40:21 -04:00
|
|
|
}
|
|
|
|
|
2015-07-11 18:16:33 -04:00
|
|
|
ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) {
|
2014-06-27 15:33:23 -04:00
|
|
|
return LoadSectionExeFS("icon", buffer);
|
2014-06-22 15:40:21 -04:00
|
|
|
}
|
|
|
|
|
2015-07-11 18:16:33 -04:00
|
|
|
ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) {
|
2014-06-27 15:33:23 -04:00
|
|
|
return LoadSectionExeFS("banner", buffer);
|
2014-06-22 15:40:21 -04:00
|
|
|
}
|
|
|
|
|
2015-07-11 18:16:33 -04:00
|
|
|
ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) {
|
2014-06-27 15:33:23 -04:00
|
|
|
return LoadSectionExeFS("logo", buffer);
|
2014-06-22 15:40:21 -04:00
|
|
|
}
|
|
|
|
|
2016-12-15 04:54:25 -05: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-17 20:38:01 -04:00
|
|
|
ResultStatus AppLoader_NCCH::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset,
|
|
|
|
u64& size) {
|
2015-07-11 18:16:33 -04:00
|
|
|
if (!file.IsOpen())
|
2015-01-06 16:30:40 -05:00
|
|
|
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
|
|
|
|
2016-09-17 20:38:01 -04:00
|
|
|
if (file.GetSize() < romfs_offset + romfs_size)
|
2015-07-09 17:55:23 -04:00
|
|
|
return ResultStatus::Error;
|
2014-06-22 15:40:21 -04:00
|
|
|
|
2015-07-09 17:55:23 -04: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-06 20:01:19 -05:00
|
|
|
return ResultStatus::Error;
|
2015-01-06 16:30:40 -05:00
|
|
|
|
2015-07-09 17:55:23 -04:00
|
|
|
offset = romfs_offset;
|
|
|
|
size = romfs_size;
|
|
|
|
|
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
|