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/cpu/io.h>
25
#include <grub/i18n.h>
27
static grub_extcmd_t cmd_read_byte, cmd_read_word, cmd_read_dword;
28
static grub_command_t cmd_write_byte, cmd_write_word, cmd_write_dword;
30
static const struct grub_arg_option options[] =
32
{0, 'v', 0, N_("Save read value into variable VARNAME."),
33
N_("VARNAME"), ARG_TYPE_STRING},
39
grub_cmd_read (grub_extcmd_context_t ctxt, int argc, char **argv)
41
grub_target_addr_t addr;
42
grub_uint32_t value = 0;
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 ("in") - 1])
51
value = grub_inl (addr);
55
value = grub_inw (addr);
59
value = grub_inb (addr);
63
if (ctxt->state[0].set)
65
char buf[sizeof ("XXXXXXXX")];
66
grub_snprintf (buf, sizeof (buf), "%x", value);
67
grub_env_set (ctxt->state[0].arg, buf);
70
grub_printf ("0x%x\n", value);
76
grub_cmd_write (grub_command_t cmd, int argc, char **argv)
78
grub_target_addr_t addr;
80
grub_uint32_t mask = 0xffffffff;
82
if (argc != 2 && argc != 3)
83
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid number of arguments");
85
addr = grub_strtoul (argv[0], 0, 0);
86
value = grub_strtoul (argv[1], 0, 0);
88
mask = grub_strtoul (argv[2], 0, 0);
90
switch (cmd->name[sizeof ("out") - 1])
93
if (mask != 0xffffffff)
94
grub_outl ((grub_inl (addr) & ~mask) | value, addr);
96
grub_outl (value, addr);
100
if ((mask & 0xffff) != 0xffff)
101
grub_outw ((grub_inw (addr) & ~mask) | value, addr);
103
grub_outw (value, addr);
107
if ((mask & 0xff) != 0xff)
108
grub_outb ((grub_inb (addr) & ~mask) | value, addr);
110
grub_outb (value, addr);
120
grub_register_extcmd ("inb", grub_cmd_read, 0,
121
N_("PORT"), N_("Read byte from PORT."), options);
123
grub_register_extcmd ("inw", grub_cmd_read, 0,
124
N_("PORT"), N_("Read word from PORT."), options);
126
grub_register_extcmd ("inl", grub_cmd_read, 0,
127
N_("PORT"), N_("Read dword from PORT."), options);
129
grub_register_command ("outb", grub_cmd_write,
130
N_("PORT VALUE [MASK]"),
131
N_("Write byte VALUE to PORT."));
133
grub_register_command ("outw", grub_cmd_write,
134
N_("PORT VALUE [MASK]"),
135
N_("Write word VALUE to PORT."));
137
grub_register_command ("outl", grub_cmd_write,
138
N_("ADDR VALUE [MASK]"),
139
N_("Write dword VALUE to PORT."));
144
grub_unregister_extcmd (cmd_read_byte);
145
grub_unregister_extcmd (cmd_read_word);
146
grub_unregister_extcmd (cmd_read_dword);
147
grub_unregister_command (cmd_write_byte);
148
grub_unregister_command (cmd_write_word);
149
grub_unregister_command (cmd_write_dword);