/
NetKDRequest.cpp
545 lines (463 loc) · 17.2 KB
/
NetKDRequest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// Copyright 2017 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "Core/IOS/Network/KD/NetKDRequest.h"
#include <algorithm>
#include <array>
#include <string>
#include <string_view>
#include <utility>
#include "Common/BitUtils.h"
#include "Common/CommonPaths.h"
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/Logging/Log.h"
#include "Common/NandPaths.h"
#include "Common/SettingsHandler.h"
#include "Core/CommonTitles.h"
#include "Core/HW/Memmap.h"
#include "Core/IOS/FS/FileSystem.h"
#include "Core/IOS/Network/KD/VFF/VFFUtil.h"
#include "Core/IOS/Network/Socket.h"
#include "Core/IOS/Uids.h"
#include "Core/System.h"
#include "Core/WC24PatchEngine.h"
namespace IOS::HLE
{
namespace
{
enum class HardwareModel : u8
{
RVT = 0,
RVV = 0,
RVL = 1,
RVD = 2,
Unknown = 7
};
u8 GetAreaCode(std::string_view area)
{
static constexpr std::array<std::pair<std::string_view, u8>, 13> regions{{
{"JPN", 0},
{"USA", 1},
{"EUR", 2},
{"AUS", 2},
{"BRA", 1},
{"TWN", 3},
{"ROC", 3},
{"KOR", 4},
{"HKG", 5},
{"ASI", 5},
{"LTN", 1},
{"SAF", 2},
{"CHN", 6},
}};
const auto entry_pos = std::find_if(regions.cbegin(), regions.cend(),
[&area](const auto& entry) { return entry.first == area; });
if (entry_pos != regions.end())
return entry_pos->second;
return 7; // Unknown
}
HardwareModel GetHardwareModel(std::string_view model)
{
static constexpr std::array<std::pair<std::string_view, HardwareModel>, 4> models{{
{"RVL", HardwareModel::RVL},
{"RVT", HardwareModel::RVT},
{"RVV", HardwareModel::RVV},
{"RVD", HardwareModel::RVD},
}};
const auto entry_pos = std::find_if(models.cbegin(), models.cend(),
[&model](const auto& entry) { return entry.first == model; });
if (entry_pos != models.cend())
return entry_pos->second;
return HardwareModel::Unknown;
}
s32 NWC24MakeUserID(u64* nwc24_id, u32 hollywood_id, u16 id_ctr, HardwareModel hardware_model,
u8 area_code)
{
static constexpr std::array<u8, 8> table2{
0x1, 0x5, 0x0, 0x4, 0x2, 0x3, 0x6, 0x7,
};
static constexpr std::array<u8, 16> table1{
0x4, 0xB, 0x7, 0x9, 0xF, 0x1, 0xD, 0x3, 0xC, 0x2, 0x6, 0xE, 0x8, 0x0, 0xA, 0x5,
};
constexpr auto u64_get_byte = [](u64 value, u32 shift) -> u8 { return u8(value >> (shift * 8)); };
constexpr auto u64_insert_byte = [](u64 value, u32 shift, u8 byte) -> u64 {
const u64 mask = 0x00000000000000FFULL << (shift * 8);
const u64 inst = u64{byte} << (shift * 8);
return (value & ~mask) | inst;
};
u64 mix_id = (u64{area_code} << 50) | (u64(hardware_model) << 47) | (u64{hollywood_id} << 15) |
(u64{id_ctr} << 10);
const u64 mix_id_copy1 = mix_id;
u32 ctr = 0;
for (ctr = 0; ctr <= 42; ctr++)
{
u64 value = mix_id >> (52 - ctr);
if ((value & 1) != 0)
{
value = 0x0000000000000635ULL << (42 - ctr);
mix_id ^= value;
}
}
mix_id = (mix_id_copy1 | (mix_id & 0xFFFFFFFFUL)) ^ 0x0000B3B3B3B3B3B3ULL;
mix_id = (mix_id >> 10) | ((mix_id & 0x3FF) << (11 + 32));
for (ctr = 0; ctr <= 5; ctr++)
{
const u8 ret = u64_get_byte(mix_id, ctr);
const u8 foobar = u8((u32{table1[(ret >> 4) & 0xF]} << 4) | table1[ret & 0xF]);
mix_id = u64_insert_byte(mix_id, ctr, foobar & 0xff);
}
const u64 mix_id_copy2 = mix_id;
for (ctr = 0; ctr <= 5; ctr++)
{
const u8 ret = u64_get_byte(mix_id_copy2, ctr);
mix_id = u64_insert_byte(mix_id, table2[ctr], ret);
}
mix_id &= 0x001FFFFFFFFFFFFFULL;
mix_id = (mix_id << 1) | ((mix_id >> 52) & 1);
mix_id ^= 0x00005E5E5E5E5E5EULL;
mix_id &= 0x001FFFFFFFFFFFFFULL;
*nwc24_id = mix_id;
if (mix_id > 9999999999999999ULL)
return NWC24::WC24_ERR_FATAL;
return NWC24::WC24_OK;
}
} // Anonymous namespace
NetKDRequestDevice::NetKDRequestDevice(EmulationKernel& ios, const std::string& device_name)
: EmulationDevice(ios, device_name), config{ios.GetFS()}, m_dl_list{ios.GetFS()}
{
// Enable all NWC24 permissions
m_scheduler_buffer[1] = Common::swap32(-1);
m_work_queue.Reset("WiiConnect24 Worker", [this](AsyncTask task) {
const IPCReply reply = task.handler();
{
std::lock_guard lg(m_async_reply_lock);
m_async_replies.emplace(AsyncReply{task.request, reply.return_value});
}
});
}
NetKDRequestDevice::~NetKDRequestDevice()
{
auto socket_manager = GetEmulationKernel().GetSocketManager();
if (socket_manager)
socket_manager->Clean();
}
void NetKDRequestDevice::Update()
{
{
std::lock_guard lg(m_async_reply_lock);
while (!m_async_replies.empty())
{
const auto& reply = m_async_replies.front();
GetEmulationKernel().EnqueueIPCReply(reply.request, reply.return_value);
m_async_replies.pop();
}
}
}
void NetKDRequestDevice::LogError(ErrorType error_type, s32 error_code)
{
s32 new_code{};
switch (error_type)
{
case ErrorType::Account:
new_code = -(101200 - error_code);
break;
case ErrorType::Client:
new_code = -(107300 - error_code);
break;
case ErrorType::KD_Download:
new_code = -(107200 - error_code);
break;
case ErrorType::Server:
new_code = -(117000 + error_code);
break;
}
std::lock_guard lg(m_scheduler_buffer_lock);
m_scheduler_buffer[32 + (m_error_count % 32)] = Common::swap32(new_code);
m_error_count++;
m_scheduler_buffer[5] = Common::swap32(m_error_count);
m_scheduler_buffer[2] = Common::swap32(new_code);
}
NWC24::ErrorCode NetKDRequestDevice::KDDownload(const u16 entry_index,
const std::optional<u8> subtask_id)
{
std::vector<u8> file_data;
// Content metadata
const std::string content_name = m_dl_list.GetVFFContentName(entry_index, subtask_id);
std::string url = m_dl_list.GetDownloadURL(entry_index, subtask_id);
// Reroute to custom server if enabled.
const std::vector<std::string> parts = SplitString(url, '/');
if (parts.size() < 3)
{
// Invalid URL
LogError(ErrorType::KD_Download, NWC24::WC24_ERR_SERVER);
return NWC24::WC24_ERR_SERVER;
}
if (std::optional<std::string> patch =
WC24PatchEngine::GetNetworkPatch(parts[2], WC24PatchEngine::IsKD{true}))
{
url = ReplaceAll(url, parts[2], patch.value());
}
INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_DOWNLOAD_NOW_EX - NI - URL: {}", url);
INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_DOWNLOAD_NOW_EX - NI - Name: {}", content_name);
const Common::HttpRequest::Response response = m_http.Get(url);
if (!response)
{
const s32 last_response_code = m_http.GetLastResponseCode();
ERROR_LOG_FMT(IOS_WC24, "Failed to request data at {}. HTTP Status Code: {}", url,
last_response_code);
// On a real Wii, KD throws 107305 if it cannot connect to the host. While other issues other
// than invalid host may arise, this code is essentially a catch-all for HTTP client failure.
LogError(last_response_code ? ErrorType::Server : ErrorType::Client,
last_response_code ? last_response_code : NWC24::WC24_ERR_NULL);
return NWC24::WC24_ERR_SERVER;
}
// Check if the filesize is smaller than the header size.
if (response->size() < sizeof(NWC24::WC24File))
{
ERROR_LOG_FMT(IOS_WC24, "File at {} is too small to be a valid file.", url);
LogError(ErrorType::KD_Download, NWC24::WC24_ERR_BROKEN);
return NWC24::WC24_ERR_BROKEN;
}
// Now we read the file
NWC24::WC24File wc24File;
std::memcpy(&wc24File, response->data(), sizeof(NWC24::WC24File));
std::vector<u8> temp_buffer(response->begin() + 320, response->end());
if (m_dl_list.IsEncrypted(entry_index))
{
NWC24::WC24PubkMod pubkMod = m_dl_list.GetWC24PubkMod(entry_index);
file_data = std::vector<u8>(response->size() - 320);
Common::AES::CryptOFB(pubkMod.aes_key, wc24File.iv, wc24File.iv, temp_buffer.data(),
file_data.data(), temp_buffer.size());
}
else
{
file_data = std::move(temp_buffer);
}
NWC24::ErrorCode reply = IOS::HLE::NWC24::OpenVFF(m_dl_list.GetVFFPath(entry_index), content_name,
m_ios.GetFS(), file_data);
if (reply != NWC24::WC24_OK)
LogError(ErrorType::KD_Download, reply);
return reply;
}
IPCReply NetKDRequestDevice::HandleNWC24DownloadNowEx(const IOCtlRequest& request)
{
m_dl_list.ReadDlList();
auto& system = GetSystem();
auto& memory = system.GetMemory();
const u32 flags = memory.Read_U32(request.buffer_in);
// Nintendo converts the entry ID between a u32 and u16
// several times, presumably for alignment purposes.
// We'll skip past buffer_in+4 and keep the entry index as a u16.
const u16 entry_index = memory.Read_U16(request.buffer_in + 6);
const u32 subtask_bitmask = memory.Read_U32(request.buffer_in + 8);
INFO_LOG_FMT(IOS_WC24,
"NET_KD_REQ: IOCTL_NWC24_DOWNLOAD_NOW_EX - NI - flags: {}, index: {}, bitmask: {}",
flags, entry_index, subtask_bitmask);
if (entry_index >= NWC24::NWC24Dl::MAX_ENTRIES)
{
ERROR_LOG_FMT(IOS_WC24, "NET_KD_REQ: Entry index out of range.");
LogError(ErrorType::KD_Download, NWC24::WC24_ERR_INVALID_VALUE);
WriteReturnValue(NWC24::WC24_ERR_INVALID_VALUE, request.buffer_out);
return IPCReply(IPC_SUCCESS);
}
if (!m_dl_list.DoesEntryExist(entry_index))
{
ERROR_LOG_FMT(IOS_WC24, "NET_KD_REQ: Requested entry does not exist in download list!");
LogError(ErrorType::KD_Download, NWC24::WC24_ERR_NOT_FOUND);
WriteReturnValue(NWC24::WC24_ERR_NOT_FOUND, request.buffer_out);
return IPCReply(IPC_SUCCESS);
}
// While in theory reply will always get initialized by KDDownload, things happen.
// Returning NWC24::WC24_ERR_BROKEN or anything that isn't OK will prompt the channel to fix the
// entry's data.
NWC24::ErrorCode reply = NWC24::WC24_ERR_BROKEN;
// Determine if we have subtasks to handle
if (Common::ExtractBit(flags, 2))
{
for (u8 subtask_id = 0; subtask_id < 32; subtask_id++)
{
if (Common::ExtractBit(subtask_bitmask, subtask_id))
{
reply = KDDownload(entry_index, subtask_id);
if (reply != NWC24::WC24_OK)
{
// An error has occurred, break out and return error.
break;
}
}
}
}
else
{
reply = KDDownload(entry_index, std::nullopt);
}
WriteReturnValue(reply, request.buffer_out);
return IPCReply(IPC_SUCCESS);
}
std::optional<IPCReply> NetKDRequestDevice::IOCtl(const IOCtlRequest& request)
{
enum : u32
{
IOCTL_NWC24_SUSPEND_SCHEDULER = 0x01,
IOCTL_NWC24_EXEC_TRY_SUSPEND_SCHEDULER = 0x02,
IOCTL_NWC24_EXEC_RESUME_SCHEDULER = 0x03,
IOCTL_NWC24_KD_GET_TIME_TRIGGERS = 0x04,
IOCTL_NWC24_SET_SCHEDULE_SPAN = 0x05,
IOCTL_NWC24_STARTUP_SOCKET = 0x06,
IOCTL_NWC24_CLEANUP_SOCKET = 0x07,
IOCTL_NWC24_LOCK_SOCKET = 0x08,
IOCTL_NWC24_UNLOCK_SOCKET = 0x09,
IOCTL_NWC24_CHECK_MAIL_NOW = 0x0A,
IOCTL_NWC24_SEND_MAIL_NOW = 0x0B,
IOCTL_NWC24_RECEIVE_MAIL_NOW = 0x0C,
IOCTL_NWC24_SAVE_MAIL_NOW = 0x0D,
IOCTL_NWC24_DOWNLOAD_NOW_EX = 0x0E,
IOCTL_NWC24_REQUEST_GENERATED_USER_ID = 0x0F,
IOCTL_NWC24_REQUEST_REGISTER_USER_ID = 0x10,
IOCTL_NWC24_GET_SCHEDULER_STAT = 0x1E,
IOCTL_NWC24_SET_FILTER_MODE = 0x1F,
IOCTL_NWC24_SET_DEBUG_MODE = 0x20,
IOCTL_NWC24_KD_SET_NEXT_WAKEUP = 0x21,
IOCTL_NWC24_SET_SCRIPT_MODE = 0x22,
IOCTL_NWC24_REQUEST_SHUTDOWN = 0x28,
};
auto& system = GetSystem();
auto& memory = system.GetMemory();
s32 return_value = 0;
switch (request.request)
{
case IOCTL_NWC24_SUSPEND_SCHEDULER:
// NWC24iResumeForCloseLib from NWC24SuspendScheduler (Input: none, Output: 32 bytes)
INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_SUSPEND_SCHEDULER - NI");
WriteReturnValue(0, request.buffer_out); // no error
break;
case IOCTL_NWC24_EXEC_TRY_SUSPEND_SCHEDULER: // NWC24iResumeForCloseLib
INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_EXEC_TRY_SUSPEND_SCHEDULER - NI");
break;
case IOCTL_NWC24_EXEC_RESUME_SCHEDULER: // NWC24iResumeForCloseLib
INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_EXEC_RESUME_SCHEDULER - NI");
WriteReturnValue(0, request.buffer_out); // no error
break;
case IOCTL_NWC24_STARTUP_SOCKET: // NWC24iStartupSocket
WriteReturnValue(0, request.buffer_out);
memory.Write_U32(0, request.buffer_out + 4);
return_value = 0;
INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_STARTUP_SOCKET - NI");
break;
case IOCTL_NWC24_CLEANUP_SOCKET:
INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_CLEANUP_SOCKET");
GetEmulationKernel().GetSocketManager()->Clean();
break;
case IOCTL_NWC24_LOCK_SOCKET: // WiiMenu
INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_LOCK_SOCKET - NI");
break;
case IOCTL_NWC24_UNLOCK_SOCKET:
INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_UNLOCK_SOCKET - NI");
break;
case IOCTL_NWC24_REQUEST_REGISTER_USER_ID:
INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_REQUEST_REGISTER_USER_ID");
WriteReturnValue(0, request.buffer_out);
memory.Write_U32(0, request.buffer_out + 4);
break;
case IOCTL_NWC24_REQUEST_GENERATED_USER_ID: // (Input: none, Output: 32 bytes)
INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_REQUEST_GENERATED_USER_ID");
if (config.IsCreated())
{
const std::string settings_file_path =
Common::GetTitleDataPath(Titles::SYSTEM_MENU) + "/" WII_SETTING;
std::string area, model;
const auto fs = m_ios.GetFS();
if (const auto file = fs->OpenFile(PID_KD, PID_KD, settings_file_path, FS::Mode::Read))
{
Common::SettingsHandler::Buffer data;
if (file->Read(data.data(), data.size()))
{
const Common::SettingsHandler gen{std::move(data)};
area = gen.GetValue("AREA");
model = gen.GetValue("MODEL");
}
}
if (!area.empty() && !model.empty())
{
const u8 area_code = GetAreaCode(area);
const u8 id_ctr = u8(config.IdGen());
const HardwareModel hardware_model = GetHardwareModel(model);
const u32 hollywood_id = m_ios.GetIOSC().GetDeviceId();
u64 user_id = 0;
const s32 ret = NWC24MakeUserID(&user_id, hollywood_id, id_ctr, hardware_model, area_code);
config.SetId(user_id);
config.IncrementIdGen();
config.SetCreationStage(NWC24::NWC24CreationStage::Generated);
config.SetChecksum(config.CalculateNwc24ConfigChecksum());
config.WriteConfig();
config.WriteCBK();
WriteReturnValue(ret, request.buffer_out);
}
else
{
LogError(ErrorType::Account, NWC24::WC24_ERR_INVALID_VALUE);
WriteReturnValue(NWC24::WC24_ERR_FATAL, request.buffer_out);
}
}
else if (config.IsGenerated())
{
WriteReturnValue(NWC24::WC24_ERR_ID_GENERATED, request.buffer_out);
}
else if (config.IsRegistered())
{
WriteReturnValue(NWC24::WC24_ERR_ID_REGISTERED, request.buffer_out);
}
memory.Write_U64(config.Id(), request.buffer_out + 4);
memory.Write_U32(u32(config.CreationStage()), request.buffer_out + 0xC);
break;
case IOCTL_NWC24_GET_SCHEDULER_STAT:
{
if (request.buffer_out == 0 || request.buffer_out % 4 != 0 || request.buffer_out_size < 16)
{
return_value = IPC_EINVAL;
ERROR_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_GET_SCHEDULER_STAT = IPC_EINVAL");
break;
}
INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_GET_SCHEDULER_STAT - buffer out size: {}",
request.buffer_out_size);
// On a real Wii, GetSchedulerStat copies memory containing a list of error codes recorded by
// KD among other things. In most instances there will never be more than one error code
// recorded as we do not have a scheduler.
const u32 out_size = std::min(request.buffer_out_size, 256U);
memory.CopyToEmu(request.buffer_out, m_scheduler_buffer.data(), out_size);
break;
}
case IOCTL_NWC24_SAVE_MAIL_NOW:
INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_SAVE_MAIL_NOW - NI");
break;
case IOCTL_NWC24_DOWNLOAD_NOW_EX:
return LaunchAsyncTask(&NetKDRequestDevice::HandleNWC24DownloadNowEx, request);
case IOCTL_NWC24_REQUEST_SHUTDOWN:
{
if (request.buffer_in == 0 || request.buffer_in % 4 != 0 || request.buffer_in_size < 8 ||
request.buffer_out == 0 || request.buffer_out % 4 != 0 || request.buffer_out_size < 4)
{
return_value = IPC_EINVAL;
ERROR_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_REQUEST_SHUTDOWN = IPC_EINVAL");
break;
}
INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_REQUEST_SHUTDOWN");
[[maybe_unused]] const u32 event = memory.Read_U32(request.buffer_in);
// TODO: Advertise shutdown event
// TODO: Shutdown USB keyboard LEDs if event == 3
// TODO: IOCTLV_NCD_SETCONFIG
// TODO: DHCP related features
// SOGetInterfaceOpt(0xfffe,0x4003); // IP settings
// SOGetInterfaceOpt(0xfffe,0xc001); // DHCP lease time remaining?
// SOGetInterfaceOpt(0xfffe,0x1003); // Error
// Call /dev/net/ip/top 0x1b (SOCleanup), it closes all sockets
GetEmulationKernel().GetSocketManager()->Clean();
return_value = IPC_SUCCESS;
break;
}
default:
request.Log(GetDeviceName(), Common::Log::LogType::IOS_WC24);
}
return IPCReply(return_value);
}
} // namespace IOS::HLE