memrw.c 4.5 KB

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