From 564488862c0add8523815707fd180dab7a9de266 Mon Sep 17 00:00:00 2001 From: Andrei Nistor Date: Sun, 29 Nov 2026 09:00:35 +0200 Subject: [PATCH] mtd: spinand: bound the ID table walk by its entry count drv_nand_read_id() looks the chip's ID up in spinand_flash_ids with a loop bounded by sizeof(spinand_flash_ids), which is the size of the table in bytes. On NT9856x the table has 25 entries of 40 bytes, so the bound is 1000. A listed chip matches early and the loop breaks, but for an unlisted chip the walk continues past the last entry and reads the memory after the table as more entries. The index is a uint8_t, so it wraps at 256 before it can reach 1000, and the loop ends only if some bytes past the table happen to match the ID. In practice the probe spins forever with info->lock held. Bound the loop with ARRAY_SIZE() instead. The SPI020 driver has a copy of the same function with the same loop over its own table; change it the same way. Tested on a Reolink E340 (NT98566, FORESEE F35SQA512M) started by kexec: the chip is found and named from the table, ECC strength and bitflip threshold read 1, and UBI attaches and both UBIFS volumes mount. The SPI020 driver is built only for NA51068; it was compile-tested. Co-Authored-By: Claude Opus 5.5 --- drivers/mtd/spiflash/spi020_nand.c | 2 +- drivers/mtd/spiflash/spinand.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/mtd/spiflash/spi020_nand.c b/drivers/mtd/spiflash/spi020_nand.c index 3dba1ce6998..8ae3c8f0816 100644 --- a/drivers/mtd/spiflash/spi020_nand.c +++ b/drivers/mtd/spiflash/spi020_nand.c @@ -673,7 +673,7 @@ static int drv_nand_read_id(struct drv_nand_dev_info *info, uint32_t *id) pr_debug("id = 0x%02x 0x%02x 0x%02x 0x%02x\n", card_id[0], card_id[1], card_id[2], card_id[3]); - for (i = 0; i < sizeof(spinand_flash_ids); i++) { + for (i = 0; i < ARRAY_SIZE(spinand_flash_ids); i++) { if ((card_id[0] == spinand_flash_ids[i].mfr_id) && \ (card_id[1] == spinand_flash_ids[i].dev_id)) { id_count = spinand_flash_ids[i].id_len; diff --git a/drivers/mtd/spiflash/spinand.c b/drivers/mtd/spiflash/spinand.c index 01e1a8f73c8..06e679f9077 100644 --- a/drivers/mtd/spiflash/spinand.c +++ b/drivers/mtd/spiflash/spinand.c @@ -839,7 +839,7 @@ static int drv_nand_read_id(struct drv_nand_dev_info *info, uint32_t *id) pr_debug("id = 0x%02x 0x%02x 0x%02x 0x%02x\n", card_id[0], card_id[1], card_id[2], card_id[3]); - for (i = 0; i < sizeof(spinand_flash_ids); i++) { + for (i = 0; i < ARRAY_SIZE(spinand_flash_ids); i++) { if ((card_id[0] == spinand_flash_ids[i].mfr_id) && \ (card_id[1] == spinand_flash_ids[i].dev_id)) { id_count = spinand_flash_ids[i].id_len;