xiaoxiang781216 commented on code in PR #1288: URL: https://github.com/apache/incubator-nuttx-apps/pull/1288#discussion_r956598043
########## testing/mtd_config_fs/Makefile: ########## @@ -0,0 +1,34 @@ +############################################################################ +# apps/testing/mtd_nvs/Makefile Review Comment: ```suggestion # apps/testing/mtd_config_fs/Makefile ``` ########## testing/mtd_config_fs/Kconfig: ########## @@ -0,0 +1,49 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config TESTING_FAILSAFE_MTD_CONFIG Review Comment: let's change prefix from TESTING_FAILSAFE_MTD_CONFIG_ to TESTING_MTD_CONFIG_FAILSAFE_ ########## testing/mtd_config_fs/mtd_config_fs_test_main.c: ########## @@ -0,0 +1,2040 @@ +/**************************************************************************** + * apps/testing/mtd_config_fs/mtd_config_fs_test_main.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/mtd/mtd.h> +#include <nuttx/mtd/configdata.h> + +#include <sys/mount.h> +#include <sys/ioctl.h> +#include <sys/statfs.h> + +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <malloc.h> +#include <unistd.h> +#include <fcntl.h> +#include <dirent.h> +#include <string.h> +#include <errno.h> +#include <nuttx/crc8.h> +#include <debug.h> +#include <assert.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Configuration ************************************************************/ + +#define TEST_KEY1 "testkey1" +#define TEST_KEY2 "testkey2" +#define TEST_DATA1 "abc" +#define TEST_DATA2 "def" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Allocation Table Entry */ + +begin_packed_struct struct nvs_ate +{ + uint32_t id; /* data id */ + uint16_t offset; /* data offset within sector */ + uint16_t len; /* data len within sector */ + uint16_t key_len; /* key string len */ + uint8_t part; /* part of a multipart data - future extension */ + uint8_t crc8; /* crc8 check of the ate entry */ + uint8_t expired; /* 0xFF-newest entry, others-old entry */ + uint8_t reserved; /* for future extension */ + uint16_t reserved2; /* for future extension */ +} end_packed_struct; + +/* Pre-allocated simulated flash */ + +struct mtdnvs_ctx_s +{ + char mountdir[CONFIG_TESTING_FAILSAFE_MTD_CONFIG_MOUNTPT_MAXNAME]; + struct mallinfo mmbefore; + struct mallinfo mmprevious; + struct mallinfo mmafter; +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: fnv_hash_32 + ****************************************************************************/ + +static uint32_t fnv_hash_32(FAR const uint8_t *input, uint32_t len) +{ + uint32_t i = 0; + uint32_t hval = 2166136261; + + /* FNV-1 hash each octet in the buffer */ + + while (i++ < len) + { + /* multiply by the 32 bit FNV magic prime mod 2^32 */ + + hval *= 0x01000193; + + /* xor the bottom with the current octet */ + + hval ^= (uint32_t)*input++; + } + + return hval; +} + +/**************************************************************************** + * Name: fill_crc8_update + ****************************************************************************/ + +static void fill_crc8_update(FAR struct nvs_ate *entry) +{ + uint8_t ate_crc; + + ate_crc = crc8part((FAR const uint8_t *)entry, + offsetof(struct nvs_ate, crc8), 0xff); + entry->crc8 = ate_crc; +} + +/**************************************************************************** + * Name: fill_gc_done_ate + ****************************************************************************/ + +static void fill_corrupted_close_ate(FAR struct nvs_ate *close_ate) +{ + memset((FAR void *)close_ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + close_ate->id = 0xffffffff; + close_ate->len = 0U; +} + +/**************************************************************************** + * Name: fill_close_ate + ****************************************************************************/ + +static void fill_close_ate(FAR struct nvs_ate *close_ate, int offset) +{ + memset((FAR void *)close_ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + close_ate->id = 0xffffffff; + close_ate->len = 0U; + close_ate->offset = offset; + fill_crc8_update(close_ate); +} + +/**************************************************************************** + * Name: fill_gc_done_ate + ****************************************************************************/ + +static void fill_gc_done_ate(FAR struct nvs_ate *gc_done_ate) +{ + memset((FAR void *)gc_done_ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + gc_done_ate->id = 0xffffffff; + gc_done_ate->len = 0U; + fill_crc8_update(gc_done_ate); +} + +/**************************************************************************** + * Name: fill_ate + ****************************************************************************/ + +static void fill_ate(FAR struct nvs_ate *ate, FAR const char *key, + uint16_t len, uint16_t offset, bool expired) +{ + memset((FAR void *)ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + ate->id = fnv_hash_32((FAR const uint8_t *)key, strlen(key) + 1) + % 0xfffffffd + 1; + ate->len = len; + ate->offset = offset; + ate->key_len = strlen(key) + 1; + fill_crc8_update(ate); + ate->expired = expired ? 0x7f : 0xff; +} + +/**************************************************************************** + * Name: fill_corrupted_ate + ****************************************************************************/ + +static void fill_corrupted_ate(FAR struct nvs_ate *ate, + FAR const char *key, uint16_t len, uint16_t offset) +{ + memset((FAR void *)ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + ate->id = fnv_hash_32((FAR const uint8_t *)key, strlen(key) + 1) + % 0xfffffffd + 1; + ate->len = len; + ate->offset = offset; + ate->key_len = strlen(key) + 1; +} + +/**************************************************************************** + * Name: mtdnvs_showmemusage + ****************************************************************************/ + +static void mtdnvs_showmemusage(struct mallinfo *mmbefore, + struct mallinfo *mmafter) +{ + printf("VARIABLE BEFORE AFTER DELTA\n"); + printf("======== ======== ======== ========\n"); + printf("arena %8x %8x %8x\n", mmbefore->arena , mmafter->arena, + mmafter->arena - mmbefore->arena); + printf("ordblks %8d %8d %8d\n", mmbefore->ordblks , mmafter->ordblks, + mmafter->ordblks - mmbefore->ordblks); + printf("mxordblk %8x %8x %8x\n", mmbefore->mxordblk, mmafter->mxordblk, + mmafter->mxordblk - mmbefore->mxordblk); + printf("uordblks %8x %8x %8x\n", mmbefore->uordblks, mmafter->uordblks, + mmafter->uordblks - mmbefore->uordblks); + printf("fordblks %8x %8x %8x\n", mmbefore->fordblks, mmafter->fordblks, + mmafter->fordblks - mmbefore->fordblks); +} + +/**************************************************************************** + * Name: mtdnvs_endmemusage + ****************************************************************************/ + +static void mtdnvs_endmemusage(FAR struct mtdnvs_ctx_s *ctx) +{ + ctx->mmafter = mallinfo(); + + printf("\nFinal memory usage:\n"); + mtdnvs_showmemusage(&ctx->mmbefore, &ctx->mmafter); +} + +/**************************************************************************** + * Name: show_useage + ****************************************************************************/ + +static void show_useage(void) +{ + printf("Usage : mtdnvs_test [OPTION [ARG]] ...\n"); + printf("-h show this help statement\n"); + printf("-m mount point to be tested e.g. [%s]\n", + CONFIG_TESTING_FAILSAFE_MTD_CONFIG_MOUNTPT_NAME); +} + +/**************************************************************************** + * Name: teardown + ****************************************************************************/ + +static int teardown(void) +{ + int fd; + int ret; + + fd = open("/dev/config", 0); + if (fd < 0) + { + printf("%s:open failed, ret=%d\n", __func__, fd); + return -ENOENT; + } + + ret = ioctl(fd, MTDIOC_BULKERASE, NULL); + if (ret < 0) + { + printf("%s:clear failed, ret=%d\n", __func__, ret); + return -EACCES; + } + + close(fd); + + ret = mtdconfig_unregister(); + if (ret < 0) + { + printf("%s:mtdnvs_unregister failed, ret=%d\n", __func__, ret); + return ret; + } + + return ret; +} + +extern int find_mtddriver(FAR const char *pathname, + FAR struct inode **ppinode); + +/**************************************************************************** + * Name: setup + ****************************************************************************/ + +static int setup(struct mtdnvs_ctx_s *ctx) +{ + int ret; + FAR struct inode *sys_node; + + ret = find_mtddriver(CONFIG_TESTING_FAILSAFE_MTD_CONFIG_MOUNTPT_NAME, + &sys_node); + if (ret < 0) + { + printf("ERROR: open %s failed: %d\n", + CONFIG_TESTING_FAILSAFE_MTD_CONFIG_MOUNTPT_NAME, ret); + return -ENOENT; Review Comment: ```suggestion return ret; ``` ########## testing/mtd_config_fs/mtd_config_fs_test_main.c: ########## @@ -0,0 +1,2040 @@ +/**************************************************************************** + * apps/testing/mtd_config_fs/mtd_config_fs_test_main.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/mtd/mtd.h> +#include <nuttx/mtd/configdata.h> + +#include <sys/mount.h> +#include <sys/ioctl.h> +#include <sys/statfs.h> + +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <malloc.h> +#include <unistd.h> +#include <fcntl.h> +#include <dirent.h> +#include <string.h> +#include <errno.h> +#include <nuttx/crc8.h> +#include <debug.h> +#include <assert.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Configuration ************************************************************/ + +#define TEST_KEY1 "testkey1" +#define TEST_KEY2 "testkey2" +#define TEST_DATA1 "abc" +#define TEST_DATA2 "def" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Allocation Table Entry */ + +begin_packed_struct struct nvs_ate +{ + uint32_t id; /* data id */ + uint16_t offset; /* data offset within sector */ + uint16_t len; /* data len within sector */ + uint16_t key_len; /* key string len */ + uint8_t part; /* part of a multipart data - future extension */ + uint8_t crc8; /* crc8 check of the ate entry */ + uint8_t expired; /* 0xFF-newest entry, others-old entry */ + uint8_t reserved; /* for future extension */ + uint16_t reserved2; /* for future extension */ +} end_packed_struct; + +/* Pre-allocated simulated flash */ + +struct mtdnvs_ctx_s +{ + char mountdir[CONFIG_TESTING_FAILSAFE_MTD_CONFIG_MOUNTPT_MAXNAME]; + struct mallinfo mmbefore; + struct mallinfo mmprevious; + struct mallinfo mmafter; +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: fnv_hash_32 + ****************************************************************************/ + +static uint32_t fnv_hash_32(FAR const uint8_t *input, uint32_t len) +{ + uint32_t i = 0; + uint32_t hval = 2166136261; + + /* FNV-1 hash each octet in the buffer */ + + while (i++ < len) + { + /* multiply by the 32 bit FNV magic prime mod 2^32 */ + + hval *= 0x01000193; + + /* xor the bottom with the current octet */ + + hval ^= (uint32_t)*input++; + } + + return hval; +} + +/**************************************************************************** + * Name: fill_crc8_update + ****************************************************************************/ + +static void fill_crc8_update(FAR struct nvs_ate *entry) +{ + uint8_t ate_crc; + + ate_crc = crc8part((FAR const uint8_t *)entry, + offsetof(struct nvs_ate, crc8), 0xff); + entry->crc8 = ate_crc; +} + +/**************************************************************************** + * Name: fill_gc_done_ate + ****************************************************************************/ + +static void fill_corrupted_close_ate(FAR struct nvs_ate *close_ate) +{ + memset((FAR void *)close_ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + close_ate->id = 0xffffffff; + close_ate->len = 0U; +} + +/**************************************************************************** + * Name: fill_close_ate + ****************************************************************************/ + +static void fill_close_ate(FAR struct nvs_ate *close_ate, int offset) +{ + memset((FAR void *)close_ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + close_ate->id = 0xffffffff; + close_ate->len = 0U; + close_ate->offset = offset; + fill_crc8_update(close_ate); +} + +/**************************************************************************** + * Name: fill_gc_done_ate + ****************************************************************************/ + +static void fill_gc_done_ate(FAR struct nvs_ate *gc_done_ate) +{ + memset((FAR void *)gc_done_ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + gc_done_ate->id = 0xffffffff; + gc_done_ate->len = 0U; + fill_crc8_update(gc_done_ate); +} + +/**************************************************************************** + * Name: fill_ate + ****************************************************************************/ + +static void fill_ate(FAR struct nvs_ate *ate, FAR const char *key, + uint16_t len, uint16_t offset, bool expired) +{ + memset((FAR void *)ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + ate->id = fnv_hash_32((FAR const uint8_t *)key, strlen(key) + 1) + % 0xfffffffd + 1; + ate->len = len; + ate->offset = offset; + ate->key_len = strlen(key) + 1; + fill_crc8_update(ate); + ate->expired = expired ? 0x7f : 0xff; +} + +/**************************************************************************** + * Name: fill_corrupted_ate + ****************************************************************************/ + +static void fill_corrupted_ate(FAR struct nvs_ate *ate, + FAR const char *key, uint16_t len, uint16_t offset) +{ + memset((FAR void *)ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + ate->id = fnv_hash_32((FAR const uint8_t *)key, strlen(key) + 1) + % 0xfffffffd + 1; + ate->len = len; + ate->offset = offset; + ate->key_len = strlen(key) + 1; +} + +/**************************************************************************** + * Name: mtdnvs_showmemusage + ****************************************************************************/ + +static void mtdnvs_showmemusage(struct mallinfo *mmbefore, + struct mallinfo *mmafter) +{ + printf("VARIABLE BEFORE AFTER DELTA\n"); + printf("======== ======== ======== ========\n"); + printf("arena %8x %8x %8x\n", mmbefore->arena , mmafter->arena, + mmafter->arena - mmbefore->arena); + printf("ordblks %8d %8d %8d\n", mmbefore->ordblks , mmafter->ordblks, + mmafter->ordblks - mmbefore->ordblks); + printf("mxordblk %8x %8x %8x\n", mmbefore->mxordblk, mmafter->mxordblk, + mmafter->mxordblk - mmbefore->mxordblk); + printf("uordblks %8x %8x %8x\n", mmbefore->uordblks, mmafter->uordblks, + mmafter->uordblks - mmbefore->uordblks); + printf("fordblks %8x %8x %8x\n", mmbefore->fordblks, mmafter->fordblks, + mmafter->fordblks - mmbefore->fordblks); +} + +/**************************************************************************** + * Name: mtdnvs_endmemusage + ****************************************************************************/ + +static void mtdnvs_endmemusage(FAR struct mtdnvs_ctx_s *ctx) +{ + ctx->mmafter = mallinfo(); + + printf("\nFinal memory usage:\n"); + mtdnvs_showmemusage(&ctx->mmbefore, &ctx->mmafter); +} + +/**************************************************************************** + * Name: show_useage + ****************************************************************************/ + +static void show_useage(void) +{ + printf("Usage : mtdnvs_test [OPTION [ARG]] ...\n"); + printf("-h show this help statement\n"); + printf("-m mount point to be tested e.g. [%s]\n", + CONFIG_TESTING_FAILSAFE_MTD_CONFIG_MOUNTPT_NAME); +} + +/**************************************************************************** + * Name: teardown + ****************************************************************************/ + +static int teardown(void) +{ + int fd; + int ret; + + fd = open("/dev/config", 0); + if (fd < 0) + { + printf("%s:open failed, ret=%d\n", __func__, fd); + return -ENOENT; Review Comment: ```suggestion return -errno; ``` ########## testing/mtd_config_fs/Kconfig: ########## @@ -0,0 +1,49 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config TESTING_FAILSAFE_MTD_CONFIG + tristate "MTD Config fail-safe storage test" + default n Review Comment: depends on MTD_CONFIG_FAIL_SAFE ########## testing/mtd_config_fs/mtd_config_fs_test_main.c: ########## @@ -0,0 +1,2040 @@ +/**************************************************************************** + * apps/testing/mtd_config_fs/mtd_config_fs_test_main.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/mtd/mtd.h> +#include <nuttx/mtd/configdata.h> + +#include <sys/mount.h> +#include <sys/ioctl.h> +#include <sys/statfs.h> + +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <malloc.h> +#include <unistd.h> +#include <fcntl.h> +#include <dirent.h> +#include <string.h> +#include <errno.h> +#include <nuttx/crc8.h> +#include <debug.h> +#include <assert.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Configuration ************************************************************/ + +#define TEST_KEY1 "testkey1" +#define TEST_KEY2 "testkey2" +#define TEST_DATA1 "abc" +#define TEST_DATA2 "def" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Allocation Table Entry */ + +begin_packed_struct struct nvs_ate +{ + uint32_t id; /* data id */ + uint16_t offset; /* data offset within sector */ + uint16_t len; /* data len within sector */ + uint16_t key_len; /* key string len */ + uint8_t part; /* part of a multipart data - future extension */ + uint8_t crc8; /* crc8 check of the ate entry */ + uint8_t expired; /* 0xFF-newest entry, others-old entry */ + uint8_t reserved; /* for future extension */ + uint16_t reserved2; /* for future extension */ +} end_packed_struct; + +/* Pre-allocated simulated flash */ + +struct mtdnvs_ctx_s +{ + char mountdir[CONFIG_TESTING_FAILSAFE_MTD_CONFIG_MOUNTPT_MAXNAME]; + struct mallinfo mmbefore; + struct mallinfo mmprevious; + struct mallinfo mmafter; +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: fnv_hash_32 + ****************************************************************************/ + +static uint32_t fnv_hash_32(FAR const uint8_t *input, uint32_t len) +{ + uint32_t i = 0; + uint32_t hval = 2166136261; + + /* FNV-1 hash each octet in the buffer */ + + while (i++ < len) + { + /* multiply by the 32 bit FNV magic prime mod 2^32 */ + + hval *= 0x01000193; + + /* xor the bottom with the current octet */ + + hval ^= (uint32_t)*input++; + } + + return hval; +} + +/**************************************************************************** + * Name: fill_crc8_update + ****************************************************************************/ + +static void fill_crc8_update(FAR struct nvs_ate *entry) +{ + uint8_t ate_crc; + + ate_crc = crc8part((FAR const uint8_t *)entry, + offsetof(struct nvs_ate, crc8), 0xff); + entry->crc8 = ate_crc; +} + +/**************************************************************************** + * Name: fill_gc_done_ate + ****************************************************************************/ + +static void fill_corrupted_close_ate(FAR struct nvs_ate *close_ate) +{ + memset((FAR void *)close_ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + close_ate->id = 0xffffffff; + close_ate->len = 0U; +} + +/**************************************************************************** + * Name: fill_close_ate + ****************************************************************************/ + +static void fill_close_ate(FAR struct nvs_ate *close_ate, int offset) +{ + memset((FAR void *)close_ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + close_ate->id = 0xffffffff; + close_ate->len = 0U; + close_ate->offset = offset; + fill_crc8_update(close_ate); +} + +/**************************************************************************** + * Name: fill_gc_done_ate + ****************************************************************************/ + +static void fill_gc_done_ate(FAR struct nvs_ate *gc_done_ate) +{ + memset((FAR void *)gc_done_ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + gc_done_ate->id = 0xffffffff; + gc_done_ate->len = 0U; + fill_crc8_update(gc_done_ate); +} + +/**************************************************************************** + * Name: fill_ate + ****************************************************************************/ + +static void fill_ate(FAR struct nvs_ate *ate, FAR const char *key, + uint16_t len, uint16_t offset, bool expired) +{ + memset((FAR void *)ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + ate->id = fnv_hash_32((FAR const uint8_t *)key, strlen(key) + 1) + % 0xfffffffd + 1; + ate->len = len; + ate->offset = offset; + ate->key_len = strlen(key) + 1; + fill_crc8_update(ate); + ate->expired = expired ? 0x7f : 0xff; +} + +/**************************************************************************** + * Name: fill_corrupted_ate + ****************************************************************************/ + +static void fill_corrupted_ate(FAR struct nvs_ate *ate, + FAR const char *key, uint16_t len, uint16_t offset) +{ + memset((FAR void *)ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + ate->id = fnv_hash_32((FAR const uint8_t *)key, strlen(key) + 1) + % 0xfffffffd + 1; + ate->len = len; + ate->offset = offset; + ate->key_len = strlen(key) + 1; +} + +/**************************************************************************** + * Name: mtdnvs_showmemusage + ****************************************************************************/ + +static void mtdnvs_showmemusage(struct mallinfo *mmbefore, + struct mallinfo *mmafter) +{ + printf("VARIABLE BEFORE AFTER DELTA\n"); + printf("======== ======== ======== ========\n"); + printf("arena %8x %8x %8x\n", mmbefore->arena , mmafter->arena, + mmafter->arena - mmbefore->arena); + printf("ordblks %8d %8d %8d\n", mmbefore->ordblks , mmafter->ordblks, + mmafter->ordblks - mmbefore->ordblks); + printf("mxordblk %8x %8x %8x\n", mmbefore->mxordblk, mmafter->mxordblk, + mmafter->mxordblk - mmbefore->mxordblk); + printf("uordblks %8x %8x %8x\n", mmbefore->uordblks, mmafter->uordblks, + mmafter->uordblks - mmbefore->uordblks); + printf("fordblks %8x %8x %8x\n", mmbefore->fordblks, mmafter->fordblks, + mmafter->fordblks - mmbefore->fordblks); +} + +/**************************************************************************** + * Name: mtdnvs_endmemusage + ****************************************************************************/ + +static void mtdnvs_endmemusage(FAR struct mtdnvs_ctx_s *ctx) +{ + ctx->mmafter = mallinfo(); + + printf("\nFinal memory usage:\n"); + mtdnvs_showmemusage(&ctx->mmbefore, &ctx->mmafter); +} + +/**************************************************************************** + * Name: show_useage + ****************************************************************************/ + +static void show_useage(void) +{ + printf("Usage : mtdnvs_test [OPTION [ARG]] ...\n"); Review Comment: ```suggestion printf("Usage : mtdconfig_fs_test [OPTION [ARG]] ...\n"); ``` ########## testing/mtd_config_fs/mtd_config_fs_test_main.c: ########## @@ -0,0 +1,2040 @@ +/**************************************************************************** + * apps/testing/mtd_config_fs/mtd_config_fs_test_main.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/mtd/mtd.h> +#include <nuttx/mtd/configdata.h> + +#include <sys/mount.h> +#include <sys/ioctl.h> +#include <sys/statfs.h> + +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <malloc.h> +#include <unistd.h> +#include <fcntl.h> +#include <dirent.h> +#include <string.h> +#include <errno.h> +#include <nuttx/crc8.h> +#include <debug.h> +#include <assert.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Configuration ************************************************************/ + +#define TEST_KEY1 "testkey1" +#define TEST_KEY2 "testkey2" +#define TEST_DATA1 "abc" +#define TEST_DATA2 "def" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Allocation Table Entry */ + +begin_packed_struct struct nvs_ate +{ + uint32_t id; /* data id */ + uint16_t offset; /* data offset within sector */ + uint16_t len; /* data len within sector */ + uint16_t key_len; /* key string len */ + uint8_t part; /* part of a multipart data - future extension */ + uint8_t crc8; /* crc8 check of the ate entry */ + uint8_t expired; /* 0xFF-newest entry, others-old entry */ + uint8_t reserved; /* for future extension */ + uint16_t reserved2; /* for future extension */ +} end_packed_struct; + +/* Pre-allocated simulated flash */ + +struct mtdnvs_ctx_s +{ + char mountdir[CONFIG_TESTING_FAILSAFE_MTD_CONFIG_MOUNTPT_MAXNAME]; + struct mallinfo mmbefore; + struct mallinfo mmprevious; + struct mallinfo mmafter; +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: fnv_hash_32 + ****************************************************************************/ + +static uint32_t fnv_hash_32(FAR const uint8_t *input, uint32_t len) +{ + uint32_t i = 0; + uint32_t hval = 2166136261; + + /* FNV-1 hash each octet in the buffer */ + + while (i++ < len) + { + /* multiply by the 32 bit FNV magic prime mod 2^32 */ + + hval *= 0x01000193; + + /* xor the bottom with the current octet */ + + hval ^= (uint32_t)*input++; + } + + return hval; +} + +/**************************************************************************** + * Name: fill_crc8_update + ****************************************************************************/ + +static void fill_crc8_update(FAR struct nvs_ate *entry) +{ + uint8_t ate_crc; + + ate_crc = crc8part((FAR const uint8_t *)entry, + offsetof(struct nvs_ate, crc8), 0xff); + entry->crc8 = ate_crc; +} + +/**************************************************************************** + * Name: fill_gc_done_ate + ****************************************************************************/ + +static void fill_corrupted_close_ate(FAR struct nvs_ate *close_ate) +{ + memset((FAR void *)close_ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + close_ate->id = 0xffffffff; + close_ate->len = 0U; +} + +/**************************************************************************** + * Name: fill_close_ate + ****************************************************************************/ + +static void fill_close_ate(FAR struct nvs_ate *close_ate, int offset) +{ + memset((FAR void *)close_ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + close_ate->id = 0xffffffff; + close_ate->len = 0U; + close_ate->offset = offset; + fill_crc8_update(close_ate); +} + +/**************************************************************************** + * Name: fill_gc_done_ate + ****************************************************************************/ + +static void fill_gc_done_ate(FAR struct nvs_ate *gc_done_ate) +{ + memset((FAR void *)gc_done_ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + gc_done_ate->id = 0xffffffff; + gc_done_ate->len = 0U; + fill_crc8_update(gc_done_ate); +} + +/**************************************************************************** + * Name: fill_ate + ****************************************************************************/ + +static void fill_ate(FAR struct nvs_ate *ate, FAR const char *key, + uint16_t len, uint16_t offset, bool expired) +{ + memset((FAR void *)ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + ate->id = fnv_hash_32((FAR const uint8_t *)key, strlen(key) + 1) + % 0xfffffffd + 1; + ate->len = len; + ate->offset = offset; + ate->key_len = strlen(key) + 1; + fill_crc8_update(ate); + ate->expired = expired ? 0x7f : 0xff; +} + +/**************************************************************************** + * Name: fill_corrupted_ate + ****************************************************************************/ + +static void fill_corrupted_ate(FAR struct nvs_ate *ate, + FAR const char *key, uint16_t len, uint16_t offset) +{ + memset((FAR void *)ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + ate->id = fnv_hash_32((FAR const uint8_t *)key, strlen(key) + 1) + % 0xfffffffd + 1; + ate->len = len; + ate->offset = offset; + ate->key_len = strlen(key) + 1; +} + +/**************************************************************************** + * Name: mtdnvs_showmemusage + ****************************************************************************/ + +static void mtdnvs_showmemusage(struct mallinfo *mmbefore, + struct mallinfo *mmafter) +{ + printf("VARIABLE BEFORE AFTER DELTA\n"); + printf("======== ======== ======== ========\n"); + printf("arena %8x %8x %8x\n", mmbefore->arena , mmafter->arena, + mmafter->arena - mmbefore->arena); + printf("ordblks %8d %8d %8d\n", mmbefore->ordblks , mmafter->ordblks, + mmafter->ordblks - mmbefore->ordblks); + printf("mxordblk %8x %8x %8x\n", mmbefore->mxordblk, mmafter->mxordblk, + mmafter->mxordblk - mmbefore->mxordblk); + printf("uordblks %8x %8x %8x\n", mmbefore->uordblks, mmafter->uordblks, + mmafter->uordblks - mmbefore->uordblks); + printf("fordblks %8x %8x %8x\n", mmbefore->fordblks, mmafter->fordblks, + mmafter->fordblks - mmbefore->fordblks); +} + +/**************************************************************************** + * Name: mtdnvs_endmemusage + ****************************************************************************/ + +static void mtdnvs_endmemusage(FAR struct mtdnvs_ctx_s *ctx) +{ + ctx->mmafter = mallinfo(); + + printf("\nFinal memory usage:\n"); + mtdnvs_showmemusage(&ctx->mmbefore, &ctx->mmafter); +} + +/**************************************************************************** + * Name: show_useage + ****************************************************************************/ + +static void show_useage(void) +{ + printf("Usage : mtdnvs_test [OPTION [ARG]] ...\n"); + printf("-h show this help statement\n"); + printf("-m mount point to be tested e.g. [%s]\n", + CONFIG_TESTING_FAILSAFE_MTD_CONFIG_MOUNTPT_NAME); +} + +/**************************************************************************** + * Name: teardown + ****************************************************************************/ + +static int teardown(void) +{ + int fd; + int ret; + + fd = open("/dev/config", 0); + if (fd < 0) + { + printf("%s:open failed, ret=%d\n", __func__, fd); + return -ENOENT; + } + + ret = ioctl(fd, MTDIOC_BULKERASE, NULL); + if (ret < 0) + { + printf("%s:clear failed, ret=%d\n", __func__, ret); + return -EACCES; Review Comment: ```suggestion return -errno; ``` ########## testing/mtd_config_fs/mtd_config_fs_test_main.c: ########## @@ -0,0 +1,2040 @@ +/**************************************************************************** + * apps/testing/mtd_config_fs/mtd_config_fs_test_main.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/mtd/mtd.h> +#include <nuttx/mtd/configdata.h> + +#include <sys/mount.h> +#include <sys/ioctl.h> +#include <sys/statfs.h> + +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <malloc.h> +#include <unistd.h> +#include <fcntl.h> +#include <dirent.h> +#include <string.h> +#include <errno.h> +#include <nuttx/crc8.h> +#include <debug.h> +#include <assert.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Configuration ************************************************************/ + +#define TEST_KEY1 "testkey1" +#define TEST_KEY2 "testkey2" +#define TEST_DATA1 "abc" +#define TEST_DATA2 "def" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Allocation Table Entry */ + +begin_packed_struct struct nvs_ate +{ + uint32_t id; /* data id */ + uint16_t offset; /* data offset within sector */ + uint16_t len; /* data len within sector */ + uint16_t key_len; /* key string len */ + uint8_t part; /* part of a multipart data - future extension */ + uint8_t crc8; /* crc8 check of the ate entry */ + uint8_t expired; /* 0xFF-newest entry, others-old entry */ + uint8_t reserved; /* for future extension */ + uint16_t reserved2; /* for future extension */ +} end_packed_struct; + +/* Pre-allocated simulated flash */ + +struct mtdnvs_ctx_s +{ + char mountdir[CONFIG_TESTING_FAILSAFE_MTD_CONFIG_MOUNTPT_MAXNAME]; + struct mallinfo mmbefore; + struct mallinfo mmprevious; + struct mallinfo mmafter; +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: fnv_hash_32 + ****************************************************************************/ + +static uint32_t fnv_hash_32(FAR const uint8_t *input, uint32_t len) +{ + uint32_t i = 0; + uint32_t hval = 2166136261; + + /* FNV-1 hash each octet in the buffer */ + + while (i++ < len) + { + /* multiply by the 32 bit FNV magic prime mod 2^32 */ + + hval *= 0x01000193; + + /* xor the bottom with the current octet */ + + hval ^= (uint32_t)*input++; + } + + return hval; +} + +/**************************************************************************** + * Name: fill_crc8_update + ****************************************************************************/ + +static void fill_crc8_update(FAR struct nvs_ate *entry) +{ + uint8_t ate_crc; + + ate_crc = crc8part((FAR const uint8_t *)entry, + offsetof(struct nvs_ate, crc8), 0xff); + entry->crc8 = ate_crc; +} + +/**************************************************************************** + * Name: fill_gc_done_ate + ****************************************************************************/ + +static void fill_corrupted_close_ate(FAR struct nvs_ate *close_ate) +{ + memset((FAR void *)close_ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + close_ate->id = 0xffffffff; + close_ate->len = 0U; +} + +/**************************************************************************** + * Name: fill_close_ate + ****************************************************************************/ + +static void fill_close_ate(FAR struct nvs_ate *close_ate, int offset) +{ + memset((FAR void *)close_ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + close_ate->id = 0xffffffff; + close_ate->len = 0U; + close_ate->offset = offset; + fill_crc8_update(close_ate); +} + +/**************************************************************************** + * Name: fill_gc_done_ate + ****************************************************************************/ + +static void fill_gc_done_ate(FAR struct nvs_ate *gc_done_ate) +{ + memset((FAR void *)gc_done_ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + gc_done_ate->id = 0xffffffff; + gc_done_ate->len = 0U; + fill_crc8_update(gc_done_ate); +} + +/**************************************************************************** + * Name: fill_ate + ****************************************************************************/ + +static void fill_ate(FAR struct nvs_ate *ate, FAR const char *key, + uint16_t len, uint16_t offset, bool expired) +{ + memset((FAR void *)ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + ate->id = fnv_hash_32((FAR const uint8_t *)key, strlen(key) + 1) + % 0xfffffffd + 1; + ate->len = len; + ate->offset = offset; + ate->key_len = strlen(key) + 1; + fill_crc8_update(ate); + ate->expired = expired ? 0x7f : 0xff; +} + +/**************************************************************************** + * Name: fill_corrupted_ate + ****************************************************************************/ + +static void fill_corrupted_ate(FAR struct nvs_ate *ate, + FAR const char *key, uint16_t len, uint16_t offset) +{ + memset((FAR void *)ate, CONFIG_MTD_CONFIG_ERASEDVALUE, + sizeof(struct nvs_ate)); + ate->id = fnv_hash_32((FAR const uint8_t *)key, strlen(key) + 1) + % 0xfffffffd + 1; + ate->len = len; + ate->offset = offset; + ate->key_len = strlen(key) + 1; +} + +/**************************************************************************** + * Name: mtdnvs_showmemusage + ****************************************************************************/ + +static void mtdnvs_showmemusage(struct mallinfo *mmbefore, + struct mallinfo *mmafter) +{ + printf("VARIABLE BEFORE AFTER DELTA\n"); + printf("======== ======== ======== ========\n"); + printf("arena %8x %8x %8x\n", mmbefore->arena , mmafter->arena, + mmafter->arena - mmbefore->arena); + printf("ordblks %8d %8d %8d\n", mmbefore->ordblks , mmafter->ordblks, + mmafter->ordblks - mmbefore->ordblks); + printf("mxordblk %8x %8x %8x\n", mmbefore->mxordblk, mmafter->mxordblk, + mmafter->mxordblk - mmbefore->mxordblk); + printf("uordblks %8x %8x %8x\n", mmbefore->uordblks, mmafter->uordblks, + mmafter->uordblks - mmbefore->uordblks); + printf("fordblks %8x %8x %8x\n", mmbefore->fordblks, mmafter->fordblks, + mmafter->fordblks - mmbefore->fordblks); +} + +/**************************************************************************** + * Name: mtdnvs_endmemusage + ****************************************************************************/ + +static void mtdnvs_endmemusage(FAR struct mtdnvs_ctx_s *ctx) +{ + ctx->mmafter = mallinfo(); + + printf("\nFinal memory usage:\n"); + mtdnvs_showmemusage(&ctx->mmbefore, &ctx->mmafter); +} + +/**************************************************************************** + * Name: show_useage + ****************************************************************************/ + +static void show_useage(void) +{ + printf("Usage : mtdnvs_test [OPTION [ARG]] ...\n"); + printf("-h show this help statement\n"); + printf("-m mount point to be tested e.g. [%s]\n", + CONFIG_TESTING_FAILSAFE_MTD_CONFIG_MOUNTPT_NAME); +} + +/**************************************************************************** + * Name: teardown + ****************************************************************************/ + +static int teardown(void) +{ + int fd; + int ret; + + fd = open("/dev/config", 0); + if (fd < 0) + { + printf("%s:open failed, ret=%d\n", __func__, fd); + return -ENOENT; + } + + ret = ioctl(fd, MTDIOC_BULKERASE, NULL); + if (ret < 0) + { + printf("%s:clear failed, ret=%d\n", __func__, ret); + return -EACCES; + } + + close(fd); + + ret = mtdconfig_unregister(); + if (ret < 0) + { + printf("%s:mtdnvs_unregister failed, ret=%d\n", __func__, ret); Review Comment: ```suggestion printf("%s:mtdconfig_unregister failed, ret=%d\n", __func__, ret); ``` ########## testing/mtd_config_fs/mtd_config_fs_test_main.c: ########## @@ -0,0 +1,2040 @@ +/**************************************************************************** + * apps/testing/mtd_config_fs/mtd_config_fs_test_main.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/mtd/mtd.h> Review Comment: don't need include mtd.h? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org