2009-04-13 Felix Zielcke <fzielcke@z-51.de>
[grub2/phcoder/solaris.git] / kern / rescue.c
blob69a5db90ee34a3e5ffc301520c4d69bd6cd97839
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>
33 #include <grub/list.h>
34 #include <grub/command.h>
36 #define GRUB_RESCUE_BUF_SIZE 256
37 #define GRUB_RESCUE_MAX_ARGS 20
39 static char linebuf[GRUB_RESCUE_BUF_SIZE];
41 /* Prompt to input a command and read the line. */
42 static void
43 grub_rescue_get_command_line (const char *prompt)
45 int c;
46 int pos = 0;
48 grub_printf (prompt);
49 grub_memset (linebuf, 0, GRUB_RESCUE_BUF_SIZE);
51 while ((c = GRUB_TERM_ASCII_CHAR (grub_getkey ())) != '\n' && c != '\r')
53 if (grub_isprint (c))
55 if (pos < GRUB_RESCUE_BUF_SIZE - 1)
57 linebuf[pos++] = c;
58 grub_putchar (c);
61 else if (c == '\b')
63 if (pos > 0)
65 linebuf[--pos] = 0;
66 grub_putchar (c);
67 grub_putchar (' ');
68 grub_putchar (c);
71 grub_refresh ();
74 grub_putchar ('\n');
75 grub_refresh ();
78 static void
79 attempt_normal_mode (void)
81 grub_command_t cmd;
83 cmd = grub_command_find ("normal");
84 if (cmd)
85 (cmd->func) (cmd, 0, 0);
88 /* Enter the rescue mode. */
89 void
90 grub_enter_rescue_mode (void)
92 auto grub_err_t getline (char **line);
94 grub_err_t getline (char **line)
96 grub_rescue_get_command_line ("> ");
97 *line = linebuf;
98 return 0;
101 grub_register_core_commands ();
103 /* First of all, attempt to execute the normal mode. */
104 attempt_normal_mode ();
106 grub_printf ("Entering rescue mode...\n");
108 while (1)
110 char *line = linebuf;
111 char *name;
112 int n;
113 grub_command_t cmd;
114 char **args;
116 /* Print an error, if any. */
117 grub_print_error ();
118 grub_errno = GRUB_ERR_NONE;
120 /* Get a command line. */
121 grub_rescue_get_command_line ("grub rescue> ");
122 if (line[0] == 0)
123 continue;
125 if (grub_parser_split_cmdline (line, getline, &n, &args) || n < 0)
126 continue;
128 /* In case of an assignment set the environment accordingly
129 instead of calling a function. */
130 if (n == 0 && grub_strchr (line, '='))
132 char *val = grub_strchr (args[0], '=');
133 val[0] = 0;
134 grub_env_set (args[0], val + 1);
135 val[0] = '=';
136 grub_free (args[0]);
137 continue;
140 /* Get the command name. */
141 name = args[0];
143 /* If nothing is specified, restart. */
144 if (*name == '\0')
146 grub_free (args[0]);
147 continue;
150 cmd = grub_command_find (name);
151 if (cmd)
153 (cmd->func) (cmd, n, &args[1]);
155 else
157 grub_printf ("Unknown command `%s'\n", name);
158 grub_printf ("Try `help' for usage\n");
161 grub_free (args[0]);