blob: be95ed44e6ef3ef23b5d6c2017fd551f930e5300 [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
Heinrich Schuchardte731af42019-10-26 23:53:48 +020010#include <common.h>
Rob Clarkad644e72017-09-13 18:05:37 -040011#include <efi_loader.h>
Heinrich Schuchardtf2d2b3a2020-06-22 18:10:27 +020012#include <efi_variable.h>
Simon Glass09140112020-05-10 11:40:03 -060013#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060014#include <env_internal.h>
Heinrich Schuchardte731af42019-10-26 23:53:48 +020015#include <hexdump.h>
Heinrich Schuchardt7dda1632020-07-14 21:25:28 +020016#include <log.h>
Heinrich Schuchardte731af42019-10-26 23:53:48 +020017#include <malloc.h>
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090018#include <rtc.h>
AKASHI Takahirod99a87f2019-01-21 12:43:13 +010019#include <search.h>
Simon Glassba06b3c2020-05-10 11:39:52 -060020#include <uuid.h>
AKASHI Takahiroe3f5c9c2020-04-21 09:38:17 +090021#include <crypto/pkcs7_parser.h>
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090022#include <linux/compat.h>
Simon Glass3db71102019-11-14 12:57:16 -070023#include <u-boot/crc.h>
Heinrich Schuchardt7dda1632020-07-14 21:25:28 +020024#include <asm/sections.h>
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090025
26#ifdef CONFIG_EFI_SECURE_BOOT
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090027
28/**
29 * 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 Schuchardt30f92ce2020-05-03 16:29:00 +020046 * Return: status code
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090047 */
Heinrich Schuchardtd47671c2021-09-09 07:12:14 +020048static efi_status_t efi_variable_authenticate(const u16 *variable,
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090049 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 Takahiro0658bb22020-08-12 09:37:50 +090061 u8 *ebuf;
Heinrich Schuchardt99bfab82020-07-15 12:40:35 +020062 enum efi_auth_var_type var_type;
AKASHI Takahiro767f6ee2020-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 Takahiro0658bb22020-08-12 09:37:50 +090069 ebuf = NULL;
AKASHI Takahiro767f6ee2020-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 Schuchardt33f183f2020-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 Takahiro767f6ee2020-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 Takahiro767f6ee2020-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 Takahiro0658bb22020-08-12 09:37:50 +0900131
132 /* ebuf should be kept valid during the authentication */
Sughosh Ganu201b8062020-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 Wildt9ad15222020-05-07 02:13:18 +0200137 if (!var_sig) {
AKASHI Takahiroce3c3862020-06-09 14:09:34 +0900138 EFI_PRINT("Parsing variable's signature failed\n");
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900139 goto err;
140 }
141
142 /* signature database used for authentication */
Heinrich Schuchardt99bfab82020-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 Takahiro767f6ee2020-04-14 11:51:41 +0900147 /* with PK */
Simon Glass156ccbc2022-01-23 12:55:12 -0700148 truststore = efi_sigstore_parse_sigdb(u"PK");
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900149 if (!truststore)
150 goto err;
Heinrich Schuchardt99bfab82020-07-15 12:40:35 +0200151 break;
152 case EFI_AUTH_VAR_DB:
153 case EFI_AUTH_VAR_DBX:
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900154 /* with PK and KEK */
Simon Glass156ccbc2022-01-23 12:55:12 -0700155 truststore = efi_sigstore_parse_sigdb(u"KEK");
156 truststore2 = efi_sigstore_parse_sigdb(u"PK");
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900157 if (!truststore) {
158 if (!truststore2)
159 goto err;
160
161 truststore = truststore2;
162 truststore2 = NULL;
163 }
Heinrich Schuchardt99bfab82020-07-15 12:40:35 +0200164 break;
165 default:
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900166 /* TODO: support private authenticated variables */
167 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
Heinrich Schuchardtd47671c2021-09-09 07:12:14 +0200222efi_status_t efi_set_variable_int(const u16 *variable_name,
223 const efi_guid_t *vendor,
Heinrich Schuchardtf2d2b3a2020-06-22 18:10:27 +0200224 u32 attributes, efi_uintn_t data_size,
225 const void *data, bool ro_check)
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900226{
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200227 struct efi_var_entry *var;
228 efi_uintn_t ret;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900229 bool append, delete;
230 u64 time = 0;
Heinrich Schuchardt99bfab82020-07-15 12:40:35 +0200231 enum efi_auth_var_type var_type;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900232
Masahisa Kojima26a35022023-02-21 11:33:17 +0900233 if (!variable_name || !*variable_name || !vendor)
234 return EFI_INVALID_PARAMETER;
235
236 if (data_size && !data)
237 return EFI_INVALID_PARAMETER;
238
239 /* EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated */
240 if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
241 return EFI_UNSUPPORTED;
242
243 /* Make sure if runtime bit is set, boot service bit is set also */
244 if ((attributes &
245 (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) ==
246 EFI_VARIABLE_RUNTIME_ACCESS)
247 return EFI_INVALID_PARAMETER;
248
249 /* only EFI_VARIABLE_NON_VOLATILE attribute is invalid */
250 if ((attributes & EFI_VARIABLE_MASK) == EFI_VARIABLE_NON_VOLATILE)
251 return EFI_INVALID_PARAMETER;
252
253 /* Make sure HR is set with NV, BS and RT */
254 if (attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD &&
255 (!(attributes & EFI_VARIABLE_NON_VOLATILE) ||
256 !(attributes & EFI_VARIABLE_RUNTIME_ACCESS) ||
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200257 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)))
258 return EFI_INVALID_PARAMETER;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900259
260 /* check if a variable exists */
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200261 var = efi_var_mem_find(vendor, variable_name, NULL);
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900262 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
263 attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
264 delete = !append && (!data_size || !attributes);
265
266 /* check attributes */
Heinrich Schuchardt7dda1632020-07-14 21:25:28 +0200267 var_type = efi_auth_var_get_type(variable_name, vendor);
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200268 if (var) {
269 if (ro_check && (var->attr & EFI_VARIABLE_READ_ONLY))
270 return EFI_WRITE_PROTECTED;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900271
Heinrich Schuchardt7dda1632020-07-14 21:25:28 +0200272 if (IS_ENABLED(CONFIG_EFI_VARIABLES_PRESEED)) {
Heinrich Schuchardtb191aa42021-08-26 04:30:24 +0200273 if (var_type >= EFI_AUTH_VAR_PK)
Heinrich Schuchardt7dda1632020-07-14 21:25:28 +0200274 return EFI_WRITE_PROTECTED;
275 }
276
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900277 /* attributes won't be changed */
278 if (!delete &&
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200279 ((ro_check && var->attr != attributes) ||
280 (!ro_check && ((var->attr & ~(u32)EFI_VARIABLE_READ_ONLY)
Heinrich Schuchardtf2d2b3a2020-06-22 18:10:27 +0200281 != (attributes & ~(u32)EFI_VARIABLE_READ_ONLY))))) {
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200282 return EFI_INVALID_PARAMETER;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900283 }
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200284 time = var->time;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900285 } else {
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200286 if (delete || append)
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900287 /*
288 * Trying to delete or to update a non-existent
289 * variable.
290 */
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200291 return EFI_NOT_FOUND;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900292 }
293
Heinrich Schuchardtb191aa42021-08-26 04:30:24 +0200294 if (var_type >= EFI_AUTH_VAR_PK) {
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900295 /* authentication is mandatory */
296 if (!(attributes &
297 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200298 EFI_PRINT("%ls: TIME_BASED_AUTHENTICATED_WRITE_ACCESS required\n",
AKASHI Takahiroce3c3862020-06-09 14:09:34 +0900299 variable_name);
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200300 return EFI_INVALID_PARAMETER;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900301 }
302 }
303
304 /* authenticate a variable */
305 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900306 if (attributes &
307 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200308 u32 env_attr;
309
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900310 ret = efi_variable_authenticate(variable_name, vendor,
311 &data_size, &data,
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200312 attributes, &env_attr,
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900313 &time);
314 if (ret != EFI_SUCCESS)
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200315 return ret;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900316
317 /* last chance to check for delete */
318 if (!data_size)
319 delete = true;
320 }
321 } else {
322 if (attributes &
Masahisa Kojima26a35022023-02-21 11:33:17 +0900323 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
AKASHI Takahiroce3c3862020-06-09 14:09:34 +0900324 EFI_PRINT("Secure boot is not configured\n");
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200325 return EFI_INVALID_PARAMETER;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900326 }
327 }
328
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900329 if (delete) {
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200330 /* EFI_NOT_FOUND has been handled before */
Heinrich Schuchardtab02c3f2020-11-02 19:32:24 +0100331 attributes = var->attr;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900332 ret = EFI_SUCCESS;
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200333 } else if (append) {
334 u16 *old_data = var->name;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900335
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200336 for (; *old_data; ++old_data)
337 ;
338 ++old_data;
339 ret = efi_var_mem_ins(variable_name, vendor, attributes,
340 var->length, old_data, data_size, data,
341 time);
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900342 } else {
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200343 ret = efi_var_mem_ins(variable_name, vendor, attributes,
344 data_size, data, 0, NULL, time);
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900345 }
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200346 efi_var_mem_del(var);
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900347
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200348 if (ret != EFI_SUCCESS)
349 return ret;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900350
Heinrich Schuchardt99bfab82020-07-15 12:40:35 +0200351 if (var_type == EFI_AUTH_VAR_PK)
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200352 ret = efi_init_secure_state();
353 else
354 ret = EFI_SUCCESS;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900355
Heinrich Schuchardt90c42052023-01-19 14:49:33 +0100356 /*
357 * Write non-volatile EFI variables to file
358 * TODO: check if a value change has occured to avoid superfluous writes
359 */
360 if (attributes & EFI_VARIABLE_NON_VOLATILE)
Heinrich Schuchardt5f7dcf02020-03-19 18:21:58 +0000361 efi_var_to_file();
362
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200363 return EFI_SUCCESS;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900364}
365
Heinrich Schuchardt01df8cf2020-06-26 17:57:48 +0200366efi_status_t efi_query_variable_info_int(u32 attributes,
367 u64 *maximum_variable_storage_size,
368 u64 *remaining_variable_storage_size,
369 u64 *maximum_variable_size)
370{
Masahisa Kojima454a9442023-02-02 22:53:35 +0900371 if (attributes == 0)
372 return EFI_INVALID_PARAMETER;
373
374 /* EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated */
375 if ((attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) ||
376 ((attributes & EFI_VARIABLE_MASK) == 0))
377 return EFI_UNSUPPORTED;
378
379 if ((attributes & EFI_VARIABLE_MASK) == EFI_VARIABLE_NON_VOLATILE)
380 return EFI_INVALID_PARAMETER;
381
382 /* Make sure if runtime bit is set, boot service bit is set also. */
383 if ((attributes &
384 (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) ==
385 EFI_VARIABLE_RUNTIME_ACCESS)
386 return EFI_INVALID_PARAMETER;
387
Masahisa Kojima454a9442023-02-02 22:53:35 +0900388 if (attributes & ~(u32)EFI_VARIABLE_MASK)
389 return EFI_INVALID_PARAMETER;
390
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200391 *maximum_variable_storage_size = EFI_VAR_BUF_SIZE -
392 sizeof(struct efi_var_file);
393 *remaining_variable_storage_size = efi_var_mem_free();
394 *maximum_variable_size = EFI_VAR_BUF_SIZE -
395 sizeof(struct efi_var_file) -
396 sizeof(struct efi_var_entry);
397 return EFI_SUCCESS;
Heinrich Schuchardt01df8cf2020-06-26 17:57:48 +0200398}
399
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +0100400/**
Heinrich Schuchardt01df8cf2020-06-26 17:57:48 +0200401 * efi_query_variable_info_runtime() - runtime implementation of
402 * QueryVariableInfo()
Heinrich Schuchardtce435282019-06-20 12:13:05 +0200403 *
404 * @attributes: bitmask to select variables to be
405 * queried
406 * @maximum_variable_storage_size: maximum size of storage area for the
407 * selected variable types
408 * @remaining_variable_storage_size: remaining size of storage are for the
409 * selected variable types
410 * @maximum_variable_size: maximum size of a variable of the
411 * selected type
412 * Returns: status code
413 */
Heinrich Schuchardt12472572023-02-10 09:06:17 +0100414static efi_status_t __efi_runtime EFIAPI efi_query_variable_info_runtime(
Heinrich Schuchardtce435282019-06-20 12:13:05 +0200415 u32 attributes,
416 u64 *maximum_variable_storage_size,
417 u64 *remaining_variable_storage_size,
418 u64 *maximum_variable_size)
419{
420 return EFI_UNSUPPORTED;
421}
Heinrich Schuchardt88192092019-06-20 13:52:16 +0200422
423/**
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +0200424 * efi_set_variable_runtime() - runtime implementation of SetVariable()
Heinrich Schuchardte5b44622019-07-14 12:11:16 +0200425 *
426 * @variable_name: name of the variable
427 * @vendor: vendor GUID
428 * @attributes: attributes of the variable
429 * @data_size: size of the buffer with the variable value
430 * @data: buffer with the variable value
431 * Return: status code
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +0200432 */
433static efi_status_t __efi_runtime EFIAPI
434efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
435 u32 attributes, efi_uintn_t data_size,
436 const void *data)
437{
438 return EFI_UNSUPPORTED;
439}
440
441/**
442 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
443 */
444void efi_variables_boot_exit_notify(void)
445{
Heinrich Schuchardt5f7dcf02020-03-19 18:21:58 +0000446 /* Switch variable services functions to runtime version */
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +0200447 efi_runtime_services.get_variable = efi_get_variable_runtime;
448 efi_runtime_services.get_next_variable_name =
449 efi_get_next_variable_name_runtime;
450 efi_runtime_services.set_variable = efi_set_variable_runtime;
Heinrich Schuchardt01df8cf2020-06-26 17:57:48 +0200451 efi_runtime_services.query_variable_info =
452 efi_query_variable_info_runtime;
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +0200453 efi_update_table_header_crc32(&efi_runtime_services.hdr);
454}
455
456/**
Heinrich Schuchardt88192092019-06-20 13:52:16 +0200457 * efi_init_variables() - initialize variable services
458 *
459 * Return: status code
460 */
461efi_status_t efi_init_variables(void)
462{
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900463 efi_status_t ret;
464
Heinrich Schuchardtab7296c2020-07-04 18:34:15 +0200465 ret = efi_var_mem_init();
466 if (ret != EFI_SUCCESS)
467 return ret;
468
Ilias Apalodimas77bb1472022-12-29 10:13:22 +0200469 ret = efi_var_from_file();
470 if (ret != EFI_SUCCESS)
471 return ret;
Heinrich Schuchardt7dda1632020-07-14 21:25:28 +0200472 if (IS_ENABLED(CONFIG_EFI_VARIABLES_PRESEED)) {
473 ret = efi_var_restore((struct efi_var_file *)
Heinrich Schuchardt9ef82e22021-08-25 19:13:24 +0200474 __efi_var_file_begin, true);
Heinrich Schuchardt7dda1632020-07-14 21:25:28 +0200475 if (ret != EFI_SUCCESS)
476 log_err("Invalid EFI variable seed\n");
477 }
478
AKASHI Takahirof68a6d52020-08-13 17:05:29 +0900479
480 return efi_init_secure_state();
Heinrich Schuchardt88192092019-06-20 13:52:16 +0200481}