Skip to content

Commit a58a2b6

Browse files
committed
Merge branch 'bugfix/wl_encryption_size' into 'master'
Bugfix/wl encryption size See merge request idf/esp-idf!2037
2 parents 36098f3 + 5cbb194 commit a58a2b6

File tree

6 files changed

+37
-14
lines changed

6 files changed

+37
-14
lines changed

components/wear_levelling/WL_Flash.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,13 @@ esp_err_t WL_Flash::updateWL()
318318
uint32_t byte_pos = this->state.pos * this->cfg.wr_size;
319319
this->used_bits = 0;
320320
// write state to mem. We updating only affected bits
321-
result |= this->flash_drv->write(this->addr_state1 + sizeof(wl_state_t) + byte_pos, &this->used_bits, 1);
321+
result |= this->flash_drv->write(this->addr_state1 + sizeof(wl_state_t) + byte_pos, &this->used_bits, this->cfg.wr_size);
322322
if (result != ESP_OK) {
323323
ESP_LOGE(TAG, "%s - update position 1 result=%08x", __func__, result);
324324
this->state.access_count = this->state.max_count - 1; // we will update next time
325325
return result;
326326
}
327-
result |= this->flash_drv->write(this->addr_state2 + sizeof(wl_state_t) + byte_pos, &this->used_bits, 1);
327+
result |= this->flash_drv->write(this->addr_state2 + sizeof(wl_state_t) + byte_pos, &this->used_bits, this->cfg.wr_size);
328328
if (result != ESP_OK) {
329329
ESP_LOGE(TAG, "%s - update position 2 result=%08x", __func__, result);
330330
this->state.access_count = this->state.max_count - 1; // we will update next time

components/wear_levelling/private_include/WL_Config.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@
2020
* @brief This class is used as a structure to configure wear levelling module
2121
*
2222
*/
23-
typedef struct WL_Config_s {
24-
size_t start_addr; /*!< start address in the flash*/
23+
24+
#if defined(_MSC_VER)
25+
#define ALIGNED_(x) __declspec(align(x))
26+
#else
27+
#if defined(__GNUC__)
28+
#define ALIGNED_(x) __attribute__ ((aligned(x)))
29+
#endif
30+
#endif
31+
32+
typedef struct ALIGNED_(16) WL_Config_s { /*!< Size of wl_config_t structure should be divided by 16 for encryption*/
33+
size_t start_addr; /*!< start address in the flash*/
2534
uint32_t full_mem_size; /*!< Amount of memory used to store data in bytes*/
2635
uint32_t page_size; /*!< One page size in bytes. Page could be more then memory block. This parameter must be page_size >= N*block_size.*/
2736
uint32_t sector_size; /*!< size of flash memory sector that will be erased and stored at once (erase)*/
@@ -30,7 +39,10 @@ typedef struct WL_Config_s {
3039
uint32_t version; /*!< A version of current implementatioon. To erase and reallocate complete memory this ID must be different from id before.*/
3140
size_t temp_buff_size; /*!< Size of temporary allocated buffer to copy from one flash area to another. The best way, if this value will be equal to sector size.*/
3241
uint32_t crc; /*!< CRC for this config*/
33-
3442
} wl_config_t;
3543

44+
#ifndef _MSC_VER // MSVS has different format for this define
45+
static_assert(sizeof(wl_config_t) % 16 == 0, "Size of wl_config_t structure should be compatible with flash encryption");
46+
#endif // _MSC_VER
47+
3648
#endif // _WL_Config_H_

components/wear_levelling/test_wl_host/Flash_Emulator.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
#include <stdlib.h>
1919
#include <string.h>
2020

21-
Flash_Emulator::Flash_Emulator(size_t size, size_t sector_sise)
21+
Flash_Emulator::Flash_Emulator(size_t size, size_t sector_sise, size_t min_size)
2222
{
2323
this->reset_count = 0x7fffffff;
2424
this->size = size;
2525
this->sector_sise = sector_sise;
26+
this->min_size = min_size;
2627
this->buff = (uint8_t *)malloc(this->size);
2728
this->access_count = new uint32_t[this->size / this->sector_sise];
2829
memset(this->access_count, 0, this->size / this->sector_sise * sizeof(uint32_t));
@@ -81,6 +82,14 @@ esp_err_t Flash_Emulator::erase_range(size_t start_address, size_t size)
8182
esp_err_t Flash_Emulator::write(size_t dest_addr, const void *src, size_t size)
8283
{
8384
esp_err_t result = ESP_OK;
85+
if ((size % this->min_size) != 0) {
86+
result = ESP_ERR_INVALID_SIZE;
87+
return result;
88+
}
89+
if ((dest_addr % this->min_size) != 0) {
90+
result = ESP_ERR_INVALID_SIZE;
91+
return result;
92+
}
8493
if ((this->reset_count != 0x7fffffff) && (this->reset_count != 0)) {
8594
this->reset_count--;
8695
}

components/wear_levelling/test_wl_host/Flash_Emulator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Flash_Emulator : public Flash_Access
2525
{
2626

2727
public:
28-
Flash_Emulator(size_t size, size_t sector_sise);
28+
Flash_Emulator(size_t size, size_t sector_sise, size_t min_size);
2929

3030
virtual size_t chip_size();
3131

@@ -43,6 +43,7 @@ class Flash_Emulator : public Flash_Access
4343
public:
4444
size_t size;
4545
size_t sector_sise;
46+
size_t min_size;
4647
uint8_t *buff;
4748

4849
uint32_t *access_count;

components/wear_levelling/test_wl_host/wl_tests_host.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#define FLASH_PAGE_SIZE FLASH_SECTOR_SIZE*1
2626
#define FLASH_UPDATERATE 3
2727
#define FLASH_TEMP_SIZE FLASH_SECTOR_SIZE
28-
#define FLASH_WR_BLOCK_SIZE 2
28+
#define FLASH_WR_BLOCK_SIZE 16
2929

3030
static const char *TAG = "wl_test_host";
3131
Flash_Access *s_flash;
@@ -47,7 +47,7 @@ TEST_CASE("flash starts with all bytes == 0xff", "[spi_flash_emu]")
4747
wl->wr_size = FLASH_WR_BLOCK_SIZE;
4848

4949
WL_Flash *wl_flash = new WL_Flash();
50-
Flash_Emulator *emul = new Flash_Emulator(FLASH_ACCESS_SIZE + FLASH_START_ADDR, FLASH_SECTOR_SIZE);
50+
Flash_Emulator *emul = new Flash_Emulator(FLASH_ACCESS_SIZE + FLASH_START_ADDR, FLASH_SECTOR_SIZE, FLASH_WR_BLOCK_SIZE);
5151
CHECK(wl_flash->config(wl, emul) == ESP_OK);
5252

5353
test_power_down(wl_flash, emul, TEST_COUNT_MAX);

components/wear_levelling/wear_levelling.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ esp_err_t wl_mount(const esp_partition_t *partition, wl_handle_t *out_handle)
7676
break;
7777
}
7878
}
79-
if (*out_handle == WL_INVALID_HANDLE) {
80-
ESP_LOGE(TAG, "MAX_WL_HANDLES=%d instances already allocated", MAX_WL_HANDLES);
81-
result = ESP_ERR_NO_MEM;
82-
goto out;
83-
}
8479

8580
wl_ext_cfg_t cfg;
8681
cfg.full_mem_size = partition->size;
@@ -94,6 +89,12 @@ esp_err_t wl_mount(const esp_partition_t *partition, wl_handle_t *out_handle)
9489
// FAT sector size by default will be 512
9590
cfg.fat_sector_size = CONFIG_WL_SECTOR_SIZE;
9691

92+
if (*out_handle == WL_INVALID_HANDLE) {
93+
ESP_LOGE(TAG, "MAX_WL_HANDLES=%d instances already allocated", MAX_WL_HANDLES);
94+
result = ESP_ERR_NO_MEM;
95+
goto out;
96+
}
97+
9798
// Allocate memory for a Partition object, and then initialize the object
9899
// using placement new operator. This way we can recover from out of
99100
// memory condition.

0 commit comments

Comments
 (0)