2009-02-01 Felix Zielcke <fzielcke@z-51.de>
[grub2/phcoder/solaris.git] / kern / rescue.c
blobe333ab5f59678e7041923ca4080589027de711f0
1 /* rescue.c - rescue mode */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2003,2005,2007 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/>.
20 #include <grub/kernel.h>
21 #include <grub/rescue.h>
22 #include <grub/term.h>
23 #include <grub/misc.h>
24 #include <grub/disk.h>
25 #include <grub/file.h>
26 #include <grub/mm.h>
27 #include <grub/err.h>
28 #include <grub/loader.h>
29 #include <grub/dl.h>
30 #include <grub/partition.h>
31 #include <grub/env.h>
32 #include <grub/parser.h>
34 #define GRUB_RESCUE_BUF_SIZE 256
35 #define GRUB_RESCUE_MAX_ARGS 20
37 struct grub_rescue_command
39 const char *name;
40 void (*func) (int argc, char *argv[]);
41 const char *message;
42 struct grub_rescue_command *next;
44 typedef struct grub_rescue_command *grub_rescue_command_t;
46 static char linebuf[GRUB_RESCUE_BUF_SIZE];
48 static grub_rescue_command_t grub_rescue_command_list;
50 void
51 grub_rescue_register_command (const char *name,
52 void (*func) (int argc, char *argv[]),
53 const char *message)
55 grub_rescue_command_t cmd;
57 cmd = (grub_rescue_command_t) grub_malloc (sizeof (*cmd));
58 if (! cmd)
59 return;
61 cmd->name = name;
62 cmd->func = func;
63 cmd->message = message;
65 cmd->next = grub_rescue_command_list;
66 grub_rescue_command_list = cmd;
69 void
70 grub_rescue_unregister_command (const char *name)
72 grub_rescue_command_t *p, q;
74 for (p = &grub_rescue_command_list, q = *p; q; p = &(q->next), q = q->next)
75 if (grub_strcmp (name, q->name) == 0)
77 *p = q->next;
78 grub_free (q);
79 break;
83 /* Prompt to input a command and read the line. */
84 static void
85 grub_rescue_get_command_line (const char *prompt)
87 int c;
88 int pos = 0;
90 grub_printf (prompt);
91 grub_memset (linebuf, 0, GRUB_RESCUE_BUF_SIZE);
93 while ((c = GRUB_TERM_ASCII_CHAR (grub_getkey ())) != '\n' && c != '\r')
95 if (grub_isprint (c))
97 if (pos < GRUB_RESCUE_BUF_SIZE - 1)
99 linebuf[pos++] = c;
100 grub_putchar (c);
103 else if (c == '\b')
105 if (pos > 0)
107 linebuf[--pos] = 0;
108 grub_putchar (c);
109 grub_putchar (' ');
110 grub_putchar (c);
113 grub_refresh ();
116 grub_putchar ('\n');
117 grub_refresh ();
120 /* boot */
121 static void
122 grub_rescue_cmd_boot (int argc __attribute__ ((unused)),
123 char *argv[] __attribute__ ((unused)))
125 grub_loader_boot ();
128 /* cat FILE */
129 static void
130 grub_rescue_cmd_cat (int argc, char *argv[])
132 grub_file_t file;
133 char buf[GRUB_DISK_SECTOR_SIZE];
134 grub_ssize_t size;
136 if (argc < 1)
138 grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
139 return;
142 file = grub_file_open (argv[0]);
143 if (! file)
144 return;
146 while ((size = grub_file_read (file, buf, sizeof (buf))) > 0)
148 int i;
150 for (i = 0; i < size; i++)
152 unsigned char c = buf[i];
154 if ((grub_isprint (c) || grub_isspace (c)) && c != '\r')
155 grub_putchar (c);
156 else
158 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
159 grub_printf ("<%x>", (int) c);
160 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
165 grub_putchar ('\n');
166 grub_refresh ();
167 grub_file_close (file);
170 static int
171 grub_rescue_print_devices (const char *name)
173 grub_printf ("(%s) ", name);
175 return 0;
178 static int
179 grub_rescue_print_files (const char *filename, int dir)
181 grub_printf ("%s%s ", filename, dir ? "/" : "");
183 return 0;
186 /* ls [ARG] */
187 static void
188 grub_rescue_cmd_ls (int argc, char *argv[])
190 if (argc < 1)
192 grub_device_iterate (grub_rescue_print_devices);
193 grub_putchar ('\n');
194 grub_refresh ();
196 else
198 char *device_name;
199 grub_device_t dev;
200 grub_fs_t fs;
201 char *path;
203 device_name = grub_file_get_device_name (argv[0]);
204 dev = grub_device_open (device_name);
205 if (! dev)
206 goto fail;
208 fs = grub_fs_probe (dev);
209 path = grub_strchr (argv[0], ')');
210 if (! path)
211 path = argv[0];
212 else
213 path++;
215 if (! path && ! device_name)
217 grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
218 goto fail;
221 if (! path)
223 if (grub_errno == GRUB_ERR_UNKNOWN_FS)
224 grub_errno = GRUB_ERR_NONE;
226 grub_printf ("(%s): Filesystem is %s.\n",
227 device_name, fs ? fs->name : "unknown");
229 else if (fs)
231 (fs->dir) (dev, path, grub_rescue_print_files);
232 grub_putchar ('\n');
233 grub_refresh ();
236 fail:
237 if (dev)
238 grub_device_close (dev);
240 grub_free (device_name);
244 /* help */
245 static void
246 grub_rescue_cmd_help (int argc __attribute__ ((unused)),
247 char *argv[] __attribute__ ((unused)))
249 grub_rescue_command_t p, q;
251 /* Sort the commands. This is not a good algorithm, but this is enough,
252 because rescue mode has a small number of commands. */
253 for (p = grub_rescue_command_list; p; p = p->next)
254 for (q = p->next; q; q = q->next)
255 if (grub_strcmp (p->name, q->name) > 0)
257 struct grub_rescue_command tmp;
259 tmp.name = p->name;
260 tmp.func = p->func;
261 tmp.message = p->message;
263 p->name = q->name;
264 p->func = q->func;
265 p->message = q->message;
267 q->name = tmp.name;
268 q->func = tmp.func;
269 q->message = tmp.message;
272 /* Print them. */
273 for (p = grub_rescue_command_list; p; p = p->next)
274 grub_printf ("%s\t%s\n", p->name, p->message);
277 #if 0
278 static void
279 grub_rescue_cmd_info (void)
281 extern void grub_disk_cache_get_performance (unsigned long *,
282 unsigned long *);
283 unsigned long hits, misses;
285 grub_disk_cache_get_performance (&hits, &misses);
286 grub_printf ("Disk cache: hits = %u, misses = %u ", hits, misses);
287 if (hits + misses)
289 unsigned long ratio = hits * 10000 / (hits + misses);
290 grub_printf ("(%u.%u%%)\n", ratio / 100, ratio % 100);
292 else
293 grub_printf ("(N/A)\n");
295 #endif
297 /* root [DEVICE] */
298 static void
299 grub_rescue_cmd_root (int argc, char *argv[])
301 grub_device_t dev;
302 grub_fs_t fs;
304 if (argc > 0)
306 char *device_name = grub_file_get_device_name (argv[0]);
307 if (! device_name)
308 return;
310 grub_env_set ("root", device_name);
311 grub_free (device_name);
314 dev = grub_device_open (0);
315 if (! dev)
316 return;
318 fs = grub_fs_probe (dev);
319 if (grub_errno == GRUB_ERR_UNKNOWN_FS)
320 grub_errno = GRUB_ERR_NONE;
322 grub_printf ("(%s): Filesystem is %s.\n",
323 grub_env_get ("root"), fs ? fs->name : "unknown");
325 grub_device_close (dev);
328 #if 0
329 static void
330 grub_rescue_cmd_testload (int argc, char *argv[])
332 grub_file_t file;
333 char *buf;
334 grub_ssize_t size;
335 grub_ssize_t pos;
336 auto void read_func (unsigned long sector, unsigned offset, unsigned len);
338 void read_func (unsigned long sector __attribute__ ((unused)),
339 unsigned offset __attribute__ ((unused)),
340 unsigned len __attribute__ ((unused)))
342 grub_putchar ('.');
343 grub_refresh ();
346 if (argc < 1)
348 grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
349 return;
352 file = grub_file_open (argv[0]);
353 if (! file)
354 return;
356 size = grub_file_size (file) & ~(GRUB_DISK_SECTOR_SIZE - 1);
357 if (size == 0)
359 grub_file_close (file);
360 return;
363 buf = grub_malloc (size);
364 if (! buf)
365 goto fail;
367 grub_printf ("Reading %s sequentially", argv[0]);
368 file->read_hook = read_func;
369 if (grub_file_read (file, buf, size) != size)
370 goto fail;
371 grub_printf (" Done.\n");
373 /* Read sequentially again. */
374 grub_printf ("Reading %s sequentially again", argv[0]);
375 if (grub_file_seek (file, 0) < 0)
376 goto fail;
378 for (pos = 0; pos < size; pos += GRUB_DISK_SECTOR_SIZE)
380 char sector[GRUB_DISK_SECTOR_SIZE];
382 if (grub_file_read (file, sector, GRUB_DISK_SECTOR_SIZE)
383 != GRUB_DISK_SECTOR_SIZE)
384 goto fail;
386 if (grub_memcmp (sector, buf + pos, GRUB_DISK_SECTOR_SIZE) != 0)
388 grub_printf ("\nDiffers in %d\n", pos);
389 goto fail;
392 grub_printf (" Done.\n");
394 /* Read backwards and compare. */
395 grub_printf ("Reading %s backwards", argv[0]);
396 pos = size;
397 while (pos > 0)
399 char sector[GRUB_DISK_SECTOR_SIZE];
401 pos -= GRUB_DISK_SECTOR_SIZE;
403 if (grub_file_seek (file, pos) < 0)
404 goto fail;
406 if (grub_file_read (file, sector, GRUB_DISK_SECTOR_SIZE)
407 != GRUB_DISK_SECTOR_SIZE)
408 goto fail;
410 if (grub_memcmp (sector, buf + pos, GRUB_DISK_SECTOR_SIZE) != 0)
412 int i;
414 grub_printf ("\nDiffers in %d\n", pos);
416 for (i = 0; i < GRUB_DISK_SECTOR_SIZE; i++)
417 grub_putchar (buf[pos + i]);
419 if (i)
420 grub_refresh ();
422 goto fail;
425 grub_printf (" Done.\n");
427 fail:
429 grub_file_close (file);
430 grub_free (buf);
432 #endif
434 /* dump ADDRESS [SIZE] */
435 static void
436 grub_rescue_cmd_dump (int argc, char *argv[])
438 grub_uint8_t *addr;
439 grub_size_t size = 4;
441 if (argc == 0)
443 grub_error (GRUB_ERR_BAD_ARGUMENT, "no address specified");
444 return;
447 addr = (grub_uint8_t *) grub_strtoul (argv[0], 0, 0);
448 if (grub_errno)
449 return;
451 if (argc > 1)
452 size = (grub_size_t) grub_strtoul (argv[1], 0, 0);
454 while (size--)
456 grub_printf ("%x%x ", *addr >> 4, *addr & 0xf);
457 addr++;
461 /* insmod MODULE */
462 static void
463 grub_rescue_cmd_insmod (int argc, char *argv[])
465 char *p;
466 grub_dl_t mod;
468 if (argc == 0)
470 grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
471 return;
474 p = grub_strchr (argv[0], '/');
475 if (! p)
476 mod = grub_dl_load (argv[0]);
477 else
478 mod = grub_dl_load_file (argv[0]);
480 if (mod)
481 grub_dl_ref (mod);
484 /* rmmod MODULE */
485 static void
486 grub_rescue_cmd_rmmod (int argc, char *argv[])
488 grub_dl_t mod;
490 if (argc == 0)
492 grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
493 return;
496 mod = grub_dl_get (argv[0]);
497 if (! mod)
499 grub_error (GRUB_ERR_BAD_ARGUMENT, "no such module");
500 return;
503 if (grub_dl_unref (mod) <= 0)
504 grub_dl_unload (mod);
507 /* lsmod */
508 static void
509 grub_rescue_cmd_lsmod (int argc __attribute__ ((unused)),
510 char *argv[] __attribute__ ((unused)))
512 auto int print_module (grub_dl_t mod);
514 int print_module (grub_dl_t mod)
516 grub_dl_dep_t dep;
518 grub_printf ("%s\t%d\t\t", mod->name, mod->ref_count);
519 for (dep = mod->dep; dep; dep = dep->next)
521 if (dep != mod->dep)
522 grub_putchar (',');
524 grub_printf ("%s", dep->mod->name);
526 grub_putchar ('\n');
527 grub_refresh ();
529 return 0;
532 grub_printf ("Name\tRef Count\tDependencies\n");
533 grub_dl_iterate (print_module);
536 /* set ENVVAR=VALUE */
537 static void
538 grub_rescue_cmd_set (int argc, char *argv[])
540 char *var;
541 char *val;
543 auto int print_env (struct grub_env_var *env);
545 int print_env (struct grub_env_var *env)
547 grub_printf ("%s=%s\n", env->name, env->value);
548 return 0;
551 if (argc < 1)
553 grub_env_iterate (print_env);
554 return;
557 var = argv[0];
558 val = grub_strchr (var, '=');
559 if (! val)
561 grub_error (GRUB_ERR_BAD_ARGUMENT, "not an assignment");
562 return;
565 val[0] = 0;
566 grub_env_set (var, val + 1);
567 val[0] = '=';
570 static void
571 grub_rescue_cmd_unset (int argc, char *argv[])
573 if (argc < 1)
575 grub_error (GRUB_ERR_BAD_ARGUMENT, "no environment variable specified");
576 return;
579 grub_env_unset (argv[0]);
582 /* exit */
583 static void
584 grub_rescue_cmd_exit (int argc __attribute__ ((unused)),
585 char *argv[] __attribute__ ((unused)))
587 grub_exit ();
590 static void
591 attempt_normal_mode (void)
593 grub_rescue_command_t cmd;
595 for (cmd = grub_rescue_command_list; cmd; cmd = cmd->next)
597 if (grub_strcmp ("normal", cmd->name) == 0)
599 (cmd->func) (0, 0);
600 break;
605 /* Enter the rescue mode. */
606 void
607 grub_enter_rescue_mode (void)
609 auto grub_err_t getline (char **line);
611 grub_err_t getline (char **line)
613 grub_rescue_get_command_line ("> ");
614 *line = linebuf;
615 return 0;
618 /* First of all, attempt to execute the normal mode. */
619 attempt_normal_mode ();
621 grub_printf ("Entering rescue mode...\n");
623 grub_rescue_register_command ("boot", grub_rescue_cmd_boot,
624 "boot an operating system");
625 grub_rescue_register_command ("cat", grub_rescue_cmd_cat,
626 "show the contents of a file");
627 grub_rescue_register_command ("help", grub_rescue_cmd_help,
628 "show this message");
629 grub_rescue_register_command ("ls", grub_rescue_cmd_ls,
630 "list devices or files");
631 grub_rescue_register_command ("root", grub_rescue_cmd_root,
632 "set the root device");
633 grub_rescue_register_command ("dump", grub_rescue_cmd_dump,
634 "dump memory");
635 grub_rescue_register_command ("insmod", grub_rescue_cmd_insmod,
636 "insert a module");
637 grub_rescue_register_command ("rmmod", grub_rescue_cmd_rmmod,
638 "remove a module");
639 grub_rescue_register_command ("lsmod", grub_rescue_cmd_lsmod,
640 "show loaded modules");
641 grub_rescue_register_command ("set", grub_rescue_cmd_set,
642 "set an environment variable");
643 grub_rescue_register_command ("unset", grub_rescue_cmd_unset,
644 "remove an environment variable");
645 grub_rescue_register_command ("exit", grub_rescue_cmd_exit,
646 "exit from GRUB");
648 while (1)
650 char *line = linebuf;
651 char *name;
652 int n;
653 grub_rescue_command_t cmd;
654 char **args;
656 /* Print an error, if any. */
657 grub_print_error ();
658 grub_errno = GRUB_ERR_NONE;
660 /* Get a command line. */
661 grub_rescue_get_command_line ("grub rescue> ");
662 if (line[0] == 0)
663 continue;
665 if (grub_parser_split_cmdline (line, getline, &n, &args) || n < 0)
666 continue;
668 /* In case of an assignment set the environment accordingly
669 instead of calling a function. */
670 if (n == 0 && grub_strchr (line, '='))
672 char *val = grub_strchr (args[0], '=');
673 val[0] = 0;
674 grub_env_set (args[0], val + 1);
675 val[0] = '=';
676 grub_free (args[0]);
677 continue;
680 /* Get the command name. */
681 name = args[0];
683 /* If nothing is specified, restart. */
684 if (*name == '\0')
686 grub_free (args[0]);
687 continue;
690 /* Find the command and execute it. */
691 for (cmd = grub_rescue_command_list; cmd; cmd = cmd->next)
693 if (grub_strcmp (name, cmd->name) == 0)
695 (cmd->func) (n, &args[1]);
696 break;
700 /* If not found, print an error message. */
701 if (! cmd)
703 grub_printf ("Unknown command `%s'\n", name);
704 grub_printf ("Try `help' for usage\n");
707 grub_free (args[0]);