]> git.proxmox.com Git - grub2.git/blob - grub-core/kern/main.c
Import grub2_2.02+dfsg1.orig.tar.xz
[grub2.git] / grub-core / kern / main.c
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.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
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>
32
33 #ifdef GRUB_MACHINE_PCBIOS
34 #include <grub/machine/memory.h>
35 #endif
36
37 grub_addr_t
38 grub_modules_get_end (void)
39 {
40 struct grub_module_info *modinfo;
41
42 modinfo = (struct grub_module_info *) grub_modbase;
43
44 /* Check if there are any modules. */
45 if ((modinfo == 0) || modinfo->magic != GRUB_MODULE_MAGIC)
46 return grub_modbase;
47
48 return grub_modbase + modinfo->size;
49 }
50
51 /* Load all modules in core. */
52 static void
53 grub_load_modules (void)
54 {
55 struct grub_module_header *header;
56 FOR_MODULES (header)
57 {
58 /* Not an ELF module, skip. */
59 if (header->type != OBJ_TYPE_ELF)
60 continue;
61
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);
65
66 if (grub_errno)
67 grub_print_error ();
68 }
69 }
70
71 static char *load_config;
72
73 static void
74 grub_load_config (void)
75 {
76 struct grub_module_header *header;
77 FOR_MODULES (header)
78 {
79 /* Not an embedded config, skip. */
80 if (header->type != OBJ_TYPE_CONFIG)
81 continue;
82
83 load_config = grub_malloc (header->size - sizeof (struct grub_module_header) + 1);
84 if (!load_config)
85 {
86 grub_print_error ();
87 break;
88 }
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;
94 }
95 }
96
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)
102 {
103 /* XXX Is it better to check the existence of the device? */
104 grub_size_t len = grub_strlen (val);
105
106 if (val[0] == '(' && val[len - 1] == ')')
107 return grub_strndup (val + 1, len - 2);
108
109 return grub_strdup (val);
110 }
111
112 static void
113 grub_set_prefix_and_root (void)
114 {
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;
121
122 FOR_MODULES (header)
123 if (header->type == OBJ_TYPE_PREFIX)
124 prefix = (char *) header + sizeof (struct grub_module_header);
125
126 grub_register_variable_hook ("root", 0, grub_env_write_root);
127
128 grub_machine_get_bootlocation (&fwdevice, &fwpath);
129
130 if (fwdevice)
131 {
132 char *cmdpath;
133
134 cmdpath = grub_xasprintf ("(%s)%s", fwdevice, fwpath ? : "");
135 if (cmdpath)
136 {
137 grub_env_set ("cmdpath", cmdpath);
138 grub_env_export ("cmdpath");
139 grub_free (cmdpath);
140 }
141 }
142
143 if (prefix)
144 {
145 char *pptr = NULL;
146 if (prefix[0] == '(')
147 {
148 pptr = grub_strrchr (prefix, ')');
149 if (pptr)
150 {
151 device = grub_strndup (prefix + 1, pptr - prefix - 1);
152 pptr++;
153 }
154 }
155 if (!pptr)
156 pptr = prefix;
157 if (pptr[0])
158 path = grub_strdup (pptr);
159 }
160
161 if (!device && fwdevice)
162 device = fwdevice;
163 else if (fwdevice && (device[0] == ',' || !device[0]))
164 {
165 /* We have a partition, but still need to fill in the drive. */
166 char *comma, *new_device;
167
168 for (comma = fwdevice; *comma; )
169 {
170 if (comma[0] == '\\' && comma[1] == ',')
171 {
172 comma += 2;
173 continue;
174 }
175 if (*comma == ',')
176 break;
177 comma++;
178 }
179 if (*comma)
180 {
181 char *drive = grub_strndup (fwdevice, comma - fwdevice);
182 new_device = grub_xasprintf ("%s%s", drive, device);
183 grub_free (drive);
184 }
185 else
186 new_device = grub_xasprintf ("%s%s", fwdevice, device);
187
188 grub_free (fwdevice);
189 grub_free (device);
190 device = new_device;
191 }
192 else
193 grub_free (fwdevice);
194 if (fwpath && !path)
195 {
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;
204 }
205 else
206 grub_free (fwpath);
207 if (device)
208 {
209 char *prefix_set;
210
211 prefix_set = grub_xasprintf ("(%s)%s", device, path ? : "");
212 if (prefix_set)
213 {
214 grub_env_set ("prefix", prefix_set);
215 grub_free (prefix_set);
216 }
217 grub_env_set ("root", device);
218 }
219
220 grub_free (device);
221 grub_free (path);
222 grub_print_error ();
223 }
224
225 /* Load the normal mode module and execute the normal mode if possible. */
226 static void
227 grub_load_normal_mode (void)
228 {
229 /* Load the module. */
230 grub_dl_load ("normal");
231
232 /* Print errors if any. */
233 grub_print_error ();
234 grub_errno = 0;
235
236 grub_command_execute ("normal", 0, 0);
237 }
238
239 static void
240 reclaim_module_space (void)
241 {
242 grub_addr_t modstart, modend;
243
244 if (!grub_modbase)
245 return;
246
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;
254
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
261 }
262
263 /* The main routine. */
264 void __attribute__ ((noreturn))
265 grub_main (void)
266 {
267 /* First of all, initialize the machine. */
268 grub_machine_init ();
269
270 grub_boot_time ("After machine init.");
271
272 /* Hello. */
273 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
274 grub_printf ("Welcome to GRUB!\n\n");
275 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
276
277 grub_load_config ();
278
279 grub_boot_time ("Before loading embedded modules.");
280
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 ();
287
288 grub_boot_time ("After loading embedded modules.");
289
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");
295
296 /* Reclaim space used for modules. */
297 reclaim_module_space ();
298
299 grub_boot_time ("After reclaiming module space.");
300
301 grub_register_core_commands ();
302
303 grub_boot_time ("Before execution of embedded config.");
304
305 if (load_config)
306 grub_parser_execute (load_config);
307
308 grub_boot_time ("After execution of embedded config. Attempt to go to normal mode");
309
310 grub_load_normal_mode ();
311 grub_rescue_run ();
312 }