]> git.proxmox.com Git - grub2.git/blob - grub-core/kern/main.c
Add noreturn attributes and remove unreachable code.
[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 /* 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)
37 {
38 struct grub_module_info *modinfo;
39
40 modinfo = (struct grub_module_info *) grub_modbase;
41
42 /* Check if there are any modules. */
43 if ((modinfo == 0) || modinfo->magic != GRUB_MODULE_MAGIC)
44 return grub_modbase;
45
46 return grub_modbase + modinfo->size;
47 }
48 #endif
49
50 /* Load all modules in core. */
51 static void
52 grub_load_modules (void)
53 {
54 struct grub_module_header *header;
55 FOR_MODULES (header)
56 {
57 /* Not an ELF module, skip. */
58 if (header->type != OBJ_TYPE_ELF)
59 continue;
60
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);
64
65 if (grub_errno)
66 grub_print_error ();
67 }
68 }
69
70 static void
71 grub_load_config (void)
72 {
73 struct grub_module_header *header;
74 FOR_MODULES (header)
75 {
76 /* Not an embedded config, skip. */
77 if (header->type != OBJ_TYPE_CONFIG)
78 continue;
79
80 grub_parser_execute ((char *) header +
81 sizeof (struct grub_module_header));
82 break;
83 }
84 }
85
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)
91 {
92 /* XXX Is it better to check the existence of the device? */
93 grub_size_t len = grub_strlen (val);
94
95 if (val[0] == '(' && val[len - 1] == ')')
96 return grub_strndup (val + 1, len - 2);
97
98 return grub_strdup (val);
99 }
100
101 static void
102 grub_set_prefix_and_root (void)
103 {
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;
110
111 FOR_MODULES (header)
112 if (header->type == OBJ_TYPE_PREFIX)
113 prefix = (char *) header + sizeof (struct grub_module_header);
114
115 grub_register_variable_hook ("root", 0, grub_env_write_root);
116
117 if (prefix)
118 {
119 char *pptr = NULL;
120 if (prefix[0] == '(')
121 {
122 pptr = grub_strrchr (prefix, ')');
123 if (pptr)
124 {
125 device = grub_strndup (prefix + 1, pptr - prefix - 1);
126 pptr++;
127 }
128 }
129 if (!pptr)
130 pptr = prefix;
131 if (pptr[0])
132 path = grub_strdup (pptr);
133 }
134 if ((!device || device[0] == ',' || !device[0]) || !path)
135 grub_machine_get_bootlocation (&fwdevice, &fwpath);
136
137 if (!device && fwdevice)
138 device = fwdevice;
139 else if (fwdevice && (device[0] == ',' || !device[0]))
140 {
141 /* We have a partition, but still need to fill in the drive. */
142 char *comma, *new_device;
143
144 comma = grub_strchr (fwdevice, ',');
145 if (comma)
146 {
147 char *drive = grub_strndup (fwdevice, comma - fwdevice);
148 new_device = grub_xasprintf ("%s%s", drive, device);
149 grub_free (drive);
150 }
151 else
152 new_device = grub_xasprintf ("%s%s", fwdevice, device);
153
154 grub_free (fwdevice);
155 grub_free (device);
156 device = new_device;
157 }
158 if (fwpath && !path)
159 path = fwpath;
160 if (device)
161 {
162 char *prefix_set;
163
164 prefix_set = grub_xasprintf ("(%s)%s", device, path ? : "");
165 if (prefix_set)
166 {
167 grub_env_set ("prefix", prefix_set);
168 grub_free (prefix_set);
169 }
170 grub_env_set ("root", device);
171 }
172
173 grub_free (device);
174 grub_free (path);
175 grub_print_error ();
176 }
177
178 /* Load the normal mode module and execute the normal mode if possible. */
179 static void
180 grub_load_normal_mode (void)
181 {
182 /* Load the module. */
183 grub_dl_load ("normal");
184
185 /* Print errors if any. */
186 grub_print_error ();
187 grub_errno = 0;
188
189 grub_command_execute ("normal", 0, 0);
190 }
191
192 /* The main routine. */
193 void __attribute__ ((noreturn))
194 grub_main (void)
195 {
196 /* First of all, initialize the machine. */
197 grub_machine_init ();
198
199 /* Hello. */
200 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
201 grub_printf ("Welcome to GRUB!\n\n");
202 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
203
204 /* Load pre-loaded modules and free the space. */
205 grub_register_exported_symbols ();
206 #ifdef GRUB_LINKER_HAVE_INIT
207 grub_arch_dl_init_linker ();
208 #endif
209 grub_load_modules ();
210
211 /* It is better to set the root device as soon as possible,
212 for convenience. */
213 grub_set_prefix_and_root ();
214 grub_env_export ("root");
215 grub_env_export ("prefix");
216
217 grub_register_core_commands ();
218
219 grub_load_config ();
220 grub_load_normal_mode ();
221 grub_rescue_run ();
222 }