mtd: Do not allow MTD devices with inconsistent erase properties
authorBoris Brezillon <boris.brezillon@free-electrons.com>
Fri, 15 Dec 2017 12:39:51 +0000 (13:39 +0100)
committerBoris Brezillon <boris.brezillon@free-electrons.com>
Sat, 6 Jan 2018 14:09:40 +0000 (15:09 +0100)
When mtd->erasesize is 0 or mtd->_erase is NULL, that means the device
does not support the erase operation, which in turn means it should
have the MTD_NO_ERASE flag set.

Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>
Reviewed-by: Miquel Raynal <miquel.raynal@free-electrons.com>
drivers/mtd/mtdcore.c

index f80e911b8843819db8dcd1956c76ce2bf60b5ab8..642c35dde6861fea7e930364663edd45f91aae8e 100644 (file)
@@ -503,6 +503,11 @@ int add_mtd_device(struct mtd_info *mtd)
                return -EEXIST;
 
        BUG_ON(mtd->writesize == 0);
+
+       if (WARN_ON((!mtd->erasesize || !mtd->_erase) &&
+                   !(mtd->flags & MTD_NO_ERASE)))
+               return -EINVAL;
+
        mutex_lock(&mtd_table_mutex);
 
        i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);