1 /* multiboot_loader.c - boot multiboot 1 or 2 OS image */
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>
25 #include <grub/file.h>
29 #include <grub/misc.h>
30 #include <grub/gzio.h>
31 #include <grub/command.h>
35 /* This tracks which version of multiboot to use when using
36 * the module command. By default use multiboot version 1.
38 * 1 - Multiboot version 1
39 * 2 - Multiboot version 2
42 static unsigned int module_version_status
= 1;
45 find_multi_boot1_header (grub_file_t file
)
47 struct grub_multiboot_header
*header
;
48 char buffer
[MULTIBOOT_SEARCH
];
52 len
= grub_file_read (file
, buffer
, MULTIBOOT_SEARCH
);
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
))
74 find_multi_boot2_header (grub_file_t file
)
76 struct multiboot_header
*header
;
77 char buffer
[MULTIBOOT_SEARCH
];
81 len
= grub_file_read (file
, buffer
, MULTIBOOT_SEARCH
);
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
)
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
);
112 grub_error (GRUB_ERR_BAD_ARGUMENT
, "No kernel specified");
116 file
= grub_gzfile_open (argv
[0], 1);
119 grub_error (GRUB_ERR_BAD_ARGUMENT
, "Couldn't open file");
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;
130 grub_error (GRUB_ERR_BAD_OS
, "Multiboot header not found");
134 /* close file before calling functions */
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_LINUXBIOS)
143 if (header_multi_ver_found
== 1)
145 grub_dprintf ("multiboot_loader",
146 "Launching multiboot 1 grub_multiboot() function\n");
147 grub_multiboot (argc
, argv
);
148 module_version_status
= 1;
151 if (header_multi_ver_found
== 0 || header_multi_ver_found
== 2)
153 grub_dprintf ("multiboot_loader",
154 "Launching multiboot 2 grub_multiboot2() function\n");
155 grub_multiboot2 (argc
, argv
);
156 module_version_status
= 2;
163 grub_file_close (file
);
165 grub_dl_unref (my_mod
);
171 grub_cmd_module_loader (grub_command_t cmd
__attribute__ ((unused
)),
172 int argc
, char *argv
[])
175 #if defined(GRUB_MACHINE_PCBIOS) || defined(GRUB_MACHINE_LINUXBIOS)
176 if (module_version_status
== 1)
178 grub_dprintf("multiboot_loader",
179 "Launching multiboot 1 grub_module() function\n");
180 grub_module (argc
, argv
);
183 if (module_version_status
== 2)
185 grub_dprintf("multiboot_loader",
186 "Launching multiboot 2 grub_module2() function\n");
187 grub_module2 (argc
, argv
);
193 static grub_command_t cmd_multiboot
, cmd_module
;
195 GRUB_MOD_INIT(multiboot
)
198 grub_register_command ("multiboot", grub_cmd_multiboot_loader
,
199 0, "load a multiboot kernel");
201 grub_register_command ("module", grub_cmd_module_loader
,
202 0, "load a multiboot module");
207 GRUB_MOD_FINI(multiboot
)
209 grub_unregister_command (cmd_multiboot
);
210 grub_unregister_command (cmd_module
);