2003-01-07 07:54:07 +00:00
|
|
|
/* main.c - the kernel main routine */
|
2002-12-27 08:53:07 +00:00
|
|
|
/*
|
2004-04-04 13:46:03 +00:00
|
|
|
* GRUB -- GRand Unified Bootloader
|
2008-01-05 12:04:35 +00:00
|
|
|
* Copyright (C) 2002,2003,2005,2006,2008 Free Software Foundation, Inc.
|
2002-12-27 08:53:07 +00:00
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2002-12-27 08:53:07 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-21 23:32:33 +00:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2002-12-27 08:53:07 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
2002-12-27 08:53:07 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-21 23:32:33 +00:00
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
2002-12-27 08:53:07 +00:00
|
|
|
*/
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/kernel.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/symbol.h>
|
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/term.h>
|
|
|
|
#include <grub/rescue.h>
|
|
|
|
#include <grub/file.h>
|
|
|
|
#include <grub/device.h>
|
|
|
|
#include <grub/env.h>
|
2008-08-04 22:54:06 +00:00
|
|
|
#include <grub/mm.h>
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2008-08-02 12:17:44 +00:00
|
|
|
void
|
|
|
|
grub_module_iterate (int (*hook) (struct grub_module_header *header))
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2005-01-04 14:01:45 +00:00
|
|
|
struct grub_module_info *modinfo;
|
2004-04-04 13:46:03 +00:00
|
|
|
struct grub_module_header *header;
|
2005-01-04 14:01:45 +00:00
|
|
|
grub_addr_t modbase;
|
|
|
|
|
|
|
|
modbase = grub_arch_modules_addr ();
|
|
|
|
modinfo = (struct grub_module_info *) modbase;
|
|
|
|
|
|
|
|
/* Check if there are any modules. */
|
|
|
|
if ((modinfo == 0) || modinfo->magic != GRUB_MODULE_MAGIC)
|
|
|
|
return;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2005-01-04 14:01:45 +00:00
|
|
|
for (header = (struct grub_module_header *) (modbase + modinfo->offset);
|
|
|
|
header < (struct grub_module_header *) (modbase + modinfo->size);
|
2004-04-04 13:46:03 +00:00
|
|
|
header = (struct grub_module_header *) ((char *) header + header->size))
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2008-08-02 12:17:44 +00:00
|
|
|
if (hook (header))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load all modules in core. */
|
|
|
|
static void
|
|
|
|
grub_load_modules (void)
|
|
|
|
{
|
|
|
|
auto int hook (struct grub_module_header *);
|
|
|
|
int hook (struct grub_module_header *header)
|
|
|
|
{
|
|
|
|
/* Not an ELF module, skip. */
|
|
|
|
if (header->type != OBJ_TYPE_ELF)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (! grub_dl_load_core ((char *) header + sizeof (struct grub_module_header),
|
|
|
|
(header->size - sizeof (struct grub_module_header))))
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_fatal ("%s", grub_errmsg);
|
2008-08-02 12:17:44 +00:00
|
|
|
|
|
|
|
return 0;
|
2002-12-27 08:53:07 +00:00
|
|
|
}
|
|
|
|
|
2008-08-02 12:17:44 +00:00
|
|
|
grub_module_iterate (hook);
|
2002-12-27 08:53:07 +00:00
|
|
|
}
|
|
|
|
|
2005-07-20 20:30:46 +00:00
|
|
|
/* Write hook for the environment variables of root. Remove surrounding
|
|
|
|
parentheses, if any. */
|
|
|
|
static char *
|
2005-08-12 19:53:33 +00:00
|
|
|
grub_env_write_root (struct grub_env_var *var __attribute__ ((unused)),
|
|
|
|
const char *val)
|
2005-07-20 20:30:46 +00:00
|
|
|
{
|
|
|
|
/* XXX Is it better to check the existence of the device? */
|
|
|
|
grub_size_t len = grub_strlen (val);
|
|
|
|
|
|
|
|
if (val[0] == '(' && val[len - 1] == ')')
|
|
|
|
return grub_strndup (val + 1, len - 2);
|
|
|
|
|
|
|
|
return grub_strdup (val);
|
|
|
|
}
|
|
|
|
|
2003-01-07 07:54:07 +00:00
|
|
|
/* Set the root device according to the dl prefix. */
|
|
|
|
static void
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_set_root_dev (void)
|
2003-01-07 07:54:07 +00:00
|
|
|
{
|
|
|
|
const char *prefix;
|
|
|
|
|
2005-07-20 20:30:46 +00:00
|
|
|
grub_register_variable_hook ("root", 0, grub_env_write_root);
|
2008-01-05 12:04:35 +00:00
|
|
|
grub_env_export ("root");
|
2005-07-20 20:30:46 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
prefix = grub_env_get ("prefix");
|
2003-01-07 07:54:07 +00:00
|
|
|
|
|
|
|
if (prefix)
|
|
|
|
{
|
|
|
|
char *dev;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
dev = grub_file_get_device_name (prefix);
|
2003-01-07 07:54:07 +00:00
|
|
|
if (dev)
|
|
|
|
{
|
2005-07-20 20:30:46 +00:00
|
|
|
grub_env_set ("root", dev);
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_free (dev);
|
2003-01-07 07:54:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the normal mode module and execute the normal mode if possible. */
|
|
|
|
static void
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_load_normal_mode (void)
|
2003-01-07 07:54:07 +00:00
|
|
|
{
|
2003-01-20 04:13:46 +00:00
|
|
|
/* Load the module. */
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_dl_load ("normal");
|
2003-01-07 07:54:07 +00:00
|
|
|
|
2008-02-10 17:05:10 +00:00
|
|
|
/* Something went wrong. Print errors here to let user know why we're entering rescue mode. */
|
|
|
|
grub_print_error ();
|
2003-01-07 07:54:07 +00:00
|
|
|
}
|
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
/* The main routine. */
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_main (void)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
|
|
|
/* First of all, initialize the machine. */
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_machine_init ();
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
/* Hello. */
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
|
|
|
|
grub_printf ("Welcome to GRUB!\n\n");
|
|
|
|
grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2003-01-07 07:54:07 +00:00
|
|
|
/* Load pre-loaded modules and free the space. */
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_register_exported_symbols ();
|
|
|
|
grub_load_modules ();
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2006-04-25 20:08:31 +00:00
|
|
|
/* It is better to set the root device as soon as possible,
|
|
|
|
for convenience. */
|
|
|
|
grub_machine_set_prefix ();
|
2008-06-19 20:08:57 +00:00
|
|
|
grub_env_export ("prefix");
|
2006-04-25 20:08:31 +00:00
|
|
|
grub_set_root_dev ();
|
|
|
|
|
2003-01-20 04:13:46 +00:00
|
|
|
/* Load the normal mode module. */
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_load_normal_mode ();
|
2003-01-07 07:54:07 +00:00
|
|
|
|
2003-01-20 04:13:46 +00:00
|
|
|
/* Enter the rescue mode. */
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_enter_rescue_mode ();
|
2002-12-27 08:53:07 +00:00
|
|
|
}
|