blob: 371c441295253f536ccd8147d18b990c5d4f31df [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
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
Jeremy Kerr310ad752012-10-19 15:16:45 +0800100 * Length of a GUID string (strlen("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))
101 * not including trailing NUL
102 */
103#define GUID_LEN 36
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/*
106 * The maximum size of VariableName + Data = 1024
107 * Therefore, it's reasonable to save that much
108 * space in each part of the structure,
109 * and we use a page for reading/writing.
110 */
111
112struct efi_variable {
113 efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
114 efi_guid_t VendorGuid;
115 unsigned long DataSize;
116 __u8 Data[1024];
117 efi_status_t Status;
118 __u32 Attributes;
119} __attribute__((packed));
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121struct efivar_entry {
Mike Waychison4142ef12011-03-11 17:43:11 -0800122 struct efivars *efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct efi_variable var;
124 struct list_head list;
125 struct kobject kobj;
126};
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128struct efivar_attribute {
129 struct attribute attr;
130 ssize_t (*show) (struct efivar_entry *entry, char *buf);
131 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
132};
133
Matthew Garrett5d9db882012-10-05 13:54:56 +0800134static struct efivars __efivars;
135static struct efivar_operations ops;
136
Mike Waychison7644c162011-07-21 16:58:00 -0400137#define PSTORE_EFI_ATTRIBUTES \
138 (EFI_VARIABLE_NON_VOLATILE | \
139 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
140 EFI_VARIABLE_RUNTIME_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define EFIVAR_ATTR(_name, _mode, _show, _store) \
143struct efivar_attribute efivar_attr_##_name = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900144 .attr = {.name = __stringify(_name), .mode = _mode}, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .show = _show, \
146 .store = _store, \
147};
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
150#define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
151
152/*
153 * Prototype for sysfs creation function
154 */
155static int
Mike Waychison4142ef12011-03-11 17:43:11 -0800156efivar_create_sysfs_entry(struct efivars *efivars,
157 unsigned long variable_name_size,
158 efi_char16_t *variable_name,
159 efi_guid_t *vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161/* Return the number of unicode characters in data */
162static unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400163utf16_strnlen(efi_char16_t *s, size_t maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 unsigned long length = 0;
166
Mike Waychisona2940902011-07-21 16:57:57 -0400167 while (*s++ != 0 && length < maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 length++;
169 return length;
170}
171
Tony Luckb728a5c2011-08-02 15:08:30 -0700172static inline unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400173utf16_strlen(efi_char16_t *s)
174{
175 return utf16_strnlen(s, ~0UL);
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
179 * Return the number of bytes is the length of this string
180 * Note: this is NOT the same as the number of unicode characters
181 */
182static inline unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400183utf16_strsize(efi_char16_t *data, unsigned long maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Mike Waychisona2940902011-07-21 16:57:57 -0400185 return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Mike Waychison828aa1f2011-07-21 16:57:58 -0400188static inline int
189utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
190{
191 while (1) {
192 if (len == 0)
193 return 0;
194 if (*a < *b)
195 return -1;
196 if (*a > *b)
197 return 1;
198 if (*a == 0) /* implies *b == 0 */
199 return 0;
200 a++;
201 b++;
202 len--;
203 }
204}
205
Matthew Garrettfec6c202012-04-30 16:11:30 -0400206static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400207validate_device_path(struct efi_variable *var, int match, u8 *buffer,
208 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400209{
210 struct efi_generic_dev_path *node;
211 int offset = 0;
212
213 node = (struct efi_generic_dev_path *)buffer;
214
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400215 if (len < sizeof(*node))
216 return false;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400217
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400218 while (offset <= len - sizeof(*node) &&
219 node->length >= sizeof(*node) &&
220 node->length <= len - offset) {
221 offset += node->length;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400222
223 if ((node->type == EFI_DEV_END_PATH ||
224 node->type == EFI_DEV_END_PATH2) &&
225 node->sub_type == EFI_DEV_END_ENTIRE)
226 return true;
227
228 node = (struct efi_generic_dev_path *)(buffer + offset);
229 }
230
231 /*
232 * If we're here then either node->length pointed past the end
233 * of the buffer or we reached the end of the buffer without
234 * finding a device path end node.
235 */
236 return false;
237}
238
239static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400240validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
241 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400242{
243 /* An array of 16-bit integers */
244 if ((len % 2) != 0)
245 return false;
246
247 return true;
248}
249
250static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400251validate_load_option(struct efi_variable *var, int match, u8 *buffer,
252 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400253{
254 u16 filepathlength;
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400255 int i, desclength = 0, namelen;
256
257 namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
Matthew Garrettfec6c202012-04-30 16:11:30 -0400258
259 /* Either "Boot" or "Driver" followed by four digits of hex */
260 for (i = match; i < match+4; i++) {
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400261 if (var->VariableName[i] > 127 ||
262 hex_to_bin(var->VariableName[i] & 0xff) < 0)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400263 return true;
264 }
265
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400266 /* Reject it if there's 4 digits of hex and then further content */
267 if (namelen > match + 4)
268 return false;
269
270 /* A valid entry must be at least 8 bytes */
271 if (len < 8)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400272 return false;
273
274 filepathlength = buffer[4] | buffer[5] << 8;
275
276 /*
277 * There's no stored length for the description, so it has to be
278 * found by hand
279 */
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400280 desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400281
282 /* Each boot entry must have a descriptor */
283 if (!desclength)
284 return false;
285
286 /*
287 * If the sum of the length of the description, the claimed filepath
288 * length and the original header are greater than the length of the
289 * variable, it's malformed
290 */
291 if ((desclength + filepathlength + 6) > len)
292 return false;
293
294 /*
295 * And, finally, check the filepath
296 */
297 return validate_device_path(var, match, buffer + desclength + 6,
298 filepathlength);
299}
300
301static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400302validate_uint16(struct efi_variable *var, int match, u8 *buffer,
303 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400304{
305 /* A single 16-bit integer */
306 if (len != 2)
307 return false;
308
309 return true;
310}
311
312static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400313validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
314 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400315{
316 int i;
317
318 for (i = 0; i < len; i++) {
319 if (buffer[i] > 127)
320 return false;
321
322 if (buffer[i] == 0)
323 return true;
324 }
325
326 return false;
327}
328
329struct variable_validate {
330 char *name;
331 bool (*validate)(struct efi_variable *var, int match, u8 *data,
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400332 unsigned long len);
Matthew Garrettfec6c202012-04-30 16:11:30 -0400333};
334
335static const struct variable_validate variable_validate[] = {
336 { "BootNext", validate_uint16 },
337 { "BootOrder", validate_boot_order },
338 { "DriverOrder", validate_boot_order },
339 { "Boot*", validate_load_option },
340 { "Driver*", validate_load_option },
341 { "ConIn", validate_device_path },
342 { "ConInDev", validate_device_path },
343 { "ConOut", validate_device_path },
344 { "ConOutDev", validate_device_path },
345 { "ErrOut", validate_device_path },
346 { "ErrOutDev", validate_device_path },
347 { "Timeout", validate_uint16 },
348 { "Lang", validate_ascii_string },
349 { "PlatformLang", validate_ascii_string },
350 { "", NULL },
351};
352
353static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400354validate_var(struct efi_variable *var, u8 *data, unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400355{
356 int i;
357 u16 *unicode_name = var->VariableName;
358
359 for (i = 0; variable_validate[i].validate != NULL; i++) {
360 const char *name = variable_validate[i].name;
361 int match;
362
363 for (match = 0; ; match++) {
364 char c = name[match];
365 u16 u = unicode_name[match];
366
367 /* All special variables are plain ascii */
368 if (u > 127)
369 return true;
370
371 /* Wildcard in the matching name means we've matched */
372 if (c == '*')
373 return variable_validate[i].validate(var,
374 match, data, len);
375
376 /* Case sensitive match */
377 if (c != u)
378 break;
379
380 /* Reached the end of the string while matching */
381 if (!c)
382 return variable_validate[i].validate(var,
383 match, data, len);
384 }
385 }
386
387 return true;
388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390static efi_status_t
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400391get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 efi_status_t status;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 var->DataSize = 1024;
Mike Waychison32958142011-03-11 17:43:21 -0800396 status = efivars->ops->get_variable(var->VariableName,
397 &var->VendorGuid,
398 &var->Attributes,
399 &var->DataSize,
400 var->Data);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400401 return status;
402}
403
404static efi_status_t
405get_var_data(struct efivars *efivars, struct efi_variable *var)
406{
407 efi_status_t status;
408
409 spin_lock(&efivars->lock);
410 status = get_var_data_locked(efivars, var);
Mike Waychison29422692011-03-11 17:43:00 -0800411 spin_unlock(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (status != EFI_SUCCESS) {
414 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
415 status);
416 }
417 return status;
418}
419
420static ssize_t
421efivar_guid_read(struct efivar_entry *entry, char *buf)
422{
423 struct efi_variable *var = &entry->var;
424 char *str = buf;
425
426 if (!entry || !buf)
427 return 0;
428
429 efi_guid_unparse(&var->VendorGuid, str);
430 str += strlen(str);
431 str += sprintf(str, "\n");
432
433 return str - buf;
434}
435
436static ssize_t
437efivar_attr_read(struct efivar_entry *entry, char *buf)
438{
439 struct efi_variable *var = &entry->var;
440 char *str = buf;
441 efi_status_t status;
442
443 if (!entry || !buf)
444 return -EINVAL;
445
Mike Waychison4142ef12011-03-11 17:43:11 -0800446 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (status != EFI_SUCCESS)
448 return -EIO;
449
Khalid Aziz70839092012-09-10 12:52:42 -0600450 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600452 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600454 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600456 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
457 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
458 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
459 str += sprintf(str,
460 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
461 if (var->Attributes &
462 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
463 str += sprintf(str,
464 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
465 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
466 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return str - buf;
468}
469
470static ssize_t
471efivar_size_read(struct efivar_entry *entry, char *buf)
472{
473 struct efi_variable *var = &entry->var;
474 char *str = buf;
475 efi_status_t status;
476
477 if (!entry || !buf)
478 return -EINVAL;
479
Mike Waychison4142ef12011-03-11 17:43:11 -0800480 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (status != EFI_SUCCESS)
482 return -EIO;
483
484 str += sprintf(str, "0x%lx\n", var->DataSize);
485 return str - buf;
486}
487
488static ssize_t
489efivar_data_read(struct efivar_entry *entry, char *buf)
490{
491 struct efi_variable *var = &entry->var;
492 efi_status_t status;
493
494 if (!entry || !buf)
495 return -EINVAL;
496
Mike Waychison4142ef12011-03-11 17:43:11 -0800497 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (status != EFI_SUCCESS)
499 return -EIO;
500
501 memcpy(buf, var->Data, var->DataSize);
502 return var->DataSize;
503}
504/*
505 * We allow each variable to be edited via rewriting the
506 * entire efi variable structure.
507 */
508static ssize_t
509efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
510{
511 struct efi_variable *new_var, *var = &entry->var;
Mike Waychison4142ef12011-03-11 17:43:11 -0800512 struct efivars *efivars = entry->efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 efi_status_t status = EFI_NOT_FOUND;
514
515 if (count != sizeof(struct efi_variable))
516 return -EINVAL;
517
518 new_var = (struct efi_variable *)buf;
519 /*
520 * If only updating the variable data, then the name
521 * and guid should remain the same
522 */
523 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
524 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
525 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
526 return -EINVAL;
527 }
528
529 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
530 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
531 return -EINVAL;
532 }
533
Matthew Garrettfec6c202012-04-30 16:11:30 -0400534 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
535 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
536 printk(KERN_ERR "efivars: Malformed variable content\n");
537 return -EINVAL;
538 }
539
Mike Waychison29422692011-03-11 17:43:00 -0800540 spin_lock(&efivars->lock);
Mike Waychison32958142011-03-11 17:43:21 -0800541 status = efivars->ops->set_variable(new_var->VariableName,
542 &new_var->VendorGuid,
543 new_var->Attributes,
544 new_var->DataSize,
545 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Mike Waychison29422692011-03-11 17:43:00 -0800547 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 if (status != EFI_SUCCESS) {
550 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
551 status);
552 return -EIO;
553 }
554
555 memcpy(&entry->var, new_var, count);
556 return count;
557}
558
559static ssize_t
560efivar_show_raw(struct efivar_entry *entry, char *buf)
561{
562 struct efi_variable *var = &entry->var;
563 efi_status_t status;
564
565 if (!entry || !buf)
566 return 0;
567
Mike Waychison4142ef12011-03-11 17:43:11 -0800568 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (status != EFI_SUCCESS)
570 return -EIO;
571
572 memcpy(buf, var, sizeof(*var));
573 return sizeof(*var);
574}
575
576/*
577 * Generic read/write functions that call the specific functions of
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200578 * the attributes...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 */
580static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
581 char *buf)
582{
583 struct efivar_entry *var = to_efivar_entry(kobj);
584 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500585 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 if (!capable(CAP_SYS_ADMIN))
588 return -EACCES;
589
590 if (efivar_attr->show) {
591 ret = efivar_attr->show(var, buf);
592 }
593 return ret;
594}
595
596static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
597 const char *buf, size_t count)
598{
599 struct efivar_entry *var = to_efivar_entry(kobj);
600 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500601 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (!capable(CAP_SYS_ADMIN))
604 return -EACCES;
605
606 if (efivar_attr->store)
607 ret = efivar_attr->store(var, buf, count);
608
609 return ret;
610}
611
Emese Revfy52cf25d2010-01-19 02:58:23 +0100612static const struct sysfs_ops efivar_attr_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 .show = efivar_attr_show,
614 .store = efivar_attr_store,
615};
616
617static void efivar_release(struct kobject *kobj)
618{
619 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 kfree(var);
621}
622
623static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
624static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
625static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
626static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
627static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
628
629static struct attribute *def_attrs[] = {
630 &efivar_attr_guid.attr,
631 &efivar_attr_size.attr,
632 &efivar_attr_attributes.attr,
633 &efivar_attr_data.attr,
634 &efivar_attr_raw_var.attr,
635 NULL,
636};
637
Greg Kroah-Hartmane89a4112007-10-11 10:47:49 -0600638static struct kobj_type efivar_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 .release = efivar_release,
640 .sysfs_ops = &efivar_attr_ops,
641 .default_attrs = def_attrs,
642};
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644static inline void
645efivar_unregister(struct efivar_entry *var)
646{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800647 kobject_put(&var->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Matthew Garrett5d9db882012-10-05 13:54:56 +0800650static int efivarfs_file_open(struct inode *inode, struct file *file)
651{
652 file->private_data = inode->i_private;
653 return 0;
654}
655
Matt Fleming7253eab2012-10-16 15:58:07 +0100656static int efi_status_to_err(efi_status_t status)
657{
658 int err;
659
660 switch (status) {
661 case EFI_INVALID_PARAMETER:
662 err = -EINVAL;
663 break;
664 case EFI_OUT_OF_RESOURCES:
665 err = -ENOSPC;
666 break;
667 case EFI_DEVICE_ERROR:
668 err = -EIO;
669 break;
670 case EFI_WRITE_PROTECTED:
671 err = -EROFS;
672 break;
673 case EFI_SECURITY_VIOLATION:
674 err = -EACCES;
675 break;
676 case EFI_NOT_FOUND:
Matt Fleming1fa7e692013-01-16 13:47:05 +0000677 err = -EIO;
Matt Fleming7253eab2012-10-16 15:58:07 +0100678 break;
679 default:
680 err = -EINVAL;
681 }
682
683 return err;
684}
685
Matthew Garrett5d9db882012-10-05 13:54:56 +0800686static ssize_t efivarfs_file_write(struct file *file,
687 const char __user *userbuf, size_t count, loff_t *ppos)
688{
689 struct efivar_entry *var = file->private_data;
690 struct efivars *efivars;
691 efi_status_t status;
692 void *data;
693 u32 attributes;
694 struct inode *inode = file->f_mapping->host;
Matt Fleming07b1c5b2012-10-23 12:35:43 +0100695 unsigned long datasize = count - sizeof(attributes);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800696 unsigned long newdatasize;
Matt Fleming89d16662012-11-09 21:02:56 +0000697 u64 storage_size, remaining_size, max_size;
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100698 ssize_t bytes = 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800699
700 if (count < sizeof(attributes))
701 return -EINVAL;
702
Matt Fleming89d16662012-11-09 21:02:56 +0000703 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
704 return -EFAULT;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800705
Matt Fleming89d16662012-11-09 21:02:56 +0000706 if (attributes & ~(EFI_VARIABLE_MASK))
707 return -EINVAL;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800708
709 efivars = var->efivars;
710
Matt Fleming89d16662012-11-09 21:02:56 +0000711 /*
712 * Ensure that the user can't allocate arbitrarily large
713 * amounts of memory. Pick a default size of 64K if
714 * QueryVariableInfo() isn't supported by the firmware.
715 */
716 spin_lock(&efivars->lock);
717
718 if (!efivars->ops->query_variable_info)
719 status = EFI_UNSUPPORTED;
720 else {
721 const struct efivar_operations *fops = efivars->ops;
722 status = fops->query_variable_info(attributes, &storage_size,
723 &remaining_size, &max_size);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800724 }
725
Matt Fleming89d16662012-11-09 21:02:56 +0000726 spin_unlock(&efivars->lock);
727
728 if (status != EFI_SUCCESS) {
729 if (status != EFI_UNSUPPORTED)
730 return efi_status_to_err(status);
731
732 remaining_size = 65536;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800733 }
734
Matt Fleming89d16662012-11-09 21:02:56 +0000735 if (datasize > remaining_size)
736 return -ENOSPC;
737
738 data = kmalloc(datasize, GFP_KERNEL);
739 if (!data)
740 return -ENOMEM;
741
Matthew Garrett5d9db882012-10-05 13:54:56 +0800742 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100743 bytes = -EFAULT;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800744 goto out;
745 }
746
747 if (validate_var(&var->var, data, datasize) == false) {
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100748 bytes = -EINVAL;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800749 goto out;
750 }
751
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800752 /*
753 * The lock here protects the get_variable call, the conditional
754 * set_variable call, and removal of the variable from the efivars
755 * list (in the case of an authenticated delete).
756 */
757 spin_lock(&efivars->lock);
758
Matthew Garrett5d9db882012-10-05 13:54:56 +0800759 status = efivars->ops->set_variable(var->var.VariableName,
760 &var->var.VendorGuid,
761 attributes, datasize,
762 data);
763
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800764 if (status != EFI_SUCCESS) {
765 spin_unlock(&efivars->lock);
766 kfree(data);
767
Matt Fleming7253eab2012-10-16 15:58:07 +0100768 return efi_status_to_err(status);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800769 }
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800770
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100771 bytes = count;
772
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800773 /*
774 * Writing to the variable may have caused a change in size (which
775 * could either be an append or an overwrite), or the variable to be
776 * deleted. Perform a GetVariable() so we can tell what actually
777 * happened.
778 */
779 newdatasize = 0;
780 status = efivars->ops->get_variable(var->var.VariableName,
781 &var->var.VendorGuid,
782 NULL, &newdatasize,
783 NULL);
784
785 if (status == EFI_BUFFER_TOO_SMALL) {
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800786 spin_unlock(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800787 mutex_lock(&inode->i_mutex);
788 i_size_write(inode, newdatasize + sizeof(attributes));
789 mutex_unlock(&inode->i_mutex);
790
791 } else if (status == EFI_NOT_FOUND) {
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800792 list_del(&var->list);
793 spin_unlock(&efivars->lock);
794 efivar_unregister(var);
795 drop_nlink(inode);
Matt Fleming791eb562013-01-16 21:55:36 +0000796 d_delete(file->f_dentry);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800797 dput(file->f_dentry);
798
799 } else {
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800800 spin_unlock(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800801 pr_warn("efivarfs: inconsistent EFI variable implementation? "
802 "status = %lx\n", status);
803 }
804
Matthew Garrett5d9db882012-10-05 13:54:56 +0800805out:
806 kfree(data);
807
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100808 return bytes;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800809}
810
811static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
812 size_t count, loff_t *ppos)
813{
814 struct efivar_entry *var = file->private_data;
815 struct efivars *efivars = var->efivars;
816 efi_status_t status;
817 unsigned long datasize = 0;
818 u32 attributes;
819 void *data;
Andy Whitcroftd142df02012-10-11 11:32:17 +0100820 ssize_t size = 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800821
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800822 spin_lock(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800823 status = efivars->ops->get_variable(var->var.VariableName,
824 &var->var.VendorGuid,
825 &attributes, &datasize, NULL);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800826 spin_unlock(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800827
828 if (status != EFI_BUFFER_TOO_SMALL)
Matt Fleming7253eab2012-10-16 15:58:07 +0100829 return efi_status_to_err(status);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800830
Matt Flemingd2923842012-10-22 15:23:29 +0100831 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800832
833 if (!data)
Matt Fleming7253eab2012-10-16 15:58:07 +0100834 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800835
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800836 spin_lock(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800837 status = efivars->ops->get_variable(var->var.VariableName,
838 &var->var.VendorGuid,
839 &attributes, &datasize,
Matt Flemingd2923842012-10-22 15:23:29 +0100840 (data + sizeof(attributes)));
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800841 spin_unlock(&efivars->lock);
842
Matt Fleming7253eab2012-10-16 15:58:07 +0100843 if (status != EFI_SUCCESS) {
844 size = efi_status_to_err(status);
Andy Whitcroftd142df02012-10-11 11:32:17 +0100845 goto out_free;
Matt Fleming7253eab2012-10-16 15:58:07 +0100846 }
Matthew Garrett5d9db882012-10-05 13:54:56 +0800847
Matt Flemingd2923842012-10-22 15:23:29 +0100848 memcpy(data, &attributes, sizeof(attributes));
Matthew Garrett5d9db882012-10-05 13:54:56 +0800849 size = simple_read_from_buffer(userbuf, count, ppos,
Matt Flemingd2923842012-10-22 15:23:29 +0100850 data, datasize + sizeof(attributes));
Andy Whitcroftd142df02012-10-11 11:32:17 +0100851out_free:
Matthew Garrett5d9db882012-10-05 13:54:56 +0800852 kfree(data);
853
854 return size;
855}
856
857static void efivarfs_evict_inode(struct inode *inode)
858{
859 clear_inode(inode);
860}
861
862static const struct super_operations efivarfs_ops = {
863 .statfs = simple_statfs,
864 .drop_inode = generic_delete_inode,
865 .evict_inode = efivarfs_evict_inode,
866 .show_options = generic_show_options,
867};
868
869static struct super_block *efivarfs_sb;
870
871static const struct inode_operations efivarfs_dir_inode_operations;
872
873static const struct file_operations efivarfs_file_operations = {
874 .open = efivarfs_file_open,
875 .read = efivarfs_file_read,
876 .write = efivarfs_file_write,
877 .llseek = no_llseek,
878};
879
880static struct inode *efivarfs_get_inode(struct super_block *sb,
881 const struct inode *dir, int mode, dev_t dev)
882{
883 struct inode *inode = new_inode(sb);
884
885 if (inode) {
886 inode->i_ino = get_next_ino();
Matthew Garrett5d9db882012-10-05 13:54:56 +0800887 inode->i_mode = mode;
888 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
889 switch (mode & S_IFMT) {
890 case S_IFREG:
891 inode->i_fop = &efivarfs_file_operations;
892 break;
893 case S_IFDIR:
894 inode->i_op = &efivarfs_dir_inode_operations;
895 inode->i_fop = &simple_dir_operations;
896 inc_nlink(inode);
897 break;
898 }
899 }
900 return inode;
901}
902
903static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
904{
905 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
906 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
907 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
908 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
909 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
910 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
911 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
912 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
913 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
914 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
915 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
916 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
917 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
918 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
919 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
920 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
921}
922
923static int efivarfs_create(struct inode *dir, struct dentry *dentry,
924 umode_t mode, bool excl)
925{
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100926 struct inode *inode;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800927 struct efivars *efivars = &__efivars;
928 struct efivar_entry *var;
929 int namelen, i = 0, err = 0;
930
Jeremy Kerr310ad752012-10-19 15:16:45 +0800931 /*
932 * We need a GUID, plus at least one letter for the variable name,
933 * plus the '-' separator
934 */
935 if (dentry->d_name.len < GUID_LEN + 2)
Matthew Garrett5d9db882012-10-05 13:54:56 +0800936 return -EINVAL;
937
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100938 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800939 if (!inode)
Matt Flemingaeeaa8d2012-10-23 12:41:03 +0100940 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800941
942 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100943 if (!var) {
944 err = -ENOMEM;
945 goto out;
946 }
Matthew Garrett5d9db882012-10-05 13:54:56 +0800947
Jeremy Kerr310ad752012-10-19 15:16:45 +0800948 /* length of the variable name itself: remove GUID and separator */
949 namelen = dentry->d_name.len - GUID_LEN - 1;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800950
951 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
952 &var->var.VendorGuid);
953
954 for (i = 0; i < namelen; i++)
955 var->var.VariableName[i] = dentry->d_name.name[i];
956
957 var->var.VariableName[i] = '\0';
958
959 inode->i_private = var;
960 var->efivars = efivars;
961 var->kobj.kset = efivars->kset;
962
963 err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
964 dentry->d_name.name);
965 if (err)
966 goto out;
967
968 kobject_uevent(&var->kobj, KOBJ_ADD);
969 spin_lock(&efivars->lock);
970 list_add(&var->list, &efivars->list);
971 spin_unlock(&efivars->lock);
972 d_instantiate(dentry, inode);
973 dget(dentry);
974out:
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100975 if (err) {
Matthew Garrett5d9db882012-10-05 13:54:56 +0800976 kfree(var);
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100977 iput(inode);
978 }
Matthew Garrett5d9db882012-10-05 13:54:56 +0800979 return err;
980}
981
982static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
983{
984 struct efivar_entry *var = dentry->d_inode->i_private;
985 struct efivars *efivars = var->efivars;
986 efi_status_t status;
987
988 spin_lock(&efivars->lock);
989
990 status = efivars->ops->set_variable(var->var.VariableName,
991 &var->var.VendorGuid,
992 0, 0, NULL);
993
994 if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
995 list_del(&var->list);
996 spin_unlock(&efivars->lock);
997 efivar_unregister(var);
Lingzhu Xiangde5fe952013-01-05 13:59:52 +0800998 drop_nlink(dentry->d_inode);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800999 dput(dentry);
1000 return 0;
1001 }
1002
1003 spin_unlock(&efivars->lock);
1004 return -EINVAL;
1005};
1006
Matt Fleminge83af1f2012-11-15 20:03:16 +00001007static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
Matthew Garrett5d9db882012-10-05 13:54:56 +08001008{
1009 struct inode *inode = NULL;
1010 struct dentry *root;
1011 struct efivar_entry *entry, *n;
1012 struct efivars *efivars = &__efivars;
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001013 char *name;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001014
1015 efivarfs_sb = sb;
1016
1017 sb->s_maxbytes = MAX_LFS_FILESIZE;
1018 sb->s_blocksize = PAGE_CACHE_SIZE;
1019 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
Matt Fleming91716322012-10-22 15:51:45 +01001020 sb->s_magic = EFIVARFS_MAGIC;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001021 sb->s_op = &efivarfs_ops;
1022 sb->s_time_gran = 1;
1023
1024 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +01001025 if (!inode)
1026 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001027 inode->i_op = &efivarfs_dir_inode_operations;
1028
1029 root = d_make_root(inode);
1030 sb->s_root = root;
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +01001031 if (!root)
1032 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001033
1034 list_for_each_entry_safe(entry, n, &efivars->list, list) {
Matthew Garrett5d9db882012-10-05 13:54:56 +08001035 struct dentry *dentry, *root = efivarfs_sb->s_root;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001036 unsigned long size = 0;
1037 int len, i;
1038
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001039 inode = NULL;
1040
Matthew Garrett5d9db882012-10-05 13:54:56 +08001041 len = utf16_strlen(entry->var.VariableName);
1042
Jeremy Kerr310ad752012-10-19 15:16:45 +08001043 /* name, plus '-', plus GUID, plus NUL*/
1044 name = kmalloc(len + 1 + GUID_LEN + 1, GFP_ATOMIC);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001045 if (!name)
1046 goto fail;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001047
1048 for (i = 0; i < len; i++)
1049 name[i] = entry->var.VariableName[i] & 0xFF;
1050
1051 name[len] = '-';
1052
1053 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
1054
Jeremy Kerr310ad752012-10-19 15:16:45 +08001055 name[len+GUID_LEN+1] = '\0';
Matthew Garrett5d9db882012-10-05 13:54:56 +08001056
1057 inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
1058 S_IFREG | 0644, 0);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001059 if (!inode)
1060 goto fail_name;
1061
Matthew Garrett5d9db882012-10-05 13:54:56 +08001062 dentry = d_alloc_name(root, name);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001063 if (!dentry)
1064 goto fail_inode;
1065
Andy Whitcroftc0359db2012-10-11 11:32:20 +01001066 /* copied by the above to local storage in the dentry. */
1067 kfree(name);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001068
Jeremy Kerrf5f6a602012-10-11 21:19:11 +08001069 spin_lock(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001070 efivars->ops->get_variable(entry->var.VariableName,
1071 &entry->var.VendorGuid,
1072 &entry->var.Attributes,
1073 &size,
1074 NULL);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +08001075 spin_unlock(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001076
1077 mutex_lock(&inode->i_mutex);
1078 inode->i_private = entry;
Matt Fleming94a193f2013-01-11 13:30:46 +00001079 i_size_write(inode, size + sizeof(entry->var.Attributes));
Matthew Garrett5d9db882012-10-05 13:54:56 +08001080 mutex_unlock(&inode->i_mutex);
1081 d_add(dentry, inode);
1082 }
1083
1084 return 0;
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001085
1086fail_inode:
1087 iput(inode);
1088fail_name:
1089 kfree(name);
1090fail:
1091 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001092}
1093
1094static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
1095 int flags, const char *dev_name, void *data)
1096{
1097 return mount_single(fs_type, flags, data, efivarfs_fill_super);
1098}
1099
1100static void efivarfs_kill_sb(struct super_block *sb)
1101{
1102 kill_litter_super(sb);
1103 efivarfs_sb = NULL;
1104}
1105
1106static struct file_system_type efivarfs_type = {
1107 .name = "efivarfs",
1108 .mount = efivarfs_mount,
1109 .kill_sb = efivarfs_kill_sb,
1110};
1111
1112static const struct inode_operations efivarfs_dir_inode_operations = {
1113 .lookup = simple_lookup,
1114 .unlink = efivarfs_unlink,
1115 .create = efivarfs_create,
1116};
1117
1118static struct pstore_info efi_pstore_info;
1119
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001120#ifdef CONFIG_PSTORE
1121
1122static int efi_pstore_open(struct pstore_info *psi)
1123{
1124 struct efivars *efivars = psi->data;
1125
1126 spin_lock(&efivars->lock);
1127 efivars->walk_entry = list_first_entry(&efivars->list,
1128 struct efivar_entry, list);
1129 return 0;
1130}
1131
1132static int efi_pstore_close(struct pstore_info *psi)
1133{
1134 struct efivars *efivars = psi->data;
1135
1136 spin_unlock(&efivars->lock);
1137 return 0;
1138}
1139
1140static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001141 int *count, struct timespec *timespec,
Kees Cookf6f82852011-11-17 12:58:07 -08001142 char **buf, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001143{
1144 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1145 struct efivars *efivars = psi->data;
1146 char name[DUMP_NAME_LEN];
1147 int i;
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001148 int cnt;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001149 unsigned int part, size;
1150 unsigned long time;
1151
1152 while (&efivars->walk_entry->list != &efivars->list) {
1153 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
1154 vendor)) {
1155 for (i = 0; i < DUMP_NAME_LEN; i++) {
1156 name[i] = efivars->walk_entry->var.VariableName[i];
1157 }
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001158 if (sscanf(name, "dump-type%u-%u-%d-%lu",
1159 type, &part, &cnt, &time) == 4) {
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001160 *id = part;
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001161 *count = cnt;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001162 timespec->tv_sec = time;
1163 timespec->tv_nsec = 0;
Seiji Aguchi0f7de852012-11-14 20:28:50 +00001164 } else if (sscanf(name, "dump-type%u-%u-%lu",
1165 type, &part, &time) == 3) {
1166 /*
1167 * Check if an old format,
1168 * which doesn't support holding
1169 * multiple logs, remains.
1170 */
1171 *id = part;
1172 *count = 0;
1173 timespec->tv_sec = time;
1174 timespec->tv_nsec = 0;
1175 } else {
1176 efivars->walk_entry = list_entry(
1177 efivars->walk_entry->list.next,
1178 struct efivar_entry, list);
1179 continue;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001180 }
Seiji Aguchi0f7de852012-11-14 20:28:50 +00001181
1182 get_var_data_locked(efivars, &efivars->walk_entry->var);
1183 size = efivars->walk_entry->var.DataSize;
1184 *buf = kmalloc(size, GFP_KERNEL);
1185 if (*buf == NULL)
1186 return -ENOMEM;
1187 memcpy(*buf, efivars->walk_entry->var.Data,
1188 size);
1189 efivars->walk_entry = list_entry(
1190 efivars->walk_entry->list.next,
1191 struct efivar_entry, list);
1192 return size;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001193 }
1194 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1195 struct efivar_entry, list);
1196 }
1197 return 0;
1198}
1199
Kees Cook3d6d8d22011-11-17 13:13:29 -08001200static int efi_pstore_write(enum pstore_type_id type,
1201 enum kmsg_dump_reason reason, u64 *id,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001202 unsigned int part, int count, size_t size,
1203 struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001204{
1205 char name[DUMP_NAME_LEN];
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001206 efi_char16_t efi_name[DUMP_NAME_LEN];
1207 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1208 struct efivars *efivars = psi->data;
Chen Gongb238b8f2011-10-12 09:17:24 -07001209 int i, ret = 0;
Seiji Aguchid80a3612012-11-14 20:25:37 +00001210 u64 storage_space, remaining_space, max_variable_size;
1211 efi_status_t status = EFI_NOT_FOUND;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001212
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001213 spin_lock(&efivars->lock);
1214
Seiji Aguchid80a3612012-11-14 20:25:37 +00001215 /*
1216 * Check if there is a space enough to log.
1217 * size: a size of logging data
1218 * DUMP_NAME_LEN * 2: a maximum size of variable name
1219 */
1220 status = efivars->ops->query_variable_info(PSTORE_EFI_ATTRIBUTES,
1221 &storage_space,
1222 &remaining_space,
1223 &max_variable_size);
1224 if (status || remaining_space < size + DUMP_NAME_LEN * 2) {
1225 spin_unlock(&efivars->lock);
1226 *id = part;
1227 return -ENOSPC;
1228 }
1229
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001230 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
Seiji Aguchi96480d92012-11-14 20:26:46 +00001231 get_seconds());
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001232
1233 for (i = 0; i < DUMP_NAME_LEN; i++)
1234 efi_name[i] = name[i];
1235
Mike Waychison7644c162011-07-21 16:58:00 -04001236 efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001237 size, psi->buf);
1238
1239 spin_unlock(&efivars->lock);
1240
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001241 if (size)
Chen Gongb238b8f2011-10-12 09:17:24 -07001242 ret = efivar_create_sysfs_entry(efivars,
Mike Waychisona2940902011-07-21 16:57:57 -04001243 utf16_strsize(efi_name,
1244 DUMP_NAME_LEN * 2),
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001245 efi_name, &vendor);
1246
Chen Gongb238b8f2011-10-12 09:17:24 -07001247 *id = part;
1248 return ret;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001249};
1250
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001251static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001252 struct timespec time, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001253{
Seiji Aguchia9efd392012-11-14 20:27:28 +00001254 char name[DUMP_NAME_LEN];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001255 efi_char16_t efi_name[DUMP_NAME_LEN];
Seiji Aguchif94ec0c2012-11-14 20:29:21 +00001256 char name_old[DUMP_NAME_LEN];
1257 efi_char16_t efi_name_old[DUMP_NAME_LEN];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001258 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1259 struct efivars *efivars = psi->data;
1260 struct efivar_entry *entry, *found = NULL;
1261 int i;
1262
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001263 sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001264 time.tv_sec);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001265
1266 spin_lock(&efivars->lock);
1267
1268 for (i = 0; i < DUMP_NAME_LEN; i++)
Seiji Aguchia9efd392012-11-14 20:27:28 +00001269 efi_name[i] = name[i];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001270
1271 /*
Seiji Aguchia9efd392012-11-14 20:27:28 +00001272 * Clean up an entry with the same name
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001273 */
1274
1275 list_for_each_entry(entry, &efivars->list, list) {
1276 get_var_data_locked(efivars, &entry->var);
1277
1278 if (efi_guidcmp(entry->var.VendorGuid, vendor))
1279 continue;
1280 if (utf16_strncmp(entry->var.VariableName, efi_name,
Seiji Aguchif94ec0c2012-11-14 20:29:21 +00001281 utf16_strlen(efi_name))) {
1282 /*
1283 * Check if an old format,
1284 * which doesn't support holding
1285 * multiple logs, remains.
1286 */
1287 sprintf(name_old, "dump-type%u-%u-%lu", type,
1288 (unsigned int)id, time.tv_sec);
1289
1290 for (i = 0; i < DUMP_NAME_LEN; i++)
1291 efi_name_old[i] = name_old[i];
1292
1293 if (utf16_strncmp(entry->var.VariableName, efi_name_old,
1294 utf16_strlen(efi_name_old)))
1295 continue;
1296 }
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001297
1298 /* found */
1299 found = entry;
1300 efivars->ops->set_variable(entry->var.VariableName,
1301 &entry->var.VendorGuid,
1302 PSTORE_EFI_ATTRIBUTES,
1303 0, NULL);
Seiji Aguchia9efd392012-11-14 20:27:28 +00001304 break;
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001305 }
1306
1307 if (found)
1308 list_del(&found->list);
1309
1310 spin_unlock(&efivars->lock);
1311
1312 if (found)
1313 efivar_unregister(found);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001314
1315 return 0;
1316}
1317#else
1318static int efi_pstore_open(struct pstore_info *psi)
1319{
1320 return 0;
1321}
1322
1323static int efi_pstore_close(struct pstore_info *psi)
1324{
1325 return 0;
1326}
1327
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001328static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type, int *count,
Christoph Fritzeee628d2011-11-28 23:49:33 +01001329 struct timespec *timespec,
1330 char **buf, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001331{
1332 return -1;
1333}
1334
Kees Cook3d6d8d22011-11-17 13:13:29 -08001335static int efi_pstore_write(enum pstore_type_id type,
1336 enum kmsg_dump_reason reason, u64 *id,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001337 unsigned int part, int count, size_t size,
1338 struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001339{
1340 return 0;
1341}
1342
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001343static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001344 struct timespec time, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001345{
1346 return 0;
1347}
1348#endif
1349
1350static struct pstore_info efi_pstore_info = {
1351 .owner = THIS_MODULE,
1352 .name = "efi",
1353 .open = efi_pstore_open,
1354 .close = efi_pstore_close,
1355 .read = efi_pstore_read,
1356 .write = efi_pstore_write,
1357 .erase = efi_pstore_erase,
1358};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Chris Wright2c3c8be2010-05-12 18:28:57 -07001360static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001361 struct bin_attribute *bin_attr,
1362 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 struct efi_variable *new_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001365 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001366 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 efi_status_t status = EFI_NOT_FOUND;
1369 int found = 0;
1370
1371 if (!capable(CAP_SYS_ADMIN))
1372 return -EACCES;
1373
Matthew Garrettfec6c202012-04-30 16:11:30 -04001374 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
1375 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
1376 printk(KERN_ERR "efivars: Malformed variable content\n");
1377 return -EINVAL;
1378 }
1379
Mike Waychison29422692011-03-11 17:43:00 -08001380 spin_lock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 /*
1383 * Does this variable already exist?
1384 */
Mike Waychison29422692011-03-11 17:43:00 -08001385 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -04001386 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1387 strsize2 = utf16_strsize(new_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (strsize1 == strsize2 &&
1389 !memcmp(&(search_efivar->var.VariableName),
1390 new_var->VariableName, strsize1) &&
1391 !efi_guidcmp(search_efivar->var.VendorGuid,
1392 new_var->VendorGuid)) {
1393 found = 1;
1394 break;
1395 }
1396 }
1397 if (found) {
Mike Waychison29422692011-03-11 17:43:00 -08001398 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return -EINVAL;
1400 }
1401
1402 /* now *really* create the variable via EFI */
Mike Waychison32958142011-03-11 17:43:21 -08001403 status = efivars->ops->set_variable(new_var->VariableName,
1404 &new_var->VendorGuid,
1405 new_var->Attributes,
1406 new_var->DataSize,
1407 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 if (status != EFI_SUCCESS) {
1410 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1411 status);
Mike Waychison29422692011-03-11 17:43:00 -08001412 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return -EIO;
1414 }
Mike Waychison29422692011-03-11 17:43:00 -08001415 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 /* Create the entry in sysfs. Locking is not required here */
Mike Waychison4142ef12011-03-11 17:43:11 -08001418 status = efivar_create_sysfs_entry(efivars,
Mike Waychisona2940902011-07-21 16:57:57 -04001419 utf16_strsize(new_var->VariableName,
1420 1024),
Mike Waychison4142ef12011-03-11 17:43:11 -08001421 new_var->VariableName,
1422 &new_var->VendorGuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (status) {
1424 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
1425 }
1426 return count;
1427}
1428
Chris Wright2c3c8be2010-05-12 18:28:57 -07001429static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001430 struct bin_attribute *bin_attr,
1431 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
1433 struct efi_variable *del_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001434 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001435 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 efi_status_t status = EFI_NOT_FOUND;
1438 int found = 0;
1439
1440 if (!capable(CAP_SYS_ADMIN))
1441 return -EACCES;
1442
Mike Waychison29422692011-03-11 17:43:00 -08001443 spin_lock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 /*
1446 * Does this variable already exist?
1447 */
Mike Waychison29422692011-03-11 17:43:00 -08001448 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -04001449 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1450 strsize2 = utf16_strsize(del_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (strsize1 == strsize2 &&
1452 !memcmp(&(search_efivar->var.VariableName),
1453 del_var->VariableName, strsize1) &&
1454 !efi_guidcmp(search_efivar->var.VendorGuid,
1455 del_var->VendorGuid)) {
1456 found = 1;
1457 break;
1458 }
1459 }
1460 if (!found) {
Mike Waychison29422692011-03-11 17:43:00 -08001461 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return -EINVAL;
1463 }
1464 /* force the Attributes/DataSize to 0 to ensure deletion */
1465 del_var->Attributes = 0;
1466 del_var->DataSize = 0;
1467
Mike Waychison32958142011-03-11 17:43:21 -08001468 status = efivars->ops->set_variable(del_var->VariableName,
1469 &del_var->VendorGuid,
1470 del_var->Attributes,
1471 del_var->DataSize,
1472 del_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 if (status != EFI_SUCCESS) {
1475 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1476 status);
Mike Waychison29422692011-03-11 17:43:00 -08001477 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return -EIO;
1479 }
Matt Domsch496a0fc2007-01-26 00:57:18 -08001480 list_del(&search_efivar->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /* We need to release this lock before unregistering. */
Mike Waychison29422692011-03-11 17:43:00 -08001482 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 efivar_unregister(search_efivar);
1484
1485 /* It's dead Jim.... */
1486 return count;
1487}
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489/*
1490 * Let's not leave out systab information that snuck into
1491 * the efivars driver
1492 */
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001493static ssize_t systab_show(struct kobject *kobj,
1494 struct kobj_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 char *str = buf;
1497
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001498 if (!kobj || !buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return -EINVAL;
1500
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -08001501 if (efi.mps != EFI_INVALID_TABLE_ADDR)
1502 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
1503 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
1504 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
1505 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
1506 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
1507 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
1508 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
1509 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
1510 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
1511 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
1512 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
1513 if (efi.uga != EFI_INVALID_TABLE_ADDR)
1514 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 return str - buf;
1517}
1518
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001519static struct kobj_attribute efi_attr_systab =
1520 __ATTR(systab, 0400, systab_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001522static struct attribute *efi_subsys_attrs[] = {
1523 &efi_attr_systab.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 NULL, /* maybe more in the future? */
1525};
1526
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001527static struct attribute_group efi_subsys_attr_group = {
1528 .attrs = efi_subsys_attrs,
1529};
1530
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08001531static struct kobject *efi_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533/*
1534 * efivar_create_sysfs_entry()
1535 * Requires:
1536 * variable_name_size = number of bytes required to hold
1537 * variable_name (not counting the NULL
1538 * character at the end.
Mike Waychison29422692011-03-11 17:43:00 -08001539 * efivars->lock is not held on entry or exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 * Returns 1 on failure, 0 on success
1541 */
1542static int
Mike Waychison4142ef12011-03-11 17:43:11 -08001543efivar_create_sysfs_entry(struct efivars *efivars,
1544 unsigned long variable_name_size,
1545 efi_char16_t *variable_name,
1546 efi_guid_t *vendor_guid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Jeremy Kerr310ad752012-10-19 15:16:45 +08001548 int i, short_name_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 char *short_name;
1550 struct efivar_entry *new_efivar;
1551
Jeremy Kerr310ad752012-10-19 15:16:45 +08001552 /*
1553 * Length of the variable bytes in ASCII, plus the '-' separator,
1554 * plus the GUID, plus trailing NUL
1555 */
1556 short_name_size = variable_name_size / sizeof(efi_char16_t)
1557 + 1 + GUID_LEN + 1;
1558
1559 short_name = kzalloc(short_name_size, GFP_KERNEL);
Deepak Saxena9c215382005-11-07 01:01:24 -08001560 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 if (!short_name || !new_efivar) {
Jesper Juhl0933ad92005-06-25 14:59:15 -07001563 kfree(short_name);
1564 kfree(new_efivar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return 1;
1566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Mike Waychison4142ef12011-03-11 17:43:11 -08001568 new_efivar->efivars = efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 memcpy(new_efivar->var.VariableName, variable_name,
1570 variable_name_size);
1571 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
1572
1573 /* Convert Unicode to normal chars (assume top bits are 0),
1574 ala UTF-8 */
1575 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
1576 short_name[i] = variable_name[i] & 0xFF;
1577 }
1578 /* This is ugly, but necessary to separate one vendor's
1579 private variables from another's. */
1580
1581 *(short_name + strlen(short_name)) = '-';
1582 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
1583
Mike Waychison29422692011-03-11 17:43:00 -08001584 new_efivar->kobj.kset = efivars->kset;
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001585 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
1586 "%s", short_name);
Jeff Garzik69b21862006-10-11 01:22:23 -07001587 if (i) {
1588 kfree(short_name);
1589 kfree(new_efivar);
1590 return 1;
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001593 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
Jesper Juhl0933ad92005-06-25 14:59:15 -07001594 kfree(short_name);
1595 short_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Mike Waychison29422692011-03-11 17:43:00 -08001597 spin_lock(&efivars->lock);
1598 list_add(&new_efivar->list, &efivars->list);
1599 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 return 0;
1602}
Mike Waychisond502fbb2011-03-11 17:43:06 -08001603
1604static int
1605create_efivars_bin_attributes(struct efivars *efivars)
1606{
1607 struct bin_attribute *attr;
1608 int error;
1609
1610 /* new_var */
1611 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1612 if (!attr)
1613 return -ENOMEM;
1614
1615 attr->attr.name = "new_var";
1616 attr->attr.mode = 0200;
1617 attr->write = efivar_create;
1618 attr->private = efivars;
1619 efivars->new_var = attr;
1620
1621 /* del_var */
1622 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1623 if (!attr) {
1624 error = -ENOMEM;
1625 goto out_free;
1626 }
1627 attr->attr.name = "del_var";
1628 attr->attr.mode = 0200;
1629 attr->write = efivar_delete;
1630 attr->private = efivars;
1631 efivars->del_var = attr;
1632
1633 sysfs_bin_attr_init(efivars->new_var);
1634 sysfs_bin_attr_init(efivars->del_var);
1635
1636 /* Register */
1637 error = sysfs_create_bin_file(&efivars->kset->kobj,
1638 efivars->new_var);
1639 if (error) {
1640 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
1641 " due to error %d\n", error);
1642 goto out_free;
1643 }
1644 error = sysfs_create_bin_file(&efivars->kset->kobj,
1645 efivars->del_var);
1646 if (error) {
1647 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
1648 " due to error %d\n", error);
1649 sysfs_remove_bin_file(&efivars->kset->kobj,
1650 efivars->new_var);
1651 goto out_free;
1652 }
1653
1654 return 0;
1655out_free:
Dan Carpenter051d51b2011-03-18 10:12:14 +03001656 kfree(efivars->del_var);
1657 efivars->del_var = NULL;
Mike Waychisond502fbb2011-03-11 17:43:06 -08001658 kfree(efivars->new_var);
1659 efivars->new_var = NULL;
1660 return error;
1661}
1662
Mike Waychison4fc756b2011-03-11 17:43:27 -08001663void unregister_efivars(struct efivars *efivars)
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001664{
1665 struct efivar_entry *entry, *n;
Mike Waychison4142ef12011-03-11 17:43:11 -08001666
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001667 list_for_each_entry_safe(entry, n, &efivars->list, list) {
1668 spin_lock(&efivars->lock);
1669 list_del(&entry->list);
1670 spin_unlock(&efivars->lock);
1671 efivar_unregister(entry);
1672 }
1673 if (efivars->new_var)
1674 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
1675 if (efivars->del_var)
1676 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
1677 kfree(efivars->new_var);
1678 kfree(efivars->del_var);
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08001679 kobject_put(efivars->kobject);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001680 kset_unregister(efivars->kset);
1681}
Mike Waychison4fc756b2011-03-11 17:43:27 -08001682EXPORT_SYMBOL_GPL(unregister_efivars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Mike Waychison4fc756b2011-03-11 17:43:27 -08001684int register_efivars(struct efivars *efivars,
1685 const struct efivar_operations *ops,
1686 struct kobject *parent_kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 efi_status_t status = EFI_NOT_FOUND;
1689 efi_guid_t vendor_guid;
1690 efi_char16_t *variable_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 unsigned long variable_name_size = 1024;
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001692 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Deepak Saxena9c215382005-11-07 01:01:24 -08001694 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (!variable_name) {
1696 printk(KERN_ERR "efivars: Memory allocation failed.\n");
1697 return -ENOMEM;
1698 }
1699
Mike Waychison29422692011-03-11 17:43:00 -08001700 spin_lock_init(&efivars->lock);
1701 INIT_LIST_HEAD(&efivars->list);
Mike Waychison32958142011-03-11 17:43:21 -08001702 efivars->ops = ops;
Mike Waychison29422692011-03-11 17:43:00 -08001703
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001704 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
Mike Waychison29422692011-03-11 17:43:00 -08001705 if (!efivars->kset) {
Greg Kroah-Hartman66ac8312007-11-02 13:20:40 -07001706 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
1707 error = -ENOMEM;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001708 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08001711 efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
1712 if (!efivars->kobject) {
1713 pr_err("efivars: Subsystem registration failed.\n");
1714 error = -ENOMEM;
1715 kset_unregister(efivars->kset);
1716 goto out;
1717 }
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 /*
1720 * Per EFI spec, the maximum storage allocated for both
1721 * the variable name and variable data is 1024 bytes.
1722 */
1723
1724 do {
1725 variable_name_size = 1024;
1726
Mike Waychison32958142011-03-11 17:43:21 -08001727 status = ops->get_next_variable(&variable_name_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 variable_name,
1729 &vendor_guid);
1730 switch (status) {
1731 case EFI_SUCCESS:
Mike Waychison4142ef12011-03-11 17:43:11 -08001732 efivar_create_sysfs_entry(efivars,
1733 variable_name_size,
1734 variable_name,
1735 &vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 break;
1737 case EFI_NOT_FOUND:
1738 break;
1739 default:
1740 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
1741 status);
1742 status = EFI_NOT_FOUND;
1743 break;
1744 }
1745 } while (status != EFI_NOT_FOUND);
1746
Mike Waychisond502fbb2011-03-11 17:43:06 -08001747 error = create_efivars_bin_attributes(efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001748 if (error)
1749 unregister_efivars(efivars);
1750
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001751 efivars->efi_pstore_info = efi_pstore_info;
1752
1753 efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
1754 if (efivars->efi_pstore_info.buf) {
1755 efivars->efi_pstore_info.bufsize = 1024;
1756 efivars->efi_pstore_info.data = efivars;
Don Zickusabd4d552011-08-12 10:54:51 -07001757 spin_lock_init(&efivars->efi_pstore_info.buf_lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001758 pstore_register(&efivars->efi_pstore_info);
1759 }
1760
Matthew Garrett5d9db882012-10-05 13:54:56 +08001761 register_filesystem(&efivarfs_type);
1762
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001763out:
1764 kfree(variable_name);
1765
1766 return error;
1767}
Mike Waychison4fc756b2011-03-11 17:43:27 -08001768EXPORT_SYMBOL_GPL(register_efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001769
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001770/*
1771 * For now we register the efi subsystem with the firmware subsystem
1772 * and the vars subsystem with the efi subsystem. In the future, it
1773 * might make sense to split off the efi subsystem into its own
1774 * driver, but for now only efivars will register with it, so just
1775 * include it here.
1776 */
1777
1778static int __init
1779efivars_init(void)
1780{
1781 int error = 0;
1782
1783 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
1784 EFIVARS_DATE);
1785
Matt Fleming83e68182012-11-14 09:42:35 +00001786 if (!efi_enabled(EFI_RUNTIME_SERVICES))
Mike Waychison4fc756b2011-03-11 17:43:27 -08001787 return 0;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001788
1789 /* For now we'll register the efi directory at /sys/firmware/efi */
1790 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
1791 if (!efi_kobj) {
1792 printk(KERN_ERR "efivars: Firmware registration failed.\n");
1793 return -ENOMEM;
1794 }
1795
Mike Waychison32958142011-03-11 17:43:21 -08001796 ops.get_variable = efi.get_variable;
1797 ops.set_variable = efi.set_variable;
1798 ops.get_next_variable = efi.get_next_variable;
Seiji Aguchid80a3612012-11-14 20:25:37 +00001799 ops.query_variable_info = efi.query_variable_info;
Matt Fleming89d16662012-11-09 21:02:56 +00001800
Mike Waychison32958142011-03-11 17:43:21 -08001801 error = register_efivars(&__efivars, &ops, efi_kobj);
Dan Carpenter3116aab2011-03-18 10:12:38 +03001802 if (error)
1803 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 /* Don't forget the systab entry */
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08001806 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001807 if (error) {
1808 printk(KERN_ERR
1809 "efivars: Sysfs attribute export failed with error %d.\n",
1810 error);
Dan Carpenter3116aab2011-03-18 10:12:38 +03001811 goto err_unregister;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Dan Carpenter3116aab2011-03-18 10:12:38 +03001814 return 0;
1815
1816err_unregister:
1817 unregister_efivars(&__efivars);
1818err_put:
1819 kobject_put(efi_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return error;
1821}
1822
1823static void __exit
1824efivars_exit(void)
1825{
Matt Fleming83e68182012-11-14 09:42:35 +00001826 if (efi_enabled(EFI_RUNTIME_SERVICES)) {
Randy Dunlapaabb6e12011-05-06 13:27:41 -07001827 unregister_efivars(&__efivars);
1828 kobject_put(efi_kobj);
1829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
1832module_init(efivars_init);
1833module_exit(efivars_exit);
1834