| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DiscIO/DiscExtractor.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <array> |
| 8 | #include <cstddef> |
| 9 | #include <cstring> |
| 10 | #include <functional> |
| 11 | #include <optional> |
| 12 | #include <string> |
| 13 | #include <string_view> |
| 14 | |
| 15 | #include "Common/CommonTypes.h" |
| 16 | #include "Common/FileUtil.h" |
| 17 | #include "Common/IOFile.h" |
| 18 | #include "DiscIO/DiscUtils.h" |
| 19 | #include "DiscIO/Enums.h" |
| 20 | #include "DiscIO/Filesystem.h" |
| 21 | #include "DiscIO/Volume.h" |
| 22 | |
| 23 | namespace DiscIO |
| 24 | { |
| 25 | u64 ReadFile(const Volume& volume, const Partition& partition, const FileInfo* file_info, |
| 26 | u8* buffer, u64 max_buffer_size, u64 offset_in_file) |
| 27 | { |
| 28 | if (!file_info || file_info->IsDirectory() || offset_in_file >= file_info->GetSize()) |
| 29 | return 0; |
| 30 | |
| 31 | const u64 read_length = std::min(max_buffer_size, file_info->GetSize() - offset_in_file); |
| 32 | |
| 33 | DEBUG_LOG_FMT(DISCIO, "Reading {:x} bytes at {:x} from file {}. Offset: {:x} Size: {:x}", |
| 34 | read_length, offset_in_file, file_info->GetPath(), file_info->GetOffset(), |
| 35 | file_info->GetSize()); |
| 36 | |
| 37 | if (!volume.Read(file_info->GetOffset() + offset_in_file, read_length, buffer, partition)) |
| 38 | return 0; |
| 39 | |
| 40 | return read_length; |
| 41 | } |
| 42 | |
| 43 | u64 ReadFile(const Volume& volume, const Partition& partition, std::string_view path, u8* buffer, |
| 44 | u64 max_buffer_size, u64 offset_in_file) |
| 45 | { |
| 46 | const FileSystem* file_system = volume.GetFileSystem(partition); |
| 47 | if (!file_system) |
| 48 | return 0; |
| 49 | |
| 50 | return ReadFile(volume, partition, file_system->FindFileInfo(path).get(), buffer, max_buffer_size, |
| 51 | offset_in_file); |
| 52 | } |
| 53 | |
| 54 | bool ExportData(const Volume& volume, const Partition& partition, u64 offset, u64 size, |
| 55 | const std::string& export_filename) |
| 56 | { |
| 57 | File::IOFile f(export_filename, "wb"); |
| 58 | if (!f) |
| 59 | return false; |
| 60 | |
| 61 | while (size) |
| 62 | { |
| 63 | // Limit read size to 128 MB |
| 64 | const size_t read_size = static_cast<size_t>(std::min<u64>(size, 0x08000000)); |
| 65 | |
| 66 | std::vector<u8> buffer(read_size); |
| 67 | |
| 68 | if (!volume.Read(offset, read_size, buffer.data(), partition)) |
| 69 | return false; |
| 70 | |
| 71 | if (!f.WriteBytes(buffer.data(), read_size)) |
| 72 | return false; |
| 73 | |
| 74 | size -= read_size; |
| 75 | offset += read_size; |
| 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | bool ExportFile(const Volume& volume, const Partition& partition, const FileInfo* file_info, |
| 82 | const std::string& export_filename) |
| 83 | { |
| 84 | if (!file_info || file_info->IsDirectory()) |
| 85 | return false; |
| 86 | |
| 87 | return ExportData(volume, partition, file_info->GetOffset(), file_info->GetSize(), |
| 88 | export_filename); |
| 89 | } |
| 90 | |
| 91 | bool ExportFile(const Volume& volume, const Partition& partition, std::string_view path, |
| 92 | const std::string& export_filename) |
| 93 | { |
| 94 | const FileSystem* file_system = volume.GetFileSystem(partition); |
| 95 | if (!file_system) |
| 96 | return false; |
| 97 | |
| 98 | return ExportFile(volume, partition, file_system->FindFileInfo(path).get(), export_filename); |
| 99 | } |
| 100 | |
| 101 | void ExportDirectory(const Volume& volume, const Partition& partition, const FileInfo& directory, |
| 102 | bool recursive, const std::string& filesystem_path, |
| 103 | const std::string& export_folder, |
| 104 | const std::function<bool(const std::string& path)>& update_progress) |
| 105 | { |
| 106 | std::string export_root = export_folder + '/'; |
| 107 | if (directory.IsDirectory() && !directory.IsRoot()) |
| 108 | export_root += directory.GetName() + '/'; |
| 109 | |
| 110 | File::CreateFullPath(export_root); |
| 111 | |
| 112 | for (const FileInfo& file_info : directory) |
| 113 | { |
| 114 | const std::string name = file_info.GetName() + (file_info.IsDirectory() ? "/" : ""); |
| 115 | const std::string path = filesystem_path + name; |
| 116 | const std::string export_path = export_root + name; |
| 117 | |
| 118 | if (update_progress(path)) |
| 119 | return; |
| 120 | |
| 121 | DEBUG_LOG_FMT(DISCIO, "{}", export_path); |
| 122 | |
| 123 | if (!file_info.IsDirectory()) |
| 124 | { |
| 125 | if (File::Exists(export_path)) |
| 126 | NOTICE_LOG_FMT(DISCIO, "{} already exists", export_path); |
| 127 | else if (!ExportFile(volume, partition, &file_info, export_path)) |
| 128 | ERROR_LOG_FMT(DISCIO, "Could not export {}", export_path); |
| 129 | } |
| 130 | else if (recursive) |
| 131 | { |
| 132 | ExportDirectory(volume, partition, file_info, recursive, filesystem_path, export_root, |
| 133 | update_progress); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | bool ExportWiiUnencryptedHeader(const Volume& volume, const std::string& export_filename) |
| 139 | { |
| 140 | if (volume.GetVolumeType() != Platform::WiiDisc) |
| 141 | return false; |
| 142 | |
| 143 | File::IOFile f(export_filename, "wb"); |
| 144 | if (!f) |
| 145 | return false; |
| 146 | |
| 147 | std::array<u8, WII_NONPARTITION_DISCHEADER_SIZE> buffer; |
| 148 | |
| 149 | if (!volume.Read(WII_NONPARTITION_DISCHEADER_ADDRESS, buffer.size(), buffer.data(), |
| 150 | PARTITION_NONE)) |
| 151 | { |
| 152 | return false; |
| 153 | } |
| 154 | // NKitv1 unconditionally sets and unsets some flags when converting between Wii ISO and Wii NKit. |
| 155 | // This is because the NKit format decrypts the disc partitions and removes the h3 hash table. |
| 156 | // https://wiibrew.org/wiki/Wii_disc#Header |
| 157 | if (volume.IsNKit()) |
| 158 | std::memset(buffer.data() + 0x60, 0, 2); |
| 159 | if (!f.WriteBytes(buffer.data(), buffer.size())) |
| 160 | return false; |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | bool ExportWiiRegionData(const Volume& volume, const std::string& export_filename) |
| 165 | { |
| 166 | if (volume.GetVolumeType() != Platform::WiiDisc) |
| 167 | return false; |
| 168 | |
| 169 | return ExportData(volume, PARTITION_NONE, WII_REGION_DATA_ADDRESS, WII_REGION_DATA_SIZE, |
| 170 | export_filename); |
| 171 | } |
| 172 | |
| 173 | bool ExportTicket(const Volume& volume, const Partition& partition, |
| 174 | const std::string& export_filename) |
| 175 | { |
| 176 | if (volume.GetVolumeType() != Platform::WiiDisc) |
| 177 | return false; |
| 178 | |
| 179 | return ExportData(volume, PARTITION_NONE, partition.offset + WII_PARTITION_TICKET_ADDRESS, |
| 180 | WII_PARTITION_TICKET_SIZE, export_filename); |
| 181 | } |
| 182 | |
| 183 | bool ExportTMD(const Volume& volume, const Partition& partition, const std::string& export_filename) |
| 184 | { |
| 185 | if (volume.GetVolumeType() != Platform::WiiDisc) |
| 186 | return false; |
| 187 | |
| 188 | const std::optional<u32> size = |
| 189 | volume.ReadSwapped<u32>(partition.offset + WII_PARTITION_TMD_SIZE_ADDRESS, PARTITION_NONE); |
| 190 | const std::optional<u64> offset = volume.ReadSwappedAndShifted( |
| 191 | partition.offset + WII_PARTITION_TMD_OFFSET_ADDRESS, PARTITION_NONE); |
| 192 | if (!size || !offset) |
| 193 | return false; |
| 194 | |
| 195 | return ExportData(volume, PARTITION_NONE, partition.offset + *offset, *size, export_filename); |
| 196 | } |
| 197 | |
| 198 | bool ExportCertificateChain(const Volume& volume, const Partition& partition, |
| 199 | const std::string& export_filename) |
| 200 | { |
| 201 | if (volume.GetVolumeType() != Platform::WiiDisc) |
| 202 | return false; |
| 203 | |
| 204 | const std::optional<u32> size = volume.ReadSwapped<u32>( |
| 205 | partition.offset + WII_PARTITION_CERT_CHAIN_SIZE_ADDRESS, PARTITION_NONE); |
| 206 | const std::optional<u64> offset = volume.ReadSwappedAndShifted( |
| 207 | partition.offset + WII_PARTITION_CERT_CHAIN_OFFSET_ADDRESS, PARTITION_NONE); |
| 208 | if (!size || !offset) |
| 209 | return false; |
| 210 | |
| 211 | return ExportData(volume, PARTITION_NONE, partition.offset + *offset, *size, export_filename); |
| 212 | } |
| 213 | |
| 214 | bool ExportH3Hashes(const Volume& volume, const Partition& partition, |
| 215 | const std::string& export_filename) |
| 216 | { |
| 217 | if (volume.GetVolumeType() != Platform::WiiDisc) |
| 218 | return false; |
| 219 | |
| 220 | const std::optional<u64> offset = volume.ReadSwappedAndShifted( |
| 221 | partition.offset + WII_PARTITION_H3_OFFSET_ADDRESS, PARTITION_NONE); |
| 222 | if (!offset) |
| 223 | return false; |
| 224 | |
| 225 | return ExportData(volume, PARTITION_NONE, partition.offset + *offset, WII_PARTITION_H3_SIZE, |
| 226 | export_filename); |
| 227 | } |
| 228 | |
| 229 | bool ExportHeader(const Volume& volume, const Partition& partition, |
| 230 | const std::string& export_filename) |
| 231 | { |
| 232 | if (!IsDisc(volume.GetVolumeType())) |
| 233 | return false; |
| 234 | |
| 235 | File::IOFile f(export_filename, "wb"); |
| 236 | if (!f) |
| 237 | return false; |
| 238 | |
| 239 | std::array<u8, DISCHEADER_SIZE> buffer; |
| 240 | |
| 241 | if (!volume.Read(DISCHEADER_ADDRESS, buffer.size(), buffer.data(), partition)) |
| 242 | return false; |
| 243 | // Erase NKitv1 data |
| 244 | if (volume.IsNKit()) |
| 245 | std::memset(buffer.data() + 0x200, 0, 0x1C); |
| 246 | if (!f.WriteBytes(buffer.data(), buffer.size())) |
| 247 | return false; |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | bool ExportBI2Data(const Volume& volume, const Partition& partition, |
| 252 | const std::string& export_filename) |
| 253 | { |
| 254 | if (!IsDisc(volume.GetVolumeType())) |
| 255 | return false; |
| 256 | |
| 257 | return ExportData(volume, partition, BI2_ADDRESS, BI2_SIZE, export_filename); |
| 258 | } |
| 259 | |
| 260 | bool ExportApploader(const Volume& volume, const Partition& partition, |
| 261 | const std::string& export_filename) |
| 262 | { |
| 263 | if (!IsDisc(volume.GetVolumeType())) |
| 264 | return false; |
| 265 | |
| 266 | const std::optional<u64> apploader_size = GetApploaderSize(volume, partition); |
| 267 | if (!apploader_size) |
| 268 | return false; |
| 269 | |
| 270 | return ExportData(volume, partition, APPLOADER_ADDRESS, *apploader_size, export_filename); |
| 271 | } |
| 272 | |
| 273 | bool ExportDOL(const Volume& volume, const Partition& partition, const std::string& export_filename) |
| 274 | { |
| 275 | if (!IsDisc(volume.GetVolumeType())) |
| 276 | return false; |
| 277 | |
| 278 | const std::optional<u64> dol_offset = GetBootDOLOffset(volume, partition); |
| 279 | if (!dol_offset) |
| 280 | return false; |
| 281 | const std::optional<u32> dol_size = GetBootDOLSize(volume, partition, *dol_offset); |
| 282 | if (!dol_size) |
| 283 | return false; |
| 284 | |
| 285 | return ExportData(volume, partition, *dol_offset, *dol_size, export_filename); |
| 286 | } |
| 287 | |
| 288 | bool ExportFST(const Volume& volume, const Partition& partition, const std::string& export_filename) |
| 289 | { |
| 290 | if (!IsDisc(volume.GetVolumeType())) |
| 291 | return false; |
| 292 | |
| 293 | const std::optional<u64> fst_offset = GetFSTOffset(volume, partition); |
| 294 | const std::optional<u64> fst_size = GetFSTSize(volume, partition); |
| 295 | if (!fst_offset || !fst_size) |
| 296 | return false; |
| 297 | |
| 298 | return ExportData(volume, partition, *fst_offset, *fst_size, export_filename); |
| 299 | } |
| 300 | |
| 301 | bool ExportSystemData(const Volume& volume, const Partition& partition, |
| 302 | const std::string& export_folder) |
| 303 | { |
| 304 | bool success = true; |
| 305 | |
| 306 | File::CreateFullPath(export_folder + "/sys/"); |
| 307 | success &= ExportHeader(volume, partition, export_folder + "/sys/boot.bin"); |
| 308 | success &= ExportBI2Data(volume, partition, export_folder + "/sys/bi2.bin"); |
| 309 | success &= ExportApploader(volume, partition, export_folder + "/sys/apploader.img"); |
| 310 | success &= ExportDOL(volume, partition, export_folder + "/sys/main.dol"); |
| 311 | success &= ExportFST(volume, partition, export_folder + "/sys/fst.bin"); |
| 312 | |
| 313 | if (volume.GetVolumeType() == Platform::WiiDisc) |
| 314 | { |
| 315 | File::CreateFullPath(export_folder + "/disc/"); |
| 316 | success &= ExportWiiUnencryptedHeader(volume, export_folder + "/disc/header.bin"); |
| 317 | success &= ExportWiiRegionData(volume, export_folder + "/disc/region.bin"); |
| 318 | |
| 319 | success &= ExportTicket(volume, partition, export_folder + "/ticket.bin"); |
| 320 | success &= ExportTMD(volume, partition, export_folder + "/tmd.bin"); |
| 321 | success &= ExportCertificateChain(volume, partition, export_folder + "/cert.bin"); |
| 322 | if (volume.HasWiiHashes()) |
| 323 | success &= ExportH3Hashes(volume, partition, export_folder + "/h3.bin"); |
| 324 | } |
| 325 | |
| 326 | return success; |
| 327 | } |
| 328 | |
| 329 | } // namespace DiscIO |