blob: 9a3f1f3d2ee2e2d2ea3e63ec5529188555c3f0b2 [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 Schuchardte731af42019-10-26 23:53:48 +02008#include <common.h>
Rob Clarkad644e72017-09-13 18:05:37 -04009#include <efi_loader.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060010#include <env_internal.h>
Heinrich Schuchardte731af42019-10-26 23:53:48 +020011#include <hexdump.h>
12#include <malloc.h>
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090013#include <rtc.h>
AKASHI Takahirod99a87f2019-01-21 12:43:13 +010014#include <search.h>
AKASHI Takahiroe3f5c9c2020-04-21 09:38:17 +090015#include <crypto/pkcs7_parser.h>
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090016#include <linux/compat.h>
Simon Glass3db71102019-11-14 12:57:16 -070017#include <u-boot/crc.h>
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090018
AKASHI Takahiro434ffb62020-04-14 11:51:42 +090019enum efi_secure_mode {
20 EFI_MODE_SETUP,
21 EFI_MODE_USER,
22 EFI_MODE_AUDIT,
23 EFI_MODE_DEPLOYED,
24};
25
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +090026const efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID;
27static bool efi_secure_boot;
AKASHI Takahiro434ffb62020-04-14 11:51:42 +090028static int efi_secure_mode;
AKASHI Takahiro83535162020-04-14 11:51:43 +090029static u8 efi_vendor_keys;
Rob Clarkad644e72017-09-13 18:05:37 -040030
31#define READ_ONLY BIT(31)
32
33/*
34 * Mapping between EFI variables and u-boot variables:
35 *
36 * efi_$guid_$varname = {attributes}(type)value
37 *
38 * For example:
39 *
40 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
41 * "{ro,boot,run}(blob)0000000000000000"
42 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
43 * "(blob)00010000"
44 *
45 * The attributes are a comma separated list of these possible
46 * attributes:
47 *
48 * + ro - read-only
49 * + boot - boot-services access
50 * + run - runtime access
51 *
52 * NOTE: with current implementation, no variables are available after
53 * ExitBootServices, and all are persisted (if possible).
54 *
55 * If not specified, the attributes default to "{boot}".
56 *
57 * The required type is one of:
58 *
59 * + utf8 - raw utf8 string
60 * + blob - arbitrary length hex string
61 *
62 * Maybe a utf16 type would be useful to for a string value to be auto
63 * converted to utf16?
64 */
65
Heinrich Schuchardt506dc522018-09-23 04:08:09 +020066#define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
Rob Clarkad644e72017-09-13 18:05:37 -040067
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +010068/**
69 * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
70 * variable name
71 *
72 * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
73 * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
74 * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
75 *
76 * @native: pointer to pointer to U-Boot variable name
77 * @variable_name: UEFI variable name
78 * @vendor: vendor GUID
79 * Return: status code
80 */
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +020081static efi_status_t efi_to_native(char **native, const u16 *variable_name,
Heinrich Schuchardt0bda81b2018-12-30 20:53:51 +010082 const efi_guid_t *vendor)
Rob Clarkad644e72017-09-13 18:05:37 -040083{
84 size_t len;
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +020085 char *pos;
Rob Clarkad644e72017-09-13 18:05:37 -040086
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +020087 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
88 *native = malloc(len);
89 if (!*native)
90 return EFI_OUT_OF_RESOURCES;
Rob Clarkad644e72017-09-13 18:05:37 -040091
Heinrich Schuchardtdcdb64f2018-08-31 21:31:31 +020092 pos = *native;
93 pos += sprintf(pos, "efi_%pUl_", vendor);
94 utf16_utf8_strcpy(&pos, variable_name);
Rob Clarkad644e72017-09-13 18:05:37 -040095
96 return EFI_SUCCESS;
97}
98
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +010099/**
100 * prefix() - skip over prefix
101 *
102 * Skip over a prefix string.
103 *
104 * @str: string with prefix
105 * @prefix: prefix string
106 * Return: string without prefix, or NULL if prefix not found
107 */
Rob Clarkad644e72017-09-13 18:05:37 -0400108static const char *prefix(const char *str, const char *prefix)
109{
110 size_t n = strlen(prefix);
111 if (!strncmp(prefix, str, n))
112 return str + n;
113 return NULL;
114}
115
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +0100116/**
117 * parse_attr() - decode attributes part of variable value
118 *
119 * Convert the string encoded attributes of a UEFI variable to a bit mask.
120 * TODO: Several attributes are not supported.
121 *
122 * @str: value of U-Boot variable
123 * @attrp: pointer to UEFI attributes
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900124 * @timep: pointer to time attribute
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +0100125 * Return: pointer to remainder of U-Boot variable value
126 */
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900127static const char *parse_attr(const char *str, u32 *attrp, u64 *timep)
Rob Clarkad644e72017-09-13 18:05:37 -0400128{
129 u32 attr = 0;
130 char sep = '{';
131
132 if (*str != '{') {
133 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
134 return str;
135 }
136
137 while (*str == sep) {
138 const char *s;
139
140 str++;
141
142 if ((s = prefix(str, "ro"))) {
143 attr |= READ_ONLY;
AKASHI Takahirocee2cbc2019-06-04 15:52:06 +0900144 } else if ((s = prefix(str, "nv"))) {
145 attr |= EFI_VARIABLE_NON_VOLATILE;
Rob Clarkad644e72017-09-13 18:05:37 -0400146 } else if ((s = prefix(str, "boot"))) {
147 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
148 } else if ((s = prefix(str, "run"))) {
149 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900150 } else if ((s = prefix(str, "time="))) {
151 attr |= EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
152 hex2bin((u8 *)timep, s, sizeof(*timep));
153 s += sizeof(*timep) * 2;
154 } else if (*str == '}') {
155 break;
Rob Clarkad644e72017-09-13 18:05:37 -0400156 } else {
157 printf("invalid attribute: %s\n", str);
158 break;
159 }
160
161 str = s;
162 sep = ',';
163 }
164
165 str++;
166
167 *attrp = attr;
168
169 return str;
170}
171
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900172static efi_status_t efi_set_variable_internal(u16 *variable_name,
173 const efi_guid_t *vendor,
174 u32 attributes,
175 efi_uintn_t data_size,
176 const void *data,
177 bool ro_check);
178
179/**
AKASHI Takahirof0ff75f2020-04-21 09:39:20 +0900180 * efi_set_secure_state - modify secure boot state variables
181 * @sec_boot: value of SecureBoot
182 * @setup_mode: value of SetupMode
183 * @audit_mode: value of AuditMode
184 * @deployed_mode: value of DeployedMode
185 *
186 * Modify secure boot stat-related variables as indicated.
187 *
188 * Return: status code
189 */
190static efi_status_t efi_set_secure_state(int sec_boot, int setup_mode,
191 int audit_mode, int deployed_mode)
192{
193 u32 attributes;
194 efi_status_t ret;
195
196 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
197 EFI_VARIABLE_RUNTIME_ACCESS |
198 READ_ONLY;
199 ret = efi_set_variable_internal(L"SecureBoot",
200 &efi_global_variable_guid,
201 attributes,
202 sizeof(sec_boot), &sec_boot,
203 false);
204 if (ret != EFI_SUCCESS)
205 goto err;
206
207 ret = efi_set_variable_internal(L"SetupMode",
208 &efi_global_variable_guid,
209 attributes,
210 sizeof(setup_mode), &setup_mode,
211 false);
212 if (ret != EFI_SUCCESS)
213 goto err;
214
215 ret = efi_set_variable_internal(L"AuditMode",
216 &efi_global_variable_guid,
217 attributes,
218 sizeof(audit_mode), &audit_mode,
219 false);
220 if (ret != EFI_SUCCESS)
221 goto err;
222
223 ret = efi_set_variable_internal(L"DeployedMode",
224 &efi_global_variable_guid,
225 attributes,
226 sizeof(deployed_mode), &deployed_mode,
227 false);
228err:
229 return ret;
230}
231
232/**
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900233 * efi_transfer_secure_state - handle a secure boot state transition
234 * @mode: new state
235 *
236 * Depending on @mode, secure boot related variables are updated.
237 * Those variables are *read-only* for users, efi_set_variable_internal()
238 * is called here.
239 *
240 * Return: EFI_SUCCESS on success, status code (negative) on error
241 */
242static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
243{
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900244 efi_status_t ret;
245
AKASHI Takahirof0ff75f2020-04-21 09:39:20 +0900246 debug("Switching secure state from %d to %d\n", efi_secure_mode, mode);
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900247
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900248 if (mode == EFI_MODE_DEPLOYED) {
AKASHI Takahirof0ff75f2020-04-21 09:39:20 +0900249 ret = efi_set_secure_state(1, 0, 0, 1);
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900250 if (ret != EFI_SUCCESS)
251 goto err;
252
253 efi_secure_boot = true;
254 } else if (mode == EFI_MODE_AUDIT) {
AKASHI Takahirof0ff75f2020-04-21 09:39:20 +0900255 ret = efi_set_variable_internal(
256 L"PK", &efi_global_variable_guid,
257 EFI_VARIABLE_BOOTSERVICE_ACCESS |
258 EFI_VARIABLE_RUNTIME_ACCESS,
259 0, NULL, false);
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900260 if (ret != EFI_SUCCESS)
261 goto err;
AKASHI Takahirof0ff75f2020-04-21 09:39:20 +0900262
263 ret = efi_set_secure_state(0, 1, 1, 0);
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900264 if (ret != EFI_SUCCESS)
265 goto err;
266
267 efi_secure_boot = true;
268 } else if (mode == EFI_MODE_USER) {
AKASHI Takahirof0ff75f2020-04-21 09:39:20 +0900269 ret = efi_set_secure_state(1, 0, 0, 0);
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900270 if (ret != EFI_SUCCESS)
271 goto err;
272
273 efi_secure_boot = true;
274 } else if (mode == EFI_MODE_SETUP) {
AKASHI Takahirof0ff75f2020-04-21 09:39:20 +0900275 ret = efi_set_secure_state(0, 1, 0, 0);
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900276 if (ret != EFI_SUCCESS)
277 goto err;
278 } else {
279 return EFI_INVALID_PARAMETER;
280 }
281
AKASHI Takahiro83535162020-04-14 11:51:43 +0900282 efi_secure_mode = mode;
283
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900284 return EFI_SUCCESS;
285
286err:
287 /* TODO: What action should be taken here? */
288 printf("ERROR: Secure state transition failed\n");
289 return ret;
290}
291
292/**
293 * efi_init_secure_state - initialize secure boot state
294 *
295 * Return: EFI_SUCCESS on success, status code (negative) on error
296 */
297static efi_status_t efi_init_secure_state(void)
298{
AKASHI Takahiro83535162020-04-14 11:51:43 +0900299 enum efi_secure_mode mode;
300 efi_uintn_t size;
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900301 efi_status_t ret;
302
AKASHI Takahiro83535162020-04-14 11:51:43 +0900303 /*
304 * TODO:
305 * Since there is currently no "platform-specific" installation
306 * method of Platform Key, we can't say if VendorKeys is 0 or 1
307 * precisely.
308 */
309
310 size = 0;
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900311 ret = EFI_CALL(efi_get_variable(L"PK", &efi_global_variable_guid,
312 NULL, &size, NULL));
AKASHI Takahiro83535162020-04-14 11:51:43 +0900313 if (ret == EFI_BUFFER_TOO_SMALL) {
314 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT))
315 mode = EFI_MODE_USER;
316 else
317 mode = EFI_MODE_SETUP;
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900318
AKASHI Takahiro83535162020-04-14 11:51:43 +0900319 efi_vendor_keys = 0;
320 } else if (ret == EFI_NOT_FOUND) {
321 mode = EFI_MODE_SETUP;
322 efi_vendor_keys = 1;
323 } else {
324 goto err;
325 }
326
327 ret = efi_transfer_secure_state(mode);
328 if (ret == EFI_SUCCESS)
329 ret = efi_set_variable_internal(L"VendorKeys",
330 &efi_global_variable_guid,
331 EFI_VARIABLE_BOOTSERVICE_ACCESS
332 | EFI_VARIABLE_RUNTIME_ACCESS
333 | READ_ONLY,
334 sizeof(efi_vendor_keys),
335 &efi_vendor_keys,
336 false);
337
338err:
AKASHI Takahiro434ffb62020-04-14 11:51:42 +0900339 return ret;
340}
341
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +0100342/**
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900343 * efi_secure_boot_enabled - return if secure boot is enabled or not
344 *
345 * Return: true if enabled, false if disabled
346 */
347bool efi_secure_boot_enabled(void)
348{
349 return efi_secure_boot;
350}
351
352#ifdef CONFIG_EFI_SECURE_BOOT
353static u8 pkcs7_hdr[] = {
354 /* SEQUENCE */
355 0x30, 0x82, 0x05, 0xc7,
356 /* OID: pkcs7-signedData */
357 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
358 /* Context Structured? */
359 0xa0, 0x82, 0x05, 0xb8,
360};
361
362/**
363 * efi_variable_parse_signature - parse a signature in variable
364 * @buf: Pointer to variable's value
365 * @buflen: Length of @buf
366 *
367 * Parse a signature embedded in variable's value and instantiate
368 * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
369 * pkcs7's signedData, some header needed be prepended for correctly
370 * parsing authentication data, particularly for variable's.
371 *
372 * Return: Pointer to pkcs7_message structure on success, NULL on error
373 */
374static struct pkcs7_message *efi_variable_parse_signature(const void *buf,
375 size_t buflen)
376{
377 u8 *ebuf;
378 size_t ebuflen, len;
379 struct pkcs7_message *msg;
380
381 /*
382 * This is the best assumption to check if the binary is
383 * already in a form of pkcs7's signedData.
384 */
385 if (buflen > sizeof(pkcs7_hdr) &&
386 !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
387 msg = pkcs7_parse_message(buf, buflen);
388 goto out;
389 }
390
391 /*
392 * Otherwise, we should add a dummy prefix sequence for pkcs7
393 * message parser to be able to process.
394 * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
395 * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
396 * TODO:
397 * The header should be composed in a more refined manner.
398 */
399 debug("Makeshift prefix added to authentication data\n");
400 ebuflen = sizeof(pkcs7_hdr) + buflen;
401 if (ebuflen <= 0x7f) {
402 debug("Data is too short\n");
403 return NULL;
404 }
405
406 ebuf = malloc(ebuflen);
407 if (!ebuf) {
408 debug("Out of memory\n");
409 return NULL;
410 }
411
412 memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
413 memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
414 len = ebuflen - 4;
415 ebuf[2] = (len >> 8) & 0xff;
416 ebuf[3] = len & 0xff;
417 len = ebuflen - 0x13;
418 ebuf[0x11] = (len >> 8) & 0xff;
419 ebuf[0x12] = len & 0xff;
420
421 msg = pkcs7_parse_message(ebuf, ebuflen);
422
423 free(ebuf);
424
425out:
426 if (IS_ERR(msg))
427 return NULL;
428
429 return msg;
430}
431
432/**
433 * efi_variable_authenticate - authenticate a variable
434 * @variable: Variable name in u16
435 * @vendor: Guid of variable
436 * @data_size: Size of @data
437 * @data: Pointer to variable's value
438 * @given_attr: Attributes to be given at SetVariable()
439 * @env_attr: Attributes that an existing variable holds
440 * @time: signed time that an existing variable holds
441 *
442 * Called by efi_set_variable() to verify that the input is correct.
443 * Will replace the given data pointer with another that points to
444 * the actual data to store in the internal memory.
445 * On success, @data and @data_size will be replaced with variable's
446 * actual data, excluding authentication data, and its size, and variable's
447 * attributes and signed time will also be returned in @env_attr and @time,
448 * respectively.
449 *
450 * Return: EFI_SUCCESS on success, status code (negative) on error
451 */
452static efi_status_t efi_variable_authenticate(u16 *variable,
453 const efi_guid_t *vendor,
454 efi_uintn_t *data_size,
455 const void **data, u32 given_attr,
456 u32 *env_attr, u64 *time)
457{
458 const struct efi_variable_authentication_2 *auth;
459 struct efi_signature_store *truststore, *truststore2;
460 struct pkcs7_message *var_sig;
461 struct efi_image_regions *regs;
462 struct efi_time timestamp;
463 struct rtc_time tm;
464 u64 new_time;
465 efi_status_t ret;
466
467 var_sig = NULL;
468 truststore = NULL;
469 truststore2 = NULL;
470 regs = NULL;
471 ret = EFI_SECURITY_VIOLATION;
472
473 if (*data_size < sizeof(struct efi_variable_authentication_2))
474 goto err;
475
476 /* authentication data */
477 auth = *data;
478 if (*data_size < (sizeof(auth->time_stamp)
479 + auth->auth_info.hdr.dwLength))
480 goto err;
481
482 if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
483 goto err;
484
485 *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
486 *data_size -= (sizeof(auth->time_stamp)
487 + auth->auth_info.hdr.dwLength);
488
489 memcpy(&timestamp, &auth->time_stamp, sizeof(timestamp));
490 memset(&tm, 0, sizeof(tm));
491 tm.tm_year = timestamp.year;
492 tm.tm_mon = timestamp.month;
493 tm.tm_mday = timestamp.day;
494 tm.tm_hour = timestamp.hour;
495 tm.tm_min = timestamp.minute;
496 tm.tm_sec = timestamp.second;
497 new_time = rtc_mktime(&tm);
498
499 if (!efi_secure_boot_enabled()) {
500 /* finished checking */
501 *time = new_time;
502 return EFI_SUCCESS;
503 }
504
505 if (new_time <= *time)
506 goto err;
507
508 /* data to be digested */
509 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
510 if (!regs)
511 goto err;
512 regs->max = 5;
513 efi_image_region_add(regs, (uint8_t *)variable,
514 (uint8_t *)variable
515 + u16_strlen(variable) * sizeof(u16), 1);
516 efi_image_region_add(regs, (uint8_t *)vendor,
517 (uint8_t *)vendor + sizeof(*vendor), 1);
518 efi_image_region_add(regs, (uint8_t *)&given_attr,
519 (uint8_t *)&given_attr + sizeof(given_attr), 1);
520 efi_image_region_add(regs, (uint8_t *)&timestamp,
521 (uint8_t *)&timestamp + sizeof(timestamp), 1);
522 efi_image_region_add(regs, (uint8_t *)*data,
523 (uint8_t *)*data + *data_size, 1);
524
525 /* variable's signature list */
526 if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
527 goto err;
528 var_sig = efi_variable_parse_signature(auth->auth_info.cert_data,
529 auth->auth_info.hdr.dwLength
530 - sizeof(auth->auth_info));
531 if (IS_ERR(var_sig)) {
532 debug("Parsing variable's signature failed\n");
533 var_sig = NULL;
534 goto err;
535 }
536
537 /* signature database used for authentication */
538 if (u16_strcmp(variable, L"PK") == 0 ||
539 u16_strcmp(variable, L"KEK") == 0) {
540 /* with PK */
541 truststore = efi_sigstore_parse_sigdb(L"PK");
542 if (!truststore)
543 goto err;
544 } else if (u16_strcmp(variable, L"db") == 0 ||
545 u16_strcmp(variable, L"dbx") == 0) {
546 /* with PK and KEK */
547 truststore = efi_sigstore_parse_sigdb(L"KEK");
548 truststore2 = efi_sigstore_parse_sigdb(L"PK");
549
550 if (!truststore) {
551 if (!truststore2)
552 goto err;
553
554 truststore = truststore2;
555 truststore2 = NULL;
556 }
557 } else {
558 /* TODO: support private authenticated variables */
559 goto err;
560 }
561
562 /* verify signature */
563 if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) {
564 debug("Verified\n");
565 } else {
566 if (truststore2 &&
567 efi_signature_verify_with_sigdb(regs, var_sig,
568 truststore2, NULL)) {
569 debug("Verified\n");
570 } else {
571 debug("Verifying variable's signature failed\n");
572 goto err;
573 }
574 }
575
576 /* finished checking */
577 *time = rtc_mktime(&tm);
578 ret = EFI_SUCCESS;
579
580err:
581 efi_sigstore_free(truststore);
582 efi_sigstore_free(truststore2);
583 pkcs7_free_message(var_sig);
584 free(regs);
585
586 return ret;
587}
588#else
589static efi_status_t efi_variable_authenticate(u16 *variable,
590 const efi_guid_t *vendor,
591 efi_uintn_t *data_size,
592 const void **data, u32 given_attr,
593 u32 *env_attr, u64 *time)
594{
595 return EFI_SUCCESS;
596}
597#endif /* CONFIG_EFI_SECURE_BOOT */
598
599static
600efi_status_t EFIAPI efi_get_variable_common(u16 *variable_name,
601 const efi_guid_t *vendor,
602 u32 *attributes,
603 efi_uintn_t *data_size, void *data,
604 bool is_non_volatile)
605{
606 char *native_name;
607 efi_status_t ret;
608 unsigned long in_size;
609 const char *val = NULL, *s;
610 u64 time = 0;
611 u32 attr;
612
613 if (!variable_name || !vendor || !data_size)
614 return EFI_EXIT(EFI_INVALID_PARAMETER);
615
616 ret = efi_to_native(&native_name, variable_name, vendor);
617 if (ret)
618 return ret;
619
620 EFI_PRINT("get '%s'\n", native_name);
621
622 val = env_get(native_name);
623 free(native_name);
624 if (!val)
625 return EFI_NOT_FOUND;
626
627 val = parse_attr(val, &attr, &time);
628
629 in_size = *data_size;
630
631 if ((s = prefix(val, "(blob)"))) {
632 size_t len = strlen(s);
633
634 /* number of hexadecimal digits must be even */
635 if (len & 1)
636 return EFI_DEVICE_ERROR;
637
638 /* two characters per byte: */
639 len /= 2;
640 *data_size = len;
641
642 if (in_size < len) {
643 ret = EFI_BUFFER_TOO_SMALL;
644 goto out;
645 }
646
647 if (!data) {
648 debug("Variable with no data shouldn't exist.\n");
649 return EFI_INVALID_PARAMETER;
650 }
651
652 if (hex2bin(data, s, len))
653 return EFI_DEVICE_ERROR;
654
655 EFI_PRINT("got value: \"%s\"\n", s);
656 } else if ((s = prefix(val, "(utf8)"))) {
657 unsigned len = strlen(s) + 1;
658
659 *data_size = len;
660
661 if (in_size < len) {
662 ret = EFI_BUFFER_TOO_SMALL;
663 goto out;
664 }
665
666 if (!data) {
667 debug("Variable with no data shouldn't exist.\n");
668 return EFI_INVALID_PARAMETER;
669 }
670
671 memcpy(data, s, len);
672 ((char *)data)[len] = '\0';
673
674 EFI_PRINT("got value: \"%s\"\n", (char *)data);
675 } else {
676 EFI_PRINT("invalid value: '%s'\n", val);
677 return EFI_DEVICE_ERROR;
678 }
679
680out:
681 if (attributes)
682 *attributes = attr & EFI_VARIABLE_MASK;
683
684 return ret;
685}
686
687static
688efi_status_t EFIAPI efi_get_volatile_variable(u16 *variable_name,
689 const efi_guid_t *vendor,
690 u32 *attributes,
691 efi_uintn_t *data_size,
692 void *data)
693{
694 return efi_get_variable_common(variable_name, vendor, attributes,
695 data_size, data, false);
696}
697
698efi_status_t EFIAPI efi_get_nonvolatile_variable(u16 *variable_name,
699 const efi_guid_t *vendor,
700 u32 *attributes,
701 efi_uintn_t *data_size,
702 void *data)
703{
704 return efi_get_variable_common(variable_name, vendor, attributes,
705 data_size, data, true);
706}
707
708/**
709 * efi_efi_get_variable() - retrieve value of a UEFI variable
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +0100710 *
711 * This function implements the GetVariable runtime service.
712 *
713 * See the Unified Extensible Firmware Interface (UEFI) specification for
714 * details.
715 *
716 * @variable_name: name of the variable
717 * @vendor: vendor GUID
718 * @attributes: attributes of the variable
719 * @data_size: size of the buffer to which the variable value is copied
720 * @data: buffer to which the variable value is copied
721 * Return: status code
722 */
Heinrich Schuchardt0bda81b2018-12-30 20:53:51 +0100723efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
724 const efi_guid_t *vendor, u32 *attributes,
725 efi_uintn_t *data_size, void *data)
Rob Clarkad644e72017-09-13 18:05:37 -0400726{
Rob Clarkad644e72017-09-13 18:05:37 -0400727 efi_status_t ret;
Rob Clarkad644e72017-09-13 18:05:37 -0400728
Rob Clark778e6af2017-09-13 18:05:41 -0400729 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
Rob Clarkad644e72017-09-13 18:05:37 -0400730 data_size, data);
731
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900732 ret = efi_get_volatile_variable(variable_name, vendor, attributes,
733 data_size, data);
734 if (ret == EFI_NOT_FOUND)
735 ret = efi_get_nonvolatile_variable(variable_name, vendor,
736 attributes, data_size, data);
Rob Clarkad644e72017-09-13 18:05:37 -0400737
Heinrich Schuchardt487d8c72019-05-15 19:32:43 +0200738 return EFI_EXIT(ret);
Rob Clarkad644e72017-09-13 18:05:37 -0400739}
740
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100741static char *efi_variables_list;
742static char *efi_cur_variable;
743
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +0100744/**
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100745 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
746 * information
747 * @variable: whole data of u-boot variable (ie. name=value)
748 * @variable_name_size: size of variable_name buffer in byte
749 * @variable_name: name of uefi variable in u16, null-terminated
750 * @vendor: vendor's guid
751 * @attributes: attributes
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +0100752 *
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100753 * A uefi variable is encoded into a u-boot variable as described above.
754 * This function parses such a u-boot variable and retrieve uefi-related
755 * information into respective parameters. In return, variable_name_size
756 * is the size of variable name including NULL.
757 *
758 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
Heinrich Schuchardte5b44622019-07-14 12:11:16 +0200759 * the entire variable list has been returned,
760 * otherwise non-zero status code
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100761 */
762static efi_status_t parse_uboot_variable(char *variable,
763 efi_uintn_t *variable_name_size,
764 u16 *variable_name,
765 const efi_guid_t *vendor,
766 u32 *attributes)
767{
768 char *guid, *name, *end, c;
Heinrich Schuchardt4d7f5af2020-03-20 19:04:34 +0100769 size_t name_len;
770 efi_uintn_t old_variable_name_size;
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900771 u64 time;
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100772 u16 *p;
773
774 guid = strchr(variable, '_');
775 if (!guid)
776 return EFI_INVALID_PARAMETER;
777 guid++;
778 name = strchr(guid, '_');
779 if (!name)
780 return EFI_INVALID_PARAMETER;
781 name++;
782 end = strchr(name, '=');
783 if (!end)
784 return EFI_INVALID_PARAMETER;
785
786 name_len = end - name;
Heinrich Schuchardt4d7f5af2020-03-20 19:04:34 +0100787 old_variable_name_size = *variable_name_size;
788 *variable_name_size = sizeof(u16) * (name_len + 1);
789 if (old_variable_name_size < *variable_name_size)
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100790 return EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardt4d7f5af2020-03-20 19:04:34 +0100791
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100792 end++; /* point to value */
793
794 /* variable name */
795 p = variable_name;
796 utf8_utf16_strncpy(&p, name, name_len);
797 variable_name[name_len] = 0;
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100798
799 /* guid */
800 c = *(name - 1);
801 *(name - 1) = '\0'; /* guid need be null-terminated here */
802 uuid_str_to_bin(guid, (unsigned char *)vendor, UUID_STR_FORMAT_GUID);
803 *(name - 1) = c;
804
805 /* attributes */
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900806 parse_attr(end, attributes, &time);
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100807
808 return EFI_SUCCESS;
809}
810
811/**
812 * efi_get_next_variable_name() - enumerate the current variable names
Heinrich Schuchardte5b44622019-07-14 12:11:16 +0200813 *
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100814 * @variable_name_size: size of variable_name buffer in byte
815 * @variable_name: name of uefi variable's name in u16
816 * @vendor: vendor's guid
817 *
818 * This function implements the GetNextVariableName service.
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +0100819 *
820 * See the Unified Extensible Firmware Interface (UEFI) specification for
Heinrich Schuchardte5b44622019-07-14 12:11:16 +0200821 * details.
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +0100822 *
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100823 * Return: status code
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +0100824 */
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200825efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
826 u16 *variable_name,
Heinrich Schuchardt7a4e7172020-03-22 18:28:20 +0100827 efi_guid_t *vendor)
Rob Clarkad644e72017-09-13 18:05:37 -0400828{
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100829 char *native_name, *variable;
830 ssize_t name_len, list_len;
831 char regex[256];
832 char * const regexlist[] = {regex};
833 u32 attributes;
834 int i;
835 efi_status_t ret;
836
Rob Clark778e6af2017-09-13 18:05:41 -0400837 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
Rob Clarkad644e72017-09-13 18:05:37 -0400838
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100839 if (!variable_name_size || !variable_name || !vendor)
Heinrich Schuchardte7dae582019-03-19 18:36:21 +0100840 return EFI_EXIT(EFI_INVALID_PARAMETER);
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100841
842 if (variable_name[0]) {
843 /* check null-terminated string */
844 for (i = 0; i < *variable_name_size; i++)
845 if (!variable_name[i])
846 break;
847 if (i >= *variable_name_size)
848 return EFI_EXIT(EFI_INVALID_PARAMETER);
849
850 /* search for the last-returned variable */
851 ret = efi_to_native(&native_name, variable_name, vendor);
852 if (ret)
853 return EFI_EXIT(ret);
854
855 name_len = strlen(native_name);
856 for (variable = efi_variables_list; variable && *variable;) {
857 if (!strncmp(variable, native_name, name_len) &&
858 variable[name_len] == '=')
859 break;
860
861 variable = strchr(variable, '\n');
862 if (variable)
863 variable++;
864 }
865
866 free(native_name);
867 if (!(variable && *variable))
868 return EFI_EXIT(EFI_INVALID_PARAMETER);
869
870 /* next variable */
871 variable = strchr(variable, '\n');
872 if (variable)
873 variable++;
874 if (!(variable && *variable))
875 return EFI_EXIT(EFI_NOT_FOUND);
876 } else {
877 /*
878 *new search: free a list used in the previous search
879 */
880 free(efi_variables_list);
881 efi_variables_list = NULL;
882 efi_cur_variable = NULL;
883
884 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
885 list_len = hexport_r(&env_htab, '\n',
886 H_MATCH_REGEX | H_MATCH_KEY,
887 &efi_variables_list, 0, 1, regexlist);
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900888
Heinrich Schuchardteefb7902019-01-22 20:10:46 +0100889 if (list_len <= 1)
AKASHI Takahirod99a87f2019-01-21 12:43:13 +0100890 return EFI_EXIT(EFI_NOT_FOUND);
891
892 variable = efi_variables_list;
893 }
894
895 ret = parse_uboot_variable(variable, variable_name_size, variable_name,
896 vendor, &attributes);
897
898 return EFI_EXIT(ret);
Rob Clarkad644e72017-09-13 18:05:37 -0400899}
900
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +0900901static
902efi_status_t EFIAPI efi_set_variable_common(u16 *variable_name,
903 const efi_guid_t *vendor,
904 u32 attributes,
905 efi_uintn_t data_size,
906 const void *data,
907 bool ro_check,
908 bool is_non_volatile)
909{
910 char *native_name = NULL, *old_data = NULL, *val = NULL, *s;
911 efi_uintn_t old_size;
912 bool append, delete;
913 u64 time = 0;
914 u32 attr;
915 efi_status_t ret = EFI_SUCCESS;
916
917 debug("%s: set '%s'\n", __func__, native_name);
918
919 if (!variable_name || !*variable_name || !vendor ||
920 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
921 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
922 ret = EFI_INVALID_PARAMETER;
923 goto err;
924 }
925
926 ret = efi_to_native(&native_name, variable_name, vendor);
927 if (ret)
928 goto err;
929
930 /* check if a variable exists */
931 old_size = 0;
932 attr = 0;
933 ret = EFI_CALL(efi_get_variable(variable_name, vendor, &attr,
934 &old_size, NULL));
935 if (ret == EFI_BUFFER_TOO_SMALL) {
936 if ((is_non_volatile && !(attr & EFI_VARIABLE_NON_VOLATILE)) ||
937 (!is_non_volatile && (attr & EFI_VARIABLE_NON_VOLATILE))) {
938 ret = EFI_INVALID_PARAMETER;
939 goto err;
940 }
941 }
942
943 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
944 attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
945 delete = !append && (!data_size || !attributes);
946
947 /* check attributes */
948 if (old_size) {
949 if (ro_check && (attr & READ_ONLY)) {
950 ret = EFI_WRITE_PROTECTED;
951 goto err;
952 }
953
954 /* attributes won't be changed */
955 if (!delete &&
956 ((ro_check && attr != attributes) ||
957 (!ro_check && ((attr & ~(u32)READ_ONLY)
958 != (attributes & ~(u32)READ_ONLY))))) {
959 ret = EFI_INVALID_PARAMETER;
960 goto err;
961 }
962 } else {
963 if (delete || append) {
964 /*
965 * Trying to delete or to update a non-existent
966 * variable.
967 */
968 ret = EFI_NOT_FOUND;
969 goto err;
970 }
971 }
972
973 if (((!u16_strcmp(variable_name, L"PK") ||
974 !u16_strcmp(variable_name, L"KEK")) &&
975 !guidcmp(vendor, &efi_global_variable_guid)) ||
976 ((!u16_strcmp(variable_name, L"db") ||
977 !u16_strcmp(variable_name, L"dbx")) &&
978 !guidcmp(vendor, &efi_guid_image_security_database))) {
979 /* authentication is mandatory */
980 if (!(attributes &
981 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
982 debug("%ls: AUTHENTICATED_WRITE_ACCESS required\n",
983 variable_name);
984 ret = EFI_INVALID_PARAMETER;
985 goto err;
986 }
987 }
988
989 /* authenticate a variable */
990 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
991 if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
992 ret = EFI_INVALID_PARAMETER;
993 goto err;
994 }
995 if (attributes &
996 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
997 ret = efi_variable_authenticate(variable_name, vendor,
998 &data_size, &data,
999 attributes, &attr,
1000 &time);
1001 if (ret != EFI_SUCCESS)
1002 goto err;
1003
1004 /* last chance to check for delete */
1005 if (!data_size)
1006 delete = true;
1007 }
1008 } else {
1009 if (attributes &
1010 (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
1011 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
1012 debug("Secure boot is not configured\n");
1013 ret = EFI_INVALID_PARAMETER;
1014 goto err;
1015 }
1016 }
1017
1018 /* delete a variable */
1019 if (delete) {
1020 /* !old_size case has been handled before */
1021 val = NULL;
1022 ret = EFI_SUCCESS;
1023 goto out;
1024 }
1025
1026 if (append) {
1027 old_data = malloc(old_size);
1028 if (!old_data) {
1029 return EFI_OUT_OF_RESOURCES;
1030 goto err;
1031 }
1032 ret = EFI_CALL(efi_get_variable(variable_name, vendor,
1033 &attr, &old_size, old_data));
1034 if (ret != EFI_SUCCESS)
1035 goto err;
1036 } else {
1037 old_size = 0;
1038 }
1039
1040 val = malloc(2 * old_size + 2 * data_size
1041 + strlen("{ro,run,boot,nv,time=0123456701234567}(blob)")
1042 + 1);
1043 if (!val) {
1044 ret = EFI_OUT_OF_RESOURCES;
1045 goto err;
1046 }
1047
1048 s = val;
1049
1050 /*
1051 * store attributes
1052 */
1053 attributes &= (READ_ONLY |
1054 EFI_VARIABLE_NON_VOLATILE |
1055 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1056 EFI_VARIABLE_RUNTIME_ACCESS |
1057 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS);
1058 s += sprintf(s, "{");
1059 while (attributes) {
1060 attr = 1 << (ffs(attributes) - 1);
1061
1062 if (attr == READ_ONLY) {
1063 s += sprintf(s, "ro");
1064 } else if (attr == EFI_VARIABLE_NON_VOLATILE) {
1065 s += sprintf(s, "nv");
1066 } else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) {
1067 s += sprintf(s, "boot");
1068 } else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) {
1069 s += sprintf(s, "run");
1070 } else if (attr ==
1071 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
1072 s += sprintf(s, "time=");
1073 s = bin2hex(s, (u8 *)&time, sizeof(time));
1074 }
1075
1076 attributes &= ~attr;
1077 if (attributes)
1078 s += sprintf(s, ",");
1079 }
1080 s += sprintf(s, "}");
1081 s += sprintf(s, "(blob)");
1082
1083 /* store payload: */
1084 if (append)
1085 s = bin2hex(s, old_data, old_size);
1086 s = bin2hex(s, data, data_size);
1087 *s = '\0';
1088
1089 EFI_PRINT("setting: %s=%s\n", native_name, val);
1090
1091out:
AKASHI Takahiro434ffb62020-04-14 11:51:42 +09001092 if (env_set(native_name, val)) {
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +09001093 ret = EFI_DEVICE_ERROR;
AKASHI Takahiro434ffb62020-04-14 11:51:42 +09001094 } else {
AKASHI Takahiro83535162020-04-14 11:51:43 +09001095 bool vendor_keys_modified = false;
1096
AKASHI Takahiro434ffb62020-04-14 11:51:42 +09001097 if ((u16_strcmp(variable_name, L"PK") == 0 &&
1098 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1099 ret = efi_transfer_secure_state(
1100 (delete ? EFI_MODE_SETUP :
1101 EFI_MODE_USER));
1102 if (ret != EFI_SUCCESS)
1103 goto err;
AKASHI Takahiro83535162020-04-14 11:51:43 +09001104
1105 if (efi_secure_mode != EFI_MODE_SETUP)
1106 vendor_keys_modified = true;
1107 } else if ((u16_strcmp(variable_name, L"KEK") == 0 &&
1108 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1109 if (efi_secure_mode != EFI_MODE_SETUP)
1110 vendor_keys_modified = true;
AKASHI Takahiro434ffb62020-04-14 11:51:42 +09001111 }
AKASHI Takahiro83535162020-04-14 11:51:43 +09001112
1113 /* update VendorKeys */
1114 if (vendor_keys_modified & efi_vendor_keys) {
1115 efi_vendor_keys = 0;
1116 ret = efi_set_variable_internal(
1117 L"VendorKeys",
1118 &efi_global_variable_guid,
1119 EFI_VARIABLE_BOOTSERVICE_ACCESS
1120 | EFI_VARIABLE_RUNTIME_ACCESS
1121 | READ_ONLY,
1122 sizeof(efi_vendor_keys),
1123 &efi_vendor_keys,
1124 false);
1125 } else {
1126 ret = EFI_SUCCESS;
1127 }
AKASHI Takahiro434ffb62020-04-14 11:51:42 +09001128 }
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +09001129
1130err:
1131 free(native_name);
1132 free(old_data);
1133 free(val);
1134
1135 return ret;
1136}
1137
1138static
1139efi_status_t EFIAPI efi_set_volatile_variable(u16 *variable_name,
1140 const efi_guid_t *vendor,
1141 u32 attributes,
1142 efi_uintn_t data_size,
1143 const void *data,
1144 bool ro_check)
1145{
1146 return efi_set_variable_common(variable_name, vendor, attributes,
1147 data_size, data, ro_check, false);
1148}
1149
1150efi_status_t EFIAPI efi_set_nonvolatile_variable(u16 *variable_name,
1151 const efi_guid_t *vendor,
1152 u32 attributes,
1153 efi_uintn_t data_size,
1154 const void *data,
1155 bool ro_check)
1156{
1157 efi_status_t ret;
1158
1159 ret = efi_set_variable_common(variable_name, vendor, attributes,
1160 data_size, data, ro_check, true);
1161
1162 return ret;
1163}
1164
1165static efi_status_t efi_set_variable_internal(u16 *variable_name,
1166 const efi_guid_t *vendor,
1167 u32 attributes,
1168 efi_uintn_t data_size,
1169 const void *data,
1170 bool ro_check)
1171{
1172 efi_status_t ret;
1173
1174 if (attributes & EFI_VARIABLE_NON_VOLATILE)
1175 ret = efi_set_nonvolatile_variable(variable_name, vendor,
1176 attributes,
1177 data_size, data, ro_check);
1178 else
1179 ret = efi_set_volatile_variable(variable_name, vendor,
1180 attributes, data_size, data,
1181 ro_check);
1182
1183 return ret;
1184}
1185
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +01001186/**
Heinrich Schuchardt8f89a572019-06-20 12:03:53 +02001187 * efi_set_variable() - set value of a UEFI variable
Heinrich Schuchardt77d4d392019-01-18 19:52:05 +01001188 *
1189 * This function implements the SetVariable runtime service.
1190 *
1191 * See the Unified Extensible Firmware Interface (UEFI) specification for
1192 * details.
1193 *
1194 * @variable_name: name of the variable
1195 * @vendor: vendor GUID
1196 * @attributes: attributes of the variable
1197 * @data_size: size of the buffer with the variable value
1198 * @data: buffer with the variable value
1199 * Return: status code
1200 */
Heinrich Schuchardt0bda81b2018-12-30 20:53:51 +01001201efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
1202 const efi_guid_t *vendor, u32 attributes,
Heinrich Schuchardt452257a2018-12-30 21:03:15 +01001203 efi_uintn_t data_size, const void *data)
Rob Clarkad644e72017-09-13 18:05:37 -04001204{
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +02001205 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
Rob Clarkad644e72017-09-13 18:05:37 -04001206 data_size, data);
1207
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +09001208 /* READ_ONLY bit is not part of API */
1209 attributes &= ~(u32)READ_ONLY;
Rob Clarkad644e72017-09-13 18:05:37 -04001210
AKASHI Takahiro767f6ee2020-04-14 11:51:41 +09001211 return EFI_EXIT(efi_set_variable_internal(variable_name, vendor,
1212 attributes, data_size, data,
1213 true));
Rob Clarkad644e72017-09-13 18:05:37 -04001214}
Heinrich Schuchardtce435282019-06-20 12:13:05 +02001215
1216/**
1217 * efi_query_variable_info() - get information about EFI variables
1218 *
1219 * This function implements the QueryVariableInfo() runtime service.
1220 *
1221 * See the Unified Extensible Firmware Interface (UEFI) specification for
1222 * details.
1223 *
1224 * @attributes: bitmask to select variables to be
1225 * queried
1226 * @maximum_variable_storage_size: maximum size of storage area for the
1227 * selected variable types
1228 * @remaining_variable_storage_size: remaining size of storage are for the
1229 * selected variable types
1230 * @maximum_variable_size: maximum size of a variable of the
1231 * selected type
1232 * Returns: status code
1233 */
1234efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
1235 u32 attributes,
1236 u64 *maximum_variable_storage_size,
1237 u64 *remaining_variable_storage_size,
1238 u64 *maximum_variable_size)
1239{
1240 return EFI_UNSUPPORTED;
1241}
Heinrich Schuchardt88192092019-06-20 13:52:16 +02001242
1243/**
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +02001244 * efi_get_variable_runtime() - runtime implementation of GetVariable()
Heinrich Schuchardte5b44622019-07-14 12:11:16 +02001245 *
1246 * @variable_name: name of the variable
1247 * @vendor: vendor GUID
1248 * @attributes: attributes of the variable
1249 * @data_size: size of the buffer to which the variable value is copied
1250 * @data: buffer to which the variable value is copied
1251 * Return: status code
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +02001252 */
1253static efi_status_t __efi_runtime EFIAPI
1254efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1255 u32 *attributes, efi_uintn_t *data_size, void *data)
1256{
1257 return EFI_UNSUPPORTED;
1258}
1259
1260/**
1261 * efi_get_next_variable_name_runtime() - runtime implementation of
1262 * GetNextVariable()
Heinrich Schuchardte5b44622019-07-14 12:11:16 +02001263 *
1264 * @variable_name_size: size of variable_name buffer in byte
1265 * @variable_name: name of uefi variable's name in u16
1266 * @vendor: vendor's guid
1267 * Return: status code
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +02001268 */
1269static efi_status_t __efi_runtime EFIAPI
1270efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
Heinrich Schuchardt7a4e7172020-03-22 18:28:20 +01001271 u16 *variable_name, efi_guid_t *vendor)
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +02001272{
1273 return EFI_UNSUPPORTED;
1274}
1275
1276/**
1277 * efi_set_variable_runtime() - runtime implementation of SetVariable()
Heinrich Schuchardte5b44622019-07-14 12:11:16 +02001278 *
1279 * @variable_name: name of the variable
1280 * @vendor: vendor GUID
1281 * @attributes: attributes of the variable
1282 * @data_size: size of the buffer with the variable value
1283 * @data: buffer with the variable value
1284 * Return: status code
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +02001285 */
1286static efi_status_t __efi_runtime EFIAPI
1287efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1288 u32 attributes, efi_uintn_t data_size,
1289 const void *data)
1290{
1291 return EFI_UNSUPPORTED;
1292}
1293
1294/**
1295 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
1296 */
1297void efi_variables_boot_exit_notify(void)
1298{
1299 efi_runtime_services.get_variable = efi_get_variable_runtime;
1300 efi_runtime_services.get_next_variable_name =
1301 efi_get_next_variable_name_runtime;
1302 efi_runtime_services.set_variable = efi_set_variable_runtime;
1303 efi_update_table_header_crc32(&efi_runtime_services.hdr);
1304}
1305
1306/**
Heinrich Schuchardt88192092019-06-20 13:52:16 +02001307 * efi_init_variables() - initialize variable services
1308 *
1309 * Return: status code
1310 */
1311efi_status_t efi_init_variables(void)
1312{
AKASHI Takahiro434ffb62020-04-14 11:51:42 +09001313 efi_status_t ret;
1314
1315 ret = efi_init_secure_state();
1316
1317 return ret;
Heinrich Schuchardt88192092019-06-20 13:52:16 +02001318}