]> git.proxmox.com Git - grub2.git/blob - loader/multiboot_loader.c
This patch is to introduce multiboot 2 loading capabilities to grub2
[grub2.git] / loader / multiboot_loader.c
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.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20 #include <multiboot2.h>
21 #include <grub/multiboot_loader.h>
22 #include <grub/multiboot.h>
23 #include <grub/multiboot2.h>
24 #include <grub/elf.h>
25 #include <grub/file.h>
26 #include <grub/err.h>
27 #include <grub/rescue.h>
28 #include <grub/dl.h>
29 #include <grub/mm.h>
30 #include <grub/misc.h>
31 #include <grub/gzio.h>
32
33 grub_dl_t my_mod;
34
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 - Mulitboot version 1
39 * 2 - Mutliboot version 2
40 */
41
42 static unsigned int module_version_status = 1;
43
44 static int
45 find_multi_boot1_header (grub_file_t file)
46 {
47 struct grub_multiboot_header *header;
48 char buffer[MULTIBOOT_SEARCH];
49 int found_status = 0;
50 grub_ssize_t len;
51
52 len = grub_file_read (file, buffer, MULTIBOOT_SEARCH);
53 if (len < 32)
54 return found_status;
55
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))
61 {
62 if (header->magic == MULTIBOOT_MAGIC
63 && !(header->magic + header->flags + header->checksum))
64 {
65 found_status = 1;
66 break;
67 }
68 }
69
70 return found_status;
71 }
72
73 void
74 grub_rescue_cmd_multiboot_loader (int argc, char *argv[])
75 {
76
77 grub_file_t file = 0;
78 int header_multi_ver_found = 0;
79
80 grub_dl_ref (my_mod);
81
82 if (argc == 0)
83 {
84 grub_error (GRUB_ERR_BAD_ARGUMENT, "No kernel specified");
85 goto fail;
86 }
87
88 file = grub_gzfile_open (argv[0], 1);
89 if (! file)
90 {
91 grub_error (GRUB_ERR_BAD_ARGUMENT, "Couldn't open file");
92 goto fail;
93 }
94
95 /* find which header is in the file */
96 if (find_multi_boot1_header(file))
97 header_multi_ver_found = 1;
98 else
99 {
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;
106 }
107
108 /* close file before calling functions */
109 if (file)
110 grub_file_close (file);
111
112 /* Launch multi boot with header */
113
114 /* XXX Find a better way to identify this.
115 This is for i386-pc */
116 #ifdef __i386__
117 if (header_multi_ver_found == 1)
118 {
119 grub_dprintf ("multiboot_loader",
120 "Launching multiboot 1 grub_multiboot() function\n");
121 grub_multiboot (argc, argv);
122 module_version_status = 1;
123 }
124 #endif
125 if (header_multi_ver_found == 0 || header_multi_ver_found == 2)
126 {
127 grub_dprintf ("multiboot_loader",
128 "Launching mulitboot 2 grub_multiboot2() function\n");
129 grub_multiboot2 (argc, argv);
130 module_version_status = 2;
131 }
132
133 return;
134
135 fail:
136 if (file)
137 grub_file_close (file);
138
139 grub_dl_unref (my_mod);
140 }
141
142 void
143 grub_rescue_cmd_module_loader (int argc, char *argv[])
144 {
145
146 #ifdef __i386__
147 if (module_version_status == 1)
148 {
149 grub_dprintf("multiboot_loader",
150 "Launching multiboot 1 grub_module() function\n");
151 grub_module (argc, argv);
152 }
153 #endif
154 if (module_version_status == 2)
155 {
156 grub_dprintf("multiboot_loader",
157 "Launching multiboot 2 grub_module2() function\n");
158 grub_module2 (argc, argv);
159 }
160 }
161
162 GRUB_MOD_INIT(multiboot)
163 {
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");
168
169 my_mod = mod;
170 }
171
172 GRUB_MOD_FINI(multiboot)
173 {
174 grub_rescue_unregister_command ("mulitboot");
175 grub_rescue_unregister_command ("module");
176 }