blob: dde135fd9f81aaf067d8ab0dcc37e906c2b7f7ef [file] [log] [blame]
Ilias Apalodimas77a364f2020-05-17 22:25:44 +03001// 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>
Abdellatif El Khlifi431c7b52023-08-04 14:33:44 +01007 * Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
8 *
9 * Authors:
10 * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030011 */
12
Abdellatif El Khlifi431c7b52023-08-04 14:33:44 +010013#if CONFIG_IS_ENABLED(ARM_FFA_TRANSPORT)
14#include <arm_ffa.h>
15#endif
16#include <cpu_func.h>
17#include <dm.h>
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030018#include <efi.h>
19#include <efi_api.h>
20#include <efi_loader.h>
Heinrich Schuchardt9827e842020-06-22 18:10:27 +020021#include <efi_variable.h>
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030022#include <malloc.h>
Abdellatif El Khlifi431c7b52023-08-04 14:33:44 +010023#include <mapmem.h>
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030024#include <mm_communication.h>
Abdellatif El Khlifi431c7b52023-08-04 14:33:44 +010025#include <tee.h>
26
27#if CONFIG_IS_ENABLED(ARM_FFA_TRANSPORT)
28/* MM return codes */
29#define MM_SUCCESS (0)
30#define MM_NOT_SUPPORTED (-1)
31#define MM_INVALID_PARAMETER (-2)
32#define MM_DENIED (-3)
33#define MM_NO_MEMORY (-5)
34
35static const char *mm_sp_svc_uuid = MM_SP_UUID;
36static u16 mm_sp_id;
37#endif
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030038
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +030039extern struct efi_var_file __efi_runtime_data *efi_var_buf;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030040static efi_uintn_t max_buffer_size; /* comm + var + func + data */
41static efi_uintn_t max_payload_size; /* func + data */
42
43struct mm_connection {
44 struct udevice *tee;
45 u32 session;
46};
47
48/**
49 * get_connection() - Retrieve OP-TEE session for a specific UUID.
50 *
51 * @conn: session buffer to fill
52 * Return: status code
53 */
54static int get_connection(struct mm_connection *conn)
55{
56 static const struct tee_optee_ta_uuid uuid = PTA_STMM_UUID;
57 struct udevice *tee = NULL;
58 struct tee_open_session_arg arg;
Ilias Apalodimas555e05d2020-12-23 13:25:00 +020059 int rc = -ENODEV;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030060
61 tee = tee_find_device(tee, NULL, NULL, NULL);
62 if (!tee)
Ilias Apalodimas555e05d2020-12-23 13:25:00 +020063 goto out;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030064
65 memset(&arg, 0, sizeof(arg));
66 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
67 rc = tee_open_session(tee, &arg, 0, NULL);
Ilias Apalodimas555e05d2020-12-23 13:25:00 +020068 if (rc)
69 goto out;
70
71 /* Check the internal OP-TEE result */
72 if (arg.ret != TEE_SUCCESS) {
73 rc = -EIO;
74 goto out;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030075 }
76
Ilias Apalodimas555e05d2020-12-23 13:25:00 +020077 conn->tee = tee;
78 conn->session = arg.session;
79
80 return 0;
81out:
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030082 return rc;
83}
84
85/**
86 * optee_mm_communicate() - Pass a buffer to StandaloneMM running in OP-TEE
87 *
88 * @comm_buf: locally allocted communcation buffer
89 * @dsize: buffer size
90 * Return: status code
91 */
92static efi_status_t optee_mm_communicate(void *comm_buf, ulong dsize)
93{
94 ulong buf_size;
95 efi_status_t ret;
96 struct efi_mm_communicate_header *mm_hdr;
97 struct mm_connection conn = { NULL, 0 };
98 struct tee_invoke_arg arg;
99 struct tee_param param[2];
100 struct tee_shm *shm = NULL;
101 int rc;
102
103 if (!comm_buf)
104 return EFI_INVALID_PARAMETER;
105
106 mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
107 buf_size = mm_hdr->message_len + sizeof(efi_guid_t) + sizeof(size_t);
108
109 if (dsize != buf_size)
110 return EFI_INVALID_PARAMETER;
111
112 rc = get_connection(&conn);
113 if (rc) {
114 log_err("Unable to open OP-TEE session (err=%d)\n", rc);
115 return EFI_UNSUPPORTED;
116 }
117
118 if (tee_shm_register(conn.tee, comm_buf, buf_size, 0, &shm)) {
119 log_err("Unable to register shared memory\n");
Ilias Apalodimas555e05d2020-12-23 13:25:00 +0200120 tee_close_session(conn.tee, conn.session);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300121 return EFI_UNSUPPORTED;
122 }
123
124 memset(&arg, 0, sizeof(arg));
125 arg.func = PTA_STMM_CMDID_COMMUNICATE;
126 arg.session = conn.session;
127
128 memset(param, 0, sizeof(param));
129 param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT;
130 param[0].u.memref.size = buf_size;
131 param[0].u.memref.shm = shm;
132 param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT;
133
134 rc = tee_invoke_func(conn.tee, &arg, 2, param);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300135 tee_shm_free(shm);
136 tee_close_session(conn.tee, conn.session);
Ilias Apalodimas599ac592021-12-24 10:08:41 +0200137 if (rc)
138 return EFI_DEVICE_ERROR;
139 if (arg.ret == TEE_ERROR_EXCESS_DATA)
140 log_err("Variable payload too large\n");
141 if (arg.ret != TEE_SUCCESS)
Ilias Apalodimasfe7c1fc2020-07-22 10:32:22 +0300142 return EFI_DEVICE_ERROR;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300143
144 switch (param[1].u.value.a) {
Ilias Apalodimas128a3c52020-07-17 07:55:03 +0300145 case ARM_SVC_SPM_RET_SUCCESS:
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300146 ret = EFI_SUCCESS;
147 break;
148
Ilias Apalodimas128a3c52020-07-17 07:55:03 +0300149 case ARM_SVC_SPM_RET_INVALID_PARAMS:
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300150 ret = EFI_INVALID_PARAMETER;
151 break;
152
Ilias Apalodimas128a3c52020-07-17 07:55:03 +0300153 case ARM_SVC_SPM_RET_DENIED:
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300154 ret = EFI_ACCESS_DENIED;
155 break;
156
Ilias Apalodimas128a3c52020-07-17 07:55:03 +0300157 case ARM_SVC_SPM_RET_NO_MEMORY:
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300158 ret = EFI_OUT_OF_RESOURCES;
159 break;
160
161 default:
162 ret = EFI_ACCESS_DENIED;
163 }
164
165 return ret;
166}
167
Abdellatif El Khlifi431c7b52023-08-04 14:33:44 +0100168#if CONFIG_IS_ENABLED(ARM_FFA_TRANSPORT)
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300169/**
Abdellatif El Khlifi431c7b52023-08-04 14:33:44 +0100170 * ffa_notify_mm_sp() - Announce there is data in the shared buffer
171 *
172 * Notify the MM partition in the trusted world that
173 * data is available in the shared buffer.
174 * This is a blocking call during which trusted world has exclusive access
175 * to the MM shared buffer.
176 *
177 * Return:
178 *
179 * 0 on success
180 */
181static int ffa_notify_mm_sp(void)
182{
183 struct ffa_send_direct_data msg = {0};
184 int ret;
185 int sp_event_ret;
186 struct udevice *dev;
187
188 ret = uclass_first_device_err(UCLASS_FFA, &dev);
189 if (ret) {
190 log_err("EFI: Cannot find FF-A bus device, notify MM SP failure\n");
191 return ret;
192 }
193
194 msg.data0 = CONFIG_FFA_SHARED_MM_BUF_OFFSET; /* x3 */
195
196 ret = ffa_sync_send_receive(dev, mm_sp_id, &msg, 1);
197 if (ret)
198 return ret;
199
200 sp_event_ret = msg.data0; /* x3 */
201
202 switch (sp_event_ret) {
203 case MM_SUCCESS:
204 ret = 0;
205 break;
206 case MM_NOT_SUPPORTED:
207 ret = -EINVAL;
208 break;
209 case MM_INVALID_PARAMETER:
210 ret = -EPERM;
211 break;
212 case MM_DENIED:
213 ret = -EACCES;
214 break;
215 case MM_NO_MEMORY:
216 ret = -EBUSY;
217 break;
218 default:
219 ret = -EACCES;
220 }
221
222 return ret;
223}
224
225/**
226 * ffa_discover_mm_sp_id() - Query the MM partition ID
227 *
228 * Use the FF-A driver to get the MM partition ID.
229 * If multiple partitions are found, use the first one.
230 * This is a boot time function.
231 *
232 * Return:
233 *
234 * 0 on success
235 */
236static int ffa_discover_mm_sp_id(void)
237{
238 u32 count = 0;
239 int ret;
240 struct ffa_partition_desc *descs;
241 struct udevice *dev;
242
243 ret = uclass_first_device_err(UCLASS_FFA, &dev);
244 if (ret) {
245 log_err("EFI: Cannot find FF-A bus device, MM SP discovery failure\n");
246 return ret;
247 }
248
249 /* Ask the driver to fill the buffer with the SPs info */
250 ret = ffa_partition_info_get(dev, mm_sp_svc_uuid, &count, &descs);
251 if (ret) {
252 log_err("EFI: Failure in querying SPs info (%d), MM SP discovery failure\n", ret);
253 return ret;
254 }
255
256 /* MM SPs found , use the first one */
257
258 mm_sp_id = descs[0].info.id;
259
260 log_info("EFI: MM partition ID 0x%x\n", mm_sp_id);
261
262 return 0;
263}
264
265/**
266 * ffa_mm_communicate() - Exchange EFI services data with the MM partition using FF-A
267 * @comm_buf: locally allocated communication buffer used for rx/tx
268 * @dsize: communication buffer size
269 *
270 * Issue a door bell event to notify the MM partition (SP) running in OP-TEE
271 * that there is data to read from the shared buffer.
272 * Communication with the MM SP is performed using FF-A transport.
273 * On the event, MM SP can read the data from the buffer and
274 * update the MM shared buffer with response data.
275 * The response data is copied back to the communication buffer.
276 *
277 * Return:
278 *
279 * EFI status code
280 */
281static efi_status_t ffa_mm_communicate(void *comm_buf, ulong comm_buf_size)
282{
283 ulong tx_data_size;
284 int ffa_ret;
285 efi_status_t efi_ret;
286 struct efi_mm_communicate_header *mm_hdr;
287 void *virt_shared_buf;
288
289 if (!comm_buf)
290 return EFI_INVALID_PARAMETER;
291
292 /* Discover MM partition ID at boot time */
293 if (!mm_sp_id && ffa_discover_mm_sp_id()) {
294 log_err("EFI: Failure to discover MM SP ID at boot time, FF-A MM comms failure\n");
295 return EFI_UNSUPPORTED;
296 }
297
298 mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
299 tx_data_size = mm_hdr->message_len + sizeof(efi_guid_t) + sizeof(size_t);
300
301 if (comm_buf_size != tx_data_size || tx_data_size > CONFIG_FFA_SHARED_MM_BUF_SIZE)
302 return EFI_INVALID_PARAMETER;
303
304 /* Copy the data to the shared buffer */
305
306 virt_shared_buf = map_sysmem((phys_addr_t)CONFIG_FFA_SHARED_MM_BUF_ADDR, 0);
307 memcpy(virt_shared_buf, comm_buf, tx_data_size);
308
309 /*
310 * The secure world might have cache disabled for
311 * the device region used for shared buffer (which is the case for Optee).
312 * In this case, the secure world reads the data from DRAM.
313 * Let's flush the cache so the DRAM is updated with the latest data.
314 */
315#ifdef CONFIG_ARM64
316 invalidate_dcache_all();
317#endif
318
319 /* Announce there is data in the shared buffer */
320
321 ffa_ret = ffa_notify_mm_sp();
322
323 switch (ffa_ret) {
324 case 0: {
325 ulong rx_data_size;
326 /* Copy the MM SP response from the shared buffer to the communication buffer */
327 rx_data_size = ((struct efi_mm_communicate_header *)virt_shared_buf)->message_len +
328 sizeof(efi_guid_t) +
329 sizeof(size_t);
330
331 if (rx_data_size > comm_buf_size) {
332 efi_ret = EFI_OUT_OF_RESOURCES;
333 break;
334 }
335
336 memcpy(comm_buf, virt_shared_buf, rx_data_size);
337 efi_ret = EFI_SUCCESS;
338 break;
339 }
340 case -EINVAL:
341 efi_ret = EFI_DEVICE_ERROR;
342 break;
343 case -EPERM:
344 efi_ret = EFI_INVALID_PARAMETER;
345 break;
346 case -EACCES:
347 efi_ret = EFI_ACCESS_DENIED;
348 break;
349 case -EBUSY:
350 efi_ret = EFI_OUT_OF_RESOURCES;
351 break;
352 default:
353 efi_ret = EFI_ACCESS_DENIED;
354 }
355
356 unmap_sysmem(virt_shared_buf);
357 return efi_ret;
358}
359
360/**
361 * get_mm_comms() - detect the available MM transport
362 *
363 * Make sure the FF-A bus is probed successfully
364 * which means FF-A communication with secure world works and ready
365 * for use.
366 *
367 * If FF-A bus is not ready, use OPTEE comms.
368 *
369 * Return:
370 *
371 * MM_COMMS_FFA or MM_COMMS_OPTEE
372 */
373static enum mm_comms_select get_mm_comms(void)
374{
375 struct udevice *dev;
376 int ret;
377
378 ret = uclass_first_device_err(UCLASS_FFA, &dev);
379 if (ret) {
380 log_debug("EFI: Cannot find FF-A bus device, trying Optee comms\n");
381 return MM_COMMS_OPTEE;
382 }
383
384 return MM_COMMS_FFA;
385}
386#endif
387
388/**
389 * mm_communicate() - Adjust the communication buffer to the MM SP and send
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300390 * it to OP-TEE
391 *
Abdellatif El Khlifi431c7b52023-08-04 14:33:44 +0100392 * @comm_buf: locally allocated communication buffer
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300393 * @dsize: buffer size
Abdellatif El Khlifi431c7b52023-08-04 14:33:44 +0100394 *
395 * The SP (also called partition) can be any MM SP such as StandAlonneMM or smm-gateway.
396 * The comm_buf format is the same for both partitions.
397 * When using the u-boot OP-TEE driver, StandAlonneMM is supported.
398 * When using the u-boot FF-A driver, any MM SP is supported.
399 *
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300400 * Return: status code
401 */
402static efi_status_t mm_communicate(u8 *comm_buf, efi_uintn_t dsize)
403{
404 efi_status_t ret;
405 struct efi_mm_communicate_header *mm_hdr;
406 struct smm_variable_communicate_header *var_hdr;
Abdellatif El Khlifi431c7b52023-08-04 14:33:44 +0100407#if CONFIG_IS_ENABLED(ARM_FFA_TRANSPORT)
408 enum mm_comms_select mm_comms;
409#endif
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300410
411 dsize += MM_COMMUNICATE_HEADER_SIZE + MM_VARIABLE_COMMUNICATE_SIZE;
412 mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
413 var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
414
Abdellatif El Khlifi431c7b52023-08-04 14:33:44 +0100415#if CONFIG_IS_ENABLED(ARM_FFA_TRANSPORT)
416 mm_comms = get_mm_comms();
417 if (mm_comms == MM_COMMS_FFA)
418 ret = ffa_mm_communicate(comm_buf, dsize);
419 else
420 ret = optee_mm_communicate(comm_buf, dsize);
421#else
422 ret = optee_mm_communicate(comm_buf, dsize);
423#endif
424
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300425 if (ret != EFI_SUCCESS) {
426 log_err("%s failed!\n", __func__);
427 return ret;
428 }
429
430 return var_hdr->ret_status;
431}
432
433/**
434 * setup_mm_hdr() - Allocate a buffer for StandAloneMM and initialize the
435 * header data.
436 *
437 * @dptr: pointer address of the corresponding StandAloneMM
438 * function
439 * @payload_size: buffer size
440 * @func: standAloneMM function number
441 * @ret: EFI return code
442 * Return: buffer or NULL
443 */
444static u8 *setup_mm_hdr(void **dptr, efi_uintn_t payload_size,
445 efi_uintn_t func, efi_status_t *ret)
446{
447 const efi_guid_t mm_var_guid = EFI_MM_VARIABLE_GUID;
448 struct efi_mm_communicate_header *mm_hdr;
449 struct smm_variable_communicate_header *var_hdr;
450 u8 *comm_buf;
451
452 /* In the init function we initialize max_buffer_size with
453 * get_max_payload(). So skip the test if max_buffer_size is initialized
454 * StandAloneMM will perform similar checks and drop the buffer if it's
455 * too long
456 */
457 if (max_buffer_size && max_buffer_size <
458 (MM_COMMUNICATE_HEADER_SIZE +
459 MM_VARIABLE_COMMUNICATE_SIZE +
460 payload_size)) {
461 *ret = EFI_INVALID_PARAMETER;
462 return NULL;
463 }
464
465 comm_buf = calloc(1, MM_COMMUNICATE_HEADER_SIZE +
466 MM_VARIABLE_COMMUNICATE_SIZE +
467 payload_size);
468 if (!comm_buf) {
469 *ret = EFI_OUT_OF_RESOURCES;
470 return NULL;
471 }
472
473 mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
474 guidcpy(&mm_hdr->header_guid, &mm_var_guid);
475 mm_hdr->message_len = MM_VARIABLE_COMMUNICATE_SIZE + payload_size;
476
477 var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
478 var_hdr->function = func;
479 if (dptr)
480 *dptr = var_hdr->data;
481 *ret = EFI_SUCCESS;
482
483 return comm_buf;
484}
485
486/**
487 * get_max_payload() - Get variable payload size from StandAloneMM.
488 *
489 * @size: size of the variable in storage
490 * Return: status code
491 */
492efi_status_t EFIAPI get_max_payload(efi_uintn_t *size)
493{
494 struct smm_variable_payload_size *var_payload = NULL;
495 efi_uintn_t payload_size;
496 u8 *comm_buf = NULL;
497 efi_status_t ret;
498
499 if (!size) {
500 ret = EFI_INVALID_PARAMETER;
501 goto out;
502 }
503
504 payload_size = sizeof(*var_payload);
505 comm_buf = setup_mm_hdr((void **)&var_payload, payload_size,
506 SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE, &ret);
507 if (!comm_buf)
508 goto out;
509
510 ret = mm_communicate(comm_buf, payload_size);
511 if (ret != EFI_SUCCESS)
512 goto out;
513
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300514 /* Make sure the buffer is big enough for storing variables */
515 if (var_payload->size < MM_VARIABLE_ACCESS_HEADER_SIZE + 0x20) {
516 ret = EFI_DEVICE_ERROR;
517 goto out;
518 }
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300519 *size = var_payload->size;
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300520 /*
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300521 * There seems to be a bug in EDK2 miscalculating the boundaries and
522 * size checks, so deduct 2 more bytes to fulfill this requirement. Fix
523 * it up here to ensure backwards compatibility with older versions
524 * (cf. StandaloneMmPkg/Drivers/StandaloneMmCpu/AArch64/EventHandle.c.
525 * sizeof (EFI_MM_COMMUNICATE_HEADER) instead the size minus the
526 * flexible array member).
527 *
528 * size is guaranteed to be > 2 due to checks on the beginning.
529 */
530 *size -= 2;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300531out:
532 free(comm_buf);
533 return ret;
534}
535
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300536/*
537 * StMM can store internal attributes and properties for variables, i.e enabling
538 * R/O variables
539 */
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +0200540static efi_status_t set_property_int(const u16 *variable_name,
541 efi_uintn_t name_size,
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300542 const efi_guid_t *vendor,
543 struct var_check_property *var_property)
544{
545 struct smm_variable_var_check_property *smm_property;
546 efi_uintn_t payload_size;
547 u8 *comm_buf = NULL;
548 efi_status_t ret;
549
550 payload_size = sizeof(*smm_property) + name_size;
551 if (payload_size > max_payload_size) {
552 ret = EFI_INVALID_PARAMETER;
553 goto out;
554 }
555 comm_buf = setup_mm_hdr((void **)&smm_property, payload_size,
556 SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET,
557 &ret);
558 if (!comm_buf)
559 goto out;
560
561 guidcpy(&smm_property->guid, vendor);
562 smm_property->name_size = name_size;
563 memcpy(&smm_property->property, var_property,
564 sizeof(smm_property->property));
565 memcpy(smm_property->name, variable_name, name_size);
566
567 ret = mm_communicate(comm_buf, payload_size);
568
569out:
570 free(comm_buf);
571 return ret;
572}
573
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +0200574static efi_status_t get_property_int(const u16 *variable_name,
575 efi_uintn_t name_size,
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300576 const efi_guid_t *vendor,
577 struct var_check_property *var_property)
578{
579 struct smm_variable_var_check_property *smm_property;
580 efi_uintn_t payload_size;
581 u8 *comm_buf = NULL;
582 efi_status_t ret;
583
584 memset(var_property, 0, sizeof(*var_property));
585 payload_size = sizeof(*smm_property) + name_size;
586 if (payload_size > max_payload_size) {
587 ret = EFI_INVALID_PARAMETER;
588 goto out;
589 }
590 comm_buf = setup_mm_hdr((void **)&smm_property, payload_size,
591 SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET,
592 &ret);
593 if (!comm_buf)
594 goto out;
595
596 guidcpy(&smm_property->guid, vendor);
597 smm_property->name_size = name_size;
598 memcpy(smm_property->name, variable_name, name_size);
599
600 ret = mm_communicate(comm_buf, payload_size);
601 /*
602 * Currently only R/O property is supported in StMM.
603 * Variables that are not set to R/O will not set the property in StMM
604 * and the call will return EFI_NOT_FOUND. We are setting the
605 * properties to 0x0 so checking against that is enough for the
606 * EFI_NOT_FOUND case.
607 */
608 if (ret == EFI_NOT_FOUND)
609 ret = EFI_SUCCESS;
610 if (ret != EFI_SUCCESS)
611 goto out;
612 memcpy(var_property, &smm_property->property, sizeof(*var_property));
613
614out:
615 free(comm_buf);
616 return ret;
617}
618
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +0200619efi_status_t efi_get_variable_int(const u16 *variable_name,
620 const efi_guid_t *vendor,
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200621 u32 *attributes, efi_uintn_t *data_size,
622 void *data, u64 *timep)
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300623{
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300624 struct var_check_property var_property;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300625 struct smm_variable_access *var_acc;
626 efi_uintn_t payload_size;
627 efi_uintn_t name_size;
628 efi_uintn_t tmp_dsize;
629 u8 *comm_buf = NULL;
Ilias Apalodimasa378f962022-03-16 17:13:37 +0200630 efi_status_t ret, tmp;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300631
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200632 if (!variable_name || !vendor || !data_size) {
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300633 ret = EFI_INVALID_PARAMETER;
634 goto out;
635 }
636
637 /* Check payload size */
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200638 name_size = u16_strsize(variable_name);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300639 if (name_size > max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE) {
640 ret = EFI_INVALID_PARAMETER;
641 goto out;
642 }
643
644 /* Trim output buffer size */
645 tmp_dsize = *data_size;
646 if (name_size + tmp_dsize >
647 max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE) {
648 tmp_dsize = max_payload_size -
649 MM_VARIABLE_ACCESS_HEADER_SIZE -
650 name_size;
651 }
652
653 /* Get communication buffer and initialize header */
654 payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + tmp_dsize;
655 comm_buf = setup_mm_hdr((void **)&var_acc, payload_size,
656 SMM_VARIABLE_FUNCTION_GET_VARIABLE, &ret);
657 if (!comm_buf)
658 goto out;
659
660 /* Fill in contents */
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200661 guidcpy(&var_acc->guid, vendor);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300662 var_acc->data_size = tmp_dsize;
663 var_acc->name_size = name_size;
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200664 var_acc->attr = attributes ? *attributes : 0;
665 memcpy(var_acc->name, variable_name, name_size);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300666
667 /* Communicate */
668 ret = mm_communicate(comm_buf, payload_size);
Ilias Apalodimasa378f962022-03-16 17:13:37 +0200669 if (ret != EFI_SUCCESS && ret != EFI_BUFFER_TOO_SMALL)
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300670 goto out;
671
Ilias Apalodimasa378f962022-03-16 17:13:37 +0200672 /* Update with reported data size for trimmed case */
673 *data_size = var_acc->data_size;
674 /*
675 * UEFI > 2.7 needs the attributes set even if the buffer is
676 * smaller
677 */
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300678 if (attributes) {
Ilias Apalodimasa378f962022-03-16 17:13:37 +0200679 tmp = get_property_int(variable_name, name_size, vendor,
680 &var_property);
681 if (tmp != EFI_SUCCESS) {
682 ret = tmp;
683 goto out;
684 }
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200685 *attributes = var_acc->attr;
Ilias Apalodimasa378f962022-03-16 17:13:37 +0200686 if (var_property.property &
687 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY)
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300688 *attributes |= EFI_VARIABLE_READ_ONLY;
689 }
690
Ilias Apalodimasa378f962022-03-16 17:13:37 +0200691 /* return if ret is EFI_BUFFER_TOO_SMALL */
692 if (ret != EFI_SUCCESS)
693 goto out;
694
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300695 if (data)
696 memcpy(data, (u8 *)var_acc->name + var_acc->name_size,
697 var_acc->data_size);
698 else
699 ret = EFI_INVALID_PARAMETER;
700
701out:
702 free(comm_buf);
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200703 return ret;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300704}
705
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200706efi_status_t efi_get_next_variable_name_int(efi_uintn_t *variable_name_size,
707 u16 *variable_name,
708 efi_guid_t *guid)
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300709{
710 struct smm_variable_getnext *var_getnext;
711 efi_uintn_t payload_size;
712 efi_uintn_t out_name_size;
713 efi_uintn_t in_name_size;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300714 u8 *comm_buf = NULL;
715 efi_status_t ret;
716
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300717 if (!variable_name_size || !variable_name || !guid) {
718 ret = EFI_INVALID_PARAMETER;
719 goto out;
720 }
721
722 out_name_size = *variable_name_size;
723 in_name_size = u16_strsize(variable_name);
724
725 if (out_name_size < in_name_size) {
726 ret = EFI_INVALID_PARAMETER;
727 goto out;
728 }
729
Ilias Apalodimas24105b82020-07-01 16:41:25 +0300730 if (in_name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE) {
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300731 ret = EFI_INVALID_PARAMETER;
732 goto out;
733 }
734
735 /* Trim output buffer size */
Ilias Apalodimas91b218d2020-07-22 01:50:37 +0300736 if (out_name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE)
737 out_name_size = max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300738
739 payload_size = MM_VARIABLE_GET_NEXT_HEADER_SIZE + out_name_size;
740 comm_buf = setup_mm_hdr((void **)&var_getnext, payload_size,
741 SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME,
742 &ret);
743 if (!comm_buf)
744 goto out;
745
746 /* Fill in contents */
747 guidcpy(&var_getnext->guid, guid);
748 var_getnext->name_size = out_name_size;
749 memcpy(var_getnext->name, variable_name, in_name_size);
750 memset((u8 *)var_getnext->name + in_name_size, 0x0,
751 out_name_size - in_name_size);
752
753 /* Communicate */
754 ret = mm_communicate(comm_buf, payload_size);
755 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
756 /* Update with reported data size for trimmed case */
757 *variable_name_size = var_getnext->name_size;
758 }
759 if (ret != EFI_SUCCESS)
760 goto out;
761
762 guidcpy(guid, &var_getnext->guid);
Ilias Apalodimas91b218d2020-07-22 01:50:37 +0300763 memcpy(variable_name, var_getnext->name, var_getnext->name_size);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300764
765out:
766 free(comm_buf);
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200767 return ret;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300768}
769
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +0200770efi_status_t efi_set_variable_int(const u16 *variable_name,
771 const efi_guid_t *vendor, u32 attributes,
772 efi_uintn_t data_size, const void *data,
773 bool ro_check)
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300774{
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300775 efi_status_t ret, alt_ret = EFI_SUCCESS;
776 struct var_check_property var_property;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300777 struct smm_variable_access *var_acc;
778 efi_uintn_t payload_size;
779 efi_uintn_t name_size;
780 u8 *comm_buf = NULL;
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300781 bool ro;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300782
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200783 if (!variable_name || variable_name[0] == 0 || !vendor) {
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300784 ret = EFI_INVALID_PARAMETER;
785 goto out;
786 }
787 if (data_size > 0 && !data) {
788 ret = EFI_INVALID_PARAMETER;
789 goto out;
790 }
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300791 /* Check payload size */
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200792 name_size = u16_strsize(variable_name);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300793 payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + data_size;
794 if (payload_size > max_payload_size) {
795 ret = EFI_INVALID_PARAMETER;
796 goto out;
797 }
798
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300799 /*
800 * Allocate the buffer early, before switching to RW (if needed)
801 * so we won't need to account for any failures in reading/setting
802 * the properties, if the allocation fails
803 */
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300804 comm_buf = setup_mm_hdr((void **)&var_acc, payload_size,
805 SMM_VARIABLE_FUNCTION_SET_VARIABLE, &ret);
806 if (!comm_buf)
807 goto out;
808
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300809 ro = !!(attributes & EFI_VARIABLE_READ_ONLY);
810 attributes &= EFI_VARIABLE_MASK;
811
812 /*
813 * The API has the ability to override RO flags. If no RO check was
814 * requested switch the variable to RW for the duration of this call
815 */
816 ret = get_property_int(variable_name, name_size, vendor,
817 &var_property);
818 if (ret != EFI_SUCCESS)
819 goto out;
820
821 if (var_property.property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY) {
822 /* Bypass r/o check */
823 if (!ro_check) {
824 var_property.property &= ~VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY;
825 ret = set_property_int(variable_name, name_size, vendor, &var_property);
826 if (ret != EFI_SUCCESS)
827 goto out;
828 } else {
829 ret = EFI_WRITE_PROTECTED;
830 goto out;
831 }
832 }
833
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300834 /* Fill in contents */
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200835 guidcpy(&var_acc->guid, vendor);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300836 var_acc->data_size = data_size;
837 var_acc->name_size = name_size;
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200838 var_acc->attr = attributes;
839 memcpy(var_acc->name, variable_name, name_size);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300840 memcpy((u8 *)var_acc->name + name_size, data, data_size);
841
842 /* Communicate */
843 ret = mm_communicate(comm_buf, payload_size);
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300844 if (ret != EFI_SUCCESS)
845 alt_ret = ret;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300846
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300847 if (ro && !(var_property.property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY)) {
848 var_property.revision = VAR_CHECK_VARIABLE_PROPERTY_REVISION;
849 var_property.property |= VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY;
850 var_property.attributes = attributes;
851 var_property.minsize = 1;
852 var_property.maxsize = var_acc->data_size;
853 ret = set_property_int(variable_name, name_size, vendor, &var_property);
854 }
Heinrich Schuchardt730e2292020-07-14 08:14:08 +0200855
856 if (alt_ret != EFI_SUCCESS)
857 goto out;
858
Simon Glass90975372022-01-23 12:55:12 -0700859 if (!u16_strcmp(variable_name, u"PK"))
Heinrich Schuchardt730e2292020-07-14 08:14:08 +0200860 alt_ret = efi_init_secure_state();
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300861out:
862 free(comm_buf);
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300863 return alt_ret == EFI_SUCCESS ? ret : alt_ret;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300864}
865
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200866efi_status_t efi_query_variable_info_int(u32 attributes,
867 u64 *max_variable_storage_size,
868 u64 *remain_variable_storage_size,
869 u64 *max_variable_size)
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300870{
871 struct smm_variable_query_info *mm_query_info;
872 efi_uintn_t payload_size;
873 efi_status_t ret;
874 u8 *comm_buf;
875
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300876 payload_size = sizeof(*mm_query_info);
877 comm_buf = setup_mm_hdr((void **)&mm_query_info, payload_size,
878 SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO,
879 &ret);
880 if (!comm_buf)
881 goto out;
882
883 mm_query_info->attr = attributes;
884 ret = mm_communicate(comm_buf, payload_size);
885 if (ret != EFI_SUCCESS)
886 goto out;
887 *max_variable_storage_size = mm_query_info->max_variable_storage;
888 *remain_variable_storage_size =
889 mm_query_info->remaining_variable_storage;
890 *max_variable_size = mm_query_info->max_variable_size;
891
892out:
893 free(comm_buf);
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200894 return ret;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300895}
896
897/**
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300898 * efi_query_variable_info() - get information about EFI variables
899 *
900 * This function implements the QueryVariableInfo() runtime service.
901 *
902 * See the Unified Extensible Firmware Interface (UEFI) specification for
903 * details.
904 *
905 * @attributes: bitmask to select variables to be
906 * queried
907 * @maximum_variable_storage_size: maximum size of storage area for the
908 * selected variable types
909 * @remaining_variable_storage_size: remaining size of storage are for the
910 * selected variable types
911 * @maximum_variable_size: maximum size of a variable of the
912 * selected type
913 * Return: status code
914 */
915efi_status_t EFIAPI __efi_runtime
916efi_query_variable_info_runtime(u32 attributes, u64 *max_variable_storage_size,
917 u64 *remain_variable_storage_size,
918 u64 *max_variable_size)
919{
920 return EFI_UNSUPPORTED;
921}
922
923/**
924 * efi_set_variable_runtime() - runtime implementation of SetVariable()
925 *
926 * @variable_name: name of the variable
927 * @guid: vendor GUID
928 * @attributes: attributes of the variable
929 * @data_size: size of the buffer with the variable value
930 * @data: buffer with the variable value
931 * Return: status code
932 */
933static efi_status_t __efi_runtime EFIAPI
934efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *guid,
935 u32 attributes, efi_uintn_t data_size,
936 const void *data)
937{
938 return EFI_UNSUPPORTED;
939}
940
941/**
942 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
943 */
944void efi_variables_boot_exit_notify(void)
945{
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300946 efi_status_t ret;
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300947 u8 *comm_buf;
948 loff_t len;
949 struct efi_var_file *var_buf;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300950
951 comm_buf = setup_mm_hdr(NULL, 0,
952 SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE, &ret);
953 if (comm_buf)
954 ret = mm_communicate(comm_buf, 0);
955 else
956 ret = EFI_NOT_FOUND;
957
958 if (ret != EFI_SUCCESS)
Abdellatif El Khlifi431c7b52023-08-04 14:33:44 +0100959 log_err("Unable to notify the MM partition for ExitBootServices\n");
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300960 free(comm_buf);
961
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300962 /*
963 * Populate the list for runtime variables.
964 * asking EFI_VARIABLE_RUNTIME_ACCESS is redundant, since
965 * efi_var_mem_notify_exit_boot_services will clean those, but that's fine
966 */
967 ret = efi_var_collect(&var_buf, &len, EFI_VARIABLE_RUNTIME_ACCESS);
968 if (ret != EFI_SUCCESS)
969 log_err("Can't populate EFI variables. No runtime variables will be available\n");
970 else
Ilias Apalodimas33521442021-01-16 17:28:04 +0200971 efi_var_buf_update(var_buf);
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300972 free(var_buf);
973
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300974 /* Update runtime service table */
975 efi_runtime_services.query_variable_info =
976 efi_query_variable_info_runtime;
977 efi_runtime_services.get_variable = efi_get_variable_runtime;
978 efi_runtime_services.get_next_variable_name =
979 efi_get_next_variable_name_runtime;
980 efi_runtime_services.set_variable = efi_set_variable_runtime;
981 efi_update_table_header_crc32(&efi_runtime_services.hdr);
982}
983
984/**
985 * efi_init_variables() - initialize variable services
986 *
987 * Return: status code
988 */
989efi_status_t efi_init_variables(void)
990{
991 efi_status_t ret;
992
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300993 /* Create a cached copy of the variables that will be enabled on ExitBootServices() */
994 ret = efi_var_mem_init();
995 if (ret != EFI_SUCCESS)
996 return ret;
997
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300998 ret = get_max_payload(&max_payload_size);
999 if (ret != EFI_SUCCESS)
1000 return ret;
1001
1002 max_buffer_size = MM_COMMUNICATE_HEADER_SIZE +
1003 MM_VARIABLE_COMMUNICATE_SIZE +
1004 max_payload_size;
1005
Heinrich Schuchardt730e2292020-07-14 08:14:08 +02001006 ret = efi_init_secure_state();
1007 if (ret != EFI_SUCCESS)
1008 return ret;
1009
Ilias Apalodimas77a364f2020-05-17 22:25:44 +03001010 return EFI_SUCCESS;
1011}