DiscExtractor.cpp 11 KB

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