乐鑫技术交流
直播中

最强海贼王

9年用户 1456经验值
擅长:MEMS/传感技术
私信 关注
[问答]

wear_levelling闪存加密和FAT文件系统损坏怎么解决?

我们团队的项目使用 wear_levelling 示例项目中演示的 FatFs 文件系统来嵌入三个文本文件,每个文件最大 4KB。在 FAT 数据分区上启用闪存加密后,我们注意到第二个或第三个文件会周期性地完全损坏小字符串,或者在大文件的后半部分损坏。每次擦除并重新格式化闪存分区后都会出现这种情况,删除和替换文件后偶尔也会出现这种情况。对于给定的文件,损坏的值似乎是一致的

在启用闪存加密和安全启动 v2 的 ESP32-PICO-MINI-02 devkit 上进行一些修改后,在 wear_levelling 示例中重新创建了此行为。进一步的实验表明,当在 sdkconfig 中将 WL_SECTOR_SIZE 设置为 4096 或 0xB1000 字节(扇区大小为 512)时,当 FAT 分区大小至少为 0x84000 字节大时,该问题将消失。

闪存加密文档中指出,NVS存储库与闪存加密不兼容,但我们找不到对 FatFs 库的直接引用。闪存加密与 FAT 文件系统兼容吗?如果是这样,除了 FatFs 文档中描述的要求之外,是否还有其他最小分区大小要求以确保正常运行?

修改后的 wear_levelling 示例代码:
代码:全选
void app_main(void)
{
    ESP_LOGI(TAG, "Mounting FAT filesystem");
    // To mount device we need name of device partition, define base_path
    // and allow format partition in case if it is new one and was not formated before
    const esp_vfs_fat_mount_config_t mount_config = {
            .max_files = 4,
            .format_if_mount_failed = true,
            .allocation_unit_size = CONFIG_WL_SECTOR_SIZE
    };
    esp_err_t err = esp_vfs_fat_spiflash_mount(base_path, "storage", &mount_config, &s_wl_handle);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
        return;
    }
    ESP_LOGI(TAG, "Opening file");
    FILE *f = fopen("/spiflash/hello.txt", "wb");
    if (f == NULL) {
        ESP_LOGE(TAG, "Failed to open file for writing");
        return;
    }
    fprintf(f, "written using ESP-IDF %sn", esp_get_idf_version());
    fclose(f);
    ESP_LOGI(TAG, "File written");

    // Open file for reading
    ESP_LOGI(TAG, "Reading file");
    f = fopen("/spiflash/hello.txt", "rb");
    if (f == NULL) {
        ESP_LOGE(TAG, "Failed to open file for reading");
        return;
    }
    char line[128];
    fgets(line, sizeof(line), f);
    fclose(f);
    // strip newline
    char *pos = strchr(line, 'n');
    if (pos) {
        *pos = '';
    }
    ESP_LOGI(TAG, "Read from file: '%s'", line);


    //File 2
    ESP_LOGI(TAG, "Opening file");
    f = fopen("/spiflash/hello2.txt", "wb");
    if (f == NULL) {
        ESP_LOGE(TAG, "Failed to open file for writing");
        return;
    }
        fprintf(f, "written using ESP-IDF %sn", esp_get_idf_version());
    fclose(f);
    ESP_LOGI(TAG, "File written");

    // Open file for reading
    ESP_LOGI(TAG, "Reading file");
    f = fopen("/spiflash/hello2.txt", "rb");
    if (f == NULL) {
        ESP_LOGE(TAG, "Failed to open file for reading");
        return;
    }
    fgets(line, sizeof(line), f);
    fclose(f);
    // strip newline
    pos = strchr(line, 'n');
    if (pos) {
        *pos = '';
    }
    ESP_LOGI(TAG, "Read from file: '%s'", line);

    //File 3
    ESP_LOGI(TAG, "Opening file");
    f = fopen("/spiflash/hello3.txt", "wb");
    if (f == NULL) {
        ESP_LOGE(TAG, "Failed to open file for writing");
        return;
    }
    fprintf(f, "written using ESP-IDF %sn", esp_get_idf_version());
    fclose(f);
    ESP_LOGI(TAG, "File written");

    // Open file for reading
    ESP_LOGI(TAG, "Reading file");
    f = fopen("/spiflash/hello3.txt", "rb");
    if (f == NULL) {
        ESP_LOGE(TAG, "Failed to open file for reading");
        return;
    }
    fgets(line, sizeof(line), f);
    fclose(f);
    // strip newline
    pos = strchr(line, 'n');
    if (pos) {
        *pos = '';
    }
    ESP_LOGI(TAG, "Read from file: '%s'", line);

    // Unmount FATFS
    ESP_LOGI(TAG, "Unmounting FAT filesystem");
    ESP_ERROR_CHECK( esp_vfs_fat_spiflash_unmount(base_path, s_wl_handle));

    ESP_LOGI(TAG, "Done");
}

监控发生错误的输出:
代码:全选
I (0) cpu_start: App cpu up.
I (179) cpu_start: Pro cpu start user code
I (179) cpu_start: cpu freq: 160000000
I (179) cpu_start: Application information:
I (184) cpu_start: Project name:     wear_levelling_example
I (190) cpu_start: App version:      1
I (195) cpu_start: Compile time:     Dec  1 2022 14:40:40
I (201) cpu_start: ELF file SHA256:  bea26e30432862ed...
I (207) cpu_start: ESP-IDF:          v4.4.2-2-gc59a104546
I (213) heap_init: Initializing. RAM available for dynamic allocation:
I (220) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (226) heap_init: At 3FFB2C58 len 0002D3A8 (180 KiB): DRAM
I (232) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (239) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (245) heap_init: At 4008B6D4 len 0001492C (82 KiB): IRAM
I (252) spi_flash: detected chip: generic
I (256) spi_flash: flash io: dio
W (260) flash_encrypt: Flash encryption mode is DEVELOPMENT (not secure)
I (269) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (278) example: Mounting FAT filesystem
W (288) vfs_fat_spiflash: f_mount failed (13)
I (298) vfs_fat_spiflash: Formatting FATFS partition, allocation unit size=512
I (1998) vfs_fat_spiflash: Mounting again
I (1998) example: Opening file
I (2798) example: File written
I (2798) example: Reading file
I (2808) example: Read from file: 'written using ESP-IDF v4.4.2-2-gc59a104546'
I (2808) example: Opening file
I (3628) example: File written
I (3638) example: Reading file
I (3638) example: Read from file: 'written using ESP-IDF v4.4.2-2-gc59a104546'
I (3648) example: Opening file
I (4478) example: File written
I (4488) example: Reading file
I (4498) example: Read from file: '�(p�`���b�,(�`q:�`�k'
I (4498) example: Unmounting FAT filesystem
I (4638) example: Done
只需跟进此线程即可获得关于 FAT 文件系统组件是否正式支持闪存加密的即时响应。如果答案是否定的,我们希望尽快开始寻找替代方案。如果是,我们将继续等待更多信息的回复。谢谢。


                       

更多回帖

发帖
×
20
完善资料,
赚取积分