]> git.proxmox.com Git - grub2.git/blame - util/grub-emu.c
2005-09-29 Yoshinori K. Okuji <okuji@enbug.org>
[grub2.git] / util / grub-emu.c
CommitLineData
1f7315a3 1/*
4b13b216 2 * GRUB -- GRand Unified Bootloader
0b412211 3 * Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
1f7315a3 4 *
4b13b216 5 * GRUB is free software; you can redistribute it and/or modify
1f7315a3 6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
4b13b216 16 * along with GRUB; if not, write to the Free Software
1f7315a3 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#include <stdlib.h>
21#include <malloc.h>
22#include <sys/stat.h>
23#include <argp.h>
24#include <string.h>
e6b92c8a 25#include <signal.h>
0a74e62f 26#include <sys/types.h>
27#include <unistd.h>
1f7315a3 28
4b13b216 29#include <grub/mm.h>
30#include <grub/setjmp.h>
31#include <grub/fs.h>
32#include <grub/i386/pc/util/biosdisk.h>
33#include <grub/dl.h>
8ceafda2 34#include <grub/machine/console.h>
4b13b216 35#include <grub/util/misc.h>
36#include <grub/kernel.h>
37#include <grub/normal.h>
38#include <grub/util/getroot.h>
39#include <grub/env.h>
3f1578fe 40#include <grub/partition.h>
1f7315a3 41
42#ifdef __NetBSD__
43/* NetBSD uses /boot for its boot block. */
4b13b216 44# define DEFAULT_DIRECTORY "/grub"
1f7315a3 45#else
4b13b216 46# define DEFAULT_DIRECTORY "/boot/grub"
1f7315a3 47#endif
48
49#define DEFAULT_DEVICE_MAP DEFAULT_DIRECTORY "/device.map"
50
e6b92c8a 51/* Used for going back to the main function. */
52jmp_buf main_env;
53
0b412211 54grub_addr_t
55grub_arch_modules_addr (void)
56{
57 return 0;
58}
1f7315a3 59
c642636f 60grub_err_t
61grub_arch_dl_check_header (void *ehdr)
1f7315a3 62{
63 (void) ehdr;
1f7315a3 64
4b13b216 65 return GRUB_ERR_BAD_MODULE;
1f7315a3 66}
67
4b13b216 68grub_err_t
69grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
1f7315a3 70{
71 (void) mod;
72 (void) ehdr;
73
4b13b216 74 return GRUB_ERR_BAD_MODULE;
1f7315a3 75}
76
77void
4b13b216 78grub_machine_init (void)
1f7315a3 79{
e6b92c8a 80 signal (SIGINT, SIG_IGN);
4b13b216 81 grub_console_init ();
1f7315a3 82}
e6b92c8a 83
84void
85grub_machine_fini (void)
86{
87 grub_console_fini ();
88}
1f7315a3 89\f
90
91const char *argp_program_version = PACKAGE_STRING;
92const char *argp_program_bug_address = PACKAGE_BUGREPORT;
4b13b216 93static char doc[] = "GRUB emulator";
1f7315a3 94
95static struct argp_option options[] = {
db1771cf 96 {"root-device", 'r', "DEV", 0, "use DEV as the root device [default=guessed]", 0},
97 {"device-map", 'm', "FILE", 0, "use FILE as the device map", 0},
4b13b216 98 {"directory", 'd', "DIR", 0, "use GRUB files in the directory DIR", 0},
db1771cf 99 {"verbose", 'v', 0 , 0, "print verbose messages", 0},
0a74e62f 100 {"hold", 'H', "SECONDS", OPTION_ARG_OPTIONAL, "wait until a debugger will attach", 0},
db1771cf 101 { 0, 0, 0, 0, 0, 0 }
1f7315a3 102};
103
104struct arguments
105{
106 char *root_dev;
107 char *dev_map;
108 char *dir;
0a74e62f 109 int hold;
1f7315a3 110};
111
112static error_t
113parse_opt (int key, char *arg, struct argp_state *state)
114{
115 struct arguments *args = state->input;
116
117 switch (key)
118 {
119 case 'r':
120 args->root_dev = arg;
121 break;
122 case 'd':
123 args->dir = arg;
124 break;
125 case 'm':
126 args->dev_map = arg;
127 break;
128 case 'v':
129 verbosity++;
130 break;
0a74e62f 131 case 'H':
132 args->hold = arg ? atoi (arg) : -1;
133 break;
1f7315a3 134 case ARGP_KEY_END:
135 break;
136 default:
137 return ARGP_ERR_UNKNOWN;
138 }
139 return 0;
140}
141
db1771cf 142static struct argp argp = {options, parse_opt, 0, doc, 0, 0, 0};
1f7315a3 143\f
144
145int
146main (int argc, char *argv[])
147{
148 char *prefix = 0;
149 char rootprefix[100];
150 struct arguments args =
151 {
152 .dir = DEFAULT_DIRECTORY,
0a74e62f 153 .dev_map = DEFAULT_DEVICE_MAP,
154 .hold = 0
1f7315a3 155 };
777aff39 156
157 progname = "grub-emu";
158
1f7315a3 159 argp_parse (&argp, argc, argv, 0, 0, &args);
160
0a74e62f 161 /* Wait until the ARGS.HOLD variable is cleared by an attached debugger. */
162 if (args.hold && verbosity > 0)
163 printf ("Run \"gdb %s %d\", and set ARGS.HOLD to zero.\n",
164 progname, (int) getpid ());
165 while (args.hold)
166 {
167 if (args.hold > 0)
168 args.hold--;
169
170 sleep (1);
171 }
172
e6b92c8a 173 /* Make sure that there is a root device. */
1f7315a3 174 if (! args.root_dev)
175 {
4b13b216 176 args.root_dev = grub_guess_root_device (args.dir ? : DEFAULT_DIRECTORY);
1f7315a3 177 if (! args.root_dev)
178 {
4b13b216 179 grub_util_info ("guessing the root device failed, because of `%s'",
180 grub_errmsg);
181 grub_util_error ("Cannot guess the root device. Specify the option ``--root-device''.");
1f7315a3 182 }
183 }
184
4b13b216 185 prefix = grub_get_prefix (args.dir ? : DEFAULT_DIRECTORY);
1f7315a3 186 sprintf (rootprefix, "%s%s", args.root_dev, prefix);
db1771cf 187
4b13b216 188 grub_env_set ("prefix", rootprefix);
1f7315a3 189
190 /* XXX: This is a bit unportable. */
4b13b216 191 grub_util_biosdisk_init (args.dev_map);
3f1578fe 192 grub_pc_partition_map_init ();
193 grub_amiga_partition_map_init ();
194 grub_apple_partition_map_init ();
4ed2e1dd 195 grub_sun_partition_map_init ();
1f7315a3 196
197 /* Initialize the default modules. */
ad0bd20b 198 grub_iso9660_init ();
b4093103 199 grub_xfs_init ();
4b13b216 200 grub_fat_init ();
201 grub_ext2_init ();
66e19ef8 202 grub_ufs_init ();
203 grub_minix_init ();
64372eb4 204 grub_hfs_init ();
aa033560 205 grub_jfs_init ();
b2499b29 206 grub_xfs_init ();
4b13b216 207 grub_ls_init ();
208 grub_boot_init ();
209 grub_cmp_init ();
210 grub_cat_init ();
211 grub_terminal_init ();
67bbaf0f 212 grub_loop_init ();
990cf3aa 213 grub_help_init ();
e6b92c8a 214 grub_halt_init ();
215 grub_reboot_init ();
93f3a1d8 216 grub_default_init ();
217 grub_timeout_init ();
062aaf39 218 grub_configfile_init ();
6a85ce79 219 grub_search_init ();
67bbaf0f 220
1f7315a3 221 /* XXX: Should normal mode be started by default? */
4b13b216 222 grub_normal_init ();
223
224 /* Start GRUB! */
e6b92c8a 225 if (setjmp (main_env) == 0)
226 grub_main ();
4b13b216 227
6a85ce79 228 grub_search_fini ();
062aaf39 229 grub_configfile_fini ();
93f3a1d8 230 grub_timeout_fini ();
231 grub_default_fini ();
e6b92c8a 232 grub_reboot_fini ();
233 grub_halt_fini ();
990cf3aa 234 grub_help_fini ();
67bbaf0f 235 grub_loop_fini ();
4b13b216 236 grub_util_biosdisk_fini ();
237 grub_normal_fini ();
b2499b29 238 grub_xfs_fini ();
66e19ef8 239 grub_ufs_fini ();
4b13b216 240 grub_ext2_fini ();
66e19ef8 241 grub_minix_fini ();
64372eb4 242 grub_hfs_fini ();
aa033560 243 grub_jfs_fini ();
4b13b216 244 grub_fat_fini ();
b4093103 245 grub_xfs_fini ();
4b13b216 246 grub_boot_fini ();
247 grub_cmp_fini ();
248 grub_cat_fini ();
249 grub_terminal_fini ();
3f1578fe 250 grub_amiga_partition_map_fini ();
251 grub_pc_partition_map_fini ();
252 grub_apple_partition_map_fini ();
4ed2e1dd 253 grub_sun_partition_map_fini ();
e6b92c8a 254
255 grub_machine_fini ();
3f1578fe 256
1f7315a3 257 return 0;
258}