[PATCH v2] libmtd: avoid divide by zero

Chris Packham chris.packham at alliedtelesis.co.nz
Thu Nov 26 19:07:08 EST 2020


The concept of erase blocks doesn't apply to mtd-ram devices. Such
devices set MTD_NO_ERASE to indicate this and some report 0 for the
erase block size. Avoid a divide by zero when calculating the erase
block count for such devices.

Signed-off-by: Chris Packham <chris.packham at alliedtelesis.co.nz>
---
Changes in v2:
- Use MTD_NO_ERASE as suggested by Richard

 lib/libmtd.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lib/libmtd.c b/lib/libmtd.c
index 9d8d0e8..b581d80 100644
--- a/lib/libmtd.c
+++ b/lib/libmtd.c
@@ -791,7 +791,10 @@ int mtd_get_dev_info1(libmtd_t desc, int mtd_num, struct mtd_dev_info *mtd)
 		return -1;
 	mtd->writable = !!(ret & MTD_WRITEABLE);
 
-	mtd->eb_cnt = mtd->size / mtd->eb_size;
+	if (ret & MTD_NO_ERASE)
+		mtd->eb_cnt = 1;
+	else
+		mtd->eb_cnt = mtd->size / mtd->eb_size;
 	mtd->type = type_str2int(mtd->type_str);
 	mtd->bb_allowed = !!(mtd->type == MTD_NANDFLASH ||
 				mtd->type == MTD_MLCNANDFLASH);
-- 
2.29.2




More information about the linux-mtd mailing list