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>
|
2017-08-01 23:51:44 +00:00
|
|
|
#include <codecvt>
|
2015-06-21 13:58:59 +00:00
|
|
|
#include <cstring>
|
2017-08-01 23:51:44 +00:00
|
|
|
#include <locale>
|
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"
|
2017-09-25 06:17:38 +00:00
|
|
|
#include "core/file_sys/ncch_container.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"
|
2017-08-19 17:14:33 +00:00
|
|
|
#include "network/network.h"
|
2014-06-17 02:57:09 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Loader namespace
|
|
|
|
|
|
|
|
namespace Loader {
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
static const u64 UPDATE_MASK = 0x0000000e00000000;
|
2014-06-18 22:58:09 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
static std::string GetUpdateNCCHPath(u64_le program_id) {
|
|
|
|
u32 high = static_cast<u32>((program_id | UPDATE_MASK) >> 32);
|
|
|
|
u32 low = static_cast<u32>((program_id | UPDATE_MASK) & 0xFFFFFFFF);
|
|
|
|
|
|
|
|
return Common::StringFromFormat("%sNintendo 3DS/%s/%s/title/%08x/%08x/content/00000000.app",
|
|
|
|
FileUtil::GetUserPath(D_SDMC_IDX).c_str(), SYSTEM_ID, SDCARD_ID,
|
|
|
|
high, low);
|
|
|
|
}
|
|
|
|
|
2017-03-09 01:21:31 +00:00
|
|
|
std::pair<boost::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() {
|
2016-11-27 04:13:40 +00:00
|
|
|
if (!is_loaded) {
|
2017-09-25 06:17:38 +00:00
|
|
|
ResultStatus res = base_ncch.Load();
|
2017-03-08 21:23:28 +00:00
|
|
|
if (res != ResultStatus::Success) {
|
2017-03-09 01:21:31 +00:00
|
|
|
return std::make_pair(boost::none, res);
|
2017-03-08 21:23:28 +00:00
|
|
|
}
|
2016-11-27 04:13:40 +00:00
|
|
|
}
|
2017-09-25 06:17:38 +00:00
|
|
|
|
2017-03-08 21:23:28 +00:00
|
|
|
// Set the system mode as the one from the exheader.
|
2017-09-25 06:17:38 +00:00
|
|
|
return std::make_pair(overlay_ncch->exheader_header.arm11_system_local_caps.system_mode.Value(),
|
2017-03-09 01:21:31 +00:00
|
|
|
ResultStatus::Success);
|
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;
|
2017-09-25 06:17:38 +00:00
|
|
|
u64_le program_id;
|
|
|
|
if (ResultStatus::Success == ReadCode(code) &&
|
|
|
|
ResultStatus::Success == ReadProgramId(program_id)) {
|
2015-05-04 03:01:16 +00:00
|
|
|
std::string process_name = Common::StringFromFixedZeroTerminatedBuffer(
|
2017-09-25 06:17:38 +00:00
|
|
|
(const char*)overlay_ncch->exheader_header.codeset_info.name, 8);
|
2015-07-10 01:52:15 +00:00
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
SharedPtr<CodeSet> codeset = CodeSet::Create(process_name, program_id);
|
2015-07-10 01:52:15 +00:00
|
|
|
|
|
|
|
codeset->code.offset = 0;
|
2017-09-25 06:17:38 +00:00
|
|
|
codeset->code.addr = overlay_ncch->exheader_header.codeset_info.text.address;
|
|
|
|
codeset->code.size =
|
|
|
|
overlay_ncch->exheader_header.codeset_info.text.num_max_pages * Memory::PAGE_SIZE;
|
2015-07-10 01:52:15 +00:00
|
|
|
|
|
|
|
codeset->rodata.offset = codeset->code.offset + codeset->code.size;
|
2017-09-25 06:17:38 +00:00
|
|
|
codeset->rodata.addr = overlay_ncch->exheader_header.codeset_info.ro.address;
|
|
|
|
codeset->rodata.size =
|
|
|
|
overlay_ncch->exheader_header.codeset_info.ro.num_max_pages * Memory::PAGE_SIZE;
|
2015-07-10 01:52:15 +00:00
|
|
|
|
|
|
|
// 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.
|
2017-09-25 06:17:38 +00:00
|
|
|
u32 bss_page_size = (overlay_ncch->exheader_header.codeset_info.bss_size + 0xFFF) & ~0xFFF;
|
2015-07-10 01:52:15 +00:00
|
|
|
code.resize(code.size() + bss_page_size, 0);
|
|
|
|
|
|
|
|
codeset->data.offset = codeset->rodata.offset + codeset->rodata.size;
|
2017-09-25 06:17:38 +00:00
|
|
|
codeset->data.addr = overlay_ncch->exheader_header.codeset_info.data.address;
|
2016-09-18 00:38:01 +00:00
|
|
|
codeset->data.size =
|
2017-09-25 06:17:38 +00:00
|
|
|
overlay_ncch->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));
|
2017-09-24 21:42:42 +00:00
|
|
|
Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
|
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>(
|
2017-09-25 06:17:38 +00:00
|
|
|
overlay_ncch->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 =
|
2017-09-25 06:17:38 +00:00
|
|
|
overlay_ncch->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
|
2017-09-25 06:17:38 +00:00
|
|
|
std::array<u32, ARRAY_SIZE(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors)>
|
|
|
|
kernel_caps;
|
|
|
|
std::copy_n(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(),
|
2016-09-18 00:38:01 +00:00
|
|
|
begin(kernel_caps));
|
2015-05-04 03:01:16 +00:00
|
|
|
Kernel::g_current_process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size());
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
s32 priority = overlay_ncch->exheader_header.arm11_system_local_caps.priority;
|
|
|
|
u32 stack_size = overlay_ncch->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
|
|
|
}
|
|
|
|
|
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() {
|
2017-09-25 06:17:38 +00:00
|
|
|
u64_le ncch_program_id;
|
|
|
|
|
2016-04-13 21:04:05 +00:00
|
|
|
if (is_loaded)
|
|
|
|
return ResultStatus::ErrorAlreadyLoaded;
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
ResultStatus result = base_ncch.Load();
|
2016-04-13 21:04:05 +00:00
|
|
|
if (result != ResultStatus::Success)
|
|
|
|
return result;
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
ReadProgramId(ncch_program_id);
|
|
|
|
std::string program_id{Common::StringFromFormat("%016" PRIX64, ncch_program_id)};
|
2017-03-08 15:37:24 +00:00
|
|
|
|
2017-07-13 02:19:34 +00:00
|
|
|
LOG_INFO(Loader, "Program ID: %s", program_id.c_str());
|
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
update_ncch.OpenFile(GetUpdateNCCHPath(ncch_program_id));
|
|
|
|
result = update_ncch.Load();
|
|
|
|
if (result == ResultStatus::Success) {
|
|
|
|
overlay_ncch = &update_ncch;
|
|
|
|
}
|
|
|
|
|
2017-07-13 02:19:34 +00:00
|
|
|
Core::Telemetry().AddField(Telemetry::FieldType::Session, "ProgramId", program_id);
|
2017-05-02 04:10:04 +00:00
|
|
|
|
2017-08-19 17:14:33 +00:00
|
|
|
if (auto room_member = Network::GetRoomMember().lock()) {
|
|
|
|
Network::GameInfo game_info;
|
|
|
|
ReadTitle(game_info.name);
|
2017-09-25 06:17:38 +00:00
|
|
|
game_info.id = ncch_program_id;
|
2017-08-19 17:14:33 +00:00
|
|
|
room_member->SendGameInfo(game_info);
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-09-25 06:17:38 +00:00
|
|
|
return overlay_ncch->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) {
|
2017-09-25 06:17:38 +00:00
|
|
|
return overlay_ncch->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) {
|
2017-09-25 06:17:38 +00:00
|
|
|
return overlay_ncch->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) {
|
2017-09-25 06:17:38 +00:00
|
|
|
return overlay_ncch->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) {
|
2017-09-25 06:17:38 +00:00
|
|
|
ResultStatus result = base_ncch.ReadProgramId(out_program_id);
|
2016-12-15 09:54:25 +00:00
|
|
|
if (result != ResultStatus::Success)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
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) {
|
2017-09-25 06:17:38 +00:00
|
|
|
return base_ncch.ReadRomFS(romfs_file, offset, size);
|
|
|
|
}
|
2015-01-06 21:30:40 +00:00
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
ResultStatus AppLoader_NCCH::ReadUpdateRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
|
|
|
|
u64& offset, u64& size) {
|
|
|
|
ResultStatus result = update_ncch.ReadRomFS(romfs_file, offset, size);
|
2015-07-09 21:55:23 +00:00
|
|
|
|
2017-09-25 06:17:38 +00:00
|
|
|
if (result != ResultStatus::Success)
|
|
|
|
return base_ncch.ReadRomFS(romfs_file, offset, size);
|
2014-06-22 19:40:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:44 +00:00
|
|
|
ResultStatus AppLoader_NCCH::ReadTitle(std::string& title) {
|
|
|
|
std::vector<u8> data;
|
|
|
|
Loader::SMDH smdh;
|
|
|
|
ReadIcon(data);
|
|
|
|
|
|
|
|
if (!Loader::IsValidSMDH(data)) {
|
|
|
|
return ResultStatus::ErrorInvalidFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&smdh, data.data(), sizeof(Loader::SMDH));
|
|
|
|
|
|
|
|
const auto& short_title = smdh.GetShortTitle(SMDH::TitleLanguage::English);
|
|
|
|
auto title_end = std::find(short_title.begin(), short_title.end(), u'\0');
|
|
|
|
title = Common::UTF16ToUTF8(std::u16string{short_title.begin(), title_end});
|
|
|
|
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2014-06-17 02:57:09 +00:00
|
|
|
} // namespace Loader
|