2018-02-25 11:34:19 +01:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-09-04 03:58:19 +02:00
|
|
|
#include <cstddef>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-02-25 11:34:19 +01:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "core/file_sys/program_metadata.h"
|
2020-08-23 20:20:37 +02:00
|
|
|
#include "core/file_sys/vfs.h"
|
2018-02-25 11:34:19 +01:00
|
|
|
#include "core/loader/loader.h"
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2018-09-20 01:19:05 +02:00
|
|
|
ProgramMetadata::ProgramMetadata() = default;
|
|
|
|
|
|
|
|
ProgramMetadata::~ProgramMetadata() = default;
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
Loader::ResultStatus ProgramMetadata::Load(VirtualFile file) {
|
2019-04-05 21:52:13 +02:00
|
|
|
const std::size_t total_size = file->GetSize();
|
|
|
|
if (total_size < sizeof(Header)) {
|
2018-08-10 03:06:44 +02:00
|
|
|
return Loader::ResultStatus::ErrorBadNPDMHeader;
|
2019-04-05 21:52:13 +02:00
|
|
|
}
|
2018-02-25 11:34:19 +01:00
|
|
|
|
2019-04-05 21:52:13 +02:00
|
|
|
if (sizeof(Header) != file->ReadObject(&npdm_header)) {
|
2018-08-10 03:06:44 +02:00
|
|
|
return Loader::ResultStatus::ErrorBadNPDMHeader;
|
2019-04-05 21:52:13 +02:00
|
|
|
}
|
2018-02-25 11:34:19 +01:00
|
|
|
|
2019-04-05 21:52:13 +02:00
|
|
|
if (sizeof(AcidHeader) != file->ReadObject(&acid_header, npdm_header.acid_offset)) {
|
2018-08-10 03:06:44 +02:00
|
|
|
return Loader::ResultStatus::ErrorBadACIDHeader;
|
2019-04-05 21:52:13 +02:00
|
|
|
}
|
2018-02-25 11:34:19 +01:00
|
|
|
|
2019-04-05 21:52:13 +02:00
|
|
|
if (sizeof(AciHeader) != file->ReadObject(&aci_header, npdm_header.aci_offset)) {
|
2018-08-10 03:06:44 +02:00
|
|
|
return Loader::ResultStatus::ErrorBadACIHeader;
|
2019-04-05 21:52:13 +02:00
|
|
|
}
|
2018-07-08 05:24:51 +02:00
|
|
|
|
2019-04-05 21:52:13 +02:00
|
|
|
if (sizeof(FileAccessControl) != file->ReadObject(&acid_file_access, acid_header.fac_offset)) {
|
2018-08-10 03:06:44 +02:00
|
|
|
return Loader::ResultStatus::ErrorBadFileAccessControl;
|
2019-04-05 21:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sizeof(FileAccessHeader) != file->ReadObject(&aci_file_access, aci_header.fah_offset)) {
|
2018-08-10 03:06:44 +02:00
|
|
|
return Loader::ResultStatus::ErrorBadFileAccessHeader;
|
2019-04-05 21:52:13 +02:00
|
|
|
}
|
2018-07-08 05:24:51 +02:00
|
|
|
|
2018-12-20 05:50:20 +01:00
|
|
|
aci_kernel_capabilities.resize(aci_header.kac_size / sizeof(u32));
|
|
|
|
const u64 read_size = aci_header.kac_size;
|
|
|
|
const u64 read_offset = npdm_header.aci_offset + aci_header.kac_offset;
|
|
|
|
if (file->ReadBytes(aci_kernel_capabilities.data(), read_size, read_offset) != read_size) {
|
|
|
|
return Loader::ResultStatus::ErrorBadKernelCapabilityDescriptors;
|
|
|
|
}
|
|
|
|
|
2018-02-25 11:34:19 +01:00
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:15:52 +02:00
|
|
|
/*static*/ ProgramMetadata ProgramMetadata::GetDefault() {
|
|
|
|
ProgramMetadata result;
|
|
|
|
|
|
|
|
result.LoadManual(
|
|
|
|
true /*is_64_bit*/, FileSys::ProgramAddressSpaceType::Is39Bit /*address_space*/,
|
|
|
|
0x2c /*main_thread_prio*/, 0 /*main_thread_core*/, 0x00100000 /*main_thread_stack_size*/,
|
2021-04-23 15:37:35 +02:00
|
|
|
0 /*title_id*/, 0xFFFFFFFFFFFFFFFF /*filesystem_permissions*/,
|
|
|
|
0x1FE00000 /*system_resource_size*/, {} /*capabilities*/);
|
2020-04-21 00:15:52 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-06-05 06:20:26 +02:00
|
|
|
void ProgramMetadata::LoadManual(bool is_64_bit, ProgramAddressSpaceType address_space,
|
2019-11-12 11:20:26 +01:00
|
|
|
s32 main_thread_prio, u32 main_thread_core,
|
2019-06-05 06:20:26 +02:00
|
|
|
u32 main_thread_stack_size, u64 title_id,
|
2021-04-23 15:37:35 +02:00
|
|
|
u64 filesystem_permissions, u32 system_resource_size,
|
2019-06-05 06:20:26 +02:00
|
|
|
KernelCapabilityDescriptors capabilities) {
|
|
|
|
npdm_header.has_64_bit_instructions.Assign(is_64_bit);
|
|
|
|
npdm_header.address_space_type.Assign(address_space);
|
2019-11-12 11:20:26 +01:00
|
|
|
npdm_header.main_thread_priority = static_cast<u8>(main_thread_prio);
|
|
|
|
npdm_header.main_thread_cpu = static_cast<u8>(main_thread_core);
|
2019-06-05 06:20:26 +02:00
|
|
|
npdm_header.main_stack_size = main_thread_stack_size;
|
|
|
|
aci_header.title_id = title_id;
|
|
|
|
aci_file_access.permissions = filesystem_permissions;
|
2021-04-23 15:37:35 +02:00
|
|
|
npdm_header.system_resource_size = system_resource_size;
|
2019-06-05 06:20:26 +02:00
|
|
|
aci_kernel_capabilities = std ::move(capabilities);
|
|
|
|
}
|
|
|
|
|
2018-02-25 11:34:19 +01:00
|
|
|
bool ProgramMetadata::Is64BitProgram() const {
|
|
|
|
return npdm_header.has_64_bit_instructions;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramAddressSpaceType ProgramMetadata::GetAddressSpaceType() const {
|
|
|
|
return npdm_header.address_space_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 ProgramMetadata::GetMainThreadPriority() const {
|
|
|
|
return npdm_header.main_thread_priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 ProgramMetadata::GetMainThreadCore() const {
|
|
|
|
return npdm_header.main_thread_cpu;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 ProgramMetadata::GetMainThreadStackSize() const {
|
|
|
|
return npdm_header.main_stack_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 ProgramMetadata::GetTitleID() const {
|
|
|
|
return aci_header.title_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 ProgramMetadata::GetFilesystemPermissions() const {
|
|
|
|
return aci_file_access.permissions;
|
|
|
|
}
|
|
|
|
|
2019-07-07 18:42:54 +02:00
|
|
|
u32 ProgramMetadata::GetSystemResourceSize() const {
|
|
|
|
return npdm_header.system_resource_size;
|
|
|
|
}
|
|
|
|
|
2018-12-20 05:50:20 +01:00
|
|
|
const ProgramMetadata::KernelCapabilityDescriptors& ProgramMetadata::GetKernelCapabilities() const {
|
|
|
|
return aci_kernel_capabilities;
|
|
|
|
}
|
|
|
|
|
2018-02-25 11:34:19 +01:00
|
|
|
void ProgramMetadata::Print() const {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_DEBUG(Service_FS, "Magic: {:.4}", npdm_header.magic.data());
|
|
|
|
LOG_DEBUG(Service_FS, "Main thread priority: 0x{:02X}", npdm_header.main_thread_priority);
|
|
|
|
LOG_DEBUG(Service_FS, "Main thread core: {}", npdm_header.main_thread_cpu);
|
|
|
|
LOG_DEBUG(Service_FS, "Main thread stack size: 0x{:X} bytes", npdm_header.main_stack_size);
|
|
|
|
LOG_DEBUG(Service_FS, "Process category: {}", npdm_header.process_category);
|
|
|
|
LOG_DEBUG(Service_FS, "Flags: 0x{:02X}", npdm_header.flags);
|
|
|
|
LOG_DEBUG(Service_FS, " > 64-bit instructions: {}",
|
2018-07-02 18:20:50 +02:00
|
|
|
npdm_header.has_64_bit_instructions ? "YES" : "NO");
|
2018-02-25 11:34:19 +01:00
|
|
|
|
2018-12-28 01:16:43 +01:00
|
|
|
const char* address_space = "Unknown";
|
2018-02-25 11:34:19 +01:00
|
|
|
switch (npdm_header.address_space_type) {
|
2018-09-23 02:09:32 +02:00
|
|
|
case ProgramAddressSpaceType::Is36Bit:
|
2018-12-28 01:16:43 +01:00
|
|
|
address_space = "64-bit (36-bit address space)";
|
|
|
|
break;
|
2018-09-23 02:09:32 +02:00
|
|
|
case ProgramAddressSpaceType::Is39Bit:
|
2018-12-28 01:16:43 +01:00
|
|
|
address_space = "64-bit (39-bit address space)";
|
2018-02-25 11:34:19 +01:00
|
|
|
break;
|
|
|
|
case ProgramAddressSpaceType::Is32Bit:
|
|
|
|
address_space = "32-bit";
|
|
|
|
break;
|
2018-12-28 01:16:43 +01:00
|
|
|
case ProgramAddressSpaceType::Is32BitNoMap:
|
|
|
|
address_space = "32-bit (no map region)";
|
|
|
|
break;
|
2018-02-25 11:34:19 +01:00
|
|
|
}
|
|
|
|
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_DEBUG(Service_FS, " > Address space: {}\n", address_space);
|
2018-02-25 11:34:19 +01:00
|
|
|
|
|
|
|
// Begin ACID printing (potential perms, signed)
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_DEBUG(Service_FS, "Magic: {:.4}", acid_header.magic.data());
|
|
|
|
LOG_DEBUG(Service_FS, "Flags: 0x{:02X}", acid_header.flags);
|
|
|
|
LOG_DEBUG(Service_FS, " > Is Retail: {}", acid_header.is_retail ? "YES" : "NO");
|
|
|
|
LOG_DEBUG(Service_FS, "Title ID Min: 0x{:016X}", acid_header.title_id_min);
|
|
|
|
LOG_DEBUG(Service_FS, "Title ID Max: 0x{:016X}", acid_header.title_id_max);
|
2021-06-10 21:07:27 +02:00
|
|
|
u64_le permissions_l; // local copy to fix alignment error
|
|
|
|
std::memcpy(&permissions_l, &acid_file_access.permissions, sizeof(permissions_l));
|
|
|
|
LOG_DEBUG(Service_FS, "Filesystem Access: 0x{:016X}\n", permissions_l);
|
2018-02-25 11:34:19 +01:00
|
|
|
|
|
|
|
// Begin ACI0 printing (actual perms, unsigned)
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_DEBUG(Service_FS, "Magic: {:.4}", aci_header.magic.data());
|
|
|
|
LOG_DEBUG(Service_FS, "Title ID: 0x{:016X}", aci_header.title_id);
|
|
|
|
LOG_DEBUG(Service_FS, "Filesystem Access: 0x{:016X}\n", aci_file_access.permissions);
|
2018-02-25 11:34:19 +01:00
|
|
|
}
|
|
|
|
} // namespace FileSys
|