grub/kern/main.c
Robert Millan 58bc8bd5b1 2010-01-03 Robert Millan <rmh.grub@aybabtu.com>
* boot/i386/pc/boot.S: Update copyright year.
	* boot/i386/pc/cdboot.S: Likewise.
	* boot/i386/pc/diskboot.S: Likewise.
	* boot/i386/pc/lnxboot.S: Likewise.
	* boot/i386/pc/pxeboot.S: Likewise.
	* bus/pci.c: Likewise.
	* commands/cmp.c: Likewise.
	* commands/help.c: Likewise.
	* commands/hexdump.c: Likewise.
	* commands/i386/pc/halt.c: Likewise.
	* commands/i386/pc/play.c: Likewise.
	* commands/i386/pc/vbeinfo.c: Likewise.
	* commands/ls.c: Likewise.
	* commands/test.c: Likewise.
	* disk/dmraid_nvidia.c: Likewise.
	* disk/i386/pc/biosdisk.c: Likewise.
	* disk/ieee1275/nand.c: Likewise.
	* disk/ieee1275/ofdisk.c: Likewise.
	* disk/lvm.c: Likewise.
	* disk/raid.c: Likewise.
	* disk/raid6_recover.c: Likewise.
	* disk/scsi.c: Likewise.
	* fs/affs.c: Likewise.
	* fs/cpio.c: Likewise.
	* fs/ext2.c: Likewise.
	* fs/hfs.c: Likewise.
	* fs/iso9660.c: Likewise.
	* fs/ntfs.c: Likewise.
	* fs/sfs.c: Likewise.
	* fs/udf.c: Likewise.
	* fs/ufs.c: Likewise.
	* fs/xfs.c: Likewise.
	* gencmdlist.sh: Likewise.
	* genmk.rb: Likewise.
	* include/grub/disk.h: Likewise.
	* include/grub/efi/api.h: Likewise.
	* include/grub/efi/efi.h: Likewise.
	* include/grub/efi/pe32.h: Likewise.
	* include/grub/elf.h: Likewise.
	* include/grub/fs.h: Likewise.
	* include/grub/i386/at_keyboard.h: Likewise.
	* include/grub/i386/pc/memory.h: Likewise.
	* include/grub/i386/pc/vbe.h: Likewise.
	* include/grub/i386/pci.h: Likewise.
	* include/grub/i386/tsc.h: Likewise.
	* include/grub/ieee1275/ieee1275.h: Likewise.
	* include/grub/ntfs.h: Likewise.
	* include/grub/sparc64/ieee1275/ieee1275.h: Likewise.
	* include/grub/sparc64/libgcc.h: Likewise.
	* include/grub/symbol.h: Likewise.
	* include/grub/types.h: Likewise.
	* include/multiboot2.h: Likewise.
	* io/gzio.c: Likewise.
	* kern/device.c: Likewise.
	* kern/disk.c: Likewise.
	* kern/efi/efi.c: Likewise.
	* kern/efi/mm.c: Likewise.
	* kern/elf.c: Likewise.
	* kern/file.c: Likewise.
	* kern/i386/dl.c: Likewise.
	* kern/i386/pc/init.c: Likewise.
	* kern/i386/pc/startup.S: Likewise.
	* kern/ieee1275/ieee1275.c: Likewise.
	* kern/ieee1275/init.c: Likewise.
	* kern/main.c: Likewise.
	* kern/mm.c: Likewise.
	* kern/powerpc/dl.c: Likewise.
	* kern/sparc64/dl.c: Likewise.
	* kern/x86_64/dl.c: Likewise.
	* lib/hexdump.c: Likewise.
	* loader/efi/appleloader.c: Likewise.
	* loader/i386/ieee1275/linux.c: Likewise.
	* loader/i386/pc/chainloader.c: Likewise.
	* loader/i386/pc/linux.c: Likewise.
	* loader/i386/pc/multiboot2.c: Likewise.
	* loader/ieee1275/multiboot2.c: Likewise.
	* loader/multiboot2.c: Likewise.
	* loader/multiboot_loader.c: Likewise.
	* loader/powerpc/ieee1275/linux.c: Likewise.
	* normal/completion.c: Likewise.
	* normal/menu_entry.c: Likewise.
	* partmap/apple.c: Likewise.
	* util/grub.d/10_hurd.in: Likewise.
	* util/hostfs.c: Likewise.
	* video/readers/png.c: Likewise.
2010-01-03 22:05:07 +00:00

177 lines
4.5 KiB
C

/* main.c - the kernel main routine */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2002,2003,2005,2006,2008,2009 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* 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
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/kernel.h>
#include <grub/misc.h>
#include <grub/symbol.h>
#include <grub/dl.h>
#include <grub/term.h>
#include <grub/file.h>
#include <grub/device.h>
#include <grub/env.h>
#include <grub/mm.h>
#include <grub/command.h>
#include <grub/reader.h>
#include <grub/parser.h>
void
grub_module_iterate (int (*hook) (struct grub_module_header *header))
{
struct grub_module_info *modinfo;
struct grub_module_header *header;
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;
for (header = (struct grub_module_header *) (modbase + modinfo->offset);
header < (struct grub_module_header *) (modbase + modinfo->size);
header = (struct grub_module_header *) ((char *) header + header->size))
{
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))))
grub_fatal ("%s", grub_errmsg);
return 0;
}
grub_module_iterate (hook);
}
static void
grub_load_config (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_CONFIG)
return 0;
grub_parser_execute ((char *) header +
sizeof (struct grub_module_header));
return 1;
}
grub_module_iterate (hook);
}
/* Write hook for the environment variables of root. Remove surrounding
parentheses, if any. */
static char *
grub_env_write_root (struct grub_env_var *var __attribute__ ((unused)),
const char *val)
{
/* 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);
}
/* Set the root device according to the dl prefix. */
static void
grub_set_root_dev (void)
{
const char *prefix;
grub_register_variable_hook ("root", 0, grub_env_write_root);
grub_env_export ("root");
prefix = grub_env_get ("prefix");
if (prefix)
{
char *dev;
dev = grub_file_get_device_name (prefix);
if (dev)
{
grub_env_set ("root", dev);
grub_free (dev);
}
}
}
/* Load the normal mode module and execute the normal mode if possible. */
static void
grub_load_normal_mode (void)
{
/* Load the module. */
grub_dl_load ("normal");
/* Something went wrong. Print errors here to let user know why we're entering rescue mode. */
grub_print_error ();
grub_errno = 0;
grub_command_execute ("normal", 0, 0);
}
/* The main routine. */
void
grub_main (void)
{
/* First of all, initialize the machine. */
grub_machine_init ();
/* Hello. */
grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
grub_printf ("Welcome to GRUB!\n\n");
grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
/* Load pre-loaded modules and free the space. */
grub_register_exported_symbols ();
grub_load_modules ();
/* It is better to set the root device as soon as possible,
for convenience. */
grub_machine_set_prefix ();
grub_env_export ("prefix");
grub_set_root_dev ();
grub_register_core_commands ();
grub_register_rescue_parser ();
grub_register_rescue_reader ();
grub_load_config ();
grub_load_normal_mode ();
grub_reader_loop (0);
}