grub/kern/main.c

118 lines
3.1 KiB
C
Raw Normal View History

/* main.c - the kernel main routine */
2002-12-27 08:53:07 +00:00
/*
* GRUB -- GRand Unified Bootloader
2005-01-04 Hollis Blanchard <hollis@penguinppc.org> * TODO: Add note about endianness in grub-mkimage. * boot/powerpc/ieee1275/crt0.S (note): Remove unused .note section. * conf/powerpc-ieee1275.rmk (bin_UTILITIES): Add grub-mkimage. (grub_mkimage_SOURCES): New target. * include/grub/kernel.h (grub_start_addr): Remove variable. (grub_end_addr): Likewise. (grub_total_module_size): Likewise. (grub_kernel_image_size): Likewise. (GRUB_MODULE_MAGIC): New constant. (grub_module_info): New structure. (grub_arch_modules_addr): New prototype. (grub_get_end_addr): Remove prototype. * include/grub/i386/pc/kernel.h (grub_end_addr): New prototype. * include/grub/powerpc/ieee1275/kernel.h: New file. * include/grub/util/misc.h (grub_util_get_fp_size): New prototype. (grub_util_read_at): Likewise. (grub_util_write_image_at): Likewise. * kern/main.c (grub_get_end_addr): Remove function. (grub_load_modules): Call grub_arch_modules_addr instead of using grub_end_addr. Look for a grub_module_info struct in memory. Use the grub_module_info fields instead of calling grub_get_end_addr as loop conditions. Move grub_add_unused_region code here. (grub_add_unused_region): Remove function. * kern/i386/pc/init.c: Include grub/cache.h. (grub_machine_init): Remove call to grub_get_end_addr. Remove one call to add_mem_region. (grub_arch_modules_addr): New function. * kern/powerpc/ieee1275/init.c (grub_end_addr): Remove variable. (grub_total_module_size): Likewise. Include grub/machine/kernel.h. (grub_arch_modules_addr): New function. * util/grub-emu.c (grub_end_addr): Remove variable. (grub_total_module_size): Likewise. (grub_arch_modules_addr): New function. * util/misc.c: Include unistd.h. (grub_util_get_fp_size): New function. (grub_util_read_at): Likewise. (grub_util_write_image_at): Likewise. (grub_util_read_image): Call grub_util_read_at. (grub_util_write_image): Call grub_util_write_image_at. * util/i386/pc/grub-mkimage.c (generate_image): Allocate additional memory in kernel_img for a struct grub_module_info. Fill in that grub_module_info. * util/powerpc/ieee1275/grub-mkimage.c: New file.
2005-01-04 14:01:45 +00:00
* Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
2002-12-27 08:53:07 +00:00
*
* 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>
2002-12-27 08:53:07 +00:00
/* Load all modules in core. */
static void
grub_load_modules (void)
2002-12-27 08:53:07 +00:00
{
2005-01-04 Hollis Blanchard <hollis@penguinppc.org> * TODO: Add note about endianness in grub-mkimage. * boot/powerpc/ieee1275/crt0.S (note): Remove unused .note section. * conf/powerpc-ieee1275.rmk (bin_UTILITIES): Add grub-mkimage. (grub_mkimage_SOURCES): New target. * include/grub/kernel.h (grub_start_addr): Remove variable. (grub_end_addr): Likewise. (grub_total_module_size): Likewise. (grub_kernel_image_size): Likewise. (GRUB_MODULE_MAGIC): New constant. (grub_module_info): New structure. (grub_arch_modules_addr): New prototype. (grub_get_end_addr): Remove prototype. * include/grub/i386/pc/kernel.h (grub_end_addr): New prototype. * include/grub/powerpc/ieee1275/kernel.h: New file. * include/grub/util/misc.h (grub_util_get_fp_size): New prototype. (grub_util_read_at): Likewise. (grub_util_write_image_at): Likewise. * kern/main.c (grub_get_end_addr): Remove function. (grub_load_modules): Call grub_arch_modules_addr instead of using grub_end_addr. Look for a grub_module_info struct in memory. Use the grub_module_info fields instead of calling grub_get_end_addr as loop conditions. Move grub_add_unused_region code here. (grub_add_unused_region): Remove function. * kern/i386/pc/init.c: Include grub/cache.h. (grub_machine_init): Remove call to grub_get_end_addr. Remove one call to add_mem_region. (grub_arch_modules_addr): New function. * kern/powerpc/ieee1275/init.c (grub_end_addr): Remove variable. (grub_total_module_size): Likewise. Include grub/machine/kernel.h. (grub_arch_modules_addr): New function. * util/grub-emu.c (grub_end_addr): Remove variable. (grub_total_module_size): Likewise. (grub_arch_modules_addr): New function. * util/misc.c: Include unistd.h. (grub_util_get_fp_size): New function. (grub_util_read_at): Likewise. (grub_util_write_image_at): Likewise. (grub_util_read_image): Call grub_util_read_at. (grub_util_write_image): Call grub_util_write_image_at. * util/i386/pc/grub-mkimage.c (generate_image): Allocate additional memory in kernel_img for a struct grub_module_info. Fill in that grub_module_info. * util/powerpc/ieee1275/grub-mkimage.c: New file.
2005-01-04 14:01:45 +00:00
struct grub_module_info *modinfo;
struct grub_module_header *header;
2005-01-04 Hollis Blanchard <hollis@penguinppc.org> * TODO: Add note about endianness in grub-mkimage. * boot/powerpc/ieee1275/crt0.S (note): Remove unused .note section. * conf/powerpc-ieee1275.rmk (bin_UTILITIES): Add grub-mkimage. (grub_mkimage_SOURCES): New target. * include/grub/kernel.h (grub_start_addr): Remove variable. (grub_end_addr): Likewise. (grub_total_module_size): Likewise. (grub_kernel_image_size): Likewise. (GRUB_MODULE_MAGIC): New constant. (grub_module_info): New structure. (grub_arch_modules_addr): New prototype. (grub_get_end_addr): Remove prototype. * include/grub/i386/pc/kernel.h (grub_end_addr): New prototype. * include/grub/powerpc/ieee1275/kernel.h: New file. * include/grub/util/misc.h (grub_util_get_fp_size): New prototype. (grub_util_read_at): Likewise. (grub_util_write_image_at): Likewise. * kern/main.c (grub_get_end_addr): Remove function. (grub_load_modules): Call grub_arch_modules_addr instead of using grub_end_addr. Look for a grub_module_info struct in memory. Use the grub_module_info fields instead of calling grub_get_end_addr as loop conditions. Move grub_add_unused_region code here. (grub_add_unused_region): Remove function. * kern/i386/pc/init.c: Include grub/cache.h. (grub_machine_init): Remove call to grub_get_end_addr. Remove one call to add_mem_region. (grub_arch_modules_addr): New function. * kern/powerpc/ieee1275/init.c (grub_end_addr): Remove variable. (grub_total_module_size): Likewise. Include grub/machine/kernel.h. (grub_arch_modules_addr): New function. * util/grub-emu.c (grub_end_addr): Remove variable. (grub_total_module_size): Likewise. (grub_arch_modules_addr): New function. * util/misc.c: Include unistd.h. (grub_util_get_fp_size): New function. (grub_util_read_at): Likewise. (grub_util_write_image_at): Likewise. (grub_util_read_image): Call grub_util_read_at. (grub_util_write_image): Call grub_util_write_image_at. * util/i386/pc/grub-mkimage.c (generate_image): Allocate additional memory in kernel_img for a struct grub_module_info. Fill in that grub_module_info. * util/powerpc/ieee1275/grub-mkimage.c: New file.
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 Hollis Blanchard <hollis@penguinppc.org> * TODO: Add note about endianness in grub-mkimage. * boot/powerpc/ieee1275/crt0.S (note): Remove unused .note section. * conf/powerpc-ieee1275.rmk (bin_UTILITIES): Add grub-mkimage. (grub_mkimage_SOURCES): New target. * include/grub/kernel.h (grub_start_addr): Remove variable. (grub_end_addr): Likewise. (grub_total_module_size): Likewise. (grub_kernel_image_size): Likewise. (GRUB_MODULE_MAGIC): New constant. (grub_module_info): New structure. (grub_arch_modules_addr): New prototype. (grub_get_end_addr): Remove prototype. * include/grub/i386/pc/kernel.h (grub_end_addr): New prototype. * include/grub/powerpc/ieee1275/kernel.h: New file. * include/grub/util/misc.h (grub_util_get_fp_size): New prototype. (grub_util_read_at): Likewise. (grub_util_write_image_at): Likewise. * kern/main.c (grub_get_end_addr): Remove function. (grub_load_modules): Call grub_arch_modules_addr instead of using grub_end_addr. Look for a grub_module_info struct in memory. Use the grub_module_info fields instead of calling grub_get_end_addr as loop conditions. Move grub_add_unused_region code here. (grub_add_unused_region): Remove function. * kern/i386/pc/init.c: Include grub/cache.h. (grub_machine_init): Remove call to grub_get_end_addr. Remove one call to add_mem_region. (grub_arch_modules_addr): New function. * kern/powerpc/ieee1275/init.c (grub_end_addr): Remove variable. (grub_total_module_size): Likewise. Include grub/machine/kernel.h. (grub_arch_modules_addr): New function. * util/grub-emu.c (grub_end_addr): Remove variable. (grub_total_module_size): Likewise. (grub_arch_modules_addr): New function. * util/misc.c: Include unistd.h. (grub_util_get_fp_size): New function. (grub_util_read_at): Likewise. (grub_util_write_image_at): Likewise. (grub_util_read_image): Call grub_util_read_at. (grub_util_write_image): Call grub_util_write_image_at. * util/i386/pc/grub-mkimage.c (generate_image): Allocate additional memory in kernel_img for a struct grub_module_info. Fill in that grub_module_info. * util/powerpc/ieee1275/grub-mkimage.c: New file.
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);
header = (struct grub_module_header *) ((char *) header + header->size))
2002-12-27 08:53:07 +00:00
{
if (! grub_dl_load_core ((char *) header + header->offset,
2002-12-27 08:53:07 +00:00
(header->size - header->offset)))
grub_fatal ("%s", grub_errmsg);
2002-12-27 08:53:07 +00:00
}
2005-01-04 Hollis Blanchard <hollis@penguinppc.org> * TODO: Add note about endianness in grub-mkimage. * boot/powerpc/ieee1275/crt0.S (note): Remove unused .note section. * conf/powerpc-ieee1275.rmk (bin_UTILITIES): Add grub-mkimage. (grub_mkimage_SOURCES): New target. * include/grub/kernel.h (grub_start_addr): Remove variable. (grub_end_addr): Likewise. (grub_total_module_size): Likewise. (grub_kernel_image_size): Likewise. (GRUB_MODULE_MAGIC): New constant. (grub_module_info): New structure. (grub_arch_modules_addr): New prototype. (grub_get_end_addr): Remove prototype. * include/grub/i386/pc/kernel.h (grub_end_addr): New prototype. * include/grub/powerpc/ieee1275/kernel.h: New file. * include/grub/util/misc.h (grub_util_get_fp_size): New prototype. (grub_util_read_at): Likewise. (grub_util_write_image_at): Likewise. * kern/main.c (grub_get_end_addr): Remove function. (grub_load_modules): Call grub_arch_modules_addr instead of using grub_end_addr. Look for a grub_module_info struct in memory. Use the grub_module_info fields instead of calling grub_get_end_addr as loop conditions. Move grub_add_unused_region code here. (grub_add_unused_region): Remove function. * kern/i386/pc/init.c: Include grub/cache.h. (grub_machine_init): Remove call to grub_get_end_addr. Remove one call to add_mem_region. (grub_arch_modules_addr): New function. * kern/powerpc/ieee1275/init.c (grub_end_addr): Remove variable. (grub_total_module_size): Likewise. Include grub/machine/kernel.h. (grub_arch_modules_addr): New function. * util/grub-emu.c (grub_end_addr): Remove variable. (grub_total_module_size): Likewise. (grub_arch_modules_addr): New function. * util/misc.c: Include unistd.h. (grub_util_get_fp_size): New function. (grub_util_read_at): Likewise. (grub_util_write_image_at): Likewise. (grub_util_read_image): Call grub_util_read_at. (grub_util_write_image): Call grub_util_write_image_at. * util/i386/pc/grub-mkimage.c (generate_image): Allocate additional memory in kernel_img for a struct grub_module_info. Fill in that grub_module_info. * util/powerpc/ieee1275/grub-mkimage.c: New file.
2005-01-04 14:01:45 +00:00
/* Add the region where modules reside into dynamic memory. */
grub_mm_init_region ((void *) modinfo, modinfo->size);
2002-12-27 08:53:07 +00:00
}
/* Set the root device according to the dl prefix. */
static void
grub_set_root_dev (void)
{
const char *prefix;
prefix = grub_env_get ("prefix");
if (prefix)
{
char *dev;
dev = grub_file_get_device_name (prefix);
if (dev)
{
grub_device_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)
{
2003-01-20 Yoshinori K. Okuji <okuji@enbug.org> * include/pupa/normal.h: New file. * include/pupa/setjmp.h: Likewise. * include/pupa/i386/setjmp.h: Likewise. * normal/cmdline.c: Likewise. * normal/command.c: Likewise. * normal/main.c: Likewise. * normal/menu.c: Likewise. * normal/i386/setjmp.S: Likewise. * loader/i386/pc/linux.c (pupa_rescue_cmd_linux): Made global. (pupa_rescue_cmd_initrd): Likewise. * loader/i386/pc/chainloader.c (pupa_rescue_cmd_chainloader): Likewise. * kern/i386/pc/startup.S (translation_table): New variable. (translate_keycode): New function. (pupa_console_getkey): Call translate_keycode. * kern/rescue.c (attempt_normal_mode): New function. (pupa_enter_rescue_mode): Attempt to execute the normal mode. If it failed, print a message. * kern/mm.c (pupa_real_malloc): Print more information when a free magic is broken. (pupa_free): If the first free header is not free actually, set it to P. * kern/main.c (pupa_load_normal_mode): Just load the module "normal". (pupa_main): Don't print the message "Entering into rescue mode..." here. * include/pupa/i386/pc/loader.h (pupa_rescue_cmd_initrd): Declared. (pupa_rescue_cmd_initrd): Likewise. (pupa_rescue_cmd_initrd): Likewise. * include/pupa/symbol.h (FUNCTION): Specify the type. (VARIABLE): Likewise. * include/pupa/err.h (pupa_err_t): Added PUPA_ERR_UNKNOWN_COMMAND. * include/pupa/dl.h (pupa_dl_set_prefix): Exported. (pupa_dl_get_prefix): Likewise. * conf/i386-pc.rmk (pkgdata_MODULES): Added normal.mod. Added _chain.mod and _linux.mod instead of chain.mod and linux.mod. (chain_mod_SOURCES): Renamed to ... (_chain_mod_SOURCES): ... this. (chain_mod_CFLAGS): Renamed to ... (_chain_mod_CFLAGS): ... this. (linux_mod_SOURCES): Renamed to ... (_linux_mod_SOURCES): ... this. (linux_mod_CFLAGS): Renamed to ... (_linux_mod_CFLAGS): ... this. (normal_mod_SOURCES): New variable. (normal_mod_CFLAGS): Likewise. (normal_mod_ASFLAGS): Likewise. 2003-01-18 Yoshinori K. Okuji <okuji@enbug.org> * kern/rescue.c (pupa_rescue_cmd_rmmod): Call pupa_dl_unload, if possible. * kern/dl.c (pupa_dl_ref): Refer dependending modules recursively. (pupa_dl_unref): Unrefer depending modules recursively. Don't call pupa_dl_unload implicitly, because PUPA can crash if a module is unloaded before one depending on that module is unloaded. (pupa_dl_unload): Unload depending modules explicitly, if possible.
2003-01-20 04:13:46 +00:00
/* Load the module. */
grub_dl_load ("normal");
/* Ignore any error, because we have the rescue mode anyway. */
grub_errno = GRUB_ERR_NONE;
}
2002-12-27 08:53:07 +00:00
/* The main routine. */
void
grub_main (void)
2002-12-27 08:53:07 +00:00
{
/* First of all, initialize the machine. */
grub_machine_init ();
2002-12-27 08:53:07 +00:00
/* Hello. */
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
/* It is better to set the root device as soon as possible,
for convenience. */
grub_set_root_dev ();
/* Load pre-loaded modules and free the space. */
grub_register_exported_symbols ();
grub_load_modules ();
2002-12-27 08:53:07 +00:00
2003-01-20 Yoshinori K. Okuji <okuji@enbug.org> * include/pupa/normal.h: New file. * include/pupa/setjmp.h: Likewise. * include/pupa/i386/setjmp.h: Likewise. * normal/cmdline.c: Likewise. * normal/command.c: Likewise. * normal/main.c: Likewise. * normal/menu.c: Likewise. * normal/i386/setjmp.S: Likewise. * loader/i386/pc/linux.c (pupa_rescue_cmd_linux): Made global. (pupa_rescue_cmd_initrd): Likewise. * loader/i386/pc/chainloader.c (pupa_rescue_cmd_chainloader): Likewise. * kern/i386/pc/startup.S (translation_table): New variable. (translate_keycode): New function. (pupa_console_getkey): Call translate_keycode. * kern/rescue.c (attempt_normal_mode): New function. (pupa_enter_rescue_mode): Attempt to execute the normal mode. If it failed, print a message. * kern/mm.c (pupa_real_malloc): Print more information when a free magic is broken. (pupa_free): If the first free header is not free actually, set it to P. * kern/main.c (pupa_load_normal_mode): Just load the module "normal". (pupa_main): Don't print the message "Entering into rescue mode..." here. * include/pupa/i386/pc/loader.h (pupa_rescue_cmd_initrd): Declared. (pupa_rescue_cmd_initrd): Likewise. (pupa_rescue_cmd_initrd): Likewise. * include/pupa/symbol.h (FUNCTION): Specify the type. (VARIABLE): Likewise. * include/pupa/err.h (pupa_err_t): Added PUPA_ERR_UNKNOWN_COMMAND. * include/pupa/dl.h (pupa_dl_set_prefix): Exported. (pupa_dl_get_prefix): Likewise. * conf/i386-pc.rmk (pkgdata_MODULES): Added normal.mod. Added _chain.mod and _linux.mod instead of chain.mod and linux.mod. (chain_mod_SOURCES): Renamed to ... (_chain_mod_SOURCES): ... this. (chain_mod_CFLAGS): Renamed to ... (_chain_mod_CFLAGS): ... this. (linux_mod_SOURCES): Renamed to ... (_linux_mod_SOURCES): ... this. (linux_mod_CFLAGS): Renamed to ... (_linux_mod_CFLAGS): ... this. (normal_mod_SOURCES): New variable. (normal_mod_CFLAGS): Likewise. (normal_mod_ASFLAGS): Likewise. 2003-01-18 Yoshinori K. Okuji <okuji@enbug.org> * kern/rescue.c (pupa_rescue_cmd_rmmod): Call pupa_dl_unload, if possible. * kern/dl.c (pupa_dl_ref): Refer dependending modules recursively. (pupa_dl_unref): Unrefer depending modules recursively. Don't call pupa_dl_unload implicitly, because PUPA can crash if a module is unloaded before one depending on that module is unloaded. (pupa_dl_unload): Unload depending modules explicitly, if possible.
2003-01-20 04:13:46 +00:00
/* Load the normal mode module. */
grub_load_normal_mode ();
2003-01-20 Yoshinori K. Okuji <okuji@enbug.org> * include/pupa/normal.h: New file. * include/pupa/setjmp.h: Likewise. * include/pupa/i386/setjmp.h: Likewise. * normal/cmdline.c: Likewise. * normal/command.c: Likewise. * normal/main.c: Likewise. * normal/menu.c: Likewise. * normal/i386/setjmp.S: Likewise. * loader/i386/pc/linux.c (pupa_rescue_cmd_linux): Made global. (pupa_rescue_cmd_initrd): Likewise. * loader/i386/pc/chainloader.c (pupa_rescue_cmd_chainloader): Likewise. * kern/i386/pc/startup.S (translation_table): New variable. (translate_keycode): New function. (pupa_console_getkey): Call translate_keycode. * kern/rescue.c (attempt_normal_mode): New function. (pupa_enter_rescue_mode): Attempt to execute the normal mode. If it failed, print a message. * kern/mm.c (pupa_real_malloc): Print more information when a free magic is broken. (pupa_free): If the first free header is not free actually, set it to P. * kern/main.c (pupa_load_normal_mode): Just load the module "normal". (pupa_main): Don't print the message "Entering into rescue mode..." here. * include/pupa/i386/pc/loader.h (pupa_rescue_cmd_initrd): Declared. (pupa_rescue_cmd_initrd): Likewise. (pupa_rescue_cmd_initrd): Likewise. * include/pupa/symbol.h (FUNCTION): Specify the type. (VARIABLE): Likewise. * include/pupa/err.h (pupa_err_t): Added PUPA_ERR_UNKNOWN_COMMAND. * include/pupa/dl.h (pupa_dl_set_prefix): Exported. (pupa_dl_get_prefix): Likewise. * conf/i386-pc.rmk (pkgdata_MODULES): Added normal.mod. Added _chain.mod and _linux.mod instead of chain.mod and linux.mod. (chain_mod_SOURCES): Renamed to ... (_chain_mod_SOURCES): ... this. (chain_mod_CFLAGS): Renamed to ... (_chain_mod_CFLAGS): ... this. (linux_mod_SOURCES): Renamed to ... (_linux_mod_SOURCES): ... this. (linux_mod_CFLAGS): Renamed to ... (_linux_mod_CFLAGS): ... this. (normal_mod_SOURCES): New variable. (normal_mod_CFLAGS): Likewise. (normal_mod_ASFLAGS): Likewise. 2003-01-18 Yoshinori K. Okuji <okuji@enbug.org> * kern/rescue.c (pupa_rescue_cmd_rmmod): Call pupa_dl_unload, if possible. * kern/dl.c (pupa_dl_ref): Refer dependending modules recursively. (pupa_dl_unref): Unrefer depending modules recursively. Don't call pupa_dl_unload implicitly, because PUPA can crash if a module is unloaded before one depending on that module is unloaded. (pupa_dl_unload): Unload depending modules explicitly, if possible.
2003-01-20 04:13:46 +00:00
/* Enter the rescue mode. */
grub_enter_rescue_mode ();
2002-12-27 08:53:07 +00:00
}