Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / kern / emu / main.c
blob0418aae64f76eaba875a60d8c7fea0fc8a7aae02
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2003,2004,2005,2006,2007,2008,2009,2010 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 <time.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <setjmp.h>
23 #include <sys/stat.h>
24 #include <string.h>
25 #include <signal.h>
26 #include <sys/types.h>
27 #include <unistd.h>
29 #include <grub/dl.h>
30 #include <grub/mm.h>
31 #include <grub/setjmp.h>
32 #include <grub/fs.h>
33 #include <grub/emu/hostdisk.h>
34 #include <grub/time.h>
35 #include <grub/emu/console.h>
36 #include <grub/emu/misc.h>
37 #include <grub/kernel.h>
38 #include <grub/normal.h>
39 #include <grub/emu/getroot.h>
40 #include <grub/env.h>
41 #include <grub/partition.h>
42 #include <grub/i18n.h>
44 #include "progname.h"
45 #include <argp.h>
47 #define ENABLE_RELOCATABLE 0
49 /* Used for going back to the main function. */
50 static jmp_buf main_env;
52 /* Store the prefix specified by an argument. */
53 static char *root_dev = NULL, *dir = NULL;
55 int grub_no_autoload;
57 grub_addr_t grub_modbase = 0;
59 void
60 grub_reboot (void)
62 longjmp (main_env, 1);
65 void
66 grub_machine_init (void)
70 void
71 grub_machine_get_bootlocation (char **device, char **path)
73 *device = root_dev;
74 *path = dir;
77 void
78 grub_machine_fini (void)
80 grub_console_fini ();
85 static struct argp_option options[] = {
86 {"root", 'r', N_("DEVICE_NAME"), 0, N_("Set root device."), 2},
87 {"device-map", 'm', N_("FILE"), 0,
88 /* TRANSLATORS: There are many devices in device map. */
89 N_("use FILE as the device map [default=%s]"), 0},
90 {"directory", 'd', N_("DIR"), 0,
91 N_("use GRUB files in the directory DIR [default=%s]"), 0},
92 {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
93 {"hold", 'H', N_("SECS"), OPTION_ARG_OPTIONAL, N_("wait until a debugger will attach"), 0},
94 { 0, 0, 0, 0, 0, 0 }
97 static char *
98 help_filter (int key, const char *text, void *input __attribute__ ((unused)))
100 switch (key)
102 case 'd':
103 return xasprintf (text, DEFAULT_DIRECTORY);
104 case 'm':
105 return xasprintf (text, DEFAULT_DEVICE_MAP);
106 default:
107 return (char *) text;
111 struct arguments
113 const char *dev_map;
114 int hold;
117 static error_t
118 argp_parser (int key, char *arg, struct argp_state *state)
120 /* Get the input argument from argp_parse, which we
121 know is a pointer to our arguments structure. */
122 struct arguments *arguments = state->input;
124 switch (key)
126 case 'r':
127 free (root_dev);
128 root_dev = xstrdup (arg);
129 break;
130 case 'd':
131 free (dir);
132 dir = xstrdup (arg);
133 break;
134 case 'm':
135 arguments->dev_map = arg;
136 break;
137 case 'H':
138 arguments->hold = (arg ? atoi (arg) : -1);
139 break;
140 case 'v':
141 verbosity++;
142 break;
144 case ARGP_KEY_ARG:
146 /* Too many arguments. */
147 fprintf (stderr, _("Unknown extra argument `%s'."), arg);
148 fprintf (stderr, "\n");
149 argp_usage (state);
151 break;
153 default:
154 return ARGP_ERR_UNKNOWN;
156 return 0;
159 static struct argp argp = {
160 options, argp_parser, NULL,
161 N_("GRUB emulator."),
162 NULL, help_filter, NULL
167 void grub_hostfs_init (void);
168 void grub_hostfs_fini (void);
169 void grub_host_init (void);
170 void grub_host_fini (void);
171 void grub_emu_init (void);
174 main (int argc, char *argv[])
176 struct arguments arguments =
178 .dev_map = DEFAULT_DEVICE_MAP,
179 .hold = 0
181 volatile int hold = 0;
183 set_program_name (argv[0]);
185 dir = xstrdup (DEFAULT_DIRECTORY);
187 if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
189 fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
190 exit(1);
193 hold = arguments.hold;
194 /* Wait until the ARGS.HOLD variable is cleared by an attached debugger. */
195 if (hold && verbosity > 0)
196 /* TRANSLATORS: In this case GRUB tells user what he has to do. */
197 printf (_("Run `gdb %s %d', and set ARGS.HOLD to zero.\n"),
198 program_name, (int) getpid ());
199 while (hold)
201 if (hold > 0)
202 hold--;
204 sleep (1);
207 signal (SIGINT, SIG_IGN);
208 grub_emu_init ();
209 grub_console_init ();
210 grub_host_init ();
212 /* XXX: This is a bit unportable. */
213 grub_util_biosdisk_init (arguments.dev_map);
215 grub_init_all ();
217 grub_hostfs_init ();
219 grub_emu_post_init ();
221 /* Make sure that there is a root device. */
222 if (! root_dev)
223 root_dev = grub_strdup ("host");
225 dir = xstrdup (dir);
227 /* Start GRUB! */
228 if (setjmp (main_env) == 0)
229 grub_main ();
231 grub_fini_all ();
232 grub_hostfs_fini ();
233 grub_host_fini ();
235 grub_machine_fini ();
237 return 0;
240 #ifdef __MINGW32__
242 void
243 grub_millisleep (grub_uint32_t ms)
245 Sleep (ms);
248 #else
250 void
251 grub_millisleep (grub_uint32_t ms)
253 struct timespec ts;
255 ts.tv_sec = ms / 1000;
256 ts.tv_nsec = (ms % 1000) * 1000000;
257 nanosleep (&ts, NULL);
260 #endif