blob: 62e071bd834111af1953dbd300252b23ff6d46e2 [file] [log] [blame]
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * File interface for UEFI variables
4 *
5 * Copyright (c) 2020, Heinrich Schuchardt
6 */
7
8#define LOG_CATEGORY LOGC_EFI
9
10#include <common.h>
11#include <charset.h>
12#include <fs.h>
13#include <log.h>
14#include <malloc.h>
15#include <mapmem.h>
16#include <efi_loader.h>
17#include <efi_variable.h>
18#include <u-boot/crc.h>
19
20#define PART_STR_LEN 10
21
Heinrich Schuchardtdfa22782021-10-06 14:10:14 +020022/* GUID used by Shim to store the MOK database */
23#define SHIM_LOCK_GUID \
24 EFI_GUID(0x605dab50, 0xe046, 0x4300, \
25 0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23)
26
27static const efi_guid_t shim_lock_guid = SHIM_LOCK_GUID;
28
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +000029/**
30 * efi_set_blk_dev_to_system_partition() - select EFI system partition
31 *
32 * Set the EFI system partition as current block device.
33 *
34 * Return: status code
35 */
36static efi_status_t __maybe_unused efi_set_blk_dev_to_system_partition(void)
37{
38 char part_str[PART_STR_LEN];
39 int r;
40
Simon Glassfada3f92022-09-17 09:00:09 -060041 if (efi_system_partition.uclass_id == UCLASS_INVALID) {
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +000042 log_err("No EFI system partition\n");
43 return EFI_DEVICE_ERROR;
44 }
Heinrich Schuchardt54f180d2021-05-25 18:00:13 +020045 snprintf(part_str, PART_STR_LEN, "%x:%x",
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +000046 efi_system_partition.devnum, efi_system_partition.part);
Simon Glassfada3f92022-09-17 09:00:09 -060047 r = fs_set_blk_dev(blk_get_uclass_name(efi_system_partition.uclass_id),
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +000048 part_str, FS_TYPE_ANY);
49 if (r) {
50 log_err("Cannot read EFI system partition\n");
51 return EFI_DEVICE_ERROR;
52 }
53 return EFI_SUCCESS;
54}
55
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +030056efi_status_t __maybe_unused efi_var_collect(struct efi_var_file **bufp, loff_t *lenp,
57 u32 check_attr_mask)
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +000058{
59 size_t len = EFI_VAR_BUF_SIZE;
60 struct efi_var_file *buf;
61 struct efi_var_entry *var, *old_var;
62 size_t old_var_name_length = 2;
63
64 *bufp = NULL; /* Avoid double free() */
65 buf = calloc(1, len);
66 if (!buf)
67 return EFI_OUT_OF_RESOURCES;
68 var = buf->var;
69 old_var = var;
70 for (;;) {
71 efi_uintn_t data_length, var_name_length;
72 u8 *data;
73 efi_status_t ret;
74
75 if ((uintptr_t)buf + len <=
76 (uintptr_t)var->name + old_var_name_length)
77 return EFI_BUFFER_TOO_SMALL;
78
79 var_name_length = (uintptr_t)buf + len - (uintptr_t)var->name;
80 memcpy(var->name, old_var->name, old_var_name_length);
81 guidcpy(&var->guid, &old_var->guid);
82 ret = efi_get_next_variable_name_int(
83 &var_name_length, var->name, &var->guid);
84 if (ret == EFI_NOT_FOUND)
85 break;
86 if (ret != EFI_SUCCESS) {
87 free(buf);
88 return ret;
89 }
90 old_var_name_length = var_name_length;
91 old_var = var;
92
93 data = (u8 *)var->name + old_var_name_length;
94 data_length = (uintptr_t)buf + len - (uintptr_t)data;
95 ret = efi_get_variable_int(var->name, &var->guid,
96 &var->attr, &data_length, data,
97 &var->time);
98 if (ret != EFI_SUCCESS) {
99 free(buf);
100 return ret;
101 }
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300102 if ((var->attr & check_attr_mask) == check_attr_mask) {
103 var->length = data_length;
104 var = (struct efi_var_entry *)ALIGN((uintptr_t)data + data_length, 8);
105 }
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000106 }
107
108 buf->reserved = 0;
109 buf->magic = EFI_VAR_FILE_MAGIC;
110 len = (uintptr_t)var - (uintptr_t)buf;
111 buf->crc32 = crc32(0, (u8 *)buf->var,
112 len - sizeof(struct efi_var_file));
113 buf->length = len;
114 *bufp = buf;
115 *lenp = len;
116
117 return EFI_SUCCESS;
118}
119
120/**
121 * efi_var_to_file() - save non-volatile variables as file
122 *
123 * File ubootefi.var is created on the EFI system partion.
124 *
125 * Return: status code
126 */
127efi_status_t efi_var_to_file(void)
128{
129#ifdef CONFIG_EFI_VARIABLE_FILE_STORE
130 efi_status_t ret;
131 struct efi_var_file *buf;
132 loff_t len;
133 loff_t actlen;
134 int r;
135
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300136 ret = efi_var_collect(&buf, &len, EFI_VARIABLE_NON_VOLATILE);
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000137 if (ret != EFI_SUCCESS)
138 goto error;
139
140 ret = efi_set_blk_dev_to_system_partition();
141 if (ret != EFI_SUCCESS)
142 goto error;
143
144 r = fs_write(EFI_VAR_FILE_NAME, map_to_sysmem(buf), 0, len, &actlen);
145 if (r || len != actlen)
146 ret = EFI_DEVICE_ERROR;
147
148error:
149 if (ret != EFI_SUCCESS)
150 log_err("Failed to persist EFI variables\n");
151 free(buf);
152 return ret;
153#else
154 return EFI_SUCCESS;
155#endif
156}
157
Heinrich Schuchardt211317c2021-08-25 19:13:24 +0200158efi_status_t efi_var_restore(struct efi_var_file *buf, bool safe)
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000159{
160 struct efi_var_entry *var, *last_var;
Heinrich Schuchardt211317c2021-08-25 19:13:24 +0200161 u16 *data;
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000162 efi_status_t ret;
163
164 if (buf->reserved || buf->magic != EFI_VAR_FILE_MAGIC ||
165 buf->crc32 != crc32(0, (u8 *)buf->var,
166 buf->length - sizeof(struct efi_var_file))) {
167 log_err("Invalid EFI variables file\n");
168 return EFI_INVALID_PARAMETER;
169 }
170
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000171 last_var = (struct efi_var_entry *)((u8 *)buf + buf->length);
Heinrich Schuchardt211317c2021-08-25 19:13:24 +0200172 for (var = buf->var; var < last_var;
173 var = (struct efi_var_entry *)
174 ALIGN((uintptr_t)data + var->length, 8)) {
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000175
Heinrich Schuchardt211317c2021-08-25 19:13:24 +0200176 data = var->name + u16_strlen(var->name) + 1;
177
178 /*
Heinrich Schuchardt50413bb2022-12-29 09:23:03 +0100179 * Secure boot related and volatile variables shall only be
Heinrich Schuchardt211317c2021-08-25 19:13:24 +0200180 * restored from U-Boot's preseed.
181 */
182 if (!safe &&
183 (efi_auth_var_get_type(var->name, &var->guid) !=
184 EFI_AUTH_VAR_NONE ||
Heinrich Schuchardtdfa22782021-10-06 14:10:14 +0200185 !guidcmp(&var->guid, &shim_lock_guid) ||
Heinrich Schuchardt211317c2021-08-25 19:13:24 +0200186 !(var->attr & EFI_VARIABLE_NON_VOLATILE)))
187 continue;
188 if (!var->length)
189 continue;
Ilias Apalodimas3a830822022-12-29 10:13:22 +0200190 if (efi_var_mem_find(&var->guid, var->name, NULL))
191 continue;
Heinrich Schuchardt211317c2021-08-25 19:13:24 +0200192 ret = efi_var_mem_ins(var->name, &var->guid, var->attr,
193 var->length, data, 0, NULL,
194 var->time);
195 if (ret != EFI_SUCCESS)
196 log_err("Failed to set EFI variable %ls\n", var->name);
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000197 }
198 return EFI_SUCCESS;
199}
200
201/**
202 * efi_var_from_file() - read variables from file
203 *
204 * File ubootefi.var is read from the EFI system partitions and the variables
205 * stored in the file are created.
206 *
207 * In case the file does not exist yet or a variable cannot be set EFI_SUCCESS
208 * is returned.
209 *
210 * Return: status code
211 */
212efi_status_t efi_var_from_file(void)
213{
214#ifdef CONFIG_EFI_VARIABLE_FILE_STORE
215 struct efi_var_file *buf;
216 loff_t len;
217 efi_status_t ret;
218 int r;
219
220 buf = calloc(1, EFI_VAR_BUF_SIZE);
221 if (!buf) {
222 log_err("Out of memory\n");
223 return EFI_OUT_OF_RESOURCES;
224 }
225
226 ret = efi_set_blk_dev_to_system_partition();
227 if (ret != EFI_SUCCESS)
228 goto error;
229 r = fs_read(EFI_VAR_FILE_NAME, map_to_sysmem(buf), 0, EFI_VAR_BUF_SIZE,
230 &len);
231 if (r || len < sizeof(struct efi_var_file)) {
232 log_err("Failed to load EFI variables\n");
233 goto error;
234 }
Heinrich Schuchardt211317c2021-08-25 19:13:24 +0200235 if (buf->length != len || efi_var_restore(buf, false) != EFI_SUCCESS)
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000236 log_err("Invalid EFI variables file\n");
237error:
238 free(buf);
239#endif
240 return EFI_SUCCESS;
241}