blob: adfc4863202b92a1354753b4eed685047ea88525 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Randy.Dunlapc59ede72006-01-11 12:17:46 -080068#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/types.h>
70#include <linux/errno.h>
71#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090080#include <linux/slab.h>
Matthew Garrett5ee9c192011-07-21 16:57:56 -040081#include <linux/pstore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Matthew Garrett5d9db882012-10-05 13:54:56 +080083#include <linux/fs.h>
84#include <linux/ramfs.h>
85#include <linux/pagemap.h>
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#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
Matthew Garrett5ee9c192011-07-21 16:57:56 -040097#define DUMP_NAME_LEN 52
Matthew Garrett5d9db882012-10-05 13:54:56 +080098#define GUID_LEN 37
Matthew Garrett5ee9c192011-07-21 16:57:56 -040099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
101 * The maximum size of VariableName + Data = 1024
102 * Therefore, it's reasonable to save that much
103 * space in each part of the structure,
104 * and we use a page for reading/writing.
105 */
106
107struct efi_variable {
108 efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
109 efi_guid_t VendorGuid;
110 unsigned long DataSize;
111 __u8 Data[1024];
112 efi_status_t Status;
113 __u32 Attributes;
114} __attribute__((packed));
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116struct efivar_entry {
Mike Waychison4142ef12011-03-11 17:43:11 -0800117 struct efivars *efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct efi_variable var;
119 struct list_head list;
120 struct kobject kobj;
121};
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123struct efivar_attribute {
124 struct attribute attr;
125 ssize_t (*show) (struct efivar_entry *entry, char *buf);
126 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
127};
128
Matthew Garrett5d9db882012-10-05 13:54:56 +0800129static struct efivars __efivars;
130static struct efivar_operations ops;
131
Mike Waychison7644c162011-07-21 16:58:00 -0400132#define PSTORE_EFI_ATTRIBUTES \
133 (EFI_VARIABLE_NON_VOLATILE | \
134 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
135 EFI_VARIABLE_RUNTIME_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define EFIVAR_ATTR(_name, _mode, _show, _store) \
138struct efivar_attribute efivar_attr_##_name = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900139 .attr = {.name = __stringify(_name), .mode = _mode}, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .show = _show, \
141 .store = _store, \
142};
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
145#define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
146
147/*
148 * Prototype for sysfs creation function
149 */
150static int
Mike Waychison4142ef12011-03-11 17:43:11 -0800151efivar_create_sysfs_entry(struct efivars *efivars,
152 unsigned long variable_name_size,
153 efi_char16_t *variable_name,
154 efi_guid_t *vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156/* Return the number of unicode characters in data */
157static unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400158utf16_strnlen(efi_char16_t *s, size_t maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 unsigned long length = 0;
161
Mike Waychisona2940902011-07-21 16:57:57 -0400162 while (*s++ != 0 && length < maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 length++;
164 return length;
165}
166
Tony Luckb728a5c2011-08-02 15:08:30 -0700167static inline unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400168utf16_strlen(efi_char16_t *s)
169{
170 return utf16_strnlen(s, ~0UL);
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
174 * Return the number of bytes is the length of this string
175 * Note: this is NOT the same as the number of unicode characters
176 */
177static inline unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400178utf16_strsize(efi_char16_t *data, unsigned long maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Mike Waychisona2940902011-07-21 16:57:57 -0400180 return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Mike Waychison828aa1f2011-07-21 16:57:58 -0400183static inline int
184utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
185{
186 while (1) {
187 if (len == 0)
188 return 0;
189 if (*a < *b)
190 return -1;
191 if (*a > *b)
192 return 1;
193 if (*a == 0) /* implies *b == 0 */
194 return 0;
195 a++;
196 b++;
197 len--;
198 }
199}
200
Matthew Garrettfec6c202012-04-30 16:11:30 -0400201static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400202validate_device_path(struct efi_variable *var, int match, u8 *buffer,
203 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400204{
205 struct efi_generic_dev_path *node;
206 int offset = 0;
207
208 node = (struct efi_generic_dev_path *)buffer;
209
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400210 if (len < sizeof(*node))
211 return false;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400212
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400213 while (offset <= len - sizeof(*node) &&
214 node->length >= sizeof(*node) &&
215 node->length <= len - offset) {
216 offset += node->length;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400217
218 if ((node->type == EFI_DEV_END_PATH ||
219 node->type == EFI_DEV_END_PATH2) &&
220 node->sub_type == EFI_DEV_END_ENTIRE)
221 return true;
222
223 node = (struct efi_generic_dev_path *)(buffer + offset);
224 }
225
226 /*
227 * If we're here then either node->length pointed past the end
228 * of the buffer or we reached the end of the buffer without
229 * finding a device path end node.
230 */
231 return false;
232}
233
234static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400235validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
236 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400237{
238 /* An array of 16-bit integers */
239 if ((len % 2) != 0)
240 return false;
241
242 return true;
243}
244
245static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400246validate_load_option(struct efi_variable *var, int match, u8 *buffer,
247 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400248{
249 u16 filepathlength;
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400250 int i, desclength = 0, namelen;
251
252 namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
Matthew Garrettfec6c202012-04-30 16:11:30 -0400253
254 /* Either "Boot" or "Driver" followed by four digits of hex */
255 for (i = match; i < match+4; i++) {
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400256 if (var->VariableName[i] > 127 ||
257 hex_to_bin(var->VariableName[i] & 0xff) < 0)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400258 return true;
259 }
260
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400261 /* Reject it if there's 4 digits of hex and then further content */
262 if (namelen > match + 4)
263 return false;
264
265 /* A valid entry must be at least 8 bytes */
266 if (len < 8)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400267 return false;
268
269 filepathlength = buffer[4] | buffer[5] << 8;
270
271 /*
272 * There's no stored length for the description, so it has to be
273 * found by hand
274 */
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400275 desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400276
277 /* Each boot entry must have a descriptor */
278 if (!desclength)
279 return false;
280
281 /*
282 * If the sum of the length of the description, the claimed filepath
283 * length and the original header are greater than the length of the
284 * variable, it's malformed
285 */
286 if ((desclength + filepathlength + 6) > len)
287 return false;
288
289 /*
290 * And, finally, check the filepath
291 */
292 return validate_device_path(var, match, buffer + desclength + 6,
293 filepathlength);
294}
295
296static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400297validate_uint16(struct efi_variable *var, int match, u8 *buffer,
298 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400299{
300 /* A single 16-bit integer */
301 if (len != 2)
302 return false;
303
304 return true;
305}
306
307static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400308validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
309 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400310{
311 int i;
312
313 for (i = 0; i < len; i++) {
314 if (buffer[i] > 127)
315 return false;
316
317 if (buffer[i] == 0)
318 return true;
319 }
320
321 return false;
322}
323
324struct variable_validate {
325 char *name;
326 bool (*validate)(struct efi_variable *var, int match, u8 *data,
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400327 unsigned long len);
Matthew Garrettfec6c202012-04-30 16:11:30 -0400328};
329
330static const struct variable_validate variable_validate[] = {
331 { "BootNext", validate_uint16 },
332 { "BootOrder", validate_boot_order },
333 { "DriverOrder", validate_boot_order },
334 { "Boot*", validate_load_option },
335 { "Driver*", validate_load_option },
336 { "ConIn", validate_device_path },
337 { "ConInDev", validate_device_path },
338 { "ConOut", validate_device_path },
339 { "ConOutDev", validate_device_path },
340 { "ErrOut", validate_device_path },
341 { "ErrOutDev", validate_device_path },
342 { "Timeout", validate_uint16 },
343 { "Lang", validate_ascii_string },
344 { "PlatformLang", validate_ascii_string },
345 { "", NULL },
346};
347
348static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400349validate_var(struct efi_variable *var, u8 *data, unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400350{
351 int i;
352 u16 *unicode_name = var->VariableName;
353
354 for (i = 0; variable_validate[i].validate != NULL; i++) {
355 const char *name = variable_validate[i].name;
356 int match;
357
358 for (match = 0; ; match++) {
359 char c = name[match];
360 u16 u = unicode_name[match];
361
362 /* All special variables are plain ascii */
363 if (u > 127)
364 return true;
365
366 /* Wildcard in the matching name means we've matched */
367 if (c == '*')
368 return variable_validate[i].validate(var,
369 match, data, len);
370
371 /* Case sensitive match */
372 if (c != u)
373 break;
374
375 /* Reached the end of the string while matching */
376 if (!c)
377 return variable_validate[i].validate(var,
378 match, data, len);
379 }
380 }
381
382 return true;
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385static efi_status_t
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400386get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 efi_status_t status;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 var->DataSize = 1024;
Mike Waychison32958142011-03-11 17:43:21 -0800391 status = efivars->ops->get_variable(var->VariableName,
392 &var->VendorGuid,
393 &var->Attributes,
394 &var->DataSize,
395 var->Data);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400396 return status;
397}
398
399static efi_status_t
400get_var_data(struct efivars *efivars, struct efi_variable *var)
401{
402 efi_status_t status;
403
404 spin_lock(&efivars->lock);
405 status = get_var_data_locked(efivars, var);
Mike Waychison29422692011-03-11 17:43:00 -0800406 spin_unlock(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (status != EFI_SUCCESS) {
409 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
410 status);
411 }
412 return status;
413}
414
415static ssize_t
416efivar_guid_read(struct efivar_entry *entry, char *buf)
417{
418 struct efi_variable *var = &entry->var;
419 char *str = buf;
420
421 if (!entry || !buf)
422 return 0;
423
424 efi_guid_unparse(&var->VendorGuid, str);
425 str += strlen(str);
426 str += sprintf(str, "\n");
427
428 return str - buf;
429}
430
431static ssize_t
432efivar_attr_read(struct efivar_entry *entry, char *buf)
433{
434 struct efi_variable *var = &entry->var;
435 char *str = buf;
436 efi_status_t status;
437
438 if (!entry || !buf)
439 return -EINVAL;
440
Mike Waychison4142ef12011-03-11 17:43:11 -0800441 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (status != EFI_SUCCESS)
443 return -EIO;
444
Khalid Aziz70839092012-09-10 12:52:42 -0600445 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600447 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600449 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600451 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
452 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
453 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
454 str += sprintf(str,
455 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
456 if (var->Attributes &
457 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
458 str += sprintf(str,
459 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
460 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
461 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return str - buf;
463}
464
465static ssize_t
466efivar_size_read(struct efivar_entry *entry, char *buf)
467{
468 struct efi_variable *var = &entry->var;
469 char *str = buf;
470 efi_status_t status;
471
472 if (!entry || !buf)
473 return -EINVAL;
474
Mike Waychison4142ef12011-03-11 17:43:11 -0800475 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (status != EFI_SUCCESS)
477 return -EIO;
478
479 str += sprintf(str, "0x%lx\n", var->DataSize);
480 return str - buf;
481}
482
483static ssize_t
484efivar_data_read(struct efivar_entry *entry, char *buf)
485{
486 struct efi_variable *var = &entry->var;
487 efi_status_t status;
488
489 if (!entry || !buf)
490 return -EINVAL;
491
Mike Waychison4142ef12011-03-11 17:43:11 -0800492 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (status != EFI_SUCCESS)
494 return -EIO;
495
496 memcpy(buf, var->Data, var->DataSize);
497 return var->DataSize;
498}
499/*
500 * We allow each variable to be edited via rewriting the
501 * entire efi variable structure.
502 */
503static ssize_t
504efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
505{
506 struct efi_variable *new_var, *var = &entry->var;
Mike Waychison4142ef12011-03-11 17:43:11 -0800507 struct efivars *efivars = entry->efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 efi_status_t status = EFI_NOT_FOUND;
509
510 if (count != sizeof(struct efi_variable))
511 return -EINVAL;
512
513 new_var = (struct efi_variable *)buf;
514 /*
515 * If only updating the variable data, then the name
516 * and guid should remain the same
517 */
518 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
519 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
520 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
521 return -EINVAL;
522 }
523
524 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
525 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
526 return -EINVAL;
527 }
528
Matthew Garrettfec6c202012-04-30 16:11:30 -0400529 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
530 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
531 printk(KERN_ERR "efivars: Malformed variable content\n");
532 return -EINVAL;
533 }
534
Mike Waychison29422692011-03-11 17:43:00 -0800535 spin_lock(&efivars->lock);
Mike Waychison32958142011-03-11 17:43:21 -0800536 status = efivars->ops->set_variable(new_var->VariableName,
537 &new_var->VendorGuid,
538 new_var->Attributes,
539 new_var->DataSize,
540 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Mike Waychison29422692011-03-11 17:43:00 -0800542 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 if (status != EFI_SUCCESS) {
545 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
546 status);
547 return -EIO;
548 }
549
550 memcpy(&entry->var, new_var, count);
551 return count;
552}
553
554static ssize_t
555efivar_show_raw(struct efivar_entry *entry, char *buf)
556{
557 struct efi_variable *var = &entry->var;
558 efi_status_t status;
559
560 if (!entry || !buf)
561 return 0;
562
Mike Waychison4142ef12011-03-11 17:43:11 -0800563 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (status != EFI_SUCCESS)
565 return -EIO;
566
567 memcpy(buf, var, sizeof(*var));
568 return sizeof(*var);
569}
570
571/*
572 * Generic read/write functions that call the specific functions of
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200573 * the attributes...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 */
575static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
576 char *buf)
577{
578 struct efivar_entry *var = to_efivar_entry(kobj);
579 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500580 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 if (!capable(CAP_SYS_ADMIN))
583 return -EACCES;
584
585 if (efivar_attr->show) {
586 ret = efivar_attr->show(var, buf);
587 }
588 return ret;
589}
590
591static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
592 const char *buf, size_t count)
593{
594 struct efivar_entry *var = to_efivar_entry(kobj);
595 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500596 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (!capable(CAP_SYS_ADMIN))
599 return -EACCES;
600
601 if (efivar_attr->store)
602 ret = efivar_attr->store(var, buf, count);
603
604 return ret;
605}
606
Emese Revfy52cf25d2010-01-19 02:58:23 +0100607static const struct sysfs_ops efivar_attr_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 .show = efivar_attr_show,
609 .store = efivar_attr_store,
610};
611
612static void efivar_release(struct kobject *kobj)
613{
614 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 kfree(var);
616}
617
618static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
619static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
620static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
621static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
622static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
623
624static struct attribute *def_attrs[] = {
625 &efivar_attr_guid.attr,
626 &efivar_attr_size.attr,
627 &efivar_attr_attributes.attr,
628 &efivar_attr_data.attr,
629 &efivar_attr_raw_var.attr,
630 NULL,
631};
632
Greg Kroah-Hartmane89a4112007-10-11 10:47:49 -0600633static struct kobj_type efivar_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 .release = efivar_release,
635 .sysfs_ops = &efivar_attr_ops,
636 .default_attrs = def_attrs,
637};
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639static inline void
640efivar_unregister(struct efivar_entry *var)
641{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800642 kobject_put(&var->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Matthew Garrett5d9db882012-10-05 13:54:56 +0800645static int efivarfs_file_open(struct inode *inode, struct file *file)
646{
647 file->private_data = inode->i_private;
648 return 0;
649}
650
651static ssize_t efivarfs_file_write(struct file *file,
652 const char __user *userbuf, size_t count, loff_t *ppos)
653{
654 struct efivar_entry *var = file->private_data;
655 struct efivars *efivars;
656 efi_status_t status;
657 void *data;
658 u32 attributes;
659 struct inode *inode = file->f_mapping->host;
660 int datasize = count - sizeof(attributes);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800661 unsigned long newdatasize;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800662
663 if (count < sizeof(attributes))
664 return -EINVAL;
665
666 data = kmalloc(datasize, GFP_KERNEL);
667
668 if (!data)
669 return -ENOMEM;
670
671 efivars = var->efivars;
672
673 if (copy_from_user(&attributes, userbuf, sizeof(attributes))) {
674 count = -EFAULT;
675 goto out;
676 }
677
678 if (attributes & ~(EFI_VARIABLE_MASK)) {
679 count = -EINVAL;
680 goto out;
681 }
682
683 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
684 count = -EFAULT;
685 goto out;
686 }
687
688 if (validate_var(&var->var, data, datasize) == false) {
689 count = -EINVAL;
690 goto out;
691 }
692
693 status = efivars->ops->set_variable(var->var.VariableName,
694 &var->var.VendorGuid,
695 attributes, datasize,
696 data);
697
698 switch (status) {
699 case EFI_SUCCESS:
Matthew Garrett5d9db882012-10-05 13:54:56 +0800700 break;
701 case EFI_INVALID_PARAMETER:
702 count = -EINVAL;
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800703 goto out;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800704 case EFI_OUT_OF_RESOURCES:
705 count = -ENOSPC;
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800706 goto out;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800707 case EFI_DEVICE_ERROR:
708 count = -EIO;
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800709 goto out;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800710 case EFI_WRITE_PROTECTED:
711 count = -EROFS;
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800712 goto out;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800713 case EFI_SECURITY_VIOLATION:
714 count = -EACCES;
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800715 goto out;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800716 case EFI_NOT_FOUND:
717 count = -ENOENT;
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800718 goto out;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800719 default:
720 count = -EINVAL;
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800721 goto out;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800722 }
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800723
724 /*
725 * Writing to the variable may have caused a change in size (which
726 * could either be an append or an overwrite), or the variable to be
727 * deleted. Perform a GetVariable() so we can tell what actually
728 * happened.
729 */
730 newdatasize = 0;
731 status = efivars->ops->get_variable(var->var.VariableName,
732 &var->var.VendorGuid,
733 NULL, &newdatasize,
734 NULL);
735
736 if (status == EFI_BUFFER_TOO_SMALL) {
737 mutex_lock(&inode->i_mutex);
738 i_size_write(inode, newdatasize + sizeof(attributes));
739 mutex_unlock(&inode->i_mutex);
740
741 } else if (status == EFI_NOT_FOUND) {
742 spin_lock(&efivars->lock);
743 list_del(&var->list);
744 spin_unlock(&efivars->lock);
745 efivar_unregister(var);
746 drop_nlink(inode);
747 dput(file->f_dentry);
748
749 } else {
750 pr_warn("efivarfs: inconsistent EFI variable implementation? "
751 "status = %lx\n", status);
752 }
753
Matthew Garrett5d9db882012-10-05 13:54:56 +0800754out:
755 kfree(data);
756
757 return count;
758}
759
760static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
761 size_t count, loff_t *ppos)
762{
763 struct efivar_entry *var = file->private_data;
764 struct efivars *efivars = var->efivars;
765 efi_status_t status;
766 unsigned long datasize = 0;
767 u32 attributes;
768 void *data;
Andy Whitcroftd142df02012-10-11 11:32:17 +0100769 ssize_t size = 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800770
771 status = efivars->ops->get_variable(var->var.VariableName,
772 &var->var.VendorGuid,
773 &attributes, &datasize, NULL);
774
775 if (status != EFI_BUFFER_TOO_SMALL)
776 return 0;
777
778 data = kmalloc(datasize + 4, GFP_KERNEL);
779
780 if (!data)
781 return 0;
782
783 status = efivars->ops->get_variable(var->var.VariableName,
784 &var->var.VendorGuid,
785 &attributes, &datasize,
786 (data + 4));
Matthew Garrett5d9db882012-10-05 13:54:56 +0800787 if (status != EFI_SUCCESS)
Andy Whitcroftd142df02012-10-11 11:32:17 +0100788 goto out_free;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800789
790 memcpy(data, &attributes, 4);
791 size = simple_read_from_buffer(userbuf, count, ppos,
792 data, datasize + 4);
Andy Whitcroftd142df02012-10-11 11:32:17 +0100793out_free:
Matthew Garrett5d9db882012-10-05 13:54:56 +0800794 kfree(data);
795
796 return size;
797}
798
799static void efivarfs_evict_inode(struct inode *inode)
800{
801 clear_inode(inode);
802}
803
804static const struct super_operations efivarfs_ops = {
805 .statfs = simple_statfs,
806 .drop_inode = generic_delete_inode,
807 .evict_inode = efivarfs_evict_inode,
808 .show_options = generic_show_options,
809};
810
811static struct super_block *efivarfs_sb;
812
813static const struct inode_operations efivarfs_dir_inode_operations;
814
815static const struct file_operations efivarfs_file_operations = {
816 .open = efivarfs_file_open,
817 .read = efivarfs_file_read,
818 .write = efivarfs_file_write,
819 .llseek = no_llseek,
820};
821
822static struct inode *efivarfs_get_inode(struct super_block *sb,
823 const struct inode *dir, int mode, dev_t dev)
824{
825 struct inode *inode = new_inode(sb);
826
827 if (inode) {
828 inode->i_ino = get_next_ino();
829 inode->i_uid = inode->i_gid = 0;
830 inode->i_mode = mode;
831 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
832 switch (mode & S_IFMT) {
833 case S_IFREG:
834 inode->i_fop = &efivarfs_file_operations;
835 break;
836 case S_IFDIR:
837 inode->i_op = &efivarfs_dir_inode_operations;
838 inode->i_fop = &simple_dir_operations;
839 inc_nlink(inode);
840 break;
841 }
842 }
843 return inode;
844}
845
846static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
847{
848 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
849 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
850 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
851 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
852 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
853 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
854 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
855 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
856 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
857 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
858 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
859 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
860 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
861 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
862 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
863 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
864}
865
866static int efivarfs_create(struct inode *dir, struct dentry *dentry,
867 umode_t mode, bool excl)
868{
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100869 struct inode *inode;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800870 struct efivars *efivars = &__efivars;
871 struct efivar_entry *var;
872 int namelen, i = 0, err = 0;
873
874 if (dentry->d_name.len < 38)
875 return -EINVAL;
876
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100877 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800878 if (!inode)
879 return -ENOSPC;
880
881 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100882 if (!var) {
883 err = -ENOMEM;
884 goto out;
885 }
Matthew Garrett5d9db882012-10-05 13:54:56 +0800886
887 namelen = dentry->d_name.len - GUID_LEN;
888
889 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
890 &var->var.VendorGuid);
891
892 for (i = 0; i < namelen; i++)
893 var->var.VariableName[i] = dentry->d_name.name[i];
894
895 var->var.VariableName[i] = '\0';
896
897 inode->i_private = var;
898 var->efivars = efivars;
899 var->kobj.kset = efivars->kset;
900
901 err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
902 dentry->d_name.name);
903 if (err)
904 goto out;
905
906 kobject_uevent(&var->kobj, KOBJ_ADD);
907 spin_lock(&efivars->lock);
908 list_add(&var->list, &efivars->list);
909 spin_unlock(&efivars->lock);
910 d_instantiate(dentry, inode);
911 dget(dentry);
912out:
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100913 if (err) {
Matthew Garrett5d9db882012-10-05 13:54:56 +0800914 kfree(var);
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100915 iput(inode);
916 }
Matthew Garrett5d9db882012-10-05 13:54:56 +0800917 return err;
918}
919
920static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
921{
922 struct efivar_entry *var = dentry->d_inode->i_private;
923 struct efivars *efivars = var->efivars;
924 efi_status_t status;
925
926 spin_lock(&efivars->lock);
927
928 status = efivars->ops->set_variable(var->var.VariableName,
929 &var->var.VendorGuid,
930 0, 0, NULL);
931
932 if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
933 list_del(&var->list);
934 spin_unlock(&efivars->lock);
935 efivar_unregister(var);
936 drop_nlink(dir);
937 dput(dentry);
938 return 0;
939 }
940
941 spin_unlock(&efivars->lock);
942 return -EINVAL;
943};
944
945int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
946{
947 struct inode *inode = NULL;
948 struct dentry *root;
949 struct efivar_entry *entry, *n;
950 struct efivars *efivars = &__efivars;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800951
952 efivarfs_sb = sb;
953
954 sb->s_maxbytes = MAX_LFS_FILESIZE;
955 sb->s_blocksize = PAGE_CACHE_SIZE;
956 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
957 sb->s_magic = PSTOREFS_MAGIC;
958 sb->s_op = &efivarfs_ops;
959 sb->s_time_gran = 1;
960
961 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +0100962 if (!inode)
963 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800964 inode->i_op = &efivarfs_dir_inode_operations;
965
966 root = d_make_root(inode);
967 sb->s_root = root;
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +0100968 if (!root)
969 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800970
971 list_for_each_entry_safe(entry, n, &efivars->list, list) {
972 struct inode *inode;
973 struct dentry *dentry, *root = efivarfs_sb->s_root;
974 char *name;
975 unsigned long size = 0;
976 int len, i;
977
978 len = utf16_strlen(entry->var.VariableName);
979
980 /* GUID plus trailing NULL */
981 name = kmalloc(len + 38, GFP_ATOMIC);
982
983 for (i = 0; i < len; i++)
984 name[i] = entry->var.VariableName[i] & 0xFF;
985
986 name[len] = '-';
987
988 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
989
990 name[len+GUID_LEN] = '\0';
991
992 inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
993 S_IFREG | 0644, 0);
994 dentry = d_alloc_name(root, name);
995
996 efivars->ops->get_variable(entry->var.VariableName,
997 &entry->var.VendorGuid,
998 &entry->var.Attributes,
999 &size,
1000 NULL);
1001
1002 mutex_lock(&inode->i_mutex);
1003 inode->i_private = entry;
1004 i_size_write(inode, size+4);
1005 mutex_unlock(&inode->i_mutex);
1006 d_add(dentry, inode);
1007 }
1008
1009 return 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001010}
1011
1012static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
1013 int flags, const char *dev_name, void *data)
1014{
1015 return mount_single(fs_type, flags, data, efivarfs_fill_super);
1016}
1017
1018static void efivarfs_kill_sb(struct super_block *sb)
1019{
1020 kill_litter_super(sb);
1021 efivarfs_sb = NULL;
1022}
1023
1024static struct file_system_type efivarfs_type = {
1025 .name = "efivarfs",
1026 .mount = efivarfs_mount,
1027 .kill_sb = efivarfs_kill_sb,
1028};
1029
1030static const struct inode_operations efivarfs_dir_inode_operations = {
1031 .lookup = simple_lookup,
1032 .unlink = efivarfs_unlink,
1033 .create = efivarfs_create,
1034};
1035
1036static struct pstore_info efi_pstore_info;
1037
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001038#ifdef CONFIG_PSTORE
1039
1040static int efi_pstore_open(struct pstore_info *psi)
1041{
1042 struct efivars *efivars = psi->data;
1043
1044 spin_lock(&efivars->lock);
1045 efivars->walk_entry = list_first_entry(&efivars->list,
1046 struct efivar_entry, list);
1047 return 0;
1048}
1049
1050static int efi_pstore_close(struct pstore_info *psi)
1051{
1052 struct efivars *efivars = psi->data;
1053
1054 spin_unlock(&efivars->lock);
1055 return 0;
1056}
1057
1058static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
Kees Cookf6f82852011-11-17 12:58:07 -08001059 struct timespec *timespec,
1060 char **buf, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001061{
1062 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1063 struct efivars *efivars = psi->data;
1064 char name[DUMP_NAME_LEN];
1065 int i;
1066 unsigned int part, size;
1067 unsigned long time;
1068
1069 while (&efivars->walk_entry->list != &efivars->list) {
1070 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
1071 vendor)) {
1072 for (i = 0; i < DUMP_NAME_LEN; i++) {
1073 name[i] = efivars->walk_entry->var.VariableName[i];
1074 }
1075 if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
1076 *id = part;
1077 timespec->tv_sec = time;
1078 timespec->tv_nsec = 0;
1079 get_var_data_locked(efivars, &efivars->walk_entry->var);
1080 size = efivars->walk_entry->var.DataSize;
Kees Cookf6f82852011-11-17 12:58:07 -08001081 *buf = kmalloc(size, GFP_KERNEL);
1082 if (*buf == NULL)
1083 return -ENOMEM;
1084 memcpy(*buf, efivars->walk_entry->var.Data,
1085 size);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001086 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1087 struct efivar_entry, list);
1088 return size;
1089 }
1090 }
1091 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1092 struct efivar_entry, list);
1093 }
1094 return 0;
1095}
1096
Kees Cook3d6d8d22011-11-17 13:13:29 -08001097static int efi_pstore_write(enum pstore_type_id type,
1098 enum kmsg_dump_reason reason, u64 *id,
Chen Gongb238b8f2011-10-12 09:17:24 -07001099 unsigned int part, size_t size, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001100{
1101 char name[DUMP_NAME_LEN];
1102 char stub_name[DUMP_NAME_LEN];
1103 efi_char16_t efi_name[DUMP_NAME_LEN];
1104 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1105 struct efivars *efivars = psi->data;
1106 struct efivar_entry *entry, *found = NULL;
Chen Gongb238b8f2011-10-12 09:17:24 -07001107 int i, ret = 0;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001108
1109 sprintf(stub_name, "dump-type%u-%u-", type, part);
1110 sprintf(name, "%s%lu", stub_name, get_seconds());
1111
1112 spin_lock(&efivars->lock);
1113
1114 for (i = 0; i < DUMP_NAME_LEN; i++)
1115 efi_name[i] = stub_name[i];
1116
1117 /*
1118 * Clean up any entries with the same name
1119 */
1120
1121 list_for_each_entry(entry, &efivars->list, list) {
1122 get_var_data_locked(efivars, &entry->var);
1123
Mike Waychisonc4755942011-07-21 16:57:59 -04001124 if (efi_guidcmp(entry->var.VendorGuid, vendor))
1125 continue;
1126 if (utf16_strncmp(entry->var.VariableName, efi_name,
1127 utf16_strlen(efi_name)))
1128 continue;
1129 /* Needs to be a prefix */
1130 if (entry->var.VariableName[utf16_strlen(efi_name)] == 0)
1131 continue;
1132
1133 /* found */
1134 found = entry;
Mike Waychison7644c162011-07-21 16:58:00 -04001135 efivars->ops->set_variable(entry->var.VariableName,
1136 &entry->var.VendorGuid,
1137 PSTORE_EFI_ATTRIBUTES,
Mike Waychisonc4755942011-07-21 16:57:59 -04001138 0, NULL);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001139 }
1140
1141 if (found)
1142 list_del(&found->list);
1143
1144 for (i = 0; i < DUMP_NAME_LEN; i++)
1145 efi_name[i] = name[i];
1146
Mike Waychison7644c162011-07-21 16:58:00 -04001147 efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001148 size, psi->buf);
1149
1150 spin_unlock(&efivars->lock);
1151
1152 if (found)
1153 efivar_unregister(found);
1154
1155 if (size)
Chen Gongb238b8f2011-10-12 09:17:24 -07001156 ret = efivar_create_sysfs_entry(efivars,
Mike Waychisona2940902011-07-21 16:57:57 -04001157 utf16_strsize(efi_name,
1158 DUMP_NAME_LEN * 2),
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001159 efi_name, &vendor);
1160
Chen Gongb238b8f2011-10-12 09:17:24 -07001161 *id = part;
1162 return ret;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001163};
1164
1165static int efi_pstore_erase(enum pstore_type_id type, u64 id,
1166 struct pstore_info *psi)
1167{
Kees Cook3d6d8d22011-11-17 13:13:29 -08001168 efi_pstore_write(type, 0, &id, (unsigned int)id, 0, psi);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001169
1170 return 0;
1171}
1172#else
1173static int efi_pstore_open(struct pstore_info *psi)
1174{
1175 return 0;
1176}
1177
1178static int efi_pstore_close(struct pstore_info *psi)
1179{
1180 return 0;
1181}
1182
1183static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
Christoph Fritzeee628d2011-11-28 23:49:33 +01001184 struct timespec *timespec,
1185 char **buf, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001186{
1187 return -1;
1188}
1189
Kees Cook3d6d8d22011-11-17 13:13:29 -08001190static int efi_pstore_write(enum pstore_type_id type,
1191 enum kmsg_dump_reason reason, u64 *id,
Chen Gongb238b8f2011-10-12 09:17:24 -07001192 unsigned int part, size_t size, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001193{
1194 return 0;
1195}
1196
1197static int efi_pstore_erase(enum pstore_type_id type, u64 id,
1198 struct pstore_info *psi)
1199{
1200 return 0;
1201}
1202#endif
1203
1204static struct pstore_info efi_pstore_info = {
1205 .owner = THIS_MODULE,
1206 .name = "efi",
1207 .open = efi_pstore_open,
1208 .close = efi_pstore_close,
1209 .read = efi_pstore_read,
1210 .write = efi_pstore_write,
1211 .erase = efi_pstore_erase,
1212};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Chris Wright2c3c8be2010-05-12 18:28:57 -07001214static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001215 struct bin_attribute *bin_attr,
1216 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 struct efi_variable *new_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001219 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001220 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 efi_status_t status = EFI_NOT_FOUND;
1223 int found = 0;
1224
1225 if (!capable(CAP_SYS_ADMIN))
1226 return -EACCES;
1227
Matthew Garrettfec6c202012-04-30 16:11:30 -04001228 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
1229 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
1230 printk(KERN_ERR "efivars: Malformed variable content\n");
1231 return -EINVAL;
1232 }
1233
Mike Waychison29422692011-03-11 17:43:00 -08001234 spin_lock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /*
1237 * Does this variable already exist?
1238 */
Mike Waychison29422692011-03-11 17:43:00 -08001239 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -04001240 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1241 strsize2 = utf16_strsize(new_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (strsize1 == strsize2 &&
1243 !memcmp(&(search_efivar->var.VariableName),
1244 new_var->VariableName, strsize1) &&
1245 !efi_guidcmp(search_efivar->var.VendorGuid,
1246 new_var->VendorGuid)) {
1247 found = 1;
1248 break;
1249 }
1250 }
1251 if (found) {
Mike Waychison29422692011-03-11 17:43:00 -08001252 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return -EINVAL;
1254 }
1255
1256 /* now *really* create the variable via EFI */
Mike Waychison32958142011-03-11 17:43:21 -08001257 status = efivars->ops->set_variable(new_var->VariableName,
1258 &new_var->VendorGuid,
1259 new_var->Attributes,
1260 new_var->DataSize,
1261 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 if (status != EFI_SUCCESS) {
1264 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1265 status);
Mike Waychison29422692011-03-11 17:43:00 -08001266 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return -EIO;
1268 }
Mike Waychison29422692011-03-11 17:43:00 -08001269 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /* Create the entry in sysfs. Locking is not required here */
Mike Waychison4142ef12011-03-11 17:43:11 -08001272 status = efivar_create_sysfs_entry(efivars,
Mike Waychisona2940902011-07-21 16:57:57 -04001273 utf16_strsize(new_var->VariableName,
1274 1024),
Mike Waychison4142ef12011-03-11 17:43:11 -08001275 new_var->VariableName,
1276 &new_var->VendorGuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (status) {
1278 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
1279 }
1280 return count;
1281}
1282
Chris Wright2c3c8be2010-05-12 18:28:57 -07001283static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001284 struct bin_attribute *bin_attr,
1285 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 struct efi_variable *del_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001288 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001289 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 efi_status_t status = EFI_NOT_FOUND;
1292 int found = 0;
1293
1294 if (!capable(CAP_SYS_ADMIN))
1295 return -EACCES;
1296
Mike Waychison29422692011-03-11 17:43:00 -08001297 spin_lock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 /*
1300 * Does this variable already exist?
1301 */
Mike Waychison29422692011-03-11 17:43:00 -08001302 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -04001303 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1304 strsize2 = utf16_strsize(del_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (strsize1 == strsize2 &&
1306 !memcmp(&(search_efivar->var.VariableName),
1307 del_var->VariableName, strsize1) &&
1308 !efi_guidcmp(search_efivar->var.VendorGuid,
1309 del_var->VendorGuid)) {
1310 found = 1;
1311 break;
1312 }
1313 }
1314 if (!found) {
Mike Waychison29422692011-03-11 17:43:00 -08001315 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return -EINVAL;
1317 }
1318 /* force the Attributes/DataSize to 0 to ensure deletion */
1319 del_var->Attributes = 0;
1320 del_var->DataSize = 0;
1321
Mike Waychison32958142011-03-11 17:43:21 -08001322 status = efivars->ops->set_variable(del_var->VariableName,
1323 &del_var->VendorGuid,
1324 del_var->Attributes,
1325 del_var->DataSize,
1326 del_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 if (status != EFI_SUCCESS) {
1329 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1330 status);
Mike Waychison29422692011-03-11 17:43:00 -08001331 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return -EIO;
1333 }
Matt Domsch496a0fc2007-01-26 00:57:18 -08001334 list_del(&search_efivar->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /* We need to release this lock before unregistering. */
Mike Waychison29422692011-03-11 17:43:00 -08001336 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 efivar_unregister(search_efivar);
1338
1339 /* It's dead Jim.... */
1340 return count;
1341}
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343/*
1344 * Let's not leave out systab information that snuck into
1345 * the efivars driver
1346 */
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001347static ssize_t systab_show(struct kobject *kobj,
1348 struct kobj_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 char *str = buf;
1351
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001352 if (!kobj || !buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -EINVAL;
1354
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -08001355 if (efi.mps != EFI_INVALID_TABLE_ADDR)
1356 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
1357 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
1358 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
1359 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
1360 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
1361 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
1362 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
1363 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
1364 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
1365 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
1366 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
1367 if (efi.uga != EFI_INVALID_TABLE_ADDR)
1368 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 return str - buf;
1371}
1372
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001373static struct kobj_attribute efi_attr_systab =
1374 __ATTR(systab, 0400, systab_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001376static struct attribute *efi_subsys_attrs[] = {
1377 &efi_attr_systab.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 NULL, /* maybe more in the future? */
1379};
1380
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001381static struct attribute_group efi_subsys_attr_group = {
1382 .attrs = efi_subsys_attrs,
1383};
1384
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08001385static struct kobject *efi_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387/*
1388 * efivar_create_sysfs_entry()
1389 * Requires:
1390 * variable_name_size = number of bytes required to hold
1391 * variable_name (not counting the NULL
1392 * character at the end.
Mike Waychison29422692011-03-11 17:43:00 -08001393 * efivars->lock is not held on entry or exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 * Returns 1 on failure, 0 on success
1395 */
1396static int
Mike Waychison4142ef12011-03-11 17:43:11 -08001397efivar_create_sysfs_entry(struct efivars *efivars,
1398 unsigned long variable_name_size,
1399 efi_char16_t *variable_name,
1400 efi_guid_t *vendor_guid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
1403 char *short_name;
1404 struct efivar_entry *new_efivar;
1405
Deepak Saxena9c215382005-11-07 01:01:24 -08001406 short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
1407 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 if (!short_name || !new_efivar) {
Jesper Juhl0933ad92005-06-25 14:59:15 -07001410 kfree(short_name);
1411 kfree(new_efivar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return 1;
1413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Mike Waychison4142ef12011-03-11 17:43:11 -08001415 new_efivar->efivars = efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 memcpy(new_efivar->var.VariableName, variable_name,
1417 variable_name_size);
1418 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
1419
1420 /* Convert Unicode to normal chars (assume top bits are 0),
1421 ala UTF-8 */
1422 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
1423 short_name[i] = variable_name[i] & 0xFF;
1424 }
1425 /* This is ugly, but necessary to separate one vendor's
1426 private variables from another's. */
1427
1428 *(short_name + strlen(short_name)) = '-';
1429 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
1430
Mike Waychison29422692011-03-11 17:43:00 -08001431 new_efivar->kobj.kset = efivars->kset;
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001432 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
1433 "%s", short_name);
Jeff Garzik69b21862006-10-11 01:22:23 -07001434 if (i) {
1435 kfree(short_name);
1436 kfree(new_efivar);
1437 return 1;
1438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001440 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
Jesper Juhl0933ad92005-06-25 14:59:15 -07001441 kfree(short_name);
1442 short_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Mike Waychison29422692011-03-11 17:43:00 -08001444 spin_lock(&efivars->lock);
1445 list_add(&new_efivar->list, &efivars->list);
1446 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 return 0;
1449}
Mike Waychisond502fbb2011-03-11 17:43:06 -08001450
1451static int
1452create_efivars_bin_attributes(struct efivars *efivars)
1453{
1454 struct bin_attribute *attr;
1455 int error;
1456
1457 /* new_var */
1458 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1459 if (!attr)
1460 return -ENOMEM;
1461
1462 attr->attr.name = "new_var";
1463 attr->attr.mode = 0200;
1464 attr->write = efivar_create;
1465 attr->private = efivars;
1466 efivars->new_var = attr;
1467
1468 /* del_var */
1469 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1470 if (!attr) {
1471 error = -ENOMEM;
1472 goto out_free;
1473 }
1474 attr->attr.name = "del_var";
1475 attr->attr.mode = 0200;
1476 attr->write = efivar_delete;
1477 attr->private = efivars;
1478 efivars->del_var = attr;
1479
1480 sysfs_bin_attr_init(efivars->new_var);
1481 sysfs_bin_attr_init(efivars->del_var);
1482
1483 /* Register */
1484 error = sysfs_create_bin_file(&efivars->kset->kobj,
1485 efivars->new_var);
1486 if (error) {
1487 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
1488 " due to error %d\n", error);
1489 goto out_free;
1490 }
1491 error = sysfs_create_bin_file(&efivars->kset->kobj,
1492 efivars->del_var);
1493 if (error) {
1494 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
1495 " due to error %d\n", error);
1496 sysfs_remove_bin_file(&efivars->kset->kobj,
1497 efivars->new_var);
1498 goto out_free;
1499 }
1500
1501 return 0;
1502out_free:
Dan Carpenter051d51b2011-03-18 10:12:14 +03001503 kfree(efivars->del_var);
1504 efivars->del_var = NULL;
Mike Waychisond502fbb2011-03-11 17:43:06 -08001505 kfree(efivars->new_var);
1506 efivars->new_var = NULL;
1507 return error;
1508}
1509
Mike Waychison4fc756b2011-03-11 17:43:27 -08001510void unregister_efivars(struct efivars *efivars)
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001511{
1512 struct efivar_entry *entry, *n;
Mike Waychison4142ef12011-03-11 17:43:11 -08001513
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001514 list_for_each_entry_safe(entry, n, &efivars->list, list) {
1515 spin_lock(&efivars->lock);
1516 list_del(&entry->list);
1517 spin_unlock(&efivars->lock);
1518 efivar_unregister(entry);
1519 }
1520 if (efivars->new_var)
1521 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
1522 if (efivars->del_var)
1523 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
1524 kfree(efivars->new_var);
1525 kfree(efivars->del_var);
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08001526 kobject_put(efivars->kobject);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001527 kset_unregister(efivars->kset);
1528}
Mike Waychison4fc756b2011-03-11 17:43:27 -08001529EXPORT_SYMBOL_GPL(unregister_efivars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Mike Waychison4fc756b2011-03-11 17:43:27 -08001531int register_efivars(struct efivars *efivars,
1532 const struct efivar_operations *ops,
1533 struct kobject *parent_kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 efi_status_t status = EFI_NOT_FOUND;
1536 efi_guid_t vendor_guid;
1537 efi_char16_t *variable_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 unsigned long variable_name_size = 1024;
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001539 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Deepak Saxena9c215382005-11-07 01:01:24 -08001541 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (!variable_name) {
1543 printk(KERN_ERR "efivars: Memory allocation failed.\n");
1544 return -ENOMEM;
1545 }
1546
Mike Waychison29422692011-03-11 17:43:00 -08001547 spin_lock_init(&efivars->lock);
1548 INIT_LIST_HEAD(&efivars->list);
Mike Waychison32958142011-03-11 17:43:21 -08001549 efivars->ops = ops;
Mike Waychison29422692011-03-11 17:43:00 -08001550
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001551 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
Mike Waychison29422692011-03-11 17:43:00 -08001552 if (!efivars->kset) {
Greg Kroah-Hartman66ac8312007-11-02 13:20:40 -07001553 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
1554 error = -ENOMEM;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001555 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08001558 efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
1559 if (!efivars->kobject) {
1560 pr_err("efivars: Subsystem registration failed.\n");
1561 error = -ENOMEM;
1562 kset_unregister(efivars->kset);
1563 goto out;
1564 }
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 /*
1567 * Per EFI spec, the maximum storage allocated for both
1568 * the variable name and variable data is 1024 bytes.
1569 */
1570
1571 do {
1572 variable_name_size = 1024;
1573
Mike Waychison32958142011-03-11 17:43:21 -08001574 status = ops->get_next_variable(&variable_name_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 variable_name,
1576 &vendor_guid);
1577 switch (status) {
1578 case EFI_SUCCESS:
Mike Waychison4142ef12011-03-11 17:43:11 -08001579 efivar_create_sysfs_entry(efivars,
1580 variable_name_size,
1581 variable_name,
1582 &vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 break;
1584 case EFI_NOT_FOUND:
1585 break;
1586 default:
1587 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
1588 status);
1589 status = EFI_NOT_FOUND;
1590 break;
1591 }
1592 } while (status != EFI_NOT_FOUND);
1593
Mike Waychisond502fbb2011-03-11 17:43:06 -08001594 error = create_efivars_bin_attributes(efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001595 if (error)
1596 unregister_efivars(efivars);
1597
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001598 efivars->efi_pstore_info = efi_pstore_info;
1599
1600 efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
1601 if (efivars->efi_pstore_info.buf) {
1602 efivars->efi_pstore_info.bufsize = 1024;
1603 efivars->efi_pstore_info.data = efivars;
Don Zickusabd4d552011-08-12 10:54:51 -07001604 spin_lock_init(&efivars->efi_pstore_info.buf_lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001605 pstore_register(&efivars->efi_pstore_info);
1606 }
1607
Matthew Garrett5d9db882012-10-05 13:54:56 +08001608 register_filesystem(&efivarfs_type);
1609
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001610out:
1611 kfree(variable_name);
1612
1613 return error;
1614}
Mike Waychison4fc756b2011-03-11 17:43:27 -08001615EXPORT_SYMBOL_GPL(register_efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001616
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001617/*
1618 * For now we register the efi subsystem with the firmware subsystem
1619 * and the vars subsystem with the efi subsystem. In the future, it
1620 * might make sense to split off the efi subsystem into its own
1621 * driver, but for now only efivars will register with it, so just
1622 * include it here.
1623 */
1624
1625static int __init
1626efivars_init(void)
1627{
1628 int error = 0;
1629
1630 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
1631 EFIVARS_DATE);
1632
1633 if (!efi_enabled)
Mike Waychison4fc756b2011-03-11 17:43:27 -08001634 return 0;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001635
1636 /* For now we'll register the efi directory at /sys/firmware/efi */
1637 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
1638 if (!efi_kobj) {
1639 printk(KERN_ERR "efivars: Firmware registration failed.\n");
1640 return -ENOMEM;
1641 }
1642
Mike Waychison32958142011-03-11 17:43:21 -08001643 ops.get_variable = efi.get_variable;
1644 ops.set_variable = efi.set_variable;
1645 ops.get_next_variable = efi.get_next_variable;
1646 error = register_efivars(&__efivars, &ops, efi_kobj);
Dan Carpenter3116aab2011-03-18 10:12:38 +03001647 if (error)
1648 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 /* Don't forget the systab entry */
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08001651 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001652 if (error) {
1653 printk(KERN_ERR
1654 "efivars: Sysfs attribute export failed with error %d.\n",
1655 error);
Dan Carpenter3116aab2011-03-18 10:12:38 +03001656 goto err_unregister;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Dan Carpenter3116aab2011-03-18 10:12:38 +03001659 return 0;
1660
1661err_unregister:
1662 unregister_efivars(&__efivars);
1663err_put:
1664 kobject_put(efi_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return error;
1666}
1667
1668static void __exit
1669efivars_exit(void)
1670{
Randy Dunlapaabb6e12011-05-06 13:27:41 -07001671 if (efi_enabled) {
1672 unregister_efivars(&__efivars);
1673 kobject_put(efi_kobj);
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
1677module_init(efivars_init);
1678module_exit(efivars_exit);
1679