1 /* builtins.c - the GRUB builtin commands */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
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.
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.
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.
21 /* Include stdio.h before shared.h, because we can't define
22 WITHOUT_LIBC_STUBS here. */
31 #ifdef SUPPORT_NETBOOT
37 # include <terminfo.h>
42 #else /* ! GRUB_UTIL */
44 # include <smp-imps.h>
45 #endif /* ! GRUB_UTIL */
47 #ifdef USE_MD5_PASSWORDS
54 /* The type of kernel loaded. */
56 /* The boot device. */
58 /* True when the debug mode is turned on, and false
59 when it is turned off. */
61 /* The default entry. */
62 int default_entry
= 0;
63 /* The fallback entry. */
65 int fallback_entries
[MAX_FALLBACK_ENTRIES
];
66 /* The number of current entry. */
68 /* The address for Multiboot command-line buffer. */
69 static char *mb_cmdline
;
72 /* The password type. */
73 password_t password_type
;
74 /* The flag for indicating that the user is authoritative. */
77 int grub_timeout
= -1;
78 /* Whether to show the menu or not. */
80 /* The BIOS drive map. */
81 static unsigned short bios_drive_map
[DRIVE_MAP_SIZE
+ 1];
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);
90 unsigned int min_mem64
= 0;
92 #if defined(__sun) && !defined(GRUB_UTIL)
93 extern void __enable_execute_stack (void *);
95 __enable_execute_stack (void *addr
)
98 #endif /* __sun && !GRUB_UTIL */
100 /* Initialize the data for builtins. */
104 kernel_type
= KERNEL_TYPE_NONE
;
105 /* BSD and chainloading evil hacks! */
106 bootdev
= set_bootdev (0);
107 mb_cmdline
= (char *) MB_CMDLINE_BUF
;
110 /* Initialize the data for the configuration file. */
116 fallback_entryno
= -1;
117 fallback_entries
[0] = -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';
127 /* Check a password for correctness. Returns 0 if password was
128 correct, and a value != 0 for error, similarly to strcmp. */
130 check_password (char *entered
, char* expected
, password_t type
)
135 return strcmp (entered
, expected
);
137 #ifdef USE_MD5_PASSWORDS
139 return check_md5_password (entered
, expected
);
142 /* unsupported password type: be secure */
147 /* Print which sector is read when loading a file. */
149 disk_read_print_func(unsigned long long sector
, int offset
, int length
)
151 grub_printf ("[%llu,%d,%d]", sector
, offset
, length
);
157 blocklist_func (char *arg
, int flags
)
159 char *dummy
= (char *) RAW_ADDR (0x100000);
160 unsigned long long start_sector
= 0;
165 auto void disk_read_blocklist_func (unsigned long long sector
, int offset
,
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 long long sector
, int offset
,
175 if (start_sector
+ num_sectors
== sector
176 && offset
== 0 && last_length
== SECTOR_SIZE
)
179 last_length
= length
;
184 if (last_length
== SECTOR_SIZE
)
185 grub_printf ("%s%llu+%d", num_entries
? "," : "",
186 start_sector
- part_start
, num_sectors
);
187 else if (num_sectors
> 1)
188 grub_printf ("%s%llu+%d,%lld[0-%d]", num_entries
? "," : "",
189 start_sector
- part_start
, num_sectors
-1,
190 start_sector
+ num_sectors
-1 - part_start
,
193 grub_printf ("%s%llu[0-%d]", num_entries
? "," : "",
194 start_sector
- part_start
, last_length
);
202 grub_printf("%s%llu[%d-%d]", num_entries
? "," : "",
203 sector
-part_start
, offset
, offset
+length
);
208 start_sector
= sector
;
210 last_length
= length
;
215 if (! grub_open (arg
))
218 /* Print the device name. */
219 grub_printf ("(%cd%d",
220 (current_drive
& 0x80) ? 'h' : 'f',
221 current_drive
& ~0x80);
223 if ((current_partition
& 0xFF0000) != 0xFF0000)
224 grub_printf (",%d", (current_partition
>> 16) & 0xFF);
226 if ((current_partition
& 0x00FF00) != 0x00FF00)
227 grub_printf (",%c", 'a' + ((current_partition
>> 8) & 0xFF));
231 /* Read in the whole file to DUMMY. */
232 disk_read_hook
= disk_read_blocklist_func
;
233 if (! grub_read (dummy
, -1))
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. */
239 grub_printf ("%s%llu+%d", num_entries
? "," : "",
240 start_sector
- part_start
, num_sectors
);
250 static struct builtin builtin_blocklist
=
254 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
256 "Print the blocklist notation of the file FILE."
261 boot_func (char *arg
, int flags
)
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 ();
269 #ifdef SUPPORT_NETBOOT
270 /* Shut down the networking. */
276 case KERNEL_TYPE_FREEBSD
:
277 case KERNEL_TYPE_NETBSD
:
279 bsd_boot (kernel_type
, bootdev
, (char *) mbi
.cmdline
);
282 case KERNEL_TYPE_LINUX
:
287 case KERNEL_TYPE_BIG_LINUX
:
292 case KERNEL_TYPE_CHAINLOADER
:
295 /* Check if we should set the int13 handler. */
296 if (bios_drive_map
[0] != 0)
300 /* Search for SAVED_DRIVE. */
301 for (i
= 0; i
< DRIVE_MAP_SIZE
; i
++)
303 if (! bios_drive_map
[i
])
305 else if ((bios_drive_map
[i
] & 0xFF) == saved_drive
)
307 /* Exchage SAVED_DRIVE with the mapped drive. */
308 saved_drive
= (bios_drive_map
[i
] >> 8) & 0xFF;
313 /* Set the handler. This is somewhat dangerous. */
314 set_int13_handler (bios_drive_map
);
318 boot_drive
= saved_drive
;
319 chain_stage1 (0, BOOTSEC_LOCATION
, boot_part_addr
);
322 case KERNEL_TYPE_MULTIBOOT
:
324 #ifdef SUPPORT_NETBOOT
325 #ifdef SOLARIS_NETBOOT
326 if (current_drive
== NETWORK_DRIVE
) {
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.
332 mbi
.flags
&= ~MB_INFO_DRIVE_INFO
;
333 mbi
.drives_length
= dhcpack_length
;
334 mbi
.drives_addr
= dhcpack_buf
;
336 #endif /* SOLARIS_NETBOOT */
338 multi_boot ((int) entry_addr
, (int) &mbi
);
342 errnum
= ERR_BOOT_COMMAND
;
349 static struct builtin builtin_boot
=
353 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
355 "Boot the OS/chain-loader which has been loaded."
359 #ifdef SUPPORT_NETBOOT
362 bootp_func (char *arg
, int flags
)
364 int with_configfile
= 0;
366 if (grub_memcmp (arg
, "--with-configfile", sizeof ("--with-configfile") - 1)
370 arg
= skip_to (0, arg
);
375 if (errnum
== ERR_NONE
)
376 errnum
= ERR_DEV_VALUES
;
381 /* Notify the configuration. */
382 print_network_configuration ();
384 /* XXX: this can cause an endless loop, but there is no easy way to
385 detect such a loop unfortunately. */
387 configfile_func (config_file
, flags
);
392 static struct builtin builtin_bootp
=
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"
402 #endif /* SUPPORT_NETBOOT */
407 cat_func (char *arg
, int flags
)
411 if (! grub_open (arg
))
414 while (grub_read (&c
, 1))
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
<= '~'))
428 static struct builtin builtin_cat
=
432 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
434 "Print the contents of the file FILE."
440 chainloader_func (char *arg
, int flags
)
445 /* If the option `--force' is specified? */
446 if (substring ("--force", arg
) <= 0)
449 file
= skip_to (0, arg
);
453 if (! grub_open (file
))
455 kernel_type
= KERNEL_TYPE_NONE
;
459 /* Read the first block. */
460 if (grub_read ((char *) BOOTSEC_LOCATION
, SECTOR_SIZE
) != SECTOR_SIZE
)
463 kernel_type
= KERNEL_TYPE_NONE
;
465 /* This below happens, if a file whose size is less than 512 bytes
467 if (errnum
== ERR_NONE
)
468 errnum
= ERR_EXEC_FORMAT
;
473 /* If not loading it forcibly, check for the signature. */
475 && (*((unsigned short *) (BOOTSEC_LOCATION
+ BOOTSEC_SIG_OFFSET
))
476 != BOOTSEC_SIGNATURE
))
479 errnum
= ERR_EXEC_FORMAT
;
480 kernel_type
= KERNEL_TYPE_NONE
;
485 kernel_type
= KERNEL_TYPE_CHAINLOADER
;
487 /* XXX: Windows evil hack. For now, only the first five letters are
489 if (IS_PC_SLICE_TYPE_FAT (current_slice
)
490 && ! grub_memcmp ((char *) BOOTSEC_LOCATION
+ BOOTSEC_BPB_SYSTEM_ID
,
492 *((unsigned long *) (BOOTSEC_LOCATION
+ BOOTSEC_BPB_HIDDEN_SECTORS
))
500 static struct builtin builtin_chainloader
=
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."
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 */
517 cmp_func (char *arg
, int flags
)
524 /* The size of the file. */
527 /* Get the filenames from ARG. */
529 file2
= skip_to (0, arg
);
530 if (! *file1
|| ! *file2
)
532 errnum
= ERR_BAD_ARGUMENT
;
536 /* Terminate the filenames for convenience. */
537 nul_terminate (file1
);
538 nul_terminate (file2
);
540 /* Read the whole data from FILE1. */
541 addr1
= (char *) RAW_ADDR (0x100000);
542 if (! grub_open (file1
))
547 if (grub_read (addr1
, -1) != size
)
555 /* Read the whole data from FILE2. */
556 addr2
= addr1
+ size
;
557 if (! grub_open (file2
))
560 /* Check if the size of FILE2 is equal to the one of FILE2. */
563 grub_printf ("Differ in size: 0x%x [%s], 0x%x [%s]\n",
564 size
, file1
, filemax
, file2
);
569 if (! grub_read (addr2
, -1))
577 /* Now compare ADDR1 with ADDR2. */
578 for (i
= 0; i
< size
; i
++)
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
);
589 static struct builtin builtin_cmp
=
595 "Compare the file FILE1 with the FILE2 and inform the different values"
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". */
605 color_func (char *arg
, int flags
)
609 int new_normal_color
;
610 int new_highlight_color
;
611 static char *color_list
[16] =
631 auto int color_number (char *str
);
633 /* Convert the color name STR into the magical number. */
634 auto int color_number (char *str
)
640 /* Find the separator. */
641 for (ptr
= str
; *ptr
&& *ptr
!= '/'; ptr
++)
644 /* If not found, return -1. */
648 /* Terminate the string STR. */
651 /* If STR contains the prefix "blink-", then set the `blink' bit
653 if (substring ("blink-", str
) <= 0)
659 /* Search for the color name. */
660 for (i
= 0; i
< 16; i
++)
661 if (grub_strcmp (color_list
[i
], str
) == 0)
673 /* Search for the color name. */
674 for (i
= 0; i
< 8; i
++)
675 if (grub_strcmp (color_list
[i
], str
) == 0)
688 highlight
= skip_to (0, arg
);
690 new_normal_color
= color_number (normal
);
691 if (new_normal_color
< 0 && ! safe_parse_maxint (&normal
, &new_normal_color
))
694 /* The second argument is optional, so set highlight_color
695 to inverted NORMAL_COLOR. */
697 new_highlight_color
= ((new_normal_color
>> 4)
698 | ((new_normal_color
& 0xf) << 4));
701 new_highlight_color
= color_number (highlight
);
702 if (new_highlight_color
< 0
703 && ! safe_parse_maxint (&highlight
, &new_highlight_color
))
707 if (current_term
->setcolor
)
708 current_term
->setcolor (new_normal_color
, new_highlight_color
);
713 static struct builtin builtin_color
=
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."
734 configfile_func (char *arg
, int flags
)
736 char *new_config
= config_file
;
738 /* Check if the file ARG is present. */
739 if (! grub_open (arg
))
744 /* Copy ARG to CONFIG_FILE. */
745 while ((*new_config
++ = *arg
++) != 0)
749 /* Force to load the configuration file. */
753 /* Make sure that the user will not be authoritative. */
757 grub_longjmp (restart_env
, 0);
759 /* Never reach here. */
763 static struct builtin builtin_configfile
=
767 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
769 "Load FILE as the configuration file."
775 debug_func (char *arg
, int flags
)
780 grub_printf (" Debug mode is turned off\n");
785 grub_printf (" Debug mode is turned on\n");
791 static struct builtin builtin_debug
=
797 "Turn on/off the debug mode."
803 default_func (char *arg
, int flags
)
805 #ifndef SUPPORT_DISKLESS
806 if (grub_strcmp (arg
, "saved") == 0)
808 default_entry
= saved_entryno
;
811 #endif /* SUPPORT_DISKLESS */
813 if (! safe_parse_maxint (&arg
, &default_entry
))
819 static struct builtin builtin_default
=
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."
835 device_func (char *arg
, int flags
)
840 /* Get the drive number from DRIVE. */
841 if (! set_device (drive
))
844 /* Get the device argument. */
845 device
= skip_to (0, drive
);
847 /* Terminate DEVICE. */
848 nul_terminate (device
);
850 if (! *device
|| ! check_device (device
))
852 errnum
= ERR_FILE_NOT_FOUND
;
856 assign_device_name (current_drive
, device
);
861 static struct builtin builtin_device
=
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."
870 #endif /* GRUB_UTIL */
872 #ifdef SUPPORT_NETBOOT
873 /* Debug Function for RPC */
877 portmap_func (char *arg
, int flags
)
880 if (! grub_eth_probe ()){
881 grub_printf ("No ethernet card found.\n");
882 errnum
= ERR_DEV_VALUES
;
885 if ((prog
= getdec(&arg
)) == -1){
886 grub_printf("Error prog number\n");
889 arg
= skip_to (0, arg
);
890 if ((ver
= getdec(&arg
)) == -1){
891 grub_printf("Error ver number\n");
894 port
= __pmapudp_getport(ARP_SERVER
, prog
, ver
);
895 printf("portmap getport %d", port
);
899 static struct builtin builtin_portmap
=
903 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
904 "portmap prog_number vers_number",
905 "Do portmap with the prog_number and vers_number"
907 #endif /* RPC_DEBUG */
911 dhcp_func (char *arg
, int flags
)
913 int with_configfile
= 0;
915 if (grub_memcmp (arg
, "--with-configfile", sizeof ("--with-configfile") - 1)
919 arg
= skip_to (0, arg
);
924 if (errnum
== ERR_NONE
)
925 errnum
= ERR_DEV_VALUES
;
930 /* Notify the configuration. */
931 print_network_configuration ();
933 /* XXX: this can cause an endless loop, but there is no easy way to
934 detect such a loop unfortunately. */
936 configfile_func (config_file
, flags
);
938 solaris_config_file();
944 test_config_file(char *menufile
)
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
954 if (grub_open(menufile
)) {
955 grub_strcpy(config_file
, menufile
);
964 static void solaris_config_file (void)
966 static char menufile
[64];
967 static char hexdigit
[] = "0123456789ABCDEF";
972 * if DHCP option 150 has been provided, config_file will
973 * already contain the string, try it.
975 if (configfile_origin
== CFG_150
) {
976 if (test_config_file(config_file
))
981 * try to find host (MAC address) specific configfile:
982 * menu.lst.01<ether_addr>
984 grub_strcpy(c
, "menu.lst.01");
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];
992 configfile_origin
= CFG_MAC
;
993 if (test_config_file(menufile
))
997 * try to find a configfile derived from the DHCP/bootp
998 * BootFile string: menu.lst.<BootFile>
1000 if (bootfile
!= NULL
&& bootfile
[0] != 0) {
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
);
1008 grub_strcpy(c
, bootfile
);
1009 configfile_origin
= CFG_BOOTFILE
;
1010 if (test_config_file(menufile
))
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.
1019 char *cp
= config_file
;
1020 /* skip leading slashes for tftp */
1023 grub_memmove (config_file
, cp
, strlen(cp
) + 1);
1024 configfile_origin
= CFG_HARDCODED
;
1027 static struct builtin builtin_dhcp
=
1031 BUILTIN_CMDLINE
| BUILTIN_MENU
| BUILTIN_HELP_LIST
,
1033 "Initialize a network device via DHCP."
1035 #endif /* SUPPORT_NETBOOT */
1037 static int terminal_func (char *arg
, int flags
);
1039 static int verbose_func(char *arg
, int flags
) {
1041 if (grub_strcmp(arg
, "off") == 0) {
1042 silent
.status
= DEFER_SILENT
;
1045 if (flags
== BUILTIN_CMDLINE
) {
1046 silent
.status
= DEFER_VERBOSE
;
1050 silent
.status
= VERBOSE
;
1052 /* get back to text console */
1053 if (current_term
->shutdown
) {
1054 (*current_term
->shutdown
)();
1055 current_term
= term_table
; /* assumption: console is first */
1058 /* dump the buffer */
1059 if (!silent
.looped
) {
1060 /* if the buffer hasn't looped, just print it */
1061 printf("%s", silent
.buffer
);
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.
1068 printf("%s", silent
.buffer_start
+ 1);
1069 printf("%s", silent
.buffer
);
1075 static struct builtin builtin_verbose
=
1079 BUILTIN_CMDLINE
| BUILTIN_MENU
| BUILTIN_SCRIPT
| BUILTIN_HELP_LIST
,
1081 "Verbose output during menu entry (script) execution."
1084 #ifdef SUPPORT_GRAPHICS
1086 static int splashimage_func(char *arg
, int flags
) {
1087 char splashimage
[64];
1090 /* filename can only be 64 characters due to our buffer size */
1091 if (strlen(arg
) > 63)
1094 if (flags
== BUILTIN_SCRIPT
)
1095 flags
= BUILTIN_CMDLINE
;
1097 if (flags
== BUILTIN_CMDLINE
) {
1098 if (!grub_open(arg
))
1103 strcpy(splashimage
, arg
);
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
;
1113 graphics_set_splash(splashimage
);
1115 if (flags
== BUILTIN_CMDLINE
&& graphics_inited
) {
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...
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.
1131 terminal_func("graphics", flags
);
1138 static struct builtin builtin_splashimage
=
1142 BUILTIN_CMDLINE
| BUILTIN_MENU
| BUILTIN_SCRIPT
| BUILTIN_HELP_LIST
,
1144 "Load FILE as the background image when in graphics mode."
1150 foreground_func(char *arg
, int flags
)
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;
1157 foreground
= (r
<< 16) | (g
<< 8) | b
;
1158 if (graphics_inited
)
1159 graphics_set_palette(15, r
, g
, b
);
1167 static struct builtin builtin_foreground
=
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."
1180 background_func(char *arg
, int flags
)
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;
1187 background
= (r
<< 16) | (g
<< 8) | b
;
1188 if (graphics_inited
)
1189 graphics_set_palette(0, r
, g
, b
);
1196 static struct builtin builtin_background
=
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."
1206 #endif /* SUPPORT_GRAPHICS */
1213 if (current_term
->cls
)
1214 current_term
->cls();
1219 static struct builtin builtin_clear
=
1223 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
1230 displayapm_func (char *arg
, int flags
)
1232 if (mbi
.flags
& MB_INFO_APM_TABLE
)
1234 grub_printf ("APM BIOS information:\n"
1236 " 32-bit CS: 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
);
1254 grub_printf ("No APM BIOS found or probe failed\n");
1260 static struct builtin builtin_displayapm
=
1264 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
1266 "Display APM BIOS information."
1272 displaymem_func (char *arg
, int flags
)
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");
1280 grub_printf (" Lower memory: %uK, "
1281 "Upper memory (to first chipset hole): %uK\n",
1282 mbi
.mem_lower
, mbi
.mem_upper
);
1285 grub_printf (" Memory limit for 64-bit ISADIR expansion: %uMB\n",
1288 if (mbi
.flags
& MB_INFO_MEM_MAP
)
1290 struct AddrRangeDesc
*map
= (struct AddrRangeDesc
*) mbi
.mmap_addr
;
1291 int end_addr
= mbi
.mmap_addr
+ mbi
.mmap_length
;
1293 grub_printf (" [Address Range Descriptor entries "
1294 "immediately follow (values are 64-bit)]\n");
1295 while (end_addr
> (int) map
)
1299 if (map
->Type
== MB_ARD_MEMORY
)
1303 grub_printf (" %s: Base Address: 0x%x X 4GB + 0x%x,\n"
1304 " Length: 0x%x X 4GB + 0x%x bytes\n",
1306 (unsigned long) (map
->BaseAddr
>> 32),
1307 (unsigned long) (map
->BaseAddr
& 0xFFFFFFFF),
1308 (unsigned long) (map
->Length
>> 32),
1309 (unsigned long) (map
->Length
& 0xFFFFFFFF));
1311 map
= ((struct AddrRangeDesc
*) (((int) map
) + 4 + map
->size
));
1318 static struct builtin builtin_displaymem
=
1322 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
1324 "Display what GRUB thinks the system address space map of the"
1325 " machine is, including all regions of physical RAM installed."
1332 dump_func (char *arg
, int flags
)
1339 to
= skip_to (0, arg
);
1340 if (! *from
|| ! *to
)
1342 errnum
= ERR_BAD_ARGUMENT
;
1346 nul_terminate (from
);
1349 if (! grub_open (from
))
1352 fp
= fopen (to
, "w");
1359 while (grub_read (&c
, 1))
1360 if (fputc (c
, fp
) == EOF
)
1367 if (fclose (fp
) == EOF
)
1377 static struct builtin builtin_dump
=
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."
1386 #endif /* GRUB_UTIL */
1389 static char embed_info
[32];
1391 /* Embed a Stage 1.5 in the first cylinder after MBR or in the
1392 bootloader block in a FFS. */
1394 embed_func (char *arg
, int flags
)
1398 char *stage1_5_buffer
= (char *) RAW_ADDR (0x100000);
1400 unsigned long long sector
;
1403 device
= skip_to (0, stage1_5
);
1405 /* Open a Stage 1.5. */
1406 if (! grub_open (stage1_5
))
1409 /* Read the whole of the Stage 1.5. */
1410 len
= grub_read (stage1_5_buffer
, -1);
1416 size
= (len
+ SECTOR_SIZE
- 1) / SECTOR_SIZE
;
1418 /* Get the device where the Stage 1.5 will be embedded. */
1419 set_device (device
);
1423 if (current_partition
== 0xFFFFFF)
1425 /* Embed it after the MBR. */
1427 char mbr
[SECTOR_SIZE
];
1428 char ezbios_check
[2*SECTOR_SIZE
];
1431 /* Open the partition. */
1432 if (! open_partition ())
1435 /* No floppy has MBR. */
1436 if (! (current_drive
& 0x80))
1438 errnum
= ERR_DEV_VALUES
;
1442 /* Read the MBR of CURRENT_DRIVE. */
1443 if (! rawread (current_drive
, PC_MBR_SECTOR
, 0, SECTOR_SIZE
, mbr
))
1447 if (! PC_MBR_CHECK_SIG (mbr
))
1449 errnum
= ERR_BAD_PART_TABLE
;
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
)
1457 errnum
= ERR_NO_DISK_SPACE
;
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.
1465 if (! rawread (current_drive
, 1, 0, 2 * SECTOR_SIZE
, ezbios_check
))
1468 if (! memcmp (ezbios_check
+ 3, "AERMH", 5)
1469 || ! memcmp (ezbios_check
+ 512 + 3, "AERMH", 5))
1471 /* The space after the MBR is used by EZ-BIOS which we must
1474 errnum
= ERR_NO_DISK_SPACE
;
1482 /* Embed it in the bootloader block in the filesystem. */
1483 unsigned long long start_sector
;
1485 /* Open the partition. */
1486 if (! open_device ())
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
))
1493 errnum
= ERR_DEV_VALUES
;
1497 sector
= part_start
+ start_sector
;
1500 /* Clear the cache. */
1501 buf_track
= BUF_CACHE_INVALID
;
1503 /* Now perform the embedding. */
1504 if (! devwrite (sector
- part_start
, size
, stage1_5_buffer
))
1507 grub_printf (" %d sectors are embedded.\n", size
);
1508 grub_sprintf (embed_info
, "%llu+%d", sector
- part_start
, size
);
1512 static struct builtin builtin_embed
=
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."
1526 fallback_func (char *arg
, int flags
)
1535 if (! safe_parse_maxint (&arg
, &entry
))
1538 /* Remove duplications to prevent infinite looping. */
1539 for (j
= 0; j
< i
; j
++)
1540 if (entry
== fallback_entries
[j
])
1545 fallback_entries
[i
++] = entry
;
1546 if (i
== MAX_FALLBACK_ENTRIES
)
1549 arg
= skip_to (0, arg
);
1552 if (i
< MAX_FALLBACK_ENTRIES
)
1553 fallback_entries
[i
] = -1;
1555 fallback_entryno
= (i
== 0) ? -1 : 0;
1560 static struct builtin builtin_fallback
=
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."
1578 set_root (char *root
, unsigned long drive
, unsigned long part
)
1580 int bsd_part
= (part
>> 8) & 0xFF;
1581 int pc_slice
= part
>> 16;
1583 if (bsd_part
== 0xFF) {
1584 grub_sprintf (root
, "(hd%d,%d)\n", drive
- 0x80, pc_slice
);
1586 grub_sprintf (root
, "(hd%d,%d,%c)\n",
1587 drive
- 0x80, pc_slice
, bsd_part
+ 'a');
1592 find_common (char *arg
, char *root
, int for_root
, int flags
)
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
;
1602 static char bootsign
[BOOTSIGN_LEN
];
1605 * If argument has partition information (findroot command only), then
1606 * it can't be a floppy
1608 if (for_root
&& arg
[0] == '(') {
1609 tmp_argpart
= grub_strchr(arg
+ 1, ',');
1610 if (tmp_argpart
== NULL
)
1612 grub_strcpy(argpart
, tmp_argpart
);
1613 *tmp_argpart
= '\0';
1615 grub_sprintf(bootsign
, "%s/%s", BOOTSIGN_DIR
, arg
);
1616 filename
= bootsign
;
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
;
1623 /* plain vanilla find cmd */
1628 for (drive
= 0; drive
< 8; drive
++)
1630 current_drive
= drive
;
1631 current_partition
= 0xFFFFFF;
1635 saved_drive
= current_drive
;
1636 saved_partition
= current_partition
;
1637 if (grub_open (filename
))
1642 grub_sprintf(root
, "(fd%d)", drive
);
1645 grub_printf (" (fd%d)\n", drive
);
1654 for (drive
= 0x80; drive
< 0x88; drive
++)
1656 unsigned long part
= 0xFFFFFF;
1657 unsigned long long start
, len
, offset
, ext_offset
, gpt_offset
;
1658 int type
, entry
, gpt_count
, gpt_size
;
1659 char buf
[SECTOR_SIZE
];
1661 if (for_root
&& tmp_argpart
) {
1662 grub_sprintf(device
, "(hd%d%s", drive
- 0x80, argpart
);
1665 part
= current_partition
;
1666 if (open_device ()) {
1667 saved_drive
= current_drive
;
1668 saved_partition
= current_partition
;
1670 if (grub_open (filename
)) {
1673 if (is_zfs_mount
== 0) {
1674 set_root(root
, current_drive
, current_partition
);
1677 best_drive
= current_drive
;
1678 best_part
= current_partition
;
1685 current_drive
= drive
;
1686 while (next_partition (drive
, 0xFFFFFF, &part
, &type
,
1687 &start
, &len
, &offset
, &entry
,
1688 &ext_offset
, &gpt_offset
,
1689 &gpt_count
, &gpt_size
, buf
))
1691 if (type
!= PC_SLICE_TYPE_NONE
1692 && ! IS_PC_SLICE_TYPE_BSD (type
)
1693 && ! IS_PC_SLICE_TYPE_EXTENDED (type
))
1695 current_partition
= part
;
1698 saved_drive
= current_drive
;
1699 saved_partition
= current_partition
;
1700 if (grub_open (filename
))
1706 set_root(tmproot
, drive
, part
);
1708 grub_memcpy(root
, tmproot
, sizeof(tmproot
));
1709 if (is_zfs_mount
== 0) {
1712 best_drive
= current_drive
;
1713 best_part
= current_partition
;
1716 grub_printf("%s", tmproot
);
1722 /* We want to ignore any error here. */
1726 /* next_partition always sets ERRNUM in the last call, so clear
1732 if (is_zfs_mount
&& for_root
) {
1733 set_root(root
, best_drive
, best_part
);
1736 saved_drive
= tmp_drive
;
1737 saved_partition
= tmp_partition
;
1748 errnum
= ERR_FILE_NOT_FOUND
;
1753 /* Search for the filename ARG in all of partitions. */
1755 find_func (char *arg
, int flags
)
1757 return (find_common(arg
, NULL
, 0, flags
));
1760 static struct builtin builtin_find
=
1764 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
1766 "Search for the filename FILENAME in all of partitions and print the list of"
1767 " the devices which contain the file."
1773 fstest_func (char *arg
, int flags
)
1777 disk_read_hook
= NULL
;
1778 printf (" Filesystem tracing is now off\n");
1782 disk_read_hook
= disk_read_print_func
;
1783 printf (" Filesystem tracing is now on\n");
1789 static struct builtin builtin_fstest
=
1795 "Toggle filesystem test mode."
1801 geometry_func (char *arg
, int flags
)
1803 struct geometry geom
;
1810 /* Get the device number. */
1811 set_device (device
);
1815 /* Check for the geometry. */
1816 if (get_diskinfo (current_drive
, &geom
))
1818 errnum
= ERR_NO_DISK
;
1822 /* Attempt to read the first sector, because some BIOSes turns out not
1823 to support LBA even though they set the bit 0 in the support
1824 bitmap, only after reading something actually. */
1825 if (biosdisk (BIOSDISK_READ
, current_drive
, &geom
, 0, 1, SCRATCHSEG
))
1832 ptr
= skip_to (0, device
);
1835 char *cylinder
, *head
, *sector
, *total_sector
;
1836 int num_cylinder
, num_head
, num_sector
, num_total_sector
;
1839 head
= skip_to (0, cylinder
);
1840 sector
= skip_to (0, head
);
1841 total_sector
= skip_to (0, sector
);
1842 if (! safe_parse_maxint (&cylinder
, &num_cylinder
)
1843 || ! safe_parse_maxint (&head
, &num_head
)
1844 || ! safe_parse_maxint (§or
, &num_sector
))
1847 disks
[current_drive
].cylinders
= num_cylinder
;
1848 disks
[current_drive
].heads
= num_head
;
1849 disks
[current_drive
].sectors
= num_sector
;
1851 if (safe_parse_maxint (&total_sector
, &num_total_sector
))
1852 disks
[current_drive
].total_sectors
= num_total_sector
;
1854 disks
[current_drive
].total_sectors
1855 = num_cylinder
* num_head
* num_sector
;
1858 geom
= disks
[current_drive
];
1861 #endif /* GRUB_UTIL */
1864 msg
= device_map
[current_drive
];
1866 if (geom
.flags
& BIOSDISK_FLAG_LBA_EXTENSION
)
1872 grub_printf ("drive 0x%x: C/H/S = %d/%d/%d, "
1873 "The number of sectors = %llu, %s\n",
1875 geom
.cylinders
, geom
.heads
, geom
.sectors
,
1876 geom
.total_sectors
, msg
);
1877 real_open_partition (1);
1882 static struct builtin builtin_geometry
=
1886 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
1887 "geometry DRIVE [CYLINDER HEAD SECTOR [TOTAL_SECTOR]]",
1888 "Print the information for a drive DRIVE. In the grub shell, you can"
1889 " set the geometry of the drive arbitrarily. The number of the cylinders,"
1890 " the one of the heads, the one of the sectors and the one of the total"
1891 " sectors are set to CYLINDER, HEAD, SECTOR and TOTAL_SECTOR,"
1892 " respectively. If you omit TOTAL_SECTOR, then it will be calculated based"
1893 " on the C/H/S values automatically."
1899 halt_func (char *arg
, int flags
)
1903 no_apm
= (grub_memcmp (arg
, "--no-apm", 8) == 0);
1906 /* Never reach here. */
1910 static struct builtin builtin_halt
=
1914 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
1916 "Halt your system. If APM is avaiable on it, turn off the power using"
1917 " the APM BIOS, unless you specify the option `--no-apm'."
1922 #define MAX_SHORT_DOC_LEN 39
1923 #define MAX_LONG_DOC_LEN 66
1926 help_func (char *arg
, int flags
)
1930 if (grub_memcmp (arg
, "--all", sizeof ("--all") - 1) == 0)
1933 arg
= skip_to (0, arg
);
1938 /* Invoked with no argument. Print the list of the short docs. */
1939 struct builtin
**builtin
;
1942 for (builtin
= builtin_table
; *builtin
!= 0; builtin
++)
1947 /* If this cannot be used in the command-line interface,
1949 if (! ((*builtin
)->flags
& BUILTIN_CMDLINE
))
1952 /* If this doesn't need to be listed automatically and "--all"
1953 is not specified, skip this. */
1954 if (! all
&& ! ((*builtin
)->flags
& BUILTIN_HELP_LIST
))
1957 len
= grub_strlen ((*builtin
)->short_doc
);
1958 /* If the length of SHORT_DOC is too long, truncate it. */
1959 if (len
> MAX_SHORT_DOC_LEN
- 1)
1960 len
= MAX_SHORT_DOC_LEN
- 1;
1962 for (i
= 0; i
< len
; i
++)
1963 grub_putchar ((*builtin
)->short_doc
[i
]);
1965 for (; i
< MAX_SHORT_DOC_LEN
; i
++)
1969 grub_putchar ('\n');
1974 /* If the last entry was at the left column, no newline was printed
1977 grub_putchar ('\n');
1981 /* Invoked with one or more patterns. */
1984 struct builtin
**builtin
;
1987 /* Get the next argument. */
1988 next_arg
= skip_to (0, arg
);
1990 /* Terminate ARG. */
1991 nul_terminate (arg
);
1993 for (builtin
= builtin_table
; *builtin
; builtin
++)
1995 /* Skip this if this is only for the configuration file. */
1996 if (! ((*builtin
)->flags
& BUILTIN_CMDLINE
))
1999 if (substring (arg
, (*builtin
)->name
) < 1)
2001 char *doc
= (*builtin
)->long_doc
;
2003 /* At first, print the name and the short doc. */
2004 grub_printf ("%s: %s\n",
2005 (*builtin
)->name
, (*builtin
)->short_doc
);
2007 /* Print the long doc. */
2010 int len
= grub_strlen (doc
);
2013 /* If LEN is too long, fold DOC. */
2014 if (len
> MAX_LONG_DOC_LEN
)
2016 /* Fold this line at the position of a space. */
2017 for (len
= MAX_LONG_DOC_LEN
; len
> 0; len
--)
2018 if (doc
[len
- 1] == ' ')
2023 for (i
= 0; i
< len
; i
++)
2024 grub_putchar (*doc
++);
2025 grub_putchar ('\n');
2038 static struct builtin builtin_help
=
2042 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
2043 "help [--all] [PATTERN ...]",
2044 "Display helpful information about builtin commands. Not all commands"
2045 " aren't shown without the option `--all'."
2051 hiddenmenu_func (char *arg
, int flags
)
2057 static struct builtin builtin_hiddenmenu
=
2071 hide_func (char *arg
, int flags
)
2073 if (! set_device (arg
))
2076 if (! set_partition_hidden_flag (1))
2082 static struct builtin builtin_hide
=
2086 BUILTIN_CMDLINE
| BUILTIN_MENU
| BUILTIN_HELP_LIST
,
2088 "Hide PARTITION by setting the \"hidden\" bit in"
2089 " its partition type code."
2093 #ifdef SUPPORT_NETBOOT
2096 ifconfig_func (char *arg
, int flags
)
2098 char *svr
= 0, *ip
= 0, *gw
= 0, *sm
= 0;
2100 if (! grub_eth_probe ())
2102 grub_printf ("No ethernet card found.\n");
2103 errnum
= ERR_DEV_VALUES
;
2109 if (! grub_memcmp ("--server=", arg
, sizeof ("--server=") - 1))
2110 svr
= arg
+ sizeof("--server=") - 1;
2111 else if (! grub_memcmp ("--address=", arg
, sizeof ("--address=") - 1))
2112 ip
= arg
+ sizeof ("--address=") - 1;
2113 else if (! grub_memcmp ("--gateway=", arg
, sizeof ("--gateway=") - 1))
2114 gw
= arg
+ sizeof ("--gateway=") - 1;
2115 else if (! grub_memcmp ("--mask=", arg
, sizeof("--mask=") - 1))
2116 sm
= arg
+ sizeof ("--mask=") - 1;
2119 errnum
= ERR_BAD_ARGUMENT
;
2123 arg
= skip_to (0, arg
);
2126 if (! ifconfig (ip
, sm
, gw
, svr
))
2128 errnum
= ERR_BAD_ARGUMENT
;
2132 print_network_configuration ();
2136 static struct builtin builtin_ifconfig
=
2140 BUILTIN_CMDLINE
| BUILTIN_MENU
| BUILTIN_HELP_LIST
,
2141 "ifconfig [--address=IP] [--gateway=IP] [--mask=MASK] [--server=IP]",
2142 "Configure the IP address, the netmask, the gateway and the server"
2143 " address or print current network configuration."
2145 #endif /* SUPPORT_NETBOOT */
2150 impsprobe_func (char *arg
, int flags
)
2153 /* In the grub shell, we cannot probe IMPS. */
2154 errnum
= ERR_UNRECOGNIZED
;
2156 #else /* ! GRUB_UTIL */
2158 printf (" No MPS information found or probe failed\n");
2161 #endif /* ! GRUB_UTIL */
2164 static struct builtin builtin_impsprobe
=
2170 "Probe the Intel Multiprocessor Specification 1.1 or 1.4"
2171 " configuration table and boot the various CPUs which are found into"
2177 initrd_func (char *arg
, int flags
)
2179 switch (kernel_type
)
2181 case KERNEL_TYPE_LINUX
:
2182 case KERNEL_TYPE_BIG_LINUX
:
2183 if (! load_initrd (arg
))
2188 errnum
= ERR_NEED_LX_KERNEL
;
2195 static struct builtin builtin_initrd
=
2199 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
2200 "initrd FILE [ARG ...]",
2201 "Load an initial ramdisk FILE for a Linux format boot image and set the"
2202 " appropriate parameters in the Linux setup area in memory."
2208 install_func (char *arg
, int flags
)
2210 char *stage1_file
, *dest_dev
, *file
, *addr
;
2211 char *stage1_buffer
= (char *) RAW_ADDR (0x100000);
2212 char *stage2_buffer
= stage1_buffer
+ SECTOR_SIZE
;
2213 char *old_sect
= stage2_buffer
+ SECTOR_SIZE
;
2214 char *stage2_first_buffer
= old_sect
+ SECTOR_SIZE
;
2215 char *stage2_second_buffer
= stage2_first_buffer
+ SECTOR_SIZE
;
2216 /* XXX: Probably SECTOR_SIZE is reasonable. */
2217 char *config_filename
= stage2_second_buffer
+ SECTOR_SIZE
;
2218 char *dummy
= config_filename
+ SECTOR_SIZE
;
2219 int new_drive
= GRUB_INVALID_DRIVE
;
2220 int dest_drive
, dest_partition
;
2221 unsigned int dest_sector
;
2222 int src_drive
, src_partition
, src_part_start
;
2224 struct geometry dest_geom
, src_geom
;
2225 unsigned long long saved_sector
;
2226 unsigned long long stage2_first_sector
, stage2_second_sector
;
2228 int installaddr
, installlist
;
2229 /* Point to the location of the name of a configuration file in Stage 2. */
2230 char *config_file_location
;
2231 /* If FILE is a Stage 1.5? */
2232 int is_stage1_5
= 0;
2233 /* Must call grub_close? */
2235 /* If LBA is forced? */
2236 int is_force_lba
= 0;
2237 /* Was the last sector full? */
2238 int last_length
= SECTOR_SIZE
;
2241 /* If the Stage 2 is in a partition mounted by an OS, this will store
2242 the filename under the OS. */
2243 char *stage2_os_file
= 0;
2244 #endif /* GRUB_UTIL */
2246 auto void disk_read_savesect_func (unsigned long long sector
, int offset
,
2248 auto void disk_read_blocklist_func (unsigned long long sector
, int offset
,
2251 /* Save the first sector of Stage2 in STAGE2_SECT. */
2252 auto void disk_read_savesect_func (unsigned long long sector
, int offset
,
2256 printf ("[%llu]", sector
);
2258 /* ReiserFS has files which sometimes contain data not aligned
2259 on sector boundaries. Returning an error is better than
2260 silently failing. */
2261 if (offset
!= 0 || length
!= SECTOR_SIZE
)
2262 errnum
= ERR_UNALIGNED
;
2264 saved_sector
= sector
;
2267 /* Write SECTOR to INSTALLLIST, and update INSTALLADDR and
2269 auto void disk_read_blocklist_func (unsigned long long sector
, int offset
,
2273 printf("[%llu]", sector
);
2275 if (offset
!= 0 || last_length
!= SECTOR_SIZE
)
2277 /* We found a non-sector-aligned data block. */
2278 errnum
= ERR_UNALIGNED
;
2282 last_length
= length
;
2284 if (*((unsigned long *) (installlist
- 4))
2285 + *((unsigned short *) installlist
) != sector
2286 || installlist
== (int) stage2_first_buffer
+ SECTOR_SIZE
+ 4)
2290 if (*((unsigned long *) (installlist
- 8)))
2291 errnum
= ERR_WONT_FIT
;
2294 *((unsigned short *) (installlist
+ 2)) = (installaddr
>> 4);
2295 *((unsigned long *) (installlist
- 4)) = sector
;
2299 *((unsigned short *) installlist
) += 1;
2303 /* First, check the GNU-style long option. */
2306 if (grub_memcmp ("--force-lba", arg
, sizeof ("--force-lba") - 1) == 0)
2309 arg
= skip_to (0, arg
);
2312 else if (grub_memcmp ("--stage2=", arg
, sizeof ("--stage2=") - 1) == 0)
2314 stage2_os_file
= arg
+ sizeof ("--stage2=") - 1;
2315 arg
= skip_to (0, arg
);
2316 nul_terminate (stage2_os_file
);
2318 #endif /* GRUB_UTIL */
2324 dest_dev
= skip_to (0, stage1_file
);
2325 if (*dest_dev
== 'd')
2328 dest_dev
= skip_to (0, dest_dev
);
2330 file
= skip_to (0, dest_dev
);
2331 addr
= skip_to (0, file
);
2333 /* Get the installation address. */
2334 if (! safe_parse_maxint (&addr
, &installaddr
))
2336 /* ADDR is not specified. */
2342 ptr
= skip_to (0, addr
);
2344 #ifndef NO_DECOMPRESSION
2345 /* Do not decompress Stage 1 or Stage 2. */
2346 no_decompression
= 1;
2350 is_open
= grub_open (stage1_file
);
2352 || ! grub_read (stage1_buffer
, SECTOR_SIZE
) == SECTOR_SIZE
)
2355 /* Read the old sector from DEST_DEV. */
2356 if (! set_device (dest_dev
)
2357 || ! open_partition ()
2358 || ! devread (0, 0, SECTOR_SIZE
, old_sect
))
2361 /* Store the information for the destination device. */
2362 dest_drive
= current_drive
;
2363 dest_partition
= current_partition
;
2364 dest_geom
= buf_geom
;
2365 dest_sector
= part_start
;
2367 /* Copy the possible DOS BPB, 59 bytes at byte offset 3. */
2368 grub_memmove (stage1_buffer
+ BOOTSEC_BPB_OFFSET
,
2369 old_sect
+ BOOTSEC_BPB_OFFSET
,
2370 BOOTSEC_BPB_LENGTH
);
2372 /* If for a hard disk, copy the possible MBR/extended part table. */
2373 if (dest_drive
& 0x80)
2374 grub_memmove (stage1_buffer
+ STAGE1_WINDOWS_NT_MAGIC
,
2375 old_sect
+ STAGE1_WINDOWS_NT_MAGIC
,
2376 STAGE1_PARTEND
- STAGE1_WINDOWS_NT_MAGIC
);
2378 /* Check for the version and the signature of Stage 1. */
2379 if (*((short *)(stage1_buffer
+ STAGE1_VER_MAJ_OFFS
)) != COMPAT_VERSION
2380 || (*((unsigned short *) (stage1_buffer
+ BOOTSEC_SIG_OFFSET
))
2381 != BOOTSEC_SIGNATURE
))
2383 errnum
= ERR_BAD_VERSION
;
2387 /* This below is not true any longer. But should we leave this alone? */
2389 /* If DEST_DRIVE is a floppy, Stage 2 must have the iteration probe
2391 if (! (dest_drive
& 0x80)
2392 && (*((unsigned char *) (stage1_buffer
+ BOOTSEC_PART_OFFSET
)) == 0x80
2393 || stage1_buffer
[BOOTSEC_PART_OFFSET
] == 0))
2395 errnum
= ERR_BAD_VERSION
;
2402 is_open
= grub_open (file
);
2406 src_drive
= current_drive
;
2407 src_partition
= current_partition
;
2408 src_part_start
= part_start
;
2409 src_geom
= buf_geom
;
2412 new_drive
= src_drive
;
2413 else if (src_drive
!= dest_drive
)
2414 grub_printf ("Warning: the option `d' was not used, but the Stage 1 will"
2415 " be installed on a\ndifferent drive than the drive where"
2416 " the Stage 2 resides.\n");
2418 /* Set the boot drive. */
2419 *((unsigned char *) (stage1_buffer
+ STAGE1_BOOT_DRIVE
)) = new_drive
;
2421 /* Set the "force LBA" flag. */
2422 *((unsigned char *) (stage1_buffer
+ STAGE1_FORCE_LBA
)) = is_force_lba
;
2424 /* If DEST_DRIVE is a hard disk, enable the workaround, which is
2425 for buggy BIOSes which don't pass boot drive correctly. Instead,
2426 they pass 0x00 or 0x01 even when booted from 0x80. */
2427 if (dest_drive
& BIOS_FLAG_FIXED_DISK
)
2428 /* Replace the jmp (2 bytes) with double nop's. */
2429 *((unsigned short *) (stage1_buffer
+ STAGE1_BOOT_DRIVE_CHECK
))
2432 /* Read the first sector of Stage 2. */
2433 disk_read_hook
= disk_read_savesect_func
;
2434 if (grub_read (stage2_first_buffer
, SECTOR_SIZE
) != SECTOR_SIZE
)
2437 stage2_first_sector
= saved_sector
;
2438 if (stage2_first_sector
>= 0xffffffffUL
) {
2439 grub_printf ("Error: stage2 first sector must be below 2TB\n");
2443 /* Read the second sector of Stage 2. */
2444 if (grub_read (stage2_second_buffer
, SECTOR_SIZE
) != SECTOR_SIZE
)
2447 stage2_second_sector
= saved_sector
;
2449 /* Check for the version of Stage 2. */
2450 if (*((short *) (stage2_second_buffer
+ STAGE2_VER_MAJ_OFFS
))
2453 errnum
= ERR_BAD_VERSION
;
2457 /* Check for the Stage 2 id. */
2458 if (stage2_second_buffer
[STAGE2_STAGE2_ID
] != STAGE2_ID_STAGE2
)
2461 /* If INSTALLADDR is not specified explicitly in the command-line,
2462 determine it by the Stage 2 id. */
2467 installaddr
= 0x8000;
2470 installaddr
= 0x2000;
2473 *((unsigned long *) (stage1_buffer
+ STAGE1_STAGE2_SECTOR
))
2474 = stage2_first_sector
;
2475 *((unsigned short *) (stage1_buffer
+ STAGE1_STAGE2_ADDRESS
))
2477 *((unsigned short *) (stage1_buffer
+ STAGE1_STAGE2_SEGMENT
))
2480 i
= (int) stage2_first_buffer
+ SECTOR_SIZE
- 4;
2481 while (*((unsigned long *) i
))
2483 if (i
< (int) stage2_first_buffer
2484 || (*((int *) (i
- 4)) & 0x80000000)
2485 || *((unsigned short *) i
) >= 0xA00
2486 || *((short *) (i
+ 2)) == 0)
2488 errnum
= ERR_BAD_VERSION
;
2493 *((int *) (i
- 4)) = 0;
2497 installlist
= (int) stage2_first_buffer
+ SECTOR_SIZE
+ 4;
2498 installaddr
+= SECTOR_SIZE
;
2500 /* Read the whole of Stage2 except for the first sector. */
2501 grub_seek (SECTOR_SIZE
);
2503 disk_read_hook
= disk_read_blocklist_func
;
2504 if (! grub_read (dummy
, -1))
2509 /* Find a string for the configuration filename. */
2510 config_file_location
= stage2_second_buffer
+ STAGE2_VER_STR_OFFS
;
2511 while (*(config_file_location
++))
2514 /* Set the "force LBA" flag for Stage2. */
2515 *((unsigned char *) (stage2_second_buffer
+ STAGE2_FORCE_LBA
))
2520 *((long *) (stage2_second_buffer
+ STAGE2_INSTALLPART
))
2524 /* Reset the device information in FILE if it is a Stage 1.5. */
2525 unsigned long device
= 0xFFFFFFFF;
2527 grub_memmove (config_file_location
, (char *) &device
,
2531 ptr
= skip_to (0, ptr
);
2536 grub_strcpy (config_filename
, ptr
);
2537 nul_terminate (config_filename
);
2540 /* If it is a Stage 2, just copy PTR to CONFIG_FILE_LOCATION. */
2541 grub_strcpy (config_file_location
, ptr
);
2545 unsigned long device
;
2547 /* Translate the external device syntax to the internal device
2549 if (! (real_config
= set_device (ptr
)))
2551 /* The Stage 2 PTR does not contain the device name, so
2552 use the root device instead. */
2554 current_drive
= saved_drive
;
2555 current_partition
= saved_partition
;
2559 if (current_drive
== src_drive
)
2561 /* If the drive where the Stage 2 resides is the same as
2562 the one where the Stage 1.5 resides, do not embed the
2564 current_drive
= GRUB_INVALID_DRIVE
;
2567 device
= (current_drive
<< 24) | current_partition
;
2568 grub_memmove (config_file_location
, (char *) &device
,
2570 grub_strcpy (config_file_location
+ sizeof (device
),
2574 /* If a Stage 1.5 is used, then we need to modify the Stage2. */
2577 char *real_config_filename
= skip_to (0, ptr
);
2579 is_open
= grub_open (config_filename
);
2583 /* Skip the first sector. */
2584 grub_seek (SECTOR_SIZE
);
2586 disk_read_hook
= disk_read_savesect_func
;
2587 if (grub_read (stage2_buffer
, SECTOR_SIZE
) != SECTOR_SIZE
)
2595 if (*(stage2_buffer
+ STAGE2_STAGE2_ID
) != STAGE2_ID_STAGE2
)
2597 errnum
= ERR_BAD_VERSION
;
2601 /* Set the "force LBA" flag for Stage2. */
2602 *(stage2_buffer
+ STAGE2_FORCE_LBA
) = is_force_lba
;
2604 /* If REAL_CONFIG_FILENAME is specified, copy it to the Stage2. */
2605 if (*real_config_filename
)
2610 /* Find a string for the configuration filename. */
2611 location
= stage2_buffer
+ STAGE2_VER_STR_OFFS
;
2612 while (*(location
++))
2615 /* Copy the name. */
2616 grub_strcpy (location
, real_config_filename
);
2619 /* Write it to the disk. */
2620 buf_track
= BUF_CACHE_INVALID
;
2623 /* In the grub shell, access the Stage 2 via the OS filesystem
2624 service, if possible. */
2629 fp
= fopen (stage2_os_file
, "r+");
2632 errnum
= ERR_FILE_NOT_FOUND
;
2636 if (fseek (fp
, SECTOR_SIZE
, SEEK_SET
) != 0)
2639 errnum
= ERR_BAD_VERSION
;
2643 if (fwrite (stage2_buffer
, 1, SECTOR_SIZE
, fp
)
2654 #endif /* GRUB_UTIL */
2656 if (! devwrite (saved_sector
- part_start
, 1, stage2_buffer
))
2662 /* Clear the cache. */
2663 buf_track
= BUF_CACHE_INVALID
;
2665 /* Write the modified sectors of Stage2 to the disk. */
2667 if (! is_stage1_5
&& stage2_os_file
)
2671 fp
= fopen (stage2_os_file
, "r+");
2674 errnum
= ERR_FILE_NOT_FOUND
;
2678 if (fwrite (stage2_first_buffer
, 1, SECTOR_SIZE
, fp
) != SECTOR_SIZE
)
2685 if (fwrite (stage2_second_buffer
, 1, SECTOR_SIZE
, fp
) != SECTOR_SIZE
)
2695 #endif /* GRUB_UTIL */
2698 current_drive
= src_drive
;
2699 current_partition
= src_partition
;
2701 if (! open_partition ())
2704 if (! devwrite (stage2_first_sector
- src_part_start
, 1,
2705 stage2_first_buffer
))
2708 if (! devwrite (stage2_second_sector
- src_part_start
, 1,
2709 stage2_second_buffer
))
2713 /* Write the modified sector of Stage 1 to the disk. */
2714 current_drive
= dest_drive
;
2715 current_partition
= dest_partition
;
2716 if (! open_partition ())
2719 devwrite (0, 1, stage1_buffer
);
2727 #ifndef NO_DECOMPRESSION
2728 no_decompression
= 0;
2734 static struct builtin builtin_install
=
2739 "install [--stage2=STAGE2_FILE] [--force-lba] STAGE1 [d] DEVICE STAGE2 [ADDR] [p] [CONFIG_FILE] [REAL_CONFIG_FILE]",
2740 "Install STAGE1 on DEVICE, and install a blocklist for loading STAGE2"
2741 " as a Stage 2. If the option `d' is present, the Stage 1 will always"
2742 " look for the disk where STAGE2 was installed, rather than using"
2743 " the booting drive. The Stage 2 will be loaded at address ADDR, which"
2744 " will be determined automatically if you don't specify it. If"
2745 " the option `p' or CONFIG_FILE is present, then the first block"
2746 " of Stage 2 is patched with new values of the partition and name"
2747 " of the configuration file used by the true Stage 2 (for a Stage 1.5,"
2748 " this is the name of the true Stage 2) at boot time. If STAGE2 is a Stage"
2749 " 1.5 and REAL_CONFIG_FILE is present, then the Stage 2 CONFIG_FILE is"
2750 " patched with the configuration filename REAL_CONFIG_FILE."
2751 " If the option `--force-lba' is specified, disable some sanity checks"
2752 " for LBA mode. If the option `--stage2' is specified, rewrite the Stage"
2753 " 2 via your OS's filesystem instead of the raw device."
2759 ioprobe_func (char *arg
, int flags
)
2763 errnum
= ERR_UNRECOGNIZED
;
2766 #else /* ! GRUB_UTIL */
2768 unsigned short *port
;
2770 /* Get the drive number. */
2775 /* Clean out IO_MAP. */
2776 grub_memset ((char *) io_map
, 0, IO_MAP_SIZE
* sizeof (unsigned short));
2778 /* Track the int13 handler. */
2779 track_int13 (current_drive
);
2781 /* Print out the result. */
2782 for (port
= io_map
; *port
!= 0; port
++)
2783 grub_printf (" 0x%x", (unsigned int) *port
);
2787 #endif /* ! GRUB_UTIL */
2790 static struct builtin builtin_ioprobe
=
2796 "Probe I/O ports used for the drive DRIVE."
2802 kernel_func (char *arg
, int flags
)
2805 kernel_t suggested_type
= KERNEL_TYPE_NONE
;
2806 unsigned long load_flags
= 0;
2808 #ifndef AUTO_LINUX_MEM_OPT
2809 load_flags
|= KERNEL_LOAD_NO_MEM_OPTION
;
2812 /* Deal with GNU-style long options. */
2815 /* If the option `--type=TYPE' is specified, convert the string to
2817 if (grub_memcmp (arg
, "--type=", 7) == 0)
2821 if (grub_memcmp (arg
, "netbsd", 6) == 0)
2822 suggested_type
= KERNEL_TYPE_NETBSD
;
2823 else if (grub_memcmp (arg
, "freebsd", 7) == 0)
2824 suggested_type
= KERNEL_TYPE_FREEBSD
;
2825 else if (grub_memcmp (arg
, "openbsd", 7) == 0)
2826 /* XXX: For now, OpenBSD is identical to NetBSD, from GRUB's
2828 suggested_type
= KERNEL_TYPE_NETBSD
;
2829 else if (grub_memcmp (arg
, "linux", 5) == 0)
2830 suggested_type
= KERNEL_TYPE_LINUX
;
2831 else if (grub_memcmp (arg
, "biglinux", 8) == 0)
2832 suggested_type
= KERNEL_TYPE_BIG_LINUX
;
2833 else if (grub_memcmp (arg
, "multiboot", 9) == 0)
2834 suggested_type
= KERNEL_TYPE_MULTIBOOT
;
2837 errnum
= ERR_BAD_ARGUMENT
;
2841 /* If the `--no-mem-option' is specified, don't pass a Linux's mem
2842 option automatically. If the kernel is another type, this flag
2844 else if (grub_memcmp (arg
, "--no-mem-option", 15) == 0)
2845 load_flags
|= KERNEL_LOAD_NO_MEM_OPTION
;
2850 arg
= skip_to (0, arg
);
2853 len
= grub_strlen (arg
);
2855 /* Reset MB_CMDLINE. */
2856 mb_cmdline
= (char *) MB_CMDLINE_BUF
;
2857 if (len
+ 1 > MB_CMDLINE_BUFLEN
)
2859 errnum
= ERR_WONT_FIT
;
2863 /* Copy the command-line to MB_CMDLINE. */
2864 grub_memmove (mb_cmdline
, arg
, len
+ 1);
2865 kernel_type
= load_image (arg
, mb_cmdline
, suggested_type
, load_flags
);
2866 if (kernel_type
== KERNEL_TYPE_NONE
)
2869 mb_cmdline
+= grub_strlen(mb_cmdline
) + 1;
2873 static struct builtin builtin_kernel
=
2877 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
2878 "kernel [--no-mem-option] [--type=TYPE] FILE [ARG ...]",
2879 "Attempt to load the primary boot image from FILE. The rest of the"
2880 " line is passed verbatim as the \"kernel command line\". Any modules"
2881 " must be reloaded after using this command. The option --type is used"
2882 " to suggest what type of kernel to be loaded. TYPE must be either of"
2883 " \"netbsd\", \"freebsd\", \"openbsd\", \"linux\", \"biglinux\" and"
2884 " \"multiboot\". The option --no-mem-option tells GRUB not to pass a"
2885 " Linux's mem option automatically."
2889 min_mem64_func(char *arg
, int flags
)
2891 if (!safe_parse_maxint(&arg
, &min_mem64
))
2895 static struct builtin builtin_min_mem64
=
2899 BUILTIN_CMDLINE
| BUILTIN_MENU
| BUILTIN_SCRIPT
| BUILTIN_HELP_LIST
,
2900 "min_mem64 <memory in MB>",
2901 "Sets minimum memory (in MB) required for $ISADIR to expand to amd64, "
2902 "even on 64-bit capable hardware."
2906 kernel_dollar_func (char *arg
, int flags
)
2909 char newarg
[MAX_CMDLINE
];
2912 * We're going to expand the arguments twice. The first expansion, which
2913 * occurs without the benefit of knowing the ZFS object ID of the filesystem
2914 * we're booting from (if we're booting from ZFS, of course), must be
2915 * sufficient to find and read the kernel. The second expansion will
2916 * then overwrite the command line actually set in the multiboot header with
2917 * the newly-expanded one. Since $ZFS-BOOTFS expands differently after
2918 * zfs_open() has been called (kernel_func() -> load_image() -> grub_open() ->
2919 * zfs_open()), we need to do the second expansion so that the kernel is
2920 * given the right object ID argument. Note that the pointer to the
2921 * command line set in the multiboot header is always MB_CMDLINE_BUF.
2923 grub_printf("loading '%s' ...\n", arg
);
2924 if ((err
= expand_string(arg
, newarg
, MAX_CMDLINE
)) != 0) {
2929 if ((err
= kernel_func(newarg
, flags
)) != 0)
2932 mb_cmdline
= (char *)MB_CMDLINE_BUF
;
2933 if ((err
= expand_string(arg
, mb_cmdline
, MAX_CMDLINE
)) != 0) {
2938 grub_printf("loading '%s' ...\n", mb_cmdline
);
2939 mb_cmdline
+= grub_strlen(mb_cmdline
) + 1;
2943 static struct builtin builtin_kernel_dollar
=
2947 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
2948 "kernel$ [--no-mem-option] [--type=TYPE] FILE [ARG ...]",
2949 " Just like kernel, but with variable expansion, including the legacy"
2950 " (and nonconforming) variables $ISADIR and $ZFS-BOOTFS."
2956 lock_func (char *arg
, int flags
)
2958 if (! auth
&& password
)
2960 errnum
= ERR_PRIVILEGED
;
2967 static struct builtin builtin_lock
=
2973 "Break a command execution unless the user is authenticated."
2979 makeactive_func (char *arg
, int flags
)
2981 if (! make_saved_active ())
2987 static struct builtin builtin_makeactive
=
2991 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
2993 "Set the active partition on the root disk to GRUB's root device."
2994 " This command is limited to _primary_ PC partitions on a hard disk."
2999 /* Map FROM_DRIVE to TO_DRIVE. */
3001 map_func (char *arg
, int flags
)
3005 unsigned long to
, from
;
3009 from_drive
= skip_to (0, arg
);
3011 /* Get the drive number for TO_DRIVE. */
3012 set_device (to_drive
);
3017 /* Get the drive number for FROM_DRIVE. */
3018 set_device (from_drive
);
3021 from
= current_drive
;
3023 /* Search for an empty slot in BIOS_DRIVE_MAP. */
3024 for (i
= 0; i
< DRIVE_MAP_SIZE
; i
++)
3026 /* Perhaps the user wants to override the map. */
3027 if ((bios_drive_map
[i
] & 0xff) == from
)
3030 if (! bios_drive_map
[i
])
3034 if (i
== DRIVE_MAP_SIZE
)
3036 errnum
= ERR_WONT_FIT
;
3041 /* If TO is equal to FROM, delete the entry. */
3042 grub_memmove ((char *) &bios_drive_map
[i
], (char *) &bios_drive_map
[i
+ 1],
3043 sizeof (unsigned short) * (DRIVE_MAP_SIZE
- i
));
3045 bios_drive_map
[i
] = from
| (to
<< 8);
3050 static struct builtin builtin_map
=
3054 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
3055 "map TO_DRIVE FROM_DRIVE",
3056 "Map the drive FROM_DRIVE to the drive TO_DRIVE. This is necessary"
3057 " when you chain-load some operating systems, such as DOS, if such an"
3058 " OS resides at a non-first drive."
3062 #ifdef USE_MD5_PASSWORDS
3065 md5crypt_func (char *arg
, int flags
)
3071 const char *const seedchars
=
3072 "./0123456789ABCDEFGHIJKLMNOPQRST"
3073 "UVWXYZabcdefghijklmnopqrstuvwxyz";
3075 /* First create a salt. */
3077 /* The magical prefix. */
3078 grub_memset (crypted
, 0, sizeof (crypted
));
3079 grub_memmove (crypted
, "$1$", 3);
3081 /* Create the length of a salt. */
3082 seed
= currticks ();
3084 /* Generate a salt. */
3085 for (i
= 0; i
< 8 && seed
; i
++)
3087 /* FIXME: This should be more random. */
3088 crypted
[3 + i
] = seedchars
[seed
& 0x3f];
3092 /* A salt must be terminated with `$', if it is less than 8 chars. */
3093 crypted
[3 + i
] = '$';
3095 #ifdef DEBUG_MD5CRYPT
3096 grub_printf ("salt = %s\n", crypted
);
3099 /* Get a password. */
3100 grub_memset (key
, 0, sizeof (key
));
3101 get_cmdline ("Password: ", key
, sizeof (key
) - 1, '*', 0);
3103 /* Crypt the key. */
3104 make_md5_password (key
, crypted
);
3106 grub_printf ("Encrypted: %s\n", crypted
);
3110 static struct builtin builtin_md5crypt
=
3114 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
3116 "Generate a password in MD5 format."
3118 #endif /* USE_MD5_PASSWORDS */
3123 module_func (char *arg
, int flags
)
3125 int len
= grub_strlen (arg
);
3127 switch (kernel_type
)
3129 case KERNEL_TYPE_MULTIBOOT
:
3130 if (mb_cmdline
+ len
+ 1 > (char *) MB_CMDLINE_BUF
+ MB_CMDLINE_BUFLEN
)
3132 errnum
= ERR_WONT_FIT
;
3135 grub_memmove (mb_cmdline
, arg
, len
+ 1);
3136 if (! load_module (arg
, mb_cmdline
))
3139 mb_cmdline
+= grub_strlen(mb_cmdline
) + 1;
3142 case KERNEL_TYPE_LINUX
:
3143 case KERNEL_TYPE_BIG_LINUX
:
3144 if (! load_initrd (arg
))
3149 errnum
= ERR_NEED_MB_KERNEL
;
3156 static struct builtin builtin_module
=
3160 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
3161 "module FILE [ARG ...]",
3162 "Load a boot module FILE for a Multiboot format boot image (no"
3163 " interpretation of the file contents is made, so users of this"
3164 " command must know what the kernel in question expects). The"
3165 " rest of the line is passed as the \"module command line\", like"
3166 " the `kernel' command."
3171 module_dollar_func (char *arg
, int flags
)
3173 char newarg
[MAX_CMDLINE
];
3174 char *cmdline_sav
= mb_cmdline
;
3177 grub_printf("loading '%s' ...\n", arg
);
3178 if ((err
= expand_string(arg
, newarg
, MAX_CMDLINE
)) != 0) {
3183 if ((err
= module_func(newarg
, flags
)) != 0)
3186 if ((err
= expand_string(arg
, cmdline_sav
, MAX_CMDLINE
)) != 0) {
3191 grub_printf("loading '%s' ...\n", cmdline_sav
);
3192 mb_cmdline
+= grub_strlen(cmdline_sav
) + 1;
3196 static struct builtin builtin_module_dollar
=
3200 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
3201 "module FILE [ARG ...]",
3202 " Just like module, but with $ISADIR expansion."
3208 modulenounzip_func (char *arg
, int flags
)
3212 #ifndef NO_DECOMPRESSION
3213 no_decompression
= 1;
3216 ret
= module_func (arg
, flags
);
3218 #ifndef NO_DECOMPRESSION
3219 no_decompression
= 0;
3225 static struct builtin builtin_modulenounzip
=
3229 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
3230 "modulenounzip FILE [ARG ...]",
3231 "The same as `module', except that automatic decompression is"
3236 /* pager [on|off] */
3238 pager_func (char *arg
, int flags
)
3240 /* If ARG is empty, toggle the flag. */
3242 use_pager
= ! use_pager
;
3243 else if (grub_memcmp (arg
, "on", 2) == 0)
3245 else if (grub_memcmp (arg
, "off", 3) == 0)
3249 errnum
= ERR_BAD_ARGUMENT
;
3253 grub_printf (" Internal pager is now %s\n", use_pager
? "on" : "off");
3257 static struct builtin builtin_pager
=
3261 BUILTIN_CMDLINE
| BUILTIN_MENU
| BUILTIN_HELP_LIST
,
3263 "Toggle pager mode with no argument. If FLAG is given and its value"
3264 " is `on', turn on the mode. If FLAG is `off', turn off the mode."
3268 /* partnew PART TYPE START LEN */
3270 partnew_func (char *arg
, int flags
)
3272 int new_type
, new_start
, new_len
;
3273 int start_cl
, start_ch
, start_dh
;
3274 int end_cl
, end_ch
, end_dh
;
3278 /* Convert a LBA address to a CHS address in the INT 13 format. */
3279 auto void lba_to_chs (int lba
, int *cl
, int *ch
, int *dh
);
3280 void lba_to_chs (int lba
, int *cl
, int *ch
, int *dh
)
3282 int cylinder
, head
, sector
;
3284 sector
= lba
% buf_geom
.sectors
+ 1;
3285 head
= (lba
/ buf_geom
.sectors
) % buf_geom
.heads
;
3286 cylinder
= lba
/ (buf_geom
.sectors
* buf_geom
.heads
);
3288 if (cylinder
>= buf_geom
.cylinders
)
3289 cylinder
= buf_geom
.cylinders
- 1;
3291 *cl
= sector
| ((cylinder
& 0x300) >> 2);
3292 *ch
= cylinder
& 0xFF;
3296 /* Get the drive and the partition. */
3297 if (! set_device (arg
))
3300 /* The drive must be a hard disk. */
3301 if (! (current_drive
& 0x80))
3303 errnum
= ERR_BAD_ARGUMENT
;
3307 /* The partition must a primary partition. */
3308 if ((current_partition
>> 16) > 3
3309 || (current_partition
& 0xFFFF) != 0xFFFF)
3311 errnum
= ERR_BAD_ARGUMENT
;
3315 entry
= current_partition
>> 16;
3317 /* Get the new partition type. */
3318 arg
= skip_to (0, arg
);
3319 if (! safe_parse_maxint (&arg
, &new_type
))
3322 /* The partition type is unsigned char. */
3323 if (new_type
> 0xFF)
3325 errnum
= ERR_BAD_ARGUMENT
;
3329 /* Get the new partition start. */
3330 arg
= skip_to (0, arg
);
3331 if (! safe_parse_maxint (&arg
, &new_start
))
3334 /* Get the new partition length. */
3335 arg
= skip_to (0, arg
);
3336 if (! safe_parse_maxint (&arg
, &new_len
))
3340 if (! rawread (current_drive
, 0, 0, SECTOR_SIZE
, mbr
))
3343 /* Store the partition information in the MBR. */
3344 lba_to_chs (new_start
, &start_cl
, &start_ch
, &start_dh
);
3345 lba_to_chs (new_start
+ new_len
- 1, &end_cl
, &end_ch
, &end_dh
);
3347 PC_SLICE_FLAG (mbr
, entry
) = 0;
3348 PC_SLICE_HEAD (mbr
, entry
) = start_dh
;
3349 PC_SLICE_SEC (mbr
, entry
) = start_cl
;
3350 PC_SLICE_CYL (mbr
, entry
) = start_ch
;
3351 PC_SLICE_TYPE (mbr
, entry
) = new_type
;
3352 PC_SLICE_EHEAD (mbr
, entry
) = end_dh
;
3353 PC_SLICE_ESEC (mbr
, entry
) = end_cl
;
3354 PC_SLICE_ECYL (mbr
, entry
) = end_ch
;
3355 PC_SLICE_START (mbr
, entry
) = new_start
;
3356 PC_SLICE_LENGTH (mbr
, entry
) = new_len
;
3358 /* Make sure that the MBR has a valid signature. */
3359 PC_MBR_SIG (mbr
) = PC_MBR_SIGNATURE
;
3361 /* Write back the MBR to the disk. */
3362 buf_track
= BUF_CACHE_INVALID
;
3363 if (! rawwrite (current_drive
, 0, mbr
))
3369 static struct builtin builtin_partnew
=
3373 BUILTIN_CMDLINE
| BUILTIN_MENU
| BUILTIN_HELP_LIST
,
3374 "partnew PART TYPE START LEN",
3375 "Create a primary partition at the starting address START with the"
3376 " length LEN, with the type TYPE. START and LEN are in sector units."
3380 /* parttype PART TYPE */
3382 parttype_func (char *arg
, int flags
)
3385 unsigned long part
= 0xFFFFFF;
3386 unsigned long long start
, len
, offset
, ext_offset
, gpt_offset
;
3387 int entry
, type
, gpt_count
, gpt_size
;
3390 /* Get the drive and the partition. */
3391 if (! set_device (arg
))
3394 /* The drive must be a hard disk. */
3395 if (! (current_drive
& 0x80))
3397 errnum
= ERR_BAD_ARGUMENT
;
3401 /* The partition must be a PC slice. */
3402 if ((current_partition
>> 16) == 0xFF
3403 || (current_partition
& 0xFFFF) != 0xFFFF)
3405 errnum
= ERR_BAD_ARGUMENT
;
3409 /* Get the new partition type. */
3410 arg
= skip_to (0, arg
);
3411 if (! safe_parse_maxint (&arg
, &new_type
))
3414 /* The partition type is unsigned char. */
3415 if (new_type
> 0xFF)
3417 errnum
= ERR_BAD_ARGUMENT
;
3421 /* Look for the partition. */
3422 while (next_partition (current_drive
, 0xFFFFFF, &part
, &type
,
3423 &start
, &len
, &offset
, &entry
,
3424 &ext_offset
, &gpt_offset
, &gpt_count
, &gpt_size
, mbr
))
3426 /* The partition may not be a GPT partition. */
3427 if (gpt_offset
!= 0)
3429 errnum
= ERR_BAD_ARGUMENT
;
3433 if (part
== current_partition
)
3437 /* Set the type to NEW_TYPE. */
3438 PC_SLICE_TYPE (mbr
, entry
) = new_type
;
3440 /* Write back the MBR to the disk. */
3441 buf_track
= BUF_CACHE_INVALID
;
3442 if (! rawwrite (current_drive
, offset
, mbr
))
3450 /* The partition was not found. ERRNUM was set by next_partition. */
3454 static struct builtin builtin_parttype
=
3458 BUILTIN_CMDLINE
| BUILTIN_MENU
| BUILTIN_HELP_LIST
,
3459 "parttype PART TYPE",
3460 "Change the type of the partition PART to TYPE."
3466 password_func (char *arg
, int flags
)
3469 password_t type
= PASSWORD_PLAIN
;
3471 #ifdef USE_MD5_PASSWORDS
3472 if (grub_memcmp (arg
, "--md5", 5) == 0)
3474 type
= PASSWORD_MD5
;
3475 arg
= skip_to (0, arg
);
3478 if (grub_memcmp (arg
, "--", 2) == 0)
3480 type
= PASSWORD_UNSUPPORTED
;
3481 arg
= skip_to (0, arg
);
3484 if ((flags
& (BUILTIN_CMDLINE
| BUILTIN_SCRIPT
)) != 0)
3486 /* Do password check! */
3489 /* Wipe out any previously entered password */
3491 get_cmdline ("Password: ", entered
, 31, '*', 0);
3493 nul_terminate (arg
);
3494 if (check_password (entered
, arg
, type
) != 0)
3496 errnum
= ERR_PRIVILEGED
;
3502 len
= grub_strlen (arg
);
3504 /* PASSWORD NUL NUL ... */
3505 if (len
+ 2 > PASSWORD_BUFLEN
)
3507 errnum
= ERR_WONT_FIT
;
3511 /* Copy the password and clear the rest of the buffer. */
3512 password
= (char *) PASSWORD_BUF
;
3513 grub_memmove (password
, arg
, len
);
3514 grub_memset (password
+ len
, 0, PASSWORD_BUFLEN
- len
);
3515 password_type
= type
;
3520 static struct builtin builtin_password
=
3524 BUILTIN_MENU
| BUILTIN_CMDLINE
| BUILTIN_NO_ECHO
,
3525 "password [--md5] PASSWD [FILE]",
3526 "If used in the first section of a menu file, disable all"
3527 " interactive editing control (menu entry editor and"
3528 " command line). If the password PASSWD is entered, it loads the"
3529 " FILE as a new config file and restarts the GRUB Stage 2. If you"
3530 " omit the argument FILE, then GRUB just unlocks privileged"
3531 " instructions. You can also use it in the script section, in"
3532 " which case it will ask for the password, before continueing."
3533 " The option --md5 tells GRUB that PASSWD is encrypted with"
3540 pause_func (char *arg
, int flags
)
3542 printf("%s\n", arg
);
3544 /* If ESC is returned, then abort this entry. */
3545 if (ASCII_CHAR (getkey ()) == 27)
3551 static struct builtin builtin_pause
=
3555 BUILTIN_CMDLINE
| BUILTIN_NO_ECHO
,
3556 "pause [MESSAGE ...]",
3557 "Print MESSAGE, then wait until a key is pressed."
3564 quit_func (char *arg
, int flags
)
3568 /* Never reach here. */
3572 static struct builtin builtin_quit
=
3576 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
3578 "Exit from the GRUB shell."
3580 #endif /* GRUB_UTIL */
3583 #ifdef SUPPORT_NETBOOT
3586 rarp_func (char *arg
, int flags
)
3590 if (errnum
== ERR_NONE
)
3591 errnum
= ERR_DEV_VALUES
;
3596 /* Notify the configuration. */
3597 print_network_configuration ();
3601 static struct builtin builtin_rarp
=
3605 BUILTIN_CMDLINE
| BUILTIN_MENU
| BUILTIN_HELP_LIST
,
3607 "Initialize a network device via RARP."
3609 #endif /* SUPPORT_NETBOOT */
3613 read_func (char *arg
, int flags
)
3617 if (! safe_parse_maxint (&arg
, &addr
))
3620 grub_printf ("Address 0x%x: Value 0x%x\n",
3621 addr
, *((unsigned *) RAW_ADDR (addr
)));
3625 static struct builtin builtin_read
=
3631 "Read a 32-bit value from memory at address ADDR and"
3632 " display it in hex format."
3638 reboot_func (char *arg
, int flags
)
3642 /* Never reach here. */
3646 static struct builtin builtin_reboot
=
3650 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
3652 "Reboot your system."
3656 /* Print the root device information. */
3658 print_root_device (void)
3660 if (saved_drive
== NETWORK_DRIVE
)
3662 /* Network drive. */
3663 grub_printf (" (nd):");
3665 else if (saved_drive
& 0x80)
3667 /* Hard disk drive. */
3668 grub_printf (" (hd%d", saved_drive
- 0x80);
3670 if ((saved_partition
& 0xFF0000) != 0xFF0000)
3671 grub_printf (",%d", saved_partition
>> 16);
3673 if ((saved_partition
& 0x00FF00) != 0x00FF00)
3674 grub_printf (",%c", ((saved_partition
>> 8) & 0xFF) + 'a');
3680 /* Floppy disk drive. */
3681 grub_printf (" (fd%d):", saved_drive
);
3684 /* Print the filesystem information. */
3685 current_partition
= saved_partition
;
3686 current_drive
= saved_drive
;
3691 real_root_func (char *arg
, int attempt_mount
)
3697 /* If ARG is empty, just print the current root device. */
3700 print_root_device ();
3704 /* Call set_device to get the drive and the partition in ARG. */
3705 next
= set_device (arg
);
3709 /* Ignore ERR_FSYS_MOUNT. */
3712 if (! open_device () && errnum
!= ERR_FSYS_MOUNT
)
3717 /* This is necessary, because the location of a partition table
3718 must be set appropriately. */
3719 if (open_partition ())
3729 saved_partition
= current_partition
;
3730 saved_drive
= current_drive
;
3734 /* BSD and chainloading evil hacks !! */
3735 biasptr
= skip_to (0, next
);
3736 safe_parse_maxint (&biasptr
, &hdbias
);
3738 bootdev
= set_bootdev (hdbias
);
3742 /* Print the type of the filesystem. */
3750 root_func (char *arg
, int flags
)
3753 return real_root_func (arg
, 1);
3756 static struct builtin builtin_root
=
3760 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
3761 "root [DEVICE [HDBIAS]]",
3762 "Set the current \"root device\" to the device DEVICE, then"
3763 " attempt to mount it to get the partition size (for passing the"
3764 " partition descriptor in `ES:ESI', used by some chain-loaded"
3765 " bootloaders), the BSD drive-type (for booting BSD kernels using"
3766 " their native boot format), and correctly determine "
3767 " the PC partition where a BSD sub-partition is located. The"
3768 " optional HDBIAS parameter is a number to tell a BSD kernel"
3769 " how many BIOS drive numbers are on controllers before the current"
3770 " one. For example, if there is an IDE disk and a SCSI disk, and your"
3771 " FreeBSD root partition is on the SCSI disk, then use a `1' for HDBIAS."
3777 findroot_func (char *arg
, int flags
)
3782 if (grub_strlen(arg
) >= BOOTSIGN_ARGLEN
) {
3783 errnum
= ERR_BAD_ARGUMENT
;
3787 if (arg
[0] == '\0') {
3788 errnum
= ERR_BAD_ARGUMENT
;
3795 ret
= find_common(arg
, root
, 1, flags
);
3800 return real_root_func (root
, 1);
3803 static struct builtin builtin_findroot
=
3807 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
3808 "findroot <SIGNATURE | (SIGNATURE,partition[,slice])>",
3809 "Searches across all partitions for the file name SIGNATURE."
3810 " GRUB looks only in the directory /boot/grub/bootsign for the"
3811 " filename and it stops as soon as it finds the first instance of"
3812 " the file - so to be useful the name of the signature file must be"
3813 " unique across all partitions. Once the signature file is found,"
3814 " GRUB invokes the \"root\" command on that partition."
3815 " An optional partition and slice may be specified to optimize the search."
3820 * COMMAND to override the default root filesystem for ZFS
3824 bootfs_func (char *arg
, int flags
)
3831 if (current_bootfs
[0] != '\0')
3832 grub_printf ("The zfs boot filesystem is set to '%s'.\n",
3834 else if (current_rootpool
[0] != 0 && current_bootfs_obj
!= 0)
3835 grub_printf("The zfs boot filesystem is <default: %s/%u>.",
3836 current_rootpool
, current_bootfs_obj
);
3838 grub_printf ("The zfs boot filesystem will be derived from "
3839 "the default bootfs pool property.\n");
3844 /* Verify the zfs filesystem name */
3845 if (arg
[0] == '/' || arg
[0] == '\0') {
3846 errnum
= ERR_BAD_ARGUMENT
;
3849 if (current_rootpool
[0] != 0 && grub_strncmp(arg
,
3850 current_rootpool
, strlen(current_rootpool
))) {
3851 errnum
= ERR_BAD_ARGUMENT
;
3855 grub_memmove(current_bootfs
, arg
, MAXNAMELEN
);
3860 static struct builtin builtin_bootfs
=
3864 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
3865 "bootfs [ZFSBOOTFS]",
3866 "Set the current zfs boot filesystem to ZFSBOOTFS (rootpool/rootfs)."
3872 rootnoverify_func (char *arg
, int flags
)
3874 return real_root_func (arg
, 0);
3877 static struct builtin builtin_rootnoverify
=
3881 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
3882 "rootnoverify [DEVICE [HDBIAS]]",
3883 "Similar to `root', but don't attempt to mount the partition. This"
3884 " is useful for when an OS is outside of the area of the disk that"
3885 " GRUB can read, but setting the correct root device is still"
3886 " desired. Note that the items mentioned in `root' which"
3887 " derived from attempting the mount will NOT work correctly."
3893 savedefault_func (char *arg
, int flags
)
3895 #if !defined(SUPPORT_DISKLESS) && !defined(GRUB_UTIL)
3896 unsigned long tmp_drive
= saved_drive
;
3897 unsigned long tmp_partition
= saved_partition
;
3898 char *default_file
= (char *) DEFAULT_FILE_BUF
;
3900 char sect
[SECTOR_SIZE
];
3902 int sector_count
= 0;
3903 unsigned long long saved_sectors
[2];
3904 int saved_offsets
[2];
3905 int saved_lengths
[2];
3907 /* not supported for zfs root */
3908 if (is_zfs_mount
== 1) {
3909 return (0); /* no-op */
3912 /* Save sector information about at most two sectors. */
3913 auto void disk_read_savesect_func (unsigned long long sector
, int offset
,
3915 void disk_read_savesect_func (unsigned long long sector
, int offset
, int length
)
3917 if (sector_count
< 2)
3919 saved_sectors
[sector_count
] = sector
;
3920 saved_offsets
[sector_count
] = offset
;
3921 saved_lengths
[sector_count
] = length
;
3926 /* This command is only useful when you boot an entry from the menu
3928 if (! (flags
& BUILTIN_SCRIPT
))
3930 errnum
= ERR_UNRECOGNIZED
;
3934 /* Determine a saved entry number. */
3937 if (grub_memcmp (arg
, "fallback", sizeof ("fallback") - 1) == 0)
3942 for (i
= 0; i
< MAX_FALLBACK_ENTRIES
; i
++)
3944 if (fallback_entries
[i
] < 0)
3946 if (fallback_entries
[i
] == current_entryno
)
3953 if (index
>= MAX_FALLBACK_ENTRIES
|| fallback_entries
[index
] < 0)
3955 /* This is the last. */
3956 errnum
= ERR_BAD_ARGUMENT
;
3960 entryno
= fallback_entries
[index
];
3962 else if (! safe_parse_maxint (&arg
, &entryno
))
3966 entryno
= current_entryno
;
3968 /* Open the default file. */
3969 saved_drive
= boot_drive
;
3970 saved_partition
= install_partition
;
3971 if (grub_open (default_file
))
3975 disk_read_hook
= disk_read_savesect_func
;
3976 len
= grub_read (buf
, sizeof (buf
));
3980 if (len
!= sizeof (buf
))
3982 /* This is too small. Do not modify the file manually, please! */
3987 if (sector_count
> 2)
3989 /* Is this possible?! Too fragmented! */
3990 errnum
= ERR_FSYS_CORRUPT
;
3994 /* Set up a string to be written. */
3995 grub_memset (buf
, '\n', sizeof (buf
));
3996 grub_sprintf (buf
, "%d", entryno
);
3998 if (saved_lengths
[0] < sizeof (buf
))
4000 /* The file is anchored to another file and the first few bytes
4001 are spanned in two sectors. Uggh... */
4002 if (! rawread (current_drive
, saved_sectors
[0], 0, SECTOR_SIZE
,
4005 grub_memmove (sect
+ saved_offsets
[0], buf
, saved_lengths
[0]);
4006 if (! rawwrite (current_drive
, saved_sectors
[0], sect
))
4009 if (! rawread (current_drive
, saved_sectors
[1], 0, SECTOR_SIZE
,
4012 grub_memmove (sect
+ saved_offsets
[1],
4013 buf
+ saved_lengths
[0],
4014 sizeof (buf
) - saved_lengths
[0]);
4015 if (! rawwrite (current_drive
, saved_sectors
[1], sect
))
4020 /* This is a simple case. It fits into a single sector. */
4021 if (! rawread (current_drive
, saved_sectors
[0], 0, SECTOR_SIZE
,
4024 grub_memmove (sect
+ saved_offsets
[0], buf
, sizeof (buf
));
4025 if (! rawwrite (current_drive
, saved_sectors
[0], sect
))
4029 /* Clear the cache. */
4030 buf_track
= BUF_CACHE_INVALID
;
4034 saved_drive
= tmp_drive
;
4035 saved_partition
= tmp_partition
;
4037 #else /* ! SUPPORT_DISKLESS && ! GRUB_UTIL */
4038 errnum
= ERR_UNRECOGNIZED
;
4040 #endif /* ! SUPPORT_DISKLESS && ! GRUB_UTIL */
4043 static struct builtin builtin_savedefault
=
4048 "savedefault [NUM | `fallback']",
4049 "Save the current entry as the default boot entry if no argument is"
4050 " specified. If a number is specified, this number is saved. If"
4051 " `fallback' is used, next fallback entry is saved."
4055 #ifdef SUPPORT_SERIAL
4058 serial_func (char *arg
, int flags
)
4061 int units
[SERIAL_MAX_PORTS
];
4062 unsigned short ports
[SERIAL_MAX_PORTS
];
4063 unsigned int speed
= 9600;
4064 int word_len
= UART_8BITS_WORD
;
4065 int parity
= UART_NO_PARITY
;
4066 int stop_bit_len
= UART_1_STOP_BIT
;
4068 for (i
= 0; i
< SERIAL_MAX_PORTS
; ++i
)
4074 /* Process GNU-style long options.
4075 FIXME: We should implement a getopt-like function, to avoid
4079 if (grub_memcmp (arg
, "--unit=", sizeof ("--unit=") - 1) == 0)
4081 char *p
= arg
+ sizeof ("--unit=") - 1;
4087 if (i
>= SERIAL_MAX_PORTS
)
4089 errnum
= ERR_DEV_FORMAT
;
4093 if (! safe_parse_maxint (&p
, &unit
))
4096 if (unit
< 0 || unit
> 3)
4098 errnum
= ERR_DEV_VALUES
;
4104 while (*p
++ == ',');
4106 else if (grub_memcmp (arg
, "--speed=", sizeof ("--speed=") - 1) == 0)
4108 char *p
= arg
+ sizeof ("--speed=") - 1;
4111 if (! safe_parse_maxint (&p
, &num
))
4114 speed
= (unsigned int) num
;
4116 else if (grub_memcmp (arg
, "--port=", sizeof ("--port=") - 1) == 0)
4118 char *p
= arg
+ sizeof ("--port=") - 1;
4124 if (i
>= SERIAL_MAX_PORTS
)
4126 errnum
= ERR_DEV_FORMAT
;
4130 if (! safe_parse_maxint (&p
, &num
))
4133 if (num
> 0xffff || num
<= 0)
4135 errnum
= ERR_DEV_VALUES
;
4139 ports
[i
++] = (unsigned short) num
;
4141 while (*p
++ == ',');
4143 else if (grub_memcmp (arg
, "--word=", sizeof ("--word=") - 1) == 0)
4145 char *p
= arg
+ sizeof ("--word=") - 1;
4148 if (! safe_parse_maxint (&p
, &len
))
4153 case 5: word_len
= UART_5BITS_WORD
; break;
4154 case 6: word_len
= UART_6BITS_WORD
; break;
4155 case 7: word_len
= UART_7BITS_WORD
; break;
4156 case 8: word_len
= UART_8BITS_WORD
; break;
4158 errnum
= ERR_BAD_ARGUMENT
;
4162 else if (grub_memcmp (arg
, "--stop=", sizeof ("--stop=") - 1) == 0)
4164 char *p
= arg
+ sizeof ("--stop=") - 1;
4167 if (! safe_parse_maxint (&p
, &len
))
4172 case 1: stop_bit_len
= UART_1_STOP_BIT
; break;
4173 case 2: stop_bit_len
= UART_2_STOP_BITS
; break;
4175 errnum
= ERR_BAD_ARGUMENT
;
4179 else if (grub_memcmp (arg
, "--parity=", sizeof ("--parity=") - 1) == 0)
4181 char *p
= arg
+ sizeof ("--parity=") - 1;
4183 if (grub_memcmp (p
, "no", sizeof ("no") - 1) == 0)
4184 parity
= UART_NO_PARITY
;
4185 else if (grub_memcmp (p
, "odd", sizeof ("odd") - 1) == 0)
4186 parity
= UART_ODD_PARITY
;
4187 else if (grub_memcmp (p
, "even", sizeof ("even") - 1) == 0)
4188 parity
= UART_EVEN_PARITY
;
4191 errnum
= ERR_BAD_ARGUMENT
;
4196 /* In the grub shell, don't use any port number but open a tty
4198 else if (grub_memcmp (arg
, "--device=", sizeof ("--device=") - 1) == 0)
4200 char *p
= arg
+ sizeof ("--device=") - 1;
4201 char dev
[256]; /* XXX */
4204 while (*p
&& ! grub_isspace (*p
))
4208 serial_set_device (dev
);
4210 # endif /* GRUB_UTIL */
4214 arg
= skip_to (0, arg
);
4217 if (units
[0] == -1 && ports
[0] == 0)
4220 for (i
= 0; i
< SERIAL_MAX_PORTS
; ++i
)
4223 ports
[i
] = serial_hw_get_port (units
[i
]);
4227 if (serial_hw_init (ports
[i
], speed
, word_len
, parity
, stop_bit_len
))
4231 if (i
>= SERIAL_MAX_PORTS
)
4233 errnum
= ERR_BAD_ARGUMENT
;
4240 static struct builtin builtin_serial
=
4244 BUILTIN_MENU
| BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
4245 "serial [[--unit=UNIT[,UNIT...]] | [--port=PORT[,PORT...]]] [--speed=SPEED] [--word=WORD] [--parity=PARITY] [--stop=STOP] [--device=DEV]",
4246 "Initialize a serial device. UNIT is a digit that specifies which serial"
4247 " device is used (e.g. 0 == ttya (aka COM1)). If you need to specify the port"
4248 " number, set it by --port. Either but not both of --unit and --port is"
4249 " permitted; --unit takes precedence. Multiple devices may be specified,"
4250 " separated by commas; the first working device in the list will be used"
4251 " and the rest ignored. SPEED is the DTE-DTE speed. WORD is the word length,"
4252 " PARITY is the type of parity, which is one of `no', `odd' and `even'."
4253 " STOP is the length of stop bit(s). The option --device can be used only"
4254 " in the grub shell, which specifies the file name of a tty device. The"
4255 " default values are ttya, 9600, 8N1."
4257 #endif /* SUPPORT_SERIAL */
4263 char *unshifted_name
; /* the name in unshifted state */
4264 char *shifted_name
; /* the name in shifted state */
4265 unsigned char unshifted_ascii
; /* the ascii code in unshifted state */
4266 unsigned char shifted_ascii
; /* the ascii code in shifted state */
4267 unsigned char keycode
; /* keyboard scancode */
4270 /* The table for key symbols. If the "shifted" member of an entry is
4271 NULL, the entry does not have shifted state. */
4272 static struct keysym keysym_table
[] =
4274 {"escape", 0, 0x1b, 0, 0x01},
4275 {"1", "exclam", '1', '!', 0x02},
4276 {"2", "at", '2', '@', 0x03},
4277 {"3", "numbersign", '3', '#', 0x04},
4278 {"4", "dollar", '4', '$', 0x05},
4279 {"5", "percent", '5', '%', 0x06},
4280 {"6", "caret", '6', '^', 0x07},
4281 {"7", "ampersand", '7', '&', 0x08},
4282 {"8", "asterisk", '8', '*', 0x09},
4283 {"9", "parenleft", '9', '(', 0x0a},
4284 {"0", "parenright", '0', ')', 0x0b},
4285 {"minus", "underscore", '-', '_', 0x0c},
4286 {"equal", "plus", '=', '+', 0x0d},
4287 {"backspace", 0, '\b', 0, 0x0e},
4288 {"tab", 0, '\t', 0, 0x0f},
4289 {"q", "Q", 'q', 'Q', 0x10},
4290 {"w", "W", 'w', 'W', 0x11},
4291 {"e", "E", 'e', 'E', 0x12},
4292 {"r", "R", 'r', 'R', 0x13},
4293 {"t", "T", 't', 'T', 0x14},
4294 {"y", "Y", 'y', 'Y', 0x15},
4295 {"u", "U", 'u', 'U', 0x16},
4296 {"i", "I", 'i', 'I', 0x17},
4297 {"o", "O", 'o', 'O', 0x18},
4298 {"p", "P", 'p', 'P', 0x19},
4299 {"bracketleft", "braceleft", '[', '{', 0x1a},
4300 {"bracketright", "braceright", ']', '}', 0x1b},
4301 {"enter", 0, '\n', 0, 0x1c},
4302 {"control", 0, 0, 0, 0x1d},
4303 {"a", "A", 'a', 'A', 0x1e},
4304 {"s", "S", 's', 'S', 0x1f},
4305 {"d", "D", 'd', 'D', 0x20},
4306 {"f", "F", 'f', 'F', 0x21},
4307 {"g", "G", 'g', 'G', 0x22},
4308 {"h", "H", 'h', 'H', 0x23},
4309 {"j", "J", 'j', 'J', 0x24},
4310 {"k", "K", 'k', 'K', 0x25},
4311 {"l", "L", 'l', 'L', 0x26},
4312 {"semicolon", "colon", ';', ':', 0x27},
4313 {"quote", "doublequote", '\'', '"', 0x28},
4314 {"backquote", "tilde", '`', '~', 0x29},
4315 {"shift", 0, 0, 0, 0x2a},
4316 {"backslash", "bar", '\\', '|', 0x2b},
4317 {"z", "Z", 'z', 'Z', 0x2c},
4318 {"x", "X", 'x', 'X', 0x2d},
4319 {"c", "C", 'c', 'C', 0x2e},
4320 {"v", "V", 'v', 'V', 0x2f},
4321 {"b", "B", 'b', 'B', 0x30},
4322 {"n", "N", 'n', 'N', 0x31},
4323 {"m", "M", 'm', 'M', 0x32},
4324 {"comma", "less", ',', '<', 0x33},
4325 {"period", "greater", '.', '>', 0x34},
4326 {"slash", "question", '/', '?', 0x35},
4327 {"alt", 0, 0, 0, 0x38},
4328 {"space", 0, ' ', 0, 0x39},
4329 {"capslock", 0, 0, 0, 0x3a},
4330 {"F1", 0, 0, 0, 0x3b},
4331 {"F2", 0, 0, 0, 0x3c},
4332 {"F3", 0, 0, 0, 0x3d},
4333 {"F4", 0, 0, 0, 0x3e},
4334 {"F5", 0, 0, 0, 0x3f},
4335 {"F6", 0, 0, 0, 0x40},
4336 {"F7", 0, 0, 0, 0x41},
4337 {"F8", 0, 0, 0, 0x42},
4338 {"F9", 0, 0, 0, 0x43},
4339 {"F10", 0, 0, 0, 0x44},
4340 /* Caution: do not add NumLock here! we cannot deal with it properly. */
4341 {"delete", 0, 0x7f, 0, 0x53}
4345 setkey_func (char *arg
, int flags
)
4347 char *to_key
, *from_key
;
4348 int to_code
, from_code
;
4349 int map_in_interrupt
= 0;
4351 auto int find_key_code (char *key
);
4352 auto int find_ascii_code (char *key
);
4354 auto int find_key_code (char *key
)
4358 for (i
= 0; i
< sizeof (keysym_table
) / sizeof (keysym_table
[0]); i
++)
4360 if (keysym_table
[i
].unshifted_name
&&
4361 grub_strcmp (key
, keysym_table
[i
].unshifted_name
) == 0)
4362 return keysym_table
[i
].keycode
;
4363 else if (keysym_table
[i
].shifted_name
&&
4364 grub_strcmp (key
, keysym_table
[i
].shifted_name
) == 0)
4365 return keysym_table
[i
].keycode
;
4371 auto int find_ascii_code (char *key
)
4375 for (i
= 0; i
< sizeof (keysym_table
) / sizeof (keysym_table
[0]); i
++)
4377 if (keysym_table
[i
].unshifted_name
&&
4378 grub_strcmp (key
, keysym_table
[i
].unshifted_name
) == 0)
4379 return keysym_table
[i
].unshifted_ascii
;
4380 else if (keysym_table
[i
].shifted_name
&&
4381 grub_strcmp (key
, keysym_table
[i
].shifted_name
) == 0)
4382 return keysym_table
[i
].shifted_ascii
;
4389 from_key
= skip_to (0, to_key
);
4393 /* If the user specifies no argument, reset the key mappings. */
4394 grub_memset (bios_key_map
, 0, KEY_MAP_SIZE
* sizeof (unsigned short));
4395 grub_memset (ascii_key_map
, 0, KEY_MAP_SIZE
* sizeof (unsigned short));
4399 else if (! *from_key
)
4401 /* The user must specify two arguments or zero argument. */
4402 errnum
= ERR_BAD_ARGUMENT
;
4406 nul_terminate (to_key
);
4407 nul_terminate (from_key
);
4409 to_code
= find_ascii_code (to_key
);
4410 from_code
= find_ascii_code (from_key
);
4411 if (! to_code
|| ! from_code
)
4413 map_in_interrupt
= 1;
4414 to_code
= find_key_code (to_key
);
4415 from_code
= find_key_code (from_key
);
4416 if (! to_code
|| ! from_code
)
4418 errnum
= ERR_BAD_ARGUMENT
;
4423 if (map_in_interrupt
)
4427 /* Find an empty slot. */
4428 for (i
= 0; i
< KEY_MAP_SIZE
; i
++)
4430 if ((bios_key_map
[i
] & 0xff) == from_code
)
4431 /* Perhaps the user wants to overwrite the map. */
4434 if (! bios_key_map
[i
])
4438 if (i
== KEY_MAP_SIZE
)
4440 errnum
= ERR_WONT_FIT
;
4444 if (to_code
== from_code
)
4445 /* If TO is equal to FROM, delete the entry. */
4446 grub_memmove ((char *) &bios_key_map
[i
],
4447 (char *) &bios_key_map
[i
+ 1],
4448 sizeof (unsigned short) * (KEY_MAP_SIZE
- i
));
4450 bios_key_map
[i
] = (to_code
<< 8) | from_code
;
4452 /* Ugly but should work. */
4453 unset_int15_handler ();
4454 set_int15_handler ();
4460 /* Find an empty slot. */
4461 for (i
= 0; i
< KEY_MAP_SIZE
; i
++)
4463 if ((ascii_key_map
[i
] & 0xff) == from_code
)
4464 /* Perhaps the user wants to overwrite the map. */
4467 if (! ascii_key_map
[i
])
4471 if (i
== KEY_MAP_SIZE
)
4473 errnum
= ERR_WONT_FIT
;
4477 if (to_code
== from_code
)
4478 /* If TO is equal to FROM, delete the entry. */
4479 grub_memmove ((char *) &ascii_key_map
[i
],
4480 (char *) &ascii_key_map
[i
+ 1],
4481 sizeof (unsigned short) * (KEY_MAP_SIZE
- i
));
4483 ascii_key_map
[i
] = (to_code
<< 8) | from_code
;
4489 static struct builtin builtin_setkey
=
4493 BUILTIN_CMDLINE
| BUILTIN_MENU
| BUILTIN_HELP_LIST
,
4494 "setkey [TO_KEY FROM_KEY]",
4495 "Change the keyboard map. The key FROM_KEY is mapped to the key TO_KEY."
4496 " A key must be an alphabet, a digit, or one of these: escape, exclam,"
4497 " at, numbersign, dollar, percent, caret, ampersand, asterisk, parenleft,"
4498 " parenright, minus, underscore, equal, plus, backspace, tab, bracketleft,"
4499 " braceleft, bracketright, braceright, enter, control, semicolon, colon,"
4500 " quote, doublequote, backquote, tilde, shift, backslash, bar, comma,"
4501 " less, period, greater, slash, question, alt, space, capslock, FX (X"
4502 " is a digit), and delete. If no argument is specified, reset key"
4509 setup_func (char *arg
, int flags
)
4511 /* Point to the string of the installed drive/partition. */
4513 /* Point to the string of the drive/parition where the GRUB images
4516 unsigned long installed_drive
, installed_partition
;
4517 unsigned long image_drive
, image_partition
;
4518 unsigned long tmp_drive
, tmp_partition
;
4521 char config_filename
[64];
4522 char real_config_filename
[64];
4525 char *buffer
= (char *) RAW_ADDR (0x100000);
4526 int is_force_lba
= 0;
4527 char *stage2_arg
= 0;
4530 auto int check_file (char *file
);
4531 auto void sprint_device (int drive
, int partition
);
4532 auto int embed_stage1_5 (char * stage1_5
, int drive
, int partition
);
4534 /* Check if the file FILE exists like Autoconf. */
4535 int check_file (char *file
)
4539 grub_printf (" Checking if \"%s\" exists... ", file
);
4540 ret
= grub_open (file
);
4544 grub_printf ("yes\n");
4547 grub_printf ("no\n");
4552 /* Construct a device name in DEVICE. */
4553 void sprint_device (int drive
, int partition
)
4555 grub_sprintf (device
, "(%cd%d",
4556 (drive
& 0x80) ? 'h' : 'f',
4558 if ((partition
& 0xFF0000) != 0xFF0000)
4561 grub_sprintf (tmp
, ",%d", (partition
>> 16) & 0xFF);
4562 grub_strncat (device
, tmp
, sizeof (device
));
4564 if ((partition
& 0x00FF00) != 0x00FF00)
4567 grub_sprintf (tmp
, ",%c", 'a' + ((partition
>> 8) & 0xFF));
4568 grub_strncat (device
, tmp
, sizeof (device
));
4570 grub_strncat (device
, ")", sizeof (device
));
4573 int embed_stage1_5 (char *stage1_5
, int drive
, int partition
)
4575 /* We install GRUB into the MBR, so try to embed the
4576 Stage 1.5 in the sectors right after the MBR. */
4577 sprint_device (drive
, partition
);
4578 grub_sprintf (cmd_arg
, "%s %s", stage1_5
, device
);
4580 /* Notify what will be run. */
4581 grub_printf (" Running \"embed %s\"... ", cmd_arg
);
4583 embed_func (cmd_arg
, flags
);
4586 /* Construct the blocklist representation. */
4587 grub_sprintf (buffer
, "%s%s", device
, embed_info
);
4588 grub_printf ("succeeded\n");
4593 grub_printf ("failed (this is not fatal)\n");
4598 struct stage1_5_map
{
4602 struct stage1_5_map stage1_5_map
[] =
4604 {"ext2fs", "/e2fs_stage1_5"},
4605 {"fat", "/fat_stage1_5"},
4606 {"ufs2", "/ufs2_stage1_5"},
4607 {"ffs", "/ffs_stage1_5"},
4608 {"iso9660", "/iso9660_stage1_5"},
4609 {"jfs", "/jfs_stage1_5"},
4610 {"minix", "/minix_stage1_5"},
4611 {"reiserfs", "/reiserfs_stage1_5"},
4612 {"vstafs", "/vstafs_stage1_5"},
4613 {"xfs", "/xfs_stage1_5"},
4614 {"ufs", "/ufs_stage1_5"}
4617 tmp_drive
= saved_drive
;
4618 tmp_partition
= saved_partition
;
4620 /* Check if the user specifies --force-lba. */
4623 if (grub_memcmp ("--force-lba", arg
, sizeof ("--force-lba") - 1) == 0)
4626 arg
= skip_to (0, arg
);
4628 else if (grub_memcmp ("--prefix=", arg
, sizeof ("--prefix=") - 1) == 0)
4630 prefix
= arg
+ sizeof ("--prefix=") - 1;
4631 arg
= skip_to (0, arg
);
4632 nul_terminate (prefix
);
4635 else if (grub_memcmp ("--stage2=", arg
, sizeof ("--stage2=") - 1) == 0)
4638 arg
= skip_to (0, arg
);
4639 nul_terminate (stage2_arg
);
4641 #endif /* GRUB_UTIL */
4647 image_ptr
= skip_to (0, install_ptr
);
4649 /* Make sure that INSTALL_PTR is valid. */
4650 set_device (install_ptr
);
4654 installed_drive
= current_drive
;
4655 installed_partition
= current_partition
;
4657 /* Mount the drive pointed by IMAGE_PTR. */
4660 /* If the drive/partition where the images reside is specified,
4661 get the drive and the partition. */
4662 set_device (image_ptr
);
4668 /* If omitted, use SAVED_PARTITION and SAVED_DRIVE. */
4669 current_drive
= saved_drive
;
4670 current_partition
= saved_partition
;
4673 image_drive
= saved_drive
= current_drive
;
4674 image_partition
= saved_partition
= current_partition
;
4677 if (! open_device ())
4680 /* Check if stage1 exists. If the user doesn't specify the option
4681 `--prefix', attempt /boot/grub and /grub. */
4682 /* NOTE: It is dangerous to run this command without `--prefix' in the
4683 grub shell, since that affects `--stage2'. */
4686 prefix
= "/boot/grub";
4687 grub_sprintf (stage1
, "%s%s", prefix
, "/stage1");
4688 if (! check_file (stage1
))
4692 grub_sprintf (stage1
, "%s%s", prefix
, "/stage1");
4693 if (! check_file (stage1
))
4699 grub_sprintf (stage1
, "%s%s", prefix
, "/stage1");
4700 if (! check_file (stage1
))
4704 /* The prefix was determined. */
4705 grub_sprintf (stage2
, "%s%s", prefix
, "/stage2");
4706 grub_sprintf (config_filename
, "%s%s", prefix
, "/menu.lst");
4707 *real_config_filename
= 0;
4709 /* Check if stage2 exists. */
4710 if (! check_file (stage2
))
4714 char *fsys
= fsys_table
[fsys_type
].name
;
4716 int size
= sizeof (stage1_5_map
) / sizeof (stage1_5_map
[0]);
4718 /* Iterate finding the same filesystem name as FSYS. */
4719 for (i
= 0; i
< size
; i
++)
4720 if (grub_strcmp (fsys
, stage1_5_map
[i
].fsys
) == 0)
4722 /* OK, check if the Stage 1.5 exists. */
4725 grub_sprintf (stage1_5
, "%s%s", prefix
, stage1_5_map
[i
].name
);
4726 if (check_file (stage1_5
))
4728 if (embed_stage1_5 (stage1_5
,
4729 installed_drive
, installed_partition
)
4730 || embed_stage1_5 (stage1_5
,
4731 image_drive
, image_partition
))
4733 grub_strcpy (real_config_filename
, config_filename
);
4734 sprint_device (image_drive
, image_partition
);
4735 grub_sprintf (config_filename
, "%s%s", device
, stage2
);
4736 grub_strcpy (stage2
, buffer
);
4744 /* Construct a string that is used by the command "install" as its
4746 sprint_device (installed_drive
, installed_partition
);
4749 /* Don't embed a drive number unnecessarily. */
4750 grub_sprintf (cmd_arg
, "%s%s%s%s %s%s %s p %s %s",
4751 is_force_lba
? "--force-lba " : "",
4752 stage2_arg
? stage2_arg
: "",
4753 stage2_arg
? " " : "",
4755 (installed_drive
!= image_drive
) ? "d " : "",
4759 real_config_filename
);
4760 #else /* NOT USED */
4761 /* This code was used, because we belived some BIOSes had a problem
4762 that they didn't pass a booting drive correctly. It turned out,
4763 however, stage1 could trash a booting drive when checking LBA support,
4764 because some BIOSes modified the register %dx in INT 13H, AH=48H.
4765 So it becamed unclear whether GRUB should use a pre-defined booting
4766 drive or not. If the problem still exists, it would be necessary to
4767 switch back to this code. */
4768 grub_sprintf (cmd_arg
, "%s%s%s%s d %s %s p %s %s",
4769 is_force_lba
? "--force-lba " : "",
4770 stage2_arg
? stage2_arg
: "",
4771 stage2_arg
? " " : "",
4776 real_config_filename
);
4777 #endif /* NOT USED */
4779 /* Notify what will be run. */
4780 grub_printf (" Running \"install %s\"... ", cmd_arg
);
4782 /* Make sure that SAVED_DRIVE and SAVED_PARTITION are identical
4783 with IMAGE_DRIVE and IMAGE_PARTITION, respectively. */
4784 saved_drive
= image_drive
;
4785 saved_partition
= image_partition
;
4787 /* Run the command. */
4788 if (! install_func (cmd_arg
, flags
))
4789 grub_printf ("succeeded\nDone.\n");
4791 grub_printf ("failed\n");
4794 saved_drive
= tmp_drive
;
4795 saved_partition
= tmp_partition
;
4799 static struct builtin builtin_setup
=
4803 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
4804 "setup [--prefix=DIR] [--stage2=STAGE2_FILE] [--force-lba] INSTALL_DEVICE [IMAGE_DEVICE]",
4805 "Set up the installation of GRUB automatically. This command uses"
4806 " the more flexible command \"install\" in the backend and installs"
4807 " GRUB into the device INSTALL_DEVICE. If IMAGE_DEVICE is specified,"
4808 " then find the GRUB images in the device IMAGE_DEVICE, otherwise"
4809 " use the current \"root device\", which can be set by the command"
4810 " \"root\". If you know that your BIOS should support LBA but GRUB"
4811 " doesn't work in LBA mode, specify the option `--force-lba'."
4812 " If you install GRUB under the grub shell and you cannot unmount the"
4813 " partition where GRUB images reside, specify the option `--stage2'"
4814 " to tell GRUB the file name under your OS."
4818 #if defined(SUPPORT_SERIAL) || defined(SUPPORT_HERCULES) || defined(SUPPORT_GRAPHICS)
4821 terminal_func (char *arg
, int flags
)
4823 /* The index of the default terminal in TERM_TABLE. */
4824 int default_term
= -1;
4825 struct term_entry
*prev_term
= current_term
;
4829 unsigned long term_flags
= 0;
4830 /* XXX: Assume less than 32 terminals. */
4831 unsigned long term_bitmap
= 0;
4833 /* Get GNU-style long options. */
4836 if (grub_memcmp (arg
, "--dumb", sizeof ("--dumb") - 1) == 0)
4837 term_flags
|= TERM_DUMB
;
4838 else if (grub_memcmp (arg
, "--no-echo", sizeof ("--no-echo") - 1) == 0)
4839 /* ``--no-echo'' implies ``--no-edit''. */
4840 term_flags
|= (TERM_NO_ECHO
| TERM_NO_EDIT
);
4841 else if (grub_memcmp (arg
, "--no-edit", sizeof ("--no-edit") - 1) == 0)
4842 term_flags
|= TERM_NO_EDIT
;
4843 else if (grub_memcmp (arg
, "--timeout=", sizeof ("--timeout=") - 1) == 0)
4845 char *val
= arg
+ sizeof ("--timeout=") - 1;
4847 if (! safe_parse_maxint (&val
, &to
))
4850 else if (grub_memcmp (arg
, "--lines=", sizeof ("--lines=") - 1) == 0)
4852 char *val
= arg
+ sizeof ("--lines=") - 1;
4854 if (! safe_parse_maxint (&val
, &lines
))
4857 /* Probably less than four is meaningless.... */
4860 errnum
= ERR_BAD_ARGUMENT
;
4864 else if (grub_memcmp (arg
, "--silent", sizeof ("--silent") - 1) == 0)
4869 arg
= skip_to (0, arg
);
4872 /* If no argument is specified, show current setting. */
4875 grub_printf ("%s%s%s%s\n",
4877 current_term
->flags
& TERM_DUMB
? " (dumb)" : "",
4878 current_term
->flags
& TERM_NO_EDIT
? " (no edit)" : "",
4879 current_term
->flags
& TERM_NO_ECHO
? " (no echo)" : "");
4886 char *next
= skip_to (0, arg
);
4888 nul_terminate (arg
);
4890 for (i
= 0; term_table
[i
].name
; i
++)
4892 if (grub_strcmp (arg
, term_table
[i
].name
) == 0)
4894 if (term_table
[i
].flags
& TERM_NEED_INIT
)
4896 errnum
= ERR_DEV_NEED_INIT
;
4900 if (default_term
< 0)
4903 term_bitmap
|= (1 << i
);
4908 if (! term_table
[i
].name
)
4910 errnum
= ERR_BAD_ARGUMENT
;
4917 /* If multiple terminals are specified, wait until the user pushes any
4918 key on one of the terminals. */
4919 if (term_bitmap
& ~(1 << default_term
))
4921 int time1
, time2
= -1;
4923 /* XXX: Disable the pager. */
4926 /* Get current time. */
4927 while ((time1
= getrtsecs ()) == 0xFF)
4930 /* Wait for a key input. */
4935 for (i
= 0; term_table
[i
].name
; i
++)
4937 if (term_bitmap
& (1 << i
))
4939 if (term_table
[i
].checkkey () >= 0)
4941 (void) term_table
[i
].getkey ();
4949 /* Prompt the user, once per sec. */
4950 if ((time1
= getrtsecs ()) != time2
&& time1
!= 0xFF)
4954 /* Need to set CURRENT_TERM to each of selected
4956 for (i
= 0; term_table
[i
].name
; i
++)
4957 if (term_bitmap
& (1 << i
))
4959 current_term
= term_table
+ i
;
4960 grub_printf ("\rPress any key to continue.\n");
4963 /* Restore CURRENT_TERM. */
4964 current_term
= prev_term
;
4975 current_term
= term_table
+ default_term
;
4976 current_term
->flags
= term_flags
;
4981 max_lines
= current_term
->max_lines
;
4983 /* If the interface is currently the command-line,
4984 restart it to repaint the screen. */
4985 if ((current_term
!= prev_term
) && (flags
& BUILTIN_CMDLINE
)){
4986 if (prev_term
->shutdown
)
4987 prev_term
->shutdown();
4988 if (current_term
->startup
)
4989 current_term
->startup();
4990 grub_longjmp (restart_cmdline_env
, 0);
4996 static struct builtin builtin_terminal
=
5000 BUILTIN_MENU
| BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
5001 "terminal [--dumb] [--no-echo] [--no-edit] [--timeout=SECS] [--lines=LINES] [--silent] [console] [serial] [hercules] [graphics] [composite]",
5002 "Select a terminal. When multiple terminals are specified, wait until"
5003 " you push any key to continue. If both console and serial are specified,"
5004 " the terminal to which you input a key first will be selected. If no"
5005 " argument is specified, print current setting. To accomodate systems"
5006 " where console redirection may or may not be present, the composite"
5007 " console will direct output to the serial and BIOS consoles, and accept"
5008 " input from either one, without requiring selection. The option --dumb"
5009 " specifies that your terminal is dumb, otherwise, vt100-compatibility"
5010 " is assumed. If you specify --no-echo, input characters won't be echoed."
5011 " If you specify --no-edit, the BASH-like editing feature will be disabled."
5012 " If --timeout is present, this command will wait at most for SECS"
5013 " seconds. The option --lines specifies the maximum number of lines."
5014 " The option --silent is used to suppress messages."
5016 #endif /* SUPPORT_SERIAL || SUPPORT_HERCULES || SUPPORT_GRAPHICS */
5019 #ifdef SUPPORT_SERIAL
5021 terminfo_func (char *arg
, int flags
)
5023 struct terminfo term
;
5034 {"--name=", term
.name
},
5035 {"--cursor-address=", term
.cursor_address
},
5036 {"--clear-screen=", term
.clear_screen
},
5037 {"--enter-standout-mode=", term
.enter_standout_mode
},
5038 {"--exit-standout-mode=", term
.exit_standout_mode
}
5041 grub_memset (&term
, 0, sizeof (term
));
5046 char *next
= skip_to (0, arg
);
5048 nul_terminate (arg
);
5050 for (i
= 0; i
< sizeof (options
) / sizeof (options
[0]); i
++)
5052 const char *name
= options
[i
].name
;
5053 int len
= grub_strlen (name
);
5055 if (! grub_memcmp (arg
, name
, len
))
5057 grub_strcpy (options
[i
].var
, ti_unescape_string (arg
+ len
));
5062 if (i
== sizeof (options
) / sizeof (options
[0]))
5064 errnum
= ERR_BAD_ARGUMENT
;
5071 if (term
.name
[0] == 0 || term
.cursor_address
[0] == 0)
5073 errnum
= ERR_BAD_ARGUMENT
;
5077 ti_set_term (&term
);
5081 /* No option specifies printing out current settings. */
5082 ti_get_term (&term
);
5084 grub_printf ("name=%s\n",
5085 ti_escape_string (term
.name
));
5086 grub_printf ("cursor_address=%s\n",
5087 ti_escape_string (term
.cursor_address
));
5088 grub_printf ("clear_screen=%s\n",
5089 ti_escape_string (term
.clear_screen
));
5090 grub_printf ("enter_standout_mode=%s\n",
5091 ti_escape_string (term
.enter_standout_mode
));
5092 grub_printf ("exit_standout_mode=%s\n",
5093 ti_escape_string (term
.exit_standout_mode
));
5099 static struct builtin builtin_terminfo
=
5103 BUILTIN_MENU
| BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
5104 "terminfo [--name=NAME --cursor-address=SEQ [--clear-screen=SEQ]"
5105 " [--enter-standout-mode=SEQ] [--exit-standout-mode=SEQ]]",
5107 "Define the capabilities of your terminal. Use this command to"
5108 " define escape sequences, if it is not vt100-compatible."
5109 " You may use \\e for ESC and ^X for a control character."
5110 " If no option is specified, the current settings are printed."
5112 #endif /* SUPPORT_SERIAL */
5117 testload_func (char *arg
, int flags
)
5121 kernel_type
= KERNEL_TYPE_NONE
;
5123 if (! grub_open (arg
))
5126 disk_read_hook
= disk_read_print_func
;
5128 /* Perform filesystem test on the specified file. */
5129 /* Read whole file first. */
5130 grub_printf ("Whole file: ");
5132 grub_read ((char *) RAW_ADDR (0x100000), -1);
5134 /* Now compare two sections of the file read differently. */
5136 for (i
= 0; i
< 0x10ac0; i
++)
5138 *((unsigned char *) RAW_ADDR (0x200000 + i
)) = 0;
5139 *((unsigned char *) RAW_ADDR (0x300000 + i
)) = 1;
5142 /* First partial read. */
5143 grub_printf ("\nPartial read 1: ");
5146 grub_read ((char *) RAW_ADDR (0x200000), 0x7);
5147 grub_read ((char *) RAW_ADDR (0x200007), 0x100);
5148 grub_read ((char *) RAW_ADDR (0x200107), 0x10);
5149 grub_read ((char *) RAW_ADDR (0x200117), 0x999);
5150 grub_read ((char *) RAW_ADDR (0x200ab0), 0x10);
5151 grub_read ((char *) RAW_ADDR (0x200ac0), 0x10000);
5153 /* Second partial read. */
5154 grub_printf ("\nPartial read 2: ");
5157 grub_read ((char *) RAW_ADDR (0x300000), 0x10000);
5158 grub_read ((char *) RAW_ADDR (0x310000), 0x10);
5159 grub_read ((char *) RAW_ADDR (0x310010), 0x7);
5160 grub_read ((char *) RAW_ADDR (0x310017), 0x10);
5161 grub_read ((char *) RAW_ADDR (0x310027), 0x999);
5162 grub_read ((char *) RAW_ADDR (0x3109c0), 0x100);
5164 grub_printf ("\nHeader1 = 0x%x, next = 0x%x, next = 0x%x, next = 0x%x\n",
5165 *((int *) RAW_ADDR (0x200000)),
5166 *((int *) RAW_ADDR (0x200004)),
5167 *((int *) RAW_ADDR (0x200008)),
5168 *((int *) RAW_ADDR (0x20000c)));
5170 grub_printf ("Header2 = 0x%x, next = 0x%x, next = 0x%x, next = 0x%x\n",
5171 *((int *) RAW_ADDR (0x300000)),
5172 *((int *) RAW_ADDR (0x300004)),
5173 *((int *) RAW_ADDR (0x300008)),
5174 *((int *) RAW_ADDR (0x30000c)));
5176 for (i
= 0; i
< 0x10ac0; i
++)
5177 if (*((unsigned char *) RAW_ADDR (0x200000 + i
))
5178 != *((unsigned char *) RAW_ADDR (0x300000 + i
)))
5181 grub_printf ("Max is 0x10ac0: i=0x%x, filepos=0x%x\n", i
, filepos
);
5187 static struct builtin builtin_testload
=
5193 "Read the entire contents of FILE in several different ways and"
5194 " compares them, to test the filesystem code. The output is somewhat"
5195 " cryptic, but if no errors are reported and the final `i=X,"
5196 " filepos=Y' reading has X and Y equal, then it is definitely"
5197 " consistent, and very likely works correctly subject to a"
5198 " consistent offset error. If this test succeeds, then a good next"
5199 " step is to try loading a kernel."
5205 testvbe_func (char *arg
, int flags
)
5208 struct vbe_controller controller
;
5209 struct vbe_mode mode
;
5213 errnum
= ERR_BAD_ARGUMENT
;
5217 if (! safe_parse_maxint (&arg
, &mode_number
))
5220 /* Preset `VBE2'. */
5221 grub_memmove (controller
.signature
, "VBE2", 4);
5223 /* Detect VBE BIOS. */
5224 if (get_vbe_controller_info (&controller
) != 0x004F)
5226 grub_printf (" VBE BIOS is not present.\n");
5230 if (controller
.version
< 0x0200)
5232 grub_printf (" VBE version %d.%d is not supported.\n",
5233 (int) (controller
.version
>> 8),
5234 (int) (controller
.version
& 0xFF));
5238 if (get_vbe_mode_info (mode_number
, &mode
) != 0x004F
5239 || (mode
.mode_attributes
& 0x0091) != 0x0091)
5241 grub_printf (" Mode 0x%x is not supported.\n", mode_number
);
5245 /* Now trip to the graphics mode. */
5246 if (set_vbe_mode (mode_number
| (1 << 14)) != 0x004F)
5248 grub_printf (" Switching to Mode 0x%x failed.\n", mode_number
);
5252 /* Draw something on the screen... */
5254 unsigned char *base_buf
= (unsigned char *) mode
.phys_base
;
5255 int scanline
= controller
.version
>= 0x0300
5256 ? mode
.linear_bytes_per_scanline
: mode
.bytes_per_scanline
;
5257 /* FIXME: this assumes that any depth is a modulo of 8. */
5258 int bpp
= mode
.bits_per_pixel
/ 8;
5259 int width
= mode
.x_resolution
;
5260 int height
= mode
.y_resolution
;
5264 /* Iterate drawing on the screen, until the user hits any key. */
5265 while (checkkey () == -1)
5267 for (y
= 0; y
< height
; y
++)
5269 unsigned char *line_buf
= base_buf
+ scanline
* y
;
5271 for (x
= 0; x
< width
; x
++)
5273 unsigned char *buf
= line_buf
+ bpp
* x
;
5276 for (i
= 0; i
< bpp
; i
++, buf
++)
5277 *buf
= (color
>> (i
* 8)) & 0xff;
5284 /* Discard the input. */
5288 /* Back to the default text mode. */
5289 if (set_vbe_mode (0x03) != 0x004F)
5298 static struct builtin builtin_testvbe
=
5302 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
5304 "Test the VBE mode MODE. Hit any key to return."
5308 #ifdef SUPPORT_NETBOOT
5311 tftpserver_func (char *arg
, int flags
)
5313 if (! *arg
|| ! ifconfig (0, 0, 0, arg
))
5315 errnum
= ERR_BAD_ARGUMENT
;
5319 print_network_configuration ();
5323 static struct builtin builtin_tftpserver
=
5327 BUILTIN_CMDLINE
| BUILTIN_MENU
| BUILTIN_HELP_LIST
,
5328 "tftpserver IPADDR",
5329 "Override the TFTP server address."
5331 #endif /* SUPPORT_NETBOOT */
5336 timeout_func (char *arg
, int flags
)
5338 if (! safe_parse_maxint (&arg
, &grub_timeout
))
5344 static struct builtin builtin_timeout
=
5351 "Set a timeout, in SEC seconds, before automatically booting the"
5352 " default entry (normally the first entry defined)."
5359 title_func (char *arg
, int flags
)
5361 /* This function is not actually used at least currently. */
5365 static struct builtin builtin_title
=
5372 "Start a new boot entry, and set its name to the contents of the"
5373 " rest of the line, starting with the first non-space character."
5380 unhide_func (char *arg
, int flags
)
5382 if (! set_device (arg
))
5385 if (! set_partition_hidden_flag (0))
5391 static struct builtin builtin_unhide
=
5395 BUILTIN_CMDLINE
| BUILTIN_MENU
| BUILTIN_HELP_LIST
,
5397 "Unhide PARTITION by clearing the \"hidden\" bit in its"
5398 " partition type code."
5404 uppermem_func (char *arg
, int flags
)
5406 if (! safe_parse_maxint (&arg
, (int *) &mbi
.mem_upper
))
5409 mbi
.flags
&= ~MB_INFO_MEM_MAP
;
5413 static struct builtin builtin_uppermem
=
5417 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
5419 "Force GRUB to assume that only KBYTES kilobytes of upper memory are"
5420 " installed. Any system address range maps are discarded."
5426 vbeprobe_func (char *arg
, int flags
)
5428 struct vbe_controller controller
;
5429 unsigned short *mode_list
;
5430 int mode_number
= -1;
5432 auto unsigned long vbe_far_ptr_to_linear (unsigned long);
5434 unsigned long vbe_far_ptr_to_linear (unsigned long ptr
)
5436 unsigned short seg
= (ptr
>> 16);
5437 unsigned short off
= (ptr
& 0xFFFF);
5439 return (seg
<< 4) + off
;
5444 if (! safe_parse_maxint (&arg
, &mode_number
))
5448 /* Set the signature to `VBE2', to obtain VBE 3.0 information. */
5449 grub_memmove (controller
.signature
, "VBE2", 4);
5451 if (get_vbe_controller_info (&controller
) != 0x004F)
5453 grub_printf (" VBE BIOS is not present.\n");
5457 /* Check the version. */
5458 if (controller
.version
< 0x0200)
5460 grub_printf (" VBE version %d.%d is not supported.\n",
5461 (int) (controller
.version
>> 8),
5462 (int) (controller
.version
& 0xFF));
5466 /* Print some information. */
5467 grub_printf (" VBE version %d.%d\n",
5468 (int) (controller
.version
>> 8),
5469 (int) (controller
.version
& 0xFF));
5471 /* Iterate probing modes. */
5473 = (unsigned short *) vbe_far_ptr_to_linear (controller
.video_mode
);
5474 *mode_list
!= 0xFFFF;
5477 struct vbe_mode mode
;
5479 if (get_vbe_mode_info (*mode_list
, &mode
) != 0x004F)
5482 /* Skip this, if this is not supported or linear frame buffer
5483 mode is not support. */
5484 if ((mode
.mode_attributes
& 0x0081) != 0x0081)
5487 if (mode_number
== -1 || mode_number
== *mode_list
)
5490 switch (mode
.memory_model
)
5492 case 0x00: model
= "Text"; break;
5493 case 0x01: model
= "CGA graphics"; break;
5494 case 0x02: model
= "Hercules graphics"; break;
5495 case 0x03: model
= "Planar"; break;
5496 case 0x04: model
= "Packed pixel"; break;
5497 case 0x05: model
= "Non-chain 4, 256 color"; break;
5498 case 0x06: model
= "Direct Color"; break;
5499 case 0x07: model
= "YUV"; break;
5500 default: model
= "Unknown"; break;
5503 grub_printf (" 0x%x: %s, %ux%ux%u\n",
5504 (unsigned) *mode_list
,
5506 (unsigned) mode
.x_resolution
,
5507 (unsigned) mode
.y_resolution
,
5508 (unsigned) mode
.bits_per_pixel
);
5510 if (mode_number
!= -1)
5515 if (mode_number
!= -1 && mode_number
!= *mode_list
)
5516 grub_printf (" Mode 0x%x is not found or supported.\n", mode_number
);
5521 static struct builtin builtin_vbeprobe
=
5525 BUILTIN_CMDLINE
| BUILTIN_HELP_LIST
,
5527 "Probe VBE information. If the mode number MODE is specified, show only"
5528 " the information about only the mode."
5532 variable_func(char *arg
, int flags
)
5534 char name
[EV_NAMELEN
];
5543 if ((val
= grub_strchr(arg
, ' ')) != NULL
) {
5544 if (val
- arg
>= sizeof (name
)) {
5545 errnum
= ERR_WONT_FIT
;
5548 (void) grub_memcpy(name
, arg
, (val
- arg
));
5549 name
[val
- arg
] = '\0';
5550 val
= skip_to(0, arg
);
5552 if (grub_strlen(arg
) >= sizeof (name
)) {
5553 errnum
= ERR_WONT_FIT
;
5556 (void) grub_strcpy(name
, arg
);
5559 if ((err
= set_variable(name
, val
)) != 0) {
5567 static struct builtin builtin_variable
=
5571 BUILTIN_CMDLINE
| BUILTIN_MENU
| BUILTIN_SCRIPT
| BUILTIN_HELP_LIST
,
5572 "variable NAME [VALUE]",
5573 "Set the variable NAME to VALUE, or to the empty string if no value is"
5574 " given. NAME must contain no spaces. There is no quoting mechanism"
5575 " and nested variable references are not allowed. Variable values may"
5576 " be substituted into the kernel$ and module$ commands using ${NAME}."
5580 /* The table of builtin commands. Sorted in dictionary order. */
5581 struct builtin
*builtin_table
[] =
5583 #ifdef SUPPORT_GRAPHICS
5584 &builtin_background
,
5589 #ifdef SUPPORT_NETBOOT
5591 #endif /* SUPPORT_NETBOOT */
5593 &builtin_chainloader
,
5597 &builtin_configfile
,
5602 #endif /* GRUB_UTIL */
5603 #ifdef SUPPORT_NETBOOT
5605 #endif /* SUPPORT_NETBOOT */
5606 &builtin_displayapm
,
5607 &builtin_displaymem
,
5610 #endif /* GRUB_UTIL */
5615 #ifdef SUPPORT_GRAPHICS
5616 &builtin_foreground
,
5622 &builtin_hiddenmenu
,
5624 #ifdef SUPPORT_NETBOOT
5626 #endif /* SUPPORT_NETBOOT */
5632 &builtin_kernel_dollar
,
5634 &builtin_makeactive
,
5636 #ifdef USE_MD5_PASSWORDS
5638 #endif /* USE_MD5_PASSWORDS */
5641 &builtin_module_dollar
,
5642 &builtin_modulenounzip
,
5648 #if defined(RPC_DEBUG) && defined(SUPPORT_NETBOOT)
5650 #endif /* RPC_DEBUG && SUPPORT_NETBOOT */
5653 #endif /* GRUB_UTIL */
5654 #ifdef SUPPORT_NETBOOT
5656 #endif /* SUPPORT_NETBOOT */
5660 &builtin_rootnoverify
,
5661 &builtin_savedefault
,
5662 #ifdef SUPPORT_SERIAL
5664 #endif /* SUPPORT_SERIAL */
5667 #ifdef SUPPORT_GRAPHICS
5668 &builtin_splashimage
,
5669 #endif /* SUPPORT_GRAPHICS */
5670 #if defined(SUPPORT_SERIAL) || defined(SUPPORT_HERCULES) || defined(SUPPORT_GRAPHICS)
5672 #endif /* SUPPORT_SERIAL || SUPPORT_HERCULES || SUPPORT_GRAPHICS */
5673 #ifdef SUPPORT_SERIAL
5675 #endif /* SUPPORT_SERIAL */
5678 #ifdef SUPPORT_NETBOOT
5679 &builtin_tftpserver
,
5680 #endif /* SUPPORT_NETBOOT */