efi: hii: add HII config routing/access protocols
[oweals/u-boot.git] / lib / efi_loader / efi_bootmgr.c
index 857d88a879ec76b7508f5ca5321f3b74eafca93e..a095df3f540bbf351a18a07f5487c3266717316c 100644 (file)
@@ -1,15 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
- *  EFI utils
+ *  EFI boot manager
  *
  *  Copyright (c) 2017 Rob Clark
- *
- *  SPDX-License-Identifier:     GPL-2.0+
  */
 
 #include <common.h>
 #include <charset.h>
 #include <malloc.h>
 #include <efi_loader.h>
+#include <asm/unaligned.h>
 
 static const struct efi_boot_services *bs;
 static const struct efi_runtime_services *rs;
@@ -31,57 +31,83 @@ static const struct efi_runtime_services *rs;
  */
 
 
+/* Parse serialized data and transform it into efi_load_option structure */
+void efi_deserialize_load_option(struct efi_load_option *lo, u8 *data)
+{
+       lo->attributes = get_unaligned_le32(data);
+       data += sizeof(u32);
+
+       lo->file_path_length = get_unaligned_le16(data);
+       data += sizeof(u16);
+
+       /* FIXME */
+       lo->label = (u16 *)data;
+       data += (u16_strlen(lo->label) + 1) * sizeof(u16);
+
+       /* FIXME */
+       lo->file_path = (struct efi_device_path *)data;
+       data += lo->file_path_length;
+
+       lo->optional_data = data;
+}
+
 /*
- * See section 3.1.3 in the v2.7 UEFI spec for more details on
- * the layout of EFI_LOAD_OPTION.  In short it is:
- *
- *    typedef struct _EFI_LOAD_OPTION {
- *        UINT32 Attributes;
- *        UINT16 FilePathListLength;
- *        // CHAR16 Description[];   <-- variable length, NULL terminated
- *        // EFI_DEVICE_PATH_PROTOCOL FilePathList[];  <-- FilePathListLength bytes
- *        // UINT8 OptionalData[];
- *    } EFI_LOAD_OPTION;
+ * Serialize efi_load_option structure into byte stream for BootXXXX.
+ * Return a size of allocated data.
  */
-struct load_option {
-       u32 attributes;
-       u16 file_path_length;
-       u16 *label;
-       struct efi_device_path *file_path;
-       u8 *optional_data;
-};
-
-/* parse an EFI_LOAD_OPTION, as described above */
-static void parse_load_option(struct load_option *lo, void *ptr)
+unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
 {
-       lo->attributes = *(u32 *)ptr;
-       ptr += sizeof(u32);
+       unsigned long label_len, option_len;
+       unsigned long size;
+       u8 *p;
+
+       label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
+       option_len = strlen((char *)lo->optional_data);
+
+       /* total size */
+       size = sizeof(lo->attributes);
+       size += sizeof(lo->file_path_length);
+       size += label_len;
+       size += lo->file_path_length;
+       size += option_len + 1;
+       p = malloc(size);
+       if (!p)
+               return 0;
 
-       lo->file_path_length = *(u16 *)ptr;
-       ptr += sizeof(u16);
+       /* copy data */
+       *data = p;
+       memcpy(p, &lo->attributes, sizeof(lo->attributes));
+       p += sizeof(lo->attributes);
 
-       lo->label = ptr;
-       ptr += (utf16_strlen(lo->label) + 1) * 2;
+       memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
+       p += sizeof(lo->file_path_length);
 
-       lo->file_path = ptr;
-       ptr += lo->file_path_length;
+       memcpy(p, lo->label, label_len);
+       p += label_len;
 
-       lo->optional_data = ptr;
+       memcpy(p, lo->file_path, lo->file_path_length);
+       p += lo->file_path_length;
+
+       memcpy(p, lo->optional_data, option_len);
+       p += option_len;
+       *(char *)p = '\0';
+
+       return size;
 }
 
 /* free() the result */
 static void *get_var(u16 *name, const efi_guid_t *vendor,
-                    unsigned long *size)
+                    efi_uintn_t *size)
 {
        efi_guid_t *v = (efi_guid_t *)vendor;
        efi_status_t ret;
        void *buf = NULL;
 
        *size = 0;
-       EFI_CALL(ret = rs->get_variable((s16 *)name, v, NULL, size, buf));
+       EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
        if (ret == EFI_BUFFER_TOO_SMALL) {
                buf = malloc(*size);
-               EFI_CALL(ret = rs->get_variable((s16 *)name, v, NULL, size, buf));
+               EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
        }
 
        if (ret != EFI_SUCCESS) {
@@ -101,11 +127,11 @@ static void *get_var(u16 *name, const efi_guid_t *vendor,
 static void *try_load_entry(uint16_t n, struct efi_device_path **device_path,
                            struct efi_device_path **file_path)
 {
-       struct load_option lo;
+       struct efi_load_option lo;
        u16 varname[] = L"Boot0000";
        u16 hexmap[] = L"0123456789ABCDEF";
        void *load_option, *image = NULL;
-       unsigned long size;
+       efi_uintn_t size;
 
        varname[4] = hexmap[(n & 0xf000) >> 12];
        varname[5] = hexmap[(n & 0x0f00) >> 8];
@@ -116,15 +142,13 @@ static void *try_load_entry(uint16_t n, struct efi_device_path **device_path,
        if (!load_option)
                return NULL;
 
-       parse_load_option(&lo, load_option);
+       efi_deserialize_load_option(&lo, load_option);
 
        if (lo.attributes & LOAD_OPTION_ACTIVE) {
                efi_status_t ret;
-               u16 *str = NULL;
 
-               debug("%s: trying to load \"%ls\" from: %ls\n", __func__,
-                     lo.label, (str = efi_dp_str(lo.file_path)));
-               efi_free_pool(str);
+               debug("%s: trying to load \"%ls\" from %pD\n",
+                     __func__, lo.label, lo.file_path);
 
                ret = efi_load_image_from_path(lo.file_path, &image);
 
@@ -150,7 +174,7 @@ void *efi_bootmgr_load(struct efi_device_path **device_path,
                       struct efi_device_path **file_path)
 {
        uint16_t *bootorder;
-       unsigned long size;
+       efi_uintn_t size;
        void *image = NULL;
        int i, num;