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:
24
* - ROM configuration table
26
* - Networking information
29
#include <grub/loader.h>
30
#include <grub/command.h>
31
#include <grub/multiboot.h>
32
#include <grub/cpu/multiboot.h>
34
#include <grub/aout.h>
35
#include <grub/file.h>
39
#include <grub/misc.h>
41
#include <grub/cpu/relocator.h>
42
#include <grub/video.h>
43
#include <grub/memory.h>
44
#include <grub/i18n.h>
46
#ifdef GRUB_MACHINE_EFI
47
#include <grub/efi/efi.h>
50
struct grub_relocator *grub_multiboot_relocator = NULL;
51
grub_uint32_t grub_multiboot_payload_eip;
52
#if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_MULTIBOOT) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
53
#define DEFAULT_VIDEO_MODE "text"
55
#define DEFAULT_VIDEO_MODE "auto"
58
static int accepts_video;
59
static int accepts_ega_text;
60
static int console_required;
61
static grub_dl_t my_mod;
64
/* Return the length of the Multiboot mmap that will be needed to allocate
65
our platform's map. */
67
grub_get_multiboot_mmap_count (void)
69
grub_size_t count = 0;
71
auto int NESTED_FUNC_ATTR hook (grub_uint64_t, grub_uint64_t, grub_uint32_t);
72
int NESTED_FUNC_ATTR hook (grub_uint64_t addr __attribute__ ((unused)),
73
grub_uint64_t size __attribute__ ((unused)),
74
grub_memory_type_t type __attribute__ ((unused)))
80
grub_mmap_iterate (hook);
86
grub_multiboot_set_video_mode (void)
91
if (accepts_video || !GRUB_MACHINE_HAS_VGA_TEXT)
93
modevar = grub_env_get ("gfxpayload");
94
if (! modevar || *modevar == 0)
95
err = grub_video_set_mode (DEFAULT_VIDEO_MODE, 0, 0);
99
tmp = grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE, modevar);
102
err = grub_video_set_mode (tmp, 0, 0);
107
err = grub_video_set_mode ("text", 0, 0);
113
grub_multiboot_boot (void)
116
struct grub_relocator32_state state = MULTIBOOT_INITIAL_STATE;
118
state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
120
err = grub_multiboot_make_mbi (&state.MULTIBOOT_MBI_REGISTER);
125
#ifdef GRUB_MACHINE_EFI
126
err = grub_efi_finish_boot_services (NULL, NULL, NULL, NULL, NULL);
131
grub_relocator32_boot (grub_multiboot_relocator, state);
134
return GRUB_ERR_NONE;
138
grub_multiboot_unload (void)
140
grub_multiboot_free_mbi ();
142
grub_relocator_unload (grub_multiboot_relocator);
143
grub_multiboot_relocator = NULL;
145
grub_dl_unref (my_mod);
147
return GRUB_ERR_NONE;
150
#define MULTIBOOT_LOAD_ELF64
151
#include "multiboot_elfxx.c"
152
#undef MULTIBOOT_LOAD_ELF64
154
#define MULTIBOOT_LOAD_ELF32
155
#include "multiboot_elfxx.c"
156
#undef MULTIBOOT_LOAD_ELF32
158
/* Load ELF32 or ELF64. */
160
grub_multiboot_load_elf (grub_file_t file, void *buffer)
162
if (grub_multiboot_is_elf32 (buffer))
163
return grub_multiboot_load_elf32 (file, buffer);
164
else if (grub_multiboot_is_elf64 (buffer))
165
return grub_multiboot_load_elf64 (file, buffer);
167
return grub_error (GRUB_ERR_UNKNOWN_OS, "unknown ELF class");
171
grub_multiboot_set_console (int console_type, int accepted_consoles,
172
int width, int height, int depth,
175
console_required = console_req;
176
if (!(accepted_consoles
177
& (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER
178
| (GRUB_MACHINE_HAS_VGA_TEXT ? GRUB_MULTIBOOT_CONSOLE_EGA_TEXT : 0))))
180
if (console_required)
181
return grub_error (GRUB_ERR_BAD_OS,
182
"OS requires a console but none is available");
183
grub_printf ("WARNING: no console will be available to OS");
185
accepts_ega_text = 0;
186
return GRUB_ERR_NONE;
189
if (console_type == GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER)
192
if (depth && width && height)
193
buf = grub_xasprintf ("%dx%dx%d,%dx%d,auto", width,
194
height, depth, width, height);
195
else if (width && height)
196
buf = grub_xasprintf ("%dx%d,auto", width, height);
198
buf = grub_strdup ("auto");
202
grub_env_set ("gfxpayload", buf);
206
grub_env_set ("gfxpayload", "text");
208
accepts_video = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER);
209
accepts_ega_text = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_EGA_TEXT);
210
return GRUB_ERR_NONE;
214
grub_cmd_multiboot (grub_command_t cmd __attribute__ ((unused)),
215
int argc, char *argv[])
217
grub_file_t file = 0;
220
grub_loader_unset ();
223
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
225
file = grub_file_open (argv[0]);
227
return grub_error (GRUB_ERR_BAD_ARGUMENT, "couldn't open file");
229
grub_dl_ref (my_mod);
232
grub_multiboot_init_mbi (argc - 1, argv + 1);
234
grub_relocator_unload (grub_multiboot_relocator);
235
grub_multiboot_relocator = grub_relocator_new ();
237
if (!grub_multiboot_relocator)
240
err = grub_multiboot_load (file);
244
grub_multiboot_set_bootdev ();
246
grub_loader_set (grub_multiboot_boot, grub_multiboot_unload, 0);
250
grub_file_close (file);
252
if (grub_errno != GRUB_ERR_NONE)
254
grub_relocator_unload (grub_multiboot_relocator);
255
grub_multiboot_relocator = NULL;
256
grub_dl_unref (my_mod);
263
grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
264
int argc, char *argv[])
266
grub_file_t file = 0;
274
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
276
if (grub_strcmp (argv[0], "--nounzip") == 0)
284
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
286
if (!grub_multiboot_relocator)
287
return grub_error (GRUB_ERR_BAD_ARGUMENT,
288
"you need to load the multiboot kernel first");
291
grub_file_filter_disable_compression ();
293
file = grub_file_open (argv[0]);
297
size = grub_file_size (file);
299
grub_relocator_chunk_t ch;
300
err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
301
0, (0xffffffff - size) + 1,
302
size, MULTIBOOT_MOD_ALIGN,
303
GRUB_RELOCATOR_PREFERENCE_NONE);
306
grub_file_close (file);
309
module = get_virtual_current_address (ch);
310
target = (grub_addr_t) get_virtual_current_address (ch);
313
err = grub_multiboot_add_module (target, size, argc - 1, argv + 1);
316
grub_file_close (file);
320
if (grub_file_read (file, module, size) != size)
322
grub_file_close (file);
323
return grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
326
grub_file_close (file);
327
return GRUB_ERR_NONE;;
330
static grub_command_t cmd_multiboot, cmd_module;
332
GRUB_MOD_INIT(multiboot)
335
#ifdef GRUB_USE_MULTIBOOT2
336
grub_register_command ("multiboot2", grub_cmd_multiboot,
337
0, N_("Load a multiboot 2 kernel."));
339
grub_register_command ("module2", grub_cmd_module,
340
0, N_("Load a multiboot 2 module."));
342
grub_register_command ("multiboot", grub_cmd_multiboot,
343
0, N_("Load a multiboot kernel."));
345
grub_register_command ("module", grub_cmd_module,
346
0, N_("Load a multiboot module."));
352
GRUB_MOD_FINI(multiboot)
354
grub_unregister_command (cmd_multiboot);
355
grub_unregister_command (cmd_module);