diff options
author | Narr the Reg <juangerman-13@hotmail.com> | 2022-10-04 01:06:55 +0200 |
---|---|---|
committer | Narr the Reg <juangerman-13@hotmail.com> | 2022-10-04 01:06:55 +0200 |
commit | e85c19adcb71149922332150d45d671f90bd1f9f (patch) | |
tree | 39797c4c6e70786fe2e4a6aba643c48db8f33be7 /src | |
parent | Merge pull request #8955 from german77/amiibo-rewrite (diff) | |
download | yuzu-e85c19adcb71149922332150d45d671f90bd1f9f.tar yuzu-e85c19adcb71149922332150d45d671f90bd1f9f.tar.gz yuzu-e85c19adcb71149922332150d45d671f90bd1f9f.tar.bz2 yuzu-e85c19adcb71149922332150d45d671f90bd1f9f.tar.lz yuzu-e85c19adcb71149922332150d45d671f90bd1f9f.tar.xz yuzu-e85c19adcb71149922332150d45d671f90bd1f9f.tar.zst yuzu-e85c19adcb71149922332150d45d671f90bd1f9f.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/core/hid/emulated_controller.cpp | 6 | ||||
-rw-r--r-- | src/core/hle/service/nfp/amiibo_crypto.cpp | 4 | ||||
-rw-r--r-- | src/core/hle/service/nfp/nfp_device.cpp | 65 | ||||
-rw-r--r-- | src/core/hle/service/nfp/nfp_result.h | 2 | ||||
-rw-r--r-- | src/core/hle/service/nfp/nfp_types.h | 47 | ||||
-rw-r--r-- | src/core/hle/service/nfp/nfp_user.cpp | 30 |
6 files changed, 112 insertions, 42 deletions
diff --git a/src/core/hid/emulated_controller.cpp b/src/core/hid/emulated_controller.cpp index e27d84734..025f1c78e 100644 --- a/src/core/hid/emulated_controller.cpp +++ b/src/core/hid/emulated_controller.cpp @@ -1017,9 +1017,11 @@ bool EmulatedController::SetPollingMode(Common::Input::PollingMode polling_mode) auto& output_device = output_devices[static_cast<std::size_t>(DeviceIndex::Right)]; auto& nfc_output_device = output_devices[3]; - nfc_output_device->SetPollingMode(polling_mode); + const auto virtual_nfc_result = nfc_output_device->SetPollingMode(polling_mode); + const auto mapped_nfc_result = output_device->SetPollingMode(polling_mode); - return output_device->SetPollingMode(polling_mode) == Common::Input::PollingError::None; + return virtual_nfc_result == Common::Input::PollingError::None || + mapped_nfc_result == Common::Input::PollingError::None; } bool EmulatedController::SetCameraFormat( diff --git a/src/core/hle/service/nfp/amiibo_crypto.cpp b/src/core/hle/service/nfp/amiibo_crypto.cpp index ce0bc3f75..c32a6816b 100644 --- a/src/core/hle/service/nfp/amiibo_crypto.cpp +++ b/src/core/hle/service/nfp/amiibo_crypto.cpp @@ -28,7 +28,7 @@ bool IsAmiiboValid(const EncryptedNTAG215File& ntag_file) { LOG_DEBUG(Service_NFP, "model_number=0x{0:x}", static_cast<u16>(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.constant_value); + LOG_DEBUG(Service_NFP, "tag_type=0x{0:x}", amiibo_data.model_info.tag_type); LOG_DEBUG(Service_NFP, "tag_dynamic_lock=0x{0:x}", ntag_file.dynamic_lock); LOG_DEBUG(Service_NFP, "tag_CFG0=0x{0:x}", ntag_file.CFG0); @@ -55,7 +55,7 @@ bool IsAmiiboValid(const EncryptedNTAG215File& ntag_file) { if (amiibo_data.constant_value != 0xA5) { return false; } - if (amiibo_data.model_info.constant_value != 0x02) { + if (amiibo_data.model_info.tag_type != PackedTagType::Type2) { return false; } if ((ntag_file.dynamic_lock & 0xFFFFFF) != 0x0F0001U) { diff --git a/src/core/hle/service/nfp/nfp_device.cpp b/src/core/hle/service/nfp/nfp_device.cpp index 0d4ffd3a5..ec895ac01 100644 --- a/src/core/hle/service/nfp/nfp_device.cpp +++ b/src/core/hle/service/nfp/nfp_device.cpp @@ -98,11 +98,6 @@ bool NfpDevice::LoadAmiibo(std::span<const u8> data) { memcpy(&encrypted_tag_data, data.data(), sizeof(EncryptedNTAG215File)); - if (!AmiiboCrypto::IsAmiiboValid(encrypted_tag_data)) { - LOG_INFO(Service_NFP, "Invalid amiibo"); - return false; - } - device_state = DeviceState::TagFound; deactivate_event->GetReadableEvent().Clear(); activate_event->GetWritableEvent().Signal(); @@ -148,20 +143,28 @@ void NfpDevice::Finalize() { } Result NfpDevice::StartDetection(s32 protocol_) { - if (device_state == DeviceState::Initialized || device_state == DeviceState::TagRemoved) { - npad_device->SetPollingMode(Common::Input::PollingMode::NFC); - device_state = DeviceState::SearchingForTag; - protocol = protocol_; - return ResultSuccess; + if (device_state != DeviceState::Initialized && device_state != DeviceState::TagRemoved) { + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return WrongDeviceState; } - LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); - return WrongDeviceState; + if (!npad_device->SetPollingMode(Common::Input::PollingMode::NFC)) { + LOG_ERROR(Service_NFP, "Nfc not supported"); + return NfcDisabled; + } + + device_state = DeviceState::SearchingForTag; + protocol = protocol_; + return ResultSuccess; } Result NfpDevice::StopDetection() { npad_device->SetPollingMode(Common::Input::PollingMode::Active); + if (device_state == DeviceState::Initialized) { + return ResultSuccess; + } + if (device_state == DeviceState::TagFound || device_state == DeviceState::TagMounted) { CloseAmiibo(); return ResultSuccess; @@ -225,6 +228,11 @@ Result NfpDevice::Mount(MountTarget mount_target_) { return WrongDeviceState; } + if (!AmiiboCrypto::IsAmiiboValid(encrypted_tag_data)) { + LOG_ERROR(Service_NFP, "Not an amiibo"); + return NotAnAmiibo; + } + if (!AmiiboCrypto::DecodeAmiibo(encrypted_tag_data, tag_data)) { LOG_ERROR(Service_NFP, "Can't decode amiibo {}", device_state); return CorruptedData; @@ -238,6 +246,9 @@ Result NfpDevice::Mount(MountTarget mount_target_) { Result NfpDevice::Unmount() { if (device_state != DeviceState::TagMounted) { LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + if (device_state == DeviceState::TagRemoved) { + return TagRemoved; + } return WrongDeviceState; } @@ -256,6 +267,9 @@ Result NfpDevice::Unmount() { Result NfpDevice::GetTagInfo(TagInfo& tag_info) const { if (device_state != DeviceState::TagFound && device_state != DeviceState::TagMounted) { LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + if (device_state == DeviceState::TagRemoved) { + return TagRemoved; + } return WrongDeviceState; } @@ -287,12 +301,7 @@ Result NfpDevice::GetCommonInfo(CommonInfo& common_info) const { // TODO: Validate this data common_info = { - .last_write_date = - { - settings.write_date.GetYear(), - settings.write_date.GetMonth(), - settings.write_date.GetDay(), - }, + .last_write_date = settings.write_date.GetWriteDate(), .write_counter = tag_data.write_counter, .version = 0, .application_area_size = sizeof(ApplicationArea), @@ -303,6 +312,9 @@ Result NfpDevice::GetCommonInfo(CommonInfo& common_info) const { Result NfpDevice::GetModelInfo(ModelInfo& model_info) const { if (device_state != DeviceState::TagMounted) { LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + if (device_state == DeviceState::TagRemoved) { + return TagRemoved; + } return WrongDeviceState; } @@ -341,12 +353,7 @@ Result NfpDevice::GetRegisterInfo(RegisterInfo& register_info) const { // TODO: Validate this data register_info = { .mii_char_info = manager.ConvertV3ToCharInfo(tag_data.owner_mii), - .creation_date = - { - settings.init_date.GetYear(), - settings.init_date.GetMonth(), - settings.init_date.GetDay(), - }, + .creation_date = settings.init_date.GetWriteDate(), .amiibo_name = GetAmiiboName(settings), .font_region = {}, }; @@ -478,8 +485,7 @@ Result NfpDevice::GetApplicationArea(std::vector<u8>& data) const { } if (data.size() > sizeof(ApplicationArea)) { - LOG_ERROR(Service_NFP, "Wrong data size {}", data.size()); - return ResultUnknown; + data.resize(sizeof(ApplicationArea)); } memcpy(data.data(), tag_data.application_area.data(), data.size()); @@ -518,7 +524,7 @@ Result NfpDevice::SetApplicationArea(std::span<const u8> data) { Common::TinyMT rng{}; std::memcpy(tag_data.application_area.data(), data.data(), data.size()); - // HW seems to fill excess data with garbage + // Fill remaining data with random numbers rng.GenerateRandomBytes(tag_data.application_area.data() + data.size(), sizeof(ApplicationArea) - data.size()); @@ -561,12 +567,12 @@ Result NfpDevice::RecreateApplicationArea(u32 access_id, std::span<const u8> dat if (data.size() > sizeof(ApplicationArea)) { LOG_ERROR(Service_NFP, "Wrong data size {}", data.size()); - return ResultUnknown; + return WrongApplicationAreaSize; } Common::TinyMT rng{}; std::memcpy(tag_data.application_area.data(), data.data(), data.size()); - // HW seems to fill excess data with garbage + // Fill remaining data with random numbers rng.GenerateRandomBytes(tag_data.application_area.data() + data.size(), sizeof(ApplicationArea) - data.size()); @@ -612,7 +618,6 @@ u64 NfpDevice::GetHandle() const { } u32 NfpDevice::GetApplicationAreaSize() const { - // Investigate if this value is really constant return sizeof(ApplicationArea); } diff --git a/src/core/hle/service/nfp/nfp_result.h b/src/core/hle/service/nfp/nfp_result.h index ac259e2ff..d8e4cf094 100644 --- a/src/core/hle/service/nfp/nfp_result.h +++ b/src/core/hle/service/nfp/nfp_result.h @@ -8,6 +8,8 @@ namespace Service::NFP { constexpr Result DeviceNotFound(ErrorModule::NFP, 64); +constexpr Result InvalidArgument(ErrorModule::NFP, 65); +constexpr Result WrongApplicationAreaSize(ErrorModule::NFP, 68); constexpr Result WrongDeviceState(ErrorModule::NFP, 73); constexpr Result NfcDisabled(ErrorModule::NFP, 80); constexpr Result WriteAmiiboFailed(ErrorModule::NFP, 88); diff --git a/src/core/hle/service/nfp/nfp_types.h b/src/core/hle/service/nfp/nfp_types.h index dd4525b61..867ea2f36 100644 --- a/src/core/hle/service/nfp/nfp_types.h +++ b/src/core/hle/service/nfp/nfp_types.h @@ -84,6 +84,15 @@ enum class TagType : u32 { Type5, // ISO15693 RW/RO 540 bytes 106kbit/s }; +enum class PackedTagType : u8 { + None, + Type1, // ISO14443A RW 96-2k bytes 106kbit/s + Type2, // ISO14443A RW/RO 540 bytes 106kbit/s + Type3, // Sony Felica RW/RO 2k bytes 212kbit/s + Type4, // ISO14443A RW/RO 4k-32k bytes 424kbit/s + Type5, // ISO15693 RW/RO 540 bytes 106kbit/s +}; + enum class TagProtocol : u32 { None, TypeA, // ISO14443A @@ -104,6 +113,13 @@ struct TagUuid { }; static_assert(sizeof(TagUuid) == 10, "TagUuid is an invalid size"); +struct WriteDate { + u16 year; + u8 month; + u8 day; +}; +static_assert(sizeof(WriteDate) == 0x4, "WriteDate is an invalid size"); + struct AmiiboDate { u16 raw_date{}; @@ -121,6 +137,21 @@ struct AmiiboDate { return static_cast<u8>(GetValue() & 0x001F); } + WriteDate GetWriteDate() const { + if (!IsValidDate()) { + return { + .year = 2000, + .month = 1, + .day = 1, + }; + } + return { + .year = GetYear(), + .month = GetMonth(), + .day = GetDay(), + }; + } + void SetYear(u16 year) { const u16 year_converted = static_cast<u16>((year - 2000) << 9); raw_date = Common::swap16((GetValue() & ~0xFE00) | year_converted); @@ -133,6 +164,13 @@ struct AmiiboDate { const u16 day_converted = static_cast<u16>(day); raw_date = Common::swap16((GetValue() & ~0x001F) | day_converted); } + + bool IsValidDate() const { + const bool is_day_valid = GetDay() > 0 && GetDay() < 32; + const bool is_month_valid = GetMonth() >= 0 && GetMonth() < 13; + const bool is_year_valid = GetYear() >= 2000; + return is_year_valid && is_month_valid && is_day_valid; + } }; static_assert(sizeof(AmiiboDate) == 2, "AmiiboDate is an invalid size"); @@ -163,7 +201,7 @@ struct AmiiboModelInfo { AmiiboType amiibo_type; u16_be model_number; AmiiboSeries series; - u8 constant_value; // Must be 02 + PackedTagType tag_type; INSERT_PADDING_BYTES(0x4); // Unknown }; static_assert(sizeof(AmiiboModelInfo) == 0xC, "AmiiboModelInfo is an invalid size"); @@ -250,13 +288,6 @@ struct TagInfo { }; static_assert(sizeof(TagInfo) == 0x58, "TagInfo is an invalid size"); -struct WriteDate { - u16 year; - u8 month; - u8 day; -}; -static_assert(sizeof(WriteDate) == 0x4, "WriteDate is an invalid size"); - struct CommonInfo { WriteDate last_write_date; u16 write_counter; diff --git a/src/core/hle/service/nfp/nfp_user.cpp b/src/core/hle/service/nfp/nfp_user.cpp index c61df9401..4ed53b534 100644 --- a/src/core/hle/service/nfp/nfp_user.cpp +++ b/src/core/hle/service/nfp/nfp_user.cpp @@ -93,6 +93,18 @@ void IUser::ListDevices(Kernel::HLERequestContext& ctx) { return; } + if (!ctx.CanWriteBuffer()) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(InvalidArgument); + return; + } + + if (ctx.GetWriteBufferSize() == 0) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(InvalidArgument); + return; + } + std::vector<u64> nfp_devices; const std::size_t max_allowed_devices = ctx.GetWriteBufferSize() / sizeof(u64); @@ -255,6 +267,12 @@ void IUser::GetApplicationArea(Kernel::HLERequestContext& ctx) { return; } + if (!ctx.CanWriteBuffer()) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(InvalidArgument); + return; + } + auto device = GetNfpDevice(device_handle); if (!device.has_value()) { @@ -283,6 +301,12 @@ void IUser::SetApplicationArea(Kernel::HLERequestContext& ctx) { return; } + if (!ctx.CanReadBuffer()) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(InvalidArgument); + return; + } + auto device = GetNfpDevice(device_handle); if (!device.has_value()) { @@ -358,6 +382,12 @@ void IUser::CreateApplicationArea(Kernel::HLERequestContext& ctx) { return; } + if (!ctx.CanReadBuffer()) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(InvalidArgument); + return; + } + auto device = GetNfpDevice(device_handle); if (!device.has_value()) { |