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