This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new cbfaa7c3b0d drivers/mtd: Make compile time check for sane mtd 
isbad/markbad configuration
cbfaa7c3b0d is described below

commit cbfaa7c3b0dd041d63a67f4faa3faf94ea85fead
Author: Jukka Laitinen <[email protected]>
AuthorDate: Mon Jun 15 16:57:15 2026 +0300

    drivers/mtd: Make compile time check for sane mtd isbad/markbad 
configuration
    
    This removes the DEBUGASSERT in ftl_initialize_by_path. Instead, check
    compile time that FTL is enabled in case some of the drivers implement
    the isbad and markbad functions.
    
    Also select the FTL_BBM for those drivers as they require it.
    
    Signed-off-by: Jukka Laitinen <[email protected]>
---
 arch/arm/src/rp2040/rp2040_flash_mtd.c |  2 ++
 drivers/mtd/Kconfig                    |  6 ++++--
 drivers/mtd/ftl.c                      |  9 ---------
 drivers/mtd/mtd_partition.c            |  6 ++++++
 drivers/mtd/mtd_progmem.c              |  2 ++
 include/nuttx/mtd/mtd.h                | 12 ++++++++++--
 6 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/arch/arm/src/rp2040/rp2040_flash_mtd.c 
b/arch/arm/src/rp2040/rp2040_flash_mtd.c
index 01531484e65..68bbb772b82 100644
--- a/arch/arm/src/rp2040/rp2040_flash_mtd.c
+++ b/arch/arm/src/rp2040/rp2040_flash_mtd.c
@@ -185,8 +185,10 @@ static struct rp2040_flash_dev_s my_dev =
     NULL,
 #endif
     rp2040_flash_ioctl,
+#ifdef CONFIG_FTL_BBM
     NULL,
     NULL,
+#endif
     "rp_flash"
   },
   .lock = NXMUTEX_INITIALIZER,
diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig
index 343693c8ffd..b30e4f64fc6 100644
--- a/drivers/mtd/Kconfig
+++ b/drivers/mtd/Kconfig
@@ -51,8 +51,8 @@ config FTL_READAHEAD
        depends on DRVR_READAHEAD
 
 config FTL_BBM
-       bool "Enable bad block management in the FTL layer"
-       default y if MTD_NAND
+       bool
+       default n
        ---help---
                Enable logical to physical erase-block mapping in the FTL layer 
for
                MTD devices that expose bad blocks via the MTD isbad/markbad 
methods.
@@ -240,6 +240,7 @@ menuconfig MTD_NAND
        bool "MTD NAND support"
        depends on ALLOW_BSD_COMPONENTS
        default n
+       select FTL_BBM
        ---help---
                Enable support for NAND FLASH devices.
 
@@ -419,6 +420,7 @@ endif # RAMMTD
 config FILEMTD
        bool "File-based MTD driver"
        default n
+       select FTL_BBM
        ---help---
                Build support for a File-based MTD driver.
 
