lspci.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* lspci.c - List PCI devices. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008, 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/pci.h>
  20. #include <grub/dl.h>
  21. #include <grub/misc.h>
  22. #include <grub/extcmd.h>
  23. #include <grub/i18n.h>
  24. GRUB_MOD_LICENSE ("GPLv3+");
  25. struct grub_pci_classname
  26. {
  27. int class;
  28. int subclass;
  29. char *desc;
  30. };
  31. static const struct grub_pci_classname grub_pci_classes[] =
  32. {
  33. { 0, 0, "" },
  34. { 1, 0, "SCSI Controller" },
  35. { 1, 1, "IDE Controller" },
  36. { 1, 2, "Floppy Controller" },
  37. { 1, 3, "IPI Controller" },
  38. { 1, 4, "RAID Controller" },
  39. { 1, 6, "SATA Controller" },
  40. { 1, 0x80, "Mass storage Controller" },
  41. { 2, 0, "Ethernet Controller" },
  42. { 2, 1, "Token Ring Controller" },
  43. { 2, 2, "FDDI Controller" },
  44. { 2, 3, "ATM Controller" },
  45. { 2, 4, "ISDN Controller" },
  46. { 2, 0x80, "Network controller" },
  47. { 3, 0, "VGA Controller" },
  48. { 3, 1, "XGA Controller" },
  49. { 3, 2, "3D Controller" },
  50. { 3, 0x80, "Display Controller" },
  51. { 4, 0, "Multimedia Video Device" },
  52. { 4, 1, "Multimedia Audio Device" },
  53. { 4, 2, "Multimedia Telephony Device" },
  54. { 4, 0x80, "Multimedia device" },
  55. { 5, 0, "RAM Controller" },
  56. { 5, 1, "Flash Memory Controller" },
  57. { 5, 0x80, "Memory Controller" },
  58. { 6, 0, "Host Bridge" },
  59. { 6, 1, "ISA Bridge" },
  60. { 6, 2, "EISA Bride" },
  61. { 6, 3, "MCA Bridge" },
  62. { 6, 4, "PCI-PCI Bridge" },
  63. { 6, 5, "PCMCIA Bridge" },
  64. { 6, 6, "NuBus Bridge" },
  65. { 6, 7, "CardBus Bridge" },
  66. { 6, 8, "Raceway Bridge" },
  67. { 6, 0x80, "Unknown Bridge" },
  68. { 7, 0x80, "Communication controller" },
  69. { 8, 0x80, "System hardware" },
  70. { 9, 0, "Keyboard Controller" },
  71. { 9, 1, "Digitizer" },
  72. { 9, 2, "Mouse Controller" },
  73. { 9, 3, "Scanner Controller" },
  74. { 9, 4, "Gameport Controller" },
  75. { 9, 0x80, "Unknown Input Device" },
  76. { 10, 0, "Generic Docking Station" },
  77. { 10, 0x80, "Unknown Docking Station" },
  78. { 11, 0, "80386 Processor" },
  79. { 11, 1, "80486 Processor" },
  80. { 11, 2, "Pentium Processor" },
  81. { 11, 0x10, "Alpha Processor" },
  82. { 11, 0x20, "PowerPC Processor" },
  83. { 11, 0x30, "MIPS Processor" },
  84. { 11, 0x40, "Co-Processor" },
  85. { 11, 0x80, "Unknown Processor" },
  86. { 12, 3, "USB Controller" },
  87. { 12, 0x80, "Serial Bus Controller" },
  88. { 13, 0x80, "Wireless Controller" },
  89. { 14, 0, "I2O" },
  90. { 15, 0, "IrDA Controller" },
  91. { 15, 1, "Consumer IR" },
  92. { 15, 0x10, "RF-Controller" },
  93. { 15, 0x80, "Satellite Communication Controller" },
  94. { 16, 0, "Network Decryption" },
  95. { 16, 1, "Entertainment Decryption" },
  96. { 16, 0x80, "Unknown Decryption Controller" },
  97. { 17, 0, "Digital IO Module" },
  98. { 17, 0x80, "Unknown Data Input System" },
  99. { 0, 0, 0 },
  100. };
  101. static const char *
  102. grub_pci_get_class (int class, int subclass)
  103. {
  104. const struct grub_pci_classname *curr = grub_pci_classes;
  105. while (curr->desc)
  106. {
  107. if (curr->class == class && curr->subclass == subclass)
  108. return curr->desc;
  109. curr++;
  110. }
  111. return 0;
  112. }
  113. static const struct grub_arg_option options[] =
  114. {
  115. {"iospace", 'i', 0, "show I/O spaces", 0, 0},
  116. {0, 0, 0, 0, 0, 0}
  117. };
  118. static int iospace;
  119. static int NESTED_FUNC_ATTR
  120. grub_lspci_iter (grub_pci_device_t dev, grub_pci_id_t pciid)
  121. {
  122. grub_uint32_t class;
  123. const char *sclass;
  124. grub_pci_address_t addr;
  125. int reg;
  126. grub_printf ("%02x:%02x.%x %04x:%04x", grub_pci_get_bus (dev),
  127. grub_pci_get_device (dev), grub_pci_get_function (dev),
  128. pciid & 0xFFFF, pciid >> 16);
  129. addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
  130. class = grub_pci_read (addr);
  131. /* Lookup the class name, if there isn't a specific one,
  132. retry with 0x80 to get the generic class name. */
  133. sclass = grub_pci_get_class (class >> 24, (class >> 16) & 0xFF);
  134. if (! sclass)
  135. sclass = grub_pci_get_class (class >> 24, 0x80);
  136. if (! sclass)
  137. sclass = "";
  138. grub_printf (" [%04x] %s", (class >> 16) & 0xffff, sclass);
  139. grub_uint8_t pi = (class >> 8) & 0xff;
  140. if (pi)
  141. grub_printf (" [PI %02x]", pi);
  142. grub_printf ("\n");
  143. if (iospace)
  144. {
  145. reg = GRUB_PCI_REG_ADDRESSES;
  146. while (reg < GRUB_PCI_REG_CIS_POINTER)
  147. {
  148. grub_uint64_t space;
  149. addr = grub_pci_make_address (dev, reg);
  150. space = grub_pci_read (addr);
  151. reg += sizeof (grub_uint32_t);
  152. if (space == 0)
  153. continue;
  154. switch (space & GRUB_PCI_ADDR_SPACE_MASK)
  155. {
  156. case GRUB_PCI_ADDR_SPACE_IO:
  157. grub_printf ("\tIO space %d at 0x%llx\n",
  158. (unsigned) ((reg - GRUB_PCI_REG_ADDRESSES)
  159. / sizeof (grub_uint32_t)) - 1,
  160. (unsigned long long)
  161. (space & GRUB_PCI_ADDR_IO_MASK));
  162. break;
  163. case GRUB_PCI_ADDR_SPACE_MEMORY:
  164. if ((space & GRUB_PCI_ADDR_MEM_TYPE_MASK)
  165. == GRUB_PCI_ADDR_MEM_TYPE_64)
  166. {
  167. addr = grub_pci_make_address (dev, reg);
  168. space |= ((grub_uint64_t) grub_pci_read (addr)) << 32;
  169. reg += sizeof (grub_uint32_t);
  170. grub_printf ("\t64-bit memory space %d at 0x%016llx [%s]\n",
  171. (unsigned) ((reg - GRUB_PCI_REG_ADDRESSES)
  172. / sizeof (grub_uint32_t)) - 2,
  173. (unsigned long long)
  174. (space & GRUB_PCI_ADDR_MEM_MASK),
  175. space & GRUB_PCI_ADDR_MEM_PREFETCH
  176. ? "prefetchable" : "non-prefetchable");
  177. }
  178. else
  179. grub_printf ("\t32-bit memory space %d at 0x%016llx [%s]\n",
  180. (unsigned) ((reg - GRUB_PCI_REG_ADDRESSES)
  181. / sizeof (grub_uint32_t)) - 1,
  182. (unsigned long long)
  183. (space & GRUB_PCI_ADDR_MEM_MASK),
  184. space & GRUB_PCI_ADDR_MEM_PREFETCH
  185. ? "prefetchable" : "non-prefetchable");
  186. break;
  187. }
  188. }
  189. }
  190. return 0;
  191. }
  192. static grub_err_t
  193. grub_cmd_lspci (grub_extcmd_context_t ctxt,
  194. int argc __attribute__ ((unused)),
  195. char **args __attribute__ ((unused)))
  196. {
  197. iospace = ctxt->state[0].set;
  198. grub_pci_iterate (grub_lspci_iter);
  199. return GRUB_ERR_NONE;
  200. }
  201. static grub_extcmd_t cmd;
  202. GRUB_MOD_INIT(lspci)
  203. {
  204. cmd = grub_register_extcmd ("lspci", grub_cmd_lspci, 0, "[-i]",
  205. N_("List PCI devices."), options);
  206. }
  207. GRUB_MOD_FINI(lspci)
  208. {
  209. grub_unregister_extcmd (cmd);
  210. }