Updated PCI IDs to latest snapshot.
[tangerine.git] / arch / common / boot / grub2 / loader / multiboot_loader.c
blob53971dfeabb25d1edf27be79e8a870434d8e7275
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 static int
75 find_multi_boot2_header (grub_file_t file)
77 struct multiboot_header *header;
78 char buffer[MULTIBOOT_SEARCH];
79 int found_status = 0;
80 grub_ssize_t len;
82 len = grub_file_read (file, buffer, MULTIBOOT_SEARCH);
83 if (len < 32)
84 return found_status;
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)
94 found_status = 1;
95 break;
99 return found_status;
102 void
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);
111 if (argc == 0)
113 grub_error (GRUB_ERR_BAD_ARGUMENT, "No kernel specified");
114 goto fail;
117 file = grub_gzfile_open (argv[0], 1);
118 if (! file)
120 grub_error (GRUB_ERR_BAD_ARGUMENT, "Couldn't open file");
121 goto fail;
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;
129 else
131 grub_error (GRUB_ERR_BAD_OS, "Multiboot header not found");
132 goto fail;
135 /* close file before calling functions */
136 if (file)
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;
151 #endif
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;
160 return;
162 fail:
163 if (file)
164 grub_file_close (file);
166 grub_dl_unref (my_mod);
169 void
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);
180 #endif
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");
196 my_mod = mod;
199 GRUB_MOD_FINI(multiboot)
201 grub_rescue_unregister_command ("multiboot");
202 grub_rescue_unregister_command ("module");