2018-07-19 03:07:11 +02: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 05:13:57 +02:00
|
|
|
#include <array>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2018-07-30 02:47:33 +02:00
|
|
|
#include <boost/optional.hpp>
|
2018-07-19 03:07:11 +02:00
|
|
|
#include "common/common_funcs.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/swap.h"
|
2018-08-07 05:13:37 +02:00
|
|
|
#include "control_metadata.h"
|
2018-07-28 05:55:23 +02:00
|
|
|
#include "core/crypto/key_manager.h"
|
2018-07-19 03:07:11 +02:00
|
|
|
#include "core/file_sys/partition_filesystem.h"
|
2018-07-29 03:39:42 +02:00
|
|
|
#include "core/loader/loader.h"
|
2018-07-19 03:07:11 +02:00
|
|
|
|
|
|
|
namespace FileSys {
|
2018-07-30 01:00:09 +02:00
|
|
|
|
|
|
|
union NCASectionHeader;
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
enum class NCAContentType : u8 {
|
|
|
|
Program = 0,
|
|
|
|
Meta = 1,
|
|
|
|
Control = 2,
|
|
|
|
Manual = 3,
|
|
|
|
Data = 4,
|
2018-08-20 23:34:18 +02:00
|
|
|
Data_Unknown5 = 5, ///< Seems to be used on some system archives
|
2018-07-19 03:07:11 +02:00
|
|
|
};
|
|
|
|
|
2018-07-28 05:55:23 +02:00
|
|
|
enum class NCASectionCryptoType : u8 {
|
|
|
|
NONE = 1,
|
|
|
|
XTS = 2,
|
|
|
|
CTR = 3,
|
|
|
|
BKTR = 4,
|
|
|
|
};
|
|
|
|
|
2018-07-19 03:07:11 +02: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 05:55:23 +02:00
|
|
|
std::array<u8, 0x40> key_area;
|
2018-07-19 03:07:11 +02:00
|
|
|
INSERT_PADDING_BYTES(0xC0);
|
|
|
|
};
|
|
|
|
static_assert(sizeof(NCAHeader) == 0x400, "NCAHeader has incorrect size.");
|
|
|
|
|
2018-07-19 05:16:10 +02:00
|
|
|
inline bool IsDirectoryExeFS(const std::shared_ptr<VfsDirectory>& pfs) {
|
2018-07-19 03:07:11 +02:00
|
|
|
// According to switchbrew, an exefs must only contain these two files:
|
|
|
|
return pfs->GetFile("main") != nullptr && pfs->GetFile("main.npdm") != nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-28 05:55:23 +02:00
|
|
|
bool IsValidNCA(const NCAHeader& header);
|
2018-07-19 03:07:11 +02: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:
|
|
|
|
explicit NCA(VirtualFile file);
|
|
|
|
Loader::ResultStatus GetStatus() const;
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<VfsFile>> GetFiles() const override;
|
|
|
|
std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override;
|
|
|
|
std::string GetName() const override;
|
|
|
|
std::shared_ptr<VfsDirectory> GetParentDirectory() const override;
|
|
|
|
|
|
|
|
NCAContentType GetType() const;
|
|
|
|
u64 GetTitleId() const;
|
|
|
|
|
|
|
|
VirtualFile GetRomFS() const;
|
|
|
|
VirtualDir GetExeFS() const;
|
|
|
|
|
2018-07-28 05:55:23 +02:00
|
|
|
VirtualFile GetBaseFile() const;
|
|
|
|
|
2018-08-24 00:53:13 +02:00
|
|
|
bool IsUpdate() const;
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
protected:
|
|
|
|
bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override;
|
|
|
|
|
|
|
|
private:
|
2018-08-04 20:57:21 +02:00
|
|
|
u8 GetCryptoRevision() const;
|
2018-07-30 02:47:33 +02:00
|
|
|
boost::optional<Core::Crypto::Key128> GetKeyAreaKey(NCASectionCryptoType type) const;
|
2018-08-10 03:06:44 +02:00
|
|
|
boost::optional<Core::Crypto::Key128> GetTitlekey();
|
|
|
|
VirtualFile Decrypt(NCASectionHeader header, VirtualFile in, u64 starting_offset);
|
2018-07-30 01:00:09 +02:00
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
std::vector<VirtualDir> dirs;
|
|
|
|
std::vector<VirtualFile> files;
|
|
|
|
|
|
|
|
VirtualFile romfs = nullptr;
|
|
|
|
VirtualDir exefs = nullptr;
|
|
|
|
VirtualFile file;
|
|
|
|
|
|
|
|
NCAHeader header{};
|
2018-08-10 03:06:44 +02:00
|
|
|
bool has_rights_id{};
|
2018-08-24 00:53:13 +02:00
|
|
|
bool is_update{};
|
2018-07-19 03:07:11 +02:00
|
|
|
|
|
|
|
Loader::ResultStatus status{};
|
2018-07-28 05:55:23 +02:00
|
|
|
|
|
|
|
bool encrypted;
|
|
|
|
|
2018-07-29 03:39:42 +02:00
|
|
|
Core::Crypto::KeyManager keys;
|
2018-07-19 03:07:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace FileSys
|