1 /* main.c - the kernel main routine */
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>
24 #include <grub/term.h>
25 #include <grub/file.h>
26 #include <grub/device.h>
29 #include <grub/command.h>
30 #include <grub/reader.h>
31 #include <grub/parser.h>
34 grub_module_iterate (int (*hook
) (struct grub_module_header
*header
))
36 struct grub_module_info
*modinfo
;
37 struct grub_module_header
*header
;
40 modbase
= grub_arch_modules_addr ();
41 modinfo
= (struct grub_module_info
*) modbase
;
43 /* Check if there are any modules. */
44 if ((modinfo
== 0) || modinfo
->magic
!= GRUB_MODULE_MAGIC
)
47 for (header
= (struct grub_module_header
*) (modbase
+ modinfo
->offset
);
48 header
< (struct grub_module_header
*) (modbase
+ modinfo
->size
);
49 header
= (struct grub_module_header
*) ((char *) header
+ header
->size
))
56 /* This is actualy platform-independant but used only on yeeloong and sparc. */
57 #if defined (GRUB_MACHINE_MIPS_YEELOONG) || defined (GRUB_MACHINE_SPARC64)
59 grub_modules_get_end (void)
61 struct grub_module_info
*modinfo
;
64 modbase
= grub_arch_modules_addr ();
65 modinfo
= (struct grub_module_info
*) modbase
;
67 /* Check if there are any modules. */
68 if ((modinfo
== 0) || modinfo
->magic
!= GRUB_MODULE_MAGIC
)
71 return modbase
+ modinfo
->size
;
75 /* Load all modules in core. */
77 grub_load_modules (void)
79 auto int hook (struct grub_module_header
*);
80 int hook (struct grub_module_header
*header
)
82 /* Not an ELF module, skip. */
83 if (header
->type
!= OBJ_TYPE_ELF
)
86 if (! grub_dl_load_core ((char *) header
+ sizeof (struct grub_module_header
),
87 (header
->size
- sizeof (struct grub_module_header
))))
88 grub_fatal ("%s", grub_errmsg
);
96 grub_module_iterate (hook
);
100 grub_load_config (void)
102 auto int hook (struct grub_module_header
*);
103 int hook (struct grub_module_header
*header
)
105 /* Not an embedded config, skip. */
106 if (header
->type
!= OBJ_TYPE_CONFIG
)
109 grub_parser_execute ((char *) header
+
110 sizeof (struct grub_module_header
));
114 grub_module_iterate (hook
);
117 /* Write hook for the environment variables of root. Remove surrounding
118 parentheses, if any. */
120 grub_env_write_root (struct grub_env_var
*var
__attribute__ ((unused
)),
123 /* XXX Is it better to check the existence of the device? */
124 grub_size_t len
= grub_strlen (val
);
126 if (val
[0] == '(' && val
[len
- 1] == ')')
127 return grub_strndup (val
+ 1, len
- 2);
129 return grub_strdup (val
);
132 /* Set the root device according to the dl prefix. */
134 grub_set_root_dev (void)
138 grub_register_variable_hook ("root", 0, grub_env_write_root
);
140 prefix
= grub_env_get ("prefix");
146 dev
= grub_file_get_device_name (prefix
);
149 grub_env_set ("root", dev
);
155 /* Load the normal mode module and execute the normal mode if possible. */
157 grub_load_normal_mode (void)
159 /* Load the module. */
160 grub_dl_load ("normal");
162 /* Something went wrong. Print errors here to let user know why we're entering rescue mode. */
166 grub_command_execute ("normal", 0, 0);
169 /* The main routine. */
173 /* First of all, initialize the machine. */
174 grub_machine_init ();
177 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT
);
178 grub_printf ("Welcome to GRUB!\n\n");
179 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD
);
181 /* Load pre-loaded modules and free the space. */
182 grub_register_exported_symbols ();
183 #ifdef GRUB_LINKER_HAVE_INIT
184 grub_arch_dl_init_linker ();
186 grub_load_modules ();
188 /* It is better to set the root device as soon as possible,
190 grub_machine_set_prefix ();
191 grub_set_root_dev ();
193 grub_register_core_commands ();
196 grub_load_normal_mode ();