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