// Copyright 2009 Dolphin Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later #include "DiscIO/Volume.h" #include #include #include #include #include #include #include #include #include #include "Common/CommonTypes.h" #include "Common/StringUtil.h" #include "Core/IOS/ES/Formats.h" #include "DiscIO/Blob.h" #include "DiscIO/DiscUtils.h" #include "DiscIO/Enums.h" #include "DiscIO/VolumeDisc.h" #include "DiscIO/VolumeGC.h" #include "DiscIO/VolumeWad.h" #include "DiscIO/VolumeWii.h" namespace DiscIO { const IOS::ES::TicketReader Volume::INVALID_TICKET{}; const IOS::ES::TMDReader Volume::INVALID_TMD{}; const std::vector Volume::INVALID_CERT_CHAIN{}; template static void AddToSyncHash(mbedtls_sha1_context* context, const T& data) { static_assert(std::is_trivially_copyable_v); mbedtls_sha1_update_ret(context, reinterpret_cast(&data), sizeof(data)); } void Volume::ReadAndAddToSyncHash(mbedtls_sha1_context* context, u64 offset, u64 length, const Partition& partition) const { std::vector buffer(length); if (Read(offset, length, buffer.data(), partition)) mbedtls_sha1_update_ret(context, buffer.data(), buffer.size()); } void Volume::AddTMDToSyncHash(mbedtls_sha1_context* context, const Partition& partition) const { // 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); } std::map Volume::ReadWiiNames(const std::vector& data) { std::map names; for (size_t i = 0; i < NUMBER_OF_LANGUAGES; ++i) { const size_t name_start = NAME_CHARS_LENGTH * i; if (name_start + NAME_CHARS_LENGTH <= data.size()) { const std::string name = UTF16BEToUTF8(data.data() + name_start, NAME_CHARS_LENGTH); if (!name.empty()) names[static_cast(i)] = name; } } return names; } static std::unique_ptr CreateDisc(std::unique_ptr& reader) { if (reader->ReadSwapped(0x18) == WII_DISC_MAGIC) return std::make_unique(std::move(reader)); if (reader->ReadSwapped(0x1C) == GAMECUBE_DISC_MAGIC) return std::make_unique(std::move(reader)); // No known magic words found return nullptr; } std::unique_ptr CreateDisc(const std::string& path) { std::unique_ptr reader(CreateBlobReader(path)); return reader ? CreateDisc(reader) : nullptr; } static std::unique_ptr CreateWAD(std::unique_ptr& reader) { // Check for WAD // 0x206962 for boot2 wads const std::optional wad_magic = reader->ReadSwapped(0x02); if (wad_magic == u32(0x00204973) || wad_magic == u32(0x00206962)) return std::make_unique(std::move(reader)); // No known magic words found return nullptr; } std::unique_ptr CreateWAD(const std::string& path) { std::unique_ptr reader(CreateBlobReader(path)); return reader ? CreateWAD(reader) : nullptr; } std::unique_ptr CreateVolume(const std::string& path) { std::unique_ptr reader(CreateBlobReader(path)); if (reader == nullptr) return nullptr; std::unique_ptr disc = CreateDisc(reader); if (disc) return disc; std::unique_ptr wad = CreateWAD(reader); if (wad) return wad; return nullptr; } } // namespace DiscIO