1
/* multiboot.c - boot a multiboot OS image. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,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/>.
21
* FIXME: The following features from the Multiboot specification still
22
* need to be implemented:
26
* - ROM configuration table
30
/* The bits in the required part of flags field we don't support. */
31
#define UNSUPPORTED_FLAGS 0x0000fff8
33
#include <grub/loader.h>
34
#include <grub/multiboot.h>
36
#include <grub/aout.h>
37
#include <grub/file.h>
41
#include <grub/misc.h>
42
#include <grub/gzio.h>
44
#include <grub/i386/relocator.h>
45
#include <grub/video.h>
47
#ifdef GRUB_MACHINE_EFI
48
#include <grub/efi/efi.h>
51
extern grub_dl_t my_mod;
52
static grub_size_t code_size, alloc_mbi;
54
char *grub_multiboot_payload_orig;
55
grub_addr_t grub_multiboot_payload_dest;
56
grub_size_t grub_multiboot_pure_size;
57
grub_uint32_t grub_multiboot_payload_eip;
60
grub_multiboot_boot (void)
64
struct grub_relocator32_state state =
66
.eax = MULTIBOOT_BOOTLOADER_MAGIC,
69
.eip = grub_multiboot_payload_eip,
70
/* Set esp to some random location in low memory to avoid breaking
71
non-compliant kernels. */
75
mbi_size = grub_multiboot_get_mbi_size ();
76
if (alloc_mbi < mbi_size)
78
grub_multiboot_payload_orig
79
= grub_relocator32_realloc (grub_multiboot_payload_orig,
80
grub_multiboot_pure_size + mbi_size);
81
if (!grub_multiboot_payload_orig)
86
state.ebx = grub_multiboot_payload_dest + grub_multiboot_pure_size;
87
err = grub_multiboot_make_mbi (grub_multiboot_payload_orig,
88
grub_multiboot_payload_dest,
89
grub_multiboot_pure_size, mbi_size);
93
#ifdef GRUB_MACHINE_EFI
94
if (! grub_efi_finish_boot_services ())
95
grub_fatal ("cannot exit boot services");
98
grub_relocator32_boot (grub_multiboot_payload_orig,
99
grub_multiboot_payload_dest,
103
return GRUB_ERR_NONE;
107
grub_multiboot_unload (void)
109
grub_multiboot_free_mbi ();
111
grub_relocator32_free (grub_multiboot_payload_orig);
115
grub_multiboot_payload_orig = NULL;
116
grub_dl_unref (my_mod);
118
return GRUB_ERR_NONE;
121
#define MULTIBOOT_LOAD_ELF64
122
#include "multiboot_elfxx.c"
123
#undef MULTIBOOT_LOAD_ELF64
125
#define MULTIBOOT_LOAD_ELF32
126
#include "multiboot_elfxx.c"
127
#undef MULTIBOOT_LOAD_ELF32
129
/* Load ELF32 or ELF64. */
131
grub_multiboot_load_elf (grub_file_t file, void *buffer)
133
if (grub_multiboot_is_elf32 (buffer))
134
return grub_multiboot_load_elf32 (file, buffer);
135
else if (grub_multiboot_is_elf64 (buffer))
136
return grub_multiboot_load_elf64 (file, buffer);
138
return grub_error (GRUB_ERR_UNKNOWN_OS, "unknown ELF class");
142
grub_multiboot (int argc, char *argv[])
144
grub_file_t file = 0;
145
char buffer[MULTIBOOT_SEARCH];
146
struct multiboot_header *header;
149
grub_loader_unset ();
153
grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
157
file = grub_gzfile_open (argv[0], 1);
160
grub_error (GRUB_ERR_BAD_ARGUMENT, "couldn't open file");
164
len = grub_file_read (file, buffer, MULTIBOOT_SEARCH);
167
grub_error (GRUB_ERR_BAD_OS, "file too small");
171
/* Look for the multiboot header in the buffer. The header should
172
be at least 12 bytes and aligned on a 4-byte boundary. */
173
for (header = (struct multiboot_header *) buffer;
174
((char *) header <= buffer + len - 12) || (header = 0);
175
header = (struct multiboot_header *) ((char *) header + 4))
177
if (header->magic == MULTIBOOT_HEADER_MAGIC
178
&& !(header->magic + header->flags + header->checksum))
184
grub_error (GRUB_ERR_BAD_ARGUMENT, "no multiboot header found");
188
if (header->flags & UNSUPPORTED_FLAGS)
190
grub_error (GRUB_ERR_UNKNOWN_OS,
191
"unsupported flag: 0x%x", header->flags);
195
grub_relocator32_free (grub_multiboot_payload_orig);
196
grub_multiboot_payload_orig = NULL;
199
grub_multiboot_init_mbi (argc - 1, argv + 1);
201
if (header->flags & MULTIBOOT_AOUT_KLUDGE)
203
int offset = ((char *) header - buffer -
204
(header->header_addr - header->load_addr));
205
int load_size = ((header->load_end_addr == 0) ? file->size - offset :
206
header->load_end_addr - header->load_addr);
208
if (header->bss_end_addr)
209
code_size = (header->bss_end_addr - header->load_addr);
211
code_size = load_size;
212
grub_multiboot_payload_dest = header->load_addr;
214
grub_multiboot_pure_size += code_size;
216
/* Allocate a bit more to avoid relocations in most cases. */
217
alloc_mbi = grub_multiboot_get_mbi_size () + 65536;
218
grub_multiboot_payload_orig
219
= grub_relocator32_alloc (grub_multiboot_pure_size + alloc_mbi);
221
if (! grub_multiboot_payload_orig)
224
if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
227
grub_file_read (file, (void *) grub_multiboot_payload_orig, load_size);
231
if (header->bss_end_addr)
232
grub_memset ((void *) (grub_multiboot_payload_orig + load_size), 0,
233
header->bss_end_addr - header->load_addr - load_size);
235
grub_multiboot_payload_eip = header->entry_addr;
238
else if (grub_multiboot_load_elf (file, buffer) != GRUB_ERR_NONE)
241
if (header->flags & MULTIBOOT_VIDEO_MODE)
243
switch (header->mode_type)
246
grub_env_set ("gfxpayload", "text");
252
if (header->depth && header->width && header->height)
253
buf = grub_xasprintf ("%dx%dx%d,%dx%d,auto", header->width,
254
header->height, header->depth, header->width,
256
else if (header->width && header->height)
257
buf = grub_xasprintf ("%dx%d,auto", header->width, header->height);
259
buf = grub_strdup ("auto");
263
grub_env_set ("gfxpayload", buf);
270
grub_multiboot_set_accepts_video (!!(header->flags & MULTIBOOT_VIDEO_MODE));
272
grub_multiboot_set_bootdev ();
274
grub_loader_set (grub_multiboot_boot, grub_multiboot_unload, 0);
278
grub_file_close (file);
280
if (grub_errno != GRUB_ERR_NONE)
282
grub_relocator32_free (grub_multiboot_payload_orig);
283
grub_dl_unref (my_mod);
288
grub_module (int argc, char *argv[])
290
grub_file_t file = 0;
297
grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
301
if (!grub_multiboot_payload_orig)
303
grub_error (GRUB_ERR_BAD_ARGUMENT,
304
"you need to load the multiboot kernel first");
308
file = grub_gzfile_open (argv[0], 1);
312
size = grub_file_size (file);
313
module = grub_memalign (MULTIBOOT_MOD_ALIGN, size);
317
err = grub_multiboot_add_module ((grub_addr_t) module, size,
322
if (grub_file_read (file, module, size) != size)
324
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
330
grub_file_close (file);