Merge branch 'dmigen' into phcoder
[grub2/phcoder.git] / kern / main.c
blob9215d55e767f18238252c48126428303a7cf9ecd
1 /* main.c - the kernel main routine */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2003,2005,2006,2008 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 void
34 grub_module_iterate (int (*hook) (struct grub_module_header *header))
36 struct grub_module_info *modinfo;
37 struct grub_module_header *header;
38 grub_addr_t modbase;
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)
45 return;
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))
51 if (hook (header))
52 break;
56 /* Load all modules in core. */
57 static void
58 grub_load_modules (void)
60 auto int hook (struct grub_module_header *);
61 int hook (struct grub_module_header *header)
63 /* Not an ELF module, skip. */
64 if (header->type != OBJ_TYPE_ELF)
65 return 0;
67 if (! grub_dl_load_core ((char *) header + sizeof (struct grub_module_header),
68 (header->size - sizeof (struct grub_module_header))))
69 grub_fatal ("%s", grub_errmsg);
71 return 0;
74 grub_module_iterate (hook);
77 static void
78 grub_load_config (void)
80 auto int hook (struct grub_module_header *);
81 int hook (struct grub_module_header *header)
83 /* Not an ELF module, skip. */
84 if (header->type != OBJ_TYPE_CONFIG)
85 return 0;
87 grub_parser_execute ((char *) header +
88 sizeof (struct grub_module_header));
89 return 1;
92 grub_module_iterate (hook);
95 /* Write hook for the environment variables of root. Remove surrounding
96 parentheses, if any. */
97 static char *
98 grub_env_write_root (struct grub_env_var *var __attribute__ ((unused)),
99 const char *val)
101 /* XXX Is it better to check the existence of the device? */
102 grub_size_t len = grub_strlen (val);
104 if (val[0] == '(' && val[len - 1] == ')')
105 return grub_strndup (val + 1, len - 2);
107 return grub_strdup (val);
110 /* Set the root device according to the dl prefix. */
111 static void
112 grub_set_root_dev (void)
114 const char *prefix;
116 grub_register_variable_hook ("root", 0, grub_env_write_root);
117 grub_env_export ("root");
119 prefix = grub_env_get ("prefix");
121 if (prefix)
123 char *dev;
125 dev = grub_file_get_device_name (prefix);
126 if (dev)
128 grub_env_set ("root", dev);
129 grub_free (dev);
134 /* Load the normal mode module and execute the normal mode if possible. */
135 static void
136 grub_load_normal_mode (void)
138 /* Load the module. */
139 grub_dl_load ("normal");
141 /* Something went wrong. Print errors here to let user know why we're entering rescue mode. */
142 grub_print_error ();
143 grub_errno = 0;
145 grub_command_execute ("normal", 0, 0);
148 /* The main routine. */
149 void
150 grub_main (void)
152 /* First of all, initialize the machine. */
153 grub_machine_init ();
155 /* Hello. */
156 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
157 grub_printf ("Welcome to GRUB!\n\n");
158 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
160 /* Load pre-loaded modules and free the space. */
161 grub_register_exported_symbols ();
162 grub_load_modules ();
164 /* It is better to set the root device as soon as possible,
165 for convenience. */
166 grub_machine_set_prefix ();
167 grub_env_export ("prefix");
168 grub_set_root_dev ();
170 grub_register_core_commands ();
171 grub_register_rescue_parser ();
172 grub_register_rescue_reader ();
174 grub_load_config ();
175 grub_load_normal_mode ();
176 grub_reader_loop (0);