Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * EFI variable service via OP-TEE |
| 4 | * |
| 5 | * Copyright (C) 2019 Linaro Ltd. <sughosh.ganu@linaro.org> |
| 6 | * Copyright (C) 2019 Linaro Ltd. <ilias.apalodimas@linaro.org> |
Abdellatif El Khlifi | 431c7b5 | 2023-08-04 14:33:44 +0100 | [diff] [blame] | 7 | * 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 Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 11 | */ |
| 12 | |
Abdellatif El Khlifi | 431c7b5 | 2023-08-04 14:33:44 +0100 | [diff] [blame] | 13 | #if CONFIG_IS_ENABLED(ARM_FFA_TRANSPORT) |
| 14 | #include <arm_ffa.h> |
| 15 | #endif |
| 16 | #include <cpu_func.h> |
| 17 | #include <dm.h> |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 18 | #include <efi.h> |
| 19 | #include <efi_api.h> |
| 20 | #include <efi_loader.h> |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 21 | #include <efi_variable.h> |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 22 | #include <malloc.h> |
Abdellatif El Khlifi | 431c7b5 | 2023-08-04 14:33:44 +0100 | [diff] [blame] | 23 | #include <mapmem.h> |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 24 | #include <mm_communication.h> |
Abdellatif El Khlifi | 431c7b5 | 2023-08-04 14:33:44 +0100 | [diff] [blame] | 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 |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 38 | |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 39 | extern struct efi_var_file __efi_runtime_data *efi_var_buf; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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; |
Ilias Apalodimas | 555e05d | 2020-12-23 13:25:00 +0200 | [diff] [blame] | 59 | int rc = -ENODEV; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 60 | |
| 61 | tee = tee_find_device(tee, NULL, NULL, NULL); |
| 62 | if (!tee) |
Ilias Apalodimas | 555e05d | 2020-12-23 13:25:00 +0200 | [diff] [blame] | 63 | goto out; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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); |
Ilias Apalodimas | 555e05d | 2020-12-23 13:25:00 +0200 | [diff] [blame] | 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; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 75 | } |
| 76 | |
Ilias Apalodimas | 555e05d | 2020-12-23 13:25:00 +0200 | [diff] [blame] | 77 | conn->tee = tee; |
| 78 | conn->session = arg.session; |
| 79 | |
| 80 | return 0; |
| 81 | out: |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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"); |
Ilias Apalodimas | 555e05d | 2020-12-23 13:25:00 +0200 | [diff] [blame] | 120 | tee_close_session(conn.tee, conn.session); |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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); |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 135 | tee_shm_free(shm); |
| 136 | tee_close_session(conn.tee, conn.session); |
Ilias Apalodimas | 599ac59 | 2021-12-24 10:08:41 +0200 | [diff] [blame] | 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) |
Ilias Apalodimas | fe7c1fc | 2020-07-22 10:32:22 +0300 | [diff] [blame] | 142 | return EFI_DEVICE_ERROR; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 143 | |
| 144 | switch (param[1].u.value.a) { |
Ilias Apalodimas | 128a3c5 | 2020-07-17 07:55:03 +0300 | [diff] [blame] | 145 | case ARM_SVC_SPM_RET_SUCCESS: |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 146 | ret = EFI_SUCCESS; |
| 147 | break; |
| 148 | |
Ilias Apalodimas | 128a3c5 | 2020-07-17 07:55:03 +0300 | [diff] [blame] | 149 | case ARM_SVC_SPM_RET_INVALID_PARAMS: |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 150 | ret = EFI_INVALID_PARAMETER; |
| 151 | break; |
| 152 | |
Ilias Apalodimas | 128a3c5 | 2020-07-17 07:55:03 +0300 | [diff] [blame] | 153 | case ARM_SVC_SPM_RET_DENIED: |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 154 | ret = EFI_ACCESS_DENIED; |
| 155 | break; |
| 156 | |
Ilias Apalodimas | 128a3c5 | 2020-07-17 07:55:03 +0300 | [diff] [blame] | 157 | case ARM_SVC_SPM_RET_NO_MEMORY: |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 158 | ret = EFI_OUT_OF_RESOURCES; |
| 159 | break; |
| 160 | |
| 161 | default: |
| 162 | ret = EFI_ACCESS_DENIED; |
| 163 | } |
| 164 | |
| 165 | return ret; |
| 166 | } |
| 167 | |
Abdellatif El Khlifi | 431c7b5 | 2023-08-04 14:33:44 +0100 | [diff] [blame] | 168 | #if CONFIG_IS_ENABLED(ARM_FFA_TRANSPORT) |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 169 | /** |
Abdellatif El Khlifi | 431c7b5 | 2023-08-04 14:33:44 +0100 | [diff] [blame] | 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 | |
| 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 | */ |
| 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 |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 390 | * it to OP-TEE |
| 391 | * |
Abdellatif El Khlifi | 431c7b5 | 2023-08-04 14:33:44 +0100 | [diff] [blame] | 392 | * @comm_buf: locally allocated communication buffer |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 393 | * @dsize: buffer size |
Abdellatif El Khlifi | 431c7b5 | 2023-08-04 14:33:44 +0100 | [diff] [blame] | 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 | * |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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; |
Abdellatif El Khlifi | 431c7b5 | 2023-08-04 14:33:44 +0100 | [diff] [blame] | 407 | #if CONFIG_IS_ENABLED(ARM_FFA_TRANSPORT) |
| 408 | enum mm_comms_select mm_comms; |
| 409 | #endif |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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 | |
Abdellatif El Khlifi | 431c7b5 | 2023-08-04 14:33:44 +0100 | [diff] [blame] | 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 | |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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 | |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 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 | } |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 519 | *size = var_payload->size; |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 520 | /* |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 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; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 531 | out: |
| 532 | free(comm_buf); |
| 533 | return ret; |
| 534 | } |
| 535 | |
Ilias Apalodimas | 5f1bce9 | 2020-07-09 23:00:40 +0300 | [diff] [blame] | 536 | /* |
| 537 | * StMM can store internal attributes and properties for variables, i.e enabling |
| 538 | * R/O variables |
| 539 | */ |
Heinrich Schuchardt | 1ad2f0d | 2021-09-09 07:12:14 +0200 | [diff] [blame] | 540 | static efi_status_t set_property_int(const u16 *variable_name, |
| 541 | efi_uintn_t name_size, |
Ilias Apalodimas | 5f1bce9 | 2020-07-09 23:00:40 +0300 | [diff] [blame] | 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 | |
Heinrich Schuchardt | 1ad2f0d | 2021-09-09 07:12:14 +0200 | [diff] [blame] | 574 | static efi_status_t get_property_int(const u16 *variable_name, |
| 575 | efi_uintn_t name_size, |
Ilias Apalodimas | 5f1bce9 | 2020-07-09 23:00:40 +0300 | [diff] [blame] | 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 | |
Heinrich Schuchardt | 1ad2f0d | 2021-09-09 07:12:14 +0200 | [diff] [blame] | 619 | efi_status_t efi_get_variable_int(const u16 *variable_name, |
| 620 | const efi_guid_t *vendor, |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 621 | u32 *attributes, efi_uintn_t *data_size, |
| 622 | void *data, u64 *timep) |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 623 | { |
Ilias Apalodimas | 5f1bce9 | 2020-07-09 23:00:40 +0300 | [diff] [blame] | 624 | struct var_check_property var_property; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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; |
Ilias Apalodimas | a378f96 | 2022-03-16 17:13:37 +0200 | [diff] [blame] | 630 | efi_status_t ret, tmp; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 631 | |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 632 | if (!variable_name || !vendor || !data_size) { |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 633 | ret = EFI_INVALID_PARAMETER; |
| 634 | goto out; |
| 635 | } |
| 636 | |
| 637 | /* Check payload size */ |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 638 | name_size = u16_strsize(variable_name); |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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 */ |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 661 | guidcpy(&var_acc->guid, vendor); |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 662 | var_acc->data_size = tmp_dsize; |
| 663 | var_acc->name_size = name_size; |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 664 | var_acc->attr = attributes ? *attributes : 0; |
| 665 | memcpy(var_acc->name, variable_name, name_size); |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 666 | |
| 667 | /* Communicate */ |
| 668 | ret = mm_communicate(comm_buf, payload_size); |
Ilias Apalodimas | a378f96 | 2022-03-16 17:13:37 +0200 | [diff] [blame] | 669 | if (ret != EFI_SUCCESS && ret != EFI_BUFFER_TOO_SMALL) |
Ilias Apalodimas | 5f1bce9 | 2020-07-09 23:00:40 +0300 | [diff] [blame] | 670 | goto out; |
| 671 | |
Ilias Apalodimas | a378f96 | 2022-03-16 17:13:37 +0200 | [diff] [blame] | 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 | */ |
Ilias Apalodimas | 5f1bce9 | 2020-07-09 23:00:40 +0300 | [diff] [blame] | 678 | if (attributes) { |
Ilias Apalodimas | a378f96 | 2022-03-16 17:13:37 +0200 | [diff] [blame] | 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 | } |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 685 | *attributes = var_acc->attr; |
Ilias Apalodimas | a378f96 | 2022-03-16 17:13:37 +0200 | [diff] [blame] | 686 | if (var_property.property & |
| 687 | VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY) |
Ilias Apalodimas | 5f1bce9 | 2020-07-09 23:00:40 +0300 | [diff] [blame] | 688 | *attributes |= EFI_VARIABLE_READ_ONLY; |
| 689 | } |
| 690 | |
Ilias Apalodimas | a378f96 | 2022-03-16 17:13:37 +0200 | [diff] [blame] | 691 | /* return if ret is EFI_BUFFER_TOO_SMALL */ |
| 692 | if (ret != EFI_SUCCESS) |
| 693 | goto out; |
| 694 | |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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); |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 703 | return ret; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 704 | } |
| 705 | |
Heinrich Schuchardt | 276c61d | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 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) |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 714 | u8 *comm_buf = NULL; |
| 715 | efi_status_t ret; |
| 716 | |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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 | |
Ilias Apalodimas | 24105b8 | 2020-07-01 16:41:25 +0300 | [diff] [blame] | 730 | if (in_name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE) { |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 731 | ret = EFI_INVALID_PARAMETER; |
| 732 | goto out; |
| 733 | } |
| 734 | |
| 735 | /* Trim output buffer size */ |
Ilias Apalodimas | 91b218d | 2020-07-22 01:50:37 +0300 | [diff] [blame] | 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; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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); |
Ilias Apalodimas | 91b218d | 2020-07-22 01:50:37 +0300 | [diff] [blame] | 763 | memcpy(variable_name, var_getnext->name, var_getnext->name_size); |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 764 | |
| 765 | out: |
| 766 | free(comm_buf); |
Heinrich Schuchardt | 276c61d | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 767 | return ret; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 768 | } |
| 769 | |
Heinrich Schuchardt | 1ad2f0d | 2021-09-09 07:12:14 +0200 | [diff] [blame] | 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) |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 774 | { |
Ilias Apalodimas | 5f1bce9 | 2020-07-09 23:00:40 +0300 | [diff] [blame] | 775 | efi_status_t ret, alt_ret = EFI_SUCCESS; |
| 776 | struct var_check_property var_property; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 777 | struct smm_variable_access *var_acc; |
| 778 | efi_uintn_t payload_size; |
| 779 | efi_uintn_t name_size; |
| 780 | u8 *comm_buf = NULL; |
Ilias Apalodimas | 5f1bce9 | 2020-07-09 23:00:40 +0300 | [diff] [blame] | 781 | bool ro; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 782 | |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 783 | if (!variable_name || variable_name[0] == 0 || !vendor) { |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 784 | ret = EFI_INVALID_PARAMETER; |
| 785 | goto out; |
| 786 | } |
| 787 | if (data_size > 0 && !data) { |
| 788 | ret = EFI_INVALID_PARAMETER; |
| 789 | goto out; |
| 790 | } |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 791 | /* Check payload size */ |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 792 | name_size = u16_strsize(variable_name); |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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 | |
Ilias Apalodimas | 5f1bce9 | 2020-07-09 23:00:40 +0300 | [diff] [blame] | 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 | */ |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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 | |
Ilias Apalodimas | 5f1bce9 | 2020-07-09 23:00:40 +0300 | [diff] [blame] | 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 | |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 834 | /* Fill in contents */ |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 835 | guidcpy(&var_acc->guid, vendor); |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 836 | var_acc->data_size = data_size; |
| 837 | var_acc->name_size = name_size; |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 838 | var_acc->attr = attributes; |
| 839 | memcpy(var_acc->name, variable_name, name_size); |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 840 | memcpy((u8 *)var_acc->name + name_size, data, data_size); |
| 841 | |
| 842 | /* Communicate */ |
| 843 | ret = mm_communicate(comm_buf, payload_size); |
Ilias Apalodimas | 5f1bce9 | 2020-07-09 23:00:40 +0300 | [diff] [blame] | 844 | if (ret != EFI_SUCCESS) |
| 845 | alt_ret = ret; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 846 | |
Ilias Apalodimas | 5f1bce9 | 2020-07-09 23:00:40 +0300 | [diff] [blame] | 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 | } |
Heinrich Schuchardt | 730e229 | 2020-07-14 08:14:08 +0200 | [diff] [blame] | 855 | |
| 856 | if (alt_ret != EFI_SUCCESS) |
| 857 | goto out; |
| 858 | |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 859 | if (!u16_strcmp(variable_name, u"PK")) |
Heinrich Schuchardt | 730e229 | 2020-07-14 08:14:08 +0200 | [diff] [blame] | 860 | alt_ret = efi_init_secure_state(); |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 861 | out: |
| 862 | free(comm_buf); |
Ilias Apalodimas | 5f1bce9 | 2020-07-09 23:00:40 +0300 | [diff] [blame] | 863 | return alt_ret == EFI_SUCCESS ? ret : alt_ret; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 864 | } |
| 865 | |
Heinrich Schuchardt | 276c61d | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 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) |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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 | |
Ilias Apalodimas | fc07153 | 2024-04-25 08:18:19 +0300 | [diff] [blame] | 876 | if (!max_variable_storage_size || |
| 877 | !remain_variable_storage_size || |
| 878 | !max_variable_size || !attributes) |
| 879 | return EFI_INVALID_PARAMETER; |
| 880 | |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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); |
Heinrich Schuchardt | 276c61d | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 899 | return ret; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | /** |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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 | { |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 951 | efi_status_t ret; |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 952 | u8 *comm_buf; |
| 953 | loff_t len; |
| 954 | struct efi_var_file *var_buf; |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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) |
Abdellatif El Khlifi | 431c7b5 | 2023-08-04 14:33:44 +0100 | [diff] [blame] | 964 | log_err("Unable to notify the MM partition for ExitBootServices\n"); |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 965 | free(comm_buf); |
| 966 | |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 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 Apalodimas | 3352144 | 2021-01-16 17:28:04 +0200 | [diff] [blame] | 971 | efi_var_buf_update(var_buf); |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 972 | free(var_buf); |
| 973 | |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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 | |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 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 | |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 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 | |
Heinrich Schuchardt | 730e229 | 2020-07-14 08:14:08 +0200 | [diff] [blame] | 1006 | ret = efi_init_secure_state(); |
| 1007 | if (ret != EFI_SUCCESS) |
| 1008 | return ret; |
| 1009 | |
Ilias Apalodimas | 77a364f | 2020-05-17 22:25:44 +0300 | [diff] [blame] | 1010 | return EFI_SUCCESS; |
| 1011 | } |