Skip to contentSkip to conversation

usb: xhci-pci: Check for errors from dm_pci_map_bar()

PATCH Superseded Pali Rohár 4 participants 7 messages Jan 18, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: xhci-pci: Check for errors from dm_pci_map_bar()

Function dm_pci_map_bar() may fail and returns NULL. Check this to prevent dereferencing a NULL pointer.

In xhci-pci this may happen when board does not enable CONFIG_PCI_PNP and PCI_BASE_ADDRESS_0 contains unconfigured zero address.

---
 drivers/usb/host/xhci-pci.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/usb/host/xhci-pci.c +11 -2
index 2b445f21b5..7f5be95f6c 100644
@@ -13,7 +13,7 @@
 #include <usb.h>
 #include <usb/xhci.h>
 
-static void xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
+static int xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
 			  struct xhci_hcor **ret_hcor)
 {
 	struct xhci_hccr *hccr;
@@ -22,6 +22,11 @@ static void xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
 
 	hccr = (struct xhci_hccr *)dm_pci_map_bar(dev,
 			PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
+	if (!hccr) {
+		printf("XHCI-PCI init cannot map PCI mem bar\n");
+		return -EIO;
+	}
+
 	hcor = (struct xhci_hcor *)((uintptr_t) hccr +
 			HC_LENGTH(xhci_readl(&hccr->cr_capbase)));
 
@@ -35,14 +40,18 @@ static void xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
 	dm_pci_read_config32(dev, PCI_COMMAND, &cmd);
 	cmd |= PCI_COMMAND_MASTER;
 	dm_pci_write_config32(dev, PCI_COMMAND, cmd);
+	return 0;
 }
 
 static int xhci_pci_probe(struct udevice *dev)
 {
 	struct xhci_hccr *hccr;
 	struct xhci_hcor *hcor;
+	int ret;
 
-	xhci_pci_init(dev, &hccr, &hcor);
+	ret = xhci_pci_init(dev, &hccr, &hcor);
+	if (ret)
+		return ret;
 
 	return xhci_register(dev, hccr, hcor);
 }
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/1 usb: xhci-pci: Check for errors from dm_pci_map_bar()

Quoted 17 lines · Pali Roh?r

Function dm_pci_map_bar() may fail and returns NULL. Check this to prevent dereferencing a NULL pointer.

In xhci-pci this may happen when board does not enable CONFIG_PCI_PNP and PCI_BASE_ADDRESS_0 contains unconfigured zero address.

---
 drivers/usb/host/xhci-pci.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/usb/host/xhci-pci.c +0 -0
index 2b445f21b5..7f5be95f6c 100644

drivers/usb/host/xhci-pci.c line 17

Quoted 6 lines
drivers/usb/host/xhci-pci.c +1 -1
@@ -13,7 +13,7 @@
 #include <usb.h>
 #include <usb/xhci.h>
 
-static void xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
+static int xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
                          struct xhci_hcor **ret_hcor)

nits: please make "struct xhci_hcor" align to "struct udevice" above

drivers/usb/host/xhci-pci.c static void xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr, · line 26

