Skip to contentSkip to conversation

cmd: mtd: check if a block has to be skipped or erased

PATCH Superseded Dario Binacchi 2 participants 7 messages Oct 24, 2022 – Oct 30, 2022

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 cmd: mtd: check if a block has to be skipped or erased

From: Mikhail Kshevetskiy <[email protected]>

As reported by patch [1], the `mtd erase' command should not erase bad blocks.
To force bad block erasing you have to use the `mtd erase.dontskipbad' command.

This patch tries to fix the same issue without modifying code taken from the linux kernel, in order to make further upgrades easier.

[1] https://lore.kernel.org/all/[email protected]/

---

Changes in v3:
- Simplify the code. mtd_erase() can't return a bad block error. Print the messaged where the bad block is found.

Changes in v2:
- Change the commit author
- Do not continue to erase if scrub option is enabled and a bad block was found but return from the function.
- Update the patch tags.

 cmd/mtd.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
cmd/mtd.c +16 -8
index ad5cc9827d55..29b2a9c04c0c 100644
@@ -434,19 +434,27 @@ static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc,
 	erase_op.mtd = mtd;
 	erase_op.addr = off;
 	erase_op.len = mtd->erasesize;
-	erase_op.scrub = scrub;
 
 	while (len) {
-		ret = mtd_erase(mtd, &erase_op);
-
-		if (ret) {
-			/* Abort if its not a bad block error */
-			if (ret != -EIO)
+		if (!scrub) {
+			ret = mtd_block_isbad(mtd, erase_op.addr);
+			if (ret < 0) {
+				printf("Failed to get bad block at 0x%08llx\n",
+				       erase_op.addr);
+				ret = CMD_RET_FAILURE;
+				goto out_put_mtd;
+			} else if (ret > 0) {
+				printf("Skipping bad block at 0x%08llx\n",
+				       erase_op.addr);
+				ret = -EIO;
 				break;
-			printf("Skipping bad block at 0x%08llx\n",
-			       erase_op.addr);
+			}
 		}
 
+		ret = mtd_erase(mtd, &erase_op);
+		if (ret)
+			break;
+
 		len -= mtd->erasesize;
 		erase_op.addr += mtd->erasesize;
 	}
Signature · 1 line
2.32.0
#1 lore raw
Quoted 67 lines · Dario Binacchi

[External email]

From: Mikhail Kshevetskiy <[email protected]>

As reported by patch [1], the `mtd erase' command should not erase bad blocks.
To force bad block erasing you have to use the `mtd erase.dontskipbad' command.

This patch tries to fix the same issue without modifying code taken from the linux kernel, in order to make further upgrades easier.

[1] https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fall%2F20221006031501.110290-2-mikhail.kshevetskiy%40iopsys.eu%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7C0ce3c4f03458440084dd08dab5a44a0e%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638022014468655016%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=VMQov1%2FRUmymW2G452jlOveOu2tFCE5MtzssuP2Ri3Y%3D&amp;reserved=0

---

Changes in v3:
- Simplify the code. mtd_erase() can't return a bad block error. Print the messaged where the bad block is found.

Changes in v2:
- Change the commit author
- Do not continue to erase if scrub option is enabled and a bad block was found but return from the function.
- Update the patch tags.

 cmd/mtd.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
cmd/mtd.c +11 -6
index ad5cc9827d55..29b2a9c04c0c 100644
@@ -434,19 +434,27 @@ static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc,
        erase_op.mtd = mtd;
        erase_op.addr = off;
        erase_op.len = mtd->erasesize;
-       erase_op.scrub = scrub;
 
        while (len) {
-               ret = mtd_erase(mtd, &erase_op);
-
-               if (ret) {
-                       /* Abort if its not a bad block error */
-                       if (ret != -EIO)
+               if (!scrub) {
+                       ret = mtd_block_isbad(mtd, erase_op.addr);
+                       if (ret < 0) {
+                               printf("Failed to get bad block at 0x%08llx\n",
+                                      erase_op.addr);
+                               ret = CMD_RET_FAILURE;
+                               goto out_put_mtd;
+                       } else if (ret > 0) {
+                               printf("Skipping bad block at 0x%08llx\n",
+                                      erase_op.addr);
+                               ret = -EIO;

cmd/mtd.c static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc, · line 457

Quoted 10 lines
cmd/mtd.c +5 -2
                                break;
-                       printf("Skipping bad block at 0x%08llx\n",
-                              erase_op.addr);
+                       }
                }
 
+               ret = mtd_erase(mtd, &erase_op);
+               if (ret)
+                       break;
+

mtd_erase() can return -EIO, see drivers/mtd/nand/spi/core.c function spinand_mtd_erase()

Quoted 6 lines
cmd/mtd.c +0 -1
                len -= mtd->erasesize;
                erase_op.addr += mtd->erasesize;
        }
--

2.32.0

#2 lore raw

Hi Mikhail,

Quoted 76 lines · Mikhail Kshevetskiy
Quoted 73 lines · Dario Binacchi

[External email]

From: Mikhail Kshevetskiy <[email protected]>

As reported by patch [1], the `mtd erase' command should not erase bad blocks.
To force bad block erasing you have to use the `mtd erase.dontskipbad' command.

This patch tries to fix the same issue without modifying code taken from the linux kernel, in order to make further upgrades easier.

[1] https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fall%2F20221006031501.110290-2-mikhail.kshevetskiy%40iopsys.eu%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7C0ce3c4f03458440084dd08dab5a44a0e%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638022014468655016%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=VMQov1%2FRUmymW2G452jlOveOu2tFCE5MtzssuP2Ri3Y%3D&amp;reserved=0

---

Changes in v3:
- Simplify the code. mtd_erase() can't return a bad block error. Print the messaged where the bad block is found.

Changes in v2:
- Change the commit author
- Do not continue to erase if scrub option is enabled and a bad block was found but return from the function.
- Update the patch tags.

 cmd/mtd.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
cmd/mtd.c +12 -8
index ad5cc9827d55..29b2a9c04c0c 100644
@@ -434,19 +434,27 @@ static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc,
        erase_op.mtd = mtd;
        erase_op.addr = off;
        erase_op.len = mtd->erasesize;
-       erase_op.scrub = scrub;
 
        while (len) {
-               ret = mtd_erase(mtd, &erase_op);
-
-               if (ret) {
-                       /* Abort if its not a bad block error */
-                       if (ret != -EIO)
+               if (!scrub) {
+                       ret = mtd_block_isbad(mtd, erase_op.addr);
+                       if (ret < 0) {
+                               printf("Failed to get bad block at 0x%08llx\n",
+                                      erase_op.addr);
+                               ret = CMD_RET_FAILURE;
+                               goto out_put_mtd;
+                       } else if (ret > 0) {
+                               printf("Skipping bad block at 0x%08llx\n",
+                                      erase_op.addr);
+                               ret = -EIO;
                                break;
-                       printf("Skipping bad block at 0x%08llx\n",
-                              erase_op.addr);
+                       }
                }
 
Quoted 7 lines

cmd/mtd.c static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc, · line 457

Quoted 4 lines
cmd/mtd.c +4 -0
+               ret = mtd_erase(mtd, &erase_op);
+               if (ret)
+                       break;
+

mtd_erase() can return -EIO, see drivers/mtd/nand/spi/core.c function spinand_mtd_erase()

If I compare my original patch [1] with yours [2], I see no difference in behavior except for ret checking after calling mtd_erase() which, in my case, was wrong.
Do you agree?

Further, checking for a bad block inside the do_mtd_erase(), the mtd_erase() can return -EIO only in the case of a protected block. In case of the scrub option enabled the bad block is erased, otherwise the block is jumped in the do_mtd_erase() without calling the mtd_erase().

I think that now we can distinguish between a bad block and a protected block inside the do_mtd_erase(), always if it's true that the -EIO error is only returned for bad/protected block by low level operations.

[1] https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/
[2] https://patchwork.amarulasolutions.com/patch/2458/

Thanks and regards,
Dario

Quoted 9 lines
Quoted 9 lines
cmd/mtd.c +0 -1
                len -= mtd->erasesize;
                erase_op.addr += mtd->erasesize;
        }
--

2.32.0

Signature · 17 lines
Dario Binacchi

Embedded Linux Developer

[email protected]

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
[email protected]

www.amarulasolutions.com
#3 lore raw
Quoted 91 lines · Dario Binacchi

[External email]

Hi Mikhail,

Quoted 75 lines · Mikhail Kshevetskiy
Quoted 73 lines · Dario Binacchi

[External email]

From: Mikhail Kshevetskiy <[email protected]>

As reported by patch [1], the `mtd erase' command should not erase bad blocks.
To force bad block erasing you have to use the `mtd erase.dontskipbad' command.

This patch tries to fix the same issue without modifying code taken from the linux kernel, in order to make further upgrades easier.

[1] https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fall%2F20221006031501.110290-2-mikhail.kshevetskiy%40iopsys.eu%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7C02dff372bf00435cd94208dab71b6aed%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638023625630435867%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=YRmQ5XcWRw8gcy4Hy3nK29J%2FnttlD10lQvH%2B5YnBrxU%3D&amp;reserved=0

---

Changes in v3:
- Simplify the code. mtd_erase() can't return a bad block error. Print the messaged where the bad block is found.

Changes in v2:
- Change the commit author
- Do not continue to erase if scrub option is enabled and a bad block was found but return from the function.
- Update the patch tags.

 cmd/mtd.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
cmd/mtd.c +12 -8
index ad5cc9827d55..29b2a9c04c0c 100644
@@ -434,19 +434,27 @@ static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc,
        erase_op.mtd = mtd;
        erase_op.addr = off;
        erase_op.len = mtd->erasesize;
-       erase_op.scrub = scrub;
 
        while (len) {
-               ret = mtd_erase(mtd, &erase_op);
-
-               if (ret) {
-                       /* Abort if its not a bad block error */
-                       if (ret != -EIO)
+               if (!scrub) {
+                       ret = mtd_block_isbad(mtd, erase_op.addr);
+                       if (ret < 0) {
+                               printf("Failed to get bad block at 0x%08llx\n",
+                                      erase_op.addr);
+                               ret = CMD_RET_FAILURE;
+                               goto out_put_mtd;
+                       } else if (ret > 0) {
+                               printf("Skipping bad block at 0x%08llx\n",
+                                      erase_op.addr);
+                               ret = -EIO;
                                break;
-                       printf("Skipping bad block at 0x%08llx\n",
-                              erase_op.addr);
+                       }
                }
 
Quoted 6 lines

cmd/mtd.c static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc, · line 457

Quoted 4 lines
cmd/mtd.c +4 -0
+               ret = mtd_erase(mtd, &erase_op);
+               if (ret)
+                       break;
+

mtd_erase() can return -EIO, see drivers/mtd/nand/spi/core.c function spinand_mtd_erase()

Quoted 4 lines

If I compare my original patch [1] with yours [2], I see no difference in behavior except for ret checking after calling mtd_erase() which, in my case, was wrong.
Do you agree?

yes

Quoted 5 lines

Further, checking for a bad block inside the do_mtd_erase(), the mtd_erase() can return -EIO only in the case of a protected block. In case of the scrub option enabled the bad block is erased, otherwise the block is jumped in the do_mtd_erase() without calling the mtd_erase().

I see -EIO error in the following cases

a) physical bad block

b) hardware failures (ex: some timeout expired)

maybe it also returned for protected block as well.

Quoted 4 lines

I think that now we can distinguish between a bad block and a protected block inside the do_mtd_erase(), always if it's true that the -EIO error is only returned for bad/protected block by low level operations.

Could you describe your idea in more details. I do not see a way to distinguish bad and protected blocks.

The main problem with your v3 patch is a termination of erasing loop on any mtd_erase() failure. It becomes worse if mtd_erase() returns -EIO. In this case you'll stop erasing due to error, but do_mtd_erase() will return success.

Mikhail

[1] https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpatchwork.ozlabs.org%2Fproject%2Fuboot%2Fpatch%2F20221021152929.3598415-1-dario.binacchi%40amarulasolutions.com%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7C02dff372bf00435cd94208dab71b6aed%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638023625630435867%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=BK5CkLi6MBMaByX8%2FK0vKhAMKciyFE0TuOnNqsljrLc%3D&amp;reserved=0
[2] https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpatchwork.amarulasolutions.com%2Fpatch%2F2458%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7C02dff372bf00435cd94208dab71b6aed%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638023625630435867%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=wzYLYjzuTOn8t7aHWxxUyT6IoKM72C%2FTTmC2nNzxVck%3D&amp;reserved=0

Thanks and regards,
Dario

Quoted 29 lines
Quoted 8 lines
Quoted 8 lines
cmd/mtd.c +0 -1
                len -= mtd->erasesize;
                erase_op.addr += mtd->erasesize;
        }
--

2.32.0

--

Dario Binacchi

Embedded Linux Developer

[email protected]

__________________________________

Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
[email protected]

https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.amarulasolutions.com%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7C02dff372bf00435cd94208dab71b6aed%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638023625630435867%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=HhmwzN7An3%2FyWcgkO7uORGfVKmlzdMp9cwFjaD4nLHs%3D&amp;reserved=0

#4 lore raw

Hi Mikhail,

Quoted 94 lines · Mikhail Kshevetskiy
Quoted 91 lines · Dario Binacchi

[External email]

Hi Mikhail,

Quoted 81 lines · Mikhail Kshevetskiy
Quoted 67 lines · Dario Binacchi

[External email]

From: Mikhail Kshevetskiy <[email protected]>

As reported by patch [1], the `mtd erase' command should not erase bad blocks.
To force bad block erasing you have to use the `mtd erase.dontskipbad' command.

This patch tries to fix the same issue without modifying code taken from the linux kernel, in order to make further upgrades easier.

[1] https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fall%2F20221006031501.110290-2-mikhail.kshevetskiy%40iopsys.eu%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7C02dff372bf00435cd94208dab71b6aed%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638023625630435867%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=YRmQ5XcWRw8gcy4Hy3nK29J%2FnttlD10lQvH%2B5YnBrxU%3D&amp;reserved=0

---

Changes in v3:
- Simplify the code. mtd_erase() can't return a bad block error. Print the messaged where the bad block is found.

Changes in v2:
- Change the commit author
- Do not continue to erase if scrub option is enabled and a bad block was found but return from the function.
- Update the patch tags.

 cmd/mtd.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
cmd/mtd.c +11 -6
index ad5cc9827d55..29b2a9c04c0c 100644
@@ -434,19 +434,27 @@ static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc,
        erase_op.mtd = mtd;
        erase_op.addr = off;
        erase_op.len = mtd->erasesize;
-       erase_op.scrub = scrub;
 
        while (len) {
-               ret = mtd_erase(mtd, &erase_op);
-
-               if (ret) {
-                       /* Abort if its not a bad block error */
-                       if (ret != -EIO)
+               if (!scrub) {
+                       ret = mtd_block_isbad(mtd, erase_op.addr);
+                       if (ret < 0) {
+                               printf("Failed to get bad block at 0x%08llx\n",
+                                      erase_op.addr);
+                               ret = CMD_RET_FAILURE;
+                               goto out_put_mtd;
+                       } else if (ret > 0) {
+                               printf("Skipping bad block at 0x%08llx\n",
+                                      erase_op.addr);
+                               ret = -EIO;

cmd/mtd.c static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc, · line 457

Quoted 10 lines
cmd/mtd.c +5 -2
                                break;
-                       printf("Skipping bad block at 0x%08llx\n",
-                              erase_op.addr);
+                       }
                }
 
+               ret = mtd_erase(mtd, &erase_op);
+               if (ret)
+                       break;
+

mtd_erase() can return -EIO, see drivers/mtd/nand/spi/core.c function spinand_mtd_erase()

Quoted 6 lines
Quoted 4 lines

If I compare my original patch [1] with yours [2], I see no difference in behavior except for ret checking after calling mtd_erase() which, in my case, was wrong.
Do you agree?

yes

So I'll submit version 4 which reverts to version 1 and changes the check for the return value
of the mtd_erase(). I will also change the author of the commit. I added your signed-off. Please
retest the patch on your hardware.

Quoted 7 lines
Quoted 6 lines

Further, checking for a bad block inside the do_mtd_erase(), the mtd_erase() can return -EIO only in the case of a protected block. In case of the scrub option enabled the bad block is erased, otherwise the block is jumped in the do_mtd_erase() without calling the mtd_erase().

Quoted 7 lines

I see -EIO error in the following cases

a) physical bad block

b) hardware failures (ex: some timeout expired)

maybe it also returned for protected block as well.

So, in case (b) we may have another type of problem which is not caused by this patch.
As we can see by the current code:

while (len) {
    ret = mtd_erase(mtd, &erase_op);

    if (ret) {
        /* Abort if its not a bad block error */
        if (ret != -EIO)
            break;
        printf("Skipping bad block at 0x%08llx\n",
                 erase_op.addr);
    }

    len -= mtd->erasesize;
    erase_op.addr += mtd->erasesize;

}

if (ret && ret != -EIO)

    ret = CMD_RET_FAILURE;
else
    ret = CMD_RET_SUCCESS;
Quoted 8 lines
Quoted 5 lines

I think that now we can distinguish between a bad block and a protected block inside the do_mtd_erase(), always if it's true that the -EIO error is only returned for bad/protected block by low level operations.

Could you describe your idea in more details. I do not see a way to distinguish bad and protected blocks.

Could we use mtd_block_isreserved() ?

Thanks and regards
Dario

Quoted 43 lines

The main problem with your v3 patch is a termination of erasing loop on any mtd_erase() failure. It becomes worse if mtd_erase() returns -EIO. In this case you'll stop erasing due to error, but do_mtd_erase() will return success.

Mikhail

[1] https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpatchwork.ozlabs.org%2Fproject%2Fuboot%2Fpatch%2F20221021152929.3598415-1-dario.binacchi%40amarulasolutions.com%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7C02dff372bf00435cd94208dab71b6aed%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638023625630435867%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=BK5CkLi6MBMaByX8%2FK0vKhAMKciyFE0TuOnNqsljrLc%3D&amp;reserved=0
[2] https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpatchwork.amarulasolutions.com%2Fpatch%2F2458%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7C02dff372bf00435cd94208dab71b6aed%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638023625630435867%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=wzYLYjzuTOn8t7aHWxxUyT6IoKM72C%2FTTmC2nNzxVck%3D&amp;reserved=0

Thanks and regards,
Dario

Quoted 29 lines
Quoted 8 lines
Quoted 8 lines
cmd/mtd.c +0 -1
                len -= mtd->erasesize;
                erase_op.addr += mtd->erasesize;
        }
--

2.32.0

--

Dario Binacchi

Embedded Linux Developer

[email protected]

__________________________________

Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
[email protected]

https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.amarulasolutions.com%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7C02dff372bf00435cd94208dab71b6aed%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638023625630435867%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=HhmwzN7An3%2FyWcgkO7uORGfVKmlzdMp9cwFjaD4nLHs%3D&amp;reserved=0

Signature · 17 lines
Dario Binacchi

Embedded Linux Developer

[email protected]

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
[email protected]

www.amarulasolutions.com
#5 lore raw
Quoted 106 lines · Dario Binacchi

[External email]

Hi Mikhail,

Quoted 96 lines · Mikhail Kshevetskiy
Quoted 94 lines · Dario Binacchi

[External email]

Hi Mikhail,

Quoted 74 lines · Mikhail Kshevetskiy
Quoted 73 lines · Dario Binacchi

[External email]

From: Mikhail Kshevetskiy <[email protected]>

As reported by patch [1], the `mtd erase' command should not erase bad blocks.
To force bad block erasing you have to use the `mtd erase.dontskipbad' command.

This patch tries to fix the same issue without modifying code taken from the linux kernel, in order to make further upgrades easier.

[1] https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fall%2F20221006031501.110290-2-mikhail.kshevetskiy%40iopsys.eu%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7Cfe44aaf264764836295e08daba7ff1f7%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638027355932818989%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=TwMrP%2FpIz5R1Dfd1iXr8u2wZvDj3sdyl%2F8TADkPEDpQ%3D&amp;reserved=0

---

Changes in v3:
- Simplify the code. mtd_erase() can't return a bad block error. Print the messaged where the bad block is found.

Changes in v2:
- Change the commit author
- Do not continue to erase if scrub option is enabled and a bad block was found but return from the function.
- Update the patch tags.

 cmd/mtd.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
cmd/mtd.c +12 -8
index ad5cc9827d55..29b2a9c04c0c 100644
@@ -434,19 +434,27 @@ static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc,
        erase_op.mtd = mtd;
        erase_op.addr = off;
        erase_op.len = mtd->erasesize;
-       erase_op.scrub = scrub;
 
        while (len) {
-               ret = mtd_erase(mtd, &erase_op);
-
-               if (ret) {
-                       /* Abort if its not a bad block error */
-                       if (ret != -EIO)
+               if (!scrub) {
+                       ret = mtd_block_isbad(mtd, erase_op.addr);
+                       if (ret < 0) {
+                               printf("Failed to get bad block at 0x%08llx\n",
+                                      erase_op.addr);
+                               ret = CMD_RET_FAILURE;
+                               goto out_put_mtd;
+                       } else if (ret > 0) {
+                               printf("Skipping bad block at 0x%08llx\n",
+                                      erase_op.addr);
+                               ret = -EIO;
                                break;
-                       printf("Skipping bad block at 0x%08llx\n",
-                              erase_op.addr);
+                       }
                }
 
Quoted 6 lines

cmd/mtd.c static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc, · line 457

Quoted 4 lines
cmd/mtd.c +4 -0
+               ret = mtd_erase(mtd, &erase_op);
+               if (ret)
+                       break;
+

mtd_erase() can return -EIO, see drivers/mtd/nand/spi/core.c function spinand_mtd_erase()

If I compare my original patch [1] with yours [2], I see no difference in behavior except for ret checking after calling mtd_erase() which, in my case, was wrong.
Do you agree?

Quoted 6 lines
Quoted 1 line

yes

So I'll submit version 4 which reverts to version 1 and changes the check for the return value
of the mtd_erase(). I will also change the author of the commit. I added your signed-off. Please
retest the patch on your hardware.

v4 looks good for me.

Quoted 37 lines
Quoted 6 lines
Quoted 6 lines

Further, checking for a bad block inside the do_mtd_erase(), the mtd_erase() can return -EIO only in the case of a protected block. In case of the scrub option enabled the bad block is erased, otherwise the block is jumped in the do_mtd_erase() without calling the mtd_erase().

Quoted 7 lines

I see -EIO error in the following cases

a) physical bad block

b) hardware failures (ex: some timeout expired)

maybe it also returned for protected block as well.

So, in case (b) we may have another type of problem which is not caused by this patch.
As we can see by the current code:

while (len) {
    ret = mtd_erase(mtd, &erase_op);

    if (ret) {
        /* Abort if its not a bad block error */
        if (ret != -EIO)
            break;
        printf("Skipping bad block at 0x%08llx\n",
                 erase_op.addr);
    }

    len -= mtd->erasesize;
    erase_op.addr += mtd->erasesize;

}

if (ret && ret != -EIO)

    ret = CMD_RET_FAILURE;
else
    ret = CMD_RET_SUCCESS;
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

I think that now we can distinguish between a bad block and a protected block inside the do_mtd_erase(), always if it's true that the -EIO error is only returned for bad/protected block by low level operations.

Could you describe your idea in more details. I do not see a way to distinguish bad and protected blocks.

Could we use mtd_block_isreserved() ?

Probably no. As I remember reserved block is not very good tested. I try to use it once to protect blocks from erasing and it does not help. The code looks good, but something goes wrong. I don't remember details :-(

The issue exists both u-boot and linux.

If I have some time during a week, I'll provide mode details.

Mikhail.

Quoted 65 lines

Thanks and regards
Dario

Quoted 5 lines

The main problem with your v3 patch is a termination of erasing loop on any mtd_erase() failure. It becomes worse if mtd_erase() returns -EIO. In this case you'll stop erasing due to error, but do_mtd_erase() will return success.

Quoted 35 lines

Mikhail

[1] https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpatchwork.ozlabs.org%2Fproject%2Fuboot%2Fpatch%2F20221021152929.3598415-1-dario.binacchi%40amarulasolutions.com%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7Cfe44aaf264764836295e08daba7ff1f7%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638027355932818989%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=EZiou2JpcMoHgVxNhjtBmmnJWcX39byooOlQHuLH4Z0%3D&amp;reserved=0
[2] https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpatchwork.amarulasolutions.com%2Fpatch%2F2458%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7Cfe44aaf264764836295e08daba7ff1f7%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638027355932818989%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=8zifHAz7im8uMEXrFBOEl9wDviXcH%2B2RNUKkvjh%2FUR0%3D&amp;reserved=0

Thanks and regards,
Dario

Quoted 27 lines
Quoted 7 lines
Quoted 7 lines
cmd/mtd.c +0 -1
                len -= mtd->erasesize;
                erase_op.addr += mtd->erasesize;
        }
