blob: d03a7364615e6eb0fda13576a57e2b3bb707dbd1 [file] [log] [blame]
Ilias Apalodimas37c3ca52021-03-17 21:54:59 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2020, Linaro Limited
4 */
5
6#define LOG_CATEGORY LOGC_EFI
7#include <common.h>
8#include <env.h>
9#include <malloc.h>
10#include <dm.h>
11#include <fs.h>
12#include <efi_load_initrd.h>
13#include <efi_loader.h>
14#include <efi_variable.h>
15
16/**
17 * efi_create_current_boot_var() - Return Boot#### name were #### is replaced by
18 * the value of BootCurrent
19 *
20 * @var_name: variable name
21 * @var_name_size: size of var_name
22 *
23 * Return: Status code
24 */
25static efi_status_t efi_create_current_boot_var(u16 var_name[],
26 size_t var_name_size)
27{
28 efi_uintn_t boot_current_size;
29 efi_status_t ret;
30 u16 boot_current;
31 u16 *pos;
32
33 boot_current_size = sizeof(boot_current);
34 ret = efi_get_variable_int(L"BootCurrent",
35 &efi_global_variable_guid, NULL,
36 &boot_current_size, &boot_current, NULL);
37 if (ret != EFI_SUCCESS)
38 goto out;
39
40 pos = efi_create_indexed_name(var_name, var_name_size, "Boot",
41 boot_current);
42 if (!pos) {
43 ret = EFI_OUT_OF_RESOURCES;
44 goto out;
45 }
46
47out:
48 return ret;
49}
50
51/**
52 * efi_get_dp_from_boot() - Retrieve and return a device path from an EFI
53 * Boot### variable.
54 * A boot option may contain an array of device paths.
55 * We use a VenMedia() with a specific GUID to identify
56 * the usage of the array members. This function is
57 * used to extract a specific device path
58 *
59 * @guid: vendor GUID of the VenMedia() device path node identifying the
60 * device path
61 *
62 * Return: device path or NULL. Caller must free the returned value
63 */
64struct efi_device_path *efi_get_dp_from_boot(const efi_guid_t guid)
65{
66 struct efi_device_path *file_path = NULL;
67 struct efi_device_path *tmp = NULL;
68 struct efi_load_option lo;
69 void *var_value = NULL;
70 efi_uintn_t size;
71 efi_status_t ret;
72 u16 var_name[16];
73
74 ret = efi_create_current_boot_var(var_name, sizeof(var_name));
75 if (ret != EFI_SUCCESS)
76 return NULL;
77
78 var_value = efi_get_var(var_name, &efi_global_variable_guid, &size);
79 if (!var_value)
80 return NULL;
81
82 ret = efi_deserialize_load_option(&lo, var_value, &size);
83 if (ret != EFI_SUCCESS)
84 goto out;
85
86 tmp = efi_dp_from_lo(&lo, &size, guid);
87 if (!tmp)
88 goto out;
89
90 /* efi_dp_dup will just return NULL if efi_dp_next is NULL */
91 file_path = efi_dp_dup(efi_dp_next(tmp));
92
93out:
94 efi_free_pool(tmp);
95 free(var_value);
96
97 return file_path;
98}