iorw.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. static grub_extcmd_t cmd_read_byte, cmd_read_word, cmd_read_dword;
  26. static grub_command_t cmd_write_byte, cmd_write_word, cmd_write_dword;
  27. static const struct grub_arg_option options[] =
  28. {
  29. {0, 'v', 0, N_("Save read value into variable VARNAME."),
  30. N_("VARNAME"), ARG_TYPE_STRING},
  31. {0, 0, 0, 0, 0, 0}
  32. };
  33. static grub_err_t
  34. grub_cmd_read (grub_extcmd_t cmd, int argc, char **argv)
  35. {
  36. grub_target_addr_t addr;
  37. grub_uint32_t value = 0;
  38. char buf[sizeof ("XXXXXXXX")];
  39. if (argc != 1)
  40. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid number of arguments");
  41. addr = grub_strtoul (argv[0], 0, 0);
  42. switch (cmd->cmd->name[sizeof ("in") - 1])
  43. {
  44. case 'l':
  45. value = grub_inl (addr);
  46. break;
  47. case 'w':
  48. value = grub_inw (addr);
  49. break;
  50. case 'b':
  51. value = grub_inb (addr);
  52. break;
  53. }
  54. if (cmd->state[0].set)
  55. {
  56. grub_snprintf (buf, sizeof (buf), "%x", value);
  57. grub_env_set (cmd->state[0].arg, buf);
  58. }
  59. else
  60. grub_printf ("0x%x\n", value);
  61. return 0;
  62. }
  63. static grub_err_t
  64. grub_cmd_write (grub_command_t cmd, int argc, char **argv)
  65. {
  66. grub_target_addr_t addr;
  67. grub_uint32_t value;
  68. grub_uint32_t mask = 0xffffffff;
  69. if (argc != 2 && argc != 3)
  70. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid number of arguments");
  71. addr = grub_strtoul (argv[0], 0, 0);
  72. value = grub_strtoul (argv[1], 0, 0);
  73. if (argc == 3)
  74. mask = grub_strtoul (argv[2], 0, 0);
  75. value &= mask;
  76. switch (cmd->name[sizeof ("out") - 1])
  77. {
  78. case 'l':
  79. if (mask != 0xffffffff)
  80. grub_outl ((grub_inl (addr) & ~mask) | value, addr);
  81. else
  82. grub_outl (value, addr);
  83. break;
  84. case 'w':
  85. if ((mask & 0xffff) != 0xffff)
  86. grub_outw ((grub_inw (addr) & ~mask) | value, addr);
  87. else
  88. grub_outw (value, addr);
  89. break;
  90. case 'b':
  91. if ((mask & 0xff) != 0xff)
  92. grub_outb ((grub_inb (addr) & ~mask) | value, addr);
  93. else
  94. grub_outb (value, addr);
  95. break;
  96. }
  97. return 0;
  98. }
  99. GRUB_MOD_INIT(memrw)
  100. {
  101. cmd_read_byte =
  102. grub_register_extcmd ("inb", grub_cmd_read, GRUB_COMMAND_FLAG_BOTH,
  103. N_("PORT"), N_("Read byte from PORT."), options);
  104. cmd_read_word =
  105. grub_register_extcmd ("inw", grub_cmd_read, GRUB_COMMAND_FLAG_BOTH,
  106. N_("PORT"), N_("Read word from PORT."), options);
  107. cmd_read_dword =
  108. grub_register_extcmd ("inl", grub_cmd_read, GRUB_COMMAND_FLAG_BOTH,
  109. N_("PORT"), N_("Read dword from PORT."), options);
  110. cmd_write_byte =
  111. grub_register_command ("outb", grub_cmd_write,
  112. N_("PORT VALUE [MASK]"),
  113. N_("Write byte VALUE to PORT."));
  114. cmd_write_word =
  115. grub_register_command ("outw", grub_cmd_write,
  116. N_("PORT VALUE [MASK]"),
  117. N_("Write word VALUE to PORT."));
  118. cmd_write_dword =
  119. grub_register_command ("outl", grub_cmd_write,
  120. N_("ADDR VALUE [MASK]"),
  121. N_("Write dword VALUE to PORT."));
  122. }
  123. GRUB_MOD_FINI(memrw)
  124. {
  125. grub_unregister_extcmd (cmd_read_byte);
  126. grub_unregister_extcmd (cmd_read_word);
  127. grub_unregister_extcmd (cmd_read_dword);
  128. grub_unregister_command (cmd_write_byte);
  129. grub_unregister_command (cmd_write_word);
  130. grub_unregister_command (cmd_write_dword);
  131. }