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 GRUB_MOD_LICENSE ("GPLv3+");
28 static grub_extcmd_t cmd_read_byte
, cmd_read_word
, cmd_read_dword
;
29 static grub_command_t cmd_write_byte
, cmd_write_word
, cmd_write_dword
;
31 static const struct grub_arg_option options
[] =
33 {0, 'v', 0, N_("Save read value into variable VARNAME."),
34 N_("VARNAME"), ARG_TYPE_STRING
},
40 grub_cmd_read (grub_extcmd_context_t ctxt
, int argc
, char **argv
)
43 grub_uint32_t value
= 0;
44 char buf
[sizeof ("XXXXXXXX")];
47 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("one argument expected"));
49 addr
= grub_strtoul (argv
[0], 0, 0);
50 switch (ctxt
->extcmd
->cmd
->name
[sizeof ("read_") - 1])
53 value
= *((volatile grub_uint32_t
*) addr
);
57 value
= *((volatile grub_uint16_t
*) addr
);
61 value
= *((volatile grub_uint8_t
*) addr
);
65 if (ctxt
->state
[0].set
)
67 grub_snprintf (buf
, sizeof (buf
), "%x", value
);
68 grub_env_set (ctxt
->state
[0].arg
, buf
);
71 grub_printf ("0x%x\n", value
);
77 grub_cmd_write (grub_command_t cmd
, int argc
, char **argv
)
81 grub_uint32_t mask
= 0xffffffff;
83 if (argc
!= 2 && argc
!= 3)
84 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("two arguments expected"));
86 addr
= grub_strtoul (argv
[0], 0, 0);
87 value
= grub_strtoul (argv
[1], 0, 0);
89 mask
= grub_strtoul (argv
[2], 0, 0);
91 switch (cmd
->name
[sizeof ("write_") - 1])
94 if (mask
!= 0xffffffff)
95 *((volatile grub_uint32_t
*) addr
)
96 = (*((volatile grub_uint32_t
*) addr
) & ~mask
) | value
;
98 *((volatile grub_uint32_t
*) addr
) = value
;
102 if ((mask
& 0xffff) != 0xffff)
103 *((volatile grub_uint16_t
*) addr
)
104 = (*((volatile grub_uint16_t
*) addr
) & ~mask
) | value
;
106 *((volatile grub_uint16_t
*) addr
) = value
;
110 if ((mask
& 0xff) != 0xff)
111 *((volatile grub_uint8_t
*) addr
)
112 = (*((volatile grub_uint8_t
*) addr
) & ~mask
) | value
;
114 *((volatile grub_uint8_t
*) addr
) = value
;
124 grub_register_extcmd ("read_byte", grub_cmd_read
, 0,
125 N_("ADDR"), N_("Read 8-bit value from ADDR."),
128 grub_register_extcmd ("read_word", grub_cmd_read
, 0,
129 N_("ADDR"), N_("Read 16-bit value from ADDR."),
132 grub_register_extcmd ("read_dword", grub_cmd_read
, 0,
133 N_("ADDR"), N_("Read 32-bit value from ADDR."),
136 grub_register_command ("write_byte", grub_cmd_write
,
137 N_("ADDR VALUE [MASK]"),
138 N_("Write 8-bit VALUE to ADDR."));
140 grub_register_command ("write_word", grub_cmd_write
,
141 N_("ADDR VALUE [MASK]"),
142 N_("Write 16-bit VALUE to ADDR."));
144 grub_register_command ("write_dword", grub_cmd_write
,
145 N_("ADDR VALUE [MASK]"),
146 N_("Write 32-bit VALUE to ADDR."));
151 grub_unregister_extcmd (cmd_read_byte
);
152 grub_unregister_extcmd (cmd_read_word
);
153 grub_unregister_extcmd (cmd_read_dword
);
154 grub_unregister_command (cmd_write_byte
);
155 grub_unregister_command (cmd_write_word
);
156 grub_unregister_command (cmd_write_dword
);