1 /* rescue.c - rescue mode */
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>
28 #include <grub/loader.h>
30 #include <grub/partition.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. */
43 grub_rescue_get_command_line (const char *prompt
)
49 grub_memset (linebuf
, 0, GRUB_RESCUE_BUF_SIZE
);
51 while ((c
= GRUB_TERM_ASCII_CHAR (grub_getkey ())) != '\n' && c
!= '\r')
55 if (pos
< GRUB_RESCUE_BUF_SIZE
- 1)
79 attempt_normal_mode (void)
83 cmd
= grub_command_find ("normal");
85 (cmd
->func
) (cmd
, 0, 0);
88 /* Enter the rescue mode. */
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 ("> ");
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");
110 char *line
= linebuf
;
116 /* Print an error, if any. */
118 grub_errno
= GRUB_ERR_NONE
;
120 /* Get a command line. */
121 grub_rescue_get_command_line ("grub rescue> ");
125 if (grub_parser_split_cmdline (line
, getline
, &n
, &args
) || n
< 0)
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], '=');
134 grub_env_set (args
[0], val
+ 1);
140 /* Get the command name. */
143 /* If nothing is specified, restart. */
150 cmd
= grub_command_find (name
);
153 (cmd
->func
) (cmd
, n
, &args
[1]);
157 grub_printf ("Unknown command `%s'\n", name
);
158 grub_printf ("Try `help' for usage\n");