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 GRUB_MOD_LICENSE ("GPLv3+");
29 static grub_extcmd_t cmd_read_byte
, cmd_read_word
, cmd_read_dword
;
30 static grub_command_t cmd_write_byte
, cmd_write_word
, cmd_write_dword
;
32 static const struct grub_arg_option options
[] =
34 {0, 'v', 0, N_("Save read value into variable VARNAME."),
35 N_("VARNAME"), ARG_TYPE_STRING
},
41 grub_cmd_read (grub_extcmd_context_t ctxt
, int argc
, char **argv
)
44 grub_uint32_t value
= 0;
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 ("in") - 1])
53 value
= grub_inl (addr
);
57 value
= grub_inw (addr
);
61 value
= grub_inb (addr
);
65 if (ctxt
->state
[0].set
)
67 char buf
[sizeof ("XXXXXXXX")];
68 grub_snprintf (buf
, sizeof (buf
), "%x", value
);
69 grub_env_set (ctxt
->state
[0].arg
, buf
);
72 grub_printf ("0x%x\n", value
);
78 grub_cmd_write (grub_command_t cmd
, int argc
, char **argv
)
82 grub_uint32_t mask
= 0xffffffff;
84 if (argc
!= 2 && argc
!= 3)
85 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("two arguments expected"));
87 addr
= grub_strtoul (argv
[0], 0, 0);
88 value
= grub_strtoul (argv
[1], 0, 0);
90 mask
= grub_strtoul (argv
[2], 0, 0);
92 switch (cmd
->name
[sizeof ("out") - 1])
95 if (mask
!= 0xffffffff)
96 grub_outl ((grub_inl (addr
) & ~mask
) | value
, addr
);
98 grub_outl (value
, addr
);
102 if ((mask
& 0xffff) != 0xffff)
103 grub_outw ((grub_inw (addr
) & ~mask
) | value
, addr
);
105 grub_outw (value
, addr
);
109 if ((mask
& 0xff) != 0xff)
110 grub_outb ((grub_inb (addr
) & ~mask
) | value
, addr
);
112 grub_outb (value
, addr
);
122 grub_register_extcmd ("inb", grub_cmd_read
, 0,
123 N_("PORT"), N_("Read 8-bit value from PORT."),
126 grub_register_extcmd ("inw", grub_cmd_read
, 0,
127 N_("PORT"), N_("Read 16-bit value from PORT."),
130 grub_register_extcmd ("inl", grub_cmd_read
, 0,
131 N_("PORT"), N_("Read 32-bit value from PORT."),
134 grub_register_command ("outb", grub_cmd_write
,
135 N_("PORT VALUE [MASK]"),
136 N_("Write 8-bit VALUE to PORT."));
138 grub_register_command ("outw", grub_cmd_write
,
139 N_("PORT VALUE [MASK]"),
140 N_("Write 16-bit VALUE to PORT."));
142 grub_register_command ("outl", grub_cmd_write
,
143 N_("ADDR VALUE [MASK]"),
144 N_("Write 32-bit VALUE to PORT."));
149 grub_unregister_extcmd (cmd_read_byte
);
150 grub_unregister_extcmd (cmd_read_word
);
151 grub_unregister_extcmd (cmd_read_dword
);
152 grub_unregister_command (cmd_write_byte
);
153 grub_unregister_command (cmd_write_word
);
154 grub_unregister_command (cmd_write_dword
);