2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-07-27 23:55:23 -04:00
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <string>
|
2018-08-15 05:38:37 -04:00
|
|
|
|
|
|
|
#include <fmt/ostream.h>
|
|
|
|
|
2018-08-06 23:13:37 -04:00
|
|
|
#include "common/logging/log.h"
|
2020-08-23 14:20:37 -04:00
|
|
|
#include "core/crypto/key_manager.h"
|
2018-07-27 23:55:23 -04:00
|
|
|
#include "core/file_sys/card_image.h"
|
2018-09-03 21:58:19 -04:00
|
|
|
#include "core/file_sys/content_archive.h"
|
2018-08-25 11:48:23 -04:00
|
|
|
#include "core/file_sys/nca_metadata.h"
|
2018-07-27 23:55:23 -04:00
|
|
|
#include "core/file_sys/partition_filesystem.h"
|
2018-08-25 22:42:54 -04:00
|
|
|
#include "core/file_sys/submission_package.h"
|
2018-07-27 23:55:23 -04:00
|
|
|
#include "core/file_sys/vfs_offset.h"
|
2019-04-10 12:13:27 -04:00
|
|
|
#include "core/file_sys/vfs_vector.h"
|
2018-08-15 05:38:37 -04:00
|
|
|
#include "core/loader/loader.h"
|
2018-07-27 23:55:23 -04:00
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2019-04-10 12:13:27 -04:00
|
|
|
constexpr u64 GAMECARD_CERTIFICATE_OFFSET = 0x7000;
|
2019-06-10 22:56:00 -04:00
|
|
|
constexpr std::array partition_names{
|
|
|
|
"update",
|
|
|
|
"normal",
|
|
|
|
"secure",
|
|
|
|
"logo",
|
|
|
|
};
|
2018-08-09 21:06:44 -04:00
|
|
|
|
2021-07-20 01:10:05 -04:00
|
|
|
XCI::XCI(VirtualFile file_, u64 program_id, size_t program_index)
|
2018-10-02 23:10:24 -04:00
|
|
|
: file(std::move(file_)), program_nca_status{Loader::ResultStatus::ErrorXCIMissingProgramNCA},
|
2023-08-10 21:34:43 -04:00
|
|
|
partitions(partition_names.size()), partitions_raw(partition_names.size()),
|
|
|
|
keys{Core::Crypto::KeyManager::Instance()} {
|
2018-07-27 23:55:23 -04:00
|
|
|
if (file->ReadObject(&header) != sizeof(GamecardHeader)) {
|
2018-08-09 21:06:44 -04:00
|
|
|
status = Loader::ResultStatus::ErrorBadXCIHeader;
|
2018-07-27 23:55:23 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.magic != Common::MakeMagic('H', 'E', 'A', 'D')) {
|
2018-08-09 21:06:44 -04:00
|
|
|
status = Loader::ResultStatus::ErrorBadXCIHeader;
|
2018-07-27 23:55:23 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:50:29 -04:00
|
|
|
PartitionFilesystem main_hfs(std::make_shared<OffsetVfsFile>(
|
|
|
|
file, file->GetSize() - header.hfs_offset, header.hfs_offset));
|
|
|
|
|
|
|
|
update_normal_partition_end = main_hfs.GetFileOffsets()["secure"];
|
2018-07-27 23:55:23 -04:00
|
|
|
|
|
|
|
if (main_hfs.GetStatus() != Loader::ResultStatus::Success) {
|
|
|
|
status = main_hfs.GetStatus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (XCIPartition partition :
|
|
|
|
{XCIPartition::Update, XCIPartition::Normal, XCIPartition::Secure, XCIPartition::Logo}) {
|
2019-06-10 23:02:48 -04:00
|
|
|
const auto partition_idx = static_cast<std::size_t>(partition);
|
|
|
|
auto raw = main_hfs.GetFile(partition_names[partition_idx]);
|
|
|
|
|
2019-10-13 14:18:09 -04:00
|
|
|
partitions_raw[static_cast<std::size_t>(partition)] = std::move(raw);
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
|
|
|
|
2018-08-25 11:48:23 -04:00
|
|
|
secure_partition = std::make_shared<NSP>(
|
2020-11-24 18:16:24 -05:00
|
|
|
main_hfs.GetFile(partition_names[static_cast<std::size_t>(XCIPartition::Secure)]),
|
2021-07-20 01:10:05 -04:00
|
|
|
program_id, program_index);
|
2018-08-16 16:57:00 -04:00
|
|
|
|
2019-06-10 23:08:14 -04:00
|
|
|
ncas = secure_partition->GetNCAsCollapsed();
|
2018-08-25 11:48:23 -04:00
|
|
|
program =
|
|
|
|
secure_partition->GetNCA(secure_partition->GetProgramTitleID(), ContentRecordType::Program);
|
2021-07-20 01:10:05 -04:00
|
|
|
program_nca_status = secure_partition->GetProgramStatus();
|
2019-06-10 23:13:14 -04:00
|
|
|
if (program_nca_status == Loader::ResultStatus::ErrorNSPMissingProgramNCA) {
|
2018-09-03 18:46:56 -04:00
|
|
|
program_nca_status = Loader::ResultStatus::ErrorXCIMissingProgramNCA;
|
2019-06-10 23:13:14 -04:00
|
|
|
}
|
2018-07-29 20:47:33 -04:00
|
|
|
|
2019-09-22 21:50:29 -04:00
|
|
|
auto result = AddNCAFromPartition(XCIPartition::Normal);
|
2018-07-27 23:55:23 -04:00
|
|
|
if (result != Loader::ResultStatus::Success) {
|
|
|
|
status = result;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetFormatVersion() >= 0x2) {
|
|
|
|
result = AddNCAFromPartition(XCIPartition::Logo);
|
|
|
|
if (result != Loader::ResultStatus::Success) {
|
|
|
|
status = result;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status = Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2018-09-03 21:58:19 -04:00
|
|
|
XCI::~XCI() = default;
|
|
|
|
|
2018-07-27 23:55:23 -04:00
|
|
|
Loader::ResultStatus XCI::GetStatus() const {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-08-16 16:57:00 -04:00
|
|
|
Loader::ResultStatus XCI::GetProgramNCAStatus() const {
|
|
|
|
return program_nca_status;
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:50:29 -04:00
|
|
|
VirtualDir XCI::GetPartition(XCIPartition partition) {
|
|
|
|
const auto id = static_cast<std::size_t>(partition);
|
|
|
|
if (partitions[id] == nullptr && partitions_raw[id] != nullptr) {
|
|
|
|
partitions[id] = std::make_shared<PartitionFilesystem>(partitions_raw[id]);
|
|
|
|
}
|
|
|
|
|
2018-09-15 09:21:06 -04:00
|
|
|
return partitions[static_cast<std::size_t>(partition)];
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
|
|
|
|
2019-09-22 21:50:29 -04:00
|
|
|
std::vector<VirtualDir> XCI::GetPartitions() {
|
|
|
|
std::vector<VirtualDir> out;
|
|
|
|
for (const auto& id :
|
|
|
|
{XCIPartition::Update, XCIPartition::Normal, XCIPartition::Secure, XCIPartition::Logo}) {
|
|
|
|
const auto part = GetPartition(id);
|
|
|
|
if (part != nullptr) {
|
|
|
|
out.push_back(part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-08-25 11:48:23 -04:00
|
|
|
std::shared_ptr<NSP> XCI::GetSecurePartitionNSP() const {
|
|
|
|
return secure_partition;
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:50:29 -04:00
|
|
|
VirtualDir XCI::GetSecurePartition() {
|
2018-07-27 23:55:23 -04:00
|
|
|
return GetPartition(XCIPartition::Secure);
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:50:29 -04:00
|
|
|
VirtualDir XCI::GetNormalPartition() {
|
2018-07-27 23:55:23 -04:00
|
|
|
return GetPartition(XCIPartition::Normal);
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:50:29 -04:00
|
|
|
VirtualDir XCI::GetUpdatePartition() {
|
2018-07-27 23:55:23 -04:00
|
|
|
return GetPartition(XCIPartition::Update);
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:50:29 -04:00
|
|
|
VirtualDir XCI::GetLogoPartition() {
|
2018-07-27 23:55:23 -04:00
|
|
|
return GetPartition(XCIPartition::Logo);
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:51:46 -04:00
|
|
|
VirtualFile XCI::GetPartitionRaw(XCIPartition partition) const {
|
|
|
|
return partitions_raw[static_cast<std::size_t>(partition)];
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile XCI::GetSecurePartitionRaw() const {
|
|
|
|
return GetPartitionRaw(XCIPartition::Secure);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile XCI::GetStoragePartition0() const {
|
|
|
|
return std::make_shared<OffsetVfsFile>(file, update_normal_partition_end, 0, "partition0");
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile XCI::GetStoragePartition1() const {
|
|
|
|
return std::make_shared<OffsetVfsFile>(file, file->GetSize() - update_normal_partition_end,
|
|
|
|
update_normal_partition_end, "partition1");
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile XCI::GetNormalPartitionRaw() const {
|
|
|
|
return GetPartitionRaw(XCIPartition::Normal);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile XCI::GetUpdatePartitionRaw() const {
|
|
|
|
return GetPartitionRaw(XCIPartition::Update);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile XCI::GetLogoPartitionRaw() const {
|
|
|
|
return GetPartitionRaw(XCIPartition::Logo);
|
|
|
|
}
|
|
|
|
|
2018-09-03 18:47:23 -04:00
|
|
|
u64 XCI::GetProgramTitleID() const {
|
|
|
|
return secure_partition->GetProgramTitleID();
|
|
|
|
}
|
|
|
|
|
2021-07-20 01:10:05 -04:00
|
|
|
std::vector<u64> XCI::GetProgramTitleIDs() const {
|
|
|
|
return secure_partition->GetProgramTitleIDs();
|
|
|
|
}
|
|
|
|
|
2019-10-13 14:18:09 -04:00
|
|
|
u32 XCI::GetSystemUpdateVersion() {
|
|
|
|
const auto update = GetPartition(XCIPartition::Update);
|
2021-05-02 02:34:40 -04:00
|
|
|
if (update == nullptr) {
|
2019-10-13 14:18:09 -04:00
|
|
|
return 0;
|
2021-05-02 02:34:40 -04:00
|
|
|
}
|
2019-10-13 14:18:09 -04:00
|
|
|
|
2021-05-02 02:34:40 -04:00
|
|
|
for (const auto& update_file : update->GetFiles()) {
|
2023-08-10 21:34:43 -04:00
|
|
|
NCA nca{update_file};
|
2019-10-13 14:18:09 -04:00
|
|
|
|
2021-05-02 02:34:40 -04:00
|
|
|
if (nca.GetStatus() != Loader::ResultStatus::Success) {
|
2019-10-13 14:18:09 -04:00
|
|
|
continue;
|
2021-05-02 02:34:40 -04:00
|
|
|
}
|
2019-10-13 14:18:09 -04:00
|
|
|
|
|
|
|
if (nca.GetType() == NCAContentType::Meta && nca.GetTitleId() == 0x0100000000000816) {
|
|
|
|
const auto dir = nca.GetSubdirectories()[0];
|
|
|
|
const auto cnmt = dir->GetFile("SystemUpdate_0100000000000816.cnmt");
|
2021-05-02 02:34:40 -04:00
|
|
|
if (cnmt == nullptr) {
|
2019-10-13 14:18:09 -04:00
|
|
|
continue;
|
2021-05-02 02:34:40 -04:00
|
|
|
}
|
2019-10-13 14:18:09 -04:00
|
|
|
|
|
|
|
CNMT cnmt_data{cnmt};
|
|
|
|
|
|
|
|
const auto metas = cnmt_data.GetMetaRecords();
|
2021-05-02 02:34:40 -04:00
|
|
|
if (metas.empty()) {
|
2019-10-13 14:18:09 -04:00
|
|
|
continue;
|
2021-05-02 02:34:40 -04:00
|
|
|
}
|
2019-10-13 14:18:09 -04:00
|
|
|
|
|
|
|
return metas[0].title_version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 XCI::GetSystemUpdateTitleID() const {
|
|
|
|
return 0x0100000000000816;
|
|
|
|
}
|
|
|
|
|
2018-10-16 11:36:55 -04:00
|
|
|
bool XCI::HasProgramNCA() const {
|
|
|
|
return program != nullptr;
|
2018-08-25 11:48:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile XCI::GetProgramNCAFile() const {
|
2018-10-16 11:36:55 -04:00
|
|
|
if (!HasProgramNCA()) {
|
2018-08-25 11:48:23 -04:00
|
|
|
return nullptr;
|
2018-10-16 11:36:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return program->GetBaseFile();
|
2018-08-25 11:48:23 -04:00
|
|
|
}
|
|
|
|
|
2018-08-09 20:51:14 -04:00
|
|
|
const std::vector<std::shared_ptr<NCA>>& XCI::GetNCAs() const {
|
|
|
|
return ncas;
|
|
|
|
}
|
|
|
|
|
2018-07-27 23:55:23 -04:00
|
|
|
std::shared_ptr<NCA> XCI::GetNCAByType(NCAContentType type) const {
|
2021-07-20 01:10:05 -04:00
|
|
|
const auto program_id = secure_partition->GetProgramTitleID();
|
2022-10-21 02:34:07 -04:00
|
|
|
const auto iter =
|
|
|
|
std::find_if(ncas.begin(), ncas.end(), [type, program_id](const std::shared_ptr<NCA>& nca) {
|
2021-07-20 01:10:05 -04:00
|
|
|
return nca->GetType() == type && nca->GetTitleId() == program_id;
|
|
|
|
});
|
2018-07-28 21:39:42 -04:00
|
|
|
return iter == ncas.end() ? nullptr : *iter;
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile XCI::GetNCAFileByType(NCAContentType type) const {
|
|
|
|
auto nca = GetNCAByType(type);
|
2019-06-10 23:13:14 -04:00
|
|
|
if (nca != nullptr) {
|
2018-07-27 23:55:23 -04:00
|
|
|
return nca->GetBaseFile();
|
2019-06-10 23:13:14 -04:00
|
|
|
}
|
2018-07-27 23:55:23 -04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-12 03:57:06 -04:00
|
|
|
std::vector<VirtualFile> XCI::GetFiles() const {
|
2018-07-27 23:55:23 -04:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-08-12 03:57:06 -04:00
|
|
|
std::vector<VirtualDir> XCI::GetSubdirectories() const {
|
2018-08-12 03:53:16 -04:00
|
|
|
return {};
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string XCI::GetName() const {
|
|
|
|
return file->GetName();
|
|
|
|
}
|
|
|
|
|
2018-08-12 03:57:06 -04:00
|
|
|
VirtualDir XCI::GetParentDirectory() const {
|
2018-07-27 23:55:23 -04:00
|
|
|
return file->GetContainingDirectory();
|
|
|
|
}
|
|
|
|
|
2019-04-10 12:12:49 -04:00
|
|
|
VirtualDir XCI::ConcatenatedPseudoDirectory() {
|
|
|
|
const auto out = std::make_shared<VectorVfsDirectory>();
|
|
|
|
for (const auto& part_id : {XCIPartition::Normal, XCIPartition::Logo, XCIPartition::Secure}) {
|
|
|
|
const auto& part = GetPartition(part_id);
|
|
|
|
if (part == nullptr)
|
|
|
|
continue;
|
|
|
|
|
2021-05-02 02:34:40 -04:00
|
|
|
for (const auto& part_file : part->GetFiles())
|
|
|
|
out->AddFile(part_file);
|
2019-04-10 12:12:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::array<u8, 0x200> XCI::GetCertificate() const {
|
|
|
|
std::array<u8, 0x200> out;
|
|
|
|
file->Read(out.data(), out.size(), GAMECARD_CERTIFICATE_OFFSET);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-07-27 23:55:23 -04:00
|
|
|
Loader::ResultStatus XCI::AddNCAFromPartition(XCIPartition part) {
|
2019-06-10 23:18:17 -04:00
|
|
|
const auto partition_index = static_cast<std::size_t>(part);
|
2019-09-22 21:50:29 -04:00
|
|
|
const auto partition = GetPartition(part);
|
2019-06-10 23:18:17 -04:00
|
|
|
|
|
|
|
if (partition == nullptr) {
|
2018-08-09 21:06:44 -04:00
|
|
|
return Loader::ResultStatus::ErrorXCIMissingPartition;
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
|
|
|
|
2021-05-02 02:34:40 -04:00
|
|
|
for (const VirtualFile& partition_file : partition->GetFiles()) {
|
|
|
|
if (partition_file->GetExtension() != "nca") {
|
2018-07-27 23:55:23 -04:00
|
|
|
continue;
|
2019-06-10 23:13:14 -04:00
|
|
|
}
|
2019-06-12 16:52:15 -04:00
|
|
|
|
2023-08-10 21:34:43 -04:00
|
|
|
auto nca = std::make_shared<NCA>(partition_file);
|
2019-06-10 23:13:14 -04:00
|
|
|
if (nca->IsUpdate()) {
|
2018-08-23 18:53:37 -04:00
|
|
|
continue;
|
2019-06-10 23:13:14 -04:00
|
|
|
}
|
2018-08-16 16:57:00 -04:00
|
|
|
if (nca->GetType() == NCAContentType::Program) {
|
|
|
|
program_nca_status = nca->GetStatus();
|
|
|
|
}
|
2018-08-09 21:06:44 -04:00
|
|
|
if (nca->GetStatus() == Loader::ResultStatus::Success) {
|
2018-07-27 23:55:23 -04:00
|
|
|
ncas.push_back(std::move(nca));
|
2018-08-09 21:06:44 -04:00
|
|
|
} else {
|
|
|
|
const u16 error_id = static_cast<u16>(nca->GetStatus());
|
|
|
|
LOG_CRITICAL(Loader, "Could not load NCA {}/{}, failed with error code {:04X} ({})",
|
2019-06-10 23:18:17 -04:00
|
|
|
partition_names[partition_index], nca->GetName(), error_id,
|
2018-08-15 05:38:37 -04:00
|
|
|
nca->GetStatus());
|
2018-08-09 21:06:44 -04:00
|
|
|
}
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:50:29 -04:00
|
|
|
u8 XCI::GetFormatVersion() {
|
2018-07-27 23:55:23 -04:00
|
|
|
return GetLogoPartition() == nullptr ? 0x1 : 0x2;
|
|
|
|
}
|
|
|
|
} // namespace FileSys
|