grub/kern/main.c
okuji 2965c7cc2d 2006-04-25 Yoshinori K. Okuji <okuji@enbug.org>
A new machine-specific function "grub_machine_set_prefix" is
        defined. This is called after loading modules, so that a prefix
        initialization can use modules. Also, this change adds an
        intensive debugging feature for the memory manager via the
        configure option "--enable-mm-debug".

        * partmap/gpt.c (gpt_partition_map_iterate): Add one more into
        PART.LEN.

        * kern/sparc64/ieee1275/init.c (abort): Removed.
        (grub_stop): Likewise.
        (grub_exit): New function.
        (grub_set_prefix): Renamed to ...
        (grub_machine_set_prefix): ... this.
        (grub_machine_init): Do not call grub_set_prefix.

        * kern/powerpc/ieee1275/init.c (grub_set_prefix): Renamed to ...
        (grub_machine_set_prefix): ... this.
        (grub_machine_init): Do not call grub_set_prefix.

        * kern/i386/pc/init.c (grub_machine_set_prefix): New function.
        (grub_machine_init): Do not set the prefix here.

        * kern/i386/efi/init.c (grub_machine_set_prefix): New function.

        * kern/efi/init.c: Include grub/mm.h.
        (grub_efi_set_prefix): New function.

        * kern/efi/efi.c (grub_exit): Call grub_efi_fini.
        (grub_efi_get_filename): New function.
        (grub_print_device_path): Renamed to ...
        (grub_efi_print_device_path): ... this.

        * kern/mm.c [MM_DEBUG] (grub_malloc): Undefined.
        [MM_DEBUG] (grub_realloc): Likewise.
        [MM_DEBUG] (grub_free): Likewise.
        [MM_DEBUG] (grub_memalign): Likewise.
        [MM_DEBUG] (grub_mm_debug): New variable.
        [MM_DEBUG] (grub_debug_malloc): New function.
        [MM_DEBUG] (grub_debug_free): New function.
        [MM_DEBUG] (grub_debug_realloc): New function.
        [MM_DEBUG] (grub_debug_memalign): New function.

        * kern/misc.c (grub_abort): Print a newline to distinguish
        the message.

        * kern/main.c (grub_main): Call grub_machine_set_prefix and
        grub_set_root_dev after loading modules. This is necessary when
        setting a prefix depends on modules.

        * include/grub/efi/efi.h (grub_print_device_path): Renamed to ...
        (grub_efi_print_device_path): ... this.
        (grub_efi_get_filename): New prototype.
        (grub_efi_set_prefix): Likewise.

        * include/grub/efi/disk.h: Include grub/efi/api.h, grub/symbol.h
        and grub/disk.h.
        (grub_efidisk_get_device_handle): New prototype.
        (grub_efidisk_get_device_name): Likewise.

        * include/grub/mm.h: Include config.h.
        (MM_DEBUG): Removed.
        [MM_DEBUG && !GRUB_UTIL] (grub_mm_debug): New prototype.
        [MM_DEBUG && !GRUB_UTIL] (grub_malloc): New macro.
        [MM_DEBUG && !GRUB_UTIL] (grub_realloc): Likewise.
        [MM_DEBUG && !GRUB_UTIL] (grub_memalign): Likewise.
        [MM_DEBUG && !GRUB_UTIL] (grub_free): Likewise.
        [MM_DEBUG && !GRUB_UTIL] (grub_debug_malloc): New prototype.
        [MM_DEBUG && !GRUB_UTIL] (grub_debug_realloc): New prototype.
        [MM_DEBUG && !GRUB_UTIL] (grub_debug_memalign): New prototype.
        [MM_DEBUG && !GRUB_UTIL] (grub_debug_free): New prototype.

        * include/grub/kernel.h (grub_machine_set_prefix): New prototype.

        * disk/efi/efidisk.c: Include grub/partition.h.
        (iterate_child_devices): New function.
        (add_device): First, compare only last device path nodes, so that
        devices are sorted by the types.
        (grub_efidisk_get_device_handle): New function.
        (grub_efidisk_get_device_name): Likewise.

        * configure.ac (--enable-mm-debug): New option to enable the
        memory manager debugging feature. This makes the binary much
        bigger, so is disabled by default.
2006-04-25 20:08:31 +00:00

135 lines
3.7 KiB
C

/* main.c - the kernel main routine */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <grub/kernel.h>
#include <grub/misc.h>
#include <grub/mm.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>
/* Load all modules in core. */
static void
grub_load_modules (void)
{
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 (! grub_dl_load_core ((char *) header + header->offset,
(header->size - header->offset)))
grub_fatal ("%s", grub_errmsg);
}
/* Add the region where modules reside into dynamic memory. */
grub_mm_init_region ((void *) modinfo, modinfo->size);
}
/* 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);
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");
/* Ignore any error, because we have the rescue mode anyway. */
grub_errno = GRUB_ERR_NONE;
}
/* 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_set_root_dev ();
/* Load the normal mode module. */
grub_load_normal_mode ();
/* Enter the rescue mode. */
grub_enter_rescue_mode ();
}