multiboot: disentangle multiboot and multiboot2.
[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   struct grub_relocator_efi_state state_efi = MULTIBOOT_EFI_INITIAL_STATE;
142
143   state_efi.MULTIBOOT_EFI_ENTRY_REGISTER = grub_multiboot_payload_eip;
144   state_efi.MULTIBOOT_EFI_MBI_REGISTER = target;
145
146   grub_relocator_efi_boot (rel, state_efi);
147 }
148 #else
149 #define grub_efi_is_finished    1
150 static void
151 efi_boot (struct grub_relocator *rel __attribute__ ((unused)),
152           grub_uint32_t target __attribute__ ((unused)))
153 {
154 }
155 #endif
156
157 #if defined (__i386__) || defined (__x86_64__)
158 static void
159 normal_boot (struct grub_relocator *rel, struct grub_relocator32_state state)
160 {
161   grub_relocator32_boot (rel, state, 0);
162 }
163 #else
164 static void
165 normal_boot (struct grub_relocator *rel, struct grub_relocator32_state state)
166 {
167   grub_relocator32_boot (rel, state);
168 }
169 #endif
170
171 static grub_err_t
172 grub_multiboot_boot (void)
173 {
174   grub_err_t err;
175
176 #ifdef GRUB_USE_MULTIBOOT2
177   struct grub_relocator32_state state = MULTIBOOT2_INITIAL_STATE;
178 #else
179   struct grub_relocator32_state state = MULTIBOOT_INITIAL_STATE;
180 #endif
181   state.MULTIBOOT_ENTRY_REGISTER = GRUB_MULTIBOOT (payload_eip);
182
183   err = GRUB_MULTIBOOT (make_mbi) (&state.MULTIBOOT_MBI_REGISTER);
184
185   if (err)
186     return err;
187
188   if (grub_efi_is_finished)
189     normal_boot (GRUB_MULTIBOOT (relocator), state);
190   else
191     efi_boot (GRUB_MULTIBOOT (relocator), state.MULTIBOOT_MBI_REGISTER);
192
193   /* Not reached.  */
194   return GRUB_ERR_NONE;
195 }
196
197 static grub_err_t
198 grub_multiboot_unload (void)
199 {
200   GRUB_MULTIBOOT (free_mbi) ();
201
202   grub_relocator_unload (GRUB_MULTIBOOT (relocator));
203   GRUB_MULTIBOOT (relocator) = NULL;
204
205   grub_dl_unref (my_mod);
206
207   return GRUB_ERR_NONE;
208 }
209
210 static grub_uint64_t highest_load;
211
212 #define MULTIBOOT_LOAD_ELF64
213 #include "multiboot_elfxx.c"
214 #undef MULTIBOOT_LOAD_ELF64
215
216 #define MULTIBOOT_LOAD_ELF32
217 #include "multiboot_elfxx.c"
218 #undef MULTIBOOT_LOAD_ELF32
219
220 /* Load ELF32 or ELF64.  */
221 grub_err_t
222 GRUB_MULTIBOOT (load_elf) (mbi_load_data_t *mld)
223 {
224   if (grub_multiboot_is_elf32 (mld->buffer))
225     return grub_multiboot_load_elf32 (mld);
226   else if (grub_multiboot_is_elf64 (mld->buffer))
227     return grub_multiboot_load_elf64 (mld);
228
229   return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
230 }
231
232 grub_err_t
233 GRUB_MULTIBOOT (set_console) (int console_type, int accepted_consoles,
234                               int width, int height, int depth,
235                               int console_req)
236 {
237   console_required = console_req;
238   if (!(accepted_consoles 
239         & (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER
240            | (GRUB_MACHINE_HAS_VGA_TEXT ? GRUB_MULTIBOOT_CONSOLE_EGA_TEXT : 0))))
241     {
242       if (console_required)
243         return grub_error (GRUB_ERR_BAD_OS,
244                            "OS requires a console but none is available");
245       grub_puts_ (N_("WARNING: no console will be available to OS"));
246       accepts_video = 0;
247       accepts_ega_text = 0;
248       return GRUB_ERR_NONE;
249     }
250
251   if (console_type == GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER)
252     {
253       char *buf;
254       if (depth && width && height)
255         buf = grub_xasprintf ("%dx%dx%d,%dx%d,auto", width,
256                               height, depth, width, height);
257       else if (width && height)
258         buf = grub_xasprintf ("%dx%d,auto", width, height);
259       else
260         buf = grub_strdup ("auto");
261
262       if (!buf)
263         return grub_errno;
264       grub_env_set ("gfxpayload", buf);
265       grub_free (buf);
266     }
267   else
268     {
269 #if GRUB_MACHINE_HAS_VGA_TEXT
270       grub_env_set ("gfxpayload", "text");
271 #else
272       /* Always use video if no VGA text is available.  */
273       grub_env_set ("gfxpayload", "auto");
274 #endif
275     }
276
277   accepts_video = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER);
278   accepts_ega_text = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_EGA_TEXT);
279   return GRUB_ERR_NONE;
280 }
281
282 static grub_err_t
283 grub_cmd_multiboot (grub_command_t cmd __attribute__ ((unused)),
284                     int argc, char *argv[])
285 {
286   grub_file_t file = 0;
287   grub_err_t err;
288
289   grub_loader_unset ();
290
291   highest_load = 0;
292
293 #ifndef GRUB_USE_MULTIBOOT2
294   grub_multiboot_quirks = GRUB_MULTIBOOT_QUIRKS_NONE;
295   int option_found = 0;
296
297   do
298     {
299       option_found = 0;
300       if (argc != 0 && grub_strcmp (argv[0], "--quirk-bad-kludge") == 0)
301         {
302           argc--;
303           argv++;
304           option_found = 1;
305           grub_multiboot_quirks |= GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE;
306         }
307
308       if (argc != 0 && grub_strcmp (argv[0], "--quirk-modules-after-kernel") == 0)
309         {
310           argc--;
311           argv++;
312           option_found = 1;
313           grub_multiboot_quirks |= GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL;
314         }
315     } while (option_found);
316 #endif
317
318   if (argc == 0)
319     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
320
321   file = grub_file_open (argv[0]);
322   if (! file)
323     return grub_errno;
324
325   grub_dl_ref (my_mod);
326
327   /* Skip filename.  */
328   GRUB_MULTIBOOT (init_mbi) (argc - 1, argv + 1);
329
330   grub_relocator_unload (GRUB_MULTIBOOT (relocator));
331   GRUB_MULTIBOOT (relocator) = grub_relocator_new ();
332
333   if (!GRUB_MULTIBOOT (relocator))
334     goto fail;
335
336   err = GRUB_MULTIBOOT (load) (file, argv[0]);
337   if (err)
338     goto fail;
339
340   GRUB_MULTIBOOT (set_bootdev) ();
341
342   grub_loader_set (grub_multiboot_boot, grub_multiboot_unload, 0);
343
344  fail:
345   if (file)
346     grub_file_close (file);
347
348   if (grub_errno != GRUB_ERR_NONE)
349     {
350       grub_relocator_unload (GRUB_MULTIBOOT (relocator));
351       GRUB_MULTIBOOT (relocator) = NULL;
352       grub_dl_unref (my_mod);
353     }
354
355   return grub_errno;
356 }
357
358 static grub_err_t
359 grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
360                  int argc, char *argv[])
361 {
362   grub_file_t file = 0;
363   grub_ssize_t size;
364   void *module = NULL;
365   grub_addr_t target;
366   grub_err_t err;
367   int nounzip = 0;
368   grub_uint64_t lowest_addr = 0;
369
370   if (argc == 0)
371     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
372
373   if (grub_strcmp (argv[0], "--nounzip") == 0)
374     {
375       argv++;
376       argc--;
377       nounzip = 1;
378     }
379
380   if (argc == 0)
381     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
382
383   if (!GRUB_MULTIBOOT (relocator))
384     return grub_error (GRUB_ERR_BAD_ARGUMENT,
385                        N_("you need to load the kernel first"));
386
387   if (nounzip)
388     grub_file_filter_disable_compression ();
389
390   file = grub_file_open (argv[0]);
391   if (! file)
392     return grub_errno;
393
394 #ifndef GRUB_USE_MULTIBOOT2
395   lowest_addr = 0x100000;
396   if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL)
397     lowest_addr = ALIGN_UP (highest_load + 1048576, 4096);
398 #endif
399
400   size = grub_file_size (file);
401   if (size)
402   {
403     grub_relocator_chunk_t ch;
404     err = grub_relocator_alloc_chunk_align (GRUB_MULTIBOOT (relocator), &ch,
405                                             lowest_addr, (0xffffffff - size) + 1,
406                                             size, MULTIBOOT_MOD_ALIGN,
407                                             GRUB_RELOCATOR_PREFERENCE_NONE, 1);
408     if (err)
409       {
410         grub_file_close (file);
411         return err;
412       }
413     module = get_virtual_current_address (ch);
414     target = get_physical_target_address (ch);
415   }
416   else
417     {
418       module = 0;
419       target = 0;
420     }
421
422   err = GRUB_MULTIBOOT (add_module) (target, size, argc - 1, argv + 1);
423   if (err)
424     {
425       grub_file_close (file);
426       return err;
427     }
428
429   if (size && grub_file_read (file, module, size) != size)
430     {
431       grub_file_close (file);
432       if (!grub_errno)
433         grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
434                     argv[0]);
435       return grub_errno;
436     }
437
438   grub_file_close (file);
439   return GRUB_ERR_NONE;
440 }
441
442 static grub_command_t cmd_multiboot, cmd_module;
443
444 GRUB_MOD_INIT(multiboot)
445 {
446   cmd_multiboot =
447 #ifdef GRUB_USE_MULTIBOOT2
448     grub_register_command ("multiboot2", grub_cmd_multiboot,
449                            0, N_("Load a multiboot 2 kernel."));
450   cmd_module =
451     grub_register_command ("module2", grub_cmd_module,
452                            0, N_("Load a multiboot 2 module."));
453 #else
454     grub_register_command ("multiboot", grub_cmd_multiboot,
455                            0, N_("Load a multiboot kernel."));
456   cmd_module =
457     grub_register_command ("module", grub_cmd_module,
458                            0, N_("Load a multiboot module."));
459 #endif
460
461   my_mod = mod;
462 }
463
464 GRUB_MOD_FINI(multiboot)
465 {
466   grub_unregister_command (cmd_multiboot);
467   grub_unregister_command (cmd_module);
468 }