run autogen.sh ...
[grub2/phcoder/solaris.git] / util / grub-emu.c
blob4569d9ed9c8f5925621f78091a93750e37977f93
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/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>
40 #include <grub_emu_init.h>
42 /* Used for going back to the main function. */
43 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_machine_init (void)
76 void
77 grub_machine_set_prefix (void)
79 grub_env_set ("prefix", prefix);
80 free (prefix);
81 prefix = 0;
84 void
85 grub_machine_fini (void)
87 grub_console_fini ();
91 static struct option options[] =
93 {"root-device", required_argument, 0, 'r'},
94 {"device-map", required_argument, 0, 'm'},
95 {"directory", required_argument, 0, 'd'},
96 {"hold", optional_argument, 0, 'H'},
97 {"help", no_argument, 0, 'h'},
98 {"version", no_argument, 0, 'V'},
99 {"verbose", no_argument, 0, 'v'},
100 { 0, 0, 0, 0 }
103 static int
104 usage (int status)
106 if (status)
107 fprintf (stderr,
108 "Try ``grub-emu --help'' for more information.\n");
109 else
110 printf (
111 "Usage: grub-emu [OPTION]...\n"
112 "\n"
113 "GRUB emulator.\n"
114 "\n"
115 " -r, --root-device=DEV use DEV as the root device [default=guessed]\n"
116 " -m, --device-map=FILE use FILE as the device map [default=%s]\n"
117 " -d, --directory=DIR use GRUB files in the directory DIR [default=%s]\n"
118 " -v, --verbose print verbose messages\n"
119 " -H, --hold[=SECONDS] wait until a debugger will attach\n"
120 " -h, --help display this message and exit\n"
121 " -V, --version print version information and exit\n"
122 "\n"
123 "Report bugs to <%s>.\n", DEFAULT_DEVICE_MAP, DEFAULT_DIRECTORY, PACKAGE_BUGREPORT);
124 return status;
129 main (int argc, char *argv[])
131 char *root_dev = 0;
132 char *dir = DEFAULT_DIRECTORY;
133 char *dev_map = DEFAULT_DEVICE_MAP;
134 volatile int hold = 0;
135 int opt;
137 progname = "grub-emu";
139 while ((opt = getopt_long (argc, argv, "r:d:m:vH:hV", options, 0)) != -1)
140 switch (opt)
142 case 'r':
143 root_dev = optarg;
144 break;
145 case 'd':
146 dir = optarg;
147 break;
148 case 'm':
149 dev_map = optarg;
150 break;
151 case 'v':
152 verbosity++;
153 break;
154 case 'H':
155 hold = (optarg ? atoi (optarg) : -1);
156 break;
157 case 'h':
158 return usage (0);
159 case 'V':
160 printf ("%s (%s) %s\n", progname, PACKAGE_NAME, PACKAGE_VERSION);
161 return 0;
162 default:
163 return usage (1);
166 if (optind < argc)
168 fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind]);
169 return usage (1);
172 /* Wait until the ARGS.HOLD variable is cleared by an attached debugger. */
173 if (hold && verbosity > 0)
174 printf ("Run \"gdb %s %d\", and set ARGS.HOLD to zero.\n",
175 progname, (int) getpid ());
176 while (hold)
178 if (hold > 0)
179 hold--;
181 sleep (1);
184 signal (SIGINT, SIG_IGN);
185 grub_console_init ();
187 /* XXX: This is a bit unportable. */
188 grub_util_biosdisk_init (dev_map);
190 grub_init_all ();
192 /* Make sure that there is a root device. */
193 if (! root_dev)
195 char *device_name = grub_guess_root_device (dir);
196 if (! device_name)
197 grub_util_error ("cannot find a device for %s.\n", dir);
199 root_dev = grub_util_get_grub_dev (device_name);
200 if (! root_dev)
202 grub_util_info ("guessing the root device failed, because of `%s'",
203 grub_errmsg);
204 grub_util_error ("Cannot guess the root device. Specify the option ``--root-device''.");
208 dir = grub_get_prefix (dir);
209 prefix = xmalloc (strlen (root_dev) + 2 + strlen (dir) + 1);
210 sprintf (prefix, "(%s)%s", root_dev, dir);
211 free (dir);
213 /* Start GRUB! */
214 if (setjmp (main_env) == 0)
215 grub_main ();
217 grub_fini_all ();
219 grub_machine_fini ();
221 return 0;