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/multiboot_loader.h>
22 #include <grub/multiboot.h>
23 #include <grub/multiboot2.h>
25 #include <grub/file.h>
27 #include <grub/rescue.h>
30 #include <grub/misc.h>
31 #include <grub/gzio.h>
35 /* This tracks which version of multiboot to use when using
36 * the module command. By default use multiboot version 1.
38 * 1 - Mulitboot version 1
39 * 2 - Mutliboot 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 grub_rescue_cmd_multiboot_loader (int argc
, char *argv
[])
78 int header_multi_ver_found
= 0;
84 grub_error (GRUB_ERR_BAD_ARGUMENT
, "No kernel specified");
88 file
= grub_gzfile_open (argv
[0], 1);
91 grub_error (GRUB_ERR_BAD_ARGUMENT
, "Couldn't open file");
95 /* find which header is in the file */
96 if (find_multi_boot1_header(file
))
97 header_multi_ver_found
= 1;
100 /* The behavior is that if you don't find a multiboot 1 header
101 use multiboot 2 loader (as you do not have to have a header
102 to use multiboot 2 */
103 grub_dprintf ("multiboot_loader", "No multiboot 1 header found. \n \
104 Using multiboot 2 loader\n");
105 header_multi_ver_found
= 0;
108 /* close file before calling functions */
110 grub_file_close (file
);
112 /* Launch multi boot with header */
114 /* XXX Find a better way to identify this.
115 This is for i386-pc */
117 if (header_multi_ver_found
== 1)
119 grub_dprintf ("multiboot_loader",
120 "Launching multiboot 1 grub_multiboot() function\n");
121 grub_multiboot (argc
, argv
);
122 module_version_status
= 1;
125 if (header_multi_ver_found
== 0 || header_multi_ver_found
== 2)
127 grub_dprintf ("multiboot_loader",
128 "Launching mulitboot 2 grub_multiboot2() function\n");
129 grub_multiboot2 (argc
, argv
);
130 module_version_status
= 2;
137 grub_file_close (file
);
139 grub_dl_unref (my_mod
);
143 grub_rescue_cmd_module_loader (int argc
, char *argv
[])
147 if (module_version_status
== 1)
149 grub_dprintf("multiboot_loader",
150 "Launching multiboot 1 grub_module() function\n");
151 grub_module (argc
, argv
);
154 if (module_version_status
== 2)
156 grub_dprintf("multiboot_loader",
157 "Launching multiboot 2 grub_module2() function\n");
158 grub_module2 (argc
, argv
);
162 GRUB_MOD_INIT(multiboot
)
164 grub_rescue_register_command ("multiboot", grub_rescue_cmd_multiboot_loader
,
165 "load a multiboot kernel");
166 grub_rescue_register_command ("module", grub_rescue_cmd_module_loader
,
167 "load a multiboot module");
172 GRUB_MOD_FINI(multiboot
)
174 grub_rescue_unregister_command ("mulitboot");
175 grub_rescue_unregister_command ("module");