Heinrich Schuchardt | 29ef99c | 2020-03-22 09:07:50 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * File interface for UEFI variables |
| 4 | * |
| 5 | * Copyright (c) 2020, Heinrich Schuchardt |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <efi_loader.h> |
| 10 | #include <efi_variable.h> |
| 11 | #include <u-boot/crc.h> |
| 12 | |
Ilias Apalodimas | 3352144 | 2021-01-16 17:28:04 +0200 | [diff] [blame] | 13 | /* |
| 14 | * The variables efi_var_file and efi_var_entry must be static to avoid |
| 15 | * referencing them via the global offset table (section .got). The GOT |
| 16 | * is neither mapped as EfiRuntimeServicesData nor do we support its |
| 17 | * relocation during SetVirtualAddressMap(). |
| 18 | */ |
| 19 | static struct efi_var_file __efi_runtime_data *efi_var_buf; |
Heinrich Schuchardt | 29ef99c | 2020-03-22 09:07:50 +0100 | [diff] [blame] | 20 | static struct efi_var_entry __efi_runtime_data *efi_current_var; |
| 21 | |
| 22 | /** |
| 23 | * efi_var_mem_compare() - compare GUID and name with a variable |
| 24 | * |
| 25 | * @var: variable to compare |
| 26 | * @guid: GUID to compare |
| 27 | * @name: variable name to compare |
| 28 | * @next: pointer to next variable |
| 29 | * Return: true if match |
| 30 | */ |
| 31 | static bool __efi_runtime |
| 32 | efi_var_mem_compare(struct efi_var_entry *var, const efi_guid_t *guid, |
| 33 | const u16 *name, struct efi_var_entry **next) |
| 34 | { |
| 35 | int i; |
| 36 | u8 *guid1, *guid2; |
| 37 | const u16 *data, *var_name; |
| 38 | bool match = true; |
| 39 | |
| 40 | for (guid1 = (u8 *)&var->guid, guid2 = (u8 *)guid, i = 0; |
| 41 | i < sizeof(efi_guid_t) && match; ++i) |
| 42 | match = (guid1[i] == guid2[i]); |
| 43 | |
Heinrich Schuchardt | 4939020 | 2023-02-13 19:22:33 +0100 | [diff] [blame] | 44 | for (data = var->name, var_name = name;; ++data) { |
Heinrich Schuchardt | 29ef99c | 2020-03-22 09:07:50 +0100 | [diff] [blame] | 45 | if (match) |
| 46 | match = (*data == *var_name); |
| 47 | if (!*data) |
| 48 | break; |
Heinrich Schuchardt | 4939020 | 2023-02-13 19:22:33 +0100 | [diff] [blame] | 49 | if (*var_name) |
| 50 | ++var_name; |
Heinrich Schuchardt | 29ef99c | 2020-03-22 09:07:50 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | ++data; |
| 54 | |
| 55 | if (next) |
| 56 | *next = (struct efi_var_entry *) |
| 57 | ALIGN((uintptr_t)data + var->length, 8); |
| 58 | |
| 59 | if (match) |
| 60 | efi_current_var = var; |
| 61 | |
| 62 | return match; |
| 63 | } |
| 64 | |
| 65 | struct efi_var_entry __efi_runtime |
| 66 | *efi_var_mem_find(const efi_guid_t *guid, const u16 *name, |
| 67 | struct efi_var_entry **next) |
| 68 | { |
| 69 | struct efi_var_entry *var, *last; |
| 70 | |
| 71 | last = (struct efi_var_entry *) |
| 72 | ((uintptr_t)efi_var_buf + efi_var_buf->length); |
| 73 | |
| 74 | if (!*name) { |
| 75 | if (next) { |
| 76 | *next = efi_var_buf->var; |
| 77 | if (*next >= last) |
| 78 | *next = NULL; |
| 79 | } |
| 80 | return NULL; |
| 81 | } |
| 82 | if (efi_current_var && |
| 83 | efi_var_mem_compare(efi_current_var, guid, name, next)) { |
| 84 | if (next && *next >= last) |
| 85 | *next = NULL; |
| 86 | return efi_current_var; |
| 87 | } |
| 88 | |
| 89 | var = efi_var_buf->var; |
| 90 | if (var < last) { |
| 91 | for (; var;) { |
| 92 | struct efi_var_entry *pos; |
| 93 | bool match; |
| 94 | |
| 95 | match = efi_var_mem_compare(var, guid, name, &pos); |
| 96 | if (pos >= last) |
| 97 | pos = NULL; |
| 98 | if (match) { |
| 99 | if (next) |
| 100 | *next = pos; |
| 101 | return var; |
| 102 | } |
| 103 | var = pos; |
| 104 | } |
| 105 | } |
| 106 | if (next) |
| 107 | *next = NULL; |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | void __efi_runtime efi_var_mem_del(struct efi_var_entry *var) |
| 112 | { |
| 113 | u16 *data; |
| 114 | struct efi_var_entry *next, *last; |
| 115 | |
| 116 | if (!var) |
| 117 | return; |
| 118 | |
| 119 | last = (struct efi_var_entry *) |
| 120 | ((uintptr_t)efi_var_buf + efi_var_buf->length); |
| 121 | if (var <= efi_current_var) |
| 122 | efi_current_var = NULL; |
| 123 | |
| 124 | for (data = var->name; *data; ++data) |
| 125 | ; |
| 126 | ++data; |
| 127 | next = (struct efi_var_entry *) |
| 128 | ALIGN((uintptr_t)data + var->length, 8); |
| 129 | efi_var_buf->length -= (uintptr_t)next - (uintptr_t)var; |
| 130 | |
Heinrich Schuchardt | 447753e | 2020-07-22 07:56:14 +0200 | [diff] [blame] | 131 | /* efi_memcpy_runtime() can be used because next >= var. */ |
| 132 | efi_memcpy_runtime(var, next, (uintptr_t)last - (uintptr_t)next); |
Heinrich Schuchardt | 29ef99c | 2020-03-22 09:07:50 +0100 | [diff] [blame] | 133 | efi_var_buf->crc32 = crc32(0, (u8 *)efi_var_buf->var, |
| 134 | efi_var_buf->length - |
| 135 | sizeof(struct efi_var_file)); |
| 136 | } |
| 137 | |
| 138 | efi_status_t __efi_runtime efi_var_mem_ins( |
Heinrich Schuchardt | 1ad2f0d | 2021-09-09 07:12:14 +0200 | [diff] [blame] | 139 | const u16 *variable_name, |
Heinrich Schuchardt | 29ef99c | 2020-03-22 09:07:50 +0100 | [diff] [blame] | 140 | const efi_guid_t *vendor, u32 attributes, |
| 141 | const efi_uintn_t size1, const void *data1, |
| 142 | const efi_uintn_t size2, const void *data2, |
| 143 | const u64 time) |
| 144 | { |
| 145 | u16 *data; |
| 146 | struct efi_var_entry *var; |
| 147 | u32 var_name_len; |
| 148 | |
| 149 | var = (struct efi_var_entry *) |
| 150 | ((uintptr_t)efi_var_buf + efi_var_buf->length); |
Heinrich Schuchardt | 57a295b | 2022-12-29 10:50:54 +0100 | [diff] [blame] | 151 | var_name_len = u16_strlen(variable_name) + 1; |
Heinrich Schuchardt | 29ef99c | 2020-03-22 09:07:50 +0100 | [diff] [blame] | 152 | data = var->name + var_name_len; |
| 153 | |
| 154 | if ((uintptr_t)data - (uintptr_t)efi_var_buf + size1 + size2 > |
| 155 | EFI_VAR_BUF_SIZE) |
| 156 | return EFI_OUT_OF_RESOURCES; |
| 157 | |
| 158 | var->attr = attributes; |
| 159 | var->length = size1 + size2; |
| 160 | var->time = time; |
| 161 | |
| 162 | efi_memcpy_runtime(&var->guid, vendor, sizeof(efi_guid_t)); |
| 163 | efi_memcpy_runtime(var->name, variable_name, |
| 164 | sizeof(u16) * var_name_len); |
| 165 | efi_memcpy_runtime(data, data1, size1); |
| 166 | efi_memcpy_runtime((u8 *)data + size1, data2, size2); |
| 167 | |
| 168 | var = (struct efi_var_entry *) |
| 169 | ALIGN((uintptr_t)data + var->length, 8); |
| 170 | efi_var_buf->length = (uintptr_t)var - (uintptr_t)efi_var_buf; |
| 171 | efi_var_buf->crc32 = crc32(0, (u8 *)efi_var_buf->var, |
| 172 | efi_var_buf->length - |
| 173 | sizeof(struct efi_var_file)); |
| 174 | |
| 175 | return EFI_SUCCESS; |
| 176 | } |
| 177 | |
| 178 | u64 __efi_runtime efi_var_mem_free(void) |
| 179 | { |
| 180 | return EFI_VAR_BUF_SIZE - efi_var_buf->length - |
| 181 | sizeof(struct efi_var_entry); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * efi_var_mem_bs_del() - delete boot service only variables |
| 186 | */ |
| 187 | static void efi_var_mem_bs_del(void) |
| 188 | { |
| 189 | struct efi_var_entry *var = efi_var_buf->var; |
| 190 | |
| 191 | for (;;) { |
| 192 | struct efi_var_entry *last; |
| 193 | |
| 194 | last = (struct efi_var_entry *) |
| 195 | ((uintptr_t)efi_var_buf + efi_var_buf->length); |
| 196 | if (var >= last) |
| 197 | break; |
| 198 | if (var->attr & EFI_VARIABLE_RUNTIME_ACCESS) { |
| 199 | u16 *data; |
| 200 | |
| 201 | /* skip variable */ |
| 202 | for (data = var->name; *data; ++data) |
| 203 | ; |
| 204 | ++data; |
| 205 | var = (struct efi_var_entry *) |
| 206 | ALIGN((uintptr_t)data + var->length, 8); |
| 207 | } else { |
| 208 | /* delete variable */ |
| 209 | efi_var_mem_del(var); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * efi_var_mem_notify_exit_boot_services() - ExitBootService callback |
| 216 | * |
| 217 | * @event: callback event |
| 218 | * @context: callback context |
| 219 | */ |
Heinrich Schuchardt | 6dc6428 | 2020-09-08 10:51:27 +0000 | [diff] [blame] | 220 | static void EFIAPI |
Heinrich Schuchardt | 29ef99c | 2020-03-22 09:07:50 +0100 | [diff] [blame] | 221 | efi_var_mem_notify_exit_boot_services(struct efi_event *event, void *context) |
| 222 | { |
| 223 | EFI_ENTRY("%p, %p", event, context); |
| 224 | |
| 225 | /* Delete boot service only variables */ |
| 226 | efi_var_mem_bs_del(); |
| 227 | |
| 228 | EFI_EXIT(EFI_SUCCESS); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * efi_var_mem_notify_exit_boot_services() - SetVirtualMemoryMap callback |
| 233 | * |
| 234 | * @event: callback event |
| 235 | * @context: callback context |
| 236 | */ |
| 237 | static void EFIAPI __efi_runtime |
| 238 | efi_var_mem_notify_virtual_address_map(struct efi_event *event, void *context) |
| 239 | { |
| 240 | efi_convert_pointer(0, (void **)&efi_var_buf); |
Heinrich Schuchardt | eb7dbe8 | 2020-07-22 06:29:38 +0200 | [diff] [blame] | 241 | efi_current_var = NULL; |
Heinrich Schuchardt | 29ef99c | 2020-03-22 09:07:50 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | efi_status_t efi_var_mem_init(void) |
| 245 | { |
| 246 | u64 memory; |
| 247 | efi_status_t ret; |
| 248 | struct efi_event *event; |
| 249 | |
| 250 | ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, |
| 251 | EFI_RUNTIME_SERVICES_DATA, |
| 252 | efi_size_in_pages(EFI_VAR_BUF_SIZE), |
| 253 | &memory); |
| 254 | if (ret != EFI_SUCCESS) |
| 255 | return ret; |
| 256 | efi_var_buf = (struct efi_var_file *)(uintptr_t)memory; |
| 257 | memset(efi_var_buf, 0, EFI_VAR_BUF_SIZE); |
| 258 | efi_var_buf->magic = EFI_VAR_FILE_MAGIC; |
| 259 | efi_var_buf->length = (uintptr_t)efi_var_buf->var - |
| 260 | (uintptr_t)efi_var_buf; |
| 261 | /* crc32 for 0 bytes = 0 */ |
| 262 | |
| 263 | ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK, |
| 264 | efi_var_mem_notify_exit_boot_services, NULL, |
| 265 | NULL, &event); |
| 266 | if (ret != EFI_SUCCESS) |
| 267 | return ret; |
| 268 | ret = efi_create_event(EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE, TPL_CALLBACK, |
| 269 | efi_var_mem_notify_virtual_address_map, NULL, |
| 270 | NULL, &event); |
| 271 | if (ret != EFI_SUCCESS) |
| 272 | return ret; |
| 273 | return ret; |
| 274 | } |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 275 | |
| 276 | efi_status_t __efi_runtime |
Heinrich Schuchardt | 1ad2f0d | 2021-09-09 07:12:14 +0200 | [diff] [blame] | 277 | efi_get_variable_mem(const u16 *variable_name, const efi_guid_t *vendor, |
| 278 | u32 *attributes, efi_uintn_t *data_size, void *data, |
| 279 | u64 *timep) |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 280 | { |
| 281 | efi_uintn_t old_size; |
| 282 | struct efi_var_entry *var; |
| 283 | u16 *pdata; |
| 284 | |
| 285 | if (!variable_name || !vendor || !data_size) |
| 286 | return EFI_INVALID_PARAMETER; |
| 287 | var = efi_var_mem_find(vendor, variable_name, NULL); |
| 288 | if (!var) |
| 289 | return EFI_NOT_FOUND; |
| 290 | |
| 291 | if (attributes) |
| 292 | *attributes = var->attr; |
| 293 | if (timep) |
| 294 | *timep = var->time; |
| 295 | |
| 296 | old_size = *data_size; |
| 297 | *data_size = var->length; |
| 298 | if (old_size < var->length) |
| 299 | return EFI_BUFFER_TOO_SMALL; |
| 300 | |
| 301 | if (!data) |
| 302 | return EFI_INVALID_PARAMETER; |
| 303 | |
| 304 | for (pdata = var->name; *pdata; ++pdata) |
| 305 | ; |
| 306 | ++pdata; |
| 307 | |
| 308 | efi_memcpy_runtime(data, pdata, var->length); |
| 309 | |
| 310 | return EFI_SUCCESS; |
| 311 | } |
| 312 | |
| 313 | efi_status_t __efi_runtime |
Heinrich Schuchardt | cd9f860 | 2020-11-19 19:40:08 +0100 | [diff] [blame] | 314 | efi_get_next_variable_name_mem(efi_uintn_t *variable_name_size, |
| 315 | u16 *variable_name, efi_guid_t *vendor) |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 316 | { |
| 317 | struct efi_var_entry *var; |
Heinrich Schuchardt | 4e82c2e | 2022-12-18 06:08:57 +0000 | [diff] [blame] | 318 | efi_uintn_t len, old_size; |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 319 | u16 *pdata; |
| 320 | |
| 321 | if (!variable_name_size || !variable_name || !vendor) |
| 322 | return EFI_INVALID_PARAMETER; |
| 323 | |
Heinrich Schuchardt | 4e82c2e | 2022-12-18 06:08:57 +0000 | [diff] [blame] | 324 | len = *variable_name_size >> 1; |
| 325 | if (u16_strnlen(variable_name, len) == len) |
Heinrich Schuchardt | cd9f860 | 2020-11-19 19:40:08 +0100 | [diff] [blame] | 326 | return EFI_INVALID_PARAMETER; |
| 327 | |
| 328 | if (!efi_var_mem_find(vendor, variable_name, &var) && *variable_name) |
| 329 | return EFI_INVALID_PARAMETER; |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 330 | |
| 331 | if (!var) |
| 332 | return EFI_NOT_FOUND; |
| 333 | |
| 334 | for (pdata = var->name; *pdata; ++pdata) |
| 335 | ; |
| 336 | ++pdata; |
| 337 | |
| 338 | old_size = *variable_name_size; |
| 339 | *variable_name_size = (uintptr_t)pdata - (uintptr_t)var->name; |
| 340 | |
| 341 | if (old_size < *variable_name_size) |
| 342 | return EFI_BUFFER_TOO_SMALL; |
| 343 | |
| 344 | efi_memcpy_runtime(variable_name, var->name, *variable_name_size); |
| 345 | efi_memcpy_runtime(vendor, &var->guid, sizeof(efi_guid_t)); |
| 346 | |
| 347 | return EFI_SUCCESS; |
| 348 | } |
Ilias Apalodimas | 3352144 | 2021-01-16 17:28:04 +0200 | [diff] [blame] | 349 | |
| 350 | void efi_var_buf_update(struct efi_var_file *var_buf) |
| 351 | { |
| 352 | memcpy(efi_var_buf, var_buf, EFI_VAR_BUF_SIZE); |
| 353 | } |