Volume.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2009 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "DiscIO/Volume.h"
  4. #include <algorithm>
  5. #include <map>
  6. #include <memory>
  7. #include <optional>
  8. #include <string>
  9. #include <type_traits>
  10. #include <utility>
  11. #include <vector>
  12. #include "Common/CommonTypes.h"
  13. #include "Common/Crypto/SHA1.h"
  14. #include "Common/StringUtil.h"
  15. #include "Core/IOS/ES/Formats.h"
  16. #include "DiscIO/Blob.h"
  17. #include "DiscIO/DiscUtils.h"
  18. #include "DiscIO/Enums.h"
  19. #include "DiscIO/VolumeDisc.h"
  20. #include "DiscIO/VolumeGC.h"
  21. #include "DiscIO/VolumeWad.h"
  22. #include "DiscIO/VolumeWii.h"
  23. namespace DiscIO
  24. {
  25. const IOS::ES::TicketReader Volume::INVALID_TICKET{};
  26. const IOS::ES::TMDReader Volume::INVALID_TMD{};
  27. const std::vector<u8> Volume::INVALID_CERT_CHAIN{};
  28. template <typename T>
  29. static void AddToSyncHash(Common::SHA1::Context* context, const T& data)
  30. {
  31. static_assert(std::is_trivially_copyable_v<T>);
  32. context->Update(reinterpret_cast<const u8*>(&data), sizeof(data));
  33. }
  34. void Volume::ReadAndAddToSyncHash(Common::SHA1::Context* context, u64 offset, u64 length,
  35. const Partition& partition) const
  36. {
  37. std::vector<u8> buffer(length);
  38. if (Read(offset, length, buffer.data(), partition))
  39. context->Update(buffer);
  40. }
  41. void Volume::AddTMDToSyncHash(Common::SHA1::Context* context, const Partition& partition) const
  42. {
  43. // We want to hash some important parts of the TMD, but nothing that changes when fakesigning.
  44. // (Fakesigned WADs are very popular, and we don't want people with properly signed WADs to
  45. // unnecessarily be at a disadvantage due to most netplay partners having fakesigned WADs.)
  46. const IOS::ES::TMDReader& tmd = GetTMD(partition);
  47. if (!tmd.IsValid())
  48. return;
  49. AddToSyncHash(context, tmd.GetIOSId());
  50. AddToSyncHash(context, tmd.GetTitleId());
  51. AddToSyncHash(context, tmd.GetTitleFlags());
  52. AddToSyncHash(context, tmd.GetGroupId());
  53. AddToSyncHash(context, tmd.GetRegion());
  54. AddToSyncHash(context, tmd.GetTitleVersion());
  55. AddToSyncHash(context, tmd.GetBootIndex());
  56. for (const IOS::ES::Content& content : tmd.GetContents())
  57. AddToSyncHash(context, content);
  58. }
  59. std::map<Language, std::string> Volume::ReadWiiNames(const std::vector<char16_t>& data)
  60. {
  61. std::map<Language, std::string> names;
  62. for (size_t i = 0; i < NUMBER_OF_LANGUAGES; ++i)
  63. {
  64. const size_t name_start = NAME_CHARS_LENGTH * i;
  65. if (name_start + NAME_CHARS_LENGTH <= data.size())
  66. {
  67. const std::string name = UTF16BEToUTF8(data.data() + name_start, NAME_CHARS_LENGTH);
  68. if (!name.empty())
  69. names[static_cast<Language>(i)] = name;
  70. }
  71. }
  72. return names;
  73. }
  74. static std::unique_ptr<VolumeDisc> TryCreateDisc(std::unique_ptr<BlobReader>& reader)
  75. {
  76. if (!reader)
  77. return nullptr;
  78. if (reader->ReadSwapped<u32>(0x18) == WII_DISC_MAGIC)
  79. return std::make_unique<VolumeWii>(std::move(reader));
  80. if (reader->ReadSwapped<u32>(0x1C) == GAMECUBE_DISC_MAGIC)
  81. return std::make_unique<VolumeGC>(std::move(reader));
  82. // No known magic words found
  83. return nullptr;
  84. }
  85. std::unique_ptr<VolumeDisc> CreateDisc(std::unique_ptr<BlobReader> reader)
  86. {
  87. return TryCreateDisc(reader);
  88. }
  89. std::unique_ptr<VolumeDisc> CreateDisc(const std::string& path)
  90. {
  91. return CreateDisc(CreateBlobReader(path));
  92. }
  93. static std::unique_ptr<VolumeWAD> TryCreateWAD(std::unique_ptr<BlobReader>& reader)
  94. {
  95. if (!reader)
  96. return nullptr;
  97. // Check for WAD
  98. // 0x206962 for boot2 wads
  99. const std::optional<u32> wad_magic = reader->ReadSwapped<u32>(0x02);
  100. if (wad_magic == u32(0x00204973) || wad_magic == u32(0x00206962))
  101. return std::make_unique<VolumeWAD>(std::move(reader));
  102. // No known magic words found
  103. return nullptr;
  104. }
  105. std::unique_ptr<VolumeWAD> CreateWAD(std::unique_ptr<BlobReader> reader)
  106. {
  107. return TryCreateWAD(reader);
  108. }
  109. std::unique_ptr<VolumeWAD> CreateWAD(const std::string& path)
  110. {
  111. return CreateWAD(CreateBlobReader(path));
  112. }
  113. std::unique_ptr<Volume> CreateVolume(std::unique_ptr<BlobReader> reader)
  114. {
  115. std::unique_ptr<VolumeDisc> disc = TryCreateDisc(reader);
  116. if (disc)
  117. return disc;
  118. std::unique_ptr<VolumeWAD> wad = TryCreateWAD(reader);
  119. if (wad)
  120. return wad;
  121. return nullptr;
  122. }
  123. std::unique_ptr<Volume> CreateVolume(const std::string& path)
  124. {
  125. return CreateVolume(CreateBlobReader(path));
  126. }
  127. } // namespace DiscIO