memrw.c 4.2 KB

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