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