[PATCH] mtd: rawnand: samsung: fix pages_per_eraseblock calculation
Shuai Liu
847807867 at qq.com
Fri Oct 23 01:16:07 PDT 2026
In samsung_nand_decode_id(), the division by memorg->pagesize binds
tighter than the left shift due to C operator precedence, so the
expression is evaluated as:
(128 * 1024) << (shift / pagesize)
The shift amount is at most 7 while pagesize is at least 2048, so the
integer division always yields 0 and pages_per_eraseblock ends up
being 131072 regardless of the actual chip geometry.
nanddev_init() later computes mtd->erasesize as
pagesize * pages_per_eraseblock, overwriting the correct value set in
this function with an astronomic value (e.g. 0x20000000, 512 MiB, for
a chip with 4096-byte pages). MTD then rejects partitions not
starting on such an eraseblock boundary and forces them read-only,
breaking the rootfs on affected boards.
Add parentheses around the shift expression so that the eraseblock
size is computed before being divided by the page size.
Reported-by: kevinshq <kevinshq at 163.com>
Closes: https://bugzilla.kernel.org/show_bug.cgi?id=222140
Fixes: 629a442cad5f ("mtd: rawnand: Fill memorg during detection")
Signed-off-by: Shuai Liu <847807867 at qq.com>
---
drivers/mtd/nand/raw/nand_samsung.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/mtd/nand/raw/nand_samsung.c b/drivers/mtd/nand/raw/nand_samsung.c
index 0be6b7563..765c0a7d8 100644
--- a/drivers/mtd/nand/raw/nand_samsung.c
+++ b/drivers/mtd/nand/raw/nand_samsung.c
@@ -63,9 +63,9 @@ static void samsung_nand_decode_id(struct nand_chip *chip)
/* Get blocksize */
extid >>= 2;
- memorg->pages_per_eraseblock = (128 * 1024) <<
- (((extid >> 1) & 0x04) |
- (extid & 0x03)) /
+ memorg->pages_per_eraseblock = ((128 * 1024) <<
+ (((extid >> 1) & 0x04) |
+ (extid & 0x03))) /
memorg->pagesize;
mtd->erasesize = (128 * 1024) <<
(((extid >> 1) & 0x04) | (extid & 0x03));
--
2.43.0
More information about the linux-mtd
mailing list