1 /* minicmd.c - commands for the rescue mode */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2005,2006,2007,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/>.
24 #include <grub/misc.h>
25 #include <grub/file.h>
26 #include <grub/disk.h>
27 #include <grub/term.h>
28 #include <grub/loader.h>
29 #include <grub/command.h>
30 #include <grub/i18n.h>
34 grub_mini_cmd_cat (struct grub_command
*cmd
__attribute__ ((unused
)),
35 int argc
, char *argv
[])
38 char buf
[GRUB_DISK_SECTOR_SIZE
];
42 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "no file specified");
44 file
= grub_file_open (argv
[0]);
48 while ((size
= grub_file_read (file
, buf
, sizeof (buf
))) > 0)
52 for (i
= 0; i
< size
; i
++)
54 unsigned char c
= buf
[i
];
56 if ((grub_isprint (c
) || grub_isspace (c
)) && c
!= '\r')
57 grub_printf ("%c", c
);
60 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT
);
61 grub_printf ("<%x>", (int) c
);
62 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD
);
69 grub_file_close (file
);
76 grub_mini_cmd_help (struct grub_command
*cmd
__attribute__ ((unused
)),
77 int argc
__attribute__ ((unused
)),
78 char *argv
[] __attribute__ ((unused
)))
82 for (p
= grub_command_list
; p
; p
= p
->next
)
83 grub_printf ("%s (%d%c)\t%s\n", p
->name
,
84 p
->prio
& GRUB_PRIO_LIST_PRIO_MASK
,
85 (p
->prio
& GRUB_PRIO_LIST_FLAG_ACTIVE
) ? '+' : '-',
93 grub_rescue_cmd_info (void)
95 extern void grub_disk_cache_get_performance (unsigned long *,
97 unsigned long hits
, misses
;
99 grub_disk_cache_get_performance (&hits
, &misses
);
100 grub_printf ("Disk cache: hits = %u, misses = %u ", hits
, misses
);
103 unsigned long ratio
= hits
* 10000 / (hits
+ misses
);
104 grub_printf ("(%u.%u%%)\n", ratio
/ 100, ratio
% 100);
107 grub_printf ("(N/A)\n");
113 grub_mini_cmd_root (struct grub_command
*cmd
__attribute__ ((unused
)),
114 int argc
, char *argv
[])
121 char *device_name
= grub_file_get_device_name (argv
[0]);
125 grub_env_set ("root", device_name
);
126 grub_free (device_name
);
129 dev
= grub_device_open (0);
133 fs
= grub_fs_probe (dev
);
134 if (grub_errno
== GRUB_ERR_UNKNOWN_FS
)
135 grub_errno
= GRUB_ERR_NONE
;
137 grub_printf ("(%s): Filesystem is %s.\n",
138 grub_env_get ("root"), fs
? fs
->name
: "unknown");
140 grub_device_close (dev
);
147 grub_rescue_cmd_testload (int argc
, char *argv
[])
153 auto void read_func (unsigned long sector
, unsigned offset
, unsigned len
);
155 void read_func (unsigned long sector
__attribute__ ((unused
)),
156 unsigned offset
__attribute__ ((unused
)),
157 unsigned len
__attribute__ ((unused
)))
165 grub_error (GRUB_ERR_BAD_ARGUMENT
, "no file specified");
169 file
= grub_file_open (argv
[0]);
173 size
= grub_file_size (file
) & ~(GRUB_DISK_SECTOR_SIZE
- 1);
176 grub_file_close (file
);
180 buf
= grub_malloc (size
);
184 grub_printf ("Reading %s sequentially", argv
[0]);
185 file
->read_hook
= read_func
;
186 if (grub_file_read (file
, buf
, size
) != size
)
188 grub_printf (" Done.\n");
190 /* Read sequentially again. */
191 grub_printf ("Reading %s sequentially again", argv
[0]);
192 if (grub_file_seek (file
, 0) < 0)
195 for (pos
= 0; pos
< size
; pos
+= GRUB_DISK_SECTOR_SIZE
)
197 char sector
[GRUB_DISK_SECTOR_SIZE
];
199 if (grub_file_read (file
, sector
, GRUB_DISK_SECTOR_SIZE
)
200 != GRUB_DISK_SECTOR_SIZE
)
203 if (grub_memcmp (sector
, buf
+ pos
, GRUB_DISK_SECTOR_SIZE
) != 0)
205 grub_printf ("\nDiffers in %d\n", pos
);
209 grub_printf (" Done.\n");
211 /* Read backwards and compare. */
212 grub_printf ("Reading %s backwards", argv
[0]);
216 char sector
[GRUB_DISK_SECTOR_SIZE
];
218 pos
-= GRUB_DISK_SECTOR_SIZE
;
220 if (grub_file_seek (file
, pos
) < 0)
223 if (grub_file_read (file
, sector
, GRUB_DISK_SECTOR_SIZE
)
224 != GRUB_DISK_SECTOR_SIZE
)
227 if (grub_memcmp (sector
, buf
+ pos
, GRUB_DISK_SECTOR_SIZE
) != 0)
231 grub_printf ("\nDiffers in %d\n", pos
);
233 for (i
= 0; i
< GRUB_DISK_SECTOR_SIZE
; i
++)
234 grub_putchar (buf
[pos
+ i
]);
242 grub_printf (" Done.\n");
246 grub_file_close (file
);
251 /* dump ADDRESS [SIZE] */
253 grub_mini_cmd_dump (struct grub_command
*cmd
__attribute__ ((unused
)),
254 int argc
, char *argv
[])
257 grub_size_t size
= 4;
260 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "no address specified");
262 addr
= (grub_uint8_t
*) grub_strtoul (argv
[0], 0, 0);
267 size
= (grub_size_t
) grub_strtoul (argv
[1], 0, 0);
271 grub_printf ("%x%x ", *addr
>> 4, *addr
& 0xf);
280 grub_mini_cmd_rmmod (struct grub_command
*cmd
__attribute__ ((unused
)),
281 int argc
, char *argv
[])
286 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "no module specified");
288 mod
= grub_dl_get (argv
[0]);
290 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "no such module");
292 if (grub_dl_unref (mod
) <= 0)
293 grub_dl_unload (mod
);
300 grub_mini_cmd_lsmod (struct grub_command
*cmd
__attribute__ ((unused
)),
301 int argc
__attribute__ ((unused
)),
302 char *argv
[] __attribute__ ((unused
)))
306 grub_printf ("Name\tRef Count\tDependencies\n");
311 grub_printf ("%s\t%d\t\t", mod
->name
, mod
->ref_count
);
312 for (dep
= mod
->dep
; dep
; dep
= dep
->next
)
317 grub_printf ("%s", dep
->mod
->name
);
327 grub_mini_cmd_exit (struct grub_command
*cmd
__attribute__ ((unused
)),
328 int argc
__attribute__ ((unused
)),
329 char *argv
[] __attribute__ ((unused
)))
335 static grub_command_t cmd_cat
, cmd_help
, cmd_root
;
336 static grub_command_t cmd_dump
, cmd_rmmod
, cmd_lsmod
, cmd_exit
;
338 GRUB_MOD_INIT(minicmd
)
341 grub_register_command ("cat", grub_mini_cmd_cat
,
342 N_("FILE"), N_("Show the contents of a file."));
344 grub_register_command ("help", grub_mini_cmd_help
,
345 0, N_("Show this message."));
347 grub_register_command ("root", grub_mini_cmd_root
,
348 N_("[DEVICE]"), N_("Set the root device."));
350 grub_register_command ("dump", grub_mini_cmd_dump
,
351 N_("ADDR"), N_("Dump memory."));
353 grub_register_command ("rmmod", grub_mini_cmd_rmmod
,
354 N_("MODULE"), N_("Remove a module."));
356 grub_register_command ("lsmod", grub_mini_cmd_lsmod
,
357 0, N_("Show loaded modules."));
359 grub_register_command ("exit", grub_mini_cmd_exit
,
360 0, N_("Exit from GRUB."));
363 GRUB_MOD_FINI(minicmd
)
365 grub_unregister_command (cmd_cat
);
366 grub_unregister_command (cmd_help
);
367 grub_unregister_command (cmd_root
);
368 grub_unregister_command (cmd_dump
);
369 grub_unregister_command (cmd_rmmod
);
370 grub_unregister_command (cmd_lsmod
);
371 grub_unregister_command (cmd_exit
);