From ef246c0869e501538ffb7495c6a5dd0f206e3652 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Wed, 7 Sep 2022 13:33:40 +0800 Subject: [PATCH] mtd/config: Remove MTD_CONFIG_ERASEDVALUE since we can query this value by MTDIOC_ERASESTATE after: commit 30cb497fe125de6ae2e52a72758180437ec65974 Author: Gustavo Henrique Nihei Date: Tue Jul 13 19:33:12 2021 -0300 mtd: Add MTDIOC_ERASESTATE command for retrieving erase state value Signed-off-by: Xiang Xiao --- .../mikroe-stm32f4/configs/fulldemo/defconfig | 1 - drivers/mtd/Kconfig | 8 --- drivers/mtd/mtd_config.c | 72 +++++++++++-------- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/boards/arm/stm32/mikroe-stm32f4/configs/fulldemo/defconfig b/boards/arm/stm32/mikroe-stm32f4/configs/fulldemo/defconfig index 17193a6f1a..fb657574ad 100644 --- a/boards/arm/stm32/mikroe-stm32f4/configs/fulldemo/defconfig +++ b/boards/arm/stm32/mikroe-stm32f4/configs/fulldemo/defconfig @@ -64,7 +64,6 @@ CONFIG_MMCSD_SPICLOCK=30000000 CONFIG_MM_REGIONS=2 CONFIG_MQ_MAXMSGSIZE=64 CONFIG_MTD_CONFIG=y -CONFIG_MTD_CONFIG_ERASEDVALUE=0xFF CONFIG_MTD_PARTITION=y CONFIG_MTD_SMART_SECTOR_SIZE=512 CONFIG_NSH_ARCHINIT=y diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig index 3b2ddb4016..7db54f6644 100644 --- a/drivers/mtd/Kconfig +++ b/drivers/mtd/Kconfig @@ -171,14 +171,6 @@ config MTD_CONFIG_RAM_CONSOLIDATE Another benefit of this option is it reduces code space a bit since the "reserved block" consolidate routine is not needed. -config MTD_CONFIG_ERASEDVALUE - hex "Erased value of bytes on the MTD device" - default 0xff - ---help--- - Specifies the value of the erased state of the MTD FLASH. For - most FLASH parts, this is 0xff, but could also be zero depending - on the device. - config MTD_CONFIG_NAMED bool "Use item NAMES instead of ID/enumeration in Dev Config device" default n diff --git a/drivers/mtd/mtd_config.c b/drivers/mtd/mtd_config.c index 6cf2218b1c..35f8ddfd36 100644 --- a/drivers/mtd/mtd_config.c +++ b/drivers/mtd/mtd_config.c @@ -57,15 +57,13 @@ #ifdef CONFIG_MTD_CONFIG_NAMED # define CONFIGDATA_FORMAT_VERSION 1 +# define MTD_ERASED_ID(dev) ((dev)->erasestate) #else # define CONFIGDATA_FORMAT_VERSION 2 +# define MTD_ERASED_ID(dev) (((dev)->erasestate << 8) | (dev)->erasestate) #endif #define CONFIGDATA_BLOCK_HDR_SIZE 3 - -#define MTD_ERASED_ID ((CONFIG_MTD_CONFIG_ERASEDVALUE << 8) | \ - CONFIG_MTD_CONFIG_ERASEDVALUE) - -#define MTD_ERASED_FLAGS CONFIG_MTD_CONFIG_ERASEDVALUE +#define MTD_ERASED_FLAGS(dev) ((dev)->erasestate) /**************************************************************************** * Private Types @@ -79,6 +77,7 @@ struct mtdconfig_struct_s uint32_t erasesize; /* Size of erase block in contained MTD */ size_t nblocks; /* Number of blocks available */ size_t neraseblocks; /* Number of erase blocks available */ + uint8_t erasestate; /* Erased value */ off_t readoff; /* Read offset (for hexdump) */ FAR uint8_t *buffer; /* Temp block read buffer */ }; @@ -375,7 +374,7 @@ static int mtdconfig_findfirstentry(FAR struct mtdconfig_struct_s *dev, /* Test if this header has been released */ - if (phdr->flags != MTD_ERASED_FLAGS) + if (phdr->flags != MTD_ERASED_FLAGS(dev)) { /* This entry has been released. Advance to next entry */ @@ -488,12 +487,12 @@ read_next: /* Test if this header has is still active */ - if (phdr->flags == MTD_ERASED_FLAGS) + if (phdr->flags == MTD_ERASED_FLAGS(dev)) { #ifdef CONFIG_MTD_CONFIG_NAMED - if (phdr->name[0] == CONFIG_MTD_CONFIG_ERASEDVALUE) + if (phdr->name[0] == MTD_ERASED_ID(dev)) #else - if (phdr->id == MTD_ERASED_ID) + if (phdr->id == MTD_ERASED_ID(dev)) #endif { /* If we are searching for free space, then check @@ -630,9 +629,9 @@ static off_t mtdconfig_ramconsolidate(FAR struct mtdconfig_struct_s *dev) { phdr = (FAR struct mtdconfig_header_s *) &pbuf[src_offset]; #ifdef CONFIG_MTD_CONFIG_NAMED - if (phdr->name[0] == CONFIG_MTD_CONFIG_ERASEDVALUE) + if (phdr->name[0] == MTD_ERASED_ID(dev)) #else - if (phdr->id == MTD_ERASED_ID) + if (phdr->id == MTD_ERASED_ID(dev)) #endif { /* No more data in this erase block. */ @@ -641,7 +640,7 @@ static off_t mtdconfig_ramconsolidate(FAR struct mtdconfig_struct_s *dev) continue; } - if (phdr->flags == MTD_ERASED_FLAGS) + if (phdr->flags == MTD_ERASED_FLAGS(dev)) { /* This is an active entry. Copy it. Check if it * fits in the current destination block. @@ -838,16 +837,16 @@ retry_relocate: goto errout; } - if (hdr.flags == MTD_ERASED_FLAGS) + if (hdr.flags == MTD_ERASED_FLAGS(dev)) { /* Test if the source entry is active or if we are at the end * of data for this erase block. */ #ifdef CONFIG_MTD_CONFIG_NAMED - if (hdr.name[0] == CONFIG_MTD_CONFIG_ERASEDVALUE) + if (hdr.name[0] == MTD_ERASED_ID(dev)) #else - if (hdr.id == MTD_ERASED_ID) + if (hdr.id == MTD_ERASED_ID(dev)) #endif { /* No more data in this erase block. Advance to the @@ -1103,9 +1102,9 @@ static int mtdconfig_findentry(FAR struct mtdconfig_struct_s *dev, #endif { #ifdef CONFIG_MTD_CONFIG_NAMED - if (phdr->name[0] == CONFIG_MTD_CONFIG_ERASEDVALUE) + if (phdr->name[0] == MTD_ERASED_ID(dev)) #else - if (phdr->id == MTD_ERASED_ID) + if (phdr->id == MTD_ERASED_ID(dev)) #endif { /* Advance to the next block and continue the search */ @@ -1132,7 +1131,7 @@ static int mtdconfig_findentry(FAR struct mtdconfig_struct_s *dev, break; } - if (phdr->flags == MTD_ERASED_FLAGS) + if (phdr->flags == MTD_ERASED_FLAGS(dev)) { continue; } @@ -1232,7 +1231,7 @@ retry: { /* Mark this entry as released */ - hdr.flags = (uint8_t)~MTD_ERASED_FLAGS; + hdr.flags = (uint8_t)~MTD_ERASED_FLAGS(dev); mtdconfig_writebytes(dev, offset, &hdr.flags, sizeof(hdr.flags)); } @@ -1254,9 +1253,9 @@ retry_find: offset = mtdconfig_findfirstentry(dev, &hdr); #ifdef CONFIG_MTD_CONFIG_NAMED - if (offset > 0 && hdr.name[0] == CONFIG_MTD_CONFIG_ERASEDVALUE) + if (offset > 0 && hdr.name[0] == MTD_ERASED_ID(dev)) #else - if (offset > 0 && hdr.id == MTD_ERASED_ID) + if (offset > 0 && hdr.id == MTD_ERASED_ID(dev)) #endif { block = offset / dev->erasesize; @@ -1276,9 +1275,9 @@ retry_find: } #ifdef CONFIG_MTD_CONFIG_NAMED - if (hdr.name[0] != CONFIG_MTD_CONFIG_ERASEDVALUE) + if (hdr.name[0] != MTD_ERASED_ID(dev)) #else - if (hdr.id != MTD_ERASED_ID) + if (hdr.id != MTD_ERASED_ID(dev)) #endif { /* Read the next entry */ @@ -1343,7 +1342,7 @@ retry_find: hdr.instance = pdata->instance; #endif hdr.len = pdata->len; - hdr.flags = MTD_ERASED_FLAGS; + hdr.flags = MTD_ERASED_FLAGS(dev); ret = mtdconfig_writebytes(dev, offset, (FAR uint8_t *)&hdr, sizeof(hdr)); @@ -1361,7 +1360,7 @@ retry_find: { /* Error writing data! */ - hdr.flags = MTD_ERASED_FLAGS; + hdr.flags = MTD_ERASED_FLAGS(dev); mtdconfig_writebytes(dev, offset, (FAR uint8_t *)&hdr, sizeof(hdr.flags)); ret = -EIO; @@ -1484,7 +1483,7 @@ static int mtdconfig_deleteconfig(FAR struct mtdconfig_struct_s *dev, { /* Entry found. Mark this entry as released */ - hdr.flags = (uint8_t)~MTD_ERASED_FLAGS; + hdr.flags = (uint8_t)~MTD_ERASED_FLAGS(dev); mtdconfig_writebytes(dev, offset, &hdr.flags, sizeof(hdr.flags)); ret = OK; @@ -1521,9 +1520,9 @@ static int mtdconfig_firstconfig(FAR struct mtdconfig_struct_s *dev, #ifdef CONFIG_MTD_CONFIG_NAMED if (dev->readoff != 0 && - hdr.name[0] != CONFIG_MTD_CONFIG_ERASEDVALUE) + hdr.name[0] != MTD_ERASED_ID(dev)) #else - if (dev->readoff != 0 && hdr.id != MTD_ERASED_ID) + if (dev->readoff != 0 && hdr.id != MTD_ERASED_ID(dev)) #endif { /* Perform the read */ @@ -1596,9 +1595,9 @@ static int mtdconfig_nextconfig(FAR struct mtdconfig_struct_s *dev, #ifdef CONFIG_MTD_CONFIG_NAMED if (dev->readoff != 0 && - hdr.name[0] != CONFIG_MTD_CONFIG_ERASEDVALUE) + hdr.name[0] != MTD_ERASED_ID(dev)) #else - if (dev->readoff != 0 && hdr.id != MTD_ERASED_ID) + if (dev->readoff != 0 && hdr.id != MTD_ERASED_ID(dev)) #endif { /* Test if this is an empty slot */ @@ -1749,7 +1748,6 @@ int mtdconfig_register(FAR struct mtd_dev_s *mtd) /* Initialize the mtdconfig device structure */ dev->mtd = mtd; - nxsem_init(&dev->exclsem, 0, 1); /* Get the device geometry. (casting to uintptr_t first eliminates * complaints on some architectures where the sizeof long is different @@ -1770,6 +1768,18 @@ int mtdconfig_register(FAR struct mtd_dev_s *mtd) dev->erasesize = geo.erasesize; dev->nblocks = geo.neraseblocks * geo.erasesize / geo.blocksize; + /* And query the erase state */ + + ret = MTD_IOCTL(mtd, MTDIOC_ERASESTATE, + (unsigned long)((uintptr_t)&dev->erasestate)); + if (ret < 0) + { + ferr("ERROR: MTD ioctl(MTDIOC_ERASESTATE) failed: %d\n", ret); + kmm_free(dev); + goto errout; + } + + nxsem_init(&dev->exclsem, 0, 1); register_driver("/dev/config", &mtdconfig_fops, 0666, dev); }