Cannot create/read exsisting files using FATFS with wear levelling [secureboot + flashencryption]

noyelseth@gmail.com
Posts: 2
Joined: Tue Jul 06, 2021 12:51 pm

Cannot create/read exsisting files using FATFS with wear levelling [secureboot + flashencryption]

Postby noyelseth@gmail.com » Mon Oct 18, 2021 6:37 am

Environment
Module or chip used: ESP32-S2-WROOM-I
IDF version: v4.2.1-141-g1e3638390
Build System: idf.py
Operating System: Windows10
Using an IDE: Eclipse
Power Supply: USB

Problem Description
I'm not able to read & write a file using non-encrypted FATFS with wear levelling, secure boot[V2] and flash encryption[Release-Mode].

Wear levelling's Sdkconfig screenshot attached.
wearlevelling.png
wearlevelling.png (5.18 KiB) Viewed 929 times

Partition Table screenshot attached.
partition_table.png
partition_table.png (10.95 KiB) Viewed 929 times

I'm using wear levelling partition to store some states/errors/values into separate files. I'm using a maximum of 4 files and use file operation one at a time.
This problem is shown in some of the devices randomly likes 1 out of 10 devices.

I'm using the wrapper function to write and read files into the wear levelling partition.


Init/Mount Wear-levelling partition
  1. // Handle of the wear levelling library instance
  2. static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
  3.  
  4. /**
  5.  *
  6.  */
  7. esp_err_t init_wearlevel() {
  8.     ESP_LOGI(TAG, "Mounting FAT filesystem");
  9.     // To mount device we need name of device partition, define base_path
  10.     // and allow format partition in case if it is new one and was not formated before
  11.     const esp_vfs_fat_mount_config_t mount_config = { .max_files = 4, .format_if_mount_failed = true,
  12.             .allocation_unit_size = CONFIG_WL_SECTOR_SIZE };
  13.     esp_err_t err = esp_vfs_fat_spiflash_mount(WEARLEVEL_FATFS_BASE_PATH, "storage", &mount_config, &s_wl_handle);
  14.     if (err != ESP_OK) {
  15.         ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
  16.         return err;
  17.     }
  18.     vTaskDelay(pdMS_TO_TICKS(100));
  19.     return ESP_OK;
  20. }


Write Function
  1. /**
  2.  *
  3.  */
  4.  esp_err_t write_into_file(const char *file_path, void *data, size_t data_len) {
  5.     vTaskDelay(pdMS_TO_TICKS(100));
  6.     ESP_LOGI(TAG, "Writing File: %s", file_path);
  7.     FILE *fptr = fopen((const char *)file_path, "wb");
  8.     if (fptr == NULL) {
  9.         ESP_LOGE(TAG, "Failed to open file for writing: %s", file_path);
  10.         return ESP_FAIL;
  11.     }
  12.  
  13.     vTaskDelay(pdMS_TO_TICKS(100));
  14.     size_t w_byte = fwrite(data, data_len, 1, fptr);
  15.  
  16.     vTaskDelay(pdMS_TO_TICKS(100));
  17.  
  18.     fclose(fptr);
  19.  
  20.     if (w_byte != 1) { // write 1 number of member into to file
  21.         ESP_LOGE(TAG, "Failed to write data into the file: %s", file_path);
  22.         return ESP_FAIL;
  23.     }
  24.     ESP_LOGI(TAG, "");
  25.     return ESP_OK;
  26. }


Read Function
  1. /**
  2.  *
  3.  */
  4. esp_err_t read_from_file(const char *file_path, void *data, size_t data_len) {
  5.     vTaskDelay(pdMS_TO_TICKS(100));
  6.     ESP_LOGI(TAG, "Reading File: %s", file_path);
  7.     FILE *fptr = fopen((const char *)file_path, "rb");
  8.     if (fptr == NULL) {
  9.         ESP_LOGE(TAG, "Failed to open file for reading: %s", file_path);
  10.         return ESP_FAIL;
  11.     }
  12.  
  13.     vTaskDelay(pdMS_TO_TICKS(100));
  14.  
  15.     size_t r_byte = fread(data, data_len, 1, fptr);
  16.  
  17.     vTaskDelay(pdMS_TO_TICKS(100));
  18.  
  19.     fclose(fptr);
  20.  
  21.     if (r_byte != 1) {
  22.         ESP_LOGE(TAG, "Failed to read data from the file: %s", file_path);
  23.         return ESP_FAIL;
  24.     }
  25.     ESP_LOGI(TAG, "");
  26.     vTaskDelay(pdMS_TO_TICKS(100));
  27.     return ESP_OK;
  28. }


Functions Usages
  1. // Wearlevelling Storage Path
  2. #define CUSTOM_MAC_ADDR_FNAME "/spiflash/cust_mac.txt"
  3. uint8_t cust_mac[6] = { 0x7c, 0x7c, 0x7c, 0x00, 0x00, 0x01 };
  4.  
  5. /**
  6.  *
  7.  */
  8. if(write_into_file(CUSTOM_MAC_ADDR_FNAME , cust_mac, sizeof(cust_mac) != ESP_OK){
  9. return ESP_FAIL;
  10. }
  11.  
  12. /**
  13.  *
  14.  */
  15. uint8_t read_cust_mac[6] = { 0 };
  16. esp_err_t get_mac_id_from_file()
  17. {
  18.     ESP_LOGI(TAG, "Going to read custom mac data from %s",CUSTOM_MAC_ADDR_FNAME);
  19.    
  20.     esp_err_t err = read_from_file(CUSTOM_MAC_ADDR_FNAME, read_cust_mac, sizeof(read_cust_mac));
  21.     if(err == ESP_OK) {
  22.         ESP_LOGI(TAG, "MAC read from file = %02x:%02x:%02x:%02x:%02x:%02x", read_cust_mac[0],read_cust_mac[1],read_cust_mac[2],
  23. read_cust_mac[3],read_cust_mac[4],read_cust_mac[5],);
  24.         return ESP_OK;
  25.     } else {
  26.         return ESP_FAIL;
  27.     }
  28.    
  29.     vTaskDelay(30 / portTICK_PERIOD_MS);
  30. }

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 111 guests