1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI variable service via OP-TEE
4  *
5  *  Copyright (C) 2019 Linaro Ltd. <sughosh.ganu@linaro.org>
6  *  Copyright (C) 2019 Linaro Ltd. <ilias.apalodimas@linaro.org>
7  */
8 
9 #include <common.h>
10 #include <efi.h>
11 #include <efi_api.h>
12 #include <efi_loader.h>
13 #include <efi_variable.h>
14 #include <tee.h>
15 #include <malloc.h>
16 #include <mm_communication.h>
17 
18 #define OPTEE_PAGE_SIZE BIT(12)
19 extern struct efi_var_file __efi_runtime_data *efi_var_buf;
20 static efi_uintn_t max_buffer_size;	/* comm + var + func + data */
21 static efi_uintn_t max_payload_size;	/* func + data */
22 
23 struct mm_connection {
24 	struct udevice *tee;
25 	u32 session;
26 };
27 
28 /**
29  * get_connection() - Retrieve OP-TEE session for a specific UUID.
30  *
31  * @conn:   session buffer to fill
32  * Return:  status code
33  */
get_connection(struct mm_connection * conn)34 static int get_connection(struct mm_connection *conn)
35 {
36 	static const struct tee_optee_ta_uuid uuid = PTA_STMM_UUID;
37 	struct udevice *tee = NULL;
38 	struct tee_open_session_arg arg;
39 	int rc = -ENODEV;
40 
41 	tee = tee_find_device(tee, NULL, NULL, NULL);
42 	if (!tee)
43 		goto out;
44 
45 	memset(&arg, 0, sizeof(arg));
46 	tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
47 	rc = tee_open_session(tee, &arg, 0, NULL);
48 	if (rc)
49 		goto out;
50 
51 	/* Check the internal OP-TEE result */
52 	if (arg.ret != TEE_SUCCESS) {
53 		rc = -EIO;
54 		goto out;
55 	}
56 
57 	conn->tee = tee;
58 	conn->session = arg.session;
59 
60 	return 0;
61 out:
62 	return rc;
63 }
64 
65 /**
66  * optee_mm_communicate() - Pass a buffer to StandaloneMM running in OP-TEE
67  *
68  * @comm_buf:		locally allocted communcation buffer
69  * @dsize:		buffer size
70  * Return:		status code
71  */
optee_mm_communicate(void * comm_buf,ulong dsize)72 static efi_status_t optee_mm_communicate(void *comm_buf, ulong dsize)
73 {
74 	ulong buf_size;
75 	efi_status_t ret;
76 	struct efi_mm_communicate_header *mm_hdr;
77 	struct mm_connection conn = { NULL, 0 };
78 	struct tee_invoke_arg arg;
79 	struct tee_param param[2];
80 	struct tee_shm *shm = NULL;
81 	int rc;
82 
83 	if (!comm_buf)
84 		return EFI_INVALID_PARAMETER;
85 
86 	mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
87 	buf_size = mm_hdr->message_len + sizeof(efi_guid_t) + sizeof(size_t);
88 
89 	if (dsize != buf_size)
90 		return EFI_INVALID_PARAMETER;
91 
92 	rc = get_connection(&conn);
93 	if (rc) {
94 		log_err("Unable to open OP-TEE session (err=%d)\n", rc);
95 		return EFI_UNSUPPORTED;
96 	}
97 
98 	if (tee_shm_register(conn.tee, comm_buf, buf_size, 0, &shm)) {
99 		log_err("Unable to register shared memory\n");
100 		tee_close_session(conn.tee, conn.session);
101 		return EFI_UNSUPPORTED;
102 	}
103 
104 	memset(&arg, 0, sizeof(arg));
105 	arg.func = PTA_STMM_CMDID_COMMUNICATE;
106 	arg.session = conn.session;
107 
108 	memset(param, 0, sizeof(param));
109 	param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT;
110 	param[0].u.memref.size = buf_size;
111 	param[0].u.memref.shm = shm;
112 	param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT;
113 
114 	rc = tee_invoke_func(conn.tee, &arg, 2, param);
115 	tee_shm_free(shm);
116 	tee_close_session(conn.tee, conn.session);
117 	if (rc || arg.ret != TEE_SUCCESS)
118 		return EFI_DEVICE_ERROR;
119 
120 	switch (param[1].u.value.a) {
121 	case ARM_SVC_SPM_RET_SUCCESS:
122 		ret = EFI_SUCCESS;
123 		break;
124 
125 	case ARM_SVC_SPM_RET_INVALID_PARAMS:
126 		ret = EFI_INVALID_PARAMETER;
127 		break;
128 
129 	case ARM_SVC_SPM_RET_DENIED:
130 		ret = EFI_ACCESS_DENIED;
131 		break;
132 
133 	case ARM_SVC_SPM_RET_NO_MEMORY:
134 		ret = EFI_OUT_OF_RESOURCES;
135 		break;
136 
137 	default:
138 		ret = EFI_ACCESS_DENIED;
139 	}
140 
141 	return ret;
142 }
143 
144 /**
145  * mm_communicate() - Adjust the cmonnucation buffer to StandAlonneMM and send
146  * it to OP-TEE
147  *
148  * @comm_buf:		locally allocted communcation buffer
149  * @dsize:		buffer size
150  * Return:		status code
151  */
mm_communicate(u8 * comm_buf,efi_uintn_t dsize)152 static efi_status_t mm_communicate(u8 *comm_buf, efi_uintn_t dsize)
153 {
154 	efi_status_t ret;
155 	struct efi_mm_communicate_header *mm_hdr;
156 	struct smm_variable_communicate_header *var_hdr;
157 
158 	dsize += MM_COMMUNICATE_HEADER_SIZE + MM_VARIABLE_COMMUNICATE_SIZE;
159 	mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
160 	var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
161 
162 	ret = optee_mm_communicate(comm_buf, dsize);
163 	if (ret != EFI_SUCCESS) {
164 		log_err("%s failed!\n", __func__);
165 		return ret;
166 	}
167 
168 	return var_hdr->ret_status;
169 }
170 
171 /**
172  * setup_mm_hdr() -	Allocate a buffer for StandAloneMM and initialize the
173  *			header data.
174  *
175  * @dptr:		pointer address of the corresponding StandAloneMM
176  *			function
177  * @payload_size:	buffer size
178  * @func:		standAloneMM function number
179  * @ret:		EFI return code
180  * Return:		buffer or NULL
181  */
setup_mm_hdr(void ** dptr,efi_uintn_t payload_size,efi_uintn_t func,efi_status_t * ret)182 static u8 *setup_mm_hdr(void **dptr, efi_uintn_t payload_size,
183 			efi_uintn_t func, efi_status_t *ret)
184 {
185 	const efi_guid_t mm_var_guid = EFI_MM_VARIABLE_GUID;
186 	struct efi_mm_communicate_header *mm_hdr;
187 	struct smm_variable_communicate_header *var_hdr;
188 	u8 *comm_buf;
189 
190 	/* In the init function we initialize max_buffer_size with
191 	 * get_max_payload(). So skip the test if max_buffer_size is initialized
192 	 * StandAloneMM will perform similar checks and drop the buffer if it's
193 	 * too long
194 	 */
195 	if (max_buffer_size && max_buffer_size <
196 			(MM_COMMUNICATE_HEADER_SIZE +
197 			 MM_VARIABLE_COMMUNICATE_SIZE +
198 			 payload_size)) {
199 		*ret = EFI_INVALID_PARAMETER;
200 		return NULL;
201 	}
202 
203 	comm_buf = calloc(1, MM_COMMUNICATE_HEADER_SIZE +
204 			  MM_VARIABLE_COMMUNICATE_SIZE +
205 			  payload_size);
206 	if (!comm_buf) {
207 		*ret = EFI_OUT_OF_RESOURCES;
208 		return NULL;
209 	}
210 
211 	mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
212 	guidcpy(&mm_hdr->header_guid, &mm_var_guid);
213 	mm_hdr->message_len = MM_VARIABLE_COMMUNICATE_SIZE + payload_size;
214 
215 	var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
216 	var_hdr->function = func;
217 	if (dptr)
218 		*dptr = var_hdr->data;
219 	*ret = EFI_SUCCESS;
220 
221 	return comm_buf;
222 }
223 
224 /**
225  * get_max_payload() - Get variable payload size from StandAloneMM.
226  *
227  * @size:    size of the variable in storage
228  * Return:   status code
229  */
get_max_payload(efi_uintn_t * size)230 efi_status_t EFIAPI get_max_payload(efi_uintn_t *size)
231 {
232 	struct smm_variable_payload_size *var_payload = NULL;
233 	efi_uintn_t payload_size;
234 	u8 *comm_buf = NULL;
235 	efi_status_t ret;
236 
237 	if (!size) {
238 		ret = EFI_INVALID_PARAMETER;
239 		goto out;
240 	}
241 
242 	payload_size = sizeof(*var_payload);
243 	comm_buf = setup_mm_hdr((void **)&var_payload, payload_size,
244 				SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE, &ret);
245 	if (!comm_buf)
246 		goto out;
247 
248 	ret = mm_communicate(comm_buf, payload_size);
249 	if (ret != EFI_SUCCESS)
250 		goto out;
251 
252 	/* Make sure the buffer is big enough for storing variables */
253 	if (var_payload->size < MM_VARIABLE_ACCESS_HEADER_SIZE + 0x20) {
254 		ret = EFI_DEVICE_ERROR;
255 		goto out;
256 	}
257 	*size = var_payload->size;
258 	/*
259 	 * Although the max payload is configurable on StMM, we only share a
260 	 * single page from OP-TEE for the non-secure buffer used to communicate
261 	 * with StMM. Since OP-TEE will reject to map anything bigger than that,
262 	 * make sure we are in bounds.
263 	 */
264 	if (*size > OPTEE_PAGE_SIZE)
265 		*size = OPTEE_PAGE_SIZE - MM_COMMUNICATE_HEADER_SIZE  -
266 			MM_VARIABLE_COMMUNICATE_SIZE;
267 	/*
268 	 * There seems to be a bug in EDK2 miscalculating the boundaries and
269 	 * size checks, so deduct 2 more bytes to fulfill this requirement. Fix
270 	 * it up here to ensure backwards compatibility with older versions
271 	 * (cf. StandaloneMmPkg/Drivers/StandaloneMmCpu/AArch64/EventHandle.c.
272 	 * sizeof (EFI_MM_COMMUNICATE_HEADER) instead the size minus the
273 	 * flexible array member).
274 	 *
275 	 * size is guaranteed to be > 2 due to checks on the beginning.
276 	 */
277 	*size -= 2;
278 out:
279 	free(comm_buf);
280 	return ret;
281 }
282 
283 /*
284  * StMM can store internal attributes and properties for variables, i.e enabling
285  * R/O variables
286  */
set_property_int(const u16 * variable_name,efi_uintn_t name_size,const efi_guid_t * vendor,struct var_check_property * var_property)287 static efi_status_t set_property_int(const u16 *variable_name,
288 				     efi_uintn_t name_size,
289 				     const efi_guid_t *vendor,
290 				     struct var_check_property *var_property)
291 {
292 	struct smm_variable_var_check_property *smm_property;
293 	efi_uintn_t payload_size;
294 	u8 *comm_buf = NULL;
295 	efi_status_t ret;
296 
297 	payload_size = sizeof(*smm_property) + name_size;
298 	if (payload_size > max_payload_size) {
299 		ret = EFI_INVALID_PARAMETER;
300 		goto out;
301 	}
302 	comm_buf = setup_mm_hdr((void **)&smm_property, payload_size,
303 				SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET,
304 				&ret);
305 	if (!comm_buf)
306 		goto out;
307 
308 	guidcpy(&smm_property->guid, vendor);
309 	smm_property->name_size = name_size;
310 	memcpy(&smm_property->property, var_property,
311 	       sizeof(smm_property->property));
312 	memcpy(smm_property->name, variable_name, name_size);
313 
314 	ret = mm_communicate(comm_buf, payload_size);
315 
316 out:
317 	free(comm_buf);
318 	return ret;
319 }
320 
get_property_int(const u16 * variable_name,efi_uintn_t name_size,const efi_guid_t * vendor,struct var_check_property * var_property)321 static efi_status_t get_property_int(const u16 *variable_name,
322 				     efi_uintn_t name_size,
323 				     const efi_guid_t *vendor,
324 				     struct var_check_property *var_property)
325 {
326 	struct smm_variable_var_check_property *smm_property;
327 	efi_uintn_t payload_size;
328 	u8 *comm_buf = NULL;
329 	efi_status_t ret;
330 
331 	memset(var_property, 0, sizeof(*var_property));
332 	payload_size = sizeof(*smm_property) + name_size;
333 	if (payload_size > max_payload_size) {
334 		ret = EFI_INVALID_PARAMETER;
335 		goto out;
336 	}
337 	comm_buf = setup_mm_hdr((void **)&smm_property, payload_size,
338 				SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET,
339 				&ret);
340 	if (!comm_buf)
341 		goto out;
342 
343 	guidcpy(&smm_property->guid, vendor);
344 	smm_property->name_size = name_size;
345 	memcpy(smm_property->name, variable_name, name_size);
346 
347 	ret = mm_communicate(comm_buf, payload_size);
348 	/*
349 	 * Currently only R/O property is supported in StMM.
350 	 * Variables that are not set to R/O will not set the property in StMM
351 	 * and the call will return EFI_NOT_FOUND. We are setting the
352 	 * properties to 0x0 so checking against that is enough for the
353 	 * EFI_NOT_FOUND case.
354 	 */
355 	if (ret == EFI_NOT_FOUND)
356 		ret = EFI_SUCCESS;
357 	if (ret != EFI_SUCCESS)
358 		goto out;
359 	memcpy(var_property, &smm_property->property, sizeof(*var_property));
360 
361 out:
362 	free(comm_buf);
363 	return ret;
364 }
365 
efi_get_variable_int(const u16 * variable_name,const efi_guid_t * vendor,u32 * attributes,efi_uintn_t * data_size,void * data,u64 * timep)366 efi_status_t efi_get_variable_int(const u16 *variable_name,
367 				  const efi_guid_t *vendor,
368 				  u32 *attributes, efi_uintn_t *data_size,
369 				  void *data, u64 *timep)
370 {
371 	struct var_check_property var_property;
372 	struct smm_variable_access *var_acc;
373 	efi_uintn_t payload_size;
374 	efi_uintn_t name_size;
375 	efi_uintn_t tmp_dsize;
376 	u8 *comm_buf = NULL;
377 	efi_status_t ret;
378 
379 	if (!variable_name || !vendor || !data_size) {
380 		ret = EFI_INVALID_PARAMETER;
381 		goto out;
382 	}
383 
384 	/* Check payload size */
385 	name_size = u16_strsize(variable_name);
386 	if (name_size > max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE) {
387 		ret = EFI_INVALID_PARAMETER;
388 		goto out;
389 	}
390 
391 	/* Trim output buffer size */
392 	tmp_dsize = *data_size;
393 	if (name_size + tmp_dsize >
394 			max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE) {
395 		tmp_dsize = max_payload_size -
396 				MM_VARIABLE_ACCESS_HEADER_SIZE -
397 				name_size;
398 	}
399 
400 	/* Get communication buffer and initialize header */
401 	payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + tmp_dsize;
402 	comm_buf = setup_mm_hdr((void **)&var_acc, payload_size,
403 				SMM_VARIABLE_FUNCTION_GET_VARIABLE, &ret);
404 	if (!comm_buf)
405 		goto out;
406 
407 	/* Fill in contents */
408 	guidcpy(&var_acc->guid, vendor);
409 	var_acc->data_size = tmp_dsize;
410 	var_acc->name_size = name_size;
411 	var_acc->attr = attributes ? *attributes : 0;
412 	memcpy(var_acc->name, variable_name, name_size);
413 
414 	/* Communicate */
415 	ret = mm_communicate(comm_buf, payload_size);
416 	if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
417 		/* Update with reported data size for trimmed case */
418 		*data_size = var_acc->data_size;
419 	}
420 	if (ret != EFI_SUCCESS)
421 		goto out;
422 
423 	ret = get_property_int(variable_name, name_size, vendor, &var_property);
424 	if (ret != EFI_SUCCESS)
425 		goto out;
426 
427 	if (attributes) {
428 		*attributes = var_acc->attr;
429 		if (var_property.property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY)
430 			*attributes |= EFI_VARIABLE_READ_ONLY;
431 	}
432 
433 	if (data)
434 		memcpy(data, (u8 *)var_acc->name + var_acc->name_size,
435 		       var_acc->data_size);
436 	else
437 		ret = EFI_INVALID_PARAMETER;
438 
439 out:
440 	free(comm_buf);
441 	return ret;
442 }
443 
efi_get_next_variable_name_int(efi_uintn_t * variable_name_size,u16 * variable_name,efi_guid_t * guid)444 efi_status_t efi_get_next_variable_name_int(efi_uintn_t *variable_name_size,
445 					    u16 *variable_name,
446 					    efi_guid_t *guid)
447 {
448 	struct smm_variable_getnext *var_getnext;
449 	efi_uintn_t payload_size;
450 	efi_uintn_t out_name_size;
451 	efi_uintn_t in_name_size;
452 	u8 *comm_buf = NULL;
453 	efi_status_t ret;
454 
455 	if (!variable_name_size || !variable_name || !guid) {
456 		ret = EFI_INVALID_PARAMETER;
457 		goto out;
458 	}
459 
460 	out_name_size = *variable_name_size;
461 	in_name_size = u16_strsize(variable_name);
462 
463 	if (out_name_size < in_name_size) {
464 		ret = EFI_INVALID_PARAMETER;
465 		goto out;
466 	}
467 
468 	if (in_name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE) {
469 		ret = EFI_INVALID_PARAMETER;
470 		goto out;
471 	}
472 
473 	/* Trim output buffer size */
474 	if (out_name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE)
475 		out_name_size = max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE;
476 
477 	payload_size = MM_VARIABLE_GET_NEXT_HEADER_SIZE + out_name_size;
478 	comm_buf = setup_mm_hdr((void **)&var_getnext, payload_size,
479 				SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME,
480 				&ret);
481 	if (!comm_buf)
482 		goto out;
483 
484 	/* Fill in contents */
485 	guidcpy(&var_getnext->guid, guid);
486 	var_getnext->name_size = out_name_size;
487 	memcpy(var_getnext->name, variable_name, in_name_size);
488 	memset((u8 *)var_getnext->name + in_name_size, 0x0,
489 	       out_name_size - in_name_size);
490 
491 	/* Communicate */
492 	ret = mm_communicate(comm_buf, payload_size);
493 	if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
494 		/* Update with reported data size for trimmed case */
495 		*variable_name_size = var_getnext->name_size;
496 	}
497 	if (ret != EFI_SUCCESS)
498 		goto out;
499 
500 	guidcpy(guid, &var_getnext->guid);
501 	memcpy(variable_name, var_getnext->name, var_getnext->name_size);
502 
503 out:
504 	free(comm_buf);
505 	return ret;
506 }
507 
efi_set_variable_int(const u16 * variable_name,const efi_guid_t * vendor,u32 attributes,efi_uintn_t data_size,const void * data,bool ro_check)508 efi_status_t efi_set_variable_int(const u16 *variable_name,
509 				  const efi_guid_t *vendor, u32 attributes,
510 				  efi_uintn_t data_size, const void *data,
511 				  bool ro_check)
512 {
513 	efi_status_t ret, alt_ret = EFI_SUCCESS;
514 	struct var_check_property var_property;
515 	struct smm_variable_access *var_acc;
516 	efi_uintn_t payload_size;
517 	efi_uintn_t name_size;
518 	u8 *comm_buf = NULL;
519 	bool ro;
520 
521 	if (!variable_name || variable_name[0] == 0 || !vendor) {
522 		ret = EFI_INVALID_PARAMETER;
523 		goto out;
524 	}
525 	if (data_size > 0 && !data) {
526 		ret = EFI_INVALID_PARAMETER;
527 		goto out;
528 	}
529 	/* Check payload size */
530 	name_size = u16_strsize(variable_name);
531 	payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + data_size;
532 	if (payload_size > max_payload_size) {
533 		ret = EFI_INVALID_PARAMETER;
534 		goto out;
535 	}
536 
537 	/*
538 	 * Allocate the buffer early, before switching to RW (if needed)
539 	 * so we won't need to account for any failures in reading/setting
540 	 * the properties, if the allocation fails
541 	 */
542 	comm_buf = setup_mm_hdr((void **)&var_acc, payload_size,
543 				SMM_VARIABLE_FUNCTION_SET_VARIABLE, &ret);
544 	if (!comm_buf)
545 		goto out;
546 
547 	ro = !!(attributes & EFI_VARIABLE_READ_ONLY);
548 	attributes &= EFI_VARIABLE_MASK;
549 
550 	/*
551 	 * The API has the ability to override RO flags. If no RO check was
552 	 * requested switch the variable to RW for the duration of this call
553 	 */
554 	ret = get_property_int(variable_name, name_size, vendor,
555 			       &var_property);
556 	if (ret != EFI_SUCCESS)
557 		goto out;
558 
559 	if (var_property.property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY) {
560 		/* Bypass r/o check */
561 		if (!ro_check) {
562 			var_property.property &= ~VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY;
563 			ret = set_property_int(variable_name, name_size, vendor, &var_property);
564 			if (ret != EFI_SUCCESS)
565 				goto out;
566 		} else {
567 			ret = EFI_WRITE_PROTECTED;
568 			goto out;
569 		}
570 	}
571 
572 	/* Fill in contents */
573 	guidcpy(&var_acc->guid, vendor);
574 	var_acc->data_size = data_size;
575 	var_acc->name_size = name_size;
576 	var_acc->attr = attributes;
577 	memcpy(var_acc->name, variable_name, name_size);
578 	memcpy((u8 *)var_acc->name + name_size, data, data_size);
579 
580 	/* Communicate */
581 	ret = mm_communicate(comm_buf, payload_size);
582 	if (ret != EFI_SUCCESS)
583 		alt_ret = ret;
584 
585 	if (ro && !(var_property.property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY)) {
586 		var_property.revision = VAR_CHECK_VARIABLE_PROPERTY_REVISION;
587 		var_property.property |= VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY;
588 		var_property.attributes = attributes;
589 		var_property.minsize = 1;
590 		var_property.maxsize = var_acc->data_size;
591 		ret = set_property_int(variable_name, name_size, vendor, &var_property);
592 	}
593 
594 	if (alt_ret != EFI_SUCCESS)
595 		goto out;
596 
597 	if (!u16_strcmp(variable_name, L"PK"))
598 		alt_ret = efi_init_secure_state();
599 out:
600 	free(comm_buf);
601 	return alt_ret == EFI_SUCCESS ? ret : alt_ret;
602 }
603 
efi_query_variable_info_int(u32 attributes,u64 * max_variable_storage_size,u64 * remain_variable_storage_size,u64 * max_variable_size)604 efi_status_t efi_query_variable_info_int(u32 attributes,
605 					 u64 *max_variable_storage_size,
606 					 u64 *remain_variable_storage_size,
607 					 u64 *max_variable_size)
608 {
609 	struct smm_variable_query_info *mm_query_info;
610 	efi_uintn_t payload_size;
611 	efi_status_t ret;
612 	u8 *comm_buf;
613 
614 	payload_size = sizeof(*mm_query_info);
615 	comm_buf = setup_mm_hdr((void **)&mm_query_info, payload_size,
616 				SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO,
617 				&ret);
618 	if (!comm_buf)
619 		goto out;
620 
621 	mm_query_info->attr = attributes;
622 	ret = mm_communicate(comm_buf, payload_size);
623 	if (ret != EFI_SUCCESS)
624 		goto out;
625 	*max_variable_storage_size = mm_query_info->max_variable_storage;
626 	*remain_variable_storage_size =
627 			mm_query_info->remaining_variable_storage;
628 	*max_variable_size = mm_query_info->max_variable_size;
629 
630 out:
631 	free(comm_buf);
632 	return ret;
633 }
634 
635 /**
636  * efi_query_variable_info() - get information about EFI variables
637  *
638  * This function implements the QueryVariableInfo() runtime service.
639  *
640  * See the Unified Extensible Firmware Interface (UEFI) specification for
641  * details.
642  *
643  * @attributes:				bitmask to select variables to be
644  *					queried
645  * @maximum_variable_storage_size:	maximum size of storage area for the
646  *					selected variable types
647  * @remaining_variable_storage_size:	remaining size of storage are for the
648  *					selected variable types
649  * @maximum_variable_size:		maximum size of a variable of the
650  *					selected type
651  * Return:				status code
652  */
653 efi_status_t EFIAPI __efi_runtime
efi_query_variable_info_runtime(u32 attributes,u64 * max_variable_storage_size,u64 * remain_variable_storage_size,u64 * max_variable_size)654 efi_query_variable_info_runtime(u32 attributes, u64 *max_variable_storage_size,
655 				u64 *remain_variable_storage_size,
656 				u64 *max_variable_size)
657 {
658 	return EFI_UNSUPPORTED;
659 }
660 
661 /**
662  * efi_set_variable_runtime() - runtime implementation of SetVariable()
663  *
664  * @variable_name:	name of the variable
665  * @guid:		vendor GUID
666  * @attributes:		attributes of the variable
667  * @data_size:		size of the buffer with the variable value
668  * @data:		buffer with the variable value
669  * Return:		status code
670  */
671 static efi_status_t __efi_runtime EFIAPI
efi_set_variable_runtime(u16 * variable_name,const efi_guid_t * guid,u32 attributes,efi_uintn_t data_size,const void * data)672 efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *guid,
673 			 u32 attributes, efi_uintn_t data_size,
674 			 const void *data)
675 {
676 	return EFI_UNSUPPORTED;
677 }
678 
679 /**
680  * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
681  */
efi_variables_boot_exit_notify(void)682 void efi_variables_boot_exit_notify(void)
683 {
684 	efi_status_t ret;
685 	u8 *comm_buf;
686 	loff_t len;
687 	struct efi_var_file *var_buf;
688 
689 	comm_buf = setup_mm_hdr(NULL, 0,
690 				SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE, &ret);
691 	if (comm_buf)
692 		ret = mm_communicate(comm_buf, 0);
693 	else
694 		ret = EFI_NOT_FOUND;
695 
696 	if (ret != EFI_SUCCESS)
697 		log_err("Unable to notify StMM for ExitBootServices\n");
698 	free(comm_buf);
699 
700 	/*
701 	 * Populate the list for runtime variables.
702 	 * asking EFI_VARIABLE_RUNTIME_ACCESS is redundant, since
703 	 * efi_var_mem_notify_exit_boot_services will clean those, but that's fine
704 	 */
705 	ret = efi_var_collect(&var_buf, &len, EFI_VARIABLE_RUNTIME_ACCESS);
706 	if (ret != EFI_SUCCESS)
707 		log_err("Can't populate EFI variables. No runtime variables will be available\n");
708 	else
709 		efi_var_buf_update(var_buf);
710 	free(var_buf);
711 
712 	/* Update runtime service table */
713 	efi_runtime_services.query_variable_info =
714 			efi_query_variable_info_runtime;
715 	efi_runtime_services.get_variable = efi_get_variable_runtime;
716 	efi_runtime_services.get_next_variable_name =
717 			efi_get_next_variable_name_runtime;
718 	efi_runtime_services.set_variable = efi_set_variable_runtime;
719 	efi_update_table_header_crc32(&efi_runtime_services.hdr);
720 }
721 
722 /**
723  * efi_init_variables() - initialize variable services
724  *
725  * Return:	status code
726  */
efi_init_variables(void)727 efi_status_t efi_init_variables(void)
728 {
729 	efi_status_t ret;
730 
731 	/* Create a cached copy of the variables that will be enabled on ExitBootServices() */
732 	ret = efi_var_mem_init();
733 	if (ret != EFI_SUCCESS)
734 		return ret;
735 
736 	ret = get_max_payload(&max_payload_size);
737 	if (ret != EFI_SUCCESS)
738 		return ret;
739 
740 	max_buffer_size = MM_COMMUNICATE_HEADER_SIZE +
741 			  MM_VARIABLE_COMMUNICATE_SIZE +
742 			  max_payload_size;
743 
744 	ret = efi_init_secure_state();
745 	if (ret != EFI_SUCCESS)
746 		return ret;
747 
748 	return EFI_SUCCESS;
749 }
750