lspci.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* lspci command: enumerate PCI devices (vendor:device, class, BAR0). */
  2. #include "io.h"
  3. #include "pci.h"
  4. static void put_hex2(unsigned int v)
  5. {
  6. static const char hex[] = "0123456789ABCDEF";
  7. io_putchar(hex[(v >> 4) & 0xF]);
  8. io_putchar(hex[v & 0xF]);
  9. }
  10. static void put_hex4(unsigned int v)
  11. {
  12. put_hex2((v >> 8) & 0xFF);
  13. put_hex2(v & 0xFF);
  14. }
  15. static void put_hex8(unsigned int v)
  16. {
  17. put_hex4((v >> 16) & 0xFFFF);
  18. put_hex4(v & 0xFFFF);
  19. }
  20. void program_lspci_main(void)
  21. {
  22. unsigned int bus, dev;
  23. for (bus = 0; bus < 256; bus++) {
  24. for (dev = 0; dev < 32; dev++) {
  25. unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
  26. unsigned int class_rev, bar0;
  27. unsigned int vendor, device, cls, sub;
  28. if (id == 0xFFFFFFFFu || id == 0)
  29. continue;
  30. vendor = id & 0xFFFFu;
  31. device = (id >> 16) & 0xFFFFu;
  32. class_rev = pci_cfg_read32(bus, dev, 0, 0x08);
  33. cls = (class_rev >> 24) & 0xFFu;
  34. sub = (class_rev >> 16) & 0xFFu;
  35. bar0 = pci_cfg_read32(bus, dev, 0, 0x10);
  36. put_hex2(bus);
  37. io_putchar(':');
  38. put_hex2(dev);
  39. io_putstr(" ");
  40. put_hex4(vendor);
  41. io_putchar(':');
  42. put_hex4(device);
  43. io_putstr(" class=");
  44. put_hex2(cls);
  45. put_hex2(sub);
  46. io_putstr(" bar0=");
  47. put_hex8(bar0);
  48. io_putchar('\n');
  49. }
  50. }
  51. }