[PATCH] mtd: nand: Fix bad block marking for blocks with specific offsets in BBT

Echo Hou chaosesprit at gmail.com
Mon Jan 12 11:09:14 CET 2026


When BITS_PER_LONG is 32 and offs is in the range [29, 31], the GENMASK
macro produces a negative shift count. This undefined behavior causes
the bad block marking process to fail.

Signed-off-by: Echo Hou <chaosesprit at gmail.com>
---
 drivers/mtd/nand/bbt.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/bbt.c b/drivers/mtd/nand/bbt.c
index 4ff0999f62a..0a434a70d5f 100644
--- a/drivers/mtd/nand/bbt.c
+++ b/drivers/mtd/nand/bbt.c
@@ -115,12 +115,16 @@ int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
 	unsigned long *pos = nand->bbt.cache +
 			     ((entry * bits_per_block) / BITS_PER_LONG);
 	unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
+	unsigned int offs_h = offs + bits_per_block - 1;
 	unsigned long val = status & GENMASK(bits_per_block - 1, 0);
 
 	if (entry >= nanddev_neraseblocks(nand))
 		return -ERANGE;
 
-	pos[0] &= ~GENMASK(offs + bits_per_block - 1, offs);
+	if (offs_h > BITS_PER_LONG - 1)
+		offs_h = BITS_PER_LONG - 1;
+
+	pos[0] &= ~GENMASK(offs_h, offs);
 	pos[0] |= val << offs;
 
 	if (bits_per_block + offs > BITS_PER_LONG) {
-- 
2.43.0



More information about the U-Boot mailing list