From 33a2113b7108908605f6a5e946cdb83078c80f68 Mon Sep 17 00:00:00 2001
From: Narr the Reg <juangerman-13@hotmail.com>
Date: Tue, 8 Aug 2023 15:00:53 -0600
Subject: [PATCH] service: nfp: Fix size and increase timeout (#6868)

---
 src/core/hle/service/nfc/nfc.cpp        | 27 ++++++++++++++++---------
 src/core/hle/service/nfc/nfc_device.cpp |  4 ++--
 2 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/src/core/hle/service/nfc/nfc.cpp b/src/core/hle/service/nfc/nfc.cpp
index 62f82d8ba..f1e2801fb 100644
--- a/src/core/hle/service/nfc/nfc.cpp
+++ b/src/core/hle/service/nfc/nfc.cpp
@@ -316,7 +316,7 @@ void Module::Interface::GetTagInfo2(Kernel::HLERequestContext& ctx) {
 
     if (nfc->nfc_mode == CommunicationMode::TrainTag) {
         LOG_ERROR(Service_NFC, "CommunicationMode  {} not implemented", nfc->nfc_mode);
-        IPC::RequestBuilder rb = rp.MakeBuilder(26, 0);
+        IPC::RequestBuilder rb = rp.MakeBuilder(25, 0);
         rb.Push(RESULT_SUCCESS);
         rb.PushRaw<TagInfo2>({});
         return;
@@ -324,7 +324,7 @@ void Module::Interface::GetTagInfo2(Kernel::HLERequestContext& ctx) {
 
     TagInfo2 tag_info{};
     const auto result = nfc->device->GetTagInfo2(tag_info);
-    IPC::RequestBuilder rb = rp.MakeBuilder(26, 0);
+    IPC::RequestBuilder rb = rp.MakeBuilder(25, 0);
     rb.Push(result);
     rb.PushRaw<TagInfo2>(tag_info);
 }
@@ -383,10 +383,14 @@ void Module::Interface::OpenApplicationArea(Kernel::HLERequestContext& ctx) {
 void Module::Interface::CreateApplicationArea(Kernel::HLERequestContext& ctx) {
     IPC::RequestParser rp(ctx);
     u32 access_id = rp.Pop<u32>();
-    [[maybe_unused]] u32 size = rp.Pop<u32>();
+    u32 size = rp.Pop<u32>();
     std::vector<u8> buffer = rp.PopStaticBuffer();
 
-    LOG_CRITICAL(Service_NFC, "called, size={}", size);
+    LOG_INFO(Service_NFC, "called, size={}", size);
+
+    if (buffer.size() > size) {
+        buffer.resize(size);
+    }
 
     if (nfc->nfc_mode != CommunicationMode::Amiibo) {
         IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
@@ -402,8 +406,9 @@ void Module::Interface::CreateApplicationArea(Kernel::HLERequestContext& ctx) {
 
 void Module::Interface::ReadApplicationArea(Kernel::HLERequestContext& ctx) {
     IPC::RequestParser rp(ctx);
+    u32 size = rp.Pop<u32>();
 
-    LOG_INFO(Service_NFC, "called");
+    LOG_INFO(Service_NFC, "called, size={}", size);
 
     nfc->device->RescheduleTagRemoveEvent();
 
@@ -413,7 +418,7 @@ void Module::Interface::ReadApplicationArea(Kernel::HLERequestContext& ctx) {
         return;
     }
 
-    std::vector<u8> buffer(sizeof(ApplicationArea));
+    std::vector<u8> buffer(size);
     const auto result = nfc->device->GetApplicationArea(buffer);
 
     IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
@@ -423,11 +428,15 @@ void Module::Interface::ReadApplicationArea(Kernel::HLERequestContext& ctx) {
 
 void Module::Interface::WriteApplicationArea(Kernel::HLERequestContext& ctx) {
     IPC::RequestParser rp(ctx);
-    [[maybe_unused]] u32 size = rp.Pop<u32>();
+    u32 size = rp.Pop<u32>();
     std::vector<u8> tag_uuid_info = rp.PopStaticBuffer();
     std::vector<u8> buffer = rp.PopStaticBuffer();
 
-    LOG_CRITICAL(Service_NFC, "called, size={}", size);
+    LOG_INFO(Service_NFC, "called, size={}", size);
+
+    if (buffer.size() > size) {
+        buffer.resize(size);
+    }
 
     if (nfc->nfc_mode != CommunicationMode::Amiibo) {
         IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
@@ -540,7 +549,7 @@ void Module::Interface::GetIdentificationBlock(Kernel::HLERequestContext& ctx) {
     ModelInfo model_info{};
     const auto result = nfc->device->GetModelInfo(model_info);
 
-    IPC::RequestBuilder rb = rp.MakeBuilder(0x1F, 0);
+    IPC::RequestBuilder rb = rp.MakeBuilder(14, 0);
     rb.Push(result);
     rb.PushRaw<ModelInfo>(model_info);
 }
diff --git a/src/core/hle/service/nfc/nfc_device.cpp b/src/core/hle/service/nfc/nfc_device.cpp
index 18ac135cc..107ff8101 100644
--- a/src/core/hle/service/nfc/nfc_device.cpp
+++ b/src/core/hle/service/nfc/nfc_device.cpp
@@ -1101,8 +1101,8 @@ void NfcDevice::BuildAmiiboWithoutKeys() {
 }
 
 void NfcDevice::RescheduleTagRemoveEvent() {
-    /// The interval at which the amiibo will be removed automatically 1.5s
-    static constexpr u64 amiibo_removal_interval = nsToCycles(1500 * 1000 * 1000);
+    /// The interval at which the amiibo will be removed automatically 3s
+    static constexpr u64 amiibo_removal_interval = msToCycles(3 * 1000);
 
     system.CoreTiming().UnscheduleEvent(remove_amiibo_event, 0);