From 446b57e89f65dd0599cd53866c138dd5f6872238 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 11 Oct 2023 23:08:25 +0200 Subject: [PATCH] Changed fds_write_sync to take length in bytes instead of next multiple of 4 --- CHANGELOG.md | 1 + firmware/application/src/app_cmd.c | 2 +- firmware/application/src/app_main.c | 7 +++---- firmware/application/src/rfid/nfctag/hf/nfc_mf1.c | 14 ++++++-------- firmware/application/src/rfid/nfctag/hf/nfc_ntag.c | 4 ++-- .../application/src/rfid/nfctag/lf/lf_tag_em.c | 4 ++-- .../application/src/rfid/nfctag/tag_emulation.c | 6 ++---- firmware/application/src/settings.c | 2 +- firmware/application/src/utils/fds_util.c | 7 ++++++- firmware/application/src/utils/fds_util.h | 2 +- 10 files changed, 25 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bc29768..9daf03f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] + - Changed fds_write_sync to take length in bytes instead of next multiple of 4 (@doegox) - Fixed field LED when LF reading and HF cloning (@doegox) - Added renaming of slot into "cloned" when having cloned an ID/UID with a button (@doegox) - Fixed spurious LED in battery level, assigned battery level to long press B (@doegox) diff --git a/firmware/application/src/app_cmd.c b/firmware/application/src/app_cmd.c index 23bd09db..7998ec97 100644 --- a/firmware/application/src/app_cmd.c +++ b/firmware/application/src/app_cmd.c @@ -764,7 +764,7 @@ static data_frame_tx_t *cmd_processor_set_slot_tag_nick(uint16_t cmd, uint16_t s buffer[0] = length - 2; memcpy(buffer + 1, data + 2, buffer[0]); - bool ret = fds_write_sync(map_info.id, map_info.key, sizeof(buffer) / 4, buffer); + bool ret = fds_write_sync(map_info.id, map_info.key, sizeof(buffer), buffer); if (!ret) { return data_frame_make(cmd, STATUS_FLASH_WRITE_FAIL, 0, NULL); } diff --git a/firmware/application/src/app_main.c b/firmware/application/src/app_main.c index d8f472cd..8baca7b6 100644 --- a/firmware/application/src/app_main.c +++ b/firmware/application/src/app_main.c @@ -702,17 +702,16 @@ static void btn_fn_copy_ic_uid(void) { if (lf_copy_succeeded || hf_copy_succeeded) { fds_slot_record_map_t map_info; char *nick = "cloned"; - uint8_t buffer[((((1+strlen(nick))-1)/4)+1)*4]; + uint8_t buffer[36]; buffer[0] = strlen(nick); memcpy(buffer + 1, nick, buffer[0]); - // ASSERT(sizeof(buffer) % 4 == 0); if (lf_copy_succeeded) { get_fds_map_by_slot_sense_type_for_nick(slot_now, TAG_SENSE_LF, &map_info); - fds_write_sync(map_info.id, map_info.key, sizeof(buffer) / 4, buffer); + fds_write_sync(map_info.id, map_info.key, sizeof(buffer), buffer); } if (hf_copy_succeeded) { get_fds_map_by_slot_sense_type_for_nick(slot_now, TAG_SENSE_HF, &map_info); - fds_write_sync(map_info.id, map_info.key, sizeof(buffer) / 4, buffer); + fds_write_sync(map_info.id, map_info.key, sizeof(buffer), buffer); } } // keep reader mode or exit reader mode. diff --git a/firmware/application/src/rfid/nfctag/hf/nfc_mf1.c b/firmware/application/src/rfid/nfctag/hf/nfc_mf1.c index 417c6753..72649563 100644 --- a/firmware/application/src/rfid/nfctag/hf/nfc_mf1.c +++ b/firmware/application/src/rfid/nfctag/hf/nfc_mf1.c @@ -1102,10 +1102,8 @@ void nfc_tag_mf1_reset_handler() { * @return Suppose type == tag_type_mifare_1024, * The length of the information should be the anti -collision information plus the configuration information plus the length of the sector */ -static int get_information_size_by_tag_type(tag_specific_type_t type, bool auth_align) { - int size_raw = sizeof(nfc_tag_14a_coll_res_entity_t) + sizeof(nfc_tag_mf1_configure_t) + (get_block_max_by_tag_type(type) * NFC_TAG_MF1_DATA_SIZE); - int size_align = size_raw + (size_raw % 4); - return auth_align ? size_align : size_raw; +static int get_information_size_by_tag_type(tag_specific_type_t type) { + return sizeof(nfc_tag_14a_coll_res_entity_t) + sizeof(nfc_tag_mf1_configure_t) + (get_block_max_by_tag_type(type) * NFC_TAG_MF1_DATA_SIZE); } /** @brief MF1's callback before saving data @@ -1124,7 +1122,7 @@ int nfc_tag_mf1_data_savecb(tag_specific_type_t type, tag_data_buffer_t *buffer) m_tag_information->config.mode_block_write = NFC_TAG_MF1_WRITE_SHADOW; } // Save the corresponding size data according to the current label type - return get_information_size_by_tag_type(type, false); + return get_information_size_by_tag_type(type); } else { return 0; } @@ -1136,7 +1134,7 @@ int nfc_tag_mf1_data_savecb(tag_specific_type_t type, tag_data_buffer_t *buffer) */ int nfc_tag_mf1_data_loadcb(tag_specific_type_t type, tag_data_buffer_t *buffer) { // Make sure that external capacity is enough to convert to an information structure - int info_size = get_information_size_by_tag_type(type, false); + int info_size = get_information_size_by_tag_type(type); if (buffer->length >= info_size) { //Convert the data buffer to MF1 structure type m_tag_information = (nfc_tag_mf1_information_t *)buffer->buffer; @@ -1200,9 +1198,9 @@ bool nfc_tag_mf1_data_factory(uint8_t slot, tag_specific_type_t tag_type) { tag_sense_type_t sense_type = get_sense_type_from_tag_type(tag_type); fds_slot_record_map_t map_info; get_fds_map_by_slot_sense_type_for_dump(slot, sense_type, &map_info); - int info_size = get_information_size_by_tag_type(tag_type, true); // auto 4 byte align. + int info_size = get_information_size_by_tag_type(tag_type); NRF_LOG_INFO("MF1 info size: %d", info_size); - bool ret = fds_write_sync(map_info.id, map_info.key, info_size / 4, p_mf1_information); + bool ret = fds_write_sync(map_info.id, map_info.key, info_size, p_mf1_information); if (ret) { NRF_LOG_INFO("Factory slot data success."); } else { diff --git a/firmware/application/src/rfid/nfctag/hf/nfc_ntag.c b/firmware/application/src/rfid/nfctag/hf/nfc_ntag.c index b5627d5d..19fabc2b 100644 --- a/firmware/application/src/rfid/nfctag/hf/nfc_ntag.c +++ b/firmware/application/src/rfid/nfctag/hf/nfc_ntag.c @@ -289,9 +289,9 @@ bool nfc_tag_ntag_data_factory(uint8_t slot, tag_specific_type_t tag_type) { tag_sense_type_t sense_type = get_sense_type_from_tag_type(tag_type); fds_slot_record_map_t map_info; get_fds_map_by_slot_sense_type_for_dump(slot, sense_type, &map_info); - int info_size = get_information_size_by_tag_type(tag_type); // auto 4 byte align. + int info_size = get_information_size_by_tag_type(tag_type); NRF_LOG_INFO("NTAG info size: %d", info_size); - bool ret = fds_write_sync(map_info.id, map_info.key, info_size / 4, p_ntag_information); + bool ret = fds_write_sync(map_info.id, map_info.key, info_size, p_ntag_information); if (ret) { NRF_LOG_INFO("Factory slot data success."); } else { diff --git a/firmware/application/src/rfid/nfctag/lf/lf_tag_em.c b/firmware/application/src/rfid/nfctag/lf/lf_tag_em.c index 9d480557..faa0735c 100644 --- a/firmware/application/src/rfid/nfctag/lf/lf_tag_em.c +++ b/firmware/application/src/rfid/nfctag/lf/lf_tag_em.c @@ -411,13 +411,13 @@ int lf_tag_em410x_data_savecb(tag_specific_type_t type, tag_data_buffer_t *buffe */ bool lf_tag_em410x_data_factory(uint8_t slot, tag_specific_type_t tag_type) { // default id, must to align(4), more word... - uint8_t tag_id[8] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x88 }; + uint8_t tag_id[5] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x88 }; // Write the data in Flash tag_sense_type_t sense_type = get_sense_type_from_tag_type(tag_type); fds_slot_record_map_t map_info; // Get the special card slot FDS record information get_fds_map_by_slot_sense_type_for_dump(slot, sense_type, &map_info); //Call the blocked FDS to write the function, and write the data of the specified field type of the card slot into the Flash - bool ret = fds_write_sync(map_info.id, map_info.key, sizeof(tag_id) / 4, (uint8_t *)tag_id); + bool ret = fds_write_sync(map_info.id, map_info.key, sizeof(tag_id), (uint8_t *)tag_id); if (ret) { NRF_LOG_INFO("Factory slot data success."); } else { diff --git a/firmware/application/src/rfid/nfctag/tag_emulation.c b/firmware/application/src/rfid/nfctag/tag_emulation.c index 77cc0bd4..65a05e45 100644 --- a/firmware/application/src/rfid/nfctag/tag_emulation.c +++ b/firmware/application/src/rfid/nfctag/tag_emulation.c @@ -264,10 +264,8 @@ static void save_data_by_tag_type(uint8_t slot, tag_specific_type_t tag_type) { // Get the special card slot FDS record information fds_slot_record_map_t map_info; get_fds_map_by_slot_sense_type_for_dump(slot, sense_type, &map_info); - // Calculate the length of the data to be saved (automatically fill in the whole word) - int data_word_length = (data_byte_length / 4) + (data_byte_length % 4 > 0 ? 1 : 0); // Call the blocked FDS to write the function, and write the data of the specified field type of the card slot into the Flash - bool ret = fds_write_sync(map_info.id, map_info.key, data_word_length, buffer->buffer); + bool ret = fds_write_sync(map_info.id, map_info.key, data_byte_length, buffer->buffer); if (ret) { NRF_LOG_INFO("Save tag slot data success."); } else { @@ -505,7 +503,7 @@ static void tag_emulation_save_config(void) { calc_14a_crc_lut((uint8_t *)&slotConfig, sizeof(slotConfig), (uint8_t *)&new_calc_crc); if (new_calc_crc != m_slot_config_crc) { // Before saving, make sure that the card slot configuration has changed NRF_LOG_INFO("Save tag slot config start."); - bool ret = fds_write_sync(FDS_EMULATION_CONFIG_FILE_ID, FDS_EMULATION_CONFIG_RECORD_KEY, sizeof(slotConfig) / 4, (uint8_t *)&slotConfig); + bool ret = fds_write_sync(FDS_EMULATION_CONFIG_FILE_ID, FDS_EMULATION_CONFIG_RECORD_KEY, sizeof(slotConfig), (uint8_t *)&slotConfig); if (ret) { NRF_LOG_INFO("Save tag slot config success."); m_slot_config_crc = new_calc_crc; diff --git a/firmware/application/src/settings.c b/firmware/application/src/settings.c index b5e2b722..4afc4a05 100644 --- a/firmware/application/src/settings.c +++ b/firmware/application/src/settings.c @@ -125,7 +125,7 @@ uint8_t settings_save_config(void) { // We are saving the configuration, we need to calculate the crc code of the current configuration to judge whether the following data is updated if (config_did_change()) { // Before saving, make sure that the configuration has changed NRF_LOG_INFO("Save config start."); - bool ret = fds_write_sync(FDS_SETTINGS_FILE_ID, FDS_SETTINGS_RECORD_KEY, sizeof(config) / 4, (uint8_t *)&config); + bool ret = fds_write_sync(FDS_SETTINGS_FILE_ID, FDS_SETTINGS_RECORD_KEY, sizeof(config), (uint8_t *)&config); if (ret) { NRF_LOG_INFO("Save config success."); update_config_crc(); diff --git a/firmware/application/src/utils/fds_util.c b/firmware/application/src/utils/fds_util.c index 23d28b84..4ce63344 100644 --- a/firmware/application/src/utils/fds_util.c +++ b/firmware/application/src/utils/fds_util.c @@ -107,7 +107,7 @@ static ret_code_t fds_write_record_nogc(uint16_t id, uint16_t key, uint16_t data /** * Write record */ -bool fds_write_sync(uint16_t id, uint16_t key, uint16_t data_length_words, void *buffer) { +bool fds_write_sync(uint16_t id, uint16_t key, uint16_t length, void *buffer) { // Make only one task running APP_ERROR_CHECK_BOOL(!fds_operation_info.waiting); // write result @@ -117,6 +117,11 @@ bool fds_write_sync(uint16_t id, uint16_t key, uint16_t data_length_words, void fds_operation_info.key = key; fds_operation_info.success = false; fds_operation_info.waiting = true; + // compute needed words + if (length == 0) { + return ret; + } + uint16_t data_length_words = ((length - 1) / 4) + 1; // CCall the write implementation function without automatic GC ret_code_t err_code = fds_write_record_nogc(id, key, data_length_words, buffer); diff --git a/firmware/application/src/utils/fds_util.h b/firmware/application/src/utils/fds_util.h index 76909487..7ca2db99 100644 --- a/firmware/application/src/utils/fds_util.h +++ b/firmware/application/src/utils/fds_util.h @@ -5,7 +5,7 @@ bool fds_read_sync(uint16_t id, uint16_t key, uint16_t *length, uint8_t *buffer); -bool fds_write_sync(uint16_t id, uint16_t key, uint16_t data_length_words, void *buffer); +bool fds_write_sync(uint16_t id, uint16_t key, uint16_t length, void *buffer); int fds_delete_sync(uint16_t id, uint16_t key); bool fds_is_exists(uint16_t id, uint16_t key); void fds_util_init(void);