blob: 36bac8670279861e1a42f2f653ef3372ae830570 [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>
Heinrich Schuchardt9827e842020-06-22 18:10:27 +020010#include <efi_variable.h>
Simon Glassed38aef2020-05-10 11:40:03 -060011#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060012#include <env_internal.h>
Heinrich Schuchardt147d5d32019-10-26 23:53:48 +020013#include <hexdump.h>
14#include <malloc.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090015#include <rtc.h>
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +010016#include <search.h>
Simon Glass6f044982020-05-10 11:39:52 -060017#include <uuid.h>
AKASHI Takahiro6ec67672020-04-21 09:38:17 +090018#include <crypto/pkcs7_parser.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
Rob Clark15f3d742017-09-13 18:05:37 -040033/*
34 * Mapping between EFI variables and u-boot variables:
35 *
36 * efi_$guid_$varname = {attributes}(type)value
37 *
38 * For example:
39 *
40 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
41 * "{ro,boot,run}(blob)0000000000000000"
42 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
43 * "(blob)00010000"
44 *
45 * The attributes are a comma separated list of these possible
46 * attributes:
47 *
48 * + ro - read-only
49 * + boot - boot-services access
50 * + run - runtime access
51 *
52 * NOTE: with current implementation, no variables are available after
53 * ExitBootServices, and all are persisted (if possible).
54 *
55 * If not specified, the attributes default to "{boot}".
56 *
57 * The required type is one of:
58 *
59 * + utf8 - raw utf8 string
60 * + blob - arbitrary length hex string
61 *
62 * Maybe a utf16 type would be useful to for a string value to be auto
63 * converted to utf16?
64 */
65
Heinrich Schuchardt44389cb2018-09-23 04:08:09 +020066#define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
Rob Clark15f3d742017-09-13 18:05:37 -040067
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +010068/**
69 * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
70 * variable name
71 *
72 * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
73 * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
74 * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
75 *
76 * @native: pointer to pointer to U-Boot variable name
77 * @variable_name: UEFI variable name
78 * @vendor: vendor GUID
79 * Return: status code
80 */
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020081static efi_status_t efi_to_native(char **native, const u16 *variable_name,
Heinrich Schuchardte06ae192018-12-30 20:53:51 +010082 const efi_guid_t *vendor)
Rob Clark15f3d742017-09-13 18:05:37 -040083{
84 size_t len;
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020085 char *pos;
Rob Clark15f3d742017-09-13 18:05:37 -040086
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020087 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
88 *native = malloc(len);
89 if (!*native)
90 return EFI_OUT_OF_RESOURCES;
Rob Clark15f3d742017-09-13 18:05:37 -040091
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020092 pos = *native;
93 pos += sprintf(pos, "efi_%pUl_", vendor);
94 utf16_utf8_strcpy(&pos, variable_name);
Rob Clark15f3d742017-09-13 18:05:37 -040095
96 return EFI_SUCCESS;
97}
98
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +010099/**
100 * prefix() - skip over prefix
101 *
102 * Skip over a prefix string.
103 *
104 * @str: string with prefix
105 * @prefix: prefix string
106 * Return: string without prefix, or NULL if prefix not found
107 */
Rob Clark15f3d742017-09-13 18:05:37 -0400108static const char *prefix(const char *str, const char *prefix)
109{
110 size_t n = strlen(prefix);
111 if (!strncmp(prefix, str, n))
112 return str + n;
113 return NULL;
114}
115
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100116/**
117 * parse_attr() - decode attributes part of variable value
118 *
119 * Convert the string encoded attributes of a UEFI variable to a bit mask.
120 * TODO: Several attributes are not supported.
121 *
122 * @str: value of U-Boot variable
123 * @attrp: pointer to UEFI attributes
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900124 * @timep: pointer to time attribute
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100125 * Return: pointer to remainder of U-Boot variable value
126 */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900127static const char *parse_attr(const char *str, u32 *attrp, u64 *timep)
Rob Clark15f3d742017-09-13 18:05:37 -0400128{
129 u32 attr = 0;
130 char sep = '{';
131
132 if (*str != '{') {
133 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
134 return str;
135 }
136
137 while (*str == sep) {
138 const char *s;
139
140 str++;
141
142 if ((s = prefix(str, "ro"))) {
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200143 attr |= EFI_VARIABLE_READ_ONLY;
AKASHI Takahiro7f334332019-06-04 15:52:06 +0900144 } else if ((s = prefix(str, "nv"))) {
145 attr |= EFI_VARIABLE_NON_VOLATILE;
Rob Clark15f3d742017-09-13 18:05:37 -0400146 } else if ((s = prefix(str, "boot"))) {
147 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
148 } else if ((s = prefix(str, "run"))) {
149 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900150 } else if ((s = prefix(str, "time="))) {
151 attr |= EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
152 hex2bin((u8 *)timep, s, sizeof(*timep));
153 s += sizeof(*timep) * 2;
154 } else if (*str == '}') {
155 break;
Rob Clark15f3d742017-09-13 18:05:37 -0400156 } else {
157 printf("invalid attribute: %s\n", str);
158 break;
159 }
160
161 str = s;
162 sep = ',';
163 }
164
165 str++;
166
167 *attrp = attr;
168
169 return str;
170}
171
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900172/**
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900173 * efi_set_secure_state - modify secure boot state variables
Heinrich Schuchardtbae548c2020-06-24 12:38:00 +0200174 * @secure_boot: value of SecureBoot
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900175 * @setup_mode: value of SetupMode
176 * @audit_mode: value of AuditMode
177 * @deployed_mode: value of DeployedMode
178 *
Heinrich Schuchardtbae548c2020-06-24 12:38:00 +0200179 * Modify secure boot status related variables as indicated.
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900180 *
181 * Return: status code
182 */
Heinrich Schuchardtbae548c2020-06-24 12:38:00 +0200183static efi_status_t efi_set_secure_state(u8 secure_boot, u8 setup_mode,
184 u8 audit_mode, u8 deployed_mode)
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900185{
186 u32 attributes;
187 efi_status_t ret;
188
189 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
190 EFI_VARIABLE_RUNTIME_ACCESS |
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200191 EFI_VARIABLE_READ_ONLY;
192 ret = efi_set_variable_int(L"SecureBoot", &efi_global_variable_guid,
193 attributes, sizeof(secure_boot),
194 &secure_boot, false);
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900195 if (ret != EFI_SUCCESS)
196 goto err;
197
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200198 ret = efi_set_variable_int(L"SetupMode", &efi_global_variable_guid,
199 attributes, sizeof(setup_mode),
200 &setup_mode, false);
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900201 if (ret != EFI_SUCCESS)
202 goto err;
203
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200204 ret = efi_set_variable_int(L"AuditMode", &efi_global_variable_guid,
205 attributes, sizeof(audit_mode),
206 &audit_mode, false);
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900207 if (ret != EFI_SUCCESS)
208 goto err;
209
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200210 ret = efi_set_variable_int(L"DeployedMode",
211 &efi_global_variable_guid, attributes,
212 sizeof(deployed_mode), &deployed_mode,
213 false);
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900214err:
215 return ret;
216}
217
218/**
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900219 * efi_transfer_secure_state - handle a secure boot state transition
220 * @mode: new state
221 *
222 * Depending on @mode, secure boot related variables are updated.
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200223 * Those variables are *read-only* for users, efi_set_variable_int()
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900224 * is called here.
225 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +0200226 * Return: status code
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900227 */
228static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
229{
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900230 efi_status_t ret;
231
AKASHI Takahiro37123732020-06-09 14:09:34 +0900232 EFI_PRINT("Switching secure state from %d to %d\n", efi_secure_mode,
233 mode);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900234
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900235 if (mode == EFI_MODE_DEPLOYED) {
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900236 ret = efi_set_secure_state(1, 0, 0, 1);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900237 if (ret != EFI_SUCCESS)
238 goto err;
239
240 efi_secure_boot = true;
241 } else if (mode == EFI_MODE_AUDIT) {
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200242 ret = efi_set_variable_int(L"PK", &efi_global_variable_guid,
243 EFI_VARIABLE_BOOTSERVICE_ACCESS |
244 EFI_VARIABLE_RUNTIME_ACCESS,
245 0, NULL, false);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900246 if (ret != EFI_SUCCESS)
247 goto err;
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900248
249 ret = efi_set_secure_state(0, 1, 1, 0);
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_USER) {
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900255 ret = efi_set_secure_state(1, 0, 0, 0);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900256 if (ret != EFI_SUCCESS)
257 goto err;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900258
259 efi_secure_boot = true;
260 } else if (mode == EFI_MODE_SETUP) {
AKASHI Takahirodae117a2020-04-21 09:39:20 +0900261 ret = efi_set_secure_state(0, 1, 0, 0);
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900262 if (ret != EFI_SUCCESS)
263 goto err;
264 } else {
265 return EFI_INVALID_PARAMETER;
266 }
267
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900268 efi_secure_mode = mode;
269
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900270 return EFI_SUCCESS;
271
272err:
273 /* TODO: What action should be taken here? */
274 printf("ERROR: Secure state transition failed\n");
275 return ret;
276}
277
278/**
279 * efi_init_secure_state - initialize secure boot state
280 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +0200281 * Return: status code
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900282 */
283static efi_status_t efi_init_secure_state(void)
284{
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900285 enum efi_secure_mode mode;
286 efi_uintn_t size;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900287 efi_status_t ret;
288
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900289 /*
290 * TODO:
291 * Since there is currently no "platform-specific" installation
292 * method of Platform Key, we can't say if VendorKeys is 0 or 1
293 * precisely.
294 */
295
296 size = 0;
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200297 ret = efi_get_variable_int(L"PK", &efi_global_variable_guid,
298 NULL, &size, NULL, NULL);
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900299 if (ret == EFI_BUFFER_TOO_SMALL) {
300 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT))
301 mode = EFI_MODE_USER;
302 else
303 mode = EFI_MODE_SETUP;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900304
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900305 efi_vendor_keys = 0;
306 } else if (ret == EFI_NOT_FOUND) {
307 mode = EFI_MODE_SETUP;
308 efi_vendor_keys = 1;
309 } else {
310 goto err;
311 }
312
313 ret = efi_transfer_secure_state(mode);
314 if (ret == EFI_SUCCESS)
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200315 ret = efi_set_variable_int(L"VendorKeys",
316 &efi_global_variable_guid,
317 EFI_VARIABLE_BOOTSERVICE_ACCESS |
318 EFI_VARIABLE_RUNTIME_ACCESS |
319 EFI_VARIABLE_READ_ONLY,
320 sizeof(efi_vendor_keys),
321 &efi_vendor_keys, false);
AKASHI Takahiro94b139a2020-04-14 11:51:43 +0900322
323err:
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900324 return ret;
325}
326
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100327/**
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900328 * efi_secure_boot_enabled - return if secure boot is enabled or not
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100329 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900330 * Return: true if enabled, false if disabled
331 */
332bool efi_secure_boot_enabled(void)
333{
334 return efi_secure_boot;
335}
336
337#ifdef CONFIG_EFI_SECURE_BOOT
338static u8 pkcs7_hdr[] = {
339 /* SEQUENCE */
340 0x30, 0x82, 0x05, 0xc7,
341 /* OID: pkcs7-signedData */
342 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
343 /* Context Structured? */
344 0xa0, 0x82, 0x05, 0xb8,
345};
346
347/**
348 * efi_variable_parse_signature - parse a signature in variable
349 * @buf: Pointer to variable's value
350 * @buflen: Length of @buf
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100351 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900352 * Parse a signature embedded in variable's value and instantiate
353 * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
354 * pkcs7's signedData, some header needed be prepended for correctly
355 * parsing authentication data, particularly for variable's.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100356 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900357 * Return: Pointer to pkcs7_message structure on success, NULL on error
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100358 */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900359static struct pkcs7_message *efi_variable_parse_signature(const void *buf,
360 size_t buflen)
361{
362 u8 *ebuf;
363 size_t ebuflen, len;
364 struct pkcs7_message *msg;
365
366 /*
367 * This is the best assumption to check if the binary is
368 * already in a form of pkcs7's signedData.
369 */
370 if (buflen > sizeof(pkcs7_hdr) &&
371 !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
372 msg = pkcs7_parse_message(buf, buflen);
373 goto out;
374 }
375
376 /*
377 * Otherwise, we should add a dummy prefix sequence for pkcs7
378 * message parser to be able to process.
379 * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
380 * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
381 * TODO:
382 * The header should be composed in a more refined manner.
383 */
AKASHI Takahiro37123732020-06-09 14:09:34 +0900384 EFI_PRINT("Makeshift prefix added to authentication data\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900385 ebuflen = sizeof(pkcs7_hdr) + buflen;
386 if (ebuflen <= 0x7f) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900387 EFI_PRINT("Data is too short\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900388 return NULL;
389 }
390
391 ebuf = malloc(ebuflen);
392 if (!ebuf) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900393 EFI_PRINT("Out of memory\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900394 return NULL;
395 }
396
397 memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
398 memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
399 len = ebuflen - 4;
400 ebuf[2] = (len >> 8) & 0xff;
401 ebuf[3] = len & 0xff;
402 len = ebuflen - 0x13;
403 ebuf[0x11] = (len >> 8) & 0xff;
404 ebuf[0x12] = len & 0xff;
405
406 msg = pkcs7_parse_message(ebuf, ebuflen);
407
408 free(ebuf);
409
410out:
411 if (IS_ERR(msg))
412 return NULL;
413
414 return msg;
415}
416
417/**
418 * efi_variable_authenticate - authenticate a variable
419 * @variable: Variable name in u16
420 * @vendor: Guid of variable
421 * @data_size: Size of @data
422 * @data: Pointer to variable's value
423 * @given_attr: Attributes to be given at SetVariable()
424 * @env_attr: Attributes that an existing variable holds
425 * @time: signed time that an existing variable holds
426 *
427 * Called by efi_set_variable() to verify that the input is correct.
428 * Will replace the given data pointer with another that points to
429 * the actual data to store in the internal memory.
430 * On success, @data and @data_size will be replaced with variable's
431 * actual data, excluding authentication data, and its size, and variable's
432 * attributes and signed time will also be returned in @env_attr and @time,
433 * respectively.
434 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +0200435 * Return: status code
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900436 */
437static efi_status_t efi_variable_authenticate(u16 *variable,
438 const efi_guid_t *vendor,
439 efi_uintn_t *data_size,
440 const void **data, u32 given_attr,
441 u32 *env_attr, u64 *time)
442{
443 const struct efi_variable_authentication_2 *auth;
444 struct efi_signature_store *truststore, *truststore2;
445 struct pkcs7_message *var_sig;
446 struct efi_image_regions *regs;
447 struct efi_time timestamp;
448 struct rtc_time tm;
449 u64 new_time;
450 efi_status_t ret;
451
452 var_sig = NULL;
453 truststore = NULL;
454 truststore2 = NULL;
455 regs = NULL;
456 ret = EFI_SECURITY_VIOLATION;
457
458 if (*data_size < sizeof(struct efi_variable_authentication_2))
459 goto err;
460
461 /* authentication data */
462 auth = *data;
463 if (*data_size < (sizeof(auth->time_stamp)
464 + auth->auth_info.hdr.dwLength))
465 goto err;
466
467 if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
468 goto err;
469
Heinrich Schuchardt7a76a3f2020-07-01 12:44:00 +0200470 memcpy(&timestamp, &auth->time_stamp, sizeof(timestamp));
471 if (timestamp.pad1 || timestamp.nanosecond || timestamp.timezone ||
472 timestamp.daylight || timestamp.pad2)
473 goto err;
474
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900475 *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
476 *data_size -= (sizeof(auth->time_stamp)
477 + auth->auth_info.hdr.dwLength);
478
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900479 memset(&tm, 0, sizeof(tm));
480 tm.tm_year = timestamp.year;
481 tm.tm_mon = timestamp.month;
482 tm.tm_mday = timestamp.day;
483 tm.tm_hour = timestamp.hour;
484 tm.tm_min = timestamp.minute;
485 tm.tm_sec = timestamp.second;
486 new_time = rtc_mktime(&tm);
487
488 if (!efi_secure_boot_enabled()) {
489 /* finished checking */
490 *time = new_time;
491 return EFI_SUCCESS;
492 }
493
494 if (new_time <= *time)
495 goto err;
496
497 /* data to be digested */
498 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
499 if (!regs)
500 goto err;
501 regs->max = 5;
502 efi_image_region_add(regs, (uint8_t *)variable,
503 (uint8_t *)variable
504 + u16_strlen(variable) * sizeof(u16), 1);
505 efi_image_region_add(regs, (uint8_t *)vendor,
506 (uint8_t *)vendor + sizeof(*vendor), 1);
507 efi_image_region_add(regs, (uint8_t *)&given_attr,
508 (uint8_t *)&given_attr + sizeof(given_attr), 1);
509 efi_image_region_add(regs, (uint8_t *)&timestamp,
510 (uint8_t *)&timestamp + sizeof(timestamp), 1);
511 efi_image_region_add(regs, (uint8_t *)*data,
512 (uint8_t *)*data + *data_size, 1);
513
514 /* variable's signature list */
515 if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
516 goto err;
517 var_sig = efi_variable_parse_signature(auth->auth_info.cert_data,
518 auth->auth_info.hdr.dwLength
519 - sizeof(auth->auth_info));
Patrick Wildtcd8a55b2020-05-07 02:13:18 +0200520 if (!var_sig) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900521 EFI_PRINT("Parsing variable's signature failed\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900522 goto err;
523 }
524
525 /* signature database used for authentication */
526 if (u16_strcmp(variable, L"PK") == 0 ||
527 u16_strcmp(variable, L"KEK") == 0) {
528 /* with PK */
529 truststore = efi_sigstore_parse_sigdb(L"PK");
530 if (!truststore)
531 goto err;
532 } else if (u16_strcmp(variable, L"db") == 0 ||
533 u16_strcmp(variable, L"dbx") == 0) {
534 /* with PK and KEK */
535 truststore = efi_sigstore_parse_sigdb(L"KEK");
536 truststore2 = efi_sigstore_parse_sigdb(L"PK");
537
538 if (!truststore) {
539 if (!truststore2)
540 goto err;
541
542 truststore = truststore2;
543 truststore2 = NULL;
544 }
545 } else {
546 /* TODO: support private authenticated variables */
547 goto err;
548 }
549
550 /* verify signature */
551 if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900552 EFI_PRINT("Verified\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900553 } else {
554 if (truststore2 &&
555 efi_signature_verify_with_sigdb(regs, var_sig,
556 truststore2, NULL)) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900557 EFI_PRINT("Verified\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900558 } else {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900559 EFI_PRINT("Verifying variable's signature failed\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900560 goto err;
561 }
562 }
563
564 /* finished checking */
Heinrich Schuchardt5106dc72020-06-30 12:02:14 +0200565 *time = new_time;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900566 ret = EFI_SUCCESS;
567
568err:
569 efi_sigstore_free(truststore);
570 efi_sigstore_free(truststore2);
571 pkcs7_free_message(var_sig);
572 free(regs);
573
574 return ret;
575}
576#else
577static efi_status_t efi_variable_authenticate(u16 *variable,
578 const efi_guid_t *vendor,
579 efi_uintn_t *data_size,
580 const void **data, u32 given_attr,
581 u32 *env_attr, u64 *time)
582{
583 return EFI_SUCCESS;
584}
585#endif /* CONFIG_EFI_SECURE_BOOT */
586
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200587efi_status_t efi_get_variable_int(u16 *variable_name, const efi_guid_t *vendor,
588 u32 *attributes, efi_uintn_t *data_size,
589 void *data, u64 *timep)
Rob Clark15f3d742017-09-13 18:05:37 -0400590{
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200591 char *native_name;
Rob Clark15f3d742017-09-13 18:05:37 -0400592 efi_status_t ret;
593 unsigned long in_size;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900594 const char *val = NULL, *s;
595 u64 time = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400596 u32 attr;
597
Rob Clark15f3d742017-09-13 18:05:37 -0400598 if (!variable_name || !vendor || !data_size)
Heinrich Schuchardt563e5522020-06-29 11:49:58 +0200599 return EFI_INVALID_PARAMETER;
Rob Clark15f3d742017-09-13 18:05:37 -0400600
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200601 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400602 if (ret)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900603 return ret;
Rob Clark15f3d742017-09-13 18:05:37 -0400604
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200605 EFI_PRINT("get '%s'\n", native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400606
607 val = env_get(native_name);
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200608 free(native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400609 if (!val)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900610 return EFI_NOT_FOUND;
Rob Clark15f3d742017-09-13 18:05:37 -0400611
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900612 val = parse_attr(val, &attr, &time);
Rob Clark15f3d742017-09-13 18:05:37 -0400613
Heinrich Schuchardt2db39ef2020-07-01 15:32:47 +0200614 if (timep)
615 *timep = time;
616
Rob Clark15f3d742017-09-13 18:05:37 -0400617 in_size = *data_size;
618
619 if ((s = prefix(val, "(blob)"))) {
Heinrich Schuchardt2d8aedc2019-01-18 12:31:54 +0100620 size_t len = strlen(s);
Rob Clark15f3d742017-09-13 18:05:37 -0400621
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700622 /* number of hexadecimal digits must be even */
623 if (len & 1)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900624 return EFI_DEVICE_ERROR;
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700625
Rob Clark15f3d742017-09-13 18:05:37 -0400626 /* two characters per byte: */
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700627 len /= 2;
Rob Clark15f3d742017-09-13 18:05:37 -0400628 *data_size = len;
629
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200630 if (in_size < len) {
631 ret = EFI_BUFFER_TOO_SMALL;
632 goto out;
633 }
Rob Clark15f3d742017-09-13 18:05:37 -0400634
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900635 if (!data) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900636 EFI_PRINT("Variable with no data shouldn't exist.\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900637 return EFI_INVALID_PARAMETER;
638 }
Rob Clark15f3d742017-09-13 18:05:37 -0400639
Heinrich Schuchardt2d8aedc2019-01-18 12:31:54 +0100640 if (hex2bin(data, s, len))
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900641 return EFI_DEVICE_ERROR;
Rob Clark15f3d742017-09-13 18:05:37 -0400642
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200643 EFI_PRINT("got value: \"%s\"\n", s);
Rob Clark15f3d742017-09-13 18:05:37 -0400644 } else if ((s = prefix(val, "(utf8)"))) {
645 unsigned len = strlen(s) + 1;
646
647 *data_size = len;
648
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200649 if (in_size < len) {
650 ret = EFI_BUFFER_TOO_SMALL;
651 goto out;
652 }
Rob Clark15f3d742017-09-13 18:05:37 -0400653
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900654 if (!data) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900655 EFI_PRINT("Variable with no data shouldn't exist.\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900656 return EFI_INVALID_PARAMETER;
657 }
Rob Clark15f3d742017-09-13 18:05:37 -0400658
659 memcpy(data, s, len);
660 ((char *)data)[len] = '\0';
661
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200662 EFI_PRINT("got value: \"%s\"\n", (char *)data);
Rob Clark15f3d742017-09-13 18:05:37 -0400663 } else {
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200664 EFI_PRINT("invalid value: '%s'\n", val);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900665 return EFI_DEVICE_ERROR;
Rob Clark15f3d742017-09-13 18:05:37 -0400666 }
667
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200668out:
Rob Clark15f3d742017-09-13 18:05:37 -0400669 if (attributes)
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200670 *attributes = attr;
Rob Clark15f3d742017-09-13 18:05:37 -0400671
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900672 return ret;
673}
674
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100675static char *efi_variables_list;
676static char *efi_cur_variable;
677
678/**
679 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
680 * information
681 * @variable: whole data of u-boot variable (ie. name=value)
682 * @variable_name_size: size of variable_name buffer in byte
683 * @variable_name: name of uefi variable in u16, null-terminated
684 * @vendor: vendor's guid
685 * @attributes: attributes
686 *
687 * A uefi variable is encoded into a u-boot variable as described above.
688 * This function parses such a u-boot variable and retrieve uefi-related
689 * information into respective parameters. In return, variable_name_size
690 * is the size of variable name including NULL.
691 *
692 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200693 * the entire variable list has been returned,
694 * otherwise non-zero status code
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100695 */
696static efi_status_t parse_uboot_variable(char *variable,
697 efi_uintn_t *variable_name_size,
698 u16 *variable_name,
699 const efi_guid_t *vendor,
700 u32 *attributes)
701{
702 char *guid, *name, *end, c;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100703 size_t name_len;
704 efi_uintn_t old_variable_name_size;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900705 u64 time;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100706 u16 *p;
707
708 guid = strchr(variable, '_');
709 if (!guid)
710 return EFI_INVALID_PARAMETER;
711 guid++;
712 name = strchr(guid, '_');
713 if (!name)
714 return EFI_INVALID_PARAMETER;
715 name++;
716 end = strchr(name, '=');
717 if (!end)
718 return EFI_INVALID_PARAMETER;
719
720 name_len = end - name;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100721 old_variable_name_size = *variable_name_size;
722 *variable_name_size = sizeof(u16) * (name_len + 1);
723 if (old_variable_name_size < *variable_name_size)
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100724 return EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100725
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100726 end++; /* point to value */
727
728 /* variable name */
729 p = variable_name;
730 utf8_utf16_strncpy(&p, name, name_len);
731 variable_name[name_len] = 0;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100732
733 /* guid */
734 c = *(name - 1);
735 *(name - 1) = '\0'; /* guid need be null-terminated here */
AKASHI Takahiro4854f782020-05-08 14:51:21 +0900736 if (uuid_str_to_bin(guid, (unsigned char *)vendor,
737 UUID_STR_FORMAT_GUID))
738 /* The only error would be EINVAL. */
739 return EFI_INVALID_PARAMETER;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100740 *(name - 1) = c;
741
742 /* attributes */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900743 parse_attr(end, attributes, &time);
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100744
745 return EFI_SUCCESS;
746}
747
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200748efi_status_t efi_get_next_variable_name_int(efi_uintn_t *variable_name_size,
749 u16 *variable_name,
750 efi_guid_t *vendor)
Rob Clark15f3d742017-09-13 18:05:37 -0400751{
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100752 char *native_name, *variable;
753 ssize_t name_len, list_len;
754 char regex[256];
755 char * const regexlist[] = {regex};
756 u32 attributes;
757 int i;
758 efi_status_t ret;
759
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100760 if (!variable_name_size || !variable_name || !vendor)
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200761 return EFI_INVALID_PARAMETER;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100762
763 if (variable_name[0]) {
764 /* check null-terminated string */
765 for (i = 0; i < *variable_name_size; i++)
766 if (!variable_name[i])
767 break;
768 if (i >= *variable_name_size)
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200769 return EFI_INVALID_PARAMETER;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100770
771 /* search for the last-returned variable */
772 ret = efi_to_native(&native_name, variable_name, vendor);
773 if (ret)
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200774 return ret;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100775
776 name_len = strlen(native_name);
777 for (variable = efi_variables_list; variable && *variable;) {
778 if (!strncmp(variable, native_name, name_len) &&
779 variable[name_len] == '=')
780 break;
781
782 variable = strchr(variable, '\n');
783 if (variable)
784 variable++;
785 }
786
787 free(native_name);
788 if (!(variable && *variable))
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200789 return EFI_INVALID_PARAMETER;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100790
791 /* next variable */
792 variable = strchr(variable, '\n');
793 if (variable)
794 variable++;
795 if (!(variable && *variable))
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200796 return EFI_NOT_FOUND;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100797 } else {
798 /*
799 *new search: free a list used in the previous search
800 */
801 free(efi_variables_list);
802 efi_variables_list = NULL;
803 efi_cur_variable = NULL;
804
805 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
806 list_len = hexport_r(&env_htab, '\n',
807 H_MATCH_REGEX | H_MATCH_KEY,
808 &efi_variables_list, 0, 1, regexlist);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900809
Heinrich Schuchardtfd738482019-01-22 20:10:46 +0100810 if (list_len <= 1)
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200811 return EFI_NOT_FOUND;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100812
813 variable = efi_variables_list;
814 }
815
816 ret = parse_uboot_variable(variable, variable_name_size, variable_name,
817 vendor, &attributes);
818
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200819 return ret;
Rob Clark15f3d742017-09-13 18:05:37 -0400820}
821
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200822efi_status_t efi_set_variable_int(u16 *variable_name, const efi_guid_t *vendor,
823 u32 attributes, efi_uintn_t data_size,
824 const void *data, bool ro_check)
Rob Clark15f3d742017-09-13 18:05:37 -0400825{
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900826 char *native_name = NULL, *old_data = NULL, *val = NULL, *s;
827 efi_uintn_t old_size;
828 bool append, delete;
829 u64 time = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400830 u32 attr;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900831 efi_status_t ret = EFI_SUCCESS;
Rob Clark15f3d742017-09-13 18:05:37 -0400832
Heinrich Schuchardtadba3962019-06-12 23:28:42 +0200833 if (!variable_name || !*variable_name || !vendor ||
834 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900835 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200836 ret = EFI_INVALID_PARAMETER;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900837 goto err;
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200838 }
Rob Clark15f3d742017-09-13 18:05:37 -0400839
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200840 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400841 if (ret)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900842 goto err;
Rob Clark15f3d742017-09-13 18:05:37 -0400843
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900844 /* check if a variable exists */
845 old_size = 0;
846 attr = 0;
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200847 ret = efi_get_variable_int(variable_name, vendor, &attr,
848 &old_size, NULL, &time);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900849 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
850 attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
851 delete = !append && (!data_size || !attributes);
852
853 /* check attributes */
854 if (old_size) {
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200855 if (ro_check && (attr & EFI_VARIABLE_READ_ONLY)) {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900856 ret = EFI_WRITE_PROTECTED;
857 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900858 }
859
860 /* attributes won't be changed */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900861 if (!delete &&
862 ((ro_check && attr != attributes) ||
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200863 (!ro_check && ((attr & ~(u32)EFI_VARIABLE_READ_ONLY)
864 != (attributes & ~(u32)EFI_VARIABLE_READ_ONLY))))) {
AKASHI Takahiro186b5a42019-05-24 15:59:03 +0900865 ret = EFI_INVALID_PARAMETER;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900866 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900867 }
868 } else {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900869 if (delete || append) {
Heinrich Schuchardt8021bbe2019-09-26 21:40:18 +0200870 /*
871 * Trying to delete or to update a non-existent
872 * variable.
873 */
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900874 ret = EFI_NOT_FOUND;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900875 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900876 }
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900877 }
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900878
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900879 if (((!u16_strcmp(variable_name, L"PK") ||
880 !u16_strcmp(variable_name, L"KEK")) &&
881 !guidcmp(vendor, &efi_global_variable_guid)) ||
882 ((!u16_strcmp(variable_name, L"db") ||
883 !u16_strcmp(variable_name, L"dbx")) &&
884 !guidcmp(vendor, &efi_guid_image_security_database))) {
885 /* authentication is mandatory */
886 if (!(attributes &
887 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900888 EFI_PRINT("%ls: AUTHENTICATED_WRITE_ACCESS required\n",
889 variable_name);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900890 ret = EFI_INVALID_PARAMETER;
891 goto err;
892 }
893 }
894
895 /* authenticate a variable */
896 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
897 if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
898 ret = EFI_INVALID_PARAMETER;
899 goto err;
900 }
901 if (attributes &
902 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
903 ret = efi_variable_authenticate(variable_name, vendor,
904 &data_size, &data,
905 attributes, &attr,
906 &time);
907 if (ret != EFI_SUCCESS)
908 goto err;
909
910 /* last chance to check for delete */
911 if (!data_size)
912 delete = true;
913 }
914 } else {
915 if (attributes &
916 (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
917 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
AKASHI Takahiro37123732020-06-09 14:09:34 +0900918 EFI_PRINT("Secure boot is not configured\n");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900919 ret = EFI_INVALID_PARAMETER;
920 goto err;
921 }
922 }
923
924 /* delete a variable */
925 if (delete) {
926 /* !old_size case has been handled before */
927 val = NULL;
928 ret = EFI_SUCCESS;
929 goto out;
930 }
931
932 if (append) {
933 old_data = malloc(old_size);
934 if (!old_data) {
Heinrich Schuchardtebbda7b2020-05-06 01:37:25 +0200935 ret = EFI_OUT_OF_RESOURCES;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900936 goto err;
937 }
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200938 ret = efi_get_variable_int(variable_name, vendor,
939 &attr, &old_size, old_data, NULL);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900940 if (ret != EFI_SUCCESS)
941 goto err;
942 } else {
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900943 old_size = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400944 }
945
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900946 val = malloc(2 * old_size + 2 * data_size
947 + strlen("{ro,run,boot,nv,time=0123456701234567}(blob)")
948 + 1);
Heinrich Schuchardtbd095f52018-10-02 05:30:05 +0200949 if (!val) {
950 ret = EFI_OUT_OF_RESOURCES;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900951 goto err;
Heinrich Schuchardtbd095f52018-10-02 05:30:05 +0200952 }
Rob Clark15f3d742017-09-13 18:05:37 -0400953
954 s = val;
955
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900956 /*
957 * store attributes
958 */
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200959 attributes &= (EFI_VARIABLE_READ_ONLY |
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900960 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro7f334332019-06-04 15:52:06 +0900961 EFI_VARIABLE_BOOTSERVICE_ACCESS |
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900962 EFI_VARIABLE_RUNTIME_ACCESS |
963 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS);
Rob Clark15f3d742017-09-13 18:05:37 -0400964 s += sprintf(s, "{");
965 while (attributes) {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900966 attr = 1 << (ffs(attributes) - 1);
Rob Clark15f3d742017-09-13 18:05:37 -0400967
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200968 if (attr == EFI_VARIABLE_READ_ONLY) {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900969 s += sprintf(s, "ro");
970 } else if (attr == EFI_VARIABLE_NON_VOLATILE) {
AKASHI Takahiro7f334332019-06-04 15:52:06 +0900971 s += sprintf(s, "nv");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900972 } else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) {
Rob Clark15f3d742017-09-13 18:05:37 -0400973 s += sprintf(s, "boot");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900974 } else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) {
Rob Clark15f3d742017-09-13 18:05:37 -0400975 s += sprintf(s, "run");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900976 } else if (attr ==
977 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
978 s += sprintf(s, "time=");
979 s = bin2hex(s, (u8 *)&time, sizeof(time));
980 }
Rob Clark15f3d742017-09-13 18:05:37 -0400981
982 attributes &= ~attr;
983 if (attributes)
984 s += sprintf(s, ",");
985 }
986 s += sprintf(s, "}");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900987 s += sprintf(s, "(blob)");
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900988
Rob Clark15f3d742017-09-13 18:05:37 -0400989 /* store payload: */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900990 if (append)
991 s = bin2hex(s, old_data, old_size);
Heinrich Schuchardt807899d2019-01-18 18:54:26 +0100992 s = bin2hex(s, data, data_size);
Rob Clark15f3d742017-09-13 18:05:37 -0400993 *s = '\0';
994
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200995 EFI_PRINT("setting: %s=%s\n", native_name, val);
Rob Clark15f3d742017-09-13 18:05:37 -0400996
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900997out:
AKASHI Takahiroc78dc192020-04-14 11:51:42 +0900998 if (env_set(native_name, val)) {
Rob Clark15f3d742017-09-13 18:05:37 -0400999 ret = EFI_DEVICE_ERROR;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001000 } else {
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001001 bool vendor_keys_modified = false;
1002
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001003 if ((u16_strcmp(variable_name, L"PK") == 0 &&
1004 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1005 ret = efi_transfer_secure_state(
1006 (delete ? EFI_MODE_SETUP :
1007 EFI_MODE_USER));
1008 if (ret != EFI_SUCCESS)
1009 goto err;
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001010
1011 if (efi_secure_mode != EFI_MODE_SETUP)
1012 vendor_keys_modified = true;
1013 } else if ((u16_strcmp(variable_name, L"KEK") == 0 &&
1014 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1015 if (efi_secure_mode != EFI_MODE_SETUP)
1016 vendor_keys_modified = true;
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001017 }
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001018
1019 /* update VendorKeys */
1020 if (vendor_keys_modified & efi_vendor_keys) {
1021 efi_vendor_keys = 0;
Heinrich Schuchardt9827e842020-06-22 18:10:27 +02001022 ret = efi_set_variable_int(
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001023 L"VendorKeys",
1024 &efi_global_variable_guid,
1025 EFI_VARIABLE_BOOTSERVICE_ACCESS
1026 | EFI_VARIABLE_RUNTIME_ACCESS
Heinrich Schuchardt9827e842020-06-22 18:10:27 +02001027 | EFI_VARIABLE_READ_ONLY,
AKASHI Takahiro94b139a2020-04-14 11:51:43 +09001028 sizeof(efi_vendor_keys),
1029 &efi_vendor_keys,
1030 false);
1031 } else {
1032 ret = EFI_SUCCESS;
1033 }
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001034 }
Rob Clark15f3d742017-09-13 18:05:37 -04001035
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001036err:
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +02001037 free(native_name);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001038 free(old_data);
Rob Clark15f3d742017-09-13 18:05:37 -04001039 free(val);
1040
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001041 return ret;
1042}
1043
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +02001044efi_status_t efi_query_variable_info_int(u32 attributes,
1045 u64 *maximum_variable_storage_size,
1046 u64 *remaining_variable_storage_size,
1047 u64 *maximum_variable_size)
1048{
1049 return EFI_UNSUPPORTED;
1050}
1051
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +09001052/**
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +02001053 * efi_query_variable_info_runtime() - runtime implementation of
1054 * QueryVariableInfo()
Heinrich Schuchardt9b19dae2019-06-20 12:13:05 +02001055 *
1056 * @attributes: bitmask to select variables to be
1057 * queried
1058 * @maximum_variable_storage_size: maximum size of storage area for the
1059 * selected variable types
1060 * @remaining_variable_storage_size: remaining size of storage are for the
1061 * selected variable types
1062 * @maximum_variable_size: maximum size of a variable of the
1063 * selected type
1064 * Returns: status code
1065 */
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +02001066efi_status_t __efi_runtime EFIAPI efi_query_variable_info_runtime(
Heinrich Schuchardt9b19dae2019-06-20 12:13:05 +02001067 u32 attributes,
1068 u64 *maximum_variable_storage_size,
1069 u64 *remaining_variable_storage_size,
1070 u64 *maximum_variable_size)
1071{
1072 return EFI_UNSUPPORTED;
1073}
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001074
1075/**
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001076 * efi_get_variable_runtime() - runtime implementation of GetVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001077 *
1078 * @variable_name: name of the variable
1079 * @vendor: vendor GUID
1080 * @attributes: attributes of the variable
1081 * @data_size: size of the buffer to which the variable value is copied
1082 * @data: buffer to which the variable value is copied
1083 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001084 */
1085static efi_status_t __efi_runtime EFIAPI
1086efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1087 u32 *attributes, efi_uintn_t *data_size, void *data)
1088{
1089 return EFI_UNSUPPORTED;
1090}
1091
1092/**
1093 * efi_get_next_variable_name_runtime() - runtime implementation of
1094 * GetNextVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001095 *
1096 * @variable_name_size: size of variable_name buffer in byte
1097 * @variable_name: name of uefi variable's name in u16
1098 * @vendor: vendor's guid
1099 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001100 */
1101static efi_status_t __efi_runtime EFIAPI
1102efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
Heinrich Schuchardt82cd6c42020-03-22 18:28:20 +01001103 u16 *variable_name, efi_guid_t *vendor)
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001104{
1105 return EFI_UNSUPPORTED;
1106}
1107
1108/**
1109 * efi_set_variable_runtime() - runtime implementation of SetVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001110 *
1111 * @variable_name: name of the variable
1112 * @vendor: vendor GUID
1113 * @attributes: attributes of the variable
1114 * @data_size: size of the buffer with the variable value
1115 * @data: buffer with the variable value
1116 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001117 */
1118static efi_status_t __efi_runtime EFIAPI
1119efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1120 u32 attributes, efi_uintn_t data_size,
1121 const void *data)
1122{
1123 return EFI_UNSUPPORTED;
1124}
1125
1126/**
1127 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
1128 */
1129void efi_variables_boot_exit_notify(void)
1130{
1131 efi_runtime_services.get_variable = efi_get_variable_runtime;
1132 efi_runtime_services.get_next_variable_name =
1133 efi_get_next_variable_name_runtime;
1134 efi_runtime_services.set_variable = efi_set_variable_runtime;
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +02001135 efi_runtime_services.query_variable_info =
1136 efi_query_variable_info_runtime;
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001137 efi_update_table_header_crc32(&efi_runtime_services.hdr);
1138}
1139
1140/**
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001141 * efi_init_variables() - initialize variable services
1142 *
1143 * Return: status code
1144 */
1145efi_status_t efi_init_variables(void)
1146{
AKASHI Takahiroc78dc192020-04-14 11:51:42 +09001147 efi_status_t ret;
1148
1149 ret = efi_init_secure_state();
1150
1151 return ret;
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001152}