2018-07-19 01:07:11 +00:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-07-19 03:13:57 +00:00
|
|
|
#include <array>
|
|
|
|
#include <memory>
|
2018-10-30 04:03:25 +00:00
|
|
|
#include <optional>
|
2018-07-19 03:13:57 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2018-10-30 04:03:25 +00:00
|
|
|
|
2018-07-19 01:07:11 +00:00
|
|
|
#include "common/common_funcs.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/swap.h"
|
2018-07-28 03:55:23 +00:00
|
|
|
#include "core/crypto/key_manager.h"
|
2018-09-04 01:58:19 +00:00
|
|
|
#include "core/file_sys/vfs.h"
|
|
|
|
|
|
|
|
namespace Loader {
|
|
|
|
enum class ResultStatus : u16;
|
|
|
|
}
|
2018-07-19 01:07:11 +00:00
|
|
|
|
|
|
|
namespace FileSys {
|
2018-07-29 23:00:09 +00:00
|
|
|
|
|
|
|
union NCASectionHeader;
|
|
|
|
|
2019-03-19 19:14:52 +00:00
|
|
|
/// Describes the type of content within an NCA archive.
|
2018-07-19 01:07:11 +00:00
|
|
|
enum class NCAContentType : u8 {
|
2019-03-19 19:14:52 +00:00
|
|
|
/// Executable-related data
|
2018-07-19 01:07:11 +00:00
|
|
|
Program = 0,
|
2019-03-19 19:14:52 +00:00
|
|
|
|
|
|
|
/// Metadata.
|
2018-07-19 01:07:11 +00:00
|
|
|
Meta = 1,
|
2019-03-19 19:14:52 +00:00
|
|
|
|
|
|
|
/// Access control data.
|
2018-07-19 01:07:11 +00:00
|
|
|
Control = 2,
|
2019-03-19 19:14:52 +00:00
|
|
|
|
|
|
|
/// Information related to the game manual
|
|
|
|
/// e.g. Legal information, etc.
|
2018-07-19 01:07:11 +00:00
|
|
|
Manual = 3,
|
2019-03-19 19:14:52 +00:00
|
|
|
|
|
|
|
/// System data.
|
2018-07-19 01:07:11 +00:00
|
|
|
Data = 4,
|
2019-03-19 19:14:52 +00:00
|
|
|
|
|
|
|
/// Data that can be accessed by applications.
|
|
|
|
PublicData = 5,
|
2018-07-19 01:07:11 +00:00
|
|
|
};
|
|
|
|
|
2018-07-28 03:55:23 +00:00
|
|
|
enum class NCASectionCryptoType : u8 {
|
|
|
|
NONE = 1,
|
|
|
|
XTS = 2,
|
|
|
|
CTR = 3,
|
|
|
|
BKTR = 4,
|
|
|
|
};
|
|
|
|
|
2018-07-19 01:07:11 +00:00
|
|
|
struct NCASectionTableEntry {
|
|
|
|
u32_le media_offset;
|
|
|
|
u32_le media_end_offset;
|
|
|
|
INSERT_PADDING_BYTES(0x8);
|
|
|
|
};
|
|
|
|
static_assert(sizeof(NCASectionTableEntry) == 0x10, "NCASectionTableEntry has incorrect size.");
|
|
|
|
|
|
|
|
struct NCAHeader {
|
|
|
|
std::array<u8, 0x100> rsa_signature_1;
|
|
|
|
std::array<u8, 0x100> rsa_signature_2;
|
|
|
|
u32_le magic;
|
|
|
|
u8 is_system;
|
|
|
|
NCAContentType content_type;
|
|
|
|
u8 crypto_type;
|
|
|
|
u8 key_index;
|
|
|
|
u64_le size;
|
|
|
|
u64_le title_id;
|
|
|
|
INSERT_PADDING_BYTES(0x4);
|
|
|
|
u32_le sdk_version;
|
|
|
|
u8 crypto_type_2;
|
|
|
|
INSERT_PADDING_BYTES(15);
|
|
|
|
std::array<u8, 0x10> rights_id;
|
|
|
|
std::array<NCASectionTableEntry, 0x4> section_tables;
|
|
|
|
std::array<std::array<u8, 0x20>, 0x4> hash_tables;
|
2018-07-28 03:55:23 +00:00
|
|
|
std::array<u8, 0x40> key_area;
|
2018-07-19 01:07:11 +00:00
|
|
|
INSERT_PADDING_BYTES(0xC0);
|
|
|
|
};
|
|
|
|
static_assert(sizeof(NCAHeader) == 0x400, "NCAHeader has incorrect size.");
|
|
|
|
|
2020-12-10 06:31:58 +00:00
|
|
|
inline bool IsDirectoryExeFS(const VirtualDir& pfs) {
|
2018-07-19 01:07:11 +00:00
|
|
|
// According to switchbrew, an exefs must only contain these two files:
|
|
|
|
return pfs->GetFile("main") != nullptr && pfs->GetFile("main.npdm") != nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-15 20:56:32 +00:00
|
|
|
inline bool IsDirectoryLogoPartition(const VirtualDir& pfs) {
|
|
|
|
// NintendoLogo is the static image in the top left corner while StartupMovie is the animation
|
|
|
|
// in the bottom right corner.
|
|
|
|
return pfs->GetFile("NintendoLogo.png") != nullptr &&
|
|
|
|
pfs->GetFile("StartupMovie.gif") != nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-19 01:07:11 +00:00
|
|
|
// An implementation of VfsDirectory that represents a Nintendo Content Archive (NCA) conatiner.
|
|
|
|
// After construction, use GetStatus to determine if the file is valid and ready to be used.
|
|
|
|
class NCA : public ReadOnlyVfsDirectory {
|
|
|
|
public:
|
2018-08-29 02:37:42 +00:00
|
|
|
explicit NCA(VirtualFile file, VirtualFile bktr_base_romfs = nullptr,
|
2020-05-20 19:28:16 +00:00
|
|
|
u64 bktr_base_ivfc_offset = 0);
|
2018-09-19 23:19:05 +00:00
|
|
|
~NCA() override;
|
|
|
|
|
2018-07-19 01:07:11 +00:00
|
|
|
Loader::ResultStatus GetStatus() const;
|
|
|
|
|
2020-12-10 06:31:58 +00:00
|
|
|
std::vector<VirtualFile> GetFiles() const override;
|
|
|
|
std::vector<VirtualDir> GetSubdirectories() const override;
|
2018-07-19 01:07:11 +00:00
|
|
|
std::string GetName() const override;
|
2020-12-10 06:31:58 +00:00
|
|
|
VirtualDir GetParentDirectory() const override;
|
2018-07-19 01:07:11 +00:00
|
|
|
|
|
|
|
NCAContentType GetType() const;
|
|
|
|
u64 GetTitleId() const;
|
2019-04-10 14:23:13 +00:00
|
|
|
std::array<u8, 0x10> GetRightsId() const;
|
|
|
|
u32 GetSDKVersion() const;
|
2018-08-25 23:01:46 +00:00
|
|
|
bool IsUpdate() const;
|
2018-07-19 01:07:11 +00:00
|
|
|
|
|
|
|
VirtualFile GetRomFS() const;
|
|
|
|
VirtualDir GetExeFS() const;
|
|
|
|
|
2018-07-28 03:55:23 +00:00
|
|
|
VirtualFile GetBaseFile() const;
|
|
|
|
|
2018-08-29 02:37:42 +00:00
|
|
|
// Returns the base ivfc offset used in BKTR patching.
|
|
|
|
u64 GetBaseIVFCOffset() const;
|
|
|
|
|
2019-01-15 20:56:32 +00:00
|
|
|
VirtualDir GetLogoPartition() const;
|
|
|
|
|
2018-07-19 01:07:11 +00:00
|
|
|
private:
|
2018-10-16 16:12:50 +00:00
|
|
|
bool CheckSupportedNCA(const NCAHeader& header);
|
|
|
|
bool HandlePotentialHeaderDecryption();
|
|
|
|
|
|
|
|
std::vector<NCASectionHeader> ReadSectionHeaders() const;
|
|
|
|
bool ReadSections(const std::vector<NCASectionHeader>& sections, u64 bktr_base_ivfc_offset);
|
|
|
|
bool ReadRomFSSection(const NCASectionHeader& section, const NCASectionTableEntry& entry,
|
|
|
|
u64 bktr_base_ivfc_offset);
|
|
|
|
bool ReadPFS0Section(const NCASectionHeader& section, const NCASectionTableEntry& entry);
|
|
|
|
|
2018-08-04 18:57:21 +00:00
|
|
|
u8 GetCryptoRevision() const;
|
2018-10-30 04:03:25 +00:00
|
|
|
std::optional<Core::Crypto::Key128> GetKeyAreaKey(NCASectionCryptoType type) const;
|
|
|
|
std::optional<Core::Crypto::Key128> GetTitlekey();
|
2018-10-16 16:08:15 +00:00
|
|
|
VirtualFile Decrypt(const NCASectionHeader& header, VirtualFile in, u64 starting_offset);
|
2018-07-29 23:00:09 +00:00
|
|
|
|
2018-07-19 01:07:11 +00:00
|
|
|
std::vector<VirtualDir> dirs;
|
|
|
|
std::vector<VirtualFile> files;
|
|
|
|
|
|
|
|
VirtualFile romfs = nullptr;
|
|
|
|
VirtualDir exefs = nullptr;
|
2019-01-15 20:56:32 +00:00
|
|
|
VirtualDir logo = nullptr;
|
2018-07-19 01:07:11 +00:00
|
|
|
VirtualFile file;
|
2018-08-25 23:01:46 +00:00
|
|
|
VirtualFile bktr_base_romfs;
|
2018-10-16 16:12:50 +00:00
|
|
|
u64 ivfc_offset = 0;
|
2018-07-19 01:07:11 +00:00
|
|
|
|
|
|
|
NCAHeader header{};
|
2018-08-10 01:06:44 +00:00
|
|
|
bool has_rights_id{};
|
2018-07-19 01:07:11 +00:00
|
|
|
|
|
|
|
Loader::ResultStatus status{};
|
2018-07-28 03:55:23 +00:00
|
|
|
|
2018-10-16 16:12:50 +00:00
|
|
|
bool encrypted = false;
|
|
|
|
bool is_update = false;
|
2018-07-28 03:55:23 +00:00
|
|
|
|
2020-08-23 18:20:37 +00:00
|
|
|
Core::Crypto::KeyManager& keys;
|
2018-07-19 01:07:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace FileSys
|