Fix compilation for x86_64-efi.
[grub.git] / grub-core / loader / multiboot.c
1 /* multiboot.c - boot a multiboot OS image. */
2 /*
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  *  (at your option) any later version.
10  *
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.
15  *
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/>.
18  */
19
20 /*
21  *  FIXME: The following features from the Multiboot specification still
22  *         need to be implemented:
23  *  - drives table
24  *  - ROM configuration table
25  *  - SMBIOS tables
26  *  - Networking information
27  */
28
29 #include <grub/loader.h>
30 #include <grub/command.h>
31 #ifdef GRUB_USE_MULTIBOOT2
32 #include <grub/multiboot2.h>
33 #define GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER GRUB_MULTIBOOT2_CONSOLE_FRAMEBUFFER
34 #define GRUB_MULTIBOOT_CONSOLE_EGA_TEXT GRUB_MULTIBOOT2_CONSOLE_EGA_TEXT
35 #define GRUB_MULTIBOOT(x) grub_multiboot2_ ## x
36 #else
37 #include <grub/multiboot.h>
38 #define GRUB_MULTIBOOT(x) grub_multiboot_ ## x
39 #endif
40 #include <grub/cpu/multiboot.h>
41 #include <grub/elf.h>
42 #include <grub/aout.h>
43 #include <grub/file.h>
44 #include <grub/err.h>
45 #include <grub/dl.h>
46 #include <grub/mm.h>
47 #include <grub/misc.h>
48 #include <grub/env.h>
49 #include <grub/cpu/relocator.h>
50 #include <grub/video.h>
51 #include <grub/memory.h>
52 #include <grub/i18n.h>
53
54 GRUB_MOD_LICENSE ("GPLv3+");
55
56 #ifdef GRUB_MACHINE_EFI
57 #include <grub/efi/efi.h>
58 #endif
59
60 struct grub_relocator *GRUB_MULTIBOOT (relocator) = NULL;
61 grub_uint32_t GRUB_MULTIBOOT (payload_eip);
62 #if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_MULTIBOOT) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
63 #define DEFAULT_VIDEO_MODE "text"
64 #else
65 #define DEFAULT_VIDEO_MODE "auto"
66 #endif
67
68 static int accepts_video;
69 static int accepts_ega_text;
70 static int console_required;
71 static grub_dl_t my_mod;
72
73
74 /* Helper for grub_get_multiboot_mmap_count.  */
75 static int
76 count_hook (grub_uint64_t addr __attribute__ ((unused)),
77             grub_uint64_t size __attribute__ ((unused)),
78             grub_memory_type_t type __attribute__ ((unused)), void *data)
79 {
80   grub_size_t *count = data;
81
82   (*count)++;
83   return 0;
84 }
85
86 /* Return the length of the Multiboot mmap that will be needed to allocate
87    our platform's map.  */
88 grub_uint32_t
89 GRUB_MULTIBOOT (get_mmap_count) (void)
90 {
91   grub_size_t count = 0;
92
93   grub_mmap_iterate (count_hook, &count);
94
95   return count;
96 }
97
98 grub_err_t
99 GRUB_MULTIBOOT (set_video_mode) (void)
100 {
101   grub_err_t err;
102   const char *modevar;
103
104 #if GRUB_MACHINE_HAS_VGA_TEXT
105   if (accepts_video)
106 #endif
107     {
108       modevar = grub_env_get ("gfxpayload");
109       if (! modevar || *modevar == 0)
110         err = grub_video_set_mode (DEFAULT_VIDEO_MODE, 0, 0);
111       else
112         {
113           char *tmp;
114           tmp = grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE, modevar);
115           if (! tmp)
116             return grub_errno;
117           err = grub_video_set_mode (tmp, 0, 0);
118           grub_free (tmp);
119         }
120     }
121 #if GRUB_MACHINE_HAS_VGA_TEXT
122   else
123     err = grub_video_set_mode ("text", 0, 0);
124 #endif
125
126   return err;
127 }
128
129 #ifdef GRUB_MACHINE_EFI
130 #ifdef __x86_64__
131 #define grub_relocator_efi_boot         grub_relocator64_efi_boot
132 #define grub_relocator_efi_state        grub_relocator64_efi_state
133 #endif
134 #endif
135
136 #ifdef grub_relocator_efi_boot
137 static void
138 efi_boot (struct grub_relocator *rel,
139           grub_uint32_t target)
140 {
141 #ifdef GRUB_USE_MULTIBOOT2
142   struct grub_relocator_efi_state state_efi = MULTIBOOT2_EFI_INITIAL_STATE;
143 #else
144   struct grub_relocator_efi_state state_efi = MULTIBOOT_EFI_INITIAL_STATE;
145 #endif
146   state_efi.MULTIBOOT_EFI_ENTRY_REGISTER = GRUB_MULTIBOOT (payload_eip);
147   state_efi.MULTIBOOT_EFI_MBI_REGISTER = target;
148
149   grub_relocator_efi_boot (rel, state_efi);
150 }
151 #else
152 #define grub_efi_is_finished    1
153 static void
154 efi_boot (struct grub_relocator *rel __attribute__ ((unused)),
155           grub_uint32_t target __attribute__ ((unused)))
156 {
157 }
158 #endif
159
160 #if defined (__i386__) || defined (__x86_64__)
161 static void
162 normal_boot (struct grub_relocator *rel, struct grub_relocator32_state state)
163 {
164   grub_relocator32_boot (rel, state, 0);
165 }
166 #else
167 static void
168 normal_boot (struct grub_relocator *rel, struct grub_relocator32_state state)
169 {
170   grub_relocator32_boot (rel, state);
171 }
172 #endif
173
174 static grub_err_t
175 grub_multiboot_boot (void)
176 {
177   grub_err_t err;
178
179 #ifdef GRUB_USE_MULTIBOOT2
180   struct grub_relocator32_state state = MULTIBOOT2_INITIAL_STATE;
181 #else
182   struct grub_relocator32_state state = MULTIBOOT_INITIAL_STATE;
183 #endif
184   state.MULTIBOOT_ENTRY_REGISTER = GRUB_MULTIBOOT (payload_eip);
185
186   err = GRUB_MULTIBOOT (make_mbi) (&state.MULTIBOOT_MBI_REGISTER);
187
188   if (err)
189     return err;
190
191   if (grub_efi_is_finished)
192     normal_boot (GRUB_MULTIBOOT (relocator), state);
193   else
194     efi_boot (GRUB_MULTIBOOT (relocator), state.MULTIBOOT_MBI_REGISTER);
195
196   /* Not reached.  */
197   return GRUB_ERR_NONE;
198 }
199
200 static grub_err_t
201 grub_multiboot_unload (void)
202 {
203   GRUB_MULTIBOOT (free_mbi) ();
204
205   grub_relocator_unload (GRUB_MULTIBOOT (relocator));
206   GRUB_MULTIBOOT (relocator) = NULL;
207
208   grub_dl_unref (my_mod);
209
210   return GRUB_ERR_NONE;
211 }
212
213 static grub_uint64_t highest_load;
214
215 #define MULTIBOOT_LOAD_ELF64
216 #include "multiboot_elfxx.c"
217 #undef MULTIBOOT_LOAD_ELF64
218
219 #define MULTIBOOT_LOAD_ELF32
220 #include "multiboot_elfxx.c"
221 #undef MULTIBOOT_LOAD_ELF32
222
223 /* Load ELF32 or ELF64.  */
224 grub_err_t
225 GRUB_MULTIBOOT (load_elf) (mbi_load_data_t *mld)
226 {
227   if (grub_multiboot_is_elf32 (mld->buffer))
228     return grub_multiboot_load_elf32 (mld);
229   else if (grub_multiboot_is_elf64 (mld->buffer))
230     return grub_multiboot_load_elf64 (mld);
231
232   return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
233 }
234
235 grub_err_t
236 GRUB_MULTIBOOT (set_console) (int console_type, int accepted_consoles,
237                               int width, int height, int depth,
238                               int console_req)
239 {
240   console_required = console_req;
241   if (!(accepted_consoles 
242         & (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER
243            | (GRUB_MACHINE_HAS_VGA_TEXT ? GRUB_MULTIBOOT_CONSOLE_EGA_TEXT : 0))))
244     {
245       if (console_required)
246         return grub_error (GRUB_ERR_BAD_OS,
247                            "OS requires a console but none is available");
248       grub_puts_ (N_("WARNING: no console will be available to OS"));
249       accepts_video = 0;
250       accepts_ega_text = 0;
251       return GRUB_ERR_NONE;
252     }
253
254   if (console_type == GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER)
255     {
256       char *buf;
257       if (depth && width && height)
258         buf = grub_xasprintf ("%dx%dx%d,%dx%d,auto", width,
259                               height, depth, width, height);
260       else if (width && height)
261         buf = grub_xasprintf ("%dx%d,auto", width, height);
262       else
263         buf = grub_strdup ("auto");
264
265       if (!buf)
266         return grub_errno;
267       grub_env_set ("gfxpayload", buf);
268       grub_free (buf);
269     }
270   else
271     {
272 #if GRUB_MACHINE_HAS_VGA_TEXT
273       grub_env_set ("gfxpayload", "text");
274 #else
275       /* Always use video if no VGA text is available.  */
276       grub_env_set ("gfxpayload", "auto");
277 #endif
278     }
279
280   accepts_video = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER);
281   accepts_ega_text = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_EGA_TEXT);
282   return GRUB_ERR_NONE;
283 }
284
285 static grub_err_t
286 grub_cmd_multiboot (grub_command_t cmd __attribute__ ((unused)),
287                     int argc, char *argv[])
288 {
289   grub_file_t file = 0;
290   grub_err_t err;
291
292   grub_loader_unset ();
293
294   highest_load = 0;
295
296 #ifndef GRUB_USE_MULTIBOOT2
297   grub_multiboot_quirks = GRUB_MULTIBOOT_QUIRKS_NONE;
298   int option_found = 0;
299
300   do
301     {
302       option_found = 0;
303       if (argc != 0 && grub_strcmp (argv[0], "--quirk-bad-kludge") == 0)
304         {
305           argc--;
306           argv++;
307           option_found = 1;
308           grub_multiboot_quirks |= GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE;
309         }
310
311       if (argc != 0 && grub_strcmp (argv[0], "--quirk-modules-after-kernel") == 0)
312         {
313           argc--;
314           argv++;
315           option_found = 1;
316           grub_multiboot_quirks |= GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL;
317         }
318     } while (option_found);
319 #endif
320
321   if (argc == 0)
322     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
323
324   file = grub_file_open (argv[0]);
325   if (! file)
326     return grub_errno;
327
328   grub_dl_ref (my_mod);
329
330   /* Skip filename.  */
331   GRUB_MULTIBOOT (init_mbi) (argc - 1, argv + 1);
332
333   grub_relocator_unload (GRUB_MULTIBOOT (relocator));
334   GRUB_MULTIBOOT (relocator) = grub_relocator_new ();
335
336   if (!GRUB_MULTIBOOT (relocator))
337     goto fail;
338
339   err = GRUB_MULTIBOOT (load) (file, argv[0]);
340   if (err)
341     goto fail;
342
343   GRUB_MULTIBOOT (set_bootdev) ();
344
345   grub_loader_set (grub_multiboot_boot, grub_multiboot_unload, 0);
346
347  fail:
348   if (file)
349     grub_file_close (file);
350
351   if (grub_errno != GRUB_ERR_NONE)
352     {
353       grub_relocator_unload (GRUB_MULTIBOOT (relocator));
354       GRUB_MULTIBOOT (relocator) = NULL;
355       grub_dl_unref (my_mod);
356     }
357
358   return grub_errno;
359 }
360
361 static grub_err_t
362 grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
363                  int argc, char *argv[])
364 {
365   grub_file_t file = 0;
366   grub_ssize_t size;
367   void *module = NULL;
368   grub_addr_t target;
369   grub_err_t err;
370   int nounzip = 0;
371   grub_uint64_t lowest_addr = 0;
372
373   if (argc == 0)
374     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
375
376   if (grub_strcmp (argv[0], "--nounzip") == 0)
377     {
378       argv++;
379       argc--;
380       nounzip = 1;
381     }
382
383   if (argc == 0)
384     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
385
386   if (!GRUB_MULTIBOOT (relocator))
387     return grub_error (GRUB_ERR_BAD_ARGUMENT,
388                        N_("you need to load the kernel first"));
389
390   if (nounzip)
391     grub_file_filter_disable_compression ();
392
393   file = grub_file_open (argv[0]);
394   if (! file)
395     return grub_errno;
396
397 #ifndef GRUB_USE_MULTIBOOT2
398   lowest_addr = 0x100000;
399   if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL)
400     lowest_addr = ALIGN_UP (highest_load + 1048576, 4096);
401 #endif
402
403   size = grub_file_size (file);
404   if (size)
405   {
406     grub_relocator_chunk_t ch;
407     err = grub_relocator_alloc_chunk_align (GRUB_MULTIBOOT (relocator), &ch,
408                                             lowest_addr, (0xffffffff - size) + 1,
409                                             size, MULTIBOOT_MOD_ALIGN,
410                                             GRUB_RELOCATOR_PREFERENCE_NONE, 1);
411     if (err)
412       {
413         grub_file_close (file);
414         return err;
415       }
416     module = get_virtual_current_address (ch);
417     target = get_physical_target_address (ch);
418   }
419   else
420     {
421       module = 0;
422       target = 0;
423     }
424
425   err = GRUB_MULTIBOOT (add_module) (target, size, argc - 1, argv + 1);
426   if (err)
427     {
428       grub_file_close (file);
429       return err;
430     }
431
432   if (size && grub_file_read (file, module, size) != size)
433     {
434       grub_file_close (file);
435       if (!grub_errno)
436         grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
437                     argv[0]);
438       return grub_errno;
439     }
440
441   grub_file_close (file);
442   return GRUB_ERR_NONE;
443 }
444
445 static grub_command_t cmd_multiboot, cmd_module;
446
447 GRUB_MOD_INIT(multiboot)
448 {
449   cmd_multiboot =
450 #ifdef GRUB_USE_MULTIBOOT2
451     grub_register_command ("multiboot2", grub_cmd_multiboot,
452                            0, N_("Load a multiboot 2 kernel."));
453   cmd_module =
454     grub_register_command ("module2", grub_cmd_module,
455                            0, N_("Load a multiboot 2 module."));
456 #else
457     grub_register_command ("multiboot", grub_cmd_multiboot,
458                            0, N_("Load a multiboot kernel."));
459   cmd_module =
460     grub_register_command ("module", grub_cmd_module,
461                            0, N_("Load a multiboot module."));
462 #endif
463
464   my_mod = mod;
465 }
466
467 GRUB_MOD_FINI(multiboot)
468 {
469   grub_unregister_command (cmd_multiboot);
470   grub_unregister_command (cmd_module);
471 }