1 /* multiboot_loader.c - boot multiboot 1 or 2 OS image */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2007 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 <multiboot2.h>
21 #include <grub/machine/machine.h>
22 #include <grub/multiboot_loader.h>
23 #include <grub/multiboot.h>
24 #include <grub/multiboot2.h>
26 #include <grub/file.h>
28 #include <grub/rescue.h>
31 #include <grub/misc.h>
32 #include <grub/gzio.h>
36 /* This tracks which version of multiboot to use when using
37 * the module command. By default use multiboot version 1.
39 * 1 - Multiboot version 1
40 * 2 - Multiboot version 2
43 static unsigned int module_version_status
= 1;
46 find_multi_boot1_header (grub_file_t file
)
48 struct grub_multiboot_header
*header
;
49 char buffer
[MULTIBOOT_SEARCH
];
53 len
= grub_file_read (file
, buffer
, MULTIBOOT_SEARCH
);
57 /* Look for the multiboot header in the buffer. The header should
58 be at least 12 bytes and aligned on a 4-byte boundary. */
59 for (header
= (struct grub_multiboot_header
*) buffer
;
60 ((char *) header
<= buffer
+ len
- 12) || (header
= 0);
61 header
= (struct grub_multiboot_header
*) ((char *) header
+ 4))
63 if (header
->magic
== MULTIBOOT_MAGIC
64 && !(header
->magic
+ header
->flags
+ header
->checksum
))
75 find_multi_boot2_header (grub_file_t file
)
77 struct multiboot_header
*header
;
78 char buffer
[MULTIBOOT_SEARCH
];
82 len
= grub_file_read (file
, buffer
, MULTIBOOT_SEARCH
);
86 /* Look for the multiboot header in the buffer. The header should
87 be at least 8 bytes and aligned on a 8-byte boundary. */
88 for (header
= (struct multiboot_header
*) buffer
;
89 ((char *) header
<= buffer
+ len
- 8) || (header
= 0);
90 header
= (struct multiboot_header
*) ((char *) header
+ 8))
92 if (header
->magic
== MULTIBOOT2_HEADER_MAGIC
)
103 grub_rescue_cmd_multiboot_loader (int argc
, char *argv
[])
106 grub_file_t file
= 0;
107 int header_multi_ver_found
= 0;
109 grub_dl_ref (my_mod
);
113 grub_error (GRUB_ERR_BAD_ARGUMENT
, "No kernel specified");
117 file
= grub_gzfile_open (argv
[0], 1);
120 grub_error (GRUB_ERR_BAD_ARGUMENT
, "Couldn't open file");
124 /* find which header is in the file */
125 if (find_multi_boot1_header (file
))
126 header_multi_ver_found
= 1;
127 else if (find_multi_boot2_header (file
))
128 header_multi_ver_found
= 2;
131 grub_error (GRUB_ERR_BAD_OS
, "Multiboot header not found");
135 /* close file before calling functions */
137 grub_file_close (file
);
139 /* Launch multi boot with header */
141 /* XXX Find a better way to identify this.
142 This is for i386-pc */
143 #if defined(GRUB_MACHINE_PCBIOS) || defined(GRUB_MACHINE_LINUXBIOS)
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;
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;
164 grub_file_close (file
);
166 grub_dl_unref (my_mod
);
170 grub_rescue_cmd_module_loader (int argc
, char *argv
[])
173 #if defined(GRUB_MACHINE_PCBIOS) || defined(GRUB_MACHINE_LINUXBIOS)
174 if (module_version_status
== 1)
176 grub_dprintf("multiboot_loader",
177 "Launching multiboot 1 grub_module() function\n");
178 grub_module (argc
, argv
);
181 if (module_version_status
== 2)
183 grub_dprintf("multiboot_loader",
184 "Launching multiboot 2 grub_module2() function\n");
185 grub_module2 (argc
, argv
);
189 GRUB_MOD_INIT(multiboot
)
191 grub_rescue_register_command ("multiboot", grub_rescue_cmd_multiboot_loader
,
192 "load a multiboot kernel");
193 grub_rescue_register_command ("module", grub_rescue_cmd_module_loader
,
194 "load a multiboot module");
199 GRUB_MOD_FINI(multiboot
)
201 grub_rescue_unregister_command ("multiboot");
202 grub_rescue_unregister_command ("module");