]> git.proxmox.com Git - grub2.git/blob - grub-core/loader/efi/chainloader.c
bump version to 2.06-13+pmx2
[grub2.git] / grub-core / loader / efi / chainloader.c
1 /* chainloader.c - boot another boot loader */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2004,2006,2007,2008 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 /* TODO: support load options. */
21
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/charset.h>
29 #include <grub/mm.h>
30 #include <grub/types.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/command.h>
36 #include <grub/i18n.h>
37 #include <grub/net.h>
38 #if defined (__i386__) || defined (__x86_64__)
39 #include <grub/macho.h>
40 #include <grub/i386/macho.h>
41 #endif
42
43 GRUB_MOD_LICENSE ("GPLv3+");
44
45 static grub_dl_t my_mod;
46
47 static grub_efi_physical_address_t address;
48 static grub_efi_uintn_t pages;
49 static grub_efi_device_path_t *file_path;
50 static grub_efi_handle_t image_handle;
51 static grub_efi_char16_t *cmdline;
52
53 static grub_err_t
54 grub_chainloader_unload (void)
55 {
56 grub_efi_boot_services_t *b;
57
58 b = grub_efi_system_table->boot_services;
59 efi_call_1 (b->unload_image, image_handle);
60 efi_call_2 (b->free_pages, address, pages);
61
62 grub_free (file_path);
63 grub_free (cmdline);
64 cmdline = 0;
65 file_path = 0;
66
67 grub_dl_unref (my_mod);
68 return GRUB_ERR_NONE;
69 }
70
71 static grub_err_t
72 grub_chainloader_boot (void)
73 {
74 grub_efi_boot_services_t *b;
75 grub_efi_status_t status;
76 grub_efi_uintn_t exit_data_size;
77 grub_efi_char16_t *exit_data = NULL;
78
79 b = grub_efi_system_table->boot_services;
80 status = efi_call_3 (b->start_image, image_handle, &exit_data_size, &exit_data);
81 if (status != GRUB_EFI_SUCCESS)
82 {
83 if (exit_data)
84 {
85 char *buf;
86
87 buf = grub_malloc (exit_data_size * 4 + 1);
88 if (buf)
89 {
90 *grub_utf16_to_utf8 ((grub_uint8_t *) buf,
91 exit_data, exit_data_size) = 0;
92
93 grub_error (GRUB_ERR_BAD_OS, "%s", buf);
94 grub_free (buf);
95 }
96 }
97 else
98 grub_error (GRUB_ERR_BAD_OS, "unknown error");
99 }
100
101 if (exit_data)
102 efi_call_1 (b->free_pool, exit_data);
103
104 grub_loader_unset ();
105
106 return grub_errno;
107 }
108
109 static grub_err_t
110 copy_file_path (grub_efi_file_path_device_path_t *fp,
111 const char *str, grub_efi_uint16_t len)
112 {
113 grub_efi_char16_t *p, *path_name;
114 grub_efi_uint16_t size;
115
116 fp->header.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE;
117 fp->header.subtype = GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE;
118
119 path_name = grub_calloc (len, GRUB_MAX_UTF16_PER_UTF8 * sizeof (*path_name));
120 if (!path_name)
121 return grub_error (GRUB_ERR_OUT_OF_MEMORY, "failed to allocate path buffer");
122
123 size = grub_utf8_to_utf16 (path_name, len * GRUB_MAX_UTF16_PER_UTF8,
124 (const grub_uint8_t *) str, len, 0);
125 for (p = path_name; p < path_name + size; p++)
126 if (*p == '/')
127 *p = '\\';
128
129 grub_memcpy (fp->path_name, path_name, size * sizeof (*fp->path_name));
130 /* File Path is NULL terminated */
131 fp->path_name[size++] = '\0';
132 fp->header.length = size * sizeof (grub_efi_char16_t) + sizeof (*fp);
133 grub_free (path_name);
134 return GRUB_ERR_NONE;
135 }
136
137 static grub_efi_device_path_t *
138 make_file_path (grub_efi_device_path_t *dp, const char *filename)
139 {
140 char *dir_start;
141 char *dir_end;
142 grub_size_t size;
143 grub_efi_device_path_t *d;
144
145 dir_start = grub_strchr (filename, ')');
146 if (! dir_start)
147 dir_start = (char *) filename;
148 else
149 dir_start++;
150
151 dir_end = grub_strrchr (dir_start, '/');
152 if (! dir_end)
153 {
154 grub_error (GRUB_ERR_BAD_FILENAME, "invalid EFI file path");
155 return 0;
156 }
157
158 size = 0;
159 d = dp;
160 while (d)
161 {
162 grub_size_t len = GRUB_EFI_DEVICE_PATH_LENGTH (d);
163
164 if (len < 4)
165 {
166 grub_error (GRUB_ERR_OUT_OF_RANGE,
167 "malformed EFI Device Path node has length=%" PRIuGRUB_SIZE, len);
168 return NULL;
169 }
170
171 size += len;
172 if ((GRUB_EFI_END_ENTIRE_DEVICE_PATH (d)))
173 break;
174 d = GRUB_EFI_NEXT_DEVICE_PATH (d);
175 }
176
177 /* File Path is NULL terminated. Allocate space for 2 extra characters */
178 /* FIXME why we split path in two components? */
179 file_path = grub_malloc (size
180 + ((grub_strlen (dir_start) + 2)
181 * GRUB_MAX_UTF16_PER_UTF8
182 * sizeof (grub_efi_char16_t))
183 + sizeof (grub_efi_file_path_device_path_t) * 2);
184 if (! file_path)
185 return 0;
186
187 grub_memcpy (file_path, dp, size);
188
189 /* Fill the file path for the directory. */
190 d = (grub_efi_device_path_t *) ((char *) file_path
191 + ((char *) d - (char *) dp));
192 grub_efi_print_device_path (d);
193 if (copy_file_path ((grub_efi_file_path_device_path_t *) d,
194 dir_start, dir_end - dir_start) != GRUB_ERR_NONE)
195 {
196 fail:
197 grub_free (file_path);
198 return 0;
199 }
200
201 /* Fill the file path for the file. */
202 d = GRUB_EFI_NEXT_DEVICE_PATH (d);
203 if (copy_file_path ((grub_efi_file_path_device_path_t *) d,
204 dir_end + 1, grub_strlen (dir_end + 1)) != GRUB_ERR_NONE)
205 goto fail;
206
207 /* Fill the end of device path nodes. */
208 d = GRUB_EFI_NEXT_DEVICE_PATH (d);
209 d->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
210 d->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
211 d->length = sizeof (*d);
212
213 return file_path;
214 }
215
216 static grub_err_t
217 grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
218 int argc, char *argv[])
219 {
220 grub_file_t file = 0;
221 grub_ssize_t size;
222 grub_efi_status_t status;
223 grub_efi_boot_services_t *b;
224 grub_device_t dev = 0;
225 grub_efi_device_path_t *dp = 0;
226 grub_efi_loaded_image_t *loaded_image;
227 char *filename;
228 void *boot_image = 0;
229 grub_efi_handle_t dev_handle = 0;
230
231 if (argc == 0)
232 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
233 filename = argv[0];
234
235 grub_dl_ref (my_mod);
236
237 /* Initialize some global variables. */
238 address = 0;
239 image_handle = 0;
240 file_path = 0;
241
242 b = grub_efi_system_table->boot_services;
243
244 file = grub_file_open (filename, GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE);
245 if (! file)
246 goto fail;
247
248 /* Get the root device's device path. */
249 dev = grub_device_open (0);
250 if (! dev)
251 goto fail;
252
253 if (dev->disk)
254 dev_handle = grub_efidisk_get_device_handle (dev->disk);
255 else if (dev->net && dev->net->server)
256 {
257 grub_net_network_level_address_t addr;
258 struct grub_net_network_level_interface *inf;
259 grub_net_network_level_address_t gateway;
260 grub_err_t err;
261
262 err = grub_net_resolve_address (dev->net->server, &addr);
263 if (err)
264 goto fail;
265
266 err = grub_net_route_address (addr, &gateway, &inf);
267 if (err)
268 goto fail;
269
270 dev_handle = grub_efinet_get_device_handle (inf->card);
271 }
272
273 if (dev_handle)
274 dp = grub_efi_get_device_path (dev_handle);
275
276 if (! dp)
277 {
278 grub_error (GRUB_ERR_BAD_DEVICE, "not a valid root device");
279 goto fail;
280 }
281
282 file_path = make_file_path (dp, filename);
283 if (! file_path)
284 goto fail;
285
286 grub_printf ("file path: ");
287 grub_efi_print_device_path (file_path);
288
289 size = grub_file_size (file);
290 if (!size)
291 {
292 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
293 filename);
294 goto fail;
295 }
296 pages = (((grub_efi_uintn_t) size + ((1 << 12) - 1)) >> 12);
297
298 status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ANY_PAGES,
299 GRUB_EFI_LOADER_CODE,
300 pages, &address);
301 if (status != GRUB_EFI_SUCCESS)
302 {
303 grub_dprintf ("chain", "Failed to allocate %u pages\n",
304 (unsigned int) pages);
305 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
306 goto fail;
307 }
308
309 boot_image = (void *) ((grub_addr_t) address);
310 if (grub_file_read (file, boot_image, size) != size)
311 {
312 if (grub_errno == GRUB_ERR_NONE)
313 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
314 filename);
315
316 goto fail;
317 }
318
319 #if defined (__i386__) || defined (__x86_64__)
320 if (size >= (grub_ssize_t) sizeof (struct grub_macho_fat_header))
321 {
322 struct grub_macho_fat_header *head = boot_image;
323 if (head->magic
324 == grub_cpu_to_le32_compile_time (GRUB_MACHO_FAT_EFI_MAGIC))
325 {
326 grub_uint32_t i;
327 struct grub_macho_fat_arch *archs
328 = (struct grub_macho_fat_arch *) (head + 1);
329 for (i = 0; i < grub_cpu_to_le32 (head->nfat_arch); i++)
330 {
331 if (GRUB_MACHO_CPUTYPE_IS_HOST_CURRENT (archs[i].cputype))
332 break;
333 }
334 if (i == grub_cpu_to_le32 (head->nfat_arch))
335 {
336 grub_error (GRUB_ERR_BAD_OS, "no compatible arch found");
337 goto fail;
338 }
339 if (grub_cpu_to_le32 (archs[i].offset)
340 > ~grub_cpu_to_le32 (archs[i].size)
341 || grub_cpu_to_le32 (archs[i].offset)
342 + grub_cpu_to_le32 (archs[i].size)
343 > (grub_size_t) size)
344 {
345 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
346 filename);
347 goto fail;
348 }
349 boot_image = (char *) boot_image + grub_cpu_to_le32 (archs[i].offset);
350 size = grub_cpu_to_le32 (archs[i].size);
351 }
352 }
353 #endif
354
355 status = efi_call_6 (b->load_image, 0, grub_efi_image_handle, file_path,
356 boot_image, size,
357 &image_handle);
358 if (status != GRUB_EFI_SUCCESS)
359 {
360 if (status == GRUB_EFI_OUT_OF_RESOURCES)
361 grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of resources");
362 else
363 grub_error (GRUB_ERR_BAD_OS, "cannot load image");
364
365 goto fail;
366 }
367
368 /* LoadImage does not set a device handler when the image is
369 loaded from memory, so it is necessary to set it explicitly here.
370 This is a mess. */
371 loaded_image = grub_efi_get_loaded_image (image_handle);
372 if (! loaded_image)
373 {
374 grub_error (GRUB_ERR_BAD_OS, "no loaded image available");
375 goto fail;
376 }
377 loaded_image->device_handle = dev_handle;
378
379 if (argc > 1)
380 {
381 int i, len;
382 grub_efi_char16_t *p16;
383
384 for (i = 1, len = 0; i < argc; i++)
385 len += grub_strlen (argv[i]) + 1;
386
387 len *= sizeof (grub_efi_char16_t);
388 cmdline = p16 = grub_malloc (len);
389 if (! cmdline)
390 goto fail;
391
392 for (i = 1; i < argc; i++)
393 {
394 char *p8;
395
396 p8 = argv[i];
397 while (*p8)
398 *(p16++) = *(p8++);
399
400 *(p16++) = ' ';
401 }
402 *(--p16) = 0;
403
404 loaded_image->load_options = cmdline;
405 loaded_image->load_options_size = len;
406 }
407
408 grub_file_close (file);
409 grub_device_close (dev);
410
411 grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 0);
412 return 0;
413
414 fail:
415
416 if (dev)
417 grub_device_close (dev);
418
419 if (file)
420 grub_file_close (file);
421
422 grub_free (file_path);
423
424 if (address)
425 efi_call_2 (b->free_pages, address, pages);
426
427 grub_dl_unref (my_mod);
428
429 return grub_errno;
430 }
431
432 static grub_command_t cmd;
433
434 GRUB_MOD_INIT(chainloader)
435 {
436 cmd = grub_register_command ("chainloader", grub_cmd_chainloader,
437 0, N_("Load another boot loader."));
438 my_mod = mod;
439 }
440
441 GRUB_MOD_FINI(chainloader)
442 {
443 grub_unregister_command (cmd);
444 }