--

2.32.0

--

Dario Binacchi

Embedded Linux Developer

[email protected]

__________________________________

Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
[email protected]

https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.amarulasolutions.com%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7Cfe44aaf264764836295e08daba7ff1f7%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638027355932818989%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=nBKZLjQtxq8u83xF968C73piVKRpJ1k7iVXAuMWjD5o%3D&amp;reserved=0

--

Dario Binacchi

Embedded Linux Developer

[email protected]

__________________________________

Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
[email protected]

https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.amarulasolutions.com%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7Cfe44aaf264764836295e08daba7ff1f7%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638027355932818989%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=nBKZLjQtxq8u83xF968C73piVKRpJ1k7iVXAuMWjD5o%3D&amp;reserved=0

#6 lore raw
In reply to Dario Binacchi
Quoted 58 lines · Dario Binacchi

[External email]

From: Mikhail Kshevetskiy <[email protected]>

As reported by patch [1], the `mtd erase' command should not erase bad blocks.
To force bad block erasing you have to use the `mtd erase.dontskipbad' command.

This patch tries to fix the same issue without modifying code taken from the linux kernel, in order to make further upgrades easier.

[1] https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fall%2F20221006031501.110290-2-mikhail.kshevetskiy%40iopsys.eu%2F&amp;data=05%7C01%7Cmikhail.kshevetskiy%40iopsys.eu%7C0ce3c4f03458440084dd08dab5a44a0e%7C7ff78d652de440f586750569e5c7a65d%7C0%7C0%7C638022014468655016%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=VMQov1%2FRUmymW2G452jlOveOu2tFCE5MtzssuP2Ri3Y%3D&amp;reserved=0

