1
/* memrw.c - command to read / write physical memory */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2009 Free Software Foundation, Inc.
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.
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.
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/>.
21
#include <grub/misc.h>
22
#include <grub/extcmd.h>
24
#include <grub/i18n.h>
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;
29
static const struct grub_arg_option options[] =
31
{0, 'v', 0, N_("Save read value into variable VARNAME."),
32
"VARNAME", ARG_TYPE_STRING},
38
grub_cmd_read (grub_extcmd_context_t ctxt, int argc, char **argv)
40
grub_target_addr_t addr;
41
grub_uint32_t value = 0;
42
char buf[sizeof ("XXXXXXXX")];
45
return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid number of arguments");
47
addr = grub_strtoul (argv[0], 0, 0);
48
switch (ctxt->extcmd->cmd->name[sizeof ("read_") - 1])
51
value = *((volatile grub_uint32_t *) addr);
55
value = *((volatile grub_uint16_t *) addr);
59
value = *((volatile grub_uint8_t *) addr);
63
if (ctxt->state[0].set)
65
grub_snprintf (buf, sizeof (buf), "%x", value);
66
grub_env_set (ctxt->state[0].arg, buf);
69
grub_printf ("0x%x\n", value);
75
grub_cmd_write (grub_command_t cmd, int argc, char **argv)
77
grub_target_addr_t addr;
79
grub_uint32_t mask = 0xffffffff;
81
if (argc != 2 && argc != 3)
82
return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid number of arguments");
84
addr = grub_strtoul (argv[0], 0, 0);
85
value = grub_strtoul (argv[1], 0, 0);
87
mask = grub_strtoul (argv[2], 0, 0);
89
switch (cmd->name[sizeof ("write_") - 1])
92
if (mask != 0xffffffff)
93
*((volatile grub_uint32_t *) addr)
94
= (*((volatile grub_uint32_t *) addr) & ~mask) | value;
96
*((volatile grub_uint32_t *) addr) = value;
100
if ((mask & 0xffff) != 0xffff)
101
*((volatile grub_uint16_t *) addr)
102
= (*((volatile grub_uint16_t *) addr) & ~mask) | value;
104
*((volatile grub_uint16_t *) addr) = value;
108
if ((mask & 0xff) != 0xff)
109
*((volatile grub_uint8_t *) addr)
110
= (*((volatile grub_uint8_t *) addr) & ~mask) | value;
112
*((volatile grub_uint8_t *) addr) = value;
122
grub_register_extcmd ("read_byte", grub_cmd_read, 0,
123
N_("ADDR"), N_("Read byte from ADDR."), options);
125
grub_register_extcmd ("read_word", grub_cmd_read, 0,
126
N_("ADDR"), N_("Read word from ADDR."), options);
128
grub_register_extcmd ("read_dword", grub_cmd_read, 0,
129
N_("ADDR"), N_("Read dword from ADDR."), options);
131
grub_register_command ("write_byte", grub_cmd_write,
132
N_("ADDR VALUE [MASK]"), N_("Write byte VALUE to ADDR."));
134
grub_register_command ("write_word", grub_cmd_write,
135
N_("ADDR VALUE [MASK]"), N_("Write word VALUE to ADDR."));
137
grub_register_command ("write_dword", grub_cmd_write,
138
N_("ADDR VALUE [MASK]"), N_("Write dword VALUE to ADDR."));
143
grub_unregister_extcmd (cmd_read_byte);
144
grub_unregister_extcmd (cmd_read_word);
145
grub_unregister_extcmd (cmd_read_dword);
146
grub_unregister_command (cmd_write_byte);
147
grub_unregister_command (cmd_write_word);
148
grub_unregister_command (cmd_write_dword);