1 /* chainloader.c - boot another boot loader */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2004,2006,2007,2008 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>
25 #include <grub/device.h>
26 #include <grub/disk.h>
27 #include <grub/misc.h>
28 #include <grub/charset.h>
30 #include <grub/types.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>
38 #if defined (__i386__) || defined (__x86_64__)
39 #include <grub/macho.h>
40 #include <grub/i386/macho.h>
43 GRUB_MOD_LICENSE ("GPLv3+");
45 static grub_dl_t my_mod
;
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
;
54 grub_chainloader_unload (void)
56 grub_efi_boot_services_t
*b
;
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
);
62 grub_free (file_path
);
67 grub_dl_unref (my_mod
);
72 grub_chainloader_boot (void)
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
;
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
)
87 buf
= grub_malloc (exit_data_size
* 4 + 1);
90 *grub_utf16_to_utf8 ((grub_uint8_t
*) buf
,
91 exit_data
, exit_data_size
) = 0;
93 grub_error (GRUB_ERR_BAD_OS
, buf
);
98 grub_error (GRUB_ERR_BAD_OS
, "unknown error");
102 efi_call_1 (b
->free_pool
, exit_data
);
104 grub_loader_unset ();
110 copy_file_path (grub_efi_file_path_device_path_t
*fp
,
111 const char *str
, grub_efi_uint16_t len
)
113 grub_efi_char16_t
*p
, *path_name
;
114 grub_efi_uint16_t size
;
116 fp
->header
.type
= GRUB_EFI_MEDIA_DEVICE_PATH_TYPE
;
117 fp
->header
.subtype
= GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE
;
119 path_name
= grub_malloc (len
* GRUB_MAX_UTF16_PER_UTF8
* sizeof (*path_name
));
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
++)
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
);
136 static grub_efi_device_path_t
*
137 make_file_path (grub_efi_device_path_t
*dp
, const char *filename
)
142 grub_efi_device_path_t
*d
;
144 dir_start
= grub_strchr (filename
, ')');
146 dir_start
= (char *) filename
;
150 dir_end
= grub_strrchr (dir_start
, '/');
153 grub_error (GRUB_ERR_BAD_FILENAME
, "invalid EFI file path");
161 size
+= GRUB_EFI_DEVICE_PATH_LENGTH (d
);
162 if ((GRUB_EFI_END_ENTIRE_DEVICE_PATH (d
)))
164 d
= GRUB_EFI_NEXT_DEVICE_PATH (d
);
167 /* File Path is NULL terminated. Allocate space for 2 extra characters */
168 /* FIXME why we split path in two components? */
169 file_path
= grub_malloc (size
170 + ((grub_strlen (dir_start
) + 2)
171 * GRUB_MAX_UTF16_PER_UTF8
172 * sizeof (grub_efi_char16_t
))
173 + sizeof (grub_efi_file_path_device_path_t
) * 2);
177 grub_memcpy (file_path
, dp
, size
);
179 /* Fill the file path for the directory. */
180 d
= (grub_efi_device_path_t
*) ((char *) file_path
181 + ((char *) d
- (char *) dp
));
182 //grub_efi_print_device_path (d);
183 copy_file_path ((grub_efi_file_path_device_path_t
*) d
,
184 dir_start
, dir_end
- dir_start
);
186 /* Fill the file path for the file. */
187 d
= GRUB_EFI_NEXT_DEVICE_PATH (d
);
188 copy_file_path ((grub_efi_file_path_device_path_t
*) d
,
189 dir_end
+ 1, grub_strlen (dir_end
+ 1));
191 /* Fill the end of device path nodes. */
192 d
= GRUB_EFI_NEXT_DEVICE_PATH (d
);
193 d
->type
= GRUB_EFI_END_DEVICE_PATH_TYPE
;
194 d
->subtype
= GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE
;
195 d
->length
= sizeof (*d
);
201 grub_cmd_chainloader (grub_command_t cmd
__attribute__ ((unused
)),
202 int argc
, char *argv
[])
204 grub_file_t file
= 0;
206 grub_efi_status_t status
;
207 grub_efi_boot_services_t
*b
;
208 grub_device_t dev
= 0;
209 grub_efi_device_path_t
*dp
= 0;
210 grub_efi_loaded_image_t
*loaded_image
;
212 void *boot_image
= 0;
213 grub_efi_handle_t dev_handle
= 0;
216 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
219 grub_dl_ref (my_mod
);
221 /* Initialize some global variables. */
226 b
= grub_efi_system_table
->boot_services
;
228 file
= grub_file_open (filename
, GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE
);
232 /* Get the root device's device path. */
233 dev
= grub_device_open (0);
238 dev_handle
= grub_efidisk_get_device_handle (dev
->disk
);
239 else if (dev
->net
&& dev
->net
->server
)
241 grub_net_network_level_address_t addr
;
242 struct grub_net_network_level_interface
*inf
;
243 grub_net_network_level_address_t gateway
;
246 err
= grub_net_resolve_address (dev
->net
->server
, &addr
);
250 err
= grub_net_route_address (addr
, &gateway
, &inf
);
254 dev_handle
= grub_efinet_get_device_handle (inf
->card
);
258 dp
= grub_efi_get_device_path (dev_handle
);
262 grub_error (GRUB_ERR_BAD_DEVICE
, "not a valid root device");
266 file_path
= make_file_path (dp
, filename
);
270 //grub_printf ("file path: ");
271 //grub_efi_print_device_path (file_path);
273 size
= grub_file_size (file
);
276 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
280 pages
= (((grub_efi_uintn_t
) size
+ ((1 << 12) - 1)) >> 12);
282 status
= efi_call_4 (b
->allocate_pages
, GRUB_EFI_ALLOCATE_ANY_PAGES
,
283 GRUB_EFI_LOADER_CODE
,
285 if (status
!= GRUB_EFI_SUCCESS
)
287 grub_dprintf ("chain", "Failed to allocate %u pages\n",
288 (unsigned int) pages
);
289 grub_error (GRUB_ERR_OUT_OF_MEMORY
, N_("out of memory"));
293 boot_image
= (void *) ((grub_addr_t
) address
);
294 if (grub_file_read (file
, boot_image
, size
) != size
)
296 if (grub_errno
== GRUB_ERR_NONE
)
297 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
303 #if defined (__i386__) || defined (__x86_64__)
304 if (size
>= (grub_ssize_t
) sizeof (struct grub_macho_fat_header
))
306 struct grub_macho_fat_header
*head
= boot_image
;
308 == grub_cpu_to_le32_compile_time (GRUB_MACHO_FAT_EFI_MAGIC
))
311 struct grub_macho_fat_arch
*archs
312 = (struct grub_macho_fat_arch
*) (head
+ 1);
313 for (i
= 0; i
< grub_cpu_to_le32 (head
->nfat_arch
); i
++)
315 if (GRUB_MACHO_CPUTYPE_IS_HOST_CURRENT (archs
[i
].cputype
))
318 if (i
== grub_cpu_to_le32 (head
->nfat_arch
))
320 grub_error (GRUB_ERR_BAD_OS
, "no compatible arch found");
323 if (grub_cpu_to_le32 (archs
[i
].offset
)
324 > ~grub_cpu_to_le32 (archs
[i
].size
)
325 || grub_cpu_to_le32 (archs
[i
].offset
)
326 + grub_cpu_to_le32 (archs
[i
].size
)
327 > (grub_size_t
) size
)
329 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
333 boot_image
= (char *) boot_image
+ grub_cpu_to_le32 (archs
[i
].offset
);
334 size
= grub_cpu_to_le32 (archs
[i
].size
);
339 status
= efi_call_6 (b
->load_image
, 0, grub_efi_image_handle
, file_path
,
342 if (status
!= GRUB_EFI_SUCCESS
)
344 if (status
== GRUB_EFI_OUT_OF_RESOURCES
)
345 grub_error (GRUB_ERR_OUT_OF_MEMORY
, "out of resources");
347 grub_error (GRUB_ERR_BAD_OS
, "cannot load image");
352 /* LoadImage does not set a device handler when the image is
353 loaded from memory, so it is necessary to set it explicitly here.
355 loaded_image
= grub_efi_get_loaded_image (image_handle
);
358 grub_error (GRUB_ERR_BAD_OS
, "no loaded image available");
361 loaded_image
->device_handle
= dev_handle
;
366 grub_efi_char16_t
*p16
;
368 for (i
= 1, len
= 0; i
< argc
; i
++)
369 len
+= grub_strlen (argv
[i
]) + 1;
371 len
*= sizeof (grub_efi_char16_t
);
372 cmdline
= p16
= grub_malloc (len
);
376 for (i
= 1; i
< argc
; i
++)
388 loaded_image
->load_options
= cmdline
;
389 loaded_image
->load_options_size
= len
;
392 grub_file_close (file
);
393 grub_device_close (dev
);
395 grub_loader_set (grub_chainloader_boot
, grub_chainloader_unload
, 0);
401 grub_device_close (dev
);
404 grub_file_close (file
);
406 grub_free (file_path
);
409 efi_call_2 (b
->free_pages
, address
, pages
);
411 grub_dl_unref (my_mod
);
416 static grub_command_t cmd
;
418 GRUB_MOD_INIT(chainloader
)
420 cmd
= grub_register_command ("chainloader", grub_cmd_chainloader
,
421 0, N_("Load another boot loader."));
425 GRUB_MOD_FINI(chainloader
)
427 grub_unregister_command (cmd
);