blob: feb5c60ddd731522125bfaac5eeedc336610bae8 [file] [log] [blame]
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -07001/*
2 * Copyright 2019 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "pci.hpp"
18
Patrick Venture9b37b092020-05-28 20:58:57 -070019extern "C"
20{
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070021#include <pci/pci.h>
22} // extern "C"
23
Patrick Venture24141612019-05-03 17:59:18 -070024#include <cstring>
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070025#include <optional>
26#include <vector>
27
28namespace host_tool
29{
30
31std::vector<PciDevice>
32 PciUtilImpl::getPciDevices(std::optional<PciFilter> filter)
33{
34 PciFilter test;
35 bool check = false;
36 std::vector<PciDevice> results;
37
38 if (filter.has_value())
39 {
40 test = filter.value();
41 check = true;
42 }
43
44 pci_scan_bus(pacc);
45 struct pci_dev* dev;
46
47 for (dev = pacc->devices; dev; dev = dev->next)
48 {
49 PciDevice item;
50
51 pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS);
52
53 item.bus = dev->bus;
54 item.dev = dev->dev;
55 item.func = dev->func;
56 item.vid = dev->vendor_id;
57 item.did = dev->device_id;
Patrick Venture24141612019-05-03 17:59:18 -070058 std::memcpy(item.bars, dev->base_addr, sizeof(dev->base_addr));
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070059
60 if (check)
61 {
62 if (test.vid == dev->vendor_id && test.did == dev->device_id)
63 {
64 results.push_back(item);
65 }
66 }
67 else
68 {
69 results.push_back(item);
70 }
71 }
72
73 return results;
74}
75
76} // namespace host_tool