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