efi_loader: Pass proper device path in on boot
authorAlexander Graf <agraf@suse.de>
Fri, 4 Mar 2016 00:10:14 +0000 (01:10 +0100)
committerTom Rini <trini@konsulko.com>
Wed, 16 Mar 2016 01:30:14 +0000 (21:30 -0400)
EFI payloads can query for the device they were booted from. Because
we have a disconnect between loading binaries and running binaries,
we passed in a dummy device path so far.

Unfortunately that breaks grub2's logic to find its configuration
file from the same device it was booted from.

This patch adds logic to have the "load" command call into our efi
code to set the device path to the one we last loaded a binary from.

With this grub2 properly detects where we got booted from and can
find its configuration file, even when searching by-partition.

Signed-off-by: Alexander Graf <agraf@suse.de>
cmd/bootefi.c
cmd/fs.c
include/efi_loader.h
lib/efi_loader/efi_disk.c

index 2b104d4908489b17382e8e1c2418f348a36095b9..2f0b90a9da83393f374a6932ef138df2f5136a75 100644 (file)
  * In addition to the originating device we also declare the file path
  * of "bootefi" based loads to be /bootefi.
  */
-static struct efi_device_path_file_path bootefi_dummy_path[] = {
+static struct efi_device_path_file_path bootefi_image_path[] = {
        {
                .dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
                .dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
-               .dp.length = sizeof(bootefi_dummy_path[0]),
+               .dp.length = sizeof(bootefi_image_path[0]),
                .str = { 'b','o','o','t','e','f','i' },
        }, {
                .dp.type = DEVICE_PATH_TYPE_END,
                .dp.sub_type = DEVICE_PATH_SUB_TYPE_END,
-               .dp.length = sizeof(bootefi_dummy_path[0]),
+               .dp.length = sizeof(bootefi_image_path[0]),
        }
 };
 
@@ -38,14 +38,14 @@ static efi_status_t bootefi_open_dp(void *handle, efi_guid_t *protocol,
                        void **protocol_interface, void *agent_handle,
                        void *controller_handle, uint32_t attributes)
 {
-       *protocol_interface = bootefi_dummy_path;
+       *protocol_interface = bootefi_image_path;
        return EFI_SUCCESS;
 }
 
 /* The EFI loaded_image interface for the image executed via "bootefi" */
 static struct efi_loaded_image loaded_image_info = {
-       .device_handle = bootefi_dummy_path,
-       .file_path = bootefi_dummy_path,
+       .device_handle = bootefi_image_path,
+       .file_path = bootefi_image_path,
 };
 
 /* The EFI object struct for the image executed via "bootefi" */
@@ -63,7 +63,7 @@ static struct efi_object loaded_image_info_obj = {
                {
                        /*
                         * When asking for the device path interface, return
-                        * bootefi_dummy_path
+                        * bootefi_image_path
                         */
                        .guid = &efi_guid_device_path,
                        .open = &bootefi_open_dp,
@@ -73,11 +73,11 @@ static struct efi_object loaded_image_info_obj = {
 
 /* The EFI object struct for the device the "bootefi" image was loaded from */
 static struct efi_object bootefi_device_obj = {
-       .handle = bootefi_dummy_path,
+       .handle = bootefi_image_path,
        .protocols = {
                {
                        /* When asking for the device path interface, return
-                        * bootefi_dummy_path */
+                        * bootefi_image_path */
                        .guid = &efi_guid_device_path,
                        .open = &bootefi_open_dp,
                }
@@ -188,3 +188,19 @@ U_BOOT_CMD(
        "Boots an EFI payload from memory\n",
        bootefi_help_text
 );
+
+void efi_set_bootdev(const char *dev, const char *devnr)
+{
+       char devname[16] = { 0 }; /* dp->str is u16[16] long */
+       char *colon;
+
+       /* Assemble the condensed device name we use in efi_disk.c */
+       snprintf(devname, sizeof(devname), "%s%s", dev, devnr);
+       colon = strchr(devname, ':');
+       if (colon)
+               *colon = '\0';
+
+       /* Patch the bootefi_image_path to the target device */
+       memset(bootefi_image_path[0].str, 0, sizeof(bootefi_image_path[0].str));
+       ascii2unicode(bootefi_image_path[0].str, devname);
+}
index 8f8f1b2bfcf108988bd4ac46d32b7a7c64380910..be8f2899e93aa052be21a4cd5e154823575b8177 100644 (file)
--- a/cmd/fs.c
+++ b/cmd/fs.c
@@ -9,6 +9,7 @@
 #include <common.h>
 #include <command.h>
 #include <fs.h>
+#include <efi_loader.h>
 
 static int do_size_wrapper(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
@@ -26,6 +27,7 @@ U_BOOT_CMD(
 static int do_load_wrapper(cmd_tbl_t *cmdtp, int flag, int argc,
                                char * const argv[])
 {
+       efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "");
        return do_load(cmdtp, flag, argc, argv, FS_TYPE_ANY);
 }
 
index 11be685f0e1e5ac4cf8cea971ab816d6f9c98732..74bed26662d07ba9a96cf68b4a0044c4a6c60879 100644 (file)
@@ -109,6 +109,8 @@ void efi_restore_gd(void);
 efi_status_t efi_exit_func(efi_status_t ret);
 /* Call this to relocate the runtime section to an address space */
 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map);
+/* Call this to set the current device name */
+void efi_set_bootdev(const char *dev, const char *devnr);
 
 /* Generic EFI memory allocator, call this to get memory */
 void *efi_alloc(uint64_t len, int memory_type);
@@ -129,6 +131,13 @@ uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
 /* Called by board init to initialize the EFI memory map */
 int efi_memory_init(void);
 
+/* Convert strings from normal C strings to uEFI strings */
+static inline void ascii2unicode(u16 *unicode, char *ascii)
+{
+       while (*ascii)
+               *(unicode++) = *(ascii++);
+}
+
 /*
  * Use these to indicate that your code / data should go into the EFI runtime
  * section and thus still be available when the OS is running
@@ -144,5 +153,6 @@ int efi_memory_init(void);
 
 /* No loader configured, stub out EFI_ENTRY */
 static inline void efi_restore_gd(void) { }
+static inline void efi_set_bootdev(const char *dev, const char *devnr) { }
 
 #endif
index 6f543ff5cb08f491c9b1fb625655a03810f9948e..aaff947596f04c13cdc67cd70e6378c41f42c8bf 100644 (file)
@@ -29,12 +29,6 @@ struct efi_disk_obj {
        struct efi_device_path_file_path *dp;
 };
 
-static void ascii2unicode(u16 *unicode, char *ascii)
-{
-       while (*ascii)
-               *(unicode++) = *(ascii++);
-}
-
 static efi_status_t efi_disk_open_block(void *handle, efi_guid_t *protocol,
                        void **protocol_interface, void *agent_handle,
                        void *controller_handle, uint32_t attributes)