Skip to contentSkip to conversation

cmd: sf: Fix sf probe crash

PATCH Applied Weizhao Ouyang 6 participants 15 messages Jan 4, 2024 – Apr 14, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cmd: sf: Fix sf probe crash

Handle the return value of spi_flash_probe_bus_cs() to avoid sf probe crashes.

---
 cmd/sf.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
cmd/sf.c +3 -2
index 730996c02b..e3866899f6 100644
@@ -135,8 +135,9 @@ static int do_spi_flash_probe(int argc, char *const argv[])
 	}
 	flash = NULL;
 	if (use_dt) {
-		spi_flash_probe_bus_cs(bus, cs, &new);
-		flash = dev_get_uclass_priv(new);
+		ret = spi_flash_probe_bus_cs(bus, cs, &new);
+		if (!ret)
+			flash = dev_get_uclass_priv(new);
 	} else {
 		flash = spi_flash_probe(bus, cs, speed, mode);
 	}
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw

Hi

Quoted 26 lines · Weizhao Ouyang

Handle the return value of spi_flash_probe_bus_cs() to avoid sf probe crashes.

---
 cmd/sf.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
cmd/sf.c +3 -3
index 730996c02b..e3866899f6 100644
@@ -135,8 +135,9 @@ static int do_spi_flash_probe(int argc, char *const argv[])
        }
        flash = NULL;
        if (use_dt) {
-               spi_flash_probe_bus_cs(bus, cs, &new);
-               flash = dev_get_uclass_priv(new);
+               ret = spi_flash_probe_bus_cs(bus, cs, &new);
+               if (!ret)
+                       flash = dev_get_uclass_priv(new);
        } else {
                flash = spi_flash_probe(bus, cs, speed, mode);
        }
--
Quoted 2 lines

2.39.2

Suggest to add documentation of spi_flash_probe_bus_cs in a separated patch

Michael

#2 lore raw
In reply to Weizhao Ouyang
Quoted 13 lines · Weizhao Ouyang

Handle the return value of spi_flash_probe_bus_cs() to avoid sf probe crashes.

---
  cmd/sf.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
cmd/sf.c +0 -0
index 730996c02b..e3866899f6 100644

cmd/sf.c static int do_spi_flash_probe(int argc, char *const argv[]) · line 138

