make the linux-ppc packags be in synch with other platforms
[tangerine.git] / arch / common / boot / grub2 / kern / main.c
blob09de03ade05a0271e58443053413b3fb2a026759
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/mm.h>
23 #include <grub/symbol.h>
24 #include <grub/dl.h>
25 #include <grub/term.h>
26 #include <grub/rescue.h>
27 #include <grub/file.h>
28 #include <grub/device.h>
29 #include <grub/env.h>
31 /* Load all modules in core. */
32 static void
33 grub_load_modules (void)
35 struct grub_module_info *modinfo;
36 struct grub_module_header *header;
37 grub_addr_t modbase;
39 modbase = grub_arch_modules_addr ();
40 modinfo = (struct grub_module_info *) modbase;
42 /* Check if there are any modules. */
43 if ((modinfo == 0) || modinfo->magic != GRUB_MODULE_MAGIC)
44 return;
46 for (header = (struct grub_module_header *) (modbase + modinfo->offset);
47 header < (struct grub_module_header *) (modbase + modinfo->size);
48 header = (struct grub_module_header *) ((char *) header + header->size))
50 if (! grub_dl_load_core ((char *) header + header->offset,
51 (header->size - header->offset)))
52 grub_fatal ("%s", grub_errmsg);
55 /* Add the region where modules reside into dynamic memory. */
56 grub_mm_init_region ((void *) modinfo, modinfo->size);
59 /* Write hook for the environment variables of root. Remove surrounding
60 parentheses, if any. */
61 static char *
62 grub_env_write_root (struct grub_env_var *var __attribute__ ((unused)),
63 const char *val)
65 /* XXX Is it better to check the existence of the device? */
66 grub_size_t len = grub_strlen (val);
68 if (val[0] == '(' && val[len - 1] == ')')
69 return grub_strndup (val + 1, len - 2);
71 return grub_strdup (val);
74 /* Set the root device according to the dl prefix. */
75 static void
76 grub_set_root_dev (void)
78 const char *prefix;
80 grub_register_variable_hook ("root", 0, grub_env_write_root);
81 grub_env_export ("root");
83 prefix = grub_env_get ("prefix");
85 if (prefix)
87 char *dev;
89 dev = grub_file_get_device_name (prefix);
90 if (dev)
92 grub_env_set ("root", dev);
93 grub_free (dev);
98 /* Load the normal mode module and execute the normal mode if possible. */
99 static void
100 grub_load_normal_mode (void)
102 /* Load the module. */
103 grub_dl_load ("normal");
105 /* Something went wrong. Print errors here to let user know why we're entering rescue mode. */
106 grub_print_error ();
109 /* The main routine. */
110 void
111 grub_main (void)
113 /* First of all, initialize the machine. */
114 grub_machine_init ();
116 /* Hello. */
117 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
118 grub_printf ("Welcome to GRUB!\n\n");
119 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
121 /* Load pre-loaded modules and free the space. */
122 grub_register_exported_symbols ();
123 grub_load_modules ();
125 /* It is better to set the root device as soon as possible,
126 for convenience. */
127 grub_machine_set_prefix ();
128 grub_set_root_dev ();
130 /* Load the normal mode module. */
131 grub_load_normal_mode ();
133 /* Enter the rescue mode. */
134 grub_enter_rescue_mode ();