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