From 85bc9eee778ab4f701aff7fe660abf16d9cf0b71 Mon Sep 17 00:00:00 2001 From: Georgii Surkov Date: Wed, 25 Oct 2023 18:34:18 +0300 Subject: [PATCH 1/3] Increase MF DESFire result buffer --- lib/nfc/protocols/mf_desfire/mf_desfire_poller.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/nfc/protocols/mf_desfire/mf_desfire_poller.c b/lib/nfc/protocols/mf_desfire/mf_desfire_poller.c index c74bbc89da6..11db021d57c 100644 --- a/lib/nfc/protocols/mf_desfire/mf_desfire_poller.c +++ b/lib/nfc/protocols/mf_desfire/mf_desfire_poller.c @@ -6,7 +6,8 @@ #define TAG "MfDesfirePoller" -#define MF_DESFIRE_BUF_SIZE_MAX (64U) +#define MF_DESFIRE_BUF_SIZE (64U) +#define MF_DESFIRE_RESULT_BUF_SIZE (512U) typedef NfcCommand (*MfDesfirePollerReadHandler)(MfDesfirePoller* instance); @@ -20,10 +21,10 @@ static MfDesfirePoller* mf_desfire_poller_alloc(Iso14443_4aPoller* iso14443_4a_p MfDesfirePoller* instance = malloc(sizeof(MfDesfirePoller)); instance->iso14443_4a_poller = iso14443_4a_poller; instance->data = mf_desfire_alloc(); - instance->tx_buffer = bit_buffer_alloc(MF_DESFIRE_BUF_SIZE_MAX); - instance->rx_buffer = bit_buffer_alloc(MF_DESFIRE_BUF_SIZE_MAX); - instance->input_buffer = bit_buffer_alloc(MF_DESFIRE_BUF_SIZE_MAX); - instance->result_buffer = bit_buffer_alloc(MF_DESFIRE_BUF_SIZE_MAX); + instance->tx_buffer = bit_buffer_alloc(MF_DESFIRE_BUF_SIZE); + instance->rx_buffer = bit_buffer_alloc(MF_DESFIRE_BUF_SIZE); + instance->input_buffer = bit_buffer_alloc(MF_DESFIRE_BUF_SIZE); + instance->result_buffer = bit_buffer_alloc(MF_DESFIRE_RESULT_BUF_SIZE); instance->mf_desfire_event.data = &instance->mf_desfire_event_data; From 762cf53f14aef43a9a3843601b6605603e839efc Mon Sep 17 00:00:00 2001 From: Georgii Surkov Date: Wed, 25 Oct 2023 18:50:49 +0300 Subject: [PATCH 2/3] Ignore chunks that do not fit into the result buffer and show warning --- lib/nfc/protocols/mf_desfire/mf_desfire_poller_i.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/nfc/protocols/mf_desfire/mf_desfire_poller_i.c b/lib/nfc/protocols/mf_desfire/mf_desfire_poller_i.c index e86cb4c68c2..6f2d8de9e50 100644 --- a/lib/nfc/protocols/mf_desfire/mf_desfire_poller_i.c +++ b/lib/nfc/protocols/mf_desfire/mf_desfire_poller_i.c @@ -59,7 +59,15 @@ MfDesfireError mf_desfire_send_chunks( break; } - bit_buffer_append_right(rx_buffer, instance->rx_buffer, sizeof(uint8_t)); + const size_t rx_size = bit_buffer_get_size_bytes(instance->rx_buffer); + const size_t rx_capacity_remaining = + bit_buffer_get_capacity_bytes(rx_buffer) - bit_buffer_get_size_bytes(rx_buffer); + + if(rx_size <= rx_capacity_remaining) { + bit_buffer_append_right(rx_buffer, instance->rx_buffer, sizeof(uint8_t)); + } else { + FURI_LOG_W(TAG, "RX buffer overflow: ignoring %zu bytes", rx_size); + } } } while(false); From bfc19cc1d381634dd539a9bff6258d11480419b9 Mon Sep 17 00:00:00 2001 From: Georgii Surkov Date: Wed, 25 Oct 2023 20:31:38 +0300 Subject: [PATCH 3/3] Display information about partial files --- .../mf_desfire/mf_desfire_render.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/applications/main/nfc/helpers/protocol_support/mf_desfire/mf_desfire_render.c b/applications/main/nfc/helpers/protocol_support/mf_desfire/mf_desfire_render.c index 883ea720b05..1da508f29e6 100644 --- a/applications/main/nfc/helpers/protocol_support/mf_desfire/mf_desfire_render.c +++ b/applications/main/nfc/helpers/protocol_support/mf_desfire/mf_desfire_render.c @@ -190,6 +190,8 @@ void nfc_render_mf_desfire_file_settings_data( uint32_t record_count = 1; uint32_t record_size = 0; + const uint32_t total_size = simple_array_get_count(data->data); + switch(settings->type) { case MfDesfireFileTypeStandard: case MfDesfireFileTypeBackup: @@ -219,9 +221,20 @@ void nfc_render_mf_desfire_file_settings_data( } for(uint32_t rec = 0; rec < record_count; rec++) { - furi_string_cat_printf(str, "record %lu\n", rec); + const uint32_t size_offset = rec * record_size; + const uint32_t size_remaining = total_size > size_offset ? total_size - size_offset : 0; + + if(size_remaining < record_size) { + furi_string_cat_printf( + str, "record %lu (partial %lu of %lu)\n", rec, size_remaining, record_size); + record_size = size_remaining; + } else { + furi_string_cat_printf(str, "record %lu\n", rec); + } + for(uint32_t ch = 0; ch < record_size; ch += 4) { furi_string_cat_printf(str, "%03lx|", ch); + for(uint32_t i = 0; i < 4; i++) { if(ch + i < record_size) { const uint32_t data_index = rec * record_size + ch + i; @@ -232,6 +245,7 @@ void nfc_render_mf_desfire_file_settings_data( furi_string_cat_printf(str, " "); } } + for(uint32_t i = 0; i < 4 && ch + i < record_size; i++) { const uint32_t data_index = rec * record_size + ch + i; const uint8_t data_byte = @@ -242,8 +256,10 @@ void nfc_render_mf_desfire_file_settings_data( furi_string_cat_printf(str, "."); } } + furi_string_push_back(str, '\n'); } + furi_string_push_back(str, '\n'); } }