memrw.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. GRUB_MOD_LICENSE ("GPLv3+");
  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_context_t ctxt, int argc, char **argv)
  35. {
  36. grub_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, N_("one argument expected"));
  41. addr = grub_strtoul (argv[0], 0, 0);
  42. switch (ctxt->extcmd->cmd->name[sizeof ("read_") - 1])
  43. {
  44. case 'd':
  45. value = *((volatile grub_uint32_t *) addr);
  46. break;
  47. case 'w':
  48. value = *((volatile grub_uint16_t *) addr);
  49. break;
  50. case 'b':
  51. value = *((volatile grub_uint8_t *) addr);
  52. break;
  53. }
  54. if (ctxt->state[0].set)
  55. {
  56. grub_snprintf (buf, sizeof (buf), "%x", value);
  57. grub_env_set (ctxt->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_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, N_("two arguments expected"));
  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 ("write_") - 1])
  77. {
  78. case 'd':
  79. if (mask != 0xffffffff)
  80. *((volatile grub_uint32_t *) addr)
  81. = (*((volatile grub_uint32_t *) addr) & ~mask) | value;
  82. else
  83. *((volatile grub_uint32_t *) addr) = value;
  84. break;
  85. case 'w':
  86. if ((mask & 0xffff) != 0xffff)
  87. *((volatile grub_uint16_t *) addr)
  88. = (*((volatile grub_uint16_t *) addr) & ~mask) | value;
  89. else
  90. *((volatile grub_uint16_t *) addr) = value;
  91. break;
  92. case 'b':
  93. if ((mask & 0xff) != 0xff)
  94. *((volatile grub_uint8_t *) addr)
  95. = (*((volatile grub_uint8_t *) addr) & ~mask) | value;
  96. else
  97. *((volatile grub_uint8_t *) addr) = value;
  98. break;
  99. }
  100. return 0;
  101. }
  102. GRUB_MOD_INIT(memrw)
  103. {
  104. cmd_read_byte =
  105. grub_register_extcmd ("read_byte", grub_cmd_read, 0,
  106. N_("ADDR"), N_("Read 8-bit value from ADDR."),
  107. options);
  108. cmd_read_word =
  109. grub_register_extcmd ("read_word", grub_cmd_read, 0,
  110. N_("ADDR"), N_("Read 16-bit value from ADDR."),
  111. options);
  112. cmd_read_dword =
  113. grub_register_extcmd ("read_dword", grub_cmd_read, 0,
  114. N_("ADDR"), N_("Read 32-bit value from ADDR."),
  115. options);
  116. cmd_write_byte =
  117. grub_register_command ("write_byte", grub_cmd_write,
  118. N_("ADDR VALUE [MASK]"),
  119. N_("Write 8-bit VALUE to ADDR."));
  120. cmd_write_word =
  121. grub_register_command ("write_word", grub_cmd_write,
  122. N_("ADDR VALUE [MASK]"),
  123. N_("Write 16-bit VALUE to ADDR."));
  124. cmd_write_dword =
  125. grub_register_command ("write_dword", grub_cmd_write,
  126. N_("ADDR VALUE [MASK]"),
  127. N_("Write 32-bit VALUE to ADDR."));
  128. }
  129. GRUB_MOD_FINI(memrw)
  130. {
  131. grub_unregister_extcmd (cmd_read_byte);
  132. grub_unregister_extcmd (cmd_read_word);
  133. grub_unregister_extcmd (cmd_read_dword);
  134. grub_unregister_command (cmd_write_byte);
  135. grub_unregister_command (cmd_write_word);
  136. grub_unregister_command (cmd_write_dword);
  137. }