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>
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, "Save read value into variable VARNAME.",
32
"VARNAME", ARG_TYPE_STRING},
38
grub_cmd_read (grub_extcmd_t cmd, int argc, char **argv)
40
grub_target_addr_t addr;
41
grub_uint32_t value = 0;
44
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid number of arguments");
46
addr = grub_strtoul (argv[0], 0, 0);
47
switch (cmd->cmd->name[sizeof ("grub_in") - 1])
50
value = grub_inl (addr);
54
value = grub_inw (addr);
58
value = grub_inb (addr);
62
if (cmd->state[0].set)
64
char buf[sizeof ("XXXXXXXX")];
65
grub_snprintf (buf, sizeof (buf), "%x", value);
66
grub_env_set (cmd->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 ("grub_out") - 1])
92
if (mask != 0xffffffff)
93
grub_outl ((grub_inl (addr) & ~mask) | value, addr);
95
grub_outl (value, addr);
99
if ((mask & 0xffff) != 0xffff)
100
grub_outw ((grub_inw (addr) & ~mask) | value, addr);
102
grub_outw (value, addr);
106
if ((mask & 0xff) != 0xff)
107
grub_outb ((grub_inb (addr) & ~mask) | value, addr);
109
grub_outb (value, addr);
119
grub_register_extcmd ("grub_inb", grub_cmd_read, GRUB_COMMAND_FLAG_BOTH,
120
"grub_inb PORT", "Read byte from PORT.", options);
122
grub_register_extcmd ("grub_inw", grub_cmd_read, GRUB_COMMAND_FLAG_BOTH,
123
"grub_inw PORT", "Read word from PORT.", options);
125
grub_register_extcmd ("grub_inl", grub_cmd_read, GRUB_COMMAND_FLAG_BOTH,
126
"grub_inl PORT", "Read dword from PORT.", options);
128
grub_register_command ("grub_outb", grub_cmd_write,
129
"grub_outb PORT VALUE [MASK]", "Write byte VALUE to PORT.");
131
grub_register_command ("grub_outw", grub_cmd_write,
132
"grub_outw PORT VALUE [MASK]", "Write word VALUE to PORT.");
134
grub_register_command ("grub_outl", grub_cmd_write,
135
"grub_outl ADDR VALUE [MASK]", "Write dword VALUE to PORT.");
140
grub_unregister_extcmd (cmd_read_byte);
141
grub_unregister_extcmd (cmd_read_word);
142
grub_unregister_extcmd (cmd_read_dword);
143
grub_unregister_command (cmd_write_byte);
144
grub_unregister_command (cmd_write_word);
145
grub_unregister_command (cmd_write_dword);