| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <array> |
| 7 |
#include <functional> |
| 8 |
#include <map> |
| 9 |
#include <memory> |
| 10 |
#include <optional> |
| 11 |
#include <string> |
| 12 |
#include <vector> |
| 13 |
|
| 14 |
#include "Common/CommonTypes.h" |
| 15 |
#include "Common/Crypto/SHA1.h" |
| 16 |
#include "Common/Lazy.h" |
| 17 |
#include "Core/IOS/ES/Formats.h" |
| 18 |
#include "DiscIO/Filesystem.h" |
| 19 |
#include "DiscIO/Volume.h" |
| 20 |
#include "DiscIO/VolumeDisc.h" |
| 21 |
|
| 22 |
#include "Common/Crypto/AES.h" |
| 23 |
|
| 24 |
namespace DiscIO |
| 25 |
{ |
| 26 |
class BlobReader; |
| 27 |
enum class BlobType; |
| 28 |
enum class Country; |
| 29 |
class FileSystem; |
| 30 |
enum class Language; |
| 31 |
enum class Region; |
| 32 |
enum class Platform; |
| 33 |
|
| 34 |
class VolumeWii final : public VolumeDisc |
| 35 |
{ |
| 36 |
public: |
| 37 |
static constexpr size_t AES_KEY_SIZE = Common::AES::Context::KEY_SIZE; |
| 38 |
|
| 39 |
static constexpr u32 BLOCKS_PER_GROUP = 0x40; |
| 40 |
|
| 41 |
static constexpr u64 BLOCK_HEADER_SIZE = 0x0400; |
| 42 |
static constexpr u64 BLOCK_DATA_SIZE = 0x7C00; |
| 43 |
static constexpr u64 BLOCK_TOTAL_SIZE = BLOCK_HEADER_SIZE + BLOCK_DATA_SIZE; |
| 44 |
|
| 45 |
static constexpr u64 GROUP_HEADER_SIZE = BLOCK_HEADER_SIZE * BLOCKS_PER_GROUP; |
| 46 |
static constexpr u64 GROUP_DATA_SIZE = BLOCK_DATA_SIZE * BLOCKS_PER_GROUP; |
| 47 |
static constexpr u64 GROUP_TOTAL_SIZE = GROUP_HEADER_SIZE + GROUP_DATA_SIZE; |
| 48 |
|
| 49 |
struct HashBlock |
| 50 |
{ |
| 51 |
std::array<Common::SHA1::Digest, 31> h0; |
| 52 |
std::array<u8, 20> padding_0; |
| 53 |
std::array<Common::SHA1::Digest, 8> h1; |
| 54 |
std::array<u8, 32> padding_1; |
| 55 |
std::array<Common::SHA1::Digest, 8> h2; |
| 56 |
std::array<u8, 32> padding_2; |
| 57 |
}; |
| 58 |
static_assert(sizeof(HashBlock) == BLOCK_HEADER_SIZE); |
| 59 |
|
| 60 |
VolumeWii(std::unique_ptr<BlobReader> reader); |
| 61 |
~VolumeWii() override; |
| 62 |
bool Read(u64 offset, u64 length, u8* buffer, const Partition& partition) const override; |
| 63 |
bool HasWiiHashes() const override; |
| 64 |
bool HasWiiEncryption() const override; |
| 65 |
std::vector<Partition> GetPartitions() const override; |
| 66 |
Partition GetGamePartition() const override; |
| 67 |
std::optional<u32> GetPartitionType(const Partition& partition) const override; |
| 68 |
std::optional<u64> GetTitleID(const Partition& partition) const override; |
| 69 |
const IOS::ES::TicketReader& GetTicket(const Partition& partition) const override; |
| 70 |
const IOS::ES::TMDReader& GetTMD(const Partition& partition) const override; |
| 71 |
const std::vector<u8>& GetCertificateChain(const Partition& partition) const override; |
| 72 |
const FileSystem* GetFileSystem(const Partition& partition) const override; |
| 73 |
static u64 OffsetInHashedPartitionToRawOffset(u64 offset, const Partition& partition, |
| 74 |
u64 partition_data_offset); |
| 75 |
u64 PartitionOffsetToRawOffset(u64 offset, const Partition& partition) const override; |
| 76 |
std::string GetGameTDBID(const Partition& partition = PARTITION_NONE) const override; |
| 77 |
std::map<Language, std::string> GetLongNames() const override; |
| 78 |
std::vector<u32> GetBanner(u32* width, u32* height) const override; |
| 79 |
|
| 80 |
Platform GetVolumeType() const override; |
| 81 |
bool IsDatelDisc() const override; |
| 82 |
bool CheckH3TableIntegrity(const Partition& partition) const override; |
| 83 |
bool CheckBlockIntegrity(u64 block_index, const u8* encrypted_data, |
| 84 |
const Partition& partition) const override; |
| 85 |
bool CheckBlockIntegrity(u64 block_index, const Partition& partition) const override; |
| 86 |
|
| 87 |
Region GetRegion() const override; |
| 88 |
BlobType GetBlobType() const override; |
| 89 |
u64 GetDataSize() const override; |
| 90 |
DataSizeType GetDataSizeType() const override; |
| 91 |
u64 GetRawSize() const override; |
| 92 |
const BlobReader& GetBlobReader() const override; |
| 93 |
std::array<u8, 20> GetSyncHash() const override; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
static bool HashGroup(const std::array<u8, BLOCK_DATA_SIZE> in[BLOCKS_PER_GROUP], |
| 100 |
HashBlock out[BLOCKS_PER_GROUP], |
| 101 |
const std::function<bool(size_t block)>& read_function = {}); |
| 102 |
|
| 103 |
static bool EncryptGroup(u64 offset, u64 partition_data_offset, u64 partition_data_decrypted_size, |
| 104 |
const std::array<u8, AES_KEY_SIZE>& key, BlobReader* blob, |
| 105 |
std::array<u8, GROUP_TOTAL_SIZE>* out, |
| 106 |
const std::function<void(HashBlock hash_blocks[BLOCKS_PER_GROUP])>& |
| 107 |
hash_exception_callback = {}); |
| 108 |
|
| 109 |
static void DecryptBlockHashes(const u8* in, HashBlock* out, Common::AES::Context* aes_context); |
| 110 |
static void DecryptBlockData(const u8* in, u8* out, Common::AES::Context* aes_context); |
| 111 |
|
| 112 |
protected: |
| 113 |
u32 GetOffsetShift() const override { return 2; } |
| 114 |
|
| 115 |
private: |
| 116 |
struct PartitionDetails |
| 117 |
{ |
| 118 |
Common::Lazy<std::unique_ptr<Common::AES::Context>> key; |
| 119 |
Common::Lazy<IOS::ES::TicketReader> ticket; |
| 120 |
Common::Lazy<IOS::ES::TMDReader> tmd; |
| 121 |
Common::Lazy<std::vector<u8>> cert_chain; |
| 122 |
Common::Lazy<std::vector<u8>> h3_table; |
| 123 |
Common::Lazy<std::unique_ptr<FileSystem>> file_system; |
| 124 |
Common::Lazy<u64> data_offset; |
| 125 |
u32 type = 0; |
| 126 |
}; |
| 127 |
|
| 128 |
std::unique_ptr<BlobReader> m_reader; |
| 129 |
std::map<Partition, PartitionDetails> m_partitions; |
| 130 |
Partition m_game_partition; |
| 131 |
bool m_has_hashes; |
| 132 |
bool m_has_encryption; |
| 133 |
|
| 134 |
mutable u64 m_last_decrypted_block; |
| 135 |
mutable u8 m_last_decrypted_block_data[BLOCK_DATA_SIZE]{}; |
| 136 |
}; |
| 137 |
|
| 138 |
} |