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