From 3d24eb54ec8303c093dbd34b766c5e420fafcf83 Mon Sep 17 00:00:00 2001 From: german77 Date: Tue, 15 Jun 2021 23:58:08 -0500 Subject: [PATCH 1/8] nfp: Sort functions by command number --- src/core/hle/service/nfp/nfp.cpp | 188 +++++++++++++++---------------- 1 file changed, 94 insertions(+), 94 deletions(-) diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index 761d0d3c6..e8d3a6b53 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -120,12 +120,13 @@ private: state = State::Initialized; } - void GetState(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFC, "called"); + void Finalize(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFP, "called"); - IPC::ResponseBuilder rb{ctx, 3, 0}; + device_state = DeviceState::Finalized; + + IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ResultSuccess); - rb.PushRaw(static_cast(state)); } void ListDevices(Kernel::HLERequestContext& ctx) { @@ -140,35 +141,14 @@ private: rb.Push(1); } - void GetNpadId(Kernel::HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - const u64 dev_handle = rp.Pop(); - LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); + void StartDetection(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFP, "called"); - IPC::ResponseBuilder rb{ctx, 3}; + if (device_state == DeviceState::Initialized || device_state == DeviceState::TagRemoved) { + device_state = DeviceState::SearchingForTag; + } + IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ResultSuccess); - rb.Push(npad_id); - } - - void AttachActivateEvent(Kernel::HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - const u64 dev_handle = rp.Pop(); - LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); - - IPC::ResponseBuilder rb{ctx, 2, 1}; - rb.Push(ResultSuccess); - rb.PushCopyObjects(nfp_interface.GetNFCEvent()); - has_attached_handle = true; - } - - void AttachDeactivateEvent(Kernel::HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - const u64 dev_handle = rp.Pop(); - LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); - - IPC::ResponseBuilder rb{ctx, 2, 1}; - rb.Push(ResultSuccess); - rb.PushCopyObjects(deactivate_event->GetReadableEvent()); } void StopDetection(Kernel::HLERequestContext& ctx) { @@ -191,22 +171,37 @@ private: rb.Push(ResultSuccess); } - void GetDeviceState(Kernel::HLERequestContext& ctx) { + void Mount(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_NFP, "called"); + device_state = DeviceState::TagNearby; + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); + } + + void Unmount(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFP, "called"); + + device_state = DeviceState::TagFound; + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); + } + + void OpenApplicationArea(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_NFP, "(STUBBED) called"); + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::ERR_NO_APPLICATION_AREA); + } + + void GetApplicationArea(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_NFP, "(STUBBED) called"); + + // TODO(ogniK): Pull application area from amiibo + IPC::ResponseBuilder rb{ctx, 3}; rb.Push(ResultSuccess); - rb.Push(static_cast(device_state)); - } - - void StartDetection(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); - - if (device_state == DeviceState::Initialized || device_state == DeviceState::TagRemoved) { - device_state = DeviceState::SearchingForTag; - } - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); + rb.PushRaw(0); // This is from the GetCommonInfo stub } void GetTagInfo(Kernel::HLERequestContext& ctx) { @@ -226,49 +221,6 @@ private: rb.Push(ResultSuccess); } - void Mount(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); - - device_state = DeviceState::TagNearby; - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); - } - - void GetModelInfo(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); - - IPC::ResponseBuilder rb{ctx, 2}; - const auto& amiibo = nfp_interface.GetAmiiboBuffer(); - ctx.WriteBuffer(amiibo.model_info); - rb.Push(ResultSuccess); - } - - void Unmount(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); - - device_state = DeviceState::TagFound; - - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); - } - - void Finalize(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); - - device_state = DeviceState::Finalized; - - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); - } - - void AttachAvailabilityChangeEvent(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NFP, "(STUBBED) called"); - - IPC::ResponseBuilder rb{ctx, 2, 1}; - rb.Push(ResultSuccess); - rb.PushCopyObjects(availability_change_event->GetReadableEvent()); - } - void GetRegisterInfo(Kernel::HLERequestContext& ctx) { LOG_WARNING(Service_NFP, "(STUBBED) called"); @@ -291,10 +243,60 @@ private: rb.Push(ResultSuccess); } - void OpenApplicationArea(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NFP, "(STUBBED) called"); + void GetModelInfo(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFP, "called"); + IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ErrCodes::ERR_NO_APPLICATION_AREA); + const auto& amiibo = nfp_interface.GetAmiiboBuffer(); + ctx.WriteBuffer(amiibo.model_info); + rb.Push(ResultSuccess); + } + + void AttachActivateEvent(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + const u64 dev_handle = rp.Pop(); + LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); + + IPC::ResponseBuilder rb{ctx, 2, 1}; + rb.Push(ResultSuccess); + rb.PushCopyObjects(nfp_interface.GetNFCEvent()); + has_attached_handle = true; + } + + void AttachDeactivateEvent(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + const u64 dev_handle = rp.Pop(); + LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); + + IPC::ResponseBuilder rb{ctx, 2, 1}; + rb.Push(ResultSuccess); + rb.PushCopyObjects(deactivate_event.GetReadableEvent()); + } + + void GetState(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFC, "called"); + + IPC::ResponseBuilder rb{ctx, 3, 0}; + rb.Push(ResultSuccess); + rb.PushRaw(static_cast(state)); + } + + void GetDeviceState(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFP, "called"); + + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(ResultSuccess); + rb.Push(static_cast(device_state)); + } + + void GetNpadId(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + const u64 dev_handle = rp.Pop(); + LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); + + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(ResultSuccess); + rb.Push(npad_id); } void GetApplicationAreaSize(Kernel::HLERequestContext& ctx) { @@ -305,14 +307,12 @@ private: rb.PushRaw(0); // This is from the GetCommonInfo stub } - void GetApplicationArea(Kernel::HLERequestContext& ctx) { + void AttachAvailabilityChangeEvent(Kernel::HLERequestContext& ctx) { LOG_WARNING(Service_NFP, "(STUBBED) called"); - // TODO(ogniK): Pull application area from amiibo - - IPC::ResponseBuilder rb{ctx, 3}; + IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(ResultSuccess); - rb.PushRaw(0); // This is from the GetCommonInfo stub + rb.PushCopyObjects(availability_change_event.GetReadableEvent()); } Module::Interface& nfp_interface; From c001a2af2519d69137b4cb7584daba84964c836c Mon Sep 17 00:00:00 2001 From: german77 Date: Wed, 16 Jun 2021 01:03:56 -0500 Subject: [PATCH 2/8] nfp: Move IUser class to header and add missing enum and structs --- src/core/hle/service/nfp/nfp.cpp | 550 ++++++++++++++----------------- src/core/hle/service/nfp/nfp.h | 102 ++++++ 2 files changed, 347 insertions(+), 305 deletions(-) diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index e8d3a6b53..cedade5c2 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -17,315 +17,255 @@ namespace ErrCodes { constexpr ResultCode ERR_NO_APPLICATION_AREA(ErrorModule::NFP, 152); } // namespace ErrCodes +IUser::IUser(Module::Interface& nfp_interface_, Core::System& system_) + : ServiceFramework{system_, "NFP::IUser"}, nfp_interface{nfp_interface_}, + deactivate_event{system.Kernel()}, availability_change_event{system.Kernel()} { + static const FunctionInfo functions[] = { + {0, &IUser::Initialize, "Initialize"}, + {1, &IUser::Finalize, "Finalize"}, + {2, &IUser::ListDevices, "ListDevices"}, + {3, &IUser::StartDetection, "StartDetection"}, + {4, &IUser::StopDetection, "StopDetection"}, + {5, &IUser::Mount, "Mount"}, + {6, &IUser::Unmount, "Unmount"}, + {7, &IUser::OpenApplicationArea, "OpenApplicationArea"}, + {8, &IUser::GetApplicationArea, "GetApplicationArea"}, + {9, nullptr, "SetApplicationArea"}, + {10, nullptr, "Flush"}, + {11, nullptr, "Restore"}, + {12, nullptr, "CreateApplicationArea"}, + {13, &IUser::GetTagInfo, "GetTagInfo"}, + {14, &IUser::GetRegisterInfo, "GetRegisterInfo"}, + {15, &IUser::GetCommonInfo, "GetCommonInfo"}, + {16, &IUser::GetModelInfo, "GetModelInfo"}, + {17, &IUser::AttachActivateEvent, "AttachActivateEvent"}, + {18, &IUser::AttachDeactivateEvent, "AttachDeactivateEvent"}, + {19, &IUser::GetState, "GetState"}, + {20, &IUser::GetDeviceState, "GetDeviceState"}, + {21, &IUser::GetNpadId, "GetNpadId"}, + {22, &IUser::GetApplicationAreaSize, "GetApplicationAreaSize"}, + {23, &IUser::AttachAvailabilityChangeEvent, "AttachAvailabilityChangeEvent"}, + {24, nullptr, "RecreateApplicationArea"}, + }; + RegisterHandlers(functions); + + Kernel::KAutoObject::Create(std::addressof(deactivate_event)); + Kernel::KAutoObject::Create(std::addressof(availability_change_event)); + + deactivate_event.Initialize("IUser:DeactivateEvent"); + availability_change_event.Initialize("IUser:AvailabilityChangeEvent"); +} + +void IUser::Initialize(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFC, "called"); + + IPC::ResponseBuilder rb{ctx, 2, 0}; + rb.Push(ResultSuccess); + + state = State::Initialized; +} + +void IUser::Finalize(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFP, "called"); + + device_state = DeviceState::Finalized; + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); +} + +void IUser::ListDevices(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + const u32 array_size = rp.Pop(); + LOG_DEBUG(Service_NFP, "called, array_size={}", array_size); + + ctx.WriteBuffer(device_handle); + + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(ResultSuccess); + rb.Push(1); +} + +void IUser::StartDetection(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFP, "called"); + + if (device_state == DeviceState::Initialized || device_state == DeviceState::TagRemoved) { + device_state = DeviceState::SearchingForTag; + } + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); +} + +void IUser::StopDetection(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFP, "called"); + + switch (device_state) { + case DeviceState::TagFound: + case DeviceState::TagMounted: + deactivate_event.GetWritableEvent().Signal(); + device_state = DeviceState::Initialized; + break; + case DeviceState::SearchingForTag: + case DeviceState::TagRemoved: + device_state = DeviceState::Initialized; + break; + default: + break; + } + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); +} + +void IUser::Mount(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFP, "called"); + + device_state = DeviceState::TagMounted; + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); +} + +void IUser::Unmount(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFP, "called"); + + device_state = DeviceState::TagFound; + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); +} + +void IUser::OpenApplicationArea(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_NFP, "(STUBBED) called"); + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::ERR_NO_APPLICATION_AREA); +} + +void IUser::GetApplicationArea(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_NFP, "(STUBBED) called"); + + // TODO(ogniK): Pull application area from amiibo + + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(ResultSuccess); + rb.PushRaw(0); // This is from the GetCommonInfo stub +} + +void IUser::GetTagInfo(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFP, "called"); + + IPC::ResponseBuilder rb{ctx, 2}; + const auto& amiibo = nfp_interface.GetAmiiboBuffer(); + const TagInfo tag_info{ + .uuid = amiibo.uuid, + .uuid_length = static_cast(amiibo.uuid.size()), + .protocol = 1, // TODO(ogniK): Figure out actual values + .tag_type = 2, + }; + ctx.WriteBuffer(tag_info); + rb.Push(ResultSuccess); +} + +void IUser::GetRegisterInfo(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_NFP, "(STUBBED) called"); + + // TODO(ogniK): Pull Mii and owner data from amiibo + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); +} + +void IUser::GetCommonInfo(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_NFP, "(STUBBED) called"); + + // TODO(ogniK): Pull common information from amiibo + + CommonInfo common_info{}; + common_info.application_area_size = 0; + ctx.WriteBuffer(common_info); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); +} + +void IUser::GetModelInfo(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFP, "called"); + + IPC::ResponseBuilder rb{ctx, 2}; + const auto& amiibo = nfp_interface.GetAmiiboBuffer(); + ctx.WriteBuffer(amiibo.model_info); + rb.Push(ResultSuccess); +} + +void IUser::AttachActivateEvent(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + const u64 dev_handle = rp.Pop(); + LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); + + IPC::ResponseBuilder rb{ctx, 2, 1}; + rb.Push(ResultSuccess); + rb.PushCopyObjects(nfp_interface.GetNFCEvent()); + has_attached_handle = true; +} + +void IUser::AttachDeactivateEvent(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + const u64 dev_handle = rp.Pop(); + LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); + + IPC::ResponseBuilder rb{ctx, 2, 1}; + rb.Push(ResultSuccess); + rb.PushCopyObjects(deactivate_event.GetReadableEvent()); +} + +void IUser::GetState(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFC, "called"); + + IPC::ResponseBuilder rb{ctx, 3, 0}; + rb.Push(ResultSuccess); + rb.PushRaw(static_cast(state)); +} + +void IUser::GetDeviceState(Kernel::HLERequestContext& ctx) { + LOG_DEBUG(Service_NFP, "called"); + + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(ResultSuccess); + rb.Push(static_cast(device_state)); +} + +void IUser::GetNpadId(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + const u64 dev_handle = rp.Pop(); + LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); + + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(ResultSuccess); + rb.Push(npad_id); +} + +void IUser::GetApplicationAreaSize(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_NFP, "(STUBBED) called"); + // We don't need to worry about this since we can just open the file + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(ResultSuccess); + rb.PushRaw(0); // This is from the GetCommonInfo stub +} + +void IUser::AttachAvailabilityChangeEvent(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_NFP, "(STUBBED) called"); + + IPC::ResponseBuilder rb{ctx, 2, 1}; + rb.Push(ResultSuccess); + rb.PushCopyObjects(availability_change_event.GetReadableEvent()); +} + Module::Interface::Interface(std::shared_ptr module_, Core::System& system_, const char* name) - : ServiceFramework{system_, name}, module{std::move(module_)}, service_context{system_, - "NFP::IUser"} { - nfc_tag_load = service_context.CreateEvent("NFP::IUser:NFCTagDetected"); + : ServiceFramework{system_, name}, nfc_tag_load{system.Kernel()}, module{std::move(module_)} { + Kernel::KAutoObject::Create(std::addressof(nfc_tag_load)); + nfc_tag_load.Initialize("IUser:NFCTagDetected"); } -Module::Interface::~Interface() { - service_context.CloseEvent(nfc_tag_load); -} - -class IUser final : public ServiceFramework { -public: - explicit IUser(Module::Interface& nfp_interface_, Core::System& system_, - KernelHelpers::ServiceContext& service_context_) - : ServiceFramework{system_, "NFP::IUser"}, nfp_interface{nfp_interface_}, - service_context{service_context_} { - static const FunctionInfo functions[] = { - {0, &IUser::Initialize, "Initialize"}, - {1, &IUser::Finalize, "Finalize"}, - {2, &IUser::ListDevices, "ListDevices"}, - {3, &IUser::StartDetection, "StartDetection"}, - {4, &IUser::StopDetection, "StopDetection"}, - {5, &IUser::Mount, "Mount"}, - {6, &IUser::Unmount, "Unmount"}, - {7, &IUser::OpenApplicationArea, "OpenApplicationArea"}, - {8, &IUser::GetApplicationArea, "GetApplicationArea"}, - {9, nullptr, "SetApplicationArea"}, - {10, nullptr, "Flush"}, - {11, nullptr, "Restore"}, - {12, nullptr, "CreateApplicationArea"}, - {13, &IUser::GetTagInfo, "GetTagInfo"}, - {14, &IUser::GetRegisterInfo, "GetRegisterInfo"}, - {15, &IUser::GetCommonInfo, "GetCommonInfo"}, - {16, &IUser::GetModelInfo, "GetModelInfo"}, - {17, &IUser::AttachActivateEvent, "AttachActivateEvent"}, - {18, &IUser::AttachDeactivateEvent, "AttachDeactivateEvent"}, - {19, &IUser::GetState, "GetState"}, - {20, &IUser::GetDeviceState, "GetDeviceState"}, - {21, &IUser::GetNpadId, "GetNpadId"}, - {22, &IUser::GetApplicationAreaSize, "GetApplicationAreaSize"}, - {23, &IUser::AttachAvailabilityChangeEvent, "AttachAvailabilityChangeEvent"}, - {24, nullptr, "RecreateApplicationArea"}, - }; - RegisterHandlers(functions); - - deactivate_event = service_context.CreateEvent("NFP::IUser:DeactivateEvent"); - availability_change_event = - service_context.CreateEvent("NFP::IUser:AvailabilityChangeEvent"); - } - - ~IUser() override { - service_context.CloseEvent(deactivate_event); - service_context.CloseEvent(availability_change_event); - } - -private: - struct TagInfo { - std::array uuid; - u8 uuid_length; // TODO(ogniK): Figure out if this is actual the uuid length or does it - // mean something else - std::array padding_1; - u32_le protocol; - u32_le tag_type; - std::array padding_2; - }; - static_assert(sizeof(TagInfo) == 0x54, "TagInfo is an invalid size"); - - enum class State : u32 { - NonInitialized = 0, - Initialized = 1, - }; - - enum class DeviceState : u32 { - Initialized = 0, - SearchingForTag = 1, - TagFound = 2, - TagRemoved = 3, - TagNearby = 4, - Unknown5 = 5, - Finalized = 6 - }; - - struct CommonInfo { - u16_be last_write_year; - u8 last_write_month; - u8 last_write_day; - u16_be write_counter; - u16_be version; - u32_be application_area_size; - INSERT_PADDING_BYTES(0x34); - }; - static_assert(sizeof(CommonInfo) == 0x40, "CommonInfo is an invalid size"); - - void Initialize(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFC, "called"); - - IPC::ResponseBuilder rb{ctx, 2, 0}; - rb.Push(ResultSuccess); - - state = State::Initialized; - } - - void Finalize(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); - - device_state = DeviceState::Finalized; - - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); - } - - void ListDevices(Kernel::HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - const u32 array_size = rp.Pop(); - LOG_DEBUG(Service_NFP, "called, array_size={}", array_size); - - ctx.WriteBuffer(device_handle); - - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.Push(1); - } - - void StartDetection(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); - - if (device_state == DeviceState::Initialized || device_state == DeviceState::TagRemoved) { - device_state = DeviceState::SearchingForTag; - } - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); - } - - void StopDetection(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); - - switch (device_state) { - case DeviceState::TagFound: - case DeviceState::TagNearby: - deactivate_event->GetWritableEvent().Signal(); - device_state = DeviceState::Initialized; - break; - case DeviceState::SearchingForTag: - case DeviceState::TagRemoved: - device_state = DeviceState::Initialized; - break; - default: - break; - } - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); - } - - void Mount(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); - - device_state = DeviceState::TagNearby; - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); - } - - void Unmount(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); - - device_state = DeviceState::TagFound; - - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); - } - - void OpenApplicationArea(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NFP, "(STUBBED) called"); - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ErrCodes::ERR_NO_APPLICATION_AREA); - } - - void GetApplicationArea(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NFP, "(STUBBED) called"); - - // TODO(ogniK): Pull application area from amiibo - - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.PushRaw(0); // This is from the GetCommonInfo stub - } - - void GetTagInfo(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); - - IPC::ResponseBuilder rb{ctx, 2}; - const auto& amiibo = nfp_interface.GetAmiiboBuffer(); - const TagInfo tag_info{ - .uuid = amiibo.uuid, - .uuid_length = static_cast(amiibo.uuid.size()), - .padding_1 = {}, - .protocol = 1, // TODO(ogniK): Figure out actual values - .tag_type = 2, - .padding_2 = {}, - }; - ctx.WriteBuffer(tag_info); - rb.Push(ResultSuccess); - } - - void GetRegisterInfo(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NFP, "(STUBBED) called"); - - // TODO(ogniK): Pull Mii and owner data from amiibo - - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); - } - - void GetCommonInfo(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NFP, "(STUBBED) called"); - - // TODO(ogniK): Pull common information from amiibo - - CommonInfo common_info{}; - common_info.application_area_size = 0; - ctx.WriteBuffer(common_info); - - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); - } - - void GetModelInfo(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); - - IPC::ResponseBuilder rb{ctx, 2}; - const auto& amiibo = nfp_interface.GetAmiiboBuffer(); - ctx.WriteBuffer(amiibo.model_info); - rb.Push(ResultSuccess); - } - - void AttachActivateEvent(Kernel::HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - const u64 dev_handle = rp.Pop(); - LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); - - IPC::ResponseBuilder rb{ctx, 2, 1}; - rb.Push(ResultSuccess); - rb.PushCopyObjects(nfp_interface.GetNFCEvent()); - has_attached_handle = true; - } - - void AttachDeactivateEvent(Kernel::HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - const u64 dev_handle = rp.Pop(); - LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); - - IPC::ResponseBuilder rb{ctx, 2, 1}; - rb.Push(ResultSuccess); - rb.PushCopyObjects(deactivate_event.GetReadableEvent()); - } - - void GetState(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFC, "called"); - - IPC::ResponseBuilder rb{ctx, 3, 0}; - rb.Push(ResultSuccess); - rb.PushRaw(static_cast(state)); - } - - void GetDeviceState(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); - - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.Push(static_cast(device_state)); - } - - void GetNpadId(Kernel::HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - const u64 dev_handle = rp.Pop(); - LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); - - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.Push(npad_id); - } - - void GetApplicationAreaSize(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NFP, "(STUBBED) called"); - // We don't need to worry about this since we can just open the file - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.PushRaw(0); // This is from the GetCommonInfo stub - } - - void AttachAvailabilityChangeEvent(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NFP, "(STUBBED) called"); - - IPC::ResponseBuilder rb{ctx, 2, 1}; - rb.Push(ResultSuccess); - rb.PushCopyObjects(availability_change_event.GetReadableEvent()); - } - - Module::Interface& nfp_interface; - KernelHelpers::ServiceContext& service_context; - - bool has_attached_handle{}; - const u64 device_handle{0}; // Npad device 1 - const u32 npad_id{0}; // Player 1 controller - State state{State::NonInitialized}; - DeviceState device_state{DeviceState::Initialized}; - Kernel::KEvent* deactivate_event; - Kernel::KEvent* availability_change_event; -}; +Module::Interface::~Interface() = default; void Module::Interface::CreateUserInterface(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_NFP, "called"); diff --git a/src/core/hle/service/nfp/nfp.h b/src/core/hle/service/nfp/nfp.h index 95c127efb..8d9db880a 100644 --- a/src/core/hle/service/nfp/nfp.h +++ b/src/core/hle/service/nfp/nfp.h @@ -8,6 +8,8 @@ #include #include "core/hle/service/kernel_helpers.h" +#include "core/hle/kernel/k_event.h" +#include "core/hle/service/mii/manager.h" #include "core/hle/service/service.h" namespace Kernel { @@ -16,6 +18,106 @@ class KEvent; namespace Service::NFP { +enum class ServiceType : u32 { + User = 0, + Debug = 1, + System = 2, +}; + +enum class State : u32 { + NonInitialized = 0, + Initialized = 1, +}; + +enum class DeviceState : u32 { + Initialized = 0, + SearchingForTag = 1, + TagFound = 2, + TagRemoved = 3, + TagMounted = 4, + Unaviable = 5, + Finalized = 6 +}; + +enum class MountTarget : u32 { + Rom = 1, + Ram = 2, + All = 3, +}; + +struct TagInfo { + std::array uuid; + u8 uuid_length; + INSERT_PADDING_BYTES(0x15); + u32_le protocol; + u32_le tag_type; + INSERT_PADDING_BYTES(0x2c); +}; +static_assert(sizeof(TagInfo) == 0x54, "TagInfo is an invalid size"); + +struct CommonInfo { + u16_be last_write_year; + u8 last_write_month; + u8 last_write_day; + u16_be write_counter; + u16_be version; + u32_be application_area_size; + INSERT_PADDING_BYTES(0x34); +}; +static_assert(sizeof(CommonInfo) == 0x40, "CommonInfo is an invalid size"); + +struct ModelInfo { + std::array ammibo_id; + INSERT_PADDING_BYTES(0x38); +}; +static_assert(sizeof(ModelInfo) == 0x40, "ModelInfo is an invalid size"); + +struct RegisterInfo { + Service::Mii::MiiInfo mii; + u16_be first_write_year; + u8 first_write_month; + u8 first_write_day; + std::array amiibo_name; + INSERT_PADDING_BYTES(0x99); +}; +// static_assert(sizeof(RegisterInfo) == 0x106, "RegisterInfo is an invalid size"); + +class IUser final : public ServiceFramework { +public: + explicit IUser(Module::Interface& nfp_interface_, Core::System& system_); + +private: + void Initialize(Kernel::HLERequestContext& ctx); + void Finalize(Kernel::HLERequestContext& ctx); + void ListDevices(Kernel::HLERequestContext& ctx); + void StartDetection(Kernel::HLERequestContext& ctx); + void StopDetection(Kernel::HLERequestContext& ctx); + void Mount(Kernel::HLERequestContext& ctx); + void Unmount(Kernel::HLERequestContext& ctx); + void OpenApplicationArea(Kernel::HLERequestContext& ctx); + void GetApplicationArea(Kernel::HLERequestContext& ctx); + void GetTagInfo(Kernel::HLERequestContext& ctx); + void GetRegisterInfo(Kernel::HLERequestContext& ctx); + void GetCommonInfo(Kernel::HLERequestContext& ctx); + void GetModelInfo(Kernel::HLERequestContext& ctx); + void AttachActivateEvent(Kernel::HLERequestContext& ctx); + void AttachDeactivateEvent(Kernel::HLERequestContext& ctx); + void GetState(Kernel::HLERequestContext& ctx); + void GetDeviceState(Kernel::HLERequestContext& ctx); + void GetNpadId(Kernel::HLERequestContext& ctx); + void GetApplicationAreaSize(Kernel::HLERequestContext& ctx); + void AttachAvailabilityChangeEvent(Kernel::HLERequestContext& ctx); + + bool has_attached_handle{}; + const u64 device_handle{0}; // Npad device 1 + const u32 npad_id{0}; // Player 1 controller + State state{State::NonInitialized}; + DeviceState device_state{DeviceState::Initialized}; + Module::Interface& nfp_interface; + Kernel::KEvent deactivate_event; + Kernel::KEvent availability_change_event; +}; + class Module final { public: class Interface : public ServiceFramework { From fc9abd3c625c3eb8b7cc9445eef1291744ee52f6 Mon Sep 17 00:00:00 2001 From: german77 Date: Wed, 16 Jun 2021 22:09:38 -0500 Subject: [PATCH 3/8] nfp: Improve implementation --- src/core/hid/emulated_controller.cpp | 6 + src/core/hid/emulated_controller.h | 2 + src/core/hle/service/nfp/nfp.cpp | 658 ++++++++++++++++++++++----- src/core/hle/service/nfp/nfp.h | 189 +++++--- 4 files changed, 669 insertions(+), 186 deletions(-) diff --git a/src/core/hid/emulated_controller.cpp b/src/core/hid/emulated_controller.cpp index a7cdf45e6..61ceea629 100644 --- a/src/core/hid/emulated_controller.cpp +++ b/src/core/hid/emulated_controller.cpp @@ -884,6 +884,12 @@ bool EmulatedController::TestVibration(std::size_t device_index) { return SetVibration(device_index, DEFAULT_VIBRATION_VALUE); } +bool EmulatedController::SetPollingMode(Common::Input::PollingMode polling_mode) { + LOG_INFO(Service_HID, "Set polling mode {}", polling_mode); + auto& output_device = output_devices[static_cast(DeviceIndex::Right)]; + return output_device->SetPollingMode(polling_mode) == Common::Input::PollingError::None; +} + void EmulatedController::SetLedPattern() { for (auto& device : output_devices) { if (!device) { diff --git a/src/core/hid/emulated_controller.h b/src/core/hid/emulated_controller.h index d8642c5b3..7785e6110 100644 --- a/src/core/hid/emulated_controller.h +++ b/src/core/hid/emulated_controller.h @@ -309,6 +309,8 @@ public: */ bool TestVibration(std::size_t device_index); + bool SetPollingMode(Common::Input::PollingMode polling_mode); + /// Returns the led pattern corresponding to this emulated controller LedPattern GetLedPattern() const; diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index cedade5c2..7f1fb3a71 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -7,6 +7,9 @@ #include "common/logging/log.h" #include "core/core.h" +#include "core/hid/emulated_controller.h" +#include "core/hid/hid_core.h" +#include "core/hid/hid_types.h" #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/k_event.h" #include "core/hle/service/nfp/nfp.h" @@ -14,12 +17,18 @@ namespace Service::NFP { namespace ErrCodes { -constexpr ResultCode ERR_NO_APPLICATION_AREA(ErrorModule::NFP, 152); +constexpr ResultCode DeviceNotFound(ErrorModule::NFP, 64); +constexpr ResultCode WrongDeviceState(ErrorModule::NFP, 73); +constexpr ResultCode ApplicationAreaIsNotInitialized(ErrorModule::NFP, 128); +constexpr ResultCode NoApplicationArea(ErrorModule::NFP, 152); +constexpr ResultCode ApplicationAreaExist(ErrorModule::NFP, 168); } // namespace ErrCodes +constexpr u32 ApplicationAreaSize = 0xD8; + IUser::IUser(Module::Interface& nfp_interface_, Core::System& system_) - : ServiceFramework{system_, "NFP::IUser"}, nfp_interface{nfp_interface_}, - deactivate_event{system.Kernel()}, availability_change_event{system.Kernel()} { + : ServiceFramework{system_, "NFP::IUser"}, service_context{system_, service_name}, + nfp_interface{nfp_interface_} { static const FunctionInfo functions[] = { {0, &IUser::Initialize, "Initialize"}, {1, &IUser::Finalize, "Finalize"}, @@ -30,10 +39,10 @@ IUser::IUser(Module::Interface& nfp_interface_, Core::System& system_) {6, &IUser::Unmount, "Unmount"}, {7, &IUser::OpenApplicationArea, "OpenApplicationArea"}, {8, &IUser::GetApplicationArea, "GetApplicationArea"}, - {9, nullptr, "SetApplicationArea"}, + {9, &IUser::SetApplicationArea, "SetApplicationArea"}, {10, nullptr, "Flush"}, {11, nullptr, "Restore"}, - {12, nullptr, "CreateApplicationArea"}, + {12, &IUser::CreateApplicationArea, "CreateApplicationArea"}, {13, &IUser::GetTagInfo, "GetTagInfo"}, {14, &IUser::GetRegisterInfo, "GetRegisterInfo"}, {15, &IUser::GetCommonInfo, "GetCommonInfo"}, @@ -49,220 +58,416 @@ IUser::IUser(Module::Interface& nfp_interface_, Core::System& system_) }; RegisterHandlers(functions); - Kernel::KAutoObject::Create(std::addressof(deactivate_event)); - Kernel::KAutoObject::Create(std::addressof(availability_change_event)); - - deactivate_event.Initialize("IUser:DeactivateEvent"); - availability_change_event.Initialize("IUser:AvailabilityChangeEvent"); + availability_change_event = service_context.CreateEvent("IUser:AvailabilityChangeEvent"); } void IUser::Initialize(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFC, "called"); + LOG_INFO(Service_NFC, "called"); + + state = State::Initialized; + + // TODO(german77): Loop through all interfaces + nfp_interface.Initialize(); IPC::ResponseBuilder rb{ctx, 2, 0}; rb.Push(ResultSuccess); - - state = State::Initialized; } void IUser::Finalize(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); + LOG_INFO(Service_NFP, "called"); - device_state = DeviceState::Finalized; + state = State::NonInitialized; + + // TODO(german77): Loop through all interfaces + nfp_interface.Finalize(); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ResultSuccess); } void IUser::ListDevices(Kernel::HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - const u32 array_size = rp.Pop(); - LOG_DEBUG(Service_NFP, "called, array_size={}", array_size); + LOG_INFO(Service_NFP, "called"); - ctx.WriteBuffer(device_handle); + std::vector devices; + + // TODO(german77): Loop through all interfaces + devices.push_back(nfp_interface.GetHandle()); + + ctx.WriteBuffer(devices); IPC::ResponseBuilder rb{ctx, 3}; rb.Push(ResultSuccess); - rb.Push(1); + rb.Push(devices.size()); } void IUser::StartDetection(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop()}; + const auto nfp_protocol{rp.Pop()}; + LOG_INFO(Service_NFP, "called, device_handle={}, nfp_protocol={}", device_handle, nfp_protocol); - if (device_state == DeviceState::Initialized || device_state == DeviceState::TagRemoved) { - device_state = DeviceState::SearchingForTag; + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + const auto result = nfp_interface.StartDetection(); + 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(ResultSuccess); + rb.Push(ErrCodes::DeviceNotFound); } void IUser::StopDetection(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop()}; + LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); - switch (device_state) { - case DeviceState::TagFound: - case DeviceState::TagMounted: - deactivate_event.GetWritableEvent().Signal(); - device_state = DeviceState::Initialized; - break; - case DeviceState::SearchingForTag: - case DeviceState::TagRemoved: - device_state = DeviceState::Initialized; - break; - default: - break; + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + const auto result = nfp_interface.StopDetection(); + 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(ResultSuccess); + rb.Push(ErrCodes::DeviceNotFound); } void IUser::Mount(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop()}; + const auto model_type{rp.PopEnum()}; + const auto mount_target{rp.PopEnum()}; + LOG_INFO(Service_NFP, "called, device_handle={}, model_type={}, mount_target={}", device_handle, + model_type, mount_target); + + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + const auto result = nfp_interface.Mount(); + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(result); + return; + } + + LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle); - device_state = DeviceState::TagMounted; IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); + rb.Push(ErrCodes::DeviceNotFound); } void IUser::Unmount(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop()}; + LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); - device_state = DeviceState::TagFound; + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + const auto result = nfp_interface.Unmount(); + 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(ResultSuccess); + rb.Push(ErrCodes::DeviceNotFound); } void IUser::OpenApplicationArea(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NFP, "(STUBBED) called"); + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop()}; + const auto access_id{rp.Pop()}; + LOG_INFO(Service_NFP, "called, device_handle={}, access_id={}", device_handle, access_id); + + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + const auto result = nfp_interface.OpenApplicationArea(access_id); + 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::ERR_NO_APPLICATION_AREA); + rb.Push(ErrCodes::DeviceNotFound); } void IUser::GetApplicationArea(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NFP, "(STUBBED) called"); + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop()}; + LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); - // TODO(ogniK): Pull application area from amiibo + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + std::vector data{}; + const auto result = nfp_interface.GetApplicationArea(data); - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.PushRaw(0); // This is from the GetCommonInfo stub + ctx.WriteBuffer(data); + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(result); + rb.PushRaw(data.size()); + return; + } + + LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::DeviceNotFound); +} + +void IUser::SetApplicationArea(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop()}; + const auto data{ctx.ReadBuffer()}; + LOG_INFO(Service_NFP, "called, device_handle={}, data_size={}", device_handle, data.size()); + + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + const auto result = nfp_interface.SetApplicationArea(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); +} + +void IUser::CreateApplicationArea(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop()}; + const auto access_id{rp.Pop()}; + const auto data{ctx.ReadBuffer()}; + LOG_INFO(Service_NFP, "called, device_handle={}, data_size={}, access_id={}", device_handle, + access_id, data.size()); + + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + const auto result = nfp_interface.CreateApplicationArea(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); } void IUser::GetTagInfo(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop()}; + LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); + + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + TagInfo tag_info{}; + const auto result = nfp_interface.GetTagInfo(tag_info); + ctx.WriteBuffer(tag_info); + 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}; - const auto& amiibo = nfp_interface.GetAmiiboBuffer(); - const TagInfo tag_info{ - .uuid = amiibo.uuid, - .uuid_length = static_cast(amiibo.uuid.size()), - .protocol = 1, // TODO(ogniK): Figure out actual values - .tag_type = 2, - }; - ctx.WriteBuffer(tag_info); - rb.Push(ResultSuccess); + rb.Push(ErrCodes::DeviceNotFound); } void IUser::GetRegisterInfo(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NFP, "(STUBBED) called"); + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop()}; + LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); - // TODO(ogniK): Pull Mii and owner data from amiibo + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + RegisterInfo register_info{}; + const auto result = nfp_interface.GetRegisterInfo(register_info); + ctx.WriteBuffer(register_info); + 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(ResultSuccess); + rb.Push(ErrCodes::DeviceNotFound); } void IUser::GetCommonInfo(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NFP, "(STUBBED) called"); + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop()}; + LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); - // TODO(ogniK): Pull common information from amiibo + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + CommonInfo common_info{}; + const auto result = nfp_interface.GetCommonInfo(common_info); + ctx.WriteBuffer(common_info); + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(result); + return; + } - CommonInfo common_info{}; - common_info.application_area_size = 0; - ctx.WriteBuffer(common_info); + LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle); IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); + rb.Push(ErrCodes::DeviceNotFound); } void IUser::GetModelInfo(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop()}; + LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); + + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + ModelInfo model_info{}; + const auto result = nfp_interface.GetModelInfo(model_info); + ctx.WriteBuffer(model_info); + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); + return; + } + + LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle); IPC::ResponseBuilder rb{ctx, 2}; - const auto& amiibo = nfp_interface.GetAmiiboBuffer(); - ctx.WriteBuffer(amiibo.model_info); - rb.Push(ResultSuccess); + rb.Push(ErrCodes::DeviceNotFound); } void IUser::AttachActivateEvent(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - const u64 dev_handle = rp.Pop(); - LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); + const auto device_handle{rp.Pop()}; + LOG_DEBUG(Service_NFP, "called, device_handle={}", device_handle); - IPC::ResponseBuilder rb{ctx, 2, 1}; - rb.Push(ResultSuccess); - rb.PushCopyObjects(nfp_interface.GetNFCEvent()); - has_attached_handle = true; + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + IPC::ResponseBuilder rb{ctx, 2, 1}; + rb.Push(ResultSuccess); + rb.PushCopyObjects(nfp_interface.GetActivateEvent()); + return; + } + + LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::DeviceNotFound); } void IUser::AttachDeactivateEvent(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - const u64 dev_handle = rp.Pop(); - LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); + const auto device_handle{rp.Pop()}; + LOG_DEBUG(Service_NFP, "called, device_handle={}", device_handle); - IPC::ResponseBuilder rb{ctx, 2, 1}; - rb.Push(ResultSuccess); - rb.PushCopyObjects(deactivate_event.GetReadableEvent()); + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + IPC::ResponseBuilder rb{ctx, 2, 1}; + rb.Push(ResultSuccess); + rb.PushCopyObjects(nfp_interface.GetDeactivateEvent()); + return; + } + + LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::DeviceNotFound); } void IUser::GetState(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFC, "called"); + LOG_INFO(Service_NFC, "called"); IPC::ResponseBuilder rb{ctx, 3, 0}; rb.Push(ResultSuccess); - rb.PushRaw(static_cast(state)); + rb.PushEnum(state); } void IUser::GetDeviceState(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_NFP, "called"); + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop()}; + LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.Push(static_cast(device_state)); + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(ResultSuccess); + rb.PushEnum(nfp_interface.GetCurrentState()); + return; + } + + LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::DeviceNotFound); } void IUser::GetNpadId(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - const u64 dev_handle = rp.Pop(); - LOG_DEBUG(Service_NFP, "called, dev_handle=0x{:X}", dev_handle); + const auto device_handle{rp.Pop()}; + LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.Push(npad_id); + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(ResultSuccess); + rb.PushEnum(nfp_interface.GetNpadId()); + return; + } + + LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::DeviceNotFound); } void IUser::GetApplicationAreaSize(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NFP, "(STUBBED) called"); - // We don't need to worry about this since we can just open the file - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.PushRaw(0); // This is from the GetCommonInfo stub + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop()}; + LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); + + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + IPC::ResponseBuilder rb{ctx, 3}; + rb.Push(ResultSuccess); + rb.Push(ApplicationAreaSize); + return; + } + + LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::DeviceNotFound); } void IUser::AttachAvailabilityChangeEvent(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_NFP, "(STUBBED) called"); + LOG_DEBUG(Service_NFP, "(STUBBED) called"); IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(ResultSuccess); - rb.PushCopyObjects(availability_change_event.GetReadableEvent()); + rb.PushCopyObjects(availability_change_event->GetReadableEvent()); } Module::Interface::Interface(std::shared_ptr module_, Core::System& system_, const char* name) - : ServiceFramework{system_, name}, nfc_tag_load{system.Kernel()}, module{std::move(module_)} { - Kernel::KAutoObject::Create(std::addressof(nfc_tag_load)); - nfc_tag_load.Initialize("IUser:NFCTagDetected"); + : ServiceFramework{system_, name}, module{std::move(module_)}, + npad_id{Core::HID::NpadIdType::Player1}, service_context{system_, service_name} { + activate_event = service_context.CreateEvent("IUser:NFPActivateEvent"); + deactivate_event = service_context.CreateEvent("IUser:NFPDeactivateEvent"); } Module::Interface::~Interface() = default; @@ -272,25 +477,254 @@ void Module::Interface::CreateUserInterface(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(ResultSuccess); - rb.PushIpcInterface(*this, system, service_context); + rb.PushIpcInterface(*this, system); } bool Module::Interface::LoadAmiibo(const std::vector& buffer) { if (buffer.size() < sizeof(AmiiboFile)) { + LOG_ERROR(Service_NFP, "Wrong file size"); return false; } + if (device_state != DeviceState::SearchingForTag) { + LOG_ERROR(Service_NFP, "Game is not looking for amiibos, current state {}", device_state); + return false; + } + + LOG_INFO(Service_NFP, "New Amiibo detected"); std::memcpy(&amiibo, buffer.data(), sizeof(amiibo)); - nfc_tag_load->GetWritableEvent().Signal(); + device_state = DeviceState::TagFound; + activate_event->GetWritableEvent().Signal(); return true; } -Kernel::KReadableEvent& Module::Interface::GetNFCEvent() { - return nfc_tag_load->GetReadableEvent(); +void Module::Interface::CloseAmiibo() { + LOG_INFO(Service_NFP, "Remove amiibo"); + device_state = DeviceState::TagRemoved; + write_counter = 0; + is_application_area_initialized = false; + application_area_id = 0; + application_area_data.clear(); + deactivate_event->GetWritableEvent().Signal(); } -const Module::Interface::AmiiboFile& Module::Interface::GetAmiiboBuffer() const { - return amiibo; +Kernel::KReadableEvent& Module::Interface::GetActivateEvent() const { + return activate_event->GetReadableEvent(); +} + +Kernel::KReadableEvent& Module::Interface::GetDeactivateEvent() const { + return deactivate_event->GetReadableEvent(); +} + +void Module::Interface::Initialize() { + device_state = DeviceState::Initialized; +} + +void Module::Interface::Finalize() { + device_state = DeviceState::Unaviable; + write_counter = 0; + is_application_area_initialized = false; + application_area_id = 0; + application_area_data.clear(); +} + +ResultCode Module::Interface::StartDetection() { + auto npad_device = system.HIDCore().GetEmulatedController(npad_id); + + // TODO(german77): Add callback for when nfc data is available + + if (device_state == DeviceState::Initialized || device_state == DeviceState::TagRemoved) { + npad_device->SetPollingMode(Common::Input::PollingMode::NFC); + device_state = DeviceState::SearchingForTag; + return ResultSuccess; + } + + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return ErrCodes::WrongDeviceState; +} + +ResultCode Module::Interface::StopDetection() { + auto npad_device = system.HIDCore().GetEmulatedController(npad_id); + npad_device->SetPollingMode(Common::Input::PollingMode::Active); + + if (device_state == DeviceState::TagFound || device_state == DeviceState::TagMounted) { + CloseAmiibo(); + return ResultSuccess; + } + if (device_state == DeviceState::SearchingForTag || device_state == DeviceState::TagRemoved) { + device_state = DeviceState::Initialized; + return ResultSuccess; + } + + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return ErrCodes::WrongDeviceState; +} + +ResultCode Module::Interface::Mount() { + if (device_state == DeviceState::TagFound) { + device_state = DeviceState::TagMounted; + return ResultSuccess; + } + + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return ErrCodes::WrongDeviceState; +} + +ResultCode Module::Interface::Unmount() { + if (device_state == DeviceState::TagMounted) { + is_application_area_initialized = false; + application_area_id = 0; + application_area_data.clear(); + device_state = DeviceState::TagFound; + return ResultSuccess; + } + + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return ErrCodes::WrongDeviceState; +} + +ResultCode Module::Interface::GetTagInfo(TagInfo& tag_info) const { + if (device_state == DeviceState::TagFound || device_state == DeviceState::TagMounted) { + // Read this data from the amiibo save file + tag_info = { + .uuid = amiibo.uuid, + .uuid_length = static_cast(amiibo.uuid.size()), + .protocol = 0xFFFFFFFF, // TODO(ogniK): Figure out actual values + .tag_type = 0xFFFFFFFF, + }; + return ResultSuccess; + } + + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return ErrCodes::WrongDeviceState; +} + +ResultCode Module::Interface::GetCommonInfo(CommonInfo& common_info) const { + if (device_state != DeviceState::TagMounted) { + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return ErrCodes::WrongDeviceState; + } + + // Read this data from the amiibo save file + common_info = { + .last_write_year = 2022, + .last_write_month = 2, + .last_write_day = 7, + .write_counter = write_counter, + .version = 1, + .application_area_size = ApplicationAreaSize, + }; + return ResultSuccess; +} + +ResultCode Module::Interface::GetModelInfo(ModelInfo& model_info) const { + if (device_state != DeviceState::TagMounted) { + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return ErrCodes::WrongDeviceState; + } + + model_info = amiibo.model_info; + return ResultSuccess; +} + +ResultCode Module::Interface::GetRegisterInfo(RegisterInfo& register_info) const { + if (device_state != DeviceState::TagMounted) { + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return ErrCodes::WrongDeviceState; + } + + Service::Mii::MiiManager manager; + + // Read this data from the amiibo save file + register_info = { + .mii_char_info = manager.BuildDefault(0), + .first_write_year = 2022, + .first_write_month = 2, + .first_write_day = 7, + .amiibo_name = {'Y', 'u', 'z', 'u', 'A', 'm', 'i', 'i', 'b', 'o', 0}, + .unknown = {}, + }; + return ResultSuccess; +} + +ResultCode Module::Interface::OpenApplicationArea(u32 access_id) { + if (device_state != DeviceState::TagMounted) { + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return ErrCodes::WrongDeviceState; + } + // if (AmiiboApplicationDataExist(access_id)) { + // application_area_data = LoadAmiiboApplicationData(access_id); + // application_area_id = access_id; + // is_application_area_initialized = true; + // } + if (!is_application_area_initialized) { + LOG_ERROR(Service_NFP, "Application area is not initialized"); + return ErrCodes::ApplicationAreaIsNotInitialized; + } + return ResultSuccess; +} + +ResultCode Module::Interface::GetApplicationArea(std::vector& data) const { + if (device_state != DeviceState::TagMounted) { + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return ErrCodes::WrongDeviceState; + } + if (!is_application_area_initialized) { + LOG_ERROR(Service_NFP, "Application area is not initialized"); + return ErrCodes::ApplicationAreaIsNotInitialized; + } + + data = application_area_data; + + return ResultSuccess; +} + +ResultCode Module::Interface::SetApplicationArea(const std::vector& data) { + if (device_state != DeviceState::TagMounted) { + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return ErrCodes::WrongDeviceState; + } + if (!is_application_area_initialized) { + LOG_ERROR(Service_NFP, "Application area is not initialized"); + return ErrCodes::ApplicationAreaIsNotInitialized; + } + application_area_data = data; + write_counter++; + // SaveAmiiboApplicationData(application_area_id,application_area_data); + return ResultSuccess; +} + +ResultCode Module::Interface::CreateApplicationArea(u32 access_id, const std::vector& data) { + if (device_state != DeviceState::TagMounted) { + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return ErrCodes::WrongDeviceState; + } + // if (AmiiboApplicationDataExist(access_id)) { + // LOG_ERROR(Service_NFP, "Application area already exist"); + // return ErrCodes::ApplicationAreaExist; + // } + // if (LoadAmiiboApplicationData(access_id,data)) { + // is_application_area_initialized = true; + // application_area_id = access_id; + // } + application_area_data = data; + application_area_id = access_id; + write_counter = 0; + // SaveAmiiboApplicationData(application_area_id,application_area_data); + return ResultSuccess; +} + +u64 Module::Interface::GetHandle() const { + // Generate a handle based of the npad id + return static_cast(npad_id); +} + +DeviceState Module::Interface::GetCurrentState() const { + return device_state; +} + +Core::HID::NpadIdType Module::Interface::GetNpadId() const { + return npad_id; } void InstallInterfaces(SM::ServiceManager& service_manager, Core::System& system) { diff --git a/src/core/hle/service/nfp/nfp.h b/src/core/hle/service/nfp/nfp.h index 8d9db880a..4642ed634 100644 --- a/src/core/hle/service/nfp/nfp.h +++ b/src/core/hle/service/nfp/nfp.h @@ -8,79 +8,155 @@ #include #include "core/hle/service/kernel_helpers.h" -#include "core/hle/kernel/k_event.h" -#include "core/hle/service/mii/manager.h" +#include "core/hle/service/mii/mii_manager.h" #include "core/hle/service/service.h" namespace Kernel { class KEvent; -} +class KReadableEvent; +} // namespace Kernel + +namespace Core::HID { +enum class NpadIdType : u32; +} // namespace Core::HID namespace Service::NFP { enum class ServiceType : u32 { - User = 0, - Debug = 1, - System = 2, + User, + Debug, + System, }; enum class State : u32 { - NonInitialized = 0, - Initialized = 1, + NonInitialized, + Initialized, }; enum class DeviceState : u32 { - Initialized = 0, - SearchingForTag = 1, - TagFound = 2, - TagRemoved = 3, - TagMounted = 4, - Unaviable = 5, - Finalized = 6 + Initialized, + SearchingForTag, + TagFound, + TagRemoved, + TagMounted, + Unaviable, + Finalized, +}; + +enum class ModelType : u32 { + Amiibo, }; enum class MountTarget : u32 { - Rom = 1, - Ram = 2, - All = 3, + Rom, + Ram, + All, }; struct TagInfo { std::array uuid; u8 uuid_length; INSERT_PADDING_BYTES(0x15); - u32_le protocol; - u32_le tag_type; - INSERT_PADDING_BYTES(0x2c); + u32 protocol; + u32 tag_type; + INSERT_PADDING_BYTES(0x30); }; -static_assert(sizeof(TagInfo) == 0x54, "TagInfo is an invalid size"); +static_assert(sizeof(TagInfo) == 0x58, "TagInfo is an invalid size"); struct CommonInfo { - u16_be last_write_year; + u16 last_write_year; u8 last_write_month; u8 last_write_day; - u16_be write_counter; - u16_be version; - u32_be application_area_size; + u16 write_counter; + u16 version; + u32 application_area_size; INSERT_PADDING_BYTES(0x34); }; static_assert(sizeof(CommonInfo) == 0x40, "CommonInfo is an invalid size"); struct ModelInfo { - std::array ammibo_id; - INSERT_PADDING_BYTES(0x38); + u16 character_id; + u8 character_variant; + u8 figure_type; + u16 model_number; + u8 series; + INSERT_PADDING_BYTES(0x39); }; static_assert(sizeof(ModelInfo) == 0x40, "ModelInfo is an invalid size"); struct RegisterInfo { - Service::Mii::MiiInfo mii; - u16_be first_write_year; + Service::Mii::MiiInfo mii_char_info; + u16 first_write_year; u8 first_write_month; u8 first_write_day; std::array amiibo_name; - INSERT_PADDING_BYTES(0x99); + u8 unknown; + INSERT_PADDING_BYTES(0x98); +}; +static_assert(sizeof(RegisterInfo) == 0x100, "RegisterInfo is an invalid size"); + +class Module final { +public: + class Interface : public ServiceFramework { + public: + explicit Interface(std::shared_ptr module_, Core::System& system_, + const char* name); + ~Interface() override; + + struct AmiiboFile { + std::array uuid; + INSERT_PADDING_BYTES(0x4); // Compability container + INSERT_PADDING_BYTES(0x46); + ModelInfo model_info; + }; + static_assert(sizeof(AmiiboFile) == 0x94, "AmiiboFile is an invalid size"); + + void CreateUserInterface(Kernel::HLERequestContext& ctx); + bool LoadAmiibo(const std::vector& buffer); + void CloseAmiibo(); + + void Initialize(); + void Finalize(); + + ResultCode StartDetection(); + ResultCode StopDetection(); + ResultCode Mount(); + ResultCode Unmount(); + + ResultCode GetTagInfo(TagInfo& tag_info) const; + ResultCode GetCommonInfo(CommonInfo& common_info) const; + ResultCode GetModelInfo(ModelInfo& model_info) const; + ResultCode GetRegisterInfo(RegisterInfo& register_info) const; + + ResultCode OpenApplicationArea(u32 access_id); + ResultCode GetApplicationArea(std::vector& data) const; + ResultCode SetApplicationArea(const std::vector& data); + ResultCode CreateApplicationArea(u32 access_id, const std::vector& data); + + u64 GetHandle() const; + DeviceState GetCurrentState() const; + Core::HID::NpadIdType GetNpadId() const; + + Kernel::KReadableEvent& GetActivateEvent() const; + Kernel::KReadableEvent& GetDeactivateEvent() const; + + protected: + std::shared_ptr module; + + private: + const Core::HID::NpadIdType npad_id; + + DeviceState device_state{DeviceState::Unaviable}; + KernelHelpers::ServiceContext service_context; + Kernel::KEvent* activate_event; + Kernel::KEvent* deactivate_event; + AmiiboFile amiibo{}; + u16 write_counter{}; + bool is_application_area_initialized{}; + u32 application_area_id; + std::vector application_area_data; + }; }; -// static_assert(sizeof(RegisterInfo) == 0x106, "RegisterInfo is an invalid size"); class IUser final : public ServiceFramework { public: @@ -96,6 +172,8 @@ private: void Unmount(Kernel::HLERequestContext& ctx); void OpenApplicationArea(Kernel::HLERequestContext& ctx); void GetApplicationArea(Kernel::HLERequestContext& ctx); + void SetApplicationArea(Kernel::HLERequestContext& ctx); + void CreateApplicationArea(Kernel::HLERequestContext& ctx); void GetTagInfo(Kernel::HLERequestContext& ctx); void GetRegisterInfo(Kernel::HLERequestContext& ctx); void GetCommonInfo(Kernel::HLERequestContext& ctx); @@ -108,50 +186,13 @@ private: void GetApplicationAreaSize(Kernel::HLERequestContext& ctx); void AttachAvailabilityChangeEvent(Kernel::HLERequestContext& ctx); - bool has_attached_handle{}; - const u64 device_handle{0}; // Npad device 1 - const u32 npad_id{0}; // Player 1 controller - State state{State::NonInitialized}; - DeviceState device_state{DeviceState::Initialized}; + KernelHelpers::ServiceContext service_context; + + // TODO(german77): We should have a vector of interfaces Module::Interface& nfp_interface; - Kernel::KEvent deactivate_event; - Kernel::KEvent availability_change_event; -}; -class Module final { -public: - class Interface : public ServiceFramework { - public: - explicit Interface(std::shared_ptr module_, Core::System& system_, - const char* name); - ~Interface() override; - - struct ModelInfo { - std::array amiibo_identification_block; - INSERT_PADDING_BYTES(0x38); - }; - static_assert(sizeof(ModelInfo) == 0x40, "ModelInfo is an invalid size"); - - struct AmiiboFile { - std::array uuid; - INSERT_PADDING_BYTES(0x4a); - ModelInfo model_info; - }; - static_assert(sizeof(AmiiboFile) == 0x94, "AmiiboFile is an invalid size"); - - void CreateUserInterface(Kernel::HLERequestContext& ctx); - bool LoadAmiibo(const std::vector& buffer); - Kernel::KReadableEvent& GetNFCEvent(); - const AmiiboFile& GetAmiiboBuffer() const; - - protected: - std::shared_ptr module; - - private: - KernelHelpers::ServiceContext service_context; - Kernel::KEvent* nfc_tag_load; - AmiiboFile amiibo{}; - }; + State state{State::NonInitialized}; + Kernel::KEvent* availability_change_event; }; void InstallInterfaces(SM::ServiceManager& service_manager, Core::System& system); From 41b65d38fa57dcfefa9e944ef90c66bb5aa5d254 Mon Sep 17 00:00:00 2001 From: german77 Date: Mon, 7 Feb 2022 21:39:39 -0600 Subject: [PATCH 4/8] yuzu: Allow to open and remove the amiibo --- src/yuzu/configuration/config.cpp | 6 +++--- src/yuzu/main.cpp | 21 ++++++++++++++++++++- src/yuzu/main.ui | 2 +- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp index 2c8c10c50..53f1a2974 100644 --- a/src/yuzu/configuration/config.cpp +++ b/src/yuzu/configuration/config.cpp @@ -75,13 +75,13 @@ const std::array Config::default_hotkeys{{ {QStringLiteral("Exit Fullscreen"), QStringLiteral("Main Window"), {QStringLiteral("Esc"), QStringLiteral(""), Qt::WindowShortcut}}, {QStringLiteral("Exit yuzu"), QStringLiteral("Main Window"), {QStringLiteral("Ctrl+Q"), QStringLiteral("Home+Minus"), Qt::WindowShortcut}}, {QStringLiteral("Fullscreen"), QStringLiteral("Main Window"), {QStringLiteral("F11"), QStringLiteral("Home+B"), Qt::WindowShortcut}}, - {QStringLiteral("Load Amiibo"), QStringLiteral("Main Window"), {QStringLiteral("F2"), QStringLiteral("Home+A"), Qt::WidgetWithChildrenShortcut}}, {QStringLiteral("Load File"), QStringLiteral("Main Window"), {QStringLiteral("Ctrl+O"), QStringLiteral(""), Qt::WidgetWithChildrenShortcut}}, + {QStringLiteral("Load/Remove Amiibo"), QStringLiteral("Main Window"), {QStringLiteral("F2"), QStringLiteral("Home+A"), Qt::WidgetWithChildrenShortcut}}, {QStringLiteral("Restart Emulation"), QStringLiteral("Main Window"), {QStringLiteral("F6"), QStringLiteral(""), Qt::WindowShortcut}}, {QStringLiteral("Stop Emulation"), QStringLiteral("Main Window"), {QStringLiteral("F5"), QStringLiteral(""), Qt::WindowShortcut}}, - {QStringLiteral("TAS Start/Stop"), QStringLiteral("Main Window"), {QStringLiteral("Ctrl+F5"), QStringLiteral(""), Qt::ApplicationShortcut}}, - {QStringLiteral("TAS Reset"), QStringLiteral("Main Window"), {QStringLiteral("Ctrl+F6"), QStringLiteral(""), Qt::ApplicationShortcut}}, {QStringLiteral("TAS Record"), QStringLiteral("Main Window"), {QStringLiteral("Ctrl+F7"), QStringLiteral(""), Qt::ApplicationShortcut}}, + {QStringLiteral("TAS Reset"), QStringLiteral("Main Window"), {QStringLiteral("Ctrl+F6"), QStringLiteral(""), Qt::ApplicationShortcut}}, + {QStringLiteral("TAS Start/Stop"), QStringLiteral("Main Window"), {QStringLiteral("Ctrl+F5"), QStringLiteral(""), Qt::ApplicationShortcut}}, {QStringLiteral("Toggle Filter Bar"), QStringLiteral("Main Window"), {QStringLiteral("Ctrl+F"), QStringLiteral(""), Qt::WindowShortcut}}, {QStringLiteral("Toggle Framerate Limit"), QStringLiteral("Main Window"), {QStringLiteral("Ctrl+U"), QStringLiteral("Home+Y"), Qt::ApplicationShortcut}}, {QStringLiteral("Toggle Mouse Panning"), QStringLiteral("Main Window"), {QStringLiteral("Ctrl+F9"), QStringLiteral(""), Qt::ApplicationShortcut}}, diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index c788db12d..395527607 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -980,7 +980,7 @@ void GMainWindow::InitializeHotkeys() { hotkey_registry.LoadHotkeys(); LinkActionShortcut(ui->action_Load_File, QStringLiteral("Load File")); - LinkActionShortcut(ui->action_Load_Amiibo, QStringLiteral("Load Amiibo")); + LinkActionShortcut(ui->action_Load_Amiibo, QStringLiteral("Load/Remove Amiibo")); LinkActionShortcut(ui->action_Exit, QStringLiteral("Exit yuzu")); LinkActionShortcut(ui->action_Restart, QStringLiteral("Restart Emulation")); LinkActionShortcut(ui->action_Pause, QStringLiteral("Continue/Pause Emulation")); @@ -2912,6 +2912,25 @@ void GMainWindow::OnLoadAmiibo() { return; } + Service::SM::ServiceManager& sm = system->ServiceManager(); + auto nfc = sm.GetService("nfp:user"); + if (nfc == nullptr) { + QMessageBox::warning(this, tr("Error"), + tr("The current game is not looking for amiibos")); + return; + } + const auto nfc_state = nfc->GetCurrentState(); + if (nfc_state == Service::NFP::DeviceState::TagFound || + nfc_state == Service::NFP::DeviceState::TagMounted) { + nfc->CloseAmiibo(); + return; + } + + if (nfc_state != Service::NFP::DeviceState::SearchingForTag) { + QMessageBox::warning(this, tr("Error"), tr("The current game is not looking for amiibos")); + return; + } + is_amiibo_file_select_active = true; const QString extensions{QStringLiteral("*.bin")}; const QString file_filter = tr("Amiibo File (%1);; All Files (*.*)").arg(extensions); diff --git a/src/yuzu/main.ui b/src/yuzu/main.ui index 5719b2ee4..6ab95b9a5 100644 --- a/src/yuzu/main.ui +++ b/src/yuzu/main.ui @@ -266,7 +266,7 @@ false - Load &Amiibo... + Load/Remove &Amiibo... From 29f9a454eb2597eb50db68c31cb655c51de72dae Mon Sep 17 00:00:00 2001 From: Narr the Reg Date: Tue, 8 Feb 2022 14:09:30 -0600 Subject: [PATCH 5/8] nfp: Validate amiibo files --- src/core/hle/service/nfp/nfp.cpp | 115 ++++++++++++++++++++++--------- src/core/hle/service/nfp/nfp.h | 71 ++++++++++++++++--- 2 files changed, 145 insertions(+), 41 deletions(-) diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index 7f1fb3a71..1e91aa340 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -108,7 +108,7 @@ void IUser::StartDetection(Kernel::HLERequestContext& ctx) { // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { - const auto result = nfp_interface.StartDetection(); + const auto result = nfp_interface.StartDetection(nfp_protocol); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(result); return; @@ -209,7 +209,6 @@ void IUser::GetApplicationArea(Kernel::HLERequestContext& ctx) { if (device_handle == nfp_interface.GetHandle()) { std::vector data{}; const auto result = nfp_interface.GetApplicationArea(data); - ctx.WriteBuffer(data); IPC::ResponseBuilder rb{ctx, 3}; rb.Push(result); @@ -232,7 +231,6 @@ void IUser::SetApplicationArea(Kernel::HLERequestContext& ctx) { // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { const auto result = nfp_interface.SetApplicationArea(data); - IPC::ResponseBuilder rb{ctx, 2}; rb.Push(result); return; @@ -255,7 +253,6 @@ void IUser::CreateApplicationArea(Kernel::HLERequestContext& ctx) { // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { const auto result = nfp_interface.CreateApplicationArea(access_id, data); - IPC::ResponseBuilder rb{ctx, 2}; rb.Push(result); return; @@ -390,7 +387,7 @@ void IUser::AttachDeactivateEvent(Kernel::HLERequestContext& ctx) { } void IUser::GetState(Kernel::HLERequestContext& ctx) { - LOG_INFO(Service_NFC, "called"); + LOG_DEBUG(Service_NFC, "called"); IPC::ResponseBuilder rb{ctx, 3, 0}; rb.Push(ResultSuccess); @@ -400,7 +397,7 @@ void IUser::GetState(Kernel::HLERequestContext& ctx) { void IUser::GetDeviceState(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; const auto device_handle{rp.Pop()}; - LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); + LOG_DEBUG(Service_NFP, "called, device_handle={}", device_handle); // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { @@ -419,7 +416,7 @@ void IUser::GetDeviceState(Kernel::HLERequestContext& ctx) { void IUser::GetNpadId(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; const auto device_handle{rp.Pop()}; - LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); + LOG_DEBUG(Service_NFP, "called, device_handle={}", device_handle); // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { @@ -438,7 +435,7 @@ void IUser::GetNpadId(Kernel::HLERequestContext& ctx) { void IUser::GetApplicationAreaSize(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; const auto device_handle{rp.Pop()}; - LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); + LOG_DEBUG(Service_NFP, "called, device_handle={}", device_handle); // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { @@ -493,6 +490,11 @@ bool Module::Interface::LoadAmiibo(const std::vector& buffer) { LOG_INFO(Service_NFP, "New Amiibo detected"); std::memcpy(&amiibo, buffer.data(), sizeof(amiibo)); + + if (!IsAmiiboValid()) { + return false; + } + device_state = DeviceState::TagFound; activate_event->GetWritableEvent().Signal(); return true; @@ -501,13 +503,54 @@ bool Module::Interface::LoadAmiibo(const std::vector& buffer) { void Module::Interface::CloseAmiibo() { LOG_INFO(Service_NFP, "Remove amiibo"); device_state = DeviceState::TagRemoved; - write_counter = 0; is_application_area_initialized = false; application_area_id = 0; application_area_data.clear(); deactivate_event->GetWritableEvent().Signal(); } +bool Module::Interface::IsAmiiboValid() const { + LOG_INFO(Service_NFP, "uuid_lock=0x{0:x}", amiibo.uuid_lock); + LOG_INFO(Service_NFP, "compability_container=0x{0:x}", amiibo.compability_container); + LOG_INFO(Service_NFP, "crypto_init=0x{0:x}", amiibo.crypto_init); + LOG_INFO(Service_NFP, "write_count={}", amiibo.write_count); + + LOG_INFO(Service_NFP, "character_id=0x{0:x}", amiibo.model_info.character_id); + LOG_INFO(Service_NFP, "character_variant={}", amiibo.model_info.character_variant); + LOG_INFO(Service_NFP, "amiibo_type={}", amiibo.model_info.amiibo_type); + LOG_INFO(Service_NFP, "model_number=0x{0:x}", amiibo.model_info.model_number); + LOG_INFO(Service_NFP, "series={}", amiibo.model_info.series); + LOG_INFO(Service_NFP, "fixed_value=0x{0:x}", amiibo.model_info.fixed); + + LOG_INFO(Service_NFP, "tag_dynamic_lock=0x{0:x}", amiibo.tag_dynamic_lock); + LOG_INFO(Service_NFP, "tag_CFG0=0x{0:x}", amiibo.tag_CFG0); + LOG_INFO(Service_NFP, "tag_CFG1=0x{0:x}", amiibo.tag_CFG1); + + // Check against all know constants on an amiibo binary + if (amiibo.uuid_lock != 0xE00F) { + return false; + } + if (amiibo.compability_container != 0xEEFF10F1UL) { + return false; + } + if ((amiibo.crypto_init & 0xFF) != 0xA5) { + return false; + } + if (amiibo.model_info.fixed != 0x02) { + return false; + } + if ((amiibo.tag_dynamic_lock & 0xFFFFFF) != 0x0F0001) { + return false; + } + if (amiibo.tag_CFG0 != 0x04000000UL) { + return false; + } + if (amiibo.tag_CFG1 != 0x5F) { + return false; + } + return true; +} + Kernel::KReadableEvent& Module::Interface::GetActivateEvent() const { return activate_event->GetReadableEvent(); } @@ -522,13 +565,12 @@ void Module::Interface::Initialize() { void Module::Interface::Finalize() { device_state = DeviceState::Unaviable; - write_counter = 0; is_application_area_initialized = false; application_area_id = 0; application_area_data.clear(); } -ResultCode Module::Interface::StartDetection() { +ResultCode Module::Interface::StartDetection(s32 protocol_) { auto npad_device = system.HIDCore().GetEmulatedController(npad_id); // TODO(german77): Add callback for when nfc data is available @@ -536,6 +578,7 @@ ResultCode Module::Interface::StartDetection() { if (device_state == DeviceState::Initialized || device_state == DeviceState::TagRemoved) { npad_device->SetPollingMode(Common::Input::PollingMode::NFC); device_state = DeviceState::SearchingForTag; + protocol = protocol_; return ResultSuccess; } @@ -589,8 +632,8 @@ ResultCode Module::Interface::GetTagInfo(TagInfo& tag_info) const { tag_info = { .uuid = amiibo.uuid, .uuid_length = static_cast(amiibo.uuid.size()), - .protocol = 0xFFFFFFFF, // TODO(ogniK): Figure out actual values - .tag_type = 0xFFFFFFFF, + .protocol = protocol, + .tag_type = static_cast(amiibo.model_info.amiibo_type), }; return ResultSuccess; } @@ -610,7 +653,7 @@ ResultCode Module::Interface::GetCommonInfo(CommonInfo& common_info) const { .last_write_year = 2022, .last_write_month = 2, .last_write_day = 7, - .write_counter = write_counter, + .write_counter = amiibo.write_count, .version = 1, .application_area_size = ApplicationAreaSize, }; @@ -652,11 +695,11 @@ ResultCode Module::Interface::OpenApplicationArea(u32 access_id) { LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); return ErrCodes::WrongDeviceState; } - // if (AmiiboApplicationDataExist(access_id)) { - // application_area_data = LoadAmiiboApplicationData(access_id); - // application_area_id = access_id; - // is_application_area_initialized = true; - // } + if (AmiiboApplicationDataExist(access_id)) { + application_area_data = LoadAmiiboApplicationData(access_id); + application_area_id = access_id; + is_application_area_initialized = true; + } if (!is_application_area_initialized) { LOG_ERROR(Service_NFP, "Application area is not initialized"); return ErrCodes::ApplicationAreaIsNotInitialized; @@ -689,8 +732,7 @@ ResultCode Module::Interface::SetApplicationArea(const std::vector& data) { return ErrCodes::ApplicationAreaIsNotInitialized; } application_area_data = data; - write_counter++; - // SaveAmiiboApplicationData(application_area_id,application_area_data); + SaveAmiiboApplicationData(application_area_id, application_area_data); return ResultSuccess; } @@ -699,21 +741,32 @@ ResultCode Module::Interface::CreateApplicationArea(u32 access_id, const std::ve LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); return ErrCodes::WrongDeviceState; } - // if (AmiiboApplicationDataExist(access_id)) { - // LOG_ERROR(Service_NFP, "Application area already exist"); - // return ErrCodes::ApplicationAreaExist; - // } - // if (LoadAmiiboApplicationData(access_id,data)) { - // is_application_area_initialized = true; - // application_area_id = access_id; - // } + if (AmiiboApplicationDataExist(access_id)) { + LOG_ERROR(Service_NFP, "Application area already exist"); + return ErrCodes::ApplicationAreaExist; + } application_area_data = data; application_area_id = access_id; - write_counter = 0; - // SaveAmiiboApplicationData(application_area_id,application_area_data); + SaveAmiiboApplicationData(application_area_id, application_area_data); return ResultSuccess; } +bool Module::Interface::AmiiboApplicationDataExist(u32 access_id) const { + // TODO(german77): Check if file exist + return false; +} + +const std::vector Module::Interface::LoadAmiiboApplicationData(u32 access_id) const { + // TODO(german77): Read file + std::vector data(ApplicationAreaSize); + return data; +} + +void Module::Interface::SaveAmiiboApplicationData(u32 access_id, + const std::vector& data) const { + // TODO(german77): Save file +} + u64 Module::Interface::GetHandle() const { // Generate a handle based of the npad id return static_cast(npad_id); diff --git a/src/core/hle/service/nfp/nfp.h b/src/core/hle/service/nfp/nfp.h index 4642ed634..633539dcc 100644 --- a/src/core/hle/service/nfp/nfp.h +++ b/src/core/hle/service/nfp/nfp.h @@ -53,11 +53,43 @@ enum class MountTarget : u32 { All, }; +enum class AmiiboType : u8 { + Figure, + Card, + Yarn, +}; + +enum class AmiiboSeries : u8 { + SuperSmashBros, + SuperMario, + ChibiRobo, + YoshiWoollyWorld, + Splatoon, + AnimalCrossing, + EightBitMario, + Skylanders, + Unknown8, + TheLegendOfZelda, + ShovelKnight, + Unknown11, + Kiby, + Pokemon, + MarioSportsSuperstars, + MonsterHunter, + BoxBoy, + Pikmin, + FireEmblem, + Metroid, + Others, + MegaMan, + Diablo +}; + struct TagInfo { std::array uuid; u8 uuid_length; INSERT_PADDING_BYTES(0x15); - u32 protocol; + s32 protocol; u32 tag_type; INSERT_PADDING_BYTES(0x30); }; @@ -77,10 +109,13 @@ static_assert(sizeof(CommonInfo) == 0x40, "CommonInfo is an invalid size"); struct ModelInfo { u16 character_id; u8 character_variant; - u8 figure_type; + AmiiboType amiibo_type; u16 model_number; - u8 series; - INSERT_PADDING_BYTES(0x39); + AmiiboSeries series; + u8 fixed; // Must be 02 + INSERT_PADDING_BYTES(0x4); // Unknown + INSERT_PADDING_BYTES(0x20); // Probably a SHA256-(HMAC?) hash + INSERT_PADDING_BYTES(0x14); // SHA256-HMAC }; static_assert(sizeof(ModelInfo) == 0x40, "ModelInfo is an invalid size"); @@ -105,11 +140,21 @@ public: struct AmiiboFile { std::array uuid; - INSERT_PADDING_BYTES(0x4); // Compability container - INSERT_PADDING_BYTES(0x46); - ModelInfo model_info; + u16 uuid_lock; // Must be 0F E0 + u32 compability_container; // Must be F1 10 FF EE + u16 crypto_init; // Must be A5 XX + u16 write_count; // Number of times the amiibo has been written? + INSERT_PADDING_BYTES(0x20); // System crypts + INSERT_PADDING_BYTES(0x20); // SHA256-(HMAC?) hash + ModelInfo model_info; // This struct is bigger than documentation + INSERT_PADDING_BYTES(0xC); // SHA256-HMAC + INSERT_PADDING_BYTES(0x114); // section 1 encrypted buffer + INSERT_PADDING_BYTES(0x54); // section 2 encrypted buffer + u32 tag_dynamic_lock; // Must be 01 00 0F XX + u32 tag_CFG0; // Must be 00 00 00 04 + u32 tag_CFG1; // Must be 50 00 00 00 }; - static_assert(sizeof(AmiiboFile) == 0x94, "AmiiboFile is an invalid size"); + static_assert(sizeof(AmiiboFile) == 0x214, "AmiiboFile is an invalid size"); void CreateUserInterface(Kernel::HLERequestContext& ctx); bool LoadAmiibo(const std::vector& buffer); @@ -118,7 +163,7 @@ public: void Initialize(); void Finalize(); - ResultCode StartDetection(); + ResultCode StartDetection(s32 protocol_); ResultCode StopDetection(); ResultCode Mount(); ResultCode Unmount(); @@ -144,6 +189,12 @@ public: std::shared_ptr module; private: + /// Validates that the amiibo file is not corrupted + bool IsAmiiboValid() const; + bool AmiiboApplicationDataExist(u32 access_id) const; + const std::vector LoadAmiiboApplicationData(u32 access_id) const; + void SaveAmiiboApplicationData(u32 access_id, const std::vector& data) const; + const Core::HID::NpadIdType npad_id; DeviceState device_state{DeviceState::Unaviable}; @@ -151,7 +202,7 @@ public: Kernel::KEvent* activate_event; Kernel::KEvent* deactivate_event; AmiiboFile amiibo{}; - u16 write_counter{}; + s32 protocol; bool is_application_area_initialized{}; u32 application_area_id; std::vector application_area_data; From e35c2fd5d0b58742d5da6682b3fb7ee4bc00204d Mon Sep 17 00:00:00 2001 From: german77 Date: Tue, 8 Feb 2022 16:53:53 -0600 Subject: [PATCH 6/8] nfp: Address compiler issues --- src/core/hle/service/nfp/nfp.cpp | 47 ++++++++++++++++---------------- src/yuzu/main.cpp | 3 +- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index 1e91aa340..459fa798f 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -20,7 +20,6 @@ namespace ErrCodes { constexpr ResultCode DeviceNotFound(ErrorModule::NFP, 64); constexpr ResultCode WrongDeviceState(ErrorModule::NFP, 73); constexpr ResultCode ApplicationAreaIsNotInitialized(ErrorModule::NFP, 128); -constexpr ResultCode NoApplicationArea(ErrorModule::NFP, 152); constexpr ResultCode ApplicationAreaExist(ErrorModule::NFP, 168); } // namespace ErrCodes @@ -97,7 +96,7 @@ void IUser::ListDevices(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 3}; rb.Push(ResultSuccess); - rb.Push(devices.size()); + rb.Push(static_cast(devices.size())); } void IUser::StartDetection(Kernel::HLERequestContext& ctx) { @@ -184,7 +183,8 @@ void IUser::OpenApplicationArea(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; const auto device_handle{rp.Pop()}; const auto access_id{rp.Pop()}; - LOG_INFO(Service_NFP, "called, device_handle={}, access_id={}", device_handle, access_id); + LOG_WARNING(Service_NFP, "(STUBBED) called, device_handle={}, access_id={}", device_handle, + access_id); // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { @@ -212,7 +212,7 @@ void IUser::GetApplicationArea(Kernel::HLERequestContext& ctx) { ctx.WriteBuffer(data); IPC::ResponseBuilder rb{ctx, 3}; rb.Push(result); - rb.PushRaw(data.size()); + rb.Push(static_cast(data.size())); return; } @@ -226,7 +226,8 @@ void IUser::SetApplicationArea(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; const auto device_handle{rp.Pop()}; const auto data{ctx.ReadBuffer()}; - LOG_INFO(Service_NFP, "called, device_handle={}, data_size={}", device_handle, data.size()); + LOG_WARNING(Service_NFP, "(STUBBED) called, device_handle={}, data_size={}", device_handle, + data.size()); // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { @@ -247,8 +248,8 @@ void IUser::CreateApplicationArea(Kernel::HLERequestContext& ctx) { const auto device_handle{rp.Pop()}; const auto access_id{rp.Pop()}; const auto data{ctx.ReadBuffer()}; - LOG_INFO(Service_NFP, "called, device_handle={}, data_size={}, access_id={}", device_handle, - access_id, data.size()); + LOG_WARNING(Service_NFP, "(STUBBED) called, device_handle={}, data_size={}, access_id={}", + device_handle, access_id, data.size()); // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { @@ -338,7 +339,7 @@ void IUser::GetModelInfo(Kernel::HLERequestContext& ctx) { const auto result = nfp_interface.GetModelInfo(model_info); ctx.WriteBuffer(model_info); IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); + rb.Push(result); return; } @@ -488,7 +489,7 @@ bool Module::Interface::LoadAmiibo(const std::vector& buffer) { return false; } - LOG_INFO(Service_NFP, "New Amiibo detected"); + LOG_INFO(Service_NFP, "Amiibo detected"); std::memcpy(&amiibo, buffer.data(), sizeof(amiibo)); if (!IsAmiiboValid()) { @@ -510,21 +511,21 @@ void Module::Interface::CloseAmiibo() { } bool Module::Interface::IsAmiiboValid() const { - LOG_INFO(Service_NFP, "uuid_lock=0x{0:x}", amiibo.uuid_lock); - LOG_INFO(Service_NFP, "compability_container=0x{0:x}", amiibo.compability_container); - LOG_INFO(Service_NFP, "crypto_init=0x{0:x}", amiibo.crypto_init); - LOG_INFO(Service_NFP, "write_count={}", amiibo.write_count); + LOG_DEBUG(Service_NFP, "uuid_lock=0x{0:x}", amiibo.uuid_lock); + LOG_DEBUG(Service_NFP, "compability_container=0x{0:x}", amiibo.compability_container); + LOG_DEBUG(Service_NFP, "crypto_init=0x{0:x}", amiibo.crypto_init); + LOG_DEBUG(Service_NFP, "write_count={}", amiibo.write_count); - LOG_INFO(Service_NFP, "character_id=0x{0:x}", amiibo.model_info.character_id); - LOG_INFO(Service_NFP, "character_variant={}", amiibo.model_info.character_variant); - LOG_INFO(Service_NFP, "amiibo_type={}", amiibo.model_info.amiibo_type); - LOG_INFO(Service_NFP, "model_number=0x{0:x}", amiibo.model_info.model_number); - LOG_INFO(Service_NFP, "series={}", amiibo.model_info.series); - LOG_INFO(Service_NFP, "fixed_value=0x{0:x}", amiibo.model_info.fixed); + LOG_DEBUG(Service_NFP, "character_id=0x{0:x}", amiibo.model_info.character_id); + LOG_DEBUG(Service_NFP, "character_variant={}", amiibo.model_info.character_variant); + LOG_DEBUG(Service_NFP, "amiibo_type={}", amiibo.model_info.amiibo_type); + LOG_DEBUG(Service_NFP, "model_number=0x{0:x}", amiibo.model_info.model_number); + LOG_DEBUG(Service_NFP, "series={}", amiibo.model_info.series); + LOG_DEBUG(Service_NFP, "fixed_value=0x{0:x}", amiibo.model_info.fixed); - LOG_INFO(Service_NFP, "tag_dynamic_lock=0x{0:x}", amiibo.tag_dynamic_lock); - LOG_INFO(Service_NFP, "tag_CFG0=0x{0:x}", amiibo.tag_CFG0); - LOG_INFO(Service_NFP, "tag_CFG1=0x{0:x}", amiibo.tag_CFG1); + LOG_DEBUG(Service_NFP, "tag_dynamic_lock=0x{0:x}", amiibo.tag_dynamic_lock); + LOG_DEBUG(Service_NFP, "tag_CFG0=0x{0:x}", amiibo.tag_CFG0); + LOG_DEBUG(Service_NFP, "tag_CFG1=0x{0:x}", amiibo.tag_CFG1); // Check against all know constants on an amiibo binary if (amiibo.uuid_lock != 0xE00F) { @@ -701,7 +702,7 @@ ResultCode Module::Interface::OpenApplicationArea(u32 access_id) { is_application_area_initialized = true; } if (!is_application_area_initialized) { - LOG_ERROR(Service_NFP, "Application area is not initialized"); + LOG_WARNING(Service_NFP, "Application area is not initialized"); return ErrCodes::ApplicationAreaIsNotInitialized; } return ResultSuccess; diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 395527607..a67ab030b 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -2915,8 +2915,7 @@ void GMainWindow::OnLoadAmiibo() { Service::SM::ServiceManager& sm = system->ServiceManager(); auto nfc = sm.GetService("nfp:user"); if (nfc == nullptr) { - QMessageBox::warning(this, tr("Error"), - tr("The current game is not looking for amiibos")); + QMessageBox::warning(this, tr("Error"), tr("The current game is not looking for amiibos")); return; } const auto nfc_state = nfc->GetCurrentState(); From 6a1ad031530f506a1bc789c42d639dc11ce4f2ea Mon Sep 17 00:00:00 2001 From: Narr the Reg Date: Thu, 10 Feb 2022 10:58:37 -0600 Subject: [PATCH 7/8] nfp: Separate nfc tag from amiibo data --- src/core/hid/emulated_controller.h | 9 +++- src/core/hle/service/nfp/nfp.cpp | 70 +++++++++++++++++------------- src/core/hle/service/nfp/nfp.h | 37 +++++++++++----- 3 files changed, 74 insertions(+), 42 deletions(-) diff --git a/src/core/hid/emulated_controller.h b/src/core/hid/emulated_controller.h index 7785e6110..aa52f9572 100644 --- a/src/core/hid/emulated_controller.h +++ b/src/core/hid/emulated_controller.h @@ -299,16 +299,21 @@ public: /** * Sends a specific vibration to the output device - * @return returns true if vibration had no errors + * @return true if vibration had no errors */ bool SetVibration(std::size_t device_index, VibrationValue vibration); /** * Sends a small vibration to the output device - * @return returns true if SetVibration was successfull + * @return true if SetVibration was successfull */ bool TestVibration(std::size_t device_index); + /** + * Sets the desired data to be polled from a controller + * @param polling_mode type of input desired buttons, gyro, nfc, ir, etc. + * @return true if SetPollingMode was successfull + */ bool SetPollingMode(Common::Input::PollingMode polling_mode); /// Returns the led pattern corresponding to this emulated controller diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index 459fa798f..63e257975 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -479,7 +479,7 @@ void Module::Interface::CreateUserInterface(Kernel::HLERequestContext& ctx) { } bool Module::Interface::LoadAmiibo(const std::vector& buffer) { - if (buffer.size() < sizeof(AmiiboFile)) { + if (buffer.size() < sizeof(NTAG215File)) { LOG_ERROR(Service_NFP, "Wrong file size"); return false; } @@ -490,12 +490,15 @@ bool Module::Interface::LoadAmiibo(const std::vector& buffer) { } LOG_INFO(Service_NFP, "Amiibo detected"); - std::memcpy(&amiibo, buffer.data(), sizeof(amiibo)); + std::memcpy(&tag_data, buffer.data(), sizeof(tag_data)); if (!IsAmiiboValid()) { return false; } + // This value can't be dumped from a tag. Generate it + tag_data.PWD = GetTagPassword(tag_data.uuid); + device_state = DeviceState::TagFound; activate_event->GetWritableEvent().Signal(); return true; @@ -511,42 +514,43 @@ void Module::Interface::CloseAmiibo() { } bool Module::Interface::IsAmiiboValid() const { - LOG_DEBUG(Service_NFP, "uuid_lock=0x{0:x}", amiibo.uuid_lock); - LOG_DEBUG(Service_NFP, "compability_container=0x{0:x}", amiibo.compability_container); - LOG_DEBUG(Service_NFP, "crypto_init=0x{0:x}", amiibo.crypto_init); - LOG_DEBUG(Service_NFP, "write_count={}", amiibo.write_count); + const auto& amiibo_data = tag_data.user_memory; + LOG_DEBUG(Service_NFP, "uuid_lock=0x{0:x}", tag_data.lock_bytes); + LOG_DEBUG(Service_NFP, "compability_container=0x{0:x}", tag_data.compability_container); + LOG_DEBUG(Service_NFP, "crypto_init=0x{0:x}", amiibo_data.crypto_init); + LOG_DEBUG(Service_NFP, "write_count={}", amiibo_data.write_count); - LOG_DEBUG(Service_NFP, "character_id=0x{0:x}", amiibo.model_info.character_id); - LOG_DEBUG(Service_NFP, "character_variant={}", amiibo.model_info.character_variant); - LOG_DEBUG(Service_NFP, "amiibo_type={}", amiibo.model_info.amiibo_type); - LOG_DEBUG(Service_NFP, "model_number=0x{0:x}", amiibo.model_info.model_number); - LOG_DEBUG(Service_NFP, "series={}", amiibo.model_info.series); - LOG_DEBUG(Service_NFP, "fixed_value=0x{0:x}", amiibo.model_info.fixed); + LOG_DEBUG(Service_NFP, "character_id=0x{0:x}", amiibo_data.model_info.character_id); + LOG_DEBUG(Service_NFP, "character_variant={}", amiibo_data.model_info.character_variant); + LOG_DEBUG(Service_NFP, "amiibo_type={}", amiibo_data.model_info.amiibo_type); + LOG_DEBUG(Service_NFP, "model_number=0x{0:x}", amiibo_data.model_info.model_number); + LOG_DEBUG(Service_NFP, "series={}", amiibo_data.model_info.series); + LOG_DEBUG(Service_NFP, "fixed_value=0x{0:x}", amiibo_data.model_info.fixed); - LOG_DEBUG(Service_NFP, "tag_dynamic_lock=0x{0:x}", amiibo.tag_dynamic_lock); - LOG_DEBUG(Service_NFP, "tag_CFG0=0x{0:x}", amiibo.tag_CFG0); - LOG_DEBUG(Service_NFP, "tag_CFG1=0x{0:x}", amiibo.tag_CFG1); + LOG_DEBUG(Service_NFP, "tag_dynamic_lock=0x{0:x}", tag_data.dynamic_lock); + LOG_DEBUG(Service_NFP, "tag_CFG0=0x{0:x}", tag_data.CFG0); + LOG_DEBUG(Service_NFP, "tag_CFG1=0x{0:x}", tag_data.CFG1); // Check against all know constants on an amiibo binary - if (amiibo.uuid_lock != 0xE00F) { + if (tag_data.lock_bytes != 0xE00F) { return false; } - if (amiibo.compability_container != 0xEEFF10F1UL) { + if (tag_data.compability_container != 0xEEFF10F1U) { return false; } - if ((amiibo.crypto_init & 0xFF) != 0xA5) { + if ((amiibo_data.crypto_init & 0xFF) != 0xA5) { return false; } - if (amiibo.model_info.fixed != 0x02) { + if (amiibo_data.model_info.fixed != 0x02) { return false; } - if ((amiibo.tag_dynamic_lock & 0xFFFFFF) != 0x0F0001) { + if ((tag_data.dynamic_lock & 0xFFFFFF) != 0x0F0001) { return false; } - if (amiibo.tag_CFG0 != 0x04000000UL) { + if (tag_data.CFG0 != 0x04000000U) { return false; } - if (amiibo.tag_CFG1 != 0x5F) { + if (tag_data.CFG1 != 0x5F) { return false; } return true; @@ -629,12 +633,11 @@ ResultCode Module::Interface::Unmount() { ResultCode Module::Interface::GetTagInfo(TagInfo& tag_info) const { if (device_state == DeviceState::TagFound || device_state == DeviceState::TagMounted) { - // Read this data from the amiibo save file tag_info = { - .uuid = amiibo.uuid, - .uuid_length = static_cast(amiibo.uuid.size()), + .uuid = tag_data.uuid, + .uuid_length = static_cast(tag_data.uuid.size()), .protocol = protocol, - .tag_type = static_cast(amiibo.model_info.amiibo_type), + .tag_type = static_cast(tag_data.user_memory.model_info.amiibo_type), }; return ResultSuccess; } @@ -654,7 +657,7 @@ ResultCode Module::Interface::GetCommonInfo(CommonInfo& common_info) const { .last_write_year = 2022, .last_write_month = 2, .last_write_day = 7, - .write_counter = amiibo.write_count, + .write_counter = tag_data.user_memory.write_count, .version = 1, .application_area_size = ApplicationAreaSize, }; @@ -667,7 +670,7 @@ ResultCode Module::Interface::GetModelInfo(ModelInfo& model_info) const { return ErrCodes::WrongDeviceState; } - model_info = amiibo.model_info; + model_info = tag_data.user_memory.model_info; return ResultSuccess; } @@ -757,7 +760,7 @@ bool Module::Interface::AmiiboApplicationDataExist(u32 access_id) const { return false; } -const std::vector Module::Interface::LoadAmiiboApplicationData(u32 access_id) const { +std::vector Module::Interface::LoadAmiiboApplicationData(u32 access_id) const { // TODO(german77): Read file std::vector data(ApplicationAreaSize); return data; @@ -781,6 +784,15 @@ Core::HID::NpadIdType Module::Interface::GetNpadId() const { return npad_id; } +u32 Module::Interface::GetTagPassword(const TagUuid& uuid) const { + // Verifiy that the generated password is correct + u32 password = 0xAA ^ (uuid[1] ^ uuid[3]); + password &= (0x55 ^ (uuid[2] ^ uuid[4])) << 8; + password &= (0xAA ^ (uuid[3] ^ uuid[5])) << 16; + password &= (0x55 ^ (uuid[4] ^ uuid[6])) << 24; + return password; +} + void InstallInterfaces(SM::ServiceManager& service_manager, Core::System& system) { auto module = std::make_shared(); std::make_shared(module, system)->InstallAsService(service_manager); diff --git a/src/core/hle/service/nfp/nfp.h b/src/core/hle/service/nfp/nfp.h index 633539dcc..bc3b1967f 100644 --- a/src/core/hle/service/nfp/nfp.h +++ b/src/core/hle/service/nfp/nfp.h @@ -7,6 +7,7 @@ #include #include +#include "common/common_funcs.h" #include "core/hle/service/kernel_helpers.h" #include "core/hle/service/mii/mii_manager.h" #include "core/hle/service/service.h" @@ -85,8 +86,10 @@ enum class AmiiboSeries : u8 { Diablo }; +using TagUuid = std::array; + struct TagInfo { - std::array uuid; + TagUuid uuid; u8 uuid_length; INSERT_PADDING_BYTES(0x15); s32 protocol; @@ -138,10 +141,7 @@ public: const char* name); ~Interface() override; - struct AmiiboFile { - std::array uuid; - u16 uuid_lock; // Must be 0F E0 - u32 compability_container; // Must be F1 10 FF EE + struct EncryptedAmiiboFile { u16 crypto_init; // Must be A5 XX u16 write_count; // Number of times the amiibo has been written? INSERT_PADDING_BYTES(0x20); // System crypts @@ -150,11 +150,22 @@ public: INSERT_PADDING_BYTES(0xC); // SHA256-HMAC INSERT_PADDING_BYTES(0x114); // section 1 encrypted buffer INSERT_PADDING_BYTES(0x54); // section 2 encrypted buffer - u32 tag_dynamic_lock; // Must be 01 00 0F XX - u32 tag_CFG0; // Must be 00 00 00 04 - u32 tag_CFG1; // Must be 50 00 00 00 }; - static_assert(sizeof(AmiiboFile) == 0x214, "AmiiboFile is an invalid size"); + static_assert(sizeof(EncryptedAmiiboFile) == 0x1F8, "AmiiboFile is an invalid size"); + + struct NTAG215File { + TagUuid uuid; // Unique serial number + u16 lock_bytes; // Set defined pages as read only + u32 compability_container; // Defines available memory + EncryptedAmiiboFile user_memory; // Writable data + u32 dynamic_lock; // Dynamic lock + u32 CFG0; // Defines memory protected by password + u32 CFG1; // Defines number of verification attempts + u32 PWD; // Password to allow write access + u16 PACK; // Password acknowledge reply + u16 RFUI; // Reserved for future use + }; + static_assert(sizeof(NTAG215File) == 0x21C, "NTAG215File is an invalid size"); void CreateUserInterface(Kernel::HLERequestContext& ctx); bool LoadAmiibo(const std::vector& buffer); @@ -191,17 +202,21 @@ public: private: /// Validates that the amiibo file is not corrupted bool IsAmiiboValid() const; + bool AmiiboApplicationDataExist(u32 access_id) const; - const std::vector LoadAmiiboApplicationData(u32 access_id) const; + std::vector LoadAmiiboApplicationData(u32 access_id) const; void SaveAmiiboApplicationData(u32 access_id, const std::vector& data) const; + /// return password needed to allow write access to protected memory + u32 GetTagPassword(const TagUuid& uuid) const; + const Core::HID::NpadIdType npad_id; DeviceState device_state{DeviceState::Unaviable}; KernelHelpers::ServiceContext service_context; Kernel::KEvent* activate_event; Kernel::KEvent* deactivate_event; - AmiiboFile amiibo{}; + NTAG215File tag_data{}; s32 protocol; bool is_application_area_initialized{}; u32 application_area_id; From b57d61010f347e74875f0c8a1003b4f84fa7b062 Mon Sep 17 00:00:00 2001 From: german77 Date: Sun, 13 Feb 2022 11:59:47 -0600 Subject: [PATCH 8/8] nfp: Allow files without password data --- src/core/hle/service/nfp/nfp.cpp | 24 +++++++++++++++++------- src/core/hle/service/nfp/nfp.h | 11 ++++++++--- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index 63e257975..513107715 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -479,25 +479,35 @@ void Module::Interface::CreateUserInterface(Kernel::HLERequestContext& ctx) { } bool Module::Interface::LoadAmiibo(const std::vector& buffer) { - if (buffer.size() < sizeof(NTAG215File)) { - LOG_ERROR(Service_NFP, "Wrong file size"); - return false; - } - if (device_state != DeviceState::SearchingForTag) { LOG_ERROR(Service_NFP, "Game is not looking for amiibos, current state {}", device_state); return false; } + constexpr auto tag_size = sizeof(NTAG215File); + constexpr auto tag_size_without_password = sizeof(NTAG215File) - sizeof(NTAG215Password); + + std::vector amiibo_buffer = buffer; + + if (amiibo_buffer.size() < tag_size_without_password) { + LOG_ERROR(Service_NFP, "Wrong file size {}", buffer.size()); + return false; + } + + // Ensure it has the correct size + if (amiibo_buffer.size() != tag_size) { + amiibo_buffer.resize(tag_size, 0); + } + LOG_INFO(Service_NFP, "Amiibo detected"); - std::memcpy(&tag_data, buffer.data(), sizeof(tag_data)); + std::memcpy(&tag_data, buffer.data(), tag_size); if (!IsAmiiboValid()) { return false; } // This value can't be dumped from a tag. Generate it - tag_data.PWD = GetTagPassword(tag_data.uuid); + tag_data.password.PWD = GetTagPassword(tag_data.uuid); device_state = DeviceState::TagFound; activate_event->GetWritableEvent().Signal(); diff --git a/src/core/hle/service/nfp/nfp.h b/src/core/hle/service/nfp/nfp.h index bc3b1967f..022f13b29 100644 --- a/src/core/hle/service/nfp/nfp.h +++ b/src/core/hle/service/nfp/nfp.h @@ -153,6 +153,13 @@ public: }; static_assert(sizeof(EncryptedAmiiboFile) == 0x1F8, "AmiiboFile is an invalid size"); + struct NTAG215Password { + u32 PWD; // Password to allow write access + u16 PACK; // Password acknowledge reply + u16 RFUI; // Reserved for future use + }; + static_assert(sizeof(NTAG215Password) == 0x8, "NTAG215Password is an invalid size"); + struct NTAG215File { TagUuid uuid; // Unique serial number u16 lock_bytes; // Set defined pages as read only @@ -161,9 +168,7 @@ public: u32 dynamic_lock; // Dynamic lock u32 CFG0; // Defines memory protected by password u32 CFG1; // Defines number of verification attempts - u32 PWD; // Password to allow write access - u16 PACK; // Password acknowledge reply - u16 RFUI; // Reserved for future use + NTAG215Password password; // Password data }; static_assert(sizeof(NTAG215File) == 0x21C, "NTAG215File is an invalid size");