2014-06-17 04:57:09 +02:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 06:38:14 +01:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-06-17 04:57:09 +02:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2015-05-09 10:05:59 +02:00
|
|
|
#include <algorithm>
|
2017-03-08 16:37:24 +01:00
|
|
|
#include <cinttypes>
|
2017-08-02 01:51:44 +02:00
|
|
|
#include <codecvt>
|
2015-06-21 15:58:59 +02:00
|
|
|
#include <cstring>
|
2017-08-02 01:51:44 +02:00
|
|
|
#include <locale>
|
2014-06-19 00:58:09 +02:00
|
|
|
#include <memory>
|
2018-09-17 17:41:33 +02:00
|
|
|
#include <vector>
|
2018-04-30 00:37:15 +02:00
|
|
|
#include <fmt/format.h>
|
2015-05-06 09:06:12 +02:00
|
|
|
#include "common/logging/log.h"
|
2015-05-04 05:01:16 +02:00
|
|
|
#include "common/string_util.h"
|
|
|
|
#include "common/swap.h"
|
2017-05-02 06:10:04 +02:00
|
|
|
#include "core/core.h"
|
2017-09-25 08:17:38 +02:00
|
|
|
#include "core/file_sys/ncch_container.h"
|
2017-10-01 18:41:40 +02:00
|
|
|
#include "core/file_sys/title_metadata.h"
|
2015-06-21 15:58:59 +02:00
|
|
|
#include "core/hle/kernel/process.h"
|
2015-05-12 22:25:15 +02:00
|
|
|
#include "core/hle/kernel/resource_limit.h"
|
2017-10-06 00:23:03 +02:00
|
|
|
#include "core/hle/service/am/am.h"
|
2016-11-30 10:32:09 +01:00
|
|
|
#include "core/hle/service/cfg/cfg.h"
|
2016-05-18 00:06:33 +02:00
|
|
|
#include "core/hle/service/fs/archive.h"
|
2016-09-21 08:52:38 +02:00
|
|
|
#include "core/loader/ncch.h"
|
2016-11-30 10:32:09 +01:00
|
|
|
#include "core/loader/smdh.h"
|
2015-05-13 03:38:29 +02:00
|
|
|
#include "core/memory.h"
|
2017-08-19 19:14:33 +02:00
|
|
|
#include "network/network.h"
|
2014-06-17 04:57:09 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Loader namespace
|
|
|
|
|
|
|
|
namespace Loader {
|
|
|
|
|
2017-09-25 08:17:38 +02:00
|
|
|
static const u64 UPDATE_MASK = 0x0000000e00000000;
|
2014-06-19 00:58:09 +02:00
|
|
|
|
2015-01-07 00:10:13 +01: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;
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:37:55 +02:00
|
|
|
std::pair<std::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() {
|
2016-11-27 05:13:40 +01:00
|
|
|
if (!is_loaded) {
|
2017-09-25 08:17:38 +02:00
|
|
|
ResultStatus res = base_ncch.Load();
|
2017-03-08 22:23:28 +01:00
|
|
|
if (res != ResultStatus::Success) {
|
2018-10-05 12:37:55 +02:00
|
|
|
return std::make_pair(std::optional<u32>{}, res);
|
2017-03-08 22:23:28 +01:00
|
|
|
}
|
2016-11-27 05:13:40 +01:00
|
|
|
}
|
2017-09-25 08:17:38 +02:00
|
|
|
|
2017-03-08 22:23:28 +01:00
|
|
|
// Set the system mode as the one from the exheader.
|
2017-09-25 08:17:38 +02:00
|
|
|
return std::make_pair(overlay_ncch->exheader_header.arm11_system_local_caps.system_mode.Value(),
|
2017-03-09 02:21:31 +01:00
|
|
|
ResultStatus::Success);
|
2016-11-20 02:40:04 +01:00
|
|
|
}
|
|
|
|
|
2019-03-23 21:04:19 +01:00
|
|
|
ResultStatus AppLoader_NCCH::LoadExec(std::shared_ptr<Kernel::Process>& process) {
|
2015-07-10 03:52:15 +02:00
|
|
|
using Kernel::CodeSet;
|
|
|
|
|
2014-11-19 09:49:13 +01:00
|
|
|
if (!is_loaded)
|
2014-06-19 00:58:09 +02:00
|
|
|
return ResultStatus::ErrorNotLoaded;
|
2014-06-17 04:57:09 +02:00
|
|
|
|
2014-06-27 21:33:23 +02:00
|
|
|
std::vector<u8> code;
|
2017-09-25 08:17:38 +02:00
|
|
|
u64_le program_id;
|
|
|
|
if (ResultStatus::Success == ReadCode(code) &&
|
|
|
|
ResultStatus::Success == ReadProgramId(program_id)) {
|
2015-05-04 05:01:16 +02:00
|
|
|
std::string process_name = Common::StringFromFixedZeroTerminatedBuffer(
|
2017-09-25 08:17:38 +02:00
|
|
|
(const char*)overlay_ncch->exheader_header.codeset_info.name, 8);
|
2015-07-10 03:52:15 +02:00
|
|
|
|
2019-03-23 21:04:19 +01:00
|
|
|
std::shared_ptr<CodeSet> codeset =
|
2018-10-12 21:21:32 +02:00
|
|
|
Core::System::GetInstance().Kernel().CreateCodeSet(process_name, program_id);
|
2015-07-10 03:52:15 +02:00
|
|
|
|
2018-08-23 18:23:21 +02:00
|
|
|
codeset->CodeSegment().offset = 0;
|
|
|
|
codeset->CodeSegment().addr = overlay_ncch->exheader_header.codeset_info.text.address;
|
|
|
|
codeset->CodeSegment().size =
|
2017-09-25 08:17:38 +02:00
|
|
|
overlay_ncch->exheader_header.codeset_info.text.num_max_pages * Memory::PAGE_SIZE;
|
2015-07-10 03:52:15 +02:00
|
|
|
|
2018-08-23 18:23:21 +02:00
|
|
|
codeset->RODataSegment().offset =
|
|
|
|
codeset->CodeSegment().offset + codeset->CodeSegment().size;
|
|
|
|
codeset->RODataSegment().addr = overlay_ncch->exheader_header.codeset_info.ro.address;
|
|
|
|
codeset->RODataSegment().size =
|
2017-09-25 08:17:38 +02:00
|
|
|
overlay_ncch->exheader_header.codeset_info.ro.num_max_pages * Memory::PAGE_SIZE;
|
2015-07-10 03:52:15 +02: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 08:17:38 +02:00
|
|
|
u32 bss_page_size = (overlay_ncch->exheader_header.codeset_info.bss_size + 0xFFF) & ~0xFFF;
|
2015-07-10 03:52:15 +02:00
|
|
|
code.resize(code.size() + bss_page_size, 0);
|
|
|
|
|
2018-08-23 18:23:21 +02:00
|
|
|
codeset->DataSegment().offset =
|
|
|
|
codeset->RODataSegment().offset + codeset->RODataSegment().size;
|
|
|
|
codeset->DataSegment().addr = overlay_ncch->exheader_header.codeset_info.data.address;
|
|
|
|
codeset->DataSegment().size =
|
2017-09-25 08:17:38 +02:00
|
|
|
overlay_ncch->exheader_header.codeset_info.data.num_max_pages * Memory::PAGE_SIZE +
|
|
|
|
bss_page_size;
|
2015-07-10 03:52:15 +02:00
|
|
|
|
2019-12-21 13:31:34 +01:00
|
|
|
// Apply patches now that the entire codeset (including .bss) has been allocated
|
|
|
|
const ResultStatus patch_result = overlay_ncch->ApplyCodePatch(code);
|
|
|
|
if (patch_result != ResultStatus::Success && patch_result != ResultStatus::ErrorNotUsed)
|
|
|
|
return patch_result;
|
2019-06-30 18:27:00 +02:00
|
|
|
|
2018-08-23 18:23:21 +02:00
|
|
|
codeset->entrypoint = codeset->CodeSegment().addr;
|
Port various minor changes from yuzu PRs (#4725)
* common/thread: Remove unused functions
Many of these functions are carried over from Dolphin (where they aren't
used anymore). Given these have no use (and we really shouldn't be
screwing around with OS-specific thread scheduler handling from the
emulator, these can be removed.
The function for setting the thread name is left, however, since it can
have debugging utility usages.
* input_common/sdl: Use a type alias to shorten declaration of GetPollers
Just makes the definitions a little bit more tidy.
* input_common/sdl: Correct return values within implementations of GetPollers()
In both cases, we weren't actually returning anything, which is
undefined behavior.
* yuzu/debugger/graphics_surface: Fill in missing surface format listings
Fills in the missing surface types that were marked as unknown. The
order corresponds with the TextureFormat enum within
video_core/texture.h.
We also don't need to all of these strings as translatable (only the
first string, as it's an English word).
* yuzu/debugger/graphics_surface: Clean up connection overload deduction
We can utilize qOverload with the signal connections to make the
function deducing a little less ugly.
* yuzu/debugger/graphics_surface: Tidy up SaveSurface
- Use QStringLiteral where applicable.
- Use const where applicable
- Remove unnecessary precondition check (we already assert the pixbuf
being non null)
* yuzu/debugger/graphics_surface: Display error messages for file I/O errors
* core: Add missing override specifiers where applicable
Applies the override specifier where applicable. In the case of
destructors that are defaulted in their definition, they can
simply be removed.
This also removes the unnecessary inclusions being done in audin_u and
audrec_u, given their close proximity.
* kernel/thread: Make parameter of GetWaitObjectIndex() const qualified
The pointed to member is never actually modified, so it can be made
const.
* kernel/thread: Avoid sign conversion within GetCommandBufferAddress()
Previously this was performing a u64 + int sign conversion. When dealing
with addresses, we should generally be keeping the arithmetic in the
same signedness type.
This also gets rid of the static lifetime of the constant, as there's no
need to make a trivial type like this potentially live for the entire
duration of the program.
* kernel/codeset: Make CodeSet's memory data member a regular std::vector
The use of a shared_ptr is an implementation detail of the VMManager
itself when mapping memory. Because of that, we shouldn't require all
users of the CodeSet to have to allocate the shared_ptr ahead of time.
It's intended that CodeSet simply pass in the required direct data, and
that the memory manager takes care of it from that point on.
This means we just do the shared pointer allocation in a single place,
when loading modules, as opposed to in each loader.
* kernel/wait_object: Make ShouldWait() take thread members by pointer-to-const
Given this is intended as a querying function, it doesn't make sense to
allow the implementer to modify the state of the given thread.
2019-05-01 14:28:49 +02:00
|
|
|
codeset->memory = std::move(code);
|
2015-07-10 03:52:15 +02:00
|
|
|
|
2018-10-12 21:33:36 +02:00
|
|
|
process = Core::System::GetInstance().Kernel().CreateProcess(std::move(codeset));
|
2015-05-04 05:01:16 +02:00
|
|
|
|
2015-05-12 22:25:15 +02:00
|
|
|
// Attach a resource limit to the process based on the resource limit category
|
2017-09-27 01:17:47 +02:00
|
|
|
process->resource_limit =
|
2018-10-13 22:41:34 +02:00
|
|
|
Core::System::GetInstance().Kernel().ResourceLimit().GetForCategory(
|
|
|
|
static_cast<Kernel::ResourceLimitCategory>(
|
|
|
|
overlay_ncch->exheader_header.arm11_system_local_caps.resource_limit_category));
|
2015-05-12 22:25:15 +02:00
|
|
|
|
2016-04-17 21:01:40 +02:00
|
|
|
// Set the default CPU core for this process
|
2017-09-27 01:17:47 +02:00
|
|
|
process->ideal_processor =
|
2017-09-25 08:17:38 +02:00
|
|
|
overlay_ncch->exheader_header.arm11_system_local_caps.ideal_processor;
|
2016-04-17 21:01:40 +02:00
|
|
|
|
2016-10-20 16:26:59 +02:00
|
|
|
// Copy data while converting endianness
|
2017-09-25 08:17:38 +02: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 02:38:01 +02:00
|
|
|
begin(kernel_caps));
|
2017-09-27 01:17:47 +02:00
|
|
|
process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size());
|
2015-05-04 05:01:16 +02:00
|
|
|
|
2017-09-25 08:17:38 +02:00
|
|
|
s32 priority = overlay_ncch->exheader_header.arm11_system_local_caps.priority;
|
|
|
|
u32 stack_size = overlay_ncch->exheader_header.codeset_info.stack_size;
|
2017-09-27 01:17:47 +02:00
|
|
|
process->Run(priority, stack_size);
|
2014-06-27 21:33:23 +02:00
|
|
|
return ResultStatus::Success;
|
2014-06-17 04:57:09 +02:00
|
|
|
}
|
2014-06-27 21:33:23 +02:00
|
|
|
return ResultStatus::Error;
|
2014-06-17 04:57:09 +02:00
|
|
|
}
|
|
|
|
|
2016-11-30 10:32:09 +01: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;
|
2018-09-17 17:41:33 +02:00
|
|
|
std::vector<u32> regions;
|
2016-11-30 10:32:09 +01:00
|
|
|
for (u32 region = 0; region < REGION_COUNT; ++region) {
|
|
|
|
if (region_lockout & 1) {
|
2018-09-17 17:41:33 +02:00
|
|
|
regions.push_back(region);
|
2016-11-30 10:32:09 +01:00
|
|
|
}
|
|
|
|
region_lockout >>= 1;
|
|
|
|
}
|
2018-10-12 11:50:50 +02:00
|
|
|
auto cfg = Service::CFG::GetModule(Core::System::GetInstance());
|
|
|
|
ASSERT_MSG(cfg, "CFG Module missing!");
|
|
|
|
cfg->SetPreferredRegionCodes(regions);
|
2016-11-30 10:32:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-23 21:04:19 +01:00
|
|
|
ResultStatus AppLoader_NCCH::Load(std::shared_ptr<Kernel::Process>& process) {
|
2017-09-25 08:17:38 +02:00
|
|
|
u64_le ncch_program_id;
|
|
|
|
|
2016-04-13 23:04:05 +02:00
|
|
|
if (is_loaded)
|
|
|
|
return ResultStatus::ErrorAlreadyLoaded;
|
|
|
|
|
2017-09-25 08:17:38 +02:00
|
|
|
ResultStatus result = base_ncch.Load();
|
2016-04-13 23:04:05 +02:00
|
|
|
if (result != ResultStatus::Success)
|
|
|
|
return result;
|
|
|
|
|
2017-09-25 08:17:38 +02:00
|
|
|
ReadProgramId(ncch_program_id);
|
2018-04-30 00:37:15 +02:00
|
|
|
std::string program_id{fmt::format("{:016X}", ncch_program_id)};
|
2017-03-08 16:37:24 +01:00
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_INFO(Loader, "Program ID: {}", program_id);
|
2017-07-13 04:19:34 +02:00
|
|
|
|
2017-10-06 00:23:03 +02:00
|
|
|
update_ncch.OpenFile(Service::AM::GetTitleContentPath(Service::FS::MediaType::SDMC,
|
|
|
|
ncch_program_id | UPDATE_MASK));
|
2017-09-25 08:17:38 +02:00
|
|
|
result = update_ncch.Load();
|
|
|
|
if (result == ResultStatus::Success) {
|
|
|
|
overlay_ncch = &update_ncch;
|
|
|
|
}
|
|
|
|
|
2019-03-09 18:46:37 +01:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
system.TelemetrySession().AddField(Telemetry::FieldType::Session, "ProgramId", program_id);
|
2017-05-02 06:10:04 +02:00
|
|
|
|
2017-08-19 19:14:33 +02:00
|
|
|
if (auto room_member = Network::GetRoomMember().lock()) {
|
|
|
|
Network::GameInfo game_info;
|
|
|
|
ReadTitle(game_info.name);
|
2017-09-25 08:17:38 +02:00
|
|
|
game_info.id = ncch_program_id;
|
2017-08-19 19:14:33 +02:00
|
|
|
room_member->SendGameInfo(game_info);
|
|
|
|
}
|
|
|
|
|
2015-01-06 22:30:40 +01:00
|
|
|
is_loaded = true; // Set state to loaded
|
|
|
|
|
2017-09-27 01:17:47 +02:00
|
|
|
result = LoadExec(process); // Load the executable into memory for booting
|
2016-05-18 00:06:33 +02:00
|
|
|
if (ResultStatus::Success != result)
|
|
|
|
return result;
|
|
|
|
|
2019-03-09 18:46:37 +01:00
|
|
|
system.ArchiveManager().RegisterSelfNCCH(*this);
|
2016-11-30 10:32:09 +01:00
|
|
|
|
|
|
|
ParseRegionLockoutInfo();
|
|
|
|
|
2016-05-18 00:06:33 +02:00
|
|
|
return ResultStatus::Success;
|
2014-06-17 04:57:09 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 10:52:18 +02:00
|
|
|
ResultStatus AppLoader_NCCH::IsExecutable(bool& out_executable) {
|
|
|
|
Loader::ResultStatus result = overlay_ncch->Load();
|
|
|
|
if (result != Loader::ResultStatus::Success)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
out_executable = overlay_ncch->ncch_header.is_executable != 0;
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2015-07-12 00:16:33 +02:00
|
|
|
ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) {
|
2017-09-25 08:17:38 +02:00
|
|
|
return overlay_ncch->LoadSectionExeFS(".code", buffer);
|
2014-06-22 21:40:21 +02:00
|
|
|
}
|
|
|
|
|
2015-07-12 00:16:33 +02:00
|
|
|
ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) {
|
2017-09-25 08:17:38 +02:00
|
|
|
return overlay_ncch->LoadSectionExeFS("icon", buffer);
|
2014-06-22 21:40:21 +02:00
|
|
|
}
|
|
|
|
|
2015-07-12 00:16:33 +02:00
|
|
|
ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) {
|
2017-09-25 08:17:38 +02:00
|
|
|
return overlay_ncch->LoadSectionExeFS("banner", buffer);
|
2014-06-22 21:40:21 +02:00
|
|
|
}
|
|
|
|
|
2015-07-12 00:16:33 +02:00
|
|
|
ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) {
|
2017-09-25 08:17:38 +02:00
|
|
|
return overlay_ncch->LoadSectionExeFS("logo", buffer);
|
2014-06-22 21:40:21 +02:00
|
|
|
}
|
|
|
|
|
2016-12-15 10:54:25 +01:00
|
|
|
ResultStatus AppLoader_NCCH::ReadProgramId(u64& out_program_id) {
|
2017-09-25 08:17:38 +02:00
|
|
|
ResultStatus result = base_ncch.ReadProgramId(out_program_id);
|
2016-12-15 10:54:25 +01:00
|
|
|
if (result != ResultStatus::Success)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
return ResultStatus::Success;
|
2018-09-16 06:48:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ResultStatus AppLoader_NCCH::ReadExtdataId(u64& out_extdata_id) {
|
|
|
|
ResultStatus result = base_ncch.ReadExtdataId(out_extdata_id);
|
|
|
|
if (result != ResultStatus::Success)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
return ResultStatus::Success;
|
2016-12-15 10:54:25 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 17:30:54 +02:00
|
|
|
ResultStatus AppLoader_NCCH::ReadRomFS(std::shared_ptr<FileSys::RomFSReader>& romfs_file) {
|
|
|
|
return base_ncch.ReadRomFS(romfs_file);
|
2017-09-25 08:17:38 +02:00
|
|
|
}
|
2015-01-06 22:30:40 +01:00
|
|
|
|
2018-07-28 17:30:54 +02:00
|
|
|
ResultStatus AppLoader_NCCH::ReadUpdateRomFS(std::shared_ptr<FileSys::RomFSReader>& romfs_file) {
|
|
|
|
ResultStatus result = update_ncch.ReadRomFS(romfs_file);
|
2015-07-09 23:55:23 +02:00
|
|
|
|
2017-09-25 08:17:38 +02:00
|
|
|
if (result != ResultStatus::Success)
|
2018-07-28 17:30:54 +02:00
|
|
|
return base_ncch.ReadRomFS(romfs_file);
|
2017-12-11 07:33:02 +01:00
|
|
|
|
|
|
|
return ResultStatus::Success;
|
2014-06-22 21:40:21 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 01:51:44 +02: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 04:57:09 +02:00
|
|
|
} // namespace Loader
|