blob: fa2b6bc7a8697670f0163026b8eb88d73122cfa8 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clark15f3d742017-09-13 18:05:37 -04002/*
Heinrich Schuchardtc69addd2020-03-19 17:15:18 +00003 * UEFI runtime variable services
Rob Clark15f3d742017-09-13 18:05:37 -04004 *
Heinrich Schuchardtc69addd2020-03-19 17:15:18 +00005 * Copyright (c) 2017 Rob Clark
Rob Clark15f3d742017-09-13 18:05:37 -04006 */
7
Heinrich Schuchardt4b7d5c12020-07-14 21:25:28 +02008#define LOG_CATEGORY LOGC_EFI
9
Heinrich Schuchardt147d5d32019-10-26 23:53:48 +020010#include <common.h>
Rob Clark15f3d742017-09-13 18:05:37 -040011#include <efi_loader.h>
Heinrich Schuchardt9827e842020-06-22 18:10:27 +020012#include <efi_variable.h>
Simon Glassed38aef2020-05-10 11:40:03 -060013#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060014#include <env_internal.h>
Heinrich Schuchardt147d5d32019-10-26 23:53:48 +020015#include <hexdump.h>
Heinrich Schuchardt4b7d5c12020-07-14 21:25:28 +020016#include <log.h>
Heinrich Schuchardt147d5d32019-10-26 23:53:48 +020017#include <malloc.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090018#include <rtc.h>
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +010019#include <search.h>
Simon Glass6f044982020-05-10 11:39:52 -060020#include <uuid.h>
AKASHI Takahiro6ec67672020-04-21 09:38:17 +090021#include <crypto/pkcs7_parser.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090022#include <linux/compat.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070023#include <u-boot/crc.h>
Heinrich Schuchardt4b7d5c12020-07-14 21:25:28 +020024#include <asm/sections.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090025
26#ifdef CONFIG_EFI_SECURE_BOOT
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090027
28/**
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090029 * efi_variable_authenticate - authenticate a variable
30 * @variable: Variable name in u16
31 * @vendor: Guid of variable
32 * @data_size: Size of @data
33 * @data: Pointer to variable's value
34 * @given_attr: Attributes to be given at SetVariable()
35 * @env_attr: Attributes that an existing variable holds
36 * @time: signed time that an existing variable holds
37 *
38 * Called by efi_set_variable() to verify that the input is correct.
39 * Will replace the given data pointer with another that points to
40 * the actual data to store in the internal memory.
41 * On success, @data and @data_size will be replaced with variable's
42 * actual data, excluding authentication data, and its size, and variable's
43 * attributes and signed time will also be returned in @env_attr and @time,
44 * respectively.
45 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +020046 * Return: status code
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090047 */
48static efi_status_t efi_variable_authenticate(u16 *variable,
49 const efi_guid_t *vendor,
50 efi_uintn_t *data_size,
51 const void **data, u32 given_attr,
52 u32 *env_attr, u64 *time)
53{
54 const struct efi_variable_authentication_2 *auth;
55 struct efi_signature_store *truststore, *truststore2;
56 struct pkcs7_message *var_sig;
57 struct efi_image_regions *regs;
58 struct efi_time timestamp;
59 struct rtc_time tm;
60 u64 new_time;
AKASHI Takahiro1bb6edf2020-08-12 09:37:50 +090061 u8 *ebuf;
Heinrich Schuchardt3a280332020-07-15 12:40:35 +020062 enum efi_auth_var_type var_type;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090063 efi_status_t ret;
64
65 var_sig = NULL;
66 truststore = NULL;
67 truststore2 = NULL;
68 regs = NULL;
AKASHI Takahiro1bb6edf2020-08-12 09:37:50 +090069 ebuf = NULL;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090070 ret = EFI_SECURITY_VIOLATION;
71
72 if (*data_size < sizeof(struct efi_variable_authentication_2))
73 goto err;
74
75 /* authentication data */
76 auth = *data;
77 if (*data_size < (sizeof(auth->time_stamp)
78 + auth->auth_info.hdr.dwLength))
79 goto err;
80
81 if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
82 goto err;
83
Heinrich Schuchardt7a76a3f2020-07-01 12:44:00 +020084 memcpy(&timestamp, &auth->time_stamp, sizeof(timestamp));
85 if (timestamp.pad1 || timestamp.nanosecond || timestamp.timezone ||
86 timestamp.daylight || timestamp.pad2)
87 goto err;
88
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090089 *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
90 *data_size -= (sizeof(auth->time_stamp)
91 + auth->auth_info.hdr.dwLength);
92
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090093 memset(&tm, 0, sizeof(tm));
94 tm.tm_year = timestamp.year;
95 tm.tm_mon = timestamp.month;
96 tm.tm_mday = timestamp.day;
97 tm.tm_hour = timestamp.hour;
98 tm.tm_min = timestamp.minute;
99 tm.tm_sec = timestamp.second;
100 new_time = rtc_mktime(&tm);
101
102 if (!efi_secure_boot_enabled()) {
103 /* finished checking */
104 *time = new_time;
105 return EFI_SUCCESS;
106 }
107
108 if (new_time <= *time)
109 goto err;
110
111 /* data to be digested */
112 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
113 if (!regs)
114 goto err;
115 regs->max = 5;
116 efi_image_region_add(regs, (uint8_t *)variable,
117 (uint8_t *)variable
118 + u16_strlen(variable) * sizeof(u16), 1);
119 efi_image_region_add(regs, (uint8_t *)vendor,
120 (uint8_t *)vendor + sizeof(*vendor), 1);
121 efi_image_region_add(regs, (uint8_t *)&given_attr,
122 (uint8_t *)&given_attr + sizeof(given_attr), 1);
123 efi_image_region_add(regs, (uint8_t *)&timestamp,
124 (uint8_t *)&timestamp + sizeof(timestamp), 1);
125 efi_image_region_add(regs, (uint8_t *)*data,
126 (uint8_t *)*data + *data_size, 1);
127
128 /* variable's signature list */
129 if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
130 goto err;
AKASHI Takahiro1bb6edf2020-08-12 09:37:50 +0900131
132 /* ebuf should be kept valid during the authentication */
Sughosh Ganuea350082020-12-30 19:27:07 +0530133 var_sig = efi_parse_pkcs7_header(auth->auth_info.cert_data,
134 auth->auth_info.hdr.dwLength
135 - sizeof(auth->auth_info),
136 &ebuf);
Patrick Wildtcd8a55b2020-05-07 02:13:18 +0200137 if (!var_sig) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900138 EFI_PRINT("Parsing variable's signature failed\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900139 goto err;
140 }
141
142 /* signature database used for authentication */
Heinrich Schuchardt3a280332020-07-15 12:40:35 +0200143 var_type = efi_auth_var_get_type(variable, vendor);
144 switch (var_type) {
145 case EFI_AUTH_VAR_PK:
146 case EFI_AUTH_VAR_KEK:
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900147 /* with PK */
148 truststore = efi_sigstore_parse_sigdb(L"PK");
149 if (!truststore)
150 goto err;
Heinrich Schuchardt3a280332020-07-15 12:40:35 +0200151 break;
152 case EFI_AUTH_VAR_DB:
153 case EFI_AUTH_VAR_DBX:
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900154 /* with PK and KEK */
155 truststore = efi_sigstore_parse_sigdb(L"KEK");
156 truststore2 = efi_sigstore_parse_sigdb(L"PK");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900157 if (!truststore) {
158 if (!truststore2)
159 goto err;
160
161 truststore = truststore2;
162 truststore2 = NULL;
163 }
Heinrich Schuchardt3a280332020-07-15 12:40:35 +0200164 break;
165 default:
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900166 /* TODO: support private authenticated variables */
167 goto err;
168 }
169
170 /* verify signature */
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900171 if (efi_signature_verify(regs, var_sig, truststore, NULL)) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900172 EFI_PRINT("Verified\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900173 } else {
174 if (truststore2 &&
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900175 efi_signature_verify(regs, var_sig, truststore2, NULL)) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900176 EFI_PRINT("Verified\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900177 } else {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900178 EFI_PRINT("Verifying variable's signature failed\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900179 goto err;
180 }
181 }
182
183 /* finished checking */
Heinrich Schuchardt5106dc72020-06-30 12:02:14 +0200184 *time = new_time;
AKASHI Takahirob0f49ee2020-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 Takahiro1bb6edf2020-08-12 09:37:50 +0900191 free(ebuf);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900192 free(regs);
193
194 return ret;
195}
196#else
197static efi_status_t efi_variable_authenticate(u16 *variable,
198 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 Schuchardt047276c2020-07-04 18:34:15 +0200207efi_status_t __efi_runtime
208efi_get_variable_int(u16 *variable_name, const efi_guid_t *vendor,
209 u32 *attributes, efi_uintn_t *data_size, void *data,
210 u64 *timep)
Rob Clark15f3d742017-09-13 18:05:37 -0400211{
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300212 return efi_get_variable_mem(variable_name, vendor, attributes, data_size, data, timep);
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100213}
214
Heinrich Schuchardt047276c2020-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 Clark15f3d742017-09-13 18:05:37 -0400218{
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300219 return efi_get_next_variable_name_mem(variable_name_size, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400220}
221
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200222efi_status_t efi_set_variable_int(u16 *variable_name, const efi_guid_t *vendor,
223 u32 attributes, efi_uintn_t data_size,
224 const void *data, bool ro_check)
Rob Clark15f3d742017-09-13 18:05:37 -0400225{
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200226 struct efi_var_entry *var;
227 efi_uintn_t ret;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900228 bool append, delete;
229 u64 time = 0;
Heinrich Schuchardt3a280332020-07-15 12:40:35 +0200230 enum efi_auth_var_type var_type;
Rob Clark15f3d742017-09-13 18:05:37 -0400231
Heinrich Schuchardtadba3962019-06-12 23:28:42 +0200232 if (!variable_name || !*variable_name || !vendor ||
233 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200234 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)))
235 return EFI_INVALID_PARAMETER;
Rob Clark15f3d742017-09-13 18:05:37 -0400236
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900237 /* check if a variable exists */
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200238 var = efi_var_mem_find(vendor, variable_name, NULL);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900239 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
240 attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
241 delete = !append && (!data_size || !attributes);
242
243 /* check attributes */
Heinrich Schuchardt4b7d5c12020-07-14 21:25:28 +0200244 var_type = efi_auth_var_get_type(variable_name, vendor);
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200245 if (var) {
246 if (ro_check && (var->attr & EFI_VARIABLE_READ_ONLY))
247 return EFI_WRITE_PROTECTED;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900248
Heinrich Schuchardt4b7d5c12020-07-14 21:25:28 +0200249 if (IS_ENABLED(CONFIG_EFI_VARIABLES_PRESEED)) {
Heinrich Schuchardt65b616f2021-08-26 04:30:24 +0200250 if (var_type >= EFI_AUTH_VAR_PK)
Heinrich Schuchardt4b7d5c12020-07-14 21:25:28 +0200251 return EFI_WRITE_PROTECTED;
252 }
253
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900254 /* attributes won't be changed */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900255 if (!delete &&
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200256 ((ro_check && var->attr != attributes) ||
257 (!ro_check && ((var->attr & ~(u32)EFI_VARIABLE_READ_ONLY)
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200258 != (attributes & ~(u32)EFI_VARIABLE_READ_ONLY))))) {
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200259 return EFI_INVALID_PARAMETER;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900260 }
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200261 time = var->time;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900262 } else {
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200263 if (delete || append)
Heinrich Schuchardt8021bbe2019-09-26 21:40:18 +0200264 /*
265 * Trying to delete or to update a non-existent
266 * variable.
267 */
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200268 return EFI_NOT_FOUND;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900269 }
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900270
Heinrich Schuchardt65b616f2021-08-26 04:30:24 +0200271 if (var_type >= EFI_AUTH_VAR_PK) {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900272 /* authentication is mandatory */
273 if (!(attributes &
274 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200275 EFI_PRINT("%ls: TIME_BASED_AUTHENTICATED_WRITE_ACCESS required\n",
AKASHI Takahiro37123732020-06-09 14:09:34 +0900276 variable_name);
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200277 return EFI_INVALID_PARAMETER;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900278 }
279 }
280
281 /* authenticate a variable */
282 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200283 if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
284 return EFI_INVALID_PARAMETER;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900285 if (attributes &
286 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200287 u32 env_attr;
288
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900289 ret = efi_variable_authenticate(variable_name, vendor,
290 &data_size, &data,
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200291 attributes, &env_attr,
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900292 &time);
293 if (ret != EFI_SUCCESS)
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200294 return ret;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900295
296 /* last chance to check for delete */
297 if (!data_size)
298 delete = true;
299 }
300 } else {
301 if (attributes &
302 (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
303 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900304 EFI_PRINT("Secure boot is not configured\n");
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200305 return EFI_INVALID_PARAMETER;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900306 }
307 }
308
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900309 if (delete) {
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200310 /* EFI_NOT_FOUND has been handled before */
Heinrich Schuchardt0f7a6802020-11-02 19:32:24 +0100311 attributes = var->attr;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900312 ret = EFI_SUCCESS;
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200313 } else if (append) {
314 u16 *old_data = var->name;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900315
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200316 for (; *old_data; ++old_data)
317 ;
318 ++old_data;
319 ret = efi_var_mem_ins(variable_name, vendor, attributes,
320 var->length, old_data, data_size, data,
321 time);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900322 } else {
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200323 ret = efi_var_mem_ins(variable_name, vendor, attributes,
324 data_size, data, 0, NULL, time);
Rob Clark15f3d742017-09-13 18:05:37 -0400325 }
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200326 efi_var_mem_del(var);
Rob Clark15f3d742017-09-13 18:05:37 -0400327
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200328 if (ret != EFI_SUCCESS)
329 return ret;
Rob Clark15f3d742017-09-13 18:05:37 -0400330
Heinrich Schuchardt3a280332020-07-15 12:40:35 +0200331 if (var_type == EFI_AUTH_VAR_PK)
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200332 ret = efi_init_secure_state();
333 else
334 ret = EFI_SUCCESS;
Rob Clark15f3d742017-09-13 18:05:37 -0400335
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000336 /* Write non-volatile EFI variables to file */
337 if (attributes & EFI_VARIABLE_NON_VOLATILE &&
338 ret == EFI_SUCCESS && efi_obj_list_initialized == EFI_SUCCESS)
339 efi_var_to_file();
340
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200341 return EFI_SUCCESS;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900342}
343
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200344efi_status_t efi_query_variable_info_int(u32 attributes,
345 u64 *maximum_variable_storage_size,
346 u64 *remaining_variable_storage_size,
347 u64 *maximum_variable_size)
348{
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200349 *maximum_variable_storage_size = EFI_VAR_BUF_SIZE -
350 sizeof(struct efi_var_file);
351 *remaining_variable_storage_size = efi_var_mem_free();
352 *maximum_variable_size = EFI_VAR_BUF_SIZE -
353 sizeof(struct efi_var_file) -
354 sizeof(struct efi_var_entry);
355 return EFI_SUCCESS;
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200356}
357
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900358/**
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200359 * efi_query_variable_info_runtime() - runtime implementation of
360 * QueryVariableInfo()
Heinrich Schuchardt9b19dae2019-06-20 12:13:05 +0200361 *
362 * @attributes: bitmask to select variables to be
363 * queried
364 * @maximum_variable_storage_size: maximum size of storage area for the
365 * selected variable types
366 * @remaining_variable_storage_size: remaining size of storage are for the
367 * selected variable types
368 * @maximum_variable_size: maximum size of a variable of the
369 * selected type
370 * Returns: status code
371 */
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200372efi_status_t __efi_runtime EFIAPI efi_query_variable_info_runtime(
Heinrich Schuchardt9b19dae2019-06-20 12:13:05 +0200373 u32 attributes,
374 u64 *maximum_variable_storage_size,
375 u64 *remaining_variable_storage_size,
376 u64 *maximum_variable_size)
377{
378 return EFI_UNSUPPORTED;
379}
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +0200380
381/**
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +0200382 * efi_set_variable_runtime() - runtime implementation of SetVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200383 *
384 * @variable_name: name of the variable
385 * @vendor: vendor GUID
386 * @attributes: attributes of the variable
387 * @data_size: size of the buffer with the variable value
388 * @data: buffer with the variable value
389 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +0200390 */
391static efi_status_t __efi_runtime EFIAPI
392efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
393 u32 attributes, efi_uintn_t data_size,
394 const void *data)
395{
396 return EFI_UNSUPPORTED;
397}
398
399/**
400 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
401 */
402void efi_variables_boot_exit_notify(void)
403{
Heinrich Schuchardt09a8d502020-03-19 18:21:58 +0000404 /* Switch variable services functions to runtime version */
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +0200405 efi_runtime_services.get_variable = efi_get_variable_runtime;
406 efi_runtime_services.get_next_variable_name =
407 efi_get_next_variable_name_runtime;
408 efi_runtime_services.set_variable = efi_set_variable_runtime;
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200409 efi_runtime_services.query_variable_info =
410 efi_query_variable_info_runtime;
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +0200411 efi_update_table_header_crc32(&efi_runtime_services.hdr);
412}
413
414/**
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +0200415 * efi_init_variables() - initialize variable services
416 *
417 * Return: status code
418 */
419efi_status_t efi_init_variables(void)
420{
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900421 efi_status_t ret;
422
Heinrich Schuchardt047276c2020-07-04 18:34:15 +0200423 ret = efi_var_mem_init();
424 if (ret != EFI_SUCCESS)
425 return ret;
426
Heinrich Schuchardt4b7d5c12020-07-14 21:25:28 +0200427 if (IS_ENABLED(CONFIG_EFI_VARIABLES_PRESEED)) {
428 ret = efi_var_restore((struct efi_var_file *)
Heinrich Schuchardt211317c2021-08-25 19:13:24 +0200429 __efi_var_file_begin, true);
Heinrich Schuchardt4b7d5c12020-07-14 21:25:28 +0200430 if (ret != EFI_SUCCESS)
431 log_err("Invalid EFI variable seed\n");
432 }
433
AKASHI Takahiro2e0d94a2020-08-13 17:05:29 +0900434 ret = efi_var_from_file();
435 if (ret != EFI_SUCCESS)
436 return ret;
437
438 return efi_init_secure_state();
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +0200439}