DiscUtils.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright 2021 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "DiscIO/DiscUtils.h"
  4. #include <algorithm>
  5. #include <locale>
  6. #include <optional>
  7. #include <string>
  8. #include <vector>
  9. #include <fmt/format.h>
  10. #include "Common/CommonTypes.h"
  11. #include "Common/MathUtil.h"
  12. #include "DiscIO/Blob.h"
  13. #include "DiscIO/Filesystem.h"
  14. #include "DiscIO/Volume.h"
  15. namespace DiscIO
  16. {
  17. std::string NameForPartitionType(u32 partition_type, bool include_prefix)
  18. {
  19. switch (partition_type)
  20. {
  21. case PARTITION_DATA:
  22. return "DATA";
  23. case PARTITION_UPDATE:
  24. return "UPDATE";
  25. case PARTITION_CHANNEL:
  26. return "CHANNEL";
  27. case PARTITION_INSTALL:
  28. // wit doesn't recognize the name "INSTALL", so we can't use it when naming partition folders
  29. if (!include_prefix)
  30. return "INSTALL";
  31. [[fallthrough]];
  32. default:
  33. const std::string type_as_game_id{static_cast<char>((partition_type >> 24) & 0xFF),
  34. static_cast<char>((partition_type >> 16) & 0xFF),
  35. static_cast<char>((partition_type >> 8) & 0xFF),
  36. static_cast<char>(partition_type & 0xFF)};
  37. if (std::all_of(type_as_game_id.cbegin(), type_as_game_id.cend(),
  38. [](char c) { return std::isalnum(c, std::locale::classic()); }))
  39. {
  40. return include_prefix ? "P-" + type_as_game_id : type_as_game_id;
  41. }
  42. return fmt::format("{}{}", include_prefix ? "P" : "", partition_type);
  43. }
  44. }
  45. std::optional<u64> GetApploaderSize(const Volume& volume, const Partition& partition)
  46. {
  47. constexpr u64 header_size = 0x20;
  48. const std::optional<u32> apploader_size = volume.ReadSwapped<u32>(0x2440 + 0x14, partition);
  49. const std::optional<u32> trailer_size = volume.ReadSwapped<u32>(0x2440 + 0x18, partition);
  50. if (!apploader_size || !trailer_size)
  51. return std::nullopt;
  52. return header_size + *apploader_size + *trailer_size;
  53. }
  54. std::optional<u64> GetBootDOLOffset(const Volume& volume, const Partition& partition)
  55. {
  56. const Platform volume_type = volume.GetVolumeType();
  57. if (!IsDisc(volume_type))
  58. return std::nullopt;
  59. std::optional<u64> dol_offset = volume.ReadSwappedAndShifted(0x420, partition);
  60. // Datel AR disc has 0x00000000 as the offset (invalid) and doesn't use it in the AppLoader.
  61. if (dol_offset && *dol_offset == 0)
  62. dol_offset.reset();
  63. return dol_offset;
  64. }
  65. std::optional<u32> GetBootDOLSize(const Volume& volume, const Partition& partition, u64 dol_offset)
  66. {
  67. if (!IsDisc(volume.GetVolumeType()))
  68. return std::nullopt;
  69. u32 dol_size = 0;
  70. // Iterate through the 7 code segments
  71. for (size_t i = 0; i < 7; i++)
  72. {
  73. const std::optional<u32> offset = volume.ReadSwapped<u32>(dol_offset + 0x00 + i * 4, partition);
  74. const std::optional<u32> size = volume.ReadSwapped<u32>(dol_offset + 0x90 + i * 4, partition);
  75. if (!offset || !size)
  76. return {};
  77. dol_size = std::max(*offset + *size, dol_size);
  78. }
  79. // Iterate through the 11 data segments
  80. for (size_t i = 0; i < 11; i++)
  81. {
  82. const std::optional<u32> offset = volume.ReadSwapped<u32>(dol_offset + 0x1c + i * 4, partition);
  83. const std::optional<u32> size = volume.ReadSwapped<u32>(dol_offset + 0xac + i * 4, partition);
  84. if (!offset || !size)
  85. return {};
  86. dol_size = std::max(*offset + *size, dol_size);
  87. }
  88. return dol_size;
  89. }
  90. std::optional<u64> GetFSTOffset(const Volume& volume, const Partition& partition)
  91. {
  92. const Platform volume_type = volume.GetVolumeType();
  93. if (!IsDisc(volume_type))
  94. return std::nullopt;
  95. return volume.ReadSwappedAndShifted(0x424, partition);
  96. }
  97. std::optional<u64> GetFSTSize(const Volume& volume, const Partition& partition)
  98. {
  99. const Platform volume_type = volume.GetVolumeType();
  100. if (!IsDisc(volume_type))
  101. return std::nullopt;
  102. return volume.ReadSwappedAndShifted(0x428, partition);
  103. }
  104. u64 GetBiggestReferencedOffset(const Volume& volume)
  105. {
  106. std::vector<Partition> partitions = volume.GetPartitions();
  107. // If a partition doesn't seem to contain any valid data, skip it.
  108. // This can happen when certain programs that create WBFS files scrub the entirety of
  109. // the Masterpiece partitions in Super Smash Bros. Brawl without removing them from
  110. // the partition table. https://bugs.dolphin-emu.org/issues/8733
  111. std::erase_if(partitions, [&](const Partition& partition) {
  112. return volume.ReadSwapped<u32>(0x18, partition) != WII_DISC_MAGIC;
  113. });
  114. if (partitions.empty())
  115. partitions.push_back(PARTITION_NONE);
  116. return GetBiggestReferencedOffset(volume, partitions);
  117. }
  118. static u64 GetBiggestReferencedOffset(const Volume& volume, const FileInfo& file_info)
  119. {
  120. if (file_info.IsDirectory())
  121. {
  122. u64 biggest_offset = 0;
  123. for (const FileInfo& f : file_info)
  124. biggest_offset = std::max(biggest_offset, GetBiggestReferencedOffset(volume, f));
  125. return biggest_offset;
  126. }
  127. else
  128. {
  129. return file_info.GetOffset() + file_info.GetSize();
  130. }
  131. }
  132. u64 GetBiggestReferencedOffset(const Volume& volume, const std::vector<Partition>& partitions)
  133. {
  134. const u64 disc_header_size = volume.GetVolumeType() == Platform::GameCubeDisc ? 0x460 : 0x50000;
  135. u64 biggest_offset = disc_header_size;
  136. for (const Partition& partition : partitions)
  137. {
  138. if (partition != PARTITION_NONE)
  139. {
  140. const u64 offset = volume.PartitionOffsetToRawOffset(0x440, partition);
  141. biggest_offset = std::max(biggest_offset, offset);
  142. }
  143. const std::optional<u64> dol_offset = GetBootDOLOffset(volume, partition);
  144. if (dol_offset)
  145. {
  146. const std::optional<u64> dol_size = GetBootDOLSize(volume, partition, *dol_offset);
  147. if (dol_size)
  148. {
  149. const u64 offset = volume.PartitionOffsetToRawOffset(*dol_offset + *dol_size, partition);
  150. biggest_offset = std::max(biggest_offset, offset);
  151. }
  152. }
  153. const std::optional<u64> fst_offset = GetFSTOffset(volume, partition);
  154. const std::optional<u64> fst_size = GetFSTSize(volume, partition);
  155. if (fst_offset && fst_size)
  156. {
  157. const u64 offset = volume.PartitionOffsetToRawOffset(*fst_offset + *fst_size, partition);
  158. biggest_offset = std::max(biggest_offset, offset);
  159. }
  160. const FileSystem* fs = volume.GetFileSystem(partition);
  161. if (fs)
  162. {
  163. const u64 offset_in_partition = GetBiggestReferencedOffset(volume, fs->GetRoot());
  164. const u64 offset = volume.PartitionOffsetToRawOffset(offset_in_partition, partition);
  165. biggest_offset = std::max(biggest_offset, offset);
  166. }
  167. }
  168. return biggest_offset;
  169. }
  170. bool IsGCZBlockSizeLegacyCompatible(int block_size, u64 file_size)
  171. {
  172. // In order for versions of Dolphin prior to 5.0-11893 to be able to convert a GCZ file
  173. // to ISO without messing up the final part of the file in some way, the file size
  174. // must be an integer multiple of the block size (fixed in 3aa463c) and must not be
  175. // an integer multiple of the block size multiplied by 32 (fixed in 26b21e3).
  176. return file_size % block_size == 0 && file_size % (block_size * 32) != 0;
  177. }
  178. bool IsDiscImageBlockSizeValid(int block_size, DiscIO::BlobType format)
  179. {
  180. switch (format)
  181. {
  182. case DiscIO::BlobType::GCZ:
  183. // Block size "must" be a power of 2
  184. if (!MathUtil::IsPow2(block_size))
  185. return false;
  186. break;
  187. case DiscIO::BlobType::WIA:
  188. // Block size must not be less than the minimum, and must be a multiple of it
  189. if (block_size < WIA_MIN_BLOCK_SIZE || block_size % WIA_MIN_BLOCK_SIZE != 0)
  190. return false;
  191. break;
  192. case DiscIO::BlobType::RVZ:
  193. // Block size must not be smaller than the minimum
  194. // Block sizes smaller than the large block size threshold must be a power of 2
  195. // Block sizes larger than that threshold must be a multiple of the threshold
  196. if (block_size < RVZ_MIN_BLOCK_SIZE ||
  197. (block_size < RVZ_BIG_BLOCK_SIZE_LCM && !MathUtil::IsPow2(block_size)) ||
  198. (block_size > RVZ_BIG_BLOCK_SIZE_LCM && block_size % RVZ_BIG_BLOCK_SIZE_LCM != 0))
  199. {
  200. return false;
  201. }
  202. break;
  203. default:
  204. ASSERT(false);
  205. break;
  206. }
  207. return true;
  208. }
  209. } // namespace DiscIO