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