2018-07-27 22:55:23 -05:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <string>
|
2018-08-15 04:38:37 -05:00
|
|
|
|
|
|
|
#include <fmt/ostream.h>
|
|
|
|
|
2018-08-06 22:13:37 -05:00
|
|
|
#include "common/logging/log.h"
|
2018-07-27 22:55:23 -05:00
|
|
|
#include "core/file_sys/card_image.h"
|
2018-09-03 20:58:19 -05:00
|
|
|
#include "core/file_sys/content_archive.h"
|
2018-08-25 10:48:23 -05:00
|
|
|
#include "core/file_sys/nca_metadata.h"
|
2018-07-27 22:55:23 -05:00
|
|
|
#include "core/file_sys/partition_filesystem.h"
|
2018-08-25 21:42:54 -05:00
|
|
|
#include "core/file_sys/submission_package.h"
|
2018-07-27 22:55:23 -05:00
|
|
|
#include "core/file_sys/vfs_offset.h"
|
2018-08-15 04:38:37 -05:00
|
|
|
#include "core/loader/loader.h"
|
2018-07-27 22:55:23 -05:00
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2018-08-09 20:06:44 -05:00
|
|
|
constexpr std::array<const char*, 0x4> partition_names = {"update", "normal", "secure", "logo"};
|
|
|
|
|
2018-07-27 22:55:23 -05:00
|
|
|
XCI::XCI(VirtualFile file_) : file(std::move(file_)), partitions(0x4) {
|
|
|
|
if (file->ReadObject(&header) != sizeof(GamecardHeader)) {
|
2018-08-09 20:06:44 -05:00
|
|
|
status = Loader::ResultStatus::ErrorBadXCIHeader;
|
2018-07-27 22:55:23 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.magic != Common::MakeMagic('H', 'E', 'A', 'D')) {
|
2018-08-09 20:06:44 -05:00
|
|
|
status = Loader::ResultStatus::ErrorBadXCIHeader;
|
2018-07-27 22:55:23 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PartitionFilesystem main_hfs(
|
|
|
|
std::make_shared<OffsetVfsFile>(file, header.hfs_size, header.hfs_offset));
|
|
|
|
|
|
|
|
if (main_hfs.GetStatus() != Loader::ResultStatus::Success) {
|
|
|
|
status = main_hfs.GetStatus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (XCIPartition partition :
|
|
|
|
{XCIPartition::Update, XCIPartition::Normal, XCIPartition::Secure, XCIPartition::Logo}) {
|
2018-09-15 08:21:06 -05:00
|
|
|
auto raw = main_hfs.GetFile(partition_names[static_cast<std::size_t>(partition)]);
|
2018-07-27 22:55:23 -05:00
|
|
|
if (raw != nullptr)
|
2018-09-15 08:21:06 -05:00
|
|
|
partitions[static_cast<std::size_t>(partition)] =
|
|
|
|
std::make_shared<PartitionFilesystem>(raw);
|
2018-07-27 22:55:23 -05:00
|
|
|
}
|
|
|
|
|
2018-08-25 10:48:23 -05:00
|
|
|
secure_partition = std::make_shared<NSP>(
|
2018-09-15 08:21:06 -05:00
|
|
|
main_hfs.GetFile(partition_names[static_cast<std::size_t>(XCIPartition::Secure)]));
|
2018-08-16 15:57:00 -05:00
|
|
|
|
2018-08-25 10:48:23 -05:00
|
|
|
const auto secure_ncas = secure_partition->GetNCAsCollapsed();
|
|
|
|
std::copy(secure_ncas.begin(), secure_ncas.end(), std::back_inserter(ncas));
|
|
|
|
|
|
|
|
program =
|
|
|
|
secure_partition->GetNCA(secure_partition->GetProgramTitleID(), ContentRecordType::Program);
|
2018-09-03 17:46:56 -05:00
|
|
|
program_nca_status = secure_partition->GetProgramStatus(secure_partition->GetProgramTitleID());
|
|
|
|
if (program_nca_status == Loader::ResultStatus::ErrorNSPMissingProgramNCA)
|
|
|
|
program_nca_status = Loader::ResultStatus::ErrorXCIMissingProgramNCA;
|
2018-07-29 19:47:33 -05:00
|
|
|
|
2018-08-25 10:48:23 -05:00
|
|
|
auto result = AddNCAFromPartition(XCIPartition::Update);
|
2018-07-27 22:55:23 -05:00
|
|
|
if (result != Loader::ResultStatus::Success) {
|
|
|
|
status = result;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = AddNCAFromPartition(XCIPartition::Normal);
|
|
|
|
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 20:58:19 -05:00
|
|
|
XCI::~XCI() = default;
|
|
|
|
|
2018-07-27 22:55:23 -05:00
|
|
|
Loader::ResultStatus XCI::GetStatus() const {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-08-16 15:57:00 -05:00
|
|
|
Loader::ResultStatus XCI::GetProgramNCAStatus() const {
|
|
|
|
return program_nca_status;
|
|
|
|
}
|
|
|
|
|
2018-07-27 22:55:23 -05:00
|
|
|
VirtualDir XCI::GetPartition(XCIPartition partition) const {
|
2018-09-15 08:21:06 -05:00
|
|
|
return partitions[static_cast<std::size_t>(partition)];
|
2018-07-27 22:55:23 -05:00
|
|
|
}
|
|
|
|
|
2018-08-25 10:48:23 -05:00
|
|
|
std::shared_ptr<NSP> XCI::GetSecurePartitionNSP() const {
|
|
|
|
return secure_partition;
|
|
|
|
}
|
|
|
|
|
2018-07-27 22:55:23 -05:00
|
|
|
VirtualDir XCI::GetSecurePartition() const {
|
|
|
|
return GetPartition(XCIPartition::Secure);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualDir XCI::GetNormalPartition() const {
|
|
|
|
return GetPartition(XCIPartition::Normal);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualDir XCI::GetUpdatePartition() const {
|
|
|
|
return GetPartition(XCIPartition::Update);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualDir XCI::GetLogoPartition() const {
|
|
|
|
return GetPartition(XCIPartition::Logo);
|
|
|
|
}
|
|
|
|
|
2018-09-03 17:47:23 -05:00
|
|
|
u64 XCI::GetProgramTitleID() const {
|
|
|
|
return secure_partition->GetProgramTitleID();
|
|
|
|
}
|
|
|
|
|
2018-08-25 10:48:23 -05:00
|
|
|
std::shared_ptr<NCA> XCI::GetProgramNCA() const {
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile XCI::GetProgramNCAFile() const {
|
|
|
|
if (GetProgramNCA() == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
return GetProgramNCA()->GetBaseFile();
|
|
|
|
}
|
|
|
|
|
2018-08-09 19:51:14 -05:00
|
|
|
const std::vector<std::shared_ptr<NCA>>& XCI::GetNCAs() const {
|
|
|
|
return ncas;
|
|
|
|
}
|
|
|
|
|
2018-07-27 22:55:23 -05:00
|
|
|
std::shared_ptr<NCA> XCI::GetNCAByType(NCAContentType type) const {
|
2018-07-29 18:00:09 -05:00
|
|
|
const auto iter =
|
|
|
|
std::find_if(ncas.begin(), ncas.end(),
|
|
|
|
[type](const std::shared_ptr<NCA>& nca) { return nca->GetType() == type; });
|
2018-07-28 20:39:42 -05:00
|
|
|
return iter == ncas.end() ? nullptr : *iter;
|
2018-07-27 22:55:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile XCI::GetNCAFileByType(NCAContentType type) const {
|
|
|
|
auto nca = GetNCAByType(type);
|
|
|
|
if (nca != nullptr)
|
|
|
|
return nca->GetBaseFile();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-12 02:57:06 -05:00
|
|
|
std::vector<VirtualFile> XCI::GetFiles() const {
|
2018-07-27 22:55:23 -05:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-08-12 02:57:06 -05:00
|
|
|
std::vector<VirtualDir> XCI::GetSubdirectories() const {
|
2018-08-12 02:53:16 -05:00
|
|
|
return {};
|
2018-07-27 22:55:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string XCI::GetName() const {
|
|
|
|
return file->GetName();
|
|
|
|
}
|
|
|
|
|
2018-08-12 02:57:06 -05:00
|
|
|
VirtualDir XCI::GetParentDirectory() const {
|
2018-07-27 22:55:23 -05:00
|
|
|
return file->GetContainingDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XCI::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Loader::ResultStatus XCI::AddNCAFromPartition(XCIPartition part) {
|
2018-09-15 08:21:06 -05:00
|
|
|
if (partitions[static_cast<std::size_t>(part)] == nullptr) {
|
2018-08-09 20:06:44 -05:00
|
|
|
return Loader::ResultStatus::ErrorXCIMissingPartition;
|
2018-07-27 22:55:23 -05:00
|
|
|
}
|
|
|
|
|
2018-09-15 08:21:06 -05:00
|
|
|
for (const VirtualFile& file : partitions[static_cast<std::size_t>(part)]->GetFiles()) {
|
2018-07-27 22:55:23 -05:00
|
|
|
if (file->GetExtension() != "nca")
|
|
|
|
continue;
|
|
|
|
auto nca = std::make_shared<NCA>(file);
|
2018-08-23 17:53:37 -05:00
|
|
|
// TODO(DarkLordZach): Add proper Rev1+ Support
|
|
|
|
if (nca->IsUpdate())
|
|
|
|
continue;
|
2018-08-16 15:57:00 -05:00
|
|
|
if (nca->GetType() == NCAContentType::Program) {
|
|
|
|
program_nca_status = nca->GetStatus();
|
|
|
|
}
|
2018-08-09 20:06:44 -05:00
|
|
|
if (nca->GetStatus() == Loader::ResultStatus::Success) {
|
2018-07-27 22:55:23 -05:00
|
|
|
ncas.push_back(std::move(nca));
|
2018-08-09 20:06:44 -05:00
|
|
|
} else {
|
|
|
|
const u16 error_id = static_cast<u16>(nca->GetStatus());
|
|
|
|
LOG_CRITICAL(Loader, "Could not load NCA {}/{}, failed with error code {:04X} ({})",
|
2018-09-15 08:21:06 -05:00
|
|
|
partition_names[static_cast<std::size_t>(part)], nca->GetName(), error_id,
|
2018-08-15 04:38:37 -05:00
|
|
|
nca->GetStatus());
|
2018-08-09 20:06:44 -05:00
|
|
|
}
|
2018-07-27 22:55:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 XCI::GetFormatVersion() const {
|
|
|
|
return GetLogoPartition() == nullptr ? 0x1 : 0x2;
|
|
|
|
}
|
|
|
|
} // namespace FileSys
|