diff options
Diffstat (limited to 'src/core/hle/service/nfp/nfp.cpp')
-rw-r--r-- | src/core/hle/service/nfp/nfp.cpp | 481 |
1 files changed, 345 insertions, 136 deletions
diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index 6c5b41dd1..4dba05a6a 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -4,6 +4,8 @@ #include <array> #include <atomic> +#include "common/fs/file.h" +#include "common/fs/path_util.h" #include "common/logging/log.h" #include "core/core.h" #include "core/hid/emulated_controller.h" @@ -12,6 +14,7 @@ #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/k_event.h" #include "core/hle/service/mii/mii_manager.h" +#include "core/hle/service/nfp/amiibo_crypto.h" #include "core/hle/service/nfp/nfp.h" #include "core/hle/service/nfp/nfp_user.h" @@ -19,12 +22,13 @@ namespace Service::NFP { namespace ErrCodes { constexpr Result DeviceNotFound(ErrorModule::NFP, 64); constexpr Result WrongDeviceState(ErrorModule::NFP, 73); +constexpr Result NfcDisabled(ErrorModule::NFP, 80); +constexpr Result WriteAmiiboFailed(ErrorModule::NFP, 88); constexpr Result ApplicationAreaIsNotInitialized(ErrorModule::NFP, 128); +constexpr Result WrongApplicationAreaId(ErrorModule::NFP, 152); constexpr Result ApplicationAreaExist(ErrorModule::NFP, 168); } // namespace ErrCodes -constexpr u32 ApplicationAreaSize = 0xD8; - IUser::IUser(Module::Interface& nfp_interface_, Core::System& system_) : ServiceFramework{system_, "NFP::IUser"}, service_context{system_, service_name}, nfp_interface{nfp_interface_} { @@ -39,7 +43,7 @@ IUser::IUser(Module::Interface& nfp_interface_, Core::System& system_) {7, &IUser::OpenApplicationArea, "OpenApplicationArea"}, {8, &IUser::GetApplicationArea, "GetApplicationArea"}, {9, &IUser::SetApplicationArea, "SetApplicationArea"}, - {10, nullptr, "Flush"}, + {10, &IUser::Flush, "Flush"}, {11, nullptr, "Restore"}, {12, &IUser::CreateApplicationArea, "CreateApplicationArea"}, {13, &IUser::GetTagInfo, "GetTagInfo"}, @@ -87,11 +91,23 @@ void IUser::Finalize(Kernel::HLERequestContext& ctx) { void IUser::ListDevices(Kernel::HLERequestContext& ctx) { LOG_INFO(Service_NFP, "called"); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + std::vector<u64> devices; // TODO(german77): Loop through all interfaces devices.push_back(nfp_interface.GetHandle()); + if (devices.size() == 0) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::DeviceNotFound); + return; + } + ctx.WriteBuffer(devices); IPC::ResponseBuilder rb{ctx, 3}; @@ -105,6 +121,12 @@ void IUser::StartDetection(Kernel::HLERequestContext& ctx) { const auto nfp_protocol{rp.Pop<s32>()}; LOG_INFO(Service_NFP, "called, device_handle={}, nfp_protocol={}", device_handle, nfp_protocol); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { const auto result = nfp_interface.StartDetection(nfp_protocol); @@ -124,6 +146,12 @@ void IUser::StopDetection(Kernel::HLERequestContext& ctx) { const auto device_handle{rp.Pop<u64>()}; LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { const auto result = nfp_interface.StopDetection(); @@ -146,6 +174,12 @@ void IUser::Mount(Kernel::HLERequestContext& ctx) { LOG_INFO(Service_NFP, "called, device_handle={}, model_type={}, mount_target={}", device_handle, model_type, mount_target); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { const auto result = nfp_interface.Mount(); @@ -165,6 +199,12 @@ void IUser::Unmount(Kernel::HLERequestContext& ctx) { const auto device_handle{rp.Pop<u64>()}; LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { const auto result = nfp_interface.Unmount(); @@ -186,6 +226,12 @@ void IUser::OpenApplicationArea(Kernel::HLERequestContext& ctx) { LOG_WARNING(Service_NFP, "(STUBBED) called, device_handle={}, access_id={}", device_handle, access_id); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { const auto result = nfp_interface.OpenApplicationArea(access_id); @@ -205,9 +251,15 @@ void IUser::GetApplicationArea(Kernel::HLERequestContext& ctx) { const auto device_handle{rp.Pop<u64>()}; LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { - std::vector<u8> data{}; + ApplicationArea data{}; const auto result = nfp_interface.GetApplicationArea(data); ctx.WriteBuffer(data); IPC::ResponseBuilder rb{ctx, 3}; @@ -229,6 +281,12 @@ void IUser::SetApplicationArea(Kernel::HLERequestContext& ctx) { LOG_WARNING(Service_NFP, "(STUBBED) called, device_handle={}, data_size={}", device_handle, data.size()); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { const auto result = nfp_interface.SetApplicationArea(data); @@ -243,6 +301,31 @@ void IUser::SetApplicationArea(Kernel::HLERequestContext& ctx) { rb.Push(ErrCodes::DeviceNotFound); } +void IUser::Flush(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + const auto device_handle{rp.Pop<u64>()}; + LOG_WARNING(Service_NFP, "(STUBBED) called, device_handle={}", device_handle); + + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + + // TODO(german77): Loop through all interfaces + if (device_handle == nfp_interface.GetHandle()) { + const auto result = nfp_interface.Flush(); + 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<u64>()}; @@ -251,6 +334,12 @@ void IUser::CreateApplicationArea(Kernel::HLERequestContext& ctx) { LOG_WARNING(Service_NFP, "(STUBBED) called, device_handle={}, data_size={}, access_id={}", device_handle, access_id, data.size()); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { const auto result = nfp_interface.CreateApplicationArea(access_id, data); @@ -270,6 +359,12 @@ void IUser::GetTagInfo(Kernel::HLERequestContext& ctx) { const auto device_handle{rp.Pop<u64>()}; LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { TagInfo tag_info{}; @@ -291,6 +386,12 @@ void IUser::GetRegisterInfo(Kernel::HLERequestContext& ctx) { const auto device_handle{rp.Pop<u64>()}; LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { RegisterInfo register_info{}; @@ -312,6 +413,12 @@ void IUser::GetCommonInfo(Kernel::HLERequestContext& ctx) { const auto device_handle{rp.Pop<u64>()}; LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { CommonInfo common_info{}; @@ -333,6 +440,12 @@ void IUser::GetModelInfo(Kernel::HLERequestContext& ctx) { const auto device_handle{rp.Pop<u64>()}; LOG_INFO(Service_NFP, "called, device_handle={}", device_handle); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { ModelInfo model_info{}; @@ -354,6 +467,12 @@ void IUser::AttachActivateEvent(Kernel::HLERequestContext& ctx) { const auto device_handle{rp.Pop<u64>()}; LOG_DEBUG(Service_NFP, "called, device_handle={}", device_handle); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { IPC::ResponseBuilder rb{ctx, 2, 1}; @@ -373,6 +492,12 @@ void IUser::AttachDeactivateEvent(Kernel::HLERequestContext& ctx) { const auto device_handle{rp.Pop<u64>()}; LOG_DEBUG(Service_NFP, "called, device_handle={}", device_handle); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { IPC::ResponseBuilder rb{ctx, 2, 1}; @@ -419,6 +544,12 @@ void IUser::GetNpadId(Kernel::HLERequestContext& ctx) { const auto device_handle{rp.Pop<u64>()}; LOG_DEBUG(Service_NFP, "called, device_handle={}", device_handle); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + // TODO(german77): Loop through all interfaces if (device_handle == nfp_interface.GetHandle()) { IPC::ResponseBuilder rb{ctx, 3}; @@ -442,7 +573,7 @@ void IUser::GetApplicationAreaSize(Kernel::HLERequestContext& ctx) { if (device_handle == nfp_interface.GetHandle()) { IPC::ResponseBuilder rb{ctx, 3}; rb.Push(ResultSuccess); - rb.Push(ApplicationAreaSize); + rb.Push(sizeof(ApplicationArea)); return; } @@ -455,6 +586,12 @@ void IUser::GetApplicationAreaSize(Kernel::HLERequestContext& ctx) { void IUser::AttachAvailabilityChangeEvent(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_NFP, "(STUBBED) called"); + if (state == State::NonInitialized) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ErrCodes::NfcDisabled); + return; + } + IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(ResultSuccess); rb.PushCopyObjects(availability_change_event->GetReadableEvent()); @@ -478,36 +615,42 @@ void Module::Interface::CreateUserInterface(Kernel::HLERequestContext& ctx) { rb.PushIpcInterface<IUser>(*this, system); } -bool Module::Interface::LoadAmiibo(const std::vector<u8>& buffer) { - 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); +bool Module::Interface::LoadAmiiboFile(const std::string& filename) { constexpr auto tag_size_without_password = sizeof(NTAG215File) - sizeof(NTAG215Password); + const Common::FS::IOFile amiibo_file{filename, Common::FS::FileAccessMode::Read, + Common::FS::FileType::BinaryFile}; - std::vector<u8> amiibo_buffer = buffer; + if (!amiibo_file.IsOpen()) { + LOG_ERROR(Core, "Amiibo is already on use"); + return false; + } - if (amiibo_buffer.size() < tag_size_without_password) { - LOG_ERROR(Service_NFP, "Wrong file size {}", buffer.size()); + // Workaround for files with missing password data + std::array<u8, sizeof(EncryptedNTAG215File)> buffer{}; + if (amiibo_file.Read(buffer) < tag_size_without_password) { + LOG_ERROR(Core, "Failed to read amiibo file"); return false; } + memcpy(&encrypted_tag_data, buffer.data(), sizeof(EncryptedNTAG215File)); - // Ensure it has the correct size - if (amiibo_buffer.size() != tag_size) { - amiibo_buffer.resize(tag_size, 0); + if (!AmiiboCrypto::IsAmiiboValid(encrypted_tag_data)) { + LOG_INFO(Service_NFP, "Invalid amiibo"); + return false; } - LOG_INFO(Service_NFP, "Amiibo detected"); - std::memcpy(&tag_data, buffer.data(), tag_size); + file_path = filename; + return true; +} - if (!IsAmiiboValid()) { +bool Module::Interface::LoadAmiibo(const std::string& filename) { + if (device_state != DeviceState::SearchingForTag) { + LOG_ERROR(Service_NFP, "Game is not looking for amiibos, current state {}", device_state); return false; } - // This value can't be dumped from a tag. Generate it - tag_data.password.PWD = GetTagPassword(tag_data.uuid); + if (!LoadAmiiboFile(filename)) { + return false; + } device_state = DeviceState::TagFound; activate_event->GetWritableEvent().Signal(); @@ -517,55 +660,13 @@ bool Module::Interface::LoadAmiibo(const std::vector<u8>& buffer) { void Module::Interface::CloseAmiibo() { LOG_INFO(Service_NFP, "Remove amiibo"); device_state = DeviceState::TagRemoved; + is_data_decoded = false; is_application_area_initialized = false; - application_area_id = 0; - application_area_data.clear(); + encrypted_tag_data = {}; + tag_data = {}; deactivate_event->GetWritableEvent().Signal(); } -bool Module::Interface::IsAmiiboValid() const { - 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_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}", 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 (tag_data.lock_bytes != 0xE00F) { - return false; - } - if (tag_data.compability_container != 0xEEFF10F1U) { - return false; - } - if ((amiibo_data.crypto_init & 0xFF) != 0xA5) { - return false; - } - if (amiibo_data.model_info.fixed != 0x02) { - return false; - } - if ((tag_data.dynamic_lock & 0xFFFFFF) != 0x0F0001) { - return false; - } - if (tag_data.CFG0 != 0x04000000U) { - return false; - } - if (tag_data.CFG1 != 0x5F) { - return false; - } - return true; -} - Kernel::KReadableEvent& Module::Interface::GetActivateEvent() const { return activate_event->GetReadableEvent(); } @@ -576,13 +677,20 @@ Kernel::KReadableEvent& Module::Interface::GetDeactivateEvent() const { void Module::Interface::Initialize() { device_state = DeviceState::Initialized; + is_data_decoded = false; + is_application_area_initialized = false; + encrypted_tag_data = {}; + tag_data = {}; } void Module::Interface::Finalize() { + if (device_state == DeviceState::TagMounted) { + Unmount(); + } + if (device_state == DeviceState::SearchingForTag || device_state == DeviceState::TagRemoved) { + StopDetection(); + } device_state = DeviceState::Unaviable; - is_application_area_initialized = false; - application_area_id = 0; - application_area_data.clear(); } Result Module::Interface::StartDetection(s32 protocol_) { @@ -618,42 +726,102 @@ Result Module::Interface::StopDetection() { return ErrCodes::WrongDeviceState; } -Result Module::Interface::Mount() { - if (device_state == DeviceState::TagFound) { - device_state = DeviceState::TagMounted; +Result Module::Interface::Flush() { + // Ignore write command if we can't encrypt the data + if (!is_data_decoded) { return ResultSuccess; } - LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); - return ErrCodes::WrongDeviceState; + constexpr auto tag_size_without_password = sizeof(NTAG215File) - sizeof(NTAG215Password); + EncryptedNTAG215File tmp_encrypted_tag_data{}; + const Common::FS::IOFile amiibo_file{file_path, Common::FS::FileAccessMode::ReadWrite, + Common::FS::FileType::BinaryFile}; + + if (!amiibo_file.IsOpen()) { + LOG_ERROR(Core, "Amiibo is already on use"); + return ErrCodes::WriteAmiiboFailed; + } + + // Workaround for files with missing password data + std::array<u8, sizeof(EncryptedNTAG215File)> buffer{}; + if (amiibo_file.Read(buffer) < tag_size_without_password) { + LOG_ERROR(Core, "Failed to read amiibo file"); + return ErrCodes::WriteAmiiboFailed; + } + memcpy(&tmp_encrypted_tag_data, buffer.data(), sizeof(EncryptedNTAG215File)); + + if (!AmiiboCrypto::IsAmiiboValid(tmp_encrypted_tag_data)) { + LOG_INFO(Service_NFP, "Invalid amiibo"); + return ErrCodes::WriteAmiiboFailed; + } + + bool is_uuid_equal = memcmp(tmp_encrypted_tag_data.uuid.data(), tag_data.uuid.data(), 8) == 0; + bool is_character_equal = tmp_encrypted_tag_data.user_memory.model_info.character_id == + tag_data.model_info.character_id; + if (!is_uuid_equal || !is_character_equal) { + LOG_ERROR(Core, "Not the same amiibo"); + return ErrCodes::WriteAmiiboFailed; + } + + if (!AmiiboCrypto::EncodeAmiibo(tag_data, encrypted_tag_data)) { + LOG_ERROR(Core, "Failed to encode data"); + return ErrCodes::WriteAmiiboFailed; + } + + // Return to the start of the file + if (!amiibo_file.Seek(0)) { + LOG_ERROR(Service_NFP, "Error writting to file"); + return ErrCodes::WriteAmiiboFailed; + } + + if (!amiibo_file.Write(encrypted_tag_data)) { + LOG_ERROR(Service_NFP, "Error writting to file"); + return ErrCodes::WriteAmiiboFailed; + } + + return ResultSuccess; +} + +Result Module::Interface::Mount() { + if (device_state != DeviceState::TagFound) { + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return ErrCodes::WrongDeviceState; + } + + is_data_decoded = AmiiboCrypto::DecodeAmiibo(encrypted_tag_data, tag_data); + LOG_INFO(Service_NFP, "Is amiibo decoded {}", is_data_decoded); + + is_application_area_initialized = false; + device_state = DeviceState::TagMounted; + return ResultSuccess; } Result 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; + if (device_state != DeviceState::TagMounted) { + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return ErrCodes::WrongDeviceState; } - LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); - return ErrCodes::WrongDeviceState; + is_data_decoded = false; + is_application_area_initialized = false; + device_state = DeviceState::TagFound; + return ResultSuccess; } Result Module::Interface::GetTagInfo(TagInfo& tag_info) const { - if (device_state == DeviceState::TagFound || device_state == DeviceState::TagMounted) { - tag_info = { - .uuid = tag_data.uuid, - .uuid_length = static_cast<u8>(tag_data.uuid.size()), - .protocol = protocol, - .tag_type = static_cast<u32>(tag_data.user_memory.model_info.amiibo_type), - }; - return ResultSuccess; + if (device_state != DeviceState::TagFound && device_state != DeviceState::TagMounted) { + LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); + return ErrCodes::WrongDeviceState; } - LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); - return ErrCodes::WrongDeviceState; + tag_info = { + .uuid = encrypted_tag_data.uuid, + .uuid_length = static_cast<u8>(encrypted_tag_data.uuid.size()), + .protocol = protocol, + .tag_type = static_cast<u32>(encrypted_tag_data.user_memory.model_info.amiibo_type), + }; + + return ResultSuccess; } Result Module::Interface::GetCommonInfo(CommonInfo& common_info) const { @@ -662,14 +830,28 @@ Result Module::Interface::GetCommonInfo(CommonInfo& common_info) const { return ErrCodes::WrongDeviceState; } - // Read this data from the amiibo save file + if (is_data_decoded) { + const auto& settings = tag_data.settings; + // TODO: Validate this data + common_info = { + .last_write_year = static_cast<u16>(settings.write_date.year.Value()), + .last_write_month = static_cast<u8>(settings.write_date.month.Value()), + .last_write_day = static_cast<u8>(settings.write_date.day.Value()), + .write_counter = settings.crc_counter, + .version = 1, + .application_area_size = sizeof(ApplicationArea), + }; + return ResultSuccess; + } + + // Generate a generic answer common_info = { .last_write_year = 2022, .last_write_month = 2, .last_write_day = 7, - .write_counter = tag_data.user_memory.write_count, + .write_counter = 0, .version = 1, - .application_area_size = ApplicationAreaSize, + .application_area_size = sizeof(ApplicationArea), }; return ResultSuccess; } @@ -680,7 +862,15 @@ Result Module::Interface::GetModelInfo(ModelInfo& model_info) const { return ErrCodes::WrongDeviceState; } - model_info = tag_data.user_memory.model_info; + const auto& model_info_data = encrypted_tag_data.user_memory.model_info; + model_info = { + .character_id = model_info_data.character_id, + .character_variant = model_info_data.character_variant, + .amiibo_type = model_info_data.amiibo_type, + .model_number = model_info_data.model_number, + .series = model_info_data.series, + .constant_value = model_info_data.constant_value, + }; return ResultSuccess; } @@ -690,9 +880,30 @@ Result Module::Interface::GetRegisterInfo(RegisterInfo& register_info) const { return ErrCodes::WrongDeviceState; } - Service::Mii::MiiManager manager; + if (is_data_decoded) { + const auto& settings = tag_data.settings; + + // Amiibo name is u16 while the register info is u8. Figure out how to handle this properly + std::array<u8, 11> amiibo_name{}; + for (std::size_t i = 0; i < sizeof(amiibo_name) - 1; ++i) { + amiibo_name[i] = static_cast<u8>(settings.amiibo_name[i]); + } + + // TODO: Validate this data + register_info = { + .mii_char_info = AmiiboCrypto::AmiiboRegisterInfoToMii(tag_data.owner_mii), + .first_write_year = static_cast<u16>(settings.init_date.year.Value()), + .first_write_month = static_cast<u8>(settings.init_date.month.Value()), + .first_write_day = static_cast<u8>(settings.init_date.day.Value()), + .amiibo_name = amiibo_name, + .unknown = {}, + }; + + return ResultSuccess; + } - // Read this data from the amiibo save file + // Generate a generic answer + Service::Mii::MiiManager manager; register_info = { .mii_char_info = manager.BuildDefault(0), .first_write_year = 2022, @@ -709,29 +920,39 @@ Result 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; + + // Fallback for lack of amiibo keys + if (!is_data_decoded) { + LOG_WARNING(Service_NFP, "Application area is not initialized"); + return ErrCodes::ApplicationAreaIsNotInitialized; } - if (!is_application_area_initialized) { + + if (tag_data.settings.settings.appdata_initialized == 0) { LOG_WARNING(Service_NFP, "Application area is not initialized"); return ErrCodes::ApplicationAreaIsNotInitialized; } + + if (tag_data.application_area_id != access_id) { + LOG_WARNING(Service_NFP, "Wrong application area id"); + return ErrCodes::WrongApplicationAreaId; + } + + is_application_area_initialized = true; return ResultSuccess; } -Result Module::Interface::GetApplicationArea(std::vector<u8>& data) const { +Result Module::Interface::GetApplicationArea(ApplicationArea& 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; + data = tag_data.application_area; return ResultSuccess; } @@ -741,12 +962,18 @@ Result Module::Interface::SetApplicationArea(const std::vector<u8>& data) { 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; - SaveAmiiboApplicationData(application_area_id, application_area_data); + + if (data.size() != sizeof(ApplicationArea)) { + LOG_ERROR(Service_NFP, "Wrong data size {}", data.size()); + return ResultUnknown; + } + + std::memcpy(&tag_data.application_area, data.data(), sizeof(ApplicationArea)); return ResultSuccess; } @@ -755,30 +982,21 @@ Result Module::Interface::CreateApplicationArea(u32 access_id, const std::vector LOG_ERROR(Service_NFP, "Wrong device state {}", device_state); return ErrCodes::WrongDeviceState; } - if (AmiiboApplicationDataExist(access_id)) { + + if (tag_data.settings.settings.appdata_initialized != 0) { LOG_ERROR(Service_NFP, "Application area already exist"); return ErrCodes::ApplicationAreaExist; } - application_area_data = data; - application_area_id = access_id; - 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; -} + if (data.size() != sizeof(ApplicationArea)) { + LOG_ERROR(Service_NFP, "Wrong data size {}", data.size()); + return ResultUnknown; + } -std::vector<u8> Module::Interface::LoadAmiiboApplicationData(u32 access_id) const { - // TODO(german77): Read file - std::vector<u8> data(ApplicationAreaSize); - return data; -} + std::memcpy(&tag_data.application_area, data.data(), sizeof(ApplicationArea)); + tag_data.application_area_id = access_id; -void Module::Interface::SaveAmiiboApplicationData(u32 access_id, - const std::vector<u8>& data) const { - // TODO(german77): Save file + return ResultSuccess; } u64 Module::Interface::GetHandle() const { @@ -794,15 +1012,6 @@ 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<Module>(); std::make_shared<NFP_User>(module, system)->InstallAsService(service_manager); |