2022-04-23 09:59:50 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-06-21 16:16:23 +01:00
|
|
|
|
2018-07-20 05:10:21 +01:00
|
|
|
#include <utility>
|
2018-06-21 16:16:23 +01:00
|
|
|
|
2023-09-06 06:06:03 +01:00
|
|
|
#include "common/hex_util.h"
|
|
|
|
#include "common/scope_exit.h"
|
2019-04-22 22:56:56 +01:00
|
|
|
#include "core/core.h"
|
2018-07-19 02:07:11 +01:00
|
|
|
#include "core/file_sys/content_archive.h"
|
2023-08-11 02:34:43 +01:00
|
|
|
#include "core/file_sys/nca_metadata.h"
|
|
|
|
#include "core/file_sys/registered_cache.h"
|
2018-08-21 01:36:36 +01:00
|
|
|
#include "core/file_sys/romfs_factory.h"
|
2021-04-24 06:04:28 +01:00
|
|
|
#include "core/hle/kernel/k_process.h"
|
2018-06-21 16:16:23 +01:00
|
|
|
#include "core/hle/service/filesystem/filesystem.h"
|
2018-08-15 03:37:12 +01:00
|
|
|
#include "core/loader/deconstructed_rom_directory.h"
|
2018-06-21 16:16:23 +01:00
|
|
|
#include "core/loader/nca.h"
|
2023-09-06 06:06:03 +01:00
|
|
|
#include "mbedtls/sha256.h"
|
2018-06-21 16:16:23 +01:00
|
|
|
|
|
|
|
namespace Loader {
|
|
|
|
|
2018-08-05 23:27:05 +01:00
|
|
|
AppLoader_NCA::AppLoader_NCA(FileSys::VirtualFile file_)
|
|
|
|
: AppLoader(std::move(file_)), nca(std::make_unique<FileSys::NCA>(file)) {}
|
2018-06-21 16:16:23 +01:00
|
|
|
|
2018-08-15 03:37:12 +01:00
|
|
|
AppLoader_NCA::~AppLoader_NCA() = default;
|
|
|
|
|
2021-04-27 17:05:34 +01:00
|
|
|
FileType AppLoader_NCA::IdentifyType(const FileSys::VirtualFile& nca_file) {
|
|
|
|
const FileSys::NCA nca(nca_file);
|
2018-07-08 04:24:51 +01:00
|
|
|
|
2018-07-28 04:55:23 +01:00
|
|
|
if (nca.GetStatus() == ResultStatus::Success &&
|
2021-04-27 17:05:34 +01:00
|
|
|
nca.GetType() == FileSys::NCAContentType::Program) {
|
2018-06-21 16:16:23 +01:00
|
|
|
return FileType::NCA;
|
2021-04-27 17:05:34 +01:00
|
|
|
}
|
2018-06-21 16:16:23 +01:00
|
|
|
|
|
|
|
return FileType::Error;
|
|
|
|
}
|
|
|
|
|
2021-04-24 06:04:28 +01:00
|
|
|
AppLoader_NCA::LoadResult AppLoader_NCA::Load(Kernel::KProcess& process, Core::System& system) {
|
2018-06-21 16:16:23 +01:00
|
|
|
if (is_loaded) {
|
2019-04-09 22:03:04 +01:00
|
|
|
return {ResultStatus::ErrorAlreadyLoaded, {}};
|
2018-06-21 16:16:23 +01:00
|
|
|
}
|
|
|
|
|
2018-08-05 23:27:05 +01:00
|
|
|
const auto result = nca->GetStatus();
|
2018-06-21 16:16:23 +01:00
|
|
|
if (result != ResultStatus::Success) {
|
2019-04-09 22:03:04 +01:00
|
|
|
return {result, {}};
|
2018-06-21 16:16:23 +01:00
|
|
|
}
|
|
|
|
|
2019-04-09 22:03:04 +01:00
|
|
|
if (nca->GetType() != FileSys::NCAContentType::Program) {
|
|
|
|
return {ResultStatus::ErrorNCANotProgram, {}};
|
|
|
|
}
|
2018-07-19 02:07:11 +01:00
|
|
|
|
2023-08-11 02:34:43 +01:00
|
|
|
auto exefs = nca->GetExeFS();
|
2019-04-09 22:03:04 +01:00
|
|
|
if (exefs == nullptr) {
|
2023-08-11 02:34:43 +01:00
|
|
|
LOG_INFO(Loader, "No ExeFS found in NCA, looking for ExeFS from update");
|
|
|
|
|
|
|
|
// This NCA may be a sparse base of an installed title.
|
|
|
|
// Try to fetch the ExeFS from the installed update.
|
|
|
|
const auto& installed = system.GetContentProvider();
|
|
|
|
const auto update_nca = installed.GetEntry(FileSys::GetUpdateTitleID(nca->GetTitleId()),
|
|
|
|
FileSys::ContentRecordType::Program);
|
|
|
|
|
|
|
|
if (update_nca) {
|
|
|
|
exefs = update_nca->GetExeFS();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exefs == nullptr) {
|
|
|
|
return {ResultStatus::ErrorNoExeFS, {}};
|
|
|
|
}
|
2019-04-09 22:03:04 +01:00
|
|
|
}
|
2018-07-19 02:07:11 +01:00
|
|
|
|
2018-08-26 00:04:48 +01:00
|
|
|
directory_loader = std::make_unique<AppLoader_DeconstructedRomDirectory>(exefs, true);
|
2018-06-21 16:16:23 +01:00
|
|
|
|
2020-09-16 13:19:25 +01:00
|
|
|
const auto load_result = directory_loader->Load(process, system);
|
2019-04-09 22:03:04 +01:00
|
|
|
if (load_result.first != ResultStatus::Success) {
|
2018-08-05 23:27:05 +01:00
|
|
|
return load_result;
|
2019-04-09 22:03:04 +01:00
|
|
|
}
|
2018-06-21 16:16:23 +01:00
|
|
|
|
2019-04-09 22:03:04 +01:00
|
|
|
if (nca->GetRomFS() != nullptr && nca->GetRomFS()->GetSize() > 0) {
|
2020-09-16 23:29:24 +01:00
|
|
|
system.GetFileSystemController().RegisterRomFS(std::make_unique<FileSys::RomFSFactory>(
|
|
|
|
*this, system.GetContentProvider(), system.GetFileSystemController()));
|
2019-04-09 22:03:04 +01:00
|
|
|
}
|
2018-07-08 04:24:51 +01:00
|
|
|
|
2018-06-21 16:16:23 +01:00
|
|
|
is_loaded = true;
|
2019-04-09 22:03:04 +01:00
|
|
|
return load_result;
|
2018-07-08 04:24:51 +01:00
|
|
|
}
|
|
|
|
|
2023-09-06 06:06:03 +01:00
|
|
|
ResultStatus AppLoader_NCA::VerifyIntegrity(std::function<bool(size_t, size_t)> progress_callback) {
|
|
|
|
using namespace Common::Literals;
|
|
|
|
|
|
|
|
constexpr size_t NcaFileNameWithHashLength = 36;
|
|
|
|
constexpr size_t NcaFileNameHashLength = 32;
|
|
|
|
constexpr size_t NcaSha256HashLength = 32;
|
|
|
|
constexpr size_t NcaSha256HalfHashLength = NcaSha256HashLength / 2;
|
|
|
|
|
|
|
|
// Get the file name.
|
|
|
|
const auto name = file->GetName();
|
|
|
|
|
|
|
|
// We won't try to verify meta NCAs.
|
|
|
|
if (name.ends_with(".cnmt.nca")) {
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we can verify this file. NCAs should be named after their hashes.
|
|
|
|
if (!name.ends_with(".nca") || name.size() != NcaFileNameWithHashLength) {
|
|
|
|
LOG_WARNING(Loader, "Unable to validate NCA with name {}", name);
|
|
|
|
return ResultStatus::ErrorIntegrityVerificationNotImplemented;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the expected truncated hash of the NCA.
|
|
|
|
const auto input_hash =
|
|
|
|
Common::HexStringToVector(file->GetName().substr(0, NcaFileNameHashLength), false);
|
|
|
|
|
|
|
|
// Declare buffer to read into.
|
|
|
|
std::vector<u8> buffer(4_MiB);
|
|
|
|
|
|
|
|
// Initialize sha256 verification context.
|
|
|
|
mbedtls_sha256_context ctx;
|
|
|
|
mbedtls_sha256_init(&ctx);
|
|
|
|
mbedtls_sha256_starts_ret(&ctx, 0);
|
|
|
|
|
|
|
|
// Ensure we maintain a clean state on exit.
|
|
|
|
SCOPE_EXIT({ mbedtls_sha256_free(&ctx); });
|
|
|
|
|
|
|
|
// Declare counters.
|
|
|
|
const size_t total_size = file->GetSize();
|
|
|
|
size_t processed_size = 0;
|
|
|
|
|
|
|
|
// Begin iterating the file.
|
|
|
|
while (processed_size < total_size) {
|
|
|
|
// Refill the buffer.
|
|
|
|
const size_t intended_read_size = std::min(buffer.size(), total_size - processed_size);
|
|
|
|
const size_t read_size = file->Read(buffer.data(), intended_read_size, processed_size);
|
|
|
|
|
|
|
|
// Update the hash function with the buffer contents.
|
|
|
|
mbedtls_sha256_update_ret(&ctx, buffer.data(), read_size);
|
|
|
|
|
|
|
|
// Update counters.
|
|
|
|
processed_size += read_size;
|
|
|
|
|
|
|
|
// Call the progress function.
|
|
|
|
if (!progress_callback(processed_size, total_size)) {
|
|
|
|
return ResultStatus::ErrorIntegrityVerificationFailed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize context and compute the output hash.
|
|
|
|
std::array<u8, NcaSha256HashLength> output_hash;
|
|
|
|
mbedtls_sha256_finish_ret(&ctx, output_hash.data());
|
|
|
|
|
|
|
|
// Compare to expected.
|
|
|
|
if (std::memcmp(input_hash.data(), output_hash.data(), NcaSha256HalfHashLength) != 0) {
|
|
|
|
LOG_ERROR(Loader, "NCA hash mismatch detected for file {}", name);
|
|
|
|
return ResultStatus::ErrorIntegrityVerificationFailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// File verified.
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2018-07-19 02:07:11 +01:00
|
|
|
ResultStatus AppLoader_NCA::ReadRomFS(FileSys::VirtualFile& dir) {
|
2021-04-27 17:05:34 +01:00
|
|
|
if (nca == nullptr) {
|
2018-08-10 02:06:44 +01:00
|
|
|
return ResultStatus::ErrorNotInitialized;
|
2021-04-27 17:05:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nca->GetRomFS() == nullptr || nca->GetRomFS()->GetSize() == 0) {
|
2018-08-10 02:06:44 +01:00
|
|
|
return ResultStatus::ErrorNoRomFS;
|
2021-04-27 17:05:34 +01:00
|
|
|
}
|
|
|
|
|
2018-07-19 02:07:11 +01:00
|
|
|
dir = nca->GetRomFS();
|
2018-06-21 16:16:23 +01:00
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:55:23 +01:00
|
|
|
ResultStatus AppLoader_NCA::ReadProgramId(u64& out_program_id) {
|
2021-04-27 17:05:34 +01:00
|
|
|
if (nca == nullptr || nca->GetStatus() != ResultStatus::Success) {
|
2018-08-10 02:06:44 +01:00
|
|
|
return ResultStatus::ErrorNotInitialized;
|
2021-04-27 17:05:34 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 04:55:23 +01:00
|
|
|
out_program_id = nca->GetTitleId();
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2019-01-15 20:56:52 +00:00
|
|
|
ResultStatus AppLoader_NCA::ReadBanner(std::vector<u8>& buffer) {
|
2021-04-27 17:05:34 +01:00
|
|
|
if (nca == nullptr || nca->GetStatus() != ResultStatus::Success) {
|
2019-01-15 20:56:52 +00:00
|
|
|
return ResultStatus::ErrorNotInitialized;
|
2021-04-27 17:05:34 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 20:56:52 +00:00
|
|
|
const auto logo = nca->GetLogoPartition();
|
2021-04-27 17:05:34 +01:00
|
|
|
if (logo == nullptr) {
|
2019-01-15 20:56:52 +00:00
|
|
|
return ResultStatus::ErrorNoIcon;
|
2021-04-27 17:05:34 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 20:56:52 +00:00
|
|
|
buffer = logo->GetFile("StartupMovie.gif")->ReadAllBytes();
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultStatus AppLoader_NCA::ReadLogo(std::vector<u8>& buffer) {
|
2021-04-27 17:05:34 +01:00
|
|
|
if (nca == nullptr || nca->GetStatus() != ResultStatus::Success) {
|
2019-01-15 20:56:52 +00:00
|
|
|
return ResultStatus::ErrorNotInitialized;
|
2021-04-27 17:05:34 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 20:56:52 +00:00
|
|
|
const auto logo = nca->GetLogoPartition();
|
2021-04-27 17:05:34 +01:00
|
|
|
if (logo == nullptr) {
|
2019-01-15 20:56:52 +00:00
|
|
|
return ResultStatus::ErrorNoIcon;
|
2021-04-27 17:05:34 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 20:56:52 +00:00
|
|
|
buffer = logo->GetFile("NintendoLogo.png")->ReadAllBytes();
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
2019-05-26 16:40:41 +01:00
|
|
|
|
|
|
|
ResultStatus AppLoader_NCA::ReadNSOModules(Modules& modules) {
|
|
|
|
if (directory_loader == nullptr) {
|
|
|
|
return ResultStatus::ErrorNotInitialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
return directory_loader->ReadNSOModules(modules);
|
|
|
|
}
|
|
|
|
|
2018-06-21 16:16:23 +01:00
|
|
|
} // namespace Loader
|