vaseboot/VasEBoot-core/commands/minicmd.c

222 lines
5.9 KiB
C

/* minicmd.c - commands for the rescue mode */
/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2003,2005,2006,2007,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/mm.h>
#include <VasEBoot/err.h>
#include <VasEBoot/env.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/file.h>
#include <VasEBoot/disk.h>
#include <VasEBoot/term.h>
#include <VasEBoot/loader.h>
#include <VasEBoot/command.h>
#include <VasEBoot/i18n.h>
VasEBoot_MOD_LICENSE ("GPLv3+");
/* cat FILE */
static VasEBoot_err_t
VasEBoot_mini_cmd_cat (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc, char *argv[])
{
VasEBoot_file_t file;
char buf[VasEBoot_DISK_SECTOR_SIZE];
VasEBoot_ssize_t size;
if (argc < 1)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("filename expected"));
file = VasEBoot_file_open (argv[0]);
if (! file)
return VasEBoot_errno;
while ((size = VasEBoot_file_read (file, buf, sizeof (buf))) > 0)
{
int i;
for (i = 0; i < size; i++)
{
unsigned char c = buf[i];
if ((VasEBoot_isprint (c) || VasEBoot_isspace (c)) && c != '\r')
VasEBoot_printf ("%c", c);
else
{
VasEBoot_setcolorstate (VasEBoot_TERM_COLOR_HIGHLIGHT);
VasEBoot_printf ("<%x>", (int) c);
VasEBoot_setcolorstate (VasEBoot_TERM_COLOR_STANDARD);
}
}
}
VasEBoot_xputs ("\n");
VasEBoot_refresh ();
VasEBoot_file_close (file);
return 0;
}
/* help */
static VasEBoot_err_t
VasEBoot_mini_cmd_help (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc __attribute__ ((unused)),
char *argv[] __attribute__ ((unused)))
{
VasEBoot_command_t p;
for (p = VasEBoot_command_list; p; p = p->next)
VasEBoot_printf ("%s (%d%c)\t%s\n", p->name,
p->prio & VasEBoot_COMMAND_PRIO_MASK,
(p->prio & VasEBoot_COMMAND_FLAG_ACTIVE) ? '+' : '-',
p->description);
return 0;
}
/* dump ADDRESS [SIZE] */
static VasEBoot_err_t
VasEBoot_mini_cmd_dump (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc, char *argv[])
{
VasEBoot_uint8_t *addr;
VasEBoot_size_t size = 4;
if (argc == 0)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, "no address specified");
#if VasEBoot_CPU_SIZEOF_VOID_P == VasEBoot_CPU_SIZEOF_LONG
#define VasEBoot_strtoaddr VasEBoot_strtoul
#else
#define VasEBoot_strtoaddr VasEBoot_strtoull
#endif
addr = (VasEBoot_uint8_t *) VasEBoot_strtoaddr (argv[0], 0, 0);
if (VasEBoot_errno)
return VasEBoot_errno;
if (argc > 1)
size = (VasEBoot_size_t) VasEBoot_strtoaddr (argv[1], 0, 0);
while (size--)
{
VasEBoot_printf ("%x%x ", *addr >> 4, *addr & 0xf);
addr++;
}
return 0;
}
/* rmmod MODULE */
static VasEBoot_err_t
VasEBoot_mini_cmd_rmmod (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc, char *argv[])
{
VasEBoot_dl_t mod;
if (argc == 0)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, "no module specified");
mod = VasEBoot_dl_get (argv[0]);
if (! mod)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, "no such module");
if (VasEBoot_dl_unref (mod) <= 0)
VasEBoot_dl_unload (mod);
return 0;
}
/* lsmod */
static VasEBoot_err_t
VasEBoot_mini_cmd_lsmod (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc __attribute__ ((unused)),
char *argv[] __attribute__ ((unused)))
{
VasEBoot_dl_t mod;
/* TRANSLATORS: this is module list header. Name
is module name, Ref Count is a reference counter
(how many modules or open descriptors use it).
Dependencies are the other modules it uses.
*/
VasEBoot_printf_ (N_("Name\tRef Count\tDependencies\n"));
FOR_DL_MODULES (mod)
{
VasEBoot_dl_dep_t dep;
VasEBoot_printf ("%s\t%d\t\t", mod->name, mod->ref_count);
for (dep = mod->dep; dep; dep = dep->next)
{
if (dep != mod->dep)
VasEBoot_xputs (",");
VasEBoot_printf ("%s", dep->mod->name);
}
VasEBoot_xputs ("\n");
}
return 0;
}
/* exit */
static VasEBoot_err_t __attribute__ ((noreturn))
VasEBoot_mini_cmd_exit (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc __attribute__ ((unused)),
char *argv[] __attribute__ ((unused)))
{
VasEBoot_exit ();
/* Not reached. */
}
static VasEBoot_command_t cmd_cat, cmd_help;
static VasEBoot_command_t cmd_dump, cmd_rmmod, cmd_lsmod, cmd_exit;
VasEBoot_MOD_INIT(minicmd)
{
cmd_cat =
VasEBoot_register_command ("cat", VasEBoot_mini_cmd_cat,
N_("FILE"), N_("Show the contents of a file."));
cmd_help =
VasEBoot_register_command ("help", VasEBoot_mini_cmd_help,
0, N_("Show this message."));
cmd_dump =
VasEBoot_register_command ("dump", VasEBoot_mini_cmd_dump,
N_("ADDR [SIZE]"), N_("Show memory contents."));
cmd_rmmod =
VasEBoot_register_command ("rmmod", VasEBoot_mini_cmd_rmmod,
N_("MODULE"), N_("Remove a module."));
cmd_lsmod =
VasEBoot_register_command ("lsmod", VasEBoot_mini_cmd_lsmod,
0, N_("Show loaded modules."));
cmd_exit =
VasEBoot_register_command ("exit", VasEBoot_mini_cmd_exit,
0, N_("Exit from VasEBoot."));
}
VasEBoot_MOD_FINI(minicmd)
{
VasEBoot_unregister_command (cmd_cat);
VasEBoot_unregister_command (cmd_help);
VasEBoot_unregister_command (cmd_dump);
VasEBoot_unregister_command (cmd_rmmod);
VasEBoot_unregister_command (cmd_lsmod);
VasEBoot_unregister_command (cmd_exit);
}