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