---

Changes in v3:
- Simplify the code. mtd_erase() can't return a bad block error. Print the messaged where the bad block is found.

Changes in v2:
- Change the commit author
- Do not continue to erase if scrub option is enabled and a bad block was found but return from the function.
- Update the patch tags.

 cmd/mtd.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
cmd/mtd.c +2 -6
index ad5cc9827d55..29b2a9c04c0c 100644
@@ -434,19 +434,27 @@ static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc,
        erase_op.mtd = mtd;
        erase_op.addr = off;
        erase_op.len = mtd->erasesize;
-       erase_op.scrub = scrub;
 
        while (len) {
-               ret = mtd_erase(mtd, &erase_op);
-
-               if (ret) {
-                       /* Abort if its not a bad block error */
-                       if (ret != -EIO)
+               if (!scrub) {
+                       ret = mtd_block_isbad(mtd, erase_op.addr);

cmd/mtd.c static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc, · line 450

Quoted 10 lines
cmd/mtd.c +9 -0
+                       if (ret < 0) {
+                               printf("Failed to get bad block at 0x%08llx\n",
+                                      erase_op.addr);
+                               ret = CMD_RET_FAILURE;
+                               goto out_put_mtd;
+                       } else if (ret > 0) {
+                               printf("Skipping bad block at 0x%08llx\n",
+                                      erase_op.addr);
+                               ret = -EIO;
                                break;

That is bad.//Skipping bad block is an expected behavior. We should not fail on it.

Quoted 15 lines
cmd/mtd.c +5 -3
-                       printf("Skipping bad block at 0x%08llx\n",
-                              erase_op.addr);
+                       }
                }
 
+               ret = mtd_erase(mtd, &erase_op);
+               if (ret)
+                       break;
+
                len -= mtd->erasesize;
                erase_op.addr += mtd->erasesize;
        }
--

2.32.0