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>
33 grub_mini_cmd_cat (struct grub_command
*cmd
__attribute__ ((unused
)),
34 int argc
, char *argv
[])
37 char buf
[GRUB_DISK_SECTOR_SIZE
];
41 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "no file specified");
43 file
= grub_file_open (argv
[0]);
47 while ((size
= grub_file_read (file
, buf
, sizeof (buf
))) > 0)
51 for (i
= 0; i
< size
; i
++)
53 unsigned char c
= buf
[i
];
55 if ((grub_isprint (c
) || grub_isspace (c
)) && c
!= '\r')
59 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT
);
60 grub_printf ("<%x>", (int) c
);
61 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD
);
68 grub_file_close (file
);
75 grub_mini_cmd_help (struct grub_command
*cmd
__attribute__ ((unused
)),
76 int argc
__attribute__ ((unused
)),
77 char *argv
[] __attribute__ ((unused
)))
81 for (p
= grub_command_list
; p
; p
= p
->next
)
82 grub_printf ("%s (%d%c)\t%s\n", p
->name
,
83 p
->prio
& GRUB_PRIO_LIST_PRIO_MASK
,
84 (p
->prio
& GRUB_PRIO_LIST_FLAG_ACTIVE
) ? '+' : '-',
92 grub_rescue_cmd_info (void)
94 extern void grub_disk_cache_get_performance (unsigned long *,
96 unsigned long hits
, misses
;
98 grub_disk_cache_get_performance (&hits
, &misses
);
99 grub_printf ("Disk cache: hits = %u, misses = %u ", hits
, misses
);
102 unsigned long ratio
= hits
* 10000 / (hits
+ misses
);
103 grub_printf ("(%u.%u%%)\n", ratio
/ 100, ratio
% 100);
106 grub_printf ("(N/A)\n");
112 grub_mini_cmd_root (struct grub_command
*cmd
__attribute__ ((unused
)),
113 int argc
, char *argv
[])
120 char *device_name
= grub_file_get_device_name (argv
[0]);
124 grub_env_set ("root", device_name
);
125 grub_free (device_name
);
128 dev
= grub_device_open (0);
132 fs
= grub_fs_probe (dev
);
133 if (grub_errno
== GRUB_ERR_UNKNOWN_FS
)
134 grub_errno
= GRUB_ERR_NONE
;
136 grub_printf ("(%s): Filesystem is %s.\n",
137 grub_env_get ("root"), fs
? fs
->name
: "unknown");
139 grub_device_close (dev
);
146 grub_rescue_cmd_testload (int argc
, char *argv
[])
152 auto void read_func (unsigned long sector
, unsigned offset
, unsigned len
);
154 void read_func (unsigned long sector
__attribute__ ((unused
)),
155 unsigned offset
__attribute__ ((unused
)),
156 unsigned len
__attribute__ ((unused
)))
164 grub_error (GRUB_ERR_BAD_ARGUMENT
, "no file specified");
168 file
= grub_file_open (argv
[0]);
172 size
= grub_file_size (file
) & ~(GRUB_DISK_SECTOR_SIZE
- 1);
175 grub_file_close (file
);
179 buf
= grub_malloc (size
);
183 grub_printf ("Reading %s sequentially", argv
[0]);
184 file
->read_hook
= read_func
;
185 if (grub_file_read (file
, buf
, size
) != size
)
187 grub_printf (" Done.\n");
189 /* Read sequentially again. */
190 grub_printf ("Reading %s sequentially again", argv
[0]);
191 if (grub_file_seek (file
, 0) < 0)
194 for (pos
= 0; pos
< size
; pos
+= GRUB_DISK_SECTOR_SIZE
)
196 char sector
[GRUB_DISK_SECTOR_SIZE
];
198 if (grub_file_read (file
, sector
, GRUB_DISK_SECTOR_SIZE
)
199 != GRUB_DISK_SECTOR_SIZE
)
202 if (grub_memcmp (sector
, buf
+ pos
, GRUB_DISK_SECTOR_SIZE
) != 0)
204 grub_printf ("\nDiffers in %d\n", pos
);
208 grub_printf (" Done.\n");
210 /* Read backwards and compare. */
211 grub_printf ("Reading %s backwards", argv
[0]);
215 char sector
[GRUB_DISK_SECTOR_SIZE
];
217 pos
-= GRUB_DISK_SECTOR_SIZE
;
219 if (grub_file_seek (file
, pos
) < 0)
222 if (grub_file_read (file
, sector
, GRUB_DISK_SECTOR_SIZE
)
223 != GRUB_DISK_SECTOR_SIZE
)
226 if (grub_memcmp (sector
, buf
+ pos
, GRUB_DISK_SECTOR_SIZE
) != 0)
230 grub_printf ("\nDiffers in %d\n", pos
);
232 for (i
= 0; i
< GRUB_DISK_SECTOR_SIZE
; i
++)
233 grub_putchar (buf
[pos
+ i
]);
241 grub_printf (" Done.\n");
245 grub_file_close (file
);
250 /* dump ADDRESS [SIZE] */
252 grub_mini_cmd_dump (struct grub_command
*cmd
__attribute__ ((unused
)),
253 int argc
, char *argv
[])
256 grub_size_t size
= 4;
259 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "no address specified");
261 addr
= (grub_uint8_t
*) grub_strtoul (argv
[0], 0, 0);
266 size
= (grub_size_t
) grub_strtoul (argv
[1], 0, 0);
270 grub_printf ("%x%x ", *addr
>> 4, *addr
& 0xf);
279 grub_mini_cmd_rmmod (struct grub_command
*cmd
__attribute__ ((unused
)),
280 int argc
, char *argv
[])
285 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "no module specified");
287 mod
= grub_dl_get (argv
[0]);
289 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "no such module");
291 if (grub_dl_unref (mod
) <= 0)
292 grub_dl_unload (mod
);
299 grub_mini_cmd_lsmod (struct grub_command
*cmd
__attribute__ ((unused
)),
300 int argc
__attribute__ ((unused
)),
301 char *argv
[] __attribute__ ((unused
)))
303 auto int print_module (grub_dl_t mod
);
305 int print_module (grub_dl_t mod
)
309 grub_printf ("%s\t%d\t\t", mod
->name
, mod
->ref_count
);
310 for (dep
= mod
->dep
; dep
; dep
= dep
->next
)
315 grub_printf ("%s", dep
->mod
->name
);
323 grub_printf ("Name\tRef Count\tDependencies\n");
324 grub_dl_iterate (print_module
);
331 grub_mini_cmd_exit (struct grub_command
*cmd
__attribute__ ((unused
)),
332 int argc
__attribute__ ((unused
)),
333 char *argv
[] __attribute__ ((unused
)))
339 static grub_command_t cmd_cat
, cmd_help
, cmd_root
;
340 static grub_command_t cmd_dump
, cmd_rmmod
, cmd_lsmod
, cmd_exit
;
342 GRUB_MOD_INIT(minicmd
)
345 grub_register_command ("cat", grub_mini_cmd_cat
,
346 "cat FILE", "show the contents of a file");
348 grub_register_command ("help", grub_mini_cmd_help
,
349 0, "show this message");
351 grub_register_command ("root", grub_mini_cmd_root
,
352 "root [DEVICE]", "set the root device");
354 grub_register_command ("dump", grub_mini_cmd_dump
,
355 "dump ADDR", "dump memory");
357 grub_register_command ("rmmod", grub_mini_cmd_rmmod
,
358 "rmmod MODULE", "remove a module");
360 grub_register_command ("lsmod", grub_mini_cmd_lsmod
,
361 0, "show loaded modules");
363 grub_register_command ("exit", grub_mini_cmd_exit
,
364 0, "exit from GRUB");
367 GRUB_MOD_FINI(minicmd
)
369 grub_unregister_command (cmd_cat
);
370 grub_unregister_command (cmd_help
);
371 grub_unregister_command (cmd_root
);
372 grub_unregister_command (cmd_dump
);
373 grub_unregister_command (cmd_rmmod
);
374 grub_unregister_command (cmd_lsmod
);
375 grub_unregister_command (cmd_exit
);