summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar liamwhite2023-03-27 12:16:24 -0400
committerGravatar GitHub2023-03-27 12:16:24 -0400
commit3b30f5d8235a8b1118ffb9da932ffd3e6e15a3f6 (patch)
treee267b6cd31eebc440c6accdaacd6927824381502
parentMerge pull request #9994 from liamwhite/integer-constant (diff)
parentservice: nfp: Add plain amiibo support (diff)
downloadyuzu-3b30f5d8235a8b1118ffb9da932ffd3e6e15a3f6.tar.gz
yuzu-3b30f5d8235a8b1118ffb9da932ffd3e6e15a3f6.tar.xz
yuzu-3b30f5d8235a8b1118ffb9da932ffd3e6e15a3f6.zip
Merge pull request #9995 from german77/plain
service: nfp: Add plain amiibo support
Diffstat (limited to '')
-rw-r--r--src/core/hle/service/nfp/amiibo_crypto.cpp4
-rw-r--r--src/core/hle/service/nfp/amiibo_crypto.h3
-rw-r--r--src/core/hle/service/nfp/nfp_device.cpp34
-rw-r--r--src/core/hle/service/nfp/nfp_device.h1
-rw-r--r--src/core/hle/service/nfp/nfp_types.h3
5 files changed, 37 insertions, 8 deletions
diff --git a/src/core/hle/service/nfp/amiibo_crypto.cpp b/src/core/hle/service/nfp/amiibo_crypto.cpp
index bba862fb2..a3622e792 100644
--- a/src/core/hle/service/nfp/amiibo_crypto.cpp
+++ b/src/core/hle/service/nfp/amiibo_crypto.cpp
@@ -70,6 +70,10 @@ bool IsAmiiboValid(const EncryptedNTAG215File& ntag_file) {
70 return true; 70 return true;
71} 71}
72 72
73bool IsAmiiboValid(const NTAG215File& ntag_file) {
74 return IsAmiiboValid(EncodedDataToNfcData(ntag_file));
75}
76
73NTAG215File NfcDataToEncodedData(const EncryptedNTAG215File& nfc_data) { 77NTAG215File NfcDataToEncodedData(const EncryptedNTAG215File& nfc_data) {
74 NTAG215File encoded_data{}; 78 NTAG215File encoded_data{};
75 79
diff --git a/src/core/hle/service/nfp/amiibo_crypto.h b/src/core/hle/service/nfp/amiibo_crypto.h
index c9fd67a39..f6208ee6b 100644
--- a/src/core/hle/service/nfp/amiibo_crypto.h
+++ b/src/core/hle/service/nfp/amiibo_crypto.h
@@ -60,6 +60,9 @@ static_assert(sizeof(DerivedKeys) == 0x30, "DerivedKeys is an invalid size");
60/// Validates that the amiibo file is not corrupted 60/// Validates that the amiibo file is not corrupted
61bool IsAmiiboValid(const EncryptedNTAG215File& ntag_file); 61bool IsAmiiboValid(const EncryptedNTAG215File& ntag_file);
62 62
63/// Validates that the amiibo file is not corrupted
64bool IsAmiiboValid(const NTAG215File& ntag_file);
65
63/// Converts from encrypted file format to encoded file format 66/// Converts from encrypted file format to encoded file format
64NTAG215File NfcDataToEncodedData(const EncryptedNTAG215File& nfc_data); 67NTAG215File NfcDataToEncodedData(const EncryptedNTAG215File& nfc_data);
65 68
diff --git a/src/core/hle/service/nfp/nfp_device.cpp b/src/core/hle/service/nfp/nfp_device.cpp
index 5990e1473..607e70968 100644
--- a/src/core/hle/service/nfp/nfp_device.cpp
+++ b/src/core/hle/service/nfp/nfp_device.cpp
@@ -121,7 +121,16 @@ bool NfpDevice::LoadAmiibo(std::span<const u8> data) {
121 121
122 // TODO: Filter by allowed_protocols here 122 // TODO: Filter by allowed_protocols here
123 123
124 memcpy(&encrypted_tag_data, data.data(), sizeof(EncryptedNTAG215File)); 124 memcpy(&tag_data, data.data(), sizeof(EncryptedNTAG215File));
125 is_plain_amiibo = AmiiboCrypto::IsAmiiboValid(tag_data);
126
127 if (is_plain_amiibo) {
128 encrypted_tag_data = AmiiboCrypto::EncodedDataToNfcData(tag_data);
129 LOG_INFO(Service_NFP, "Using plain amiibo");
130 } else {
131 tag_data = {};
132 memcpy(&encrypted_tag_data, data.data(), sizeof(EncryptedNTAG215File));
133 }
125 134
126 device_state = DeviceState::TagFound; 135 device_state = DeviceState::TagFound;
127 deactivate_event->GetReadableEvent().Clear(); 136 deactivate_event->GetReadableEvent().Clear();
@@ -232,13 +241,17 @@ Result NfpDevice::Flush() {
232 241
233 tag_data.write_counter++; 242 tag_data.write_counter++;
234 243
235 if (!AmiiboCrypto::EncodeAmiibo(tag_data, encrypted_tag_data)) { 244 std::vector<u8> data(sizeof(EncryptedNTAG215File));
236 LOG_ERROR(Service_NFP, "Failed to encode data"); 245 if (is_plain_amiibo) {
237 return WriteAmiiboFailed; 246 memcpy(data.data(), &tag_data, sizeof(tag_data));
238 } 247 } else {
248 if (!AmiiboCrypto::EncodeAmiibo(tag_data, encrypted_tag_data)) {
249 LOG_ERROR(Service_NFP, "Failed to encode data");
250 return WriteAmiiboFailed;
251 }
239 252
240 std::vector<u8> data(sizeof(encrypted_tag_data)); 253 memcpy(data.data(), &encrypted_tag_data, sizeof(encrypted_tag_data));
241 memcpy(data.data(), &encrypted_tag_data, sizeof(encrypted_tag_data)); 254 }
242 255
243 if (!npad_device->WriteNfc(data)) { 256 if (!npad_device->WriteNfc(data)) {
244 LOG_ERROR(Service_NFP, "Error writing to file"); 257 LOG_ERROR(Service_NFP, "Error writing to file");
@@ -256,6 +269,13 @@ Result NfpDevice::Mount(MountTarget mount_target_) {
256 return WrongDeviceState; 269 return WrongDeviceState;
257 } 270 }
258 271
272 // The loaded amiibo is not encrypted
273 if (is_plain_amiibo) {
274 device_state = DeviceState::TagMounted;
275 mount_target = mount_target_;
276 return ResultSuccess;
277 }
278
259 if (!AmiiboCrypto::IsAmiiboValid(encrypted_tag_data)) { 279 if (!AmiiboCrypto::IsAmiiboValid(encrypted_tag_data)) {
260 LOG_ERROR(Service_NFP, "Not an amiibo"); 280 LOG_ERROR(Service_NFP, "Not an amiibo");
261 return NotAnAmiibo; 281 return NotAnAmiibo;
diff --git a/src/core/hle/service/nfp/nfp_device.h b/src/core/hle/service/nfp/nfp_device.h
index 27122e86e..7f963730d 100644
--- a/src/core/hle/service/nfp/nfp_device.h
+++ b/src/core/hle/service/nfp/nfp_device.h
@@ -95,6 +95,7 @@ private:
95 bool is_initalized{}; 95 bool is_initalized{};
96 bool is_data_moddified{}; 96 bool is_data_moddified{};
97 bool is_app_area_open{}; 97 bool is_app_area_open{};
98 bool is_plain_amiibo{};
98 TagProtocol allowed_protocols{}; 99 TagProtocol allowed_protocols{};
99 s64 current_posix_time{}; 100 s64 current_posix_time{};
100 MountTarget mount_target{MountTarget::None}; 101 MountTarget mount_target{MountTarget::None};
diff --git a/src/core/hle/service/nfp/nfp_types.h b/src/core/hle/service/nfp/nfp_types.h
index b3599a513..70c878552 100644
--- a/src/core/hle/service/nfp/nfp_types.h
+++ b/src/core/hle/service/nfp/nfp_types.h
@@ -309,7 +309,8 @@ struct EncryptedNTAG215File {
309 u32 CFG1; // Defines number of verification attempts 309 u32 CFG1; // Defines number of verification attempts
310 NTAG215Password password; // Password data 310 NTAG215Password password; // Password data
311}; 311};
312static_assert(sizeof(EncryptedNTAG215File) == 0x21C, "EncryptedNTAG215File is an invalid size"); 312static_assert(sizeof(EncryptedNTAG215File) == sizeof(NTAG215File),
313 "EncryptedNTAG215File is an invalid size");
313static_assert(std::is_trivially_copyable_v<EncryptedNTAG215File>, 314static_assert(std::is_trivially_copyable_v<EncryptedNTAG215File>,
314 "EncryptedNTAG215File must be trivially copyable."); 315 "EncryptedNTAG215File must be trivially copyable.");
315 316