blob: 58931c4efd7445fe491d591c41e6e42a1c4c1422 [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>
7 */
8
9#include <common.h>
10#include <efi.h>
11#include <efi_api.h>
12#include <efi_loader.h>
Heinrich Schuchardt9827e842020-06-22 18:10:27 +020013#include <efi_variable.h>
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030014#include <tee.h>
15#include <malloc.h>
16#include <mm_communication.h>
17
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +030018extern struct efi_var_file __efi_runtime_data *efi_var_buf;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030019static efi_uintn_t max_buffer_size; /* comm + var + func + data */
20static efi_uintn_t max_payload_size; /* func + data */
21
22struct mm_connection {
23 struct udevice *tee;
24 u32 session;
25};
26
27/**
28 * get_connection() - Retrieve OP-TEE session for a specific UUID.
29 *
30 * @conn: session buffer to fill
31 * Return: status code
32 */
33static int get_connection(struct mm_connection *conn)
34{
35 static const struct tee_optee_ta_uuid uuid = PTA_STMM_UUID;
36 struct udevice *tee = NULL;
37 struct tee_open_session_arg arg;
Ilias Apalodimas555e05d2020-12-23 13:25:00 +020038 int rc = -ENODEV;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030039
40 tee = tee_find_device(tee, NULL, NULL, NULL);
41 if (!tee)
Ilias Apalodimas555e05d2020-12-23 13:25:00 +020042 goto out;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030043
44 memset(&arg, 0, sizeof(arg));
45 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
46 rc = tee_open_session(tee, &arg, 0, NULL);
Ilias Apalodimas555e05d2020-12-23 13:25:00 +020047 if (rc)
48 goto out;
49
50 /* Check the internal OP-TEE result */
51 if (arg.ret != TEE_SUCCESS) {
52 rc = -EIO;
53 goto out;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030054 }
55
Ilias Apalodimas555e05d2020-12-23 13:25:00 +020056 conn->tee = tee;
57 conn->session = arg.session;
58
59 return 0;
60out:
Ilias Apalodimas77a364f2020-05-17 22:25:44 +030061 return rc;
62}
63
64/**
65 * optee_mm_communicate() - Pass a buffer to StandaloneMM running in OP-TEE
66 *
67 * @comm_buf: locally allocted communcation buffer
68 * @dsize: buffer size
69 * Return: status code
70 */
71static efi_status_t optee_mm_communicate(void *comm_buf, ulong dsize)
72{
73 ulong buf_size;
74 efi_status_t ret;
75 struct efi_mm_communicate_header *mm_hdr;
76 struct mm_connection conn = { NULL, 0 };
77 struct tee_invoke_arg arg;
78 struct tee_param param[2];
79 struct tee_shm *shm = NULL;
80 int rc;
81
82 if (!comm_buf)
83 return EFI_INVALID_PARAMETER;
84
85 mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
86 buf_size = mm_hdr->message_len + sizeof(efi_guid_t) + sizeof(size_t);
87
88 if (dsize != buf_size)
89 return EFI_INVALID_PARAMETER;
90
91 rc = get_connection(&conn);
92 if (rc) {
93 log_err("Unable to open OP-TEE session (err=%d)\n", rc);
94 return EFI_UNSUPPORTED;
95 }
96
97 if (tee_shm_register(conn.tee, comm_buf, buf_size, 0, &shm)) {
98 log_err("Unable to register shared memory\n");
Ilias Apalodimas555e05d2020-12-23 13:25:00 +020099 tee_close_session(conn.tee, conn.session);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300100 return EFI_UNSUPPORTED;
101 }
102
103 memset(&arg, 0, sizeof(arg));
104 arg.func = PTA_STMM_CMDID_COMMUNICATE;
105 arg.session = conn.session;
106
107 memset(param, 0, sizeof(param));
108 param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT;
109 param[0].u.memref.size = buf_size;
110 param[0].u.memref.shm = shm;
111 param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT;
112
113 rc = tee_invoke_func(conn.tee, &arg, 2, param);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300114 tee_shm_free(shm);
115 tee_close_session(conn.tee, conn.session);
Ilias Apalodimas599ac592021-12-24 10:08:41 +0200116 if (rc)
117 return EFI_DEVICE_ERROR;
118 if (arg.ret == TEE_ERROR_EXCESS_DATA)
119 log_err("Variable payload too large\n");
120 if (arg.ret != TEE_SUCCESS)
Ilias Apalodimasfe7c1fc2020-07-22 10:32:22 +0300121 return EFI_DEVICE_ERROR;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300122
123 switch (param[1].u.value.a) {
Ilias Apalodimas128a3c52020-07-17 07:55:03 +0300124 case ARM_SVC_SPM_RET_SUCCESS:
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300125 ret = EFI_SUCCESS;
126 break;
127
Ilias Apalodimas128a3c52020-07-17 07:55:03 +0300128 case ARM_SVC_SPM_RET_INVALID_PARAMS:
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300129 ret = EFI_INVALID_PARAMETER;
130 break;
131
Ilias Apalodimas128a3c52020-07-17 07:55:03 +0300132 case ARM_SVC_SPM_RET_DENIED:
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300133 ret = EFI_ACCESS_DENIED;
134 break;
135
Ilias Apalodimas128a3c52020-07-17 07:55:03 +0300136 case ARM_SVC_SPM_RET_NO_MEMORY:
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300137 ret = EFI_OUT_OF_RESOURCES;
138 break;
139
140 default:
141 ret = EFI_ACCESS_DENIED;
142 }
143
144 return ret;
145}
146
147/**
148 * mm_communicate() - Adjust the cmonnucation buffer to StandAlonneMM and send
149 * it to OP-TEE
150 *
151 * @comm_buf: locally allocted communcation buffer
152 * @dsize: buffer size
153 * Return: status code
154 */
155static efi_status_t mm_communicate(u8 *comm_buf, efi_uintn_t dsize)
156{
157 efi_status_t ret;
158 struct efi_mm_communicate_header *mm_hdr;
159 struct smm_variable_communicate_header *var_hdr;
160
161 dsize += MM_COMMUNICATE_HEADER_SIZE + MM_VARIABLE_COMMUNICATE_SIZE;
162 mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
163 var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
164
165 ret = optee_mm_communicate(comm_buf, dsize);
166 if (ret != EFI_SUCCESS) {
167 log_err("%s failed!\n", __func__);
168 return ret;
169 }
170
171 return var_hdr->ret_status;
172}
173
174/**
175 * setup_mm_hdr() - Allocate a buffer for StandAloneMM and initialize the
176 * header data.
177 *
178 * @dptr: pointer address of the corresponding StandAloneMM
179 * function
180 * @payload_size: buffer size
181 * @func: standAloneMM function number
182 * @ret: EFI return code
183 * Return: buffer or NULL
184 */
185static u8 *setup_mm_hdr(void **dptr, efi_uintn_t payload_size,
186 efi_uintn_t func, efi_status_t *ret)
187{
188 const efi_guid_t mm_var_guid = EFI_MM_VARIABLE_GUID;
189 struct efi_mm_communicate_header *mm_hdr;
190 struct smm_variable_communicate_header *var_hdr;
191 u8 *comm_buf;
192
193 /* In the init function we initialize max_buffer_size with
194 * get_max_payload(). So skip the test if max_buffer_size is initialized
195 * StandAloneMM will perform similar checks and drop the buffer if it's
196 * too long
197 */
198 if (max_buffer_size && max_buffer_size <
199 (MM_COMMUNICATE_HEADER_SIZE +
200 MM_VARIABLE_COMMUNICATE_SIZE +
201 payload_size)) {
202 *ret = EFI_INVALID_PARAMETER;
203 return NULL;
204 }
205
206 comm_buf = calloc(1, MM_COMMUNICATE_HEADER_SIZE +
207 MM_VARIABLE_COMMUNICATE_SIZE +
208 payload_size);
209 if (!comm_buf) {
210 *ret = EFI_OUT_OF_RESOURCES;
211 return NULL;
212 }
213
214 mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
215 guidcpy(&mm_hdr->header_guid, &mm_var_guid);
216 mm_hdr->message_len = MM_VARIABLE_COMMUNICATE_SIZE + payload_size;
217
218 var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
219 var_hdr->function = func;
220 if (dptr)
221 *dptr = var_hdr->data;
222 *ret = EFI_SUCCESS;
223
224 return comm_buf;
225}
226
227/**
228 * get_max_payload() - Get variable payload size from StandAloneMM.
229 *
230 * @size: size of the variable in storage
231 * Return: status code
232 */
233efi_status_t EFIAPI get_max_payload(efi_uintn_t *size)
234{
235 struct smm_variable_payload_size *var_payload = NULL;
236 efi_uintn_t payload_size;
237 u8 *comm_buf = NULL;
238 efi_status_t ret;
239
240 if (!size) {
241 ret = EFI_INVALID_PARAMETER;
242 goto out;
243 }
244
245 payload_size = sizeof(*var_payload);
246 comm_buf = setup_mm_hdr((void **)&var_payload, payload_size,
247 SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE, &ret);
248 if (!comm_buf)
249 goto out;
250
251 ret = mm_communicate(comm_buf, payload_size);
252 if (ret != EFI_SUCCESS)
253 goto out;
254
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300255 /* Make sure the buffer is big enough for storing variables */
256 if (var_payload->size < MM_VARIABLE_ACCESS_HEADER_SIZE + 0x20) {
257 ret = EFI_DEVICE_ERROR;
258 goto out;
259 }
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300260 *size = var_payload->size;
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300261 /*
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300262 * There seems to be a bug in EDK2 miscalculating the boundaries and
263 * size checks, so deduct 2 more bytes to fulfill this requirement. Fix
264 * it up here to ensure backwards compatibility with older versions
265 * (cf. StandaloneMmPkg/Drivers/StandaloneMmCpu/AArch64/EventHandle.c.
266 * sizeof (EFI_MM_COMMUNICATE_HEADER) instead the size minus the
267 * flexible array member).
268 *
269 * size is guaranteed to be > 2 due to checks on the beginning.
270 */
271 *size -= 2;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300272out:
273 free(comm_buf);
274 return ret;
275}
276
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300277/*
278 * StMM can store internal attributes and properties for variables, i.e enabling
279 * R/O variables
280 */
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +0200281static efi_status_t set_property_int(const u16 *variable_name,
282 efi_uintn_t name_size,
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300283 const efi_guid_t *vendor,
284 struct var_check_property *var_property)
285{
286 struct smm_variable_var_check_property *smm_property;
287 efi_uintn_t payload_size;
288 u8 *comm_buf = NULL;
289 efi_status_t ret;
290
291 payload_size = sizeof(*smm_property) + name_size;
292 if (payload_size > max_payload_size) {
293 ret = EFI_INVALID_PARAMETER;
294 goto out;
295 }
296 comm_buf = setup_mm_hdr((void **)&smm_property, payload_size,
297 SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET,
298 &ret);
299 if (!comm_buf)
300 goto out;
301
302 guidcpy(&smm_property->guid, vendor);
303 smm_property->name_size = name_size;
304 memcpy(&smm_property->property, var_property,
305 sizeof(smm_property->property));
306 memcpy(smm_property->name, variable_name, name_size);
307
308 ret = mm_communicate(comm_buf, payload_size);
309
310out:
311 free(comm_buf);
312 return ret;
313}
314
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +0200315static efi_status_t get_property_int(const u16 *variable_name,
316 efi_uintn_t name_size,
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300317 const efi_guid_t *vendor,
318 struct var_check_property *var_property)
319{
320 struct smm_variable_var_check_property *smm_property;
321 efi_uintn_t payload_size;
322 u8 *comm_buf = NULL;
323 efi_status_t ret;
324
325 memset(var_property, 0, sizeof(*var_property));
326 payload_size = sizeof(*smm_property) + name_size;
327 if (payload_size > max_payload_size) {
328 ret = EFI_INVALID_PARAMETER;
329 goto out;
330 }
331 comm_buf = setup_mm_hdr((void **)&smm_property, payload_size,
332 SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET,
333 &ret);
334 if (!comm_buf)
335 goto out;
336
337 guidcpy(&smm_property->guid, vendor);
338 smm_property->name_size = name_size;
339 memcpy(smm_property->name, variable_name, name_size);
340
341 ret = mm_communicate(comm_buf, payload_size);
342 /*
343 * Currently only R/O property is supported in StMM.
344 * Variables that are not set to R/O will not set the property in StMM
345 * and the call will return EFI_NOT_FOUND. We are setting the
346 * properties to 0x0 so checking against that is enough for the
347 * EFI_NOT_FOUND case.
348 */
349 if (ret == EFI_NOT_FOUND)
350 ret = EFI_SUCCESS;
351 if (ret != EFI_SUCCESS)
352 goto out;
353 memcpy(var_property, &smm_property->property, sizeof(*var_property));
354
355out:
356 free(comm_buf);
357 return ret;
358}
359
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +0200360efi_status_t efi_get_variable_int(const u16 *variable_name,
361 const efi_guid_t *vendor,
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200362 u32 *attributes, efi_uintn_t *data_size,
363 void *data, u64 *timep)
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300364{
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300365 struct var_check_property var_property;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300366 struct smm_variable_access *var_acc;
367 efi_uintn_t payload_size;
368 efi_uintn_t name_size;
369 efi_uintn_t tmp_dsize;
370 u8 *comm_buf = NULL;
371 efi_status_t ret;
372
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200373 if (!variable_name || !vendor || !data_size) {
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300374 ret = EFI_INVALID_PARAMETER;
375 goto out;
376 }
377
378 /* Check payload size */
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200379 name_size = u16_strsize(variable_name);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300380 if (name_size > max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE) {
381 ret = EFI_INVALID_PARAMETER;
382 goto out;
383 }
384
385 /* Trim output buffer size */
386 tmp_dsize = *data_size;
387 if (name_size + tmp_dsize >
388 max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE) {
389 tmp_dsize = max_payload_size -
390 MM_VARIABLE_ACCESS_HEADER_SIZE -
391 name_size;
392 }
393
394 /* Get communication buffer and initialize header */
395 payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + tmp_dsize;
396 comm_buf = setup_mm_hdr((void **)&var_acc, payload_size,
397 SMM_VARIABLE_FUNCTION_GET_VARIABLE, &ret);
398 if (!comm_buf)
399 goto out;
400
401 /* Fill in contents */
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200402 guidcpy(&var_acc->guid, vendor);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300403 var_acc->data_size = tmp_dsize;
404 var_acc->name_size = name_size;
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200405 var_acc->attr = attributes ? *attributes : 0;
406 memcpy(var_acc->name, variable_name, name_size);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300407
408 /* Communicate */
409 ret = mm_communicate(comm_buf, payload_size);
410 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
411 /* Update with reported data size for trimmed case */
412 *data_size = var_acc->data_size;
413 }
414 if (ret != EFI_SUCCESS)
415 goto out;
416
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300417 ret = get_property_int(variable_name, name_size, vendor, &var_property);
418 if (ret != EFI_SUCCESS)
419 goto out;
420
421 if (attributes) {
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200422 *attributes = var_acc->attr;
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300423 if (var_property.property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY)
424 *attributes |= EFI_VARIABLE_READ_ONLY;
425 }
426
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300427 if (data)
428 memcpy(data, (u8 *)var_acc->name + var_acc->name_size,
429 var_acc->data_size);
430 else
431 ret = EFI_INVALID_PARAMETER;
432
433out:
434 free(comm_buf);
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200435 return ret;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300436}
437
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200438efi_status_t efi_get_next_variable_name_int(efi_uintn_t *variable_name_size,
439 u16 *variable_name,
440 efi_guid_t *guid)
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300441{
442 struct smm_variable_getnext *var_getnext;
443 efi_uintn_t payload_size;
444 efi_uintn_t out_name_size;
445 efi_uintn_t in_name_size;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300446 u8 *comm_buf = NULL;
447 efi_status_t ret;
448
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300449 if (!variable_name_size || !variable_name || !guid) {
450 ret = EFI_INVALID_PARAMETER;
451 goto out;
452 }
453
454 out_name_size = *variable_name_size;
455 in_name_size = u16_strsize(variable_name);
456
457 if (out_name_size < in_name_size) {
458 ret = EFI_INVALID_PARAMETER;
459 goto out;
460 }
461
Ilias Apalodimas24105b82020-07-01 16:41:25 +0300462 if (in_name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE) {
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300463 ret = EFI_INVALID_PARAMETER;
464 goto out;
465 }
466
467 /* Trim output buffer size */
Ilias Apalodimas91b218d2020-07-22 01:50:37 +0300468 if (out_name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE)
469 out_name_size = max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300470
471 payload_size = MM_VARIABLE_GET_NEXT_HEADER_SIZE + out_name_size;
472 comm_buf = setup_mm_hdr((void **)&var_getnext, payload_size,
473 SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME,
474 &ret);
475 if (!comm_buf)
476 goto out;
477
478 /* Fill in contents */
479 guidcpy(&var_getnext->guid, guid);
480 var_getnext->name_size = out_name_size;
481 memcpy(var_getnext->name, variable_name, in_name_size);
482 memset((u8 *)var_getnext->name + in_name_size, 0x0,
483 out_name_size - in_name_size);
484
485 /* Communicate */
486 ret = mm_communicate(comm_buf, payload_size);
487 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
488 /* Update with reported data size for trimmed case */
489 *variable_name_size = var_getnext->name_size;
490 }
491 if (ret != EFI_SUCCESS)
492 goto out;
493
494 guidcpy(guid, &var_getnext->guid);
Ilias Apalodimas91b218d2020-07-22 01:50:37 +0300495 memcpy(variable_name, var_getnext->name, var_getnext->name_size);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300496
497out:
498 free(comm_buf);
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200499 return ret;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300500}
501
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +0200502efi_status_t efi_set_variable_int(const u16 *variable_name,
503 const efi_guid_t *vendor, u32 attributes,
504 efi_uintn_t data_size, const void *data,
505 bool ro_check)
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300506{
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300507 efi_status_t ret, alt_ret = EFI_SUCCESS;
508 struct var_check_property var_property;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300509 struct smm_variable_access *var_acc;
510 efi_uintn_t payload_size;
511 efi_uintn_t name_size;
512 u8 *comm_buf = NULL;
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300513 bool ro;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300514
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200515 if (!variable_name || variable_name[0] == 0 || !vendor) {
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300516 ret = EFI_INVALID_PARAMETER;
517 goto out;
518 }
519 if (data_size > 0 && !data) {
520 ret = EFI_INVALID_PARAMETER;
521 goto out;
522 }
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300523 /* Check payload size */
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200524 name_size = u16_strsize(variable_name);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300525 payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + data_size;
526 if (payload_size > max_payload_size) {
527 ret = EFI_INVALID_PARAMETER;
528 goto out;
529 }
530
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300531 /*
532 * Allocate the buffer early, before switching to RW (if needed)
533 * so we won't need to account for any failures in reading/setting
534 * the properties, if the allocation fails
535 */
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300536 comm_buf = setup_mm_hdr((void **)&var_acc, payload_size,
537 SMM_VARIABLE_FUNCTION_SET_VARIABLE, &ret);
538 if (!comm_buf)
539 goto out;
540
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300541 ro = !!(attributes & EFI_VARIABLE_READ_ONLY);
542 attributes &= EFI_VARIABLE_MASK;
543
544 /*
545 * The API has the ability to override RO flags. If no RO check was
546 * requested switch the variable to RW for the duration of this call
547 */
548 ret = get_property_int(variable_name, name_size, vendor,
549 &var_property);
550 if (ret != EFI_SUCCESS)
551 goto out;
552
553 if (var_property.property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY) {
554 /* Bypass r/o check */
555 if (!ro_check) {
556 var_property.property &= ~VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY;
557 ret = set_property_int(variable_name, name_size, vendor, &var_property);
558 if (ret != EFI_SUCCESS)
559 goto out;
560 } else {
561 ret = EFI_WRITE_PROTECTED;
562 goto out;
563 }
564 }
565
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300566 /* Fill in contents */
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200567 guidcpy(&var_acc->guid, vendor);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300568 var_acc->data_size = data_size;
569 var_acc->name_size = name_size;
Heinrich Schuchardt9827e842020-06-22 18:10:27 +0200570 var_acc->attr = attributes;
571 memcpy(var_acc->name, variable_name, name_size);
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300572 memcpy((u8 *)var_acc->name + name_size, data, data_size);
573
574 /* Communicate */
575 ret = mm_communicate(comm_buf, payload_size);
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300576 if (ret != EFI_SUCCESS)
577 alt_ret = ret;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300578
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300579 if (ro && !(var_property.property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY)) {
580 var_property.revision = VAR_CHECK_VARIABLE_PROPERTY_REVISION;
581 var_property.property |= VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY;
582 var_property.attributes = attributes;
583 var_property.minsize = 1;
584 var_property.maxsize = var_acc->data_size;
585 ret = set_property_int(variable_name, name_size, vendor, &var_property);
586 }
Heinrich Schuchardt730e2292020-07-14 08:14:08 +0200587
588 if (alt_ret != EFI_SUCCESS)
589 goto out;
590
Simon Glass90975372022-01-23 12:55:12 -0700591 if (!u16_strcmp(variable_name, u"PK"))
Heinrich Schuchardt730e2292020-07-14 08:14:08 +0200592 alt_ret = efi_init_secure_state();
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300593out:
594 free(comm_buf);
Ilias Apalodimas5f1bce92020-07-09 23:00:40 +0300595 return alt_ret == EFI_SUCCESS ? ret : alt_ret;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300596}
597
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200598efi_status_t efi_query_variable_info_int(u32 attributes,
599 u64 *max_variable_storage_size,
600 u64 *remain_variable_storage_size,
601 u64 *max_variable_size)
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300602{
603 struct smm_variable_query_info *mm_query_info;
604 efi_uintn_t payload_size;
605 efi_status_t ret;
606 u8 *comm_buf;
607
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300608 payload_size = sizeof(*mm_query_info);
609 comm_buf = setup_mm_hdr((void **)&mm_query_info, payload_size,
610 SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO,
611 &ret);
612 if (!comm_buf)
613 goto out;
614
615 mm_query_info->attr = attributes;
616 ret = mm_communicate(comm_buf, payload_size);
617 if (ret != EFI_SUCCESS)
618 goto out;
619 *max_variable_storage_size = mm_query_info->max_variable_storage;
620 *remain_variable_storage_size =
621 mm_query_info->remaining_variable_storage;
622 *max_variable_size = mm_query_info->max_variable_size;
623
624out:
625 free(comm_buf);
Heinrich Schuchardt276c61d2020-06-26 17:57:48 +0200626 return ret;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300627}
628
629/**
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300630 * efi_query_variable_info() - get information about EFI variables
631 *
632 * This function implements the QueryVariableInfo() runtime service.
633 *
634 * See the Unified Extensible Firmware Interface (UEFI) specification for
635 * details.
636 *
637 * @attributes: bitmask to select variables to be
638 * queried
639 * @maximum_variable_storage_size: maximum size of storage area for the
640 * selected variable types
641 * @remaining_variable_storage_size: remaining size of storage are for the
642 * selected variable types
643 * @maximum_variable_size: maximum size of a variable of the
644 * selected type
645 * Return: status code
646 */
647efi_status_t EFIAPI __efi_runtime
648efi_query_variable_info_runtime(u32 attributes, u64 *max_variable_storage_size,
649 u64 *remain_variable_storage_size,
650 u64 *max_variable_size)
651{
652 return EFI_UNSUPPORTED;
653}
654
655/**
656 * efi_set_variable_runtime() - runtime implementation of SetVariable()
657 *
658 * @variable_name: name of the variable
659 * @guid: vendor GUID
660 * @attributes: attributes of the variable
661 * @data_size: size of the buffer with the variable value
662 * @data: buffer with the variable value
663 * Return: status code
664 */
665static efi_status_t __efi_runtime EFIAPI
666efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *guid,
667 u32 attributes, efi_uintn_t data_size,
668 const void *data)
669{
670 return EFI_UNSUPPORTED;
671}
672
673/**
674 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
675 */
676void efi_variables_boot_exit_notify(void)
677{
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300678 efi_status_t ret;
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300679 u8 *comm_buf;
680 loff_t len;
681 struct efi_var_file *var_buf;
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300682
683 comm_buf = setup_mm_hdr(NULL, 0,
684 SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE, &ret);
685 if (comm_buf)
686 ret = mm_communicate(comm_buf, 0);
687 else
688 ret = EFI_NOT_FOUND;
689
690 if (ret != EFI_SUCCESS)
691 log_err("Unable to notify StMM for ExitBootServices\n");
692 free(comm_buf);
693
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300694 /*
695 * Populate the list for runtime variables.
696 * asking EFI_VARIABLE_RUNTIME_ACCESS is redundant, since
697 * efi_var_mem_notify_exit_boot_services will clean those, but that's fine
698 */
699 ret = efi_var_collect(&var_buf, &len, EFI_VARIABLE_RUNTIME_ACCESS);
700 if (ret != EFI_SUCCESS)
701 log_err("Can't populate EFI variables. No runtime variables will be available\n");
702 else
Ilias Apalodimas33521442021-01-16 17:28:04 +0200703 efi_var_buf_update(var_buf);
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300704 free(var_buf);
705
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300706 /* Update runtime service table */
707 efi_runtime_services.query_variable_info =
708 efi_query_variable_info_runtime;
709 efi_runtime_services.get_variable = efi_get_variable_runtime;
710 efi_runtime_services.get_next_variable_name =
711 efi_get_next_variable_name_runtime;
712 efi_runtime_services.set_variable = efi_set_variable_runtime;
713 efi_update_table_header_crc32(&efi_runtime_services.hdr);
714}
715
716/**
717 * efi_init_variables() - initialize variable services
718 *
719 * Return: status code
720 */
721efi_status_t efi_init_variables(void)
722{
723 efi_status_t ret;
724
Ilias Apalodimasa4d1b1b2020-07-23 15:49:49 +0300725 /* Create a cached copy of the variables that will be enabled on ExitBootServices() */
726 ret = efi_var_mem_init();
727 if (ret != EFI_SUCCESS)
728 return ret;
729
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300730 ret = get_max_payload(&max_payload_size);
731 if (ret != EFI_SUCCESS)
732 return ret;
733
734 max_buffer_size = MM_COMMUNICATE_HEADER_SIZE +
735 MM_VARIABLE_COMMUNICATE_SIZE +
736 max_payload_size;
737
Heinrich Schuchardt730e2292020-07-14 08:14:08 +0200738 ret = efi_init_secure_state();
739 if (ret != EFI_SUCCESS)
740 return ret;
741
Ilias Apalodimas77a364f2020-05-17 22:25:44 +0300742 return EFI_SUCCESS;
743}