]> git.proxmox.com Git - grub2.git/blob - kern/rescue.c
2004-03-14 Marco Gerards <metgerards@student.han.nl>
[grub2.git] / kern / rescue.c
1 /* rescue.c - rescue mode */
2 /*
3 * PUPA -- Preliminary Universal Programming Architecture for GRUB
4 * Copyright (C) 2002, 2003 Free Software Foundation, Inc.
5 *
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <pupa/kernel.h>
22 #include <pupa/rescue.h>
23 #include <pupa/term.h>
24 #include <pupa/misc.h>
25 #include <pupa/disk.h>
26 #include <pupa/file.h>
27 #include <pupa/mm.h>
28 #include <pupa/err.h>
29 #include <pupa/loader.h>
30 #include <pupa/dl.h>
31 #include <pupa/machine/partition.h>
32 #include <pupa/env.h>
33
34 #define PUPA_RESCUE_BUF_SIZE 256
35 #define PUPA_RESCUE_MAX_ARGS 20
36
37 struct pupa_rescue_command
38 {
39 const char *name;
40 void (*func) (int argc, char *argv[]);
41 const char *message;
42 struct pupa_rescue_command *next;
43 };
44 typedef struct pupa_rescue_command *pupa_rescue_command_t;
45
46 static char linebuf[PUPA_RESCUE_BUF_SIZE];
47
48 static pupa_rescue_command_t pupa_rescue_command_list;
49
50 void
51 pupa_rescue_register_command (const char *name,
52 void (*func) (int argc, char *argv[]),
53 const char *message)
54 {
55 pupa_rescue_command_t cmd;
56
57 cmd = (pupa_rescue_command_t) pupa_malloc (sizeof (*cmd));
58 if (! cmd)
59 return;
60
61 cmd->name = name;
62 cmd->func = func;
63 cmd->message = message;
64
65 cmd->next = pupa_rescue_command_list;
66 pupa_rescue_command_list = cmd;
67 }
68
69 void
70 pupa_rescue_unregister_command (const char *name)
71 {
72 pupa_rescue_command_t *p, q;
73
74 for (p = &pupa_rescue_command_list, q = *p; q; p = &(q->next), q = q->next)
75 if (pupa_strcmp (name, q->name) == 0)
76 {
77 *p = q->next;
78 pupa_free (q);
79 break;
80 }
81 }
82
83 /* Prompt to input a command and read the line. */
84 static void
85 pupa_rescue_get_command_line (const char *prompt)
86 {
87 int c;
88 int pos = 0;
89
90 pupa_printf (prompt);
91 pupa_memset (linebuf, 0, PUPA_RESCUE_BUF_SIZE);
92
93 while ((c = PUPA_TERM_ASCII_CHAR (pupa_getkey ())) != '\n' && c != '\r')
94 {
95 if (pupa_isprint (c))
96 {
97 if (pos < PUPA_RESCUE_BUF_SIZE - 1)
98 {
99 linebuf[pos++] = c;
100 pupa_putchar (c);
101 }
102 }
103 else if (c == '\b')
104 {
105 if (pos > 0)
106 {
107 linebuf[--pos] = 0;
108 pupa_putchar (c);
109 pupa_putchar (' ');
110 pupa_putchar (c);
111 }
112 }
113 pupa_refresh ();
114 }
115
116 pupa_putchar ('\n');
117 pupa_refresh ();
118 }
119
120 /* boot */
121 static void
122 pupa_rescue_cmd_boot (int argc __attribute__ ((unused)),
123 char *argv[] __attribute__ ((unused)))
124 {
125 pupa_loader_boot ();
126 }
127
128 /* cat FILE */
129 static void
130 pupa_rescue_cmd_cat (int argc, char *argv[])
131 {
132 pupa_file_t file;
133 char buf[PUPA_DISK_SECTOR_SIZE];
134 pupa_ssize_t size;
135
136 if (argc < 1)
137 {
138 pupa_error (PUPA_ERR_BAD_ARGUMENT, "no file specified");
139 return;
140 }
141
142 file = pupa_file_open (argv[0]);
143 if (! file)
144 return;
145
146 while ((size = pupa_file_read (file, buf, sizeof (buf))) > 0)
147 {
148 int i;
149
150 for (i = 0; i < size; i++)
151 {
152 unsigned char c = buf[i];
153
154 if (pupa_isprint (c) || pupa_isspace (c))
155 pupa_putchar (c);
156 else
157 {
158 pupa_setcolorstate (PUPA_TERM_COLOR_HIGHLIGHT);
159 pupa_printf ("<%x>", (int) c);
160 pupa_setcolorstate (PUPA_TERM_COLOR_STANDARD);
161 }
162 }
163 }
164
165 pupa_putchar ('\n');
166 pupa_refresh ();
167 pupa_file_close (file);
168 }
169
170 static int
171 pupa_rescue_print_disks (const char *name)
172 {
173 pupa_device_t dev;
174 auto int print_partition (const pupa_partition_t p);
175
176 int print_partition (const pupa_partition_t p)
177 {
178 char *pname = pupa_partition_get_name (p);
179
180 if (pname)
181 {
182 pupa_printf ("(%s,%s) ", name, pname);
183 pupa_free (pname);
184 }
185
186 return 0;
187 }
188
189 dev = pupa_device_open (name);
190 pupa_errno = PUPA_ERR_NONE;
191
192 if (dev)
193 {
194 pupa_printf ("(%s) ", name);
195
196 if (dev->disk && dev->disk->has_partitions)
197 {
198 pupa_partition_iterate (dev->disk, print_partition);
199 pupa_errno = PUPA_ERR_NONE;
200 }
201
202 pupa_device_close (dev);
203 }
204
205 return 0;
206 }
207
208 static int
209 pupa_rescue_print_files (const char *filename, int dir)
210 {
211 pupa_printf ("%s%s ", filename, dir ? "/" : "");
212
213 return 0;
214 }
215
216 /* ls [ARG] */
217 static void
218 pupa_rescue_cmd_ls (int argc, char *argv[])
219 {
220 if (argc < 1)
221 {
222 pupa_disk_dev_iterate (pupa_rescue_print_disks);
223 pupa_putchar ('\n');
224 pupa_refresh ();
225 }
226 else
227 {
228 char *device_name;
229 pupa_device_t dev;
230 pupa_fs_t fs;
231 char *path;
232
233 device_name = pupa_file_get_device_name (argv[0]);
234 dev = pupa_device_open (device_name);
235 if (! dev)
236 goto fail;
237
238 fs = pupa_fs_probe (dev);
239 path = pupa_strchr (argv[0], '/');
240
241 if (! path && ! device_name)
242 {
243 pupa_error (PUPA_ERR_BAD_ARGUMENT, "invalid argument");
244 goto fail;
245 }
246
247 if (! path)
248 {
249 if (pupa_errno == PUPA_ERR_UNKNOWN_FS)
250 pupa_errno = PUPA_ERR_NONE;
251
252 pupa_printf ("(%s): Filesystem is %s.\n",
253 device_name, fs ? fs->name : "unknown");
254 }
255 else if (fs)
256 {
257 (fs->dir) (dev, path, pupa_rescue_print_files);
258 pupa_putchar ('\n');
259 pupa_refresh ();
260 }
261
262 fail:
263 if (dev)
264 pupa_device_close (dev);
265
266 pupa_free (device_name);
267 }
268 }
269
270 /* help */
271 static void
272 pupa_rescue_cmd_help (int argc __attribute__ ((unused)),
273 char *argv[] __attribute__ ((unused)))
274 {
275 pupa_rescue_command_t p, q;
276
277 /* Sort the commands. This is not a good algorithm, but this is enough,
278 because rescue mode has a small number of commands. */
279 for (p = pupa_rescue_command_list; p; p = p->next)
280 for (q = p->next; q; q = q->next)
281 if (pupa_strcmp (p->name, q->name) > 0)
282 {
283 struct pupa_rescue_command tmp;
284
285 tmp.name = p->name;
286 tmp.func = p->func;
287 tmp.message = p->message;
288
289 p->name = q->name;
290 p->func = q->func;
291 p->message = q->message;
292
293 q->name = tmp.name;
294 q->func = tmp.func;
295 q->message = tmp.message;
296 }
297
298 /* Print them. */
299 for (p = pupa_rescue_command_list; p; p = p->next)
300 pupa_printf ("%s\t%s\n", p->name, p->message);
301 }
302
303 #if 0
304 static void
305 pupa_rescue_cmd_info (void)
306 {
307 extern void pupa_disk_cache_get_performance (unsigned long *,
308 unsigned long *);
309 unsigned long hits, misses;
310
311 pupa_disk_cache_get_performance (&hits, &misses);
312 pupa_printf ("Disk cache: hits = %u, misses = %u ", hits, misses);
313 if (hits + misses)
314 {
315 unsigned long ratio = hits * 10000 / (hits + misses);
316 pupa_printf ("(%u.%u%%)\n", ratio / 100, ratio % 100);
317 }
318 else
319 pupa_printf ("(N/A)\n");
320 }
321 #endif
322
323 /* root [DEVICE] */
324 static void
325 pupa_rescue_cmd_root (int argc, char *argv[])
326 {
327 pupa_device_t dev;
328 pupa_fs_t fs;
329
330 if (argc > 0)
331 {
332 char *device_name = pupa_file_get_device_name (argv[0]);
333 if (! device_name)
334 return;
335
336 pupa_device_set_root (device_name);
337 pupa_free (device_name);
338 }
339
340 dev = pupa_device_open (0);
341 if (! dev)
342 return;
343
344 fs = pupa_fs_probe (dev);
345 if (pupa_errno == PUPA_ERR_UNKNOWN_FS)
346 pupa_errno = PUPA_ERR_NONE;
347
348 pupa_printf ("(%s): Filesystem is %s.\n",
349 pupa_device_get_root (), fs ? fs->name : "unknown");
350
351 pupa_device_close (dev);
352 }
353
354 #if 0
355 static void
356 pupa_rescue_cmd_testload (int argc, char *argv[])
357 {
358 pupa_file_t file;
359 char *buf;
360 pupa_ssize_t size;
361 pupa_ssize_t pos;
362 auto void read_func (unsigned long sector, unsigned offset, unsigned len);
363
364 void read_func (unsigned long sector __attribute__ ((unused)),
365 unsigned offset __attribute__ ((unused)),
366 unsigned len __attribute__ ((unused)))
367 {
368 pupa_putchar ('.');
369 pupa_refresh ();
370 }
371
372 if (argc < 1)
373 {
374 pupa_error (PUPA_ERR_BAD_ARGUMENT, "no file specified");
375 return;
376 }
377
378 file = pupa_file_open (argv[0]);
379 if (! file)
380 return;
381
382 size = pupa_file_size (file) & ~(PUPA_DISK_SECTOR_SIZE - 1);
383 if (size == 0)
384 {
385 pupa_file_close (file);
386 return;
387 }
388
389 buf = pupa_malloc (size);
390 if (! buf)
391 goto fail;
392
393 pupa_printf ("Reading %s sequentially", argv[0]);
394 file->read_hook = read_func;
395 if (pupa_file_read (file, buf, size) != size)
396 goto fail;
397 pupa_printf (" Done.\n");
398
399 /* Read sequentially again. */
400 pupa_printf ("Reading %s sequentially again", argv[0]);
401 if (pupa_file_seek (file, 0) < 0)
402 goto fail;
403
404 for (pos = 0; pos < size; pos += PUPA_DISK_SECTOR_SIZE)
405 {
406 char sector[PUPA_DISK_SECTOR_SIZE];
407
408 if (pupa_file_read (file, sector, PUPA_DISK_SECTOR_SIZE)
409 != PUPA_DISK_SECTOR_SIZE)
410 goto fail;
411
412 if (pupa_memcmp (sector, buf + pos, PUPA_DISK_SECTOR_SIZE) != 0)
413 {
414 pupa_printf ("\nDiffers in %d\n", pos);
415 goto fail;
416 }
417 }
418 pupa_printf (" Done.\n");
419
420 /* Read backwards and compare. */
421 pupa_printf ("Reading %s backwards", argv[0]);
422 pos = size;
423 while (pos > 0)
424 {
425 char sector[PUPA_DISK_SECTOR_SIZE];
426
427 pos -= PUPA_DISK_SECTOR_SIZE;
428
429 if (pupa_file_seek (file, pos) < 0)
430 goto fail;
431
432 if (pupa_file_read (file, sector, PUPA_DISK_SECTOR_SIZE)
433 != PUPA_DISK_SECTOR_SIZE)
434 goto fail;
435
436 if (pupa_memcmp (sector, buf + pos, PUPA_DISK_SECTOR_SIZE) != 0)
437 {
438 int i;
439
440 pupa_printf ("\nDiffers in %d\n", pos);
441
442 for (i = 0; i < PUPA_DISK_SECTOR_SIZE; i++)
443 pupa_putchar (buf[pos + i]);
444
445 if (i)
446 pupa_refresh ();
447
448 goto fail;
449 }
450 }
451 pupa_printf (" Done.\n");
452
453 fail:
454
455 pupa_file_close (file);
456 pupa_free (buf);
457 }
458 #endif
459
460 /* dump ADDRESS [SIZE] */
461 static void
462 pupa_rescue_cmd_dump (int argc, char *argv[])
463 {
464 pupa_uint8_t *addr;
465 pupa_size_t size = 4;
466
467 if (argc == 0)
468 {
469 pupa_error (PUPA_ERR_BAD_ARGUMENT, "no address specified");
470 return;
471 }
472
473 addr = (pupa_uint8_t *) pupa_strtoul (argv[0], 0, 0);
474 if (pupa_errno)
475 return;
476
477 if (argc > 1)
478 size = (pupa_size_t) pupa_strtoul (argv[1], 0, 0);
479
480 while (size--)
481 {
482 pupa_printf ("%x%x ", *addr >> 4, *addr & 0xf);
483 addr++;
484 }
485 }
486
487 /* insmod MODULE */
488 static void
489 pupa_rescue_cmd_insmod (int argc, char *argv[])
490 {
491 char *p;
492 pupa_dl_t mod;
493
494 if (argc == 0)
495 {
496 pupa_error (PUPA_ERR_BAD_ARGUMENT, "no module specified");
497 return;
498 }
499
500 p = pupa_strchr (argv[0], '/');
501 if (! p)
502 mod = pupa_dl_load (argv[0]);
503 else
504 mod = pupa_dl_load_file (argv[0]);
505
506 if (mod)
507 pupa_dl_ref (mod);
508 }
509
510 /* rmmod MODULE */
511 static void
512 pupa_rescue_cmd_rmmod (int argc, char *argv[])
513 {
514 pupa_dl_t mod;
515
516 if (argc == 0)
517 {
518 pupa_error (PUPA_ERR_BAD_ARGUMENT, "no module specified");
519 return;
520 }
521
522 mod = pupa_dl_get (argv[0]);
523 if (! mod)
524 {
525 pupa_error (PUPA_ERR_BAD_ARGUMENT, "no such module");
526 return;
527 }
528
529 if (! pupa_dl_unref (mod))
530 pupa_dl_unload (mod);
531 }
532
533 /* lsmod */
534 static void
535 pupa_rescue_cmd_lsmod (int argc __attribute__ ((unused)),
536 char *argv[] __attribute__ ((unused)))
537 {
538 auto int print_module (pupa_dl_t mod);
539
540 int print_module (pupa_dl_t mod)
541 {
542 pupa_dl_dep_t dep;
543
544 pupa_printf ("%s\t%d\t\t", mod->name, mod->ref_count);
545 for (dep = mod->dep; dep; dep = dep->next)
546 {
547 if (dep != mod->dep)
548 pupa_putchar (',');
549
550 pupa_printf ("%s", dep->mod->name);
551 }
552 pupa_putchar ('\n');
553 pupa_refresh ();
554
555 return 0;
556 }
557
558 pupa_printf ("Name\tRef Count\tDependencies\n");
559 pupa_dl_iterate (print_module);
560 }
561
562 /* set ENVVAR=VALUE */
563 static void
564 pupa_rescue_cmd_set (int argc, char *argv[])
565 {
566 char *var;
567 char *val;
568
569 auto int print_env (struct pupa_env_var *env);
570
571 int print_env (struct pupa_env_var *env)
572 {
573 pupa_printf ("%s=%s\n", env->name, env->value);
574 return 0;
575 }
576
577 if (argc < 1)
578 {
579 pupa_env_iterate (print_env);
580 return;
581 }
582
583 var = argv[0];
584 val = pupa_strchr (var, '=');
585 if (! val)
586 {
587 pupa_error (PUPA_ERR_BAD_ARGUMENT, "not an assignment");
588 return;
589 }
590
591 val[0] = 0;
592 pupa_env_set (var, val + 1);
593 val[0] = '=';
594 }
595
596 static void
597 pupa_rescue_cmd_unset (int argc, char *argv[])
598 {
599 if (argc < 1)
600 {
601 pupa_error (PUPA_ERR_BAD_ARGUMENT, "no environment variable specified");
602 return;
603 }
604
605 pupa_env_unset (argv[0]);
606 }
607
608 static void
609 attempt_normal_mode (void)
610 {
611 pupa_rescue_command_t cmd;
612
613 for (cmd = pupa_rescue_command_list; cmd; cmd = cmd->next)
614 {
615 if (pupa_strcmp ("normal", cmd->name) == 0)
616 {
617 (cmd->func) (0, 0);
618 break;
619 }
620 }
621 }
622
623 /* Enter the rescue mode. */
624 void
625 pupa_enter_rescue_mode (void)
626 {
627 auto pupa_err_t getline (char **line);
628
629 pupa_err_t getline (char **line)
630 {
631 pupa_rescue_get_command_line ("> ");
632 *line = linebuf;
633 return 0;
634 }
635
636 /* First of all, attempt to execute the normal mode. */
637 attempt_normal_mode ();
638
639 pupa_printf ("Entering into rescue mode...\n");
640
641 pupa_rescue_register_command ("boot", pupa_rescue_cmd_boot,
642 "boot an operating system");
643 pupa_rescue_register_command ("cat", pupa_rescue_cmd_cat,
644 "show the contents of a file");
645 pupa_rescue_register_command ("help", pupa_rescue_cmd_help,
646 "show this message");
647 pupa_rescue_register_command ("ls", pupa_rescue_cmd_ls,
648 "list devices or files");
649 pupa_rescue_register_command ("root", pupa_rescue_cmd_root,
650 "set the root device");
651 pupa_rescue_register_command ("dump", pupa_rescue_cmd_dump,
652 "dump memory");
653 pupa_rescue_register_command ("insmod", pupa_rescue_cmd_insmod,
654 "insert a module");
655 pupa_rescue_register_command ("rmmod", pupa_rescue_cmd_rmmod,
656 "remove a module");
657 pupa_rescue_register_command ("lsmod", pupa_rescue_cmd_lsmod,
658 "show loaded modules");
659 pupa_rescue_register_command ("set", pupa_rescue_cmd_set,
660 "set an environment variable");
661 pupa_rescue_register_command ("unset", pupa_rescue_cmd_unset,
662 "remove an environment variable");
663
664 while (1)
665 {
666 char *line = linebuf;
667 char *name;
668 int n;
669 pupa_rescue_command_t cmd;
670 char **args;
671
672 /* Print an error, if any. */
673 pupa_print_error ();
674 pupa_errno = PUPA_ERR_NONE;
675
676 /* Get a command line. */
677 pupa_rescue_get_command_line ("pupa rescue> ");
678
679 if (pupa_split_cmdline (line, getline, &n, &args))
680 continue;
681
682 /* In case of an assignment set the environment accordingly
683 instead of calling a function. */
684 if (n == 0 && pupa_strchr (line, '='))
685 {
686 char *val = pupa_strchr (args[0], '=');
687 val[0] = 0;
688 pupa_env_set (args[0], val + 1);
689 val[0] = '=';
690 continue;
691 }
692
693 /* Get the command name. */
694 name = args[0];
695
696 /* If nothing is specified, restart. */
697 if (*name == '\0')
698 continue;
699
700 /* Find the command and execute it. */
701 for (cmd = pupa_rescue_command_list; cmd; cmd = cmd->next)
702 {
703 if (pupa_strcmp (name, cmd->name) == 0)
704 {
705 (cmd->func) (n, &args[1]);
706 break;
707 }
708 }
709
710 /* If not found, print an error message. */
711 if (! cmd)
712 {
713 pupa_printf ("Unknown command `%s'\n", name);
714 pupa_printf ("Try `help' for usage\n");
715 }
716 }
717 }