2016-07-06 20:33:05 +02:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2016-07-06 20:33:05 +02:00
|
|
|
|
2017-03-03 20:43:52 +01:00
|
|
|
#include "DiscIO/Volume.h"
|
|
|
|
|
2016-07-06 20:33:05 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
2017-05-19 18:33:21 +02:00
|
|
|
#include <memory>
|
2017-06-04 10:33:14 +02:00
|
|
|
#include <optional>
|
2016-07-06 20:33:05 +02:00
|
|
|
#include <string>
|
2020-06-07 22:58:03 +02:00
|
|
|
#include <type_traits>
|
2016-07-06 20:33:05 +02:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
2022-07-24 07:45:10 +02:00
|
|
|
#include "Common/Crypto/SHA1.h"
|
2016-07-06 20:33:05 +02:00
|
|
|
#include "Common/StringUtil.h"
|
2017-03-03 20:43:52 +01:00
|
|
|
|
2020-06-07 22:58:03 +02:00
|
|
|
#include "Core/IOS/ES/Formats.h"
|
2017-05-19 18:33:21 +02:00
|
|
|
#include "DiscIO/Blob.h"
|
2021-03-09 21:06:57 +01:00
|
|
|
#include "DiscIO/DiscUtils.h"
|
2016-07-06 20:33:05 +02:00
|
|
|
#include "DiscIO/Enums.h"
|
2020-06-04 20:33:52 +02:00
|
|
|
#include "DiscIO/VolumeDisc.h"
|
2017-05-19 18:33:21 +02:00
|
|
|
#include "DiscIO/VolumeGC.h"
|
|
|
|
#include "DiscIO/VolumeWad.h"
|
2017-06-06 11:49:01 +02:00
|
|
|
#include "DiscIO/VolumeWii.h"
|
2016-07-06 20:33:05 +02:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2017-06-06 11:49:01 +02:00
|
|
|
const IOS::ES::TicketReader Volume::INVALID_TICKET{};
|
|
|
|
const IOS::ES::TMDReader Volume::INVALID_TMD{};
|
2019-03-21 23:04:44 +01:00
|
|
|
const std::vector<u8> Volume::INVALID_CERT_CHAIN{};
|
2017-05-20 18:33:36 +02:00
|
|
|
|
2020-06-07 22:58:03 +02:00
|
|
|
template <typename T>
|
2022-07-24 07:45:10 +02:00
|
|
|
static void AddToSyncHash(Common::SHA1::Context* context, const T& data)
|
2020-06-07 22:58:03 +02:00
|
|
|
{
|
|
|
|
static_assert(std::is_trivially_copyable_v<T>);
|
2022-07-24 07:45:10 +02:00
|
|
|
context->Update(reinterpret_cast<const u8*>(&data), sizeof(data));
|
2020-06-07 22:58:03 +02:00
|
|
|
}
|
|
|
|
|
2022-07-24 07:45:10 +02:00
|
|
|
void Volume::ReadAndAddToSyncHash(Common::SHA1::Context* context, u64 offset, u64 length,
|
2020-06-07 22:58:03 +02:00
|
|
|
const Partition& partition) const
|
|
|
|
{
|
|
|
|
std::vector<u8> buffer(length);
|
|
|
|
if (Read(offset, length, buffer.data(), partition))
|
2022-07-24 07:45:10 +02:00
|
|
|
context->Update(buffer);
|
2020-06-07 22:58:03 +02:00
|
|
|
}
|
|
|
|
|
2022-07-24 07:45:10 +02:00
|
|
|
void Volume::AddTMDToSyncHash(Common::SHA1::Context* context, const Partition& partition) const
|
2020-06-07 22:58:03 +02:00
|
|
|
{
|
|
|
|
// We want to hash some important parts of the TMD, but nothing that changes when fakesigning.
|
|
|
|
// (Fakesigned WADs are very popular, and we don't want people with properly signed WADs to
|
|
|
|
// unnecessarily be at a disadvantage due to most netplay partners having fakesigned WADs.)
|
|
|
|
|
|
|
|
const IOS::ES::TMDReader& tmd = GetTMD(partition);
|
|
|
|
if (!tmd.IsValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
AddToSyncHash(context, tmd.GetIOSId());
|
|
|
|
AddToSyncHash(context, tmd.GetTitleId());
|
|
|
|
AddToSyncHash(context, tmd.GetTitleFlags());
|
|
|
|
AddToSyncHash(context, tmd.GetGroupId());
|
|
|
|
AddToSyncHash(context, tmd.GetRegion());
|
|
|
|
AddToSyncHash(context, tmd.GetTitleVersion());
|
|
|
|
AddToSyncHash(context, tmd.GetBootIndex());
|
|
|
|
|
|
|
|
for (const IOS::ES::Content& content : tmd.GetContents())
|
|
|
|
AddToSyncHash(context, content);
|
|
|
|
}
|
|
|
|
|
2017-11-02 21:05:37 +01:00
|
|
|
std::map<Language, std::string> Volume::ReadWiiNames(const std::vector<char16_t>& data)
|
2016-07-06 20:33:05 +02:00
|
|
|
{
|
|
|
|
std::map<Language, std::string> names;
|
|
|
|
for (size_t i = 0; i < NUMBER_OF_LANGUAGES; ++i)
|
|
|
|
{
|
2017-11-02 21:05:37 +01:00
|
|
|
const size_t name_start = NAME_CHARS_LENGTH * i;
|
|
|
|
if (name_start + NAME_CHARS_LENGTH <= data.size())
|
2016-07-06 20:33:05 +02:00
|
|
|
{
|
2017-11-02 21:05:37 +01:00
|
|
|
const std::string name = UTF16BEToUTF8(data.data() + name_start, NAME_CHARS_LENGTH);
|
2016-07-06 20:33:05 +02:00
|
|
|
if (!name.empty())
|
|
|
|
names[static_cast<Language>(i)] = name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return names;
|
|
|
|
}
|
2017-05-19 18:33:21 +02:00
|
|
|
|
2021-09-22 05:43:05 +02:00
|
|
|
static std::unique_ptr<VolumeDisc> TryCreateDisc(std::unique_ptr<BlobReader>& reader)
|
2017-05-19 18:33:21 +02:00
|
|
|
{
|
2021-09-22 05:43:05 +02:00
|
|
|
if (!reader)
|
|
|
|
return nullptr;
|
|
|
|
|
2021-03-09 21:06:57 +01:00
|
|
|
if (reader->ReadSwapped<u32>(0x18) == WII_DISC_MAGIC)
|
2017-06-06 11:49:01 +02:00
|
|
|
return std::make_unique<VolumeWii>(std::move(reader));
|
2017-05-19 18:33:21 +02:00
|
|
|
|
2021-03-09 21:06:57 +01:00
|
|
|
if (reader->ReadSwapped<u32>(0x1C) == GAMECUBE_DISC_MAGIC)
|
2019-07-14 15:49:42 +02:00
|
|
|
return std::make_unique<VolumeGC>(std::move(reader));
|
|
|
|
|
|
|
|
// No known magic words found
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-09-22 05:43:05 +02:00
|
|
|
std::unique_ptr<VolumeDisc> CreateDisc(std::unique_ptr<BlobReader> reader)
|
|
|
|
{
|
|
|
|
return TryCreateDisc(reader);
|
|
|
|
}
|
|
|
|
|
2019-07-14 15:49:42 +02:00
|
|
|
std::unique_ptr<VolumeDisc> CreateDisc(const std::string& path)
|
|
|
|
{
|
2021-09-22 05:43:05 +02:00
|
|
|
return CreateDisc(CreateBlobReader(path));
|
2019-07-14 15:49:42 +02:00
|
|
|
}
|
|
|
|
|
2021-09-22 05:43:05 +02:00
|
|
|
static std::unique_ptr<VolumeWAD> TryCreateWAD(std::unique_ptr<BlobReader>& reader)
|
2019-07-14 15:49:42 +02:00
|
|
|
{
|
2021-09-22 05:43:05 +02:00
|
|
|
if (!reader)
|
|
|
|
return nullptr;
|
|
|
|
|
2017-05-19 18:33:21 +02:00
|
|
|
// Check for WAD
|
|
|
|
// 0x206962 for boot2 wads
|
2017-06-04 10:33:14 +02:00
|
|
|
const std::optional<u32> wad_magic = reader->ReadSwapped<u32>(0x02);
|
|
|
|
if (wad_magic == u32(0x00204973) || wad_magic == u32(0x00206962))
|
2017-06-06 11:49:01 +02:00
|
|
|
return std::make_unique<VolumeWAD>(std::move(reader));
|
2017-05-19 18:33:21 +02:00
|
|
|
|
|
|
|
// No known magic words found
|
|
|
|
return nullptr;
|
2016-07-06 20:33:05 +02:00
|
|
|
}
|
2017-05-19 18:33:21 +02:00
|
|
|
|
2021-09-22 05:43:05 +02:00
|
|
|
std::unique_ptr<VolumeWAD> CreateWAD(std::unique_ptr<BlobReader> reader)
|
2019-07-14 15:49:42 +02:00
|
|
|
{
|
2021-09-22 05:43:05 +02:00
|
|
|
return TryCreateWAD(reader);
|
2019-07-14 15:49:42 +02:00
|
|
|
}
|
|
|
|
|
2021-09-22 05:43:05 +02:00
|
|
|
std::unique_ptr<VolumeWAD> CreateWAD(const std::string& path)
|
2019-07-14 15:49:42 +02:00
|
|
|
{
|
2021-09-22 05:43:05 +02:00
|
|
|
return CreateWAD(CreateBlobReader(path));
|
|
|
|
}
|
2019-07-14 15:49:42 +02:00
|
|
|
|
2021-09-22 05:43:05 +02:00
|
|
|
std::unique_ptr<Volume> CreateVolume(std::unique_ptr<BlobReader> reader)
|
|
|
|
{
|
|
|
|
std::unique_ptr<VolumeDisc> disc = TryCreateDisc(reader);
|
2019-07-14 15:49:42 +02:00
|
|
|
if (disc)
|
|
|
|
return disc;
|
|
|
|
|
2021-09-22 05:43:05 +02:00
|
|
|
std::unique_ptr<VolumeWAD> wad = TryCreateWAD(reader);
|
2019-07-14 15:49:42 +02:00
|
|
|
if (wad)
|
|
|
|
return wad;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-09-22 05:43:05 +02:00
|
|
|
std::unique_ptr<Volume> CreateVolume(const std::string& path)
|
|
|
|
{
|
|
|
|
return CreateVolume(CreateBlobReader(path));
|
|
|
|
}
|
2019-05-06 01:48:12 +02:00
|
|
|
} // namespace DiscIO
|