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