Fix cross compilation (e.g. on Darwin). Following changes to make.tmpl,
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / kern / main.c
blob9cad0c4485cccfc7684f3ae5e8a6e9864a7912cf
1 /* main.c - the kernel main routine */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2003,2005,2006,2008,2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/kernel.h>
21 #include <grub/misc.h>
22 #include <grub/symbol.h>
23 #include <grub/dl.h>
24 #include <grub/term.h>
25 #include <grub/file.h>
26 #include <grub/device.h>
27 #include <grub/env.h>
28 #include <grub/mm.h>
29 #include <grub/command.h>
30 #include <grub/reader.h>
31 #include <grub/parser.h>
33 #ifdef GRUB_MACHINE_PCBIOS
34 #include <grub/machine/memory.h>
35 #endif
37 grub_addr_t
38 grub_modules_get_end (void)
40 struct grub_module_info *modinfo;
42 modinfo = (struct grub_module_info *) grub_modbase;
44 /* Check if there are any modules. */
45 if ((modinfo == 0) || modinfo->magic != GRUB_MODULE_MAGIC)
46 return grub_modbase;
48 return grub_modbase + modinfo->size;
51 /* Load all modules in core. */
52 static void
53 grub_load_modules (void)
55 struct grub_module_header *header;
56 FOR_MODULES (header)
58 /* Not an ELF module, skip. */
59 if (header->type != OBJ_TYPE_ELF)
60 continue;
62 if (! grub_dl_load_core ((char *) header + sizeof (struct grub_module_header),
63 (header->size - sizeof (struct grub_module_header))))
64 grub_fatal ("%s", grub_errmsg);
66 if (grub_errno)
67 grub_print_error ();
71 static char *load_config;
73 static void
74 grub_load_config (void)
76 struct grub_module_header *header;
77 FOR_MODULES (header)
79 /* Not an embedded config, skip. */
80 if (header->type != OBJ_TYPE_CONFIG)
81 continue;
83 load_config = grub_malloc (header->size - sizeof (struct grub_module_header) + 1);
84 if (!load_config)
86 grub_print_error ();
87 break;
89 grub_memcpy (load_config, (char *) header +
90 sizeof (struct grub_module_header),
91 header->size - sizeof (struct grub_module_header));
92 load_config[header->size - sizeof (struct grub_module_header)] = 0;
93 break;
97 /* Write hook for the environment variables of root. Remove surrounding
98 parentheses, if any. */
99 static char *
100 grub_env_write_root (struct grub_env_var *var __attribute__ ((unused)),
101 const char *val)
103 /* XXX Is it better to check the existence of the device? */
104 grub_size_t len = grub_strlen (val);
106 if (val[0] == '(' && val[len - 1] == ')')
107 return grub_strndup (val + 1, len - 2);
109 return grub_strdup (val);
112 static void
113 grub_set_prefix_and_root (void)
115 char *device = NULL;
116 char *path = NULL;
117 char *fwdevice = NULL;
118 char *fwpath = NULL;
119 char *prefix = NULL;
120 struct grub_module_header *header;
122 FOR_MODULES (header)
123 if (header->type == OBJ_TYPE_PREFIX)
124 prefix = (char *) header + sizeof (struct grub_module_header);
126 grub_register_variable_hook ("root", 0, grub_env_write_root);
128 grub_machine_get_bootlocation (&fwdevice, &fwpath);
130 if (fwdevice)
132 char *cmdpath;
134 cmdpath = grub_xasprintf ("(%s)%s", fwdevice, fwpath ? : "");
135 if (cmdpath)
137 grub_env_set ("cmdpath", cmdpath);
138 grub_env_export ("cmdpath");
139 grub_free (cmdpath);
143 if (prefix)
145 char *pptr = NULL;
146 if (prefix[0] == '(')
148 pptr = grub_strrchr (prefix, ')');
149 if (pptr)
151 device = grub_strndup (prefix + 1, pptr - prefix - 1);
152 pptr++;
155 if (!pptr)
156 pptr = prefix;
157 if (pptr[0])
158 path = grub_strdup (pptr);
161 if (!device && fwdevice)
162 device = fwdevice;
163 else if (fwdevice && (device[0] == ',' || !device[0]))
165 /* We have a partition, but still need to fill in the drive. */
166 char *comma, *new_device;
168 for (comma = fwdevice; *comma; )
170 if (comma[0] == '\\' && comma[1] == ',')
172 comma += 2;
173 continue;
175 if (*comma == ',')
176 break;
177 comma++;
179 if (*comma)
181 char *drive = grub_strndup (fwdevice, comma - fwdevice);
182 new_device = grub_xasprintf ("%s%s", drive, device);
183 grub_free (drive);
185 else
186 new_device = grub_xasprintf ("%s%s", fwdevice, device);
188 grub_free (fwdevice);
189 grub_free (device);
190 device = new_device;
192 else
193 grub_free (fwdevice);
194 if (fwpath && !path)
196 grub_size_t len = grub_strlen (fwpath);
197 while (len > 1 && fwpath[len - 1] == '/')
198 fwpath[--len] = 0;
199 if (len >= sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1
200 && grub_memcmp (fwpath + len - (sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1), GRUB_TARGET_CPU "-" GRUB_PLATFORM,
201 sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1) == 0)
202 fwpath[len - (sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1)] = 0;
203 path = fwpath;
205 else
206 grub_free (fwpath);
207 if (device)
209 char *prefix_set;
211 prefix_set = grub_xasprintf ("(%s)%s", device, path ? : "");
212 if (prefix_set)
214 grub_env_set ("prefix", prefix_set);
215 grub_free (prefix_set);
217 grub_env_set ("root", device);
220 grub_free (device);
221 grub_free (path);
222 grub_print_error ();
225 /* Load the normal mode module and execute the normal mode if possible. */
226 static void
227 grub_load_normal_mode (void)
229 /* Load the module. */
230 grub_dl_load ("normal");
232 /* Print errors if any. */
233 grub_print_error ();
234 grub_errno = 0;
236 grub_command_execute ("normal", 0, 0);
239 static void
240 reclaim_module_space (void)
242 grub_addr_t modstart, modend;
244 if (!grub_modbase)
245 return;
247 #ifdef GRUB_MACHINE_PCBIOS
248 modstart = GRUB_MEMORY_MACHINE_DECOMPRESSION_ADDR;
249 #else
250 modstart = grub_modbase;
251 #endif
252 modend = grub_modules_get_end ();
253 grub_modbase = 0;
255 #if GRUB_KERNEL_PRELOAD_SPACE_REUSABLE
256 grub_mm_init_region ((void *) modstart, modend - modstart);
257 #else
258 (void) modstart;
259 (void) modend;
260 #endif
263 /* The main routine. */
264 void __attribute__ ((noreturn))
265 grub_main (void)
267 /* First of all, initialize the machine. */
268 grub_machine_init ();
270 grub_boot_time ("After machine init.");
272 /* Hello. */
273 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
274 grub_printf ("Welcome to GRUB!\n\n");
275 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
277 grub_load_config ();
279 grub_boot_time ("Before loading embedded modules.");
281 /* Load pre-loaded modules and free the space. */
282 grub_register_exported_symbols ();
283 #ifdef GRUB_LINKER_HAVE_INIT
284 grub_arch_dl_init_linker ();
285 #endif
286 grub_load_modules ();
288 grub_boot_time ("After loading embedded modules.");
290 /* It is better to set the root device as soon as possible,
291 for convenience. */
292 grub_set_prefix_and_root ();
293 grub_env_export ("root");
294 grub_env_export ("prefix");
296 /* Reclaim space used for modules. */
297 reclaim_module_space ();
299 grub_boot_time ("After reclaiming module space.");
301 grub_register_core_commands ();
303 grub_boot_time ("Before execution of embedded config.");
305 if (load_config)
306 grub_parser_execute (load_config);
308 grub_boot_time ("After execution of embedded config. Attempt to go to normal mode");
310 grub_load_normal_mode ();
311 grub_rescue_run ();