3

I need to get Vendor ID and Device ID of all PCI bus Devices from Linux using C/C++ (inline asm allowed), but I can't even understand from what to start.

Please, give me some pieces of advice or code parts.

Andrei Shpakovskiy
  • 138
  • 1
  • 2
  • 7
  • looking at https://stackoverflow.com/questions/25908782/in-linux-is-there-a-way-to-find-out-which-pci-card-is-plugged-into-which-pci-sl, couldn't lspci give you these info? Or isn't it already in a folder like /dev? – B. Go Nov 23 '19 at 18:28
  • Sure, lspci gives me this information, but main idea is to get it by myself. – Andrei Shpakovskiy Nov 23 '19 at 18:34
  • 1
    either find the code of lspci, as it's open source, or parse its outputs... – B. Go Nov 23 '19 at 18:35
  • To get it yourself, iterate over the contents of `/sys/bus/pci/devices` (or `/proc/bus/pci`). – larsks Nov 23 '19 at 18:41
  • 1
    The [busybox lspci.c](https://github.com/brgl/busybox/blob/master/util-linux/lspci.c) is short enough that we could even post it here. – KamilCuk Nov 23 '19 at 18:49

1 Answers1

3

How to get Vendor ID and Device ID of all PCI Devices?

In short, you have to write a C program that does:

grep PCI_ID /sys/bus/pci/devices/*/uevent

And extract relevant data after = and after :.

So what you have to do is:

  • iterate over directories in /sys/bus/pci/devices with readdir_r
  • for each directory
    • open the uevent file from inside that directory
    • read lines from the file until PCI_ID is found
    • if found
      • basically match the line with sscanf(line, "PCI_ID=%4x:%4x\n", &vendor_id, &device_id)

I couldn't find any documentation about uevent inside /sys/bus/pci/devices. This answer is based on reverse engineering busybox lspci.c sources.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    Do I correctly understand, that there are some files, where I can get some additional information about device's header type, I/O Register values, ROM Registers, Class code, etc.? – Andrei Shpakovskiy Nov 23 '19 at 19:14