[U-Boot] [PATCH v1] dm: usb: Prevent NULL hub in usb_device_info()

Bernhard Nortmann bernhard.nortmann at web.de
Wed Jun 29 10:43:18 CEST 2016


This patch modifies the usb_device_info() function to enumerate
active USB hubs by iterating UCLASS_USB_HUB directly.

The previous code used UCLASS_USB nodes instead and retrieved
their first child node, expecting it to be a hub. However, it
did not protect against retrieving (and dereferencing) a NULL
pointer this way.

Depending on the available USB hardware, this might happen easily.
For example the USB controller on sun7i-a20 has top-level OHCI
nodes that won't have any children as long as no USB1-only
peripheral is attached. ("usb tree" will only show EHCI nodes.)

The failure can also be demonstrated with U-Boot's sandbox
architecture, by simply enabling the inactive "usb_2" node in
test.dts. This creates a similar situation, where the existing
usb_device_info() implementation would crash (segfault) when
issuing a "usb info" command.

Signed-off-by: Bernhard Nortmann <bernhard.nortmann at web.de>

---

 cmd/usb.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/cmd/usb.c b/cmd/usb.c
index 58d9db2..3146f54 100644
--- a/cmd/usb.c
+++ b/cmd/usb.c
@@ -602,18 +602,13 @@ static void show_info(struct udevice *dev)
 
 static int usb_device_info(void)
 {
-	struct udevice *bus;
-
-	for (uclass_first_device(UCLASS_USB, &bus);
-	     bus;
-	     uclass_next_device(&bus)) {
-		struct udevice *hub;
+	struct udevice *hub;
 
-		device_find_first_child(bus, &hub);
-		if (device_get_uclass_id(hub) == UCLASS_USB_HUB &&
-		    device_active(hub)) {
+	for (uclass_first_device(UCLASS_USB_HUB, &hub);
+	     hub;
+	     uclass_next_device(&hub)) {
+		if (device_active(hub))
 			show_info(hub);
-		}
 	}
 
 	return 0;
-- 
2.7.3



More information about the U-Boot mailing list