Quoted 8 lines
drivers/usb/host/xhci-pci.c +2 -0
 {
        struct xhci_hccr *hccr;
@@ -22,6 +22,11 @@ static void xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
 
        hccr = (struct xhci_hccr *)dm_pci_map_bar(dev,
                        PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
+       if (!hccr) {
+               printf("XHCI-PCI init cannot map PCI mem bar\n");

nits: use lower case "xhci-pci"

Quoted 16 lines
drivers/usb/host/xhci-pci.c +4 -0
+               return -EIO;
+       }
+
        hcor = (struct xhci_hcor *)((uintptr_t) hccr +
                        HC_LENGTH(xhci_readl(&hccr->cr_capbase)));
 
@@ -35,14 +40,18 @@ static void xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
        dm_pci_read_config32(dev, PCI_COMMAND, &cmd);
        cmd |= PCI_COMMAND_MASTER;
        dm_pci_write_config32(dev, PCI_COMMAND, cmd);
+       return 0;
 }
 
 static int xhci_pci_probe(struct udevice *dev)
 {
        struct xhci_hccr *hccr;

drivers/usb/host/xhci-pci.c static void xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr, · line 57

Quoted 10 lines
drivers/usb/host/xhci-pci.c +4 -1
        struct xhci_hcor *hcor;
+       int ret;
 
-       xhci_pci_init(dev, &hccr, &hcor);
+       ret = xhci_pci_init(dev, &hccr, &hcor);
+       if (ret)
+               return ret;
 
        return xhci_register(dev, hccr, hcor);
 }

Other than above,

#2 lore raw

PATCH v2 1/1 usb: xhci-pci: Check for errors from dm_pci_map_bar()

Function dm_pci_map_bar() may fail and returns NULL. Check this to prevent dereferencing a NULL pointer.

In xhci-pci this may happen when board does not enable CONFIG_PCI_PNP and PCI_BASE_ADDRESS_0 contains unconfigured zero address.

---
 drivers/usb/host/xhci-pci.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/usb/host/xhci-pci.c +12 -3
index 2b445f21b5..6c5024d3f1 100644
@@ -13,8 +13,8 @@
 #include <usb.h>
 #include <usb/xhci.h>
 
-static void xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
-			  struct xhci_hcor **ret_hcor)
+static int xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
+			 struct xhci_hcor **ret_hcor)
 {
 	struct xhci_hccr *hccr;
 	struct xhci_hcor *hcor;
@@ -22,6 +22,11 @@ static void xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
 
 	hccr = (struct xhci_hccr *)dm_pci_map_bar(dev,
 			PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
+	if (!hccr) {
+		printf("xhci-pci init cannot map PCI mem bar\n");
+		return -EIO;
+	}
+
 	hcor = (struct xhci_hcor *)((uintptr_t) hccr +
 			HC_LENGTH(xhci_readl(&hccr->cr_capbase)));
 
@@ -35,14 +40,18 @@ static void xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
 	dm_pci_read_config32(dev, PCI_COMMAND, &cmd);
 	cmd |= PCI_COMMAND_MASTER;
 	dm_pci_write_config32(dev, PCI_COMMAND, cmd);
+	return 0;
 }
 
 static int xhci_pci_probe(struct udevice *dev)
 {
 	struct xhci_hccr *hccr;
 	struct xhci_hcor *hcor;
+	int ret;
 
-	xhci_pci_init(dev, &hccr, &hcor);
+	ret = xhci_pci_init(dev, &hccr, &hcor);
+	if (ret)
+		return ret;
 
 	return xhci_register(dev, hccr, hcor);
 }
Signature · 1 line
2.20.1
#3 lore raw

PATCH v2 1/1 usb: xhci-pci: Check for errors from dm_pci_map_bar()

Quoted 4 lines · Pali Roh?r

Function dm_pci_map_bar() may fail and returns NULL. Check this to prevent dereferencing a NULL pointer.

Quoted 8 lines

In xhci-pci this may happen when board does not enable CONFIG_PCI_PNP and PCI_BASE_ADDRESS_0 contains unconfigured zero address.

---
 drivers/usb/host/xhci-pci.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
Applied
#4 lore raw

PATCH v2 1/1 usb: xhci-pci: Check for errors from dm_pci_map_bar()

In reply to Pali Rohár
Quoted 5 lines · Pali Roh?r

Function dm_pci_map_bar() may fail and returns NULL. Check this to prevent dereferencing a NULL pointer.

In xhci-pci this may happen when board does not enable CONFIG_PCI_PNP and PCI_BASE_ADDRESS_0 contains unconfigured zero address.

Applied, thanks

#6 lore raw

PATCH 1/1 usb: xhci-pci: Check for errors from dm_pci_map_bar()

In reply to Pali Rohár
Quoted 7 lines · Pali Roh?r

Function dm_pci_map_bar() may fail and returns NULL. Check this to prevent dereferencing a NULL pointer.

In xhci-pci this may happen when board does not enable CONFIG_PCI_PNP and PCI_BASE_ADDRESS_0 contains unconfigured zero address.

Thanks,
Stefan

Quoted 40 lines
---
  drivers/usb/host/xhci-pci.c | 13 +++++++++++--
  1 file changed, 11 insertions(+), 2 deletions(-)
drivers/usb/host/xhci-pci.c +7 -1
index 2b445f21b5..7f5be95f6c 100644
@@ -13,7 +13,7 @@
  #include <usb.h>
  #include <usb/xhci.h>
 
-static void xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
+static int xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
  			  struct xhci_hcor **ret_hcor)
  {
  	struct xhci_hccr *hccr;
@@ -22,6 +22,11 @@ static void xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
 
  	hccr = (struct xhci_hccr *)dm_pci_map_bar(dev,
  			PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
+	if (!hccr) {
+		printf("XHCI-PCI init cannot map PCI mem bar\n");
+		return -EIO;
+	}
+
  	hcor = (struct xhci_hcor *)((uintptr_t) hccr +
  			HC_LENGTH(xhci_readl(&hccr->cr_capbase)));
 
@@ -35,14 +40,18 @@ static void xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr,
  	dm_pci_read_config32(dev, PCI_COMMAND, &cmd);
  	cmd |= PCI_COMMAND_MASTER;
  	dm_pci_write_config32(dev, PCI_COMMAND, cmd);
+	return 0;
  }
 
  static int xhci_pci_probe(struct udevice *dev)
  {
  	struct xhci_hccr *hccr;

drivers/usb/host/xhci-pci.c static void xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr, · line 57

Quoted 10 lines
drivers/usb/host/xhci-pci.c +4 -1
  	struct xhci_hcor *hcor;
+	int ret;
 
-	xhci_pci_init(dev, &hccr, &hcor);
+	ret = xhci_pci_init(dev, &hccr, &hcor);
+	if (ret)
+		return ret;
 
  	return xhci_register(dev, hccr, hcor);
  }

Viele Gr??e,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr at denx.de
#5 lore raw

PATCH v2 1/1 usb: xhci-pci: Check for errors from dm_pci_map_bar()

In reply to Pali Rohár
Quoted 7 lines · Pali Roh?r

Function dm_pci_map_bar() may fail and returns NULL. Check this to prevent dereferencing a NULL pointer.

In xhci-pci this may happen when board does not enable CONFIG_PCI_PNP and PCI_BASE_ADDRESS_0 contains unconfigured zero address.

Thanks,
Stefan