]>
Commit | Line | Data |
---|---|---|
f042e47e IA |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | |
3 | * EFI variable service via OP-TEE | |
4 | * | |
5 | * Copyright (C) 2019 Linaro Ltd. <[email protected]> | |
6 | * Copyright (C) 2019 Linaro Ltd. <[email protected]> | |
218b062e AEK |
7 | * Copyright 2022-2023 Arm Limited and/or its affiliates <[email protected]> |
8 | * | |
9 | * Authors: | |
10 | * Abdellatif El Khlifi <[email protected]> | |
f042e47e IA |
11 | */ |
12 | ||
218b062e AEK |
13 | #if CONFIG_IS_ENABLED(ARM_FFA_TRANSPORT) |
14 | #include <arm_ffa.h> | |
15 | #endif | |
16 | #include <cpu_func.h> | |
17 | #include <dm.h> | |
f042e47e IA |
18 | #include <efi.h> |
19 | #include <efi_api.h> | |
20 | #include <efi_loader.h> | |
f2d2b3a1 | 21 | #include <efi_variable.h> |
f042e47e | 22 | #include <malloc.h> |
218b062e | 23 | #include <mapmem.h> |
f042e47e | 24 | #include <mm_communication.h> |
218b062e AEK |
25 | #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 | ||
35 | static const char *mm_sp_svc_uuid = MM_SP_UUID; | |
36 | static u16 mm_sp_id; | |
37 | #endif | |
f042e47e | 38 | |
e01aed47 | 39 | extern struct efi_var_file __efi_runtime_data *efi_var_buf; |
f042e47e IA |
40 | static efi_uintn_t max_buffer_size; /* comm + var + func + data */ |
41 | static efi_uintn_t max_payload_size; /* func + data */ | |
42 | ||
43 | struct 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 | */ | |
54 | static 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; | |
548fb67e | 59 | int rc = -ENODEV; |
f042e47e IA |
60 | |
61 | tee = tee_find_device(tee, NULL, NULL, NULL); | |
62 | if (!tee) | |
548fb67e | 63 | goto out; |
f042e47e IA |
64 | |
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); | |
548fb67e IA |
68 | 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; | |
f042e47e IA |
75 | } |
76 | ||
548fb67e IA |
77 | conn->tee = tee; |
78 | conn->session = arg.session; | |
79 | ||
80 | return 0; | |
81 | out: | |
f042e47e IA |
82 | 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 | */ | |
92 | static 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"); | |
548fb67e | 120 | tee_close_session(conn.tee, conn.session); |
f042e47e IA |
121 | 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); | |
f042e47e IA |
135 | tee_shm_free(shm); |
136 | tee_close_session(conn.tee, conn.session); | |
2b18d95d IA |
137 | 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) | |
9b87d442 | 142 | return EFI_DEVICE_ERROR; |
f042e47e IA |
143 | |
144 | switch (param[1].u.value.a) { | |
5d1f79ba | 145 | case ARM_SVC_SPM_RET_SUCCESS: |
f042e47e IA |
146 | ret = EFI_SUCCESS; |
147 | break; | |
148 | ||
5d1f79ba | 149 | case ARM_SVC_SPM_RET_INVALID_PARAMS: |
f042e47e IA |
150 | ret = EFI_INVALID_PARAMETER; |
151 | break; | |
152 | ||
5d1f79ba | 153 | case ARM_SVC_SPM_RET_DENIED: |
f042e47e IA |
154 | ret = EFI_ACCESS_DENIED; |
155 | break; | |
156 | ||
5d1f79ba | 157 | case ARM_SVC_SPM_RET_NO_MEMORY: |
f042e47e IA |
158 | ret = EFI_OUT_OF_RESOURCES; |
159 | break; | |
160 | ||
161 | default: | |
162 | ret = EFI_ACCESS_DENIED; | |
163 | } | |
164 | ||
165 | return ret; | |
166 | } | |
167 | ||
218b062e AEK |
168 | #if CONFIG_IS_ENABLED(ARM_FFA_TRANSPORT) |
169 | /** | |
170 | * 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 | */ | |
181 | static 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 | */ | |
236 | static 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 | ||
f042e47e | 265 | /** |
218b062e AEK |
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 | */ | |
281 | static 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 | */ | |
373 | static 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 | |
f042e47e IA |
390 | * it to OP-TEE |
391 | * | |
218b062e | 392 | * @comm_buf: locally allocated communication buffer |
f042e47e | 393 | * @dsize: buffer size |
218b062e AEK |
394 | * |
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 | * | |
f042e47e IA |
400 | * Return: status code |
401 | */ | |
402 | static 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; | |
218b062e AEK |
407 | #if CONFIG_IS_ENABLED(ARM_FFA_TRANSPORT) |
408 | enum mm_comms_select mm_comms; | |
409 | #endif | |
f042e47e IA |
410 | |
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 | ||
218b062e AEK |
415 | #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 | ||
f042e47e IA |
425 | 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 | */ | |
444 | static 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 | */ | |
492 | efi_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 | ||
e01aed47 IA |
514 | /* 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 | } | |
f042e47e | 519 | *size = var_payload->size; |
e01aed47 IA |
520 | /* |
521 | * 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; | |
f042e47e IA |
531 | out: |
532 | free(comm_buf); | |
533 | return ret; | |
534 | } | |
535 | ||
f96744b2 IA |
536 | /* |
537 | * StMM can store internal attributes and properties for variables, i.e enabling | |
538 | * R/O variables | |
539 | */ | |
d47671c6 HS |
540 | static efi_status_t set_property_int(const u16 *variable_name, |
541 | efi_uintn_t name_size, | |
f96744b2 IA |
542 | 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 | ||
569 | out: | |
570 | free(comm_buf); | |
571 | return ret; | |
572 | } | |
573 | ||
d47671c6 HS |
574 | static efi_status_t get_property_int(const u16 *variable_name, |
575 | efi_uintn_t name_size, | |
f96744b2 IA |
576 | 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 | ||
614 | out: | |
615 | free(comm_buf); | |
616 | return ret; | |
617 | } | |
618 | ||
d47671c6 HS |
619 | efi_status_t efi_get_variable_int(const u16 *variable_name, |
620 | const efi_guid_t *vendor, | |
f2d2b3a1 HS |
621 | u32 *attributes, efi_uintn_t *data_size, |
622 | void *data, u64 *timep) | |
f042e47e | 623 | { |
f96744b2 | 624 | struct var_check_property var_property; |
f042e47e IA |
625 | 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; | |
ff6af6ee | 630 | efi_status_t ret, tmp; |
f042e47e | 631 | |
f2d2b3a1 | 632 | if (!variable_name || !vendor || !data_size) { |
f042e47e IA |
633 | ret = EFI_INVALID_PARAMETER; |
634 | goto out; | |
635 | } | |
636 | ||
637 | /* Check payload size */ | |
f2d2b3a1 | 638 | name_size = u16_strsize(variable_name); |
f042e47e IA |
639 | 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 */ | |
f2d2b3a1 | 661 | guidcpy(&var_acc->guid, vendor); |
f042e47e IA |
662 | var_acc->data_size = tmp_dsize; |
663 | var_acc->name_size = name_size; | |
f2d2b3a1 HS |
664 | var_acc->attr = attributes ? *attributes : 0; |
665 | memcpy(var_acc->name, variable_name, name_size); | |
f042e47e IA |
666 | |
667 | /* Communicate */ | |
668 | ret = mm_communicate(comm_buf, payload_size); | |
ff6af6ee | 669 | if (ret != EFI_SUCCESS && ret != EFI_BUFFER_TOO_SMALL) |
f96744b2 IA |
670 | goto out; |
671 | ||
ff6af6ee IA |
672 | /* 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 | */ | |
f96744b2 | 678 | if (attributes) { |
ff6af6ee IA |
679 | tmp = get_property_int(variable_name, name_size, vendor, |
680 | &var_property); | |
681 | if (tmp != EFI_SUCCESS) { | |
682 | ret = tmp; | |
683 | goto out; | |
684 | } | |
f2d2b3a1 | 685 | *attributes = var_acc->attr; |
ff6af6ee IA |
686 | if (var_property.property & |
687 | VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY) | |
f96744b2 IA |
688 | *attributes |= EFI_VARIABLE_READ_ONLY; |
689 | } | |
690 | ||
ff6af6ee IA |
691 | /* return if ret is EFI_BUFFER_TOO_SMALL */ |
692 | if (ret != EFI_SUCCESS) | |
693 | goto out; | |
694 | ||
f042e47e IA |
695 | 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 | ||
701 | out: | |
702 | free(comm_buf); | |
f2d2b3a1 | 703 | return ret; |
f042e47e IA |
704 | } |
705 | ||
01df8cf3 HS |
706 | efi_status_t efi_get_next_variable_name_int(efi_uintn_t *variable_name_size, |
707 | u16 *variable_name, | |
708 | efi_guid_t *guid) | |
f042e47e IA |
709 | { |
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; | |
f042e47e IA |
714 | u8 *comm_buf = NULL; |
715 | efi_status_t ret; | |
716 | ||
f042e47e IA |
717 | 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 | ||
ecb833a0 | 730 | if (in_name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE) { |
f042e47e IA |
731 | ret = EFI_INVALID_PARAMETER; |
732 | goto out; | |
733 | } | |
734 | ||
735 | /* Trim output buffer size */ | |
db94dfbd IA |
736 | 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; | |
f042e47e IA |
738 | |
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); | |
db94dfbd | 763 | memcpy(variable_name, var_getnext->name, var_getnext->name_size); |
f042e47e IA |
764 | |
765 | out: | |
766 | free(comm_buf); | |
01df8cf3 | 767 | return ret; |
f042e47e IA |
768 | } |
769 | ||
d47671c6 HS |
770 | efi_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) | |
f042e47e | 774 | { |
f96744b2 IA |
775 | efi_status_t ret, alt_ret = EFI_SUCCESS; |
776 | struct var_check_property var_property; | |
f042e47e IA |
777 | struct smm_variable_access *var_acc; |
778 | efi_uintn_t payload_size; | |
779 | efi_uintn_t name_size; | |
780 | u8 *comm_buf = NULL; | |
f96744b2 | 781 | bool ro; |
f042e47e | 782 | |
f2d2b3a1 | 783 | if (!variable_name || variable_name[0] == 0 || !vendor) { |
f042e47e IA |
784 | ret = EFI_INVALID_PARAMETER; |
785 | goto out; | |
786 | } | |
787 | if (data_size > 0 && !data) { | |
788 | ret = EFI_INVALID_PARAMETER; | |
789 | goto out; | |
790 | } | |
f042e47e | 791 | /* Check payload size */ |
f2d2b3a1 | 792 | name_size = u16_strsize(variable_name); |
f042e47e IA |
793 | 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 | ||
f96744b2 IA |
799 | /* |
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 | */ | |
f042e47e IA |
804 | 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 | ||
f96744b2 IA |
809 | 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 | ||
f042e47e | 834 | /* Fill in contents */ |
f2d2b3a1 | 835 | guidcpy(&var_acc->guid, vendor); |
f042e47e IA |
836 | var_acc->data_size = data_size; |
837 | var_acc->name_size = name_size; | |
f2d2b3a1 HS |
838 | var_acc->attr = attributes; |
839 | memcpy(var_acc->name, variable_name, name_size); | |
f042e47e IA |
840 | memcpy((u8 *)var_acc->name + name_size, data, data_size); |
841 | ||
842 | /* Communicate */ | |
843 | ret = mm_communicate(comm_buf, payload_size); | |
f96744b2 IA |
844 | if (ret != EFI_SUCCESS) |
845 | alt_ret = ret; | |
846 | ||
847 | 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 | } | |
1a7b0f6e HS |
855 | |
856 | if (alt_ret != EFI_SUCCESS) | |
857 | goto out; | |
858 | ||
156ccbc3 | 859 | if (!u16_strcmp(variable_name, u"PK")) |
1a7b0f6e | 860 | alt_ret = efi_init_secure_state(); |
f042e47e IA |
861 | out: |
862 | free(comm_buf); | |
f96744b2 | 863 | return alt_ret == EFI_SUCCESS ? ret : alt_ret; |
f042e47e IA |
864 | } |
865 | ||
01df8cf3 HS |
866 | efi_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) | |
f042e47e IA |
870 | { |
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 | ||
9677192c IA |
876 | if (!max_variable_storage_size || |
877 | !remain_variable_storage_size || | |
878 | !max_variable_size || !attributes) | |
879 | return EFI_INVALID_PARAMETER; | |
880 | ||
f042e47e IA |
881 | payload_size = sizeof(*mm_query_info); |
882 | comm_buf = setup_mm_hdr((void **)&mm_query_info, payload_size, | |
883 | SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO, | |
884 | &ret); | |
885 | if (!comm_buf) | |
886 | goto out; | |
887 | ||
888 | mm_query_info->attr = attributes; | |
889 | ret = mm_communicate(comm_buf, payload_size); | |
890 | if (ret != EFI_SUCCESS) | |
891 | goto out; | |
892 | *max_variable_storage_size = mm_query_info->max_variable_storage; | |
893 | *remain_variable_storage_size = | |
894 | mm_query_info->remaining_variable_storage; | |
895 | *max_variable_size = mm_query_info->max_variable_size; | |
896 | ||
897 | out: | |
898 | free(comm_buf); | |
01df8cf3 | 899 | return ret; |
f042e47e IA |
900 | } |
901 | ||
f042e47e IA |
902 | /** |
903 | * efi_query_variable_info() - get information about EFI variables | |
904 | * | |
905 | * This function implements the QueryVariableInfo() runtime service. | |
906 | * | |
907 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
908 | * details. | |
909 | * | |
910 | * @attributes: bitmask to select variables to be | |
911 | * queried | |
912 | * @maximum_variable_storage_size: maximum size of storage area for the | |
913 | * selected variable types | |
914 | * @remaining_variable_storage_size: remaining size of storage are for the | |
915 | * selected variable types | |
916 | * @maximum_variable_size: maximum size of a variable of the | |
917 | * selected type | |
918 | * Return: status code | |
919 | */ | |
920 | efi_status_t EFIAPI __efi_runtime | |
921 | efi_query_variable_info_runtime(u32 attributes, u64 *max_variable_storage_size, | |
922 | u64 *remain_variable_storage_size, | |
923 | u64 *max_variable_size) | |
924 | { | |
925 | return EFI_UNSUPPORTED; | |
926 | } | |
927 | ||
928 | /** | |
929 | * efi_set_variable_runtime() - runtime implementation of SetVariable() | |
930 | * | |
931 | * @variable_name: name of the variable | |
932 | * @guid: vendor GUID | |
933 | * @attributes: attributes of the variable | |
934 | * @data_size: size of the buffer with the variable value | |
935 | * @data: buffer with the variable value | |
936 | * Return: status code | |
937 | */ | |
938 | static efi_status_t __efi_runtime EFIAPI | |
939 | efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *guid, | |
940 | u32 attributes, efi_uintn_t data_size, | |
941 | const void *data) | |
942 | { | |
943 | return EFI_UNSUPPORTED; | |
944 | } | |
945 | ||
946 | /** | |
947 | * efi_variables_boot_exit_notify() - notify ExitBootServices() is called | |
948 | */ | |
949 | void efi_variables_boot_exit_notify(void) | |
950 | { | |
f042e47e | 951 | efi_status_t ret; |
e01aed47 IA |
952 | u8 *comm_buf; |
953 | loff_t len; | |
954 | struct efi_var_file *var_buf; | |
f042e47e IA |
955 | |
956 | comm_buf = setup_mm_hdr(NULL, 0, | |
957 | SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE, &ret); | |
958 | if (comm_buf) | |
959 | ret = mm_communicate(comm_buf, 0); | |
960 | else | |
961 | ret = EFI_NOT_FOUND; | |
962 | ||
963 | if (ret != EFI_SUCCESS) | |
218b062e | 964 | log_err("Unable to notify the MM partition for ExitBootServices\n"); |
f042e47e IA |
965 | free(comm_buf); |
966 | ||
e01aed47 IA |
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 | |
53e54bf5 | 971 | efi_var_buf_update(var_buf); |
e01aed47 IA |
972 | free(var_buf); |
973 | ||
f042e47e IA |
974 | /* 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 | */ | |
989 | efi_status_t efi_init_variables(void) | |
990 | { | |
991 | efi_status_t ret; | |
992 | ||
e01aed47 IA |
993 | /* 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 | ||
f042e47e IA |
998 | 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 | ||
1a7b0f6e HS |
1006 | ret = efi_init_secure_state(); |
1007 | if (ret != EFI_SUCCESS) | |
1008 | return ret; | |
1009 | ||
f042e47e IA |
1010 | return EFI_SUCCESS; |
1011 | } |