2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-10-06 05:30:08 +02:00
|
|
|
|
2018-07-20 06:10:21 +02:00
|
|
|
#include <utility>
|
2017-10-06 05:30:08 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2017-10-15 06:11:38 +02:00
|
|
|
#include "common/common_funcs.h"
|
2018-07-23 23:27:50 +02:00
|
|
|
#include "common/common_types.h"
|
2017-10-06 05:30:08 +02:00
|
|
|
#include "common/logging/log.h"
|
2021-04-15 01:07:40 +02:00
|
|
|
#include "common/settings.h"
|
2017-10-06 05:30:08 +02:00
|
|
|
#include "common/swap.h"
|
2019-04-22 23:56:56 +02:00
|
|
|
#include "core/core.h"
|
2018-07-23 18:33:24 +02:00
|
|
|
#include "core/file_sys/control_metadata.h"
|
2018-10-28 19:56:12 +01:00
|
|
|
#include "core/file_sys/romfs_factory.h"
|
2024-01-16 06:23:01 +01:00
|
|
|
#include "core/file_sys/vfs/vfs_offset.h"
|
2019-03-20 17:40:09 +01:00
|
|
|
#include "core/hle/kernel/code_set.h"
|
2021-02-13 02:58:31 +01:00
|
|
|
#include "core/hle/kernel/k_page_table.h"
|
2021-04-24 07:04:28 +02:00
|
|
|
#include "core/hle/kernel/k_process.h"
|
2020-12-31 08:01:08 +01:00
|
|
|
#include "core/hle/kernel/k_thread.h"
|
2018-10-28 19:56:12 +01:00
|
|
|
#include "core/hle/service/filesystem/filesystem.h"
|
2017-10-06 05:30:08 +02:00
|
|
|
#include "core/loader/nro.h"
|
2018-09-30 20:04:48 +02:00
|
|
|
#include "core/loader/nso.h"
|
2017-10-06 05:30:08 +02:00
|
|
|
#include "core/memory.h"
|
|
|
|
|
2023-11-20 14:52:18 +01:00
|
|
|
#ifdef HAS_NCE
|
2023-11-29 22:49:16 +01:00
|
|
|
#include "core/arm/nce/patcher.h"
|
2023-11-17 22:44:53 +01:00
|
|
|
#endif
|
|
|
|
|
2017-10-06 05:30:08 +02:00
|
|
|
namespace Loader {
|
|
|
|
|
|
|
|
struct NroSegmentHeader {
|
|
|
|
u32_le offset;
|
|
|
|
u32_le size;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(NroSegmentHeader) == 0x8, "NroSegmentHeader has incorrect size.");
|
|
|
|
|
|
|
|
struct NroHeader {
|
|
|
|
INSERT_PADDING_BYTES(0x4);
|
|
|
|
u32_le module_header_offset;
|
2023-06-09 22:11:30 +02:00
|
|
|
u32 magic_ext1;
|
|
|
|
u32 magic_ext2;
|
2017-10-06 05:30:08 +02:00
|
|
|
u32_le magic;
|
|
|
|
INSERT_PADDING_BYTES(0x4);
|
|
|
|
u32_le file_size;
|
|
|
|
INSERT_PADDING_BYTES(0x4);
|
|
|
|
std::array<NroSegmentHeader, 3> segments; // Text, RoData, Data (in that order)
|
|
|
|
u32_le bss_size;
|
|
|
|
INSERT_PADDING_BYTES(0x44);
|
|
|
|
};
|
|
|
|
static_assert(sizeof(NroHeader) == 0x80, "NroHeader has incorrect size.");
|
|
|
|
|
|
|
|
struct ModHeader {
|
|
|
|
u32_le magic;
|
|
|
|
u32_le dynamic_offset;
|
|
|
|
u32_le bss_start_offset;
|
|
|
|
u32_le bss_end_offset;
|
|
|
|
u32_le unwind_start_offset;
|
|
|
|
u32_le unwind_end_offset;
|
|
|
|
u32_le module_offset; // Offset to runtime-generated module object. typically equal to .bss base
|
|
|
|
};
|
|
|
|
static_assert(sizeof(ModHeader) == 0x1c, "ModHeader has incorrect size.");
|
|
|
|
|
2018-07-23 18:33:24 +02:00
|
|
|
struct AssetSection {
|
|
|
|
u64_le offset;
|
|
|
|
u64_le size;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(AssetSection) == 0x10, "AssetSection has incorrect size.");
|
|
|
|
|
|
|
|
struct AssetHeader {
|
|
|
|
u32_le magic;
|
|
|
|
u32_le format_version;
|
|
|
|
AssetSection icon;
|
|
|
|
AssetSection nacp;
|
|
|
|
AssetSection romfs;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(AssetHeader) == 0x38, "AssetHeader has incorrect size.");
|
|
|
|
|
2021-04-27 18:05:34 +02:00
|
|
|
AppLoader_NRO::AppLoader_NRO(FileSys::VirtualFile file_) : AppLoader(std::move(file_)) {
|
2018-07-23 18:33:24 +02:00
|
|
|
NroHeader nro_header{};
|
2018-07-23 23:24:27 +02:00
|
|
|
if (file->ReadObject(&nro_header) != sizeof(NroHeader)) {
|
2018-07-23 18:33:24 +02:00
|
|
|
return;
|
2018-07-23 23:24:27 +02:00
|
|
|
}
|
2018-07-23 18:33:24 +02:00
|
|
|
|
|
|
|
if (file->GetSize() >= nro_header.file_size + sizeof(AssetHeader)) {
|
2018-07-23 23:24:27 +02:00
|
|
|
const u64 offset = nro_header.file_size;
|
2018-07-23 18:33:24 +02:00
|
|
|
AssetHeader asset_header{};
|
2018-07-23 23:24:27 +02:00
|
|
|
if (file->ReadObject(&asset_header, offset) != sizeof(AssetHeader)) {
|
2018-07-23 18:33:24 +02:00
|
|
|
return;
|
2018-07-23 23:24:27 +02:00
|
|
|
}
|
2018-07-23 18:33:24 +02:00
|
|
|
|
2018-07-23 23:24:27 +02:00
|
|
|
if (asset_header.format_version != 0) {
|
2018-07-23 18:33:24 +02:00
|
|
|
LOG_WARNING(Loader,
|
|
|
|
"NRO Asset Header has format {}, currently supported format is 0. If "
|
|
|
|
"strange glitches occur with metadata, check NRO assets.",
|
|
|
|
asset_header.format_version);
|
2018-07-23 23:24:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (asset_header.magic != Common::MakeMagic('A', 'S', 'E', 'T')) {
|
2018-07-23 18:33:24 +02:00
|
|
|
return;
|
2018-07-23 23:24:27 +02:00
|
|
|
}
|
2018-07-23 18:33:24 +02:00
|
|
|
|
|
|
|
if (asset_header.nacp.size > 0) {
|
|
|
|
nacp = std::make_unique<FileSys::NACP>(std::make_shared<FileSys::OffsetVfsFile>(
|
|
|
|
file, asset_header.nacp.size, offset + asset_header.nacp.offset, "Control.nacp"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asset_header.romfs.size > 0) {
|
|
|
|
romfs = std::make_shared<FileSys::OffsetVfsFile>(
|
|
|
|
file, asset_header.romfs.size, offset + asset_header.romfs.offset, "game.romfs");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asset_header.icon.size > 0) {
|
|
|
|
icon_data = file->ReadBytes(asset_header.icon.size, offset + asset_header.icon.offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-20 21:48:37 +01:00
|
|
|
|
2018-07-23 23:27:50 +02:00
|
|
|
AppLoader_NRO::~AppLoader_NRO() = default;
|
|
|
|
|
2021-04-27 18:05:34 +02:00
|
|
|
FileType AppLoader_NRO::IdentifyType(const FileSys::VirtualFile& nro_file) {
|
2017-10-06 05:30:08 +02:00
|
|
|
// Read NSO header
|
|
|
|
NroHeader nro_header{};
|
2021-04-27 18:05:34 +02:00
|
|
|
if (sizeof(NroHeader) != nro_file->ReadObject(&nro_header)) {
|
2017-10-06 05:30:08 +02:00
|
|
|
return FileType::Error;
|
|
|
|
}
|
2017-10-15 06:11:38 +02:00
|
|
|
if (nro_header.magic == Common::MakeMagic('N', 'R', 'O', '0')) {
|
2017-10-06 05:30:08 +02:00
|
|
|
return FileType::NRO;
|
|
|
|
}
|
|
|
|
return FileType::Error;
|
|
|
|
}
|
|
|
|
|
2023-06-09 22:11:30 +02:00
|
|
|
bool AppLoader_NRO::IsHomebrew() {
|
|
|
|
// Read NSO header
|
|
|
|
NroHeader nro_header{};
|
|
|
|
if (sizeof(NroHeader) != file->ReadObject(&nro_header)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return nro_header.magic_ext1 == Common::MakeMagic('H', 'O', 'M', 'E') &&
|
|
|
|
nro_header.magic_ext2 == Common::MakeMagic('B', 'R', 'E', 'W');
|
|
|
|
}
|
|
|
|
|
2017-10-06 05:30:08 +02:00
|
|
|
static constexpr u32 PageAlignSize(u32 size) {
|
2022-08-19 01:28:55 +02:00
|
|
|
return static_cast<u32>((size + Core::Memory::YUZU_PAGEMASK) & ~Core::Memory::YUZU_PAGEMASK);
|
2017-10-06 05:30:08 +02:00
|
|
|
}
|
|
|
|
|
2023-11-17 22:44:53 +01:00
|
|
|
static bool LoadNroImpl(Core::System& system, Kernel::KProcess& process,
|
|
|
|
const std::vector<u8>& data) {
|
2018-10-24 00:35:20 +02:00
|
|
|
if (data.size() < sizeof(NroHeader)) {
|
2017-10-06 05:30:08 +02:00
|
|
|
return {};
|
|
|
|
}
|
2018-10-24 00:35:20 +02:00
|
|
|
|
|
|
|
// Read NSO header
|
|
|
|
NroHeader nro_header{};
|
|
|
|
std::memcpy(&nro_header, data.data(), sizeof(NroHeader));
|
2017-10-15 06:11:38 +02:00
|
|
|
if (nro_header.magic != Common::MakeMagic('N', 'R', 'O', '0')) {
|
2017-10-06 05:30:08 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build program image
|
2019-07-19 00:15:53 +02:00
|
|
|
Kernel::PhysicalMemory program_image(PageAlignSize(nro_header.file_size));
|
2018-10-24 00:35:20 +02:00
|
|
|
std::memcpy(program_image.data(), data.data(), program_image.size());
|
2018-07-23 23:24:27 +02:00
|
|
|
if (program_image.size() != PageAlignSize(nro_header.file_size)) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return {};
|
2018-07-23 23:24:27 +02:00
|
|
|
}
|
2017-10-06 05:30:08 +02:00
|
|
|
|
2018-10-12 17:36:31 +02:00
|
|
|
Kernel::CodeSet codeset;
|
2018-07-19 02:29:04 +02:00
|
|
|
for (std::size_t i = 0; i < nro_header.segments.size(); ++i) {
|
2018-10-12 17:36:31 +02:00
|
|
|
codeset.segments[i].addr = nro_header.segments[i].offset;
|
|
|
|
codeset.segments[i].offset = nro_header.segments[i].offset;
|
|
|
|
codeset.segments[i].size = PageAlignSize(nro_header.segments[i].size);
|
2017-10-06 05:30:08 +02:00
|
|
|
}
|
|
|
|
|
2021-06-28 21:58:16 +02:00
|
|
|
if (!Settings::values.program_args.GetValue().empty()) {
|
|
|
|
const auto arg_data = Settings::values.program_args.GetValue();
|
2018-10-12 17:36:31 +02:00
|
|
|
codeset.DataSegment().size += NSO_ARGUMENT_DATA_ALLOCATION_SIZE;
|
2018-10-05 19:52:07 +02:00
|
|
|
NSOArgumentHeader args_header{
|
|
|
|
NSO_ARGUMENT_DATA_ALLOCATION_SIZE, static_cast<u32_le>(arg_data.size()), {}};
|
|
|
|
const auto end_offset = program_image.size();
|
|
|
|
program_image.resize(static_cast<u32>(program_image.size()) +
|
|
|
|
NSO_ARGUMENT_DATA_ALLOCATION_SIZE);
|
|
|
|
std::memcpy(program_image.data() + end_offset, &args_header, sizeof(NSOArgumentHeader));
|
|
|
|
std::memcpy(program_image.data() + end_offset + sizeof(NSOArgumentHeader), arg_data.data(),
|
2018-09-30 20:04:48 +02:00
|
|
|
arg_data.size());
|
|
|
|
}
|
|
|
|
|
2018-01-18 00:16:09 +01:00
|
|
|
// Default .bss to NRO header bss size if MOD0 section doesn't exist
|
|
|
|
u32 bss_size{PageAlignSize(nro_header.bss_size)};
|
2018-10-30 02:55:06 +01:00
|
|
|
|
|
|
|
// Read MOD header
|
|
|
|
ModHeader mod_header{};
|
2017-10-06 05:30:08 +02:00
|
|
|
std::memcpy(&mod_header, program_image.data() + nro_header.module_header_offset,
|
|
|
|
sizeof(ModHeader));
|
2018-10-30 02:55:06 +01:00
|
|
|
|
2017-10-15 06:11:38 +02:00
|
|
|
const bool has_mod_header{mod_header.magic == Common::MakeMagic('M', 'O', 'D', '0')};
|
2017-10-06 05:30:08 +02:00
|
|
|
if (has_mod_header) {
|
|
|
|
// Resize program image to include .bss section and page align each section
|
|
|
|
bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset);
|
|
|
|
}
|
2018-10-30 02:55:06 +01:00
|
|
|
|
2018-10-12 17:36:31 +02:00
|
|
|
codeset.DataSegment().size += bss_size;
|
2018-01-18 21:18:43 +01:00
|
|
|
program_image.resize(static_cast<u32>(program_image.size()) + bss_size);
|
2023-11-18 15:10:39 +01:00
|
|
|
size_t image_size = program_image.size();
|
2017-10-06 05:30:08 +02:00
|
|
|
|
2023-11-20 14:52:18 +01:00
|
|
|
#ifdef HAS_NCE
|
2023-11-17 22:44:53 +01:00
|
|
|
const auto& code = codeset.CodeSegment();
|
|
|
|
|
2023-11-19 10:21:53 +01:00
|
|
|
// NROs always have a 39-bit address space.
|
2023-11-17 22:44:53 +01:00
|
|
|
Settings::SetNceEnabled(true);
|
|
|
|
|
|
|
|
// Create NCE patcher
|
|
|
|
Core::NCE::Patcher patch{};
|
|
|
|
|
|
|
|
if (Settings::IsNceEnabled()) {
|
|
|
|
// Patch SVCs and MRS calls in the guest code
|
|
|
|
patch.PatchText(program_image, code);
|
|
|
|
|
|
|
|
// We only support PostData patching for NROs.
|
2023-11-19 10:21:53 +01:00
|
|
|
ASSERT(patch.GetPatchMode() == Core::NCE::PatchMode::PostData);
|
2023-11-17 22:44:53 +01:00
|
|
|
|
|
|
|
// Update patch section.
|
|
|
|
auto& patch_segment = codeset.PatchSegment();
|
|
|
|
patch_segment.addr = image_size;
|
2023-11-19 10:21:53 +01:00
|
|
|
patch_segment.size = static_cast<u32>(patch.GetSectionSize());
|
2023-11-17 22:44:53 +01:00
|
|
|
|
|
|
|
// Add patch section size to the module size.
|
|
|
|
image_size += patch_segment.size;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Enable direct memory mapping in case of NCE.
|
|
|
|
const u64 fastmem_base = [&]() -> size_t {
|
|
|
|
if (Settings::IsNceEnabled()) {
|
|
|
|
auto& buffer = system.DeviceMemory().buffer;
|
|
|
|
buffer.EnableDirectMappedAddress();
|
|
|
|
return reinterpret_cast<u64>(buffer.VirtualBasePointer());
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}();
|
|
|
|
|
2020-04-21 00:16:55 +02:00
|
|
|
// Setup the process code layout
|
2023-09-14 20:34:05 +02:00
|
|
|
if (process
|
2023-11-17 22:44:53 +01:00
|
|
|
.LoadFromMetadata(FileSys::ProgramMetadata::GetDefault(), image_size, fastmem_base,
|
2023-11-17 20:58:29 +01:00
|
|
|
false)
|
2020-04-21 00:16:55 +02:00
|
|
|
.IsError()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-17 22:44:53 +01:00
|
|
|
// Relocate code patch and copy to the program_image if running under NCE.
|
|
|
|
// This needs to be after LoadFromMetadata so we can use the process entry point.
|
2023-11-20 14:52:18 +01:00
|
|
|
#ifdef HAS_NCE
|
2023-11-17 22:44:53 +01:00
|
|
|
if (Settings::IsNceEnabled()) {
|
|
|
|
patch.RelocateAndCopy(process.GetEntryPoint(), code, program_image,
|
|
|
|
&process.GetPostHandlers());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-10-06 05:30:08 +02:00
|
|
|
// Load codeset for current process
|
2019-03-22 19:51:36 +01:00
|
|
|
codeset.memory = std::move(program_image);
|
2023-08-25 23:59:32 +02:00
|
|
|
process.LoadModule(std::move(codeset), process.GetEntryPoint());
|
2017-10-06 05:30:08 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-17 22:44:53 +01:00
|
|
|
bool AppLoader_NRO::LoadNro(Core::System& system, Kernel::KProcess& process,
|
|
|
|
const FileSys::VfsFile& nro_file) {
|
|
|
|
return LoadNroImpl(system, process, nro_file.ReadAllBytes());
|
2018-10-24 00:35:20 +02:00
|
|
|
}
|
|
|
|
|
2021-04-24 07:04:28 +02:00
|
|
|
AppLoader_NRO::LoadResult AppLoader_NRO::Load(Kernel::KProcess& process, Core::System& system) {
|
2017-10-06 05:30:08 +02:00
|
|
|
if (is_loaded) {
|
2019-04-09 23:03:04 +02:00
|
|
|
return {ResultStatus::ErrorAlreadyLoaded, {}};
|
2017-10-06 05:30:08 +02:00
|
|
|
}
|
|
|
|
|
2023-11-17 22:44:53 +01:00
|
|
|
if (!LoadNro(system, process, *file)) {
|
2019-04-09 23:03:04 +02:00
|
|
|
return {ResultStatus::ErrorLoadingNRO, {}};
|
2017-10-06 05:30:08 +02:00
|
|
|
}
|
|
|
|
|
2024-01-13 19:45:05 +01:00
|
|
|
u64 program_id{};
|
|
|
|
ReadProgramId(program_id);
|
|
|
|
system.GetFileSystemController().RegisterProcess(
|
|
|
|
process.GetProcessId(), program_id,
|
|
|
|
std::make_unique<FileSys::RomFSFactory>(*this, system.GetContentProvider(),
|
|
|
|
system.GetFileSystemController()));
|
2017-10-06 05:30:08 +02:00
|
|
|
|
|
|
|
is_loaded = true;
|
2021-01-01 11:06:06 +01:00
|
|
|
return {ResultStatus::Success, LoadParameters{Kernel::KThread::DefaultThreadPriority,
|
|
|
|
Core::Memory::DEFAULT_STACK_SIZE}};
|
2017-10-06 05:30:08 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 18:33:24 +02:00
|
|
|
ResultStatus AppLoader_NRO::ReadIcon(std::vector<u8>& buffer) {
|
2018-07-23 23:24:27 +02:00
|
|
|
if (icon_data.empty()) {
|
2018-08-10 03:06:44 +02:00
|
|
|
return ResultStatus::ErrorNoIcon;
|
2018-07-23 23:24:27 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 18:33:24 +02:00
|
|
|
buffer = icon_data;
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultStatus AppLoader_NRO::ReadProgramId(u64& out_program_id) {
|
2018-07-23 23:24:27 +02:00
|
|
|
if (nacp == nullptr) {
|
2018-08-10 03:06:44 +02:00
|
|
|
return ResultStatus::ErrorNoControl;
|
2018-07-23 23:24:27 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 18:33:24 +02:00
|
|
|
out_program_id = nacp->GetTitleId();
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultStatus AppLoader_NRO::ReadRomFS(FileSys::VirtualFile& dir) {
|
2018-07-23 23:24:27 +02:00
|
|
|
if (romfs == nullptr) {
|
2018-08-10 03:06:44 +02:00
|
|
|
return ResultStatus::ErrorNoRomFS;
|
2018-07-23 23:24:27 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 18:33:24 +02:00
|
|
|
dir = romfs;
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultStatus AppLoader_NRO::ReadTitle(std::string& title) {
|
2018-07-23 23:24:27 +02:00
|
|
|
if (nacp == nullptr) {
|
2018-08-10 03:06:44 +02:00
|
|
|
return ResultStatus::ErrorNoControl;
|
2018-07-23 23:24:27 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 18:33:24 +02:00
|
|
|
title = nacp->GetApplicationName();
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
2018-08-26 01:05:04 +02:00
|
|
|
|
2019-09-07 23:40:21 +02:00
|
|
|
ResultStatus AppLoader_NRO::ReadControlData(FileSys::NACP& control) {
|
|
|
|
if (nacp == nullptr) {
|
|
|
|
return ResultStatus::ErrorNoControl;
|
|
|
|
}
|
|
|
|
|
|
|
|
control = *nacp;
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2018-08-26 16:53:31 +02:00
|
|
|
bool AppLoader_NRO::IsRomFSUpdatable() const {
|
2018-08-26 01:05:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-06 05:30:08 +02:00
|
|
|
} // namespace Loader
|