U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cmd: usb: Prevent reset in usb tree/info  command
@ 2023-06-19 10:26 Xavier Drudis Ferran
  2023-06-19 11:54 ` Marek Vasut
  0 siblings, 1 reply; 9+ messages in thread
From: Xavier Drudis Ferran @ 2023-06-19 10:26 UTC (permalink / raw)
  To: u-boot
  Cc: Marek Vasut, Simon Glass, e, Lukasz Majewski, Sean Anderson,
	Suneel Garapati, Bin Meng

When DISTRO_DEFAULTS is not set, the default environment has
bootcmd=bootflow and this will cause a UCLASS_BOOTDEV device to be
added as sibling of those UCLASS_BLK devices in boot_targets, until
boot succeeds from some device. If none succeeds, and usb is in
boot_targets, and an usb storage device is plugged to some usb port at
boot time, its UCLASS_MASS_STORAGE device will have a UCLASS_BOOTDEV
device as child, besides a UCLASS_BLK child.

If once the boot fails the user enters at the U-Boot shell prompt:

usb info

or

usb tree

The code in cmd/usb.c will eventually recurse into the UCLASS_BOOTDEV
and pass a null pointer to usb_device (because it has no parent_priv_).
This causes a reset.

Fix it (twice) by checking for null parent_priv_ and adding
UCLASS_BOOTDEV to the list of ignored class ids before the recursive
call.

This prevents the current particular problem with UCLASS_BOOTDEV, even
in case it ever gets some parent_priv_ struct which is not an
usb_device, despite being the child of a usb_device->dev. And it also
prevents possible future problems if other children are added to usb
devices that don't have parent_priv_ because they are not part of the
usb tree, just abstractions of functionality (like UCLASS_BLK and
UCLASS_BOOTDEV are now).

Signed-off-by: Xavier Drudis Ferran <xdrudis@tinet.cat>
---

v2: added UCLASS_BOOTDEV check (discussion of v1 dried up without much
    evident consensus, so hopefully Simon Glass likes it better now)
    [ https://patchwork.ozlabs.org/project/uboot/patch/ZH39SVA1vbZR3+Gk@xdrudis.tinet.cat/ ]
---

Apologies to the people in Cc: for resending this. I had a typo in the
email list address.

---
 cmd/usb.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/cmd/usb.c b/cmd/usb.c
index 6193728384..23253f2223 100644
--- a/cmd/usb.c
+++ b/cmd/usb.c
@@ -421,7 +421,9 @@ static void usb_show_tree_graph(struct usb_device *dev, char *pre)
 		 * Ignore emulators and block child devices, we only want
 		 * real devices
 		 */
-		if ((device_get_uclass_id(child) != UCLASS_USB_EMUL) &&
+		if (udev &&
+		    (device_get_uclass_id(child) != UCLASS_BOOTDEV) &&
+		    (device_get_uclass_id(child) != UCLASS_USB_EMUL) &&
 		    (device_get_uclass_id(child) != UCLASS_BLK)) {
 			usb_show_tree_graph(udev, pre);
 			pre[index] = 0;
@@ -604,10 +606,12 @@ static void usb_show_info(struct usb_device *udev)
 	     child;
 	     device_find_next_child(&child)) {
 		if (device_active(child) &&
+		    (device_get_uclass_id(child) != UCLASS_BOOTDEV) &&
 		    (device_get_uclass_id(child) != UCLASS_USB_EMUL) &&
 		    (device_get_uclass_id(child) != UCLASS_BLK)) {
 			udev = dev_get_parent_priv(child);
-			usb_show_info(udev);
+			if (udev)
+				usb_show_info(udev);
 		}
 	}
 }
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread
[parent not found: <ZJAqKxrO7qa8r6Kq@xdrudis.tinet.cat>]
* [PATCH] cmd: usb: Prevent reset in usb tree/info  command
@ 2023-06-05 15:20 Xavier Drudis Ferran
  2023-06-07 22:05 ` Marek Vasut
  0 siblings, 1 reply; 9+ messages in thread
From: Xavier Drudis Ferran @ 2023-06-05 15:20 UTC (permalink / raw)
  To: u-boot; +Cc: Simon Glass, Lukasz Majewski, Sean Anderson, Marek Vasut

Add a check to avoid dommed (by null pointer dereference) recursive
call, not only for UCLASS_BLK.

When booting my Rock Pi 4B+ with a USB mass storage stick plugged
into one of the USB 2 ports (EHCI), when it is plugged before power
on, when issuing a

usb tree

or

usb info

command I get a "Synchronous Error" and a reset just after printing the
mass storage device in the usb tree or usb info. It might depend on the
contents of the USB stick too, I'm not sure.

It seems like I have two devices as children of the mass storage
device.  When there's only a UCLASS_BLK it works fine, but when there's
a UCLASS_BLK and a UCLASS_BOOTDEV, it recurses with a null udev as
first parameter and fails.

Likewise for usb_show_info().

Not sure if this should be a patch, an RFC or a bug report.  There may
be a better way to solve this, I haven't researched commit
201417d700a2ab09 ("bootstd: Add the bootdev uclass") and bootdev flow
properly, or thought about cases where udev is not null but the
recursive call might need preventing too. Feel free to think it over
before merging (or after). But this at least fixes a reset at an
innocent looking usb tree or usb info command. Maybe we can improve it
later?

Cc: Simon Glass <sjg@chromium.org>
Cc: Lukasz Majewski <lukma@denx.de>
Cc: Marek Vasut <marex@denx.de>


Signed-off-by: Xavier Drudis Ferran <xdrudis@tinet.cat>
---
 cmd/usb.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/cmd/usb.c b/cmd/usb.c
index 73addb04c4..7e6065aa51 100644
--- a/cmd/usb.c
+++ b/cmd/usb.c
@@ -421,7 +421,8 @@ static void usb_show_tree_graph(struct usb_device *dev, char *pre)
 		 * Ignore emulators and block child devices, we only want
 		 * real devices
 		 */
-		if ((device_get_uclass_id(child) != UCLASS_USB_EMUL) &&
+		if (udev &&
+		    (device_get_uclass_id(child) != UCLASS_USB_EMUL) &&
 		    (device_get_uclass_id(child) != UCLASS_BLK)) {
 			usb_show_tree_graph(udev, pre);
 			pre[index] = 0;
@@ -607,7 +608,8 @@ static void usb_show_info(struct usb_device *udev)
 		    (device_get_uclass_id(child) != UCLASS_USB_EMUL) &&
 		    (device_get_uclass_id(child) != UCLASS_BLK)) {
 			udev = dev_get_parent_priv(child);
-			usb_show_info(udev);
+			if (udev)
+				usb_show_info(udev);
 		}
 	}
 }
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-06-20  9:49 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-19 10:26 [PATCH] cmd: usb: Prevent reset in usb tree/info command Xavier Drudis Ferran
2023-06-19 11:54 ` Marek Vasut
     [not found] <ZJAqKxrO7qa8r6Kq@xdrudis.tinet.cat>
2023-06-19 21:49 ` Marek Vasut
2023-06-20  9:17   ` Xavier Drudis Ferran
2023-06-20  9:49     ` Marek Vasut
  -- strict thread matches above, loose matches on Subject: below --
2023-06-05 15:20 Xavier Drudis Ferran
2023-06-07 22:05 ` Marek Vasut
2023-06-08  7:39   ` Xavier Drudis Ferran
2023-06-09  1:20     ` Marek Vasut

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox