iorw.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* memrw.c - command to read / write physical memory */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 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/dl.h>
  20. #include <grub/misc.h>
  21. #include <grub/extcmd.h>
  22. #include <grub/env.h>
  23. #include <grub/cpu/io.h>
  24. #include <grub/i18n.h>
  25. #include <grub/lockdown.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. static grub_extcmd_t cmd_read_byte, cmd_read_word, cmd_read_dword;
  28. static grub_command_t cmd_write_byte, cmd_write_word, cmd_write_dword;
  29. static const struct grub_arg_option options[] =
  30. {
  31. {0, 'v', 0, N_("Save read value into variable VARNAME."),
  32. N_("VARNAME"), ARG_TYPE_STRING},
  33. {0, 0, 0, 0, 0, 0}
  34. };
  35. static grub_err_t
  36. grub_cmd_read (grub_extcmd_context_t ctxt, int argc, char **argv)
  37. {
  38. grub_port_t addr;
  39. grub_uint32_t value = 0;
  40. if (argc != 1)
  41. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
  42. addr = grub_strtoul (argv[0], 0, 0);
  43. switch (ctxt->extcmd->cmd->name[sizeof ("in") - 1])
  44. {
  45. case 'l':
  46. value = grub_inl (addr);
  47. break;
  48. case 'w':
  49. value = grub_inw (addr);
  50. break;
  51. case 'b':
  52. value = grub_inb (addr);
  53. break;
  54. }
  55. if (ctxt->state[0].set)
  56. {
  57. char buf[sizeof ("XXXXXXXX")];
  58. grub_snprintf (buf, sizeof (buf), "%x", value);
  59. grub_env_set (ctxt->state[0].arg, buf);
  60. }
  61. else
  62. grub_printf ("0x%x\n", value);
  63. return 0;
  64. }
  65. static grub_err_t
  66. grub_cmd_write (grub_command_t cmd, int argc, char **argv)
  67. {
  68. grub_port_t addr;
  69. grub_uint32_t value;
  70. grub_uint32_t mask = 0xffffffff;
  71. if (argc != 2 && argc != 3)
  72. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
  73. addr = grub_strtoul (argv[0], 0, 0);
  74. value = grub_strtoul (argv[1], 0, 0);
  75. if (argc == 3)
  76. mask = grub_strtoul (argv[2], 0, 0);
  77. value &= mask;
  78. switch (cmd->name[sizeof ("out") - 1])
  79. {
  80. case 'l':
  81. if (mask != 0xffffffff)
  82. grub_outl ((grub_inl (addr) & ~mask) | value, addr);
  83. else
  84. grub_outl (value, addr);
  85. break;
  86. case 'w':
  87. if ((mask & 0xffff) != 0xffff)
  88. grub_outw ((grub_inw (addr) & ~mask) | value, addr);
  89. else
  90. grub_outw (value, addr);
  91. break;
  92. case 'b':
  93. if ((mask & 0xff) != 0xff)
  94. grub_outb ((grub_inb (addr) & ~mask) | value, addr);
  95. else
  96. grub_outb (value, addr);
  97. break;
  98. }
  99. return 0;
  100. }
  101. GRUB_MOD_INIT(memrw)
  102. {
  103. cmd_read_byte =
  104. grub_register_extcmd ("inb", grub_cmd_read, 0,
  105. N_("PORT"), N_("Read 8-bit value from PORT."),
  106. options);
  107. cmd_read_word =
  108. grub_register_extcmd ("inw", grub_cmd_read, 0,
  109. N_("PORT"), N_("Read 16-bit value from PORT."),
  110. options);
  111. cmd_read_dword =
  112. grub_register_extcmd ("inl", grub_cmd_read, 0,
  113. N_("PORT"), N_("Read 32-bit value from PORT."),
  114. options);
  115. cmd_write_byte =
  116. grub_register_command_lockdown ("outb", grub_cmd_write,
  117. N_("PORT VALUE [MASK]"),
  118. N_("Write 8-bit VALUE to PORT."));
  119. cmd_write_word =
  120. grub_register_command_lockdown ("outw", grub_cmd_write,
  121. N_("PORT VALUE [MASK]"),
  122. N_("Write 16-bit VALUE to PORT."));
  123. cmd_write_dword =
  124. grub_register_command_lockdown ("outl", grub_cmd_write,
  125. N_("ADDR VALUE [MASK]"),
  126. N_("Write 32-bit VALUE to PORT."));
  127. }
  128. GRUB_MOD_FINI(memrw)
  129. {
  130. grub_unregister_extcmd (cmd_read_byte);
  131. grub_unregister_extcmd (cmd_read_word);
  132. grub_unregister_extcmd (cmd_read_dword);
  133. grub_unregister_command (cmd_write_byte);
  134. grub_unregister_command (cmd_write_word);
  135. grub_unregister_command (cmd_write_dword);
  136. }