lspci.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. struct grub_pci_classname
  25. {
  26. int class;
  27. int subclass;
  28. char *desc;
  29. };
  30. static const struct grub_pci_classname grub_pci_classes[] =
  31. {
  32. { 0, 0, "" },
  33. { 1, 0, "SCSI Controller" },
  34. { 1, 1, "IDE Controller" },
  35. { 1, 2, "Floppy Controller" },
  36. { 1, 3, "IPI Controller" },
  37. { 1, 4, "RAID Controller" },
  38. { 1, 6, "SATA Controller" },
  39. { 1, 0x80, "Mass storage Controller" },
  40. { 2, 0, "Ethernet Controller" },
  41. { 2, 1, "Token Ring Controller" },
  42. { 2, 2, "FDDI Controller" },
  43. { 2, 3, "ATM Controller" },
  44. { 2, 4, "ISDN Controller" },
  45. { 2, 0x80, "Network controller" },
  46. { 3, 0, "VGA Controller" },
  47. { 3, 1, "XGA Controller" },
  48. { 3, 2, "3D Controller" },
  49. { 3, 0x80, "Display Controller" },
  50. { 4, 0, "Multimedia Video Device" },
  51. { 4, 1, "Multimedia Audio Device" },
  52. { 4, 2, "Multimedia Telephony Device" },
  53. { 4, 0x80, "Multimedia device" },
  54. { 5, 0, "RAM Controller" },
  55. { 5, 1, "Flash Memory Controller" },
  56. { 5, 0x80, "Memory Controller" },
  57. { 6, 0, "Host Bridge" },
  58. { 6, 1, "ISA Bridge" },
  59. { 6, 2, "EISA Bride" },
  60. { 6, 3, "MCA Bridge" },
  61. { 6, 4, "PCI-PCI Bridge" },
  62. { 6, 5, "PCMCIA Bridge" },
  63. { 6, 6, "NuBus Bridge" },
  64. { 6, 7, "CardBus Bridge" },
  65. { 6, 8, "Raceway Bridge" },
  66. { 6, 0x80, "Unknown Bridge" },
  67. { 7, 0x80, "Communication controller" },
  68. { 8, 0x80, "System hardware" },
  69. { 9, 0, "Keyboard Controller" },
  70. { 9, 1, "Digitizer" },
  71. { 9, 2, "Mouse Controller" },
  72. { 9, 3, "Scanner Controller" },
  73. { 9, 4, "Gameport Controller" },
  74. { 9, 0x80, "Unknown Input Device" },
  75. { 10, 0, "Generic Docking Station" },
  76. { 10, 0x80, "Unknown Docking Station" },
  77. { 11, 0, "80386 Processor" },
  78. { 11, 1, "80486 Processor" },
  79. { 11, 2, "Pentium Processor" },
  80. { 11, 0x10, "Alpha Processor" },
  81. { 11, 0x20, "PowerPC Processor" },
  82. { 11, 0x30, "MIPS Processor" },
  83. { 11, 0x40, "Co-Processor" },
  84. { 11, 0x80, "Unknown Processor" },
  85. { 12, 3, "USB Controller" },
  86. { 12, 0x80, "Serial Bus Controller" },
  87. { 13, 0x80, "Wireless Controller" },
  88. { 14, 0, "I2O" },
  89. { 15, 0, "IrDA Controller" },
  90. { 15, 1, "Consumer IR" },
  91. { 15, 0x10, "RF-Controller" },
  92. { 15, 0x80, "Satellite Communication Controller" },
  93. { 16, 0, "Network Decryption" },
  94. { 16, 1, "Entertainment Decryption" },
  95. { 16, 0x80, "Unknown Decryption Controller" },
  96. { 17, 0, "Digital IO Module" },
  97. { 17, 0x80, "Unknown Data Input System" },
  98. { 0, 0, 0 },
  99. };
  100. static const char *
  101. grub_pci_get_class (int class, int subclass)
  102. {
  103. const struct grub_pci_classname *curr = grub_pci_classes;
  104. while (curr->desc)
  105. {
  106. if (curr->class == class && curr->subclass == subclass)
  107. return curr->desc;
  108. curr++;
  109. }
  110. return 0;
  111. }
  112. static const struct grub_arg_option options[] =
  113. {
  114. {"iospace", 'i', 0, "show I/O spaces", 0, 0},
  115. {0, 0, 0, 0, 0, 0}
  116. };
  117. static int
  118. grub_lspci_iter (grub_pci_device_t dev, grub_pci_id_t pciid, void *closure)
  119. {
  120. int *iospace = closure;
  121. grub_uint32_t class;
  122. const char *sclass;
  123. grub_pci_address_t addr;
  124. int reg;
  125. grub_printf ("%02x:%02x.%x %04x:%04x", grub_pci_get_bus (dev),
  126. grub_pci_get_device (dev), grub_pci_get_function (dev),
  127. pciid & 0xFFFF, pciid >> 16);
  128. addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
  129. class = grub_pci_read (addr);
  130. /* Lookup the class name, if there isn't a specific one,
  131. retry with 0x80 to get the generic class name. */
  132. sclass = grub_pci_get_class (class >> 24, (class >> 16) & 0xFF);
  133. if (! sclass)
  134. sclass = grub_pci_get_class (class >> 24, 0x80);
  135. if (! sclass)
  136. sclass = "";
  137. grub_printf (" [%04x] %s", (class >> 16) & 0xffff, sclass);
  138. grub_uint8_t pi = (class >> 8) & 0xff;
  139. if (pi)
  140. grub_printf (" [PI %02x]", pi);
  141. grub_printf ("\n");
  142. if (*iospace)
  143. {
  144. reg = GRUB_PCI_REG_ADDRESSES;
  145. while (reg < GRUB_PCI_REG_CIS_POINTER)
  146. {
  147. grub_uint64_t space;
  148. addr = grub_pci_make_address (dev, reg);
  149. space = grub_pci_read (addr);
  150. reg += sizeof (grub_uint32_t);
  151. if (space == 0)
  152. continue;
  153. switch (space & GRUB_PCI_ADDR_SPACE_MASK)
  154. {
  155. case GRUB_PCI_ADDR_SPACE_IO:
  156. grub_printf ("\tIO space %d at 0x%llx\n",
  157. (unsigned) ((reg - GRUB_PCI_REG_ADDRESSES)
  158. / sizeof (grub_uint32_t)) - 1,
  159. (unsigned long long)
  160. (space & GRUB_PCI_ADDR_IO_MASK));
  161. break;
  162. case GRUB_PCI_ADDR_SPACE_MEMORY:
  163. if ((space & GRUB_PCI_ADDR_MEM_TYPE_MASK)
  164. == GRUB_PCI_ADDR_MEM_TYPE_64)
  165. {
  166. addr = grub_pci_make_address (dev, reg);
  167. space |= ((grub_uint64_t) grub_pci_read (addr)) << 32;
  168. reg += sizeof (grub_uint32_t);
  169. grub_printf ("\t64-bit memory space %d at 0x%016llx [%s]\n",
  170. (unsigned) ((reg - GRUB_PCI_REG_ADDRESSES)
  171. / sizeof (grub_uint32_t)) - 2,
  172. (unsigned long long)
  173. (space & GRUB_PCI_ADDR_MEM_MASK),
  174. space & GRUB_PCI_ADDR_MEM_PREFETCH
  175. ? "prefetchable" : "non-prefetchable");
  176. }
  177. else
  178. grub_printf ("\t32-bit memory space %d at 0x%016llx [%s]\n",
  179. (unsigned) ((reg - GRUB_PCI_REG_ADDRESSES)
  180. / sizeof (grub_uint32_t)) - 1,
  181. (unsigned long long)
  182. (space & GRUB_PCI_ADDR_MEM_MASK),
  183. space & GRUB_PCI_ADDR_MEM_PREFETCH
  184. ? "prefetchable" : "non-prefetchable");
  185. break;
  186. }
  187. }
  188. }
  189. return 0;
  190. }
  191. static grub_err_t
  192. grub_cmd_lspci (grub_extcmd_t cmd,
  193. int argc __attribute__ ((unused)),
  194. char **args __attribute__ ((unused)))
  195. {
  196. static int iospace;
  197. iospace = cmd->state[0].set;
  198. grub_pci_iterate (grub_lspci_iter, &iospace);
  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, GRUB_COMMAND_FLAG_BOTH,
  205. "[-i]", N_("List PCI devices."), options);
  206. }
  207. GRUB_MOD_FINI(lspci)
  208. {
  209. grub_unregister_extcmd (cmd);
  210. }