multiboot.c 9.92 KB
Newer Older
1 2
/* 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.
5
 *
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 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
12 13 14 15 16
 *  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
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18 19
 */

20
/*
21 22 23 24
 *  FIXME: The following features from the Multiboot specification still
 *         need to be implemented:
 *  - drives table
 *  - ROM configuration table
25 26
 *  - SMBIOS tables
 *  - Networking information
27 28
 */

29
#include <grub/loader.h>
30
#include <grub/command.h>
31
#include <grub/multiboot.h>
32
#include <grub/cpu/multiboot.h>
33
#include <grub/elf.h>
34
#include <grub/aout.h>
35 36 37 38 39
#include <grub/file.h>
#include <grub/err.h>
#include <grub/dl.h>
#include <grub/mm.h>
#include <grub/misc.h>
40
#include <grub/env.h>
41
#include <grub/cpu/relocator.h>
42
#include <grub/video.h>
43
#include <grub/memory.h>
44
#include <grub/i18n.h>
45

46 47
GRUB_MOD_LICENSE ("GPLv3+");

48 49 50
#ifdef GRUB_MACHINE_EFI
#include <grub/efi/efi.h>
#endif
51

52
struct grub_relocator *grub_multiboot_relocator = NULL;
53
grub_uint32_t grub_multiboot_payload_eip;
54
#if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_MULTIBOOT) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
55 56 57 58 59 60
#define DEFAULT_VIDEO_MODE "text"
#else
#define DEFAULT_VIDEO_MODE "auto"
#endif

static int accepts_video;
61 62
static int accepts_ega_text;
static int console_required;
63 64
static grub_dl_t my_mod;

65

66 67 68 69 70 71 72 73 74 75 76 77
/* Helper for grub_get_multiboot_mmap_count.  */
static int
count_hook (grub_uint64_t addr __attribute__ ((unused)),
	    grub_uint64_t size __attribute__ ((unused)),
	    grub_memory_type_t type __attribute__ ((unused)), void *data)
{
  grub_size_t *count = data;

  (*count)++;
  return 0;
}

78 79 80
/* Return the length of the Multiboot mmap that will be needed to allocate
   our platform's map.  */
grub_uint32_t
81
grub_get_multiboot_mmap_count (void)
82 83 84
{
  grub_size_t count = 0;

85
  grub_mmap_iterate (count_hook, &count);
86

87
  return count;
88 89 90 91 92 93 94 95
}

grub_err_t
grub_multiboot_set_video_mode (void)
{
  grub_err_t err;
  const char *modevar;

96 97 98
#if GRUB_MACHINE_HAS_VGA_TEXT
  if (accepts_video)
#endif
99 100 101
    {
      modevar = grub_env_get ("gfxpayload");
      if (! modevar || *modevar == 0)
102
	err = grub_video_set_mode (DEFAULT_VIDEO_MODE, 0, 0);
103 104 105
      else
	{
	  char *tmp;
106
	  tmp = grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE, modevar);
107 108
	  if (! tmp)
	    return grub_errno;
109
	  err = grub_video_set_mode (tmp, 0, 0);
110 111 112
	  grub_free (tmp);
	}
    }
113
#if GRUB_MACHINE_HAS_VGA_TEXT
114
  else
115
    err = grub_video_set_mode ("text", 0, 0);
116
#endif
117 118 119

  return err;
}
120

121 122
static grub_err_t
grub_multiboot_boot (void)
123
{
124
  grub_err_t err;
125 126 127
  struct grub_relocator32_state state = MULTIBOOT_INITIAL_STATE;

  state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
128

129
  err = grub_multiboot_make_mbi (&state.MULTIBOOT_MBI_REGISTER);
130 131 132 133

  if (err)
    return err;

134
#if defined (__i386__) || defined (__x86_64__)
135
  grub_relocator32_boot (grub_multiboot_relocator, state, 0);
136 137 138
#else
  grub_relocator32_boot (grub_multiboot_relocator, state);
#endif
139 140

  /* Not reached.  */
141
  return GRUB_ERR_NONE;
142 143
}

144 145
static grub_err_t
grub_multiboot_unload (void)
146
{
147 148
  grub_multiboot_free_mbi ();

149 150
  grub_relocator_unload (grub_multiboot_relocator);
  grub_multiboot_relocator = NULL;
151

152
  grub_dl_unref (my_mod);
153

154
  return GRUB_ERR_NONE;
155 156
}

