vaseboot/VasEBoot-core/commands/iorw.c

156 lines
4.3 KiB
C

/* memrw.c - command to read / write physical memory */
/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2009 Free Software Foundation, Inc.
*
* VasEBoot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VasEBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VasEBoot. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/dl.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/extcmd.h>
#include <VasEBoot/env.h>
#include <VasEBoot/cpu/io.h>
#include <VasEBoot/i18n.h>
VasEBoot_MOD_LICENSE ("GPLv3+");
static VasEBoot_extcmd_t cmd_read_byte, cmd_read_word, cmd_read_dword;
static VasEBoot_command_t cmd_write_byte, cmd_write_word, cmd_write_dword;
static const struct VasEBoot_arg_option options[] =
{
{0, 'v', 0, N_("Save read value into variable VARNAME."),
N_("VARNAME"), ARG_TYPE_STRING},
{0, 0, 0, 0, 0, 0}
};
static VasEBoot_err_t
VasEBoot_cmd_read (VasEBoot_extcmd_context_t ctxt, int argc, char **argv)
{
VasEBoot_port_t addr;
VasEBoot_uint32_t value = 0;
if (argc != 1)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("one argument expected"));
addr = VasEBoot_strtoul (argv[0], 0, 0);
switch (ctxt->extcmd->cmd->name[sizeof ("in") - 1])
{
case 'l':
value = VasEBoot_inl (addr);
break;
case 'w':
value = VasEBoot_inw (addr);
break;
case 'b':
value = VasEBoot_inb (addr);
break;
}
if (ctxt->state[0].set)
{
char buf[sizeof ("XXXXXXXX")];
VasEBoot_snprintf (buf, sizeof (buf), "%x", value);
VasEBoot_env_set (ctxt->state[0].arg, buf);
}
else
VasEBoot_printf ("0x%x\n", value);
return 0;
}
static VasEBoot_err_t
VasEBoot_cmd_write (VasEBoot_command_t cmd, int argc, char **argv)
{
VasEBoot_port_t addr;
VasEBoot_uint32_t value;
VasEBoot_uint32_t mask = 0xffffffff;
if (argc != 2 && argc != 3)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("two arguments expected"));
addr = VasEBoot_strtoul (argv[0], 0, 0);
value = VasEBoot_strtoul (argv[1], 0, 0);
if (argc == 3)
mask = VasEBoot_strtoul (argv[2], 0, 0);
value &= mask;
switch (cmd->name[sizeof ("out") - 1])
{
case 'l':
if (mask != 0xffffffff)
VasEBoot_outl ((VasEBoot_inl (addr) & ~mask) | value, addr);
else
VasEBoot_outl (value, addr);
break;
case 'w':
if ((mask & 0xffff) != 0xffff)
VasEBoot_outw ((VasEBoot_inw (addr) & ~mask) | value, addr);
else
VasEBoot_outw (value, addr);
break;
case 'b':
if ((mask & 0xff) != 0xff)
VasEBoot_outb ((VasEBoot_inb (addr) & ~mask) | value, addr);
else
VasEBoot_outb (value, addr);
break;
}
return 0;
}
VasEBoot_MOD_INIT(memrw)
{
cmd_read_byte =
VasEBoot_register_extcmd ("inb", VasEBoot_cmd_read, 0,
N_("PORT"), N_("Read 8-bit value from PORT."),
options);
cmd_read_word =
VasEBoot_register_extcmd ("inw", VasEBoot_cmd_read, 0,
N_("PORT"), N_("Read 16-bit value from PORT."),
options);
cmd_read_dword =
VasEBoot_register_extcmd ("inl", VasEBoot_cmd_read, 0,
N_("PORT"), N_("Read 32-bit value from PORT."),
options);
cmd_write_byte =
VasEBoot_register_command ("outb", VasEBoot_cmd_write,
N_("PORT VALUE [MASK]"),
N_("Write 8-bit VALUE to PORT."));
cmd_write_word =
VasEBoot_register_command ("outw", VasEBoot_cmd_write,
N_("PORT VALUE [MASK]"),
N_("Write 16-bit VALUE to PORT."));
cmd_write_dword =
VasEBoot_register_command ("outl", VasEBoot_cmd_write,
N_("ADDR VALUE [MASK]"),
N_("Write 32-bit VALUE to PORT."));
}
VasEBoot_MOD_FINI(memrw)
{
VasEBoot_unregister_extcmd (cmd_read_byte);
VasEBoot_unregister_extcmd (cmd_read_word);
VasEBoot_unregister_extcmd (cmd_read_dword);
VasEBoot_unregister_command (cmd_write_byte);
VasEBoot_unregister_command (cmd_write_word);
VasEBoot_unregister_command (cmd_write_dword);
}