2018-07-27 23:55:23 -04:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <string>
|
|
|
|
#include <core/loader/loader.h>
|
2018-08-06 23:13:37 -04:00
|
|
|
#include "common/logging/log.h"
|
2018-07-27 23:55:23 -04:00
|
|
|
#include "core/file_sys/card_image.h"
|
|
|
|
#include "core/file_sys/partition_filesystem.h"
|
|
|
|
#include "core/file_sys/vfs_offset.h"
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2018-08-09 21:06:44 -04:00
|
|
|
constexpr std::array<const char*, 0x4> partition_names = {"update", "normal", "secure", "logo"};
|
|
|
|
|
2018-07-27 23:55:23 -04:00
|
|
|
XCI::XCI(VirtualFile file_) : file(std::move(file_)), partitions(0x4) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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}) {
|
|
|
|
auto raw = main_hfs.GetFile(partition_names[static_cast<size_t>(partition)]);
|
|
|
|
if (raw != nullptr)
|
|
|
|
partitions[static_cast<size_t>(partition)] = std::make_shared<PartitionFilesystem>(raw);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = AddNCAFromPartition(XCIPartition::Secure);
|
|
|
|
if (result != Loader::ResultStatus::Success) {
|
|
|
|
status = result;
|
|
|
|
return;
|
|
|
|
}
|
2018-07-29 20:47:33 -04:00
|
|
|
|
2018-07-27 23:55:23 -04:00
|
|
|
result = AddNCAFromPartition(XCIPartition::Update);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Loader::ResultStatus XCI::GetStatus() const {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualDir XCI::GetPartition(XCIPartition partition) const {
|
|
|
|
return partitions[static_cast<size_t>(partition)];
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<NCA> XCI::GetNCAByType(NCAContentType type) const {
|
2018-07-29 19:00:09 -04: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 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);
|
|
|
|
if (nca != nullptr)
|
|
|
|
return nca->GetBaseFile();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<VfsFile>> XCI::GetFiles() const {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<VfsDirectory>> XCI::GetSubdirectories() const {
|
|
|
|
return std::vector<std::shared_ptr<VfsDirectory>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string XCI::GetName() const {
|
|
|
|
return file->GetName();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<VfsDirectory> XCI::GetParentDirectory() const {
|
|
|
|
return file->GetContainingDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XCI::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Loader::ResultStatus XCI::AddNCAFromPartition(XCIPartition part) {
|
|
|
|
if (partitions[static_cast<size_t>(part)] == nullptr) {
|
2018-08-09 21:06:44 -04:00
|
|
|
return Loader::ResultStatus::ErrorXCIMissingPartition;
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
|
|
|
|
2018-07-28 21:39:42 -04:00
|
|
|
for (const VirtualFile& file : partitions[static_cast<size_t>(part)]->GetFiles()) {
|
2018-07-27 23:55:23 -04:00
|
|
|
if (file->GetExtension() != "nca")
|
|
|
|
continue;
|
|
|
|
auto nca = std::make_shared<NCA>(file);
|
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} ({})",
|
|
|
|
partition_names[static_cast<size_t>(part)], nca->GetName(), error_id,
|
|
|
|
Loader::GetMessageForResultStatus(nca->GetStatus()));
|
|
|
|
}
|
2018-07-27 23:55:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 XCI::GetFormatVersion() const {
|
|
|
|
return GetLogoPartition() == nullptr ? 0x1 : 0x2;
|
|
|
|
}
|
|
|
|
} // namespace FileSys
|