blob: 47bb79920575e8629f92faf0443865184a26a7f8 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clarkad644e72017-09-13 18:05:37 -04002/*
Heinrich Schuchardtf0b0f7f2020-03-19 17:15:18 +00003 * UEFI runtime variable services
Rob Clarkad644e72017-09-13 18:05:37 -04004 *
Heinrich Schuchardtf0b0f7f2020-03-19 17:15:18 +00005 * Copyright (c) 2017 Rob Clark
Rob Clarkad644e72017-09-13 18:05:37 -04006 */
7
Heinrich Schuchardt7dda1632020-07-14 21:25:28 +02008#define LOG_CATEGORY LOGC_EFI
9
Rob Clarkad644e72017-09-13 18:05:37 -040010#include <efi_loader.h>
Heinrich Schuchardtf2d2b3a2020-06-22 18:10:27 +020011#include <efi_variable.h>
Simon Glass09140112020-05-10 11:40:03 -060012#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060013#include <env_internal.h>
Heinrich Schuchardte731af42019-10-26 23:53:48 +020014#include <hexdump.h>
Heinrich Schuchardt7dda1632020-07-14 21:25:28 +020015#include <log.h>
Heinrich Schuchardte731af42019-10-26 23:53:48 +020016#include <malloc.h>
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090017#include <rtc.h>
AKASHI Takahirod99a87f2019-01-21 12:43:13 +010018#include <search.h>
Simon Glassba06b3c2020-05-10 11:39:52 -060019#include <uuid.h>
AKASHI Takahiroe3f5c9c2020-04-21 09:38:17 +090020#include <crypto/pkcs7_parser.h>
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090021#include <linux/compat.h>
Simon Glass3db71102019-11-14 12:57:16 -070022#include <u-boot/crc.h>
Heinrich Schuchardt7dda1632020-07-14 21:25:28 +020023#include <asm/sections.h>
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090024
25#ifdef CONFIG_EFI_SECURE_BOOT
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090026
27/**
28 * efi_variable_authenticate - authenticate a variable
29 * @variable: Variable name in u16
30 * @vendor: Guid of variable
31 * @data_size: Size of @data
32 * @data: Pointer to variable's value
33 * @given_attr: Attributes to be given at SetVariable()
34 * @env_attr: Attributes that an existing variable holds
35 * @time: signed time that an existing variable holds
36 *
37 * Called by efi_set_variable() to verify that the input is correct.
38 * Will replace the given data pointer with another that points to
39 * the actual data to store in the internal memory.
40 * On success, @data and @data_size will be replaced with variable's
41 * actual data, excluding authentication data, and its size, and variable's
42 * attributes and signed time will also be returned in @env_attr and @time,
43 * respectively.
44 *
Heinrich Schuchardt30f92ce2020-05-03 16:29:00 +020045 * Return: status code
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090046 */
Heinrich Schuchardtd47671c2021-09-09 07:12:14 +020047static efi_status_t efi_variable_authenticate(const u16 *variable,
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090048 const efi_guid_t *vendor,
49 efi_uintn_t *data_size,
50 const void **data, u32 given_attr,
51 u32 *env_attr, u64 *time)
52{
53 const struct efi_variable_authentication_2 *auth;
54 struct efi_signature_store *truststore, *truststore2;
55 struct pkcs7_message *var_sig;
56 struct efi_image_regions *regs;
57 struct efi_time timestamp;
58 struct rtc_time tm;
59 u64 new_time;
AKASHI Takahiro0658bb22020-08-12 09:37:50 +090060 u8 *ebuf;
Heinrich Schuchardt99bfab82020-07-15 12:40:35 +020061 enum efi_auth_var_type var_type;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090062 efi_status_t ret;
63
64 var_sig = NULL;
65 truststore = NULL;
66 truststore2 = NULL;
67 regs = NULL;
AKASHI Takahiro0658bb22020-08-12 09:37:50 +090068 ebuf = NULL;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090069 ret = EFI_SECURITY_VIOLATION;
70
71 if (*data_size < sizeof(struct efi_variable_authentication_2))
72 goto err;
73
74 /* authentication data */
75 auth = *data;
76 if (*data_size < (sizeof(auth->time_stamp)
77 + auth->auth_info.hdr.dwLength))
78 goto err;
79
80 if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
81 goto err;
82
Heinrich Schuchardt33f183f2020-07-01 12:44:00 +020083 memcpy(&timestamp, &auth->time_stamp, sizeof(timestamp));
84 if (timestamp.pad1 || timestamp.nanosecond || timestamp.timezone ||
85 timestamp.daylight || timestamp.pad2)
86 goto err;
87
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090088 *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
89 *data_size -= (sizeof(auth->time_stamp)
90 + auth->auth_info.hdr.dwLength);
91
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090092 memset(&tm, 0, sizeof(tm));
93 tm.tm_year = timestamp.year;
94 tm.tm_mon = timestamp.month;
95 tm.tm_mday = timestamp.day;
96 tm.tm_hour = timestamp.hour;
97 tm.tm_min = timestamp.minute;
98 tm.tm_sec = timestamp.second;
99 new_time = rtc_mktime(&tm);
100
101 if (!efi_secure_boot_enabled()) {
102 /* finished checking */
103 *time = new_time;
104 return EFI_SUCCESS;
105 }
106
107 if (new_time <= *time)
108 goto err;
109
110 /* data to be digested */
111 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
112 if (!regs)
113 goto err;
114 regs->max = 5;
115 efi_image_region_add(regs, (uint8_t *)variable,
116 (uint8_t *)variable
117 + u16_strlen(variable) * sizeof(u16), 1);
118 efi_image_region_add(regs, (uint8_t *)vendor,
119 (uint8_t *)vendor + sizeof(*vendor), 1);
120 efi_image_region_add(regs, (uint8_t *)&given_attr,
121 (uint8_t *)&given_attr + sizeof(given_attr), 1);
122 efi_image_region_add(regs, (uint8_t *)&timestamp,
123 (uint8_t *)&timestamp + sizeof(timestamp), 1);
124 efi_image_region_add(regs, (uint8_t *)*data,
125 (uint8_t *)*data + *data_size, 1);
126
127 /* variable's signature list */
128 if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
129 goto err;
AKASHI Takahiro0658bb22020-08-12 09:37:50 +0900130
131 /* ebuf should be kept valid during the authentication */
Sughosh Ganu201b8062020-12-30 19:27:07 +0530132 var_sig = efi_parse_pkcs7_header(auth->auth_info.cert_data,
133 auth->auth_info.hdr.dwLength
134 - sizeof(auth->auth_info),
135 &ebuf);
Patrick Wildt9ad15222020-05-07 02:13:18 +0200136 if (!var_sig) {
AKASHI Takahiroce3c3862020-06-09 14:09:34 +0900137 EFI_PRINT("Parsing variable's signature failed\n");
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900138 goto err;
139 }
140
141 /* signature database used for authentication */
Heinrich Schuchardt99bfab82020-07-15 12:40:35 +0200142 var_type = efi_auth_var_get_type(variable, vendor);
143 switch (var_type) {
144 case EFI_AUTH_VAR_PK:
145 case EFI_AUTH_VAR_KEK:
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900146 /* with PK */
Simon Glass156ccbc2022-01-23 12:55:12 -0700147 truststore = efi_sigstore_parse_sigdb(u"PK");
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900148 if (!truststore)
149 goto err;
Heinrich Schuchardt99bfab82020-07-15 12:40:35 +0200150 break;
151 case EFI_AUTH_VAR_DB:
152 case EFI_AUTH_VAR_DBX:
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900153 /* with PK and KEK */
Simon Glass156ccbc2022-01-23 12:55:12 -0700154 truststore = efi_sigstore_parse_sigdb(u"KEK");
155 truststore2 = efi_sigstore_parse_sigdb(u"PK");
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900156 if (!truststore) {
157 if (!truststore2)
158 goto err;
159
160 truststore = truststore2;
161 truststore2 = NULL;
162 }
Heinrich Schuchardt99bfab82020-07-15 12:40:35 +0200163 break;
164 default:
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900165 /* TODO: support private authenticated variables */
Weizhao Ouyang0ec42792024-04-10 12:19:33 +0000166 ret = EFI_UNSUPPORTED;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900167 goto err;
168 }
169
170 /* verify signature */
AKASHI Takahiro1115edd2020-07-21 19:35:22 +0900171 if (efi_signature_verify(regs, var_sig, truststore, NULL)) {
AKASHI Takahiroce3c3862020-06-09 14:09:34 +0900172 EFI_PRINT("Verified\n");
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900173 } else {
174 if (truststore2 &&
AKASHI Takahiro1115edd2020-07-21 19:35:22 +0900175 efi_signature_verify(regs, var_sig, truststore2, NULL)) {
AKASHI Takahiroce3c3862020-06-09 14:09:34 +0900176 EFI_PRINT("Verified\n");
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900177 } else {
AKASHI Takahiroce3c3862020-06-09 14:09:34 +0900178 EFI_PRINT("Verifying variable's signature failed\n");
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900179 goto err;
180 }
181 }
182
183 /* finished checking */
Heinrich Schuchardt3a92f852020-06-30 12:02:14 +0200184 *time = new_time;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900185 ret = EFI_SUCCESS;
186
187err:
188 efi_sigstore_free(truststore);
189 efi_sigstore_free(truststore2);
190 pkcs7_free_message(var_sig);
AKASHI Takahiro0658bb22020-08-12 09:37:50 +0900191 free(ebuf);
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900192 free(regs);
193
194 return ret;
195}
196#else
Heinrich Schuchardtd47671c2021-09-09 07:12:14 +0200197static efi_status_t efi_variable_authenticate(const u16 *variable,
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900198 const efi_guid_t *vendor,
199 efi_uintn_t *data_size,
200 const void **data, u32 given_attr,
201 u32 *env_attr, u64 *time)
202{
203 return EFI_SUCCESS;
204}
205#endif /* CONFIG_EFI_SECURE_BOOT */
206
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200207efi_status_t __efi_runtime
Heinrich Schuchardtd47671c2021-09-09 07:12:14 +0200208efi_get_variable_int(const u16 *variable_name, const efi_guid_t *vendor,
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200209 u32 *attributes, efi_uintn_t *data_size, void *data,
210 u64 *timep)
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900211{
Ilias Apalodimase01aed42020-07-23 15:49:49 +0300212 return efi_get_variable_mem(variable_name, vendor, attributes, data_size, data, timep);
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100213}
214
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200215efi_status_t __efi_runtime
216efi_get_next_variable_name_int(efi_uintn_t *variable_name_size,
217 u16 *variable_name, efi_guid_t *vendor)
Rob Clarkad644e72017-09-13 18:05:37 -0400218{
Ilias Apalodimase01aed42020-07-23 15:49:49 +0300219 return efi_get_next_variable_name_mem(variable_name_size, variable_name, vendor);
Rob Clarkad644e72017-09-13 18:05:37 -0400220}
221
Ilias Apalodimasc28d32f2024-04-18 15:54:50 +0300222/**
223 * setvariable_allowed() - checks defined by the UEFI spec for setvariable
224 *
225 * @variable_name: name of the variable
226 * @vendor: vendor GUID
227 * @attributes: attributes of the variable
228 * @data_size: size of the buffer with the variable value
229 * @data: buffer with the variable value
230 * Return: status code
231 */
232static efi_status_t __efi_runtime
233setvariable_allowed(const u16 *variable_name, const efi_guid_t *vendor,
234 u32 attributes, efi_uintn_t data_size, const void *data)
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900235{
Masahisa Kojima26a35022023-02-21 11:33:17 +0900236 if (!variable_name || !*variable_name || !vendor)
237 return EFI_INVALID_PARAMETER;
238
239 if (data_size && !data)
240 return EFI_INVALID_PARAMETER;
241
Heinrich Schuchardte0fa2cf2024-04-03 17:33:35 +0200242 /*
243 * EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated.
244 * We don't support EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS.
245 */
246 if (attributes & (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | \
247 EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS))
Masahisa Kojima26a35022023-02-21 11:33:17 +0900248 return EFI_UNSUPPORTED;
249
250 /* Make sure if runtime bit is set, boot service bit is set also */
251 if ((attributes &
252 (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) ==
253 EFI_VARIABLE_RUNTIME_ACCESS)
254 return EFI_INVALID_PARAMETER;
255
256 /* only EFI_VARIABLE_NON_VOLATILE attribute is invalid */
257 if ((attributes & EFI_VARIABLE_MASK) == EFI_VARIABLE_NON_VOLATILE)
258 return EFI_INVALID_PARAMETER;
259
260 /* Make sure HR is set with NV, BS and RT */
261 if (attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD &&
262 (!(attributes & EFI_VARIABLE_NON_VOLATILE) ||
263 !(attributes & EFI_VARIABLE_RUNTIME_ACCESS) ||
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200264 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)))
265 return EFI_INVALID_PARAMETER;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900266
Ilias Apalodimasc28d32f2024-04-18 15:54:50 +0300267 return EFI_SUCCESS;
268}
269
270efi_status_t efi_set_variable_int(const u16 *variable_name,
271 const efi_guid_t *vendor,
272 u32 attributes, efi_uintn_t data_size,
273 const void *data, bool ro_check)
274{
275 struct efi_var_entry *var;
276 efi_uintn_t ret;
277 bool append, delete;
278 u64 time = 0;
279 enum efi_auth_var_type var_type;
280
281 ret = setvariable_allowed(variable_name, vendor, attributes, data_size,
282 data);
283 if (ret != EFI_SUCCESS)
284 return ret;
285
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900286 /* check if a variable exists */
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200287 var = efi_var_mem_find(vendor, variable_name, NULL);
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900288 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
Heinrich Schuchardt19327c12024-04-03 17:33:33 +0200289 attributes &= ~EFI_VARIABLE_APPEND_WRITE;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900290 delete = !append && (!data_size || !attributes);
291
292 /* check attributes */
Heinrich Schuchardt7dda1632020-07-14 21:25:28 +0200293 var_type = efi_auth_var_get_type(variable_name, vendor);
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200294 if (var) {
295 if (ro_check && (var->attr & EFI_VARIABLE_READ_ONLY))
296 return EFI_WRITE_PROTECTED;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900297
Heinrich Schuchardt7dda1632020-07-14 21:25:28 +0200298 if (IS_ENABLED(CONFIG_EFI_VARIABLES_PRESEED)) {
Heinrich Schuchardtb191aa42021-08-26 04:30:24 +0200299 if (var_type >= EFI_AUTH_VAR_PK)
Heinrich Schuchardt7dda1632020-07-14 21:25:28 +0200300 return EFI_WRITE_PROTECTED;
301 }
302
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900303 /* attributes won't be changed */
304 if (!delete &&
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200305 ((ro_check && var->attr != attributes) ||
Heinrich Schuchardt3b51c3a2024-04-03 17:33:34 +0200306 (!ro_check && ((var->attr & ~EFI_VARIABLE_READ_ONLY)
307 != (attributes & ~EFI_VARIABLE_READ_ONLY))))) {
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200308 return EFI_INVALID_PARAMETER;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900309 }
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200310 time = var->time;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900311 } else {
Masahisa Kojima584a2ad2024-04-02 18:09:50 +0900312 /*
313 * UEFI specification does not clearly describe the expected
314 * behavior of append write with data size 0, we follow
315 * the EDK II reference implementation.
316 */
317 if (append && !data_size)
318 return EFI_SUCCESS;
319
320 /*
321 * EFI_VARIABLE_APPEND_WRITE to non-existent variable is accepted
322 * and new variable is created in EDK II reference implementation.
323 * We follow it and only check the deletion here.
324 */
325 if (delete)
326 /* Trying to delete a non-existent variable. */
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200327 return EFI_NOT_FOUND;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900328 }
329
Heinrich Schuchardtb191aa42021-08-26 04:30:24 +0200330 if (var_type >= EFI_AUTH_VAR_PK) {
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900331 /* authentication is mandatory */
332 if (!(attributes &
333 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200334 EFI_PRINT("%ls: TIME_BASED_AUTHENTICATED_WRITE_ACCESS required\n",
AKASHI Takahiroce3c3862020-06-09 14:09:34 +0900335 variable_name);
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200336 return EFI_INVALID_PARAMETER;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900337 }
338 }
339
340 /* authenticate a variable */
341 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900342 if (attributes &
343 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200344 u32 env_attr;
345
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900346 ret = efi_variable_authenticate(variable_name, vendor,
347 &data_size, &data,
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200348 attributes, &env_attr,
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900349 &time);
350 if (ret != EFI_SUCCESS)
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200351 return ret;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900352
353 /* last chance to check for delete */
354 if (!data_size)
355 delete = true;
356 }
357 } else {
358 if (attributes &
Masahisa Kojima26a35022023-02-21 11:33:17 +0900359 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
AKASHI Takahiroce3c3862020-06-09 14:09:34 +0900360 EFI_PRINT("Secure boot is not configured\n");
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200361 return EFI_INVALID_PARAMETER;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900362 }
363 }
364
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900365 if (delete) {
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200366 /* EFI_NOT_FOUND has been handled before */
Heinrich Schuchardtab02c3f2020-11-02 19:32:24 +0100367 attributes = var->attr;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900368 ret = EFI_SUCCESS;
Masahisa Kojima584a2ad2024-04-02 18:09:50 +0900369 } else if (append && var) {
370 /*
371 * data is appended if EFI_VARIABLE_APPEND_WRITE is set and
372 * variable exists.
373 */
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200374 u16 *old_data = var->name;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900375
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200376 for (; *old_data; ++old_data)
377 ;
378 ++old_data;
379 ret = efi_var_mem_ins(variable_name, vendor, attributes,
380 var->length, old_data, data_size, data,
381 time);
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900382 } else {
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200383 ret = efi_var_mem_ins(variable_name, vendor, attributes,
384 data_size, data, 0, NULL, time);
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900385 }
386
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200387 if (ret != EFI_SUCCESS)
388 return ret;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900389
Ilias Apalodimas6b0f1942024-04-03 18:43:53 +0300390 efi_var_mem_del(var);
391
Heinrich Schuchardt99bfab82020-07-15 12:40:35 +0200392 if (var_type == EFI_AUTH_VAR_PK)
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200393 ret = efi_init_secure_state();
394 else
395 ret = EFI_SUCCESS;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900396
Heinrich Schuchardt90c42052023-01-19 14:49:33 +0100397 /*
398 * Write non-volatile EFI variables to file
399 * TODO: check if a value change has occured to avoid superfluous writes
400 */
401 if (attributes & EFI_VARIABLE_NON_VOLATILE)
Heinrich Schuchardt5f7dcf02020-03-19 18:21:58 +0000402 efi_var_to_file();
403
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200404 return EFI_SUCCESS;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900405}
406
Heinrich Schuchardt01df8cf2020-06-26 17:57:48 +0200407efi_status_t efi_query_variable_info_int(u32 attributes,
408 u64 *maximum_variable_storage_size,
409 u64 *remaining_variable_storage_size,
410 u64 *maximum_variable_size)
411{
Masahisa Kojima454a9442023-02-02 22:53:35 +0900412 if (attributes == 0)
413 return EFI_INVALID_PARAMETER;
414
415 /* EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated */
416 if ((attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) ||
417 ((attributes & EFI_VARIABLE_MASK) == 0))
418 return EFI_UNSUPPORTED;
419
420 if ((attributes & EFI_VARIABLE_MASK) == EFI_VARIABLE_NON_VOLATILE)
421 return EFI_INVALID_PARAMETER;
422
423 /* Make sure if runtime bit is set, boot service bit is set also. */
424 if ((attributes &
425 (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) ==
426 EFI_VARIABLE_RUNTIME_ACCESS)
427 return EFI_INVALID_PARAMETER;
428
Heinrich Schuchardt19327c12024-04-03 17:33:33 +0200429 if (attributes & ~EFI_VARIABLE_MASK)
Masahisa Kojima454a9442023-02-02 22:53:35 +0900430 return EFI_INVALID_PARAMETER;
431
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200432 *maximum_variable_storage_size = EFI_VAR_BUF_SIZE -
433 sizeof(struct efi_var_file);
434 *remaining_variable_storage_size = efi_var_mem_free();
435 *maximum_variable_size = EFI_VAR_BUF_SIZE -
436 sizeof(struct efi_var_file) -
437 sizeof(struct efi_var_entry);
438 return EFI_SUCCESS;
Heinrich Schuchardt01df8cf2020-06-26 17:57:48 +0200439}
440
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +0100441/**
Heinrich Schuchardt01df8cf2020-06-26 17:57:48 +0200442 * efi_query_variable_info_runtime() - runtime implementation of
443 * QueryVariableInfo()
Heinrich Schuchardtce435282019-06-20 12:13:05 +0200444 *
445 * @attributes: bitmask to select variables to be
446 * queried
447 * @maximum_variable_storage_size: maximum size of storage area for the
448 * selected variable types
449 * @remaining_variable_storage_size: remaining size of storage are for the
450 * selected variable types
451 * @maximum_variable_size: maximum size of a variable of the
452 * selected type
453 * Returns: status code
454 */
Heinrich Schuchardt12472572023-02-10 09:06:17 +0100455static efi_status_t __efi_runtime EFIAPI efi_query_variable_info_runtime(
Heinrich Schuchardtce435282019-06-20 12:13:05 +0200456 u32 attributes,
457 u64 *maximum_variable_storage_size,
458 u64 *remaining_variable_storage_size,
459 u64 *maximum_variable_size)
460{
461 return EFI_UNSUPPORTED;
462}
Heinrich Schuchardt88192092019-06-20 13:52:16 +0200463
464/**
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +0200465 * efi_set_variable_runtime() - runtime implementation of SetVariable()
Heinrich Schuchardte5b44622019-07-14 12:11:16 +0200466 *
467 * @variable_name: name of the variable
468 * @vendor: vendor GUID
469 * @attributes: attributes of the variable
470 * @data_size: size of the buffer with the variable value
471 * @data: buffer with the variable value
472 * Return: status code
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +0200473 */
474static efi_status_t __efi_runtime EFIAPI
475efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
476 u32 attributes, efi_uintn_t data_size,
477 const void *data)
478{
Ilias Apalodimasc28d32f2024-04-18 15:54:50 +0300479 struct efi_var_entry *var;
480 efi_uintn_t ret;
481 bool append, delete;
482 u64 time = 0;
483
484 if (!IS_ENABLED(CONFIG_EFI_RT_VOLATILE_STORE))
485 return EFI_UNSUPPORTED;
486
487 /*
488 * Authenticated variables are not supported. The EFI spec
489 * in ยง32.3.6 requires keys to be stored in non-volatile storage which
490 * is tamper and delete resistant.
491 * The rest of the checks are in setvariable_allowed()
492 */
493 if (attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
494 return EFI_INVALID_PARAMETER;
495
496 ret = setvariable_allowed(variable_name, vendor, attributes, data_size,
497 data);
498 if (ret != EFI_SUCCESS)
499 return ret;
500
501 /* check if a variable exists */
502 var = efi_var_mem_find(vendor, variable_name, NULL);
503 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
504 attributes &= ~EFI_VARIABLE_APPEND_WRITE;
505 delete = !append && (!data_size || !attributes);
506
507 /* BS only variables are hidden deny writing them */
508 if (!delete && !(attributes & EFI_VARIABLE_RUNTIME_ACCESS))
509 return EFI_INVALID_PARAMETER;
510
511 if (var) {
512 if (var->attr & EFI_VARIABLE_READ_ONLY ||
513 !(var->attr & EFI_VARIABLE_NON_VOLATILE))
514 return EFI_WRITE_PROTECTED;
515
516 /* attributes won't be changed */
517 if (!delete && (((var->attr & ~EFI_VARIABLE_READ_ONLY) !=
518 (attributes & ~EFI_VARIABLE_READ_ONLY))))
519 return EFI_INVALID_PARAMETER;
520 time = var->time;
521 } else {
522 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
523 return EFI_INVALID_PARAMETER;
524 if (append && !data_size)
525 return EFI_SUCCESS;
526 if (delete)
527 return EFI_NOT_FOUND;
528 }
529
530 if (delete) {
531 /* EFI_NOT_FOUND has been handled before */
532 attributes = var->attr;
533 ret = EFI_SUCCESS;
534 } else if (append && var) {
535 u16 *old_data = (void *)((uintptr_t)var->name +
536 sizeof(u16) * (u16_strlen(var->name) + 1));
537
538 ret = efi_var_mem_ins(variable_name, vendor, attributes,
539 var->length, old_data, data_size, data,
540 time);
541 } else {
542 ret = efi_var_mem_ins(variable_name, vendor, attributes,
543 data_size, data, 0, NULL, time);
544 }
545
546 if (ret != EFI_SUCCESS)
547 return ret;
548 /* We are always inserting new variables, get rid of the old copy */
549 efi_var_mem_del(var);
550
551 return EFI_SUCCESS;
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +0200552}
553
554/**
555 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
556 */
557void efi_variables_boot_exit_notify(void)
558{
Heinrich Schuchardt5f7dcf02020-03-19 18:21:58 +0000559 /* Switch variable services functions to runtime version */
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +0200560 efi_runtime_services.get_variable = efi_get_variable_runtime;
561 efi_runtime_services.get_next_variable_name =
562 efi_get_next_variable_name_runtime;
563 efi_runtime_services.set_variable = efi_set_variable_runtime;
Heinrich Schuchardt01df8cf2020-06-26 17:57:48 +0200564 efi_runtime_services.query_variable_info =
565 efi_query_variable_info_runtime;
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +0200566 efi_update_table_header_crc32(&efi_runtime_services.hdr);
567}
568
569/**
Heinrich Schuchardt88192092019-06-20 13:52:16 +0200570 * efi_init_variables() - initialize variable services
571 *
572 * Return: status code
573 */
574efi_status_t efi_init_variables(void)
575{
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900576 efi_status_t ret;
577
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200578 ret = efi_var_mem_init();
579 if (ret != EFI_SUCCESS)
580 return ret;
581
Ilias Apalodimas77bb1472022-12-29 10:13:22 +0200582 ret = efi_var_from_file();
583 if (ret != EFI_SUCCESS)
584 return ret;
Heinrich Schuchardt7dda1632020-07-14 21:25:28 +0200585 if (IS_ENABLED(CONFIG_EFI_VARIABLES_PRESEED)) {
586 ret = efi_var_restore((struct efi_var_file *)
Heinrich Schuchardt9ef82e22021-08-25 19:13:24 +0200587 __efi_var_file_begin, true);
Heinrich Schuchardt7dda1632020-07-14 21:25:28 +0200588 if (ret != EFI_SUCCESS)
589 log_err("Invalid EFI variable seed\n");
590 }
591
AKASHI Takahirof68a6d52020-08-13 17:05:29 +0900592
593 return efi_init_secure_state();
Heinrich Schuchardt88192092019-06-20 13:52:16 +0200594}