Add a few new files.
[grub2/phcoder/solaris.git] / kern / main.c
blobacf6911dd7a3cf58f32eba3bdb8fb6a4b91a9f9c
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/rescue.h>
26 #include <grub/file.h>
27 #include <grub/device.h>
28 #include <grub/env.h>
30 void
31 grub_module_iterate (int (*hook) (struct grub_module_header *header))
33 struct grub_module_info *modinfo;
34 struct grub_module_header *header;
35 grub_addr_t modbase;
37 modbase = grub_arch_modules_addr ();
38 modinfo = (struct grub_module_info *) modbase;
40 /* Check if there are any modules. */
41 if ((modinfo == 0) || modinfo->magic != GRUB_MODULE_MAGIC)
42 return;
44 for (header = (struct grub_module_header *) (modbase + modinfo->offset);
45 header < (struct grub_module_header *) (modbase + modinfo->size);
46 header = (struct grub_module_header *) ((char *) header + header->size))
48 if (hook (header))
49 break;
53 /* Load all modules in core. */
54 static void
55 grub_load_modules (void)
57 auto int hook (struct grub_module_header *);
58 int hook (struct grub_module_header *header)
60 /* Not an ELF module, skip. */
61 if (header->type != OBJ_TYPE_ELF)
62 return 0;
64 if (! grub_dl_load_core ((char *) header + sizeof (struct grub_module_header),
65 (header->size - sizeof (struct grub_module_header))))
66 grub_fatal ("%s", grub_errmsg);
68 return 0;
71 grub_module_iterate (hook);
74 /* Write hook for the environment variables of root. Remove surrounding
75 parentheses, if any. */
76 static char *
77 grub_env_write_root (struct grub_env_var *var __attribute__ ((unused)),
78 const char *val)
80 /* XXX Is it better to check the existence of the device? */
81 grub_size_t len = grub_strlen (val);
83 if (val[0] == '(' && val[len - 1] == ')')
84 return grub_strndup (val + 1, len - 2);
86 return grub_strdup (val);
89 /* Set the root device according to the dl prefix. */
90 static void
91 grub_set_root_dev (void)
93 const char *prefix;
95 grub_register_variable_hook ("root", 0, grub_env_write_root);
96 grub_env_export ("root");
98 prefix = grub_env_get ("prefix");
100 if (prefix)
102 char *dev;
104 dev = grub_file_get_device_name (prefix);
105 if (dev)
107 grub_env_set ("root", dev);
108 grub_free (dev);
113 /* Load the normal mode module and execute the normal mode if possible. */
114 static void
115 grub_load_normal_mode (void)
117 /* Load the module. */
118 grub_dl_load ("normal");
120 /* Something went wrong. Print errors here to let user know why we're entering rescue mode. */
121 grub_print_error ();
124 /* The main routine. */
125 void
126 grub_main (void)
128 /* First of all, initialize the machine. */
129 grub_machine_init ();
131 /* Hello. */
132 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
133 grub_printf ("Welcome to GRUB!\n\n");
134 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
136 /* Load pre-loaded modules and free the space. */
137 grub_register_exported_symbols ();
138 grub_load_modules ();
140 /* It is better to set the root device as soon as possible,
141 for convenience. */
142 grub_machine_set_prefix ();
143 grub_env_export ("prefix");
144 grub_set_root_dev ();
146 /* Load the normal mode module. */
147 grub_load_normal_mode ();
149 /* Enter the rescue mode. */
150 grub_enter_rescue_mode ();