]> git.proxmox.com Git - grub2.git/blob - util/grub-emu.c
2005-02-27 Yoshinori K. Okuji <okuji@enbug.org>
[grub2.git] / util / grub-emu.c
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
4 *
5 * GRUB is free software; you can redistribute it and/or modify
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
16 * along with GRUB; if not, write to the Free Software
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>
25 #include <signal.h>
26
27 #include <grub/mm.h>
28 #include <grub/setjmp.h>
29 #include <grub/fs.h>
30 #include <grub/i386/pc/util/biosdisk.h>
31 #include <grub/dl.h>
32 #include <grub/machine/console.h>
33 #include <grub/util/misc.h>
34 #include <grub/kernel.h>
35 #include <grub/normal.h>
36 #include <grub/util/getroot.h>
37 #include <grub/env.h>
38 #include <grub/partition.h>
39
40 #ifdef __NetBSD__
41 /* NetBSD uses /boot for its boot block. */
42 # define DEFAULT_DIRECTORY "/grub"
43 #else
44 # define DEFAULT_DIRECTORY "/boot/grub"
45 #endif
46
47 #define DEFAULT_DEVICE_MAP DEFAULT_DIRECTORY "/device.map"
48
49 /* Used for going back to the main function. */
50 jmp_buf main_env;
51
52 grub_addr_t
53 grub_arch_modules_addr (void)
54 {
55 return 0;
56 }
57
58 grub_err_t
59 grub_arch_dl_check_header (void *ehdr)
60 {
61 (void) ehdr;
62
63 return GRUB_ERR_BAD_MODULE;
64 }
65
66 grub_err_t
67 grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
68 {
69 (void) mod;
70 (void) ehdr;
71
72 return GRUB_ERR_BAD_MODULE;
73 }
74
75 void
76 grub_machine_init (void)
77 {
78 signal (SIGINT, SIG_IGN);
79 grub_console_init ();
80 }
81
82 void
83 grub_machine_fini (void)
84 {
85 grub_console_fini ();
86 }
87 \f
88
89 const char *argp_program_version = PACKAGE_STRING;
90 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
91 static char doc[] = "GRUB emulator";
92
93 static struct argp_option options[] = {
94 {"root-device", 'r', "DEV", 0, "use DEV as the root device [default=guessed]", 0},
95 {"device-map", 'm', "FILE", 0, "use FILE as the device map", 0},
96 {"directory", 'd', "DIR", 0, "use GRUB files in the directory DIR", 0},
97 {"verbose", 'v', 0 , 0, "print verbose messages", 0},
98 { 0, 0, 0, 0, 0, 0 }
99 };
100
101 struct arguments
102 {
103 char *root_dev;
104 char *dev_map;
105 char *dir;
106 };
107
108 static error_t
109 parse_opt (int key, char *arg, struct argp_state *state)
110 {
111 struct arguments *args = state->input;
112
113 switch (key)
114 {
115 case 'r':
116 args->root_dev = arg;
117 break;
118 case 'd':
119 args->dir = arg;
120 break;
121 case 'm':
122 args->dev_map = arg;
123 break;
124 case 'v':
125 verbosity++;
126 break;
127 case ARGP_KEY_END:
128 break;
129 default:
130 return ARGP_ERR_UNKNOWN;
131 }
132 return 0;
133 }
134
135 static struct argp argp = {options, parse_opt, 0, doc, 0, 0, 0};
136 \f
137
138 int
139 main (int argc, char *argv[])
140 {
141 char *prefix = 0;
142 char rootprefix[100];
143 struct arguments args =
144 {
145 .dir = DEFAULT_DIRECTORY,
146 .dev_map = DEFAULT_DEVICE_MAP
147 };
148
149 progname = "grub-emu";
150
151 argp_parse (&argp, argc, argv, 0, 0, &args);
152
153 /* Make sure that there is a root device. */
154 if (! args.root_dev)
155 {
156 args.root_dev = grub_guess_root_device (args.dir ? : DEFAULT_DIRECTORY);
157 if (! args.root_dev)
158 {
159 grub_util_info ("guessing the root device failed, because of `%s'",
160 grub_errmsg);
161 grub_util_error ("Cannot guess the root device. Specify the option ``--root-device''.");
162 }
163 }
164
165 prefix = grub_get_prefix (args.dir ? : DEFAULT_DIRECTORY);
166 sprintf (rootprefix, "%s%s", args.root_dev, prefix);
167
168 grub_env_set ("prefix", rootprefix);
169
170 /* XXX: This is a bit unportable. */
171 grub_util_biosdisk_init (args.dev_map);
172 grub_pc_partition_map_init ();
173 grub_amiga_partition_map_init ();
174 grub_apple_partition_map_init ();
175 grub_sun_partition_map_init ();
176
177 /* Initialize the default modules. */
178 grub_iso9660_init ();
179 grub_fat_init ();
180 grub_ext2_init ();
181 grub_ufs_init ();
182 grub_minix_init ();
183 grub_hfs_init ();
184 grub_jfs_init ();
185 grub_ls_init ();
186 grub_boot_init ();
187 grub_cmp_init ();
188 grub_cat_init ();
189 grub_terminal_init ();
190 grub_loop_init ();
191 grub_help_init ();
192 grub_halt_init ();
193 grub_reboot_init ();
194 grub_default_init ();
195 grub_timeout_init ();
196
197 /* XXX: Should normal mode be started by default? */
198 grub_normal_init ();
199
200 /* Start GRUB! */
201 if (setjmp (main_env) == 0)
202 grub_main ();
203
204 grub_timeout_fini ();
205 grub_default_fini ();
206 grub_reboot_fini ();
207 grub_halt_fini ();
208 grub_help_fini ();
209 grub_loop_fini ();
210 grub_util_biosdisk_fini ();
211 grub_normal_fini ();
212 grub_ufs_fini ();
213 grub_ext2_fini ();
214 grub_minix_fini ();
215 grub_hfs_fini ();
216 grub_jfs_fini ();
217 grub_fat_fini ();
218 grub_boot_fini ();
219 grub_cmp_fini ();
220 grub_cat_fini ();
221 grub_terminal_fini ();
222 grub_amiga_partition_map_fini ();
223 grub_pc_partition_map_fini ();
224 grub_apple_partition_map_fini ();
225 grub_sun_partition_map_fini ();
226
227 grub_machine_fini ();
228
229 return 0;
230 }