]> git.proxmox.com Git - grub2.git/blob - loader/multiboot.c
* loader/multiboot.c (GRUB_MOD_INIT) [GRUB_USE_MULTIBOOT2]:
[grub2.git] / 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 * - VBE support
24 * - symbol table
25 * - drives table
26 * - ROM configuration table
27 * - APM table
28 */
29
30 #include <grub/loader.h>
31 #include <grub/command.h>
32 #include <grub/machine/loader.h>
33 #include <grub/multiboot.h>
34 #include <grub/cpu/multiboot.h>
35 #include <grub/elf.h>
36 #include <grub/aout.h>
37 #include <grub/file.h>
38 #include <grub/err.h>
39 #include <grub/dl.h>
40 #include <grub/mm.h>
41 #include <grub/misc.h>
42 #include <grub/gzio.h>
43 #include <grub/env.h>
44 #include <grub/cpu/relocator.h>
45 #include <grub/video.h>
46 #include <grub/memory.h>
47 #include <grub/i18n.h>
48
49 #ifdef GRUB_MACHINE_EFI
50 #include <grub/efi/efi.h>
51 #endif
52
53 #if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
54 #define DEFAULT_VIDEO_MODE "text"
55 #else
56 #define DEFAULT_VIDEO_MODE "auto"
57 #endif
58
59 grub_size_t grub_multiboot_alloc_mbi;
60
61 char *grub_multiboot_payload_orig;
62 grub_addr_t grub_multiboot_payload_dest;
63 grub_size_t grub_multiboot_pure_size;
64 grub_uint32_t grub_multiboot_payload_eip;
65 static int accepts_video;
66 static int accepts_ega_text;
67 static int console_required;
68 static grub_dl_t my_mod;
69
70
71 /* Return the length of the Multiboot mmap that will be needed to allocate
72 our platform's map. */
73 grub_uint32_t
74 grub_get_multiboot_mmap_count (void)
75 {
76 grub_size_t count = 0;
77
78 auto int NESTED_FUNC_ATTR hook (grub_uint64_t, grub_uint64_t, grub_uint32_t);
79 int NESTED_FUNC_ATTR hook (grub_uint64_t addr __attribute__ ((unused)),
80 grub_uint64_t size __attribute__ ((unused)),
81 grub_uint32_t type __attribute__ ((unused)))
82 {
83 count++;
84 return 0;
85 }
86
87 grub_mmap_iterate (hook);
88
89 return count;
90 }
91
92 grub_err_t
93 grub_multiboot_set_video_mode (void)
94 {
95 grub_err_t err;
96 const char *modevar;
97
98 if (accepts_video || !GRUB_MACHINE_HAS_VGA_TEXT)
99 {
100 modevar = grub_env_get ("gfxpayload");
101 if (! modevar || *modevar == 0)
102 err = grub_video_set_mode (DEFAULT_VIDEO_MODE, 0, 0);
103 else
104 {
105 char *tmp;
106 tmp = grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE, modevar);
107 if (! tmp)
108 return grub_errno;
109 err = grub_video_set_mode (tmp, 0, 0);
110 grub_free (tmp);
111 }
112 }
113 else
114 err = grub_video_set_mode ("text", 0, 0);
115
116 return err;
117 }
118
119 static grub_err_t
120 grub_multiboot_boot (void)
121 {
122 grub_size_t mbi_size;
123 grub_err_t err;
124 struct grub_relocator32_state state = MULTIBOOT_INITIAL_STATE;
125
126 state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
127
128 mbi_size = grub_multiboot_get_mbi_size ();
129 if (grub_multiboot_alloc_mbi < mbi_size)
130 {
131 grub_multiboot_payload_orig
132 = grub_relocator32_realloc (grub_multiboot_payload_orig,
133 grub_multiboot_pure_size + mbi_size);
134 if (!grub_multiboot_payload_orig)
135 return grub_errno;
136 grub_multiboot_alloc_mbi = mbi_size;
137 }
138
139 state.MULTIBOOT_MBI_REGISTER = grub_multiboot_payload_dest
140 + grub_multiboot_pure_size;
141 err = grub_multiboot_make_mbi (grub_multiboot_payload_orig,
142 grub_multiboot_payload_dest,
143 grub_multiboot_pure_size, mbi_size);
144 if (err)
145 return err;
146
147 #ifdef GRUB_MACHINE_EFI
148 if (! grub_efi_finish_boot_services ())
149 grub_fatal ("cannot exit boot services");
150 #endif
151
152 grub_relocator32_boot (grub_multiboot_payload_orig,
153 grub_multiboot_payload_dest,
154 state);
155
156 /* Not reached. */
157 return GRUB_ERR_NONE;
158 }
159
160 static grub_err_t
161 grub_multiboot_unload (void)
162 {
163 grub_multiboot_free_mbi ();
164
165 grub_relocator32_free (grub_multiboot_payload_orig);
166
167 grub_multiboot_alloc_mbi = 0;
168
169 grub_multiboot_payload_orig = NULL;
170 grub_dl_unref (my_mod);
171
172 return GRUB_ERR_NONE;
173 }
174
175 #define MULTIBOOT_LOAD_ELF64
176 #include "multiboot_elfxx.c"
177 #undef MULTIBOOT_LOAD_ELF64
178
179 #define MULTIBOOT_LOAD_ELF32
180 #include "multiboot_elfxx.c"
181 #undef MULTIBOOT_LOAD_ELF32
182
183 /* Load ELF32 or ELF64. */
184 grub_err_t
185 grub_multiboot_load_elf (grub_file_t file, void *buffer)
186 {
187 if (grub_multiboot_is_elf32 (buffer))
188 return grub_multiboot_load_elf32 (file, buffer);
189 else if (grub_multiboot_is_elf64 (buffer))
190 return grub_multiboot_load_elf64 (file, buffer);
191
192 return grub_error (GRUB_ERR_UNKNOWN_OS, "unknown ELF class");
193 }
194
195 grub_err_t
196 grub_multiboot_set_console (int console_type, int accepted_consoles,
197 int width, int height, int depth,
198 int console_req)
199 {
200 console_required = console_req;
201 if (!(accepted_consoles
202 & (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER
203 | (GRUB_MACHINE_HAS_VGA_TEXT ? GRUB_MULTIBOOT_CONSOLE_EGA_TEXT : 0))))
204 {
205 if (console_required)
206 return grub_error (GRUB_ERR_BAD_OS,
207 "OS requires a console but none is available");
208 grub_printf ("WARNING: no console will be available to OS");
209 accepts_video = 0;
210 accepts_ega_text = 0;
211 return GRUB_ERR_NONE;
212 }
213
214 if (console_type == GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER)
215 {
216 char *buf;
217 if (depth && width && height)
218 buf = grub_xasprintf ("%dx%dx%d,%dx%d,auto", width,
219 height, depth, width, height);
220 else if (width && height)
221 buf = grub_xasprintf ("%dx%d,auto", width, height);
222 else
223 buf = grub_strdup ("auto");
224
225 if (!buf)
226 return grub_errno;
227 grub_env_set ("gfxpayload", buf);
228 grub_free (buf);
229 }
230 else
231 grub_env_set ("gfxpayload", "text");
232
233 accepts_video = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER);
234 accepts_ega_text = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_EGA_TEXT);
235 return GRUB_ERR_NONE;
236 }
237
238 static grub_err_t
239 grub_cmd_multiboot (grub_command_t cmd __attribute__ ((unused)),
240 int argc, char *argv[])
241 {
242 grub_file_t file = 0;
243 grub_err_t err;
244
245 grub_loader_unset ();
246
247 if (argc == 0)
248 return grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
249
250 file = grub_gzfile_open (argv[0], 1);
251 if (! file)
252 return grub_error (GRUB_ERR_BAD_ARGUMENT, "couldn't open file");
253
254 grub_dl_ref (my_mod);
255
256 /* Skip filename. */
257 grub_multiboot_init_mbi (argc - 1, argv + 1);
258
259 grub_relocator32_free (grub_multiboot_payload_orig);
260 grub_multiboot_payload_orig = NULL;
261
262 err = grub_multiboot_load (file);
263 if (err)
264 goto fail;
265
266 grub_multiboot_set_bootdev ();
267
268 grub_loader_set (grub_multiboot_boot, grub_multiboot_unload, 0);
269
270 fail:
271 if (file)
272 grub_file_close (file);
273
274 if (grub_errno != GRUB_ERR_NONE)
275 {
276 grub_relocator32_free (grub_multiboot_payload_orig);
277 grub_multiboot_free_mbi ();
278 grub_dl_unref (my_mod);
279 }
280
281 return grub_errno;
282 }
283
284 static grub_err_t
285 grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
286 int argc, char *argv[])
287 {
288 grub_file_t file = 0;
289 grub_ssize_t size;
290 char *module = 0;
291 grub_err_t err;
292
293 if (argc == 0)
294 return grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
295
296 if (!grub_multiboot_payload_orig)
297 return grub_error (GRUB_ERR_BAD_ARGUMENT,
298 "you need to load the multiboot kernel first");
299
300 file = grub_gzfile_open (argv[0], 1);
301 if (! file)
302 return grub_errno;
303
304 size = grub_file_size (file);
305 module = grub_memalign (MULTIBOOT_MOD_ALIGN, size);
306 if (! module)
307 {
308 grub_file_close (file);
309 return grub_errno;
310 }
311
312 err = grub_multiboot_add_module ((grub_addr_t) module, size,
313 argc - 1, argv + 1);
314 if (err)
315 {
316 grub_file_close (file);
317 return err;
318 }
319
320 if (grub_file_read (file, module, size) != size)
321 {
322 grub_file_close (file);
323 return grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
324 }
325
326 grub_file_close (file);
327 return GRUB_ERR_NONE;;
328 }
329
330 static grub_command_t cmd_multiboot, cmd_module;
331
332 GRUB_MOD_INIT(multiboot)
333 {
334 cmd_multiboot =
335 #ifdef GRUB_USE_MULTIBOOT2
336 grub_register_command ("multiboot2", grub_cmd_multiboot,
337 0, N_("Load a multiboot 2 kernel."));
338 cmd_module =
339 grub_register_command ("module2", grub_cmd_module,
340 0, N_("Load a multiboot 2 module."));
341 #else
342 grub_register_command ("multiboot", grub_cmd_multiboot,
343 0, N_("Load a multiboot kernel."));
344 cmd_module =
345 grub_register_command ("module", grub_cmd_module,
346 0, N_("Load a multiboot module."));
347 #endif
348
349 my_mod = mod;
350 }
351
352 GRUB_MOD_FINI(multiboot)
353 {
354 grub_unregister_command (cmd_multiboot);
355 grub_unregister_command (cmd_module);
356 }