From ff3947a1b93f1591d17076563a806139bdaf6f75 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 24 Aug 2021 07:13:06 -0400 Subject: [PATCH 01/11] NetKDRequest: Minor adjustments to NWC24MakeUserID - Make use of std::array - Move helper functions inside it as lambdas - Eliminate sign-conversions that were occurring (promotion of u8 to int) --- .../Core/Core/IOS/Network/KD/NetKDRequest.cpp | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp b/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp index 92b5cbaa62..3dedbc726f 100644 --- a/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp +++ b/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp @@ -3,6 +3,7 @@ #include "Core/IOS/Network/KD/NetKDRequest.h" +#include #include #include @@ -201,34 +202,35 @@ u8 NetKDRequestDevice::GetHardwareModel(const std::string& model) const return MODEL_ELSE; } -static u8 u64_get_byte(u64 value, u8 shift) -{ - return (u8)(value >> (shift * 8)); -} - -static u64 u64_insert_byte(u64 value, u8 shift, u8 byte) -{ - u64 mask = 0x00000000000000FFULL << (shift * 8); - u64 inst = (u64)byte << (shift * 8); - return (value & ~mask) | inst; -} - s32 NetKDRequestDevice::NWC24MakeUserID(u64* nwc24_id, u32 hollywood_id, u16 id_ctr, u8 hardware_model, u8 area_code) { - const u8 table2[8] = {0x1, 0x5, 0x0, 0x4, 0x2, 0x3, 0x6, 0x7}; - const u8 table1[16] = {0x4, 0xB, 0x7, 0x9, 0xF, 0x1, 0xD, 0x3, - 0xC, 0x2, 0x6, 0xE, 0x8, 0x0, 0xA, 0x5}; + static constexpr std::array table2{ + 0x1, 0x5, 0x0, 0x4, 0x2, 0x3, 0x6, 0x7, + }; + static constexpr std::array table1{ + 0x4, 0xB, 0x7, 0x9, 0xF, 0x1, 0xD, 0x3, 0xC, 0x2, 0x6, 0xE, 0x8, 0x0, 0xA, 0x5, + }; - u64 mix_id = ((u64)area_code << 50) | ((u64)hardware_model << 47) | ((u64)hollywood_id << 15) | - ((u64)id_ctr << 10); - u64 mix_id_copy1 = mix_id; + constexpr auto u64_get_byte = [](u64 value, u32 shift) -> u8 { + return u8(value >> (shift * 8)); + }; - int ctr = 0; + 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) + if ((value & 1) != 0) { value = 0x0000000000000635ULL << (42 - ctr); mix_id ^= value; @@ -240,15 +242,16 @@ s32 NetKDRequestDevice::NWC24MakeUserID(u64* nwc24_id, u32 hollywood_id, u16 id_ for (ctr = 0; ctr <= 5; ctr++) { - u8 ret = u64_get_byte(mix_id, ctr); - u8 foobar = ((table1[(ret >> 4) & 0xF]) << 4) | (table1[ret & 0xF]); + 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); } - u64 mix_id_copy2 = mix_id; + + const u64 mix_id_copy2 = mix_id; for (ctr = 0; ctr <= 5; ctr++) { - u8 ret = u64_get_byte(mix_id_copy2, ctr); + const u8 ret = u64_get_byte(mix_id_copy2, ctr); mix_id = u64_insert_byte(mix_id, table2[ctr], ret); } From 1e27880014b5d49bb017e9d3110a7ae5f6310fdc Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 24 Aug 2021 07:18:00 -0400 Subject: [PATCH 02/11] NetKDRequest: Make private member functions completely internal These functions don't touch any class state, so they can be turned into internal helper functions. While we're at it, we can move the enumerations as well. --- .../Core/Core/IOS/Network/KD/NetKDRequest.cpp | 229 ++++++++++-------- .../Core/Core/IOS/Network/KD/NetKDRequest.h | 40 --- 2 files changed, 132 insertions(+), 137 deletions(-) diff --git a/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp b/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp index 3dedbc726f..022086cc23 100644 --- a/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp +++ b/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp @@ -22,6 +22,112 @@ namespace IOS::HLE { +namespace +{ +enum : u8 +{ + MODEL_RVT = 0, + MODEL_RVV = 0, + MODEL_RVL = 1, + MODEL_RVD = 2, + MODEL_ELSE = 7 +}; + +u8 GetAreaCode(const std::string& area) +{ + static const std::map 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 = regions.find(area); + if (entry_pos != regions.end()) + return entry_pos->second; + + return 7; // Unknown +} + +u8 GetHardwareModel(const std::string& model) +{ + static const std::map models = { + {"RVL", MODEL_RVL}, + {"RVT", MODEL_RVT}, + {"RVV", MODEL_RVV}, + {"RVD", MODEL_RVD}, + }; + + const auto entry_pos = models.find(model); + if (entry_pos != models.end()) + return entry_pos->second; + + return MODEL_ELSE; +} + +s32 NWC24MakeUserID(u64* nwc24_id, u32 hollywood_id, u16 id_ctr, u8 hardware_model, u8 area_code) +{ + static constexpr std::array table2{ + 0x1, 0x5, 0x0, 0x4, 0x2, 0x3, 0x6, 0x7, + }; + static constexpr std::array 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(Kernel& ios, const std::string& device_name) : Device(ios, device_name), config{ios.GetFS()} { @@ -34,6 +140,32 @@ NetKDRequestDevice::~NetKDRequestDevice() std::optional NetKDRequestDevice::IOCtl(const IOCtlRequest& request) { + enum : u32 + { + IOCTL_NWC24_SUSPEND_SCHEDULAR = 0x01, + IOCTL_NWC24_EXEC_TRY_SUSPEND_SCHEDULAR = 0x02, + IOCTL_NWC24_EXEC_RESUME_SCHEDULAR = 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_SCHEDULAR_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, + }; + s32 return_value = 0; switch (request.request) { @@ -171,101 +303,4 @@ std::optional NetKDRequestDevice::IOCtl(const IOCtlRequest& request) return IPCReply(return_value); } - -u8 NetKDRequestDevice::GetAreaCode(const std::string& area) const -{ - static const std::map 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}, - }; - - auto entryPos = regions.find(area); - if (entryPos != regions.end()) - return entryPos->second; - - return 7; // Unknown -} - -u8 NetKDRequestDevice::GetHardwareModel(const std::string& model) const -{ - static const std::map models = { - {"RVL", MODEL_RVL}, - {"RVT", MODEL_RVT}, - {"RVV", MODEL_RVV}, - {"RVD", MODEL_RVD}, - }; - - auto entryPos = models.find(model); - if (entryPos != models.end()) - return entryPos->second; - - return MODEL_ELSE; -} - -s32 NetKDRequestDevice::NWC24MakeUserID(u64* nwc24_id, u32 hollywood_id, u16 id_ctr, - u8 hardware_model, u8 area_code) -{ - static constexpr std::array table2{ - 0x1, 0x5, 0x0, 0x4, 0x2, 0x3, 0x6, 0x7, - }; - static constexpr std::array 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; -} } // namespace IOS::HLE diff --git a/Source/Core/Core/IOS/Network/KD/NetKDRequest.h b/Source/Core/Core/IOS/Network/KD/NetKDRequest.h index 402296ae97..6c4beb20a1 100644 --- a/Source/Core/Core/IOS/Network/KD/NetKDRequest.h +++ b/Source/Core/Core/IOS/Network/KD/NetKDRequest.h @@ -23,46 +23,6 @@ public: std::optional IOCtl(const IOCtlRequest& request) override; private: - enum - { - IOCTL_NWC24_SUSPEND_SCHEDULAR = 0x01, - IOCTL_NWC24_EXEC_TRY_SUSPEND_SCHEDULAR = 0x02, - IOCTL_NWC24_EXEC_RESUME_SCHEDULAR = 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_SCHEDULAR_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, - }; - - enum - { - MODEL_RVT = 0, - MODEL_RVV = 0, - MODEL_RVL = 1, - MODEL_RVD = 2, - MODEL_ELSE = 7 - }; - - u8 GetAreaCode(const std::string& area) const; - u8 GetHardwareModel(const std::string& model) const; - - s32 NWC24MakeUserID(u64* nwc24_id, u32 hollywood_id, u16 id_ctr, u8 hardware_model, u8 area_code); - NWC24::NWC24Config config; }; } // namespace IOS::HLE From 3a38e9900124e0f20a3c5e2c99421c7de9582abf Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 24 Aug 2021 07:26:43 -0400 Subject: [PATCH 03/11] NetKDRequest: Make hardware model enum strongly typed Makes it a distinct type instead of decaying to an ambiguous u8. --- .../Core/Core/IOS/Network/KD/NetKDRequest.cpp | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp b/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp index 022086cc23..6593d040b8 100644 --- a/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp +++ b/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp @@ -24,13 +24,13 @@ namespace IOS::HLE { namespace { -enum : u8 +enum class HardwareModel : u8 { - MODEL_RVT = 0, - MODEL_RVV = 0, - MODEL_RVL = 1, - MODEL_RVD = 2, - MODEL_ELSE = 7 + RVT = 0, + RVV = 0, + RVL = 1, + RVD = 2, + Unknown = 7 }; u8 GetAreaCode(const std::string& area) @@ -47,23 +47,24 @@ u8 GetAreaCode(const std::string& area) return 7; // Unknown } -u8 GetHardwareModel(const std::string& model) +HardwareModel GetHardwareModel(const std::string& model) { - static const std::map models = { - {"RVL", MODEL_RVL}, - {"RVT", MODEL_RVT}, - {"RVV", MODEL_RVV}, - {"RVD", MODEL_RVD}, + static const std::map models = { + {"RVL", HardwareModel::RVL}, + {"RVT", HardwareModel::RVT}, + {"RVV", HardwareModel::RVV}, + {"RVD", HardwareModel::RVD}, }; const auto entry_pos = models.find(model); if (entry_pos != models.end()) return entry_pos->second; - return MODEL_ELSE; + return HardwareModel::Unknown; } -s32 NWC24MakeUserID(u64* nwc24_id, u32 hollywood_id, u16 id_ctr, u8 hardware_model, u8 area_code) +s32 NWC24MakeUserID(u64* nwc24_id, u32 hollywood_id, u16 id_ctr, HardwareModel hardware_model, + u8 area_code) { static constexpr std::array table2{ 0x1, 0x5, 0x0, 0x4, 0x2, 0x3, 0x6, 0x7, @@ -80,7 +81,7 @@ s32 NWC24MakeUserID(u64* nwc24_id, u32 hollywood_id, u16 id_ctr, u8 hardware_mod return (value & ~mask) | inst; }; - u64 mix_id = (u64{area_code} << 50) | (u64{hardware_model} << 47) | (u64{hollywood_id} << 15) | + 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; @@ -232,15 +233,15 @@ std::optional NetKDRequestDevice::IOCtl(const IOCtlRequest& request) if (!area.empty() && !model.empty()) { - u8 area_code = GetAreaCode(area); - u8 id_ctr = config.IdGen(); - u8 hardware_model = GetHardwareModel(model); + const u8 area_code = GetAreaCode(area); + const u8 id_ctr = u8(config.IdGen()); + const HardwareModel hardware_model = GetHardwareModel(model); - u32 HollywoodID = m_ios.GetIOSC().GetDeviceId(); - u64 UserID = 0; + const u32 hollywood_id = m_ios.GetIOSC().GetDeviceId(); + u64 user_id = 0; - s32 ret = NWC24MakeUserID(&UserID, HollywoodID, id_ctr, hardware_model, area_code); - config.SetId(UserID); + const s32 ret = NWC24MakeUserID(&user_id, hollywood_id, id_ctr, hardware_model, area_code); + config.SetId(user_id); config.IncrementIdGen(); config.SetCreationStage(NWC24::NWC24Config::NWC24_IDCS_GENERATED); config.WriteConfig(); From b49bd76d91689fd348f536fcfd346a2947c5767c Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 24 Aug 2021 07:28:20 -0400 Subject: [PATCH 04/11] NetKDRequest: Fix typo in IOCtl enum names (schedular -> scheduler) --- .../Core/Core/IOS/Network/KD/NetKDRequest.cpp | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp b/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp index 6593d040b8..cc98153068 100644 --- a/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp +++ b/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp @@ -143,9 +143,9 @@ std::optional NetKDRequestDevice::IOCtl(const IOCtlRequest& request) { enum : u32 { - IOCTL_NWC24_SUSPEND_SCHEDULAR = 0x01, - IOCTL_NWC24_EXEC_TRY_SUSPEND_SCHEDULAR = 0x02, - IOCTL_NWC24_EXEC_RESUME_SCHEDULAR = 0x03, + 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, @@ -159,7 +159,7 @@ std::optional NetKDRequestDevice::IOCtl(const IOCtlRequest& request) IOCTL_NWC24_DOWNLOAD_NOW_EX = 0x0E, IOCTL_NWC24_REQUEST_GENERATED_USER_ID = 0x0F, IOCTL_NWC24_REQUEST_REGISTER_USER_ID = 0x10, - IOCTL_NWC24_GET_SCHEDULAR_STAT = 0x1E, + 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, @@ -170,18 +170,18 @@ std::optional NetKDRequestDevice::IOCtl(const IOCtlRequest& request) s32 return_value = 0; switch (request.request) { - case IOCTL_NWC24_SUSPEND_SCHEDULAR: + case IOCTL_NWC24_SUSPEND_SCHEDULER: // NWC24iResumeForCloseLib from NWC24SuspendScheduler (Input: none, Output: 32 bytes) - INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_SUSPEND_SCHEDULAR - NI"); + 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_SCHEDULAR: // NWC24iResumeForCloseLib - INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_EXEC_TRY_SUSPEND_SCHEDULAR - NI"); + 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_SCHEDULAR: // NWC24iResumeForCloseLib - INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_EXEC_RESUME_SCHEDULAR - NI"); + 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; @@ -265,8 +265,8 @@ std::optional NetKDRequestDevice::IOCtl(const IOCtlRequest& request) Memory::Write_U32(config.CreationStage(), request.buffer_out + 0xC); break; - case IOCTL_NWC24_GET_SCHEDULAR_STAT: - INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_GET_SCHEDULAR_STAT - NI"); + case IOCTL_NWC24_GET_SCHEDULER_STAT: + INFO_LOG_FMT(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_GET_SCHEDULER_STAT - NI"); break; case IOCTL_NWC24_SAVE_MAIL_NOW: From e65363f05ff41d10af0d01229f6b1adac44dfc66 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 24 Aug 2021 07:35:02 -0400 Subject: [PATCH 05/11] NetKDRequest: Make use of constexpr arrays containing pairs instead of maps Currently we were using heap allocating maps that last for the entire duration of the emulator running. Given the size N of both of these maps are very small (< 20 elements), we can just make use of an array of pairs and perform linear scans. This is also fine, given this code isn't particularly "hot" either, so this won't be run often. --- .../Core/Core/IOS/Network/KD/NetKDRequest.cpp | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp b/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp index cc98153068..d5980423f5 100644 --- a/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp +++ b/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp @@ -3,9 +3,11 @@ #include "Core/IOS/Network/KD/NetKDRequest.h" +#include #include -#include #include +#include +#include #include "Common/CommonPaths.h" #include "Common/CommonTypes.h" @@ -33,31 +35,44 @@ enum class HardwareModel : u8 Unknown = 7 }; -u8 GetAreaCode(const std::string& area) +u8 GetAreaCode(std::string_view area) { - static const std::map 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}, - }; + static constexpr std::array, 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 = regions.find(area); + 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(const std::string& model) +HardwareModel GetHardwareModel(std::string_view model) { - static const std::map models = { + static constexpr std::array, 4> models{{ {"RVL", HardwareModel::RVL}, {"RVT", HardwareModel::RVT}, {"RVV", HardwareModel::RVV}, {"RVD", HardwareModel::RVD}, - }; + }}; - const auto entry_pos = models.find(model); - if (entry_pos != models.end()) + 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; From 82371b89fc2c67ecba8f8c4949b676a686af4e08 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 24 Aug 2021 07:38:30 -0400 Subject: [PATCH 06/11] NetKDRequest: Remove unused include --- Source/Core/Core/IOS/Network/KD/NetKDRequest.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/Core/Core/IOS/Network/KD/NetKDRequest.h b/Source/Core/Core/IOS/Network/KD/NetKDRequest.h index 6c4beb20a1..9172a490c3 100644 --- a/Source/Core/Core/IOS/Network/KD/NetKDRequest.h +++ b/Source/Core/Core/IOS/Network/KD/NetKDRequest.h @@ -5,7 +5,6 @@ #include -#include "Common/CommonTypes.h" #include "Core/IOS/Device.h" #include "Core/IOS/Network/KD/NWC24Config.h" From bc939df69ccd706cfd48eb55c2ed74b1ebfeafc4 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 24 Aug 2021 08:02:04 -0400 Subject: [PATCH 07/11] NWC24Config: Make creation stage an enum class Makes the enum strongly typed instead of interacting with a raw u32 value. While we're at it, we can add helpers to the NWC24Config to make using code poke at the internals of the class a little bit less and also make the querying a little nicer to read. --- .../Core/Core/IOS/Network/KD/NWC24Config.cpp | 10 ++++---- Source/Core/Core/IOS/Network/KD/NWC24Config.h | 24 +++++++++++-------- .../Core/Core/IOS/Network/KD/NetKDRequest.cpp | 10 ++++---- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/Source/Core/Core/IOS/Network/KD/NWC24Config.cpp b/Source/Core/Core/IOS/Network/KD/NWC24Config.cpp index 4d7264ff27..794b1ff9be 100644 --- a/Source/Core/Core/IOS/Network/KD/NWC24Config.cpp +++ b/Source/Core/Core/IOS/Network/KD/NWC24Config.cpp @@ -60,7 +60,7 @@ void NWC24Config::ResetConfig() SetMagic(0x57634366); SetUnk(8); - SetCreationStage(NWC24_IDCS_INITIAL); + SetCreationStage(NWC24CreationStage::Initial); SetEnableBooting(0); SetEmail("@wii.com"); @@ -165,14 +165,14 @@ void NWC24Config::SetChecksum(u32 checksum) m_data.checksum = Common::swap32(checksum); } -u32 NWC24Config::CreationStage() const +NWC24CreationStage NWC24Config::CreationStage() const { - return Common::swap32(m_data.creation_stage); + return NWC24CreationStage(Common::swap32(u32(m_data.creation_stage))); } -void NWC24Config::SetCreationStage(u32 creation_stage) +void NWC24Config::SetCreationStage(NWC24CreationStage creation_stage) { - m_data.creation_stage = Common::swap32(creation_stage); + m_data.creation_stage = NWC24CreationStage(Common::swap32(u32(creation_stage))); } u32 NWC24Config::EnableBooting() const diff --git a/Source/Core/Core/IOS/Network/KD/NWC24Config.h b/Source/Core/Core/IOS/Network/KD/NWC24Config.h index 5f7dc08d73..82fef35e8e 100644 --- a/Source/Core/Core/IOS/Network/KD/NWC24Config.h +++ b/Source/Core/Core/IOS/Network/KD/NWC24Config.h @@ -25,16 +25,16 @@ enum ErrorCode : s32 WC24_ERR_ID_NOT_REGISTERED = -44, }; +enum class NWC24CreationStage : u32 +{ + Initial = 0, + Generated = 1, + Registered = 2 +}; + class NWC24Config final { public: - enum - { - NWC24_IDCS_INITIAL = 0, - NWC24_IDCS_GENERATED = 1, - NWC24_IDCS_REGISTERED = 2 - }; - enum { URL_COUNT = 0x05, @@ -65,8 +65,12 @@ public: u32 Checksum() const; void SetChecksum(u32 checksum); - u32 CreationStage() const; - void SetCreationStage(u32 creation_stage); + NWC24CreationStage CreationStage() const; + void SetCreationStage(NWC24CreationStage creation_stage); + + bool IsCreated() const { return CreationStage() == NWC24CreationStage::Initial; } + bool IsGenerated() const { return CreationStage() == NWC24CreationStage::Generated; } + bool IsRegistered() const { return CreationStage() == NWC24CreationStage::Registered; } u32 EnableBooting() const; void SetEnableBooting(u32 enable_booting); @@ -85,7 +89,7 @@ private: u32 unk_04; // must be 8 u64 nwc24_id; u32 id_generation; - u32 creation_stage; // 0:not_generated; 1:generated; 2:registered + NWC24CreationStage creation_stage; char email[MAX_EMAIL_LENGTH]; char paswd[MAX_PASSWORD_LENGTH]; char mlchkid[0x24]; diff --git a/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp b/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp index d5980423f5..986633ed76 100644 --- a/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp +++ b/Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp @@ -228,7 +228,7 @@ std::optional NetKDRequestDevice::IOCtl(const IOCtlRequest& request) 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.CreationStage() == NWC24::NWC24Config::NWC24_IDCS_INITIAL) + if (config.IsCreated()) { const std::string settings_file_path = Common::GetTitleDataPath(Titles::SYSTEM_MENU) + "/" WII_SETTING; @@ -258,7 +258,7 @@ std::optional NetKDRequestDevice::IOCtl(const IOCtlRequest& request) const s32 ret = NWC24MakeUserID(&user_id, hollywood_id, id_ctr, hardware_model, area_code); config.SetId(user_id); config.IncrementIdGen(); - config.SetCreationStage(NWC24::NWC24Config::NWC24_IDCS_GENERATED); + config.SetCreationStage(NWC24::NWC24CreationStage::Generated); config.WriteConfig(); WriteReturnValue(ret, request.buffer_out); @@ -268,16 +268,16 @@ std::optional NetKDRequestDevice::IOCtl(const IOCtlRequest& request) WriteReturnValue(NWC24::WC24_ERR_FATAL, request.buffer_out); } } - else if (config.CreationStage() == NWC24::NWC24Config::NWC24_IDCS_GENERATED) + else if (config.IsGenerated()) { WriteReturnValue(NWC24::WC24_ERR_ID_GENERATED, request.buffer_out); } - else if (config.CreationStage() == NWC24::NWC24Config::NWC24_IDCS_REGISTERED) + 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(config.CreationStage(), request.buffer_out + 0xC); + Memory::Write_U32(u32(config.CreationStage()), request.buffer_out + 0xC); break; case IOCTL_NWC24_GET_SCHEDULER_STAT: From a60af199d8232c49197cc5f0d9da48d13d5a27dd Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 24 Aug 2021 08:04:27 -0400 Subject: [PATCH 08/11] NWC24Config: Move array length constants into the private part of the class These are only used internally and not intended to be used outside of the class itself. --- Source/Core/Core/IOS/Network/KD/NWC24Config.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Source/Core/Core/IOS/Network/KD/NWC24Config.h b/Source/Core/Core/IOS/Network/KD/NWC24Config.h index 82fef35e8e..c792815b3f 100644 --- a/Source/Core/Core/IOS/Network/KD/NWC24Config.h +++ b/Source/Core/Core/IOS/Network/KD/NWC24Config.h @@ -35,14 +35,6 @@ enum class NWC24CreationStage : u32 class NWC24Config final { public: - enum - { - URL_COUNT = 0x05, - MAX_URL_LENGTH = 0x80, - MAX_EMAIL_LENGTH = 0x40, - MAX_PASSWORD_LENGTH = 0x20, - }; - explicit NWC24Config(std::shared_ptr fs); void ReadConfig(); @@ -82,6 +74,14 @@ public: void SetEmail(const char* email); private: + enum + { + URL_COUNT = 0x05, + MAX_URL_LENGTH = 0x80, + MAX_EMAIL_LENGTH = 0x40, + MAX_PASSWORD_LENGTH = 0x20, + }; + #pragma pack(push, 1) struct ConfigData final { From 6268ee44a156a575f3a12b7f48a4244b7c8279b0 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 24 Aug 2021 08:23:02 -0400 Subject: [PATCH 09/11] NWC24Config: Provide name for unk_04 config member According to WiiBrew this is a version number (thanks to Awesomebing1 for documenting this!) --- Source/Core/Core/IOS/Network/KD/NWC24Config.cpp | 12 ++++++------ Source/Core/Core/IOS/Network/KD/NWC24Config.h | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Source/Core/Core/IOS/Network/KD/NWC24Config.cpp b/Source/Core/Core/IOS/Network/KD/NWC24Config.cpp index 794b1ff9be..4ff427907c 100644 --- a/Source/Core/Core/IOS/Network/KD/NWC24Config.cpp +++ b/Source/Core/Core/IOS/Network/KD/NWC24Config.cpp @@ -59,7 +59,7 @@ void NWC24Config::ResetConfig() memset(&m_data, 0, sizeof(m_data)); SetMagic(0x57634366); - SetUnk(8); + SetVersion(8); SetCreationStage(NWC24CreationStage::Initial); SetEnableBooting(0); SetEmail("@wii.com"); @@ -110,7 +110,7 @@ s32 NWC24Config::CheckNwc24Config() const return -14; } - if (Unk() != 8) + if (Version() != 8) return -27; return 0; @@ -126,14 +126,14 @@ void NWC24Config::SetMagic(u32 magic) m_data.magic = Common::swap32(magic); } -u32 NWC24Config::Unk() const +u32 NWC24Config::Version() const { - return Common::swap32(m_data.unk_04); + return Common::swap32(m_data.version); } -void NWC24Config::SetUnk(u32 unk_04) +void NWC24Config::SetVersion(u32 version) { - m_data.unk_04 = Common::swap32(unk_04); + m_data.version = Common::swap32(version); } u32 NWC24Config::IdGen() const diff --git a/Source/Core/Core/IOS/Network/KD/NWC24Config.h b/Source/Core/Core/IOS/Network/KD/NWC24Config.h index c792815b3f..30a814cc19 100644 --- a/Source/Core/Core/IOS/Network/KD/NWC24Config.h +++ b/Source/Core/Core/IOS/Network/KD/NWC24Config.h @@ -47,8 +47,8 @@ public: u32 Magic() const; void SetMagic(u32 magic); - u32 Unk() const; - void SetUnk(u32 unk_04); + u32 Version() const; + void SetVersion(u32 version); u32 IdGen() const; void SetIdGen(u32 id_generation); @@ -85,8 +85,8 @@ private: #pragma pack(push, 1) struct ConfigData final { - u32 magic; // 'WcCf' 0x57634366 - u32 unk_04; // must be 8 + u32 magic; // 'WcCf' 0x57634366 + u32 version; // must be 8 u64 nwc24_id; u32 id_generation; NWC24CreationStage creation_stage; From caba81ad3b388865357e831637ff17afb78ae059 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 24 Aug 2021 08:08:36 -0400 Subject: [PATCH 10/11] NetKDTime: Move internal IOCtl values into IOCtl function These are only ever used here, so they can be turned into an internal implementation detail. --- Source/Core/Core/IOS/Network/KD/NetKDTime.cpp | 9 +++++++++ Source/Core/Core/IOS/Network/KD/NetKDTime.h | 9 --------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Source/Core/Core/IOS/Network/KD/NetKDTime.cpp b/Source/Core/Core/IOS/Network/KD/NetKDTime.cpp index 6e48a0b8bd..5e4098ff5f 100644 --- a/Source/Core/Core/IOS/Network/KD/NetKDTime.cpp +++ b/Source/Core/Core/IOS/Network/KD/NetKDTime.cpp @@ -20,6 +20,15 @@ NetKDTimeDevice::~NetKDTimeDevice() = default; std::optional NetKDTimeDevice::IOCtl(const IOCtlRequest& request) { + enum : u32 + { + IOCTL_NW24_GET_UNIVERSAL_TIME = 0x14, + IOCTL_NW24_SET_UNIVERSAL_TIME = 0x15, + IOCTL_NW24_UNIMPLEMENTED = 0x16, + IOCTL_NW24_SET_RTC_COUNTER = 0x17, + IOCTL_NW24_GET_TIME_DIFF = 0x18, + }; + s32 result = 0; u32 common_result = 0; // TODO Writes stuff to /shared2/nwc24/misc.bin diff --git a/Source/Core/Core/IOS/Network/KD/NetKDTime.h b/Source/Core/Core/IOS/Network/KD/NetKDTime.h index ab5439642d..55c071ddaf 100644 --- a/Source/Core/Core/IOS/Network/KD/NetKDTime.h +++ b/Source/Core/Core/IOS/Network/KD/NetKDTime.h @@ -31,15 +31,6 @@ private: // what the host OS thinks void SetAdjustedUTC(u64 wii_utc); - enum - { - IOCTL_NW24_GET_UNIVERSAL_TIME = 0x14, - IOCTL_NW24_SET_UNIVERSAL_TIME = 0x15, - IOCTL_NW24_UNIMPLEMENTED = 0x16, - IOCTL_NW24_SET_RTC_COUNTER = 0x17, - IOCTL_NW24_GET_TIME_DIFF = 0x18, - }; - u64 rtc = 0; s64 utcdiff = 0; }; From 3c0d4b7d8fd7c2fbb0f3741dfd892693d439689e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 24 Aug 2021 08:13:23 -0400 Subject: [PATCH 11/11] NetKDTime: Eliminate implicit sign conversions Silences compiler warnings and also makes the conversions here explicit to the reader. --- Source/Core/Core/IOS/Network/KD/NetKDTime.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Source/Core/Core/IOS/Network/KD/NetKDTime.cpp b/Source/Core/Core/IOS/Network/KD/NetKDTime.cpp index 5e4098ff5f..5011647e6e 100644 --- a/Source/Core/Core/IOS/Network/KD/NetKDTime.cpp +++ b/Source/Core/Core/IOS/Network/KD/NetKDTime.cpp @@ -85,13 +85,17 @@ std::optional NetKDTimeDevice::IOCtl(const IOCtlRequest& request) u64 NetKDTimeDevice::GetAdjustedUTC() const { - return ExpansionInterface::CEXIIPL::GetEmulatedTime(ExpansionInterface::CEXIIPL::UNIX_EPOCH) + - utcdiff; + using namespace ExpansionInterface; + + const u32 emulated_time = CEXIIPL::GetEmulatedTime(CEXIIPL::UNIX_EPOCH); + return u64(s64(emulated_time) + utcdiff); } void NetKDTimeDevice::SetAdjustedUTC(u64 wii_utc) { - utcdiff = ExpansionInterface::CEXIIPL::GetEmulatedTime(ExpansionInterface::CEXIIPL::UNIX_EPOCH) - - wii_utc; + using namespace ExpansionInterface; + + const u32 emulated_time = CEXIIPL::GetEmulatedTime(CEXIIPL::UNIX_EPOCH); + utcdiff = s64(emulated_time - wii_utc); } } // namespace IOS::HLE