minicmd.c 5.49 KB
Newer Older
1 2 3
/* minicmd.c - commands for the rescue mode */
/*
 *  GRUB  --  GRand Unified Bootloader
4
 *  Copyright (C) 2003,2005,2006,2007,2009  Free Software Foundation, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 *  GRUB 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.
 *
 *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/dl.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/env.h>
#include <grub/misc.h>
#include <grub/file.h>
#include <grub/disk.h>
#include <grub/term.h>
#include <grub/loader.h>
#include <grub/command.h>
30
#include <grub/i18n.h>
31

32 33
GRUB_MOD_LICENSE ("GPLv3+");

34 35 36 37 38 39 40 41 42 43
/* cat FILE */
static grub_err_t
grub_mini_cmd_cat (struct grub_command *cmd __attribute__ ((unused)),
		   int argc, char *argv[])
{
  grub_file_t file;
  char buf[GRUB_DISK_SECTOR_SIZE];
  grub_ssize_t size;

  if (argc < 1)
44
    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
45 46 47 48 49 50 51 52 53 54 55 56 57 58

  file = grub_file_open (argv[0]);
  if (! file)
    return grub_errno;

  while ((size = grub_file_read (file, buf, sizeof (buf))) > 0)
    {
      int i;

      for (i = 0; i < size; i++)
	{
	  unsigned char c = buf[i];

	  if ((grub_isprint (c) || grub_isspace (c)) && c != '\r')
59
	    grub_printf ("%c", c);
60 61 62 63 64 65 66 67 68
	  else
	    {
	      grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
	      grub_printf ("<%x>", (int) c);
	      grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
	    }
	}
    }

69
  grub_xputs ("\n");
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  grub_refresh ();
  grub_file_close (file);

  return 0;
}

/* help */
static grub_err_t
grub_mini_cmd_help (struct grub_command *cmd __attribute__ ((unused)),
		    int argc __attribute__ ((unused)),
		    char *argv[] __attribute__ ((unused)))
{
  grub_command_t p;

  for (p = grub_command_list; p; p = p->next)
    grub_printf ("%s (%d%c)\t%s\n", p->name,
86 87
		 p->prio & GRUB_COMMAND_PRIO_MASK,
		 (p->prio & GRUB_COMMAND_FLAG_ACTIVE) ? '+' : '-',
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
		 p->description);

  return 0;
}

/* dump ADDRESS [SIZE] */
static grub_err_t
grub_mini_cmd_dump (struct grub_command *cmd __attribute__ ((unused)),
		    int argc, char *argv[])
{
  grub_uint8_t *addr;
  grub_size_t size = 4;

  if (argc == 0)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "no address specified");

104 105 106 107 108 109 110
#if GRUB_CPU_SIZEOF_VOID_P == GRUB_CPU_SIZEOF_LONG
#define grub_strtoaddr grub_strtoul
#else
#define grub_strtoaddr grub_strtoull
#endif

  addr = (grub_uint8_t *) grub_strtoaddr (argv[0], 0, 0);
111 112 113 114
  if (grub_errno)
    return grub_errno;

  if (argc > 1)
115
    size = (grub_size_t) grub_strtoaddr (argv[1], 0, 0);
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

  while (size--)
    {
      grub_printf ("%x%x ", *addr >> 4, *addr & 0xf);
      addr++;
    }

  return 0;
}

/* rmmod MODULE */
static grub_err_t
grub_mini_cmd_rmmod (struct grub_command *cmd __attribute__ ((unused)),
		     int argc, char *argv[])
{
  grub_dl_t mod;

  if (argc == 0)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");

  mod = grub_dl_get (argv[0]);
  if (! mod)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "no such module");

  if (grub_dl_unref (mod) <= 0)
    grub_dl_unload (mod);

  return 0;
}

/* lsmod */
static grub_err_t
grub_mini_cmd_lsmod (struct grub_command *cmd __attribute__ ((unused)),
		     int argc __attribute__ ((unused)),
		     char *argv[] __attribute__ ((unused)))
{
152
  grub_dl_t mod;
153

154 155 156 157 158
  /* 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.
   */
159
  grub_printf_ (N_("Name\tRef Count\tDependencies\n"));
160 161 162 163 164 165 166 167
  FOR_DL_MODULES (mod)
  {
    grub_dl_dep_t dep;

    grub_printf ("%s\t%d\t\t", mod->name, mod->ref_count);
    for (dep = mod->dep; dep; dep = dep->next)
      {
	if (dep != mod->dep)
168
	  grub_xputs (",");
169 170 171

	grub_printf ("%s", dep->mod->name);
      }
172
    grub_xputs ("\n");
173
  }
174 175 176 177 178

  return 0;
}

/* exit */
179
static grub_err_t __attribute__ ((noreturn))
180 181 182 183 184
grub_mini_cmd_exit (struct grub_command *cmd __attribute__ ((unused)),
		    int argc __attribute__ ((unused)),
		    char *argv[] __attribute__ ((unused)))
{
  grub_exit ();
185
  /* Not reached.  */
186 187
}

188
static grub_command_t cmd_cat, cmd_help;
189 190 191 192 193 194
static grub_command_t cmd_dump, cmd_rmmod, cmd_lsmod, cmd_exit;

GRUB_MOD_INIT(minicmd)
{
  cmd_cat =
    grub_register_command ("cat", grub_mini_cmd_cat,
195
			   N_("FILE"), N_("Show the contents of a file."));
196 197
  cmd_help =
    grub_register_command ("help", grub_mini_cmd_help,
198
			   0, N_("Show this message."));
199 200
  cmd_dump =
    grub_register_command ("dump", grub_mini_cmd_dump,
201
			   N_("ADDR [SIZE]"), N_("Show memory contents."));
202 203
  cmd_rmmod =
    grub_register_command ("rmmod", grub_mini_cmd_rmmod,
204
			   N_("MODULE"), N_("Remove a module."));
205 206
  cmd_lsmod =
    grub_register_command ("lsmod", grub_mini_cmd_lsmod,
207
			   0, N_("Show loaded modules."));
208 209
  cmd_exit =
    grub_register_command ("exit", grub_mini_cmd_exit,
210
			   0, N_("Exit from GRUB."));
211 212 213 214 215 216 217 218 219 220 221
}

GRUB_MOD_FINI(minicmd)
{
  grub_unregister_command (cmd_cat);
  grub_unregister_command (cmd_help);
  grub_unregister_command (cmd_dump);
  grub_unregister_command (cmd_rmmod);
  grub_unregister_command (cmd_lsmod);
  grub_unregister_command (cmd_exit);
}