blob: 74a9c65402a889925e4478f61129d3f8ba985eff [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>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090019#include <linux/compat.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070020#include <u-boot/crc.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090021
AKASHI Takahiroc78dc192020-04-14 11:51:42 +090022enum efi_secure_mode {
23 EFI_MODE_SETUP,
24 EFI_MODE_USER,
25 EFI_MODE_AUDIT,
26 EFI_MODE_DEPLOYED,
27};
28
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090029static bool efi_secure_boot;
Heinrich Schuchardte422dcc2020-06-24 12:14:49 +020030static enum efi_secure_mode 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
Heinrich Schuchardtbae548c2020-06-24 12:38:00 +0200188 * @secure_boot: value of SecureBoot
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900189 * @setup_mode: value of SetupMode
190 * @audit_mode: value of AuditMode
191 * @deployed_mode: value of DeployedMode
192 *
Heinrich Schuchardtbae548c2020-06-24 12:38:00 +0200193 * Modify secure boot status related variables as indicated.
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900194 *
195 * Return: status code
196 */
Heinrich Schuchardtbae548c2020-06-24 12:38:00 +0200197static efi_status_t efi_set_secure_state(u8 secure_boot, u8 setup_mode,
198 u8 audit_mode, u8 deployed_mode)
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900199{
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,
Heinrich Schuchardtbae548c2020-06-24 12:38:00 +0200207 attributes, sizeof(secure_boot),
208 &secure_boot, 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 Takahiro37123732020-06-09 14:09:34 +0900246 EFI_PRINT("Switching secure state from %d to %d\n", efi_secure_mode,
247 mode);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900248
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900249 if (mode == EFI_MODE_DEPLOYED) {
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900250 ret = efi_set_secure_state(1, 0, 0, 1);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900251 if (ret != EFI_SUCCESS)
252 goto err;
253
254 efi_secure_boot = true;
255 } else if (mode == EFI_MODE_AUDIT) {
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200256 ret = efi_set_variable_common(L"PK", &efi_global_variable_guid,
257 EFI_VARIABLE_BOOTSERVICE_ACCESS |
258 EFI_VARIABLE_RUNTIME_ACCESS,
259 0, NULL, false);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900260 if (ret != EFI_SUCCESS)
261 goto err;
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900262
263 ret = efi_set_secure_state(0, 1, 1, 0);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900264 if (ret != EFI_SUCCESS)
265 goto err;
266
267 efi_secure_boot = true;
268 } else if (mode == EFI_MODE_USER) {
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900269 ret = efi_set_secure_state(1, 0, 0, 0);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900270 if (ret != EFI_SUCCESS)
271 goto err;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900272
273 efi_secure_boot = true;
274 } else if (mode == EFI_MODE_SETUP) {
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900275 ret = efi_set_secure_state(0, 1, 0, 0);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900276 if (ret != EFI_SUCCESS)
277 goto err;
278 } else {
279 return EFI_INVALID_PARAMETER;
280 }
281
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900282 efi_secure_mode = mode;
283
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900284 return EFI_SUCCESS;
285
286err:
287 /* TODO: What action should be taken here? */
288 printf("ERROR: Secure state transition failed\n");
289 return ret;
290}
291
292/**
293 * efi_init_secure_state - initialize secure boot state
294 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +0200295 * Return: status code
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900296 */
297static efi_status_t efi_init_secure_state(void)
298{
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900299 enum efi_secure_mode mode;
300 efi_uintn_t size;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900301 efi_status_t ret;
302
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900303 /*
304 * TODO:
305 * Since there is currently no "platform-specific" installation
306 * method of Platform Key, we can't say if VendorKeys is 0 or 1
307 * precisely.
308 */
309
310 size = 0;
Heinrich Schuchardtda0a7912020-05-06 01:51:04 +0200311 ret = efi_get_variable_common(L"PK", &efi_global_variable_guid,
312 NULL, &size, NULL);
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900313 if (ret == EFI_BUFFER_TOO_SMALL) {
314 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT))
315 mode = EFI_MODE_USER;
316 else
317 mode = EFI_MODE_SETUP;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900318
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900319 efi_vendor_keys = 0;
320 } else if (ret == EFI_NOT_FOUND) {
321 mode = EFI_MODE_SETUP;
322 efi_vendor_keys = 1;
323 } else {
324 goto err;
325 }
326
327 ret = efi_transfer_secure_state(mode);
328 if (ret == EFI_SUCCESS)
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200329 ret = efi_set_variable_common(L"VendorKeys",
330 &efi_global_variable_guid,
331 EFI_VARIABLE_BOOTSERVICE_ACCESS |
332 EFI_VARIABLE_RUNTIME_ACCESS |
333 READ_ONLY,
334 sizeof(efi_vendor_keys),
335 &efi_vendor_keys, false);
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900336
337err:
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900338 return ret;
339}
340
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100341/**
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900342 * efi_secure_boot_enabled - return if secure boot is enabled or not
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100343 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900344 * Return: true if enabled, false if disabled
345 */
346bool efi_secure_boot_enabled(void)
347{
348 return efi_secure_boot;
349}
350
351#ifdef CONFIG_EFI_SECURE_BOOT
352static u8 pkcs7_hdr[] = {
353 /* SEQUENCE */
354 0x30, 0x82, 0x05, 0xc7,
355 /* OID: pkcs7-signedData */
356 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
357 /* Context Structured? */
358 0xa0, 0x82, 0x05, 0xb8,
359};
360
361/**
362 * efi_variable_parse_signature - parse a signature in variable
363 * @buf: Pointer to variable's value
364 * @buflen: Length of @buf
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100365 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900366 * Parse a signature embedded in variable's value and instantiate
367 * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
368 * pkcs7's signedData, some header needed be prepended for correctly
369 * parsing authentication data, particularly for variable's.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100370 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900371 * Return: Pointer to pkcs7_message structure on success, NULL on error
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100372 */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900373static struct pkcs7_message *efi_variable_parse_signature(const void *buf,
374 size_t buflen)
375{
376 u8 *ebuf;
377 size_t ebuflen, len;
378 struct pkcs7_message *msg;
379
380 /*
381 * This is the best assumption to check if the binary is
382 * already in a form of pkcs7's signedData.
383 */
384 if (buflen > sizeof(pkcs7_hdr) &&
385 !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
386 msg = pkcs7_parse_message(buf, buflen);
387 goto out;
388 }
389
390 /*
391 * Otherwise, we should add a dummy prefix sequence for pkcs7
392 * message parser to be able to process.
393 * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
394 * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
395 * TODO:
396 * The header should be composed in a more refined manner.
397 */
AKASHI Takahiro37123732020-06-09 14:09:34 +0900398 EFI_PRINT("Makeshift prefix added to authentication data\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900399 ebuflen = sizeof(pkcs7_hdr) + buflen;
400 if (ebuflen <= 0x7f) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900401 EFI_PRINT("Data is too short\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900402 return NULL;
403 }
404
405 ebuf = malloc(ebuflen);
406 if (!ebuf) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900407 EFI_PRINT("Out of memory\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900408 return NULL;
409 }
410
411 memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
412 memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
413 len = ebuflen - 4;
414 ebuf[2] = (len >> 8) & 0xff;
415 ebuf[3] = len & 0xff;
416 len = ebuflen - 0x13;
417 ebuf[0x11] = (len >> 8) & 0xff;
418 ebuf[0x12] = len & 0xff;
419
420 msg = pkcs7_parse_message(ebuf, ebuflen);
421
422 free(ebuf);
423
424out:
425 if (IS_ERR(msg))
426 return NULL;
427
428 return msg;
429}
430
431/**
432 * efi_variable_authenticate - authenticate a variable
433 * @variable: Variable name in u16
434 * @vendor: Guid of variable
435 * @data_size: Size of @data
436 * @data: Pointer to variable's value
437 * @given_attr: Attributes to be given at SetVariable()
438 * @env_attr: Attributes that an existing variable holds
439 * @time: signed time that an existing variable holds
440 *
441 * Called by efi_set_variable() to verify that the input is correct.
442 * Will replace the given data pointer with another that points to
443 * the actual data to store in the internal memory.
444 * On success, @data and @data_size will be replaced with variable's
445 * actual data, excluding authentication data, and its size, and variable's
446 * attributes and signed time will also be returned in @env_attr and @time,
447 * respectively.
448 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +0200449 * Return: status code
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900450 */
451static efi_status_t efi_variable_authenticate(u16 *variable,
452 const efi_guid_t *vendor,
453 efi_uintn_t *data_size,
454 const void **data, u32 given_attr,
455 u32 *env_attr, u64 *time)
456{
457 const struct efi_variable_authentication_2 *auth;
458 struct efi_signature_store *truststore, *truststore2;
459 struct pkcs7_message *var_sig;
460 struct efi_image_regions *regs;
461 struct efi_time timestamp;
462 struct rtc_time tm;
463 u64 new_time;
464 efi_status_t ret;
465
466 var_sig = NULL;
467 truststore = NULL;
468 truststore2 = NULL;
469 regs = NULL;
470 ret = EFI_SECURITY_VIOLATION;
471
472 if (*data_size < sizeof(struct efi_variable_authentication_2))
473 goto err;
474
475 /* authentication data */
476 auth = *data;
477 if (*data_size < (sizeof(auth->time_stamp)
478 + auth->auth_info.hdr.dwLength))
479 goto err;
480
481 if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
482 goto err;
483
484 *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
485 *data_size -= (sizeof(auth->time_stamp)
486 + auth->auth_info.hdr.dwLength);
487
488 memcpy(&timestamp, &auth->time_stamp, sizeof(timestamp));
489 memset(&tm, 0, sizeof(tm));
490 tm.tm_year = timestamp.year;
491 tm.tm_mon = timestamp.month;
492 tm.tm_mday = timestamp.day;
493 tm.tm_hour = timestamp.hour;
494 tm.tm_min = timestamp.minute;
495 tm.tm_sec = timestamp.second;
496 new_time = rtc_mktime(&tm);
497
498 if (!efi_secure_boot_enabled()) {
499 /* finished checking */
500 *time = new_time;
501 return EFI_SUCCESS;
502 }
503
504 if (new_time <= *time)
505 goto err;
506
507 /* data to be digested */
508 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
509 if (!regs)
510 goto err;
511 regs->max = 5;
512 efi_image_region_add(regs, (uint8_t *)variable,
513 (uint8_t *)variable
514 + u16_strlen(variable) * sizeof(u16), 1);
515 efi_image_region_add(regs, (uint8_t *)vendor,
516 (uint8_t *)vendor + sizeof(*vendor), 1);
517 efi_image_region_add(regs, (uint8_t *)&given_attr,
518 (uint8_t *)&given_attr + sizeof(given_attr), 1);
519 efi_image_region_add(regs, (uint8_t *)&timestamp,
520 (uint8_t *)&timestamp + sizeof(timestamp), 1);
521 efi_image_region_add(regs, (uint8_t *)*data,
522 (uint8_t *)*data + *data_size, 1);
523
524 /* variable's signature list */
525 if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
526 goto err;
527 var_sig = efi_variable_parse_signature(auth->auth_info.cert_data,
528 auth->auth_info.hdr.dwLength
529 - sizeof(auth->auth_info));
Patrick Wildtcd8a55b2020-05-07 02:13:18 +0200530 if (!var_sig) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900531 EFI_PRINT("Parsing variable's signature failed\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900532 goto err;
533 }
534
535 /* signature database used for authentication */
536 if (u16_strcmp(variable, L"PK") == 0 ||
537 u16_strcmp(variable, L"KEK") == 0) {
538 /* with PK */
539 truststore = efi_sigstore_parse_sigdb(L"PK");
540 if (!truststore)
541 goto err;
542 } else if (u16_strcmp(variable, L"db") == 0 ||
543 u16_strcmp(variable, L"dbx") == 0) {
544 /* with PK and KEK */
545 truststore = efi_sigstore_parse_sigdb(L"KEK");
546 truststore2 = efi_sigstore_parse_sigdb(L"PK");
547
548 if (!truststore) {
549 if (!truststore2)
550 goto err;
551
552 truststore = truststore2;
553 truststore2 = NULL;
554 }
555 } else {
556 /* TODO: support private authenticated variables */
557 goto err;
558 }
559
560 /* verify signature */
561 if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900562 EFI_PRINT("Verified\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900563 } else {
564 if (truststore2 &&
565 efi_signature_verify_with_sigdb(regs, var_sig,
566 truststore2, NULL)) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900567 EFI_PRINT("Verified\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900568 } else {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900569 EFI_PRINT("Verifying variable's signature failed\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900570 goto err;
571 }
572 }
573
574 /* finished checking */
575 *time = rtc_mktime(&tm);
576 ret = EFI_SUCCESS;
577
578err:
579 efi_sigstore_free(truststore);
580 efi_sigstore_free(truststore2);
581 pkcs7_free_message(var_sig);
582 free(regs);
583
584 return ret;
585}
586#else
587static efi_status_t efi_variable_authenticate(u16 *variable,
588 const efi_guid_t *vendor,
589 efi_uintn_t *data_size,
590 const void **data, u32 given_attr,
591 u32 *env_attr, u64 *time)
592{
593 return EFI_SUCCESS;
594}
595#endif /* CONFIG_EFI_SECURE_BOOT */
596
Heinrich Schuchardtda0a7912020-05-06 01:51:04 +0200597static efi_status_t efi_get_variable_common(u16 *variable_name,
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900598 const efi_guid_t *vendor,
599 u32 *attributes,
Heinrich Schuchardt87ab0b22020-04-18 12:31:17 +0200600 efi_uintn_t *data_size, void *data)
Rob Clark15f3d742017-09-13 18:05:37 -0400601{
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200602 char *native_name;
Rob Clark15f3d742017-09-13 18:05:37 -0400603 efi_status_t ret;
604 unsigned long in_size;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900605 const char *val = NULL, *s;
606 u64 time = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400607 u32 attr;
608
Rob Clark15f3d742017-09-13 18:05:37 -0400609 if (!variable_name || !vendor || !data_size)
Heinrich Schuchardt563e5522020-06-29 11:49:58 +0200610 return EFI_INVALID_PARAMETER;
Rob Clark15f3d742017-09-13 18:05:37 -0400611
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200612 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400613 if (ret)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900614 return ret;
Rob Clark15f3d742017-09-13 18:05:37 -0400615
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200616 EFI_PRINT("get '%s'\n", native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400617
618 val = env_get(native_name);
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200619 free(native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400620 if (!val)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900621 return EFI_NOT_FOUND;
Rob Clark15f3d742017-09-13 18:05:37 -0400622
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900623 val = parse_attr(val, &attr, &time);
Rob Clark15f3d742017-09-13 18:05:37 -0400624
625 in_size = *data_size;
626
627 if ((s = prefix(val, "(blob)"))) {
Heinrich Schuchardt2d8aedc2019-01-18 12:31:54 +0100628 size_t len = strlen(s);
Rob Clark15f3d742017-09-13 18:05:37 -0400629
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700630 /* number of hexadecimal digits must be even */
631 if (len & 1)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900632 return EFI_DEVICE_ERROR;
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700633
Rob Clark15f3d742017-09-13 18:05:37 -0400634 /* two characters per byte: */
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700635 len /= 2;
Rob Clark15f3d742017-09-13 18:05:37 -0400636 *data_size = len;
637
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200638 if (in_size < len) {
639 ret = EFI_BUFFER_TOO_SMALL;
640 goto out;
641 }
Rob Clark15f3d742017-09-13 18:05:37 -0400642
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900643 if (!data) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900644 EFI_PRINT("Variable with no data shouldn't exist.\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900645 return EFI_INVALID_PARAMETER;
646 }
Rob Clark15f3d742017-09-13 18:05:37 -0400647
Heinrich Schuchardt2d8aedc2019-01-18 12:31:54 +0100648 if (hex2bin(data, s, len))
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900649 return EFI_DEVICE_ERROR;
Rob Clark15f3d742017-09-13 18:05:37 -0400650
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200651 EFI_PRINT("got value: \"%s\"\n", s);
Rob Clark15f3d742017-09-13 18:05:37 -0400652 } else if ((s = prefix(val, "(utf8)"))) {
653 unsigned len = strlen(s) + 1;
654
655 *data_size = len;
656
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200657 if (in_size < len) {
658 ret = EFI_BUFFER_TOO_SMALL;
659 goto out;
660 }
Rob Clark15f3d742017-09-13 18:05:37 -0400661
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900662 if (!data) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900663 EFI_PRINT("Variable with no data shouldn't exist.\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900664 return EFI_INVALID_PARAMETER;
665 }
Rob Clark15f3d742017-09-13 18:05:37 -0400666
667 memcpy(data, s, len);
668 ((char *)data)[len] = '\0';
669
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200670 EFI_PRINT("got value: \"%s\"\n", (char *)data);
Rob Clark15f3d742017-09-13 18:05:37 -0400671 } else {
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200672 EFI_PRINT("invalid value: '%s'\n", val);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900673 return EFI_DEVICE_ERROR;
Rob Clark15f3d742017-09-13 18:05:37 -0400674 }
675
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200676out:
Rob Clark15f3d742017-09-13 18:05:37 -0400677 if (attributes)
678 *attributes = attr & EFI_VARIABLE_MASK;
679
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900680 return ret;
681}
682
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900683/**
684 * efi_efi_get_variable() - retrieve value of a UEFI variable
685 *
686 * This function implements the GetVariable runtime service.
687 *
688 * See the Unified Extensible Firmware Interface (UEFI) specification for
689 * details.
690 *
691 * @variable_name: name of the variable
692 * @vendor: vendor GUID
693 * @attributes: attributes of the variable
694 * @data_size: size of the buffer to which the variable value is copied
695 * @data: buffer to which the variable value is copied
696 * Return: status code
697 */
698efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
699 const efi_guid_t *vendor, u32 *attributes,
700 efi_uintn_t *data_size, void *data)
701{
702 efi_status_t ret;
703
704 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
705 data_size, data);
706
Heinrich Schuchardt87ab0b22020-04-18 12:31:17 +0200707 ret = efi_get_variable_common(variable_name, vendor, attributes,
708 data_size, data);
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200709 return EFI_EXIT(ret);
Rob Clark15f3d742017-09-13 18:05:37 -0400710}
711
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100712static char *efi_variables_list;
713static char *efi_cur_variable;
714
715/**
716 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
717 * information
718 * @variable: whole data of u-boot variable (ie. name=value)
719 * @variable_name_size: size of variable_name buffer in byte
720 * @variable_name: name of uefi variable in u16, null-terminated
721 * @vendor: vendor's guid
722 * @attributes: attributes
723 *
724 * A uefi variable is encoded into a u-boot variable as described above.
725 * This function parses such a u-boot variable and retrieve uefi-related
726 * information into respective parameters. In return, variable_name_size
727 * is the size of variable name including NULL.
728 *
729 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200730 * the entire variable list has been returned,
731 * otherwise non-zero status code
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100732 */
733static efi_status_t parse_uboot_variable(char *variable,
734 efi_uintn_t *variable_name_size,
735 u16 *variable_name,
736 const efi_guid_t *vendor,
737 u32 *attributes)
738{
739 char *guid, *name, *end, c;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100740 size_t name_len;
741 efi_uintn_t old_variable_name_size;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900742 u64 time;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100743 u16 *p;
744
745 guid = strchr(variable, '_');
746 if (!guid)
747 return EFI_INVALID_PARAMETER;
748 guid++;
749 name = strchr(guid, '_');
750 if (!name)
751 return EFI_INVALID_PARAMETER;
752 name++;
753 end = strchr(name, '=');
754 if (!end)
755 return EFI_INVALID_PARAMETER;
756
757 name_len = end - name;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100758 old_variable_name_size = *variable_name_size;
759 *variable_name_size = sizeof(u16) * (name_len + 1);
760 if (old_variable_name_size < *variable_name_size)
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100761 return EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100762
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100763 end++; /* point to value */
764
765 /* variable name */
766 p = variable_name;
767 utf8_utf16_strncpy(&p, name, name_len);
768 variable_name[name_len] = 0;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100769
770 /* guid */
771 c = *(name - 1);
772 *(name - 1) = '\0'; /* guid need be null-terminated here */
AKASHI Takahiro4854f782020-05-08 14:51:21 +0900773 if (uuid_str_to_bin(guid, (unsigned char *)vendor,
774 UUID_STR_FORMAT_GUID))
775 /* The only error would be EINVAL. */
776 return EFI_INVALID_PARAMETER;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100777 *(name - 1) = c;
778
779 /* attributes */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900780 parse_attr(end, attributes, &time);
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100781
782 return EFI_SUCCESS;
783}
784
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100785/**
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100786 * efi_get_next_variable_name() - enumerate the current variable names
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200787 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100788 * @variable_name_size: size of variable_name buffer in byte
789 * @variable_name: name of uefi variable's name in u16
790 * @vendor: vendor's guid
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100791 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100792 * This function implements the GetNextVariableName service.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100793 *
794 * See the Unified Extensible Firmware Interface (UEFI) specification for
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200795 * details.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100796 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100797 * Return: status code
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100798 */
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200799efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
800 u16 *variable_name,
Heinrich Schuchardt82cd6c42020-03-22 18:28:20 +0100801 efi_guid_t *vendor)
Rob Clark15f3d742017-09-13 18:05:37 -0400802{
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100803 char *native_name, *variable;
804 ssize_t name_len, list_len;
805 char regex[256];
806 char * const regexlist[] = {regex};
807 u32 attributes;
808 int i;
809 efi_status_t ret;
810
Rob Clark238f88c2017-09-13 18:05:41 -0400811 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400812
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100813 if (!variable_name_size || !variable_name || !vendor)
Heinrich Schuchardt793a6252019-03-19 18:36:21 +0100814 return EFI_EXIT(EFI_INVALID_PARAMETER);
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100815
816 if (variable_name[0]) {
817 /* check null-terminated string */
818 for (i = 0; i < *variable_name_size; i++)
819 if (!variable_name[i])
820 break;
821 if (i >= *variable_name_size)
822 return EFI_EXIT(EFI_INVALID_PARAMETER);
823
824 /* search for the last-returned variable */
825 ret = efi_to_native(&native_name, variable_name, vendor);
826 if (ret)
827 return EFI_EXIT(ret);
828
829 name_len = strlen(native_name);
830 for (variable = efi_variables_list; variable && *variable;) {
831 if (!strncmp(variable, native_name, name_len) &&
832 variable[name_len] == '=')
833 break;
834
835 variable = strchr(variable, '\n');
836 if (variable)
837 variable++;
838 }
839
840 free(native_name);
841 if (!(variable && *variable))
842 return EFI_EXIT(EFI_INVALID_PARAMETER);
843
844 /* next variable */
845 variable = strchr(variable, '\n');
846 if (variable)
847 variable++;
848 if (!(variable && *variable))
849 return EFI_EXIT(EFI_NOT_FOUND);
850 } else {
851 /*
852 *new search: free a list used in the previous search
853 */
854 free(efi_variables_list);
855 efi_variables_list = NULL;
856 efi_cur_variable = NULL;
857
858 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
859 list_len = hexport_r(&env_htab, '\n',
860 H_MATCH_REGEX | H_MATCH_KEY,
861 &efi_variables_list, 0, 1, regexlist);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900862
Heinrich Schuchardtfd738482019-01-22 20:10:46 +0100863 if (list_len <= 1)
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100864 return EFI_EXIT(EFI_NOT_FOUND);
865
866 variable = efi_variables_list;
867 }
868
869 ret = parse_uboot_variable(variable, variable_name_size, variable_name,
870 vendor, &attributes);
871
872 return EFI_EXIT(ret);
Rob Clark15f3d742017-09-13 18:05:37 -0400873}
874
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200875static efi_status_t efi_set_variable_common(u16 *variable_name,
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900876 const efi_guid_t *vendor,
877 u32 attributes,
878 efi_uintn_t data_size,
879 const void *data,
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +0200880 bool ro_check)
Rob Clark15f3d742017-09-13 18:05:37 -0400881{
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900882 char *native_name = NULL, *old_data = NULL, *val = NULL, *s;
883 efi_uintn_t old_size;
884 bool append, delete;
885 u64 time = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400886 u32 attr;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900887 efi_status_t ret = EFI_SUCCESS;
Rob Clark15f3d742017-09-13 18:05:37 -0400888
Heinrich Schuchardtadba3962019-06-12 23:28:42 +0200889 if (!variable_name || !*variable_name || !vendor ||
890 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900891 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200892 ret = EFI_INVALID_PARAMETER;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900893 goto err;
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200894 }
Rob Clark15f3d742017-09-13 18:05:37 -0400895
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200896 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400897 if (ret)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900898 goto err;
Rob Clark15f3d742017-09-13 18:05:37 -0400899
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900900 /* check if a variable exists */
901 old_size = 0;
902 attr = 0;
Heinrich Schuchardtda0a7912020-05-06 01:51:04 +0200903 ret = efi_get_variable_common(variable_name, vendor, &attr,
904 &old_size, NULL);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900905 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
906 attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
907 delete = !append && (!data_size || !attributes);
908
909 /* check attributes */
910 if (old_size) {
911 if (ro_check && (attr & READ_ONLY)) {
912 ret = EFI_WRITE_PROTECTED;
913 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900914 }
915
916 /* attributes won't be changed */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900917 if (!delete &&
918 ((ro_check && attr != attributes) ||
919 (!ro_check && ((attr & ~(u32)READ_ONLY)
920 != (attributes & ~(u32)READ_ONLY))))) {
AKASHI Takahiro186b5a42019-05-24 15:59:03 +0900921 ret = EFI_INVALID_PARAMETER;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900922 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900923 }
924 } else {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900925 if (delete || append) {
Heinrich Schuchardt8021bbe2019-09-26 21:40:18 +0200926 /*
927 * Trying to delete or to update a non-existent
928 * variable.
929 */
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900930 ret = EFI_NOT_FOUND;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900931 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900932 }
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900933 }
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900934
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900935 if (((!u16_strcmp(variable_name, L"PK") ||
936 !u16_strcmp(variable_name, L"KEK")) &&
937 !guidcmp(vendor, &efi_global_variable_guid)) ||
938 ((!u16_strcmp(variable_name, L"db") ||
939 !u16_strcmp(variable_name, L"dbx")) &&
940 !guidcmp(vendor, &efi_guid_image_security_database))) {
941 /* authentication is mandatory */
942 if (!(attributes &
943 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900944 EFI_PRINT("%ls: AUTHENTICATED_WRITE_ACCESS required\n",
945 variable_name);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900946 ret = EFI_INVALID_PARAMETER;
947 goto err;
948 }
949 }
950
951 /* authenticate a variable */
952 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
953 if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
954 ret = EFI_INVALID_PARAMETER;
955 goto err;
956 }
957 if (attributes &
958 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
959 ret = efi_variable_authenticate(variable_name, vendor,
960 &data_size, &data,
961 attributes, &attr,
962 &time);
963 if (ret != EFI_SUCCESS)
964 goto err;
965
966 /* last chance to check for delete */
967 if (!data_size)
968 delete = true;
969 }
970 } else {
971 if (attributes &
972 (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
973 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900974 EFI_PRINT("Secure boot is not configured\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900975 ret = EFI_INVALID_PARAMETER;
976 goto err;
977 }
978 }
979
980 /* delete a variable */
981 if (delete) {
982 /* !old_size case has been handled before */
983 val = NULL;
984 ret = EFI_SUCCESS;
985 goto out;
986 }
987
988 if (append) {
989 old_data = malloc(old_size);
990 if (!old_data) {
Heinrich Schuchardtebbda7b2020-05-06 01:37:25 +0200991 ret = EFI_OUT_OF_RESOURCES;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900992 goto err;
993 }
Heinrich Schuchardtda0a7912020-05-06 01:51:04 +0200994 ret = efi_get_variable_common(variable_name, vendor,
995 &attr, &old_size, old_data);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900996 if (ret != EFI_SUCCESS)
997 goto err;
998 } else {
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900999 old_size = 0;
Rob Clark15f3d742017-09-13 18:05:37 -04001000 }
1001
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001002 val = malloc(2 * old_size + 2 * data_size
1003 + strlen("{ro,run,boot,nv,time=0123456701234567}(blob)")
1004 + 1);
Heinrich Schuchardtbd095f52018-10-02 05:30:05 +02001005 if (!val) {
1006 ret = EFI_OUT_OF_RESOURCES;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001007 goto err;
Heinrich Schuchardtbd095f52018-10-02 05:30:05 +02001008 }
Rob Clark15f3d742017-09-13 18:05:37 -04001009
1010 s = val;
1011
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001012 /*
1013 * store attributes
1014 */
1015 attributes &= (READ_ONLY |
1016 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro7f334332019-06-04 15:52:06 +09001017 EFI_VARIABLE_BOOTSERVICE_ACCESS |
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001018 EFI_VARIABLE_RUNTIME_ACCESS |
1019 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS);
Rob Clark15f3d742017-09-13 18:05:37 -04001020 s += sprintf(s, "{");
1021 while (attributes) {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001022 attr = 1 << (ffs(attributes) - 1);
Rob Clark15f3d742017-09-13 18:05:37 -04001023
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001024 if (attr == READ_ONLY) {
1025 s += sprintf(s, "ro");
1026 } else if (attr == EFI_VARIABLE_NON_VOLATILE) {
AKASHI Takahiro7f334332019-06-04 15:52:06 +09001027 s += sprintf(s, "nv");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001028 } else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) {
Rob Clark15f3d742017-09-13 18:05:37 -04001029 s += sprintf(s, "boot");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001030 } else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) {
Rob Clark15f3d742017-09-13 18:05:37 -04001031 s += sprintf(s, "run");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001032 } else if (attr ==
1033 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
1034 s += sprintf(s, "time=");
1035 s = bin2hex(s, (u8 *)&time, sizeof(time));
1036 }
Rob Clark15f3d742017-09-13 18:05:37 -04001037
1038 attributes &= ~attr;
1039 if (attributes)
1040 s += sprintf(s, ",");
1041 }
1042 s += sprintf(s, "}");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001043 s += sprintf(s, "(blob)");
AKASHI Takahiroe5023b72019-09-06 15:09:52 +09001044
Rob Clark15f3d742017-09-13 18:05:37 -04001045 /* store payload: */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001046 if (append)
1047 s = bin2hex(s, old_data, old_size);
Heinrich Schuchardt807899d2019-01-18 18:54:26 +01001048 s = bin2hex(s, data, data_size);
Rob Clark15f3d742017-09-13 18:05:37 -04001049 *s = '\0';
1050
Heinrich Schuchardt55111c32019-04-04 21:36:44 +02001051 EFI_PRINT("setting: %s=%s\n", native_name, val);
Rob Clark15f3d742017-09-13 18:05:37 -04001052
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001053out:
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001054 if (env_set(native_name, val)) {
Rob Clark15f3d742017-09-13 18:05:37 -04001055 ret = EFI_DEVICE_ERROR;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001056 } else {
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001057 bool vendor_keys_modified = false;
1058
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001059 if ((u16_strcmp(variable_name, L"PK") == 0 &&
1060 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1061 ret = efi_transfer_secure_state(
1062 (delete ? EFI_MODE_SETUP :
1063 EFI_MODE_USER));
1064 if (ret != EFI_SUCCESS)
1065 goto err;
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001066
1067 if (efi_secure_mode != EFI_MODE_SETUP)
1068 vendor_keys_modified = true;
1069 } else if ((u16_strcmp(variable_name, L"KEK") == 0 &&
1070 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1071 if (efi_secure_mode != EFI_MODE_SETUP)
1072 vendor_keys_modified = true;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001073 }
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001074
1075 /* update VendorKeys */
1076 if (vendor_keys_modified & efi_vendor_keys) {
1077 efi_vendor_keys = 0;
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +02001078 ret = efi_set_variable_common(
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001079 L"VendorKeys",
1080 &efi_global_variable_guid,
1081 EFI_VARIABLE_BOOTSERVICE_ACCESS
1082 | EFI_VARIABLE_RUNTIME_ACCESS
1083 | READ_ONLY,
1084 sizeof(efi_vendor_keys),
1085 &efi_vendor_keys,
1086 false);
1087 } else {
1088 ret = EFI_SUCCESS;
1089 }
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001090 }
Rob Clark15f3d742017-09-13 18:05:37 -04001091
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001092err:
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +02001093 free(native_name);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001094 free(old_data);
Rob Clark15f3d742017-09-13 18:05:37 -04001095 free(val);
1096
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001097 return ret;
1098}
1099
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001100/**
1101 * efi_set_variable() - set value of a UEFI variable
1102 *
1103 * This function implements the SetVariable runtime service.
1104 *
1105 * See the Unified Extensible Firmware Interface (UEFI) specification for
1106 * details.
1107 *
1108 * @variable_name: name of the variable
1109 * @vendor: vendor GUID
1110 * @attributes: attributes of the variable
1111 * @data_size: size of the buffer with the variable value
1112 * @data: buffer with the variable value
1113 * Return: status code
1114 */
1115efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
1116 const efi_guid_t *vendor, u32 attributes,
1117 efi_uintn_t data_size, const void *data)
1118{
1119 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
1120 data_size, data);
1121
1122 /* READ_ONLY bit is not part of API */
1123 attributes &= ~(u32)READ_ONLY;
1124
Heinrich Schuchardt37ab7362020-05-03 10:02:20 +02001125 return EFI_EXIT(efi_set_variable_common(variable_name, vendor,
1126 attributes, data_size, data,
1127 true));
Rob Clark15f3d742017-09-13 18:05:37 -04001128}
Heinrich Schuchardt9b19dae2019-06-20 12:13:05 +02001129
1130/**
1131 * efi_query_variable_info() - get information about EFI variables
1132 *
1133 * This function implements the QueryVariableInfo() runtime service.
1134 *
1135 * See the Unified Extensible Firmware Interface (UEFI) specification for
1136 * details.
1137 *
1138 * @attributes: bitmask to select variables to be
1139 * queried
1140 * @maximum_variable_storage_size: maximum size of storage area for the
1141 * selected variable types
1142 * @remaining_variable_storage_size: remaining size of storage are for the
1143 * selected variable types
1144 * @maximum_variable_size: maximum size of a variable of the
1145 * selected type
1146 * Returns: status code
1147 */
1148efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
1149 u32 attributes,
1150 u64 *maximum_variable_storage_size,
1151 u64 *remaining_variable_storage_size,
1152 u64 *maximum_variable_size)
1153{
1154 return EFI_UNSUPPORTED;
1155}
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001156
1157/**
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001158 * efi_get_variable_runtime() - runtime implementation of GetVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001159 *
1160 * @variable_name: name of the variable
1161 * @vendor: vendor GUID
1162 * @attributes: attributes of the variable
1163 * @data_size: size of the buffer to which the variable value is copied
1164 * @data: buffer to which the variable value is copied
1165 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001166 */
1167static efi_status_t __efi_runtime EFIAPI
1168efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1169 u32 *attributes, efi_uintn_t *data_size, void *data)
1170{
1171 return EFI_UNSUPPORTED;
1172}
1173
1174/**
1175 * efi_get_next_variable_name_runtime() - runtime implementation of
1176 * GetNextVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001177 *
1178 * @variable_name_size: size of variable_name buffer in byte
1179 * @variable_name: name of uefi variable's name in u16
1180 * @vendor: vendor's guid
1181 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001182 */
1183static efi_status_t __efi_runtime EFIAPI
1184efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
Heinrich Schuchardt82cd6c42020-03-22 18:28:20 +01001185 u16 *variable_name, efi_guid_t *vendor)
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001186{
1187 return EFI_UNSUPPORTED;
1188}
1189
1190/**
1191 * efi_set_variable_runtime() - runtime implementation of SetVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001192 *
1193 * @variable_name: name of the variable
1194 * @vendor: vendor GUID
1195 * @attributes: attributes of the variable
1196 * @data_size: size of the buffer with the variable value
1197 * @data: buffer with the variable value
1198 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001199 */
1200static efi_status_t __efi_runtime EFIAPI
1201efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1202 u32 attributes, efi_uintn_t data_size,
1203 const void *data)
1204{
1205 return EFI_UNSUPPORTED;
1206}
1207
1208/**
1209 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
1210 */
1211void efi_variables_boot_exit_notify(void)
1212{
1213 efi_runtime_services.get_variable = efi_get_variable_runtime;
1214 efi_runtime_services.get_next_variable_name =
1215 efi_get_next_variable_name_runtime;
1216 efi_runtime_services.set_variable = efi_set_variable_runtime;
1217 efi_update_table_header_crc32(&efi_runtime_services.hdr);
1218}
1219
1220/**
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001221 * efi_init_variables() - initialize variable services
1222 *
1223 * Return: status code
1224 */
1225efi_status_t efi_init_variables(void)
1226{
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001227 efi_status_t ret;
1228
1229 ret = efi_init_secure_state();
1230
1231 return ret;
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001232}