1
/* chainloader.c - boot another boot loader */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2002,2004,2007,2009,2010 Free Software Foundation, Inc.
6
* GRUB is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* GRUB is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20
#include <grub/loader.h>
21
#include <grub/cpu/loader.h>
22
#include <grub/machine/chainloader.h>
23
#include <grub/file.h>
25
#include <grub/device.h>
26
#include <grub/disk.h>
27
#include <grub/misc.h>
28
#include <grub/types.h>
29
#include <grub/machine/init.h>
30
#include <grub/partition.h>
31
#include <grub/machine/memory.h>
33
#include <grub/command.h>
34
#include <grub/machine/biosnum.h>
35
#include <grub/i18n.h>
36
#include <grub/video.h>
39
/* This is an asm part of the chainloader. */
40
void grub_chainloader_real_boot (int drive, void *part_addr) __attribute__ ((noreturn));
42
static grub_dl_t my_mod;
43
static int boot_drive;
44
static void *boot_part_addr;
47
grub_chainloader_boot (void)
49
grub_video_set_mode ("text", 0, 0);
50
grub_chainloader_real_boot (boot_drive, boot_part_addr);
52
/* Never reach here. */
57
grub_chainloader_unload (void)
59
grub_dl_unref (my_mod);
64
grub_chainloader_cmd (const char *filename, grub_chainloader_flags_t flags)
67
grub_uint16_t signature;
74
file = grub_file_open (filename);
78
/* Read the first block. */
79
if (grub_file_read (file, (void *) 0x7C00, GRUB_DISK_SECTOR_SIZE)
80
!= GRUB_DISK_SECTOR_SIZE)
82
if (grub_errno == GRUB_ERR_NONE)
83
grub_error (GRUB_ERR_BAD_OS, "too small");
88
/* Check the signature. */
89
signature = *((grub_uint16_t *) (0x7C00 + GRUB_DISK_SECTOR_SIZE - 2));
90
if (signature != grub_le_to_cpu16 (0xaa55)
91
&& ! (flags & GRUB_CHAINLOADER_FORCE))
93
grub_error (GRUB_ERR_BAD_OS, "invalid signature");
97
grub_file_close (file);
99
/* Obtain the partition table from the root device. */
100
drive = grub_get_root_biosnumber ();
101
dev = grub_device_open (0);
102
if (dev && dev->disk && dev->disk->partition)
104
grub_disk_read (dev->disk, dev->disk->partition->offset, 446, 64,
105
(void *) GRUB_MEMORY_MACHINE_PART_TABLE_ADDR);
106
part_addr = (void *) (GRUB_MEMORY_MACHINE_PART_TABLE_ADDR
107
+ (dev->disk->partition->index << 4));
111
grub_device_close (dev);
113
/* Ignore errors. Perhaps it's not fatal. */
114
grub_errno = GRUB_ERR_NONE;
117
boot_part_addr = part_addr;
119
grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 1);
125
grub_file_close (file);
127
grub_dl_unref (my_mod);
131
grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
132
int argc, char *argv[])
134
grub_chainloader_flags_t flags = 0;
136
if (argc > 0 && grub_strcmp (argv[0], "--force") == 0)
138
flags |= GRUB_CHAINLOADER_FORCE;
144
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
146
grub_chainloader_cmd (argv[0], flags);
151
static grub_command_t cmd;
153
GRUB_MOD_INIT(chainloader)
155
cmd = grub_register_command ("chainloader", grub_cmd_chainloader,
156
0, N_("Load another boot loader."));
160
GRUB_MOD_FINI(chainloader)
162
grub_unregister_command (cmd);