2009-07-01 Robert Millan <rmh.grub@aybabtu.com>
[grub2/phcoder/solaris.git] / loader / multiboot_loader.c
blob29c34d9f83f46cb0a7e16ac10e33a613a37d202d
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_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;
150 #endif
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;
159 return grub_errno;
161 fail:
162 if (file)
163 grub_file_close (file);
165 grub_dl_unref (my_mod);
167 return grub_errno;
170 static grub_err_t
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);
182 #endif
183 if (module_version_status == 2)
185 grub_dprintf("multiboot_loader",
186 "Launching multiboot 2 grub_module2() function\n");
187 grub_module2 (argc, argv);
190 return grub_errno;
193 static grub_command_t cmd_multiboot, cmd_module;
195 GRUB_MOD_INIT(multiboot)
197 cmd_multiboot =
198 grub_register_command ("multiboot", grub_cmd_multiboot_loader,
199 0, "load a multiboot kernel");
200 cmd_module =
201 grub_register_command ("module", grub_cmd_module_loader,
202 0, "load a multiboot module");
204 my_mod = mod;
207 GRUB_MOD_FINI(multiboot)
209 grub_unregister_command (cmd_multiboot);
210 grub_unregister_command (cmd_module);