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