Quoted 6 lines
cmd/sf.c +1 -2
@@ -135,8 +135,9 @@ static int do_spi_flash_probe(int argc, char *const argv[])
  	}
  	flash = NULL;
  	if (use_dt) {
-		spi_flash_probe_bus_cs(bus, cs, &new);
-		flash = dev_get_uclass_priv(new);
+		ret = spi_flash_probe_bus_cs(bus, cs, &new);

if (ret)

	return ret;

don't you want to rather propagate that error?

Thanks,
Michal

#3 lore raw
Quoted 25 lines · Michal Simek
Quoted 13 lines · Weizhao Ouyang

Handle the return value of spi_flash_probe_bus_cs() to avoid sf probe crashes.

---
  cmd/sf.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
cmd/sf.c +0 -0
index 730996c02b..e3866899f6 100644

cmd/sf.c static int do_spi_flash_probe(int argc, char *const argv[]) · line 138

Quoted 6 lines
cmd/sf.c +1 -2
@@ -135,8 +135,9 @@ static int do_spi_flash_probe(int argc, char *const argv[])
      }
      flash = NULL;
      if (use_dt) {
-             spi_flash_probe_bus_cs(bus, cs, &new);
-             flash = dev_get_uclass_priv(new);
+             ret = spi_flash_probe_bus_cs(bus, cs, &new);

if (ret)

Quoted 4 lines
        return ret;

don't you want to rather propagate that error?

Well, since the spi_flash is empty, the following code will print the error message and return.

BR,
Weizhao

#4 lore raw
Quoted 28 lines · Weizhao Ouyang
Quoted 27 lines · Michal Simek
Quoted 13 lines · Weizhao Ouyang

Handle the return value of spi_flash_probe_bus_cs() to avoid sf probe crashes.

---
   cmd/sf.c | 5 +++--
   1 file changed, 3 insertions(+), 2 deletions(-)
cmd/sf.c +0 -0
index 730996c02b..e3866899f6 100644

cmd/sf.c static int do_spi_flash_probe(int argc, char *const argv[]) · line 138

Quoted 6 lines
cmd/sf.c +1 -2
@@ -135,8 +135,9 @@ static int do_spi_flash_probe(int argc, char *const argv[])
       }
       flash = NULL;
       if (use_dt) {
-             spi_flash_probe_bus_cs(bus, cs, &new);
-             flash = dev_get_uclass_priv(new);
+             ret = spi_flash_probe_bus_cs(bus, cs, &new);

if (ret)

         return ret;
Quoted 5 lines
Quoted 2 lines

don't you want to rather propagate that error?

Well, since the spi_flash is empty, the following code will print the error message and return.

And you return 0 which means everything is fine. But is everything fine in this case? Or do you want to see the error?

This is command it means you can simply use && and if previous command succeed you can call something else.

Thanks,
Michal

#5 lore raw
Quoted 35 lines · Michal Simek
Quoted 31 lines · Weizhao Ouyang
Quoted 29 lines · Michal Simek
Quoted 13 lines · Weizhao Ouyang

Handle the return value of spi_flash_probe_bus_cs() to avoid sf probe crashes.

---
   cmd/sf.c | 5 +++--
   1 file changed, 3 insertions(+), 2 deletions(-)
cmd/sf.c +0 -0
index 730996c02b..e3866899f6 100644

cmd/sf.c static int do_spi_flash_probe(int argc, char *const argv[]) · line 138

Quoted 6 lines
cmd/sf.c +1 -2
@@ -135,8 +135,9 @@ static int do_spi_flash_probe(int argc, char *const argv[])
       }
       flash = NULL;
       if (use_dt) {
-             spi_flash_probe_bus_cs(bus, cs, &new);
-             flash = dev_get_uclass_priv(new);
+             ret = spi_flash_probe_bus_cs(bus, cs, &new);

if (ret)

         return ret;

don't you want to rather propagate that error?

Quoted 9 lines
Quoted 2 lines

Well, since the spi_flash is empty, the following code will print the error message and return.

And you return 0 which means everything is fine. But is everything fine in this case? Or do you want to see the error?

This is command it means you can simply use && and if previous command succeed you can call something else.

Hi Michal,

Please check the code that follows this commit snippet:

if (!flash) {
    printf("Failed to initialize SPI flash at %u:%u (error %d)\n",
        bus, cs, ret);
    return 1;
}

it will print the error and return 1 as the return value.

BR,
Weizhao

Acked-by
#6 lore raw
Quoted 50 lines · Weizhao Ouyang
Quoted 35 lines · Michal Simek
Quoted 31 lines · Weizhao Ouyang
Quoted 29 lines · Michal Simek
Quoted 13 lines · Weizhao Ouyang

Handle the return value of spi_flash_probe_bus_cs() to avoid sf probe crashes.

---
    cmd/sf.c | 5 +++--
    1 file changed, 3 insertions(+), 2 deletions(-)
cmd/sf.c +0 -0
index 730996c02b..e3866899f6 100644

cmd/sf.c static int do_spi_flash_probe(int argc, char *const argv[]) · line 138

Quoted 6 lines
cmd/sf.c +1 -2
@@ -135,8 +135,9 @@ static int do_spi_flash_probe(int argc, char *const argv[])
        }
        flash = NULL;
        if (use_dt) {
-             spi_flash_probe_bus_cs(bus, cs, &new);
-             flash = dev_get_uclass_priv(new);
+             ret = spi_flash_probe_bus_cs(bus, cs, &new);

if (ret)

          return ret;

don't you want to rather propagate that error?

Quoted 9 lines
Quoted 2 lines

Well, since the spi_flash is empty, the following code will print the error message and return.

And you return 0 which means everything is fine. But is everything fine in this case? Or do you want to see the error?

This is command it means you can simply use && and if previous command succeed you can call something else.

Hi Michal,

Please check the code that follows this commit snippet:

Quoted 7 lines
if (!flash) {
     printf("Failed to initialize SPI flash at %u:%u (error %d)\n",
         bus, cs, ret);
     return 1;
}

it will print the error and return 1 as the return value.

ok. Then good.

Thanks,
Michal

#7 lore raw
In reply to Weizhao Ouyang

Hi

Quoted 29 lines · Weizhao Ouyang
Quoted 27 lines · Michal Simek
Quoted 13 lines · Weizhao Ouyang

Handle the return value of spi_flash_probe_bus_cs() to avoid sf probe crashes.

---
  cmd/sf.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
cmd/sf.c +0 -0
index 730996c02b..e3866899f6 100644

cmd/sf.c static int do_spi_flash_probe(int argc, char *const argv[]) · line 138

Quoted 6 lines
cmd/sf.c +1 -2
@@ -135,8 +135,9 @@ static int do_spi_flash_probe(int argc, char *const argv[])
      }
      flash = NULL;
      if (use_dt) {
-             spi_flash_probe_bus_cs(bus, cs, &new);
-             flash = dev_get_uclass_priv(new);
+             ret = spi_flash_probe_bus_cs(bus, cs, &new);

if (ret)

        return ret;
Quoted 6 lines
Quoted 2 lines

don't you want to rather propagate that error?

Well, since the spi_flash is empty, the following code will print the error message and return.

flash = NULL;

  if (!flash) {
                printf("Failed to initialize SPI flash at %u:%u (error %d)\n",
                       bus, cs, ret);
                return 1;
  }

This code does not change error handling that is already present here.

Michael

Quoted 2 lines

BR,
Weizhao

Signature · 11 lines
Michael Nazzareno Trimarchi
Co-Founder & Chief Executive Officer
M. +39 347 913 2170
[email protected]
__________________________________

Amarula Solutions BV
Joop Geesinkweg 125, 1114 AB, Amsterdam, NL
T. +31 (0)85 111 9172
[email protected]
www.amarulasolutions.com
#8 lore raw
Quoted 42 lines · Michael Nazzareno Trimarchi

Hi

Quoted 29 lines · Weizhao Ouyang
Quoted 27 lines · Michal Simek
Quoted 13 lines · Weizhao Ouyang

Handle the return value of spi_flash_probe_bus_cs() to avoid sf probe crashes.

---
  cmd/sf.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
cmd/sf.c +0 -0
index 730996c02b..e3866899f6 100644

cmd/sf.c static int do_spi_flash_probe(int argc, char *const argv[]) · line 138

Quoted 6 lines
cmd/sf.c +1 -2
@@ -135,8 +135,9 @@ static int do_spi_flash_probe(int argc, char *const argv[])
      }
      flash = NULL;
      if (use_dt) {
-             spi_flash_probe_bus_cs(bus, cs, &new);
-             flash = dev_get_uclass_priv(new);
+             ret = spi_flash_probe_bus_cs(bus, cs, &new);

if (ret)

        return ret;
Quoted 6 lines
Quoted 2 lines

don't you want to rather propagate that error?

Well, since the spi_flash is empty, the following code will print the error message and return.

flash = NULL;

Quoted 7 lines
  if (!flash) {
                printf("Failed to initialize SPI flash at %u:%u (error %d)\n",
                       bus, cs, ret);
                return 1;
  }

This code does not change error handling that is already present here.

Hi Michael,

Sorry I didn't get your point. This patch is designed to avoid spi_flash_probe_bus_cs() crash by null pointer, and let the current existing error handling to prompt it. So I'm not trying to change the current error handling.

BR,
Weizhao

Quoted 19 lines

Michael

Quoted 2 lines

BR,
Weizhao

--
Michael Nazzareno Trimarchi
Co-Founder & Chief Executive Officer
M. +39 347 913 2170
[email protected]
__________________________________

Amarula Solutions BV
Joop Geesinkweg 125, 1114 AB, Amsterdam, NL
T. +31 (0)85 111 9172
[email protected]
www.amarulasolutions.com

#9 lore raw

Hi

Quoted 51 lines · Weizhao Ouyang
Quoted 48 lines · Michael Nazzareno Trimarchi

Hi

Quoted 29 lines · Weizhao Ouyang
Quoted 27 lines · Michal Simek
Quoted 13 lines · Weizhao Ouyang

Handle the return value of spi_flash_probe_bus_cs() to avoid sf probe crashes.

---
  cmd/sf.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
cmd/sf.c +0 -0
index 730996c02b..e3866899f6 100644

cmd/sf.c static int do_spi_flash_probe(int argc, char *const argv[]) · line 138

Quoted 6 lines
cmd/sf.c +1 -2
@@ -135,8 +135,9 @@ static int do_spi_flash_probe(int argc, char *const argv[])
      }
      flash = NULL;
      if (use_dt) {
-             spi_flash_probe_bus_cs(bus, cs, &new);
-             flash = dev_get_uclass_priv(new);
+             ret = spi_flash_probe_bus_cs(bus, cs, &new);

if (ret)

        return ret;
Quoted 6 lines
Quoted 2 lines

don't you want to rather propagate that error?

Well, since the spi_flash is empty, the following code will print the error message and return.

flash = NULL;

  if (!flash) {
                printf("Failed to initialize SPI flash at %u:%u (error %d)\n",
                       bus, cs, ret);
                return 1;
  }
Quoted 9 lines
Quoted 1 line

This code does not change error handling that is already present here.

Hi Michael,

Sorry I didn't get your point. This patch is designed to avoid spi_flash_probe_bus_cs() crash by null pointer, and let the current existing error handling to prompt it. So I'm not trying to change the current error handling.

I have said exactly the same so I agree with you.

Michael

Quoted 22 lines

BR,
Weizhao

Quoted 19 lines

Michael

Quoted 2 lines

BR,
Weizhao

--
Michael Nazzareno Trimarchi
Co-Founder & Chief Executive Officer
M. +39 347 913 2170
[email protected]
__________________________________

Amarula Solutions BV
Joop Geesinkweg 125, 1114 AB, Amsterdam, NL
T. +31 (0)85 111 9172
[email protected]
www.amarulasolutions.com

Signature · 11 lines
Michael Nazzareno Trimarchi
Co-Founder & Chief Executive Officer
M. +39 347 913 2170
[email protected]
__________________________________

Amarula Solutions BV
Joop Geesinkweg 125, 1114 AB, Amsterdam, NL
T. +31 (0)85 111 9172
[email protected]
www.amarulasolutions.com
#10 lore raw
In reply to Weizhao Ouyang

Gentle ping. Not merged yet.

BR,
Weizhao

Quoted 28 lines · Weizhao Ouyang

Handle the return value of spi_flash_probe_bus_cs() to avoid sf probe crashes.

---
 cmd/sf.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
cmd/sf.c +3 -3
index 730996c02b..e3866899f6 100644
@@ -135,8 +135,9 @@ static int do_spi_flash_probe(int argc, char *const argv[])
        }
        flash = NULL;
        if (use_dt) {
-               spi_flash_probe_bus_cs(bus, cs, &new);
-               flash = dev_get_uclass_priv(new);
+               ret = spi_flash_probe_bus_cs(bus, cs, &new);
+               if (!ret)
+                       flash = dev_get_uclass_priv(new);
        } else {
                flash = spi_flash_probe(bus, cs, speed, mode);
        }
