From 97f26e99d13cde7a0aca603c4b6be8d013a6ba50 Mon Sep 17 00:00:00 2001 From: Konstantin Kondrashov Date: Thu, 27 Jun 2024 13:03:58 +0300 Subject: [PATCH] fix(efuse): Fix warnings when COMPILER_STATIC_ANALYZER=y - fixed a false-positive warning in esp_efuse_utility - refactored an efuse function --- components/efuse/CMakeLists.txt | 7 -- components/efuse/src/esp_efuse_utility.c | 77 ++++++-------------- components/efuse/test_apps/main/test_efuse.c | 3 + 3 files changed, 26 insertions(+), 61 deletions(-) diff --git a/components/efuse/CMakeLists.txt b/components/efuse/CMakeLists.txt index b42eb753de..e0f57b1d34 100644 --- a/components/efuse/CMakeLists.txt +++ b/components/efuse/CMakeLists.txt @@ -94,10 +94,3 @@ set(EFUSE_TEST_TABLE_CSV_PATH "${COMPONENT_DIR}/test/esp_efuse_test_table.csv") add_custom_target(efuse_test_table COMMAND "${python}" "${CMAKE_CURRENT_SOURCE_DIR}/efuse_table_gen.py" ${EFUSE_TEST_TABLE_CSV_PATH} ${GEN_EFUSE_TABLE_ARG}) - -################### -# GNU analyzer excludes -if(CONFIG_COMPILER_STATIC_ANALYZER AND CMAKE_C_COMPILER_ID STREQUAL "GNU") # TODO IDF-10086 - set_source_files_properties(src/esp_efuse_utility.c - PROPERTIES COMPILE_FLAGS "-fno-analyzer") -endif() diff --git a/components/efuse/src/esp_efuse_utility.c b/components/efuse/src/esp_efuse_utility.c index da9da5330c..7afbda05ee 100644 --- a/components/efuse/src/esp_efuse_utility.c +++ b/components/efuse/src/esp_efuse_utility.c @@ -28,10 +28,7 @@ uint32_t virt_blocks[EFUSE_BLK_MAX][COUNT_EFUSE_REG_PER_BLOCK]; extern const esp_efuse_range_addr_t range_read_addr_blocks[]; extern const esp_efuse_range_addr_t range_write_addr_blocks[]; -static int get_reg_num(int bit_start, int bit_count, int i_reg); -static int get_starting_bit_num_in_reg(int bit_start, int i_reg); static uint32_t get_mask(unsigned int bit_count, unsigned int shift); -static int get_count_bits_in_reg(int bit_start, int bit_count, int i_reg); static void write_reg(esp_efuse_block_t blk, unsigned int num_reg, uint32_t value); static uint32_t fill_reg(int bit_start_in_reg, int bit_count_in_reg, uint8_t* blob, int* filled_bits_blob); static uint32_t set_cnt_in_reg(int bit_start_in_reg, int bit_count_used_in_reg, uint32_t reg_masked, size_t* cnt); @@ -47,29 +44,36 @@ esp_err_t esp_efuse_utility_process(const esp_efuse_desc_t* field[], void* ptr, int field_len = esp_efuse_get_field_size(field); int req_size = (ptr_size_bits == 0) ? field_len : MIN(ptr_size_bits, field_len); - int i = 0; unsigned count_before = s_burn_counter; - while (err == ESP_OK && req_size > bits_counter && field[i] != NULL) { - if (check_range_of_bits(field[i]->efuse_block, field[i]->bit_start, field[i]->bit_count) == false) { + for (const esp_efuse_desc_t* f = field[0]; (err == ESP_OK && req_size > bits_counter && f != NULL); ++f) { + if (check_range_of_bits(f->efuse_block, f->bit_start, f->bit_count) == false) { ESP_EARLY_LOGE(TAG, "Range of data does not match the coding scheme"); err = ESP_ERR_CODING; + break; } - int i_reg = 0; - int num_reg; - while (err == ESP_OK && req_size > bits_counter && - (num_reg = get_reg_num(field[i]->bit_start, field[i]->bit_count, i_reg)) != -1) { - int start_bit = get_starting_bit_num_in_reg(field[i]->bit_start, i_reg); - int num_bits = get_count_bits_in_reg(field[i]->bit_start, field[i]->bit_count, i_reg); - if ((bits_counter + num_bits) > req_size) { // Limits the length of the field. - num_bits = req_size - bits_counter; + int end_bit_in_block = (f->bit_start + f->bit_count - 1); + int first_reg = f->bit_start / 32; + int last_reg = end_bit_in_block / 32; + int start_bit_in_reg = f->bit_start % 32; + + for (int num_reg = first_reg; num_reg <= last_reg; ++num_reg) { + int first_bit_in_reg = (num_reg == first_reg) ? start_bit_in_reg % 32 : 0; + int last_bit_in_reg = (num_reg == last_reg) ? end_bit_in_block % 32 : 31; + int use_bit_in_reg = (last_bit_in_reg - first_bit_in_reg) + 1; + if ((bits_counter + use_bit_in_reg) > req_size) { // Limits the length of the field. + use_bit_in_reg = req_size - bits_counter; + } + int end_bit_in_reg = start_bit_in_reg + use_bit_in_reg - 1; + ESP_EARLY_LOGD(TAG, "BLK%d REG%d [%d-%d], len=%d bits", (int)f->efuse_block, num_reg, start_bit_in_reg, end_bit_in_reg, use_bit_in_reg); + + err = func_proc(num_reg, f->efuse_block, start_bit_in_reg, use_bit_in_reg, ptr, &bits_counter); + start_bit_in_reg = 0; + + if (err != ESP_OK || req_size <= bits_counter) { + break; } - ESP_EARLY_LOGD(TAG, "In EFUSE_BLK%d__DATA%d_REG is used %d bits starting with %d bit", - (int)field[i]->efuse_block, num_reg, num_bits, start_bit); - err = func_proc(num_reg, field[i]->efuse_block, start_bit, num_bits, ptr, &bits_counter); - ++i_reg; } - i++; } unsigned count_after = s_burn_counter; if (err == ESP_OK && @@ -309,41 +313,6 @@ static uint32_t get_mask(unsigned int bit_count, unsigned int shift) return mask << shift; } -// return the register number in the array. return -1 if all registers for field was selected. -static int get_reg_num(int bit_start, int bit_count, int i_reg) -{ - int num_reg = i_reg + bit_start / 32; - - if (num_reg > (bit_start + bit_count - 1) / 32) { - return -1; - } - - return num_reg; -} - -// returns the starting bit number in the register. -static int get_starting_bit_num_in_reg(int bit_start, int i_reg) -{ - return (i_reg == 0) ? bit_start % 32 : 0; -} - -// Returns the number of bits in the register. -static int get_count_bits_in_reg(int bit_start, int bit_count, int i_reg) -{ - int ret_count = 0; - int num_reg = 0; - int last_used_bit = (bit_start + bit_count - 1); - for (int num_bit = bit_start; num_bit <= last_used_bit; ++num_bit) { - ++ret_count; - if ((((num_bit + 1) % 32) == 0) || (num_bit == last_used_bit)) { - if (i_reg == num_reg++) { - return ret_count; - } - ret_count = 0; - } - } - return 0; -} // fill efuse register from array. static uint32_t fill_reg(int bit_start_in_reg, int bit_count_in_reg, uint8_t* blob, int* filled_bits_blob) diff --git a/components/efuse/test_apps/main/test_efuse.c b/components/efuse/test_apps/main/test_efuse.c index 1785b10300..4aa63356f2 100644 --- a/components/efuse/test_apps/main/test_efuse.c +++ b/components/efuse/test_apps/main/test_efuse.c @@ -487,8 +487,11 @@ void check_efuse_table_test(int cycle) size_t field_size = esp_efuse_get_field_size(field); int arr_size = esp_efuse_utility_get_number_of_items(field_size, 8); uint8_t *arr_w = (uint8_t *) malloc(arr_size); + TEST_ASSERT_NOT_NULL(arr_w); uint8_t *arr_r = (uint8_t *) malloc(arr_size); + TEST_ASSERT_NOT_NULL(arr_r); uint8_t *arr_temp = (uint8_t *) malloc(arr_size); + TEST_ASSERT_NOT_NULL(arr_temp); ESP_LOGI(TAG, "Test#%d", num_test); for (int c = 1; c <= cycle; ++c) { ESP_LOGI(TAG, "Cycle#%d/%d", c, cycle);