2009-07-07 Pavel Roskin <proski@gnu.org>
[grub2/bean.git] / commands / memrw.c
blobadffb7fc87ce75b223a7dd459e074cec1ae31ad4
1 /* memrw.c - command to read / write physical memory */
2 /*
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/>.
20 #include <grub/dl.h>
21 #include <grub/misc.h>
22 #include <grub/command.h>
24 static grub_command_t cmd_read_byte, cmd_read_word, cmd_read_dword;
25 static grub_command_t cmd_write_byte, cmd_write_word, cmd_write_dword;
27 static grub_err_t
28 grub_cmd_read (grub_command_t cmd, int argc, char **argv)
30 grub_target_addr_t addr;
31 grub_uint32_t value;
33 if (argc != 1)
34 return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid number of arguments");
36 addr = grub_strtoul (argv[0], 0, 0);
37 if (cmd->name[5] == 'd')
38 value = *((grub_uint32_t *) addr);
39 else if (cmd->name[5] == 'w')
40 value = *((grub_uint16_t *) addr);
41 else
42 value = *((grub_uint8_t *) addr);
44 grub_printf ("0x%x\n", value);
46 return 0;
49 static grub_err_t
50 grub_cmd_write (grub_command_t cmd, int argc, char **argv)
52 grub_target_addr_t addr;
53 grub_uint32_t value;
55 if (argc != 2)
56 return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid number of arguments");
58 addr = grub_strtoul (argv[0], 0, 0);
59 value = grub_strtoul (argv[1], 0, 0);
60 if (cmd->name[6] == 'd')
61 *((grub_uint32_t *) addr) = value;
62 else if (cmd->name[6] == 'w')
63 *((grub_uint16_t *) addr) = (grub_uint16_t) value;
64 else
65 *((grub_uint8_t *) addr) = (grub_uint8_t) value;
67 return 0;
70 GRUB_MOD_INIT(memrw)
72 cmd_read_byte =
73 grub_register_command ("read_byte", grub_cmd_read,
74 "read_byte ADDR", "read byte.");
75 cmd_read_word =
76 grub_register_command ("read_word", grub_cmd_read,
77 "read_word ADDR", "read word.");
78 cmd_read_dword =
79 grub_register_command ("read_dword", grub_cmd_read,
80 "read_dword ADDR", "read dword.");
81 cmd_write_byte =
82 grub_register_command ("write_byte", grub_cmd_write,
83 "write_byte ADDR VALUE", "write byte.");
84 cmd_write_word =
85 grub_register_command ("write_word", grub_cmd_write,
86 "write_word ADDR VALUE", "write word.");
87 cmd_write_dword =
88 grub_register_command ("write_dword", grub_cmd_write,
89 "write_dword ADDR VALUE", "write dword.");
92 GRUB_MOD_FINI(memrw)
94 grub_unregister_command (cmd_read_byte);
95 grub_unregister_command (cmd_read_word);
96 grub_unregister_command (cmd_read_dword);
97 grub_unregister_command (cmd_write_byte);
98 grub_unregister_command (cmd_write_word);
99 grub_unregister_command (cmd_write_dword);