Updated PCI IDs to latest snapshot.
[tangerine.git] / arch / common / boot / grub2 / loader / efi / chainloader.c
bloba8b3322cc7462f20db4c8ceeb65f7379739ac411
1 /* chainloader.c - boot another boot loader */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2004,2006,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 /* TODO: support load options. */
22 #include <grub/loader.h>
23 #include <grub/file.h>
24 #include <grub/err.h>
25 #include <grub/device.h>
26 #include <grub/disk.h>
27 #include <grub/misc.h>
28 #include <grub/mm.h>
29 #include <grub/types.h>
30 #include <grub/rescue.h>
31 #include <grub/dl.h>
32 #include <grub/efi/api.h>
33 #include <grub/efi/efi.h>
34 #include <grub/efi/disk.h>
35 #include <grub/efi/chainloader.h>
37 static grub_dl_t my_mod;
39 static grub_efi_physical_address_t address;
40 static grub_efi_uintn_t pages;
41 static grub_efi_device_path_t *file_path;
42 static grub_efi_handle_t image_handle;
43 static grub_efi_char16_t *cmdline;
45 static grub_err_t
46 grub_chainloader_unload (void)
48 grub_efi_boot_services_t *b;
50 b = grub_efi_system_table->boot_services;
51 efi_call_1 (b->unload_image, image_handle);
52 efi_call_2 (b->free_pages, address, pages);
54 grub_free (file_path);
55 grub_free (cmdline);
56 cmdline = 0;
58 grub_dl_unref (my_mod);
59 return GRUB_ERR_NONE;
62 static grub_err_t
63 grub_chainloader_boot (void)
65 grub_efi_boot_services_t *b;
66 grub_efi_status_t status;
67 grub_efi_uintn_t exit_data_size;
68 grub_efi_char16_t *exit_data;
70 b = grub_efi_system_table->boot_services;
71 status = efi_call_3 (b->start_image, image_handle, &exit_data_size, &exit_data);
72 if (status != GRUB_EFI_SUCCESS)
74 if (exit_data)
76 char *buf;
78 buf = grub_malloc (exit_data_size * 4 + 1);
79 if (buf)
81 *grub_utf16_to_utf8 ((grub_uint8_t *) buf,
82 exit_data, exit_data_size) = 0;
84 grub_error (GRUB_ERR_BAD_OS, buf);
85 grub_free (buf);
87 else
88 grub_error (GRUB_ERR_BAD_OS, "unknown error");
92 if (exit_data)
93 efi_call_1 (b->free_pool, exit_data);
95 grub_chainloader_unload ();
97 return grub_errno;
100 static void
101 copy_file_path (grub_efi_file_path_device_path_t *fp,
102 const char *str, grub_efi_uint16_t len)
104 grub_efi_char16_t *p;
105 grub_efi_uint16_t size;
107 fp->header.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE;
108 fp->header.subtype = GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE;
109 size = len * sizeof (grub_efi_char16_t) + sizeof (*fp);
110 fp->header.length[0] = (grub_efi_uint8_t) (size & 0xff);
111 fp->header.length[1] = (grub_efi_uint8_t) (size >> 8);
112 for (p = fp->path_name; len > 0; len--, p++, str++)
114 /* FIXME: this assumes that the path is in ASCII. */
115 *p = (grub_efi_char16_t) (*str == '/' ? '\\' : *str);
119 static grub_efi_device_path_t *
120 make_file_path (grub_efi_device_path_t *dp, const char *filename)
122 char *dir_start;
123 char *dir_end;
124 grub_size_t size;
125 grub_efi_device_path_t *d;
127 dir_start = grub_strchr (filename, ')');
128 if (! dir_start)
129 dir_start = (char *) filename;
130 else
131 dir_start++;
133 dir_end = grub_strrchr (dir_start, '/');
134 if (! dir_end)
136 grub_error (GRUB_ERR_BAD_FILENAME, "invalid EFI file path");
137 return 0;
140 size = 0;
141 d = dp;
142 while (1)
144 size += GRUB_EFI_DEVICE_PATH_LENGTH (d);
145 if ((GRUB_EFI_END_ENTIRE_DEVICE_PATH (d)))
146 break;
147 d = GRUB_EFI_NEXT_DEVICE_PATH (d);
150 file_path = grub_malloc (size
151 + ((grub_strlen (dir_start) + 1)
152 * sizeof (grub_efi_char16_t))
153 + sizeof (grub_efi_file_path_device_path_t) * 2);
154 if (! file_path)
155 return 0;
157 grub_memcpy (file_path, dp, size);
159 /* Fill the file path for the directory. */
160 d = (grub_efi_device_path_t *) ((char *) file_path
161 + ((char *) d - (char *) dp));
162 grub_efi_print_device_path (d);
163 copy_file_path ((grub_efi_file_path_device_path_t *) d,
164 dir_start, dir_end - dir_start);
166 /* Fill the file path for the file. */
167 d = GRUB_EFI_NEXT_DEVICE_PATH (d);
168 copy_file_path ((grub_efi_file_path_device_path_t *) d,
169 dir_end + 1, grub_strlen (dir_end + 1));
171 /* Fill the end of device path nodes. */
172 d = GRUB_EFI_NEXT_DEVICE_PATH (d);
173 d->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
174 d->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
175 d->length[0] = sizeof (*d);
176 d->length[1] = 0;
178 return file_path;
181 void
182 grub_rescue_cmd_chainloader (int argc, char *argv[])
184 grub_file_t file = 0;
185 grub_ssize_t size;
186 grub_efi_status_t status;
187 grub_efi_boot_services_t *b;
188 grub_efi_handle_t dev_handle = 0;
189 grub_device_t dev = 0;
190 grub_efi_device_path_t *dp = 0;
191 grub_efi_loaded_image_t *loaded_image;
192 char *filename;
194 if (argc == 0)
196 grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
197 return;
199 filename = argv[0];
201 grub_dl_ref (my_mod);
203 /* Initialize some global variables. */
204 address = 0;
205 image_handle = 0;
206 file_path = 0;
208 b = grub_efi_system_table->boot_services;
210 file = grub_file_open (filename);
211 if (! file)
212 goto fail;
214 /* Get the root device's device path. */
215 dev = grub_device_open (0);
216 if (! dev)
217 goto fail;
219 if (dev->disk)
221 dev_handle = grub_efidisk_get_device_handle (dev->disk);
222 if (dev_handle)
223 dp = grub_efi_get_device_path (dev_handle);
226 if (! dev->disk || ! dev_handle || ! dp)
228 grub_error (GRUB_ERR_BAD_DEVICE, "not a valid root device");
229 goto fail;
232 file_path = make_file_path (dp, filename);
233 if (! file_path)
234 goto fail;
236 grub_printf ("file path: ");
237 grub_efi_print_device_path (file_path);
239 size = grub_file_size (file);
240 pages = (((grub_efi_uintn_t) size + ((1 << 12) - 1)) >> 12);
242 status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ANY_PAGES,
243 GRUB_EFI_LOADER_CODE,
244 pages, &address);
245 if (status != GRUB_EFI_SUCCESS)
247 grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate %u pages", pages);
248 goto fail;
251 if (grub_file_read (file, (void *) ((grub_addr_t) address), size) != size)
253 if (grub_errno == GRUB_ERR_NONE)
254 grub_error (GRUB_ERR_BAD_OS, "too small");
256 goto fail;
259 status = efi_call_6 (b->load_image, 0, grub_efi_image_handle, file_path,
260 (void *) ((grub_addr_t) address), size,
261 &image_handle);
262 if (status != GRUB_EFI_SUCCESS)
264 if (status == GRUB_EFI_OUT_OF_RESOURCES)
265 grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of resources");
266 else
267 grub_error (GRUB_ERR_BAD_OS, "cannot load image");
269 goto fail;
272 /* LoadImage does not set a device handler when the image is
273 loaded from memory, so it is necessary to set it explicitly here.
274 This is a mess. */
275 loaded_image = grub_efi_get_loaded_image (image_handle);
276 if (! loaded_image)
278 grub_error (GRUB_ERR_BAD_OS, "no loaded image available");
279 goto fail;
281 loaded_image->device_handle = dev_handle;
283 grub_file_close (file);
285 if (argc > 1)
287 int i, len;
288 grub_efi_char16_t *p16;
290 for (i = 1, len = 0; i < argc; i++)
291 len += grub_strlen (argv[i]) + 1;
293 len *= sizeof (grub_efi_char16_t);
294 cmdline = p16 = grub_malloc (len);
295 if (! cmdline)
296 goto fail;
298 for (i = 1; i < argc; i++)
300 char *p8;
302 p8 = argv[i];
303 while (*p8)
304 *(p16++) = *(p8++);
306 *(p16++) = ' ';
308 *(--p16) = 0;
310 loaded_image->load_options = cmdline;
311 loaded_image->load_options_size = len;
314 grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 0);
315 return;
317 fail:
319 if (dev)
320 grub_device_close (dev);
322 if (file)
323 grub_file_close (file);
325 if (file_path)
326 grub_free (file_path);
328 if (address)
329 efi_call_2 (b->free_pages, address, pages);
331 grub_dl_unref (my_mod);
334 static const char loader_name[] = "chainloader";
336 GRUB_MOD_INIT(chainloader)
338 grub_rescue_register_command (loader_name,
339 grub_rescue_cmd_chainloader,
340 "load another boot loader");
341 my_mod = mod;
344 GRUB_MOD_FINI(chainloader)
346 grub_rescue_unregister_command (loader_name);