Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

efi: vars: Remove deprecated 'efivars' sysfs interface

Commit 5d9db883761a ("efi: Add support for a UEFI variable filesystem")
dated Oct 5, 2012, introduced a new efivarfs pseudo-filesystem to
replace the efivars sysfs interface that was used up to that point to
expose EFI variables to user space.

The main problem with the sysfs interface was that it only supported up
to 1024 bytes of payload per file, whereas the underlying variables
themselves are only bounded by a platform specific per-variable and
global limit that is typically much higher than 1024 bytes.

The deprecated sysfs interface is only enabled on x86 and Itanium, other
EFI enabled architectures only support the efivarfs pseudo-filesystem.

So let's finally rip off the band aid, and drop the old interface
entirely. This will make it easier to refactor and clean up the
underlying infrastructure that is shared between efivars, efivarfs and
efi-pstore, and is long overdue for a makeover.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>

+3 -963
+1 -1
Documentation/x86/x86_64/uefi.rst
··· 29 29 be selected:: 30 30 31 31 CONFIG_EFI=y 32 - CONFIG_EFI_VARS=y or m # optional 32 + CONFIG_EFIVAR_FS=y or m # optional 33 33 34 34 - Create a VFAT partition on the disk 35 35 - Copy the following to the VFAT partition:
-1
arch/arm/configs/milbeaut_m10v_defconfig
··· 44 44 CONFIG_VFP=y 45 45 CONFIG_NEON=y 46 46 CONFIG_KERNEL_MODE_NEON=y 47 - CONFIG_EFI_VARS=m 48 47 CONFIG_EFI_CAPSULE_LOADER=m 49 48 CONFIG_ARM_CRYPTO=y 50 49 CONFIG_CRYPTO_SHA1_ARM_NEON=m
-1
arch/ia64/configs/bigsur_defconfig
··· 10 10 CONFIG_NR_CPUS=2 11 11 CONFIG_PREEMPT=y 12 12 CONFIG_IA64_PALINFO=y 13 - CONFIG_EFI_VARS=y 14 13 CONFIG_BINFMT_MISC=m 15 14 CONFIG_ACPI_BUTTON=m 16 15 CONFIG_ACPI_FAN=m
-1
arch/ia64/configs/generic_defconfig
··· 21 21 CONFIG_IA64_PALINFO=y 22 22 CONFIG_KEXEC=y 23 23 CONFIG_CRASH_DUMP=y 24 - CONFIG_EFI_VARS=y 25 24 CONFIG_BINFMT_MISC=m 26 25 CONFIG_ACPI_BUTTON=m 27 26 CONFIG_ACPI_FAN=m
-1
arch/ia64/configs/gensparse_defconfig
··· 18 18 CONFIG_SPARSEMEM_MANUAL=y 19 19 CONFIG_IA64_MCA_RECOVERY=y 20 20 CONFIG_IA64_PALINFO=y 21 - CONFIG_EFI_VARS=y 22 21 CONFIG_BINFMT_MISC=m 23 22 CONFIG_ACPI_BUTTON=m 24 23 CONFIG_ACPI_FAN=m
-1
arch/ia64/configs/tiger_defconfig
··· 23 23 CONFIG_IA64_MCA_RECOVERY=y 24 24 CONFIG_IA64_PALINFO=y 25 25 CONFIG_KEXEC=y 26 - CONFIG_EFI_VARS=y 27 26 CONFIG_BINFMT_MISC=m 28 27 CONFIG_ACPI_BUTTON=m 29 28 CONFIG_ACPI_FAN=m
-1
arch/ia64/configs/zx1_defconfig
··· 12 12 CONFIG_IA64_MCA_RECOVERY=y 13 13 CONFIG_IA64_PALINFO=y 14 14 CONFIG_CRASH_DUMP=y 15 - CONFIG_EFI_VARS=y 16 15 CONFIG_BINFMT_MISC=y 17 16 CONFIG_HOTPLUG_PCI=y 18 17 CONFIG_HOTPLUG_PCI_ACPI=y
-1
arch/x86/configs/i386_defconfig
··· 135 135 CONFIG_DEVTMPFS_MOUNT=y 136 136 CONFIG_DEBUG_DEVRES=y 137 137 CONFIG_CONNECTOR=y 138 - CONFIG_EFI_VARS=y 139 138 CONFIG_EFI_CAPSULE_LOADER=y 140 139 CONFIG_BLK_DEV_LOOP=y 141 140 CONFIG_VIRTIO_BLK=y
-1
arch/x86/configs/x86_64_defconfig
··· 134 134 CONFIG_DEVTMPFS_MOUNT=y 135 135 CONFIG_DEBUG_DEVRES=y 136 136 CONFIG_CONNECTOR=y 137 - CONFIG_EFI_VARS=y 138 137 CONFIG_BLK_DEV_LOOP=y 139 138 CONFIG_VIRTIO_BLK=y 140 139 CONFIG_BLK_DEV_SD=y
-12
drivers/firmware/efi/Kconfig
··· 2 2 menu "EFI (Extensible Firmware Interface) Support" 3 3 depends on EFI 4 4 5 - config EFI_VARS 6 - tristate "EFI Variable Support via sysfs" 7 - depends on EFI && (X86 || IA64) 8 - default n 9 - help 10 - If you say Y here, you are able to get EFI (Extensible Firmware 11 - Interface) variable information via sysfs. You may read, 12 - write, create, and destroy EFI variables through this interface. 13 - Note that this driver is only retained for compatibility with 14 - legacy users: new users should use the efivarfs filesystem 15 - instead. 16 - 17 5 config EFI_ESRT 18 6 bool 19 7 depends on EFI && !IA64
-1
drivers/firmware/efi/Makefile
··· 17 17 obj-$(CONFIG_EFI) += capsule.o 18 18 endif 19 19 obj-$(CONFIG_EFI_PARAMS_FROM_FDT) += fdtparams.o 20 - obj-$(CONFIG_EFI_VARS) += efivars.o 21 20 obj-$(CONFIG_EFI_ESRT) += esrt.o 22 21 obj-$(CONFIG_EFI_VARS_PSTORE) += efi-pstore.o 23 22 obj-$(CONFIG_UEFI_CPER) += cper.o
-660
drivers/firmware/efi/efivars.c
··· 1 - // SPDX-License-Identifier: GPL-2.0+ 2 - /* 3 - * Originally from efivars.c, 4 - * 5 - * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com> 6 - * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com> 7 - * 8 - * This code takes all variables accessible from EFI runtime and 9 - * exports them via sysfs 10 - */ 11 - 12 - #include <linux/efi.h> 13 - #include <linux/module.h> 14 - #include <linux/slab.h> 15 - #include <linux/ucs2_string.h> 16 - #include <linux/compat.h> 17 - 18 - #define EFIVARS_VERSION "0.08" 19 - #define EFIVARS_DATE "2004-May-17" 20 - 21 - MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>"); 22 - MODULE_DESCRIPTION("sysfs interface to EFI Variables"); 23 - MODULE_LICENSE("GPL"); 24 - MODULE_VERSION(EFIVARS_VERSION); 25 - 26 - static LIST_HEAD(efivar_sysfs_list); 27 - 28 - static struct kset *efivars_kset; 29 - 30 - static struct bin_attribute *efivars_new_var; 31 - static struct bin_attribute *efivars_del_var; 32 - 33 - struct compat_efi_variable { 34 - efi_char16_t VariableName[EFI_VAR_NAME_LEN/sizeof(efi_char16_t)]; 35 - efi_guid_t VendorGuid; 36 - __u32 DataSize; 37 - __u8 Data[1024]; 38 - __u32 Status; 39 - __u32 Attributes; 40 - } __packed; 41 - 42 - struct efivar_attribute { 43 - struct attribute attr; 44 - ssize_t (*show) (struct efivar_entry *entry, char *buf); 45 - ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count); 46 - }; 47 - 48 - #define EFIVAR_ATTR(_name, _mode, _show, _store) \ 49 - struct efivar_attribute efivar_attr_##_name = { \ 50 - .attr = {.name = __stringify(_name), .mode = _mode}, \ 51 - .show = _show, \ 52 - .store = _store, \ 53 - }; 54 - 55 - #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr) 56 - #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj) 57 - 58 - /* 59 - * Prototype for sysfs creation function 60 - */ 61 - static int 62 - efivar_create_sysfs_entry(struct efivar_entry *new_var); 63 - 64 - static ssize_t 65 - efivar_guid_read(struct efivar_entry *entry, char *buf) 66 - { 67 - struct efi_variable *var = &entry->var; 68 - char *str = buf; 69 - 70 - if (!entry || !buf) 71 - return 0; 72 - 73 - efi_guid_to_str(&var->VendorGuid, str); 74 - str += strlen(str); 75 - str += sprintf(str, "\n"); 76 - 77 - return str - buf; 78 - } 79 - 80 - static ssize_t 81 - efivar_attr_read(struct efivar_entry *entry, char *buf) 82 - { 83 - struct efi_variable *var = &entry->var; 84 - unsigned long size = sizeof(var->Data); 85 - char *str = buf; 86 - int ret; 87 - 88 - if (!entry || !buf) 89 - return -EINVAL; 90 - 91 - ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data); 92 - var->DataSize = size; 93 - if (ret) 94 - return -EIO; 95 - 96 - if (var->Attributes & EFI_VARIABLE_NON_VOLATILE) 97 - str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n"); 98 - if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS) 99 - str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n"); 100 - if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) 101 - str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n"); 102 - if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) 103 - str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n"); 104 - if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) 105 - str += sprintf(str, 106 - "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n"); 107 - if (var->Attributes & 108 - EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) 109 - str += sprintf(str, 110 - "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n"); 111 - if (var->Attributes & EFI_VARIABLE_APPEND_WRITE) 112 - str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n"); 113 - return str - buf; 114 - } 115 - 116 - static ssize_t 117 - efivar_size_read(struct efivar_entry *entry, char *buf) 118 - { 119 - struct efi_variable *var = &entry->var; 120 - unsigned long size = sizeof(var->Data); 121 - char *str = buf; 122 - int ret; 123 - 124 - if (!entry || !buf) 125 - return -EINVAL; 126 - 127 - ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data); 128 - var->DataSize = size; 129 - if (ret) 130 - return -EIO; 131 - 132 - str += sprintf(str, "0x%lx\n", var->DataSize); 133 - return str - buf; 134 - } 135 - 136 - static ssize_t 137 - efivar_data_read(struct efivar_entry *entry, char *buf) 138 - { 139 - struct efi_variable *var = &entry->var; 140 - unsigned long size = sizeof(var->Data); 141 - int ret; 142 - 143 - if (!entry || !buf) 144 - return -EINVAL; 145 - 146 - ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data); 147 - var->DataSize = size; 148 - if (ret) 149 - return -EIO; 150 - 151 - memcpy(buf, var->Data, var->DataSize); 152 - return var->DataSize; 153 - } 154 - 155 - static inline int 156 - sanity_check(struct efi_variable *var, efi_char16_t *name, efi_guid_t vendor, 157 - unsigned long size, u32 attributes, u8 *data) 158 - { 159 - /* 160 - * If only updating the variable data, then the name 161 - * and guid should remain the same 162 - */ 163 - if (memcmp(name, var->VariableName, sizeof(var->VariableName)) || 164 - efi_guidcmp(vendor, var->VendorGuid)) { 165 - printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n"); 166 - return -EINVAL; 167 - } 168 - 169 - if ((size <= 0) || (attributes == 0)){ 170 - printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n"); 171 - return -EINVAL; 172 - } 173 - 174 - if ((attributes & ~EFI_VARIABLE_MASK) != 0 || 175 - efivar_validate(vendor, name, data, size) == false) { 176 - printk(KERN_ERR "efivars: Malformed variable content\n"); 177 - return -EINVAL; 178 - } 179 - 180 - return 0; 181 - } 182 - 183 - static void 184 - copy_out_compat(struct efi_variable *dst, struct compat_efi_variable *src) 185 - { 186 - memcpy(dst->VariableName, src->VariableName, EFI_VAR_NAME_LEN); 187 - memcpy(dst->Data, src->Data, sizeof(src->Data)); 188 - 189 - dst->VendorGuid = src->VendorGuid; 190 - dst->DataSize = src->DataSize; 191 - dst->Attributes = src->Attributes; 192 - } 193 - 194 - /* 195 - * We allow each variable to be edited via rewriting the 196 - * entire efi variable structure. 197 - */ 198 - static ssize_t 199 - efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count) 200 - { 201 - struct efi_variable *new_var, *var = &entry->var; 202 - efi_char16_t *name; 203 - unsigned long size; 204 - efi_guid_t vendor; 205 - u32 attributes; 206 - u8 *data; 207 - int err; 208 - 209 - if (!entry || !buf) 210 - return -EINVAL; 211 - 212 - if (in_compat_syscall()) { 213 - struct compat_efi_variable *compat; 214 - 215 - if (count != sizeof(*compat)) 216 - return -EINVAL; 217 - 218 - compat = (struct compat_efi_variable *)buf; 219 - attributes = compat->Attributes; 220 - vendor = compat->VendorGuid; 221 - name = compat->VariableName; 222 - size = compat->DataSize; 223 - data = compat->Data; 224 - 225 - err = sanity_check(var, name, vendor, size, attributes, data); 226 - if (err) 227 - return err; 228 - 229 - copy_out_compat(&entry->var, compat); 230 - } else { 231 - if (count != sizeof(struct efi_variable)) 232 - return -EINVAL; 233 - 234 - new_var = (struct efi_variable *)buf; 235 - 236 - attributes = new_var->Attributes; 237 - vendor = new_var->VendorGuid; 238 - name = new_var->VariableName; 239 - size = new_var->DataSize; 240 - data = new_var->Data; 241 - 242 - err = sanity_check(var, name, vendor, size, attributes, data); 243 - if (err) 244 - return err; 245 - 246 - memcpy(&entry->var, new_var, count); 247 - } 248 - 249 - err = efivar_entry_set(entry, attributes, size, data, NULL); 250 - if (err) { 251 - printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err); 252 - return -EIO; 253 - } 254 - 255 - return count; 256 - } 257 - 258 - static ssize_t 259 - efivar_show_raw(struct efivar_entry *entry, char *buf) 260 - { 261 - struct efi_variable *var = &entry->var; 262 - struct compat_efi_variable *compat; 263 - unsigned long datasize = sizeof(var->Data); 264 - size_t size; 265 - int ret; 266 - 267 - if (!entry || !buf) 268 - return 0; 269 - 270 - ret = efivar_entry_get(entry, &var->Attributes, &datasize, var->Data); 271 - var->DataSize = datasize; 272 - if (ret) 273 - return -EIO; 274 - 275 - if (in_compat_syscall()) { 276 - compat = (struct compat_efi_variable *)buf; 277 - 278 - size = sizeof(*compat); 279 - memcpy(compat->VariableName, var->VariableName, 280 - EFI_VAR_NAME_LEN); 281 - memcpy(compat->Data, var->Data, sizeof(compat->Data)); 282 - 283 - compat->VendorGuid = var->VendorGuid; 284 - compat->DataSize = var->DataSize; 285 - compat->Attributes = var->Attributes; 286 - } else { 287 - size = sizeof(*var); 288 - memcpy(buf, var, size); 289 - } 290 - 291 - return size; 292 - } 293 - 294 - /* 295 - * Generic read/write functions that call the specific functions of 296 - * the attributes... 297 - */ 298 - static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr, 299 - char *buf) 300 - { 301 - struct efivar_entry *var = to_efivar_entry(kobj); 302 - struct efivar_attribute *efivar_attr = to_efivar_attr(attr); 303 - ssize_t ret = -EIO; 304 - 305 - if (!capable(CAP_SYS_ADMIN)) 306 - return -EACCES; 307 - 308 - if (efivar_attr->show) { 309 - ret = efivar_attr->show(var, buf); 310 - } 311 - return ret; 312 - } 313 - 314 - static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr, 315 - const char *buf, size_t count) 316 - { 317 - struct efivar_entry *var = to_efivar_entry(kobj); 318 - struct efivar_attribute *efivar_attr = to_efivar_attr(attr); 319 - ssize_t ret = -EIO; 320 - 321 - if (!capable(CAP_SYS_ADMIN)) 322 - return -EACCES; 323 - 324 - if (efivar_attr->store) 325 - ret = efivar_attr->store(var, buf, count); 326 - 327 - return ret; 328 - } 329 - 330 - static const struct sysfs_ops efivar_attr_ops = { 331 - .show = efivar_attr_show, 332 - .store = efivar_attr_store, 333 - }; 334 - 335 - static void efivar_release(struct kobject *kobj) 336 - { 337 - struct efivar_entry *var = to_efivar_entry(kobj); 338 - kfree(var); 339 - } 340 - 341 - static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL); 342 - static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL); 343 - static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL); 344 - static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL); 345 - static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw); 346 - 347 - static struct attribute *def_attrs[] = { 348 - &efivar_attr_guid.attr, 349 - &efivar_attr_size.attr, 350 - &efivar_attr_attributes.attr, 351 - &efivar_attr_data.attr, 352 - &efivar_attr_raw_var.attr, 353 - NULL, 354 - }; 355 - ATTRIBUTE_GROUPS(def); 356 - 357 - static struct kobj_type efivar_ktype = { 358 - .release = efivar_release, 359 - .sysfs_ops = &efivar_attr_ops, 360 - .default_groups = def_groups, 361 - }; 362 - 363 - static ssize_t efivar_create(struct file *filp, struct kobject *kobj, 364 - struct bin_attribute *bin_attr, 365 - char *buf, loff_t pos, size_t count) 366 - { 367 - struct compat_efi_variable *compat = (struct compat_efi_variable *)buf; 368 - struct efi_variable *new_var = (struct efi_variable *)buf; 369 - struct efivar_entry *new_entry; 370 - bool need_compat = in_compat_syscall(); 371 - efi_char16_t *name; 372 - unsigned long size; 373 - u32 attributes; 374 - u8 *data; 375 - int err; 376 - 377 - if (!capable(CAP_SYS_ADMIN)) 378 - return -EACCES; 379 - 380 - if (need_compat) { 381 - if (count != sizeof(*compat)) 382 - return -EINVAL; 383 - 384 - attributes = compat->Attributes; 385 - name = compat->VariableName; 386 - size = compat->DataSize; 387 - data = compat->Data; 388 - } else { 389 - if (count != sizeof(*new_var)) 390 - return -EINVAL; 391 - 392 - attributes = new_var->Attributes; 393 - name = new_var->VariableName; 394 - size = new_var->DataSize; 395 - data = new_var->Data; 396 - } 397 - 398 - if ((attributes & ~EFI_VARIABLE_MASK) != 0 || 399 - efivar_validate(new_var->VendorGuid, name, data, 400 - size) == false) { 401 - printk(KERN_ERR "efivars: Malformed variable content\n"); 402 - return -EINVAL; 403 - } 404 - 405 - new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL); 406 - if (!new_entry) 407 - return -ENOMEM; 408 - 409 - if (need_compat) 410 - copy_out_compat(&new_entry->var, compat); 411 - else 412 - memcpy(&new_entry->var, new_var, sizeof(*new_var)); 413 - 414 - err = efivar_entry_set(new_entry, attributes, size, 415 - data, &efivar_sysfs_list); 416 - if (err) { 417 - if (err == -EEXIST) 418 - err = -EINVAL; 419 - goto out; 420 - } 421 - 422 - if (efivar_create_sysfs_entry(new_entry)) { 423 - printk(KERN_WARNING "efivars: failed to create sysfs entry.\n"); 424 - kfree(new_entry); 425 - } 426 - return count; 427 - 428 - out: 429 - kfree(new_entry); 430 - return err; 431 - } 432 - 433 - static ssize_t efivar_delete(struct file *filp, struct kobject *kobj, 434 - struct bin_attribute *bin_attr, 435 - char *buf, loff_t pos, size_t count) 436 - { 437 - struct efi_variable *del_var = (struct efi_variable *)buf; 438 - struct compat_efi_variable *compat; 439 - struct efivar_entry *entry; 440 - efi_char16_t *name; 441 - efi_guid_t vendor; 442 - int err = 0; 443 - 444 - if (!capable(CAP_SYS_ADMIN)) 445 - return -EACCES; 446 - 447 - if (in_compat_syscall()) { 448 - if (count != sizeof(*compat)) 449 - return -EINVAL; 450 - 451 - compat = (struct compat_efi_variable *)buf; 452 - name = compat->VariableName; 453 - vendor = compat->VendorGuid; 454 - } else { 455 - if (count != sizeof(*del_var)) 456 - return -EINVAL; 457 - 458 - name = del_var->VariableName; 459 - vendor = del_var->VendorGuid; 460 - } 461 - 462 - if (efivar_entry_iter_begin()) 463 - return -EINTR; 464 - entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true); 465 - if (!entry) 466 - err = -EINVAL; 467 - else if (__efivar_entry_delete(entry)) 468 - err = -EIO; 469 - 470 - efivar_entry_iter_end(); 471 - 472 - if (err) 473 - return err; 474 - 475 - efivar_unregister(entry); 476 - 477 - /* It's dead Jim.... */ 478 - return count; 479 - } 480 - 481 - /** 482 - * efivar_create_sysfs_entry - create a new entry in sysfs 483 - * @new_var: efivar entry to create 484 - * 485 - * Returns 0 on success, negative error code on failure 486 - */ 487 - static int 488 - efivar_create_sysfs_entry(struct efivar_entry *new_var) 489 - { 490 - int short_name_size; 491 - char *short_name; 492 - unsigned long utf8_name_size; 493 - efi_char16_t *variable_name = new_var->var.VariableName; 494 - int ret; 495 - 496 - /* 497 - * Length of the variable bytes in UTF8, plus the '-' separator, 498 - * plus the GUID, plus trailing NUL 499 - */ 500 - utf8_name_size = ucs2_utf8size(variable_name); 501 - short_name_size = utf8_name_size + 1 + EFI_VARIABLE_GUID_LEN + 1; 502 - 503 - short_name = kmalloc(short_name_size, GFP_KERNEL); 504 - if (!short_name) 505 - return -ENOMEM; 506 - 507 - ucs2_as_utf8(short_name, variable_name, short_name_size); 508 - 509 - /* This is ugly, but necessary to separate one vendor's 510 - private variables from another's. */ 511 - short_name[utf8_name_size] = '-'; 512 - efi_guid_to_str(&new_var->var.VendorGuid, 513 - short_name + utf8_name_size + 1); 514 - 515 - new_var->kobj.kset = efivars_kset; 516 - 517 - ret = kobject_init_and_add(&new_var->kobj, &efivar_ktype, 518 - NULL, "%s", short_name); 519 - kfree(short_name); 520 - if (ret) { 521 - kobject_put(&new_var->kobj); 522 - return ret; 523 - } 524 - 525 - kobject_uevent(&new_var->kobj, KOBJ_ADD); 526 - __efivar_entry_add(new_var, &efivar_sysfs_list); 527 - 528 - return 0; 529 - } 530 - 531 - static int 532 - create_efivars_bin_attributes(void) 533 - { 534 - struct bin_attribute *attr; 535 - int error; 536 - 537 - /* new_var */ 538 - attr = kzalloc(sizeof(*attr), GFP_KERNEL); 539 - if (!attr) 540 - return -ENOMEM; 541 - 542 - attr->attr.name = "new_var"; 543 - attr->attr.mode = 0200; 544 - attr->write = efivar_create; 545 - efivars_new_var = attr; 546 - 547 - /* del_var */ 548 - attr = kzalloc(sizeof(*attr), GFP_KERNEL); 549 - if (!attr) { 550 - error = -ENOMEM; 551 - goto out_free; 552 - } 553 - attr->attr.name = "del_var"; 554 - attr->attr.mode = 0200; 555 - attr->write = efivar_delete; 556 - efivars_del_var = attr; 557 - 558 - sysfs_bin_attr_init(efivars_new_var); 559 - sysfs_bin_attr_init(efivars_del_var); 560 - 561 - /* Register */ 562 - error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_new_var); 563 - if (error) { 564 - printk(KERN_ERR "efivars: unable to create new_var sysfs file" 565 - " due to error %d\n", error); 566 - goto out_free; 567 - } 568 - 569 - error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_del_var); 570 - if (error) { 571 - printk(KERN_ERR "efivars: unable to create del_var sysfs file" 572 - " due to error %d\n", error); 573 - sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var); 574 - goto out_free; 575 - } 576 - 577 - return 0; 578 - out_free: 579 - kfree(efivars_del_var); 580 - efivars_del_var = NULL; 581 - kfree(efivars_new_var); 582 - efivars_new_var = NULL; 583 - return error; 584 - } 585 - 586 - static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor, 587 - unsigned long name_size, void *data) 588 - { 589 - struct efivar_entry *entry; 590 - 591 - entry = kzalloc(sizeof(*entry), GFP_KERNEL); 592 - if (!entry) 593 - return -ENOMEM; 594 - 595 - memcpy(entry->var.VariableName, name, name_size); 596 - memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t)); 597 - 598 - efivar_create_sysfs_entry(entry); 599 - 600 - return 0; 601 - } 602 - 603 - static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data) 604 - { 605 - efivar_entry_remove(entry); 606 - efivar_unregister(entry); 607 - return 0; 608 - } 609 - 610 - static void efivars_sysfs_exit(void) 611 - { 612 - /* Remove all entries and destroy */ 613 - int err; 614 - 615 - err = efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list, NULL); 616 - if (err) { 617 - pr_err("efivars: Failed to destroy sysfs entries\n"); 618 - return; 619 - } 620 - 621 - if (efivars_new_var) 622 - sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var); 623 - if (efivars_del_var) 624 - sysfs_remove_bin_file(&efivars_kset->kobj, efivars_del_var); 625 - kfree(efivars_new_var); 626 - kfree(efivars_del_var); 627 - kset_unregister(efivars_kset); 628 - } 629 - 630 - static int efivars_sysfs_init(void) 631 - { 632 - struct kobject *parent_kobj = efivars_kobject(); 633 - int error = 0; 634 - 635 - /* No efivars has been registered yet */ 636 - if (!parent_kobj || !efivar_supports_writes()) 637 - return 0; 638 - 639 - printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION, 640 - EFIVARS_DATE); 641 - 642 - efivars_kset = kset_create_and_add("vars", NULL, parent_kobj); 643 - if (!efivars_kset) { 644 - printk(KERN_ERR "efivars: Subsystem registration failed.\n"); 645 - return -ENOMEM; 646 - } 647 - 648 - efivar_init(efivars_sysfs_callback, NULL, true, &efivar_sysfs_list); 649 - 650 - error = create_efivars_bin_attributes(); 651 - if (error) { 652 - efivars_sysfs_exit(); 653 - return error; 654 - } 655 - 656 - return 0; 657 - } 658 - 659 - module_init(efivars_sysfs_init); 660 - module_exit(efivars_sysfs_exit);
+2 -263
drivers/firmware/efi/vars.c
··· 548 548 } 549 549 550 550 /** 551 - * __efivar_entry_delete - delete an EFI variable 552 - * @entry: entry containing EFI variable to delete 553 - * 554 - * Delete the variable from the firmware but leave @entry on the 555 - * variable list. 556 - * 557 - * This function differs from efivar_entry_delete() because it does 558 - * not remove @entry from the variable list. Also, it is safe to be 559 - * called from within a efivar_entry_iter_begin() and 560 - * efivar_entry_iter_end() region, unlike efivar_entry_delete(). 561 - * 562 - * Returns 0 on success, or a converted EFI status code if 563 - * set_variable() fails. 564 - */ 565 - int __efivar_entry_delete(struct efivar_entry *entry) 566 - { 567 - efi_status_t status; 568 - 569 - if (!__efivars) 570 - return -EINVAL; 571 - 572 - status = __efivars->ops->set_variable(entry->var.VariableName, 573 - &entry->var.VendorGuid, 574 - 0, 0, NULL); 575 - 576 - return efi_status_to_err(status); 577 - } 578 - EXPORT_SYMBOL_GPL(__efivar_entry_delete); 579 - 580 - /** 581 551 * efivar_entry_delete - delete variable and remove entry from list 582 552 * @entry: entry containing variable to delete 583 553 * ··· 583 613 return 0; 584 614 } 585 615 EXPORT_SYMBOL_GPL(efivar_entry_delete); 586 - 587 - /** 588 - * efivar_entry_set - call set_variable() 589 - * @entry: entry containing the EFI variable to write 590 - * @attributes: variable attributes 591 - * @size: size of @data buffer 592 - * @data: buffer containing variable data 593 - * @head: head of variable list 594 - * 595 - * Calls set_variable() for an EFI variable. If creating a new EFI 596 - * variable, this function is usually followed by efivar_entry_add(). 597 - * 598 - * Before writing the variable, the remaining EFI variable storage 599 - * space is checked to ensure there is enough room available. 600 - * 601 - * If @head is not NULL a lookup is performed to determine whether 602 - * the entry is already on the list. 603 - * 604 - * Returns 0 on success, -EINTR if we can't grab the semaphore, 605 - * -EEXIST if a lookup is performed and the entry already exists on 606 - * the list, or a converted EFI status code if set_variable() fails. 607 - */ 608 - int efivar_entry_set(struct efivar_entry *entry, u32 attributes, 609 - unsigned long size, void *data, struct list_head *head) 610 - { 611 - const struct efivar_operations *ops; 612 - efi_status_t status; 613 - efi_char16_t *name = entry->var.VariableName; 614 - efi_guid_t vendor = entry->var.VendorGuid; 615 - 616 - if (down_interruptible(&efivars_lock)) 617 - return -EINTR; 618 - 619 - if (!__efivars) { 620 - up(&efivars_lock); 621 - return -EINVAL; 622 - } 623 - ops = __efivars->ops; 624 - if (head && efivar_entry_find(name, vendor, head, false)) { 625 - up(&efivars_lock); 626 - return -EEXIST; 627 - } 628 - 629 - status = check_var_size(attributes, size + ucs2_strsize(name, 1024)); 630 - if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED) 631 - status = ops->set_variable(name, &vendor, 632 - attributes, size, data); 633 - 634 - up(&efivars_lock); 635 - 636 - return efi_status_to_err(status); 637 - 638 - } 639 - EXPORT_SYMBOL_GPL(efivar_entry_set); 640 - 641 - /* 642 - * efivar_entry_set_nonblocking - call set_variable_nonblocking() 643 - * 644 - * This function is guaranteed to not block and is suitable for calling 645 - * from crash/panic handlers. 646 - * 647 - * Crucially, this function will not block if it cannot acquire 648 - * efivars_lock. Instead, it returns -EBUSY. 649 - */ 650 - static int 651 - efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor, 652 - u32 attributes, unsigned long size, void *data) 653 - { 654 - const struct efivar_operations *ops; 655 - efi_status_t status; 656 - 657 - if (down_trylock(&efivars_lock)) 658 - return -EBUSY; 659 - 660 - if (!__efivars) { 661 - up(&efivars_lock); 662 - return -EINVAL; 663 - } 664 - 665 - status = check_var_size_nonblocking(attributes, 666 - size + ucs2_strsize(name, 1024)); 667 - if (status != EFI_SUCCESS) { 668 - up(&efivars_lock); 669 - return -ENOSPC; 670 - } 671 - 672 - ops = __efivars->ops; 673 - status = ops->set_variable_nonblocking(name, &vendor, attributes, 674 - size, data); 675 - 676 - up(&efivars_lock); 677 - return efi_status_to_err(status); 678 - } 679 - 680 - /** 681 - * efivar_entry_set_safe - call set_variable() if enough space in firmware 682 - * @name: buffer containing the variable name 683 - * @vendor: variable vendor guid 684 - * @attributes: variable attributes 685 - * @block: can we block in this context? 686 - * @size: size of @data buffer 687 - * @data: buffer containing variable data 688 - * 689 - * Ensures there is enough free storage in the firmware for this variable, and 690 - * if so, calls set_variable(). If creating a new EFI variable, this function 691 - * is usually followed by efivar_entry_add(). 692 - * 693 - * Returns 0 on success, -ENOSPC if the firmware does not have enough 694 - * space for set_variable() to succeed, or a converted EFI status code 695 - * if set_variable() fails. 696 - */ 697 - int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes, 698 - bool block, unsigned long size, void *data) 699 - { 700 - const struct efivar_operations *ops; 701 - efi_status_t status; 702 - unsigned long varsize; 703 - 704 - if (!__efivars) 705 - return -EINVAL; 706 - 707 - ops = __efivars->ops; 708 - if (!ops->query_variable_store) 709 - return -ENOSYS; 710 - 711 - /* 712 - * If the EFI variable backend provides a non-blocking 713 - * ->set_variable() operation and we're in a context where we 714 - * cannot block, then we need to use it to avoid live-locks, 715 - * since the implication is that the regular ->set_variable() 716 - * will block. 717 - * 718 - * If no ->set_variable_nonblocking() is provided then 719 - * ->set_variable() is assumed to be non-blocking. 720 - */ 721 - if (!block && ops->set_variable_nonblocking) 722 - return efivar_entry_set_nonblocking(name, vendor, attributes, 723 - size, data); 724 - 725 - varsize = size + ucs2_strsize(name, 1024); 726 - if (!block) { 727 - if (down_trylock(&efivars_lock)) 728 - return -EBUSY; 729 - status = check_var_size_nonblocking(attributes, varsize); 730 - } else { 731 - if (down_interruptible(&efivars_lock)) 732 - return -EINTR; 733 - status = check_var_size(attributes, varsize); 734 - } 735 - 736 - if (status != EFI_SUCCESS) { 737 - up(&efivars_lock); 738 - return -ENOSPC; 739 - } 740 - 741 - status = ops->set_variable(name, &vendor, attributes, size, data); 742 - 743 - up(&efivars_lock); 744 - 745 - return efi_status_to_err(status); 746 - } 747 - EXPORT_SYMBOL_GPL(efivar_entry_set_safe); 748 - 749 - /** 750 - * efivar_entry_find - search for an entry 751 - * @name: the EFI variable name 752 - * @guid: the EFI variable vendor's guid 753 - * @head: head of the variable list 754 - * @remove: should we remove the entry from the list? 755 - * 756 - * Search for an entry on the variable list that has the EFI variable 757 - * name @name and vendor guid @guid. If an entry is found on the list 758 - * and @remove is true, the entry is removed from the list. 759 - * 760 - * The caller MUST call efivar_entry_iter_begin() and 761 - * efivar_entry_iter_end() before and after the invocation of this 762 - * function, respectively. 763 - * 764 - * Returns the entry if found on the list, %NULL otherwise. 765 - */ 766 - struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid, 767 - struct list_head *head, bool remove) 768 - { 769 - struct efivar_entry *entry, *n; 770 - int strsize1, strsize2; 771 - bool found = false; 772 - 773 - list_for_each_entry_safe(entry, n, head, list) { 774 - strsize1 = ucs2_strsize(name, 1024); 775 - strsize2 = ucs2_strsize(entry->var.VariableName, 1024); 776 - if (strsize1 == strsize2 && 777 - !memcmp(name, &(entry->var.VariableName), strsize1) && 778 - !efi_guidcmp(guid, entry->var.VendorGuid)) { 779 - found = true; 780 - break; 781 - } 782 - } 783 - 784 - if (!found) 785 - return NULL; 786 - 787 - if (remove) 788 - list_del(&entry->list); 789 - 790 - return entry; 791 - } 792 - EXPORT_SYMBOL_GPL(efivar_entry_find); 793 616 794 617 /** 795 618 * efivar_entry_size - obtain the size of a variable ··· 774 1011 EXPORT_SYMBOL_GPL(efivar_entry_set_get_size); 775 1012 776 1013 /** 777 - * efivar_entry_iter_begin - begin iterating the variable list 778 - * 779 - * Lock the variable list to prevent entry insertion and removal until 780 - * efivar_entry_iter_end() is called. This function is usually used in 781 - * conjunction with __efivar_entry_iter() or efivar_entry_iter(). 782 - */ 783 - int efivar_entry_iter_begin(void) 784 - { 785 - return down_interruptible(&efivars_lock); 786 - } 787 - EXPORT_SYMBOL_GPL(efivar_entry_iter_begin); 788 - 789 - /** 790 - * efivar_entry_iter_end - finish iterating the variable list 791 - * 792 - * Unlock the variable list and allow modifications to the list again. 793 - */ 794 - void efivar_entry_iter_end(void) 795 - { 796 - up(&efivars_lock); 797 - } 798 - EXPORT_SYMBOL_GPL(efivar_entry_iter_end); 799 - 800 - /** 801 1014 * efivar_entry_iter - iterate over variable list 802 1015 * @func: callback function 803 1016 * @head: head of variable list ··· 793 1054 struct efivar_entry *entry, *n; 794 1055 int err = 0; 795 1056 796 - err = efivar_entry_iter_begin(); 1057 + err = down_interruptible(&efivars_lock); 797 1058 if (err) 798 1059 return err; 799 1060 ··· 802 1063 if (err) 803 1064 break; 804 1065 } 805 - efivar_entry_iter_end(); 1066 + up(&efivars_lock); 806 1067 807 1068 return err; 808 1069 }
-18
include/linux/efi.h
··· 1045 1045 struct kobject kobj; 1046 1046 }; 1047 1047 1048 - static inline void 1049 - efivar_unregister(struct efivar_entry *var) 1050 - { 1051 - kobject_put(&var->kobj); 1052 - } 1053 - 1054 1048 int efivars_register(struct efivars *efivars, 1055 1049 const struct efivar_operations *ops, 1056 1050 struct kobject *kobject); ··· 1058 1064 int efivar_entry_add(struct efivar_entry *entry, struct list_head *head); 1059 1065 void __efivar_entry_add(struct efivar_entry *entry, struct list_head *head); 1060 1066 void efivar_entry_remove(struct efivar_entry *entry); 1061 - 1062 - int __efivar_entry_delete(struct efivar_entry *entry); 1063 1067 int efivar_entry_delete(struct efivar_entry *entry); 1064 1068 1065 1069 int efivar_entry_size(struct efivar_entry *entry, unsigned long *size); ··· 1065 1073 unsigned long *size, void *data); 1066 1074 int efivar_entry_get(struct efivar_entry *entry, u32 *attributes, 1067 1075 unsigned long *size, void *data); 1068 - int efivar_entry_set(struct efivar_entry *entry, u32 attributes, 1069 - unsigned long size, void *data, struct list_head *head); 1070 1076 int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes, 1071 1077 unsigned long *size, void *data, bool *set); 1072 - int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes, 1073 - bool block, unsigned long size, void *data); 1074 - 1075 - int efivar_entry_iter_begin(void); 1076 - void efivar_entry_iter_end(void); 1077 1078 1078 1079 int efivar_entry_iter(int (*func)(struct efivar_entry *, void *), 1079 1080 struct list_head *head, void *data); 1080 - 1081 - struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid, 1082 - struct list_head *head, bool remove); 1083 1081 1084 1082 bool efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data, 1085 1083 unsigned long data_size);