2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2014-02-10 11:54:46 -07:00
|
|
|
#pragma once
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2020-01-23 08:47:49 -07:00
|
|
|
#include <array>
|
2017-06-08 08:07:01 -06:00
|
|
|
#include <cstddef>
|
2017-06-10 09:51:22 -06:00
|
|
|
#include <map>
|
2014-08-25 21:40:05 -06:00
|
|
|
#include <memory>
|
2017-06-04 02:33:14 -06:00
|
|
|
#include <optional>
|
2017-06-08 08:07:01 -06:00
|
|
|
#include <set>
|
2013-10-18 01:32:56 -06:00
|
|
|
#include <string>
|
2017-06-08 08:37:51 -06:00
|
|
|
#include <variant>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <vector>
|
2013-10-18 01:32:56 -06:00
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "Common/CommonTypes.h"
|
2017-06-07 12:32:09 -06:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "DiscIO/Blob.h"
|
2020-01-23 08:47:49 -07:00
|
|
|
#include "DiscIO/WiiEncryptionCache.h"
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
namespace File
|
|
|
|
{
|
|
|
|
struct FSTEntry;
|
2017-06-07 12:32:09 -06:00
|
|
|
class IOFile;
|
2019-05-05 17:48:12 -06:00
|
|
|
} // namespace File
|
2014-02-20 17:47:53 -07:00
|
|
|
|
2008-09-22 18:05:08 -06:00
|
|
|
namespace DiscIO
|
|
|
|
{
|
2017-06-11 05:22:13 -06:00
|
|
|
enum class PartitionType : u32;
|
|
|
|
|
2020-01-23 08:47:49 -07:00
|
|
|
class DirectoryBlobReader;
|
|
|
|
|
2017-06-11 06:45:42 -06:00
|
|
|
// Returns true if the path is inside a DirectoryBlob and doesn't represent the DirectoryBlob itself
|
|
|
|
bool ShouldHideFromGameList(const std::string& volume_path);
|
|
|
|
|
2021-09-20 23:58:41 -06:00
|
|
|
using ContentSource =
|
|
|
|
std::variant<std::string, // File
|
|
|
|
const u8*, // Memory
|
|
|
|
DirectoryBlobReader* // Partition (which one it is is determined by m_offset)
|
|
|
|
>;
|
|
|
|
|
2017-06-08 08:07:01 -06:00
|
|
|
class DiscContent
|
|
|
|
{
|
|
|
|
public:
|
2021-09-20 23:58:41 -06:00
|
|
|
DiscContent(u64 offset, u64 size, ContentSource source);
|
2017-06-08 08:07:01 -06:00
|
|
|
|
|
|
|
// Provided because it's convenient when searching for DiscContent in an std::set
|
|
|
|
explicit DiscContent(u64 offset);
|
|
|
|
|
|
|
|
u64 GetOffset() const;
|
2017-08-01 11:04:33 -06:00
|
|
|
u64 GetEndOffset() const;
|
2017-06-08 08:07:01 -06:00
|
|
|
u64 GetSize() const;
|
|
|
|
bool Read(u64* offset, u64* length, u8** buffer) const;
|
|
|
|
|
2017-08-01 11:04:33 -06:00
|
|
|
bool operator==(const DiscContent& other) const { return GetEndOffset() == other.GetEndOffset(); }
|
2017-06-08 08:07:01 -06:00
|
|
|
bool operator!=(const DiscContent& other) const { return !(*this == other); }
|
2017-08-01 11:04:33 -06:00
|
|
|
bool operator<(const DiscContent& other) const { return GetEndOffset() < other.GetEndOffset(); }
|
2017-06-08 08:07:01 -06:00
|
|
|
bool operator>(const DiscContent& other) const { return other < *this; }
|
2021-09-21 00:01:21 -06:00
|
|
|
bool operator<=(const DiscContent& other) const { return !(*this > other); }
|
|
|
|
bool operator>=(const DiscContent& other) const { return !(*this < other); }
|
2018-04-12 06:18:04 -06:00
|
|
|
|
2017-06-08 08:07:01 -06:00
|
|
|
private:
|
2021-09-21 17:30:08 -06:00
|
|
|
// Position of this content chunk within its parent DiscContentContainer.
|
2017-06-08 08:07:01 -06:00
|
|
|
u64 m_offset;
|
2021-09-21 17:30:08 -06:00
|
|
|
|
|
|
|
// Number of bytes this content chunk takes up.
|
2017-06-08 08:07:01 -06:00
|
|
|
u64 m_size = 0;
|
2021-09-21 17:30:08 -06:00
|
|
|
|
|
|
|
// Where and how to find the data for this content chunk.
|
2017-06-08 08:37:51 -06:00
|
|
|
ContentSource m_content_source;
|
2017-06-08 08:07:01 -06:00
|
|
|
};
|
|
|
|
|
2017-08-01 09:45:46 -06:00
|
|
|
class DiscContentContainer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template <typename T>
|
2021-09-20 23:40:59 -06:00
|
|
|
void AddReference(u64 offset, const std::vector<T>& vector)
|
2017-08-01 09:45:46 -06:00
|
|
|
{
|
|
|
|
return Add(offset, vector.size() * sizeof(T), reinterpret_cast<const u8*>(vector.data()));
|
|
|
|
}
|
2021-09-20 23:58:41 -06:00
|
|
|
void Add(u64 offset, u64 size, ContentSource source);
|
2017-08-01 11:13:19 -06:00
|
|
|
u64 CheckSizeAndAdd(u64 offset, const std::string& path);
|
|
|
|
u64 CheckSizeAndAdd(u64 offset, u64 max_size, const std::string& path);
|
2017-08-01 09:45:46 -06:00
|
|
|
|
|
|
|
bool Read(u64 offset, u64 length, u8* buffer) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::set<DiscContent> m_contents;
|
|
|
|
};
|
|
|
|
|
2017-06-10 07:54:26 -06:00
|
|
|
class DirectoryBlobPartition
|
|
|
|
{
|
|
|
|
public:
|
2017-06-10 09:51:22 -06:00
|
|
|
DirectoryBlobPartition() = default;
|
2017-06-10 09:31:03 -06:00
|
|
|
DirectoryBlobPartition(const std::string& root_directory, std::optional<bool> is_wii);
|
2017-06-10 07:54:26 -06:00
|
|
|
|
|
|
|
// We do not allow copying, because it might mess up the pointers inside DiscContents
|
|
|
|
DirectoryBlobPartition(const DirectoryBlobPartition&) = delete;
|
|
|
|
DirectoryBlobPartition& operator=(const DirectoryBlobPartition&) = delete;
|
|
|
|
DirectoryBlobPartition(DirectoryBlobPartition&&) = default;
|
|
|
|
DirectoryBlobPartition& operator=(DirectoryBlobPartition&&) = default;
|
|
|
|
|
|
|
|
bool IsWii() const { return m_is_wii; }
|
2017-06-10 11:07:12 -06:00
|
|
|
u64 GetDataSize() const { return m_data_size; }
|
2017-06-11 05:22:13 -06:00
|
|
|
const std::string& GetRootDirectory() const { return m_root_directory; }
|
2017-07-18 13:06:35 -06:00
|
|
|
const std::vector<u8>& GetHeader() const { return m_disc_header; }
|
2017-08-01 09:45:46 -06:00
|
|
|
const DiscContentContainer& GetContents() const { return m_contents; }
|
2018-04-12 06:18:04 -06:00
|
|
|
|
2020-01-23 08:47:49 -07:00
|
|
|
const std::array<u8, VolumeWii::AES_KEY_SIZE>& GetKey() const { return m_key; }
|
|
|
|
void SetKey(std::array<u8, VolumeWii::AES_KEY_SIZE> key) { m_key = key; }
|
|
|
|
|
2017-06-10 07:54:26 -06:00
|
|
|
private:
|
2017-06-10 09:31:03 -06:00
|
|
|
void SetDiscHeaderAndDiscType(std::optional<bool> is_wii);
|
2017-06-10 07:54:26 -06:00
|
|
|
void SetBI2();
|
|
|
|
|
|
|
|
// Returns DOL address
|
|
|
|
u64 SetApploader();
|
|
|
|
// Returns FST address
|
|
|
|
u64 SetDOL(u64 dol_address);
|
|
|
|
|
|
|
|
void BuildFST(u64 fst_address);
|
|
|
|
|
|
|
|
// FST creation
|
|
|
|
void WriteEntryData(u32* entry_offset, u8 type, u32 name_offset, u64 data_offset, u64 length,
|
|
|
|
u32 address_shift);
|
|
|
|
void WriteEntryName(u32* name_offset, const std::string& name, u64 name_table_offset);
|
|
|
|
void WriteDirectory(const File::FSTEntry& parent_entry, u32* fst_offset, u32* name_offset,
|
|
|
|
u64* data_offset, u32 parent_entry_index, u64 name_table_offset);
|
|
|
|
|
2017-08-01 09:45:46 -06:00
|
|
|
DiscContentContainer m_contents;
|
2017-07-18 13:06:35 -06:00
|
|
|
std::vector<u8> m_disc_header;
|
2017-07-18 07:53:04 -06:00
|
|
|
std::vector<u8> m_bi2;
|
2017-06-10 07:54:26 -06:00
|
|
|
std::vector<u8> m_apploader;
|
|
|
|
std::vector<u8> m_fst_data;
|
|
|
|
|
2021-09-03 22:43:19 -06:00
|
|
|
std::array<u8, VolumeWii::AES_KEY_SIZE> m_key{};
|
2020-01-23 08:47:49 -07:00
|
|
|
|
2017-06-10 07:54:26 -06:00
|
|
|
std::string m_root_directory;
|
2017-06-10 09:51:22 -06:00
|
|
|
bool m_is_wii = false;
|
2017-06-10 07:54:26 -06:00
|
|
|
// GameCube has no shift, Wii has 2 bit shift
|
2017-06-10 09:51:22 -06:00
|
|
|
u32 m_address_shift = 0;
|
2017-06-10 11:07:12 -06:00
|
|
|
|
2021-09-03 22:43:19 -06:00
|
|
|
u64 m_data_size = 0;
|
2017-06-10 07:54:26 -06:00
|
|
|
};
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
class DirectoryBlobReader : public BlobReader
|
2008-09-22 18:05:08 -06:00
|
|
|
{
|
2020-01-23 08:47:49 -07:00
|
|
|
friend DiscContent;
|
|
|
|
|
2009-12-10 02:16:10 -07:00
|
|
|
public:
|
2017-06-09 04:45:34 -06:00
|
|
|
static std::unique_ptr<DirectoryBlobReader> Create(const std::string& dol_path);
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2017-06-08 08:37:51 -06:00
|
|
|
// We do not allow copying, because it might mess up the pointers inside DiscContents
|
|
|
|
DirectoryBlobReader(const DirectoryBlobReader&) = delete;
|
|
|
|
DirectoryBlobReader& operator=(const DirectoryBlobReader&) = delete;
|
|
|
|
DirectoryBlobReader(DirectoryBlobReader&&) = default;
|
|
|
|
DirectoryBlobReader& operator=(DirectoryBlobReader&&) = default;
|
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
bool Read(u64 offset, u64 length, u8* buffer) override;
|
2020-08-29 07:12:02 -06:00
|
|
|
bool SupportsReadWiiDecrypted(u64 offset, u64 size, u64 partition_data_offset) const override;
|
2019-12-27 14:04:51 -07:00
|
|
|
bool ReadWiiDecrypted(u64 offset, u64 size, u8* buffer, u64 partition_data_offset) override;
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
BlobType GetBlobType() const override;
|
2020-06-07 06:11:00 -06:00
|
|
|
|
2017-06-07 12:32:09 -06:00
|
|
|
u64 GetRawSize() const override;
|
|
|
|
u64 GetDataSize() const override;
|
2019-03-21 15:20:23 -06:00
|
|
|
bool IsDataSizeAccurate() const override { return true; }
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2020-06-07 06:11:00 -06:00
|
|
|
u64 GetBlockSize() const override { return 0; }
|
|
|
|
bool HasFastRandomAccessInBlock() const override { return true; }
|
2020-06-21 12:41:50 -06:00
|
|
|
std::string GetCompressionMethod() const override { return {}; }
|
2020-06-07 06:11:00 -06:00
|
|
|
|
2009-12-10 02:16:10 -07:00
|
|
|
private:
|
2017-06-11 05:22:13 -06:00
|
|
|
struct PartitionWithType
|
|
|
|
{
|
|
|
|
PartitionWithType(DirectoryBlobPartition&& partition_, PartitionType type_)
|
|
|
|
: partition(std::move(partition_)), type(type_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DirectoryBlobPartition partition;
|
|
|
|
PartitionType type;
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit DirectoryBlobReader(const std::string& game_partition_root,
|
|
|
|
const std::string& true_root);
|
2017-06-07 12:32:09 -06:00
|
|
|
|
2020-08-29 07:12:02 -06:00
|
|
|
const DirectoryBlobPartition* GetPartition(u64 offset, u64 size, u64 partition_data_offset) const;
|
|
|
|
|
2020-01-23 08:47:49 -07:00
|
|
|
bool EncryptPartitionData(u64 offset, u64 size, u8* buffer, u64 partition_data_offset,
|
|
|
|
u64 partition_data_decrypted_size);
|
|
|
|
|
2017-06-10 12:11:58 -06:00
|
|
|
void SetNonpartitionDiscHeader(const std::vector<u8>& partition_header,
|
|
|
|
const std::string& game_partition_root);
|
|
|
|
void SetWiiRegionData(const std::string& game_partition_root);
|
2017-06-11 05:22:13 -06:00
|
|
|
void SetPartitions(std::vector<PartitionWithType>&& partitions);
|
2020-01-23 08:47:49 -07:00
|
|
|
void SetPartitionHeader(DirectoryBlobPartition* partition, u64 partition_address);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-10 09:51:22 -06:00
|
|
|
// For GameCube:
|
|
|
|
DirectoryBlobPartition m_gamecube_pseudopartition;
|
|
|
|
|
|
|
|
// For Wii:
|
2017-08-01 09:45:46 -06:00
|
|
|
DiscContentContainer m_nonpartition_contents;
|
2017-06-10 09:51:22 -06:00
|
|
|
std::map<u64, DirectoryBlobPartition> m_partitions;
|
2020-01-23 08:47:49 -07:00
|
|
|
WiiEncryptionCache m_encryption_cache;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-09 04:08:17 -06:00
|
|
|
bool m_is_wii;
|
2020-01-23 08:47:49 -07:00
|
|
|
bool m_encrypted;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-07-18 13:06:35 -06:00
|
|
|
std::vector<u8> m_disc_header_nonpartition;
|
2017-06-11 05:22:13 -06:00
|
|
|
std::vector<u8> m_partition_table;
|
2017-06-10 01:53:36 -06:00
|
|
|
std::vector<u8> m_wii_region_data;
|
2017-06-13 07:52:02 -06:00
|
|
|
std::vector<std::vector<u8>> m_partition_headers;
|
2017-06-10 11:07:12 -06:00
|
|
|
|
|
|
|
u64 m_data_size;
|
2008-09-22 18:05:08 -06:00
|
|
|
};
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2019-05-05 17:48:12 -06:00
|
|
|
} // namespace DiscIO
|