pcidump.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* lspci.c - List PCI devices. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2013 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/env.h>
  24. #include <grub/mm.h>
  25. #include <grub/i18n.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. struct iter_cxt
  28. {
  29. grub_uint32_t pciid_check_mask, pciid_check_value;
  30. int bus, device, function;
  31. int check_bus, check_device, check_function;
  32. };
  33. static const struct grub_arg_option options[] =
  34. {
  35. {0, 'd', 0, N_("Select device by vendor and device IDs."),
  36. N_("[vendor]:[device]"), ARG_TYPE_STRING},
  37. {0, 's', 0, N_("Select device by its position on the bus."),
  38. N_("[bus]:[slot][.func]"), ARG_TYPE_STRING},
  39. {0, 0, 0, 0, 0, 0}
  40. };
  41. static int
  42. grub_pcidump_iter (grub_pci_device_t dev, grub_pci_id_t pciid,
  43. void *data)
  44. {
  45. struct iter_cxt *ctx = data;
  46. grub_pci_address_t addr;
  47. int i;
  48. if ((pciid & ctx->pciid_check_mask) != ctx->pciid_check_value)
  49. return 0;
  50. if (ctx->check_bus && grub_pci_get_bus (dev) != ctx->bus)
  51. return 0;
  52. if (ctx->check_device && grub_pci_get_device (dev) != ctx->device)
  53. return 0;
  54. if (ctx->check_function && grub_pci_get_function (dev) != ctx->function)
  55. return 0;
  56. for (i = 0; i < 256; i += 4)
  57. {
  58. addr = grub_pci_make_address (dev, i);
  59. grub_printf ("%08x ", grub_pci_read (addr));
  60. if ((i & 0xc) == 0xc)
  61. grub_printf ("\n");
  62. }
  63. return 0;
  64. }
  65. static grub_err_t
  66. grub_cmd_pcidump (grub_extcmd_context_t ctxt,
  67. int argc __attribute__ ((unused)),
  68. char **argv __attribute__ ((unused)))
  69. {
  70. const char *ptr;
  71. struct iter_cxt ctx =
  72. {
  73. .pciid_check_value = 0,
  74. .pciid_check_mask = 0,
  75. .check_bus = 0,
  76. .check_device = 0,
  77. .check_function = 0,
  78. .bus = 0,
  79. .function = 0,
  80. .device = 0
  81. };
  82. if (ctxt->state[0].set)
  83. {
  84. ptr = ctxt->state[0].arg;
  85. ctx.pciid_check_value |= (grub_strtoul (ptr, (char **) &ptr, 16) & 0xffff);
  86. if (grub_errno == GRUB_ERR_BAD_NUMBER)
  87. {
  88. grub_errno = GRUB_ERR_NONE;
  89. ptr = ctxt->state[0].arg;
  90. }
  91. else
  92. ctx.pciid_check_mask |= 0xffff;
  93. if (grub_errno)
  94. return grub_errno;
  95. if (*ptr != ':')
  96. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing `%c' symbol"), ':');
  97. ptr++;
  98. ctx.pciid_check_value |= (grub_strtoul (ptr, (char **) &ptr, 16) & 0xffff)
  99. << 16;
  100. if (grub_errno == GRUB_ERR_BAD_NUMBER)
  101. grub_errno = GRUB_ERR_NONE;
  102. else
  103. ctx.pciid_check_mask |= 0xffff0000;
  104. }
  105. ctx.pciid_check_value &= ctx.pciid_check_mask;
  106. if (ctxt->state[1].set)
  107. {
  108. const char *optr;
  109. ptr = ctxt->state[1].arg;
  110. optr = ptr;
  111. ctx.bus = grub_strtoul (ptr, (char **) &ptr, 16);
  112. if (grub_errno == GRUB_ERR_BAD_NUMBER)
  113. {
  114. grub_errno = GRUB_ERR_NONE;
  115. ptr = optr;
  116. }
  117. else
  118. ctx.check_bus = 1;
  119. if (grub_errno)
  120. return grub_errno;
  121. if (*ptr != ':')
  122. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing `%c' symbol"), ':');
  123. ptr++;
  124. optr = ptr;
  125. ctx.device = grub_strtoul (ptr, (char **) &ptr, 16);
  126. if (grub_errno == GRUB_ERR_BAD_NUMBER)
  127. {
  128. grub_errno = GRUB_ERR_NONE;
  129. ptr = optr;
  130. }
  131. else
  132. ctx.check_device = 1;
  133. if (*ptr == '.')
  134. {
  135. ptr++;
  136. ctx.function = grub_strtoul (ptr, (char **) &ptr, 16);
  137. if (grub_errno)
  138. return grub_errno;
  139. ctx.check_function = 1;
  140. }
  141. }
  142. grub_pci_iterate (grub_pcidump_iter, &ctx);
  143. return GRUB_ERR_NONE;
  144. }
  145. static grub_extcmd_t cmd;
  146. GRUB_MOD_INIT(pcidump)
  147. {
  148. cmd = grub_register_extcmd ("pcidump", grub_cmd_pcidump, 0,
  149. N_("[-s POSITION] [-d DEVICE]"),
  150. N_("Show raw dump of the PCI configuration space."), options);
  151. }
  152. GRUB_MOD_FINI(pcidump)
  153. {
  154. grub_unregister_extcmd (cmd);
  155. }