diff options
| author | Simon Glass <[email protected]> | 2020-07-07 13:12:10 -0600 |
|---|---|---|
| committer | Bin Meng <[email protected]> | 2020-07-17 14:32:24 +0800 |
| commit | 20349781a3ca833c67126888ddfce7c1517c772e (patch) | |
| tree | eb84642875edd981bf865836d8f69ca61e4ede42 | |
| parent | 58a6ccd34e79acb7835699eab9c1ecb6cc8bbd47 (diff) | |
pci: Avoid a crash in device_is_on_pci_bus()
This function cannot currently be called on the root node. Add a check
for this as well as a test.
Signed-off-by: Simon Glass <[email protected]>
Reviewed-by: Wolfgang Wallner <[email protected]>
Reviewed-by: Bin Meng <[email protected]>
| -rw-r--r-- | include/dm/device.h | 2 | ||||
| -rw-r--r-- | test/dm/pci.c | 14 |
2 files changed, 15 insertions, 1 deletions
diff --git a/include/dm/device.h b/include/dm/device.h index f5738a0cee9..953706cf525 100644 --- a/include/dm/device.h +++ b/include/dm/device.h @@ -764,7 +764,7 @@ int dev_enable_by_path(const char *path); */ static inline bool device_is_on_pci_bus(const struct udevice *dev) { - return device_get_uclass_id(dev->parent) == UCLASS_PCI; + return dev->parent && device_get_uclass_id(dev->parent) == UCLASS_PCI; } /** diff --git a/test/dm/pci.c b/test/dm/pci.c index fb93e4c78ae..39e82b3699f 100644 --- a/test/dm/pci.c +++ b/test/dm/pci.c @@ -339,3 +339,17 @@ static int dm_test_pci_addr_live(struct unit_test_state *uts) } DM_TEST(dm_test_pci_addr_live, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_LIVE_TREE); + +/* Test device_is_on_pci_bus() */ +static int dm_test_pci_on_bus(struct unit_test_state *uts) +{ + struct udevice *dev; + + ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &dev)); + ut_asserteq(true, device_is_on_pci_bus(dev)); + ut_asserteq(false, device_is_on_pci_bus(dev_get_parent(dev))); + ut_asserteq(true, device_is_on_pci_bus(dev)); + + return 0; +} +DM_TEST(dm_test_pci_on_bus, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
