From 384490e23196397c5603516693a319dbbd5ed59c Mon Sep 17 00:00:00 2001 From: turbocool3r Date: Mon, 15 Jul 2024 03:17:25 +0300 Subject: [PATCH] Fix an old bug in `hf mfu econfig` that prevented anticollision resolution data from being updated sometimes. --- firmware/application/src/app_cmd.c | 42 ++++++++++++++++--- .../src/rfid/nfctag/hf/nfc_mf0_ntag.c | 4 +- .../src/rfid/nfctag/hf/nfc_mf0_ntag.h | 1 + 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/firmware/application/src/app_cmd.c b/firmware/application/src/app_cmd.c index 85462ca..18713da 100644 --- a/firmware/application/src/app_cmd.c +++ b/firmware/application/src/app_cmd.c @@ -688,13 +688,44 @@ static data_frame_tx_t *cmd_processor_em410x_get_emu_id(uint16_t cmd, uint16_t s return data_frame_make(cmd, STATUS_SUCCESS, LF_EM410X_TAG_ID_SIZE, responseData); } -static data_frame_tx_t *cmd_processor_hf14a_get_anti_coll_data(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { +static nfc_tag_14a_coll_res_reference_t *get_coll_res_data(bool write) { + nfc_tag_14a_coll_res_reference_t *info; tag_slot_specific_type_t tag_types; + tag_emulation_get_specific_types_by_slot(tag_emulation_get_slot(), &tag_types); - if (tag_types.tag_hf == TAG_TYPE_UNDEFINED) { - return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); // no data in slot, don't send garbage + + switch (tag_types.tag_hf) { + case TAG_TYPE_MIFARE_1024: + case TAG_TYPE_MIFARE_2048: + case TAG_TYPE_MIFARE_4096: + case TAG_TYPE_MIFARE_Mini: + info = write ? get_mifare_coll_res() : get_saved_mifare_coll_res(); + break; + case TAG_TYPE_MF0ICU1: + case TAG_TYPE_MF0ICU2: + case TAG_TYPE_MF0UL11: + case TAG_TYPE_MF0UL21: + case TAG_TYPE_NTAG_210: + case TAG_TYPE_NTAG_212: + case TAG_TYPE_NTAG_213: + case TAG_TYPE_NTAG_215: + case TAG_TYPE_NTAG_216: + info = nfc_tag_mf0_ntag_get_coll_res(); + break; + default: + // no collision resolution data for slot + info = NULL; + break; } - nfc_tag_14a_coll_res_reference_t *info = get_saved_mifare_coll_res(); + + return info; +} + +static data_frame_tx_t *cmd_processor_hf14a_get_anti_coll_data(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { + nfc_tag_14a_coll_res_reference_t *info = get_coll_res_data(false); + + if (info == NULL) return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); + // uidlen[1]|uid[uidlen]|atqa[2]|sak[1]|atslen[1]|ats[atslen] // dynamic length, so no struct uint8_t payload[1 + *info->size + 2 + 1 + 1 + 254]; @@ -947,7 +978,8 @@ static data_frame_tx_t *cmd_processor_hf14a_set_anti_coll_data(uint16_t cmd, uin (length < 1 + data[0] + 2 + 1 + 1 + data[1 + data[0] + 2 + 1])) { return data_frame_make(cmd, STATUS_PAR_ERR, 0, NULL); } - nfc_tag_14a_coll_res_reference_t *info = get_mifare_coll_res(); + nfc_tag_14a_coll_res_reference_t *info = get_coll_res_data(true); + uint16_t offset = 0; *(info->size) = (nfc_tag_14a_uid_size)data[offset]; offset++; diff --git a/firmware/application/src/rfid/nfctag/hf/nfc_mf0_ntag.c b/firmware/application/src/rfid/nfctag/hf/nfc_mf0_ntag.c index 6d55f39..125d9e1 100644 --- a/firmware/application/src/rfid/nfctag/hf/nfc_mf0_ntag.c +++ b/firmware/application/src/rfid/nfctag/hf/nfc_mf0_ntag.c @@ -1026,7 +1026,7 @@ static void nfc_tag_mf0_ntag_state_handler(uint8_t *p_data, uint16_t szDataBits) return; } -static nfc_tag_14a_coll_res_reference_t *get_coll_res() { +nfc_tag_14a_coll_res_reference_t *nfc_tag_mf0_ntag_get_coll_res() { // Use a separate anti -conflict information instead of using the information in the sector m_shadow_coll_res.sak = m_tag_information->res_coll.sak; m_shadow_coll_res.atqa = m_tag_information->res_coll.atqa; @@ -1070,7 +1070,7 @@ int nfc_tag_mf0_ntag_data_loadcb(tag_specific_type_t type, tag_data_buffer_t *bu m_tag_type = type; // Register 14A communication management interface nfc_tag_14a_handler_t handler_for_14a = { - .get_coll_res = get_coll_res, + .get_coll_res = nfc_tag_mf0_ntag_get_coll_res, .cb_state = nfc_tag_mf0_ntag_state_handler, .cb_reset = nfc_tag_mf0_ntag_reset_handler, }; diff --git a/firmware/application/src/rfid/nfctag/hf/nfc_mf0_ntag.h b/firmware/application/src/rfid/nfctag/hf/nfc_mf0_ntag.h index 92be868..b65a01e 100644 --- a/firmware/application/src/rfid/nfctag/hf/nfc_mf0_ntag.h +++ b/firmware/application/src/rfid/nfctag/hf/nfc_mf0_ntag.h @@ -75,6 +75,7 @@ int nfc_tag_mf0_ntag_get_nr_pages_by_tag_type(tag_specific_type_t tag_type); uint8_t *nfc_tag_mf0_ntag_get_counter_data_by_index(uint8_t index); uint8_t *nfc_tag_mf0_ntag_get_version_data(void); uint8_t *nfc_tag_mf0_ntag_get_signature_data(void); +nfc_tag_14a_coll_res_reference_t *nfc_tag_mf0_ntag_get_coll_res(void); int nfc_tag_mf0_ntag_get_uid_mode(void); bool nfc_tag_mf0_ntag_set_uid_mode(bool enabled);