blob: fd5c41f830b1e5181b376942a56dd0f0b327af51 [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 Schuchardt147d5d32019-10-26 23:53:48 +02008#include <common.h>
Rob Clark15f3d742017-09-13 18:05:37 -04009#include <efi_loader.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060010#include <env_internal.h>
Heinrich Schuchardt147d5d32019-10-26 23:53:48 +020011#include <hexdump.h>
12#include <malloc.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090013#include <rtc.h>
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +010014#include <search.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090015#include <linux/compat.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070016#include <u-boot/crc.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090017#include "../lib/crypto/pkcs7_parser.h"
18
AKASHI Takahiroc78dc192020-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 Takahirob0f49ee2020-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 Takahiroc78dc192020-04-14 11:51:42 +090028static int efi_secure_mode;
Rob Clark15f3d742017-09-13 18:05:37 -040029
30#define READ_ONLY BIT(31)
31
32/*
33 * Mapping between EFI variables and u-boot variables:
34 *
35 * efi_$guid_$varname = {attributes}(type)value
36 *
37 * For example:
38 *
39 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
40 * "{ro,boot,run}(blob)0000000000000000"
41 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
42 * "(blob)00010000"
43 *
44 * The attributes are a comma separated list of these possible
45 * attributes:
46 *
47 * + ro - read-only
48 * + boot - boot-services access
49 * + run - runtime access
50 *
51 * NOTE: with current implementation, no variables are available after
52 * ExitBootServices, and all are persisted (if possible).
53 *
54 * If not specified, the attributes default to "{boot}".
55 *
56 * The required type is one of:
57 *
58 * + utf8 - raw utf8 string
59 * + blob - arbitrary length hex string
60 *
61 * Maybe a utf16 type would be useful to for a string value to be auto
62 * converted to utf16?
63 */
64
Heinrich Schuchardt44389cb2018-09-23 04:08:09 +020065#define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
Rob Clark15f3d742017-09-13 18:05:37 -040066
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +010067/**
68 * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
69 * variable name
70 *
71 * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
72 * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
73 * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
74 *
75 * @native: pointer to pointer to U-Boot variable name
76 * @variable_name: UEFI variable name
77 * @vendor: vendor GUID
78 * Return: status code
79 */
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020080static efi_status_t efi_to_native(char **native, const u16 *variable_name,
Heinrich Schuchardte06ae192018-12-30 20:53:51 +010081 const efi_guid_t *vendor)
Rob Clark15f3d742017-09-13 18:05:37 -040082{
83 size_t len;
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020084 char *pos;
Rob Clark15f3d742017-09-13 18:05:37 -040085
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020086 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
87 *native = malloc(len);
88 if (!*native)
89 return EFI_OUT_OF_RESOURCES;
Rob Clark15f3d742017-09-13 18:05:37 -040090
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020091 pos = *native;
92 pos += sprintf(pos, "efi_%pUl_", vendor);
93 utf16_utf8_strcpy(&pos, variable_name);
Rob Clark15f3d742017-09-13 18:05:37 -040094
95 return EFI_SUCCESS;
96}
97
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +010098/**
99 * prefix() - skip over prefix
100 *
101 * Skip over a prefix string.
102 *
103 * @str: string with prefix
104 * @prefix: prefix string
105 * Return: string without prefix, or NULL if prefix not found
106 */
Rob Clark15f3d742017-09-13 18:05:37 -0400107static const char *prefix(const char *str, const char *prefix)
108{
109 size_t n = strlen(prefix);
110 if (!strncmp(prefix, str, n))
111 return str + n;
112 return NULL;
113}
114
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100115/**
116 * parse_attr() - decode attributes part of variable value
117 *
118 * Convert the string encoded attributes of a UEFI variable to a bit mask.
119 * TODO: Several attributes are not supported.
120 *
121 * @str: value of U-Boot variable
122 * @attrp: pointer to UEFI attributes
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900123 * @timep: pointer to time attribute
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100124 * Return: pointer to remainder of U-Boot variable value
125 */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900126static const char *parse_attr(const char *str, u32 *attrp, u64 *timep)
Rob Clark15f3d742017-09-13 18:05:37 -0400127{
128 u32 attr = 0;
129 char sep = '{';
130
131 if (*str != '{') {
132 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
133 return str;
134 }
135
136 while (*str == sep) {
137 const char *s;
138
139 str++;
140
141 if ((s = prefix(str, "ro"))) {
142 attr |= READ_ONLY;
AKASHI Takahiro7f334332019-06-04 15:52:06 +0900143 } else if ((s = prefix(str, "nv"))) {
144 attr |= EFI_VARIABLE_NON_VOLATILE;
Rob Clark15f3d742017-09-13 18:05:37 -0400145 } else if ((s = prefix(str, "boot"))) {
146 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
147 } else if ((s = prefix(str, "run"))) {
148 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900149 } else if ((s = prefix(str, "time="))) {
150 attr |= EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
151 hex2bin((u8 *)timep, s, sizeof(*timep));
152 s += sizeof(*timep) * 2;
153 } else if (*str == '}') {
154 break;
Rob Clark15f3d742017-09-13 18:05:37 -0400155 } else {
156 printf("invalid attribute: %s\n", str);
157 break;
158 }
159
160 str = s;
161 sep = ',';
162 }
163
164 str++;
165
166 *attrp = attr;
167
168 return str;
169}
170
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900171static efi_status_t efi_set_variable_internal(u16 *variable_name,
172 const efi_guid_t *vendor,
173 u32 attributes,
174 efi_uintn_t data_size,
175 const void *data,
176 bool ro_check);
177
178/**
179 * efi_transfer_secure_state - handle a secure boot state transition
180 * @mode: new state
181 *
182 * Depending on @mode, secure boot related variables are updated.
183 * Those variables are *read-only* for users, efi_set_variable_internal()
184 * is called here.
185 *
186 * Return: EFI_SUCCESS on success, status code (negative) on error
187 */
188static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
189{
190 u32 attributes;
191 u8 val;
192 efi_status_t ret;
193
194 debug("Secure state from %d to %d\n", efi_secure_mode, mode);
195
196 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
197 EFI_VARIABLE_RUNTIME_ACCESS;
198 if (mode == EFI_MODE_DEPLOYED) {
199 val = 1;
200 ret = efi_set_variable_internal(L"SecureBoot",
201 &efi_global_variable_guid,
202 attributes | READ_ONLY,
203 sizeof(val), &val,
204 false);
205 if (ret != EFI_SUCCESS)
206 goto err;
207 val = 0;
208 ret = efi_set_variable_internal(L"SetupMode",
209 &efi_global_variable_guid,
210 attributes | READ_ONLY,
211 sizeof(val), &val,
212 false);
213 if (ret != EFI_SUCCESS)
214 goto err;
215 val = 0;
216 ret = efi_set_variable_internal(L"AuditMode",
217 &efi_global_variable_guid,
218 attributes | READ_ONLY,
219 sizeof(val), &val,
220 false);
221 if (ret != EFI_SUCCESS)
222 goto err;
223 val = 1;
224 ret = efi_set_variable_internal(L"DeployedMode",
225 &efi_global_variable_guid,
226 attributes | READ_ONLY,
227 sizeof(val), &val,
228 false);
229 if (ret != EFI_SUCCESS)
230 goto err;
231
232 efi_secure_boot = true;
233 } else if (mode == EFI_MODE_AUDIT) {
234 ret = efi_set_variable_internal(L"PK",
235 &efi_global_variable_guid,
236 attributes,
237 0, NULL,
238 false);
239 if (ret != EFI_SUCCESS)
240 goto err;
241 val = 0;
242 ret = efi_set_variable_internal(L"SecureBoot",
243 &efi_global_variable_guid,
244 attributes | READ_ONLY,
245 sizeof(val), &val,
246 false);
247 if (ret != EFI_SUCCESS)
248 goto err;
249 val = 1;
250 ret = efi_set_variable_internal(L"SetupMode",
251 &efi_global_variable_guid,
252 attributes | READ_ONLY,
253 sizeof(val), &val,
254 false);
255 if (ret != EFI_SUCCESS)
256 goto err;
257 val = 1;
258 ret = efi_set_variable_internal(L"AuditMode",
259 &efi_global_variable_guid,
260 attributes | READ_ONLY,
261 sizeof(val), &val,
262 false);
263 if (ret != EFI_SUCCESS)
264 goto err;
265 val = 0;
266 ret = efi_set_variable_internal(L"DeployedMode",
267 &efi_global_variable_guid,
268 attributes | READ_ONLY,
269 sizeof(val), &val,
270 false);
271 if (ret != EFI_SUCCESS)
272 goto err;
273
274 efi_secure_boot = true;
275 } else if (mode == EFI_MODE_USER) {
276 val = 1;
277 ret = efi_set_variable_internal(L"SecureBoot",
278 &efi_global_variable_guid,
279 attributes | READ_ONLY,
280 sizeof(val), &val,
281 false);
282 if (ret != EFI_SUCCESS)
283 goto err;
284 val = 0;
285 ret = efi_set_variable_internal(L"SetupMode",
286 &efi_global_variable_guid,
287 attributes | READ_ONLY,
288 sizeof(val), &val,
289 false);
290 if (ret != EFI_SUCCESS)
291 goto err;
292 val = 0;
293 ret = efi_set_variable_internal(L"AuditMode",
294 &efi_global_variable_guid,
295 attributes,
296 sizeof(val), &val,
297 false);
298 if (ret != EFI_SUCCESS)
299 goto err;
300 val = 0;
301 ret = efi_set_variable_internal(L"DeployedMode",
302 &efi_global_variable_guid,
303 attributes,
304 sizeof(val), &val,
305 false);
306 if (ret != EFI_SUCCESS)
307 goto err;
308
309 efi_secure_boot = true;
310 } else if (mode == EFI_MODE_SETUP) {
311 val = 0;
312 ret = efi_set_variable_internal(L"SecureBoot",
313 &efi_global_variable_guid,
314 attributes | READ_ONLY,
315 sizeof(val), &val,
316 false);
317 if (ret != EFI_SUCCESS)
318 goto err;
319 val = 1;
320 ret = efi_set_variable_internal(L"SetupMode",
321 &efi_global_variable_guid,
322 attributes | READ_ONLY,
323 sizeof(val), &val,
324 false);
325 if (ret != EFI_SUCCESS)
326 goto err;
327 val = 0;
328 ret = efi_set_variable_internal(L"AuditMode",
329 &efi_global_variable_guid,
330 attributes,
331 sizeof(val), &val,
332 false);
333 if (ret != EFI_SUCCESS)
334 goto err;
335 val = 0;
336 ret = efi_set_variable_internal(L"DeployedMode",
337 &efi_global_variable_guid,
338 attributes | READ_ONLY,
339 sizeof(val), &val,
340 false);
341 if (ret != EFI_SUCCESS)
342 goto err;
343 } else {
344 return EFI_INVALID_PARAMETER;
345 }
346
347 return EFI_SUCCESS;
348
349err:
350 /* TODO: What action should be taken here? */
351 printf("ERROR: Secure state transition failed\n");
352 return ret;
353}
354
355/**
356 * efi_init_secure_state - initialize secure boot state
357 *
358 * Return: EFI_SUCCESS on success, status code (negative) on error
359 */
360static efi_status_t efi_init_secure_state(void)
361{
362 efi_uintn_t size = 0;
363 efi_status_t ret;
364
365 ret = EFI_CALL(efi_get_variable(L"PK", &efi_global_variable_guid,
366 NULL, &size, NULL));
367 if (ret == EFI_BUFFER_TOO_SMALL && IS_ENABLED(CONFIG_EFI_SECURE_BOOT))
368 ret = efi_transfer_secure_state(EFI_MODE_USER);
369 else
370 ret = efi_transfer_secure_state(EFI_MODE_SETUP);
371
372 return ret;
373}
374
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100375/**
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900376 * efi_secure_boot_enabled - return if secure boot is enabled or not
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100377 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900378 * Return: true if enabled, false if disabled
379 */
380bool efi_secure_boot_enabled(void)
381{
382 return efi_secure_boot;
383}
384
385#ifdef CONFIG_EFI_SECURE_BOOT
386static u8 pkcs7_hdr[] = {
387 /* SEQUENCE */
388 0x30, 0x82, 0x05, 0xc7,
389 /* OID: pkcs7-signedData */
390 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
391 /* Context Structured? */
392 0xa0, 0x82, 0x05, 0xb8,
393};
394
395/**
396 * efi_variable_parse_signature - parse a signature in variable
397 * @buf: Pointer to variable's value
398 * @buflen: Length of @buf
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100399 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900400 * Parse a signature embedded in variable's value and instantiate
401 * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
402 * pkcs7's signedData, some header needed be prepended for correctly
403 * parsing authentication data, particularly for variable's.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100404 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900405 * Return: Pointer to pkcs7_message structure on success, NULL on error
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100406 */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900407static struct pkcs7_message *efi_variable_parse_signature(const void *buf,
408 size_t buflen)
409{
410 u8 *ebuf;
411 size_t ebuflen, len;
412 struct pkcs7_message *msg;
413
414 /*
415 * This is the best assumption to check if the binary is
416 * already in a form of pkcs7's signedData.
417 */
418 if (buflen > sizeof(pkcs7_hdr) &&
419 !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
420 msg = pkcs7_parse_message(buf, buflen);
421 goto out;
422 }
423
424 /*
425 * Otherwise, we should add a dummy prefix sequence for pkcs7
426 * message parser to be able to process.
427 * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
428 * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
429 * TODO:
430 * The header should be composed in a more refined manner.
431 */
432 debug("Makeshift prefix added to authentication data\n");
433 ebuflen = sizeof(pkcs7_hdr) + buflen;
434 if (ebuflen <= 0x7f) {
435 debug("Data is too short\n");
436 return NULL;
437 }
438
439 ebuf = malloc(ebuflen);
440 if (!ebuf) {
441 debug("Out of memory\n");
442 return NULL;
443 }
444
445 memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
446 memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
447 len = ebuflen - 4;
448 ebuf[2] = (len >> 8) & 0xff;
449 ebuf[3] = len & 0xff;
450 len = ebuflen - 0x13;
451 ebuf[0x11] = (len >> 8) & 0xff;
452 ebuf[0x12] = len & 0xff;
453
454 msg = pkcs7_parse_message(ebuf, ebuflen);
455
456 free(ebuf);
457
458out:
459 if (IS_ERR(msg))
460 return NULL;
461
462 return msg;
463}
464
465/**
466 * efi_variable_authenticate - authenticate a variable
467 * @variable: Variable name in u16
468 * @vendor: Guid of variable
469 * @data_size: Size of @data
470 * @data: Pointer to variable's value
471 * @given_attr: Attributes to be given at SetVariable()
472 * @env_attr: Attributes that an existing variable holds
473 * @time: signed time that an existing variable holds
474 *
475 * Called by efi_set_variable() to verify that the input is correct.
476 * Will replace the given data pointer with another that points to
477 * the actual data to store in the internal memory.
478 * On success, @data and @data_size will be replaced with variable's
479 * actual data, excluding authentication data, and its size, and variable's
480 * attributes and signed time will also be returned in @env_attr and @time,
481 * respectively.
482 *
483 * Return: EFI_SUCCESS on success, status code (negative) on error
484 */
485static efi_status_t efi_variable_authenticate(u16 *variable,
486 const efi_guid_t *vendor,
487 efi_uintn_t *data_size,
488 const void **data, u32 given_attr,
489 u32 *env_attr, u64 *time)
490{
491 const struct efi_variable_authentication_2 *auth;
492 struct efi_signature_store *truststore, *truststore2;
493 struct pkcs7_message *var_sig;
494 struct efi_image_regions *regs;
495 struct efi_time timestamp;
496 struct rtc_time tm;
497 u64 new_time;
498 efi_status_t ret;
499
500 var_sig = NULL;
501 truststore = NULL;
502 truststore2 = NULL;
503 regs = NULL;
504 ret = EFI_SECURITY_VIOLATION;
505
506 if (*data_size < sizeof(struct efi_variable_authentication_2))
507 goto err;
508
509 /* authentication data */
510 auth = *data;
511 if (*data_size < (sizeof(auth->time_stamp)
512 + auth->auth_info.hdr.dwLength))
513 goto err;
514
515 if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
516 goto err;
517
518 *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
519 *data_size -= (sizeof(auth->time_stamp)
520 + auth->auth_info.hdr.dwLength);
521
522 memcpy(&timestamp, &auth->time_stamp, sizeof(timestamp));
523 memset(&tm, 0, sizeof(tm));
524 tm.tm_year = timestamp.year;
525 tm.tm_mon = timestamp.month;
526 tm.tm_mday = timestamp.day;
527 tm.tm_hour = timestamp.hour;
528 tm.tm_min = timestamp.minute;
529 tm.tm_sec = timestamp.second;
530 new_time = rtc_mktime(&tm);
531
532 if (!efi_secure_boot_enabled()) {
533 /* finished checking */
534 *time = new_time;
535 return EFI_SUCCESS;
536 }
537
538 if (new_time <= *time)
539 goto err;
540
541 /* data to be digested */
542 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
543 if (!regs)
544 goto err;
545 regs->max = 5;
546 efi_image_region_add(regs, (uint8_t *)variable,
547 (uint8_t *)variable
548 + u16_strlen(variable) * sizeof(u16), 1);
549 efi_image_region_add(regs, (uint8_t *)vendor,
550 (uint8_t *)vendor + sizeof(*vendor), 1);
551 efi_image_region_add(regs, (uint8_t *)&given_attr,
552 (uint8_t *)&given_attr + sizeof(given_attr), 1);
553 efi_image_region_add(regs, (uint8_t *)&timestamp,
554 (uint8_t *)&timestamp + sizeof(timestamp), 1);
555 efi_image_region_add(regs, (uint8_t *)*data,
556 (uint8_t *)*data + *data_size, 1);
557
558 /* variable's signature list */
559 if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
560 goto err;
561 var_sig = efi_variable_parse_signature(auth->auth_info.cert_data,
562 auth->auth_info.hdr.dwLength
563 - sizeof(auth->auth_info));
564 if (IS_ERR(var_sig)) {
565 debug("Parsing variable's signature failed\n");
566 var_sig = NULL;
567 goto err;
568 }
569
570 /* signature database used for authentication */
571 if (u16_strcmp(variable, L"PK") == 0 ||
572 u16_strcmp(variable, L"KEK") == 0) {
573 /* with PK */
574 truststore = efi_sigstore_parse_sigdb(L"PK");
575 if (!truststore)
576 goto err;
577 } else if (u16_strcmp(variable, L"db") == 0 ||
578 u16_strcmp(variable, L"dbx") == 0) {
579 /* with PK and KEK */
580 truststore = efi_sigstore_parse_sigdb(L"KEK");
581 truststore2 = efi_sigstore_parse_sigdb(L"PK");
582
583 if (!truststore) {
584 if (!truststore2)
585 goto err;
586
587 truststore = truststore2;
588 truststore2 = NULL;
589 }
590 } else {
591 /* TODO: support private authenticated variables */
592 goto err;
593 }
594
595 /* verify signature */
596 if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) {
597 debug("Verified\n");
598 } else {
599 if (truststore2 &&
600 efi_signature_verify_with_sigdb(regs, var_sig,
601 truststore2, NULL)) {
602 debug("Verified\n");
603 } else {
604 debug("Verifying variable's signature failed\n");
605 goto err;
606 }
607 }
608
609 /* finished checking */
610 *time = rtc_mktime(&tm);
611 ret = EFI_SUCCESS;
612
613err:
614 efi_sigstore_free(truststore);
615 efi_sigstore_free(truststore2);
616 pkcs7_free_message(var_sig);
617 free(regs);
618
619 return ret;
620}
621#else
622static efi_status_t efi_variable_authenticate(u16 *variable,
623 const efi_guid_t *vendor,
624 efi_uintn_t *data_size,
625 const void **data, u32 given_attr,
626 u32 *env_attr, u64 *time)
627{
628 return EFI_SUCCESS;
629}
630#endif /* CONFIG_EFI_SECURE_BOOT */
631
632static
633efi_status_t EFIAPI efi_get_variable_common(u16 *variable_name,
634 const efi_guid_t *vendor,
635 u32 *attributes,
636 efi_uintn_t *data_size, void *data,
637 bool is_non_volatile)
Rob Clark15f3d742017-09-13 18:05:37 -0400638{
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200639 char *native_name;
Rob Clark15f3d742017-09-13 18:05:37 -0400640 efi_status_t ret;
641 unsigned long in_size;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900642 const char *val = NULL, *s;
643 u64 time = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400644 u32 attr;
645
Rob Clark15f3d742017-09-13 18:05:37 -0400646 if (!variable_name || !vendor || !data_size)
647 return EFI_EXIT(EFI_INVALID_PARAMETER);
648
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200649 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400650 if (ret)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900651 return ret;
Rob Clark15f3d742017-09-13 18:05:37 -0400652
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200653 EFI_PRINT("get '%s'\n", native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400654
655 val = env_get(native_name);
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200656 free(native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400657 if (!val)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900658 return EFI_NOT_FOUND;
Rob Clark15f3d742017-09-13 18:05:37 -0400659
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900660 val = parse_attr(val, &attr, &time);
Rob Clark15f3d742017-09-13 18:05:37 -0400661
662 in_size = *data_size;
663
664 if ((s = prefix(val, "(blob)"))) {
Heinrich Schuchardt2d8aedc2019-01-18 12:31:54 +0100665 size_t len = strlen(s);
Rob Clark15f3d742017-09-13 18:05:37 -0400666
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700667 /* number of hexadecimal digits must be even */
668 if (len & 1)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900669 return EFI_DEVICE_ERROR;
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700670
Rob Clark15f3d742017-09-13 18:05:37 -0400671 /* two characters per byte: */
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700672 len /= 2;
Rob Clark15f3d742017-09-13 18:05:37 -0400673 *data_size = len;
674
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200675 if (in_size < len) {
676 ret = EFI_BUFFER_TOO_SMALL;
677 goto out;
678 }
Rob Clark15f3d742017-09-13 18:05:37 -0400679
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900680 if (!data) {
681 debug("Variable with no data shouldn't exist.\n");
682 return EFI_INVALID_PARAMETER;
683 }
Rob Clark15f3d742017-09-13 18:05:37 -0400684
Heinrich Schuchardt2d8aedc2019-01-18 12:31:54 +0100685 if (hex2bin(data, s, len))
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900686 return EFI_DEVICE_ERROR;
Rob Clark15f3d742017-09-13 18:05:37 -0400687
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200688 EFI_PRINT("got value: \"%s\"\n", s);
Rob Clark15f3d742017-09-13 18:05:37 -0400689 } else if ((s = prefix(val, "(utf8)"))) {
690 unsigned len = strlen(s) + 1;
691
692 *data_size = len;
693
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200694 if (in_size < len) {
695 ret = EFI_BUFFER_TOO_SMALL;
696 goto out;
697 }
Rob Clark15f3d742017-09-13 18:05:37 -0400698
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900699 if (!data) {
700 debug("Variable with no data shouldn't exist.\n");
701 return EFI_INVALID_PARAMETER;
702 }
Rob Clark15f3d742017-09-13 18:05:37 -0400703
704 memcpy(data, s, len);
705 ((char *)data)[len] = '\0';
706
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200707 EFI_PRINT("got value: \"%s\"\n", (char *)data);
Rob Clark15f3d742017-09-13 18:05:37 -0400708 } else {
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200709 EFI_PRINT("invalid value: '%s'\n", val);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900710 return EFI_DEVICE_ERROR;
Rob Clark15f3d742017-09-13 18:05:37 -0400711 }
712
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200713out:
Rob Clark15f3d742017-09-13 18:05:37 -0400714 if (attributes)
715 *attributes = attr & EFI_VARIABLE_MASK;
716
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900717 return ret;
718}
719
720static
721efi_status_t EFIAPI efi_get_volatile_variable(u16 *variable_name,
722 const efi_guid_t *vendor,
723 u32 *attributes,
724 efi_uintn_t *data_size,
725 void *data)
726{
727 return efi_get_variable_common(variable_name, vendor, attributes,
728 data_size, data, false);
729}
730
731efi_status_t EFIAPI efi_get_nonvolatile_variable(u16 *variable_name,
732 const efi_guid_t *vendor,
733 u32 *attributes,
734 efi_uintn_t *data_size,
735 void *data)
736{
737 return efi_get_variable_common(variable_name, vendor, attributes,
738 data_size, data, true);
739}
740
741/**
742 * efi_efi_get_variable() - retrieve value of a UEFI variable
743 *
744 * This function implements the GetVariable runtime service.
745 *
746 * See the Unified Extensible Firmware Interface (UEFI) specification for
747 * details.
748 *
749 * @variable_name: name of the variable
750 * @vendor: vendor GUID
751 * @attributes: attributes of the variable
752 * @data_size: size of the buffer to which the variable value is copied
753 * @data: buffer to which the variable value is copied
754 * Return: status code
755 */
756efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
757 const efi_guid_t *vendor, u32 *attributes,
758 efi_uintn_t *data_size, void *data)
759{
760 efi_status_t ret;
761
762 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
763 data_size, data);
764
765 ret = efi_get_volatile_variable(variable_name, vendor, attributes,
766 data_size, data);
767 if (ret == EFI_NOT_FOUND)
768 ret = efi_get_nonvolatile_variable(variable_name, vendor,
769 attributes, data_size, data);
770
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200771 return EFI_EXIT(ret);
Rob Clark15f3d742017-09-13 18:05:37 -0400772}
773
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100774static char *efi_variables_list;
775static char *efi_cur_variable;
776
777/**
778 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
779 * information
780 * @variable: whole data of u-boot variable (ie. name=value)
781 * @variable_name_size: size of variable_name buffer in byte
782 * @variable_name: name of uefi variable in u16, null-terminated
783 * @vendor: vendor's guid
784 * @attributes: attributes
785 *
786 * A uefi variable is encoded into a u-boot variable as described above.
787 * This function parses such a u-boot variable and retrieve uefi-related
788 * information into respective parameters. In return, variable_name_size
789 * is the size of variable name including NULL.
790 *
791 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200792 * the entire variable list has been returned,
793 * otherwise non-zero status code
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100794 */
795static efi_status_t parse_uboot_variable(char *variable,
796 efi_uintn_t *variable_name_size,
797 u16 *variable_name,
798 const efi_guid_t *vendor,
799 u32 *attributes)
800{
801 char *guid, *name, *end, c;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100802 size_t name_len;
803 efi_uintn_t old_variable_name_size;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900804 u64 time;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100805 u16 *p;
806
807 guid = strchr(variable, '_');
808 if (!guid)
809 return EFI_INVALID_PARAMETER;
810 guid++;
811 name = strchr(guid, '_');
812 if (!name)
813 return EFI_INVALID_PARAMETER;
814 name++;
815 end = strchr(name, '=');
816 if (!end)
817 return EFI_INVALID_PARAMETER;
818
819 name_len = end - name;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100820 old_variable_name_size = *variable_name_size;
821 *variable_name_size = sizeof(u16) * (name_len + 1);
822 if (old_variable_name_size < *variable_name_size)
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100823 return EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100824
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100825 end++; /* point to value */
826
827 /* variable name */
828 p = variable_name;
829 utf8_utf16_strncpy(&p, name, name_len);
830 variable_name[name_len] = 0;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100831
832 /* guid */
833 c = *(name - 1);
834 *(name - 1) = '\0'; /* guid need be null-terminated here */
835 uuid_str_to_bin(guid, (unsigned char *)vendor, UUID_STR_FORMAT_GUID);
836 *(name - 1) = c;
837
838 /* attributes */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900839 parse_attr(end, attributes, &time);
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100840
841 return EFI_SUCCESS;
842}
843
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100844/**
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100845 * efi_get_next_variable_name() - enumerate the current variable names
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200846 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100847 * @variable_name_size: size of variable_name buffer in byte
848 * @variable_name: name of uefi variable's name in u16
849 * @vendor: vendor's guid
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100850 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100851 * This function implements the GetNextVariableName service.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100852 *
853 * See the Unified Extensible Firmware Interface (UEFI) specification for
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200854 * details.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100855 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100856 * Return: status code
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100857 */
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200858efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
859 u16 *variable_name,
Heinrich Schuchardt82cd6c42020-03-22 18:28:20 +0100860 efi_guid_t *vendor)
Rob Clark15f3d742017-09-13 18:05:37 -0400861{
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100862 char *native_name, *variable;
863 ssize_t name_len, list_len;
864 char regex[256];
865 char * const regexlist[] = {regex};
866 u32 attributes;
867 int i;
868 efi_status_t ret;
869
Rob Clark238f88c2017-09-13 18:05:41 -0400870 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400871
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100872 if (!variable_name_size || !variable_name || !vendor)
Heinrich Schuchardt793a6252019-03-19 18:36:21 +0100873 return EFI_EXIT(EFI_INVALID_PARAMETER);
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100874
875 if (variable_name[0]) {
876 /* check null-terminated string */
877 for (i = 0; i < *variable_name_size; i++)
878 if (!variable_name[i])
879 break;
880 if (i >= *variable_name_size)
881 return EFI_EXIT(EFI_INVALID_PARAMETER);
882
883 /* search for the last-returned variable */
884 ret = efi_to_native(&native_name, variable_name, vendor);
885 if (ret)
886 return EFI_EXIT(ret);
887
888 name_len = strlen(native_name);
889 for (variable = efi_variables_list; variable && *variable;) {
890 if (!strncmp(variable, native_name, name_len) &&
891 variable[name_len] == '=')
892 break;
893
894 variable = strchr(variable, '\n');
895 if (variable)
896 variable++;
897 }
898
899 free(native_name);
900 if (!(variable && *variable))
901 return EFI_EXIT(EFI_INVALID_PARAMETER);
902
903 /* next variable */
904 variable = strchr(variable, '\n');
905 if (variable)
906 variable++;
907 if (!(variable && *variable))
908 return EFI_EXIT(EFI_NOT_FOUND);
909 } else {
910 /*
911 *new search: free a list used in the previous search
912 */
913 free(efi_variables_list);
914 efi_variables_list = NULL;
915 efi_cur_variable = NULL;
916
917 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
918 list_len = hexport_r(&env_htab, '\n',
919 H_MATCH_REGEX | H_MATCH_KEY,
920 &efi_variables_list, 0, 1, regexlist);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900921
Heinrich Schuchardtfd738482019-01-22 20:10:46 +0100922 if (list_len <= 1)
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100923 return EFI_EXIT(EFI_NOT_FOUND);
924
925 variable = efi_variables_list;
926 }
927
928 ret = parse_uboot_variable(variable, variable_name_size, variable_name,
929 vendor, &attributes);
930
931 return EFI_EXIT(ret);
Rob Clark15f3d742017-09-13 18:05:37 -0400932}
933
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900934static
935efi_status_t EFIAPI efi_set_variable_common(u16 *variable_name,
936 const efi_guid_t *vendor,
937 u32 attributes,
938 efi_uintn_t data_size,
939 const void *data,
940 bool ro_check,
941 bool is_non_volatile)
Rob Clark15f3d742017-09-13 18:05:37 -0400942{
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900943 char *native_name = NULL, *old_data = NULL, *val = NULL, *s;
944 efi_uintn_t old_size;
945 bool append, delete;
946 u64 time = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400947 u32 attr;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900948 efi_status_t ret = EFI_SUCCESS;
Rob Clark15f3d742017-09-13 18:05:37 -0400949
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900950 debug("%s: set '%s'\n", __func__, native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400951
Heinrich Schuchardtadba3962019-06-12 23:28:42 +0200952 if (!variable_name || !*variable_name || !vendor ||
953 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900954 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200955 ret = EFI_INVALID_PARAMETER;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900956 goto err;
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200957 }
Rob Clark15f3d742017-09-13 18:05:37 -0400958
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200959 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400960 if (ret)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900961 goto err;
Rob Clark15f3d742017-09-13 18:05:37 -0400962
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900963 /* check if a variable exists */
964 old_size = 0;
965 attr = 0;
966 ret = EFI_CALL(efi_get_variable(variable_name, vendor, &attr,
967 &old_size, NULL));
968 if (ret == EFI_BUFFER_TOO_SMALL) {
969 if ((is_non_volatile && !(attr & EFI_VARIABLE_NON_VOLATILE)) ||
970 (!is_non_volatile && (attr & EFI_VARIABLE_NON_VOLATILE))) {
971 ret = EFI_INVALID_PARAMETER;
972 goto err;
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200973 }
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900974 }
AKASHI Takahiro186b5a42019-05-24 15:59:03 +0900975
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900976 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
977 attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
978 delete = !append && (!data_size || !attributes);
979
980 /* check attributes */
981 if (old_size) {
982 if (ro_check && (attr & READ_ONLY)) {
983 ret = EFI_WRITE_PROTECTED;
984 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900985 }
986
987 /* attributes won't be changed */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900988 if (!delete &&
989 ((ro_check && attr != attributes) ||
990 (!ro_check && ((attr & ~(u32)READ_ONLY)
991 != (attributes & ~(u32)READ_ONLY))))) {
AKASHI Takahiro186b5a42019-05-24 15:59:03 +0900992 ret = EFI_INVALID_PARAMETER;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900993 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900994 }
995 } else {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900996 if (delete || append) {
Heinrich Schuchardt8021bbe2019-09-26 21:40:18 +0200997 /*
998 * Trying to delete or to update a non-existent
999 * variable.
1000 */
AKASHI Takahiroe5023b72019-09-06 15:09:52 +09001001 ret = EFI_NOT_FOUND;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001002 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +09001003 }
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001004 }
AKASHI Takahiroe5023b72019-09-06 15:09:52 +09001005
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001006 if (((!u16_strcmp(variable_name, L"PK") ||
1007 !u16_strcmp(variable_name, L"KEK")) &&
1008 !guidcmp(vendor, &efi_global_variable_guid)) ||
1009 ((!u16_strcmp(variable_name, L"db") ||
1010 !u16_strcmp(variable_name, L"dbx")) &&
1011 !guidcmp(vendor, &efi_guid_image_security_database))) {
1012 /* authentication is mandatory */
1013 if (!(attributes &
1014 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
1015 debug("%ls: AUTHENTICATED_WRITE_ACCESS required\n",
1016 variable_name);
1017 ret = EFI_INVALID_PARAMETER;
1018 goto err;
1019 }
1020 }
1021
1022 /* authenticate a variable */
1023 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
1024 if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
1025 ret = EFI_INVALID_PARAMETER;
1026 goto err;
1027 }
1028 if (attributes &
1029 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
1030 ret = efi_variable_authenticate(variable_name, vendor,
1031 &data_size, &data,
1032 attributes, &attr,
1033 &time);
1034 if (ret != EFI_SUCCESS)
1035 goto err;
1036
1037 /* last chance to check for delete */
1038 if (!data_size)
1039 delete = true;
1040 }
1041 } else {
1042 if (attributes &
1043 (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
1044 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
1045 debug("Secure boot is not configured\n");
1046 ret = EFI_INVALID_PARAMETER;
1047 goto err;
1048 }
1049 }
1050
1051 /* delete a variable */
1052 if (delete) {
1053 /* !old_size case has been handled before */
1054 val = NULL;
1055 ret = EFI_SUCCESS;
1056 goto out;
1057 }
1058
1059 if (append) {
1060 old_data = malloc(old_size);
1061 if (!old_data) {
1062 return EFI_OUT_OF_RESOURCES;
1063 goto err;
1064 }
1065 ret = EFI_CALL(efi_get_variable(variable_name, vendor,
1066 &attr, &old_size, old_data));
1067 if (ret != EFI_SUCCESS)
1068 goto err;
1069 } else {
AKASHI Takahiroe5023b72019-09-06 15:09:52 +09001070 old_size = 0;
Rob Clark15f3d742017-09-13 18:05:37 -04001071 }
1072
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001073 val = malloc(2 * old_size + 2 * data_size
1074 + strlen("{ro,run,boot,nv,time=0123456701234567}(blob)")
1075 + 1);
Heinrich Schuchardtbd095f52018-10-02 05:30:05 +02001076 if (!val) {
1077 ret = EFI_OUT_OF_RESOURCES;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001078 goto err;
Heinrich Schuchardtbd095f52018-10-02 05:30:05 +02001079 }
Rob Clark15f3d742017-09-13 18:05:37 -04001080
1081 s = val;
1082
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001083 /*
1084 * store attributes
1085 */
1086 attributes &= (READ_ONLY |
1087 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro7f334332019-06-04 15:52:06 +09001088 EFI_VARIABLE_BOOTSERVICE_ACCESS |
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001089 EFI_VARIABLE_RUNTIME_ACCESS |
1090 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS);
Rob Clark15f3d742017-09-13 18:05:37 -04001091 s += sprintf(s, "{");
1092 while (attributes) {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001093 attr = 1 << (ffs(attributes) - 1);
Rob Clark15f3d742017-09-13 18:05:37 -04001094
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001095 if (attr == READ_ONLY) {
1096 s += sprintf(s, "ro");
1097 } else if (attr == EFI_VARIABLE_NON_VOLATILE) {
AKASHI Takahiro7f334332019-06-04 15:52:06 +09001098 s += sprintf(s, "nv");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001099 } else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) {
Rob Clark15f3d742017-09-13 18:05:37 -04001100 s += sprintf(s, "boot");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001101 } else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) {
Rob Clark15f3d742017-09-13 18:05:37 -04001102 s += sprintf(s, "run");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001103 } else if (attr ==
1104 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
1105 s += sprintf(s, "time=");
1106 s = bin2hex(s, (u8 *)&time, sizeof(time));
1107 }
Rob Clark15f3d742017-09-13 18:05:37 -04001108
1109 attributes &= ~attr;
1110 if (attributes)
1111 s += sprintf(s, ",");
1112 }
1113 s += sprintf(s, "}");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001114 s += sprintf(s, "(blob)");
AKASHI Takahiroe5023b72019-09-06 15:09:52 +09001115
Rob Clark15f3d742017-09-13 18:05:37 -04001116 /* store payload: */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001117 if (append)
1118 s = bin2hex(s, old_data, old_size);
Heinrich Schuchardt807899d2019-01-18 18:54:26 +01001119 s = bin2hex(s, data, data_size);
Rob Clark15f3d742017-09-13 18:05:37 -04001120 *s = '\0';
1121
Heinrich Schuchardt55111c32019-04-04 21:36:44 +02001122 EFI_PRINT("setting: %s=%s\n", native_name, val);
Rob Clark15f3d742017-09-13 18:05:37 -04001123
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001124out:
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001125 if (env_set(native_name, val)) {
Rob Clark15f3d742017-09-13 18:05:37 -04001126 ret = EFI_DEVICE_ERROR;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001127 } else {
1128 if ((u16_strcmp(variable_name, L"PK") == 0 &&
1129 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1130 ret = efi_transfer_secure_state(
1131 (delete ? EFI_MODE_SETUP :
1132 EFI_MODE_USER));
1133 if (ret != EFI_SUCCESS)
1134 goto err;
1135 }
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001136 ret = EFI_SUCCESS;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001137 }
Rob Clark15f3d742017-09-13 18:05:37 -04001138
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001139err:
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +02001140 free(native_name);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001141 free(old_data);
Rob Clark15f3d742017-09-13 18:05:37 -04001142 free(val);
1143
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001144 return ret;
1145}
1146
1147static
1148efi_status_t EFIAPI efi_set_volatile_variable(u16 *variable_name,
1149 const efi_guid_t *vendor,
1150 u32 attributes,
1151 efi_uintn_t data_size,
1152 const void *data,
1153 bool ro_check)
1154{
1155 return efi_set_variable_common(variable_name, vendor, attributes,
1156 data_size, data, ro_check, false);
1157}
1158
1159efi_status_t EFIAPI efi_set_nonvolatile_variable(u16 *variable_name,
1160 const efi_guid_t *vendor,
1161 u32 attributes,
1162 efi_uintn_t data_size,
1163 const void *data,
1164 bool ro_check)
1165{
1166 efi_status_t ret;
1167
1168 ret = efi_set_variable_common(variable_name, vendor, attributes,
1169 data_size, data, ro_check, true);
1170
1171 return ret;
1172}
1173
1174static efi_status_t efi_set_variable_internal(u16 *variable_name,
1175 const efi_guid_t *vendor,
1176 u32 attributes,
1177 efi_uintn_t data_size,
1178 const void *data,
1179 bool ro_check)
1180{
1181 efi_status_t ret;
1182
1183 if (attributes & EFI_VARIABLE_NON_VOLATILE)
1184 ret = efi_set_nonvolatile_variable(variable_name, vendor,
1185 attributes,
1186 data_size, data, ro_check);
1187 else
1188 ret = efi_set_volatile_variable(variable_name, vendor,
1189 attributes, data_size, data,
1190 ro_check);
1191
1192 return ret;
1193}
1194
1195/**
1196 * efi_set_variable() - set value of a UEFI variable
1197 *
1198 * This function implements the SetVariable runtime service.
1199 *
1200 * See the Unified Extensible Firmware Interface (UEFI) specification for
1201 * details.
1202 *
1203 * @variable_name: name of the variable
1204 * @vendor: vendor GUID
1205 * @attributes: attributes of the variable
1206 * @data_size: size of the buffer with the variable value
1207 * @data: buffer with the variable value
1208 * Return: status code
1209 */
1210efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
1211 const efi_guid_t *vendor, u32 attributes,
1212 efi_uintn_t data_size, const void *data)
1213{
1214 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
1215 data_size, data);
1216
1217 /* READ_ONLY bit is not part of API */
1218 attributes &= ~(u32)READ_ONLY;
1219
1220 return EFI_EXIT(efi_set_variable_internal(variable_name, vendor,
1221 attributes, data_size, data,
1222 true));
Rob Clark15f3d742017-09-13 18:05:37 -04001223}
Heinrich Schuchardt9b19dae2019-06-20 12:13:05 +02001224
1225/**
1226 * efi_query_variable_info() - get information about EFI variables
1227 *
1228 * This function implements the QueryVariableInfo() runtime service.
1229 *
1230 * See the Unified Extensible Firmware Interface (UEFI) specification for
1231 * details.
1232 *
1233 * @attributes: bitmask to select variables to be
1234 * queried
1235 * @maximum_variable_storage_size: maximum size of storage area for the
1236 * selected variable types
1237 * @remaining_variable_storage_size: remaining size of storage are for the
1238 * selected variable types
1239 * @maximum_variable_size: maximum size of a variable of the
1240 * selected type
1241 * Returns: status code
1242 */
1243efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
1244 u32 attributes,
1245 u64 *maximum_variable_storage_size,
1246 u64 *remaining_variable_storage_size,
1247 u64 *maximum_variable_size)
1248{
1249 return EFI_UNSUPPORTED;
1250}
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001251
1252/**
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001253 * efi_get_variable_runtime() - runtime implementation of GetVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001254 *
1255 * @variable_name: name of the variable
1256 * @vendor: vendor GUID
1257 * @attributes: attributes of the variable
1258 * @data_size: size of the buffer to which the variable value is copied
1259 * @data: buffer to which the variable value is copied
1260 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001261 */
1262static efi_status_t __efi_runtime EFIAPI
1263efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1264 u32 *attributes, efi_uintn_t *data_size, void *data)
1265{
1266 return EFI_UNSUPPORTED;
1267}
1268
1269/**
1270 * efi_get_next_variable_name_runtime() - runtime implementation of
1271 * GetNextVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001272 *
1273 * @variable_name_size: size of variable_name buffer in byte
1274 * @variable_name: name of uefi variable's name in u16
1275 * @vendor: vendor's guid
1276 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001277 */
1278static efi_status_t __efi_runtime EFIAPI
1279efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
Heinrich Schuchardt82cd6c42020-03-22 18:28:20 +01001280 u16 *variable_name, efi_guid_t *vendor)
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001281{
1282 return EFI_UNSUPPORTED;
1283}
1284
1285/**
1286 * efi_set_variable_runtime() - runtime implementation of SetVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001287 *
1288 * @variable_name: name of the variable
1289 * @vendor: vendor GUID
1290 * @attributes: attributes of the variable
1291 * @data_size: size of the buffer with the variable value
1292 * @data: buffer with the variable value
1293 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001294 */
1295static efi_status_t __efi_runtime EFIAPI
1296efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1297 u32 attributes, efi_uintn_t data_size,
1298 const void *data)
1299{
1300 return EFI_UNSUPPORTED;
1301}
1302
1303/**
1304 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
1305 */
1306void efi_variables_boot_exit_notify(void)
1307{
1308 efi_runtime_services.get_variable = efi_get_variable_runtime;
1309 efi_runtime_services.get_next_variable_name =
1310 efi_get_next_variable_name_runtime;
1311 efi_runtime_services.set_variable = efi_set_variable_runtime;
1312 efi_update_table_header_crc32(&efi_runtime_services.hdr);
1313}
1314
1315/**
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001316 * efi_init_variables() - initialize variable services
1317 *
1318 * Return: status code
1319 */
1320efi_status_t efi_init_variables(void)
1321{
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001322 efi_status_t ret;
1323
1324 ret = efi_init_secure_state();
1325
1326 return ret;
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001327}