2008-08-02 Robert Millan <rmh@aybabtu.com>
[grub2/jjazz.git] / loader / multiboot_loader.c
blobd56b42c205adbb60073b48b06aaf1281380a13e4
1 /* multiboot_loader.c - boot multiboot 1 or 2 OS image */
2 /*
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>
25 #include <grub/elf.h>
26 #include <grub/file.h>
27 #include <grub/err.h>
28 #include <grub/rescue.h>
29 #include <grub/dl.h>
30 #include <grub/mm.h>
31 #include <grub/misc.h>
32 #include <grub/gzio.h>
34 grub_dl_t my_mod;
36 /* This tracks which version of multiboot to use when using
37 * the module command. By default use multiboot version 1.
38 * values:
39 * 1 - Multiboot version 1
40 * 2 - Multiboot version 2
43 static unsigned int module_version_status = 1;
45 static int
46 find_multi_boot1_header (grub_file_t file)
48 struct grub_multiboot_header *header;
49 char buffer[MULTIBOOT_SEARCH];
50 int found_status = 0;
51 grub_ssize_t len;
53 len = grub_file_read (file, buffer, MULTIBOOT_SEARCH);
54 if (len < 32)
55 return found_status;
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))
66 found_status = 1;
67 break;
71 return found_status;
74 void
75 grub_rescue_cmd_multiboot_loader (int argc, char *argv[])
78 grub_file_t file = 0;
79 int header_multi_ver_found = 0;
81 grub_dl_ref (my_mod);
83 if (argc == 0)
85 grub_error (GRUB_ERR_BAD_ARGUMENT, "No kernel specified");
86 goto fail;
89 file = grub_gzfile_open (argv[0], 1);
90 if (! file)
92 grub_error (GRUB_ERR_BAD_ARGUMENT, "Couldn't open file");
93 goto fail;
96 /* find which header is in the file */
97 if (find_multi_boot1_header(file))
98 header_multi_ver_found = 1;
99 else
101 /* The behavior is that if you don't find a multiboot 1 header
102 use multiboot 2 loader (as you do not have to have a header
103 to use multiboot 2 */
104 grub_dprintf ("multiboot_loader", "No multiboot 1 header found. \n \
105 Using multiboot 2 loader\n");
106 header_multi_ver_found = 0;
109 /* close file before calling functions */
110 if (file)
111 grub_file_close (file);
113 /* Launch multi boot with header */
115 /* XXX Find a better way to identify this.
116 This is for i386-pc */
117 #if defined(GRUB_MACHINE_PCBIOS) || defined(GRUB_MACHINE_LINUXBIOS)
118 if (header_multi_ver_found == 1)
120 grub_dprintf ("multiboot_loader",
121 "Launching multiboot 1 grub_multiboot() function\n");
122 grub_multiboot (argc, argv);
123 module_version_status = 1;
125 #endif
126 if (header_multi_ver_found == 0 || header_multi_ver_found == 2)
128 grub_dprintf ("multiboot_loader",
129 "Launching multiboot 2 grub_multiboot2() function\n");
130 grub_multiboot2 (argc, argv);
131 module_version_status = 2;
134 return;
136 fail:
137 if (file)
138 grub_file_close (file);
140 grub_dl_unref (my_mod);
143 void
144 grub_rescue_cmd_module_loader (int argc, char *argv[])
147 #if defined(GRUB_MACHINE_PCBIOS) || defined(GRUB_MACHINE_LINUXBIOS)
148 if (module_version_status == 1)
150 grub_dprintf("multiboot_loader",
151 "Launching multiboot 1 grub_module() function\n");
152 grub_module (argc, argv);
154 #endif
155 if (module_version_status == 2)
157 grub_dprintf("multiboot_loader",
158 "Launching multiboot 2 grub_module2() function\n");
159 grub_module2 (argc, argv);
163 GRUB_MOD_INIT(multiboot)
165 grub_rescue_register_command ("multiboot", grub_rescue_cmd_multiboot_loader,
166 "load a multiboot kernel");
167 grub_rescue_register_command ("module", grub_rescue_cmd_module_loader,
168 "load a multiboot module");
170 my_mod = mod;
173 GRUB_MOD_FINI(multiboot)
175 grub_rescue_unregister_command ("multiboot");
176 grub_rescue_unregister_command ("module");