malloc->zalloc
[grub2/phcoder.git] / util / grub-emu.c
blob97f18865b4b1fb3c267fdce5d0e7867ee855180e
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2003,2004,2005,2006,2007,2008 Free Software Foundation, Inc.
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 3 of the License, or
8 * (at your option) any later version.
10 * GRUB 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.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <stdlib.h>
20 #include <sys/stat.h>
21 #include <getopt.h>
22 #include <string.h>
23 #include <signal.h>
24 #include <sys/types.h>
25 #include <unistd.h>
27 #include <grub/mm.h>
28 #include <grub/setjmp.h>
29 #include <grub/fs.h>
30 #include <grub/util/hostdisk.h>
31 #include <grub/dl.h>
32 #include <grub/util/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>
40 #include <grub_emu_init.h>
42 /* Used for going back to the main function. */
43 static jmp_buf main_env;
45 /* Store the prefix specified by an argument. */
46 static char *prefix = 0;
48 grub_addr_t
49 grub_arch_modules_addr (void)
51 return 0;
54 grub_err_t
55 grub_arch_dl_check_header (void *ehdr)
57 (void) ehdr;
59 return GRUB_ERR_BAD_MODULE;
62 grub_err_t
63 grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
65 (void) mod;
66 (void) ehdr;
68 return GRUB_ERR_BAD_MODULE;
71 void
72 grub_reboot (void)
74 longjmp (main_env, 1);
77 void
78 grub_halt (
79 #ifdef GRUB_MACHINE_PCBIOS
80 int no_apm __attribute__ ((unused))
81 #endif
84 grub_reboot ();
87 void
88 grub_machine_init (void)
92 void
93 grub_machine_set_prefix (void)
95 grub_env_set ("prefix", prefix);
96 free (prefix);
97 prefix = 0;
100 void
101 grub_machine_fini (void)
103 grub_console_fini ();
106 void
107 read_command_list (void)
112 static struct option options[] =
114 {"root-device", required_argument, 0, 'r'},
115 {"device-map", required_argument, 0, 'm'},
116 {"directory", required_argument, 0, 'd'},
117 {"hold", optional_argument, 0, 'H'},
118 {"help", no_argument, 0, 'h'},
119 {"version", no_argument, 0, 'V'},
120 {"verbose", no_argument, 0, 'v'},
121 { 0, 0, 0, 0 }
124 static int
125 usage (int status)
127 if (status)
128 fprintf (stderr,
129 "Try ``grub-emu --help'' for more information.\n");
130 else
131 printf (
132 "Usage: grub-emu [OPTION]...\n"
133 "\n"
134 "GRUB emulator.\n"
135 "\n"
136 " -r, --root-device=DEV use DEV as the root device [default=guessed]\n"
137 " -m, --device-map=FILE use FILE as the device map [default=%s]\n"
138 " -d, --directory=DIR use GRUB files in the directory DIR [default=%s]\n"
139 " -v, --verbose print verbose messages\n"
140 " -H, --hold[=SECONDS] wait until a debugger will attach\n"
141 " -h, --help display this message and exit\n"
142 " -V, --version print version information and exit\n"
143 "\n"
144 "Report bugs to <%s>.\n", DEFAULT_DEVICE_MAP, DEFAULT_DIRECTORY, PACKAGE_BUGREPORT);
145 return status;
150 main (int argc, char *argv[])
152 char *root_dev = 0;
153 char *dir = DEFAULT_DIRECTORY;
154 char *dev_map = DEFAULT_DEVICE_MAP;
155 volatile int hold = 0;
156 int opt;
158 progname = "grub-emu";
160 while ((opt = getopt_long (argc, argv, "r:d:m:vH:hV", options, 0)) != -1)
161 switch (opt)
163 case 'r':
164 root_dev = optarg;
165 break;
166 case 'd':
167 dir = optarg;
168 break;
169 case 'm':
170 dev_map = optarg;
171 break;
172 case 'v':
173 verbosity++;
174 break;
175 case 'H':
176 hold = (optarg ? atoi (optarg) : -1);
177 break;
178 case 'h':
179 return usage (0);
180 case 'V':
181 printf ("%s (%s) %s\n", progname, PACKAGE_NAME, PACKAGE_VERSION);
182 return 0;
183 default:
184 return usage (1);
187 if (optind < argc)
189 fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind]);
190 return usage (1);
193 /* Wait until the ARGS.HOLD variable is cleared by an attached debugger. */
194 if (hold && verbosity > 0)
195 printf ("Run \"gdb %s %d\", and set ARGS.HOLD to zero.\n",
196 progname, (int) getpid ());
197 while (hold)
199 if (hold > 0)
200 hold--;
202 sleep (1);
205 signal (SIGINT, SIG_IGN);
206 grub_console_init ();
208 /* XXX: This is a bit unportable. */
209 grub_util_biosdisk_init (dev_map);
211 grub_init_all ();
213 /* Make sure that there is a root device. */
214 if (! root_dev)
216 char *device_name = grub_guess_root_device (dir);
217 if (! device_name)
218 grub_util_error ("cannot find a device for %s.\n", dir);
220 root_dev = grub_util_get_grub_dev (device_name);
221 if (! root_dev)
223 grub_util_info ("guessing the root device failed, because of `%s'",
224 grub_errmsg);
225 grub_util_error ("Cannot guess the root device. Specify the option ``--root-device''.");
229 dir = grub_get_prefix (dir);
230 prefix = xmalloc (strlen (root_dev) + 2 + strlen (dir) + 1);
231 sprintf (prefix, "(%s)%s", root_dev, dir);
232 free (dir);
234 /* Start GRUB! */
235 if (setjmp (main_env) == 0)
236 grub_main ();
238 grub_fini_all ();
240 grub_machine_fini ();
242 return 0;