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/machine/loader.h>
35 #include <grub/multiboot.h>
36 #include <grub/cpu/multiboot.h>
38 #include <grub/aout.h>
39 #include <grub/file.h>
43 #include <grub/misc.h>
44 #include <grub/gzio.h>
46 #include <grub/i386/relocator.h>
47 #include <grub/video.h>
49 #ifdef GRUB_MACHINE_EFI
50 #include <grub/efi/efi.h>
53 extern grub_dl_t my_mod
;
54 static grub_size_t code_size
, alloc_mbi
;
56 char *grub_multiboot_payload_orig
;
57 grub_addr_t grub_multiboot_payload_dest
;
58 grub_size_t grub_multiboot_pure_size
;
59 grub_uint32_t grub_multiboot_payload_eip
;
62 grub_multiboot_boot (void)
66 struct grub_relocator32_state state
=
68 .eax
= MULTIBOOT_BOOTLOADER_MAGIC
,
71 .eip
= grub_multiboot_payload_eip
,
72 /* Set esp to some random location in low memory to avoid breaking
73 non-compliant kernels. */
77 mbi_size
= grub_multiboot_get_mbi_size ();
78 if (alloc_mbi
< mbi_size
)
80 grub_multiboot_payload_orig
81 = grub_relocator32_realloc (grub_multiboot_payload_orig
,
82 grub_multiboot_pure_size
+ mbi_size
);
83 if (!grub_multiboot_payload_orig
)
88 state
.ebx
= grub_multiboot_payload_dest
+ grub_multiboot_pure_size
;
89 err
= grub_multiboot_make_mbi (grub_multiboot_payload_orig
,
90 grub_multiboot_payload_dest
,
91 grub_multiboot_pure_size
, mbi_size
);
95 #ifdef GRUB_MACHINE_EFI
96 if (! grub_efi_finish_boot_services ())
97 grub_fatal ("cannot exit boot services");
100 grub_relocator32_boot (grub_multiboot_payload_orig
,
101 grub_multiboot_payload_dest
,
105 return GRUB_ERR_NONE
;
109 grub_multiboot_unload (void)
111 grub_multiboot_free_mbi ();
113 grub_relocator32_free (grub_multiboot_payload_orig
);
117 grub_multiboot_payload_orig
= NULL
;
118 grub_dl_unref (my_mod
);
120 return GRUB_ERR_NONE
;
123 #define MULTIBOOT_LOAD_ELF64
124 #include "multiboot_elfxx.c"
125 #undef MULTIBOOT_LOAD_ELF64
127 #define MULTIBOOT_LOAD_ELF32
128 #include "multiboot_elfxx.c"
129 #undef MULTIBOOT_LOAD_ELF32
131 /* Load ELF32 or ELF64. */
133 grub_multiboot_load_elf (grub_file_t file
, void *buffer
)
135 if (grub_multiboot_is_elf32 (buffer
))
136 return grub_multiboot_load_elf32 (file
, buffer
);
137 else if (grub_multiboot_is_elf64 (buffer
))
138 return grub_multiboot_load_elf64 (file
, buffer
);
140 return grub_error (GRUB_ERR_UNKNOWN_OS
, "unknown ELF class");
144 grub_multiboot (int argc
, char *argv
[])
146 grub_file_t file
= 0;
147 char buffer
[MULTIBOOT_SEARCH
];
148 struct multiboot_header
*header
;
151 grub_loader_unset ();
155 grub_error (GRUB_ERR_BAD_ARGUMENT
, "no kernel specified");
159 file
= grub_gzfile_open (argv
[0], 1);
162 grub_error (GRUB_ERR_BAD_ARGUMENT
, "couldn't open file");
166 len
= grub_file_read (file
, buffer
, MULTIBOOT_SEARCH
);
169 grub_error (GRUB_ERR_BAD_OS
, "file too small");
173 /* Look for the multiboot header in the buffer. The header should
174 be at least 12 bytes and aligned on a 4-byte boundary. */
175 for (header
= (struct multiboot_header
*) buffer
;
176 ((char *) header
<= buffer
+ len
- 12) || (header
= 0);
177 header
= (struct multiboot_header
*) ((char *) header
+ 4))
179 if (header
->magic
== MULTIBOOT_HEADER_MAGIC
180 && !(header
->magic
+ header
->flags
+ header
->checksum
))
186 grub_error (GRUB_ERR_BAD_ARGUMENT
, "no multiboot header found");
190 if (header
->flags
& UNSUPPORTED_FLAGS
)
192 grub_error (GRUB_ERR_UNKNOWN_OS
,
193 "unsupported flag: 0x%x", header
->flags
);
197 grub_relocator32_free (grub_multiboot_payload_orig
);
198 grub_multiboot_payload_orig
= NULL
;
201 grub_multiboot_init_mbi (argc
- 1, argv
+ 1);
203 if (header
->flags
& MULTIBOOT_AOUT_KLUDGE
)
205 int offset
= ((char *) header
- buffer
-
206 (header
->header_addr
- header
->load_addr
));
207 int load_size
= ((header
->load_end_addr
== 0) ? file
->size
- offset
:
208 header
->load_end_addr
- header
->load_addr
);
210 if (header
->bss_end_addr
)
211 code_size
= (header
->bss_end_addr
- header
->load_addr
);
213 code_size
= load_size
;
214 grub_multiboot_payload_dest
= header
->load_addr
;
216 grub_multiboot_pure_size
+= code_size
;
218 /* Allocate a bit more to avoid relocations in most cases. */
219 alloc_mbi
= grub_multiboot_get_mbi_size () + 65536;
220 grub_multiboot_payload_orig
221 = grub_relocator32_alloc (grub_multiboot_pure_size
+ alloc_mbi
);
223 if (! grub_multiboot_payload_orig
)
226 if ((grub_file_seek (file
, offset
)) == (grub_off_t
) -1)
229 grub_file_read (file
, (void *) grub_multiboot_payload_orig
, load_size
);
233 if (header
->bss_end_addr
)
234 grub_memset ((void *) (grub_multiboot_payload_orig
+ load_size
), 0,
235 header
->bss_end_addr
- header
->load_addr
- load_size
);
237 grub_multiboot_payload_eip
= header
->entry_addr
;
240 else if (grub_multiboot_load_elf (file
, buffer
) != GRUB_ERR_NONE
)
243 if (header
->flags
& MULTIBOOT_VIDEO_MODE
)
245 switch (header
->mode_type
)
248 grub_env_set ("gfxpayload", "text");
254 if (header
->depth
&& header
->width
&& header
->height
)
255 buf
= grub_xasprintf ("%dx%dx%d,%dx%d,auto", header
->width
,
256 header
->height
, header
->depth
, header
->width
,
258 else if (header
->width
&& header
->height
)
259 buf
= grub_xasprintf ("%dx%d,auto", header
->width
, header
->height
);
261 buf
= grub_strdup ("auto");
265 grub_env_set ("gfxpayload", buf
);
272 grub_multiboot_set_accepts_video (!!(header
->flags
& MULTIBOOT_VIDEO_MODE
));
274 grub_multiboot_set_bootdev ();
276 grub_loader_set (grub_multiboot_boot
, grub_multiboot_unload
, 0);
280 grub_file_close (file
);
282 if (grub_errno
!= GRUB_ERR_NONE
)
284 grub_relocator32_free (grub_multiboot_payload_orig
);
285 grub_dl_unref (my_mod
);
290 grub_module (int argc
, char *argv
[])
292 grub_file_t file
= 0;
299 grub_error (GRUB_ERR_BAD_ARGUMENT
, "no module specified");
303 if (!grub_multiboot_payload_orig
)
305 grub_error (GRUB_ERR_BAD_ARGUMENT
,
306 "you need to load the multiboot kernel first");
310 file
= grub_gzfile_open (argv
[0], 1);
314 size
= grub_file_size (file
);
315 module
= grub_memalign (MULTIBOOT_MOD_ALIGN
, size
);
319 err
= grub_multiboot_add_module ((grub_addr_t
) module
, size
,
324 if (grub_file_read (file
, module
, size
) != size
)
326 grub_error (GRUB_ERR_FILE_READ_ERROR
, "couldn't read file");
332 grub_file_close (file
);