157 158
static grub_uint64_t highest_load;

159 160 161
#define MULTIBOOT_LOAD_ELF64
#include "multiboot_elfxx.c"
#undef MULTIBOOT_LOAD_ELF64
162

163 164 165
#define MULTIBOOT_LOAD_ELF32
#include "multiboot_elfxx.c"
#undef MULTIBOOT_LOAD_ELF32
166 167

/* Load ELF32 or ELF64.  */
168
grub_err_t
169 170
grub_multiboot_load_elf (grub_file_t file, const char *filename,
			 void *buffer)
171 172
{
  if (grub_multiboot_is_elf32 (buffer))
173
    return grub_multiboot_load_elf32 (file, filename, buffer);
174
  else if (grub_multiboot_is_elf64 (buffer))
175
    return grub_multiboot_load_elf64 (file, filename, buffer);
176

177
  return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
178 179
}

180 181
grub_err_t
grub_multiboot_set_console (int console_type, int accepted_consoles,
182 183
			    int width, int height, int depth,
			    int console_req)
184
{
185 186 187 188
  console_required = console_req;
  if (!(accepted_consoles 
	& (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER
	   | (GRUB_MACHINE_HAS_VGA_TEXT ? GRUB_MULTIBOOT_CONSOLE_EGA_TEXT : 0))))
189
    {
190 191 192
      if (console_required)
	return grub_error (GRUB_ERR_BAD_OS,
			   "OS requires a console but none is available");
193
      grub_puts_ (N_("WARNING: no console will be available to OS"));
194 195 196
      accepts_video = 0;
      accepts_ega_text = 0;
      return GRUB_ERR_NONE;
197 198
    }

199
  if (console_type == GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER)
200
    {
201 202 203 204 205 206 207 208
      char *buf;
      if (depth && width && height)
	buf = grub_xasprintf ("%dx%dx%d,%dx%d,auto", width,
			      height, depth, width, height);
      else if (width && height)
	buf = grub_xasprintf ("%dx%d,auto", width, height);
      else
	buf = grub_strdup ("auto");
209

210 211 212 213
      if (!buf)
	return grub_errno;
      grub_env_set ("gfxpayload", buf);
      grub_free (buf);
214
    }
215 216 217 218 219 220 221 222 223
  else
    {
#if GRUB_MACHINE_HAS_VGA_TEXT
      grub_env_set ("gfxpayload", "text");
#else
      /* Always use video if no VGA text is available.  */
      grub_env_set ("gfxpayload", "auto");
#endif
    }
224

225
  accepts_video = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER);
226
  accepts_ega_text = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_EGA_TEXT);
227 228
  return GRUB_ERR_NONE;
}
229

230 231 232
static grub_err_t
grub_cmd_multiboot (grub_command_t cmd __attribute__ ((unused)),
		    int argc, char *argv[])
233
{
234
  grub_file_t file = 0;
235
  grub_err_t err;
236

237
  grub_loader_unset ();
238

239 240 241 242
  highest_load = 0;

#ifndef GRUB_USE_MULTIBOOT2
  grub_multiboot_quirks = GRUB_MULTIBOOT_QUIRKS_NONE;
243
  int option_found = 0;
244

245
  do
246
    {
247 248 249 250 251 252 253 254
      option_found = 0;
      if (argc != 0 && grub_strcmp (argv[0], "--quirk-bad-kludge") == 0)
	{
	  argc--;
	  argv++;
	  option_found = 1;
	  grub_multiboot_quirks |= GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE;
	}
255

256 257 258 259 260 261 262 263
      if (argc != 0 && grub_strcmp (argv[0], "--quirk-modules-after-kernel") == 0)
	{
	  argc--;
	  argv++;
	  option_found = 1;
	  grub_multiboot_quirks |= GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL;
	}
    } while (option_found);
264 265
#endif

266
  if (argc == 0)
267
    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
268

269
  file = grub_file_open (argv[0]);
270
  if (! file)
271
    return grub_errno;
272

273
  grub_dl_ref (my_mod);
274

275
  /* Skip filename.  */
276
  grub_multiboot_init_mbi (argc - 1, argv + 1);
277

278
  grub_relocator_unload (grub_multiboot_relocator);
279 280 281 282 283
  grub_multiboot_relocator = grub_relocator_new ();

  if (!grub_multiboot_relocator)
    goto fail;

284
  err = grub_multiboot_load (file, argv[0]);
285
  if (err)
286
    goto fail;
287

288
  grub_multiboot_set_bootdev ();
289

290
  grub_loader_set (grub_multiboot_boot, grub_multiboot_unload, 0);
291 292 293

 fail:
  if (file)
294
    grub_file_close (file);
295

296
  if (grub_errno != GRUB_ERR_NONE)
297
    {
298
      grub_relocator_unload (grub_multiboot_relocator);
299
      grub_multiboot_relocator = NULL;
300
      grub_dl_unref (my_mod);
301
    }
302 303

  return grub_errno;
304 305
}