--

2.39.2

#11 lore raw
Quoted 4 lines · Weizhao Ouyang

Gentle ping. Not merged yet.

BR,
Weizhao

Hello Dario,

that patch is in your patchwork review queue. Could you, please, have a look.

Best regards

Heinrich

Quoted 30 lines · Weizhao Ouyang
Quoted 28 lines

Handle the return value of spi_flash_probe_bus_cs() to avoid sf probe crashes.

---
  cmd/sf.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
cmd/sf.c +3 -3
index 730996c02b..e3866899f6 100644
@@ -135,8 +135,9 @@ static int do_spi_flash_probe(int argc, char *const argv[])
         }
         flash = NULL;
         if (use_dt) {
-               spi_flash_probe_bus_cs(bus, cs, &new);
-               flash = dev_get_uclass_priv(new);
+               ret = spi_flash_probe_bus_cs(bus, cs, &new);
+               if (!ret)
+                       flash = dev_get_uclass_priv(new);
         } else {
                 flash = spi_flash_probe(bus, cs, speed, mode);
         }
--

2.39.2

#12 lore raw

Hi

Quoted 11 lines · Heinrich Schuchardt
Quoted 4 lines · Weizhao Ouyang

Gentle ping. Not merged yet.

BR,
Weizhao

Hello Dario,

