From 57ff0268201eed7103f4fa8c2293175f9a053374 Mon Sep 17 00:00:00 2001 From: Andrei Nistor Date: Sun, 29 Nov 2026 09:54:47 +0200 Subject: [PATCH] mtd: spinand: end the ID tables with a NULL entry nand_scan_with_ids() walks the table it is given until it reaches an entry whose name is NULL. spinand_flash_ids has no such entry, in this driver or in the SPI020 driver, so for a chip that matches no entry nand_detect() runs past the end of the table and reads the memory after it as more entries. Add the terminator to both tables. drv_nand_read_id() bounds its own walk with ARRAY_SIZE(), which now counts the terminator. Its mfr_id and dev_id are 0, so a flash that answers 00 00, because it is absent or not responding, would match it, and spinand.c would then set page_size to 0 and divide by it. The review of #339 pointed this out and suggested stopping the loop at the entry with a NULL name, which both copies now do. An ID that matched no entry still made drv_nand_read_id() return success. spinand.c then went on with page and block sizes it never set, as flash_info comes from kmalloc(). Print the ID and return -ENODEV instead, so the probe ends in drv_nand_identify() with the existing "failed to identify flash" message. That error path freed mtd, which points into info, and info comes from devm_kzalloc(). The kfree() corrupted the slab, and later in the boot the kernel oopsed in clk_debug_init(). Drop it in both drivers; devres frees info when the probe fails. Tested on a Reolink E340 (NT98566, FORESEE F35SQA512M) started by kexec: the flash is found and named, there are 10 partitions, ECC strength and bitflip threshold read 1, UBI attaches and both UBIFS volumes mount. Tested in the nt98566 machine of OpenIPC's qemu-bench with the same camera's flash image: the FORESEE ID cd 70 probes and the root filesystem mounts; for the IDs 12 34, 00 00 and ff ff the probe fails with "flash ID 0x.. 0x.. is not in the ID table" and the boot goes on until it cannot mount root, as expected. Without this change those three IDs end in the clk_debug_init() oops. 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 | 18 ++++++++++++------ drivers/mtd/spiflash/spinand.c | 18 ++++++++++++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/drivers/mtd/spiflash/spi020_nand.c b/drivers/mtd/spiflash/spi020_nand.c index 8ae3c8f081..36da5276cd 100644 --- a/drivers/mtd/spiflash/spi020_nand.c +++ b/drivers/mtd/spiflash/spi020_nand.c @@ -383,6 +383,7 @@ static struct nand_flash_dev spinand_flash_ids[] = { {"DS35Q1GA 1GiB 3.3V", { .id = {MFG_ID_DOSILICON, 0x71} }, SZ_2K, SZ_128, SZ_128K, 0, 2, 64 }, + { NULL } }; #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0) @@ -673,7 +674,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 < ARRAY_SIZE(spinand_flash_ids); i++) { + for (i = 0; spinand_flash_ids[i].name; 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; @@ -681,10 +682,16 @@ static int drv_nand_read_id(struct drv_nand_dev_info *info, uint32_t *id) break; } } - *id = card_id[0] | (card_id[1] << 8) | (card_id[2] << 16) | \ - (card_id[3] << 24); - - ret = E_OK; + if (spinand_flash_ids[i].name) { + *id = card_id[0] | (card_id[1] << 8) | + (card_id[2] << 16) | (card_id[3] << 24); + ret = E_OK; + } else { + dev_err(&info->pdev->dev, + "flash ID 0x%02x 0x%02x is not in the ID table\n", + card_id[0], card_id[1]); + ret = -ENODEV; + } } mutex_unlock(&info->lock); @@ -1193,7 +1200,6 @@ static int spinand_probe(struct platform_device *pdev) kfree(info->data_buff); fail_free_mtd: - kfree(mtd); return ret; } diff --git a/drivers/mtd/spiflash/spinand.c b/drivers/mtd/spiflash/spinand.c index 06e679f907..84775b382e 100644 --- a/drivers/mtd/spiflash/spinand.c +++ b/drivers/mtd/spiflash/spinand.c @@ -435,6 +435,7 @@ static struct nand_flash_dev spinand_flash_ids[] = { {"F35SQA512M 512Mbit 3.3V", { .id = {MFG_ID_FORESEE, 0x70} }, SZ_2K, SZ_64, SZ_128K, 0, 2, 64 }, + { NULL } }; #ifdef CONFIG_MTD_SPINAND_HWECC @@ -839,7 +840,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 < ARRAY_SIZE(spinand_flash_ids); i++) { + for (i = 0; spinand_flash_ids[i].name; 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; @@ -853,10 +854,16 @@ static int drv_nand_read_id(struct drv_nand_dev_info *info, uint32_t *id) break; } } - *id = card_id[0] | (card_id[1] << 8) | (card_id[2] << 16) | \ - (card_id[3] << 24); - - ret = E_OK; + if (spinand_flash_ids[i].name) { + *id = card_id[0] | (card_id[1] << 8) | + (card_id[2] << 16) | (card_id[3] << 24); + ret = E_OK; + } else { + dev_err(&info->pdev->dev, + "flash ID 0x%02x 0x%02x is not in the ID table\n", + card_id[0], card_id[1]); + ret = -ENODEV; + } } mutex_unlock(&info->lock); @@ -1445,7 +1452,6 @@ static int spinand_probe(struct platform_device *pdev) kfree(info->data_buff); fail_free_mtd: - kfree(mtd); return ret; }