Debian Codegraph @

Source/Core/DiscIO/VolumeWad.cpp

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