that patch is in your patchwork review queue. Could you, please, have a look.

He is with me, I will ping about all the patches that we need to queue

Michael

Quoted 36 lines

Best regards

Heinrich

Quoted 30 lines · Weizhao Ouyang
Quoted 28 lines

Handle the return value of spi_flash_probe_bus_cs() to avoid sf probe crashes.

---
  cmd/sf.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
cmd/sf.c +3 -3
index 730996c02b..e3866899f6 100644
@@ -135,8 +135,9 @@ static int do_spi_flash_probe(int argc, char *const argv[])
         }
         flash = NULL;
         if (use_dt) {
-               spi_flash_probe_bus_cs(bus, cs, &new);
-               flash = dev_get_uclass_priv(new);
+               ret = spi_flash_probe_bus_cs(bus, cs, &new);
+               if (!ret)
+                       flash = dev_get_uclass_priv(new);
         } else {
                 flash = spi_flash_probe(bus, cs, speed, mode);
         }
--

2.39.2

Signature · 11 lines
Michael Nazzareno Trimarchi
Co-Founder & Chief Executive Officer
M. +39 347 913 2170
[email protected]
__________________________________

Amarula Solutions BV
Joop Geesinkweg 125, 1114 AB, Amsterdam, NL
T. +31 (0)85 111 9172
[email protected]
www.amarulasolutions.com
Reviewed-by on v1 1/1
#13 lore raw
In reply to Weizhao Ouyang

