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

Configure Feed

Select the types of activity you want to include in your feed.

at v3.9-rc8 2115 lines 54 kB view raw
1/* 2 * EFI Variables - efivars.c 3 * 4 * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com> 5 * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com> 6 * 7 * This code takes all variables accessible from EFI runtime and 8 * exports them via sysfs 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 * 24 * Changelog: 25 * 26 * 17 May 2004 - Matt Domsch <Matt_Domsch@dell.com> 27 * remove check for efi_enabled in exit 28 * add MODULE_VERSION 29 * 30 * 26 Apr 2004 - Matt Domsch <Matt_Domsch@dell.com> 31 * minor bug fixes 32 * 33 * 21 Apr 2004 - Matt Tolentino <matthew.e.tolentino@intel.com) 34 * converted driver to export variable information via sysfs 35 * and moved to drivers/firmware directory 36 * bumped revision number to v0.07 to reflect conversion & move 37 * 38 * 10 Dec 2002 - Matt Domsch <Matt_Domsch@dell.com> 39 * fix locking per Peter Chubb's findings 40 * 41 * 25 Mar 2002 - Matt Domsch <Matt_Domsch@dell.com> 42 * move uuid_unparse() to include/asm-ia64/efi.h:efi_guid_unparse() 43 * 44 * 12 Feb 2002 - Matt Domsch <Matt_Domsch@dell.com> 45 * use list_for_each_safe when deleting vars. 46 * remove ifdef CONFIG_SMP around include <linux/smp.h> 47 * v0.04 release to linux-ia64@linuxia64.org 48 * 49 * 20 April 2001 - Matt Domsch <Matt_Domsch@dell.com> 50 * Moved vars from /proc/efi to /proc/efi/vars, and made 51 * efi.c own the /proc/efi directory. 52 * v0.03 release to linux-ia64@linuxia64.org 53 * 54 * 26 March 2001 - Matt Domsch <Matt_Domsch@dell.com> 55 * At the request of Stephane, moved ownership of /proc/efi 56 * to efi.c, and now efivars lives under /proc/efi/vars. 57 * 58 * 12 March 2001 - Matt Domsch <Matt_Domsch@dell.com> 59 * Feedback received from Stephane Eranian incorporated. 60 * efivar_write() checks copy_from_user() return value. 61 * efivar_read/write() returns proper errno. 62 * v0.02 release to linux-ia64@linuxia64.org 63 * 64 * 26 February 2001 - Matt Domsch <Matt_Domsch@dell.com> 65 * v0.01 release to linux-ia64@linuxia64.org 66 */ 67 68#include <linux/capability.h> 69#include <linux/types.h> 70#include <linux/errno.h> 71#include <linux/init.h> 72#include <linux/mm.h> 73#include <linux/module.h> 74#include <linux/string.h> 75#include <linux/smp.h> 76#include <linux/efi.h> 77#include <linux/sysfs.h> 78#include <linux/kobject.h> 79#include <linux/device.h> 80#include <linux/slab.h> 81#include <linux/pstore.h> 82#include <linux/ctype.h> 83#include <linux/ucs2_string.h> 84 85#include <linux/fs.h> 86#include <linux/ramfs.h> 87#include <linux/pagemap.h> 88 89#include <asm/uaccess.h> 90 91#define EFIVARS_VERSION "0.08" 92#define EFIVARS_DATE "2004-May-17" 93 94MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>"); 95MODULE_DESCRIPTION("sysfs interface to EFI Variables"); 96MODULE_LICENSE("GPL"); 97MODULE_VERSION(EFIVARS_VERSION); 98 99#define DUMP_NAME_LEN 52 100 101/* 102 * Length of a GUID string (strlen("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee")) 103 * not including trailing NUL 104 */ 105#define GUID_LEN 36 106 107static bool efivars_pstore_disable = 108 IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE); 109 110module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644); 111 112/* 113 * The maximum size of VariableName + Data = 1024 114 * Therefore, it's reasonable to save that much 115 * space in each part of the structure, 116 * and we use a page for reading/writing. 117 */ 118 119struct efi_variable { 120 efi_char16_t VariableName[1024/sizeof(efi_char16_t)]; 121 efi_guid_t VendorGuid; 122 unsigned long DataSize; 123 __u8 Data[1024]; 124 efi_status_t Status; 125 __u32 Attributes; 126} __attribute__((packed)); 127 128struct efivar_entry { 129 struct efivars *efivars; 130 struct efi_variable var; 131 struct list_head list; 132 struct kobject kobj; 133}; 134 135struct efivar_attribute { 136 struct attribute attr; 137 ssize_t (*show) (struct efivar_entry *entry, char *buf); 138 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count); 139}; 140 141static struct efivars __efivars; 142static struct efivar_operations ops; 143 144#define PSTORE_EFI_ATTRIBUTES \ 145 (EFI_VARIABLE_NON_VOLATILE | \ 146 EFI_VARIABLE_BOOTSERVICE_ACCESS | \ 147 EFI_VARIABLE_RUNTIME_ACCESS) 148 149#define EFIVAR_ATTR(_name, _mode, _show, _store) \ 150struct efivar_attribute efivar_attr_##_name = { \ 151 .attr = {.name = __stringify(_name), .mode = _mode}, \ 152 .show = _show, \ 153 .store = _store, \ 154}; 155 156#define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr) 157#define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj) 158 159/* 160 * Prototype for sysfs creation function 161 */ 162static int 163efivar_create_sysfs_entry(struct efivars *efivars, 164 unsigned long variable_name_size, 165 efi_char16_t *variable_name, 166 efi_guid_t *vendor_guid); 167 168/* 169 * Prototype for workqueue functions updating sysfs entry 170 */ 171 172static void efivar_update_sysfs_entries(struct work_struct *); 173static DECLARE_WORK(efivar_work, efivar_update_sysfs_entries); 174static bool efivar_wq_enabled = true; 175 176static bool 177validate_device_path(struct efi_variable *var, int match, u8 *buffer, 178 unsigned long len) 179{ 180 struct efi_generic_dev_path *node; 181 int offset = 0; 182 183 node = (struct efi_generic_dev_path *)buffer; 184 185 if (len < sizeof(*node)) 186 return false; 187 188 while (offset <= len - sizeof(*node) && 189 node->length >= sizeof(*node) && 190 node->length <= len - offset) { 191 offset += node->length; 192 193 if ((node->type == EFI_DEV_END_PATH || 194 node->type == EFI_DEV_END_PATH2) && 195 node->sub_type == EFI_DEV_END_ENTIRE) 196 return true; 197 198 node = (struct efi_generic_dev_path *)(buffer + offset); 199 } 200 201 /* 202 * If we're here then either node->length pointed past the end 203 * of the buffer or we reached the end of the buffer without 204 * finding a device path end node. 205 */ 206 return false; 207} 208 209static bool 210validate_boot_order(struct efi_variable *var, int match, u8 *buffer, 211 unsigned long len) 212{ 213 /* An array of 16-bit integers */ 214 if ((len % 2) != 0) 215 return false; 216 217 return true; 218} 219 220static bool 221validate_load_option(struct efi_variable *var, int match, u8 *buffer, 222 unsigned long len) 223{ 224 u16 filepathlength; 225 int i, desclength = 0, namelen; 226 227 namelen = ucs2_strnlen(var->VariableName, sizeof(var->VariableName)); 228 229 /* Either "Boot" or "Driver" followed by four digits of hex */ 230 for (i = match; i < match+4; i++) { 231 if (var->VariableName[i] > 127 || 232 hex_to_bin(var->VariableName[i] & 0xff) < 0) 233 return true; 234 } 235 236 /* Reject it if there's 4 digits of hex and then further content */ 237 if (namelen > match + 4) 238 return false; 239 240 /* A valid entry must be at least 8 bytes */ 241 if (len < 8) 242 return false; 243 244 filepathlength = buffer[4] | buffer[5] << 8; 245 246 /* 247 * There's no stored length for the description, so it has to be 248 * found by hand 249 */ 250 desclength = ucs2_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2; 251 252 /* Each boot entry must have a descriptor */ 253 if (!desclength) 254 return false; 255 256 /* 257 * If the sum of the length of the description, the claimed filepath 258 * length and the original header are greater than the length of the 259 * variable, it's malformed 260 */ 261 if ((desclength + filepathlength + 6) > len) 262 return false; 263 264 /* 265 * And, finally, check the filepath 266 */ 267 return validate_device_path(var, match, buffer + desclength + 6, 268 filepathlength); 269} 270 271static bool 272validate_uint16(struct efi_variable *var, int match, u8 *buffer, 273 unsigned long len) 274{ 275 /* A single 16-bit integer */ 276 if (len != 2) 277 return false; 278 279 return true; 280} 281 282static bool 283validate_ascii_string(struct efi_variable *var, int match, u8 *buffer, 284 unsigned long len) 285{ 286 int i; 287 288 for (i = 0; i < len; i++) { 289 if (buffer[i] > 127) 290 return false; 291 292 if (buffer[i] == 0) 293 return true; 294 } 295 296 return false; 297} 298 299struct variable_validate { 300 char *name; 301 bool (*validate)(struct efi_variable *var, int match, u8 *data, 302 unsigned long len); 303}; 304 305static const struct variable_validate variable_validate[] = { 306 { "BootNext", validate_uint16 }, 307 { "BootOrder", validate_boot_order }, 308 { "DriverOrder", validate_boot_order }, 309 { "Boot*", validate_load_option }, 310 { "Driver*", validate_load_option }, 311 { "ConIn", validate_device_path }, 312 { "ConInDev", validate_device_path }, 313 { "ConOut", validate_device_path }, 314 { "ConOutDev", validate_device_path }, 315 { "ErrOut", validate_device_path }, 316 { "ErrOutDev", validate_device_path }, 317 { "Timeout", validate_uint16 }, 318 { "Lang", validate_ascii_string }, 319 { "PlatformLang", validate_ascii_string }, 320 { "", NULL }, 321}; 322 323static bool 324validate_var(struct efi_variable *var, u8 *data, unsigned long len) 325{ 326 int i; 327 u16 *unicode_name = var->VariableName; 328 329 for (i = 0; variable_validate[i].validate != NULL; i++) { 330 const char *name = variable_validate[i].name; 331 int match; 332 333 for (match = 0; ; match++) { 334 char c = name[match]; 335 u16 u = unicode_name[match]; 336 337 /* All special variables are plain ascii */ 338 if (u > 127) 339 return true; 340 341 /* Wildcard in the matching name means we've matched */ 342 if (c == '*') 343 return variable_validate[i].validate(var, 344 match, data, len); 345 346 /* Case sensitive match */ 347 if (c != u) 348 break; 349 350 /* Reached the end of the string while matching */ 351 if (!c) 352 return variable_validate[i].validate(var, 353 match, data, len); 354 } 355 } 356 357 return true; 358} 359 360static efi_status_t 361get_var_data_locked(struct efivars *efivars, struct efi_variable *var) 362{ 363 efi_status_t status; 364 365 var->DataSize = 1024; 366 status = efivars->ops->get_variable(var->VariableName, 367 &var->VendorGuid, 368 &var->Attributes, 369 &var->DataSize, 370 var->Data); 371 return status; 372} 373 374static efi_status_t 375get_var_data(struct efivars *efivars, struct efi_variable *var) 376{ 377 efi_status_t status; 378 unsigned long flags; 379 380 spin_lock_irqsave(&efivars->lock, flags); 381 status = get_var_data_locked(efivars, var); 382 spin_unlock_irqrestore(&efivars->lock, flags); 383 384 if (status != EFI_SUCCESS) { 385 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n", 386 status); 387 } 388 return status; 389} 390 391static efi_status_t 392check_var_size_locked(struct efivars *efivars, u32 attributes, 393 unsigned long size) 394{ 395 const struct efivar_operations *fops = efivars->ops; 396 397 if (!efivars->ops->query_variable_store) 398 return EFI_UNSUPPORTED; 399 400 return fops->query_variable_store(attributes, size); 401} 402 403 404static efi_status_t 405check_var_size(struct efivars *efivars, u32 attributes, unsigned long size) 406{ 407 efi_status_t status; 408 unsigned long flags; 409 410 spin_lock_irqsave(&efivars->lock, flags); 411 status = check_var_size_locked(efivars, attributes, size); 412 spin_unlock_irqrestore(&efivars->lock, flags); 413 414 return status; 415} 416 417static ssize_t 418efivar_guid_read(struct efivar_entry *entry, char *buf) 419{ 420 struct efi_variable *var = &entry->var; 421 char *str = buf; 422 423 if (!entry || !buf) 424 return 0; 425 426 efi_guid_unparse(&var->VendorGuid, str); 427 str += strlen(str); 428 str += sprintf(str, "\n"); 429 430 return str - buf; 431} 432 433static ssize_t 434efivar_attr_read(struct efivar_entry *entry, char *buf) 435{ 436 struct efi_variable *var = &entry->var; 437 char *str = buf; 438 efi_status_t status; 439 440 if (!entry || !buf) 441 return -EINVAL; 442 443 status = get_var_data(entry->efivars, var); 444 if (status != EFI_SUCCESS) 445 return -EIO; 446 447 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE) 448 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n"); 449 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS) 450 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n"); 451 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) 452 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n"); 453 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) 454 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n"); 455 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) 456 str += sprintf(str, 457 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n"); 458 if (var->Attributes & 459 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) 460 str += sprintf(str, 461 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n"); 462 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE) 463 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n"); 464 return str - buf; 465} 466 467static ssize_t 468efivar_size_read(struct efivar_entry *entry, char *buf) 469{ 470 struct efi_variable *var = &entry->var; 471 char *str = buf; 472 efi_status_t status; 473 474 if (!entry || !buf) 475 return -EINVAL; 476 477 status = get_var_data(entry->efivars, var); 478 if (status != EFI_SUCCESS) 479 return -EIO; 480 481 str += sprintf(str, "0x%lx\n", var->DataSize); 482 return str - buf; 483} 484 485static ssize_t 486efivar_data_read(struct efivar_entry *entry, char *buf) 487{ 488 struct efi_variable *var = &entry->var; 489 efi_status_t status; 490 491 if (!entry || !buf) 492 return -EINVAL; 493 494 status = get_var_data(entry->efivars, var); 495 if (status != EFI_SUCCESS) 496 return -EIO; 497 498 memcpy(buf, var->Data, var->DataSize); 499 return var->DataSize; 500} 501/* 502 * We allow each variable to be edited via rewriting the 503 * entire efi variable structure. 504 */ 505static ssize_t 506efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count) 507{ 508 struct efi_variable *new_var, *var = &entry->var; 509 struct efivars *efivars = entry->efivars; 510 efi_status_t status = EFI_NOT_FOUND; 511 512 if (count != sizeof(struct efi_variable)) 513 return -EINVAL; 514 515 new_var = (struct efi_variable *)buf; 516 /* 517 * If only updating the variable data, then the name 518 * and guid should remain the same 519 */ 520 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) || 521 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) { 522 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n"); 523 return -EINVAL; 524 } 525 526 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){ 527 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n"); 528 return -EINVAL; 529 } 530 531 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 || 532 validate_var(new_var, new_var->Data, new_var->DataSize) == false) { 533 printk(KERN_ERR "efivars: Malformed variable content\n"); 534 return -EINVAL; 535 } 536 537 spin_lock_irq(&efivars->lock); 538 539 status = check_var_size_locked(efivars, new_var->Attributes, 540 new_var->DataSize + ucs2_strsize(new_var->VariableName, 1024)); 541 542 if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED) 543 status = efivars->ops->set_variable(new_var->VariableName, 544 &new_var->VendorGuid, 545 new_var->Attributes, 546 new_var->DataSize, 547 new_var->Data); 548 549 spin_unlock_irq(&efivars->lock); 550 551 if (status != EFI_SUCCESS) { 552 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n", 553 status); 554 return -EIO; 555 } 556 557 memcpy(&entry->var, new_var, count); 558 return count; 559} 560 561static ssize_t 562efivar_show_raw(struct efivar_entry *entry, char *buf) 563{ 564 struct efi_variable *var = &entry->var; 565 efi_status_t status; 566 567 if (!entry || !buf) 568 return 0; 569 570 status = get_var_data(entry->efivars, var); 571 if (status != EFI_SUCCESS) 572 return -EIO; 573 574 memcpy(buf, var, sizeof(*var)); 575 return sizeof(*var); 576} 577 578/* 579 * Generic read/write functions that call the specific functions of 580 * the attributes... 581 */ 582static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr, 583 char *buf) 584{ 585 struct efivar_entry *var = to_efivar_entry(kobj); 586 struct efivar_attribute *efivar_attr = to_efivar_attr(attr); 587 ssize_t ret = -EIO; 588 589 if (!capable(CAP_SYS_ADMIN)) 590 return -EACCES; 591 592 if (efivar_attr->show) { 593 ret = efivar_attr->show(var, buf); 594 } 595 return ret; 596} 597 598static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr, 599 const char *buf, size_t count) 600{ 601 struct efivar_entry *var = to_efivar_entry(kobj); 602 struct efivar_attribute *efivar_attr = to_efivar_attr(attr); 603 ssize_t ret = -EIO; 604 605 if (!capable(CAP_SYS_ADMIN)) 606 return -EACCES; 607 608 if (efivar_attr->store) 609 ret = efivar_attr->store(var, buf, count); 610 611 return ret; 612} 613 614static const struct sysfs_ops efivar_attr_ops = { 615 .show = efivar_attr_show, 616 .store = efivar_attr_store, 617}; 618 619static void efivar_release(struct kobject *kobj) 620{ 621 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj); 622 kfree(var); 623} 624 625static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL); 626static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL); 627static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL); 628static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL); 629static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw); 630 631static struct attribute *def_attrs[] = { 632 &efivar_attr_guid.attr, 633 &efivar_attr_size.attr, 634 &efivar_attr_attributes.attr, 635 &efivar_attr_data.attr, 636 &efivar_attr_raw_var.attr, 637 NULL, 638}; 639 640static struct kobj_type efivar_ktype = { 641 .release = efivar_release, 642 .sysfs_ops = &efivar_attr_ops, 643 .default_attrs = def_attrs, 644}; 645 646static inline void 647efivar_unregister(struct efivar_entry *var) 648{ 649 kobject_put(&var->kobj); 650} 651 652static int efivarfs_file_open(struct inode *inode, struct file *file) 653{ 654 file->private_data = inode->i_private; 655 return 0; 656} 657 658static int efi_status_to_err(efi_status_t status) 659{ 660 int err; 661 662 switch (status) { 663 case EFI_INVALID_PARAMETER: 664 err = -EINVAL; 665 break; 666 case EFI_OUT_OF_RESOURCES: 667 err = -ENOSPC; 668 break; 669 case EFI_DEVICE_ERROR: 670 err = -EIO; 671 break; 672 case EFI_WRITE_PROTECTED: 673 err = -EROFS; 674 break; 675 case EFI_SECURITY_VIOLATION: 676 err = -EACCES; 677 break; 678 case EFI_NOT_FOUND: 679 err = -EIO; 680 break; 681 default: 682 err = -EINVAL; 683 } 684 685 return err; 686} 687 688static ssize_t efivarfs_file_write(struct file *file, 689 const char __user *userbuf, size_t count, loff_t *ppos) 690{ 691 struct efivar_entry *var = file->private_data; 692 struct efivars *efivars; 693 efi_status_t status; 694 void *data; 695 u32 attributes; 696 struct inode *inode = file->f_mapping->host; 697 unsigned long datasize = count - sizeof(attributes); 698 unsigned long newdatasize, varsize; 699 ssize_t bytes = 0; 700 701 if (count < sizeof(attributes)) 702 return -EINVAL; 703 704 if (copy_from_user(&attributes, userbuf, sizeof(attributes))) 705 return -EFAULT; 706 707 if (attributes & ~(EFI_VARIABLE_MASK)) 708 return -EINVAL; 709 710 efivars = var->efivars; 711 712 /* 713 * Ensure that the user can't allocate arbitrarily large 714 * amounts of memory. Pick a default size of 64K if 715 * QueryVariableInfo() isn't supported by the firmware. 716 */ 717 718 varsize = datasize + ucs2_strsize(var->var.VariableName, 1024); 719 status = check_var_size(efivars, attributes, varsize); 720 721 if (status != EFI_SUCCESS) { 722 if (status != EFI_UNSUPPORTED) 723 return efi_status_to_err(status); 724 725 if (datasize > 65536) 726 return -ENOSPC; 727 } 728 729 data = kmalloc(datasize, GFP_KERNEL); 730 if (!data) 731 return -ENOMEM; 732 733 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) { 734 bytes = -EFAULT; 735 goto out; 736 } 737 738 if (validate_var(&var->var, data, datasize) == false) { 739 bytes = -EINVAL; 740 goto out; 741 } 742 743 /* 744 * The lock here protects the get_variable call, the conditional 745 * set_variable call, and removal of the variable from the efivars 746 * list (in the case of an authenticated delete). 747 */ 748 spin_lock_irq(&efivars->lock); 749 750 /* 751 * Ensure that the available space hasn't shrunk below the safe level 752 */ 753 754 status = check_var_size_locked(efivars, attributes, varsize); 755 756 if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED) { 757 spin_unlock_irq(&efivars->lock); 758 kfree(data); 759 760 return efi_status_to_err(status); 761 } 762 763 status = efivars->ops->set_variable(var->var.VariableName, 764 &var->var.VendorGuid, 765 attributes, datasize, 766 data); 767 768 if (status != EFI_SUCCESS) { 769 spin_unlock_irq(&efivars->lock); 770 kfree(data); 771 772 return efi_status_to_err(status); 773 } 774 775 bytes = count; 776 777 /* 778 * Writing to the variable may have caused a change in size (which 779 * could either be an append or an overwrite), or the variable to be 780 * deleted. Perform a GetVariable() so we can tell what actually 781 * happened. 782 */ 783 newdatasize = 0; 784 status = efivars->ops->get_variable(var->var.VariableName, 785 &var->var.VendorGuid, 786 NULL, &newdatasize, 787 NULL); 788 789 if (status == EFI_BUFFER_TOO_SMALL) { 790 spin_unlock_irq(&efivars->lock); 791 mutex_lock(&inode->i_mutex); 792 i_size_write(inode, newdatasize + sizeof(attributes)); 793 mutex_unlock(&inode->i_mutex); 794 795 } else if (status == EFI_NOT_FOUND) { 796 list_del(&var->list); 797 spin_unlock_irq(&efivars->lock); 798 efivar_unregister(var); 799 drop_nlink(inode); 800 d_delete(file->f_dentry); 801 dput(file->f_dentry); 802 803 } else { 804 spin_unlock_irq(&efivars->lock); 805 pr_warn("efivarfs: inconsistent EFI variable implementation? " 806 "status = %lx\n", status); 807 } 808 809out: 810 kfree(data); 811 812 return bytes; 813} 814 815static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf, 816 size_t count, loff_t *ppos) 817{ 818 struct efivar_entry *var = file->private_data; 819 struct efivars *efivars = var->efivars; 820 efi_status_t status; 821 unsigned long datasize = 0; 822 u32 attributes; 823 void *data; 824 ssize_t size = 0; 825 826 spin_lock_irq(&efivars->lock); 827 status = efivars->ops->get_variable(var->var.VariableName, 828 &var->var.VendorGuid, 829 &attributes, &datasize, NULL); 830 spin_unlock_irq(&efivars->lock); 831 832 if (status != EFI_BUFFER_TOO_SMALL) 833 return efi_status_to_err(status); 834 835 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL); 836 837 if (!data) 838 return -ENOMEM; 839 840 spin_lock_irq(&efivars->lock); 841 status = efivars->ops->get_variable(var->var.VariableName, 842 &var->var.VendorGuid, 843 &attributes, &datasize, 844 (data + sizeof(attributes))); 845 spin_unlock_irq(&efivars->lock); 846 847 if (status != EFI_SUCCESS) { 848 size = efi_status_to_err(status); 849 goto out_free; 850 } 851 852 memcpy(data, &attributes, sizeof(attributes)); 853 size = simple_read_from_buffer(userbuf, count, ppos, 854 data, datasize + sizeof(attributes)); 855out_free: 856 kfree(data); 857 858 return size; 859} 860 861static void efivarfs_evict_inode(struct inode *inode) 862{ 863 clear_inode(inode); 864} 865 866static const struct super_operations efivarfs_ops = { 867 .statfs = simple_statfs, 868 .drop_inode = generic_delete_inode, 869 .evict_inode = efivarfs_evict_inode, 870 .show_options = generic_show_options, 871}; 872 873static struct super_block *efivarfs_sb; 874 875static const struct inode_operations efivarfs_dir_inode_operations; 876 877static const struct file_operations efivarfs_file_operations = { 878 .open = efivarfs_file_open, 879 .read = efivarfs_file_read, 880 .write = efivarfs_file_write, 881 .llseek = no_llseek, 882}; 883 884static struct inode *efivarfs_get_inode(struct super_block *sb, 885 const struct inode *dir, int mode, dev_t dev) 886{ 887 struct inode *inode = new_inode(sb); 888 889 if (inode) { 890 inode->i_ino = get_next_ino(); 891 inode->i_mode = mode; 892 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 893 switch (mode & S_IFMT) { 894 case S_IFREG: 895 inode->i_fop = &efivarfs_file_operations; 896 break; 897 case S_IFDIR: 898 inode->i_op = &efivarfs_dir_inode_operations; 899 inode->i_fop = &simple_dir_operations; 900 inc_nlink(inode); 901 break; 902 } 903 } 904 return inode; 905} 906 907/* 908 * Return true if 'str' is a valid efivarfs filename of the form, 909 * 910 * VariableName-12345678-1234-1234-1234-1234567891bc 911 */ 912static bool efivarfs_valid_name(const char *str, int len) 913{ 914 static const char dashes[GUID_LEN] = { 915 [8] = 1, [13] = 1, [18] = 1, [23] = 1 916 }; 917 const char *s = str + len - GUID_LEN; 918 int i; 919 920 /* 921 * We need a GUID, plus at least one letter for the variable name, 922 * plus the '-' separator 923 */ 924 if (len < GUID_LEN + 2) 925 return false; 926 927 /* GUID must be preceded by a '-' */ 928 if (*(s - 1) != '-') 929 return false; 930 931 /* 932 * Validate that 's' is of the correct format, e.g. 933 * 934 * 12345678-1234-1234-1234-123456789abc 935 */ 936 for (i = 0; i < GUID_LEN; i++) { 937 if (dashes[i]) { 938 if (*s++ != '-') 939 return false; 940 } else { 941 if (!isxdigit(*s++)) 942 return false; 943 } 944 } 945 946 return true; 947} 948 949static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid) 950{ 951 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]); 952 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]); 953 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]); 954 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]); 955 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]); 956 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]); 957 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]); 958 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]); 959 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]); 960 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]); 961 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]); 962 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]); 963 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]); 964 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]); 965 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]); 966 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]); 967} 968 969static int efivarfs_create(struct inode *dir, struct dentry *dentry, 970 umode_t mode, bool excl) 971{ 972 struct inode *inode; 973 struct efivars *efivars = &__efivars; 974 struct efivar_entry *var; 975 int namelen, i = 0, err = 0; 976 977 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len)) 978 return -EINVAL; 979 980 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0); 981 if (!inode) 982 return -ENOMEM; 983 984 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL); 985 if (!var) { 986 err = -ENOMEM; 987 goto out; 988 } 989 990 /* length of the variable name itself: remove GUID and separator */ 991 namelen = dentry->d_name.len - GUID_LEN - 1; 992 993 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1, 994 &var->var.VendorGuid); 995 996 for (i = 0; i < namelen; i++) 997 var->var.VariableName[i] = dentry->d_name.name[i]; 998 999 var->var.VariableName[i] = '\0'; 1000 1001 inode->i_private = var; 1002 var->efivars = efivars; 1003 var->kobj.kset = efivars->kset; 1004 1005 err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s", 1006 dentry->d_name.name); 1007 if (err) 1008 goto out; 1009 1010 kobject_uevent(&var->kobj, KOBJ_ADD); 1011 spin_lock_irq(&efivars->lock); 1012 list_add(&var->list, &efivars->list); 1013 spin_unlock_irq(&efivars->lock); 1014 d_instantiate(dentry, inode); 1015 dget(dentry); 1016out: 1017 if (err) { 1018 kfree(var); 1019 iput(inode); 1020 } 1021 return err; 1022} 1023 1024static int efivarfs_unlink(struct inode *dir, struct dentry *dentry) 1025{ 1026 struct efivar_entry *var = dentry->d_inode->i_private; 1027 struct efivars *efivars = var->efivars; 1028 efi_status_t status; 1029 1030 spin_lock_irq(&efivars->lock); 1031 1032 status = efivars->ops->set_variable(var->var.VariableName, 1033 &var->var.VendorGuid, 1034 0, 0, NULL); 1035 1036 if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) { 1037 list_del(&var->list); 1038 spin_unlock_irq(&efivars->lock); 1039 efivar_unregister(var); 1040 drop_nlink(dentry->d_inode); 1041 dput(dentry); 1042 return 0; 1043 } 1044 1045 spin_unlock_irq(&efivars->lock); 1046 return -EINVAL; 1047}; 1048 1049/* 1050 * Compare two efivarfs file names. 1051 * 1052 * An efivarfs filename is composed of two parts, 1053 * 1054 * 1. A case-sensitive variable name 1055 * 2. A case-insensitive GUID 1056 * 1057 * So we need to perform a case-sensitive match on part 1 and a 1058 * case-insensitive match on part 2. 1059 */ 1060static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode, 1061 const struct dentry *dentry, const struct inode *inode, 1062 unsigned int len, const char *str, 1063 const struct qstr *name) 1064{ 1065 int guid = len - GUID_LEN; 1066 1067 if (name->len != len) 1068 return 1; 1069 1070 /* Case-sensitive compare for the variable name */ 1071 if (memcmp(str, name->name, guid)) 1072 return 1; 1073 1074 /* Case-insensitive compare for the GUID */ 1075 return strncasecmp(name->name + guid, str + guid, GUID_LEN); 1076} 1077 1078static int efivarfs_d_hash(const struct dentry *dentry, 1079 const struct inode *inode, struct qstr *qstr) 1080{ 1081 unsigned long hash = init_name_hash(); 1082 const unsigned char *s = qstr->name; 1083 unsigned int len = qstr->len; 1084 1085 if (!efivarfs_valid_name(s, len)) 1086 return -EINVAL; 1087 1088 while (len-- > GUID_LEN) 1089 hash = partial_name_hash(*s++, hash); 1090 1091 /* GUID is case-insensitive. */ 1092 while (len--) 1093 hash = partial_name_hash(tolower(*s++), hash); 1094 1095 qstr->hash = end_name_hash(hash); 1096 return 0; 1097} 1098 1099/* 1100 * Retaining negative dentries for an in-memory filesystem just wastes 1101 * memory and lookup time: arrange for them to be deleted immediately. 1102 */ 1103static int efivarfs_delete_dentry(const struct dentry *dentry) 1104{ 1105 return 1; 1106} 1107 1108static struct dentry_operations efivarfs_d_ops = { 1109 .d_compare = efivarfs_d_compare, 1110 .d_hash = efivarfs_d_hash, 1111 .d_delete = efivarfs_delete_dentry, 1112}; 1113 1114static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name) 1115{ 1116 struct dentry *d; 1117 struct qstr q; 1118 int err; 1119 1120 q.name = name; 1121 q.len = strlen(name); 1122 1123 err = efivarfs_d_hash(NULL, NULL, &q); 1124 if (err) 1125 return ERR_PTR(err); 1126 1127 d = d_alloc(parent, &q); 1128 if (d) 1129 return d; 1130 1131 return ERR_PTR(-ENOMEM); 1132} 1133 1134static int efivarfs_fill_super(struct super_block *sb, void *data, int silent) 1135{ 1136 struct inode *inode = NULL; 1137 struct dentry *root; 1138 struct efivar_entry *entry, *n; 1139 struct efivars *efivars = &__efivars; 1140 char *name; 1141 int err = -ENOMEM; 1142 1143 efivarfs_sb = sb; 1144 1145 sb->s_maxbytes = MAX_LFS_FILESIZE; 1146 sb->s_blocksize = PAGE_CACHE_SIZE; 1147 sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 1148 sb->s_magic = EFIVARFS_MAGIC; 1149 sb->s_op = &efivarfs_ops; 1150 sb->s_d_op = &efivarfs_d_ops; 1151 sb->s_time_gran = 1; 1152 1153 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0); 1154 if (!inode) 1155 return -ENOMEM; 1156 inode->i_op = &efivarfs_dir_inode_operations; 1157 1158 root = d_make_root(inode); 1159 sb->s_root = root; 1160 if (!root) 1161 return -ENOMEM; 1162 1163 list_for_each_entry_safe(entry, n, &efivars->list, list) { 1164 struct dentry *dentry, *root = efivarfs_sb->s_root; 1165 unsigned long size = 0; 1166 int len, i; 1167 1168 inode = NULL; 1169 1170 len = ucs2_strlen(entry->var.VariableName); 1171 1172 /* name, plus '-', plus GUID, plus NUL*/ 1173 name = kmalloc(len + 1 + GUID_LEN + 1, GFP_ATOMIC); 1174 if (!name) 1175 goto fail; 1176 1177 for (i = 0; i < len; i++) 1178 name[i] = entry->var.VariableName[i] & 0xFF; 1179 1180 name[len] = '-'; 1181 1182 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1); 1183 1184 name[len+GUID_LEN+1] = '\0'; 1185 1186 inode = efivarfs_get_inode(efivarfs_sb, root->d_inode, 1187 S_IFREG | 0644, 0); 1188 if (!inode) 1189 goto fail_name; 1190 1191 dentry = efivarfs_alloc_dentry(root, name); 1192 if (IS_ERR(dentry)) { 1193 err = PTR_ERR(dentry); 1194 goto fail_inode; 1195 } 1196 1197 /* copied by the above to local storage in the dentry. */ 1198 kfree(name); 1199 1200 spin_lock_irq(&efivars->lock); 1201 efivars->ops->get_variable(entry->var.VariableName, 1202 &entry->var.VendorGuid, 1203 &entry->var.Attributes, 1204 &size, 1205 NULL); 1206 spin_unlock_irq(&efivars->lock); 1207 1208 mutex_lock(&inode->i_mutex); 1209 inode->i_private = entry; 1210 i_size_write(inode, size + sizeof(entry->var.Attributes)); 1211 mutex_unlock(&inode->i_mutex); 1212 d_add(dentry, inode); 1213 } 1214 1215 return 0; 1216 1217fail_inode: 1218 iput(inode); 1219fail_name: 1220 kfree(name); 1221fail: 1222 return err; 1223} 1224 1225static struct dentry *efivarfs_mount(struct file_system_type *fs_type, 1226 int flags, const char *dev_name, void *data) 1227{ 1228 return mount_single(fs_type, flags, data, efivarfs_fill_super); 1229} 1230 1231static void efivarfs_kill_sb(struct super_block *sb) 1232{ 1233 kill_litter_super(sb); 1234 efivarfs_sb = NULL; 1235} 1236 1237static struct file_system_type efivarfs_type = { 1238 .name = "efivarfs", 1239 .mount = efivarfs_mount, 1240 .kill_sb = efivarfs_kill_sb, 1241}; 1242MODULE_ALIAS_FS("efivarfs"); 1243 1244/* 1245 * Handle negative dentry. 1246 */ 1247static struct dentry *efivarfs_lookup(struct inode *dir, struct dentry *dentry, 1248 unsigned int flags) 1249{ 1250 if (dentry->d_name.len > NAME_MAX) 1251 return ERR_PTR(-ENAMETOOLONG); 1252 d_add(dentry, NULL); 1253 return NULL; 1254} 1255 1256static const struct inode_operations efivarfs_dir_inode_operations = { 1257 .lookup = efivarfs_lookup, 1258 .unlink = efivarfs_unlink, 1259 .create = efivarfs_create, 1260}; 1261 1262#ifdef CONFIG_EFI_VARS_PSTORE 1263 1264static int efi_pstore_open(struct pstore_info *psi) 1265{ 1266 struct efivars *efivars = psi->data; 1267 1268 spin_lock_irq(&efivars->lock); 1269 efivars->walk_entry = list_first_entry(&efivars->list, 1270 struct efivar_entry, list); 1271 return 0; 1272} 1273 1274static int efi_pstore_close(struct pstore_info *psi) 1275{ 1276 struct efivars *efivars = psi->data; 1277 1278 spin_unlock_irq(&efivars->lock); 1279 return 0; 1280} 1281 1282static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type, 1283 int *count, struct timespec *timespec, 1284 char **buf, struct pstore_info *psi) 1285{ 1286 efi_guid_t vendor = LINUX_EFI_CRASH_GUID; 1287 struct efivars *efivars = psi->data; 1288 char name[DUMP_NAME_LEN]; 1289 int i; 1290 int cnt; 1291 unsigned int part, size; 1292 unsigned long time; 1293 1294 while (&efivars->walk_entry->list != &efivars->list) { 1295 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid, 1296 vendor)) { 1297 for (i = 0; i < DUMP_NAME_LEN; i++) { 1298 name[i] = efivars->walk_entry->var.VariableName[i]; 1299 } 1300 if (sscanf(name, "dump-type%u-%u-%d-%lu", 1301 type, &part, &cnt, &time) == 4) { 1302 *id = part; 1303 *count = cnt; 1304 timespec->tv_sec = time; 1305 timespec->tv_nsec = 0; 1306 } else if (sscanf(name, "dump-type%u-%u-%lu", 1307 type, &part, &time) == 3) { 1308 /* 1309 * Check if an old format, 1310 * which doesn't support holding 1311 * multiple logs, remains. 1312 */ 1313 *id = part; 1314 *count = 0; 1315 timespec->tv_sec = time; 1316 timespec->tv_nsec = 0; 1317 } else { 1318 efivars->walk_entry = list_entry( 1319 efivars->walk_entry->list.next, 1320 struct efivar_entry, list); 1321 continue; 1322 } 1323 1324 get_var_data_locked(efivars, &efivars->walk_entry->var); 1325 size = efivars->walk_entry->var.DataSize; 1326 *buf = kmalloc(size, GFP_KERNEL); 1327 if (*buf == NULL) 1328 return -ENOMEM; 1329 memcpy(*buf, efivars->walk_entry->var.Data, 1330 size); 1331 efivars->walk_entry = list_entry( 1332 efivars->walk_entry->list.next, 1333 struct efivar_entry, list); 1334 return size; 1335 } 1336 efivars->walk_entry = list_entry(efivars->walk_entry->list.next, 1337 struct efivar_entry, list); 1338 } 1339 return 0; 1340} 1341 1342static int efi_pstore_write(enum pstore_type_id type, 1343 enum kmsg_dump_reason reason, u64 *id, 1344 unsigned int part, int count, size_t size, 1345 struct pstore_info *psi) 1346{ 1347 char name[DUMP_NAME_LEN]; 1348 efi_char16_t efi_name[DUMP_NAME_LEN]; 1349 efi_guid_t vendor = LINUX_EFI_CRASH_GUID; 1350 struct efivars *efivars = psi->data; 1351 int i, ret = 0; 1352 efi_status_t status = EFI_NOT_FOUND; 1353 unsigned long flags; 1354 1355 if (pstore_cannot_block_path(reason)) { 1356 /* 1357 * If the lock is taken by another cpu in non-blocking path, 1358 * this driver returns without entering firmware to avoid 1359 * hanging up. 1360 */ 1361 if (!spin_trylock_irqsave(&efivars->lock, flags)) 1362 return -EBUSY; 1363 } else 1364 spin_lock_irqsave(&efivars->lock, flags); 1365 1366 /* 1367 * Check if there is a space enough to log. 1368 * size: a size of logging data 1369 * DUMP_NAME_LEN * 2: a maximum size of variable name 1370 */ 1371 1372 status = check_var_size_locked(efivars, PSTORE_EFI_ATTRIBUTES, 1373 size + DUMP_NAME_LEN * 2); 1374 1375 if (status) { 1376 spin_unlock_irqrestore(&efivars->lock, flags); 1377 *id = part; 1378 return -ENOSPC; 1379 } 1380 1381 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count, 1382 get_seconds()); 1383 1384 for (i = 0; i < DUMP_NAME_LEN; i++) 1385 efi_name[i] = name[i]; 1386 1387 efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES, 1388 size, psi->buf); 1389 1390 spin_unlock_irqrestore(&efivars->lock, flags); 1391 1392 if (reason == KMSG_DUMP_OOPS && efivar_wq_enabled) 1393 schedule_work(&efivar_work); 1394 1395 *id = part; 1396 return ret; 1397}; 1398 1399static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count, 1400 struct timespec time, struct pstore_info *psi) 1401{ 1402 char name[DUMP_NAME_LEN]; 1403 efi_char16_t efi_name[DUMP_NAME_LEN]; 1404 char name_old[DUMP_NAME_LEN]; 1405 efi_char16_t efi_name_old[DUMP_NAME_LEN]; 1406 efi_guid_t vendor = LINUX_EFI_CRASH_GUID; 1407 struct efivars *efivars = psi->data; 1408 struct efivar_entry *entry, *found = NULL; 1409 int i; 1410 1411 sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count, 1412 time.tv_sec); 1413 1414 spin_lock_irq(&efivars->lock); 1415 1416 for (i = 0; i < DUMP_NAME_LEN; i++) 1417 efi_name[i] = name[i]; 1418 1419 /* 1420 * Clean up an entry with the same name 1421 */ 1422 1423 list_for_each_entry(entry, &efivars->list, list) { 1424 get_var_data_locked(efivars, &entry->var); 1425 1426 if (efi_guidcmp(entry->var.VendorGuid, vendor)) 1427 continue; 1428 if (ucs2_strncmp(entry->var.VariableName, efi_name, 1429 ucs2_strlen(efi_name))) { 1430 /* 1431 * Check if an old format, 1432 * which doesn't support holding 1433 * multiple logs, remains. 1434 */ 1435 sprintf(name_old, "dump-type%u-%u-%lu", type, 1436 (unsigned int)id, time.tv_sec); 1437 1438 for (i = 0; i < DUMP_NAME_LEN; i++) 1439 efi_name_old[i] = name_old[i]; 1440 1441 if (ucs2_strncmp(entry->var.VariableName, efi_name_old, 1442 ucs2_strlen(efi_name_old))) 1443 continue; 1444 } 1445 1446 /* found */ 1447 found = entry; 1448 efivars->ops->set_variable(entry->var.VariableName, 1449 &entry->var.VendorGuid, 1450 PSTORE_EFI_ATTRIBUTES, 1451 0, NULL); 1452 break; 1453 } 1454 1455 if (found) 1456 list_del(&found->list); 1457 1458 spin_unlock_irq(&efivars->lock); 1459 1460 if (found) 1461 efivar_unregister(found); 1462 1463 return 0; 1464} 1465 1466static struct pstore_info efi_pstore_info = { 1467 .owner = THIS_MODULE, 1468 .name = "efi", 1469 .open = efi_pstore_open, 1470 .close = efi_pstore_close, 1471 .read = efi_pstore_read, 1472 .write = efi_pstore_write, 1473 .erase = efi_pstore_erase, 1474}; 1475 1476static void efivar_pstore_register(struct efivars *efivars) 1477{ 1478 efivars->efi_pstore_info = efi_pstore_info; 1479 efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL); 1480 if (efivars->efi_pstore_info.buf) { 1481 efivars->efi_pstore_info.bufsize = 1024; 1482 efivars->efi_pstore_info.data = efivars; 1483 spin_lock_init(&efivars->efi_pstore_info.buf_lock); 1484 pstore_register(&efivars->efi_pstore_info); 1485 } 1486} 1487#else 1488static void efivar_pstore_register(struct efivars *efivars) 1489{ 1490 return; 1491} 1492#endif 1493 1494static ssize_t efivar_create(struct file *filp, struct kobject *kobj, 1495 struct bin_attribute *bin_attr, 1496 char *buf, loff_t pos, size_t count) 1497{ 1498 struct efi_variable *new_var = (struct efi_variable *)buf; 1499 struct efivars *efivars = bin_attr->private; 1500 struct efivar_entry *search_efivar, *n; 1501 unsigned long strsize1, strsize2; 1502 efi_status_t status = EFI_NOT_FOUND; 1503 int found = 0; 1504 1505 if (!capable(CAP_SYS_ADMIN)) 1506 return -EACCES; 1507 1508 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 || 1509 validate_var(new_var, new_var->Data, new_var->DataSize) == false) { 1510 printk(KERN_ERR "efivars: Malformed variable content\n"); 1511 return -EINVAL; 1512 } 1513 1514 spin_lock_irq(&efivars->lock); 1515 1516 /* 1517 * Does this variable already exist? 1518 */ 1519 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) { 1520 strsize1 = ucs2_strsize(search_efivar->var.VariableName, 1024); 1521 strsize2 = ucs2_strsize(new_var->VariableName, 1024); 1522 if (strsize1 == strsize2 && 1523 !memcmp(&(search_efivar->var.VariableName), 1524 new_var->VariableName, strsize1) && 1525 !efi_guidcmp(search_efivar->var.VendorGuid, 1526 new_var->VendorGuid)) { 1527 found = 1; 1528 break; 1529 } 1530 } 1531 if (found) { 1532 spin_unlock_irq(&efivars->lock); 1533 return -EINVAL; 1534 } 1535 1536 status = check_var_size_locked(efivars, new_var->Attributes, 1537 new_var->DataSize + ucs2_strsize(new_var->VariableName, 1024)); 1538 1539 if (status && status != EFI_UNSUPPORTED) { 1540 spin_unlock_irq(&efivars->lock); 1541 return efi_status_to_err(status); 1542 } 1543 1544 /* now *really* create the variable via EFI */ 1545 status = efivars->ops->set_variable(new_var->VariableName, 1546 &new_var->VendorGuid, 1547 new_var->Attributes, 1548 new_var->DataSize, 1549 new_var->Data); 1550 1551 if (status != EFI_SUCCESS) { 1552 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n", 1553 status); 1554 spin_unlock_irq(&efivars->lock); 1555 return -EIO; 1556 } 1557 spin_unlock_irq(&efivars->lock); 1558 1559 /* Create the entry in sysfs. Locking is not required here */ 1560 status = efivar_create_sysfs_entry(efivars, 1561 ucs2_strsize(new_var->VariableName, 1562 1024), 1563 new_var->VariableName, 1564 &new_var->VendorGuid); 1565 if (status) { 1566 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n"); 1567 } 1568 return count; 1569} 1570 1571static ssize_t efivar_delete(struct file *filp, struct kobject *kobj, 1572 struct bin_attribute *bin_attr, 1573 char *buf, loff_t pos, size_t count) 1574{ 1575 struct efi_variable *del_var = (struct efi_variable *)buf; 1576 struct efivars *efivars = bin_attr->private; 1577 struct efivar_entry *search_efivar, *n; 1578 unsigned long strsize1, strsize2; 1579 efi_status_t status = EFI_NOT_FOUND; 1580 int found = 0; 1581 1582 if (!capable(CAP_SYS_ADMIN)) 1583 return -EACCES; 1584 1585 spin_lock_irq(&efivars->lock); 1586 1587 /* 1588 * Does this variable already exist? 1589 */ 1590 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) { 1591 strsize1 = ucs2_strsize(search_efivar->var.VariableName, 1024); 1592 strsize2 = ucs2_strsize(del_var->VariableName, 1024); 1593 if (strsize1 == strsize2 && 1594 !memcmp(&(search_efivar->var.VariableName), 1595 del_var->VariableName, strsize1) && 1596 !efi_guidcmp(search_efivar->var.VendorGuid, 1597 del_var->VendorGuid)) { 1598 found = 1; 1599 break; 1600 } 1601 } 1602 if (!found) { 1603 spin_unlock_irq(&efivars->lock); 1604 return -EINVAL; 1605 } 1606 /* force the Attributes/DataSize to 0 to ensure deletion */ 1607 del_var->Attributes = 0; 1608 del_var->DataSize = 0; 1609 1610 status = efivars->ops->set_variable(del_var->VariableName, 1611 &del_var->VendorGuid, 1612 del_var->Attributes, 1613 del_var->DataSize, 1614 del_var->Data); 1615 1616 if (status != EFI_SUCCESS) { 1617 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n", 1618 status); 1619 spin_unlock_irq(&efivars->lock); 1620 return -EIO; 1621 } 1622 list_del(&search_efivar->list); 1623 /* We need to release this lock before unregistering. */ 1624 spin_unlock_irq(&efivars->lock); 1625 efivar_unregister(search_efivar); 1626 1627 /* It's dead Jim.... */ 1628 return count; 1629} 1630 1631static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor) 1632{ 1633 struct efivar_entry *entry, *n; 1634 struct efivars *efivars = &__efivars; 1635 unsigned long strsize1, strsize2; 1636 bool found = false; 1637 1638 strsize1 = ucs2_strsize(variable_name, 1024); 1639 list_for_each_entry_safe(entry, n, &efivars->list, list) { 1640 strsize2 = ucs2_strsize(entry->var.VariableName, 1024); 1641 if (strsize1 == strsize2 && 1642 !memcmp(variable_name, &(entry->var.VariableName), 1643 strsize2) && 1644 !efi_guidcmp(entry->var.VendorGuid, 1645 *vendor)) { 1646 found = true; 1647 break; 1648 } 1649 } 1650 return found; 1651} 1652 1653/* 1654 * Returns the size of variable_name, in bytes, including the 1655 * terminating NULL character, or variable_name_size if no NULL 1656 * character is found among the first variable_name_size bytes. 1657 */ 1658static unsigned long var_name_strnsize(efi_char16_t *variable_name, 1659 unsigned long variable_name_size) 1660{ 1661 unsigned long len; 1662 efi_char16_t c; 1663 1664 /* 1665 * The variable name is, by definition, a NULL-terminated 1666 * string, so make absolutely sure that variable_name_size is 1667 * the value we expect it to be. If not, return the real size. 1668 */ 1669 for (len = 2; len <= variable_name_size; len += sizeof(c)) { 1670 c = variable_name[(len / sizeof(c)) - 1]; 1671 if (!c) 1672 break; 1673 } 1674 1675 return min(len, variable_name_size); 1676} 1677 1678static void efivar_update_sysfs_entries(struct work_struct *work) 1679{ 1680 struct efivars *efivars = &__efivars; 1681 efi_guid_t vendor; 1682 efi_char16_t *variable_name; 1683 unsigned long variable_name_size = 1024; 1684 efi_status_t status = EFI_NOT_FOUND; 1685 bool found; 1686 1687 /* Add new sysfs entries */ 1688 while (1) { 1689 variable_name = kzalloc(variable_name_size, GFP_KERNEL); 1690 if (!variable_name) { 1691 pr_err("efivars: Memory allocation failed.\n"); 1692 return; 1693 } 1694 1695 spin_lock_irq(&efivars->lock); 1696 found = false; 1697 while (1) { 1698 variable_name_size = 1024; 1699 status = efivars->ops->get_next_variable( 1700 &variable_name_size, 1701 variable_name, 1702 &vendor); 1703 if (status != EFI_SUCCESS) { 1704 break; 1705 } else { 1706 if (!variable_is_present(variable_name, 1707 &vendor)) { 1708 found = true; 1709 break; 1710 } 1711 } 1712 } 1713 spin_unlock_irq(&efivars->lock); 1714 1715 if (!found) { 1716 kfree(variable_name); 1717 break; 1718 } else { 1719 variable_name_size = var_name_strnsize(variable_name, 1720 variable_name_size); 1721 efivar_create_sysfs_entry(efivars, 1722 variable_name_size, 1723 variable_name, &vendor); 1724 } 1725 } 1726} 1727 1728/* 1729 * Let's not leave out systab information that snuck into 1730 * the efivars driver 1731 */ 1732static ssize_t systab_show(struct kobject *kobj, 1733 struct kobj_attribute *attr, char *buf) 1734{ 1735 char *str = buf; 1736 1737 if (!kobj || !buf) 1738 return -EINVAL; 1739 1740 if (efi.mps != EFI_INVALID_TABLE_ADDR) 1741 str += sprintf(str, "MPS=0x%lx\n", efi.mps); 1742 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR) 1743 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20); 1744 if (efi.acpi != EFI_INVALID_TABLE_ADDR) 1745 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi); 1746 if (efi.smbios != EFI_INVALID_TABLE_ADDR) 1747 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios); 1748 if (efi.hcdp != EFI_INVALID_TABLE_ADDR) 1749 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp); 1750 if (efi.boot_info != EFI_INVALID_TABLE_ADDR) 1751 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info); 1752 if (efi.uga != EFI_INVALID_TABLE_ADDR) 1753 str += sprintf(str, "UGA=0x%lx\n", efi.uga); 1754 1755 return str - buf; 1756} 1757 1758static struct kobj_attribute efi_attr_systab = 1759 __ATTR(systab, 0400, systab_show, NULL); 1760 1761static struct attribute *efi_subsys_attrs[] = { 1762 &efi_attr_systab.attr, 1763 NULL, /* maybe more in the future? */ 1764}; 1765 1766static struct attribute_group efi_subsys_attr_group = { 1767 .attrs = efi_subsys_attrs, 1768}; 1769 1770static struct kobject *efi_kobj; 1771 1772/* 1773 * efivar_create_sysfs_entry() 1774 * Requires: 1775 * variable_name_size = number of bytes required to hold 1776 * variable_name (not counting the NULL 1777 * character at the end. 1778 * efivars->lock is not held on entry or exit. 1779 * Returns 1 on failure, 0 on success 1780 */ 1781static int 1782efivar_create_sysfs_entry(struct efivars *efivars, 1783 unsigned long variable_name_size, 1784 efi_char16_t *variable_name, 1785 efi_guid_t *vendor_guid) 1786{ 1787 int i, short_name_size; 1788 char *short_name; 1789 struct efivar_entry *new_efivar; 1790 1791 /* 1792 * Length of the variable bytes in ASCII, plus the '-' separator, 1793 * plus the GUID, plus trailing NUL 1794 */ 1795 short_name_size = variable_name_size / sizeof(efi_char16_t) 1796 + 1 + GUID_LEN + 1; 1797 1798 short_name = kzalloc(short_name_size, GFP_KERNEL); 1799 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL); 1800 1801 if (!short_name || !new_efivar) { 1802 kfree(short_name); 1803 kfree(new_efivar); 1804 return 1; 1805 } 1806 1807 new_efivar->efivars = efivars; 1808 memcpy(new_efivar->var.VariableName, variable_name, 1809 variable_name_size); 1810 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t)); 1811 1812 /* Convert Unicode to normal chars (assume top bits are 0), 1813 ala UTF-8 */ 1814 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) { 1815 short_name[i] = variable_name[i] & 0xFF; 1816 } 1817 /* This is ugly, but necessary to separate one vendor's 1818 private variables from another's. */ 1819 1820 *(short_name + strlen(short_name)) = '-'; 1821 efi_guid_unparse(vendor_guid, short_name + strlen(short_name)); 1822 1823 new_efivar->kobj.kset = efivars->kset; 1824 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL, 1825 "%s", short_name); 1826 if (i) { 1827 kfree(short_name); 1828 kfree(new_efivar); 1829 return 1; 1830 } 1831 1832 kobject_uevent(&new_efivar->kobj, KOBJ_ADD); 1833 kfree(short_name); 1834 short_name = NULL; 1835 1836 spin_lock_irq(&efivars->lock); 1837 list_add(&new_efivar->list, &efivars->list); 1838 spin_unlock_irq(&efivars->lock); 1839 1840 return 0; 1841} 1842 1843static int 1844create_efivars_bin_attributes(struct efivars *efivars) 1845{ 1846 struct bin_attribute *attr; 1847 int error; 1848 1849 /* new_var */ 1850 attr = kzalloc(sizeof(*attr), GFP_KERNEL); 1851 if (!attr) 1852 return -ENOMEM; 1853 1854 attr->attr.name = "new_var"; 1855 attr->attr.mode = 0200; 1856 attr->write = efivar_create; 1857 attr->private = efivars; 1858 efivars->new_var = attr; 1859 1860 /* del_var */ 1861 attr = kzalloc(sizeof(*attr), GFP_KERNEL); 1862 if (!attr) { 1863 error = -ENOMEM; 1864 goto out_free; 1865 } 1866 attr->attr.name = "del_var"; 1867 attr->attr.mode = 0200; 1868 attr->write = efivar_delete; 1869 attr->private = efivars; 1870 efivars->del_var = attr; 1871 1872 sysfs_bin_attr_init(efivars->new_var); 1873 sysfs_bin_attr_init(efivars->del_var); 1874 1875 /* Register */ 1876 error = sysfs_create_bin_file(&efivars->kset->kobj, 1877 efivars->new_var); 1878 if (error) { 1879 printk(KERN_ERR "efivars: unable to create new_var sysfs file" 1880 " due to error %d\n", error); 1881 goto out_free; 1882 } 1883 error = sysfs_create_bin_file(&efivars->kset->kobj, 1884 efivars->del_var); 1885 if (error) { 1886 printk(KERN_ERR "efivars: unable to create del_var sysfs file" 1887 " due to error %d\n", error); 1888 sysfs_remove_bin_file(&efivars->kset->kobj, 1889 efivars->new_var); 1890 goto out_free; 1891 } 1892 1893 return 0; 1894out_free: 1895 kfree(efivars->del_var); 1896 efivars->del_var = NULL; 1897 kfree(efivars->new_var); 1898 efivars->new_var = NULL; 1899 return error; 1900} 1901 1902void unregister_efivars(struct efivars *efivars) 1903{ 1904 struct efivar_entry *entry, *n; 1905 1906 list_for_each_entry_safe(entry, n, &efivars->list, list) { 1907 spin_lock_irq(&efivars->lock); 1908 list_del(&entry->list); 1909 spin_unlock_irq(&efivars->lock); 1910 efivar_unregister(entry); 1911 } 1912 if (efivars->new_var) 1913 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var); 1914 if (efivars->del_var) 1915 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var); 1916 kfree(efivars->new_var); 1917 kfree(efivars->del_var); 1918 kobject_put(efivars->kobject); 1919 kset_unregister(efivars->kset); 1920} 1921EXPORT_SYMBOL_GPL(unregister_efivars); 1922 1923/* 1924 * Print a warning when duplicate EFI variables are encountered and 1925 * disable the sysfs workqueue since the firmware is buggy. 1926 */ 1927static void dup_variable_bug(efi_char16_t *s16, efi_guid_t *vendor_guid, 1928 unsigned long len16) 1929{ 1930 size_t i, len8 = len16 / sizeof(efi_char16_t); 1931 char *s8; 1932 1933 /* 1934 * Disable the workqueue since the algorithm it uses for 1935 * detecting new variables won't work with this buggy 1936 * implementation of GetNextVariableName(). 1937 */ 1938 efivar_wq_enabled = false; 1939 1940 s8 = kzalloc(len8, GFP_KERNEL); 1941 if (!s8) 1942 return; 1943 1944 for (i = 0; i < len8; i++) 1945 s8[i] = s16[i]; 1946 1947 printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n", 1948 s8, vendor_guid); 1949 kfree(s8); 1950} 1951 1952int register_efivars(struct efivars *efivars, 1953 const struct efivar_operations *ops, 1954 struct kobject *parent_kobj) 1955{ 1956 efi_status_t status = EFI_NOT_FOUND; 1957 efi_guid_t vendor_guid; 1958 efi_char16_t *variable_name; 1959 unsigned long variable_name_size = 1024; 1960 int error = 0; 1961 1962 variable_name = kzalloc(variable_name_size, GFP_KERNEL); 1963 if (!variable_name) { 1964 printk(KERN_ERR "efivars: Memory allocation failed.\n"); 1965 return -ENOMEM; 1966 } 1967 1968 spin_lock_init(&efivars->lock); 1969 INIT_LIST_HEAD(&efivars->list); 1970 efivars->ops = ops; 1971 1972 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj); 1973 if (!efivars->kset) { 1974 printk(KERN_ERR "efivars: Subsystem registration failed.\n"); 1975 error = -ENOMEM; 1976 goto out; 1977 } 1978 1979 efivars->kobject = kobject_create_and_add("efivars", parent_kobj); 1980 if (!efivars->kobject) { 1981 pr_err("efivars: Subsystem registration failed.\n"); 1982 error = -ENOMEM; 1983 kset_unregister(efivars->kset); 1984 goto out; 1985 } 1986 1987 /* 1988 * Per EFI spec, the maximum storage allocated for both 1989 * the variable name and variable data is 1024 bytes. 1990 */ 1991 1992 do { 1993 variable_name_size = 1024; 1994 1995 status = ops->get_next_variable(&variable_name_size, 1996 variable_name, 1997 &vendor_guid); 1998 switch (status) { 1999 case EFI_SUCCESS: 2000 variable_name_size = var_name_strnsize(variable_name, 2001 variable_name_size); 2002 2003 /* 2004 * Some firmware implementations return the 2005 * same variable name on multiple calls to 2006 * get_next_variable(). Terminate the loop 2007 * immediately as there is no guarantee that 2008 * we'll ever see a different variable name, 2009 * and may end up looping here forever. 2010 */ 2011 if (variable_is_present(variable_name, &vendor_guid)) { 2012 dup_variable_bug(variable_name, &vendor_guid, 2013 variable_name_size); 2014 status = EFI_NOT_FOUND; 2015 break; 2016 } 2017 2018 efivar_create_sysfs_entry(efivars, 2019 variable_name_size, 2020 variable_name, 2021 &vendor_guid); 2022 break; 2023 case EFI_NOT_FOUND: 2024 break; 2025 default: 2026 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n", 2027 status); 2028 status = EFI_NOT_FOUND; 2029 break; 2030 } 2031 } while (status != EFI_NOT_FOUND); 2032 2033 error = create_efivars_bin_attributes(efivars); 2034 if (error) 2035 unregister_efivars(efivars); 2036 2037 if (!efivars_pstore_disable) 2038 efivar_pstore_register(efivars); 2039 2040 register_filesystem(&efivarfs_type); 2041 2042out: 2043 kfree(variable_name); 2044 2045 return error; 2046} 2047EXPORT_SYMBOL_GPL(register_efivars); 2048 2049/* 2050 * For now we register the efi subsystem with the firmware subsystem 2051 * and the vars subsystem with the efi subsystem. In the future, it 2052 * might make sense to split off the efi subsystem into its own 2053 * driver, but for now only efivars will register with it, so just 2054 * include it here. 2055 */ 2056 2057static int __init 2058efivars_init(void) 2059{ 2060 int error = 0; 2061 2062 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION, 2063 EFIVARS_DATE); 2064 2065 if (!efi_enabled(EFI_RUNTIME_SERVICES)) 2066 return 0; 2067 2068 /* For now we'll register the efi directory at /sys/firmware/efi */ 2069 efi_kobj = kobject_create_and_add("efi", firmware_kobj); 2070 if (!efi_kobj) { 2071 printk(KERN_ERR "efivars: Firmware registration failed.\n"); 2072 return -ENOMEM; 2073 } 2074 2075 ops.get_variable = efi.get_variable; 2076 ops.set_variable = efi.set_variable; 2077 ops.get_next_variable = efi.get_next_variable; 2078 ops.query_variable_store = efi_query_variable_store; 2079 2080 error = register_efivars(&__efivars, &ops, efi_kobj); 2081 if (error) 2082 goto err_put; 2083 2084 /* Don't forget the systab entry */ 2085 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group); 2086 if (error) { 2087 printk(KERN_ERR 2088 "efivars: Sysfs attribute export failed with error %d.\n", 2089 error); 2090 goto err_unregister; 2091 } 2092 2093 return 0; 2094 2095err_unregister: 2096 unregister_efivars(&__efivars); 2097err_put: 2098 kobject_put(efi_kobj); 2099 return error; 2100} 2101 2102static void __exit 2103efivars_exit(void) 2104{ 2105 cancel_work_sync(&efivar_work); 2106 2107 if (efi_enabled(EFI_RUNTIME_SERVICES)) { 2108 unregister_efivars(&__efivars); 2109 kobject_put(efi_kobj); 2110 } 2111} 2112 2113module_init(efivars_init); 2114module_exit(efivars_exit); 2115