diff --git a/drivers/mtd/ftl.c b/drivers/mtd/ftl.c
index f175f4f5076..eee562c787d 100644
--- a/drivers/mtd/ftl.c
+++ b/drivers/mtd/ftl.c
@@ -951,15 +951,6 @@ int ftl_initialize_by_path(FAR const char *path, FAR 
struct mtd_dev_s *mtd,
 
   finfo("path=\"%s\"\n", path);
 
-#ifndef CONFIG_FTL_BBM
-  /* It is likely a configuration error if the mtd driver implements
-   * the bad block management, but it is still disabled by the
-   * configuration.
-   */
-
-  DEBUGASSERT(mtd->isbad == NULL && mtd->markbad == NULL);
-#endif
-
   /* Allocate a FTL device structure */
 
   dev = kmm_zalloc(sizeof(struct ftl_struct_s));
diff --git a/drivers/mtd/mtd_partition.c b/drivers/mtd/mtd_partition.c
index 7de1c64db3e..bd271949d8a 100644
--- a/drivers/mtd/mtd_partition.c
+++ b/drivers/mtd/mtd_partition.c
@@ -116,8 +116,10 @@ static ssize_t part_write(FAR struct mtd_dev_s *dev, off_t 
offset,
 #endif
 static int     part_ioctl(FAR struct mtd_dev_s *dev, int cmd,
                   unsigned long arg);
+#ifdef CONFIG_FTL_BBM
 static int     part_isbad(FAR struct mtd_dev_s *dev, off_t block);
 static int     part_markbad(FAR struct mtd_dev_s *dev, off_t block);
+#endif
 
 /* File system methods */
 
@@ -495,6 +497,7 @@ static int part_ioctl(FAR struct mtd_dev_s *dev, int cmd, 
unsigned long arg)
  *
  ****************************************************************************/
 
+#ifdef CONFIG_FTL_BBM
 static int part_isbad(FAR struct mtd_dev_s *dev, off_t block)
 {
   FAR struct mtd_partition_s *priv = (FAR struct mtd_partition_s *)dev;
@@ -540,6 +543,7 @@ static int part_markbad(FAR struct mtd_dev_s *dev, off_t 
block)
 
   return -ENOSYS;
 }
+#endif
 
 #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_PROCFS_EXCLUDE_PARTITIONS)
 
@@ -906,8 +910,10 @@ FAR struct mtd_dev_s *mtd_partition(FAR struct mtd_dev_s 
*mtd,
   part->child.bwrite  = part_bwrite;
   part->child.read    = mtd->read ? part_read : NULL;
   part->child.ioctl   = part_ioctl;
+#ifdef CONFIG_FTL_BBM
   part->child.isbad   = part_isbad;
   part->child.markbad = part_markbad;
+#endif
 #ifdef CONFIG_MTD_BYTE_WRITE
   part->child.write   = mtd->write ? part_write : NULL;
 #endif
diff --git a/drivers/mtd/mtd_progmem.c b/drivers/mtd/mtd_progmem.c
index e48e5d0fdfd..e265d89e960 100644
--- a/drivers/mtd/mtd_progmem.c
+++ b/drivers/mtd/mtd_progmem.c
@@ -103,8 +103,10 @@ static struct progmem_dev_s g_progmem =
     progmem_write,
 #endif
     progmem_ioctl,
+#ifdef CONFIG_FTL_BBM
     NULL,
     NULL,
+#endif
     "progmem",
   }
 };
diff --git a/include/nuttx/mtd/mtd.h b/include/nuttx/mtd/mtd.h
index aec16db1e3a..961ffd2ddd9 100644
--- a/include/nuttx/mtd/mtd.h
+++ b/include/nuttx/mtd/mtd.h
@@ -97,8 +97,14 @@
 #define MTD_READ(d,s,n,b)  ((d)->read    ? (d)->read(d,s,n,b)   : (-ENOSYS))
 #define MTD_WRITE(d,s,n,b) ((d)->write   ? (d)->write(d,s,n,b)  : (-ENOSYS))
 #define MTD_IOCTL(d,c,a)   ((d)->ioctl   ? (d)->ioctl(d,c,a)    : (-ENOSYS))
-#define MTD_ISBAD(d,b)     ((d)->isbad   ? (d)->isbad(d,b)      : (-ENOSYS))
-#define MTD_MARKBAD(d,b)   ((d)->markbad ? (d)->markbad(d,b)    : (-ENOSYS))
+
+#ifdef CONFIG_FTL_BBM
+#  define MTD_ISBAD(d,b)   ((d)->isbad   ? (d)->isbad(d,b)      : (-ENOSYS))
+#  define MTD_MARKBAD(d,b) ((d)->markbad ? (d)->markbad(d,b)    : (-ENOSYS))
+#else
+#  define MTD_ISBAD(d,b)   (-ENOSYS)
+#  define MTD_MARKBAD(d,b) (-ENOSYS)
+#endif
 
 /* If any of the low-level device drivers declare they want sub-sector erase
  * support, then define MTD_SUBSECTOR_ERASE.
@@ -220,8 +226,10 @@ struct mtd_dev_s
 
   /* Check/Mark bad block for the specified block number */
 
+#ifdef CONFIG_FTL_BBM
   CODE int (*isbad)(FAR struct mtd_dev_s *dev, off_t block);
   CODE int (*markbad)(FAR struct mtd_dev_s *dev, off_t block);
+#endif
 
   /* Name of this MTD device */
 

Reply via email to