pcidump.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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, &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, &ptr, 16) & 0xffff) << 16;
  99. if (grub_errno == GRUB_ERR_BAD_NUMBER)
  100. grub_errno = GRUB_ERR_NONE;
  101. else
  102. ctx.pciid_check_mask |= 0xffff0000;
  103. }
  104. ctx.pciid_check_value &= ctx.pciid_check_mask;
  105. if (ctxt->state[1].set)
  106. {
  107. const char *optr;
  108. ptr = ctxt->state[1].arg;
  109. optr = ptr;
  110. ctx.bus = grub_strtoul (ptr, &ptr, 16);
  111. if (grub_errno == GRUB_ERR_BAD_NUMBER)
  112. {
  113. grub_errno = GRUB_ERR_NONE;
  114. ptr = optr;
  115. }
  116. else
  117. ctx.check_bus = 1;
  118. if (grub_errno)
  119. return grub_errno;
  120. if (*ptr != ':')
  121. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing `%c' symbol"), ':');
  122. ptr++;
  123. optr = ptr;
  124. ctx.device = grub_strtoul (ptr, &ptr, 16);
  125. if (grub_errno == GRUB_ERR_BAD_NUMBER)
  126. {
  127. grub_errno = GRUB_ERR_NONE;
  128. ptr = optr;
  129. }
  130. else
  131. ctx.check_device = 1;
  132. if (*ptr == '.')
  133. {
  134. ptr++;
  135. ctx.function = grub_strtoul (ptr, &ptr, 16);
  136. if (grub_errno)
  137. return grub_errno;
  138. ctx.check_function = 1;
  139. }
  140. }
  141. grub_pci_iterate (grub_pcidump_iter, &ctx);
  142. return GRUB_ERR_NONE;
  143. }
  144. static grub_extcmd_t cmd;
  145. GRUB_MOD_INIT(pcidump)
  146. {
  147. cmd = grub_register_extcmd ("pcidump", grub_cmd_pcidump, 0,
  148. N_("[-s POSITION] [-d DEVICE]"),
  149. N_("Show raw dump of the PCI configuration space."), options);
  150. }
  151. GRUB_MOD_FINI(pcidump)
  152. {
  153. grub_unregister_extcmd (cmd);
  154. }