Skip to contentSkip to conversation

cmd: sf: fix sf probe using non-flash device as flash handle

PATCH In review Nia Su 3 participants 3 messages Jun 9 – Oct 24

lore
Share & tools
lore

Nia Su

author
#0 lore raw

PATCH 1/1 cmd: sf: fix sf probe using non-flash device as flash handle

sf probe calls spi_flash_probe_bus_cs() which returns whatever device matches the given chip select, regardless of its type. On boards where a non-flash device (e.g. MMC) shares the SPI bus at cs 0, sf probe 0 would silently use that device as a flash handle, causing undefined behavior.

Check that the returned device belongs to UCLASS_SPI_FLASH before dereferencing its uclass private data.

Fixes fbb099183e3a ("dm: Convert spi_flash_probe() and 'sf probe' to use driver model")
---
 cmd/sf.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
cmd/sf.c +8 -2
index b5b7f671fb972b7bfe9ef60283b4c06431ce1ac0..ec9ebde2aaf032f2a2f435e0d95f1805603afb0e 100644
@@ -137,8 +137,14 @@ static int do_spi_flash_probe(int argc, char *const argv[])
 	flash = NULL;
 	if (use_dt) {
 		ret = spi_flash_probe_bus_cs(bus, cs, &new);
-		if (!ret)
-			flash = dev_get_uclass_priv(new);
+		if (!ret) {
+			if (device_get_uclass_id(new) == UCLASS_SPI_FLASH) {
+				flash = dev_get_uclass_priv(new);
+			} else {
+				printf("SF: device at %u:%u is not a SPI flash\n", bus, cs);
+				ret = -ENODEV;
+			}
+		}
 	} else {
 		flash = spi_flash_probe(bus, cs, speed, mode);
 	}

---
base-commit: 1a8b7ad50aed5f5f0a4ccbfd455233919c097293
change-id: 20260608-sf-validate-uclass-4b4973c5302b

--

Reviewed-by Peng Fan on v1 1/1
#1 lore raw
Quoted 9 lines

Subject: [PATCH] cmd: sf: fix sf probe using non-flash device as flash handle

sf probe calls spi_flash_probe_bus_cs() which returns whatever device matches the given chip select, regardless of its type. On boards where a non-flash device (e.g. MMC) shares the SPI bus at cs 0, sf probe 0 would silently use that device as a flash handle, causing undefined behavior.

Quoted 6 lines

Check that the returned device belongs to UCLASS_SPI_FLASH before dereferencing its uclass private data.

Fixes fbb099183e3a ("dm: Convert spi_flash_probe() and 'sf probe' to

use driver model")

Looks correct to me.

#2 lore raw
In reply to Nia Su
Quoted 10 lines · Nia Su

sf probe calls spi_flash_probe_bus_cs() which returns whatever device matches the given chip select, regardless of its type. On boards where a non-flash device (e.g. MMC) shares the SPI bus at cs 0, sf probe 0 would silently use that device as a flash handle, causing undefined behavior.

Check that the returned device belongs to UCLASS_SPI_FLASH before dereferencing its uclass private data.

[...]

Applied to u-boot/main, thanks!

[1/1] cmd: sf: fix sf probe using non-flash device as flash handle

      commit: a7e43205614890d8674ab07d1b5edf8216ebfbd0
Signature · 1 line
Tom