2018-01-14 00:22:39 +03:00
|
|
|
// Copyright 2018 yuzu emulator team
|
2017-10-06 06:30:08 +03:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-20 07:10:21 +03:00
|
|
|
#include <utility>
|
2017-10-06 06:30:08 +03:00
|
|
|
#include <vector>
|
|
|
|
|
2017-10-15 07:11:38 +03:00
|
|
|
#include "common/common_funcs.h"
|
2018-07-24 00:27:50 +03:00
|
|
|
#include "common/common_types.h"
|
2018-01-20 23:48:37 +03:00
|
|
|
#include "common/file_util.h"
|
2017-10-06 06:30:08 +03:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/swap.h"
|
2018-03-14 00:49:59 +03:00
|
|
|
#include "core/core.h"
|
2018-07-23 19:33:24 +03:00
|
|
|
#include "core/file_sys/control_metadata.h"
|
|
|
|
#include "core/file_sys/vfs_offset.h"
|
2018-07-13 06:22:59 +03:00
|
|
|
#include "core/gdbstub/gdbstub.h"
|
2018-08-31 19:21:34 +03:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
2017-10-06 06:30:08 +03:00
|
|
|
#include "core/hle/kernel/process.h"
|
2018-09-25 03:01:45 +03:00
|
|
|
#include "core/hle/kernel/vm_manager.h"
|
2017-10-06 06:30:08 +03:00
|
|
|
#include "core/loader/nro.h"
|
2018-09-30 21:04:48 +03:00
|
|
|
#include "core/loader/nso.h"
|
2017-10-06 06:30:08 +03:00
|
|
|
#include "core/memory.h"
|
2018-09-30 21:04:48 +03:00
|
|
|
#include "core/settings.h"
|
2017-10-06 06:30:08 +03: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;
|
|
|
|
INSERT_PADDING_BYTES(0x8);
|
|
|
|
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 19:33:24 +03: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.");
|
|
|
|
|
|
|
|
AppLoader_NRO::AppLoader_NRO(FileSys::VirtualFile file) : AppLoader(file) {
|
|
|
|
NroHeader nro_header{};
|
2018-07-24 00:24:27 +03:00
|
|
|
if (file->ReadObject(&nro_header) != sizeof(NroHeader)) {
|
2018-07-23 19:33:24 +03:00
|
|
|
return;
|
2018-07-24 00:24:27 +03:00
|
|
|
}
|
2018-07-23 19:33:24 +03:00
|
|
|
|
|
|
|
if (file->GetSize() >= nro_header.file_size + sizeof(AssetHeader)) {
|
2018-07-24 00:24:27 +03:00
|
|
|
const u64 offset = nro_header.file_size;
|
2018-07-23 19:33:24 +03:00
|
|
|
AssetHeader asset_header{};
|
2018-07-24 00:24:27 +03:00
|
|
|
if (file->ReadObject(&asset_header, offset) != sizeof(AssetHeader)) {
|
2018-07-23 19:33:24 +03:00
|
|
|
return;
|
2018-07-24 00:24:27 +03:00
|
|
|
}
|
2018-07-23 19:33:24 +03:00
|
|
|
|
2018-07-24 00:24:27 +03:00
|
|
|
if (asset_header.format_version != 0) {
|
2018-07-23 19:33:24 +03: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-24 00:24:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (asset_header.magic != Common::MakeMagic('A', 'S', 'E', 'T')) {
|
2018-07-23 19:33:24 +03:00
|
|
|
return;
|
2018-07-24 00:24:27 +03:00
|
|
|
}
|
2018-07-23 19:33:24 +03: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 23:48:37 +03:00
|
|
|
|
2018-07-24 00:27:50 +03:00
|
|
|
AppLoader_NRO::~AppLoader_NRO() = default;
|
|
|
|
|
2018-07-19 04:07:11 +03:00
|
|
|
FileType AppLoader_NRO::IdentifyType(const FileSys::VirtualFile& file) {
|
2017-10-06 06:30:08 +03:00
|
|
|
// Read NSO header
|
|
|
|
NroHeader nro_header{};
|
2018-07-19 04:07:11 +03:00
|
|
|
if (sizeof(NroHeader) != file->ReadObject(&nro_header)) {
|
2017-10-06 06:30:08 +03:00
|
|
|
return FileType::Error;
|
|
|
|
}
|
2017-10-15 07:11:38 +03:00
|
|
|
if (nro_header.magic == Common::MakeMagic('N', 'R', 'O', '0')) {
|
2017-10-06 06:30:08 +03:00
|
|
|
return FileType::NRO;
|
|
|
|
}
|
|
|
|
return FileType::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr u32 PageAlignSize(u32 size) {
|
|
|
|
return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK;
|
|
|
|
}
|
|
|
|
|
2018-07-19 04:07:11 +03:00
|
|
|
bool AppLoader_NRO::LoadNro(FileSys::VirtualFile file, VAddr load_base) {
|
2017-10-06 06:30:08 +03:00
|
|
|
// Read NSO header
|
|
|
|
NroHeader nro_header{};
|
2018-07-19 04:07:11 +03:00
|
|
|
if (sizeof(NroHeader) != file->ReadObject(&nro_header)) {
|
2017-10-06 06:30:08 +03:00
|
|
|
return {};
|
|
|
|
}
|
2017-10-15 07:11:38 +03:00
|
|
|
if (nro_header.magic != Common::MakeMagic('N', 'R', 'O', '0')) {
|
2017-10-06 06:30:08 +03:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build program image
|
2018-08-28 19:30:33 +03:00
|
|
|
auto& kernel = Core::System::GetInstance().Kernel();
|
|
|
|
Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create(kernel, "");
|
2018-07-19 04:07:11 +03:00
|
|
|
std::vector<u8> program_image = file->ReadBytes(PageAlignSize(nro_header.file_size));
|
2018-07-24 00:24:27 +03:00
|
|
|
if (program_image.size() != PageAlignSize(nro_header.file_size)) {
|
2018-07-19 04:07:11 +03:00
|
|
|
return {};
|
2018-07-24 00:24:27 +03:00
|
|
|
}
|
2017-10-06 06:30:08 +03:00
|
|
|
|
2018-07-19 03:29:04 +03:00
|
|
|
for (std::size_t i = 0; i < nro_header.segments.size(); ++i) {
|
2017-10-06 06:30:08 +03: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);
|
|
|
|
}
|
|
|
|
|
2018-09-30 21:04:48 +03:00
|
|
|
if (!Settings::values.program_args.empty()) {
|
|
|
|
const auto arg_data = Settings::values.program_args;
|
2018-10-05 20:52:07 +03:00
|
|
|
codeset->DataSegment().size += NSO_ARGUMENT_DATA_ALLOCATION_SIZE;
|
|
|
|
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 21:04:48 +03:00
|
|
|
arg_data.size());
|
|
|
|
}
|
|
|
|
|
2017-10-06 06:30:08 +03:00
|
|
|
// Read MOD header
|
|
|
|
ModHeader mod_header{};
|
2018-01-18 02:16:09 +03:00
|
|
|
// Default .bss to NRO header bss size if MOD0 section doesn't exist
|
|
|
|
u32 bss_size{PageAlignSize(nro_header.bss_size)};
|
2017-10-06 06:30:08 +03:00
|
|
|
std::memcpy(&mod_header, program_image.data() + nro_header.module_header_offset,
|
|
|
|
sizeof(ModHeader));
|
2017-10-15 07:11:38 +03:00
|
|
|
const bool has_mod_header{mod_header.magic == Common::MakeMagic('M', 'O', 'D', '0')};
|
2017-10-06 06:30:08 +03: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-08-03 21:33:59 +03:00
|
|
|
codeset->DataSegment().size += bss_size;
|
2018-01-18 23:18:43 +03:00
|
|
|
program_image.resize(static_cast<u32>(program_image.size()) + bss_size);
|
2017-10-06 06:30:08 +03:00
|
|
|
|
|
|
|
// Load codeset for current process
|
2018-07-19 04:07:11 +03:00
|
|
|
codeset->name = file->GetName();
|
2017-10-06 06:30:08 +03:00
|
|
|
codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
|
2018-03-14 00:49:59 +03:00
|
|
|
Core::CurrentProcess()->LoadModule(codeset, load_base);
|
2017-10-06 06:30:08 +03:00
|
|
|
|
2018-07-13 06:22:59 +03:00
|
|
|
// Register module with GDBStub
|
|
|
|
GDBStub::RegisterModule(codeset->name, load_base, load_base);
|
|
|
|
|
2017-10-06 06:30:08 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-29 22:57:40 +03:00
|
|
|
ResultStatus AppLoader_NRO::Load(Kernel::Process& process) {
|
2017-10-06 06:30:08 +03:00
|
|
|
if (is_loaded) {
|
|
|
|
return ResultStatus::ErrorAlreadyLoaded;
|
|
|
|
}
|
|
|
|
|
2018-01-18 02:16:09 +03:00
|
|
|
// Load NRO
|
2018-09-30 01:47:00 +03:00
|
|
|
const VAddr base_address = process.VMManager().GetCodeRegionBaseAddress();
|
2018-01-18 02:16:09 +03:00
|
|
|
|
2018-09-25 03:01:45 +03:00
|
|
|
if (!LoadNro(file, base_address)) {
|
2018-08-10 04:06:44 +03:00
|
|
|
return ResultStatus::ErrorLoadingNRO;
|
2017-10-06 06:30:08 +03:00
|
|
|
}
|
|
|
|
|
2018-09-29 22:57:40 +03:00
|
|
|
process.Run(base_address, Kernel::THREADPRIO_DEFAULT, Memory::DEFAULT_STACK_SIZE);
|
2017-10-06 06:30:08 +03:00
|
|
|
|
|
|
|
is_loaded = true;
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2018-07-23 19:33:24 +03:00
|
|
|
ResultStatus AppLoader_NRO::ReadIcon(std::vector<u8>& buffer) {
|
2018-07-24 00:24:27 +03:00
|
|
|
if (icon_data.empty()) {
|
2018-08-10 04:06:44 +03:00
|
|
|
return ResultStatus::ErrorNoIcon;
|
2018-07-24 00:24:27 +03:00
|
|
|
}
|
|
|
|
|
2018-07-23 19:33:24 +03:00
|
|
|
buffer = icon_data;
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultStatus AppLoader_NRO::ReadProgramId(u64& out_program_id) {
|
2018-07-24 00:24:27 +03:00
|
|
|
if (nacp == nullptr) {
|
2018-08-10 04:06:44 +03:00
|
|
|
return ResultStatus::ErrorNoControl;
|
2018-07-24 00:24:27 +03:00
|
|
|
}
|
|
|
|
|
2018-07-23 19:33:24 +03:00
|
|
|
out_program_id = nacp->GetTitleId();
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultStatus AppLoader_NRO::ReadRomFS(FileSys::VirtualFile& dir) {
|
2018-07-24 00:24:27 +03:00
|
|
|
if (romfs == nullptr) {
|
2018-08-10 04:06:44 +03:00
|
|
|
return ResultStatus::ErrorNoRomFS;
|
2018-07-24 00:24:27 +03:00
|
|
|
}
|
|
|
|
|
2018-07-23 19:33:24 +03:00
|
|
|
dir = romfs;
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultStatus AppLoader_NRO::ReadTitle(std::string& title) {
|
2018-07-24 00:24:27 +03:00
|
|
|
if (nacp == nullptr) {
|
2018-08-10 04:06:44 +03:00
|
|
|
return ResultStatus::ErrorNoControl;
|
2018-07-24 00:24:27 +03:00
|
|
|
}
|
|
|
|
|
2018-07-23 19:33:24 +03:00
|
|
|
title = nacp->GetApplicationName();
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
2018-08-26 02:05:04 +03:00
|
|
|
|
2018-08-26 17:53:31 +03:00
|
|
|
bool AppLoader_NRO::IsRomFSUpdatable() const {
|
2018-08-26 02:05:04 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-06 06:30:08 +03:00
|
|
|
} // namespace Loader
|