make the linux-ppc packags be in synch with other platforms
[tangerine.git] / arch / common / boot / grub2 / loader / efi / chainloader.c
blob19285d90eaaa69273a6a46a6f053287ecb77c243
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;
44 static grub_err_t
45 grub_chainloader_unload (void)
47 grub_efi_boot_services_t *b;
49 b = grub_efi_system_table->boot_services;
50 b->unload_image (image_handle);
51 b->free_pages (address, pages);
52 grub_free (file_path);
54 grub_dl_unref (my_mod);
55 return GRUB_ERR_NONE;
58 static grub_err_t
59 grub_chainloader_boot (void)
61 grub_efi_boot_services_t *b;
62 grub_efi_status_t status;
63 grub_efi_uintn_t exit_data_size;
64 grub_efi_char16_t *exit_data;
66 b = grub_efi_system_table->boot_services;
67 status = b->start_image (image_handle, &exit_data_size, &exit_data);
68 if (status != GRUB_EFI_SUCCESS)
70 if (exit_data)
72 char *buf;
74 buf = grub_malloc (exit_data_size * 4 + 1);
75 if (buf)
77 *grub_utf16_to_utf8 ((grub_uint8_t *) buf,
78 exit_data, exit_data_size) = 0;
80 grub_error (GRUB_ERR_BAD_OS, buf);
81 grub_free (buf);
83 else
84 grub_error (GRUB_ERR_BAD_OS, "unknown error");
88 if (exit_data)
89 b->free_pool (exit_data);
91 grub_chainloader_unload ();
93 return grub_errno;
96 static void
97 copy_file_path (grub_efi_file_path_device_path_t *fp,
98 const char *str, grub_efi_uint16_t len)
100 grub_efi_char16_t *p;
101 grub_efi_uint16_t size;
103 fp->header.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE;
104 fp->header.subtype = GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE;
105 size = len * sizeof (grub_efi_char16_t) + sizeof (*fp);
106 fp->header.length[0] = (grub_efi_uint8_t) (size & 0xff);
107 fp->header.length[1] = (grub_efi_uint8_t) (size >> 8);
108 for (p = fp->path_name; len > 0; len--, p++, str++)
110 /* FIXME: this assumes that the path is in ASCII. */
111 *p = (grub_efi_char16_t) (*str == '/' ? '\\' : *str);
115 static grub_efi_device_path_t *
116 make_file_path (grub_efi_device_path_t *dp, const char *filename)
118 char *dir_start;
119 char *dir_end;
120 grub_size_t size;
121 grub_efi_device_path_t *d;
123 dir_start = grub_strchr (filename, ')');
124 if (! dir_start)
125 dir_start = (char *) filename;
126 else
127 dir_start++;
129 dir_end = grub_strrchr (dir_start, '/');
130 if (! dir_end)
132 grub_error (GRUB_ERR_BAD_FILENAME, "invalid EFI file path");
133 return 0;
136 size = 0;
137 d = dp;
138 while (1)
140 size += GRUB_EFI_DEVICE_PATH_LENGTH (d);
141 if ((GRUB_EFI_END_ENTIRE_DEVICE_PATH (d)))
142 break;
143 d = GRUB_EFI_NEXT_DEVICE_PATH (d);
146 file_path = grub_malloc (size
147 + ((grub_strlen (dir_start) + 1)
148 * sizeof (grub_efi_char16_t))
149 + sizeof (grub_efi_file_path_device_path_t) * 2);
150 if (! file_path)
151 return 0;
153 grub_memcpy (file_path, dp, size);
155 /* Fill the file path for the directory. */
156 d = (grub_efi_device_path_t *) ((char *) file_path
157 + ((char *) d - (char *) dp));
158 grub_efi_print_device_path (d);
159 copy_file_path ((grub_efi_file_path_device_path_t *) d,
160 dir_start, dir_end - dir_start);
162 /* Fill the file path for the file. */
163 d = GRUB_EFI_NEXT_DEVICE_PATH (d);
164 copy_file_path ((grub_efi_file_path_device_path_t *) d,
165 dir_end + 1, grub_strlen (dir_end + 1));
167 /* Fill the end of device path nodes. */
168 d = GRUB_EFI_NEXT_DEVICE_PATH (d);
169 d->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
170 d->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
171 d->length[0] = sizeof (*d);
172 d->length[1] = 0;
174 return file_path;
177 void
178 grub_chainloader_cmd (const char *filename)
180 grub_file_t file = 0;
181 grub_ssize_t size;
182 grub_efi_status_t status;
183 grub_efi_boot_services_t *b;
184 grub_efi_handle_t dev_handle = 0;
185 grub_device_t dev = 0;
186 grub_efi_device_path_t *dp = 0;
187 grub_efi_loaded_image_t *loaded_image;
189 grub_dl_ref (my_mod);
191 /* Initialize some global variables. */
192 address = 0;
193 image_handle = 0;
194 file_path = 0;
196 b = grub_efi_system_table->boot_services;
198 file = grub_file_open (filename);
199 if (! file)
200 goto fail;
202 /* Get the root device's device path. */
203 dev = grub_device_open (0);
204 if (! dev)
205 goto fail;
207 if (dev->disk)
209 dev_handle = grub_efidisk_get_device_handle (dev->disk);
210 if (dev_handle)
211 dp = grub_efi_get_device_path (dev_handle);
214 if (! dev->disk || ! dev_handle || ! dp)
216 grub_error (GRUB_ERR_BAD_DEVICE, "not a valid root device");
217 goto fail;
220 file_path = make_file_path (dp, filename);
221 if (! file_path)
222 goto fail;
224 grub_printf ("file path: ");
225 grub_efi_print_device_path (file_path);
227 size = grub_file_size (file);
228 pages = (((grub_efi_uintn_t) size + ((1 << 12) - 1)) >> 12);
230 status = b->allocate_pages (GRUB_EFI_ALLOCATE_ANY_PAGES,
231 GRUB_EFI_LOADER_CODE,
232 pages, &address);
233 if (status != GRUB_EFI_SUCCESS)
235 grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate %u pages", pages);
236 goto fail;
239 if (grub_file_read (file, (void *) ((grub_addr_t) address), size) != size)
241 if (grub_errno == GRUB_ERR_NONE)
242 grub_error (GRUB_ERR_BAD_OS, "too small");
244 goto fail;
247 status = b->load_image (0, grub_efi_image_handle, file_path,
248 (void *) ((grub_addr_t) address), size,
249 &image_handle);
250 if (status != GRUB_EFI_SUCCESS)
252 if (status == GRUB_EFI_OUT_OF_RESOURCES)
253 grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of resources");
254 else
255 grub_error (GRUB_ERR_BAD_OS, "cannot load image");
257 goto fail;
260 /* LoadImage does not set a device handler when the image is
261 loaded from memory, so it is necessary to set it explicitly here.
262 This is a mess. */
263 loaded_image = grub_efi_get_loaded_image (image_handle);
264 if (! loaded_image)
266 grub_error (GRUB_ERR_BAD_OS, "no loaded image available");
267 goto fail;
269 loaded_image->device_handle = dev_handle;
271 grub_file_close (file);
272 grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 0);
273 return;
275 fail:
277 if (dev)
278 grub_device_close (dev);
280 if (file)
281 grub_file_close (file);
283 if (file_path)
284 grub_free (file_path);
286 if (address)
287 b->free_pages (address, pages);
289 grub_dl_unref (my_mod);
292 static void
293 grub_rescue_cmd_chainloader (int argc, char *argv[])
295 if (argc == 0)
296 grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
297 else
298 grub_chainloader_cmd (argv[0]);
301 static const char loader_name[] = "chainloader";
303 GRUB_MOD_INIT(chainloader)
305 grub_rescue_register_command (loader_name,
306 grub_rescue_cmd_chainloader,
307 "load another boot loader");
308 my_mod = mod;
311 GRUB_MOD_FINI(chainloader)
313 grub_rescue_unregister_command (loader_name);