2N/A/* main.c - the kernel main routine */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2002,2003,2005,2006,2008,2009 Free Software Foundation, Inc.
2N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/kernel.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/symbol.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/term.h>
2N/A#include <grub/file.h>
2N/A#include <grub/device.h>
2N/A#include <grub/env.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/command.h>
2N/A#include <grub/reader.h>
2N/A#include <grub/parser.h>
2N/A
2N/A/* This is actualy platform-independant but used only on loongson and sparc. */
2N/A#if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS) || defined (GRUB_MACHINE_SPARC64)
2N/Agrub_addr_t
2N/Agrub_modules_get_end (void)
2N/A{
2N/A struct grub_module_info *modinfo;
2N/A
2N/A modinfo = (struct grub_module_info *) grub_modbase;
2N/A
2N/A /* Check if there are any modules. */
2N/A if ((modinfo == 0) || modinfo->magic != GRUB_MODULE_MAGIC)
2N/A return grub_modbase;
2N/A
2N/A return grub_modbase + modinfo->size;
2N/A}
2N/A#endif
2N/A
2N/A/* Load all modules in core. */
2N/Astatic void
2N/Agrub_load_modules (void)
2N/A{
2N/A struct grub_module_header *header;
2N/A FOR_MODULES (header)
2N/A {
2N/A /* Not an ELF module, skip. */
2N/A if (header->type != OBJ_TYPE_ELF)
2N/A continue;
2N/A
2N/A if (! grub_dl_load_core ((char *) header + sizeof (struct grub_module_header),
2N/A (header->size - sizeof (struct grub_module_header))))
2N/A grub_fatal ("%s", grub_errmsg);
2N/A
2N/A if (grub_errno)
2N/A grub_print_error ();
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Agrub_load_config (void)
2N/A{
2N/A struct grub_module_header *header;
2N/A FOR_MODULES (header)
2N/A {
2N/A /* Not an embedded config, skip. */
2N/A if (header->type != OBJ_TYPE_CONFIG)
2N/A continue;
2N/A
2N/A grub_parser_execute ((char *) header +
2N/A sizeof (struct grub_module_header));
2N/A break;
2N/A }
2N/A}
2N/A
2N/A/* Write hook for the environment variables of root. Remove surrounding
2N/A parentheses, if any. */
2N/Astatic char *
2N/Agrub_env_write_root (struct grub_env_var *var __attribute__ ((unused)),
2N/A const char *val)
2N/A{
2N/A /* XXX Is it better to check the existence of the device? */
2N/A grub_size_t len = grub_strlen (val);
2N/A
2N/A if (val[0] == '(' && val[len - 1] == ')')
2N/A return grub_strndup (val + 1, len - 2);
2N/A
2N/A return grub_strdup (val);
2N/A}
2N/A
2N/Astatic void
2N/Agrub_set_prefix_and_root (void)
2N/A{
2N/A char *device = NULL;
2N/A char *path = NULL;
2N/A char *fwdevice = NULL;
2N/A char *fwpath = NULL;
2N/A char *prefix = NULL;
2N/A struct grub_module_header *header;
2N/A
2N/A FOR_MODULES (header)
2N/A if (header->type == OBJ_TYPE_PREFIX)
2N/A prefix = (char *) header + sizeof (struct grub_module_header);
2N/A
2N/A grub_register_variable_hook ("root", 0, grub_env_write_root);
2N/A
2N/A if (prefix)
2N/A {
2N/A char *pptr = NULL;
2N/A if (prefix[0] == '(')
2N/A {
2N/A pptr = grub_strrchr (prefix, ')');
2N/A if (pptr)
2N/A {
2N/A device = grub_strndup (prefix + 1, pptr - prefix - 1);
2N/A pptr++;
2N/A }
2N/A }
2N/A if (!pptr)
2N/A pptr = prefix;
2N/A if (pptr[0])
2N/A path = grub_strdup (pptr);
2N/A }
2N/A#if 0
2N/A /* Commented this out because we always want network interfaces
2N/A * plumbed, even when we have a prefix on memdisk. We could check
2N/A * if the devicename is actually memdisk and only do this then, but
2N/A * there does not appear to be any harm in doing it unconditionally
2N/A */
2N/A if ((!device || device[0] == ',' || !device[0]) || !path)
2N/A#endif
2N/A grub_machine_get_bootlocation (&fwdevice, &fwpath);
2N/A
2N/A if (!device && fwdevice)
2N/A device = fwdevice;
2N/A else if (fwdevice && (device[0] == ',' || !device[0]))
2N/A {
2N/A /* We have a partition, but still need to fill in the drive. */
2N/A char *comma, *new_device;
2N/A
2N/A comma = grub_strchr (fwdevice, ',');
2N/A if (comma)
2N/A {
2N/A char *drive = grub_strndup (fwdevice, comma - fwdevice);
2N/A new_device = grub_xasprintf ("%s%s", drive, device);
2N/A grub_free (drive);
2N/A }
2N/A else
2N/A new_device = grub_xasprintf ("%s%s", fwdevice, device);
2N/A
2N/A grub_free (fwdevice);
2N/A grub_free (device);
2N/A device = new_device;
2N/A }
2N/A if (fwpath && !path)
2N/A path = fwpath;
2N/A if (device)
2N/A {
2N/A char *prefix_set;
2N/A
2N/A prefix_set = grub_xasprintf ("(%s)%s", device, path ? : "");
2N/A if (prefix_set)
2N/A {
2N/A grub_env_set ("prefix", prefix_set);
2N/A grub_free (prefix_set);
2N/A }
2N/A if (fwdevice)
2N/A grub_env_set ("root", fwdevice);
2N/A else
2N/A grub_env_set ("root", device);
2N/A }
2N/A
2N/A grub_free (device);
2N/A grub_free (path);
2N/A grub_print_error ();
2N/A}
2N/A
2N/A/* Load the normal mode module and execute the normal mode if possible. */
2N/Astatic void
2N/Agrub_load_normal_mode (void)
2N/A{
2N/A /* Load the module. */
2N/A grub_dl_load ("normal");
2N/A
2N/A /* Print errors if any. */
2N/A grub_print_error ();
2N/A grub_errno = 0;
2N/A
2N/A grub_command_execute ("normal", 0, 0);
2N/A}
2N/A
2N/A/* The main routine. */
2N/Avoid __attribute__ ((noreturn))
2N/Agrub_main (void)
2N/A{
2N/A /* First of all, initialize the machine. */
2N/A grub_machine_init ();
2N/A
2N/A /* Hello. */
2N/A grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
2N/A grub_printf ("Welcome to GRUB!\n\n");
2N/A grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
2N/A
2N/A /* Load pre-loaded modules and free the space. */
2N/A grub_register_exported_symbols ();
2N/A#ifdef GRUB_LINKER_HAVE_INIT
2N/A grub_arch_dl_init_linker ();
2N/A#endif
2N/A grub_load_modules ();
2N/A
2N/A /* It is better to set the root device as soon as possible,
2N/A for convenience. */
2N/A grub_set_prefix_and_root ();
2N/A grub_env_export ("root");
2N/A grub_env_export ("prefix");
2N/A
2N/A grub_register_core_commands ();
2N/A
2N/A grub_load_config ();
2N/A grub_load_normal_mode ();
2N/A grub_rescue_run ();
2N/A}