306 307 308
static grub_err_t
grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
		 int argc, char *argv[])
309
{
310
  grub_file_t file = 0;
311
  grub_ssize_t size;
312 313
  void *module = NULL;
  grub_addr_t target;
314
  grub_err_t err;
315
  int nounzip = 0;
316
  grub_uint64_t lowest_addr = 0;
317 318

  if (argc == 0)
319
    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
320 321 322 323 324 325 326

  if (grub_strcmp (argv[0], "--nounzip") == 0)
    {
      argv++;
      argc--;
      nounzip = 1;
    }
327 328

  if (argc == 0)
329
    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
330

331
  if (!grub_multiboot_relocator)
332
    return grub_error (GRUB_ERR_BAD_ARGUMENT,
333
		       N_("you need to load the kernel first"));
334

335
  if (nounzip)
336 337 338
    grub_file_filter_disable_compression ();

  file = grub_file_open (argv[0]);
339
  if (! file)
340
    return grub_errno;
341

342
#ifndef GRUB_USE_MULTIBOOT2
343
  lowest_addr = 0x100000;
344 345 346 347
  if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL)
    lowest_addr = ALIGN_UP (highest_load + 1048576, 4096);
#endif

348
  size = grub_file_size (file);
349
  if (size)
350 351 352
  {
    grub_relocator_chunk_t ch;
    err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
353
					    lowest_addr, (0xffffffff - size) + 1,
354
					    size, MULTIBOOT_MOD_ALIGN,
355
					    GRUB_RELOCATOR_PREFERENCE_NONE, 1);
356 357 358 359 360 361
    if (err)
      {
	grub_file_close (file);
	return err;
      }
    module = get_virtual_current_address (ch);
362
    target = get_physical_target_address (ch);
363
  }
364 365 366 367 368
  else
    {
      module = 0;
      target = 0;
    }
369

370
  err = grub_multiboot_add_module (target, size, argc - 1, argv + 1);
371
  if (err)
372 373 374 375
    {
      grub_file_close (file);
      return err;
    }
376

377
  if (size && grub_file_read (file, module, size) != size)
378
    {
379
      grub_file_close (file);
380 381 382 383
      if (!grub_errno)
	grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
		    argv[0]);
      return grub_errno;
384
    }
385

386
  grub_file_close (file);
387
  return GRUB_ERR_NONE;
388
}
389

390 391 392 393 394 395 396 397
static grub_command_t cmd_multiboot, cmd_module;

GRUB_MOD_INIT(multiboot)
{
  cmd_multiboot =
#ifdef GRUB_USE_MULTIBOOT2
    grub_register_command ("multiboot2", grub_cmd_multiboot,
			   0, N_("Load a multiboot 2 kernel."));
398 399 400
  cmd_module =
    grub_register_command ("module2", grub_cmd_module,
			   0, N_("Load a multiboot 2 module."));
401 402 403 404 405 406
#else
    grub_register_command ("multiboot", grub_cmd_multiboot,
			   0, N_("Load a multiboot kernel."));
  cmd_module =
    grub_register_command ("module", grub_cmd_module,
			   0, N_("Load a multiboot module."));
407
#endif
408 409

  my_mod = mod;
410
}
411

412 413 414 415 416
GRUB_MOD_FINI(multiboot)
{
  grub_unregister_command (cmd_multiboot);
  grub_unregister_command (cmd_module);
}