lspci.c 6.7 KB

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