Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / iorw.c
bloba0c164e54f068008a4e45ea6554c53249bd8c266
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/extcmd.h>
23 #include <grub/env.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},
36 {0, 0, 0, 0, 0, 0}
40 static grub_err_t
41 grub_cmd_read (grub_extcmd_context_t ctxt, int argc, char **argv)
43 grub_port_t addr;
44 grub_uint32_t value = 0;
46 if (argc != 1)
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])
52 case 'l':
53 value = grub_inl (addr);
54 break;
56 case 'w':
57 value = grub_inw (addr);
58 break;
60 case 'b':
61 value = grub_inb (addr);
62 break;
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);
71 else
72 grub_printf ("0x%x\n", value);
74 return 0;
77 static grub_err_t
78 grub_cmd_write (grub_command_t cmd, int argc, char **argv)
80 grub_port_t addr;
81 grub_uint32_t value;
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);
89 if (argc == 3)
90 mask = grub_strtoul (argv[2], 0, 0);
91 value &= mask;
92 switch (cmd->name[sizeof ("out") - 1])
94 case 'l':
95 if (mask != 0xffffffff)
96 grub_outl ((grub_inl (addr) & ~mask) | value, addr);
97 else
98 grub_outl (value, addr);
99 break;
101 case 'w':
102 if ((mask & 0xffff) != 0xffff)
103 grub_outw ((grub_inw (addr) & ~mask) | value, addr);
104 else
105 grub_outw (value, addr);
106 break;
108 case 'b':
109 if ((mask & 0xff) != 0xff)
110 grub_outb ((grub_inb (addr) & ~mask) | value, addr);
111 else
112 grub_outb (value, addr);
113 break;
116 return 0;
119 GRUB_MOD_INIT(memrw)
121 cmd_read_byte =
122 grub_register_extcmd ("inb", grub_cmd_read, 0,
123 N_("PORT"), N_("Read 8-bit value from PORT."),
124 options);
125 cmd_read_word =
126 grub_register_extcmd ("inw", grub_cmd_read, 0,
127 N_("PORT"), N_("Read 16-bit value from PORT."),
128 options);
129 cmd_read_dword =
130 grub_register_extcmd ("inl", grub_cmd_read, 0,
131 N_("PORT"), N_("Read 32-bit value from PORT."),
132 options);
133 cmd_write_byte =
134 grub_register_command ("outb", grub_cmd_write,
135 N_("PORT VALUE [MASK]"),
136 N_("Write 8-bit VALUE to PORT."));
137 cmd_write_word =
138 grub_register_command ("outw", grub_cmd_write,
139 N_("PORT VALUE [MASK]"),
140 N_("Write 16-bit VALUE to PORT."));
141 cmd_write_dword =
142 grub_register_command ("outl", grub_cmd_write,
143 N_("ADDR VALUE [MASK]"),
144 N_("Write 32-bit VALUE to PORT."));
147 GRUB_MOD_FINI(memrw)
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);