diff mbox series

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

Message ID 20210118113004.16850-1-pali@kernel.org
State Accepted
Commit 5a5024fee073fea1b2f72601c4f3f006451fd176
Delegated to: Bin Meng
Headers show
Series [v2] usb: xhci-pci: Check for errors from dm_pci_map_bar() | expand

Commit Message

Pali Rohár Jan. 18, 2021, 11:30 a.m. UTC
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.

Signed-off-by: Pali Rohár <pali@kernel.org>
---
 drivers/usb/host/xhci-pci.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

Comments

Bin Meng Jan. 18, 2021, 11:44 a.m. UTC | #1
On Mon, Jan 18, 2021 at 7:30 PM Pali Rohár <pali@kernel.org> wrote:
>
> 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.
>
> Signed-off-by: Pali Rohár <pali@kernel.org>
> ---
>  drivers/usb/host/xhci-pci.c | 15 ++++++++++++---
>  1 file changed, 12 insertions(+), 3 deletions(-)
>

Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
Marek Vasut Jan. 18, 2021, 12:28 p.m. UTC | #2
On 1/18/21 12:30 PM, Pali Rohár wrote:
> 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
Stefan Roese Jan. 18, 2021, 1:27 p.m. UTC | #3
On 18.01.21 12:30, Pali Rohár wrote:
> 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.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>

Reviewed-by: Stefan Roese <sr@denx.de>

Thanks,
Stefan
diff mbox series

Patch

diff --git a/drivers/usb/host/xhci-pci.c b/drivers/usb/host/xhci-pci.c
index 2b445f21b5..6c5024d3f1 100644
--- a/drivers/usb/host/xhci-pci.c
+++ b/drivers/usb/host/xhci-pci.c
@@ -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);
 }