VolumeVerifier.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2019 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <future>
  5. #include <map>
  6. #include <memory>
  7. #include <optional>
  8. #include <string>
  9. #include <vector>
  10. #include <mbedtls/md5.h>
  11. #include "Common/CommonTypes.h"
  12. #include "Common/Crypto/SHA1.h"
  13. #include "Core/IOS/ES/Formats.h"
  14. #include "DiscIO/DiscScrubber.h"
  15. #include "DiscIO/Volume.h"
  16. // To be used as follows:
  17. //
  18. // VolumeVerifier verifier(volume, redump_verification, hashes_to_calculate);
  19. // verifier.Start();
  20. // while (verifier.GetBytesProcessed() != verifier.GetTotalBytes())
  21. // verifier.Process();
  22. // verifier.Finish();
  23. // auto result = verifier.GetResult();
  24. //
  25. // Start, Process and Finish may take some time to run.
  26. //
  27. // GetResult() can be called before the processing is finished, but the result will be incomplete.
  28. namespace DiscIO
  29. {
  30. template <typename T>
  31. struct Hashes
  32. {
  33. T crc32;
  34. T md5;
  35. T sha1;
  36. };
  37. class RedumpVerifier final
  38. {
  39. public:
  40. enum class Status
  41. {
  42. Unknown,
  43. GoodDump,
  44. BadDump,
  45. Error,
  46. };
  47. struct Result
  48. {
  49. Status status = Status::Unknown;
  50. std::string message;
  51. };
  52. void Start(const Volume& volume);
  53. Result Finish(const Hashes<std::vector<u8>>& hashes);
  54. private:
  55. enum class DownloadStatus
  56. {
  57. NotAttempted,
  58. Success,
  59. Fail,
  60. FailButOldCacheAvailable,
  61. SystemNotAvailable,
  62. };
  63. struct DownloadState
  64. {
  65. std::mutex mutex;
  66. DownloadStatus status = DownloadStatus::NotAttempted;
  67. };
  68. struct PotentialMatch
  69. {
  70. u64 size = 0;
  71. Hashes<std::vector<u8>> hashes;
  72. };
  73. static DownloadStatus DownloadDatfile(const std::string& system, DownloadStatus old_status);
  74. static std::vector<u8> ReadDatfile(const std::string& system);
  75. std::vector<PotentialMatch> ScanDatfile(const std::vector<u8>& data, const std::string& system);
  76. std::string m_game_id;
  77. u16 m_revision = 0;
  78. u8 m_disc_number = 0;
  79. u64 m_size = 0;
  80. std::future<std::vector<PotentialMatch>> m_future;
  81. Result m_result;
  82. static DownloadState m_gc_download_state;
  83. static DownloadState m_wii_download_state;
  84. };
  85. class VolumeVerifier final
  86. {
  87. public:
  88. enum class Severity
  89. {
  90. None, // Only used internally
  91. Low,
  92. Medium,
  93. High,
  94. };
  95. struct Problem
  96. {
  97. Severity severity;
  98. std::string text;
  99. };
  100. struct Result
  101. {
  102. Hashes<std::vector<u8>> hashes;
  103. std::string summary_text;
  104. std::vector<Problem> problems;
  105. RedumpVerifier::Result redump;
  106. };
  107. VolumeVerifier(const Volume& volume, bool redump_verification, Hashes<bool> hashes_to_calculate);
  108. ~VolumeVerifier();
  109. static Hashes<bool> GetDefaultHashesToCalculate();
  110. void Start();
  111. void Process();
  112. u64 GetBytesProcessed() const;
  113. u64 GetTotalBytes() const;
  114. void Finish();
  115. const Result& GetResult() const;
  116. private:
  117. struct GroupToVerify
  118. {
  119. Partition partition;
  120. u64 offset;
  121. size_t block_index_start;
  122. size_t block_index_end;
  123. };
  124. std::vector<Partition> CheckPartitions();
  125. bool CheckPartition(const Partition& partition); // Returns false if partition should be ignored
  126. std::string GetPartitionName(std::optional<u32> type) const;
  127. bool IsDebugSigned() const;
  128. bool ShouldHaveChannelPartition() const;
  129. bool ShouldHaveInstallPartition() const;
  130. bool ShouldHaveMasterpiecePartitions() const;
  131. bool ShouldBeDualLayer() const;
  132. void CheckVolumeSize();
  133. void CheckMisc();
  134. void CheckSuperPaperMario();
  135. void SetUpHashing();
  136. void WaitForAsyncOperations() const;
  137. bool ReadChunkAndWaitForAsyncOperations(u64 bytes_to_read);
  138. void AddProblem(Severity severity, std::string text);
  139. const Volume& m_volume;
  140. Result m_result;
  141. bool m_is_tgc = false;
  142. bool m_is_datel = false;
  143. bool m_is_not_retail = false;
  144. bool m_redump_verification;
  145. RedumpVerifier m_redump_verifier;
  146. bool m_read_errors_occurred = false;
  147. Hashes<bool> m_hashes_to_calculate{};
  148. bool m_calculating_any_hash = false;
  149. u32 m_crc32_context = 0;
  150. mbedtls_md5_context m_md5_context{};
  151. std::unique_ptr<Common::SHA1::Context> m_sha1_context;
  152. u64 m_excess_bytes = 0;
  153. std::vector<u8> m_data;
  154. std::future<void> m_crc32_future;
  155. std::future<void> m_md5_future;
  156. std::future<void> m_sha1_future;
  157. std::future<void> m_content_future;
  158. std::future<void> m_group_future;
  159. DiscScrubber m_scrubber;
  160. IOS::ES::TicketReader m_ticket;
  161. std::vector<u64> m_content_offsets;
  162. u16 m_content_index = 0;
  163. std::vector<GroupToVerify> m_groups;
  164. size_t m_group_index = 0; // Index in m_groups, not index in a specific partition
  165. std::map<Partition, size_t> m_block_errors;
  166. std::map<Partition, size_t> m_unused_block_errors;
  167. u64 m_biggest_referenced_offset = 0;
  168. u64 m_biggest_verified_offset = 0;
  169. bool m_started = false;
  170. bool m_done = false;
  171. u64 m_progress = 0;
  172. u64 m_max_progress = 0;
  173. DataSizeType m_data_size_type;
  174. };
  175. } // namespace DiscIO