mirror of
https://github.com/apache/nuttx.git
synced 2026-06-01 16:59:28 +08:00
mtdconfig/nvs: use a fixed size buffer instead of vla
use a fixed size buffer on stack instead of vla, When it needs to pass misra-c check Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
This commit is contained in:
+10
-3
@@ -212,9 +212,16 @@ config MTD_CONFIG_CACHE_SIZE
|
|||||||
int "Non-volatile Storage lookup cache size"
|
int "Non-volatile Storage lookup cache size"
|
||||||
default 0
|
default 0
|
||||||
depends on MTD_CONFIG_NVS
|
depends on MTD_CONFIG_NVS
|
||||||
help
|
---help---
|
||||||
Number of entries in Non-volatile Storage lookup cache.
|
Number of entries in Non-volatile Storage lookup cache.
|
||||||
It is recommended that it be a power of 2.
|
It is recommended that it be a power of 2.
|
||||||
|
|
||||||
|
config MTD_CONFIG_BUFFER_SIZE
|
||||||
|
int "Buffer size on the stack"
|
||||||
|
default 0
|
||||||
|
---help---
|
||||||
|
Define a fixed size buffer on the stack instead of using a
|
||||||
|
variable length array.
|
||||||
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|||||||
@@ -57,9 +57,17 @@
|
|||||||
|
|
||||||
#define NVS_CACHE_NO_ADDR 0xffffffff
|
#define NVS_CACHE_NO_ADDR 0xffffffff
|
||||||
|
|
||||||
#define NVS_ATE(name, size) \
|
#if CONFIG_MTD_CONFIG_BUFFER_SIZE > 0
|
||||||
char name##_buf[size]; \
|
# define NVS_BUFFER_SIZE(fs) CONFIG_MTD_CONFIG_BUFFER_SIZE
|
||||||
FAR struct nvs_ate *name = (FAR struct nvs_ate *)name##_buf
|
# define NVS_ATE(name, size) \
|
||||||
|
char name##_buf[CONFIG_MTD_CONFIG_BUFFER_SIZE]; \
|
||||||
|
FAR struct nvs_ate *name = (FAR struct nvs_ate *)name##_buf
|
||||||
|
#else
|
||||||
|
# define NVS_BUFFER_SIZE(fs) nvs_align_up(fs, 32)
|
||||||
|
# define NVS_ATE(name, size) \
|
||||||
|
char name##_buf[size]; \
|
||||||
|
FAR struct nvs_ate *name = (FAR struct nvs_ate *)name##_buf
|
||||||
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Types
|
* Private Types
|
||||||
@@ -204,6 +212,15 @@ static inline size_t nvs_align_up(FAR struct nvs_fs *fs, size_t len)
|
|||||||
return (len + (fs->progsize - 1)) & ~(fs->progsize - 1);
|
return (len + (fs->progsize - 1)) & ~(fs->progsize - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: nvs_align_down
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static inline size_t nvs_align_down(FAR struct nvs_fs *fs, size_t len)
|
||||||
|
{
|
||||||
|
return len & ~(fs->progsize - 1);
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: nvs_ate_size
|
* Name: nvs_ate_size
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@@ -220,15 +237,6 @@ static inline size_t nvs_ate_size(FAR struct nvs_fs *fs)
|
|||||||
return nvs_align_up(fs, sizeof(struct nvs_ate)) + fs->progsize;
|
return nvs_align_up(fs, sizeof(struct nvs_ate)) + fs->progsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Name: nvs_buffer_size
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
static inline size_t nvs_buffer_size(FAR struct nvs_fs *fs)
|
|
||||||
{
|
|
||||||
return nvs_align_up(fs, 32);
|
|
||||||
}
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: nvs_ate_expired
|
* Name: nvs_ate_expired
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@@ -340,7 +348,7 @@ static int nvs_flash_rd(FAR struct nvs_fs *fs, uint32_t addr,
|
|||||||
|
|
||||||
return nvs_flash_brd(fs, offset, data, len);
|
return nvs_flash_brd(fs, offset, data, len);
|
||||||
#else
|
#else
|
||||||
uint8_t buf[fs->progsize];
|
uint8_t buf[NVS_BUFFER_SIZE(fs)];
|
||||||
size_t bytes_to_rd;
|
size_t bytes_to_rd;
|
||||||
off_t begin_padding;
|
off_t begin_padding;
|
||||||
off_t offset;
|
off_t offset;
|
||||||
@@ -466,13 +474,14 @@ static int nvs_flash_block_cmp(FAR struct nvs_fs *fs, uint32_t addr,
|
|||||||
FAR const void *data, size_t len)
|
FAR const void *data, size_t len)
|
||||||
{
|
{
|
||||||
FAR const uint8_t *data8 = (FAR const uint8_t *)data;
|
FAR const uint8_t *data8 = (FAR const uint8_t *)data;
|
||||||
uint8_t buf[nvs_buffer_size(fs)];
|
uint8_t buf[NVS_BUFFER_SIZE(fs)];
|
||||||
|
size_t buf_size = nvs_align_down(fs, sizeof(buf));
|
||||||
size_t bytes_to_cmp;
|
size_t bytes_to_cmp;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
while (len > 0)
|
while (len > 0)
|
||||||
{
|
{
|
||||||
bytes_to_cmp = MIN(sizeof(buf), len);
|
bytes_to_cmp = MIN(buf_size, len);
|
||||||
rc = nvs_flash_rd(fs, addr, buf, bytes_to_cmp);
|
rc = nvs_flash_rd(fs, addr, buf, bytes_to_cmp);
|
||||||
if (rc)
|
if (rc)
|
||||||
{
|
{
|
||||||
@@ -506,14 +515,15 @@ static int nvs_flash_block_cmp(FAR struct nvs_fs *fs, uint32_t addr,
|
|||||||
static int nvs_flash_direct_cmp(FAR struct nvs_fs *fs, uint32_t addr1,
|
static int nvs_flash_direct_cmp(FAR struct nvs_fs *fs, uint32_t addr1,
|
||||||
uint32_t addr2, size_t len)
|
uint32_t addr2, size_t len)
|
||||||
{
|
{
|
||||||
uint8_t buf1[nvs_buffer_size(fs)];
|
uint8_t buf1[NVS_BUFFER_SIZE(fs)];
|
||||||
uint8_t buf2[nvs_buffer_size(fs)];
|
uint8_t buf2[NVS_BUFFER_SIZE(fs)];
|
||||||
|
size_t buf_size = nvs_align_down(fs, sizeof(buf1));
|
||||||
size_t bytes_to_cmp;
|
size_t bytes_to_cmp;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
while (len > 0)
|
while (len > 0)
|
||||||
{
|
{
|
||||||
bytes_to_cmp = MIN(sizeof(buf1), len);
|
bytes_to_cmp = MIN(buf_size, len);
|
||||||
rc = nvs_flash_rd(fs, addr1, buf1, bytes_to_cmp);
|
rc = nvs_flash_rd(fs, addr1, buf1, bytes_to_cmp);
|
||||||
if (rc)
|
if (rc)
|
||||||
{
|
{
|
||||||
@@ -553,14 +563,15 @@ static int nvs_flash_direct_cmp(FAR struct nvs_fs *fs, uint32_t addr1,
|
|||||||
static int nvs_flash_cmp_const(FAR struct nvs_fs *fs, uint32_t addr,
|
static int nvs_flash_cmp_const(FAR struct nvs_fs *fs, uint32_t addr,
|
||||||
uint8_t value, size_t len)
|
uint8_t value, size_t len)
|
||||||
{
|
{
|
||||||
uint8_t cmp[nvs_buffer_size(fs)];
|
uint8_t cmp[NVS_BUFFER_SIZE(fs)];
|
||||||
|
size_t buf_size = nvs_align_down(fs, sizeof(cmp));
|
||||||
size_t bytes_to_cmp;
|
size_t bytes_to_cmp;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
memset(cmp, value, sizeof(cmp));
|
memset(cmp, value, sizeof(cmp));
|
||||||
while (len > 0)
|
while (len > 0)
|
||||||
{
|
{
|
||||||
bytes_to_cmp = MIN(sizeof(cmp), len);
|
bytes_to_cmp = MIN(buf_size, len);
|
||||||
rc = nvs_flash_block_cmp(fs, addr, cmp, bytes_to_cmp);
|
rc = nvs_flash_block_cmp(fs, addr, cmp, bytes_to_cmp);
|
||||||
if (rc)
|
if (rc)
|
||||||
{
|
{
|
||||||
@@ -586,13 +597,14 @@ static int nvs_flash_cmp_const(FAR struct nvs_fs *fs, uint32_t addr,
|
|||||||
static int nvs_flash_block_move(FAR struct nvs_fs *fs, uint32_t addr,
|
static int nvs_flash_block_move(FAR struct nvs_fs *fs, uint32_t addr,
|
||||||
size_t len)
|
size_t len)
|
||||||
{
|
{
|
||||||
uint8_t buf[nvs_buffer_size(fs)];
|
uint8_t buf[NVS_BUFFER_SIZE(fs)];
|
||||||
|
size_t buf_size = nvs_align_down(fs, sizeof(buf));
|
||||||
size_t bytes_to_copy;
|
size_t bytes_to_copy;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
while (len)
|
while (len)
|
||||||
{
|
{
|
||||||
bytes_to_copy = MIN(sizeof(buf), len);
|
bytes_to_copy = MIN(buf_size, len);
|
||||||
rc = nvs_flash_rd(fs, addr, buf, bytes_to_copy);
|
rc = nvs_flash_rd(fs, addr, buf, bytes_to_copy);
|
||||||
if (rc)
|
if (rc)
|
||||||
{
|
{
|
||||||
@@ -799,7 +811,7 @@ static int nvs_flash_wrt_entry(FAR struct nvs_fs *fs, uint32_t id,
|
|||||||
{
|
{
|
||||||
size_t ate_size = nvs_ate_size(fs);
|
size_t ate_size = nvs_ate_size(fs);
|
||||||
NVS_ATE(entry, ate_size);
|
NVS_ATE(entry, ate_size);
|
||||||
uint8_t buf[fs->progsize];
|
uint8_t buf[NVS_BUFFER_SIZE(fs)];
|
||||||
uint16_t copy_len = 0;
|
uint16_t copy_len = 0;
|
||||||
uint16_t left;
|
uint16_t left;
|
||||||
int rc;
|
int rc;
|
||||||
@@ -826,14 +838,14 @@ static int nvs_flash_wrt_entry(FAR struct nvs_fs *fs, uint32_t id,
|
|||||||
/* Write align block which include part key + part data */
|
/* Write align block which include part key + part data */
|
||||||
|
|
||||||
left = rc;
|
left = rc;
|
||||||
memset(buf, fs->erasestate, sizeof(buf));
|
memset(buf, fs->erasestate, fs->progsize);
|
||||||
|
|
||||||
copy_len = (left + len) <= sizeof(buf) ?
|
copy_len = (left + len) <= fs->progsize ?
|
||||||
len : (sizeof(buf) - left);
|
len : (fs->progsize - left);
|
||||||
|
|
||||||
memcpy(buf, key + key_size - left, left);
|
memcpy(buf, key + key_size - left, left);
|
||||||
memcpy(buf + left, data, copy_len);
|
memcpy(buf + left, data, copy_len);
|
||||||
rc = nvs_flash_data_wrt(fs, buf, sizeof(buf));
|
rc = nvs_flash_data_wrt(fs, buf, fs->progsize);
|
||||||
if (rc)
|
if (rc)
|
||||||
{
|
{
|
||||||
ferr("Write value failed, rc=%d\n", rc);
|
ferr("Write value failed, rc=%d\n", rc);
|
||||||
@@ -852,10 +864,10 @@ static int nvs_flash_wrt_entry(FAR struct nvs_fs *fs, uint32_t id,
|
|||||||
/* Add padding at the end of data */
|
/* Add padding at the end of data */
|
||||||
|
|
||||||
left = rc;
|
left = rc;
|
||||||
memset(buf, fs->erasestate, sizeof(buf));
|
memset(buf, fs->erasestate, fs->progsize);
|
||||||
memcpy(buf, data + len - left, left);
|
memcpy(buf, data + len - left, left);
|
||||||
|
|
||||||
rc = nvs_flash_data_wrt(fs, buf, sizeof(buf));
|
rc = nvs_flash_data_wrt(fs, buf, fs->progsize);
|
||||||
if (rc)
|
if (rc)
|
||||||
{
|
{
|
||||||
ferr("Write value failed, rc=%d\n", rc);
|
ferr("Write value failed, rc=%d\n", rc);
|
||||||
@@ -1088,11 +1100,11 @@ static int nvs_add_gc_done_ate(FAR struct nvs_fs *fs)
|
|||||||
|
|
||||||
static int nvs_expire_ate(FAR struct nvs_fs *fs, uint32_t addr)
|
static int nvs_expire_ate(FAR struct nvs_fs *fs, uint32_t addr)
|
||||||
{
|
{
|
||||||
uint8_t expired[fs->progsize];
|
uint8_t expired[NVS_BUFFER_SIZE(fs)];
|
||||||
memset(expired, ~fs->erasestate, sizeof(expired));
|
memset(expired, ~fs->erasestate, fs->progsize);
|
||||||
|
|
||||||
return nvs_flash_wrt(fs, addr + nvs_align_up(fs, sizeof(struct nvs_ate)),
|
return nvs_flash_wrt(fs, addr + nvs_align_up(fs, sizeof(struct nvs_ate)),
|
||||||
expired, sizeof(expired));
|
expired, fs->progsize);
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@@ -1274,6 +1286,10 @@ static int nvs_startup(FAR struct nvs_fs *fs)
|
|||||||
NVS_ATE(second_ate, ate_size);
|
NVS_ATE(second_ate, ate_size);
|
||||||
NVS_ATE(last_ate, ate_size);
|
NVS_ATE(last_ate, ate_size);
|
||||||
|
|
||||||
|
#if CONFIG_MTD_CONFIG_BUFFER_SIZE > 0
|
||||||
|
DEBUGASSERT(ate_size <= CONFIG_MTD_CONFIG_BUFFER_SIZE);
|
||||||
|
#endif
|
||||||
|
|
||||||
rc = MTD_IOCTL(fs->mtd, MTDIOC_ERASESTATE,
|
rc = MTD_IOCTL(fs->mtd, MTDIOC_ERASESTATE,
|
||||||
(unsigned long)((uintptr_t)&fs->erasestate));
|
(unsigned long)((uintptr_t)&fs->erasestate));
|
||||||
if (rc < 0)
|
if (rc < 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user