From 0d5d5653261a6b706de47a4d5ae06480548a7d0a Mon Sep 17 00:00:00 2001 From: phcoder Date: Wed, 15 Apr 2009 12:45:50 +0000 Subject: [PATCH] 2009-04-15 Vladimir Serbinenko Move loader out of the kernel * kern/loader.c: moved to ... * commands/boot.c: ... moved here * commands/minicmd.c (grub_mini_cmd_boot): moved to ... * commands/boot.c (grub_cmd_boot): moved here. All users updated * include/grub/kernel.h (grub_machine_fini): export * include/grub/loader.h (grub_loader_is_loaded): update declaration (grub_loader_set): likewise (grub_loader_unset): likewise (grub_loader_boot): likewise * conf/common.rmk: new module boot.mod (pkglib_MODULES): add boot.mod * conf/i386-coreboot.rmk (kernel_elf_SOURCES): remove kern/loader.c (grub_emu_SOURCES): likewise * conf/i386-efi.rmk (kernel_elf_SOURCES): likewise (grub_emu_SOURCES): likewise * conf/i386-ieee1275.rmk (kernel_elf_SOURCES): likewise (grub_emu_SOURCES): likewise * conf/i386-pc.rmk (kernel_elf_SOURCES): likewise (grub_emu_SOURCES): likewise * conf/powerpc-ieee1275.rmk (kernel_elf_SOURCES): likewise (grub_emu_SOURCES): likewise * conf/sparcs64-ieee1275.rmk (kernel_elf_SOURCES): likewise (grub_emu_SOURCES): likewise * conf/x86_64-efi.rmk (kernel_elf_SOURCES): likewise (grub_emu_SOURCES): likewise --- ChangeLog | 30 ++++++++++++++++ commands/boot.c | 75 +++++++++++++++++++++++++++++++++------ commands/minicmd.c | 17 ++------- conf/common.rmk | 7 +++- conf/i386-coreboot.rmk | 4 +-- conf/i386-efi.rmk | 4 +-- conf/i386-ieee1275.rmk | 4 +-- conf/i386-pc.rmk | 4 +-- conf/powerpc-ieee1275.rmk | 4 +-- conf/sparc64-ieee1275.rmk | 4 +-- conf/x86_64-efi.rmk | 4 +-- include/grub/kernel.h | 4 +-- include/grub/loader.h | 14 ++++---- 13 files changed, 125 insertions(+), 50 deletions(-) diff --git a/ChangeLog b/ChangeLog index 13b67cc9f..910529d36 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,33 @@ +2009-04-15 Vladimir Serbinenko + + Move loader out of the kernel + + * kern/loader.c: moved to ... + * commands/boot.c: ... moved here + * commands/minicmd.c (grub_mini_cmd_boot): moved to ... + * commands/boot.c (grub_cmd_boot): moved here. All users updated + * include/grub/kernel.h (grub_machine_fini): export + * include/grub/loader.h (grub_loader_is_loaded): update declaration + (grub_loader_set): likewise + (grub_loader_unset): likewise + (grub_loader_boot): likewise + * conf/common.rmk: new module boot.mod + (pkglib_MODULES): add boot.mod + * conf/i386-coreboot.rmk (kernel_elf_SOURCES): remove kern/loader.c + (grub_emu_SOURCES): likewise + * conf/i386-efi.rmk (kernel_elf_SOURCES): likewise + (grub_emu_SOURCES): likewise + * conf/i386-ieee1275.rmk (kernel_elf_SOURCES): likewise + (grub_emu_SOURCES): likewise + * conf/i386-pc.rmk (kernel_elf_SOURCES): likewise + (grub_emu_SOURCES): likewise + * conf/powerpc-ieee1275.rmk (kernel_elf_SOURCES): likewise + (grub_emu_SOURCES): likewise + * conf/sparcs64-ieee1275.rmk (kernel_elf_SOURCES): likewise + (grub_emu_SOURCES): likewise + * conf/x86_64-efi.rmk (kernel_elf_SOURCES): likewise + (grub_emu_SOURCES): likewise + 2009-04-15 Vladimir Serbinenko use grub_lltoa instead of grub_itoa and grub_ltoa for all purposes diff --git a/commands/boot.c b/commands/boot.c index ee0a0832e..9a08feaf7 100644 --- a/commands/boot.c +++ b/commands/boot.c @@ -1,7 +1,7 @@ /* boot.c - command to boot an operating system */ /* * GRUB -- GRand Unified Bootloader - * Copyright (C) 2003,2005,2007 Free Software Foundation, Inc. + * Copyright (C) 2002,2003,2004,2005,2007,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 @@ -21,29 +21,82 @@ #include #include #include +#include -static grub_err_t -grub_cmd_boot (struct grub_arg_list *state __attribute__ ((unused)), - int argc, char **args __attribute__ ((unused))) +static grub_err_t (*grub_loader_boot_func) (void); +static grub_err_t (*grub_loader_unload_func) (void); +static int grub_loader_noreturn; + +static int grub_loader_loaded; + +int +grub_loader_is_loaded (void) { - if (argc) - return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many arguments"); + return grub_loader_loaded; +} + +void +grub_loader_set (grub_err_t (*boot) (void), + grub_err_t (*unload) (void), + int noreturn) +{ + if (grub_loader_loaded && grub_loader_unload_func) + grub_loader_unload_func (); - grub_loader_boot (); + grub_loader_boot_func = boot; + grub_loader_unload_func = unload; + grub_loader_noreturn = noreturn; - return 0; + grub_loader_loaded = 1; +} + +void +grub_loader_unset(void) +{ + if (grub_loader_loaded && grub_loader_unload_func) + grub_loader_unload_func (); + + grub_loader_boot_func = 0; + grub_loader_unload_func = 0; + + grub_loader_loaded = 0; +} + +grub_err_t +grub_loader_boot (void) +{ + if (! grub_loader_loaded) + return grub_error (GRUB_ERR_NO_KERNEL, "no loaded kernel"); + + if (grub_loader_noreturn) + grub_machine_fini (); + + return (grub_loader_boot_func) (); +} + + +/* boot */ +static grub_err_t +grub_cmd_boot (struct grub_command *cmd __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char *argv[] __attribute__ ((unused))) +{ + return grub_loader_boot (); } +static grub_command_t cmd_boot; + GRUB_MOD_INIT(boot) { (void) mod; /* To stop warning. */ - grub_register_command ("boot", grub_cmd_boot, GRUB_COMMAND_FLAG_BOTH, - "boot", "Boot an operating system.", 0); + cmd_boot = + grub_register_command ("boot", grub_cmd_boot, + 0, "boot an operating system"); } GRUB_MOD_FINI(boot) { - grub_unregister_command ("boot"); + grub_unregister_command (cmd_boot); } diff --git a/commands/minicmd.c b/commands/minicmd.c index 071c889e1..5083291f4 100644 --- a/commands/minicmd.c +++ b/commands/minicmd.c @@ -1,7 +1,7 @@ /* minicmd.c - commands for the rescue mode */ /* * GRUB -- GRand Unified Bootloader - * Copyright (C) 2003,2005,2006,2007 Free Software Foundation, Inc. + * Copyright (C) 2003,2005,2006,2007,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 @@ -28,15 +28,6 @@ #include #include -/* boot */ -static grub_err_t -grub_mini_cmd_boot (struct grub_command *cmd __attribute__ ((unused)), - int argc __attribute__ ((unused)), - char *argv[] __attribute__ ((unused))) -{ - return grub_loader_boot (); -} - /* cat FILE */ static grub_err_t grub_mini_cmd_cat (struct grub_command *cmd __attribute__ ((unused)), @@ -345,16 +336,13 @@ grub_mini_cmd_exit (struct grub_command *cmd __attribute__ ((unused)), return 0; } -static grub_command_t cmd_boot, cmd_cat, cmd_help, cmd_root; +static grub_command_t cmd_cat, cmd_help, cmd_root; static grub_command_t cmd_dump, cmd_rmmod, cmd_lsmod, cmd_exit; GRUB_MOD_INIT(minicmd) { (void) mod; /* To stop warning. */ - cmd_boot = - grub_register_command ("boot", grub_mini_cmd_boot, - 0, "boot an operating system"); cmd_cat = grub_register_command ("cat", grub_mini_cmd_cat, "cat FILE", "show the contents of a file"); @@ -380,7 +368,6 @@ GRUB_MOD_INIT(minicmd) GRUB_MOD_FINI(minicmd) { - grub_unregister_command (cmd_boot); grub_unregister_command (cmd_cat); grub_unregister_command (cmd_help); grub_unregister_command (cmd_root); diff --git a/conf/common.rmk b/conf/common.rmk index 1e1b0986a..0e63da6d5 100644 --- a/conf/common.rmk +++ b/conf/common.rmk @@ -343,7 +343,12 @@ pkglib_MODULES += minicmd.mod extcmd.mod hello.mod handler.mod \ loopback.mod fs_uuid.mod configfile.mod echo.mod \ terminfo.mod test.mod blocklist.mod hexdump.mod \ read.mod sleep.mod loadenv.mod crc.mod parttool.mod \ - pcpart.mod memrw.mod + pcpart.mod memrw.mod boot.mod + +# For boot.mod. +boot_mod_SOURCES = commands/boot.c +boot_mod_CFLAGS = $(COMMON_CFLAGS) +boot_mod_LDFLAGS = $(COMMON_LDFLAGS) # For minicmd.mod. minicmd_mod_SOURCES = commands/minicmd.c diff --git a/conf/i386-coreboot.rmk b/conf/i386-coreboot.rmk index bf6fdd05f..f7cb58dd8 100644 --- a/conf/i386-coreboot.rmk +++ b/conf/i386-coreboot.rmk @@ -16,7 +16,7 @@ kernel_elf_SOURCES = kern/i386/coreboot/startup.S \ kern/i386/multiboot_mmap.c \ kern/main.c kern/device.c \ kern/disk.c kern/dl.c kern/file.c kern/fs.c kern/err.c \ - kern/misc.c kern/mm.c kern/loader.c kern/rescue.c kern/term.c \ + kern/misc.c kern/mm.c kern/rescue.c kern/term.c \ kern/time.c kern/list.c kern/handler.c kern/command.c kern/corecmd.c \ kern/i386/dl.c kern/parser.c kern/partition.c \ kern/i386/tsc.c kern/i386/pit.c \ @@ -73,7 +73,7 @@ grub_emu_SOURCES = commands/minicmd.c commands/cat.c commands/cmp.c \ kern/err.c kern/list.c kern/handler.c \ kern/command.c kern/corecmd.c commands/extcmd.c \ normal/execute.c kern/file.c kern/fs.c normal/lexer.c \ - kern/loader.c kern/main.c kern/misc.c kern/parser.c \ + commands/boot.c kern/main.c kern/misc.c kern/parser.c \ grub_script.tab.c kern/partition.c kern/rescue.c kern/term.c \ lib/arg.c normal/cmdline.c normal/command.c normal/function.c\ normal/handler.c \ diff --git a/conf/i386-efi.rmk b/conf/i386-efi.rmk index 4cdf0b5c5..fdb83b825 100644 --- a/conf/i386-efi.rmk +++ b/conf/i386-efi.rmk @@ -50,7 +50,7 @@ grub_emu_SOURCES = commands/minicmd.c commands/cat.c commands/cmp.c \ kern/err.c kern/list.c kern/handler.c \ kern/command.c kern/corecmd.c commands/extcmd.c \ normal/execute.c kern/file.c kern/fs.c normal/lexer.c \ - kern/loader.c kern/main.c kern/misc.c kern/parser.c \ + commands/boot.c kern/main.c kern/misc.c kern/parser.c \ grub_script.tab.c kern/partition.c kern/rescue.c kern/term.c \ lib/arg.c normal/cmdline.c normal/command.c normal/datetime.c \ normal/function.c \ @@ -87,7 +87,7 @@ pkglib_MODULES = kernel.mod normal.mod chain.mod appleldr.mod \ kernel_mod_EXPORTS = no kernel_mod_SOURCES = kern/i386/efi/startup.S kern/main.c kern/device.c \ kern/disk.c kern/dl.c kern/file.c kern/fs.c kern/err.c \ - kern/misc.c kern/mm.c kern/loader.c kern/rescue.c kern/term.c \ + kern/misc.c kern/mm.c kern/rescue.c kern/term.c \ kern/i386/dl.c kern/i386/efi/init.c kern/parser.c kern/partition.c \ kern/env.c symlist.c kern/efi/efi.c kern/efi/init.c kern/efi/mm.c \ term/efi/console.c disk/efi/efidisk.c \ diff --git a/conf/i386-ieee1275.rmk b/conf/i386-ieee1275.rmk index 9bcf9f72b..cb41cce37 100644 --- a/conf/i386-ieee1275.rmk +++ b/conf/i386-ieee1275.rmk @@ -17,7 +17,7 @@ kernel_elf_SOURCES = kern/i386/ieee1275/startup.S kern/i386/ieee1275/init.c \ kern/ieee1275/cmain.c kern/ieee1275/openfw.c \ kern/main.c kern/device.c \ kern/disk.c kern/dl.c kern/file.c kern/fs.c kern/err.c \ - kern/misc.c kern/mm.c kern/loader.c kern/rescue.c kern/term.c \ + kern/misc.c kern/mm.c kern/rescue.c kern/term.c \ kern/i386/dl.c kern/parser.c kern/partition.c \ kern/env.c \ kern/time.c kern/list.c kern/handler.c kern/command.c kern/corecmd.c \ @@ -73,7 +73,7 @@ grub_emu_SOURCES = commands/minicmd.c commands/cat.c commands/cmp.c \ kern/err.c kern/list.c kern/handler.c \ kern/command.c kern/corecmd.c commands/extcmd.c \ normal/execute.c kern/file.c kern/fs.c normal/lexer.c \ - kern/loader.c kern/main.c kern/misc.c kern/parser.c \ + commands/boot.c kern/main.c kern/misc.c kern/parser.c \ grub_script.tab.c kern/partition.c kern/rescue.c kern/term.c \ lib/arg.c normal/cmdline.c normal/command.c normal/datetime.c \ normal/function.c normal/handler.c \ diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index e885184b7..265b250d8 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -46,7 +46,7 @@ cdboot_img_FORMAT = binary # For kernel.img. kernel_img_SOURCES = kern/i386/pc/startup.S kern/main.c kern/device.c \ kern/disk.c kern/dl.c kern/file.c kern/fs.c kern/err.c \ - kern/misc.c kern/mm.c kern/loader.c kern/rescue.c kern/term.c \ + kern/misc.c kern/mm.c kern/rescue.c kern/term.c \ kern/time.c kern/list.c kern/handler.c kern/command.c kern/corecmd.c \ kern/i386/dl.c kern/i386/pc/init.c kern/i386/pc/mmap.c \ kern/parser.c kern/partition.c \ @@ -132,7 +132,7 @@ grub_emu_SOURCES = commands/minicmd.c commands/cat.c commands/cmp.c \ kern/err.c kern/list.c kern/handler.c \ kern/command.c kern/corecmd.c commands/extcmd.c \ normal/execute.c kern/file.c kern/fs.c normal/lexer.c \ - kern/loader.c kern/main.c kern/misc.c kern/parser.c \ + commands/boot.c kern/main.c kern/misc.c kern/parser.c \ grub_script.tab.c kern/partition.c kern/rescue.c kern/term.c \ lib/arg.c normal/cmdline.c normal/command.c normal/datetime.c \ normal/function.c normal/handler.c \ diff --git a/conf/powerpc-ieee1275.rmk b/conf/powerpc-ieee1275.rmk index e35f7639b..d8d35d5d9 100644 --- a/conf/powerpc-ieee1275.rmk +++ b/conf/powerpc-ieee1275.rmk @@ -53,7 +53,7 @@ grub_emu_SOURCES = commands/minicmd.c commands/cat.c commands/cmp.c \ \ io/gzio.c \ kern/device.c kern/disk.c kern/dl.c kern/elf.c kern/env.c \ - kern/err.c kern/file.c kern/fs.c kern/loader.c kern/main.c \ + kern/err.c kern/file.c kern/fs.c commands/boot.c kern/main.c \ kern/misc.c kern/parser.c kern/partition.c kern/rescue.c \ kern/term.c kern/list.c kern/handler.c fs/fshelp.c \ kern/command.c kern/corecmd.c commands/extcmd.c \ @@ -81,7 +81,7 @@ grub_emu_LDFLAGS = $(LIBCURSES) kernel_elf_SOURCES = kern/powerpc/ieee1275/startup.S kern/ieee1275/cmain.c \ kern/ieee1275/ieee1275.c kern/main.c kern/device.c \ kern/disk.c kern/dl.c kern/err.c kern/file.c kern/fs.c \ - kern/misc.c kern/mm.c kern/loader.c kern/rescue.c kern/term.c \ + kern/misc.c kern/mm.c kern/rescue.c kern/term.c \ kern/list.c kern/handler.c kern/command.c kern/corecmd.c \ kern/ieee1275/init.c \ kern/ieee1275/mmap.c \ diff --git a/conf/sparc64-ieee1275.rmk b/conf/sparc64-ieee1275.rmk index f5f9a8d61..029bca2c6 100644 --- a/conf/sparc64-ieee1275.rmk +++ b/conf/sparc64-ieee1275.rmk @@ -53,7 +53,7 @@ grub_mkimage_SOURCES = util/sparc64/ieee1275/grub-mkimage.c util/misc.c \ # grub_script.tab.c \ # io/gzio.c \ # kern/device.c kern/disk.c kern/dl.c kern/env.c kern/err.c \ -# kern/file.c kern/fs.c kern/loader.c kern/main.c kern/misc.c \ +# kern/file.c kern/fs.c commands/boot.c kern/main.c kern/misc.c \ # kern/parser.c kern/partition.c kern/rescue.c kern/term.c \ # kern/list.c kern/handler.c \ # normal/arg.c normal/cmdline.c normal/command.c normal/datetime.c \ @@ -73,7 +73,7 @@ grub_emu_LDFLAGS = $(LIBCURSES) kernel_elf_SOURCES = kern/sparc64/ieee1275/init.c kern/ieee1275/ieee1275.c \ kern/main.c kern/device.c kern/disk.c kern/dl.c kern/file.c \ - kern/fs.c kern/err.c kern/misc.c kern/mm.c kern/loader.c \ + kern/fs.c kern/err.c kern/misc.c kern/mm.c \ kern/rescue.c kern/term.c term/ieee1275/ofconsole.c \ kern/sparc64/ieee1275/openfw.c disk/ieee1275/ofdisk.c \ kern/partition.c kern/env.c kern/sparc64/dl.c symlist.c \ diff --git a/conf/x86_64-efi.rmk b/conf/x86_64-efi.rmk index c2acaa5b7..d7ad9a4c8 100644 --- a/conf/x86_64-efi.rmk +++ b/conf/x86_64-efi.rmk @@ -52,7 +52,7 @@ grub_emu_SOURCES = commands/minicmd.c commands/cat.c commands/cmp.c \ kern/err.c kern/list.c kern/handler.c \ kern/command.c kern/corecmd.c commands/extcmd.c \ normal/execute.c kern/file.c kern/fs.c normal/lexer.c \ - kern/loader.c kern/main.c kern/misc.c kern/parser.c \ + commands/boot.c kern/main.c kern/misc.c kern/parser.c \ grub_script.tab.c kern/partition.c kern/rescue.c kern/term.c \ lib/arg.c normal/cmdline.c normal/command.c normal/function.c\ normal/completion.c normal/datetime.c normal/context.c \ @@ -90,7 +90,7 @@ kernel_mod_EXPORTS = no kernel_mod_SOURCES = kern/x86_64/efi/startup.S kern/x86_64/efi/callwrap.S \ kern/main.c kern/device.c \ kern/disk.c kern/dl.c kern/file.c kern/fs.c kern/err.c \ - kern/misc.c kern/mm.c kern/loader.c kern/rescue.c kern/term.c \ + kern/misc.c kern/mm.c kern/rescue.c kern/term.c \ kern/x86_64/dl.c kern/i386/efi/init.c kern/parser.c kern/partition.c \ kern/env.c symlist.c kern/efi/efi.c kern/efi/init.c kern/efi/mm.c \ kern/time.c kern/list.c kern/handler.c kern/command.c kern/corecmd.c \ diff --git a/include/grub/kernel.h b/include/grub/kernel.h index adaee5827..74aca400f 100644 --- a/include/grub/kernel.h +++ b/include/grub/kernel.h @@ -1,6 +1,6 @@ /* * GRUB -- GRand Unified Bootloader - * Copyright (C) 2002,2005,2006,2007,2008 Free Software Foundation, Inc. + * Copyright (C) 2002,2005,2006,2007,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 @@ -64,7 +64,7 @@ void grub_main (void); void grub_machine_init (void); /* The machine-specific finalization. */ -void grub_machine_fini (void); +void EXPORT_FUNC(grub_machine_fini) (void); /* The machine-specific prefix initialization. */ void grub_machine_set_prefix (void); diff --git a/include/grub/loader.h b/include/grub/loader.h index 1ae5fddbb..185d29773 100644 --- a/include/grub/loader.h +++ b/include/grub/loader.h @@ -1,7 +1,7 @@ /* loader.h - OS loaders */ /* * GRUB -- GRand Unified Bootloader - * Copyright (C) 2002,2003,2004,2006,2007 Free Software Foundation, Inc. + * Copyright (C) 2002,2003,2004,2006,2007,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 @@ -26,19 +26,19 @@ #include /* Check if a loader is loaded. */ -int EXPORT_FUNC(grub_loader_is_loaded) (void); +int grub_loader_is_loaded (void); /* Set loader functions. NORETURN must be set to true, if BOOT won't return to the original state. */ -void EXPORT_FUNC(grub_loader_set) (grub_err_t (*boot) (void), - grub_err_t (*unload) (void), - int noreturn); +void grub_loader_set (grub_err_t (*boot) (void), + grub_err_t (*unload) (void), + int noreturn); /* Unset current loader, if any. */ -void EXPORT_FUNC(grub_loader_unset) (void); +void grub_loader_unset (void); /* Call the boot hook in current loader. This may or may not return, depending on the setting by grub_loader_set. */ -grub_err_t EXPORT_FUNC(grub_loader_boot) (void); +grub_err_t grub_loader_boot (void); #endif /* ! GRUB_LOADER_HEADER */