2009-11-21 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2.git] / loader / multiboot_loader.c
blob986ee0b040e7dd01796a2a4b4037e1b0c1e1f519
1 /* multiboot_loader.c - boot multiboot 1 or 2 OS image */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2007,2008 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/>.
20 #include <grub/machine/machine.h>
21 #include <grub/multiboot.h>
22 #include <grub/multiboot2.h>
23 #include <multiboot2.h>
24 #include <grub/elf.h>
25 #include <grub/file.h>
26 #include <grub/err.h>
27 #include <grub/dl.h>
28 #include <grub/mm.h>
29 #include <grub/misc.h>
30 #include <grub/gzio.h>
31 #include <grub/command.h>
33 grub_dl_t my_mod;
35 /* This tracks which version of multiboot to use when using
36 * the module command. By default use multiboot version 1.
37 * values:
38 * 1 - Multiboot version 1
39 * 2 - Multiboot version 2
42 static unsigned int module_version_status = 1;
44 static int
45 find_multi_boot1_header (grub_file_t file)
47 struct grub_multiboot_header *header;
48 char buffer[MULTIBOOT_SEARCH];
49 int found_status = 0;
50 grub_ssize_t len;
52 len = grub_file_read (file, buffer, MULTIBOOT_SEARCH);
53 if (len < 32)
54 return found_status;
56 /* Look for the multiboot header in the buffer. The header should
57 be at least 12 bytes and aligned on a 4-byte boundary. */
58 for (header = (struct grub_multiboot_header *) buffer;
59 ((char *) header <= buffer + len - 12) || (header = 0);
60 header = (struct grub_multiboot_header *) ((char *) header + 4))
62 if (header->magic == MULTIBOOT_MAGIC
63 && !(header->magic + header->flags + header->checksum))
65 found_status = 1;
66 break;
70 return found_status;
73 static int
74 find_multi_boot2_header (grub_file_t file)
76 struct multiboot_header *header;
77 char buffer[MULTIBOOT_SEARCH];
78 int found_status = 0;
79 grub_ssize_t len;
81 len = grub_file_read (file, buffer, MULTIBOOT_SEARCH);
82 if (len < 32)
83 return found_status;
85 /* Look for the multiboot header in the buffer. The header should
86 be at least 8 bytes and aligned on a 8-byte boundary. */
87 for (header = (struct multiboot_header *) buffer;
88 ((char *) header <= buffer + len - 8) || (header = 0);
89 header = (struct multiboot_header *) ((char *) header + 8))
91 if (header->magic == MULTIBOOT2_HEADER_MAGIC)
93 found_status = 1;
94 break;
98 return found_status;
101 static grub_err_t
102 grub_cmd_multiboot_loader (grub_command_t cmd __attribute__ ((unused)),
103 int argc, char *argv[])
105 grub_file_t file = 0;
106 int header_multi_ver_found = 0;
108 grub_dl_ref (my_mod);
110 if (argc == 0)
112 grub_error (GRUB_ERR_BAD_ARGUMENT, "No kernel specified");
113 goto fail;
116 file = grub_gzfile_open (argv[0], 1);
117 if (! file)
119 grub_error (GRUB_ERR_BAD_ARGUMENT, "Couldn't open file");
120 goto fail;
123 /* find which header is in the file */
124 if (find_multi_boot1_header (file))
125 header_multi_ver_found = 1;
126 else if (find_multi_boot2_header (file))
127 header_multi_ver_found = 2;
128 else
130 grub_error (GRUB_ERR_BAD_OS, "Multiboot header not found");
131 goto fail;
134 /* close file before calling functions */
135 if (file)
136 grub_file_close (file);
138 /* Launch multi boot with header */
140 /* XXX Find a better way to identify this.
141 This is for i386-pc */
142 #if defined(GRUB_MACHINE_PCBIOS) || defined(GRUB_MACHINE_COREBOOT) || \
143 defined(GRUB_MACHINE_QEMU)
144 if (header_multi_ver_found == 1)
146 grub_dprintf ("multiboot_loader",
147 "Launching multiboot 1 grub_multiboot() function\n");
148 grub_multiboot (argc, argv);
149 module_version_status = 1;
151 #endif
152 if (header_multi_ver_found == 0 || header_multi_ver_found == 2)
154 grub_dprintf ("multiboot_loader",
155 "Launching multiboot 2 grub_multiboot2() function\n");
156 grub_multiboot2 (argc, argv);
157 module_version_status = 2;
160 return grub_errno;
162 fail:
163 if (file)
164 grub_file_close (file);
166 grub_dl_unref (my_mod);
168 return grub_errno;
171 static grub_err_t
172 grub_cmd_module_loader (grub_command_t cmd __attribute__ ((unused)),
173 int argc, char *argv[])
176 #if defined(GRUB_MACHINE_PCBIOS) || defined(GRUB_MACHINE_COREBOOT) || \
177 defined(GRUB_MACHINE_QEMU)
178 if (module_version_status == 1)
180 grub_dprintf("multiboot_loader",
181 "Launching multiboot 1 grub_module() function\n");
182 grub_module (argc, argv);
184 #endif
185 if (module_version_status == 2)
187 grub_dprintf("multiboot_loader",
188 "Launching multiboot 2 grub_module2() function\n");
189 grub_module2 (argc, argv);
192 return grub_errno;
195 static grub_command_t cmd_multiboot, cmd_module;
197 GRUB_MOD_INIT(multiboot)
199 cmd_multiboot =
200 grub_register_command ("multiboot", grub_cmd_multiboot_loader,
201 0, "load a multiboot kernel");
202 cmd_module =
203 grub_register_command ("module", grub_cmd_module_loader,
204 0, "load a multiboot module");
206 my_mod = mod;
209 GRUB_MOD_FINI(multiboot)
211 grub_unregister_command (cmd_multiboot);
212 grub_unregister_command (cmd_module);