diff options
author | Narr the Reg <juangerman-13@hotmail.com> | 2022-08-30 07:33:47 +0200 |
---|---|---|
committer | german77 <juangerman-13@hotmail.com> | 2022-09-07 08:04:00 +0200 |
commit | 19a4e12e6ea6c3d06ee227f3ef1a6cbf93850f6e (patch) | |
tree | f96054552ebe182e75fd77eabdcdf205a0dffe46 /src | |
parent | core: nfp: Implement amiibo encryption (diff) | |
download | yuzu-19a4e12e6ea6c3d06ee227f3ef1a6cbf93850f6e.tar yuzu-19a4e12e6ea6c3d06ee227f3ef1a6cbf93850f6e.tar.gz yuzu-19a4e12e6ea6c3d06ee227f3ef1a6cbf93850f6e.tar.bz2 yuzu-19a4e12e6ea6c3d06ee227f3ef1a6cbf93850f6e.tar.lz yuzu-19a4e12e6ea6c3d06ee227f3ef1a6cbf93850f6e.tar.xz yuzu-19a4e12e6ea6c3d06ee227f3ef1a6cbf93850f6e.tar.zst yuzu-19a4e12e6ea6c3d06ee227f3ef1a6cbf93850f6e.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/core/hle/service/am/applets/applet_mii_edit_types.h | 2 | ||||
-rw-r--r-- | src/core/hle/service/mii/mii.cpp | 32 | ||||
-rw-r--r-- | src/core/hle/service/mii/mii_manager.cpp | 88 | ||||
-rw-r--r-- | src/core/hle/service/mii/mii_manager.h | 9 | ||||
-rw-r--r-- | src/core/hle/service/mii/types.h | 138 | ||||
-rw-r--r-- | src/core/hle/service/nfp/amiibo_crypto.cpp | 81 | ||||
-rw-r--r-- | src/core/hle/service/nfp/amiibo_crypto.h | 3 | ||||
-rw-r--r-- | src/core/hle/service/nfp/amiibo_types.h | 161 | ||||
-rw-r--r-- | src/core/hle/service/nfp/nfp.cpp | 95 | ||||
-rw-r--r-- | src/core/hle/service/nfp/nfp.h | 4 |
10 files changed, 356 insertions, 257 deletions
diff --git a/src/core/hle/service/am/applets/applet_mii_edit_types.h b/src/core/hle/service/am/applets/applet_mii_edit_types.h index 1b145b696..4705d019f 100644 --- a/src/core/hle/service/am/applets/applet_mii_edit_types.h +++ b/src/core/hle/service/am/applets/applet_mii_edit_types.h @@ -32,7 +32,7 @@ enum class MiiEditResult : u32 { }; struct MiiEditCharInfo { - Service::Mii::MiiInfo mii_info{}; + Service::Mii::CharInfo mii_info{}; }; static_assert(sizeof(MiiEditCharInfo) == 0x58, "MiiEditCharInfo has incorrect size."); diff --git a/src/core/hle/service/mii/mii.cpp b/src/core/hle/service/mii/mii.cpp index efb569993..390514fdc 100644 --- a/src/core/hle/service/mii/mii.cpp +++ b/src/core/hle/service/mii/mii.cpp @@ -43,7 +43,7 @@ public: {20, nullptr, "IsBrokenDatabaseWithClearFlag"}, {21, &IDatabaseService::GetIndex, "GetIndex"}, {22, &IDatabaseService::SetInterfaceVersion, "SetInterfaceVersion"}, - {23, nullptr, "Convert"}, + {23, &IDatabaseService::Convert, "Convert"}, {24, nullptr, "ConvertCoreDataToCharInfo"}, {25, nullptr, "ConvertCharInfoToCoreData"}, {26, nullptr, "Append"}, @@ -130,7 +130,7 @@ private: return; } - std::vector<MiiInfo> values; + std::vector<CharInfo> values; for (const auto& element : *result) { values.emplace_back(element.info); } @@ -144,7 +144,7 @@ private: void UpdateLatest(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - const auto info{rp.PopRaw<MiiInfo>()}; + const auto info{rp.PopRaw<CharInfo>()}; const auto source_flag{rp.PopRaw<SourceFlag>()}; LOG_DEBUG(Service_Mii, "called with source_flag={}", source_flag); @@ -156,9 +156,9 @@ private: return; } - IPC::ResponseBuilder rb{ctx, 2 + sizeof(MiiInfo) / sizeof(u32)}; + IPC::ResponseBuilder rb{ctx, 2 + sizeof(CharInfo) / sizeof(u32)}; rb.Push(ResultSuccess); - rb.PushRaw<MiiInfo>(*result); + rb.PushRaw<CharInfo>(*result); } void BuildRandom(Kernel::HLERequestContext& ctx) { @@ -191,9 +191,9 @@ private: return; } - IPC::ResponseBuilder rb{ctx, 2 + sizeof(MiiInfo) / sizeof(u32)}; + IPC::ResponseBuilder rb{ctx, 2 + sizeof(CharInfo) / sizeof(u32)}; rb.Push(ResultSuccess); - rb.PushRaw<MiiInfo>(manager.BuildRandom(age, gender, race)); + rb.PushRaw<CharInfo>(manager.BuildRandom(age, gender, race)); } void BuildDefault(Kernel::HLERequestContext& ctx) { @@ -210,14 +210,14 @@ private: return; } - IPC::ResponseBuilder rb{ctx, 2 + sizeof(MiiInfo) / sizeof(u32)}; + IPC::ResponseBuilder rb{ctx, 2 + sizeof(CharInfo) / sizeof(u32)}; rb.Push(ResultSuccess); - rb.PushRaw<MiiInfo>(manager.BuildDefault(index)); + rb.PushRaw<CharInfo>(manager.BuildDefault(index)); } void GetIndex(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - const auto info{rp.PopRaw<MiiInfo>()}; + const auto info{rp.PopRaw<CharInfo>()}; LOG_DEBUG(Service_Mii, "called"); @@ -239,6 +239,18 @@ private: rb.Push(ResultSuccess); } + void Convert(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + + const auto mii_v3{rp.PopRaw<Ver3StoreData>()}; + + LOG_INFO(Service_Mii, "called"); + + IPC::ResponseBuilder rb{ctx, 2 + sizeof(CharInfo) / sizeof(u32)}; + rb.Push(ResultSuccess); + rb.PushRaw<CharInfo>(manager.ConvertV3ToCharInfo(mii_v3)); + } + constexpr bool IsInterfaceVersionSupported(u32 interface_version) const { return current_interface_version >= interface_version; } diff --git a/src/core/hle/service/mii/mii_manager.cpp b/src/core/hle/service/mii/mii_manager.cpp index 544c92a00..97d1b948f 100644 --- a/src/core/hle/service/mii/mii_manager.cpp +++ b/src/core/hle/service/mii/mii_manager.cpp @@ -42,7 +42,7 @@ std::array<T, DestArraySize> ResizeArray(const std::array<T, SourceArraySize>& i return out; } -MiiInfo ConvertStoreDataToInfo(const MiiStoreData& data) { +CharInfo ConvertStoreDataToInfo(const MiiStoreData& data) { MiiStoreBitFields bf; std::memcpy(&bf, data.data.data.data(), sizeof(MiiStoreBitFields)); @@ -409,8 +409,8 @@ u32 MiiManager::GetCount(SourceFlag source_flag) const { return static_cast<u32>(count); } -ResultVal<MiiInfo> MiiManager::UpdateLatest([[maybe_unused]] const MiiInfo& info, - SourceFlag source_flag) { +ResultVal<CharInfo> MiiManager::UpdateLatest([[maybe_unused]] const CharInfo& info, + SourceFlag source_flag) { if ((source_flag & SourceFlag::Database) == SourceFlag::None) { return ERROR_CANNOT_FIND_ENTRY; } @@ -419,14 +419,90 @@ ResultVal<MiiInfo> MiiManager::UpdateLatest([[maybe_unused]] const MiiInfo& info return ERROR_CANNOT_FIND_ENTRY; } -MiiInfo MiiManager::BuildRandom(Age age, Gender gender, Race race) { +CharInfo MiiManager::BuildRandom(Age age, Gender gender, Race race) { return ConvertStoreDataToInfo(BuildRandomStoreData(age, gender, race, user_id)); } -MiiInfo MiiManager::BuildDefault(std::size_t index) { +CharInfo MiiManager::BuildDefault(std::size_t index) { return ConvertStoreDataToInfo(BuildDefaultStoreData(RawData::DefaultMii.at(index), user_id)); } +CharInfo MiiManager::ConvertV3ToCharInfo(Ver3StoreData mii_v3) const { + Service::Mii::MiiManager manager; + auto mii = manager.BuildDefault(0); + + // Check if mii data exist + if (mii_v3.mii_name[0] == 0) { + return mii; + } + + // TODO: We are ignoring a bunch of data from the mii_v3 + + mii.gender = static_cast<u8>(mii_v3.mii_information.gender); + mii.favorite_color = static_cast<u8>(mii_v3.mii_information.favorite_color); + mii.height = mii_v3.height; + mii.build = mii_v3.build; + + mii.font_region = mii_v3.region_information.character_set; + memcpy(mii.name.data(), mii_v3.mii_name.data(), 10); + + mii.faceline_type = mii_v3.appearance_bits1.face_shape; + mii.faceline_color = mii_v3.appearance_bits1.skin_color; + mii.faceline_wrinkle = mii_v3.appearance_bits2.wrinkles; + mii.faceline_make = mii_v3.appearance_bits2.makeup; + + mii.hair_type = mii_v3.hair_style; + mii.hair_color = mii_v3.appearance_bits3.hair_color; + mii.hair_flip = mii_v3.appearance_bits3.flip_hair; + + mii.eye_type = static_cast<u8>(mii_v3.appearance_bits4.eye_type); + mii.eye_color = static_cast<u8>(mii_v3.appearance_bits4.eye_color); + mii.eye_scale = static_cast<u8>(mii_v3.appearance_bits4.eye_scale); + mii.eye_aspect = static_cast<u8>(mii_v3.appearance_bits4.eye_vertical_stretch); + mii.eye_rotate = static_cast<u8>(mii_v3.appearance_bits4.eye_rotation); + mii.eye_x = static_cast<u8>(mii_v3.appearance_bits4.eye_spacing); + mii.eye_y = static_cast<u8>(mii_v3.appearance_bits4.eye_y_position); + + mii.eyebrow_type = static_cast<u8>(mii_v3.appearance_bits5.eyebrow_style); + mii.eyebrow_color = static_cast<u8>(mii_v3.appearance_bits5.eyebrow_color); + mii.eyebrow_scale = static_cast<u8>(mii_v3.appearance_bits5.eyebrow_scale); + mii.eyebrow_aspect = static_cast<u8>(mii_v3.appearance_bits5.eyebrow_yscale); + mii.eyebrow_rotate = static_cast<u8>(mii_v3.appearance_bits5.eyebrow_rotation); + mii.eyebrow_x = static_cast<u8>(mii_v3.appearance_bits5.eyebrow_spacing); + mii.eyebrow_y = static_cast<u8>(mii_v3.appearance_bits5.eyebrow_y_position); + + mii.nose_type = static_cast<u8>(mii_v3.appearance_bits6.nose_type); + mii.nose_scale = static_cast<u8>(mii_v3.appearance_bits6.nose_scale); + mii.nose_y = static_cast<u8>(mii_v3.appearance_bits6.nose_y_position); + + mii.mouth_type = static_cast<u8>(mii_v3.appearance_bits7.mouth_type); + mii.mouth_color = static_cast<u8>(mii_v3.appearance_bits7.mouth_color); + mii.mouth_scale = static_cast<u8>(mii_v3.appearance_bits7.mouth_scale); + mii.mouth_aspect = static_cast<u8>(mii_v3.appearance_bits7.mouth_horizontal_stretch); + mii.mouth_y = static_cast<u8>(mii_v3.appearance_bits8.mouth_y_position); + + mii.mustache_type = static_cast<u8>(mii_v3.appearance_bits8.mustache_type); + mii.mustache_scale = static_cast<u8>(mii_v3.appearance_bits9.mustache_scale); + mii.mustache_y = static_cast<u8>(mii_v3.appearance_bits9.mustache_y_position); + + mii.beard_type = static_cast<u8>(mii_v3.appearance_bits9.bear_type); + mii.beard_color = static_cast<u8>(mii_v3.appearance_bits9.facial_hair_color); + + mii.glasses_type = static_cast<u8>(mii_v3.appearance_bits10.glasses_type); + mii.glasses_color = static_cast<u8>(mii_v3.appearance_bits10.glasses_color); + mii.glasses_scale = static_cast<u8>(mii_v3.appearance_bits10.glasses_scale); + mii.glasses_y = static_cast<u8>(mii_v3.appearance_bits10.glasses_y_position); + + mii.mole_type = static_cast<u8>(mii_v3.appearance_bits11.mole_enabled); + mii.mole_scale = static_cast<u8>(mii_v3.appearance_bits11.mole_scale); + mii.mole_x = static_cast<u8>(mii_v3.appearance_bits11.mole_x_position); + mii.mole_y = static_cast<u8>(mii_v3.appearance_bits11.mole_y_position); + + // TODO: Validate mii data + + return mii; +} + ResultVal<std::vector<MiiInfoElement>> MiiManager::GetDefault(SourceFlag source_flag) { std::vector<MiiInfoElement> result; @@ -441,7 +517,7 @@ ResultVal<std::vector<MiiInfoElement>> MiiManager::GetDefault(SourceFlag source_ return result; } -Result MiiManager::GetIndex([[maybe_unused]] const MiiInfo& info, u32& index) { +Result MiiManager::GetIndex([[maybe_unused]] const CharInfo& info, u32& index) { constexpr u32 INVALID_INDEX{0xFFFFFFFF}; index = INVALID_INDEX; diff --git a/src/core/hle/service/mii/mii_manager.h b/src/core/hle/service/mii/mii_manager.h index 6a286bd96..d847de0bd 100644 --- a/src/core/hle/service/mii/mii_manager.h +++ b/src/core/hle/service/mii/mii_manager.h @@ -19,11 +19,12 @@ public: bool CheckAndResetUpdateCounter(SourceFlag source_flag, u64& current_update_counter); bool IsFullDatabase() const; u32 GetCount(SourceFlag source_flag) const; - ResultVal<MiiInfo> UpdateLatest(const MiiInfo& info, SourceFlag source_flag); - MiiInfo BuildRandom(Age age, Gender gender, Race race); - MiiInfo BuildDefault(std::size_t index); + ResultVal<CharInfo> UpdateLatest(const CharInfo& info, SourceFlag source_flag); + CharInfo BuildRandom(Age age, Gender gender, Race race); + CharInfo BuildDefault(std::size_t index); + CharInfo ConvertV3ToCharInfo(Ver3StoreData mii_v3) const; ResultVal<std::vector<MiiInfoElement>> GetDefault(SourceFlag source_flag); - Result GetIndex(const MiiInfo& info, u32& index); + Result GetIndex(const CharInfo& info, u32& index); private: const Common::UUID user_id{}; diff --git a/src/core/hle/service/mii/types.h b/src/core/hle/service/mii/types.h index 45edbfeae..9e3247397 100644 --- a/src/core/hle/service/mii/types.h +++ b/src/core/hle/service/mii/types.h @@ -86,7 +86,8 @@ enum class SourceFlag : u32 { }; DECLARE_ENUM_FLAG_OPERATORS(SourceFlag); -struct MiiInfo { +// nn::mii::CharInfo +struct CharInfo { Common::UUID uuid; std::array<char16_t, 11> name; u8 font_region; @@ -140,16 +141,16 @@ struct MiiInfo { u8 mole_y; u8 padding; }; -static_assert(sizeof(MiiInfo) == 0x58, "MiiInfo has incorrect size."); -static_assert(std::has_unique_object_representations_v<MiiInfo>, - "All bits of MiiInfo must contribute to its value."); +static_assert(sizeof(CharInfo) == 0x58, "CharInfo has incorrect size."); +static_assert(std::has_unique_object_representations_v<CharInfo>, + "All bits of CharInfo must contribute to its value."); #pragma pack(push, 4) struct MiiInfoElement { - MiiInfoElement(const MiiInfo& info_, Source source_) : info{info_}, source{source_} {} + MiiInfoElement(const CharInfo& info_, Source source_) : info{info_}, source{source_} {} - MiiInfo info{}; + CharInfo info{}; Source source{}; }; static_assert(sizeof(MiiInfoElement) == 0x5c, "MiiInfoElement has incorrect size."); @@ -243,6 +244,131 @@ static_assert(sizeof(MiiStoreBitFields) == 0x1c, "MiiStoreBitFields has incorrec static_assert(std::is_trivially_copyable_v<MiiStoreBitFields>, "MiiStoreBitFields is not trivially copyable."); +// This is nn::mii::Ver3StoreData +// Based on citra HLE::Applets::MiiData and PretendoNetwork. +// https://github.com/citra-emu/citra/blob/master/src/core/hle/applets/mii_selector.h#L48 +// https://github.com/PretendoNetwork/mii-js/blob/master/mii.js#L299 +struct Ver3StoreData { + u8 version; + union { + u8 raw; + + BitField<0, 1, u8> allow_copying; + BitField<1, 1, u8> profanity_flag; + BitField<2, 2, u8> region_lock; + BitField<4, 2, u8> character_set; + } region_information; + u16_be mii_id; + u64_be system_id; + u32_be specialness_and_creation_date; + std::array<u8, 0x6> creator_mac; + u16_be padding; + union { + u16 raw; + + BitField<0, 1, u16> gender; + BitField<1, 4, u16> birth_month; + BitField<5, 5, u16> birth_day; + BitField<10, 4, u16> favorite_color; + BitField<14, 1, u16> favorite; + } mii_information; + std::array<char16_t, 0xA> mii_name; + u8 height; + u8 build; + union { + u8 raw; + + BitField<0, 1, u8> disable_sharing; + BitField<1, 4, u8> face_shape; + BitField<5, 3, u8> skin_color; + } appearance_bits1; + union { + u8 raw; + + BitField<0, 4, u8> wrinkles; + BitField<4, 4, u8> makeup; + } appearance_bits2; + u8 hair_style; + union { + u8 raw; + + BitField<0, 3, u8> hair_color; + BitField<3, 1, u8> flip_hair; + } appearance_bits3; + union { + u32 raw; + + BitField<0, 6, u32> eye_type; + BitField<6, 3, u32> eye_color; + BitField<9, 4, u32> eye_scale; + BitField<13, 3, u32> eye_vertical_stretch; + BitField<16, 5, u32> eye_rotation; + BitField<21, 4, u32> eye_spacing; + BitField<25, 5, u32> eye_y_position; + } appearance_bits4; + union { + u32 raw; + + BitField<0, 5, u32> eyebrow_style; + BitField<5, 3, u32> eyebrow_color; + BitField<8, 4, u32> eyebrow_scale; + BitField<12, 3, u32> eyebrow_yscale; + BitField<16, 4, u32> eyebrow_rotation; + BitField<21, 4, u32> eyebrow_spacing; + BitField<25, 5, u32> eyebrow_y_position; + } appearance_bits5; + union { + u16 raw; + + BitField<0, 5, u16> nose_type; + BitField<5, 4, u16> nose_scale; + BitField<9, 5, u16> nose_y_position; + } appearance_bits6; + union { + u16 raw; + + BitField<0, 6, u16> mouth_type; + BitField<6, 3, u16> mouth_color; + BitField<9, 4, u16> mouth_scale; + BitField<13, 3, u16> mouth_horizontal_stretch; + } appearance_bits7; + union { + u8 raw; + + BitField<0, 5, u8> mouth_y_position; + BitField<5, 3, u8> mustache_type; + } appearance_bits8; + u8 allow_copying; + union { + u16 raw; + + BitField<0, 3, u16> bear_type; + BitField<3, 3, u16> facial_hair_color; + BitField<6, 4, u16> mustache_scale; + BitField<10, 5, u16> mustache_y_position; + } appearance_bits9; + union { + u16 raw; + + BitField<0, 4, u16> glasses_type; + BitField<4, 3, u16> glasses_color; + BitField<7, 4, u16> glasses_scale; + BitField<11, 5, u16> glasses_y_position; + } appearance_bits10; + union { + u16 raw; + + BitField<0, 1, u16> mole_enabled; + BitField<1, 4, u16> mole_scale; + BitField<5, 5, u16> mole_x_position; + BitField<10, 5, u16> mole_y_position; + } appearance_bits11; + + std::array<u16_le, 0xA> author_name; + INSERT_PADDING_BYTES(0x4); +}; +static_assert(sizeof(Ver3StoreData) == 0x60, "Ver3StoreData is an invalid size"); + struct MiiStoreData { using Name = std::array<char16_t, 10>; diff --git a/src/core/hle/service/nfp/amiibo_crypto.cpp b/src/core/hle/service/nfp/amiibo_crypto.cpp index 211e518b0..d9d0c8f62 100644 --- a/src/core/hle/service/nfp/amiibo_crypto.cpp +++ b/src/core/hle/service/nfp/amiibo_crypto.cpp @@ -16,76 +16,6 @@ namespace Service::NFP::AmiiboCrypto { -Service::Mii::MiiInfo AmiiboRegisterInfoToMii(const AmiiboRegisterInfo& mii_info) { - - Service::Mii::MiiManager manager; - auto mii = manager.BuildDefault(0); - - // TODO: We are ignoring a bunch of data from the amiibo mii - - mii.gender = static_cast<u8>(mii_info.mii_information.gender); - mii.favorite_color = static_cast<u8>(mii_info.mii_information.favorite_color); - memcpy(mii.name.data(), mii_info.mii_name.data(), 10); - mii.height = mii_info.height; - mii.build = mii_info.build; - - mii.faceline_type = mii_info.appearance_bits1.face_shape; - mii.faceline_color = mii_info.appearance_bits1.skin_color; - mii.faceline_wrinkle = mii_info.appearance_bits2.wrinkles; - mii.faceline_make = mii_info.appearance_bits2.makeup; - - mii.hair_type = mii_info.hair_style; - mii.hair_color = mii_info.appearance_bits3.hair_color; - mii.hair_flip = mii_info.appearance_bits3.flip_hair; - - mii.eye_type = static_cast<u8>(mii_info.appearance_bits4.eye_type); - mii.eye_color = static_cast<u8>(mii_info.appearance_bits4.eye_color); - mii.eye_scale = static_cast<u8>(mii_info.appearance_bits4.eye_scale); - mii.eye_aspect = static_cast<u8>(mii_info.appearance_bits4.eye_vertical_stretch); - mii.eye_rotate = static_cast<u8>(mii_info.appearance_bits4.eye_rotation); - mii.eye_x = static_cast<u8>(mii_info.appearance_bits4.eye_spacing); - mii.eye_y = static_cast<u8>(mii_info.appearance_bits4.eye_y_position); - - mii.eyebrow_type = static_cast<u8>(mii_info.appearance_bits5.eyebrow_style); - mii.eyebrow_color = static_cast<u8>(mii_info.appearance_bits5.eyebrow_color); - mii.eyebrow_scale = static_cast<u8>(mii_info.appearance_bits5.eyebrow_scale); - mii.eyebrow_aspect = static_cast<u8>(mii_info.appearance_bits5.eyebrow_yscale); - mii.eyebrow_rotate = static_cast<u8>(mii_info.appearance_bits5.eyebrow_rotation); - mii.eyebrow_x = static_cast<u8>(mii_info.appearance_bits5.eyebrow_spacing); - mii.eyebrow_y = static_cast<u8>(mii_info.appearance_bits5.eyebrow_y_position); - - mii.nose_type = static_cast<u8>(mii_info.appearance_bits6.nose_type); - mii.nose_scale = static_cast<u8>(mii_info.appearance_bits6.nose_scale); - mii.nose_y = static_cast<u8>(mii_info.appearance_bits6.nose_y_position); - - mii.mouth_type = static_cast<u8>(mii_info.appearance_bits7.mouth_type); - mii.mouth_color = static_cast<u8>(mii_info.appearance_bits7.mouth_color); - mii.mouth_scale = static_cast<u8>(mii_info.appearance_bits7.mouth_scale); - mii.mouth_aspect = static_cast<u8>(mii_info.appearance_bits7.mouth_horizontal_stretch); - mii.mouth_y = static_cast<u8>(mii_info.appearance_bits8.mouth_y_position); - - mii.mustache_type = static_cast<u8>(mii_info.appearance_bits8.mustache_type); - mii.mustache_scale = static_cast<u8>(mii_info.appearance_bits9.mustache_scale); - mii.mustache_y = static_cast<u8>(mii_info.appearance_bits9.mustache_y_position); - - mii.beard_type = static_cast<u8>(mii_info.appearance_bits9.bear_type); - mii.beard_color = static_cast<u8>(mii_info.appearance_bits9.facial_hair_color); - - mii.glasses_type = static_cast<u8>(mii_info.appearance_bits10.glasses_type); - mii.glasses_color = static_cast<u8>(mii_info.appearance_bits10.glasses_color); - mii.glasses_scale = static_cast<u8>(mii_info.appearance_bits10.glasses_scale); - mii.glasses_y = static_cast<u8>(mii_info.appearance_bits10.glasses_y_position); - - mii.mole_type = static_cast<u8>(mii_info.appearance_bits11.mole_enabled); - mii.mole_scale = static_cast<u8>(mii_info.appearance_bits11.mole_scale); - mii.mole_x = static_cast<u8>(mii_info.appearance_bits11.mole_x_position); - mii.mole_y = static_cast<u8>(mii_info.appearance_bits11.mole_y_position); - - // TODO: Validate mii data - - return mii; -} - bool IsAmiiboValid(const EncryptedNTAG215File& ntag_file) { const auto& amiibo_data = ntag_file.user_memory; LOG_DEBUG(Service_NFP, "uuid_lock=0x{0:x}", ntag_file.static_lock); @@ -126,9 +56,8 @@ bool IsAmiiboValid(const EncryptedNTAG215File& ntag_file) { if (amiibo_data.model_info.constant_value != 0x02) { return false; } - if ((ntag_file.dynamic_lock & 0xFFFFFF) != 0x0F0001) { - return false; - } + // dynamic_lock value apparently is not constant + // ntag_file.dynamic_lock == 0x0F0001 if (ntag_file.CFG0 != 0x04000000U) { return false; } @@ -348,16 +277,16 @@ bool LoadKeys(InternalKey& locked_secret, InternalKey& unfixed_info) { Common::FS::FileType::BinaryFile}; if (!keys_file.IsOpen()) { - LOG_ERROR(Core, "No keys detected"); + LOG_ERROR(Service_NFP, "No keys detected"); return false; } if (keys_file.Read(unfixed_info) != 1) { - LOG_ERROR(Core, "Failed to read unfixed_info"); + LOG_ERROR(Service_NFP, "Failed to read unfixed_info"); return false; } if (keys_file.Read(locked_secret) != 1) { - LOG_ERROR(Core, "Failed to read locked-secret"); + LOG_ERROR(Service_NFP, "Failed to read locked-secret"); return false; } diff --git a/src/core/hle/service/nfp/amiibo_crypto.h b/src/core/hle/service/nfp/amiibo_crypto.h index bfba5dcb2..9b021a5eb 100644 --- a/src/core/hle/service/nfp/amiibo_crypto.h +++ b/src/core/hle/service/nfp/amiibo_crypto.h @@ -55,9 +55,6 @@ struct DerivedKeys { }; static_assert(sizeof(DerivedKeys) == 0x30, "DerivedKeys is an invalid size"); -/// Converts mii data from nintendo 3ds format to nintendo switch format -Service::Mii::MiiInfo AmiiboRegisterInfoToMii(const AmiiboRegisterInfo& register_info); - /// Validates that the amiibo file is not corrupted bool IsAmiiboValid(const EncryptedNTAG215File& ntag_file); diff --git a/src/core/hle/service/nfp/amiibo_types.h b/src/core/hle/service/nfp/amiibo_types.h index 49875cff4..bd0424ffd 100644 --- a/src/core/hle/service/nfp/amiibo_types.h +++ b/src/core/hle/service/nfp/amiibo_types.h @@ -5,6 +5,8 @@ #include <array> +#include "core/hle/service/mii/types.h" + namespace Service::NFP { enum class ServiceType : u32 { User, @@ -74,13 +76,17 @@ using HashData = std::array<u8, 0x20>; using ApplicationArea = std::array<u8, 0xD8>; struct AmiiboDate { - union { - u16_be raw{}; - - BitField<0, 5, u16> day; - BitField<5, 4, u16> month; - BitField<9, 7, u16> year; - }; + u16_be raw_date{}; + + u16 GetYear() const { + return ((raw_date & 0xFE00) >> 9) + 2000; + } + u8 GetMonth() const { + return ((raw_date & 0x01E0) >> 5) - 1; + } + u8 GetDay() const { + return raw_date & 0x001F; + } }; static_assert(sizeof(AmiiboDate) == 2, "AmiiboDate is an invalid size"); @@ -123,135 +129,20 @@ struct NTAG215Password { }; static_assert(sizeof(NTAG215Password) == 0x8, "NTAG215Password is an invalid size"); -// Based on citra HLE::Applets::MiiData and PretendoNetwork. -// https://github.com/citra-emu/citra/blob/master/src/core/hle/applets/mii_selector.h#L48 -// https://github.com/PretendoNetwork/mii-js/blob/master/mii.js#L299 #pragma pack(1) -struct AmiiboRegisterInfo { - u32_be mii_id; - u64_be system_id; - u32_be specialness_and_creation_date; - std::array<u8, 0x6> creator_mac; - u16_be padding; - union { - u16 raw; - - BitField<0, 1, u16> gender; - BitField<1, 4, u16> birth_month; - BitField<5, 5, u16> birth_day; - BitField<10, 4, u16> favorite_color; - BitField<14, 1, u16> favorite; - } mii_information; - std::array<char16_t, 0xA> mii_name; - u8 height; - u8 build; - union { - u8 raw; - - BitField<0, 1, u8> disable_sharing; - BitField<1, 4, u8> face_shape; - BitField<5, 3, u8> skin_color; - } appearance_bits1; - union { - u8 raw; - - BitField<0, 4, u8> wrinkles; - BitField<4, 4, u8> makeup; - } appearance_bits2; - u8 hair_style; - union { - u8 raw; - - BitField<0, 3, u8> hair_color; - BitField<3, 1, u8> flip_hair; - } appearance_bits3; - union { - u32 raw; - - BitField<0, 6, u32> eye_type; - BitField<6, 3, u32> eye_color; - BitField<9, 4, u32> eye_scale; - BitField<13, 3, u32> eye_vertical_stretch; - BitField<16, 5, u32> eye_rotation; - BitField<21, 4, u32> eye_spacing; - BitField<25, 5, u32> eye_y_position; - } appearance_bits4; - union { - u32 raw; - - BitField<0, 5, u32> eyebrow_style; - BitField<5, 3, u32> eyebrow_color; - BitField<8, 4, u32> eyebrow_scale; - BitField<12, 3, u32> eyebrow_yscale; - BitField<16, 4, u32> eyebrow_rotation; - BitField<21, 4, u32> eyebrow_spacing; - BitField<25, 5, u32> eyebrow_y_position; - } appearance_bits5; - union { - u16 raw; - - BitField<0, 5, u16> nose_type; - BitField<5, 4, u16> nose_scale; - BitField<9, 5, u16> nose_y_position; - } appearance_bits6; - union { - u16 raw; - - BitField<0, 6, u16> mouth_type; - BitField<6, 3, u16> mouth_color; - BitField<9, 4, u16> mouth_scale; - BitField<13, 3, u16> mouth_horizontal_stretch; - } appearance_bits7; - union { - u8 raw; - - BitField<0, 5, u8> mouth_y_position; - BitField<5, 3, u8> mustache_type; - } appearance_bits8; - u8 allow_copying; - union { - u16 raw; - - BitField<0, 3, u16> bear_type; - BitField<3, 3, u16> facial_hair_color; - BitField<6, 4, u16> mustache_scale; - BitField<10, 5, u16> mustache_y_position; - } appearance_bits9; - union { - u16 raw; - - BitField<0, 4, u16> glasses_type; - BitField<4, 3, u16> glasses_color; - BitField<7, 4, u16> glasses_scale; - BitField<11, 5, u16> glasses_y_position; - } appearance_bits10; - union { - u16 raw; - - BitField<0, 1, u16> mole_enabled; - BitField<1, 4, u16> mole_scale; - BitField<5, 5, u16> mole_x_position; - BitField<10, 5, u16> mole_y_position; - } appearance_bits11; - - std::array<u16_le, 0xA> author_name; - INSERT_PADDING_BYTES(0x4); -}; -static_assert(sizeof(AmiiboRegisterInfo) == 0x60, "AmiiboRegisterInfo is an invalid size"); - struct EncryptedAmiiboFile { - u8 constant_value; // Must be A5 - u16 write_counter; // Number of times the amiibo has been written? - INSERT_PADDING_BYTES(0x1); // Unknown 1 - AmiiboSettings settings; // Encrypted amiibo settings - HashData locked_hash; // Hash - AmiiboModelInfo model_info; // Encrypted amiibo model info - HashData keygen_salt; // Salt - HashData unfixed_hash; // Hash - AmiiboRegisterInfo owner_mii; // Encrypted Mii data - u64_be title_id; // Encrypted Game id - u16_be applicaton_write_counter; // Encrypted Counter - u32_be application_area_id; // Encrypted Game id + u8 constant_value; // Must be A5 + u16 write_counter; // Number of times the amiibo has been written? + INSERT_PADDING_BYTES(0x1); // Unknown 1 + AmiiboSettings settings; // Encrypted amiibo settings + HashData locked_hash; // Hash + AmiiboModelInfo model_info; // Encrypted amiibo model info + HashData keygen_salt; // Salt + HashData unfixed_hash; // Hash + Service::Mii::Ver3StoreData owner_mii; // Encrypted Mii data + u64_be title_id; // Encrypted Game id + u16_be applicaton_write_counter; // Encrypted Counter + u32_be application_area_id; // Encrypted Game id std::array<u8, 0x2> unknown; HashData hash; // Probably a SHA256-HMAC hash? ApplicationArea application_area; // Encrypted Game data @@ -267,7 +158,7 @@ struct NTAG215File { u16 write_counter; // Number of times the amiibo has been written? INSERT_PADDING_BYTES(0x1); // Unknown 1 AmiiboSettings settings; - AmiiboRegisterInfo owner_mii; // Encrypted Mii data + Service::Mii::Ver3StoreData owner_mii; // Encrypted Mii data u64_be title_id; u16_be applicaton_write_counter; // Encrypted Counter u32_be application_area_id; diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index 4dba05a6a..20fea87e6 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -24,6 +24,7 @@ constexpr Result DeviceNotFound(ErrorModule::NFP, 64); constexpr Result WrongDeviceState(ErrorModule::NFP, 73); constexpr Result NfcDisabled(ErrorModule::NFP, 80); constexpr Result WriteAmiiboFailed(ErrorModule::NFP, 88); +constexpr Result TagRemoved(ErrorModule::NFP, 97); constexpr Result ApplicationAreaIsNotInitialized(ErrorModule::NFP, 128); constexpr Result WrongApplicationAreaId(ErrorModule::NFP, 152); constexpr Result ApplicationAreaExist(ErrorModule::NFP, 168); @@ -57,7 +58,7 @@ IUser::IUser(Module::Interface& nfp_interface_, Core::System& system_) {21, &IUser::GetNpadId, "GetNpadId"}, {22, &IUser::GetApplicationAreaSize, "GetApplicationAreaSize"}, {23, &IUser::AttachAvailabilityChangeEvent, "AttachAvailabilityChangeEvent"}, - {24, nullptr, "RecreateApplicationArea"}, + {24, &IUser::RecreateApplicationArea, "RecreateApplicationArea"}, }; RegisterHandlers(functions); @@ -597,6 +598,34 @@ void IUser::AttachAvailabilityChangeEvent(Kernel::HLERequestContext& ctx) { rb.PushCopyObjects(availability_change_event->GetReadableEvent()); } +void IUser::RecreateApplicationArea(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop<u64>()}; + const auto access_id{rp.Pop<u32>()}; + const auto data{ctx.ReadBuffer()}; + LOG_WARNING(Service_NFP, "(STUBBED) called, device_handle={}, data_size={}, access_id={}", + device_handle, access_id, data.size()); + + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + const auto result = nfp_interface.RecreateApplicationArea(access_id, data); + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(result); + return; + } + + LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::DeviceNotFound); +} + Module::Interface::Interface(std::shared_ptr<Module> module_, Core::System& system_, const char* name) : ServiceFramework{system_, name}, module{std::move(module_)}, @@ -621,14 +650,14 @@ bool Module::Interface::LoadAmiiboFile(const std::string& filename) { Common::FS::FileType::BinaryFile}; if (!amiibo_file.IsOpen()) { - LOG_ERROR(Core, "Amiibo is already on use"); + LOG_ERROR(Service_NFP, "Amiibo is already on use"); return false; } // Workaround for files with missing password data std::array<u8, sizeof(EncryptedNTAG215File)> buffer{}; if (amiibo_file.Read(buffer) < tag_size_without_password) { - LOG_ERROR(Core, "Failed to read amiibo file"); + LOG_ERROR(Service_NFP, "Failed to read amiibo file"); return false; } memcpy(&encrypted_tag_data, buffer.data(), sizeof(EncryptedNTAG215File)); @@ -759,12 +788,12 @@ Result Module::Interface::Flush() { bool is_character_equal = tmp_encrypted_tag_data.user_memory.model_info.character_id == tag_data.model_info.character_id; if (!is_uuid_equal || !is_character_equal) { - LOG_ERROR(Core, "Not the same amiibo"); + LOG_ERROR(Service_NFP, "Not the same amiibo"); return ErrCodes::WriteAmiiboFailed; } if (!AmiiboCrypto::EncodeAmiibo(tag_data, encrypted_tag_data)) { - LOG_ERROR(Core, "Failed to encode data"); + LOG_ERROR(Service_NFP, "Failed to encode data"); return ErrCodes::WriteAmiiboFailed; } @@ -830,13 +859,13 @@ Result Module::Interface::GetCommonInfo(CommonInfo& common_info) const { return ErrCodes::WrongDeviceState; } - if (is_data_decoded) { + if (is_data_decoded && tag_data.settings.settings.amiibo_initialized != 0) { const auto& settings = tag_data.settings; // TODO: Validate this data common_info = { - .last_write_year = static_cast<u16>(settings.write_date.year.Value()), - .last_write_month = static_cast<u8>(settings.write_date.month.Value()), - .last_write_day = static_cast<u8>(settings.write_date.day.Value()), + .last_write_year = settings.write_date.GetYear(), + .last_write_month = settings.write_date.GetMonth(), + .last_write_day = settings.write_date.GetDay(), .write_counter = settings.crc_counter, .version = 1, .application_area_size = sizeof(ApplicationArea), @@ -877,10 +906,15 @@ Result Module::Interface::GetModelInfo(ModelInfo& model_info) const { Result Module::Interface::GetRegisterInfo(RegisterInfo& register_info) const { if (device_state != DeviceState::TagMounted) { LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + if (device_state == DeviceState::TagRemoved) { + return ErrCodes::TagRemoved; + } return ErrCodes::WrongDeviceState; } - if (is_data_decoded) { + Service::Mii::MiiManager manager; + + if (is_data_decoded && tag_data.settings.settings.amiibo_initialized != 0) { const auto& settings = tag_data.settings; // Amiibo name is u16 while the register info is u8. Figure out how to handle this properly @@ -891,10 +925,10 @@ Result Module::Interface::GetRegisterInfo(RegisterInfo& register_info) const { // TODO: Validate this data register_info = { - .mii_char_info = AmiiboCrypto::AmiiboRegisterInfoToMii(tag_data.owner_mii), - .first_write_year = static_cast<u16>(settings.init_date.year.Value()), - .first_write_month = static_cast<u8>(settings.init_date.month.Value()), - .first_write_day = static_cast<u8>(settings.init_date.day.Value()), + .mii_char_info = manager.ConvertV3ToCharInfo(tag_data.owner_mii), + .first_write_year = settings.init_date.GetYear(), + .first_write_month = settings.init_date.GetMonth(), + .first_write_day = settings.init_date.GetDay(), .amiibo_name = amiibo_name, .unknown = {}, }; @@ -903,7 +937,6 @@ Result Module::Interface::GetRegisterInfo(RegisterInfo& register_info) const { } // Generate a generic answer - Service::Mii::MiiManager manager; register_info = { .mii_char_info = manager.BuildDefault(0), .first_write_year = 2022, @@ -918,6 +951,9 @@ Result Module::Interface::GetRegisterInfo(RegisterInfo& register_info) const { Result Module::Interface::OpenApplicationArea(u32 access_id) { if (device_state != DeviceState::TagMounted) { LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + if (device_state == DeviceState::TagRemoved) { + return ErrCodes::TagRemoved; + } return ErrCodes::WrongDeviceState; } @@ -944,6 +980,9 @@ Result Module::Interface::OpenApplicationArea(u32 access_id) { Result Module::Interface::GetApplicationArea(ApplicationArea& data) const { if (device_state != DeviceState::TagMounted) { LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + if (device_state == DeviceState::TagRemoved) { + return ErrCodes::TagRemoved; + } return ErrCodes::WrongDeviceState; } @@ -960,6 +999,9 @@ Result Module::Interface::GetApplicationArea(ApplicationArea& data) const { Result Module::Interface::SetApplicationArea(const std::vector<u8>& data) { if (device_state != DeviceState::TagMounted) { LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + if (device_state == DeviceState::TagRemoved) { + return ErrCodes::TagRemoved; + } return ErrCodes::WrongDeviceState; } @@ -980,6 +1022,9 @@ Result Module::Interface::SetApplicationArea(const std::vector<u8>& data) { Result Module::Interface::CreateApplicationArea(u32 access_id, const std::vector<u8>& data) { if (device_state != DeviceState::TagMounted) { LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + if (device_state == DeviceState::TagRemoved) { + return ErrCodes::TagRemoved; + } return ErrCodes::WrongDeviceState; } @@ -999,6 +1044,26 @@ Result Module::Interface::CreateApplicationArea(u32 access_id, const std::vector return ResultSuccess; } +Result Module::Interface::RecreateApplicationArea(u32 access_id, const std::vector<u8>& data) { + if (device_state != DeviceState::TagMounted) { + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + if (device_state == DeviceState::TagRemoved) { + return ErrCodes::TagRemoved; + } + return ErrCodes::WrongDeviceState; + } + + if (data.size() != sizeof(ApplicationArea)) { + LOG_ERROR(Service_NFP, "Wrong data size {}", data.size()); + return ResultUnknown; + } + + std::memcpy(&tag_data.application_area, data.data(), sizeof(ApplicationArea)); + tag_data.application_area_id = access_id; + + return ResultSuccess; +} + u64 Module::Interface::GetHandle() const { // Generate a handle based of the npad id return static_cast<u64>(npad_id); diff --git a/src/core/hle/service/nfp/nfp.h b/src/core/hle/service/nfp/nfp.h index 3410dcfb0..6b979daba 100644 --- a/src/core/hle/service/nfp/nfp.h +++ b/src/core/hle/service/nfp/nfp.h @@ -55,7 +55,7 @@ struct ModelInfo { static_assert(sizeof(ModelInfo) == 0x40, "ModelInfo is an invalid size"); struct RegisterInfo { - Service::Mii::MiiInfo mii_char_info; + Service::Mii::CharInfo mii_char_info; u16 first_write_year; u8 first_write_month; u8 first_write_day; @@ -96,6 +96,7 @@ public: Result GetApplicationArea(ApplicationArea& data) const; Result SetApplicationArea(const std::vector<u8>& data); Result CreateApplicationArea(u32 access_id, const std::vector<u8>& data); + Result RecreateApplicationArea(u32 access_id, const std::vector<u8>& data); u64 GetHandle() const; DeviceState GetCurrentState() const; @@ -152,6 +153,7 @@ private: void GetNpadId(Kernel::HLERequestContext& ctx); void GetApplicationAreaSize(Kernel::HLERequestContext& ctx); void AttachAvailabilityChangeEvent(Kernel::HLERequestContext& ctx); + void RecreateApplicationArea(Kernel::HLERequestContext& ctx); KernelHelpers::ServiceContext service_context; |