GRUB-1.98 changes
[grub2/jjazz.git] / loader / i386 / multiboot.c
blobfc95882696d90e332b0bc11e32d1121dbdde8105
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.
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:
23 * - VBE support
24 * - symbol table
25 * - drives table
26 * - ROM configuration table
27 * - APM 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>
37 #include <grub/elf.h>
38 #include <grub/aout.h>
39 #include <grub/file.h>
40 #include <grub/err.h>
41 #include <grub/dl.h>
42 #include <grub/mm.h>
43 #include <grub/misc.h>
44 #include <grub/gzio.h>
45 #include <grub/env.h>
46 #include <grub/i386/relocator.h>
47 #include <grub/video.h>
49 #ifdef GRUB_MACHINE_EFI
50 #include <grub/efi/efi.h>
51 #endif
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;
61 static grub_err_t
62 grub_multiboot_boot (void)
64 grub_size_t mbi_size;
65 grub_err_t err;
66 struct grub_relocator32_state state =
68 .eax = MULTIBOOT_BOOTLOADER_MAGIC,
69 .ecx = 0,
70 .edx = 0,
71 .eip = grub_multiboot_payload_eip,
72 /* Set esp to some random location in low memory to avoid breaking
73 non-compliant kernels. */
74 .esp = 0x7ff00
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)
84 return grub_errno;
85 alloc_mbi = mbi_size;
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);
92 if (err)
93 return err;
95 #ifdef GRUB_MACHINE_EFI
96 if (! grub_efi_finish_boot_services ())
97 grub_fatal ("cannot exit boot services");
98 #endif
100 grub_relocator32_boot (grub_multiboot_payload_orig,
101 grub_multiboot_payload_dest,
102 state);
104 /* Not reached. */
105 return GRUB_ERR_NONE;
108 static grub_err_t
109 grub_multiboot_unload (void)
111 grub_multiboot_free_mbi ();
113 grub_relocator32_free (grub_multiboot_payload_orig);
115 alloc_mbi = 0;
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. */
132 static grub_err_t
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");
143 void
144 grub_multiboot (int argc, char *argv[])
146 grub_file_t file = 0;
147 char buffer[MULTIBOOT_SEARCH];
148 struct multiboot_header *header;
149 grub_ssize_t len;
151 grub_loader_unset ();
153 if (argc == 0)
155 grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
156 goto fail;
159 file = grub_gzfile_open (argv[0], 1);
160 if (! file)
162 grub_error (GRUB_ERR_BAD_ARGUMENT, "couldn't open file");
163 goto fail;
166 len = grub_file_read (file, buffer, MULTIBOOT_SEARCH);
167 if (len < 32)
169 grub_error (GRUB_ERR_BAD_OS, "file too small");
170 goto fail;
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))
181 break;
184 if (header == 0)
186 grub_error (GRUB_ERR_BAD_ARGUMENT, "no multiboot header found");
187 goto fail;
190 if (header->flags & UNSUPPORTED_FLAGS)
192 grub_error (GRUB_ERR_UNKNOWN_OS,
193 "unsupported flag: 0x%x", header->flags);
194 goto fail;
197 grub_relocator32_free (grub_multiboot_payload_orig);
198 grub_multiboot_payload_orig = NULL;
200 /* Skip filename. */
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);
212 else
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)
224 goto fail;
226 if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
227 goto fail;
229 grub_file_read (file, (void *) grub_multiboot_payload_orig, load_size);
230 if (grub_errno)
231 goto fail;
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)
241 goto fail;
243 if (header->flags & MULTIBOOT_VIDEO_MODE)
245 switch (header->mode_type)
247 case 1:
248 grub_env_set ("gfxpayload", "text");
249 break;
251 case 0:
253 char *buf;
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,
257 header->height);
258 else if (header->width && header->height)
259 buf = grub_xasprintf ("%dx%d,auto", header->width, header->height);
260 else
261 buf = grub_strdup ("auto");
263 if (!buf)
264 goto fail;
265 grub_env_set ("gfxpayload", buf);
266 grub_free (buf);
267 break;
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);
278 fail:
279 if (file)
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);
289 void
290 grub_module (int argc, char *argv[])
292 grub_file_t file = 0;
293 grub_ssize_t size;
294 char *module = 0;
295 grub_err_t err;
297 if (argc == 0)
299 grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
300 goto fail;
303 if (!grub_multiboot_payload_orig)
305 grub_error (GRUB_ERR_BAD_ARGUMENT,
306 "you need to load the multiboot kernel first");
307 goto fail;
310 file = grub_gzfile_open (argv[0], 1);
311 if (! file)
312 goto fail;
314 size = grub_file_size (file);
315 module = grub_memalign (MULTIBOOT_MOD_ALIGN, size);
316 if (! module)
317 goto fail;
319 err = grub_multiboot_add_module ((grub_addr_t) module, size,
320 argc - 1, argv + 1);
321 if (err)
322 goto fail;
324 if (grub_file_read (file, module, size) != size)
326 grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
327 goto fail;
330 fail:
331 if (file)
332 grub_file_close (file);