blob: 58f8fae358c170965a5b56df0794386dd5b6f7b0 [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 Takahiro6ec67672020-04-21 09:38:17 +090015#include <crypto/pkcs7_parser.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090016#include <linux/compat.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070017#include <u-boot/crc.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090018
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;
AKASHI Takahiro94b139a2020-04-14 11:51:43 +090029static u8 efi_vendor_keys;
Rob Clark15f3d742017-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 Schuchardt44389cb2018-09-23 04:08:09 +020066#define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
Rob Clark15f3d742017-09-13 18:05:37 -040067
Heinrich Schuchardt0ffda472019-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 Schuchardtaf1a9202018-08-31 21:31:31 +020081static efi_status_t efi_to_native(char **native, const u16 *variable_name,
Heinrich Schuchardte06ae192018-12-30 20:53:51 +010082 const efi_guid_t *vendor)
Rob Clark15f3d742017-09-13 18:05:37 -040083{
84 size_t len;
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020085 char *pos;
Rob Clark15f3d742017-09-13 18:05:37 -040086
Heinrich Schuchardtaf1a9202018-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 Clark15f3d742017-09-13 18:05:37 -040091
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020092 pos = *native;
93 pos += sprintf(pos, "efi_%pUl_", vendor);
94 utf16_utf8_strcpy(&pos, variable_name);
Rob Clark15f3d742017-09-13 18:05:37 -040095
96 return EFI_SUCCESS;
97}
98
Heinrich Schuchardt0ffda472019-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 Clark15f3d742017-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 Schuchardt0ffda472019-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 Takahirob0f49ee2020-04-14 11:51:41 +0900124 * @timep: pointer to time attribute
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100125 * Return: pointer to remainder of U-Boot variable value
126 */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900127static const char *parse_attr(const char *str, u32 *attrp, u64 *timep)
Rob Clark15f3d742017-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 Takahiro7f334332019-06-04 15:52:06 +0900144 } else if ((s = prefix(str, "nv"))) {
145 attr |= EFI_VARIABLE_NON_VOLATILE;
Rob Clark15f3d742017-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 Takahirob0f49ee2020-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 Clark15f3d742017-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
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200172static efi_status_t efi_set_variable_common(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);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900178
179/**
AKASHI Takahirodae117a2020-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;
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200199 ret = efi_set_variable_common(L"SecureBoot", &efi_global_variable_guid,
200 attributes, sizeof(sec_boot), &sec_boot,
201 false);
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900202 if (ret != EFI_SUCCESS)
203 goto err;
204
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200205 ret = efi_set_variable_common(L"SetupMode", &efi_global_variable_guid,
206 attributes, sizeof(setup_mode),
207 &setup_mode, false);
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900208 if (ret != EFI_SUCCESS)
209 goto err;
210
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200211 ret = efi_set_variable_common(L"AuditMode", &efi_global_variable_guid,
212 attributes, sizeof(audit_mode),
213 &audit_mode, false);
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900214 if (ret != EFI_SUCCESS)
215 goto err;
216
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200217 ret = efi_set_variable_common(L"DeployedMode",
218 &efi_global_variable_guid, attributes,
219 sizeof(deployed_mode), &deployed_mode,
220 false);
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900221err:
222 return ret;
223}
224
225/**
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900226 * efi_transfer_secure_state - handle a secure boot state transition
227 * @mode: new state
228 *
229 * Depending on @mode, secure boot related variables are updated.
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200230 * Those variables are *read-only* for users, efi_set_variable_common()
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900231 * is called here.
232 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +0200233 * Return: status code
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900234 */
235static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
236{
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900237 efi_status_t ret;
238
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900239 debug("Switching secure state from %d to %d\n", efi_secure_mode, mode);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900240
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900241 if (mode == EFI_MODE_DEPLOYED) {
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900242 ret = efi_set_secure_state(1, 0, 0, 1);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900243 if (ret != EFI_SUCCESS)
244 goto err;
245
246 efi_secure_boot = true;
247 } else if (mode == EFI_MODE_AUDIT) {
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200248 ret = efi_set_variable_common(L"PK", &efi_global_variable_guid,
249 EFI_VARIABLE_BOOTSERVICE_ACCESS |
250 EFI_VARIABLE_RUNTIME_ACCESS,
251 0, NULL, false);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900252 if (ret != EFI_SUCCESS)
253 goto err;
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900254
255 ret = efi_set_secure_state(0, 1, 1, 0);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900256 if (ret != EFI_SUCCESS)
257 goto err;
258
259 efi_secure_boot = true;
260 } else if (mode == EFI_MODE_USER) {
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900261 ret = efi_set_secure_state(1, 0, 0, 0);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900262 if (ret != EFI_SUCCESS)
263 goto err;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900264
265 efi_secure_boot = true;
266 } else if (mode == EFI_MODE_SETUP) {
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900267 ret = efi_set_secure_state(0, 1, 0, 0);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900268 if (ret != EFI_SUCCESS)
269 goto err;
270 } else {
271 return EFI_INVALID_PARAMETER;
272 }
273
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900274 efi_secure_mode = mode;
275
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900276 return EFI_SUCCESS;
277
278err:
279 /* TODO: What action should be taken here? */
280 printf("ERROR: Secure state transition failed\n");
281 return ret;
282}
283
284/**
285 * efi_init_secure_state - initialize secure boot state
286 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +0200287 * Return: status code
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900288 */
289static efi_status_t efi_init_secure_state(void)
290{
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900291 enum efi_secure_mode mode;
292 efi_uintn_t size;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900293 efi_status_t ret;
294
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900295 /*
296 * TODO:
297 * Since there is currently no "platform-specific" installation
298 * method of Platform Key, we can't say if VendorKeys is 0 or 1
299 * precisely.
300 */
301
302 size = 0;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900303 ret = EFI_CALL(efi_get_variable(L"PK", &efi_global_variable_guid,
304 NULL, &size, NULL));
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900305 if (ret == EFI_BUFFER_TOO_SMALL) {
306 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT))
307 mode = EFI_MODE_USER;
308 else
309 mode = EFI_MODE_SETUP;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900310
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900311 efi_vendor_keys = 0;
312 } else if (ret == EFI_NOT_FOUND) {
313 mode = EFI_MODE_SETUP;
314 efi_vendor_keys = 1;
315 } else {
316 goto err;
317 }
318
319 ret = efi_transfer_secure_state(mode);
320 if (ret == EFI_SUCCESS)
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200321 ret = efi_set_variable_common(L"VendorKeys",
322 &efi_global_variable_guid,
323 EFI_VARIABLE_BOOTSERVICE_ACCESS |
324 EFI_VARIABLE_RUNTIME_ACCESS |
325 READ_ONLY,
326 sizeof(efi_vendor_keys),
327 &efi_vendor_keys, false);
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900328
329err:
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900330 return ret;
331}
332
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100333/**
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900334 * efi_secure_boot_enabled - return if secure boot is enabled or not
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100335 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900336 * Return: true if enabled, false if disabled
337 */
338bool efi_secure_boot_enabled(void)
339{
340 return efi_secure_boot;
341}
342
343#ifdef CONFIG_EFI_SECURE_BOOT
344static u8 pkcs7_hdr[] = {
345 /* SEQUENCE */
346 0x30, 0x82, 0x05, 0xc7,
347 /* OID: pkcs7-signedData */
348 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
349 /* Context Structured? */
350 0xa0, 0x82, 0x05, 0xb8,
351};
352
353/**
354 * efi_variable_parse_signature - parse a signature in variable
355 * @buf: Pointer to variable's value
356 * @buflen: Length of @buf
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100357 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900358 * Parse a signature embedded in variable's value and instantiate
359 * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
360 * pkcs7's signedData, some header needed be prepended for correctly
361 * parsing authentication data, particularly for variable's.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100362 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900363 * Return: Pointer to pkcs7_message structure on success, NULL on error
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100364 */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900365static struct pkcs7_message *efi_variable_parse_signature(const void *buf,
366 size_t buflen)
367{
368 u8 *ebuf;
369 size_t ebuflen, len;
370 struct pkcs7_message *msg;
371
372 /*
373 * This is the best assumption to check if the binary is
374 * already in a form of pkcs7's signedData.
375 */
376 if (buflen > sizeof(pkcs7_hdr) &&
377 !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
378 msg = pkcs7_parse_message(buf, buflen);
379 goto out;
380 }
381
382 /*
383 * Otherwise, we should add a dummy prefix sequence for pkcs7
384 * message parser to be able to process.
385 * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
386 * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
387 * TODO:
388 * The header should be composed in a more refined manner.
389 */
390 debug("Makeshift prefix added to authentication data\n");
391 ebuflen = sizeof(pkcs7_hdr) + buflen;
392 if (ebuflen <= 0x7f) {
393 debug("Data is too short\n");
394 return NULL;
395 }
396
397 ebuf = malloc(ebuflen);
398 if (!ebuf) {
399 debug("Out of memory\n");
400 return NULL;
401 }
402
403 memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
404 memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
405 len = ebuflen - 4;
406 ebuf[2] = (len >> 8) & 0xff;
407 ebuf[3] = len & 0xff;
408 len = ebuflen - 0x13;
409 ebuf[0x11] = (len >> 8) & 0xff;
410 ebuf[0x12] = len & 0xff;
411
412 msg = pkcs7_parse_message(ebuf, ebuflen);
413
414 free(ebuf);
415
416out:
417 if (IS_ERR(msg))
418 return NULL;
419
420 return msg;
421}
422
423/**
424 * efi_variable_authenticate - authenticate a variable
425 * @variable: Variable name in u16
426 * @vendor: Guid of variable
427 * @data_size: Size of @data
428 * @data: Pointer to variable's value
429 * @given_attr: Attributes to be given at SetVariable()
430 * @env_attr: Attributes that an existing variable holds
431 * @time: signed time that an existing variable holds
432 *
433 * Called by efi_set_variable() to verify that the input is correct.
434 * Will replace the given data pointer with another that points to
435 * the actual data to store in the internal memory.
436 * On success, @data and @data_size will be replaced with variable's
437 * actual data, excluding authentication data, and its size, and variable's
438 * attributes and signed time will also be returned in @env_attr and @time,
439 * respectively.
440 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +0200441 * Return: status code
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900442 */
443static efi_status_t efi_variable_authenticate(u16 *variable,
444 const efi_guid_t *vendor,
445 efi_uintn_t *data_size,
446 const void **data, u32 given_attr,
447 u32 *env_attr, u64 *time)
448{
449 const struct efi_variable_authentication_2 *auth;
450 struct efi_signature_store *truststore, *truststore2;
451 struct pkcs7_message *var_sig;
452 struct efi_image_regions *regs;
453 struct efi_time timestamp;
454 struct rtc_time tm;
455 u64 new_time;
456 efi_status_t ret;
457
458 var_sig = NULL;
459 truststore = NULL;
460 truststore2 = NULL;
461 regs = NULL;
462 ret = EFI_SECURITY_VIOLATION;
463
464 if (*data_size < sizeof(struct efi_variable_authentication_2))
465 goto err;
466
467 /* authentication data */
468 auth = *data;
469 if (*data_size < (sizeof(auth->time_stamp)
470 + auth->auth_info.hdr.dwLength))
471 goto err;
472
473 if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
474 goto err;
475
476 *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
477 *data_size -= (sizeof(auth->time_stamp)
478 + auth->auth_info.hdr.dwLength);
479
480 memcpy(&timestamp, &auth->time_stamp, sizeof(timestamp));
481 memset(&tm, 0, sizeof(tm));
482 tm.tm_year = timestamp.year;
483 tm.tm_mon = timestamp.month;
484 tm.tm_mday = timestamp.day;
485 tm.tm_hour = timestamp.hour;
486 tm.tm_min = timestamp.minute;
487 tm.tm_sec = timestamp.second;
488 new_time = rtc_mktime(&tm);
489
490 if (!efi_secure_boot_enabled()) {
491 /* finished checking */
492 *time = new_time;
493 return EFI_SUCCESS;
494 }
495
496 if (new_time <= *time)
497 goto err;
498
499 /* data to be digested */
500 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
501 if (!regs)
502 goto err;
503 regs->max = 5;
504 efi_image_region_add(regs, (uint8_t *)variable,
505 (uint8_t *)variable
506 + u16_strlen(variable) * sizeof(u16), 1);
507 efi_image_region_add(regs, (uint8_t *)vendor,
508 (uint8_t *)vendor + sizeof(*vendor), 1);
509 efi_image_region_add(regs, (uint8_t *)&given_attr,
510 (uint8_t *)&given_attr + sizeof(given_attr), 1);
511 efi_image_region_add(regs, (uint8_t *)&timestamp,
512 (uint8_t *)&timestamp + sizeof(timestamp), 1);
513 efi_image_region_add(regs, (uint8_t *)*data,
514 (uint8_t *)*data + *data_size, 1);
515
516 /* variable's signature list */
517 if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
518 goto err;
519 var_sig = efi_variable_parse_signature(auth->auth_info.cert_data,
520 auth->auth_info.hdr.dwLength
521 - sizeof(auth->auth_info));
522 if (IS_ERR(var_sig)) {
523 debug("Parsing variable's signature failed\n");
524 var_sig = NULL;
525 goto err;
526 }
527
528 /* signature database used for authentication */
529 if (u16_strcmp(variable, L"PK") == 0 ||
530 u16_strcmp(variable, L"KEK") == 0) {
531 /* with PK */
532 truststore = efi_sigstore_parse_sigdb(L"PK");
533 if (!truststore)
534 goto err;
535 } else if (u16_strcmp(variable, L"db") == 0 ||
536 u16_strcmp(variable, L"dbx") == 0) {
537 /* with PK and KEK */
538 truststore = efi_sigstore_parse_sigdb(L"KEK");
539 truststore2 = efi_sigstore_parse_sigdb(L"PK");
540
541 if (!truststore) {
542 if (!truststore2)
543 goto err;
544
545 truststore = truststore2;
546 truststore2 = NULL;
547 }
548 } else {
549 /* TODO: support private authenticated variables */
550 goto err;
551 }
552
553 /* verify signature */
554 if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) {
555 debug("Verified\n");
556 } else {
557 if (truststore2 &&
558 efi_signature_verify_with_sigdb(regs, var_sig,
559 truststore2, NULL)) {
560 debug("Verified\n");
561 } else {
562 debug("Verifying variable's signature failed\n");
563 goto err;
564 }
565 }
566
567 /* finished checking */
568 *time = rtc_mktime(&tm);
569 ret = EFI_SUCCESS;
570
571err:
572 efi_sigstore_free(truststore);
573 efi_sigstore_free(truststore2);
574 pkcs7_free_message(var_sig);
575 free(regs);
576
577 return ret;
578}
579#else
580static efi_status_t efi_variable_authenticate(u16 *variable,
581 const efi_guid_t *vendor,
582 efi_uintn_t *data_size,
583 const void **data, u32 given_attr,
584 u32 *env_attr, u64 *time)
585{
586 return EFI_SUCCESS;
587}
588#endif /* CONFIG_EFI_SECURE_BOOT */
589
590static
591efi_status_t EFIAPI efi_get_variable_common(u16 *variable_name,
592 const efi_guid_t *vendor,
593 u32 *attributes,
Heinrich Schuchardt87ab0b22020-04-18 12:31:17 +0200594 efi_uintn_t *data_size, void *data)
Rob Clark15f3d742017-09-13 18:05:37 -0400595{
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200596 char *native_name;
Rob Clark15f3d742017-09-13 18:05:37 -0400597 efi_status_t ret;
598 unsigned long in_size;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900599 const char *val = NULL, *s;
600 u64 time = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400601 u32 attr;
602
Rob Clark15f3d742017-09-13 18:05:37 -0400603 if (!variable_name || !vendor || !data_size)
604 return EFI_EXIT(EFI_INVALID_PARAMETER);
605
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200606 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400607 if (ret)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900608 return ret;
Rob Clark15f3d742017-09-13 18:05:37 -0400609
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200610 EFI_PRINT("get '%s'\n", native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400611
612 val = env_get(native_name);
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200613 free(native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400614 if (!val)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900615 return EFI_NOT_FOUND;
Rob Clark15f3d742017-09-13 18:05:37 -0400616
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900617 val = parse_attr(val, &attr, &time);
Rob Clark15f3d742017-09-13 18:05:37 -0400618
619 in_size = *data_size;
620
621 if ((s = prefix(val, "(blob)"))) {
Heinrich Schuchardt2d8aedc2019-01-18 12:31:54 +0100622 size_t len = strlen(s);
Rob Clark15f3d742017-09-13 18:05:37 -0400623
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700624 /* number of hexadecimal digits must be even */
625 if (len & 1)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900626 return EFI_DEVICE_ERROR;
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700627
Rob Clark15f3d742017-09-13 18:05:37 -0400628 /* two characters per byte: */
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700629 len /= 2;
Rob Clark15f3d742017-09-13 18:05:37 -0400630 *data_size = len;
631
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200632 if (in_size < len) {
633 ret = EFI_BUFFER_TOO_SMALL;
634 goto out;
635 }
Rob Clark15f3d742017-09-13 18:05:37 -0400636
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900637 if (!data) {
638 debug("Variable with no data shouldn't exist.\n");
639 return EFI_INVALID_PARAMETER;
640 }
Rob Clark15f3d742017-09-13 18:05:37 -0400641
Heinrich Schuchardt2d8aedc2019-01-18 12:31:54 +0100642 if (hex2bin(data, s, len))
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900643 return EFI_DEVICE_ERROR;
Rob Clark15f3d742017-09-13 18:05:37 -0400644
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200645 EFI_PRINT("got value: \"%s\"\n", s);
Rob Clark15f3d742017-09-13 18:05:37 -0400646 } else if ((s = prefix(val, "(utf8)"))) {
647 unsigned len = strlen(s) + 1;
648
649 *data_size = len;
650
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200651 if (in_size < len) {
652 ret = EFI_BUFFER_TOO_SMALL;
653 goto out;
654 }
Rob Clark15f3d742017-09-13 18:05:37 -0400655
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900656 if (!data) {
657 debug("Variable with no data shouldn't exist.\n");
658 return EFI_INVALID_PARAMETER;
659 }
Rob Clark15f3d742017-09-13 18:05:37 -0400660
661 memcpy(data, s, len);
662 ((char *)data)[len] = '\0';
663
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200664 EFI_PRINT("got value: \"%s\"\n", (char *)data);
Rob Clark15f3d742017-09-13 18:05:37 -0400665 } else {
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200666 EFI_PRINT("invalid value: '%s'\n", val);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900667 return EFI_DEVICE_ERROR;
Rob Clark15f3d742017-09-13 18:05:37 -0400668 }
669
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200670out:
Rob Clark15f3d742017-09-13 18:05:37 -0400671 if (attributes)
672 *attributes = attr & EFI_VARIABLE_MASK;
673
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900674 return ret;
675}
676
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900677/**
678 * efi_efi_get_variable() - retrieve value of a UEFI variable
679 *
680 * This function implements the GetVariable runtime service.
681 *
682 * See the Unified Extensible Firmware Interface (UEFI) specification for
683 * details.
684 *
685 * @variable_name: name of the variable
686 * @vendor: vendor GUID
687 * @attributes: attributes of the variable
688 * @data_size: size of the buffer to which the variable value is copied
689 * @data: buffer to which the variable value is copied
690 * Return: status code
691 */
692efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
693 const efi_guid_t *vendor, u32 *attributes,
694 efi_uintn_t *data_size, void *data)
695{
696 efi_status_t ret;
697
698 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
699 data_size, data);
700
Heinrich Schuchardt87ab0b22020-04-18 12:31:17 +0200701 ret = efi_get_variable_common(variable_name, vendor, attributes,
702 data_size, data);
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200703 return EFI_EXIT(ret);
Rob Clark15f3d742017-09-13 18:05:37 -0400704}
705
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100706static char *efi_variables_list;
707static char *efi_cur_variable;
708
709/**
710 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
711 * information
712 * @variable: whole data of u-boot variable (ie. name=value)
713 * @variable_name_size: size of variable_name buffer in byte
714 * @variable_name: name of uefi variable in u16, null-terminated
715 * @vendor: vendor's guid
716 * @attributes: attributes
717 *
718 * A uefi variable is encoded into a u-boot variable as described above.
719 * This function parses such a u-boot variable and retrieve uefi-related
720 * information into respective parameters. In return, variable_name_size
721 * is the size of variable name including NULL.
722 *
723 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200724 * the entire variable list has been returned,
725 * otherwise non-zero status code
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100726 */
727static efi_status_t parse_uboot_variable(char *variable,
728 efi_uintn_t *variable_name_size,
729 u16 *variable_name,
730 const efi_guid_t *vendor,
731 u32 *attributes)
732{
733 char *guid, *name, *end, c;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100734 size_t name_len;
735 efi_uintn_t old_variable_name_size;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900736 u64 time;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100737 u16 *p;
738
739 guid = strchr(variable, '_');
740 if (!guid)
741 return EFI_INVALID_PARAMETER;
742 guid++;
743 name = strchr(guid, '_');
744 if (!name)
745 return EFI_INVALID_PARAMETER;
746 name++;
747 end = strchr(name, '=');
748 if (!end)
749 return EFI_INVALID_PARAMETER;
750
751 name_len = end - name;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100752 old_variable_name_size = *variable_name_size;
753 *variable_name_size = sizeof(u16) * (name_len + 1);
754 if (old_variable_name_size < *variable_name_size)
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100755 return EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100756
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100757 end++; /* point to value */
758
759 /* variable name */
760 p = variable_name;
761 utf8_utf16_strncpy(&p, name, name_len);
762 variable_name[name_len] = 0;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100763
764 /* guid */
765 c = *(name - 1);
766 *(name - 1) = '\0'; /* guid need be null-terminated here */
767 uuid_str_to_bin(guid, (unsigned char *)vendor, UUID_STR_FORMAT_GUID);
768 *(name - 1) = c;
769
770 /* attributes */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900771 parse_attr(end, attributes, &time);
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100772
773 return EFI_SUCCESS;
774}
775
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100776/**
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100777 * efi_get_next_variable_name() - enumerate the current variable names
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200778 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100779 * @variable_name_size: size of variable_name buffer in byte
780 * @variable_name: name of uefi variable's name in u16
781 * @vendor: vendor's guid
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100782 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100783 * This function implements the GetNextVariableName service.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100784 *
785 * See the Unified Extensible Firmware Interface (UEFI) specification for
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200786 * details.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100787 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100788 * Return: status code
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100789 */
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200790efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
791 u16 *variable_name,
Heinrich Schuchardt82cd6c42020-03-22 18:28:20 +0100792 efi_guid_t *vendor)
Rob Clark15f3d742017-09-13 18:05:37 -0400793{
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100794 char *native_name, *variable;
795 ssize_t name_len, list_len;
796 char regex[256];
797 char * const regexlist[] = {regex};
798 u32 attributes;
799 int i;
800 efi_status_t ret;
801
Rob Clark238f88c2017-09-13 18:05:41 -0400802 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400803
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100804 if (!variable_name_size || !variable_name || !vendor)
Heinrich Schuchardt793a6252019-03-19 18:36:21 +0100805 return EFI_EXIT(EFI_INVALID_PARAMETER);
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100806
807 if (variable_name[0]) {
808 /* check null-terminated string */
809 for (i = 0; i < *variable_name_size; i++)
810 if (!variable_name[i])
811 break;
812 if (i >= *variable_name_size)
813 return EFI_EXIT(EFI_INVALID_PARAMETER);
814
815 /* search for the last-returned variable */
816 ret = efi_to_native(&native_name, variable_name, vendor);
817 if (ret)
818 return EFI_EXIT(ret);
819
820 name_len = strlen(native_name);
821 for (variable = efi_variables_list; variable && *variable;) {
822 if (!strncmp(variable, native_name, name_len) &&
823 variable[name_len] == '=')
824 break;
825
826 variable = strchr(variable, '\n');
827 if (variable)
828 variable++;
829 }
830
831 free(native_name);
832 if (!(variable && *variable))
833 return EFI_EXIT(EFI_INVALID_PARAMETER);
834
835 /* next variable */
836 variable = strchr(variable, '\n');
837 if (variable)
838 variable++;
839 if (!(variable && *variable))
840 return EFI_EXIT(EFI_NOT_FOUND);
841 } else {
842 /*
843 *new search: free a list used in the previous search
844 */
845 free(efi_variables_list);
846 efi_variables_list = NULL;
847 efi_cur_variable = NULL;
848
849 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
850 list_len = hexport_r(&env_htab, '\n',
851 H_MATCH_REGEX | H_MATCH_KEY,
852 &efi_variables_list, 0, 1, regexlist);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900853
Heinrich Schuchardtfd738482019-01-22 20:10:46 +0100854 if (list_len <= 1)
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100855 return EFI_EXIT(EFI_NOT_FOUND);
856
857 variable = efi_variables_list;
858 }
859
860 ret = parse_uboot_variable(variable, variable_name_size, variable_name,
861 vendor, &attributes);
862
863 return EFI_EXIT(ret);
Rob Clark15f3d742017-09-13 18:05:37 -0400864}
865
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200866static efi_status_t efi_set_variable_common(u16 *variable_name,
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900867 const efi_guid_t *vendor,
868 u32 attributes,
869 efi_uintn_t data_size,
870 const void *data,
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200871 bool ro_check)
Rob Clark15f3d742017-09-13 18:05:37 -0400872{
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900873 char *native_name = NULL, *old_data = NULL, *val = NULL, *s;
874 efi_uintn_t old_size;
875 bool append, delete;
876 u64 time = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400877 u32 attr;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900878 efi_status_t ret = EFI_SUCCESS;
Rob Clark15f3d742017-09-13 18:05:37 -0400879
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900880 debug("%s: set '%s'\n", __func__, native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400881
Heinrich Schuchardtadba3962019-06-12 23:28:42 +0200882 if (!variable_name || !*variable_name || !vendor ||
883 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900884 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200885 ret = EFI_INVALID_PARAMETER;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900886 goto err;
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200887 }
Rob Clark15f3d742017-09-13 18:05:37 -0400888
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200889 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400890 if (ret)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900891 goto err;
Rob Clark15f3d742017-09-13 18:05:37 -0400892
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900893 /* check if a variable exists */
894 old_size = 0;
895 attr = 0;
896 ret = EFI_CALL(efi_get_variable(variable_name, vendor, &attr,
897 &old_size, NULL));
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900898 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
899 attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
900 delete = !append && (!data_size || !attributes);
901
902 /* check attributes */
903 if (old_size) {
904 if (ro_check && (attr & READ_ONLY)) {
905 ret = EFI_WRITE_PROTECTED;
906 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900907 }
908
909 /* attributes won't be changed */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900910 if (!delete &&
911 ((ro_check && attr != attributes) ||
912 (!ro_check && ((attr & ~(u32)READ_ONLY)
913 != (attributes & ~(u32)READ_ONLY))))) {
AKASHI Takahiro186b5a42019-05-24 15:59:03 +0900914 ret = EFI_INVALID_PARAMETER;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900915 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900916 }
917 } else {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900918 if (delete || append) {
Heinrich Schuchardt8021bbe2019-09-26 21:40:18 +0200919 /*
920 * Trying to delete or to update a non-existent
921 * variable.
922 */
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900923 ret = EFI_NOT_FOUND;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900924 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900925 }
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900926 }
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900927
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900928 if (((!u16_strcmp(variable_name, L"PK") ||
929 !u16_strcmp(variable_name, L"KEK")) &&
930 !guidcmp(vendor, &efi_global_variable_guid)) ||
931 ((!u16_strcmp(variable_name, L"db") ||
932 !u16_strcmp(variable_name, L"dbx")) &&
933 !guidcmp(vendor, &efi_guid_image_security_database))) {
934 /* authentication is mandatory */
935 if (!(attributes &
936 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
937 debug("%ls: AUTHENTICATED_WRITE_ACCESS required\n",
938 variable_name);
939 ret = EFI_INVALID_PARAMETER;
940 goto err;
941 }
942 }
943
944 /* authenticate a variable */
945 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
946 if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
947 ret = EFI_INVALID_PARAMETER;
948 goto err;
949 }
950 if (attributes &
951 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
952 ret = efi_variable_authenticate(variable_name, vendor,
953 &data_size, &data,
954 attributes, &attr,
955 &time);
956 if (ret != EFI_SUCCESS)
957 goto err;
958
959 /* last chance to check for delete */
960 if (!data_size)
961 delete = true;
962 }
963 } else {
964 if (attributes &
965 (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
966 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
967 debug("Secure boot is not configured\n");
968 ret = EFI_INVALID_PARAMETER;
969 goto err;
970 }
971 }
972
973 /* delete a variable */
974 if (delete) {
975 /* !old_size case has been handled before */
976 val = NULL;
977 ret = EFI_SUCCESS;
978 goto out;
979 }
980
981 if (append) {
982 old_data = malloc(old_size);
983 if (!old_data) {
984 return EFI_OUT_OF_RESOURCES;
985 goto err;
986 }
987 ret = EFI_CALL(efi_get_variable(variable_name, vendor,
988 &attr, &old_size, old_data));
989 if (ret != EFI_SUCCESS)
990 goto err;
991 } else {
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900992 old_size = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400993 }
994
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900995 val = malloc(2 * old_size + 2 * data_size
996 + strlen("{ro,run,boot,nv,time=0123456701234567}(blob)")
997 + 1);
Heinrich Schuchardtbd095f52018-10-02 05:30:05 +0200998 if (!val) {
999 ret = EFI_OUT_OF_RESOURCES;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001000 goto err;
Heinrich Schuchardtbd095f52018-10-02 05:30:05 +02001001 }
Rob Clark15f3d742017-09-13 18:05:37 -04001002
1003 s = val;
1004
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001005 /*
1006 * store attributes
1007 */
1008 attributes &= (READ_ONLY |
1009 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro7f334332019-06-04 15:52:06 +09001010 EFI_VARIABLE_BOOTSERVICE_ACCESS |
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001011 EFI_VARIABLE_RUNTIME_ACCESS |
1012 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS);
Rob Clark15f3d742017-09-13 18:05:37 -04001013 s += sprintf(s, "{");
1014 while (attributes) {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001015 attr = 1 << (ffs(attributes) - 1);
Rob Clark15f3d742017-09-13 18:05:37 -04001016
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001017 if (attr == READ_ONLY) {
1018 s += sprintf(s, "ro");
1019 } else if (attr == EFI_VARIABLE_NON_VOLATILE) {
AKASHI Takahiro7f334332019-06-04 15:52:06 +09001020 s += sprintf(s, "nv");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001021 } else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) {
Rob Clark15f3d742017-09-13 18:05:37 -04001022 s += sprintf(s, "boot");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001023 } else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) {
Rob Clark15f3d742017-09-13 18:05:37 -04001024 s += sprintf(s, "run");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001025 } else if (attr ==
1026 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
1027 s += sprintf(s, "time=");
1028 s = bin2hex(s, (u8 *)&time, sizeof(time));
1029 }
Rob Clark15f3d742017-09-13 18:05:37 -04001030
1031 attributes &= ~attr;
1032 if (attributes)
1033 s += sprintf(s, ",");
1034 }
1035 s += sprintf(s, "}");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001036 s += sprintf(s, "(blob)");
AKASHI Takahiroe5023b72019-09-06 15:09:52 +09001037
Rob Clark15f3d742017-09-13 18:05:37 -04001038 /* store payload: */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001039 if (append)
1040 s = bin2hex(s, old_data, old_size);
Heinrich Schuchardt807899d2019-01-18 18:54:26 +01001041 s = bin2hex(s, data, data_size);
Rob Clark15f3d742017-09-13 18:05:37 -04001042 *s = '\0';
1043
Heinrich Schuchardt55111c32019-04-04 21:36:44 +02001044 EFI_PRINT("setting: %s=%s\n", native_name, val);
Rob Clark15f3d742017-09-13 18:05:37 -04001045
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001046out:
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001047 if (env_set(native_name, val)) {
Rob Clark15f3d742017-09-13 18:05:37 -04001048 ret = EFI_DEVICE_ERROR;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001049 } else {
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001050 bool vendor_keys_modified = false;
1051
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001052 if ((u16_strcmp(variable_name, L"PK") == 0 &&
1053 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1054 ret = efi_transfer_secure_state(
1055 (delete ? EFI_MODE_SETUP :
1056 EFI_MODE_USER));
1057 if (ret != EFI_SUCCESS)
1058 goto err;
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001059
1060 if (efi_secure_mode != EFI_MODE_SETUP)
1061 vendor_keys_modified = true;
1062 } else if ((u16_strcmp(variable_name, L"KEK") == 0 &&
1063 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1064 if (efi_secure_mode != EFI_MODE_SETUP)
1065 vendor_keys_modified = true;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001066 }
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001067
1068 /* update VendorKeys */
1069 if (vendor_keys_modified & efi_vendor_keys) {
1070 efi_vendor_keys = 0;
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +02001071 ret = efi_set_variable_common(
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001072 L"VendorKeys",
1073 &efi_global_variable_guid,
1074 EFI_VARIABLE_BOOTSERVICE_ACCESS
1075 | EFI_VARIABLE_RUNTIME_ACCESS
1076 | READ_ONLY,
1077 sizeof(efi_vendor_keys),
1078 &efi_vendor_keys,
1079 false);
1080 } else {
1081 ret = EFI_SUCCESS;
1082 }
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001083 }
Rob Clark15f3d742017-09-13 18:05:37 -04001084
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001085err:
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +02001086 free(native_name);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001087 free(old_data);
Rob Clark15f3d742017-09-13 18:05:37 -04001088 free(val);
1089
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001090 return ret;
1091}
1092
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001093/**
1094 * efi_set_variable() - set value of a UEFI variable
1095 *
1096 * This function implements the SetVariable runtime service.
1097 *
1098 * See the Unified Extensible Firmware Interface (UEFI) specification for
1099 * details.
1100 *
1101 * @variable_name: name of the variable
1102 * @vendor: vendor GUID
1103 * @attributes: attributes of the variable
1104 * @data_size: size of the buffer with the variable value
1105 * @data: buffer with the variable value
1106 * Return: status code
1107 */
1108efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
1109 const efi_guid_t *vendor, u32 attributes,
1110 efi_uintn_t data_size, const void *data)
1111{
1112 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
1113 data_size, data);
1114
1115 /* READ_ONLY bit is not part of API */
1116 attributes &= ~(u32)READ_ONLY;
1117
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +02001118 return EFI_EXIT(efi_set_variable_common(variable_name, vendor,
1119 attributes, data_size, data,
1120 true));
Rob Clark15f3d742017-09-13 18:05:37 -04001121}
Heinrich Schuchardt9b19dae2019-06-20 12:13:05 +02001122
1123/**
1124 * efi_query_variable_info() - get information about EFI variables
1125 *
1126 * This function implements the QueryVariableInfo() runtime service.
1127 *
1128 * See the Unified Extensible Firmware Interface (UEFI) specification for
1129 * details.
1130 *
1131 * @attributes: bitmask to select variables to be
1132 * queried
1133 * @maximum_variable_storage_size: maximum size of storage area for the
1134 * selected variable types
1135 * @remaining_variable_storage_size: remaining size of storage are for the
1136 * selected variable types
1137 * @maximum_variable_size: maximum size of a variable of the
1138 * selected type
1139 * Returns: status code
1140 */
1141efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
1142 u32 attributes,
1143 u64 *maximum_variable_storage_size,
1144 u64 *remaining_variable_storage_size,
1145 u64 *maximum_variable_size)
1146{
1147 return EFI_UNSUPPORTED;
1148}
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001149
1150/**
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001151 * efi_get_variable_runtime() - runtime implementation of GetVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001152 *
1153 * @variable_name: name of the variable
1154 * @vendor: vendor GUID
1155 * @attributes: attributes of the variable
1156 * @data_size: size of the buffer to which the variable value is copied
1157 * @data: buffer to which the variable value is copied
1158 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001159 */
1160static efi_status_t __efi_runtime EFIAPI
1161efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1162 u32 *attributes, efi_uintn_t *data_size, void *data)
1163{
1164 return EFI_UNSUPPORTED;
1165}
1166
1167/**
1168 * efi_get_next_variable_name_runtime() - runtime implementation of
1169 * GetNextVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001170 *
1171 * @variable_name_size: size of variable_name buffer in byte
1172 * @variable_name: name of uefi variable's name in u16
1173 * @vendor: vendor's guid
1174 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001175 */
1176static efi_status_t __efi_runtime EFIAPI
1177efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
Heinrich Schuchardt82cd6c42020-03-22 18:28:20 +01001178 u16 *variable_name, efi_guid_t *vendor)
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001179{
1180 return EFI_UNSUPPORTED;
1181}
1182
1183/**
1184 * efi_set_variable_runtime() - runtime implementation of SetVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001185 *
1186 * @variable_name: name of the variable
1187 * @vendor: vendor GUID
1188 * @attributes: attributes of the variable
1189 * @data_size: size of the buffer with the variable value
1190 * @data: buffer with the variable value
1191 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001192 */
1193static efi_status_t __efi_runtime EFIAPI
1194efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1195 u32 attributes, efi_uintn_t data_size,
1196 const void *data)
1197{
1198 return EFI_UNSUPPORTED;
1199}
1200
1201/**
1202 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
1203 */
1204void efi_variables_boot_exit_notify(void)
1205{
1206 efi_runtime_services.get_variable = efi_get_variable_runtime;
1207 efi_runtime_services.get_next_variable_name =
1208 efi_get_next_variable_name_runtime;
1209 efi_runtime_services.set_variable = efi_set_variable_runtime;
1210 efi_update_table_header_crc32(&efi_runtime_services.hdr);
1211}
1212
1213/**
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001214 * efi_init_variables() - initialize variable services
1215 *
1216 * Return: status code
1217 */
1218efi_status_t efi_init_variables(void)
1219{
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001220 efi_status_t ret;
1221
1222 ret = efi_init_secure_state();
1223
1224 return ret;
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001225}