blob: 485384b7ee13b535308a82d608b6a1d8412947ec [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
Heinrich Schuchardt9ad37fe2021-10-15 02:33:33 +020016#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI_LOAD_FILE2_INITRD)
17/* GUID used by Linux to identify the LoadFile2 protocol with the initrd */
18const efi_guid_t efi_lf2_initrd_guid = EFI_INITRD_MEDIA_GUID;
19#endif
20
Ilias Apalodimas37c3ca52021-03-17 21:54:59 +020021/**
22 * efi_create_current_boot_var() - Return Boot#### name were #### is replaced by
23 * the value of BootCurrent
24 *
25 * @var_name: variable name
26 * @var_name_size: size of var_name
27 *
28 * Return: Status code
29 */
30static efi_status_t efi_create_current_boot_var(u16 var_name[],
31 size_t var_name_size)
32{
33 efi_uintn_t boot_current_size;
34 efi_status_t ret;
35 u16 boot_current;
36 u16 *pos;
37
38 boot_current_size = sizeof(boot_current);
39 ret = efi_get_variable_int(L"BootCurrent",
40 &efi_global_variable_guid, NULL,
41 &boot_current_size, &boot_current, NULL);
42 if (ret != EFI_SUCCESS)
43 goto out;
44
45 pos = efi_create_indexed_name(var_name, var_name_size, "Boot",
46 boot_current);
47 if (!pos) {
48 ret = EFI_OUT_OF_RESOURCES;
49 goto out;
50 }
51
52out:
53 return ret;
54}
55
56/**
57 * efi_get_dp_from_boot() - Retrieve and return a device path from an EFI
58 * Boot### variable.
59 * A boot option may contain an array of device paths.
60 * We use a VenMedia() with a specific GUID to identify
61 * the usage of the array members. This function is
62 * used to extract a specific device path
63 *
64 * @guid: vendor GUID of the VenMedia() device path node identifying the
65 * device path
66 *
67 * Return: device path or NULL. Caller must free the returned value
68 */
69struct efi_device_path *efi_get_dp_from_boot(const efi_guid_t guid)
70{
71 struct efi_device_path *file_path = NULL;
72 struct efi_device_path *tmp = NULL;
73 struct efi_load_option lo;
74 void *var_value = NULL;
75 efi_uintn_t size;
76 efi_status_t ret;
77 u16 var_name[16];
78
79 ret = efi_create_current_boot_var(var_name, sizeof(var_name));
80 if (ret != EFI_SUCCESS)
81 return NULL;
82
83 var_value = efi_get_var(var_name, &efi_global_variable_guid, &size);
84 if (!var_value)
85 return NULL;
86
87 ret = efi_deserialize_load_option(&lo, var_value, &size);
88 if (ret != EFI_SUCCESS)
89 goto out;
90
Heinrich Schuchardt05345422021-10-15 02:03:55 +020091 tmp = efi_dp_from_lo(&lo, &guid);
Ilias Apalodimas37c3ca52021-03-17 21:54:59 +020092 if (!tmp)
93 goto out;
94
95 /* efi_dp_dup will just return NULL if efi_dp_next is NULL */
96 file_path = efi_dp_dup(efi_dp_next(tmp));
97
98out:
99 efi_free_pool(tmp);
100 free(var_value);
101
102 return file_path;
103}