Hi,

Quoted 4 lines · Weizhao Ouyang

Handle the return value of spi_flash_probe_bus_cs() to avoid sf probe crashes.

This fixes a null pointer dereference when running "sf probe" and there are no spi devices enabled in the device tree for my boards, so:

Fixes 3feea0ba196a ("spi: spi_flash_probe_bus_cs() rely on DT for spi speed and mode")

Regards,
Jonas

Quoted 20 lines
---
 cmd/sf.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
cmd/sf.c +3 -2
index 730996c02b..e3866899f6 100644
@@ -135,8 +135,9 @@ static int do_spi_flash_probe(int argc, char *const argv[])
 	}
 	flash = NULL;
 	if (use_dt) {
-		spi_flash_probe_bus_cs(bus, cs, &new);
-		flash = dev_get_uclass_priv(new);
+		ret = spi_flash_probe_bus_cs(bus, cs, &new);
+		if (!ret)
+			flash = dev_get_uclass_priv(new);
 	} else {
 		flash = spi_flash_probe(bus, cs, speed, mode);
 	}
#14 lore raw

Hi Weizhao,

Quoted 23 lines · Jonas Karlman

Hi,

Quoted 4 lines · Weizhao Ouyang

Handle the return value of spi_flash_probe_bus_cs() to avoid sf probe crashes.

This fixes a null pointer dereference when running "sf probe" and there are no spi devices enabled in the device tree for my boards, so:

Fixes 3feea0ba196a ("spi: spi_flash_probe_bus_cs() rely on DT for spi speed and mode")

Regards,
Jonas

Quoted 4 lines
---
 cmd/sf.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 17 lines
Quoted 6 lines
cmd/sf.c +0 -0
index 730996c02b..e3866899f6 100644
@@ -135,8 +135,9 @@ static int do_spi_flash_probe(int argc, char *const argv[])
      }

cmd/sf.c static int do_spi_flash_probe(int argc, char *const argv[]) · line 143

Quoted 10 lines
cmd/sf.c +3 -2
      flash = NULL;
      if (use_dt) {
-             spi_flash_probe_bus_cs(bus, cs, &new);
-             flash = dev_get_uclass_priv(new);
+             ret = spi_flash_probe_bus_cs(bus, cs, &new);
+             if (!ret)
+                     flash = dev_get_uclass_priv(new);
      } else {
              flash = spi_flash_probe(bus, cs, speed, mode);
      }

Applied to nand-next

Thanks and regards
Dario

Signature · 17 lines
Dario Binacchi

Senior Embedded Linux Developer

[email protected]

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
[email protected]

www.amarulasolutions.com