blob: 85db96bd147b0a6c9551a4b731dbb525a11518d5 [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 Glassed38aef2020-05-10 11:40:03 -060010#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060011#include <env_internal.h>
Heinrich Schuchardt147d5d32019-10-26 23:53:48 +020012#include <hexdump.h>
13#include <malloc.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090014#include <rtc.h>
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +010015#include <search.h>
Simon Glass6f044982020-05-10 11:39:52 -060016#include <uuid.h>
AKASHI Takahiro6ec67672020-04-21 09:38:17 +090017#include <crypto/pkcs7_parser.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090018#include <linux/compat.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070019#include <u-boot/crc.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090020
AKASHI Takahiroc78dc192020-04-14 11:51:42 +090021enum efi_secure_mode {
22 EFI_MODE_SETUP,
23 EFI_MODE_USER,
24 EFI_MODE_AUDIT,
25 EFI_MODE_DEPLOYED,
26};
27
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090028const efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID;
29static bool efi_secure_boot;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +090030static int efi_secure_mode;
AKASHI Takahiro94b139a2020-04-14 11:51:43 +090031static u8 efi_vendor_keys;
Rob Clark15f3d742017-09-13 18:05:37 -040032
33#define READ_ONLY BIT(31)
34
Heinrich Schuchardtda0a7912020-05-06 01:51:04 +020035static efi_status_t efi_get_variable_common(u16 *variable_name,
36 const efi_guid_t *vendor,
37 u32 *attributes,
38 efi_uintn_t *data_size, void *data);
39
40static efi_status_t efi_set_variable_common(u16 *variable_name,
41 const efi_guid_t *vendor,
42 u32 attributes,
43 efi_uintn_t data_size,
44 const void *data,
45 bool ro_check);
46
Rob Clark15f3d742017-09-13 18:05:37 -040047/*
48 * Mapping between EFI variables and u-boot variables:
49 *
50 * efi_$guid_$varname = {attributes}(type)value
51 *
52 * For example:
53 *
54 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
55 * "{ro,boot,run}(blob)0000000000000000"
56 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
57 * "(blob)00010000"
58 *
59 * The attributes are a comma separated list of these possible
60 * attributes:
61 *
62 * + ro - read-only
63 * + boot - boot-services access
64 * + run - runtime access
65 *
66 * NOTE: with current implementation, no variables are available after
67 * ExitBootServices, and all are persisted (if possible).
68 *
69 * If not specified, the attributes default to "{boot}".
70 *
71 * The required type is one of:
72 *
73 * + utf8 - raw utf8 string
74 * + blob - arbitrary length hex string
75 *
76 * Maybe a utf16 type would be useful to for a string value to be auto
77 * converted to utf16?
78 */
79
Heinrich Schuchardt44389cb2018-09-23 04:08:09 +020080#define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
Rob Clark15f3d742017-09-13 18:05:37 -040081
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +010082/**
83 * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
84 * variable name
85 *
86 * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
87 * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
88 * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
89 *
90 * @native: pointer to pointer to U-Boot variable name
91 * @variable_name: UEFI variable name
92 * @vendor: vendor GUID
93 * Return: status code
94 */
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020095static efi_status_t efi_to_native(char **native, const u16 *variable_name,
Heinrich Schuchardte06ae192018-12-30 20:53:51 +010096 const efi_guid_t *vendor)
Rob Clark15f3d742017-09-13 18:05:37 -040097{
98 size_t len;
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020099 char *pos;
Rob Clark15f3d742017-09-13 18:05:37 -0400100
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200101 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
102 *native = malloc(len);
103 if (!*native)
104 return EFI_OUT_OF_RESOURCES;
Rob Clark15f3d742017-09-13 18:05:37 -0400105
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200106 pos = *native;
107 pos += sprintf(pos, "efi_%pUl_", vendor);
108 utf16_utf8_strcpy(&pos, variable_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400109
110 return EFI_SUCCESS;
111}
112
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100113/**
114 * prefix() - skip over prefix
115 *
116 * Skip over a prefix string.
117 *
118 * @str: string with prefix
119 * @prefix: prefix string
120 * Return: string without prefix, or NULL if prefix not found
121 */
Rob Clark15f3d742017-09-13 18:05:37 -0400122static const char *prefix(const char *str, const char *prefix)
123{
124 size_t n = strlen(prefix);
125 if (!strncmp(prefix, str, n))
126 return str + n;
127 return NULL;
128}
129
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100130/**
131 * parse_attr() - decode attributes part of variable value
132 *
133 * Convert the string encoded attributes of a UEFI variable to a bit mask.
134 * TODO: Several attributes are not supported.
135 *
136 * @str: value of U-Boot variable
137 * @attrp: pointer to UEFI attributes
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900138 * @timep: pointer to time attribute
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100139 * Return: pointer to remainder of U-Boot variable value
140 */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900141static const char *parse_attr(const char *str, u32 *attrp, u64 *timep)
Rob Clark15f3d742017-09-13 18:05:37 -0400142{
143 u32 attr = 0;
144 char sep = '{';
145
146 if (*str != '{') {
147 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
148 return str;
149 }
150
151 while (*str == sep) {
152 const char *s;
153
154 str++;
155
156 if ((s = prefix(str, "ro"))) {
157 attr |= READ_ONLY;
AKASHI Takahiro7f334332019-06-04 15:52:06 +0900158 } else if ((s = prefix(str, "nv"))) {
159 attr |= EFI_VARIABLE_NON_VOLATILE;
Rob Clark15f3d742017-09-13 18:05:37 -0400160 } else if ((s = prefix(str, "boot"))) {
161 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
162 } else if ((s = prefix(str, "run"))) {
163 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900164 } else if ((s = prefix(str, "time="))) {
165 attr |= EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
166 hex2bin((u8 *)timep, s, sizeof(*timep));
167 s += sizeof(*timep) * 2;
168 } else if (*str == '}') {
169 break;
Rob Clark15f3d742017-09-13 18:05:37 -0400170 } else {
171 printf("invalid attribute: %s\n", str);
172 break;
173 }
174
175 str = s;
176 sep = ',';
177 }
178
179 str++;
180
181 *attrp = attr;
182
183 return str;
184}
185
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900186/**
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900187 * efi_set_secure_state - modify secure boot state variables
188 * @sec_boot: value of SecureBoot
189 * @setup_mode: value of SetupMode
190 * @audit_mode: value of AuditMode
191 * @deployed_mode: value of DeployedMode
192 *
193 * Modify secure boot stat-related variables as indicated.
194 *
195 * Return: status code
196 */
197static efi_status_t efi_set_secure_state(int sec_boot, int setup_mode,
198 int audit_mode, int deployed_mode)
199{
200 u32 attributes;
201 efi_status_t ret;
202
203 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
204 EFI_VARIABLE_RUNTIME_ACCESS |
205 READ_ONLY;
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200206 ret = efi_set_variable_common(L"SecureBoot", &efi_global_variable_guid,
207 attributes, sizeof(sec_boot), &sec_boot,
208 false);
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900209 if (ret != EFI_SUCCESS)
210 goto err;
211
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200212 ret = efi_set_variable_common(L"SetupMode", &efi_global_variable_guid,
213 attributes, sizeof(setup_mode),
214 &setup_mode, false);
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900215 if (ret != EFI_SUCCESS)
216 goto err;
217
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200218 ret = efi_set_variable_common(L"AuditMode", &efi_global_variable_guid,
219 attributes, sizeof(audit_mode),
220 &audit_mode, false);
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900221 if (ret != EFI_SUCCESS)
222 goto err;
223
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200224 ret = efi_set_variable_common(L"DeployedMode",
225 &efi_global_variable_guid, attributes,
226 sizeof(deployed_mode), &deployed_mode,
227 false);
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900228err:
229 return ret;
230}
231
232/**
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900233 * efi_transfer_secure_state - handle a secure boot state transition
234 * @mode: new state
235 *
236 * Depending on @mode, secure boot related variables are updated.
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200237 * Those variables are *read-only* for users, efi_set_variable_common()
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900238 * is called here.
239 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +0200240 * Return: status code
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900241 */
242static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
243{
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900244 efi_status_t ret;
245
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900246 debug("Switching secure state from %d to %d\n", efi_secure_mode, mode);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900247
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900248 if (mode == EFI_MODE_DEPLOYED) {
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900249 ret = efi_set_secure_state(1, 0, 0, 1);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900250 if (ret != EFI_SUCCESS)
251 goto err;
252
253 efi_secure_boot = true;
254 } else if (mode == EFI_MODE_AUDIT) {
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200255 ret = efi_set_variable_common(L"PK", &efi_global_variable_guid,
256 EFI_VARIABLE_BOOTSERVICE_ACCESS |
257 EFI_VARIABLE_RUNTIME_ACCESS,
258 0, NULL, false);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900259 if (ret != EFI_SUCCESS)
260 goto err;
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900261
262 ret = efi_set_secure_state(0, 1, 1, 0);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900263 if (ret != EFI_SUCCESS)
264 goto err;
265
266 efi_secure_boot = true;
267 } else if (mode == EFI_MODE_USER) {
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900268 ret = efi_set_secure_state(1, 0, 0, 0);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900269 if (ret != EFI_SUCCESS)
270 goto err;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900271
272 efi_secure_boot = true;
273 } else if (mode == EFI_MODE_SETUP) {
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900274 ret = efi_set_secure_state(0, 1, 0, 0);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900275 if (ret != EFI_SUCCESS)
276 goto err;
277 } else {
278 return EFI_INVALID_PARAMETER;
279 }
280
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900281 efi_secure_mode = mode;
282
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900283 return EFI_SUCCESS;
284
285err:
286 /* TODO: What action should be taken here? */
287 printf("ERROR: Secure state transition failed\n");
288 return ret;
289}
290
291/**
292 * efi_init_secure_state - initialize secure boot state
293 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +0200294 * Return: status code
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900295 */
296static efi_status_t efi_init_secure_state(void)
297{
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900298 enum efi_secure_mode mode;
299 efi_uintn_t size;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900300 efi_status_t ret;
301
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900302 /*
303 * TODO:
304 * Since there is currently no "platform-specific" installation
305 * method of Platform Key, we can't say if VendorKeys is 0 or 1
306 * precisely.
307 */
308
309 size = 0;
Heinrich Schuchardtda0a7912020-05-06 01:51:04 +0200310 ret = efi_get_variable_common(L"PK", &efi_global_variable_guid,
311 NULL, &size, NULL);
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900312 if (ret == EFI_BUFFER_TOO_SMALL) {
313 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT))
314 mode = EFI_MODE_USER;
315 else
316 mode = EFI_MODE_SETUP;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900317
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900318 efi_vendor_keys = 0;
319 } else if (ret == EFI_NOT_FOUND) {
320 mode = EFI_MODE_SETUP;
321 efi_vendor_keys = 1;
322 } else {
323 goto err;
324 }
325
326 ret = efi_transfer_secure_state(mode);
327 if (ret == EFI_SUCCESS)
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200328 ret = efi_set_variable_common(L"VendorKeys",
329 &efi_global_variable_guid,
330 EFI_VARIABLE_BOOTSERVICE_ACCESS |
331 EFI_VARIABLE_RUNTIME_ACCESS |
332 READ_ONLY,
333 sizeof(efi_vendor_keys),
334 &efi_vendor_keys, false);
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900335
336err:
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900337 return ret;
338}
339
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100340/**
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900341 * efi_secure_boot_enabled - return if secure boot is enabled or not
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100342 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900343 * Return: true if enabled, false if disabled
344 */
345bool efi_secure_boot_enabled(void)
346{
347 return efi_secure_boot;
348}
349
350#ifdef CONFIG_EFI_SECURE_BOOT
351static u8 pkcs7_hdr[] = {
352 /* SEQUENCE */
353 0x30, 0x82, 0x05, 0xc7,
354 /* OID: pkcs7-signedData */
355 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
356 /* Context Structured? */
357 0xa0, 0x82, 0x05, 0xb8,
358};
359
360/**
361 * efi_variable_parse_signature - parse a signature in variable
362 * @buf: Pointer to variable's value
363 * @buflen: Length of @buf
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100364 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900365 * Parse a signature embedded in variable's value and instantiate
366 * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
367 * pkcs7's signedData, some header needed be prepended for correctly
368 * parsing authentication data, particularly for variable's.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100369 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900370 * Return: Pointer to pkcs7_message structure on success, NULL on error
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100371 */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900372static struct pkcs7_message *efi_variable_parse_signature(const void *buf,
373 size_t buflen)
374{
375 u8 *ebuf;
376 size_t ebuflen, len;
377 struct pkcs7_message *msg;
378
379 /*
380 * This is the best assumption to check if the binary is
381 * already in a form of pkcs7's signedData.
382 */
383 if (buflen > sizeof(pkcs7_hdr) &&
384 !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
385 msg = pkcs7_parse_message(buf, buflen);
386 goto out;
387 }
388
389 /*
390 * Otherwise, we should add a dummy prefix sequence for pkcs7
391 * message parser to be able to process.
392 * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
393 * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
394 * TODO:
395 * The header should be composed in a more refined manner.
396 */
397 debug("Makeshift prefix added to authentication data\n");
398 ebuflen = sizeof(pkcs7_hdr) + buflen;
399 if (ebuflen <= 0x7f) {
400 debug("Data is too short\n");
401 return NULL;
402 }
403
404 ebuf = malloc(ebuflen);
405 if (!ebuf) {
406 debug("Out of memory\n");
407 return NULL;
408 }
409
410 memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
411 memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
412 len = ebuflen - 4;
413 ebuf[2] = (len >> 8) & 0xff;
414 ebuf[3] = len & 0xff;
415 len = ebuflen - 0x13;
416 ebuf[0x11] = (len >> 8) & 0xff;
417 ebuf[0x12] = len & 0xff;
418
419 msg = pkcs7_parse_message(ebuf, ebuflen);
420
421 free(ebuf);
422
423out:
424 if (IS_ERR(msg))
425 return NULL;
426
427 return msg;
428}
429
430/**
431 * efi_variable_authenticate - authenticate a variable
432 * @variable: Variable name in u16
433 * @vendor: Guid of variable
434 * @data_size: Size of @data
435 * @data: Pointer to variable's value
436 * @given_attr: Attributes to be given at SetVariable()
437 * @env_attr: Attributes that an existing variable holds
438 * @time: signed time that an existing variable holds
439 *
440 * Called by efi_set_variable() to verify that the input is correct.
441 * Will replace the given data pointer with another that points to
442 * the actual data to store in the internal memory.
443 * On success, @data and @data_size will be replaced with variable's
444 * actual data, excluding authentication data, and its size, and variable's
445 * attributes and signed time will also be returned in @env_attr and @time,
446 * respectively.
447 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +0200448 * Return: status code
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900449 */
450static efi_status_t efi_variable_authenticate(u16 *variable,
451 const efi_guid_t *vendor,
452 efi_uintn_t *data_size,
453 const void **data, u32 given_attr,
454 u32 *env_attr, u64 *time)
455{
456 const struct efi_variable_authentication_2 *auth;
457 struct efi_signature_store *truststore, *truststore2;
458 struct pkcs7_message *var_sig;
459 struct efi_image_regions *regs;
460 struct efi_time timestamp;
461 struct rtc_time tm;
462 u64 new_time;
463 efi_status_t ret;
464
465 var_sig = NULL;
466 truststore = NULL;
467 truststore2 = NULL;
468 regs = NULL;
469 ret = EFI_SECURITY_VIOLATION;
470
471 if (*data_size < sizeof(struct efi_variable_authentication_2))
472 goto err;
473
474 /* authentication data */
475 auth = *data;
476 if (*data_size < (sizeof(auth->time_stamp)
477 + auth->auth_info.hdr.dwLength))
478 goto err;
479
480 if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
481 goto err;
482
483 *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
484 *data_size -= (sizeof(auth->time_stamp)
485 + auth->auth_info.hdr.dwLength);
486
487 memcpy(&timestamp, &auth->time_stamp, sizeof(timestamp));
488 memset(&tm, 0, sizeof(tm));
489 tm.tm_year = timestamp.year;
490 tm.tm_mon = timestamp.month;
491 tm.tm_mday = timestamp.day;
492 tm.tm_hour = timestamp.hour;
493 tm.tm_min = timestamp.minute;
494 tm.tm_sec = timestamp.second;
495 new_time = rtc_mktime(&tm);
496
497 if (!efi_secure_boot_enabled()) {
498 /* finished checking */
499 *time = new_time;
500 return EFI_SUCCESS;
501 }
502
503 if (new_time <= *time)
504 goto err;
505
506 /* data to be digested */
507 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
508 if (!regs)
509 goto err;
510 regs->max = 5;
511 efi_image_region_add(regs, (uint8_t *)variable,
512 (uint8_t *)variable
513 + u16_strlen(variable) * sizeof(u16), 1);
514 efi_image_region_add(regs, (uint8_t *)vendor,
515 (uint8_t *)vendor + sizeof(*vendor), 1);
516 efi_image_region_add(regs, (uint8_t *)&given_attr,
517 (uint8_t *)&given_attr + sizeof(given_attr), 1);
518 efi_image_region_add(regs, (uint8_t *)&timestamp,
519 (uint8_t *)&timestamp + sizeof(timestamp), 1);
520 efi_image_region_add(regs, (uint8_t *)*data,
521 (uint8_t *)*data + *data_size, 1);
522
523 /* variable's signature list */
524 if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
525 goto err;
526 var_sig = efi_variable_parse_signature(auth->auth_info.cert_data,
527 auth->auth_info.hdr.dwLength
528 - sizeof(auth->auth_info));
Patrick Wildtcd8a55b2020-05-07 02:13:18 +0200529 if (!var_sig) {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900530 debug("Parsing variable's signature failed\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900531 goto err;
532 }
533
534 /* signature database used for authentication */
535 if (u16_strcmp(variable, L"PK") == 0 ||
536 u16_strcmp(variable, L"KEK") == 0) {
537 /* with PK */
538 truststore = efi_sigstore_parse_sigdb(L"PK");
539 if (!truststore)
540 goto err;
541 } else if (u16_strcmp(variable, L"db") == 0 ||
542 u16_strcmp(variable, L"dbx") == 0) {
543 /* with PK and KEK */
544 truststore = efi_sigstore_parse_sigdb(L"KEK");
545 truststore2 = efi_sigstore_parse_sigdb(L"PK");
546
547 if (!truststore) {
548 if (!truststore2)
549 goto err;
550
551 truststore = truststore2;
552 truststore2 = NULL;
553 }
554 } else {
555 /* TODO: support private authenticated variables */
556 goto err;
557 }
558
559 /* verify signature */
560 if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) {
561 debug("Verified\n");
562 } else {
563 if (truststore2 &&
564 efi_signature_verify_with_sigdb(regs, var_sig,
565 truststore2, NULL)) {
566 debug("Verified\n");
567 } else {
568 debug("Verifying variable's signature failed\n");
569 goto err;
570 }
571 }
572
573 /* finished checking */
574 *time = rtc_mktime(&tm);
575 ret = EFI_SUCCESS;
576
577err:
578 efi_sigstore_free(truststore);
579 efi_sigstore_free(truststore2);
580 pkcs7_free_message(var_sig);
581 free(regs);
582
583 return ret;
584}
585#else
586static efi_status_t efi_variable_authenticate(u16 *variable,
587 const efi_guid_t *vendor,
588 efi_uintn_t *data_size,
589 const void **data, u32 given_attr,
590 u32 *env_attr, u64 *time)
591{
592 return EFI_SUCCESS;
593}
594#endif /* CONFIG_EFI_SECURE_BOOT */
595
Heinrich Schuchardtda0a7912020-05-06 01:51:04 +0200596static efi_status_t efi_get_variable_common(u16 *variable_name,
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900597 const efi_guid_t *vendor,
598 u32 *attributes,
Heinrich Schuchardt87ab0b22020-04-18 12:31:17 +0200599 efi_uintn_t *data_size, void *data)
Rob Clark15f3d742017-09-13 18:05:37 -0400600{
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200601 char *native_name;
Rob Clark15f3d742017-09-13 18:05:37 -0400602 efi_status_t ret;
603 unsigned long in_size;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900604 const char *val = NULL, *s;
605 u64 time = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400606 u32 attr;
607
Rob Clark15f3d742017-09-13 18:05:37 -0400608 if (!variable_name || !vendor || !data_size)
609 return EFI_EXIT(EFI_INVALID_PARAMETER);
610
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200611 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400612 if (ret)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900613 return ret;
Rob Clark15f3d742017-09-13 18:05:37 -0400614
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200615 EFI_PRINT("get '%s'\n", native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400616
617 val = env_get(native_name);
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200618 free(native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400619 if (!val)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900620 return EFI_NOT_FOUND;
Rob Clark15f3d742017-09-13 18:05:37 -0400621
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900622 val = parse_attr(val, &attr, &time);
Rob Clark15f3d742017-09-13 18:05:37 -0400623
624 in_size = *data_size;
625
626 if ((s = prefix(val, "(blob)"))) {
Heinrich Schuchardt2d8aedc2019-01-18 12:31:54 +0100627 size_t len = strlen(s);
Rob Clark15f3d742017-09-13 18:05:37 -0400628
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700629 /* number of hexadecimal digits must be even */
630 if (len & 1)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900631 return EFI_DEVICE_ERROR;
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700632
Rob Clark15f3d742017-09-13 18:05:37 -0400633 /* two characters per byte: */
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700634 len /= 2;
Rob Clark15f3d742017-09-13 18:05:37 -0400635 *data_size = len;
636
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200637 if (in_size < len) {
638 ret = EFI_BUFFER_TOO_SMALL;
639 goto out;
640 }
Rob Clark15f3d742017-09-13 18:05:37 -0400641
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900642 if (!data) {
643 debug("Variable with no data shouldn't exist.\n");
644 return EFI_INVALID_PARAMETER;
645 }
Rob Clark15f3d742017-09-13 18:05:37 -0400646
Heinrich Schuchardt2d8aedc2019-01-18 12:31:54 +0100647 if (hex2bin(data, s, len))
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900648 return EFI_DEVICE_ERROR;
Rob Clark15f3d742017-09-13 18:05:37 -0400649
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200650 EFI_PRINT("got value: \"%s\"\n", s);
Rob Clark15f3d742017-09-13 18:05:37 -0400651 } else if ((s = prefix(val, "(utf8)"))) {
652 unsigned len = strlen(s) + 1;
653
654 *data_size = len;
655
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200656 if (in_size < len) {
657 ret = EFI_BUFFER_TOO_SMALL;
658 goto out;
659 }
Rob Clark15f3d742017-09-13 18:05:37 -0400660
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900661 if (!data) {
662 debug("Variable with no data shouldn't exist.\n");
663 return EFI_INVALID_PARAMETER;
664 }
Rob Clark15f3d742017-09-13 18:05:37 -0400665
666 memcpy(data, s, len);
667 ((char *)data)[len] = '\0';
668
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200669 EFI_PRINT("got value: \"%s\"\n", (char *)data);
Rob Clark15f3d742017-09-13 18:05:37 -0400670 } else {
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200671 EFI_PRINT("invalid value: '%s'\n", val);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900672 return EFI_DEVICE_ERROR;
Rob Clark15f3d742017-09-13 18:05:37 -0400673 }
674
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200675out:
Rob Clark15f3d742017-09-13 18:05:37 -0400676 if (attributes)
677 *attributes = attr & EFI_VARIABLE_MASK;
678
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900679 return ret;
680}
681
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900682/**
683 * efi_efi_get_variable() - retrieve value of a UEFI variable
684 *
685 * This function implements the GetVariable runtime service.
686 *
687 * See the Unified Extensible Firmware Interface (UEFI) specification for
688 * details.
689 *
690 * @variable_name: name of the variable
691 * @vendor: vendor GUID
692 * @attributes: attributes of the variable
693 * @data_size: size of the buffer to which the variable value is copied
694 * @data: buffer to which the variable value is copied
695 * Return: status code
696 */
697efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
698 const efi_guid_t *vendor, u32 *attributes,
699 efi_uintn_t *data_size, void *data)
700{
701 efi_status_t ret;
702
703 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
704 data_size, data);
705
Heinrich Schuchardt87ab0b22020-04-18 12:31:17 +0200706 ret = efi_get_variable_common(variable_name, vendor, attributes,
707 data_size, data);
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200708 return EFI_EXIT(ret);
Rob Clark15f3d742017-09-13 18:05:37 -0400709}
710
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100711static char *efi_variables_list;
712static char *efi_cur_variable;
713
714/**
715 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
716 * information
717 * @variable: whole data of u-boot variable (ie. name=value)
718 * @variable_name_size: size of variable_name buffer in byte
719 * @variable_name: name of uefi variable in u16, null-terminated
720 * @vendor: vendor's guid
721 * @attributes: attributes
722 *
723 * A uefi variable is encoded into a u-boot variable as described above.
724 * This function parses such a u-boot variable and retrieve uefi-related
725 * information into respective parameters. In return, variable_name_size
726 * is the size of variable name including NULL.
727 *
728 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200729 * the entire variable list has been returned,
730 * otherwise non-zero status code
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100731 */
732static efi_status_t parse_uboot_variable(char *variable,
733 efi_uintn_t *variable_name_size,
734 u16 *variable_name,
735 const efi_guid_t *vendor,
736 u32 *attributes)
737{
738 char *guid, *name, *end, c;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100739 size_t name_len;
740 efi_uintn_t old_variable_name_size;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900741 u64 time;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100742 u16 *p;
743
744 guid = strchr(variable, '_');
745 if (!guid)
746 return EFI_INVALID_PARAMETER;
747 guid++;
748 name = strchr(guid, '_');
749 if (!name)
750 return EFI_INVALID_PARAMETER;
751 name++;
752 end = strchr(name, '=');
753 if (!end)
754 return EFI_INVALID_PARAMETER;
755
756 name_len = end - name;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100757 old_variable_name_size = *variable_name_size;
758 *variable_name_size = sizeof(u16) * (name_len + 1);
759 if (old_variable_name_size < *variable_name_size)
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100760 return EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100761
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100762 end++; /* point to value */
763
764 /* variable name */
765 p = variable_name;
766 utf8_utf16_strncpy(&p, name, name_len);
767 variable_name[name_len] = 0;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100768
769 /* guid */
770 c = *(name - 1);
771 *(name - 1) = '\0'; /* guid need be null-terminated here */
AKASHI Takahiro4854f782020-05-08 14:51:21 +0900772 if (uuid_str_to_bin(guid, (unsigned char *)vendor,
773 UUID_STR_FORMAT_GUID))
774 /* The only error would be EINVAL. */
775 return EFI_INVALID_PARAMETER;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100776 *(name - 1) = c;
777
778 /* attributes */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900779 parse_attr(end, attributes, &time);
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100780
781 return EFI_SUCCESS;
782}
783
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100784/**
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100785 * efi_get_next_variable_name() - enumerate the current variable names
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200786 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100787 * @variable_name_size: size of variable_name buffer in byte
788 * @variable_name: name of uefi variable's name in u16
789 * @vendor: vendor's guid
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100790 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100791 * This function implements the GetNextVariableName service.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100792 *
793 * See the Unified Extensible Firmware Interface (UEFI) specification for
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200794 * details.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100795 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100796 * Return: status code
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100797 */
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200798efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
799 u16 *variable_name,
Heinrich Schuchardt82cd6c42020-03-22 18:28:20 +0100800 efi_guid_t *vendor)
Rob Clark15f3d742017-09-13 18:05:37 -0400801{
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100802 char *native_name, *variable;
803 ssize_t name_len, list_len;
804 char regex[256];
805 char * const regexlist[] = {regex};
806 u32 attributes;
807 int i;
808 efi_status_t ret;
809
Rob Clark238f88c2017-09-13 18:05:41 -0400810 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400811
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100812 if (!variable_name_size || !variable_name || !vendor)
Heinrich Schuchardt793a6252019-03-19 18:36:21 +0100813 return EFI_EXIT(EFI_INVALID_PARAMETER);
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100814
815 if (variable_name[0]) {
816 /* check null-terminated string */
817 for (i = 0; i < *variable_name_size; i++)
818 if (!variable_name[i])
819 break;
820 if (i >= *variable_name_size)
821 return EFI_EXIT(EFI_INVALID_PARAMETER);
822
823 /* search for the last-returned variable */
824 ret = efi_to_native(&native_name, variable_name, vendor);
825 if (ret)
826 return EFI_EXIT(ret);
827
828 name_len = strlen(native_name);
829 for (variable = efi_variables_list; variable && *variable;) {
830 if (!strncmp(variable, native_name, name_len) &&
831 variable[name_len] == '=')
832 break;
833
834 variable = strchr(variable, '\n');
835 if (variable)
836 variable++;
837 }
838
839 free(native_name);
840 if (!(variable && *variable))
841 return EFI_EXIT(EFI_INVALID_PARAMETER);
842
843 /* next variable */
844 variable = strchr(variable, '\n');
845 if (variable)
846 variable++;
847 if (!(variable && *variable))
848 return EFI_EXIT(EFI_NOT_FOUND);
849 } else {
850 /*
851 *new search: free a list used in the previous search
852 */
853 free(efi_variables_list);
854 efi_variables_list = NULL;
855 efi_cur_variable = NULL;
856
857 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
858 list_len = hexport_r(&env_htab, '\n',
859 H_MATCH_REGEX | H_MATCH_KEY,
860 &efi_variables_list, 0, 1, regexlist);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900861
Heinrich Schuchardtfd738482019-01-22 20:10:46 +0100862 if (list_len <= 1)
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100863 return EFI_EXIT(EFI_NOT_FOUND);
864
865 variable = efi_variables_list;
866 }
867
868 ret = parse_uboot_variable(variable, variable_name_size, variable_name,
869 vendor, &attributes);
870
871 return EFI_EXIT(ret);
Rob Clark15f3d742017-09-13 18:05:37 -0400872}
873
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200874static efi_status_t efi_set_variable_common(u16 *variable_name,
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900875 const efi_guid_t *vendor,
876 u32 attributes,
877 efi_uintn_t data_size,
878 const void *data,
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200879 bool ro_check)
Rob Clark15f3d742017-09-13 18:05:37 -0400880{
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900881 char *native_name = NULL, *old_data = NULL, *val = NULL, *s;
882 efi_uintn_t old_size;
883 bool append, delete;
884 u64 time = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400885 u32 attr;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900886 efi_status_t ret = EFI_SUCCESS;
Rob Clark15f3d742017-09-13 18:05:37 -0400887
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900888 debug("%s: set '%s'\n", __func__, native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400889
Heinrich Schuchardtadba3962019-06-12 23:28:42 +0200890 if (!variable_name || !*variable_name || !vendor ||
891 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900892 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200893 ret = EFI_INVALID_PARAMETER;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900894 goto err;
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200895 }
Rob Clark15f3d742017-09-13 18:05:37 -0400896
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200897 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400898 if (ret)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900899 goto err;
Rob Clark15f3d742017-09-13 18:05:37 -0400900
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900901 /* check if a variable exists */
902 old_size = 0;
903 attr = 0;
Heinrich Schuchardtda0a7912020-05-06 01:51:04 +0200904 ret = efi_get_variable_common(variable_name, vendor, &attr,
905 &old_size, NULL);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900906 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
907 attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
908 delete = !append && (!data_size || !attributes);
909
910 /* check attributes */
911 if (old_size) {
912 if (ro_check && (attr & READ_ONLY)) {
913 ret = EFI_WRITE_PROTECTED;
914 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900915 }
916
917 /* attributes won't be changed */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900918 if (!delete &&
919 ((ro_check && attr != attributes) ||
920 (!ro_check && ((attr & ~(u32)READ_ONLY)
921 != (attributes & ~(u32)READ_ONLY))))) {
AKASHI Takahiro186b5a42019-05-24 15:59:03 +0900922 ret = EFI_INVALID_PARAMETER;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900923 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900924 }
925 } else {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900926 if (delete || append) {
Heinrich Schuchardt8021bbe2019-09-26 21:40:18 +0200927 /*
928 * Trying to delete or to update a non-existent
929 * variable.
930 */
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900931 ret = EFI_NOT_FOUND;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900932 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900933 }
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900934 }
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900935
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900936 if (((!u16_strcmp(variable_name, L"PK") ||
937 !u16_strcmp(variable_name, L"KEK")) &&
938 !guidcmp(vendor, &efi_global_variable_guid)) ||
939 ((!u16_strcmp(variable_name, L"db") ||
940 !u16_strcmp(variable_name, L"dbx")) &&
941 !guidcmp(vendor, &efi_guid_image_security_database))) {
942 /* authentication is mandatory */
943 if (!(attributes &
944 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
945 debug("%ls: AUTHENTICATED_WRITE_ACCESS required\n",
946 variable_name);
947 ret = EFI_INVALID_PARAMETER;
948 goto err;
949 }
950 }
951
952 /* authenticate a variable */
953 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
954 if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
955 ret = EFI_INVALID_PARAMETER;
956 goto err;
957 }
958 if (attributes &
959 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
960 ret = efi_variable_authenticate(variable_name, vendor,
961 &data_size, &data,
962 attributes, &attr,
963 &time);
964 if (ret != EFI_SUCCESS)
965 goto err;
966
967 /* last chance to check for delete */
968 if (!data_size)
969 delete = true;
970 }
971 } else {
972 if (attributes &
973 (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
974 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
975 debug("Secure boot is not configured\n");
976 ret = EFI_INVALID_PARAMETER;
977 goto err;
978 }
979 }
980
981 /* delete a variable */
982 if (delete) {
983 /* !old_size case has been handled before */
984 val = NULL;
985 ret = EFI_SUCCESS;
986 goto out;
987 }
988
989 if (append) {
990 old_data = malloc(old_size);
991 if (!old_data) {
Heinrich Schuchardtebbda7b2020-05-06 01:37:25 +0200992 ret = EFI_OUT_OF_RESOURCES;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900993 goto err;
994 }
Heinrich Schuchardtda0a7912020-05-06 01:51:04 +0200995 ret = efi_get_variable_common(variable_name, vendor,
996 &attr, &old_size, old_data);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900997 if (ret != EFI_SUCCESS)
998 goto err;
999 } else {
AKASHI Takahiroe5023b72019-09-06 15:09:52 +09001000 old_size = 0;
Rob Clark15f3d742017-09-13 18:05:37 -04001001 }
1002
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001003 val = malloc(2 * old_size + 2 * data_size
1004 + strlen("{ro,run,boot,nv,time=0123456701234567}(blob)")
1005 + 1);
Heinrich Schuchardtbd095f52018-10-02 05:30:05 +02001006 if (!val) {
1007 ret = EFI_OUT_OF_RESOURCES;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001008 goto err;
Heinrich Schuchardtbd095f52018-10-02 05:30:05 +02001009 }
Rob Clark15f3d742017-09-13 18:05:37 -04001010
1011 s = val;
1012
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001013 /*
1014 * store attributes
1015 */
1016 attributes &= (READ_ONLY |
1017 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro7f334332019-06-04 15:52:06 +09001018 EFI_VARIABLE_BOOTSERVICE_ACCESS |
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001019 EFI_VARIABLE_RUNTIME_ACCESS |
1020 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS);
Rob Clark15f3d742017-09-13 18:05:37 -04001021 s += sprintf(s, "{");
1022 while (attributes) {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001023 attr = 1 << (ffs(attributes) - 1);
Rob Clark15f3d742017-09-13 18:05:37 -04001024
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001025 if (attr == READ_ONLY) {
1026 s += sprintf(s, "ro");
1027 } else if (attr == EFI_VARIABLE_NON_VOLATILE) {
AKASHI Takahiro7f334332019-06-04 15:52:06 +09001028 s += sprintf(s, "nv");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001029 } else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) {
Rob Clark15f3d742017-09-13 18:05:37 -04001030 s += sprintf(s, "boot");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001031 } else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) {
Rob Clark15f3d742017-09-13 18:05:37 -04001032 s += sprintf(s, "run");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001033 } else if (attr ==
1034 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
1035 s += sprintf(s, "time=");
1036 s = bin2hex(s, (u8 *)&time, sizeof(time));
1037 }
Rob Clark15f3d742017-09-13 18:05:37 -04001038
1039 attributes &= ~attr;
1040 if (attributes)
1041 s += sprintf(s, ",");
1042 }
1043 s += sprintf(s, "}");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001044 s += sprintf(s, "(blob)");
AKASHI Takahiroe5023b72019-09-06 15:09:52 +09001045
Rob Clark15f3d742017-09-13 18:05:37 -04001046 /* store payload: */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001047 if (append)
1048 s = bin2hex(s, old_data, old_size);
Heinrich Schuchardt807899d2019-01-18 18:54:26 +01001049 s = bin2hex(s, data, data_size);
Rob Clark15f3d742017-09-13 18:05:37 -04001050 *s = '\0';
1051
Heinrich Schuchardt55111c32019-04-04 21:36:44 +02001052 EFI_PRINT("setting: %s=%s\n", native_name, val);
Rob Clark15f3d742017-09-13 18:05:37 -04001053
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001054out:
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001055 if (env_set(native_name, val)) {
Rob Clark15f3d742017-09-13 18:05:37 -04001056 ret = EFI_DEVICE_ERROR;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001057 } else {
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001058 bool vendor_keys_modified = false;
1059
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001060 if ((u16_strcmp(variable_name, L"PK") == 0 &&
1061 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1062 ret = efi_transfer_secure_state(
1063 (delete ? EFI_MODE_SETUP :
1064 EFI_MODE_USER));
1065 if (ret != EFI_SUCCESS)
1066 goto err;
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001067
1068 if (efi_secure_mode != EFI_MODE_SETUP)
1069 vendor_keys_modified = true;
1070 } else if ((u16_strcmp(variable_name, L"KEK") == 0 &&
1071 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1072 if (efi_secure_mode != EFI_MODE_SETUP)
1073 vendor_keys_modified = true;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001074 }
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001075
1076 /* update VendorKeys */
1077 if (vendor_keys_modified & efi_vendor_keys) {
1078 efi_vendor_keys = 0;
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +02001079 ret = efi_set_variable_common(
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001080 L"VendorKeys",
1081 &efi_global_variable_guid,
1082 EFI_VARIABLE_BOOTSERVICE_ACCESS
1083 | EFI_VARIABLE_RUNTIME_ACCESS
1084 | READ_ONLY,
1085 sizeof(efi_vendor_keys),
1086 &efi_vendor_keys,
1087 false);
1088 } else {
1089 ret = EFI_SUCCESS;
1090 }
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001091 }
Rob Clark15f3d742017-09-13 18:05:37 -04001092
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001093err:
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +02001094 free(native_name);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001095 free(old_data);
Rob Clark15f3d742017-09-13 18:05:37 -04001096 free(val);
1097
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001098 return ret;
1099}
1100
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001101/**
1102 * efi_set_variable() - set value of a UEFI variable
1103 *
1104 * This function implements the SetVariable runtime service.
1105 *
1106 * See the Unified Extensible Firmware Interface (UEFI) specification for
1107 * details.
1108 *
1109 * @variable_name: name of the variable
1110 * @vendor: vendor GUID
1111 * @attributes: attributes of the variable
1112 * @data_size: size of the buffer with the variable value
1113 * @data: buffer with the variable value
1114 * Return: status code
1115 */
1116efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
1117 const efi_guid_t *vendor, u32 attributes,
1118 efi_uintn_t data_size, const void *data)
1119{
1120 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
1121 data_size, data);
1122
1123 /* READ_ONLY bit is not part of API */
1124 attributes &= ~(u32)READ_ONLY;
1125
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +02001126 return EFI_EXIT(efi_set_variable_common(variable_name, vendor,
1127 attributes, data_size, data,
1128 true));
Rob Clark15f3d742017-09-13 18:05:37 -04001129}
Heinrich Schuchardt9b19dae2019-06-20 12:13:05 +02001130
1131/**
1132 * efi_query_variable_info() - get information about EFI variables
1133 *
1134 * This function implements the QueryVariableInfo() runtime service.
1135 *
1136 * See the Unified Extensible Firmware Interface (UEFI) specification for
1137 * details.
1138 *
1139 * @attributes: bitmask to select variables to be
1140 * queried
1141 * @maximum_variable_storage_size: maximum size of storage area for the
1142 * selected variable types
1143 * @remaining_variable_storage_size: remaining size of storage are for the
1144 * selected variable types
1145 * @maximum_variable_size: maximum size of a variable of the
1146 * selected type
1147 * Returns: status code
1148 */
1149efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
1150 u32 attributes,
1151 u64 *maximum_variable_storage_size,
1152 u64 *remaining_variable_storage_size,
1153 u64 *maximum_variable_size)
1154{
1155 return EFI_UNSUPPORTED;
1156}
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001157
1158/**
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001159 * efi_get_variable_runtime() - runtime implementation of GetVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001160 *
1161 * @variable_name: name of the variable
1162 * @vendor: vendor GUID
1163 * @attributes: attributes of the variable
1164 * @data_size: size of the buffer to which the variable value is copied
1165 * @data: buffer to which the variable value is copied
1166 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001167 */
1168static efi_status_t __efi_runtime EFIAPI
1169efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1170 u32 *attributes, efi_uintn_t *data_size, void *data)
1171{
1172 return EFI_UNSUPPORTED;
1173}
1174
1175/**
1176 * efi_get_next_variable_name_runtime() - runtime implementation of
1177 * GetNextVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001178 *
1179 * @variable_name_size: size of variable_name buffer in byte
1180 * @variable_name: name of uefi variable's name in u16
1181 * @vendor: vendor's guid
1182 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001183 */
1184static efi_status_t __efi_runtime EFIAPI
1185efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
Heinrich Schuchardt82cd6c42020-03-22 18:28:20 +01001186 u16 *variable_name, efi_guid_t *vendor)
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001187{
1188 return EFI_UNSUPPORTED;
1189}
1190
1191/**
1192 * efi_set_variable_runtime() - runtime implementation of SetVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001193 *
1194 * @variable_name: name of the variable
1195 * @vendor: vendor GUID
1196 * @attributes: attributes of the variable
1197 * @data_size: size of the buffer with the variable value
1198 * @data: buffer with the variable value
1199 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001200 */
1201static efi_status_t __efi_runtime EFIAPI
1202efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1203 u32 attributes, efi_uintn_t data_size,
1204 const void *data)
1205{
1206 return EFI_UNSUPPORTED;
1207}
1208
1209/**
1210 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
1211 */
1212void efi_variables_boot_exit_notify(void)
1213{
1214 efi_runtime_services.get_variable = efi_get_variable_runtime;
1215 efi_runtime_services.get_next_variable_name =
1216 efi_get_next_variable_name_runtime;
1217 efi_runtime_services.set_variable = efi_set_variable_runtime;
1218 efi_update_table_header_crc32(&efi_runtime_services.hdr);
1219}
1220
1221/**
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001222 * efi_init_variables() - initialize variable services
1223 *
1224 * Return: status code
1225 */
1226efi_status_t efi_init_variables(void)
1227{
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001228 efi_status_t ret;
1229
1230 ret = efi_init_secure_state();
1231
1232 return ret;
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001233}