VolumeWad.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // Copyright 2009 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "DiscIO/VolumeWad.h"
  4. #include <algorithm>
  5. #include <cstddef>
  6. #include <cstring>
  7. #include <map>
  8. #include <memory>
  9. #include <optional>
  10. #include <string>
  11. #include <utility>
  12. #include <vector>
  13. #include "Common/Align.h"
  14. #include "Common/Assert.h"
  15. #include "Common/CommonTypes.h"
  16. #include "Common/Crypto/AES.h"
  17. #include "Common/Crypto/SHA1.h"
  18. #include "Common/Logging/Log.h"
  19. #include "Common/MsgHandler.h"
  20. #include "Common/StringUtil.h"
  21. #include "Core/IOS/IOSC.h"
  22. #include "DiscIO/Blob.h"
  23. #include "DiscIO/Enums.h"
  24. #include "DiscIO/Volume.h"
  25. #include "DiscIO/WiiSaveBanner.h"
  26. namespace DiscIO
  27. {
  28. VolumeWAD::VolumeWAD(std::unique_ptr<BlobReader> reader) : m_reader(std::move(reader))
  29. {
  30. ASSERT(m_reader);
  31. // Source: http://wiibrew.org/wiki/WAD_files
  32. m_hdr_size = m_reader->ReadSwapped<u32>(0x00).value_or(0);
  33. m_cert_chain_size = m_reader->ReadSwapped<u32>(0x08).value_or(0);
  34. m_ticket_size = m_reader->ReadSwapped<u32>(0x10).value_or(0);
  35. m_tmd_size = m_reader->ReadSwapped<u32>(0x14).value_or(0);
  36. m_data_size = m_reader->ReadSwapped<u32>(0x18).value_or(0);
  37. m_opening_bnr_size = m_reader->ReadSwapped<u32>(0x1C).value_or(0);
  38. m_cert_chain_offset = Common::AlignUp(m_hdr_size, 0x40);
  39. m_ticket_offset = m_cert_chain_offset + Common::AlignUp(m_cert_chain_size, 0x40);
  40. m_tmd_offset = m_ticket_offset + Common::AlignUp(m_ticket_size, 0x40);
  41. m_data_offset = m_tmd_offset + Common::AlignUp(m_tmd_size, 0x40);
  42. m_opening_bnr_offset = m_data_offset + Common::AlignUp(m_data_size, 0x40);
  43. std::vector<u8> ticket_buffer(m_ticket_size);
  44. Read(m_ticket_offset, m_ticket_size, ticket_buffer.data());
  45. m_ticket.SetBytes(std::move(ticket_buffer));
  46. if (!IOS::ES::IsValidTMDSize(m_tmd_size))
  47. {
  48. ERROR_LOG_FMT(DISCIO, "TMD is too large: {} bytes", m_tmd_size);
  49. return;
  50. }
  51. std::vector<u8> tmd_buffer(m_tmd_size);
  52. Read(m_tmd_offset, m_tmd_size, tmd_buffer.data());
  53. m_tmd.SetBytes(std::move(tmd_buffer));
  54. m_cert_chain.resize(m_cert_chain_size);
  55. Read(m_cert_chain_offset, m_cert_chain_size, m_cert_chain.data());
  56. }
  57. bool VolumeWAD::Read(u64 offset, u64 length, u8* buffer, const Partition& partition) const
  58. {
  59. if (partition != PARTITION_NONE)
  60. return false;
  61. return m_reader->Read(offset, length, buffer);
  62. }
  63. const FileSystem* VolumeWAD::GetFileSystem(const Partition& partition) const
  64. {
  65. // TODO: Implement this?
  66. return nullptr;
  67. }
  68. Region VolumeWAD::GetRegion() const
  69. {
  70. if (!m_tmd.IsValid())
  71. return Region::Unknown;
  72. return m_tmd.GetRegion();
  73. }
  74. Country VolumeWAD::GetCountry(const Partition& partition) const
  75. {
  76. if (!m_tmd.IsValid())
  77. return Country::Unknown;
  78. const u8 country_byte = static_cast<u8>(m_tmd.GetTitleId() & 0xff);
  79. if (country_byte == 2) // SYSMENU
  80. return TypicalCountryForRegion(GetSysMenuRegion(m_tmd.GetTitleVersion()));
  81. const Region region = GetRegion();
  82. const std::optional<u16> revision = GetRevision();
  83. if (CountryCodeToRegion(country_byte, Platform::WiiWAD, region, revision) != region)
  84. return TypicalCountryForRegion(region);
  85. return CountryCodeToCountry(country_byte, Platform::WiiWAD, region, revision);
  86. }
  87. const IOS::ES::TicketReader& VolumeWAD::GetTicket(const Partition& partition) const
  88. {
  89. return m_ticket;
  90. }
  91. const IOS::ES::TMDReader& VolumeWAD::GetTMD(const Partition& partition) const
  92. {
  93. return m_tmd;
  94. }
  95. const std::vector<u8>& VolumeWAD::GetCertificateChain(const Partition& partition) const
  96. {
  97. return m_cert_chain;
  98. }
  99. std::vector<u8> VolumeWAD::GetContent(u16 index) const
  100. {
  101. u64 offset = m_data_offset;
  102. for (const IOS::ES::Content& content : m_tmd.GetContents())
  103. {
  104. const u64 aligned_size = Common::AlignUp(content.size, 0x40);
  105. if (content.index == index)
  106. {
  107. std::vector<u8> data(aligned_size);
  108. if (!m_reader->Read(offset, aligned_size, data.data()))
  109. return {};
  110. return data;
  111. }
  112. offset += aligned_size;
  113. }
  114. return {};
  115. }
  116. std::vector<u64> VolumeWAD::GetContentOffsets() const
  117. {
  118. const std::vector<IOS::ES::Content> contents = m_tmd.GetContents();
  119. std::vector<u64> content_offsets;
  120. content_offsets.reserve(contents.size());
  121. u64 offset = m_data_offset;
  122. for (const IOS::ES::Content& content : contents)
  123. {
  124. content_offsets.emplace_back(offset);
  125. offset += Common::AlignUp(content.size, 0x40);
  126. }
  127. return content_offsets;
  128. }
  129. bool VolumeWAD::CheckContentIntegrity(const IOS::ES::Content& content,
  130. const std::vector<u8>& encrypted_data,
  131. const IOS::ES::TicketReader& ticket) const
  132. {
  133. if (encrypted_data.size() != Common::AlignUp(content.size, 0x40))
  134. return false;
  135. auto context = Common::AES::CreateContextDecrypt(ticket.GetTitleKey().data());
  136. std::array<u8, 16> iv{};
  137. iv[0] = static_cast<u8>(content.index >> 8);
  138. iv[1] = static_cast<u8>(content.index & 0xFF);
  139. std::vector<u8> decrypted_data(encrypted_data.size());
  140. context->Crypt(iv.data(), encrypted_data.data(), decrypted_data.data(), decrypted_data.size());
  141. return Common::SHA1::CalculateDigest(decrypted_data.data(), content.size) == content.sha1;
  142. }
  143. IOS::ES::TicketReader VolumeWAD::GetTicketWithFixedCommonKey() const
  144. {
  145. if (!m_ticket.IsValid() || !m_tmd.IsValid())
  146. return m_ticket;
  147. const std::vector<u8> sig = m_ticket.GetSignatureData();
  148. if (!std::all_of(sig.cbegin(), sig.cend(), [](u8 a) { return a == 0; }))
  149. {
  150. // This does not look like a typical "invalid common key index" ticket, so let's assume
  151. // the index is correct. This saves some time when reading properly signed titles.
  152. return m_ticket;
  153. }
  154. const std::vector<IOS::ES::Content> contents = m_tmd.GetContents();
  155. if (contents.empty())
  156. return m_ticket;
  157. // Find the smallest content so that we spend as little time as possible in CheckContentIntegrity
  158. IOS::ES::Content smallest_content = contents[0];
  159. u64 offset_of_smallest_content = m_data_offset;
  160. u64 offset = m_data_offset;
  161. for (const IOS::ES::Content& content : contents)
  162. {
  163. if (content.size < smallest_content.size)
  164. {
  165. smallest_content = content;
  166. offset_of_smallest_content = offset;
  167. }
  168. offset += Common::AlignUp(content.size, 0x40);
  169. }
  170. std::vector<u8> content_data(Common::AlignUp(smallest_content.size, 0x40));
  171. if (!m_reader->Read(offset_of_smallest_content, content_data.size(), content_data.data()))
  172. return m_ticket;
  173. const u8 specified_index = m_ticket.GetCommonKeyIndex();
  174. if (specified_index < IOS::HLE::IOSC::COMMON_KEY_HANDLES.size() &&
  175. CheckContentIntegrity(smallest_content, content_data, m_ticket))
  176. {
  177. return m_ticket; // The common key index is already correct
  178. }
  179. // Try every common key index except the one we already tried
  180. IOS::ES::TicketReader new_ticket = m_ticket;
  181. for (u8 i = 0; i < IOS::HLE::IOSC::COMMON_KEY_HANDLES.size(); ++i)
  182. {
  183. if (i != specified_index)
  184. {
  185. new_ticket.OverwriteCommonKeyIndex(i);
  186. if (CheckContentIntegrity(smallest_content, content_data, new_ticket))
  187. return new_ticket; // We've found the common key index that should be used
  188. }
  189. }
  190. ERROR_LOG_FMT(DISCIO, "Couldn't find valid common key for WAD file ({} specified)",
  191. specified_index);
  192. return m_ticket;
  193. }
  194. std::string VolumeWAD::GetGameID(const Partition& partition) const
  195. {
  196. return m_tmd.GetGameID();
  197. }
  198. std::string VolumeWAD::GetGameTDBID(const Partition& partition) const
  199. {
  200. return m_tmd.GetGameTDBID();
  201. }
  202. std::string VolumeWAD::GetMakerID(const Partition& partition) const
  203. {
  204. char temp[2];
  205. if (!Read(0x198 + m_tmd_offset, 2, (u8*)temp, partition))
  206. return "00";
  207. // Some weird channels use 0x0000 in place of the MakerID, so we need a check here
  208. if (!Common::IsPrintableCharacter(temp[0]) || !Common::IsPrintableCharacter(temp[1]))
  209. return "00";
  210. return DecodeString(temp);
  211. }
  212. std::optional<u64> VolumeWAD::GetTitleID(const Partition& partition) const
  213. {
  214. return ReadSwapped<u64>(m_ticket_offset + 0x01DC, partition);
  215. }
  216. std::optional<u16> VolumeWAD::GetRevision(const Partition& partition) const
  217. {
  218. if (!m_tmd.IsValid())
  219. return {};
  220. return m_tmd.GetTitleVersion();
  221. }
  222. Platform VolumeWAD::GetVolumeType() const
  223. {
  224. return Platform::WiiWAD;
  225. }
  226. bool VolumeWAD::IsDatelDisc() const
  227. {
  228. return false;
  229. }
  230. bool VolumeWAD::IsNKit() const
  231. {
  232. return false;
  233. }
  234. std::map<Language, std::string> VolumeWAD::GetLongNames() const
  235. {
  236. if (!m_tmd.IsValid() || !IOS::ES::IsChannel(m_tmd.GetTitleId()))
  237. return {};
  238. std::vector<char16_t> names(NAMES_TOTAL_CHARS);
  239. if (!Read(m_opening_bnr_offset + 0x9C, NAMES_TOTAL_BYTES, reinterpret_cast<u8*>(names.data())))
  240. return std::map<Language, std::string>();
  241. return ReadWiiNames(names);
  242. }
  243. std::vector<u32> VolumeWAD::GetBanner(u32* width, u32* height) const
  244. {
  245. *width = 0;
  246. *height = 0;
  247. const std::optional<u64> title_id = GetTitleID();
  248. if (!title_id)
  249. return std::vector<u32>();
  250. return WiiSaveBanner(*title_id).GetBanner(width, height);
  251. }
  252. BlobType VolumeWAD::GetBlobType() const
  253. {
  254. return m_reader->GetBlobType();
  255. }
  256. u64 VolumeWAD::GetDataSize() const
  257. {
  258. return m_reader->GetDataSize();
  259. }
  260. DataSizeType VolumeWAD::GetDataSizeType() const
  261. {
  262. return m_reader->GetDataSizeType();
  263. }
  264. u64 VolumeWAD::GetRawSize() const
  265. {
  266. return m_reader->GetRawSize();
  267. }
  268. const BlobReader& VolumeWAD::GetBlobReader() const
  269. {
  270. return *m_reader;
  271. }
  272. std::array<u8, 20> VolumeWAD::GetSyncHash() const
  273. {
  274. // We can skip hashing the contents since the TMD contains hashes of the contents.
  275. // We specifically don't hash the ticket, since its console ID can differ without any problems.
  276. auto context = Common::SHA1::CreateContext();
  277. AddTMDToSyncHash(context.get(), PARTITION_NONE);
  278. ReadAndAddToSyncHash(context.get(), m_opening_bnr_offset, m_opening_bnr_size, PARTITION_NONE);
  279. return context->Finish();
  280. }
  281. } // namespace DiscIO