Skip to contentSkip to conversation

Fix OOB checking problem in nand_base.c

PATCH In review Stanley.Miao 3 participants 4 messages Feb 4, 2010 – Feb 15, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Fix OOB checking problem in nand_base.c

on TI OMAPL138 EVM board, the NAND Flash have 22 bytes free in OOB area. When yaffs2 writes the 28-byte tags into NAND OOB area, the NAND subsystem doesn't give any WARNING or ERROR.

Move the checking of validity of oob data from nand_do_write_oob into nand_write_oob to fix this problem.

#1 lore raw

PATCH 1/1 mtd: nand: move the checking the validity of oob into nand_write_oob

nand_write_oob will invoke nand_do_write_oob or nand_do_write_ops depending on if ops->datbuf is NULL. nand_do_write_oob checked the validity of oob but nand_do_write_ops didn't. Now move the check into nand_write_oob to ensure the validity of oobbuf.

---
 drivers/mtd/nand/nand_base.c |   69 ++++++++++++++++++++++-------------------
 1 files changed, 37 insertions(+), 32 deletions(-)
drivers/mtd/nand/nand_base.c +37 -32
index 8f2958f..29e2a06 100644
@@ -2113,40 +2113,9 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
 			     struct mtd_oob_ops *ops)
 {
-	int chipnr, page, status, len;
+	int chipnr, page, status;
 	struct nand_chip *chip = mtd->priv;
 
-	DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
-			 __func__, (unsigned int)to, (int)ops->ooblen);
-
-	if (ops->mode == MTD_OOB_AUTO)
-		len = chip->ecc.layout->oobavail;
-	else
-		len = mtd->oobsize;
-
-	/* Do not allow write past end of page */
-	if ((ops->ooboffs + ops->ooblen) > len) {
-		DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
-				"past end of page\n", __func__);
-		return -EINVAL;
-	}
-
-	if (unlikely(ops->ooboffs >= len)) {
-		DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
-				"write outside oob\n", __func__);
-		return -EINVAL;
-	}
-
-	/* Do not allow reads past end of device */
-	if (unlikely(to >= mtd->size ||
-		     ops->ooboffs + ops->ooblen >
-			((mtd->size >> chip->page_shift) -
-			 (to >> chip->page_shift)) * len)) {
-		DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
-				"end of device\n", __func__);
-		return -EINVAL;
-	}
-
 	chipnr = (int)(to >> chip->chip_shift);
 	chip->select_chip(mtd, chipnr);
 
@@ -2203,6 +2172,42 @@ static int nand_write_oob(struct mtd_info *mtd, loff_t to,
 		return -EINVAL;
 	}
 
+	if (ops->oobbuf) {
+		int len;
+		DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, ooblen = %i\n",
+				__func__, (unsigned int)to, (int)ops->ooblen);
+
+		if (ops->mode == MTD_OOB_AUTO)
+			len = chip->ecc.layout->oobavail;
+		else
+			len = mtd->oobsize;
+
+		/* Do not allow write past end of page */
+		if ((ops->ooboffs + ops->ooblen) > len) {
+			DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
+					"past end of page\n", __func__);
+			return -EINVAL;
+		}
+
+		if (unlikely(ops->ooboffs >= len)) {
+			DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
+					"write outside oob\n", __func__);
+			return -EINVAL;
+		}
+
+		/* Do not allow reads past end of device */
+		if (unlikely(to >= mtd->size ||
+					ops->ooboffs + ops->ooblen >
+					((mtd->size >> chip->page_shift) -
+					 (to >> chip->page_shift)) * len)) {
+			DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
+					"end of device\n", __func__);
+			return -EINVAL;
+		}
+
+
+	}
+
 	nand_get_device(chip, mtd, FL_WRITING);
 
 	switch(ops->mode) {
Signature · 1 line
1.5.4.3
#2 lore raw

Re: [PATCH 1/1] mtd: nand: move the checking the validity of oob into nand_write_oob

Quoted 18 lines · Stanley.Miao

nand_write_oob will invoke nand_do_write_oob or nand_do_write_ops depending on if ops->datbuf is NULL. nand_do_write_oob checked the validity of oob but nand_do_write_ops didn't. Now move the check into nand_write_oob to ensure the validity of oobbuf.

---
 drivers/mtd/nand/nand_base.c |   69 ++++++++++++++++++++++-------------------
 1 files changed, 37 insertions(+), 32 deletions(-)
drivers/mtd/nand/nand_base.c +0 -0
index 8f2958f..29e2a06 100644
@@ -2113,40 +2113,9 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
 			     struct mtd_oob_ops *ops)
 {

...

drivers/mtd/nand/nand_base.c static int nand_write_oob(struct mtd_info *mtd, loff_t to, · line 2178

Quoted 8 lines
drivers/mtd/nand/nand_base.c +4 -0
@@ -2203,6 +2172,42 @@ static int nand_write_oob(struct mtd_info *mtd, loff_t to,
 		return -EINVAL;
 	}
 
+	if (ops->oobbuf) {
+		int len;
+		DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, ooblen = %i\n",
+				__func__, (unsigned int)to, (int)ops->ooblen);

Why this check is not done for !obs->oobbuf cas as well?

Signature · 2 lines
Best Regards,
Artem Bityutskiy (Артём Битюцкий)
#3 lore raw

Re: [PATCH 1/1] mtd: nand: move the checking the validity of oob into nand_write_oob

Quoted 26 lines · Artem Bityutskiy
Quoted 18 lines · Stanley.Miao

nand_write_oob will invoke nand_do_write_oob or nand_do_write_ops depending on if ops->datbuf is NULL. nand_do_write_oob checked the validity of oob but nand_do_write_ops didn't. Now move the check into nand_write_oob to ensure the validity of oobbuf.

---
 drivers/mtd/nand/nand_base.c |   69 ++++++++++++++++++++++-------------------
 1 files changed, 37 insertions(+), 32 deletions(-)
drivers/mtd/nand/nand_base.c +0 -0
index 8f2958f..29e2a06 100644
@@ -2113,40 +2113,9 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
 			     struct mtd_oob_ops *ops)
 {

...

Quoted 4 lines
drivers/mtd/nand/nand_base.c +0 -0
@@ -2203,6 +2172,42 @@ static int nand_write_oob(struct mtd_info *mtd, loff_t to,
 		return -EINVAL;
 	}
 
Quoted 8 lines

drivers/mtd/nand/nand_base.c static int nand_write_oob(struct mtd_info *mtd, loff_t to, · line 2178

Quoted 4 lines
drivers/mtd/nand/nand_base.c +4 -0
+	if (ops->oobbuf) {
+		int len;
+		DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, ooblen = %i\n",
+				__func__, (unsigned int)to, (int)ops->ooblen);

Why this check is not done for !obs->oobbuf cas as well?

I address this and other problems in my patchset, please test if it works.

Best regards,
Maxim Levitsky