Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / kern / main.c
blob326244419eb6c47fa85266c4d74ea48578a90f63
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 /* This is actualy platform-independant but used only on loongson and sparc. */
34 #if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS) || defined (GRUB_MACHINE_SPARC64)
35 grub_addr_t
36 grub_modules_get_end (void)
38 struct grub_module_info *modinfo;
40 modinfo = (struct grub_module_info *) grub_modbase;
42 /* Check if there are any modules. */
43 if ((modinfo == 0) || modinfo->magic != GRUB_MODULE_MAGIC)
44 return grub_modbase;
46 return grub_modbase + modinfo->size;
48 #endif
50 /* Load all modules in core. */
51 static void
52 grub_load_modules (void)
54 struct grub_module_header *header;
55 FOR_MODULES (header)
57 /* Not an ELF module, skip. */
58 if (header->type != OBJ_TYPE_ELF)
59 continue;
61 if (! grub_dl_load_core ((char *) header + sizeof (struct grub_module_header),
62 (header->size - sizeof (struct grub_module_header))))
63 grub_fatal ("%s", grub_errmsg);
65 if (grub_errno)
66 grub_print_error ();
70 static void
71 grub_load_config (void)
73 struct grub_module_header *header;
74 FOR_MODULES (header)
76 /* Not an embedded config, skip. */
77 if (header->type != OBJ_TYPE_CONFIG)
78 continue;
80 grub_parser_execute ((char *) header +
81 sizeof (struct grub_module_header));
82 break;
86 /* Write hook for the environment variables of root. Remove surrounding
87 parentheses, if any. */
88 static char *
89 grub_env_write_root (struct grub_env_var *var __attribute__ ((unused)),
90 const char *val)
92 /* XXX Is it better to check the existence of the device? */
93 grub_size_t len = grub_strlen (val);
95 if (val[0] == '(' && val[len - 1] == ')')
96 return grub_strndup (val + 1, len - 2);
98 return grub_strdup (val);
101 static void
102 grub_set_prefix_and_root (void)
104 char *device = NULL;
105 char *path = NULL;
106 char *fwdevice = NULL;
107 char *fwpath = NULL;
108 char *prefix = NULL;
109 struct grub_module_header *header;
111 FOR_MODULES (header)
112 if (header->type == OBJ_TYPE_PREFIX)
113 prefix = (char *) header + sizeof (struct grub_module_header);
115 grub_register_variable_hook ("root", 0, grub_env_write_root);
117 if (prefix)
119 char *pptr = NULL;
120 if (prefix[0] == '(')
122 pptr = grub_strrchr (prefix, ')');
123 if (pptr)
125 device = grub_strndup (prefix + 1, pptr - prefix - 1);
126 pptr++;
129 if (!pptr)
130 pptr = prefix;
131 if (pptr[0])
132 path = grub_strdup (pptr);
134 if ((!device || device[0] == ',' || !device[0]) || !path)
135 grub_machine_get_bootlocation (&fwdevice, &fwpath);
137 if (!device && fwdevice)
138 device = fwdevice;
139 else if (fwdevice && (device[0] == ',' || !device[0]))
141 /* We have a partition, but still need to fill in the drive. */
142 char *comma, *new_device;
144 for (comma = fwdevice; *comma; )
146 if (comma[0] == '\\' && comma[1] == ',')
148 comma += 2;
149 continue;
151 if (*comma == ',')
152 break;
153 comma++;
155 if (*comma)
157 char *drive = grub_strndup (fwdevice, comma - fwdevice);
158 new_device = grub_xasprintf ("%s%s", drive, device);
159 grub_free (drive);
161 else
162 new_device = grub_xasprintf ("%s%s", fwdevice, device);
164 grub_free (fwdevice);
165 grub_free (device);
166 device = new_device;
168 else
169 grub_free (fwdevice);
170 if (fwpath && !path)
171 path = fwpath;
172 else
173 grub_free (fwpath);
174 if (device)
176 char *prefix_set;
178 prefix_set = grub_xasprintf ("(%s)%s", device, path ? : "");
179 if (prefix_set)
181 grub_env_set ("prefix", prefix_set);
182 grub_free (prefix_set);
184 grub_env_set ("root", device);
187 grub_free (device);
188 grub_free (path);
189 grub_print_error ();
192 /* Load the normal mode module and execute the normal mode if possible. */
193 static void
194 grub_load_normal_mode (void)
196 /* Load the module. */
197 grub_dl_load ("normal");
199 /* Print errors if any. */
200 grub_print_error ();
201 grub_errno = 0;
203 grub_command_execute ("normal", 0, 0);
206 /* The main routine. */
207 void __attribute__ ((noreturn))
208 grub_main (void)
210 /* First of all, initialize the machine. */
211 grub_machine_init ();
213 /* Hello. */
214 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
215 grub_printf ("Welcome to GRUB!\n\n");
216 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
218 /* Load pre-loaded modules and free the space. */
219 grub_register_exported_symbols ();
220 #ifdef GRUB_LINKER_HAVE_INIT
221 grub_arch_dl_init_linker ();
222 #endif
223 grub_load_modules ();
225 /* It is better to set the root device as soon as possible,
226 for convenience. */
227 grub_set_prefix_and_root ();
228 grub_env_export ("root");
229 grub_env_export ("prefix");
231 grub_register_core_commands ();
233 grub_load_config ();
234 grub_load_normal_mode ();
235 grub_rescue_run ();