From 01b113f50820b64e550be3e09d1e29918c6185da Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Fri, 14 Mar 2025 23:00:59 +0000 Subject: [PATCH] Fix chunk sizes --- lib/nfc/protocols/type_4_tag/type_4_tag_i.h | 5 ++++- lib/nfc/protocols/type_4_tag/type_4_tag_listener_i.c | 12 +++++++----- lib/nfc/protocols/type_4_tag/type_4_tag_poller_i.c | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/nfc/protocols/type_4_tag/type_4_tag_i.h b/lib/nfc/protocols/type_4_tag/type_4_tag_i.h index d64efcc2c..b0e319ba9 100644 --- a/lib/nfc/protocols/type_4_tag/type_4_tag_i.h +++ b/lib/nfc/protocols/type_4_tag/type_4_tag_i.h @@ -33,8 +33,11 @@ #define TYPE_4_TAG_T4T_CC_FILE_ID 0xE1, 0x03 #define TYPE_4_TAG_T4T_CC_VNO (0x20) +// 4a layer adds 1..3 byte prefix, 3a layer adds 2 byte suffix and has 256 byte buffer +#define TYPE_4_TAG_BUF_SIZE (256U - 3U - 2U) // Read returns 2 byte status trailer, write sends 5 byte command header -#define TYPE_4_TAG_BUF_SIZE (TYPE_4_TAG_ISO_RW_CHUNK_LEN + 5) +#define TYPE_4_TAG_CHUNK_LEN MIN(TYPE_4_TAG_BUF_SIZE - 5U, TYPE_4_TAG_ISO_RW_CHUNK_LEN) +#define TYPE_4_TAG_DEFAULT_SIZE (2048U) // Capability Container parsing structures diff --git a/lib/nfc/protocols/type_4_tag/type_4_tag_listener_i.c b/lib/nfc/protocols/type_4_tag/type_4_tag_listener_i.c index a5557f95f..94e64788a 100644 --- a/lib/nfc/protocols/type_4_tag/type_4_tag_listener_i.c +++ b/lib/nfc/protocols/type_4_tag/type_4_tag_listener_i.c @@ -112,13 +112,15 @@ static Type4TagError type_4_tag_listener_iso_read( bit_lib_num_to_bytes_be(sizeof(cc_buf), sizeof(cc->len), (void*)&cc->len); cc->t4t_vno = TYPE_4_TAG_T4T_CC_VNO; bit_lib_num_to_bytes_be( - instance->data->is_tag_specific ? instance->data->chunk_max_read : - TYPE_4_TAG_ISO_RW_CHUNK_LEN, + instance->data->is_tag_specific ? + MIN(instance->data->chunk_max_read, TYPE_4_TAG_CHUNK_LEN) : + TYPE_4_TAG_CHUNK_LEN, sizeof(cc->mle), (void*)&cc->mle); bit_lib_num_to_bytes_be( - instance->data->is_tag_specific ? instance->data->chunk_max_write : - TYPE_4_TAG_ISO_RW_CHUNK_LEN, + instance->data->is_tag_specific ? + MIN(instance->data->chunk_max_write, TYPE_4_TAG_CHUNK_LEN) : + TYPE_4_TAG_CHUNK_LEN, sizeof(cc->mlc), (void*)&cc->mlc); cc->tlv[0].type = Type4TagCcTlvTypeNdefFileCtrl; @@ -129,7 +131,7 @@ static Type4TagError type_4_tag_listener_iso_read( (void*)&cc->tlv[0].value.ndef_file_ctrl.file_id); bit_lib_num_to_bytes_be( instance->data->is_tag_specific ? instance->data->ndef_max_len : - TYPE_4_TAG_ISO_RW_CHUNK_LEN, + TYPE_4_TAG_DEFAULT_SIZE, sizeof(cc->tlv[0].value.ndef_file_ctrl.max_len), (void*)&cc->tlv[0].value.ndef_file_ctrl.max_len); cc->tlv[0].value.ndef_file_ctrl.read_perm = diff --git a/lib/nfc/protocols/type_4_tag/type_4_tag_poller_i.c b/lib/nfc/protocols/type_4_tag/type_4_tag_poller_i.c index 45a9da1fc..92caae7d7 100644 --- a/lib/nfc/protocols/type_4_tag/type_4_tag_poller_i.c +++ b/lib/nfc/protocols/type_4_tag/type_4_tag_poller_i.c @@ -198,7 +198,7 @@ Type4TagError type_4_tag_poller_read_ndef(Type4TagPoller* instance) { FURI_LOG_D(TAG, "NDEF file is empty"); break; } - uint8_t chunk_max = MIN(instance->data->chunk_max_read, TYPE_4_TAG_ISO_RW_CHUNK_LEN); + uint8_t chunk_max = MIN(instance->data->chunk_max_read, TYPE_4_TAG_CHUNK_LEN); if(ndef_len > TYPE_4_TAG_ISO_READ_P_OFFSET_MAX + chunk_max - sizeof(ndef_len)) { FURI_LOG_E(TAG, "NDEF file too long: %zu bytes", ndef_len); error = Type4TagErrorNotSupported;