1 /* builtins.c - the GRUB builtin commands */ 2 /* 3 * GRUB -- GRand Unified Bootloader 4 * Copyright (C) 1999,2000,2001,2002,2003,2004 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 stdio.h before shared.h, because we can't define 22 WITHOUT_LIBC_STUBS here. */ 23 #ifdef GRUB_UTIL 24 # include <stdio.h> 25 #endif 26 27 #include <shared.h> 28 #include <filesys.h> 29 #include <term.h> 30 31 #ifdef SUPPORT_NETBOOT 32 # include <grub.h> 33 #endif 34 35 #ifdef SUPPORT_SERIAL 36 # include <serial.h> 37 # include <terminfo.h> 38 #endif 39 40 #ifdef GRUB_UTIL 41 # include <device.h> 42 #else /* ! GRUB_UTIL */ 43 # include <apic.h> 44 # include <smp-imps.h> 45 #endif /* ! GRUB_UTIL */ 46 47 #ifdef USE_MD5_PASSWORDS 48 # include <md5.h> 49 #endif 50 51 #include <cpu.h> 52 #include <expand.h> 53 54 /* The type of kernel loaded. */ 55 kernel_t kernel_type; 56 /* The boot device. */ 57 static int bootdev; 58 /* True when the debug mode is turned on, and false 59 when it is turned off. */ 60 int debug = 0; 61 /* The default entry. */ 62 int default_entry = 0; 63 /* The fallback entry. */ 64 int fallback_entryno; 65 int fallback_entries[MAX_FALLBACK_ENTRIES]; 66 /* The number of current entry. */ 67 int current_entryno; 68 /* The address for Multiboot command-line buffer. */ 69 static char *mb_cmdline; 70 /* The password. */ 71 char *password; 72 /* The password type. */ 73 password_t password_type; 74 /* The flag for indicating that the user is authoritative. */ 75 int auth = 0; 76 /* The timeout. */ 77 int grub_timeout = -1; 78 /* Whether to show the menu or not. */ 79 int show_menu = 1; 80 /* The BIOS drive map. */ 81 static unsigned short bios_drive_map[DRIVE_MAP_SIZE + 1]; 82 83 /* Prototypes for allowing straightfoward calling of builtins functions 84 inside other functions. */ 85 static int configfile_func (char *arg, int flags); 86 #ifdef SUPPORT_NETBOOT 87 static void solaris_config_file (void); 88 #endif 89 90 unsigned int min_mem64 = 0; 91 92 #if defined(__sun) && !defined(GRUB_UTIL) 93 extern void __enable_execute_stack (void *); 94 void 95 __enable_execute_stack (void *addr) 96 { 97 } 98 #endif /* __sun && !GRUB_UTIL */ 99 100 /* Initialize the data for builtins. */ 101 void 102 init_builtins (void) 103 { 104 kernel_type = KERNEL_TYPE_NONE; 105 /* BSD and chainloading evil hacks! */ 106 bootdev = set_bootdev (0); 107 mb_cmdline = (char *) MB_CMDLINE_BUF; 108 } 109 110 /* Initialize the data for the configuration file. */ 111 void 112 init_config (void) 113 { 114 default_entry = 0; 115 password = 0; 116 fallback_entryno = -1; 117 fallback_entries[0] = -1; 118 grub_timeout = -1; 119 current_rootpool[0] = '\0'; 120 current_bootfs[0] = '\0'; 121 current_bootpath[0] = '\0'; 122 current_bootfs_obj = 0; 123 current_devid[0] = '\0'; 124 is_zfs_mount = 0; 125 } 126 127 /* Check a password for correctness. Returns 0 if password was 128 correct, and a value != 0 for error, similarly to strcmp. */ 129 int 130 check_password (char *entered, char* expected, password_t type) 131 { 132 switch (type) 133 { 134 case PASSWORD_PLAIN: 135 return strcmp (entered, expected); 136 137 #ifdef USE_MD5_PASSWORDS 138 case PASSWORD_MD5: 139 return check_md5_password (entered, expected); 140 #endif 141 default: 142 /* unsupported password type: be secure */ 143 return 1; 144 } 145 } 146 147 /* Print which sector is read when loading a file. */ 148 static void 149 disk_read_print_func(unsigned int sector, int offset, int length) 150 { 151 grub_printf ("[%u,%d,%d]", sector, offset, length); 152 } 153 154 155 /* blocklist */ 156 static int 157 blocklist_func (char *arg, int flags) 158 { 159 char *dummy = (char *) RAW_ADDR (0x100000); 160 unsigned int start_sector = 0; 161 int num_sectors = 0; 162 int num_entries = 0; 163 int last_length = 0; 164 165 auto void disk_read_blocklist_func (unsigned int sector, int offset, 166 int length); 167 168 /* Collect contiguous blocks into one entry as many as possible, 169 and print the blocklist notation on the screen. */ 170 auto void disk_read_blocklist_func (unsigned int sector, int offset, 171 int length) 172 { 173 if (num_sectors > 0) 174 { 175 if (start_sector + num_sectors == sector 176 && offset == 0 && last_length == SECTOR_SIZE) 177 { 178 num_sectors++; 179 last_length = length; 180 return; 181 } 182 else 183 { 184 if (last_length == SECTOR_SIZE) 185 grub_printf ("%s%d+%d", num_entries ? "," : "", 186 start_sector - part_start, num_sectors); 187 else if (num_sectors > 1) 188 grub_printf ("%s%d+%d,%d[0-%d]", num_entries ? "," : "", 189 start_sector - part_start, num_sectors-1, 190 start_sector + num_sectors-1 - part_start, 191 last_length); 192 else 193 grub_printf ("%s%d[0-%d]", num_entries ? "," : "", 194 start_sector - part_start, last_length); 195 num_entries++; 196 num_sectors = 0; 197 } 198 } 199 200 if (offset > 0) 201 { 202 grub_printf("%s%u[%d-%d]", num_entries ? "," : "", 203 sector-part_start, offset, offset+length); 204 num_entries++; 205 } 206 else 207 { 208 start_sector = sector; 209 num_sectors = 1; 210 last_length = length; 211 } 212 } 213 214 /* Open the file. */ 215 if (! grub_open (arg)) 216 return 1; 217 218 /* Print the device name. */ 219 grub_printf ("(%cd%d", 220 (current_drive & 0x80) ? 'h' : 'f', 221 current_drive & ~0x80); 222 223 if ((current_partition & 0xFF0000) != 0xFF0000) 224 grub_printf (",%d", (current_partition >> 16) & 0xFF); 225 226 if ((current_partition & 0x00FF00) != 0x00FF00) 227 grub_printf (",%c", 'a' + ((current_partition >> 8) & 0xFF)); 228 229 grub_printf (")"); 230 231 /* Read in the whole file to DUMMY. */ 232 disk_read_hook = disk_read_blocklist_func; 233 if (! grub_read (dummy, -1)) 234 goto fail; 235 236 /* The last entry may not be printed yet. Don't check if it is a 237 * full sector, since it doesn't matter if we read too much. */ 238 if (num_sectors > 0) 239 grub_printf ("%s%d+%d", num_entries ? "," : "", 240 start_sector - part_start, num_sectors); 241 242 grub_printf ("\n"); 243 244 fail: 245 disk_read_hook = 0; 246 grub_close (); 247 return errnum; 248 } 249 250 static struct builtin builtin_blocklist = 251 { 252 "blocklist", 253 blocklist_func, 254 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 255 "blocklist FILE", 256 "Print the blocklist notation of the file FILE." 257 }; 258 259 /* boot */ 260 static int 261 boot_func (char *arg, int flags) 262 { 263 /* Clear the int15 handler if we can boot the kernel successfully. 264 This assumes that the boot code never fails only if KERNEL_TYPE is 265 not KERNEL_TYPE_NONE. Is this assumption is bad? */ 266 if (kernel_type != KERNEL_TYPE_NONE) 267 unset_int15_handler (); 268 269 #ifdef SUPPORT_NETBOOT 270 /* Shut down the networking. */ 271 cleanup_net (); 272 #endif 273 274 switch (kernel_type) 275 { 276 case KERNEL_TYPE_FREEBSD: 277 case KERNEL_TYPE_NETBSD: 278 /* *BSD */ 279 bsd_boot (kernel_type, bootdev, (char *) mbi.cmdline); 280 break; 281 282 case KERNEL_TYPE_LINUX: 283 /* Linux */ 284 linux_boot (); 285 break; 286 287 case KERNEL_TYPE_BIG_LINUX: 288 /* Big Linux */ 289 big_linux_boot (); 290 break; 291 292 case KERNEL_TYPE_CHAINLOADER: 293 /* Chainloader */ 294 295 /* Check if we should set the int13 handler. */ 296 if (bios_drive_map[0] != 0) 297 { 298 int i; 299 300 /* Search for SAVED_DRIVE. */ 301 for (i = 0; i < DRIVE_MAP_SIZE; i++) 302 { 303 if (! bios_drive_map[i]) 304 break; 305 else if ((bios_drive_map[i] & 0xFF) == saved_drive) 306 { 307 /* Exchage SAVED_DRIVE with the mapped drive. */ 308 saved_drive = (bios_drive_map[i] >> 8) & 0xFF; 309 break; 310 } 311 } 312 313 /* Set the handler. This is somewhat dangerous. */ 314 set_int13_handler (bios_drive_map); 315 } 316 317 gateA20 (0); 318 boot_drive = saved_drive; 319 chain_stage1 (0, BOOTSEC_LOCATION, boot_part_addr); 320 break; 321 322 case KERNEL_TYPE_MULTIBOOT: 323 /* Multiboot */ 324 #ifdef SUPPORT_NETBOOT 325 #ifdef SOLARIS_NETBOOT 326 if (current_drive == NETWORK_DRIVE) { 327 /* 328 * XXX Solaris hack: use drive_info to pass network information 329 * Turn off the flag bit to the loader is technically 330 * multiboot compliant. 331 */ 332 mbi.flags &= ~MB_INFO_DRIVE_INFO; 333 mbi.drives_length = dhcpack_length; 334 mbi.drives_addr = dhcpack_buf; 335 } 336 #endif /* SOLARIS_NETBOOT */ 337 #endif 338 multi_boot ((int) entry_addr, (int) &mbi); 339 break; 340 341 default: 342 errnum = ERR_BOOT_COMMAND; 343 return 1; 344 } 345 346 return 0; 347 } 348 349 static struct builtin builtin_boot = 350 { 351 "boot", 352 boot_func, 353 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 354 "boot", 355 "Boot the OS/chain-loader which has been loaded." 356 }; 357 358 359 #ifdef SUPPORT_NETBOOT 360 /* bootp */ 361 static int 362 bootp_func (char *arg, int flags) 363 { 364 int with_configfile = 0; 365 366 if (grub_memcmp (arg, "--with-configfile", sizeof ("--with-configfile") - 1) 367 == 0) 368 { 369 with_configfile = 1; 370 arg = skip_to (0, arg); 371 } 372 373 if (! bootp ()) 374 { 375 if (errnum == ERR_NONE) 376 errnum = ERR_DEV_VALUES; 377 378 return 1; 379 } 380 381 /* Notify the configuration. */ 382 print_network_configuration (); 383 384 /* XXX: this can cause an endless loop, but there is no easy way to 385 detect such a loop unfortunately. */ 386 if (with_configfile) 387 configfile_func (config_file, flags); 388 389 return 0; 390 } 391 392 static struct builtin builtin_bootp = 393 { 394 "bootp", 395 bootp_func, 396 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_HELP_LIST, 397 "bootp [--with-configfile]", 398 "Initialize a network device via BOOTP. If the option `--with-configfile'" 399 " is given, try to load a configuration file specified by the 150 vendor" 400 " tag." 401 }; 402 #endif /* SUPPORT_NETBOOT */ 403 404 405 /* cat */ 406 static int 407 cat_func (char *arg, int flags) 408 { 409 char c; 410 411 if (! grub_open (arg)) 412 return 1; 413 414 while (grub_read (&c, 1)) 415 { 416 /* Because running "cat" with a binary file can confuse the terminal, 417 print only some characters as they are. */ 418 if (grub_isspace (c) || (c >= ' ' && c <= '~')) 419 grub_putchar (c); 420 else 421 grub_putchar ('?'); 422 } 423 424 grub_close (); 425 return 0; 426 } 427 428 static struct builtin builtin_cat = 429 { 430 "cat", 431 cat_func, 432 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 433 "cat FILE", 434 "Print the contents of the file FILE." 435 }; 436 437 438 /* chainloader */ 439 static int 440 chainloader_func (char *arg, int flags) 441 { 442 int force = 0; 443 char *file = arg; 444 445 /* If the option `--force' is specified? */ 446 if (substring ("--force", arg) <= 0) 447 { 448 force = 1; 449 file = skip_to (0, arg); 450 } 451 452 /* Open the file. */ 453 if (! grub_open (file)) 454 { 455 kernel_type = KERNEL_TYPE_NONE; 456 return 1; 457 } 458 459 /* Read the first block. */ 460 if (grub_read ((char *) BOOTSEC_LOCATION, SECTOR_SIZE) != SECTOR_SIZE) 461 { 462 grub_close (); 463 kernel_type = KERNEL_TYPE_NONE; 464 465 /* This below happens, if a file whose size is less than 512 bytes 466 is loaded. */ 467 if (errnum == ERR_NONE) 468 errnum = ERR_EXEC_FORMAT; 469 470 return 1; 471 } 472 473 /* If not loading it forcibly, check for the signature. */ 474 if (! force 475 && (*((unsigned short *) (BOOTSEC_LOCATION + BOOTSEC_SIG_OFFSET)) 476 != BOOTSEC_SIGNATURE)) 477 { 478 grub_close (); 479 errnum = ERR_EXEC_FORMAT; 480 kernel_type = KERNEL_TYPE_NONE; 481 return 1; 482 } 483 484 grub_close (); 485 kernel_type = KERNEL_TYPE_CHAINLOADER; 486 487 /* XXX: Windows evil hack. For now, only the first five letters are 488 checked. */ 489 if (IS_PC_SLICE_TYPE_FAT (current_slice) 490 && ! grub_memcmp ((char *) BOOTSEC_LOCATION + BOOTSEC_BPB_SYSTEM_ID, 491 "MSWIN", 5)) 492 *((unsigned long *) (BOOTSEC_LOCATION + BOOTSEC_BPB_HIDDEN_SECTORS)) 493 = part_start; 494 495 errnum = ERR_NONE; 496 497 return 0; 498 } 499 500 static struct builtin builtin_chainloader = 501 { 502 "chainloader", 503 chainloader_func, 504 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 505 "chainloader [--force] FILE", 506 "Load the chain-loader FILE. If --force is specified, then load it" 507 " forcibly, whether the boot loader signature is present or not." 508 }; 509 510 511 /* This function could be used to debug new filesystem code. Put a file 512 in the new filesystem and the same file in a well-tested filesystem. 513 Then, run "cmp" with the files. If no output is obtained, probably 514 the code is good, otherwise investigate what's wrong... */ 515 /* cmp FILE1 FILE2 */ 516 static int 517 cmp_func (char *arg, int flags) 518 { 519 /* The filenames. */ 520 char *file1, *file2; 521 /* The addresses. */ 522 char *addr1, *addr2; 523 int i; 524 /* The size of the file. */ 525 int size; 526 527 /* Get the filenames from ARG. */ 528 file1 = arg; 529 file2 = skip_to (0, arg); 530 if (! *file1 || ! *file2) 531 { 532 errnum = ERR_BAD_ARGUMENT; 533 return 1; 534 } 535 536 /* Terminate the filenames for convenience. */ 537 nul_terminate (file1); 538 nul_terminate (file2); 539 540 /* Read the whole data from FILE1. */ 541 addr1 = (char *) RAW_ADDR (0x100000); 542 if (! grub_open (file1)) 543 return 1; 544 545 /* Get the size. */ 546 size = filemax; 547 if (grub_read (addr1, -1) != size) 548 { 549 grub_close (); 550 return 1; 551 } 552 553 grub_close (); 554 555 /* Read the whole data from FILE2. */ 556 addr2 = addr1 + size; 557 if (! grub_open (file2)) 558 return 1; 559 560 /* Check if the size of FILE2 is equal to the one of FILE2. */ 561 if (size != filemax) 562 { 563 grub_printf ("Differ in size: 0x%x [%s], 0x%x [%s]\n", 564 size, file1, filemax, file2); 565 grub_close (); 566 return 0; 567 } 568 569 if (! grub_read (addr2, -1)) 570 { 571 grub_close (); 572 return 1; 573 } 574 575 grub_close (); 576 577 /* Now compare ADDR1 with ADDR2. */ 578 for (i = 0; i < size; i++) 579 { 580 if (addr1[i] != addr2[i]) 581 grub_printf ("Differ at the offset %d: 0x%x [%s], 0x%x [%s]\n", 582 i, (unsigned) addr1[i], file1, 583 (unsigned) addr2[i], file2); 584 } 585 586 return 0; 587 } 588 589 static struct builtin builtin_cmp = 590 { 591 "cmp", 592 cmp_func, 593 BUILTIN_CMDLINE, 594 "cmp FILE1 FILE2", 595 "Compare the file FILE1 with the FILE2 and inform the different values" 596 " if any." 597 }; 598 599 600 /* color */ 601 /* Set new colors used for the menu interface. Support two methods to 602 specify a color name: a direct integer representation and a symbolic 603 color name. An example of the latter is "blink-light-gray/blue". */ 604 static int 605 color_func (char *arg, int flags) 606 { 607 char *normal; 608 char *highlight; 609 int new_normal_color; 610 int new_highlight_color; 611 static char *color_list[16] = 612 { 613 "black", 614 "blue", 615 "green", 616 "cyan", 617 "red", 618 "magenta", 619 "brown", 620 "light-gray", 621 "dark-gray", 622 "light-blue", 623 "light-green", 624 "light-cyan", 625 "light-red", 626 "light-magenta", 627 "yellow", 628 "white" 629 }; 630 631 auto int color_number (char *str); 632 633 /* Convert the color name STR into the magical number. */ 634 auto int color_number (char *str) 635 { 636 char *ptr; 637 int i; 638 int color = 0; 639 640 /* Find the separator. */ 641 for (ptr = str; *ptr && *ptr != '/'; ptr++) 642 ; 643 644 /* If not found, return -1. */ 645 if (! *ptr) 646 return -1; 647 648 /* Terminate the string STR. */ 649 *ptr++ = 0; 650 651 /* If STR contains the prefix "blink-", then set the `blink' bit 652 in COLOR. */ 653 if (substring ("blink-", str) <= 0) 654 { 655 color = 0x80; 656 str += 6; 657 } 658 659 /* Search for the color name. */ 660 for (i = 0; i < 16; i++) 661 if (grub_strcmp (color_list[i], str) == 0) 662 { 663 color |= i; 664 break; 665 } 666 667 if (i == 16) 668 return -1; 669 670 str = ptr; 671 nul_terminate (str); 672 673 /* Search for the color name. */ 674 for (i = 0; i < 8; i++) 675 if (grub_strcmp (color_list[i], str) == 0) 676 { 677 color |= i << 4; 678 break; 679 } 680 681 if (i == 8) 682 return -1; 683 684 return color; 685 } 686 687 normal = arg; 688 highlight = skip_to (0, arg); 689 690 new_normal_color = color_number (normal); 691 if (new_normal_color < 0 && ! safe_parse_maxint (&normal, &new_normal_color)) 692 return 1; 693 694 /* The second argument is optional, so set highlight_color 695 to inverted NORMAL_COLOR. */ 696 if (! *highlight) 697 new_highlight_color = ((new_normal_color >> 4) 698 | ((new_normal_color & 0xf) << 4)); 699 else 700 { 701 new_highlight_color = color_number (highlight); 702 if (new_highlight_color < 0 703 && ! safe_parse_maxint (&highlight, &new_highlight_color)) 704 return 1; 705 } 706 707 if (current_term->setcolor) 708 current_term->setcolor (new_normal_color, new_highlight_color); 709 710 return 0; 711 } 712 713 static struct builtin builtin_color = 714 { 715 "color", 716 color_func, 717 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_HELP_LIST, 718 "color NORMAL [HIGHLIGHT]", 719 "Change the menu colors. The color NORMAL is used for most" 720 " lines in the menu, and the color HIGHLIGHT is used to highlight the" 721 " line where the cursor points. If you omit HIGHLIGHT, then the" 722 " inverted color of NORMAL is used for the highlighted line." 723 " The format of a color is \"FG/BG\". FG and BG are symbolic color names." 724 " A symbolic color name must be one of these: black, blue, green," 725 " cyan, red, magenta, brown, light-gray, dark-gray, light-blue," 726 " light-green, light-cyan, light-red, light-magenta, yellow and white." 727 " But only the first eight names can be used for BG. You can prefix" 728 " \"blink-\" to FG if you want a blinking foreground color." 729 }; 730 731 732 /* configfile */ 733 static int 734 configfile_func (char *arg, int flags) 735 { 736 char *new_config = config_file; 737 738 /* Check if the file ARG is present. */ 739 if (! grub_open (arg)) 740 return 1; 741 742 grub_close (); 743 744 /* Copy ARG to CONFIG_FILE. */ 745 while ((*new_config++ = *arg++) != 0) 746 ; 747 748 #ifdef GRUB_UTIL 749 /* Force to load the configuration file. */ 750 use_config_file = 1; 751 #endif 752 753 /* Make sure that the user will not be authoritative. */ 754 auth = 0; 755 756 /* Restart cmain. */ 757 grub_longjmp (restart_env, 0); 758 759 /* Never reach here. */ 760 return 0; 761 } 762 763 static struct builtin builtin_configfile = 764 { 765 "configfile", 766 configfile_func, 767 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 768 "configfile FILE", 769 "Load FILE as the configuration file." 770 }; 771 772 773 /* debug */ 774 static int 775 debug_func (char *arg, int flags) 776 { 777 if (debug) 778 { 779 debug = 0; 780 grub_printf (" Debug mode is turned off\n"); 781 } 782 else 783 { 784 debug = 1; 785 grub_printf (" Debug mode is turned on\n"); 786 } 787 788 return 0; 789 } 790 791 static struct builtin builtin_debug = 792 { 793 "debug", 794 debug_func, 795 BUILTIN_CMDLINE, 796 "debug", 797 "Turn on/off the debug mode." 798 }; 799 800 801 /* default */ 802 static int 803 default_func (char *arg, int flags) 804 { 805 #ifndef SUPPORT_DISKLESS 806 if (grub_strcmp (arg, "saved") == 0) 807 { 808 default_entry = saved_entryno; 809 return 0; 810 } 811 #endif /* SUPPORT_DISKLESS */ 812 813 if (! safe_parse_maxint (&arg, &default_entry)) 814 return 1; 815 816 return 0; 817 } 818 819 static struct builtin builtin_default = 820 { 821 "default", 822 default_func, 823 BUILTIN_MENU, 824 #if 0 825 "default [NUM | `saved']", 826 "Set the default entry to entry number NUM (if not specified, it is" 827 " 0, the first entry) or the entry number saved by savedefault." 828 #endif 829 }; 830 831 832 #ifdef GRUB_UTIL 833 /* device */ 834 static int 835 device_func (char *arg, int flags) 836 { 837 char *drive = arg; 838 char *device; 839 840 /* Get the drive number from DRIVE. */ 841 if (! set_device (drive)) 842 return 1; 843 844 /* Get the device argument. */ 845 device = skip_to (0, drive); 846 847 /* Terminate DEVICE. */ 848 nul_terminate (device); 849 850 if (! *device || ! check_device (device)) 851 { 852 errnum = ERR_FILE_NOT_FOUND; 853 return 1; 854 } 855 856 assign_device_name (current_drive, device); 857 858 return 0; 859 } 860 861 static struct builtin builtin_device = 862 { 863 "device", 864 device_func, 865 BUILTIN_MENU | BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 866 "device DRIVE DEVICE", 867 "Specify DEVICE as the actual drive for a BIOS drive DRIVE. This command" 868 " can be used only in the grub shell." 869 }; 870 #endif /* GRUB_UTIL */ 871 872 #ifdef SUPPORT_NETBOOT 873 /* Debug Function for RPC */ 874 #ifdef RPC_DEBUG 875 /* portmap */ 876 static int 877 portmap_func (char *arg, int flags) 878 { 879 int port, prog, ver; 880 if (! grub_eth_probe ()){ 881 grub_printf ("No ethernet card found.\n"); 882 errnum = ERR_DEV_VALUES; 883 return 1; 884 } 885 if ((prog = getdec(&arg)) == -1){ 886 grub_printf("Error prog number\n"); 887 return 1; 888 } 889 arg = skip_to (0, arg); 890 if ((ver = getdec(&arg)) == -1){ 891 grub_printf("Error ver number\n"); 892 return 1; 893 } 894 port = __pmapudp_getport(ARP_SERVER, prog, ver); 895 printf("portmap getport %d", port); 896 return 0; 897 } 898 899 static struct builtin builtin_portmap = 900 { 901 "portmap", 902 portmap_func, 903 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 904 "portmap prog_number vers_number", 905 "Do portmap with the prog_number and vers_number" 906 }; 907 #endif /* RPC_DEBUG */ 908 909 /* dhcp */ 910 static int 911 dhcp_func (char *arg, int flags) 912 { 913 int with_configfile = 0; 914 915 if (grub_memcmp (arg, "--with-configfile", sizeof ("--with-configfile") - 1) 916 == 0) 917 { 918 with_configfile = 1; 919 arg = skip_to (0, arg); 920 } 921 922 if (! dhcp ()) 923 { 924 if (errnum == ERR_NONE) 925 errnum = ERR_DEV_VALUES; 926 927 return 1; 928 } 929 930 /* Notify the configuration. */ 931 print_network_configuration (); 932 933 /* XXX: this can cause an endless loop, but there is no easy way to 934 detect such a loop unfortunately. */ 935 if (with_configfile) 936 configfile_func (config_file, flags); 937 else 938 solaris_config_file(); 939 940 return 0; 941 } 942 943 static int 944 test_config_file(char *menufile) 945 { 946 int err; 947 948 /* 949 * If the file exists, make it the default. Else, fallback 950 * to what it was. Make sure we don't change errnum in the 951 * process. 952 */ 953 err = errnum; 954 if (grub_open(menufile)) { 955 grub_strcpy(config_file, menufile); 956 grub_close(); 957 errnum = err; 958 return (1); 959 } 960 errnum = err; 961 return (0); 962 } 963 964 static void solaris_config_file (void) 965 { 966 static char menufile[64]; 967 static char hexdigit[] = "0123456789ABCDEF"; 968 char *c = menufile; 969 int i; 970 971 /* 972 * if DHCP option 150 has been provided, config_file will 973 * already contain the string, try it. 974 */ 975 if (configfile_origin == CFG_150) { 976 if (test_config_file(config_file)) 977 return; 978 } 979 980 /* 981 * try to find host (MAC address) specific configfile: 982 * menu.lst.01<ether_addr> 983 */ 984 grub_strcpy(c, "menu.lst.01"); 985 c += grub_strlen(c); 986 for (i = 0; i < ETH_ALEN; i++) { 987 unsigned char b = arptable[ARP_CLIENT].node[i]; 988 *c++ = hexdigit[b >> 4]; 989 *c++ = hexdigit[b & 0xf]; 990 } 991 *c = 0; 992 configfile_origin = CFG_MAC; 993 if (test_config_file(menufile)) 994 return; 995 996 /* 997 * try to find a configfile derived from the DHCP/bootp 998 * BootFile string: menu.lst.<BootFile> 999 */ 1000 if (bootfile != NULL && bootfile[0] != 0) { 1001 c = menufile; 1002 grub_strcpy(c, "menu.lst."); 1003 c += grub_strlen("menu.lst."); 1004 i = grub_strlen("pxegrub."); 1005 if (grub_memcmp(bootfile, "pxegrub.", i) == 0) 1006 grub_strcpy(c, bootfile + i); 1007 else 1008 grub_strcpy(c, bootfile); 1009 configfile_origin = CFG_BOOTFILE; 1010 if (test_config_file(menufile)) 1011 return; 1012 } 1013 1014 /* 1015 * Default to hard coded "/boot/grub/menu.lst" config file. 1016 * This is the last resort, so there's no need to test it, 1017 * as there's nothing else to try. 1018 */ 1019 char *cp = config_file; 1020 /* skip leading slashes for tftp */ 1021 while (*cp == '/') 1022 ++cp; 1023 grub_memmove (config_file, cp, strlen(cp) + 1); 1024 configfile_origin = CFG_HARDCODED; 1025 } 1026 1027 static struct builtin builtin_dhcp = 1028 { 1029 "dhcp", 1030 dhcp_func, 1031 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_HELP_LIST, 1032 "dhcp", 1033 "Initialize a network device via DHCP." 1034 }; 1035 #endif /* SUPPORT_NETBOOT */ 1036 1037 static int terminal_func (char *arg, int flags); 1038 1039 static int verbose_func(char *arg, int flags) { 1040 1041 if (grub_strcmp(arg, "off") == 0) { 1042 silent.status = DEFER_SILENT; 1043 return; 1044 } else 1045 if (flags == BUILTIN_CMDLINE) { 1046 silent.status = DEFER_VERBOSE; 1047 return; 1048 } 1049 1050 silent.status = VERBOSE; 1051 1052 /* get back to text console */ 1053 if (current_term->shutdown) { 1054 (*current_term->shutdown)(); 1055 current_term = term_table; /* assumption: console is first */ 1056 } 1057 1058 /* dump the buffer */ 1059 if (!silent.looped) { 1060 /* if the buffer hasn't looped, just print it */ 1061 printf("%s", silent.buffer); 1062 } else { 1063 /* 1064 * If the buffer has looped, first print the oldest part of the buffer, 1065 * which is one past the current null. Then print the newer part which 1066 * starts at the beginning of the buffer. 1067 */ 1068 printf("%s", silent.buffer_start + 1); 1069 printf("%s", silent.buffer); 1070 } 1071 1072 return 0; 1073 } 1074 1075 static struct builtin builtin_verbose = 1076 { 1077 "verbose", 1078 verbose_func, 1079 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_SCRIPT | BUILTIN_HELP_LIST, 1080 "verbose", 1081 "Verbose output during menu entry (script) execution." 1082 }; 1083 1084 #ifdef SUPPORT_GRAPHICS 1085 1086 static int splashimage_func(char *arg, int flags) { 1087 char splashimage[64]; 1088 int i; 1089 1090 /* filename can only be 64 characters due to our buffer size */ 1091 if (strlen(arg) > 63) 1092 return 1; 1093 1094 if (flags == BUILTIN_SCRIPT) 1095 flags = BUILTIN_CMDLINE; 1096 1097 if (flags == BUILTIN_CMDLINE) { 1098 if (!grub_open(arg)) 1099 return 1; 1100 grub_close(); 1101 } 1102 1103 strcpy(splashimage, arg); 1104 1105 /* get rid of TERM_NEED_INIT from the graphics terminal. */ 1106 for (i = 0; term_table[i].name; i++) { 1107 if (grub_strcmp (term_table[i].name, "graphics") == 0) { 1108 term_table[i].flags &= ~TERM_NEED_INIT; 1109 break; 1110 } 1111 } 1112 1113 graphics_set_splash(splashimage); 1114 1115 if (flags == BUILTIN_CMDLINE && graphics_inited) { 1116 /* 1117 * calling graphics_end() here flickers the screen black. OTOH not 1118 * calling it gets us odd plane interlacing / early palette switching ? 1119 * ideally one should figure out how to double buffer and switch... 1120 */ 1121 graphics_end(); 1122 graphics_init(); 1123 graphics_cls(); 1124 } 1125 1126 /* 1127 * This call does not explicitly initialize graphics mode, but rather 1128 * simply sets the terminal type unless we're in command line mode and 1129 * call this function while in terminal mode. 1130 */ 1131 terminal_func("graphics", flags); 1132 1133 reset_term = 0; 1134 1135 return 0; 1136 } 1137 1138 static struct builtin builtin_splashimage = 1139 { 1140 "splashimage", 1141 splashimage_func, 1142 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_SCRIPT | BUILTIN_HELP_LIST, 1143 "splashimage FILE", 1144 "Load FILE as the background image when in graphics mode." 1145 }; 1146 1147 1148 /* foreground */ 1149 static int 1150 foreground_func(char *arg, int flags) 1151 { 1152 if (grub_strlen(arg) == 6) { 1153 int r = ((hex(arg[0]) << 4) | hex(arg[1])) >> 2; 1154 int g = ((hex(arg[2]) << 4) | hex(arg[3])) >> 2; 1155 int b = ((hex(arg[4]) << 4) | hex(arg[5])) >> 2; 1156 1157 foreground = (r << 16) | (g << 8) | b; 1158 if (graphics_inited) 1159 graphics_set_palette(15, r, g, b); 1160 1161 return (0); 1162 } 1163 1164 return (1); 1165 } 1166 1167 static struct builtin builtin_foreground = 1168 { 1169 "foreground", 1170 foreground_func, 1171 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_HELP_LIST | BUILTIN_SCRIPT, 1172 "foreground RRGGBB", 1173 "Sets the foreground color when in graphics mode." 1174 "RR is red, GG is green, and BB blue. Numbers must be in hexadecimal." 1175 }; 1176 1177 1178 /* background */ 1179 static int 1180 background_func(char *arg, int flags) 1181 { 1182 if (grub_strlen(arg) == 6) { 1183 int r = ((hex(arg[0]) << 4) | hex(arg[1])) >> 2; 1184 int g = ((hex(arg[2]) << 4) | hex(arg[3])) >> 2; 1185 int b = ((hex(arg[4]) << 4) | hex(arg[5])) >> 2; 1186 1187 background = (r << 16) | (g << 8) | b; 1188 if (graphics_inited) 1189 graphics_set_palette(0, r, g, b); 1190 return (0); 1191 } 1192 1193 return (1); 1194 } 1195 1196 static struct builtin builtin_background = 1197 { 1198 "background", 1199 background_func, 1200 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_HELP_LIST | BUILTIN_SCRIPT, 1201 "background RRGGBB", 1202 "Sets the background color when in graphics mode." 1203 "RR is red, GG is green, and BB blue. Numbers must be in hexadecimal." 1204 }; 1205 1206 #endif /* SUPPORT_GRAPHICS */ 1207 1208 1209 /* clear */ 1210 static int 1211 clear_func() 1212 { 1213 if (current_term->cls) 1214 current_term->cls(); 1215 1216 return 0; 1217 } 1218 1219 static struct builtin builtin_clear = 1220 { 1221 "clear", 1222 clear_func, 1223 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 1224 "clear", 1225 "Clear the screen" 1226 }; 1227 1228 /* displayapm */ 1229 static int 1230 displayapm_func (char *arg, int flags) 1231 { 1232 if (mbi.flags & MB_INFO_APM_TABLE) 1233 { 1234 grub_printf ("APM BIOS information:\n" 1235 " Version: 0x%x\n" 1236 " 32-bit CS: 0x%x\n" 1237 " Offset: 0x%x\n" 1238 " 16-bit CS: 0x%x\n" 1239 " 16-bit DS: 0x%x\n" 1240 " 32-bit CS length: 0x%x\n" 1241 " 16-bit CS length: 0x%x\n" 1242 " 16-bit DS length: 0x%x\n", 1243 (unsigned) apm_bios_info.version, 1244 (unsigned) apm_bios_info.cseg, 1245 apm_bios_info.offset, 1246 (unsigned) apm_bios_info.cseg_16, 1247 (unsigned) apm_bios_info.dseg_16, 1248 (unsigned) apm_bios_info.cseg_len, 1249 (unsigned) apm_bios_info.cseg_16_len, 1250 (unsigned) apm_bios_info.dseg_16_len); 1251 } 1252 else 1253 { 1254 grub_printf ("No APM BIOS found or probe failed\n"); 1255 } 1256 1257 return 0; 1258 } 1259 1260 static struct builtin builtin_displayapm = 1261 { 1262 "displayapm", 1263 displayapm_func, 1264 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 1265 "displayapm", 1266 "Display APM BIOS information." 1267 }; 1268 1269 1270 /* displaymem */ 1271 static int 1272 displaymem_func (char *arg, int flags) 1273 { 1274 if (get_eisamemsize () != -1) 1275 grub_printf (" EISA Memory BIOS Interface is present\n"); 1276 if (get_mmap_entry ((void *) SCRATCHADDR, 0) != 0 1277 || *((int *) SCRATCHADDR) != 0) 1278 grub_printf (" Address Map BIOS Interface is present\n"); 1279 1280 grub_printf (" Lower memory: %uK, " 1281 "Upper memory (to first chipset hole): %uK\n", 1282 mbi.mem_lower, mbi.mem_upper); 1283 1284 if (min_mem64 != 0) 1285 grub_printf (" Memory limit for 64-bit ISADIR expansion: %uMB\n", 1286 min_mem64); 1287 1288 if (mbi.flags & MB_INFO_MEM_MAP) 1289 { 1290 struct AddrRangeDesc *map = (struct AddrRangeDesc *) mbi.mmap_addr; 1291 int end_addr = mbi.mmap_addr + mbi.mmap_length; 1292 1293 grub_printf (" [Address Range Descriptor entries " 1294 "immediately follow (values are 64-bit)]\n"); 1295 while (end_addr > (int) map) 1296 { 1297 char *str; 1298 1299 if (map->Type == MB_ARD_MEMORY) 1300 str = "Usable RAM"; 1301 else 1302 str = "Reserved"; 1303 grub_printf (" %s: Base Address: 0x%x X 4GB + 0x%x,\n" 1304 " Length: 0x%x X 4GB + 0x%x bytes\n", 1305 str, 1306 (unsigned long) (map->BaseAddr >> 32), 1307 (unsigned long) (map->BaseAddr & 0xFFFFFFFF), 1308 (unsigned long) (map->Length >> 32), 1309 (unsigned long) (map->Length & 0xFFFFFFFF)); 1310 1311 map = ((struct AddrRangeDesc *) (((int) map) + 4 + map->size)); 1312 } 1313 } 1314 1315 return 0; 1316 } 1317 1318 static struct builtin builtin_displaymem = 1319 { 1320 "displaymem", 1321 displaymem_func, 1322 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 1323 "displaymem", 1324 "Display what GRUB thinks the system address space map of the" 1325 " machine is, including all regions of physical RAM installed." 1326 }; 1327 1328 1329 /* dump FROM TO */ 1330 #ifdef GRUB_UTIL 1331 static int 1332 dump_func (char *arg, int flags) 1333 { 1334 char *from, *to; 1335 FILE *fp; 1336 char c; 1337 1338 from = arg; 1339 to = skip_to (0, arg); 1340 if (! *from || ! *to) 1341 { 1342 errnum = ERR_BAD_ARGUMENT; 1343 return 1; 1344 } 1345 1346 nul_terminate (from); 1347 nul_terminate (to); 1348 1349 if (! grub_open (from)) 1350 return 1; 1351 1352 fp = fopen (to, "w"); 1353 if (! fp) 1354 { 1355 errnum = ERR_WRITE; 1356 return 1; 1357 } 1358 1359 while (grub_read (&c, 1)) 1360 if (fputc (c, fp) == EOF) 1361 { 1362 errnum = ERR_WRITE; 1363 fclose (fp); 1364 return 1; 1365 } 1366 1367 if (fclose (fp) == EOF) 1368 { 1369 errnum = ERR_WRITE; 1370 return 1; 1371 } 1372 1373 grub_close (); 1374 return 0; 1375 } 1376 1377 static struct builtin builtin_dump = 1378 { 1379 "dump", 1380 dump_func, 1381 BUILTIN_CMDLINE, 1382 "dump FROM TO", 1383 "Dump the contents of the file FROM to the file TO. FROM must be" 1384 " a GRUB file and TO must be an OS file." 1385 }; 1386 #endif /* GRUB_UTIL */ 1387 1388 1389 static char embed_info[32]; 1390 /* embed */ 1391 /* Embed a Stage 1.5 in the first cylinder after MBR or in the 1392 bootloader block in a FFS. */ 1393 static int 1394 embed_func (char *arg, int flags) 1395 { 1396 char *stage1_5; 1397 char *device; 1398 char *stage1_5_buffer = (char *) RAW_ADDR (0x100000); 1399 int len, size; 1400 int sector; 1401 1402 stage1_5 = arg; 1403 device = skip_to (0, stage1_5); 1404 1405 /* Open a Stage 1.5. */ 1406 if (! grub_open (stage1_5)) 1407 return 1; 1408 1409 /* Read the whole of the Stage 1.5. */ 1410 len = grub_read (stage1_5_buffer, -1); 1411 grub_close (); 1412 1413 if (errnum) 1414 return 1; 1415 1416 size = (len + SECTOR_SIZE - 1) / SECTOR_SIZE; 1417 1418 /* Get the device where the Stage 1.5 will be embedded. */ 1419 set_device (device); 1420 if (errnum) 1421 return 1; 1422 1423 if (current_partition == 0xFFFFFF) 1424 { 1425 /* Embed it after the MBR. */ 1426 1427 char mbr[SECTOR_SIZE]; 1428 char ezbios_check[2*SECTOR_SIZE]; 1429 int i; 1430 1431 /* Open the partition. */ 1432 if (! open_partition ()) 1433 return 1; 1434 1435 /* No floppy has MBR. */ 1436 if (! (current_drive & 0x80)) 1437 { 1438 errnum = ERR_DEV_VALUES; 1439 return 1; 1440 } 1441 1442 /* Read the MBR of CURRENT_DRIVE. */ 1443 if (! rawread (current_drive, PC_MBR_SECTOR, 0, SECTOR_SIZE, mbr)) 1444 return 1; 1445 1446 /* Sanity check. */ 1447 if (! PC_MBR_CHECK_SIG (mbr)) 1448 { 1449 errnum = ERR_BAD_PART_TABLE; 1450 return 1; 1451 } 1452 1453 /* Check if the disk can store the Stage 1.5. */ 1454 for (i = 0; i < 4; i++) 1455 if (PC_SLICE_TYPE (mbr, i) && PC_SLICE_START (mbr, i) - 1 < size) 1456 { 1457 errnum = ERR_NO_DISK_SPACE; 1458 return 1; 1459 } 1460 1461 /* Check for EZ-BIOS signature. It should be in the third 1462 * sector, but due to remapping it can appear in the second, so 1463 * load and check both. 1464 */ 1465 if (! rawread (current_drive, 1, 0, 2 * SECTOR_SIZE, ezbios_check)) 1466 return 1; 1467 1468 if (! memcmp (ezbios_check + 3, "AERMH", 5) 1469 || ! memcmp (ezbios_check + 512 + 3, "AERMH", 5)) 1470 { 1471 /* The space after the MBR is used by EZ-BIOS which we must 1472 * not overwrite. 1473 */ 1474 errnum = ERR_NO_DISK_SPACE; 1475 return 1; 1476 } 1477 1478 sector = 1; 1479 } 1480 else 1481 { 1482 /* Embed it in the bootloader block in the filesystem. */ 1483 int start_sector; 1484 1485 /* Open the partition. */ 1486 if (! open_device ()) 1487 return 1; 1488 1489 /* Check if the current slice supports embedding. */ 1490 if (fsys_table[fsys_type].embed_func == 0 1491 || ! fsys_table[fsys_type].embed_func (&start_sector, size)) 1492 { 1493 errnum = ERR_DEV_VALUES; 1494 return 1; 1495 } 1496 1497 sector = part_start + start_sector; 1498 } 1499 1500 /* Clear the cache. */ 1501 buf_track = BUF_CACHE_INVALID; 1502 1503 /* Now perform the embedding. */ 1504 if (! devwrite (sector - part_start, size, stage1_5_buffer)) 1505 return 1; 1506 1507 grub_printf (" %d sectors are embedded.\n", size); 1508 grub_sprintf (embed_info, "%d+%d", sector - part_start, size); 1509 return 0; 1510 } 1511 1512 static struct builtin builtin_embed = 1513 { 1514 "embed", 1515 embed_func, 1516 BUILTIN_CMDLINE, 1517 "embed STAGE1_5 DEVICE", 1518 "Embed the Stage 1.5 STAGE1_5 in the sectors after MBR if DEVICE" 1519 " is a drive, or in the \"bootloader\" area if DEVICE is a FFS partition." 1520 " Print the number of sectors which STAGE1_5 occupies if successful." 1521 }; 1522 1523 1524 /* fallback */ 1525 static int 1526 fallback_func (char *arg, int flags) 1527 { 1528 int i = 0; 1529 1530 while (*arg) 1531 { 1532 int entry; 1533 int j; 1534 1535 if (! safe_parse_maxint (&arg, &entry)) 1536 return 1; 1537 1538 /* Remove duplications to prevent infinite looping. */ 1539 for (j = 0; j < i; j++) 1540 if (entry == fallback_entries[j]) 1541 break; 1542 if (j != i) 1543 continue; 1544 1545 fallback_entries[i++] = entry; 1546 if (i == MAX_FALLBACK_ENTRIES) 1547 break; 1548 1549 arg = skip_to (0, arg); 1550 } 1551 1552 if (i < MAX_FALLBACK_ENTRIES) 1553 fallback_entries[i] = -1; 1554 1555 fallback_entryno = (i == 0) ? -1 : 0; 1556 1557 return 0; 1558 } 1559 1560 static struct builtin builtin_fallback = 1561 { 1562 "fallback", 1563 fallback_func, 1564 BUILTIN_MENU, 1565 #if 0 1566 "fallback NUM...", 1567 "Go into unattended boot mode: if the default boot entry has any" 1568 " errors, instead of waiting for the user to do anything, it" 1569 " immediately starts over using the NUM entry (same numbering as the" 1570 " `default' command). This obviously won't help if the machine" 1571 " was rebooted by a kernel that GRUB loaded." 1572 #endif 1573 }; 1574 1575 1576 1577 void 1578 set_root (char *root, unsigned long drive, unsigned long part) 1579 { 1580 int bsd_part = (part >> 8) & 0xFF; 1581 int pc_slice = part >> 16; 1582 1583 if (bsd_part == 0xFF) { 1584 grub_sprintf (root, "(hd%d,%d)\n", drive - 0x80, pc_slice); 1585 } else { 1586 grub_sprintf (root, "(hd%d,%d,%c)\n", 1587 drive - 0x80, pc_slice, bsd_part + 'a'); 1588 } 1589 } 1590 1591 static int 1592 find_common (char *arg, char *root, int for_root, int flags) 1593 { 1594 char *filename = NULL; 1595 static char argpart[32]; 1596 static char device[32]; 1597 char *tmp_argpart = NULL; 1598 unsigned long drive; 1599 unsigned long tmp_drive = saved_drive; 1600 unsigned long tmp_partition = saved_partition; 1601 int got_file = 0; 1602 static char bootsign[BOOTSIGN_LEN]; 1603 1604 /* 1605 * If argument has partition information (findroot command only), then 1606 * it can't be a floppy 1607 */ 1608 if (for_root && arg[0] == '(') { 1609 tmp_argpart = grub_strchr(arg + 1, ','); 1610 if (tmp_argpart == NULL) 1611 goto out; 1612 grub_strcpy(argpart, tmp_argpart); 1613 *tmp_argpart = '\0'; 1614 arg++; 1615 grub_sprintf(bootsign, "%s/%s", BOOTSIGN_DIR, arg); 1616 filename = bootsign; 1617 goto harddisk; 1618 } else if (for_root && !grub_strchr(arg, '/')) { 1619 /* Boot signature without partition/slice information */ 1620 grub_sprintf(bootsign, "%s/%s", BOOTSIGN_DIR, arg); 1621 filename = bootsign; 1622 } else { 1623 /* plain vanilla find cmd */ 1624 filename = arg; 1625 } 1626 1627 /* Floppies. */ 1628 for (drive = 0; drive < 8; drive++) 1629 { 1630 current_drive = drive; 1631 current_partition = 0xFFFFFF; 1632 1633 if (open_device ()) 1634 { 1635 saved_drive = current_drive; 1636 saved_partition = current_partition; 1637 if (grub_open (filename)) 1638 { 1639 grub_close (); 1640 got_file = 1; 1641 if (for_root) { 1642 grub_sprintf(root, "(fd%d)", drive); 1643 goto out; 1644 } else 1645 grub_printf (" (fd%d)\n", drive); 1646 } 1647 } 1648 1649 errnum = ERR_NONE; 1650 } 1651 1652 harddisk: 1653 /* Hard disks. */ 1654 for (drive = 0x80; drive < 0x88; drive++) 1655 { 1656 unsigned long part = 0xFFFFFF; 1657 unsigned long start, len, offset, ext_offset; 1658 int type, entry; 1659 char buf[SECTOR_SIZE]; 1660 1661 if (for_root && tmp_argpart) { 1662 grub_sprintf(device, "(hd%d%s", drive - 0x80, argpart); 1663 set_device(device); 1664 errnum = ERR_NONE; 1665 part = current_partition; 1666 if (open_device ()) { 1667 saved_drive = current_drive; 1668 saved_partition = current_partition; 1669 errnum = ERR_NONE; 1670 if (grub_open (filename)) { 1671 grub_close (); 1672 got_file = 1; 1673 if (is_zfs_mount == 0) { 1674 set_root(root, current_drive, current_partition); 1675 goto out; 1676 } else { 1677 best_drive = current_drive; 1678 best_part = current_partition; 1679 } 1680 } 1681 } 1682 errnum = ERR_NONE; 1683 continue; 1684 } 1685 current_drive = drive; 1686 while (next_partition (drive, 0xFFFFFF, &part, &type, 1687 &start, &len, &offset, &entry, 1688 &ext_offset, buf)) 1689 { 1690 if (type != PC_SLICE_TYPE_NONE 1691 && ! IS_PC_SLICE_TYPE_BSD (type) 1692 && ! IS_PC_SLICE_TYPE_EXTENDED (type)) 1693 { 1694 current_partition = part; 1695 if (open_device ()) 1696 { 1697 saved_drive = current_drive; 1698 saved_partition = current_partition; 1699 if (grub_open (filename)) 1700 { 1701 char tmproot[32]; 1702 1703 grub_close (); 1704 got_file = 1; 1705 set_root(tmproot, drive, part); 1706 if (for_root) { 1707 grub_memcpy(root, tmproot, sizeof(tmproot)); 1708 if (is_zfs_mount == 0) { 1709 goto out; 1710 } else { 1711 best_drive = current_drive; 1712 best_part = current_partition; 1713 } 1714 } else { 1715 grub_printf("%s", tmproot); 1716 } 1717 } 1718 } 1719 } 1720 1721 /* We want to ignore any error here. */ 1722 errnum = ERR_NONE; 1723 } 1724 1725 /* next_partition always sets ERRNUM in the last call, so clear 1726 it. */ 1727 errnum = ERR_NONE; 1728 } 1729 1730 out: 1731 if (is_zfs_mount && for_root) { 1732 set_root(root, best_drive, best_part); 1733 buf_drive = -1; 1734 } else { 1735 saved_drive = tmp_drive; 1736 saved_partition = tmp_partition; 1737 } 1738 if (tmp_argpart) 1739 *tmp_argpart = ','; 1740 1741 if (got_file) 1742 { 1743 errnum = ERR_NONE; 1744 return 0; 1745 } 1746 1747 errnum = ERR_FILE_NOT_FOUND; 1748 return 1; 1749 } 1750 1751 /* find */ 1752 /* Search for the filename ARG in all of partitions. */ 1753 static int 1754 find_func (char *arg, int flags) 1755 { 1756 return (find_common(arg, NULL, 0, flags)); 1757 } 1758 1759 static struct builtin builtin_find = 1760 { 1761 "find", 1762 find_func, 1763 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 1764 "find FILENAME", 1765 "Search for the filename FILENAME in all of partitions and print the list of" 1766 " the devices which contain the file." 1767 }; 1768 1769 1770 /* fstest */ 1771 static int 1772 fstest_func (char *arg, int flags) 1773 { 1774 if (disk_read_hook) 1775 { 1776 disk_read_hook = NULL; 1777 printf (" Filesystem tracing is now off\n"); 1778 } 1779 else 1780 { 1781 disk_read_hook = disk_read_print_func; 1782 printf (" Filesystem tracing is now on\n"); 1783 } 1784 1785 return 0; 1786 } 1787 1788 static struct builtin builtin_fstest = 1789 { 1790 "fstest", 1791 fstest_func, 1792 BUILTIN_CMDLINE, 1793 "fstest", 1794 "Toggle filesystem test mode." 1795 }; 1796 1797 1798 /* geometry */ 1799 static int 1800 geometry_func (char *arg, int flags) 1801 { 1802 struct geometry geom; 1803 char *msg; 1804 char *device = arg; 1805 #ifdef GRUB_UTIL 1806 char *ptr; 1807 #endif 1808 1809 /* Get the device number. */ 1810 set_device (device); 1811 if (errnum) 1812 return 1; 1813 1814 /* Check for the geometry. */ 1815 if (get_diskinfo (current_drive, &geom)) 1816 { 1817 errnum = ERR_NO_DISK; 1818 return 1; 1819 } 1820 1821 /* Attempt to read the first sector, because some BIOSes turns out not 1822 to support LBA even though they set the bit 0 in the support 1823 bitmap, only after reading something actually. */ 1824 if (biosdisk (BIOSDISK_READ, current_drive, &geom, 0, 1, SCRATCHSEG)) 1825 { 1826 errnum = ERR_READ; 1827 return 1; 1828 } 1829 1830 #ifdef GRUB_UTIL 1831 ptr = skip_to (0, device); 1832 if (*ptr) 1833 { 1834 char *cylinder, *head, *sector, *total_sector; 1835 int num_cylinder, num_head, num_sector, num_total_sector; 1836 1837 cylinder = ptr; 1838 head = skip_to (0, cylinder); 1839 sector = skip_to (0, head); 1840 total_sector = skip_to (0, sector); 1841 if (! safe_parse_maxint (&cylinder, &num_cylinder) 1842 || ! safe_parse_maxint (&head, &num_head) 1843 || ! safe_parse_maxint (§or, &num_sector)) 1844 return 1; 1845 1846 disks[current_drive].cylinders = num_cylinder; 1847 disks[current_drive].heads = num_head; 1848 disks[current_drive].sectors = num_sector; 1849 1850 if (safe_parse_maxint (&total_sector, &num_total_sector)) 1851 disks[current_drive].total_sectors = num_total_sector; 1852 else 1853 disks[current_drive].total_sectors 1854 = num_cylinder * num_head * num_sector; 1855 errnum = 0; 1856 1857 geom = disks[current_drive]; 1858 buf_drive = -1; 1859 } 1860 #endif /* GRUB_UTIL */ 1861 1862 #ifdef GRUB_UTIL 1863 msg = device_map[current_drive]; 1864 #else 1865 if (geom.flags & BIOSDISK_FLAG_LBA_EXTENSION) 1866 msg = "LBA"; 1867 else 1868 msg = "CHS"; 1869 #endif 1870 1871 grub_printf ("drive 0x%x: C/H/S = %d/%d/%d, " 1872 "The number of sectors = %u, %s\n", 1873 current_drive, 1874 geom.cylinders, geom.heads, geom.sectors, 1875 geom.total_sectors, msg); 1876 real_open_partition (1); 1877 1878 return 0; 1879 } 1880 1881 static struct builtin builtin_geometry = 1882 { 1883 "geometry", 1884 geometry_func, 1885 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 1886 "geometry DRIVE [CYLINDER HEAD SECTOR [TOTAL_SECTOR]]", 1887 "Print the information for a drive DRIVE. In the grub shell, you can" 1888 " set the geometry of the drive arbitrarily. The number of the cylinders," 1889 " the one of the heads, the one of the sectors and the one of the total" 1890 " sectors are set to CYLINDER, HEAD, SECTOR and TOTAL_SECTOR," 1891 " respectively. If you omit TOTAL_SECTOR, then it will be calculated based" 1892 " on the C/H/S values automatically." 1893 }; 1894 1895 1896 /* halt */ 1897 static int 1898 halt_func (char *arg, int flags) 1899 { 1900 int no_apm; 1901 1902 no_apm = (grub_memcmp (arg, "--no-apm", 8) == 0); 1903 grub_halt (no_apm); 1904 1905 /* Never reach here. */ 1906 return 1; 1907 } 1908 1909 static struct builtin builtin_halt = 1910 { 1911 "halt", 1912 halt_func, 1913 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 1914 "halt [--no-apm]", 1915 "Halt your system. If APM is avaiable on it, turn off the power using" 1916 " the APM BIOS, unless you specify the option `--no-apm'." 1917 }; 1918 1919 1920 /* help */ 1921 #define MAX_SHORT_DOC_LEN 39 1922 #define MAX_LONG_DOC_LEN 66 1923 1924 static int 1925 help_func (char *arg, int flags) 1926 { 1927 int all = 0; 1928 1929 if (grub_memcmp (arg, "--all", sizeof ("--all") - 1) == 0) 1930 { 1931 all = 1; 1932 arg = skip_to (0, arg); 1933 } 1934 1935 if (! *arg) 1936 { 1937 /* Invoked with no argument. Print the list of the short docs. */ 1938 struct builtin **builtin; 1939 int left = 1; 1940 1941 for (builtin = builtin_table; *builtin != 0; builtin++) 1942 { 1943 int len; 1944 int i; 1945 1946 /* If this cannot be used in the command-line interface, 1947 skip this. */ 1948 if (! ((*builtin)->flags & BUILTIN_CMDLINE)) 1949 continue; 1950 1951 /* If this doesn't need to be listed automatically and "--all" 1952 is not specified, skip this. */ 1953 if (! all && ! ((*builtin)->flags & BUILTIN_HELP_LIST)) 1954 continue; 1955 1956 len = grub_strlen ((*builtin)->short_doc); 1957 /* If the length of SHORT_DOC is too long, truncate it. */ 1958 if (len > MAX_SHORT_DOC_LEN - 1) 1959 len = MAX_SHORT_DOC_LEN - 1; 1960 1961 for (i = 0; i < len; i++) 1962 grub_putchar ((*builtin)->short_doc[i]); 1963 1964 for (; i < MAX_SHORT_DOC_LEN; i++) 1965 grub_putchar (' '); 1966 1967 if (! left) 1968 grub_putchar ('\n'); 1969 1970 left = ! left; 1971 } 1972 1973 /* If the last entry was at the left column, no newline was printed 1974 at the end. */ 1975 if (! left) 1976 grub_putchar ('\n'); 1977 } 1978 else 1979 { 1980 /* Invoked with one or more patterns. */ 1981 do 1982 { 1983 struct builtin **builtin; 1984 char *next_arg; 1985 1986 /* Get the next argument. */ 1987 next_arg = skip_to (0, arg); 1988 1989 /* Terminate ARG. */ 1990 nul_terminate (arg); 1991 1992 for (builtin = builtin_table; *builtin; builtin++) 1993 { 1994 /* Skip this if this is only for the configuration file. */ 1995 if (! ((*builtin)->flags & BUILTIN_CMDLINE)) 1996 continue; 1997 1998 if (substring (arg, (*builtin)->name) < 1) 1999 { 2000 char *doc = (*builtin)->long_doc; 2001 2002 /* At first, print the name and the short doc. */ 2003 grub_printf ("%s: %s\n", 2004 (*builtin)->name, (*builtin)->short_doc); 2005 2006 /* Print the long doc. */ 2007 while (*doc) 2008 { 2009 int len = grub_strlen (doc); 2010 int i; 2011 2012 /* If LEN is too long, fold DOC. */ 2013 if (len > MAX_LONG_DOC_LEN) 2014 { 2015 /* Fold this line at the position of a space. */ 2016 for (len = MAX_LONG_DOC_LEN; len > 0; len--) 2017 if (doc[len - 1] == ' ') 2018 break; 2019 } 2020 2021 grub_printf (" "); 2022 for (i = 0; i < len; i++) 2023 grub_putchar (*doc++); 2024 grub_putchar ('\n'); 2025 } 2026 } 2027 } 2028 2029 arg = next_arg; 2030 } 2031 while (*arg); 2032 } 2033 2034 return 0; 2035 } 2036 2037 static struct builtin builtin_help = 2038 { 2039 "help", 2040 help_func, 2041 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 2042 "help [--all] [PATTERN ...]", 2043 "Display helpful information about builtin commands. Not all commands" 2044 " aren't shown without the option `--all'." 2045 }; 2046 2047 2048 /* hiddenmenu */ 2049 static int 2050 hiddenmenu_func (char *arg, int flags) 2051 { 2052 show_menu = 0; 2053 return 0; 2054 } 2055 2056 static struct builtin builtin_hiddenmenu = 2057 { 2058 "hiddenmenu", 2059 hiddenmenu_func, 2060 BUILTIN_MENU, 2061 #if 0 2062 "hiddenmenu", 2063 "Hide the menu." 2064 #endif 2065 }; 2066 2067 2068 /* hide */ 2069 static int 2070 hide_func (char *arg, int flags) 2071 { 2072 if (! set_device (arg)) 2073 return 1; 2074 2075 if (! set_partition_hidden_flag (1)) 2076 return 1; 2077 2078 return 0; 2079 } 2080 2081 static struct builtin builtin_hide = 2082 { 2083 "hide", 2084 hide_func, 2085 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_HELP_LIST, 2086 "hide PARTITION", 2087 "Hide PARTITION by setting the \"hidden\" bit in" 2088 " its partition type code." 2089 }; 2090 2091 2092 #ifdef SUPPORT_NETBOOT 2093 /* ifconfig */ 2094 static int 2095 ifconfig_func (char *arg, int flags) 2096 { 2097 char *svr = 0, *ip = 0, *gw = 0, *sm = 0; 2098 2099 if (! grub_eth_probe ()) 2100 { 2101 grub_printf ("No ethernet card found.\n"); 2102 errnum = ERR_DEV_VALUES; 2103 return 1; 2104 } 2105 2106 while (*arg) 2107 { 2108 if (! grub_memcmp ("--server=", arg, sizeof ("--server=") - 1)) 2109 svr = arg + sizeof("--server=") - 1; 2110 else if (! grub_memcmp ("--address=", arg, sizeof ("--address=") - 1)) 2111 ip = arg + sizeof ("--address=") - 1; 2112 else if (! grub_memcmp ("--gateway=", arg, sizeof ("--gateway=") - 1)) 2113 gw = arg + sizeof ("--gateway=") - 1; 2114 else if (! grub_memcmp ("--mask=", arg, sizeof("--mask=") - 1)) 2115 sm = arg + sizeof ("--mask=") - 1; 2116 else 2117 { 2118 errnum = ERR_BAD_ARGUMENT; 2119 return 1; 2120 } 2121 2122 arg = skip_to (0, arg); 2123 } 2124 2125 if (! ifconfig (ip, sm, gw, svr)) 2126 { 2127 errnum = ERR_BAD_ARGUMENT; 2128 return 1; 2129 } 2130 2131 print_network_configuration (); 2132 return 0; 2133 } 2134 2135 static struct builtin builtin_ifconfig = 2136 { 2137 "ifconfig", 2138 ifconfig_func, 2139 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_HELP_LIST, 2140 "ifconfig [--address=IP] [--gateway=IP] [--mask=MASK] [--server=IP]", 2141 "Configure the IP address, the netmask, the gateway and the server" 2142 " address or print current network configuration." 2143 }; 2144 #endif /* SUPPORT_NETBOOT */ 2145 2146 2147 /* impsprobe */ 2148 static int 2149 impsprobe_func (char *arg, int flags) 2150 { 2151 #ifdef GRUB_UTIL 2152 /* In the grub shell, we cannot probe IMPS. */ 2153 errnum = ERR_UNRECOGNIZED; 2154 return 1; 2155 #else /* ! GRUB_UTIL */ 2156 if (!imps_probe ()) 2157 printf (" No MPS information found or probe failed\n"); 2158 2159 return 0; 2160 #endif /* ! GRUB_UTIL */ 2161 } 2162 2163 static struct builtin builtin_impsprobe = 2164 { 2165 "impsprobe", 2166 impsprobe_func, 2167 BUILTIN_CMDLINE, 2168 "impsprobe", 2169 "Probe the Intel Multiprocessor Specification 1.1 or 1.4" 2170 " configuration table and boot the various CPUs which are found into" 2171 " a tight loop." 2172 }; 2173 2174 /* initrd */ 2175 static int 2176 initrd_func (char *arg, int flags) 2177 { 2178 switch (kernel_type) 2179 { 2180 case KERNEL_TYPE_LINUX: 2181 case KERNEL_TYPE_BIG_LINUX: 2182 if (! load_initrd (arg)) 2183 return 1; 2184 break; 2185 2186 default: 2187 errnum = ERR_NEED_LX_KERNEL; 2188 return 1; 2189 } 2190 2191 return 0; 2192 } 2193 2194 static struct builtin builtin_initrd = 2195 { 2196 "initrd", 2197 initrd_func, 2198 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 2199 "initrd FILE [ARG ...]", 2200 "Load an initial ramdisk FILE for a Linux format boot image and set the" 2201 " appropriate parameters in the Linux setup area in memory." 2202 }; 2203 2204 2205 /* install */ 2206 static int 2207 install_func (char *arg, int flags) 2208 { 2209 char *stage1_file, *dest_dev, *file, *addr; 2210 char *stage1_buffer = (char *) RAW_ADDR (0x100000); 2211 char *stage2_buffer = stage1_buffer + SECTOR_SIZE; 2212 char *old_sect = stage2_buffer + SECTOR_SIZE; 2213 char *stage2_first_buffer = old_sect + SECTOR_SIZE; 2214 char *stage2_second_buffer = stage2_first_buffer + SECTOR_SIZE; 2215 /* XXX: Probably SECTOR_SIZE is reasonable. */ 2216 char *config_filename = stage2_second_buffer + SECTOR_SIZE; 2217 char *dummy = config_filename + SECTOR_SIZE; 2218 int new_drive = GRUB_INVALID_DRIVE; 2219 int dest_drive, dest_partition; 2220 unsigned int dest_sector; 2221 int src_drive, src_partition, src_part_start; 2222 int i; 2223 struct geometry dest_geom, src_geom; 2224 unsigned int saved_sector; 2225 unsigned int stage2_first_sector, stage2_second_sector; 2226 char *ptr; 2227 int installaddr, installlist; 2228 /* Point to the location of the name of a configuration file in Stage 2. */ 2229 char *config_file_location; 2230 /* If FILE is a Stage 1.5? */ 2231 int is_stage1_5 = 0; 2232 /* Must call grub_close? */ 2233 int is_open = 0; 2234 /* If LBA is forced? */ 2235 int is_force_lba = 0; 2236 /* Was the last sector full? */ 2237 int last_length = SECTOR_SIZE; 2238 2239 #ifdef GRUB_UTIL 2240 /* If the Stage 2 is in a partition mounted by an OS, this will store 2241 the filename under the OS. */ 2242 char *stage2_os_file = 0; 2243 #endif /* GRUB_UTIL */ 2244 2245 auto void disk_read_savesect_func (unsigned int sector, int offset, 2246 int length); 2247 auto void disk_read_blocklist_func (unsigned int sector, int offset, 2248 int length); 2249 2250 /* Save the first sector of Stage2 in STAGE2_SECT. */ 2251 auto void disk_read_savesect_func (unsigned int sector, int offset, 2252 int length) 2253 { 2254 if (debug) 2255 printf ("[%u]", sector); 2256 2257 /* ReiserFS has files which sometimes contain data not aligned 2258 on sector boundaries. Returning an error is better than 2259 silently failing. */ 2260 if (offset != 0 || length != SECTOR_SIZE) 2261 errnum = ERR_UNALIGNED; 2262 2263 saved_sector = sector; 2264 } 2265 2266 /* Write SECTOR to INSTALLLIST, and update INSTALLADDR and 2267 INSTALLSECT. */ 2268 auto void disk_read_blocklist_func (unsigned int sector, int offset, 2269 int length) 2270 { 2271 if (debug) 2272 printf("[%u]", sector); 2273 2274 if (offset != 0 || last_length != SECTOR_SIZE) 2275 { 2276 /* We found a non-sector-aligned data block. */ 2277 errnum = ERR_UNALIGNED; 2278 return; 2279 } 2280 2281 last_length = length; 2282 2283 if (*((unsigned long *) (installlist - 4)) 2284 + *((unsigned short *) installlist) != sector 2285 || installlist == (int) stage2_first_buffer + SECTOR_SIZE + 4) 2286 { 2287 installlist -= 8; 2288 2289 if (*((unsigned long *) (installlist - 8))) 2290 errnum = ERR_WONT_FIT; 2291 else 2292 { 2293 *((unsigned short *) (installlist + 2)) = (installaddr >> 4); 2294 *((unsigned long *) (installlist - 4)) = sector; 2295 } 2296 } 2297 2298 *((unsigned short *) installlist) += 1; 2299 installaddr += 512; 2300 } 2301 2302 /* First, check the GNU-style long option. */ 2303 while (1) 2304 { 2305 if (grub_memcmp ("--force-lba", arg, sizeof ("--force-lba") - 1) == 0) 2306 { 2307 is_force_lba = 1; 2308 arg = skip_to (0, arg); 2309 } 2310 #ifdef GRUB_UTIL 2311 else if (grub_memcmp ("--stage2=", arg, sizeof ("--stage2=") - 1) == 0) 2312 { 2313 stage2_os_file = arg + sizeof ("--stage2=") - 1; 2314 arg = skip_to (0, arg); 2315 nul_terminate (stage2_os_file); 2316 } 2317 #endif /* GRUB_UTIL */ 2318 else 2319 break; 2320 } 2321 2322 stage1_file = arg; 2323 dest_dev = skip_to (0, stage1_file); 2324 if (*dest_dev == 'd') 2325 { 2326 new_drive = 0; 2327 dest_dev = skip_to (0, dest_dev); 2328 } 2329 file = skip_to (0, dest_dev); 2330 addr = skip_to (0, file); 2331 2332 /* Get the installation address. */ 2333 if (! safe_parse_maxint (&addr, &installaddr)) 2334 { 2335 /* ADDR is not specified. */ 2336 installaddr = 0; 2337 ptr = addr; 2338 errnum = 0; 2339 } 2340 else 2341 ptr = skip_to (0, addr); 2342 2343 #ifndef NO_DECOMPRESSION 2344 /* Do not decompress Stage 1 or Stage 2. */ 2345 no_decompression = 1; 2346 #endif 2347 2348 /* Read Stage 1. */ 2349 is_open = grub_open (stage1_file); 2350 if (! is_open 2351 || ! grub_read (stage1_buffer, SECTOR_SIZE) == SECTOR_SIZE) 2352 goto fail; 2353 2354 /* Read the old sector from DEST_DEV. */ 2355 if (! set_device (dest_dev) 2356 || ! open_partition () 2357 || ! devread (0, 0, SECTOR_SIZE, old_sect)) 2358 goto fail; 2359 2360 /* Store the information for the destination device. */ 2361 dest_drive = current_drive; 2362 dest_partition = current_partition; 2363 dest_geom = buf_geom; 2364 dest_sector = part_start; 2365 2366 /* Copy the possible DOS BPB, 59 bytes at byte offset 3. */ 2367 grub_memmove (stage1_buffer + BOOTSEC_BPB_OFFSET, 2368 old_sect + BOOTSEC_BPB_OFFSET, 2369 BOOTSEC_BPB_LENGTH); 2370 2371 /* If for a hard disk, copy the possible MBR/extended part table. */ 2372 if (dest_drive & 0x80) 2373 grub_memmove (stage1_buffer + STAGE1_WINDOWS_NT_MAGIC, 2374 old_sect + STAGE1_WINDOWS_NT_MAGIC, 2375 STAGE1_PARTEND - STAGE1_WINDOWS_NT_MAGIC); 2376 2377 /* Check for the version and the signature of Stage 1. */ 2378 if (*((short *)(stage1_buffer + STAGE1_VER_MAJ_OFFS)) != COMPAT_VERSION 2379 || (*((unsigned short *) (stage1_buffer + BOOTSEC_SIG_OFFSET)) 2380 != BOOTSEC_SIGNATURE)) 2381 { 2382 errnum = ERR_BAD_VERSION; 2383 goto fail; 2384 } 2385 2386 /* This below is not true any longer. But should we leave this alone? */ 2387 2388 /* If DEST_DRIVE is a floppy, Stage 2 must have the iteration probe 2389 routine. */ 2390 if (! (dest_drive & 0x80) 2391 && (*((unsigned char *) (stage1_buffer + BOOTSEC_PART_OFFSET)) == 0x80 2392 || stage1_buffer[BOOTSEC_PART_OFFSET] == 0)) 2393 { 2394 errnum = ERR_BAD_VERSION; 2395 goto fail; 2396 } 2397 2398 grub_close (); 2399 2400 /* Open Stage 2. */ 2401 is_open = grub_open (file); 2402 if (! is_open) 2403 goto fail; 2404 2405 src_drive = current_drive; 2406 src_partition = current_partition; 2407 src_part_start = part_start; 2408 src_geom = buf_geom; 2409 2410 if (! new_drive) 2411 new_drive = src_drive; 2412 else if (src_drive != dest_drive) 2413 grub_printf ("Warning: the option `d' was not used, but the Stage 1 will" 2414 " be installed on a\ndifferent drive than the drive where" 2415 " the Stage 2 resides.\n"); 2416 2417 /* Set the boot drive. */ 2418 *((unsigned char *) (stage1_buffer + STAGE1_BOOT_DRIVE)) = new_drive; 2419 2420 /* Set the "force LBA" flag. */ 2421 *((unsigned char *) (stage1_buffer + STAGE1_FORCE_LBA)) = is_force_lba; 2422 2423 /* If DEST_DRIVE is a hard disk, enable the workaround, which is 2424 for buggy BIOSes which don't pass boot drive correctly. Instead, 2425 they pass 0x00 or 0x01 even when booted from 0x80. */ 2426 if (dest_drive & BIOS_FLAG_FIXED_DISK) 2427 /* Replace the jmp (2 bytes) with double nop's. */ 2428 *((unsigned short *) (stage1_buffer + STAGE1_BOOT_DRIVE_CHECK)) 2429 = 0x9090; 2430 2431 /* Read the first sector of Stage 2. */ 2432 disk_read_hook = disk_read_savesect_func; 2433 if (grub_read (stage2_first_buffer, SECTOR_SIZE) != SECTOR_SIZE) 2434 goto fail; 2435 2436 stage2_first_sector = saved_sector; 2437 2438 /* Read the second sector of Stage 2. */ 2439 if (grub_read (stage2_second_buffer, SECTOR_SIZE) != SECTOR_SIZE) 2440 goto fail; 2441 2442 stage2_second_sector = saved_sector; 2443 2444 /* Check for the version of Stage 2. */ 2445 if (*((short *) (stage2_second_buffer + STAGE2_VER_MAJ_OFFS)) 2446 != COMPAT_VERSION) 2447 { 2448 errnum = ERR_BAD_VERSION; 2449 goto fail; 2450 } 2451 2452 /* Check for the Stage 2 id. */ 2453 if (stage2_second_buffer[STAGE2_STAGE2_ID] != STAGE2_ID_STAGE2) 2454 is_stage1_5 = 1; 2455 2456 /* If INSTALLADDR is not specified explicitly in the command-line, 2457 determine it by the Stage 2 id. */ 2458 if (! installaddr) 2459 { 2460 if (! is_stage1_5) 2461 /* Stage 2. */ 2462 installaddr = 0x8000; 2463 else 2464 /* Stage 1.5. */ 2465 installaddr = 0x2000; 2466 } 2467 2468 *((unsigned long *) (stage1_buffer + STAGE1_STAGE2_SECTOR)) 2469 = stage2_first_sector; 2470 *((unsigned short *) (stage1_buffer + STAGE1_STAGE2_ADDRESS)) 2471 = installaddr; 2472 *((unsigned short *) (stage1_buffer + STAGE1_STAGE2_SEGMENT)) 2473 = installaddr >> 4; 2474 2475 i = (int) stage2_first_buffer + SECTOR_SIZE - 4; 2476 while (*((unsigned long *) i)) 2477 { 2478 if (i < (int) stage2_first_buffer 2479 || (*((int *) (i - 4)) & 0x80000000) 2480 || *((unsigned short *) i) >= 0xA00 2481 || *((short *) (i + 2)) == 0) 2482 { 2483 errnum = ERR_BAD_VERSION; 2484 goto fail; 2485 } 2486 2487 *((int *) i) = 0; 2488 *((int *) (i - 4)) = 0; 2489 i -= 8; 2490 } 2491 2492 installlist = (int) stage2_first_buffer + SECTOR_SIZE + 4; 2493 installaddr += SECTOR_SIZE; 2494 2495 /* Read the whole of Stage2 except for the first sector. */ 2496 grub_seek (SECTOR_SIZE); 2497 2498 disk_read_hook = disk_read_blocklist_func; 2499 if (! grub_read (dummy, -1)) 2500 goto fail; 2501 2502 disk_read_hook = 0; 2503 2504 /* Find a string for the configuration filename. */ 2505 config_file_location = stage2_second_buffer + STAGE2_VER_STR_OFFS; 2506 while (*(config_file_location++)) 2507 ; 2508 2509 /* Set the "force LBA" flag for Stage2. */ 2510 *((unsigned char *) (stage2_second_buffer + STAGE2_FORCE_LBA)) 2511 = is_force_lba; 2512 2513 if (*ptr == 'p') 2514 { 2515 *((long *) (stage2_second_buffer + STAGE2_INSTALLPART)) 2516 = src_partition; 2517 if (is_stage1_5) 2518 { 2519 /* Reset the device information in FILE if it is a Stage 1.5. */ 2520 unsigned long device = 0xFFFFFFFF; 2521 2522 grub_memmove (config_file_location, (char *) &device, 2523 sizeof (device)); 2524 } 2525 2526 ptr = skip_to (0, ptr); 2527 } 2528 2529 if (*ptr) 2530 { 2531 grub_strcpy (config_filename, ptr); 2532 nul_terminate (config_filename); 2533 2534 if (! is_stage1_5) 2535 /* If it is a Stage 2, just copy PTR to CONFIG_FILE_LOCATION. */ 2536 grub_strcpy (config_file_location, ptr); 2537 else 2538 { 2539 char *real_config; 2540 unsigned long device; 2541 2542 /* Translate the external device syntax to the internal device 2543 syntax. */ 2544 if (! (real_config = set_device (ptr))) 2545 { 2546 /* The Stage 2 PTR does not contain the device name, so 2547 use the root device instead. */ 2548 errnum = ERR_NONE; 2549 current_drive = saved_drive; 2550 current_partition = saved_partition; 2551 real_config = ptr; 2552 } 2553 2554 if (current_drive == src_drive) 2555 { 2556 /* If the drive where the Stage 2 resides is the same as 2557 the one where the Stage 1.5 resides, do not embed the 2558 drive number. */ 2559 current_drive = GRUB_INVALID_DRIVE; 2560 } 2561 2562 device = (current_drive << 24) | current_partition; 2563 grub_memmove (config_file_location, (char *) &device, 2564 sizeof (device)); 2565 grub_strcpy (config_file_location + sizeof (device), 2566 real_config); 2567 } 2568 2569 /* If a Stage 1.5 is used, then we need to modify the Stage2. */ 2570 if (is_stage1_5) 2571 { 2572 char *real_config_filename = skip_to (0, ptr); 2573 2574 is_open = grub_open (config_filename); 2575 if (! is_open) 2576 goto fail; 2577 2578 /* Skip the first sector. */ 2579 grub_seek (SECTOR_SIZE); 2580 2581 disk_read_hook = disk_read_savesect_func; 2582 if (grub_read (stage2_buffer, SECTOR_SIZE) != SECTOR_SIZE) 2583 goto fail; 2584 2585 disk_read_hook = 0; 2586 grub_close (); 2587 is_open = 0; 2588 2589 /* Sanity check. */ 2590 if (*(stage2_buffer + STAGE2_STAGE2_ID) != STAGE2_ID_STAGE2) 2591 { 2592 errnum = ERR_BAD_VERSION; 2593 goto fail; 2594 } 2595 2596 /* Set the "force LBA" flag for Stage2. */ 2597 *(stage2_buffer + STAGE2_FORCE_LBA) = is_force_lba; 2598 2599 /* If REAL_CONFIG_FILENAME is specified, copy it to the Stage2. */ 2600 if (*real_config_filename) 2601 { 2602 /* Specified */ 2603 char *location; 2604 2605 /* Find a string for the configuration filename. */ 2606 location = stage2_buffer + STAGE2_VER_STR_OFFS; 2607 while (*(location++)) 2608 ; 2609 2610 /* Copy the name. */ 2611 grub_strcpy (location, real_config_filename); 2612 } 2613 2614 /* Write it to the disk. */ 2615 buf_track = BUF_CACHE_INVALID; 2616 2617 #ifdef GRUB_UTIL 2618 /* In the grub shell, access the Stage 2 via the OS filesystem 2619 service, if possible. */ 2620 if (stage2_os_file) 2621 { 2622 FILE *fp; 2623 2624 fp = fopen (stage2_os_file, "r+"); 2625 if (! fp) 2626 { 2627 errnum = ERR_FILE_NOT_FOUND; 2628 goto fail; 2629 } 2630 2631 if (fseek (fp, SECTOR_SIZE, SEEK_SET) != 0) 2632 { 2633 fclose (fp); 2634 errnum = ERR_BAD_VERSION; 2635 goto fail; 2636 } 2637 2638 if (fwrite (stage2_buffer, 1, SECTOR_SIZE, fp) 2639 != SECTOR_SIZE) 2640 { 2641 fclose (fp); 2642 errnum = ERR_WRITE; 2643 goto fail; 2644 } 2645 2646 fclose (fp); 2647 } 2648 else 2649 #endif /* GRUB_UTIL */ 2650 { 2651 if (! devwrite (saved_sector - part_start, 1, stage2_buffer)) 2652 goto fail; 2653 } 2654 } 2655 } 2656 2657 /* Clear the cache. */ 2658 buf_track = BUF_CACHE_INVALID; 2659 2660 /* Write the modified sectors of Stage2 to the disk. */ 2661 #ifdef GRUB_UTIL 2662 if (! is_stage1_5 && stage2_os_file) 2663 { 2664 FILE *fp; 2665 2666 fp = fopen (stage2_os_file, "r+"); 2667 if (! fp) 2668 { 2669 errnum = ERR_FILE_NOT_FOUND; 2670 goto fail; 2671 } 2672 2673 if (fwrite (stage2_first_buffer, 1, SECTOR_SIZE, fp) != SECTOR_SIZE) 2674 { 2675 fclose (fp); 2676 errnum = ERR_WRITE; 2677 goto fail; 2678 } 2679 2680 if (fwrite (stage2_second_buffer, 1, SECTOR_SIZE, fp) != SECTOR_SIZE) 2681 { 2682 fclose (fp); 2683 errnum = ERR_WRITE; 2684 goto fail; 2685 } 2686 2687 fclose (fp); 2688 } 2689 else 2690 #endif /* GRUB_UTIL */ 2691 { 2692 /* The first. */ 2693 current_drive = src_drive; 2694 current_partition = src_partition; 2695 2696 if (! open_partition ()) 2697 goto fail; 2698 2699 if (! devwrite (stage2_first_sector - src_part_start, 1, 2700 stage2_first_buffer)) 2701 goto fail; 2702 2703 if (! devwrite (stage2_second_sector - src_part_start, 1, 2704 stage2_second_buffer)) 2705 goto fail; 2706 } 2707 2708 /* Write the modified sector of Stage 1 to the disk. */ 2709 current_drive = dest_drive; 2710 current_partition = dest_partition; 2711 if (! open_partition ()) 2712 goto fail; 2713 2714 devwrite (0, 1, stage1_buffer); 2715 2716 fail: 2717 if (is_open) 2718 grub_close (); 2719 2720 disk_read_hook = 0; 2721 2722 #ifndef NO_DECOMPRESSION 2723 no_decompression = 0; 2724 #endif 2725 2726 return errnum; 2727 } 2728 2729 static struct builtin builtin_install = 2730 { 2731 "install", 2732 install_func, 2733 BUILTIN_CMDLINE, 2734 "install [--stage2=STAGE2_FILE] [--force-lba] STAGE1 [d] DEVICE STAGE2 [ADDR] [p] [CONFIG_FILE] [REAL_CONFIG_FILE]", 2735 "Install STAGE1 on DEVICE, and install a blocklist for loading STAGE2" 2736 " as a Stage 2. If the option `d' is present, the Stage 1 will always" 2737 " look for the disk where STAGE2 was installed, rather than using" 2738 " the booting drive. The Stage 2 will be loaded at address ADDR, which" 2739 " will be determined automatically if you don't specify it. If" 2740 " the option `p' or CONFIG_FILE is present, then the first block" 2741 " of Stage 2 is patched with new values of the partition and name" 2742 " of the configuration file used by the true Stage 2 (for a Stage 1.5," 2743 " this is the name of the true Stage 2) at boot time. If STAGE2 is a Stage" 2744 " 1.5 and REAL_CONFIG_FILE is present, then the Stage 2 CONFIG_FILE is" 2745 " patched with the configuration filename REAL_CONFIG_FILE." 2746 " If the option `--force-lba' is specified, disable some sanity checks" 2747 " for LBA mode. If the option `--stage2' is specified, rewrite the Stage" 2748 " 2 via your OS's filesystem instead of the raw device." 2749 }; 2750 2751 2752 /* ioprobe */ 2753 static int 2754 ioprobe_func (char *arg, int flags) 2755 { 2756 #ifdef GRUB_UTIL 2757 2758 errnum = ERR_UNRECOGNIZED; 2759 return 1; 2760 2761 #else /* ! GRUB_UTIL */ 2762 2763 unsigned short *port; 2764 2765 /* Get the drive number. */ 2766 set_device (arg); 2767 if (errnum) 2768 return 1; 2769 2770 /* Clean out IO_MAP. */ 2771 grub_memset ((char *) io_map, 0, IO_MAP_SIZE * sizeof (unsigned short)); 2772 2773 /* Track the int13 handler. */ 2774 track_int13 (current_drive); 2775 2776 /* Print out the result. */ 2777 for (port = io_map; *port != 0; port++) 2778 grub_printf (" 0x%x", (unsigned int) *port); 2779 2780 return 0; 2781 2782 #endif /* ! GRUB_UTIL */ 2783 } 2784 2785 static struct builtin builtin_ioprobe = 2786 { 2787 "ioprobe", 2788 ioprobe_func, 2789 BUILTIN_CMDLINE, 2790 "ioprobe DRIVE", 2791 "Probe I/O ports used for the drive DRIVE." 2792 }; 2793 2794 2795 /* kernel */ 2796 static int 2797 kernel_func (char *arg, int flags) 2798 { 2799 int len; 2800 kernel_t suggested_type = KERNEL_TYPE_NONE; 2801 unsigned long load_flags = 0; 2802 2803 #ifndef AUTO_LINUX_MEM_OPT 2804 load_flags |= KERNEL_LOAD_NO_MEM_OPTION; 2805 #endif 2806 2807 /* Deal with GNU-style long options. */ 2808 while (1) 2809 { 2810 /* If the option `--type=TYPE' is specified, convert the string to 2811 a kernel type. */ 2812 if (grub_memcmp (arg, "--type=", 7) == 0) 2813 { 2814 arg += 7; 2815 2816 if (grub_memcmp (arg, "netbsd", 6) == 0) 2817 suggested_type = KERNEL_TYPE_NETBSD; 2818 else if (grub_memcmp (arg, "freebsd", 7) == 0) 2819 suggested_type = KERNEL_TYPE_FREEBSD; 2820 else if (grub_memcmp (arg, "openbsd", 7) == 0) 2821 /* XXX: For now, OpenBSD is identical to NetBSD, from GRUB's 2822 point of view. */ 2823 suggested_type = KERNEL_TYPE_NETBSD; 2824 else if (grub_memcmp (arg, "linux", 5) == 0) 2825 suggested_type = KERNEL_TYPE_LINUX; 2826 else if (grub_memcmp (arg, "biglinux", 8) == 0) 2827 suggested_type = KERNEL_TYPE_BIG_LINUX; 2828 else if (grub_memcmp (arg, "multiboot", 9) == 0) 2829 suggested_type = KERNEL_TYPE_MULTIBOOT; 2830 else 2831 { 2832 errnum = ERR_BAD_ARGUMENT; 2833 return 1; 2834 } 2835 } 2836 /* If the `--no-mem-option' is specified, don't pass a Linux's mem 2837 option automatically. If the kernel is another type, this flag 2838 has no effect. */ 2839 else if (grub_memcmp (arg, "--no-mem-option", 15) == 0) 2840 load_flags |= KERNEL_LOAD_NO_MEM_OPTION; 2841 else 2842 break; 2843 2844 /* Try the next. */ 2845 arg = skip_to (0, arg); 2846 } 2847 2848 len = grub_strlen (arg); 2849 2850 /* Reset MB_CMDLINE. */ 2851 mb_cmdline = (char *) MB_CMDLINE_BUF; 2852 if (len + 1 > MB_CMDLINE_BUFLEN) 2853 { 2854 errnum = ERR_WONT_FIT; 2855 return 1; 2856 } 2857 2858 /* Copy the command-line to MB_CMDLINE. */ 2859 grub_memmove (mb_cmdline, arg, len + 1); 2860 kernel_type = load_image (arg, mb_cmdline, suggested_type, load_flags); 2861 if (kernel_type == KERNEL_TYPE_NONE) 2862 return 1; 2863 2864 mb_cmdline += grub_strlen(mb_cmdline) + 1; 2865 return 0; 2866 } 2867 2868 static struct builtin builtin_kernel = 2869 { 2870 "kernel", 2871 kernel_func, 2872 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 2873 "kernel [--no-mem-option] [--type=TYPE] FILE [ARG ...]", 2874 "Attempt to load the primary boot image from FILE. The rest of the" 2875 " line is passed verbatim as the \"kernel command line\". Any modules" 2876 " must be reloaded after using this command. The option --type is used" 2877 " to suggest what type of kernel to be loaded. TYPE must be either of" 2878 " \"netbsd\", \"freebsd\", \"openbsd\", \"linux\", \"biglinux\" and" 2879 " \"multiboot\". The option --no-mem-option tells GRUB not to pass a" 2880 " Linux's mem option automatically." 2881 }; 2882 2883 int 2884 min_mem64_func(char *arg, int flags) 2885 { 2886 if (!safe_parse_maxint(&arg, &min_mem64)) 2887 return (1); 2888 } 2889 2890 static struct builtin builtin_min_mem64 = 2891 { 2892 "min_mem64", 2893 min_mem64_func, 2894 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_SCRIPT | BUILTIN_HELP_LIST, 2895 "min_mem64 <memory in MB>", 2896 "Sets minimum memory (in MB) required for $ISADIR to expand to amd64, " 2897 "even on 64-bit capable hardware." 2898 }; 2899 2900 static int 2901 kernel_dollar_func (char *arg, int flags) 2902 { 2903 int err; 2904 char newarg[MAX_CMDLINE]; 2905 2906 /* 2907 * We're going to expand the arguments twice. The first expansion, which 2908 * occurs without the benefit of knowing the ZFS object ID of the filesystem 2909 * we're booting from (if we're booting from ZFS, of course), must be 2910 * sufficient to find and read the kernel. The second expansion will 2911 * then overwrite the command line actually set in the multiboot header with 2912 * the newly-expanded one. Since $ZFS-BOOTFS expands differently after 2913 * zfs_open() has been called (kernel_func() -> load_image() -> grub_open() -> 2914 * zfs_open()), we need to do the second expansion so that the kernel is 2915 * given the right object ID argument. Note that the pointer to the 2916 * command line set in the multiboot header is always MB_CMDLINE_BUF. 2917 */ 2918 grub_printf("loading '%s' ...\n", arg); 2919 if ((err = expand_string(arg, newarg, MAX_CMDLINE)) != 0) { 2920 errnum = err; 2921 return (1); 2922 } 2923 2924 if ((err = kernel_func(newarg, flags)) != 0) 2925 return (err); 2926 2927 mb_cmdline = (char *)MB_CMDLINE_BUF; 2928 if ((err = expand_string(arg, mb_cmdline, MAX_CMDLINE)) != 0) { 2929 errnum = err; 2930 return (1); 2931 } 2932 2933 grub_printf("loading '%s' ...\n", mb_cmdline); 2934 mb_cmdline += grub_strlen(mb_cmdline) + 1; 2935 return (0); 2936 } 2937 2938 static struct builtin builtin_kernel_dollar = 2939 { 2940 "kernel$", 2941 kernel_dollar_func, 2942 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 2943 "kernel$ [--no-mem-option] [--type=TYPE] FILE [ARG ...]", 2944 " Just like kernel, but with variable expansion, including the legacy" 2945 " (and nonconforming) variables $ISADIR and $ZFS-BOOTFS." 2946 }; 2947 2948 2949 /* lock */ 2950 static int 2951 lock_func (char *arg, int flags) 2952 { 2953 if (! auth && password) 2954 { 2955 errnum = ERR_PRIVILEGED; 2956 return 1; 2957 } 2958 2959 return 0; 2960 } 2961 2962 static struct builtin builtin_lock = 2963 { 2964 "lock", 2965 lock_func, 2966 BUILTIN_CMDLINE, 2967 "lock", 2968 "Break a command execution unless the user is authenticated." 2969 }; 2970 2971 2972 /* makeactive */ 2973 static int 2974 makeactive_func (char *arg, int flags) 2975 { 2976 if (! make_saved_active ()) 2977 return 1; 2978 2979 return 0; 2980 } 2981 2982 static struct builtin builtin_makeactive = 2983 { 2984 "makeactive", 2985 makeactive_func, 2986 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 2987 "makeactive", 2988 "Set the active partition on the root disk to GRUB's root device." 2989 " This command is limited to _primary_ PC partitions on a hard disk." 2990 }; 2991 2992 2993 /* map */ 2994 /* Map FROM_DRIVE to TO_DRIVE. */ 2995 static int 2996 map_func (char *arg, int flags) 2997 { 2998 char *to_drive; 2999 char *from_drive; 3000 unsigned long to, from; 3001 int i; 3002 3003 to_drive = arg; 3004 from_drive = skip_to (0, arg); 3005 3006 /* Get the drive number for TO_DRIVE. */ 3007 set_device (to_drive); 3008 if (errnum) 3009 return 1; 3010 to = current_drive; 3011 3012 /* Get the drive number for FROM_DRIVE. */ 3013 set_device (from_drive); 3014 if (errnum) 3015 return 1; 3016 from = current_drive; 3017 3018 /* Search for an empty slot in BIOS_DRIVE_MAP. */ 3019 for (i = 0; i < DRIVE_MAP_SIZE; i++) 3020 { 3021 /* Perhaps the user wants to override the map. */ 3022 if ((bios_drive_map[i] & 0xff) == from) 3023 break; 3024 3025 if (! bios_drive_map[i]) 3026 break; 3027 } 3028 3029 if (i == DRIVE_MAP_SIZE) 3030 { 3031 errnum = ERR_WONT_FIT; 3032 return 1; 3033 } 3034 3035 if (to == from) 3036 /* If TO is equal to FROM, delete the entry. */ 3037 grub_memmove ((char *) &bios_drive_map[i], (char *) &bios_drive_map[i + 1], 3038 sizeof (unsigned short) * (DRIVE_MAP_SIZE - i)); 3039 else 3040 bios_drive_map[i] = from | (to << 8); 3041 3042 return 0; 3043 } 3044 3045 static struct builtin builtin_map = 3046 { 3047 "map", 3048 map_func, 3049 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 3050 "map TO_DRIVE FROM_DRIVE", 3051 "Map the drive FROM_DRIVE to the drive TO_DRIVE. This is necessary" 3052 " when you chain-load some operating systems, such as DOS, if such an" 3053 " OS resides at a non-first drive." 3054 }; 3055 3056 3057 #ifdef USE_MD5_PASSWORDS 3058 /* md5crypt */ 3059 static int 3060 md5crypt_func (char *arg, int flags) 3061 { 3062 char crypted[36]; 3063 char key[32]; 3064 unsigned int seed; 3065 int i; 3066 const char *const seedchars = 3067 "./0123456789ABCDEFGHIJKLMNOPQRST" 3068 "UVWXYZabcdefghijklmnopqrstuvwxyz"; 3069 3070 /* First create a salt. */ 3071 3072 /* The magical prefix. */ 3073 grub_memset (crypted, 0, sizeof (crypted)); 3074 grub_memmove (crypted, "$1$", 3); 3075 3076 /* Create the length of a salt. */ 3077 seed = currticks (); 3078 3079 /* Generate a salt. */ 3080 for (i = 0; i < 8 && seed; i++) 3081 { 3082 /* FIXME: This should be more random. */ 3083 crypted[3 + i] = seedchars[seed & 0x3f]; 3084 seed >>= 6; 3085 } 3086 3087 /* A salt must be terminated with `$', if it is less than 8 chars. */ 3088 crypted[3 + i] = '$'; 3089 3090 #ifdef DEBUG_MD5CRYPT 3091 grub_printf ("salt = %s\n", crypted); 3092 #endif 3093 3094 /* Get a password. */ 3095 grub_memset (key, 0, sizeof (key)); 3096 get_cmdline ("Password: ", key, sizeof (key) - 1, '*', 0); 3097 3098 /* Crypt the key. */ 3099 make_md5_password (key, crypted); 3100 3101 grub_printf ("Encrypted: %s\n", crypted); 3102 return 0; 3103 } 3104 3105 static struct builtin builtin_md5crypt = 3106 { 3107 "md5crypt", 3108 md5crypt_func, 3109 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 3110 "md5crypt", 3111 "Generate a password in MD5 format." 3112 }; 3113 #endif /* USE_MD5_PASSWORDS */ 3114 3115 3116 /* module */ 3117 static int 3118 module_func (char *arg, int flags) 3119 { 3120 int len = grub_strlen (arg); 3121 3122 switch (kernel_type) 3123 { 3124 case KERNEL_TYPE_MULTIBOOT: 3125 if (mb_cmdline + len + 1 > (char *) MB_CMDLINE_BUF + MB_CMDLINE_BUFLEN) 3126 { 3127 errnum = ERR_WONT_FIT; 3128 return 1; 3129 } 3130 grub_memmove (mb_cmdline, arg, len + 1); 3131 if (! load_module (arg, mb_cmdline)) 3132 return 1; 3133 3134 mb_cmdline += grub_strlen(mb_cmdline) + 1; 3135 break; 3136 3137 case KERNEL_TYPE_LINUX: 3138 case KERNEL_TYPE_BIG_LINUX: 3139 if (! load_initrd (arg)) 3140 return 1; 3141 break; 3142 3143 default: 3144 errnum = ERR_NEED_MB_KERNEL; 3145 return 1; 3146 } 3147 3148 return 0; 3149 } 3150 3151 static struct builtin builtin_module = 3152 { 3153 "module", 3154 module_func, 3155 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 3156 "module FILE [ARG ...]", 3157 "Load a boot module FILE for a Multiboot format boot image (no" 3158 " interpretation of the file contents is made, so users of this" 3159 " command must know what the kernel in question expects). The" 3160 " rest of the line is passed as the \"module command line\", like" 3161 " the `kernel' command." 3162 }; 3163 3164 /* module$ */ 3165 static int 3166 module_dollar_func (char *arg, int flags) 3167 { 3168 char newarg[MAX_CMDLINE]; 3169 char *cmdline_sav = mb_cmdline; 3170 int err; 3171 3172 grub_printf("loading '%s' ...\n", arg); 3173 if ((err = expand_string(arg, newarg, MAX_CMDLINE)) != 0) { 3174 errnum = err; 3175 return (1); 3176 } 3177 3178 if ((err = module_func(newarg, flags)) != 0) 3179 return (err); 3180 3181 if ((err = expand_string(arg, cmdline_sav, MAX_CMDLINE)) != 0) { 3182 errnum = err; 3183 return (1); 3184 } 3185 3186 grub_printf("loading '%s' ...\n", cmdline_sav); 3187 mb_cmdline += grub_strlen(cmdline_sav) + 1; 3188 return (0); 3189 } 3190 3191 static struct builtin builtin_module_dollar = 3192 { 3193 "module$", 3194 module_dollar_func, 3195 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 3196 "module FILE [ARG ...]", 3197 " Just like module, but with $ISADIR expansion." 3198 }; 3199 3200 3201 /* modulenounzip */ 3202 static int 3203 modulenounzip_func (char *arg, int flags) 3204 { 3205 int ret; 3206 3207 #ifndef NO_DECOMPRESSION 3208 no_decompression = 1; 3209 #endif 3210 3211 ret = module_func (arg, flags); 3212 3213 #ifndef NO_DECOMPRESSION 3214 no_decompression = 0; 3215 #endif 3216 3217 return ret; 3218 } 3219 3220 static struct builtin builtin_modulenounzip = 3221 { 3222 "modulenounzip", 3223 modulenounzip_func, 3224 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 3225 "modulenounzip FILE [ARG ...]", 3226 "The same as `module', except that automatic decompression is" 3227 " disabled." 3228 }; 3229 3230 3231 /* pager [on|off] */ 3232 static int 3233 pager_func (char *arg, int flags) 3234 { 3235 /* If ARG is empty, toggle the flag. */ 3236 if (! *arg) 3237 use_pager = ! use_pager; 3238 else if (grub_memcmp (arg, "on", 2) == 0) 3239 use_pager = 1; 3240 else if (grub_memcmp (arg, "off", 3) == 0) 3241 use_pager = 0; 3242 else 3243 { 3244 errnum = ERR_BAD_ARGUMENT; 3245 return 1; 3246 } 3247 3248 grub_printf (" Internal pager is now %s\n", use_pager ? "on" : "off"); 3249 return 0; 3250 } 3251 3252 static struct builtin builtin_pager = 3253 { 3254 "pager", 3255 pager_func, 3256 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_HELP_LIST, 3257 "pager [FLAG]", 3258 "Toggle pager mode with no argument. If FLAG is given and its value" 3259 " is `on', turn on the mode. If FLAG is `off', turn off the mode." 3260 }; 3261 3262 3263 /* partnew PART TYPE START LEN */ 3264 static int 3265 partnew_func (char *arg, int flags) 3266 { 3267 int new_type, new_start, new_len; 3268 int start_cl, start_ch, start_dh; 3269 int end_cl, end_ch, end_dh; 3270 int entry; 3271 char mbr[512]; 3272 3273 /* Convert a LBA address to a CHS address in the INT 13 format. */ 3274 auto void lba_to_chs (int lba, int *cl, int *ch, int *dh); 3275 void lba_to_chs (int lba, int *cl, int *ch, int *dh) 3276 { 3277 int cylinder, head, sector; 3278 3279 sector = lba % buf_geom.sectors + 1; 3280 head = (lba / buf_geom.sectors) % buf_geom.heads; 3281 cylinder = lba / (buf_geom.sectors * buf_geom.heads); 3282 3283 if (cylinder >= buf_geom.cylinders) 3284 cylinder = buf_geom.cylinders - 1; 3285 3286 *cl = sector | ((cylinder & 0x300) >> 2); 3287 *ch = cylinder & 0xFF; 3288 *dh = head; 3289 } 3290 3291 /* Get the drive and the partition. */ 3292 if (! set_device (arg)) 3293 return 1; 3294 3295 /* The drive must be a hard disk. */ 3296 if (! (current_drive & 0x80)) 3297 { 3298 errnum = ERR_BAD_ARGUMENT; 3299 return 1; 3300 } 3301 3302 /* The partition must a primary partition. */ 3303 if ((current_partition >> 16) > 3 3304 || (current_partition & 0xFFFF) != 0xFFFF) 3305 { 3306 errnum = ERR_BAD_ARGUMENT; 3307 return 1; 3308 } 3309 3310 entry = current_partition >> 16; 3311 3312 /* Get the new partition type. */ 3313 arg = skip_to (0, arg); 3314 if (! safe_parse_maxint (&arg, &new_type)) 3315 return 1; 3316 3317 /* The partition type is unsigned char. */ 3318 if (new_type > 0xFF) 3319 { 3320 errnum = ERR_BAD_ARGUMENT; 3321 return 1; 3322 } 3323 3324 /* Get the new partition start. */ 3325 arg = skip_to (0, arg); 3326 if (! safe_parse_maxint (&arg, &new_start)) 3327 return 1; 3328 3329 /* Get the new partition length. */ 3330 arg = skip_to (0, arg); 3331 if (! safe_parse_maxint (&arg, &new_len)) 3332 return 1; 3333 3334 /* Read the MBR. */ 3335 if (! rawread (current_drive, 0, 0, SECTOR_SIZE, mbr)) 3336 return 1; 3337 3338 /* Store the partition information in the MBR. */ 3339 lba_to_chs (new_start, &start_cl, &start_ch, &start_dh); 3340 lba_to_chs (new_start + new_len - 1, &end_cl, &end_ch, &end_dh); 3341 3342 PC_SLICE_FLAG (mbr, entry) = 0; 3343 PC_SLICE_HEAD (mbr, entry) = start_dh; 3344 PC_SLICE_SEC (mbr, entry) = start_cl; 3345 PC_SLICE_CYL (mbr, entry) = start_ch; 3346 PC_SLICE_TYPE (mbr, entry) = new_type; 3347 PC_SLICE_EHEAD (mbr, entry) = end_dh; 3348 PC_SLICE_ESEC (mbr, entry) = end_cl; 3349 PC_SLICE_ECYL (mbr, entry) = end_ch; 3350 PC_SLICE_START (mbr, entry) = new_start; 3351 PC_SLICE_LENGTH (mbr, entry) = new_len; 3352 3353 /* Make sure that the MBR has a valid signature. */ 3354 PC_MBR_SIG (mbr) = PC_MBR_SIGNATURE; 3355 3356 /* Write back the MBR to the disk. */ 3357 buf_track = BUF_CACHE_INVALID; 3358 if (! rawwrite (current_drive, 0, mbr)) 3359 return 1; 3360 3361 return 0; 3362 } 3363 3364 static struct builtin builtin_partnew = 3365 { 3366 "partnew", 3367 partnew_func, 3368 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_HELP_LIST, 3369 "partnew PART TYPE START LEN", 3370 "Create a primary partition at the starting address START with the" 3371 " length LEN, with the type TYPE. START and LEN are in sector units." 3372 }; 3373 3374 3375 /* parttype PART TYPE */ 3376 static int 3377 parttype_func (char *arg, int flags) 3378 { 3379 int new_type; 3380 unsigned long part = 0xFFFFFF; 3381 unsigned long start, len, offset, ext_offset; 3382 int entry, type; 3383 char mbr[512]; 3384 3385 /* Get the drive and the partition. */ 3386 if (! set_device (arg)) 3387 return 1; 3388 3389 /* The drive must be a hard disk. */ 3390 if (! (current_drive & 0x80)) 3391 { 3392 errnum = ERR_BAD_ARGUMENT; 3393 return 1; 3394 } 3395 3396 /* The partition must be a PC slice. */ 3397 if ((current_partition >> 16) == 0xFF 3398 || (current_partition & 0xFFFF) != 0xFFFF) 3399 { 3400 errnum = ERR_BAD_ARGUMENT; 3401 return 1; 3402 } 3403 3404 /* Get the new partition type. */ 3405 arg = skip_to (0, arg); 3406 if (! safe_parse_maxint (&arg, &new_type)) 3407 return 1; 3408 3409 /* The partition type is unsigned char. */ 3410 if (new_type > 0xFF) 3411 { 3412 errnum = ERR_BAD_ARGUMENT; 3413 return 1; 3414 } 3415 3416 /* Look for the partition. */ 3417 while (next_partition (current_drive, 0xFFFFFF, &part, &type, 3418 &start, &len, &offset, &entry, 3419 &ext_offset, mbr)) 3420 { 3421 if (part == current_partition) 3422 { 3423 /* Found. */ 3424 3425 /* Set the type to NEW_TYPE. */ 3426 PC_SLICE_TYPE (mbr, entry) = new_type; 3427 3428 /* Write back the MBR to the disk. */ 3429 buf_track = BUF_CACHE_INVALID; 3430 if (! rawwrite (current_drive, offset, mbr)) 3431 return 1; 3432 3433 /* Succeed. */ 3434 return 0; 3435 } 3436 } 3437 3438 /* The partition was not found. ERRNUM was set by next_partition. */ 3439 return 1; 3440 } 3441 3442 static struct builtin builtin_parttype = 3443 { 3444 "parttype", 3445 parttype_func, 3446 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_HELP_LIST, 3447 "parttype PART TYPE", 3448 "Change the type of the partition PART to TYPE." 3449 }; 3450 3451 3452 /* password */ 3453 static int 3454 password_func (char *arg, int flags) 3455 { 3456 int len; 3457 password_t type = PASSWORD_PLAIN; 3458 3459 #ifdef USE_MD5_PASSWORDS 3460 if (grub_memcmp (arg, "--md5", 5) == 0) 3461 { 3462 type = PASSWORD_MD5; 3463 arg = skip_to (0, arg); 3464 } 3465 #endif 3466 if (grub_memcmp (arg, "--", 2) == 0) 3467 { 3468 type = PASSWORD_UNSUPPORTED; 3469 arg = skip_to (0, arg); 3470 } 3471 3472 if ((flags & (BUILTIN_CMDLINE | BUILTIN_SCRIPT)) != 0) 3473 { 3474 /* Do password check! */ 3475 char entered[32]; 3476 3477 /* Wipe out any previously entered password */ 3478 entered[0] = 0; 3479 get_cmdline ("Password: ", entered, 31, '*', 0); 3480 3481 nul_terminate (arg); 3482 if (check_password (entered, arg, type) != 0) 3483 { 3484 errnum = ERR_PRIVILEGED; 3485 return 1; 3486 } 3487 } 3488 else 3489 { 3490 len = grub_strlen (arg); 3491 3492 /* PASSWORD NUL NUL ... */ 3493 if (len + 2 > PASSWORD_BUFLEN) 3494 { 3495 errnum = ERR_WONT_FIT; 3496 return 1; 3497 } 3498 3499 /* Copy the password and clear the rest of the buffer. */ 3500 password = (char *) PASSWORD_BUF; 3501 grub_memmove (password, arg, len); 3502 grub_memset (password + len, 0, PASSWORD_BUFLEN - len); 3503 password_type = type; 3504 } 3505 return 0; 3506 } 3507 3508 static struct builtin builtin_password = 3509 { 3510 "password", 3511 password_func, 3512 BUILTIN_MENU | BUILTIN_CMDLINE | BUILTIN_NO_ECHO, 3513 "password [--md5] PASSWD [FILE]", 3514 "If used in the first section of a menu file, disable all" 3515 " interactive editing control (menu entry editor and" 3516 " command line). If the password PASSWD is entered, it loads the" 3517 " FILE as a new config file and restarts the GRUB Stage 2. If you" 3518 " omit the argument FILE, then GRUB just unlocks privileged" 3519 " instructions. You can also use it in the script section, in" 3520 " which case it will ask for the password, before continueing." 3521 " The option --md5 tells GRUB that PASSWD is encrypted with" 3522 " md5crypt." 3523 }; 3524 3525 3526 /* pause */ 3527 static int 3528 pause_func (char *arg, int flags) 3529 { 3530 printf("%s\n", arg); 3531 3532 /* If ESC is returned, then abort this entry. */ 3533 if (ASCII_CHAR (getkey ()) == 27) 3534 return 1; 3535 3536 return 0; 3537 } 3538 3539 static struct builtin builtin_pause = 3540 { 3541 "pause", 3542 pause_func, 3543 BUILTIN_CMDLINE | BUILTIN_NO_ECHO, 3544 "pause [MESSAGE ...]", 3545 "Print MESSAGE, then wait until a key is pressed." 3546 }; 3547 3548 3549 #ifdef GRUB_UTIL 3550 /* quit */ 3551 static int 3552 quit_func (char *arg, int flags) 3553 { 3554 stop (); 3555 3556 /* Never reach here. */ 3557 return 0; 3558 } 3559 3560 static struct builtin builtin_quit = 3561 { 3562 "quit", 3563 quit_func, 3564 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 3565 "quit", 3566 "Exit from the GRUB shell." 3567 }; 3568 #endif /* GRUB_UTIL */ 3569 3570 3571 #ifdef SUPPORT_NETBOOT 3572 /* rarp */ 3573 static int 3574 rarp_func (char *arg, int flags) 3575 { 3576 if (! rarp ()) 3577 { 3578 if (errnum == ERR_NONE) 3579 errnum = ERR_DEV_VALUES; 3580 3581 return 1; 3582 } 3583 3584 /* Notify the configuration. */ 3585 print_network_configuration (); 3586 return 0; 3587 } 3588 3589 static struct builtin builtin_rarp = 3590 { 3591 "rarp", 3592 rarp_func, 3593 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_HELP_LIST, 3594 "rarp", 3595 "Initialize a network device via RARP." 3596 }; 3597 #endif /* SUPPORT_NETBOOT */ 3598 3599 3600 static int 3601 read_func (char *arg, int flags) 3602 { 3603 int addr; 3604 3605 if (! safe_parse_maxint (&arg, &addr)) 3606 return 1; 3607 3608 grub_printf ("Address 0x%x: Value 0x%x\n", 3609 addr, *((unsigned *) RAW_ADDR (addr))); 3610 return 0; 3611 } 3612 3613 static struct builtin builtin_read = 3614 { 3615 "read", 3616 read_func, 3617 BUILTIN_CMDLINE, 3618 "read ADDR", 3619 "Read a 32-bit value from memory at address ADDR and" 3620 " display it in hex format." 3621 }; 3622 3623 3624 /* reboot */ 3625 static int 3626 reboot_func (char *arg, int flags) 3627 { 3628 grub_reboot (); 3629 3630 /* Never reach here. */ 3631 return 1; 3632 } 3633 3634 static struct builtin builtin_reboot = 3635 { 3636 "reboot", 3637 reboot_func, 3638 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 3639 "reboot", 3640 "Reboot your system." 3641 }; 3642 3643 3644 /* Print the root device information. */ 3645 static void 3646 print_root_device (void) 3647 { 3648 if (saved_drive == NETWORK_DRIVE) 3649 { 3650 /* Network drive. */ 3651 grub_printf (" (nd):"); 3652 } 3653 else if (saved_drive & 0x80) 3654 { 3655 /* Hard disk drive. */ 3656 grub_printf (" (hd%d", saved_drive - 0x80); 3657 3658 if ((saved_partition & 0xFF0000) != 0xFF0000) 3659 grub_printf (",%d", saved_partition >> 16); 3660 3661 if ((saved_partition & 0x00FF00) != 0x00FF00) 3662 grub_printf (",%c", ((saved_partition >> 8) & 0xFF) + 'a'); 3663 3664 grub_printf ("):"); 3665 } 3666 else 3667 { 3668 /* Floppy disk drive. */ 3669 grub_printf (" (fd%d):", saved_drive); 3670 } 3671 3672 /* Print the filesystem information. */ 3673 current_partition = saved_partition; 3674 current_drive = saved_drive; 3675 print_fsys_type (); 3676 } 3677 3678 static int 3679 real_root_func (char *arg, int attempt_mount) 3680 { 3681 int hdbias = 0; 3682 char *biasptr; 3683 char *next; 3684 3685 /* If ARG is empty, just print the current root device. */ 3686 if (! *arg) 3687 { 3688 print_root_device (); 3689 return 0; 3690 } 3691 3692 /* Call set_device to get the drive and the partition in ARG. */ 3693 next = set_device (arg); 3694 if (! next) 3695 return 1; 3696 3697 /* Ignore ERR_FSYS_MOUNT. */ 3698 if (attempt_mount) 3699 { 3700 if (! open_device () && errnum != ERR_FSYS_MOUNT) 3701 return 1; 3702 } 3703 else 3704 { 3705 /* This is necessary, because the location of a partition table 3706 must be set appropriately. */ 3707 if (open_partition ()) 3708 { 3709 set_bootdev (0); 3710 if (errnum) 3711 return 1; 3712 } 3713 } 3714 3715 /* Clear ERRNUM. */ 3716 errnum = 0; 3717 saved_partition = current_partition; 3718 saved_drive = current_drive; 3719 3720 if (attempt_mount) 3721 { 3722 /* BSD and chainloading evil hacks !! */ 3723 biasptr = skip_to (0, next); 3724 safe_parse_maxint (&biasptr, &hdbias); 3725 errnum = 0; 3726 bootdev = set_bootdev (hdbias); 3727 if (errnum) 3728 return 1; 3729 3730 /* Print the type of the filesystem. */ 3731 print_fsys_type (); 3732 } 3733 3734 return 0; 3735 } 3736 3737 static int 3738 root_func (char *arg, int flags) 3739 { 3740 is_zfs_mount = 0; 3741 return real_root_func (arg, 1); 3742 } 3743 3744 static struct builtin builtin_root = 3745 { 3746 "root", 3747 root_func, 3748 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 3749 "root [DEVICE [HDBIAS]]", 3750 "Set the current \"root device\" to the device DEVICE, then" 3751 " attempt to mount it to get the partition size (for passing the" 3752 " partition descriptor in `ES:ESI', used by some chain-loaded" 3753 " bootloaders), the BSD drive-type (for booting BSD kernels using" 3754 " their native boot format), and correctly determine " 3755 " the PC partition where a BSD sub-partition is located. The" 3756 " optional HDBIAS parameter is a number to tell a BSD kernel" 3757 " how many BIOS drive numbers are on controllers before the current" 3758 " one. For example, if there is an IDE disk and a SCSI disk, and your" 3759 " FreeBSD root partition is on the SCSI disk, then use a `1' for HDBIAS." 3760 }; 3761 3762 3763 /* findroot */ 3764 int 3765 findroot_func (char *arg, int flags) 3766 { 3767 int ret; 3768 char root[32]; 3769 3770 if (grub_strlen(arg) >= BOOTSIGN_ARGLEN) { 3771 errnum = ERR_BAD_ARGUMENT; 3772 return 1; 3773 } 3774 3775 if (arg[0] == '\0') { 3776 errnum = ERR_BAD_ARGUMENT; 3777 return 1; 3778 } 3779 3780 find_best_root = 1; 3781 best_drive = 0; 3782 best_part = 0; 3783 ret = find_common(arg, root, 1, flags); 3784 if (ret != 0) 3785 return (ret); 3786 find_best_root = 0; 3787 3788 return real_root_func (root, 1); 3789 } 3790 3791 static struct builtin builtin_findroot = 3792 { 3793 "findroot", 3794 findroot_func, 3795 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 3796 "findroot <SIGNATURE | (SIGNATURE,partition[,slice])>", 3797 "Searches across all partitions for the file name SIGNATURE." 3798 " GRUB looks only in the directory /boot/grub/bootsign for the" 3799 " filename and it stops as soon as it finds the first instance of" 3800 " the file - so to be useful the name of the signature file must be" 3801 " unique across all partitions. Once the signature file is found," 3802 " GRUB invokes the \"root\" command on that partition." 3803 " An optional partition and slice may be specified to optimize the search." 3804 }; 3805 3806 3807 /* 3808 * COMMAND to override the default root filesystem for ZFS 3809 * bootfs pool/fs 3810 */ 3811 static int 3812 bootfs_func (char *arg, int flags) 3813 { 3814 int hdbias = 0; 3815 char *biasptr; 3816 char *next; 3817 3818 if (! *arg) { 3819 if (current_bootfs[0] != '\0') 3820 grub_printf ("The zfs boot filesystem is set to '%s'.\n", 3821 current_bootfs); 3822 else if (current_rootpool[0] != 0 && current_bootfs_obj != 0) 3823 grub_printf("The zfs boot filesystem is <default: %s/%u>.", 3824 current_rootpool, current_bootfs_obj); 3825 else 3826 grub_printf ("The zfs boot filesystem will be derived from " 3827 "the default bootfs pool property.\n"); 3828 3829 return (1); 3830 } 3831 3832 /* Verify the zfs filesystem name */ 3833 if (arg[0] == '/' || arg[0] == '\0') { 3834 errnum = ERR_BAD_ARGUMENT; 3835 return 0; 3836 } 3837 if (current_rootpool[0] != 0 && grub_strncmp(arg, 3838 current_rootpool, strlen(current_rootpool))) { 3839 errnum = ERR_BAD_ARGUMENT; 3840 return 0; 3841 } 3842 3843 grub_memmove(current_bootfs, arg, MAXNAMELEN); 3844 3845 return (1); 3846 } 3847 3848 static struct builtin builtin_bootfs = 3849 { 3850 "bootfs", 3851 bootfs_func, 3852 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 3853 "bootfs [ZFSBOOTFS]", 3854 "Set the current zfs boot filesystem to ZFSBOOTFS (rootpool/rootfs)." 3855 }; 3856 3857 3858 /* rootnoverify */ 3859 static int 3860 rootnoverify_func (char *arg, int flags) 3861 { 3862 return real_root_func (arg, 0); 3863 } 3864 3865 static struct builtin builtin_rootnoverify = 3866 { 3867 "rootnoverify", 3868 rootnoverify_func, 3869 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 3870 "rootnoverify [DEVICE [HDBIAS]]", 3871 "Similar to `root', but don't attempt to mount the partition. This" 3872 " is useful for when an OS is outside of the area of the disk that" 3873 " GRUB can read, but setting the correct root device is still" 3874 " desired. Note that the items mentioned in `root' which" 3875 " derived from attempting the mount will NOT work correctly." 3876 }; 3877 3878 3879 /* savedefault */ 3880 static int 3881 savedefault_func (char *arg, int flags) 3882 { 3883 #if !defined(SUPPORT_DISKLESS) && !defined(GRUB_UTIL) 3884 unsigned long tmp_drive = saved_drive; 3885 unsigned long tmp_partition = saved_partition; 3886 char *default_file = (char *) DEFAULT_FILE_BUF; 3887 char buf[10]; 3888 char sect[SECTOR_SIZE]; 3889 int entryno; 3890 int sector_count = 0; 3891 unsigned int saved_sectors[2]; 3892 int saved_offsets[2]; 3893 int saved_lengths[2]; 3894 3895 /* not supported for zfs root */ 3896 if (is_zfs_mount == 1) { 3897 return (0); /* no-op */ 3898 } 3899 3900 /* Save sector information about at most two sectors. */ 3901 auto void disk_read_savesect_func (unsigned int sector, int offset, 3902 int length); 3903 void disk_read_savesect_func (unsigned int sector, int offset, int length) 3904 { 3905 if (sector_count < 2) 3906 { 3907 saved_sectors[sector_count] = sector; 3908 saved_offsets[sector_count] = offset; 3909 saved_lengths[sector_count] = length; 3910 } 3911 sector_count++; 3912 } 3913 3914 /* This command is only useful when you boot an entry from the menu 3915 interface. */ 3916 if (! (flags & BUILTIN_SCRIPT)) 3917 { 3918 errnum = ERR_UNRECOGNIZED; 3919 return 1; 3920 } 3921 3922 /* Determine a saved entry number. */ 3923 if (*arg) 3924 { 3925 if (grub_memcmp (arg, "fallback", sizeof ("fallback") - 1) == 0) 3926 { 3927 int i; 3928 int index = 0; 3929 3930 for (i = 0; i < MAX_FALLBACK_ENTRIES; i++) 3931 { 3932 if (fallback_entries[i] < 0) 3933 break; 3934 if (fallback_entries[i] == current_entryno) 3935 { 3936 index = i + 1; 3937 break; 3938 } 3939 } 3940 3941 if (index >= MAX_FALLBACK_ENTRIES || fallback_entries[index] < 0) 3942 { 3943 /* This is the last. */ 3944 errnum = ERR_BAD_ARGUMENT; 3945 return 1; 3946 } 3947 3948 entryno = fallback_entries[index]; 3949 } 3950 else if (! safe_parse_maxint (&arg, &entryno)) 3951 return 1; 3952 } 3953 else 3954 entryno = current_entryno; 3955 3956 /* Open the default file. */ 3957 saved_drive = boot_drive; 3958 saved_partition = install_partition; 3959 if (grub_open (default_file)) 3960 { 3961 int len; 3962 3963 disk_read_hook = disk_read_savesect_func; 3964 len = grub_read (buf, sizeof (buf)); 3965 disk_read_hook = 0; 3966 grub_close (); 3967 3968 if (len != sizeof (buf)) 3969 { 3970 /* This is too small. Do not modify the file manually, please! */ 3971 errnum = ERR_READ; 3972 goto fail; 3973 } 3974 3975 if (sector_count > 2) 3976 { 3977 /* Is this possible?! Too fragmented! */ 3978 errnum = ERR_FSYS_CORRUPT; 3979 goto fail; 3980 } 3981 3982 /* Set up a string to be written. */ 3983 grub_memset (buf, '\n', sizeof (buf)); 3984 grub_sprintf (buf, "%d", entryno); 3985 3986 if (saved_lengths[0] < sizeof (buf)) 3987 { 3988 /* The file is anchored to another file and the first few bytes 3989 are spanned in two sectors. Uggh... */ 3990 if (! rawread (current_drive, saved_sectors[0], 0, SECTOR_SIZE, 3991 sect)) 3992 goto fail; 3993 grub_memmove (sect + saved_offsets[0], buf, saved_lengths[0]); 3994 if (! rawwrite (current_drive, saved_sectors[0], sect)) 3995 goto fail; 3996 3997 if (! rawread (current_drive, saved_sectors[1], 0, SECTOR_SIZE, 3998 sect)) 3999 goto fail; 4000 grub_memmove (sect + saved_offsets[1], 4001 buf + saved_lengths[0], 4002 sizeof (buf) - saved_lengths[0]); 4003 if (! rawwrite (current_drive, saved_sectors[1], sect)) 4004 goto fail; 4005 } 4006 else 4007 { 4008 /* This is a simple case. It fits into a single sector. */ 4009 if (! rawread (current_drive, saved_sectors[0], 0, SECTOR_SIZE, 4010 sect)) 4011 goto fail; 4012 grub_memmove (sect + saved_offsets[0], buf, sizeof (buf)); 4013 if (! rawwrite (current_drive, saved_sectors[0], sect)) 4014 goto fail; 4015 } 4016 4017 /* Clear the cache. */ 4018 buf_track = BUF_CACHE_INVALID; 4019 } 4020 4021 fail: 4022 saved_drive = tmp_drive; 4023 saved_partition = tmp_partition; 4024 return errnum; 4025 #else /* ! SUPPORT_DISKLESS && ! GRUB_UTIL */ 4026 errnum = ERR_UNRECOGNIZED; 4027 return 1; 4028 #endif /* ! SUPPORT_DISKLESS && ! GRUB_UTIL */ 4029 } 4030 4031 static struct builtin builtin_savedefault = 4032 { 4033 "savedefault", 4034 savedefault_func, 4035 BUILTIN_CMDLINE, 4036 "savedefault [NUM | `fallback']", 4037 "Save the current entry as the default boot entry if no argument is" 4038 " specified. If a number is specified, this number is saved. If" 4039 " `fallback' is used, next fallback entry is saved." 4040 }; 4041 4042 4043 #ifdef SUPPORT_SERIAL 4044 /* serial */ 4045 static int 4046 serial_func (char *arg, int flags) 4047 { 4048 unsigned short port = serial_hw_get_port (0); 4049 unsigned int speed = 9600; 4050 int word_len = UART_8BITS_WORD; 4051 int parity = UART_NO_PARITY; 4052 int stop_bit_len = UART_1_STOP_BIT; 4053 4054 /* Process GNU-style long options. 4055 FIXME: We should implement a getopt-like function, to avoid 4056 duplications. */ 4057 while (1) 4058 { 4059 if (grub_memcmp (arg, "--unit=", sizeof ("--unit=") - 1) == 0) 4060 { 4061 char *p = arg + sizeof ("--unit=") - 1; 4062 int unit; 4063 4064 if (! safe_parse_maxint (&p, &unit)) 4065 return 1; 4066 4067 if (unit < 0 || unit > 3) 4068 { 4069 errnum = ERR_DEV_VALUES; 4070 return 1; 4071 } 4072 4073 port = serial_hw_get_port (unit); 4074 } 4075 else if (grub_memcmp (arg, "--speed=", sizeof ("--speed=") - 1) == 0) 4076 { 4077 char *p = arg + sizeof ("--speed=") - 1; 4078 int num; 4079 4080 if (! safe_parse_maxint (&p, &num)) 4081 return 1; 4082 4083 speed = (unsigned int) num; 4084 } 4085 else if (grub_memcmp (arg, "--port=", sizeof ("--port=") - 1) == 0) 4086 { 4087 char *p = arg + sizeof ("--port=") - 1; 4088 int num; 4089 4090 if (! safe_parse_maxint (&p, &num)) 4091 return 1; 4092 4093 port = (unsigned short) num; 4094 } 4095 else if (grub_memcmp (arg, "--word=", sizeof ("--word=") - 1) == 0) 4096 { 4097 char *p = arg + sizeof ("--word=") - 1; 4098 int len; 4099 4100 if (! safe_parse_maxint (&p, &len)) 4101 return 1; 4102 4103 switch (len) 4104 { 4105 case 5: word_len = UART_5BITS_WORD; break; 4106 case 6: word_len = UART_6BITS_WORD; break; 4107 case 7: word_len = UART_7BITS_WORD; break; 4108 case 8: word_len = UART_8BITS_WORD; break; 4109 default: 4110 errnum = ERR_BAD_ARGUMENT; 4111 return 1; 4112 } 4113 } 4114 else if (grub_memcmp (arg, "--stop=", sizeof ("--stop=") - 1) == 0) 4115 { 4116 char *p = arg + sizeof ("--stop=") - 1; 4117 int len; 4118 4119 if (! safe_parse_maxint (&p, &len)) 4120 return 1; 4121 4122 switch (len) 4123 { 4124 case 1: stop_bit_len = UART_1_STOP_BIT; break; 4125 case 2: stop_bit_len = UART_2_STOP_BITS; break; 4126 default: 4127 errnum = ERR_BAD_ARGUMENT; 4128 return 1; 4129 } 4130 } 4131 else if (grub_memcmp (arg, "--parity=", sizeof ("--parity=") - 1) == 0) 4132 { 4133 char *p = arg + sizeof ("--parity=") - 1; 4134 4135 if (grub_memcmp (p, "no", sizeof ("no") - 1) == 0) 4136 parity = UART_NO_PARITY; 4137 else if (grub_memcmp (p, "odd", sizeof ("odd") - 1) == 0) 4138 parity = UART_ODD_PARITY; 4139 else if (grub_memcmp (p, "even", sizeof ("even") - 1) == 0) 4140 parity = UART_EVEN_PARITY; 4141 else 4142 { 4143 errnum = ERR_BAD_ARGUMENT; 4144 return 1; 4145 } 4146 } 4147 # ifdef GRUB_UTIL 4148 /* In the grub shell, don't use any port number but open a tty 4149 device instead. */ 4150 else if (grub_memcmp (arg, "--device=", sizeof ("--device=") - 1) == 0) 4151 { 4152 char *p = arg + sizeof ("--device=") - 1; 4153 char dev[256]; /* XXX */ 4154 char *q = dev; 4155 4156 while (*p && ! grub_isspace (*p)) 4157 *q++ = *p++; 4158 4159 *q = 0; 4160 serial_set_device (dev); 4161 } 4162 # endif /* GRUB_UTIL */ 4163 else 4164 break; 4165 4166 arg = skip_to (0, arg); 4167 } 4168 4169 /* Initialize the serial unit. */ 4170 if (! serial_hw_init (port, speed, word_len, parity, stop_bit_len)) 4171 { 4172 errnum = ERR_BAD_ARGUMENT; 4173 return 1; 4174 } 4175 4176 return 0; 4177 } 4178 4179 static struct builtin builtin_serial = 4180 { 4181 "serial", 4182 serial_func, 4183 BUILTIN_MENU | BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 4184 "serial [--unit=UNIT] [--port=PORT] [--speed=SPEED] [--word=WORD] [--parity=PARITY] [--stop=STOP] [--device=DEV]", 4185 "Initialize a serial device. UNIT is a digit that specifies which serial" 4186 " device is used (e.g. 0 == COM1). If you need to specify the port number," 4187 " set it by --port. SPEED is the DTE-DTE speed. WORD is the word length," 4188 " PARITY is the type of parity, which is one of `no', `odd' and `even'." 4189 " STOP is the length of stop bit(s). The option --device can be used only" 4190 " in the grub shell, which specifies the file name of a tty device. The" 4191 " default values are COM1, 9600, 8N1." 4192 }; 4193 #endif /* SUPPORT_SERIAL */ 4194 4195 4196 /* setkey */ 4197 struct keysym 4198 { 4199 char *unshifted_name; /* the name in unshifted state */ 4200 char *shifted_name; /* the name in shifted state */ 4201 unsigned char unshifted_ascii; /* the ascii code in unshifted state */ 4202 unsigned char shifted_ascii; /* the ascii code in shifted state */ 4203 unsigned char keycode; /* keyboard scancode */ 4204 }; 4205 4206 /* The table for key symbols. If the "shifted" member of an entry is 4207 NULL, the entry does not have shifted state. */ 4208 static struct keysym keysym_table[] = 4209 { 4210 {"escape", 0, 0x1b, 0, 0x01}, 4211 {"1", "exclam", '1', '!', 0x02}, 4212 {"2", "at", '2', '@', 0x03}, 4213 {"3", "numbersign", '3', '#', 0x04}, 4214 {"4", "dollar", '4', '$', 0x05}, 4215 {"5", "percent", '5', '%', 0x06}, 4216 {"6", "caret", '6', '^', 0x07}, 4217 {"7", "ampersand", '7', '&', 0x08}, 4218 {"8", "asterisk", '8', '*', 0x09}, 4219 {"9", "parenleft", '9', '(', 0x0a}, 4220 {"0", "parenright", '0', ')', 0x0b}, 4221 {"minus", "underscore", '-', '_', 0x0c}, 4222 {"equal", "plus", '=', '+', 0x0d}, 4223 {"backspace", 0, '\b', 0, 0x0e}, 4224 {"tab", 0, '\t', 0, 0x0f}, 4225 {"q", "Q", 'q', 'Q', 0x10}, 4226 {"w", "W", 'w', 'W', 0x11}, 4227 {"e", "E", 'e', 'E', 0x12}, 4228 {"r", "R", 'r', 'R', 0x13}, 4229 {"t", "T", 't', 'T', 0x14}, 4230 {"y", "Y", 'y', 'Y', 0x15}, 4231 {"u", "U", 'u', 'U', 0x16}, 4232 {"i", "I", 'i', 'I', 0x17}, 4233 {"o", "O", 'o', 'O', 0x18}, 4234 {"p", "P", 'p', 'P', 0x19}, 4235 {"bracketleft", "braceleft", '[', '{', 0x1a}, 4236 {"bracketright", "braceright", ']', '}', 0x1b}, 4237 {"enter", 0, '\n', 0, 0x1c}, 4238 {"control", 0, 0, 0, 0x1d}, 4239 {"a", "A", 'a', 'A', 0x1e}, 4240 {"s", "S", 's', 'S', 0x1f}, 4241 {"d", "D", 'd', 'D', 0x20}, 4242 {"f", "F", 'f', 'F', 0x21}, 4243 {"g", "G", 'g', 'G', 0x22}, 4244 {"h", "H", 'h', 'H', 0x23}, 4245 {"j", "J", 'j', 'J', 0x24}, 4246 {"k", "K", 'k', 'K', 0x25}, 4247 {"l", "L", 'l', 'L', 0x26}, 4248 {"semicolon", "colon", ';', ':', 0x27}, 4249 {"quote", "doublequote", '\'', '"', 0x28}, 4250 {"backquote", "tilde", '`', '~', 0x29}, 4251 {"shift", 0, 0, 0, 0x2a}, 4252 {"backslash", "bar", '\\', '|', 0x2b}, 4253 {"z", "Z", 'z', 'Z', 0x2c}, 4254 {"x", "X", 'x', 'X', 0x2d}, 4255 {"c", "C", 'c', 'C', 0x2e}, 4256 {"v", "V", 'v', 'V', 0x2f}, 4257 {"b", "B", 'b', 'B', 0x30}, 4258 {"n", "N", 'n', 'N', 0x31}, 4259 {"m", "M", 'm', 'M', 0x32}, 4260 {"comma", "less", ',', '<', 0x33}, 4261 {"period", "greater", '.', '>', 0x34}, 4262 {"slash", "question", '/', '?', 0x35}, 4263 {"alt", 0, 0, 0, 0x38}, 4264 {"space", 0, ' ', 0, 0x39}, 4265 {"capslock", 0, 0, 0, 0x3a}, 4266 {"F1", 0, 0, 0, 0x3b}, 4267 {"F2", 0, 0, 0, 0x3c}, 4268 {"F3", 0, 0, 0, 0x3d}, 4269 {"F4", 0, 0, 0, 0x3e}, 4270 {"F5", 0, 0, 0, 0x3f}, 4271 {"F6", 0, 0, 0, 0x40}, 4272 {"F7", 0, 0, 0, 0x41}, 4273 {"F8", 0, 0, 0, 0x42}, 4274 {"F9", 0, 0, 0, 0x43}, 4275 {"F10", 0, 0, 0, 0x44}, 4276 /* Caution: do not add NumLock here! we cannot deal with it properly. */ 4277 {"delete", 0, 0x7f, 0, 0x53} 4278 }; 4279 4280 static int 4281 setkey_func (char *arg, int flags) 4282 { 4283 char *to_key, *from_key; 4284 int to_code, from_code; 4285 int map_in_interrupt = 0; 4286 4287 auto int find_key_code (char *key); 4288 auto int find_ascii_code (char *key); 4289 4290 auto int find_key_code (char *key) 4291 { 4292 int i; 4293 4294 for (i = 0; i < sizeof (keysym_table) / sizeof (keysym_table[0]); i++) 4295 { 4296 if (keysym_table[i].unshifted_name && 4297 grub_strcmp (key, keysym_table[i].unshifted_name) == 0) 4298 return keysym_table[i].keycode; 4299 else if (keysym_table[i].shifted_name && 4300 grub_strcmp (key, keysym_table[i].shifted_name) == 0) 4301 return keysym_table[i].keycode; 4302 } 4303 4304 return 0; 4305 } 4306 4307 auto int find_ascii_code (char *key) 4308 { 4309 int i; 4310 4311 for (i = 0; i < sizeof (keysym_table) / sizeof (keysym_table[0]); i++) 4312 { 4313 if (keysym_table[i].unshifted_name && 4314 grub_strcmp (key, keysym_table[i].unshifted_name) == 0) 4315 return keysym_table[i].unshifted_ascii; 4316 else if (keysym_table[i].shifted_name && 4317 grub_strcmp (key, keysym_table[i].shifted_name) == 0) 4318 return keysym_table[i].shifted_ascii; 4319 } 4320 4321 return 0; 4322 } 4323 4324 to_key = arg; 4325 from_key = skip_to (0, to_key); 4326 4327 if (! *to_key) 4328 { 4329 /* If the user specifies no argument, reset the key mappings. */ 4330 grub_memset (bios_key_map, 0, KEY_MAP_SIZE * sizeof (unsigned short)); 4331 grub_memset (ascii_key_map, 0, KEY_MAP_SIZE * sizeof (unsigned short)); 4332 4333 return 0; 4334 } 4335 else if (! *from_key) 4336 { 4337 /* The user must specify two arguments or zero argument. */ 4338 errnum = ERR_BAD_ARGUMENT; 4339 return 1; 4340 } 4341 4342 nul_terminate (to_key); 4343 nul_terminate (from_key); 4344 4345 to_code = find_ascii_code (to_key); 4346 from_code = find_ascii_code (from_key); 4347 if (! to_code || ! from_code) 4348 { 4349 map_in_interrupt = 1; 4350 to_code = find_key_code (to_key); 4351 from_code = find_key_code (from_key); 4352 if (! to_code || ! from_code) 4353 { 4354 errnum = ERR_BAD_ARGUMENT; 4355 return 1; 4356 } 4357 } 4358 4359 if (map_in_interrupt) 4360 { 4361 int i; 4362 4363 /* Find an empty slot. */ 4364 for (i = 0; i < KEY_MAP_SIZE; i++) 4365 { 4366 if ((bios_key_map[i] & 0xff) == from_code) 4367 /* Perhaps the user wants to overwrite the map. */ 4368 break; 4369 4370 if (! bios_key_map[i]) 4371 break; 4372 } 4373 4374 if (i == KEY_MAP_SIZE) 4375 { 4376 errnum = ERR_WONT_FIT; 4377 return 1; 4378 } 4379 4380 if (to_code == from_code) 4381 /* If TO is equal to FROM, delete the entry. */ 4382 grub_memmove ((char *) &bios_key_map[i], 4383 (char *) &bios_key_map[i + 1], 4384 sizeof (unsigned short) * (KEY_MAP_SIZE - i)); 4385 else 4386 bios_key_map[i] = (to_code << 8) | from_code; 4387 4388 /* Ugly but should work. */ 4389 unset_int15_handler (); 4390 set_int15_handler (); 4391 } 4392 else 4393 { 4394 int i; 4395 4396 /* Find an empty slot. */ 4397 for (i = 0; i < KEY_MAP_SIZE; i++) 4398 { 4399 if ((ascii_key_map[i] & 0xff) == from_code) 4400 /* Perhaps the user wants to overwrite the map. */ 4401 break; 4402 4403 if (! ascii_key_map[i]) 4404 break; 4405 } 4406 4407 if (i == KEY_MAP_SIZE) 4408 { 4409 errnum = ERR_WONT_FIT; 4410 return 1; 4411 } 4412 4413 if (to_code == from_code) 4414 /* If TO is equal to FROM, delete the entry. */ 4415 grub_memmove ((char *) &ascii_key_map[i], 4416 (char *) &ascii_key_map[i + 1], 4417 sizeof (unsigned short) * (KEY_MAP_SIZE - i)); 4418 else 4419 ascii_key_map[i] = (to_code << 8) | from_code; 4420 } 4421 4422 return 0; 4423 } 4424 4425 static struct builtin builtin_setkey = 4426 { 4427 "setkey", 4428 setkey_func, 4429 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_HELP_LIST, 4430 "setkey [TO_KEY FROM_KEY]", 4431 "Change the keyboard map. The key FROM_KEY is mapped to the key TO_KEY." 4432 " A key must be an alphabet, a digit, or one of these: escape, exclam," 4433 " at, numbersign, dollar, percent, caret, ampersand, asterisk, parenleft," 4434 " parenright, minus, underscore, equal, plus, backspace, tab, bracketleft," 4435 " braceleft, bracketright, braceright, enter, control, semicolon, colon," 4436 " quote, doublequote, backquote, tilde, shift, backslash, bar, comma," 4437 " less, period, greater, slash, question, alt, space, capslock, FX (X" 4438 " is a digit), and delete. If no argument is specified, reset key" 4439 " mappings." 4440 }; 4441 4442 4443 /* setup */ 4444 static int 4445 setup_func (char *arg, int flags) 4446 { 4447 /* Point to the string of the installed drive/partition. */ 4448 char *install_ptr; 4449 /* Point to the string of the drive/parition where the GRUB images 4450 reside. */ 4451 char *image_ptr; 4452 unsigned long installed_drive, installed_partition; 4453 unsigned long image_drive, image_partition; 4454 unsigned long tmp_drive, tmp_partition; 4455 char stage1[64]; 4456 char stage2[64]; 4457 char config_filename[64]; 4458 char real_config_filename[64]; 4459 char cmd_arg[256]; 4460 char device[16]; 4461 char *buffer = (char *) RAW_ADDR (0x100000); 4462 int is_force_lba = 0; 4463 char *stage2_arg = 0; 4464 char *prefix = 0; 4465 4466 auto int check_file (char *file); 4467 auto void sprint_device (int drive, int partition); 4468 auto int embed_stage1_5 (char * stage1_5, int drive, int partition); 4469 4470 /* Check if the file FILE exists like Autoconf. */ 4471 int check_file (char *file) 4472 { 4473 int ret; 4474 4475 grub_printf (" Checking if \"%s\" exists... ", file); 4476 ret = grub_open (file); 4477 if (ret) 4478 { 4479 grub_close (); 4480 grub_printf ("yes\n"); 4481 } 4482 else 4483 grub_printf ("no\n"); 4484 4485 return ret; 4486 } 4487 4488 /* Construct a device name in DEVICE. */ 4489 void sprint_device (int drive, int partition) 4490 { 4491 grub_sprintf (device, "(%cd%d", 4492 (drive & 0x80) ? 'h' : 'f', 4493 drive & ~0x80); 4494 if ((partition & 0xFF0000) != 0xFF0000) 4495 { 4496 char tmp[16]; 4497 grub_sprintf (tmp, ",%d", (partition >> 16) & 0xFF); 4498 grub_strncat (device, tmp, sizeof (device)); 4499 } 4500 if ((partition & 0x00FF00) != 0x00FF00) 4501 { 4502 char tmp[16]; 4503 grub_sprintf (tmp, ",%c", 'a' + ((partition >> 8) & 0xFF)); 4504 grub_strncat (device, tmp, sizeof (device)); 4505 } 4506 grub_strncat (device, ")", sizeof (device)); 4507 } 4508 4509 int embed_stage1_5 (char *stage1_5, int drive, int partition) 4510 { 4511 /* We install GRUB into the MBR, so try to embed the 4512 Stage 1.5 in the sectors right after the MBR. */ 4513 sprint_device (drive, partition); 4514 grub_sprintf (cmd_arg, "%s %s", stage1_5, device); 4515 4516 /* Notify what will be run. */ 4517 grub_printf (" Running \"embed %s\"... ", cmd_arg); 4518 4519 embed_func (cmd_arg, flags); 4520 if (! errnum) 4521 { 4522 /* Construct the blocklist representation. */ 4523 grub_sprintf (buffer, "%s%s", device, embed_info); 4524 grub_printf ("succeeded\n"); 4525 return 1; 4526 } 4527 else 4528 { 4529 grub_printf ("failed (this is not fatal)\n"); 4530 return 0; 4531 } 4532 } 4533 4534 struct stage1_5_map { 4535 char *fsys; 4536 char *name; 4537 }; 4538 struct stage1_5_map stage1_5_map[] = 4539 { 4540 {"ext2fs", "/e2fs_stage1_5"}, 4541 {"fat", "/fat_stage1_5"}, 4542 {"ufs2", "/ufs2_stage1_5"}, 4543 {"ffs", "/ffs_stage1_5"}, 4544 {"iso9660", "/iso9660_stage1_5"}, 4545 {"jfs", "/jfs_stage1_5"}, 4546 {"minix", "/minix_stage1_5"}, 4547 {"reiserfs", "/reiserfs_stage1_5"}, 4548 {"vstafs", "/vstafs_stage1_5"}, 4549 {"xfs", "/xfs_stage1_5"}, 4550 {"ufs", "/ufs_stage1_5"} 4551 }; 4552 4553 tmp_drive = saved_drive; 4554 tmp_partition = saved_partition; 4555 4556 /* Check if the user specifies --force-lba. */ 4557 while (1) 4558 { 4559 if (grub_memcmp ("--force-lba", arg, sizeof ("--force-lba") - 1) == 0) 4560 { 4561 is_force_lba = 1; 4562 arg = skip_to (0, arg); 4563 } 4564 else if (grub_memcmp ("--prefix=", arg, sizeof ("--prefix=") - 1) == 0) 4565 { 4566 prefix = arg + sizeof ("--prefix=") - 1; 4567 arg = skip_to (0, arg); 4568 nul_terminate (prefix); 4569 } 4570 #ifdef GRUB_UTIL 4571 else if (grub_memcmp ("--stage2=", arg, sizeof ("--stage2=") - 1) == 0) 4572 { 4573 stage2_arg = arg; 4574 arg = skip_to (0, arg); 4575 nul_terminate (stage2_arg); 4576 } 4577 #endif /* GRUB_UTIL */ 4578 else 4579 break; 4580 } 4581 4582 install_ptr = arg; 4583 image_ptr = skip_to (0, install_ptr); 4584 4585 /* Make sure that INSTALL_PTR is valid. */ 4586 set_device (install_ptr); 4587 if (errnum) 4588 return 1; 4589 4590 installed_drive = current_drive; 4591 installed_partition = current_partition; 4592 4593 /* Mount the drive pointed by IMAGE_PTR. */ 4594 if (*image_ptr) 4595 { 4596 /* If the drive/partition where the images reside is specified, 4597 get the drive and the partition. */ 4598 set_device (image_ptr); 4599 if (errnum) 4600 return 1; 4601 } 4602 else 4603 { 4604 /* If omitted, use SAVED_PARTITION and SAVED_DRIVE. */ 4605 current_drive = saved_drive; 4606 current_partition = saved_partition; 4607 } 4608 4609 image_drive = saved_drive = current_drive; 4610 image_partition = saved_partition = current_partition; 4611 4612 /* Open it. */ 4613 if (! open_device ()) 4614 goto fail; 4615 4616 /* Check if stage1 exists. If the user doesn't specify the option 4617 `--prefix', attempt /boot/grub and /grub. */ 4618 /* NOTE: It is dangerous to run this command without `--prefix' in the 4619 grub shell, since that affects `--stage2'. */ 4620 if (! prefix) 4621 { 4622 prefix = "/boot/grub"; 4623 grub_sprintf (stage1, "%s%s", prefix, "/stage1"); 4624 if (! check_file (stage1)) 4625 { 4626 errnum = ERR_NONE; 4627 prefix = "/grub"; 4628 grub_sprintf (stage1, "%s%s", prefix, "/stage1"); 4629 if (! check_file (stage1)) 4630 goto fail; 4631 } 4632 } 4633 else 4634 { 4635 grub_sprintf (stage1, "%s%s", prefix, "/stage1"); 4636 if (! check_file (stage1)) 4637 goto fail; 4638 } 4639 4640 /* The prefix was determined. */ 4641 grub_sprintf (stage2, "%s%s", prefix, "/stage2"); 4642 grub_sprintf (config_filename, "%s%s", prefix, "/menu.lst"); 4643 *real_config_filename = 0; 4644 4645 /* Check if stage2 exists. */ 4646 if (! check_file (stage2)) 4647 goto fail; 4648 4649 { 4650 char *fsys = fsys_table[fsys_type].name; 4651 int i; 4652 int size = sizeof (stage1_5_map) / sizeof (stage1_5_map[0]); 4653 4654 /* Iterate finding the same filesystem name as FSYS. */ 4655 for (i = 0; i < size; i++) 4656 if (grub_strcmp (fsys, stage1_5_map[i].fsys) == 0) 4657 { 4658 /* OK, check if the Stage 1.5 exists. */ 4659 char stage1_5[64]; 4660 4661 grub_sprintf (stage1_5, "%s%s", prefix, stage1_5_map[i].name); 4662 if (check_file (stage1_5)) 4663 { 4664 if (embed_stage1_5 (stage1_5, 4665 installed_drive, installed_partition) 4666 || embed_stage1_5 (stage1_5, 4667 image_drive, image_partition)) 4668 { 4669 grub_strcpy (real_config_filename, config_filename); 4670 sprint_device (image_drive, image_partition); 4671 grub_sprintf (config_filename, "%s%s", device, stage2); 4672 grub_strcpy (stage2, buffer); 4673 } 4674 } 4675 errnum = 0; 4676 break; 4677 } 4678 } 4679 4680 /* Construct a string that is used by the command "install" as its 4681 arguments. */ 4682 sprint_device (installed_drive, installed_partition); 4683 4684 #if 1 4685 /* Don't embed a drive number unnecessarily. */ 4686 grub_sprintf (cmd_arg, "%s%s%s%s %s%s %s p %s %s", 4687 is_force_lba? "--force-lba " : "", 4688 stage2_arg? stage2_arg : "", 4689 stage2_arg? " " : "", 4690 stage1, 4691 (installed_drive != image_drive) ? "d " : "", 4692 device, 4693 stage2, 4694 config_filename, 4695 real_config_filename); 4696 #else /* NOT USED */ 4697 /* This code was used, because we belived some BIOSes had a problem 4698 that they didn't pass a booting drive correctly. It turned out, 4699 however, stage1 could trash a booting drive when checking LBA support, 4700 because some BIOSes modified the register %dx in INT 13H, AH=48H. 4701 So it becamed unclear whether GRUB should use a pre-defined booting 4702 drive or not. If the problem still exists, it would be necessary to 4703 switch back to this code. */ 4704 grub_sprintf (cmd_arg, "%s%s%s%s d %s %s p %s %s", 4705 is_force_lba? "--force-lba " : "", 4706 stage2_arg? stage2_arg : "", 4707 stage2_arg? " " : "", 4708 stage1, 4709 device, 4710 stage2, 4711 config_filename, 4712 real_config_filename); 4713 #endif /* NOT USED */ 4714 4715 /* Notify what will be run. */ 4716 grub_printf (" Running \"install %s\"... ", cmd_arg); 4717 4718 /* Make sure that SAVED_DRIVE and SAVED_PARTITION are identical 4719 with IMAGE_DRIVE and IMAGE_PARTITION, respectively. */ 4720 saved_drive = image_drive; 4721 saved_partition = image_partition; 4722 4723 /* Run the command. */ 4724 if (! install_func (cmd_arg, flags)) 4725 grub_printf ("succeeded\nDone.\n"); 4726 else 4727 grub_printf ("failed\n"); 4728 4729 fail: 4730 saved_drive = tmp_drive; 4731 saved_partition = tmp_partition; 4732 return errnum; 4733 } 4734 4735 static struct builtin builtin_setup = 4736 { 4737 "setup", 4738 setup_func, 4739 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 4740 "setup [--prefix=DIR] [--stage2=STAGE2_FILE] [--force-lba] INSTALL_DEVICE [IMAGE_DEVICE]", 4741 "Set up the installation of GRUB automatically. This command uses" 4742 " the more flexible command \"install\" in the backend and installs" 4743 " GRUB into the device INSTALL_DEVICE. If IMAGE_DEVICE is specified," 4744 " then find the GRUB images in the device IMAGE_DEVICE, otherwise" 4745 " use the current \"root device\", which can be set by the command" 4746 " \"root\". If you know that your BIOS should support LBA but GRUB" 4747 " doesn't work in LBA mode, specify the option `--force-lba'." 4748 " If you install GRUB under the grub shell and you cannot unmount the" 4749 " partition where GRUB images reside, specify the option `--stage2'" 4750 " to tell GRUB the file name under your OS." 4751 }; 4752 4753 4754 #if defined(SUPPORT_SERIAL) || defined(SUPPORT_HERCULES) || defined(SUPPORT_GRAPHICS) 4755 /* terminal */ 4756 static int 4757 terminal_func (char *arg, int flags) 4758 { 4759 /* The index of the default terminal in TERM_TABLE. */ 4760 int default_term = -1; 4761 struct term_entry *prev_term = current_term; 4762 int to = -1; 4763 int lines = 0; 4764 int no_message = 0; 4765 unsigned long term_flags = 0; 4766 /* XXX: Assume less than 32 terminals. */ 4767 unsigned long term_bitmap = 0; 4768 4769 /* Get GNU-style long options. */ 4770 while (1) 4771 { 4772 if (grub_memcmp (arg, "--dumb", sizeof ("--dumb") - 1) == 0) 4773 term_flags |= TERM_DUMB; 4774 else if (grub_memcmp (arg, "--no-echo", sizeof ("--no-echo") - 1) == 0) 4775 /* ``--no-echo'' implies ``--no-edit''. */ 4776 term_flags |= (TERM_NO_ECHO | TERM_NO_EDIT); 4777 else if (grub_memcmp (arg, "--no-edit", sizeof ("--no-edit") - 1) == 0) 4778 term_flags |= TERM_NO_EDIT; 4779 else if (grub_memcmp (arg, "--timeout=", sizeof ("--timeout=") - 1) == 0) 4780 { 4781 char *val = arg + sizeof ("--timeout=") - 1; 4782 4783 if (! safe_parse_maxint (&val, &to)) 4784 return 1; 4785 } 4786 else if (grub_memcmp (arg, "--lines=", sizeof ("--lines=") - 1) == 0) 4787 { 4788 char *val = arg + sizeof ("--lines=") - 1; 4789 4790 if (! safe_parse_maxint (&val, &lines)) 4791 return 1; 4792 4793 /* Probably less than four is meaningless.... */ 4794 if (lines < 4) 4795 { 4796 errnum = ERR_BAD_ARGUMENT; 4797 return 1; 4798 } 4799 } 4800 else if (grub_memcmp (arg, "--silent", sizeof ("--silent") - 1) == 0) 4801 no_message = 1; 4802 else 4803 break; 4804 4805 arg = skip_to (0, arg); 4806 } 4807 4808 /* If no argument is specified, show current setting. */ 4809 if (! *arg) 4810 { 4811 grub_printf ("%s%s%s%s\n", 4812 current_term->name, 4813 current_term->flags & TERM_DUMB ? " (dumb)" : "", 4814 current_term->flags & TERM_NO_EDIT ? " (no edit)" : "", 4815 current_term->flags & TERM_NO_ECHO ? " (no echo)" : ""); 4816 return 0; 4817 } 4818 4819 while (*arg) 4820 { 4821 int i; 4822 char *next = skip_to (0, arg); 4823 4824 nul_terminate (arg); 4825 4826 for (i = 0; term_table[i].name; i++) 4827 { 4828 if (grub_strcmp (arg, term_table[i].name) == 0) 4829 { 4830 if (term_table[i].flags & TERM_NEED_INIT) 4831 { 4832 errnum = ERR_DEV_NEED_INIT; 4833 return 1; 4834 } 4835 4836 if (default_term < 0) 4837 default_term = i; 4838 4839 term_bitmap |= (1 << i); 4840 break; 4841 } 4842 } 4843 4844 if (! term_table[i].name) 4845 { 4846 errnum = ERR_BAD_ARGUMENT; 4847 return 1; 4848 } 4849 4850 arg = next; 4851 } 4852 4853 /* If multiple terminals are specified, wait until the user pushes any 4854 key on one of the terminals. */ 4855 if (term_bitmap & ~(1 << default_term)) 4856 { 4857 int time1, time2 = -1; 4858 4859 /* XXX: Disable the pager. */ 4860 count_lines = -1; 4861 4862 /* Get current time. */ 4863 while ((time1 = getrtsecs ()) == 0xFF) 4864 ; 4865 4866 /* Wait for a key input. */ 4867 while (to) 4868 { 4869 int i; 4870 4871 for (i = 0; term_table[i].name; i++) 4872 { 4873 if (term_bitmap & (1 << i)) 4874 { 4875 if (term_table[i].checkkey () >= 0) 4876 { 4877 (void) term_table[i].getkey (); 4878 default_term = i; 4879 4880 goto end; 4881 } 4882 } 4883 } 4884 4885 /* Prompt the user, once per sec. */ 4886 if ((time1 = getrtsecs ()) != time2 && time1 != 0xFF) 4887 { 4888 if (! no_message) 4889 { 4890 /* Need to set CURRENT_TERM to each of selected 4891 terminals. */ 4892 for (i = 0; term_table[i].name; i++) 4893 if (term_bitmap & (1 << i)) 4894 { 4895 current_term = term_table + i; 4896 grub_printf ("\rPress any key to continue.\n"); 4897 } 4898 4899 /* Restore CURRENT_TERM. */ 4900 current_term = prev_term; 4901 } 4902 4903 time2 = time1; 4904 if (to > 0) 4905 to--; 4906 } 4907 } 4908 } 4909 4910 end: 4911 current_term = term_table + default_term; 4912 current_term->flags = term_flags; 4913 4914 if (lines) 4915 max_lines = lines; 4916 else 4917 max_lines = current_term->max_lines; 4918 4919 /* If the interface is currently the command-line, 4920 restart it to repaint the screen. */ 4921 if ((current_term != prev_term) && (flags & BUILTIN_CMDLINE)){ 4922 if (prev_term->shutdown) 4923 prev_term->shutdown(); 4924 if (current_term->startup) 4925 current_term->startup(); 4926 grub_longjmp (restart_cmdline_env, 0); 4927 } 4928 4929 return 0; 4930 } 4931 4932 static struct builtin builtin_terminal = 4933 { 4934 "terminal", 4935 terminal_func, 4936 BUILTIN_MENU | BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 4937 "terminal [--dumb] [--no-echo] [--no-edit] [--timeout=SECS] [--lines=LINES] [--silent] [console] [serial] [hercules] [graphics] [composite]", 4938 "Select a terminal. When multiple terminals are specified, wait until" 4939 " you push any key to continue. If both console and serial are specified," 4940 " the terminal to which you input a key first will be selected. If no" 4941 " argument is specified, print current setting. To accomodate systems" 4942 " where console redirection may or may not be present, the composite" 4943 " console will direct output to the serial and BIOS consoles, and accept" 4944 " input from either one, without requiring selection. The option --dumb" 4945 " specifies that your terminal is dumb, otherwise, vt100-compatibility" 4946 " is assumed. If you specify --no-echo, input characters won't be echoed." 4947 " If you specify --no-edit, the BASH-like editing feature will be disabled." 4948 " If --timeout is present, this command will wait at most for SECS" 4949 " seconds. The option --lines specifies the maximum number of lines." 4950 " The option --silent is used to suppress messages." 4951 }; 4952 #endif /* SUPPORT_SERIAL || SUPPORT_HERCULES || SUPPORT_GRAPHICS */ 4953 4954 4955 #ifdef SUPPORT_SERIAL 4956 static int 4957 terminfo_func (char *arg, int flags) 4958 { 4959 struct terminfo term; 4960 4961 if (*arg) 4962 { 4963 struct 4964 { 4965 const char *name; 4966 char *var; 4967 } 4968 options[] = 4969 { 4970 {"--name=", term.name}, 4971 {"--cursor-address=", term.cursor_address}, 4972 {"--clear-screen=", term.clear_screen}, 4973 {"--enter-standout-mode=", term.enter_standout_mode}, 4974 {"--exit-standout-mode=", term.exit_standout_mode} 4975 }; 4976 4977 grub_memset (&term, 0, sizeof (term)); 4978 4979 while (*arg) 4980 { 4981 int i; 4982 char *next = skip_to (0, arg); 4983 4984 nul_terminate (arg); 4985 4986 for (i = 0; i < sizeof (options) / sizeof (options[0]); i++) 4987 { 4988 const char *name = options[i].name; 4989 int len = grub_strlen (name); 4990 4991 if (! grub_memcmp (arg, name, len)) 4992 { 4993 grub_strcpy (options[i].var, ti_unescape_string (arg + len)); 4994 break; 4995 } 4996 } 4997 4998 if (i == sizeof (options) / sizeof (options[0])) 4999 { 5000 errnum = ERR_BAD_ARGUMENT; 5001 return errnum; 5002 } 5003 5004 arg = next; 5005 } 5006 5007 if (term.name[0] == 0 || term.cursor_address[0] == 0) 5008 { 5009 errnum = ERR_BAD_ARGUMENT; 5010 return errnum; 5011 } 5012 5013 ti_set_term (&term); 5014 } 5015 else 5016 { 5017 /* No option specifies printing out current settings. */ 5018 ti_get_term (&term); 5019 5020 grub_printf ("name=%s\n", 5021 ti_escape_string (term.name)); 5022 grub_printf ("cursor_address=%s\n", 5023 ti_escape_string (term.cursor_address)); 5024 grub_printf ("clear_screen=%s\n", 5025 ti_escape_string (term.clear_screen)); 5026 grub_printf ("enter_standout_mode=%s\n", 5027 ti_escape_string (term.enter_standout_mode)); 5028 grub_printf ("exit_standout_mode=%s\n", 5029 ti_escape_string (term.exit_standout_mode)); 5030 } 5031 5032 return 0; 5033 } 5034 5035 static struct builtin builtin_terminfo = 5036 { 5037 "terminfo", 5038 terminfo_func, 5039 BUILTIN_MENU | BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 5040 "terminfo [--name=NAME --cursor-address=SEQ [--clear-screen=SEQ]" 5041 " [--enter-standout-mode=SEQ] [--exit-standout-mode=SEQ]]", 5042 5043 "Define the capabilities of your terminal. Use this command to" 5044 " define escape sequences, if it is not vt100-compatible." 5045 " You may use \\e for ESC and ^X for a control character." 5046 " If no option is specified, the current settings are printed." 5047 }; 5048 #endif /* SUPPORT_SERIAL */ 5049 5050 5051 /* testload */ 5052 static int 5053 testload_func (char *arg, int flags) 5054 { 5055 int i; 5056 5057 kernel_type = KERNEL_TYPE_NONE; 5058 5059 if (! grub_open (arg)) 5060 return 1; 5061 5062 disk_read_hook = disk_read_print_func; 5063 5064 /* Perform filesystem test on the specified file. */ 5065 /* Read whole file first. */ 5066 grub_printf ("Whole file: "); 5067 5068 grub_read ((char *) RAW_ADDR (0x100000), -1); 5069 5070 /* Now compare two sections of the file read differently. */ 5071 5072 for (i = 0; i < 0x10ac0; i++) 5073 { 5074 *((unsigned char *) RAW_ADDR (0x200000 + i)) = 0; 5075 *((unsigned char *) RAW_ADDR (0x300000 + i)) = 1; 5076 } 5077 5078 /* First partial read. */ 5079 grub_printf ("\nPartial read 1: "); 5080 5081 grub_seek (0); 5082 grub_read ((char *) RAW_ADDR (0x200000), 0x7); 5083 grub_read ((char *) RAW_ADDR (0x200007), 0x100); 5084 grub_read ((char *) RAW_ADDR (0x200107), 0x10); 5085 grub_read ((char *) RAW_ADDR (0x200117), 0x999); 5086 grub_read ((char *) RAW_ADDR (0x200ab0), 0x10); 5087 grub_read ((char *) RAW_ADDR (0x200ac0), 0x10000); 5088 5089 /* Second partial read. */ 5090 grub_printf ("\nPartial read 2: "); 5091 5092 grub_seek (0); 5093 grub_read ((char *) RAW_ADDR (0x300000), 0x10000); 5094 grub_read ((char *) RAW_ADDR (0x310000), 0x10); 5095 grub_read ((char *) RAW_ADDR (0x310010), 0x7); 5096 grub_read ((char *) RAW_ADDR (0x310017), 0x10); 5097 grub_read ((char *) RAW_ADDR (0x310027), 0x999); 5098 grub_read ((char *) RAW_ADDR (0x3109c0), 0x100); 5099 5100 grub_printf ("\nHeader1 = 0x%x, next = 0x%x, next = 0x%x, next = 0x%x\n", 5101 *((int *) RAW_ADDR (0x200000)), 5102 *((int *) RAW_ADDR (0x200004)), 5103 *((int *) RAW_ADDR (0x200008)), 5104 *((int *) RAW_ADDR (0x20000c))); 5105 5106 grub_printf ("Header2 = 0x%x, next = 0x%x, next = 0x%x, next = 0x%x\n", 5107 *((int *) RAW_ADDR (0x300000)), 5108 *((int *) RAW_ADDR (0x300004)), 5109 *((int *) RAW_ADDR (0x300008)), 5110 *((int *) RAW_ADDR (0x30000c))); 5111 5112 for (i = 0; i < 0x10ac0; i++) 5113 if (*((unsigned char *) RAW_ADDR (0x200000 + i)) 5114 != *((unsigned char *) RAW_ADDR (0x300000 + i))) 5115 break; 5116 5117 grub_printf ("Max is 0x10ac0: i=0x%x, filepos=0x%x\n", i, filepos); 5118 disk_read_hook = 0; 5119 grub_close (); 5120 return 0; 5121 } 5122 5123 static struct builtin builtin_testload = 5124 { 5125 "testload", 5126 testload_func, 5127 BUILTIN_CMDLINE, 5128 "testload FILE", 5129 "Read the entire contents of FILE in several different ways and" 5130 " compares them, to test the filesystem code. The output is somewhat" 5131 " cryptic, but if no errors are reported and the final `i=X," 5132 " filepos=Y' reading has X and Y equal, then it is definitely" 5133 " consistent, and very likely works correctly subject to a" 5134 " consistent offset error. If this test succeeds, then a good next" 5135 " step is to try loading a kernel." 5136 }; 5137 5138 5139 /* testvbe MODE */ 5140 static int 5141 testvbe_func (char *arg, int flags) 5142 { 5143 int mode_number; 5144 struct vbe_controller controller; 5145 struct vbe_mode mode; 5146 5147 if (! *arg) 5148 { 5149 errnum = ERR_BAD_ARGUMENT; 5150 return 1; 5151 } 5152 5153 if (! safe_parse_maxint (&arg, &mode_number)) 5154 return 1; 5155 5156 /* Preset `VBE2'. */ 5157 grub_memmove (controller.signature, "VBE2", 4); 5158 5159 /* Detect VBE BIOS. */ 5160 if (get_vbe_controller_info (&controller) != 0x004F) 5161 { 5162 grub_printf (" VBE BIOS is not present.\n"); 5163 return 0; 5164 } 5165 5166 if (controller.version < 0x0200) 5167 { 5168 grub_printf (" VBE version %d.%d is not supported.\n", 5169 (int) (controller.version >> 8), 5170 (int) (controller.version & 0xFF)); 5171 return 0; 5172 } 5173 5174 if (get_vbe_mode_info (mode_number, &mode) != 0x004F 5175 || (mode.mode_attributes & 0x0091) != 0x0091) 5176 { 5177 grub_printf (" Mode 0x%x is not supported.\n", mode_number); 5178 return 0; 5179 } 5180 5181 /* Now trip to the graphics mode. */ 5182 if (set_vbe_mode (mode_number | (1 << 14)) != 0x004F) 5183 { 5184 grub_printf (" Switching to Mode 0x%x failed.\n", mode_number); 5185 return 0; 5186 } 5187 5188 /* Draw something on the screen... */ 5189 { 5190 unsigned char *base_buf = (unsigned char *) mode.phys_base; 5191 int scanline = controller.version >= 0x0300 5192 ? mode.linear_bytes_per_scanline : mode.bytes_per_scanline; 5193 /* FIXME: this assumes that any depth is a modulo of 8. */ 5194 int bpp = mode.bits_per_pixel / 8; 5195 int width = mode.x_resolution; 5196 int height = mode.y_resolution; 5197 int x, y; 5198 unsigned color = 0; 5199 5200 /* Iterate drawing on the screen, until the user hits any key. */ 5201 while (checkkey () == -1) 5202 { 5203 for (y = 0; y < height; y++) 5204 { 5205 unsigned char *line_buf = base_buf + scanline * y; 5206 5207 for (x = 0; x < width; x++) 5208 { 5209 unsigned char *buf = line_buf + bpp * x; 5210 int i; 5211 5212 for (i = 0; i < bpp; i++, buf++) 5213 *buf = (color >> (i * 8)) & 0xff; 5214 } 5215 5216 color++; 5217 } 5218 } 5219 5220 /* Discard the input. */ 5221 getkey (); 5222 } 5223 5224 /* Back to the default text mode. */ 5225 if (set_vbe_mode (0x03) != 0x004F) 5226 { 5227 /* Why?! */ 5228 grub_reboot (); 5229 } 5230 5231 return 0; 5232 } 5233 5234 static struct builtin builtin_testvbe = 5235 { 5236 "testvbe", 5237 testvbe_func, 5238 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 5239 "testvbe MODE", 5240 "Test the VBE mode MODE. Hit any key to return." 5241 }; 5242 5243 5244 #ifdef SUPPORT_NETBOOT 5245 /* tftpserver */ 5246 static int 5247 tftpserver_func (char *arg, int flags) 5248 { 5249 if (! *arg || ! ifconfig (0, 0, 0, arg)) 5250 { 5251 errnum = ERR_BAD_ARGUMENT; 5252 return 1; 5253 } 5254 5255 print_network_configuration (); 5256 return 0; 5257 } 5258 5259 static struct builtin builtin_tftpserver = 5260 { 5261 "tftpserver", 5262 tftpserver_func, 5263 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_HELP_LIST, 5264 "tftpserver IPADDR", 5265 "Override the TFTP server address." 5266 }; 5267 #endif /* SUPPORT_NETBOOT */ 5268 5269 5270 /* timeout */ 5271 static int 5272 timeout_func (char *arg, int flags) 5273 { 5274 if (! safe_parse_maxint (&arg, &grub_timeout)) 5275 return 1; 5276 5277 return 0; 5278 } 5279 5280 static struct builtin builtin_timeout = 5281 { 5282 "timeout", 5283 timeout_func, 5284 BUILTIN_MENU, 5285 #if 0 5286 "timeout SEC", 5287 "Set a timeout, in SEC seconds, before automatically booting the" 5288 " default entry (normally the first entry defined)." 5289 #endif 5290 }; 5291 5292 5293 /* title */ 5294 static int 5295 title_func (char *arg, int flags) 5296 { 5297 /* This function is not actually used at least currently. */ 5298 return 0; 5299 } 5300 5301 static struct builtin builtin_title = 5302 { 5303 "title", 5304 title_func, 5305 BUILTIN_TITLE, 5306 #if 0 5307 "title [NAME ...]", 5308 "Start a new boot entry, and set its name to the contents of the" 5309 " rest of the line, starting with the first non-space character." 5310 #endif 5311 }; 5312 5313 5314 /* unhide */ 5315 static int 5316 unhide_func (char *arg, int flags) 5317 { 5318 if (! set_device (arg)) 5319 return 1; 5320 5321 if (! set_partition_hidden_flag (0)) 5322 return 1; 5323 5324 return 0; 5325 } 5326 5327 static struct builtin builtin_unhide = 5328 { 5329 "unhide", 5330 unhide_func, 5331 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_HELP_LIST, 5332 "unhide PARTITION", 5333 "Unhide PARTITION by clearing the \"hidden\" bit in its" 5334 " partition type code." 5335 }; 5336 5337 5338 /* uppermem */ 5339 static int 5340 uppermem_func (char *arg, int flags) 5341 { 5342 if (! safe_parse_maxint (&arg, (int *) &mbi.mem_upper)) 5343 return 1; 5344 5345 mbi.flags &= ~MB_INFO_MEM_MAP; 5346 return 0; 5347 } 5348 5349 static struct builtin builtin_uppermem = 5350 { 5351 "uppermem", 5352 uppermem_func, 5353 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 5354 "uppermem KBYTES", 5355 "Force GRUB to assume that only KBYTES kilobytes of upper memory are" 5356 " installed. Any system address range maps are discarded." 5357 }; 5358 5359 5360 /* vbeprobe */ 5361 static int 5362 vbeprobe_func (char *arg, int flags) 5363 { 5364 struct vbe_controller controller; 5365 unsigned short *mode_list; 5366 int mode_number = -1; 5367 5368 auto unsigned long vbe_far_ptr_to_linear (unsigned long); 5369 5370 unsigned long vbe_far_ptr_to_linear (unsigned long ptr) 5371 { 5372 unsigned short seg = (ptr >> 16); 5373 unsigned short off = (ptr & 0xFFFF); 5374 5375 return (seg << 4) + off; 5376 } 5377 5378 if (*arg) 5379 { 5380 if (! safe_parse_maxint (&arg, &mode_number)) 5381 return 1; 5382 } 5383 5384 /* Set the signature to `VBE2', to obtain VBE 3.0 information. */ 5385 grub_memmove (controller.signature, "VBE2", 4); 5386 5387 if (get_vbe_controller_info (&controller) != 0x004F) 5388 { 5389 grub_printf (" VBE BIOS is not present.\n"); 5390 return 0; 5391 } 5392 5393 /* Check the version. */ 5394 if (controller.version < 0x0200) 5395 { 5396 grub_printf (" VBE version %d.%d is not supported.\n", 5397 (int) (controller.version >> 8), 5398 (int) (controller.version & 0xFF)); 5399 return 0; 5400 } 5401 5402 /* Print some information. */ 5403 grub_printf (" VBE version %d.%d\n", 5404 (int) (controller.version >> 8), 5405 (int) (controller.version & 0xFF)); 5406 5407 /* Iterate probing modes. */ 5408 for (mode_list 5409 = (unsigned short *) vbe_far_ptr_to_linear (controller.video_mode); 5410 *mode_list != 0xFFFF; 5411 mode_list++) 5412 { 5413 struct vbe_mode mode; 5414 5415 if (get_vbe_mode_info (*mode_list, &mode) != 0x004F) 5416 continue; 5417 5418 /* Skip this, if this is not supported or linear frame buffer 5419 mode is not support. */ 5420 if ((mode.mode_attributes & 0x0081) != 0x0081) 5421 continue; 5422 5423 if (mode_number == -1 || mode_number == *mode_list) 5424 { 5425 char *model; 5426 switch (mode.memory_model) 5427 { 5428 case 0x00: model = "Text"; break; 5429 case 0x01: model = "CGA graphics"; break; 5430 case 0x02: model = "Hercules graphics"; break; 5431 case 0x03: model = "Planar"; break; 5432 case 0x04: model = "Packed pixel"; break; 5433 case 0x05: model = "Non-chain 4, 256 color"; break; 5434 case 0x06: model = "Direct Color"; break; 5435 case 0x07: model = "YUV"; break; 5436 default: model = "Unknown"; break; 5437 } 5438 5439 grub_printf (" 0x%x: %s, %ux%ux%u\n", 5440 (unsigned) *mode_list, 5441 model, 5442 (unsigned) mode.x_resolution, 5443 (unsigned) mode.y_resolution, 5444 (unsigned) mode.bits_per_pixel); 5445 5446 if (mode_number != -1) 5447 break; 5448 } 5449 } 5450 5451 if (mode_number != -1 && mode_number != *mode_list) 5452 grub_printf (" Mode 0x%x is not found or supported.\n", mode_number); 5453 5454 return 0; 5455 } 5456 5457 static struct builtin builtin_vbeprobe = 5458 { 5459 "vbeprobe", 5460 vbeprobe_func, 5461 BUILTIN_CMDLINE | BUILTIN_HELP_LIST, 5462 "vbeprobe [MODE]", 5463 "Probe VBE information. If the mode number MODE is specified, show only" 5464 " the information about only the mode." 5465 }; 5466 5467 static int 5468 variable_func(char *arg, int flags) 5469 { 5470 char name[EV_NAMELEN]; 5471 char *val; 5472 int err; 5473 5474 if (*arg == '\0') { 5475 dump_variables(); 5476 return (0); 5477 } 5478 5479 if ((val = grub_strchr(arg, ' ')) != NULL) { 5480 if (val - arg >= sizeof (name)) { 5481 errnum = ERR_WONT_FIT; 5482 return (1); 5483 } 5484 (void) grub_memcpy(name, arg, (val - arg)); 5485 name[val - arg] = '\0'; 5486 val = skip_to(0, arg); 5487 } else { 5488 if (grub_strlen(arg) >= sizeof (name)) { 5489 errnum = ERR_WONT_FIT; 5490 return (1); 5491 } 5492 (void) grub_strcpy(name, arg); 5493 } 5494 5495 if ((err = set_variable(name, val)) != 0) { 5496 errnum = err; 5497 return (1); 5498 } 5499 5500 return (0); 5501 } 5502 5503 static struct builtin builtin_variable = 5504 { 5505 "variable", 5506 variable_func, 5507 BUILTIN_CMDLINE | BUILTIN_MENU | BUILTIN_SCRIPT | BUILTIN_HELP_LIST, 5508 "variable NAME [VALUE]", 5509 "Set the variable NAME to VALUE, or to the empty string if no value is" 5510 " given. NAME must contain no spaces. There is no quoting mechanism" 5511 " and nested variable references are not allowed. Variable values may" 5512 " be substituted into the kernel$ and module$ commands using ${NAME}." 5513 }; 5514 5515 5516 /* The table of builtin commands. Sorted in dictionary order. */ 5517 struct builtin *builtin_table[] = 5518 { 5519 #ifdef SUPPORT_GRAPHICS 5520 &builtin_background, 5521 #endif 5522 &builtin_blocklist, 5523 &builtin_boot, 5524 &builtin_bootfs, 5525 #ifdef SUPPORT_NETBOOT 5526 &builtin_bootp, 5527 #endif /* SUPPORT_NETBOOT */ 5528 &builtin_cat, 5529 &builtin_chainloader, 5530 &builtin_clear, 5531 &builtin_cmp, 5532 &builtin_color, 5533 &builtin_configfile, 5534 &builtin_debug, 5535 &builtin_default, 5536 #ifdef GRUB_UTIL 5537 &builtin_device, 5538 #endif /* GRUB_UTIL */ 5539 #ifdef SUPPORT_NETBOOT 5540 &builtin_dhcp, 5541 #endif /* SUPPORT_NETBOOT */ 5542 &builtin_displayapm, 5543 &builtin_displaymem, 5544 #ifdef GRUB_UTIL 5545 &builtin_dump, 5546 #endif /* GRUB_UTIL */ 5547 &builtin_embed, 5548 &builtin_fallback, 5549 &builtin_find, 5550 &builtin_findroot, 5551 #ifdef SUPPORT_GRAPHICS 5552 &builtin_foreground, 5553 #endif 5554 &builtin_fstest, 5555 &builtin_geometry, 5556 &builtin_halt, 5557 &builtin_help, 5558 &builtin_hiddenmenu, 5559 &builtin_hide, 5560 #ifdef SUPPORT_NETBOOT 5561 &builtin_ifconfig, 5562 #endif /* SUPPORT_NETBOOT */ 5563 &builtin_impsprobe, 5564 &builtin_initrd, 5565 &builtin_install, 5566 &builtin_ioprobe, 5567 &builtin_kernel, 5568 &builtin_kernel_dollar, 5569 &builtin_lock, 5570 &builtin_makeactive, 5571 &builtin_map, 5572 #ifdef USE_MD5_PASSWORDS 5573 &builtin_md5crypt, 5574 #endif /* USE_MD5_PASSWORDS */ 5575 &builtin_min_mem64, 5576 &builtin_module, 5577 &builtin_module_dollar, 5578 &builtin_modulenounzip, 5579 &builtin_pager, 5580 &builtin_partnew, 5581 &builtin_parttype, 5582 &builtin_password, 5583 &builtin_pause, 5584 #if defined(RPC_DEBUG) && defined(SUPPORT_NETBOOT) 5585 &builtin_portmap, 5586 #endif /* RPC_DEBUG && SUPPORT_NETBOOT */ 5587 #ifdef GRUB_UTIL 5588 &builtin_quit, 5589 #endif /* GRUB_UTIL */ 5590 #ifdef SUPPORT_NETBOOT 5591 &builtin_rarp, 5592 #endif /* SUPPORT_NETBOOT */ 5593 &builtin_read, 5594 &builtin_reboot, 5595 &builtin_root, 5596 &builtin_rootnoverify, 5597 &builtin_savedefault, 5598 #ifdef SUPPORT_SERIAL 5599 &builtin_serial, 5600 #endif /* SUPPORT_SERIAL */ 5601 &builtin_setkey, 5602 &builtin_setup, 5603 #ifdef SUPPORT_GRAPHICS 5604 &builtin_splashimage, 5605 #endif /* SUPPORT_GRAPHICS */ 5606 #if defined(SUPPORT_SERIAL) || defined(SUPPORT_HERCULES) || defined(SUPPORT_GRAPHICS) 5607 &builtin_terminal, 5608 #endif /* SUPPORT_SERIAL || SUPPORT_HERCULES || SUPPORT_GRAPHICS */ 5609 #ifdef SUPPORT_SERIAL 5610 &builtin_terminfo, 5611 #endif /* SUPPORT_SERIAL */ 5612 &builtin_testload, 5613 &builtin_testvbe, 5614 #ifdef SUPPORT_NETBOOT 5615 &builtin_tftpserver, 5616 #endif /* SUPPORT_NETBOOT */ 5617 &builtin_timeout, 5618 &builtin_title, 5619 &builtin_unhide, 5620 &builtin_uppermem, 5621 &builtin_variable, 5622 &builtin_vbeprobe, 5623 &builtin_verbose, 5624 0 5625 }; 5626