make the linux-ppc packags be in synch with other platforms
[tangerine.git] / arch / common / boot / grub2 / util / grub-emu.c
blob3b562f76051c8cdf5f9cc25c16c04b5719459397
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/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>
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)
74 signal (SIGINT, SIG_IGN);
75 grub_console_init ();
78 void
79 grub_machine_set_prefix (void)
81 grub_env_set ("prefix", prefix);
82 free (prefix);
83 prefix = 0;
86 void
87 grub_machine_fini (void)
89 grub_console_fini ();
93 static struct option options[] =
95 {"root-device", required_argument, 0, 'r'},
96 {"device-map", required_argument, 0, 'm'},
97 {"directory", required_argument, 0, 'd'},
98 {"hold", optional_argument, 0, 'H'},
99 {"help", no_argument, 0, 'h'},
100 {"version", no_argument, 0, 'V'},
101 {"verbose", no_argument, 0, 'v'},
102 { 0, 0, 0, 0 }
105 static int
106 usage (int status)
108 if (status)
109 fprintf (stderr,
110 "Try ``grub-emu --help'' for more information.\n");
111 else
112 printf (
113 "Usage: grub-emu [OPTION]...\n"
114 "\n"
115 "GRUB emulator.\n"
116 "\n"
117 " -r, --root-device=DEV use DEV as the root device [default=guessed]\n"
118 " -m, --device-map=FILE use FILE as the device map [default=%s]\n"
119 " -d, --directory=DIR use GRUB files in the directory DIR [default=%s]\n"
120 " -v, --verbose print verbose messages\n"
121 " -H, --hold[=SECONDS] wait until a debugger will attach\n"
122 " -h, --help display this message and exit\n"
123 " -V, --version print version information and exit\n"
124 "\n"
125 "Report bugs to <%s>.\n", DEFAULT_DEVICE_MAP, DEFAULT_DIRECTORY, PACKAGE_BUGREPORT);
126 return status;
131 main (int argc, char *argv[])
133 char *root_dev = 0;
134 char *dir = DEFAULT_DIRECTORY;
135 char *dev_map = DEFAULT_DEVICE_MAP;
136 volatile int hold = 0;
137 int opt;
139 progname = "grub-emu";
141 while ((opt = getopt_long (argc, argv, "r:d:m:vH:hV", options, 0)) != -1)
142 switch (opt)
144 case 'r':
145 root_dev = optarg;
146 break;
147 case 'd':
148 dir = optarg;
149 break;
150 case 'm':
151 dev_map = optarg;
152 break;
153 case 'v':
154 verbosity++;
155 break;
156 case 'H':
157 hold = (optarg ? atoi (optarg) : -1);
158 break;
159 case 'h':
160 return usage (0);
161 case 'V':
162 printf ("%s (%s) %s\n", progname, PACKAGE_NAME, PACKAGE_VERSION);
163 return 0;
164 default:
165 return usage (1);
168 if (optind < argc)
170 fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind]);
171 return usage (1);
174 /* Wait until the ARGS.HOLD variable is cleared by an attached debugger. */
175 if (hold && verbosity > 0)
176 printf ("Run \"gdb %s %d\", and set ARGS.HOLD to zero.\n",
177 progname, (int) getpid ());
178 while (hold)
180 if (hold > 0)
181 hold--;
183 sleep (1);
186 /* XXX: This is a bit unportable. */
187 grub_util_biosdisk_init (dev_map);
189 grub_hostfs_init ();
191 grub_init_all ();
193 /* Make sure that there is a root device. */
194 if (! root_dev)
196 char *device_name = grub_guess_root_device (dir);
197 if (! device_name)
198 grub_util_error ("cannot find a device for %s.\n", dir);
200 root_dev = grub_util_get_grub_dev (device_name);
201 if (! root_dev)
203 grub_util_info ("guessing the root device failed, because of `%s'",
204 grub_errmsg);
205 grub_util_error ("Cannot guess the root device. Specify the option ``--root-device''.");
209 dir = grub_get_prefix (dir);
210 prefix = xmalloc (strlen (root_dev) + 2 + strlen (dir) + 1);
211 sprintf (prefix, "(%s)%s", root_dev, dir);
212 free (dir);
214 /* Start GRUB! */
215 if (setjmp (main_env) == 0)
216 grub_main ();
218 grub_fini_all ();
220 grub_hostfs_fini ();
222 grub_machine_fini ();
224 return 0;