blob: c3a2f6f11748215cb9784110c186017c2e2653b1 [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
83#include <asm/uaccess.h>
84
85#define EFIVARS_VERSION "0.08"
86#define EFIVARS_DATE "2004-May-17"
87
88MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
89MODULE_DESCRIPTION("sysfs interface to EFI Variables");
90MODULE_LICENSE("GPL");
91MODULE_VERSION(EFIVARS_VERSION);
92
Matthew Garrett5ee9c192011-07-21 16:57:56 -040093#define DUMP_NAME_LEN 52
94
Seth Forshee026f0282013-03-11 16:17:50 -050095static bool efivars_pstore_disable =
Ben Hutchings9001ccb2013-03-22 19:56:51 +000096 IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
Seth Forshee026f0282013-03-11 16:17:50 -050097
98module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
99
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
116
117struct efivar_entry {
Mike Waychison4142ef12011-03-11 17:43:11 -0800118 struct efivars *efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct efi_variable var;
120 struct list_head list;
121 struct kobject kobj;
122};
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124struct efivar_attribute {
125 struct attribute attr;
126 ssize_t (*show) (struct efivar_entry *entry, char *buf);
127 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
128};
129
Matt Fleming4025b052013-03-07 11:59:14 +0000130static struct efivars __efivars;
131static struct efivar_operations ops;
132
Mike Waychison7644c162011-07-21 16:58:00 -0400133#define PSTORE_EFI_ATTRIBUTES \
134 (EFI_VARIABLE_NON_VOLATILE | \
135 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
136 EFI_VARIABLE_RUNTIME_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define EFIVAR_ATTR(_name, _mode, _show, _store) \
139struct efivar_attribute efivar_attr_##_name = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900140 .attr = {.name = __stringify(_name), .mode = _mode}, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .show = _show, \
142 .store = _store, \
143};
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
146#define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
147
148/*
149 * Prototype for sysfs creation function
150 */
151static int
Mike Waychison4142ef12011-03-11 17:43:11 -0800152efivar_create_sysfs_entry(struct efivars *efivars,
153 unsigned long variable_name_size,
154 efi_char16_t *variable_name,
155 efi_guid_t *vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157/* Return the number of unicode characters in data */
158static unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400159utf16_strnlen(efi_char16_t *s, size_t maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 unsigned long length = 0;
162
Mike Waychisona2940902011-07-21 16:57:57 -0400163 while (*s++ != 0 && length < maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 length++;
165 return length;
166}
167
Tony Luckb728a5c2011-08-02 15:08:30 -0700168static inline unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400169utf16_strlen(efi_char16_t *s)
170{
171 return utf16_strnlen(s, ~0UL);
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
175 * Return the number of bytes is the length of this string
176 * Note: this is NOT the same as the number of unicode characters
177 */
178static inline unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400179utf16_strsize(efi_char16_t *data, unsigned long maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Mike Waychisona2940902011-07-21 16:57:57 -0400181 return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Mike Waychison828aa1f2011-07-21 16:57:58 -0400184static inline int
185utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
186{
187 while (1) {
188 if (len == 0)
189 return 0;
190 if (*a < *b)
191 return -1;
192 if (*a > *b)
193 return 1;
194 if (*a == 0) /* implies *b == 0 */
195 return 0;
196 a++;
197 b++;
198 len--;
199 }
200}
201
Matthew Garrettfec6c202012-04-30 16:11:30 -0400202static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400203validate_device_path(struct efi_variable *var, int match, u8 *buffer,
204 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400205{
206 struct efi_generic_dev_path *node;
207 int offset = 0;
208
209 node = (struct efi_generic_dev_path *)buffer;
210
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400211 if (len < sizeof(*node))
212 return false;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400213
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400214 while (offset <= len - sizeof(*node) &&
215 node->length >= sizeof(*node) &&
216 node->length <= len - offset) {
217 offset += node->length;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400218
219 if ((node->type == EFI_DEV_END_PATH ||
220 node->type == EFI_DEV_END_PATH2) &&
221 node->sub_type == EFI_DEV_END_ENTIRE)
222 return true;
223
224 node = (struct efi_generic_dev_path *)(buffer + offset);
225 }
226
227 /*
228 * If we're here then either node->length pointed past the end
229 * of the buffer or we reached the end of the buffer without
230 * finding a device path end node.
231 */
232 return false;
233}
234
235static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400236validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
237 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400238{
239 /* An array of 16-bit integers */
240 if ((len % 2) != 0)
241 return false;
242
243 return true;
244}
245
246static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400247validate_load_option(struct efi_variable *var, int match, u8 *buffer,
248 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400249{
250 u16 filepathlength;
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400251 int i, desclength = 0, namelen;
252
253 namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
Matthew Garrettfec6c202012-04-30 16:11:30 -0400254
255 /* Either "Boot" or "Driver" followed by four digits of hex */
256 for (i = match; i < match+4; i++) {
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400257 if (var->VariableName[i] > 127 ||
258 hex_to_bin(var->VariableName[i] & 0xff) < 0)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400259 return true;
260 }
261
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400262 /* Reject it if there's 4 digits of hex and then further content */
263 if (namelen > match + 4)
264 return false;
265
266 /* A valid entry must be at least 8 bytes */
267 if (len < 8)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400268 return false;
269
270 filepathlength = buffer[4] | buffer[5] << 8;
271
272 /*
273 * There's no stored length for the description, so it has to be
274 * found by hand
275 */
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400276 desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400277
278 /* Each boot entry must have a descriptor */
279 if (!desclength)
280 return false;
281
282 /*
283 * If the sum of the length of the description, the claimed filepath
284 * length and the original header are greater than the length of the
285 * variable, it's malformed
286 */
287 if ((desclength + filepathlength + 6) > len)
288 return false;
289
290 /*
291 * And, finally, check the filepath
292 */
293 return validate_device_path(var, match, buffer + desclength + 6,
294 filepathlength);
295}
296
297static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400298validate_uint16(struct efi_variable *var, int match, u8 *buffer,
299 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400300{
301 /* A single 16-bit integer */
302 if (len != 2)
303 return false;
304
305 return true;
306}
307
308static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400309validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
310 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400311{
312 int i;
313
314 for (i = 0; i < len; i++) {
315 if (buffer[i] > 127)
316 return false;
317
318 if (buffer[i] == 0)
319 return true;
320 }
321
322 return false;
323}
324
325struct variable_validate {
326 char *name;
327 bool (*validate)(struct efi_variable *var, int match, u8 *data,
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400328 unsigned long len);
Matthew Garrettfec6c202012-04-30 16:11:30 -0400329};
330
331static const struct variable_validate variable_validate[] = {
332 { "BootNext", validate_uint16 },
333 { "BootOrder", validate_boot_order },
334 { "DriverOrder", validate_boot_order },
335 { "Boot*", validate_load_option },
336 { "Driver*", validate_load_option },
337 { "ConIn", validate_device_path },
338 { "ConInDev", validate_device_path },
339 { "ConOut", validate_device_path },
340 { "ConOutDev", validate_device_path },
341 { "ErrOut", validate_device_path },
342 { "ErrOutDev", validate_device_path },
343 { "Timeout", validate_uint16 },
344 { "Lang", validate_ascii_string },
345 { "PlatformLang", validate_ascii_string },
346 { "", NULL },
347};
348
349static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400350validate_var(struct efi_variable *var, u8 *data, unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400351{
352 int i;
353 u16 *unicode_name = var->VariableName;
354
355 for (i = 0; variable_validate[i].validate != NULL; i++) {
356 const char *name = variable_validate[i].name;
357 int match;
358
359 for (match = 0; ; match++) {
360 char c = name[match];
361 u16 u = unicode_name[match];
362
363 /* All special variables are plain ascii */
364 if (u > 127)
365 return true;
366
367 /* Wildcard in the matching name means we've matched */
368 if (c == '*')
369 return variable_validate[i].validate(var,
370 match, data, len);
371
372 /* Case sensitive match */
373 if (c != u)
374 break;
375
376 /* Reached the end of the string while matching */
377 if (!c)
378 return variable_validate[i].validate(var,
379 match, data, len);
380 }
381 }
382
383 return true;
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static efi_status_t
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400387get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 efi_status_t status;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 var->DataSize = 1024;
Mike Waychison32958142011-03-11 17:43:21 -0800392 status = efivars->ops->get_variable(var->VariableName,
393 &var->VendorGuid,
394 &var->Attributes,
395 &var->DataSize,
396 var->Data);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400397 return status;
398}
399
400static efi_status_t
401get_var_data(struct efivars *efivars, struct efi_variable *var)
402{
403 efi_status_t status;
Josh Boyerff534f02013-03-11 17:47:42 -0400404 unsigned long flags;
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400405
Josh Boyerff534f02013-03-11 17:47:42 -0400406 spin_lock_irqsave(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400407 status = get_var_data_locked(efivars, var);
Josh Boyerff534f02013-03-11 17:47:42 -0400408 spin_unlock_irqrestore(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (status != EFI_SUCCESS) {
411 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
412 status);
413 }
414 return status;
415}
416
Josh Boyere887bb42013-03-11 17:48:53 -0400417static efi_status_t
418check_var_size_locked(struct efivars *efivars, u32 attributes,
419 unsigned long size)
420{
421 u64 storage_size, remaining_size, max_size;
422 efi_status_t status;
423 const struct efivar_operations *fops = efivars->ops;
424
425 if (!efivars->ops->query_variable_info)
426 return EFI_UNSUPPORTED;
427
428 status = fops->query_variable_info(attributes, &storage_size,
429 &remaining_size, &max_size);
430
431 if (status != EFI_SUCCESS)
432 return status;
433
434 if (!storage_size || size > remaining_size || size > max_size ||
435 (remaining_size - size) < (storage_size / 2))
436 return EFI_OUT_OF_RESOURCES;
437
438 return status;
439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static ssize_t
442efivar_guid_read(struct efivar_entry *entry, char *buf)
443{
444 struct efi_variable *var = &entry->var;
445 char *str = buf;
446
447 if (!entry || !buf)
448 return 0;
449
450 efi_guid_unparse(&var->VendorGuid, str);
451 str += strlen(str);
452 str += sprintf(str, "\n");
453
454 return str - buf;
455}
456
457static ssize_t
458efivar_attr_read(struct efivar_entry *entry, char *buf)
459{
460 struct efi_variable *var = &entry->var;
461 char *str = buf;
462 efi_status_t status;
463
464 if (!entry || !buf)
465 return -EINVAL;
466
Mike Waychison4142ef12011-03-11 17:43:11 -0800467 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (status != EFI_SUCCESS)
469 return -EIO;
470
Khalid Azized020042012-09-10 12:52:42 -0600471 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
Khalid Azized020042012-09-10 12:52:42 -0600473 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
Khalid Azized020042012-09-10 12:52:42 -0600475 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
Khalid Azized020042012-09-10 12:52:42 -0600477 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
478 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
479 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
480 str += sprintf(str,
481 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
482 if (var->Attributes &
483 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
484 str += sprintf(str,
485 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
486 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
487 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return str - buf;
489}
490
491static ssize_t
492efivar_size_read(struct efivar_entry *entry, char *buf)
493{
494 struct efi_variable *var = &entry->var;
495 char *str = buf;
496 efi_status_t status;
497
498 if (!entry || !buf)
499 return -EINVAL;
500
Mike Waychison4142ef12011-03-11 17:43:11 -0800501 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (status != EFI_SUCCESS)
503 return -EIO;
504
505 str += sprintf(str, "0x%lx\n", var->DataSize);
506 return str - buf;
507}
508
509static ssize_t
510efivar_data_read(struct efivar_entry *entry, char *buf)
511{
512 struct efi_variable *var = &entry->var;
513 efi_status_t status;
514
515 if (!entry || !buf)
516 return -EINVAL;
517
Mike Waychison4142ef12011-03-11 17:43:11 -0800518 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (status != EFI_SUCCESS)
520 return -EIO;
521
522 memcpy(buf, var->Data, var->DataSize);
523 return var->DataSize;
524}
525/*
526 * We allow each variable to be edited via rewriting the
527 * entire efi variable structure.
528 */
529static ssize_t
530efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
531{
532 struct efi_variable *new_var, *var = &entry->var;
Mike Waychison4142ef12011-03-11 17:43:11 -0800533 struct efivars *efivars = entry->efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 efi_status_t status = EFI_NOT_FOUND;
535
536 if (count != sizeof(struct efi_variable))
537 return -EINVAL;
538
539 new_var = (struct efi_variable *)buf;
540 /*
541 * If only updating the variable data, then the name
542 * and guid should remain the same
543 */
544 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
545 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
546 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
547 return -EINVAL;
548 }
549
550 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
551 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
552 return -EINVAL;
553 }
554
Matthew Garrettfec6c202012-04-30 16:11:30 -0400555 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
556 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
557 printk(KERN_ERR "efivars: Malformed variable content\n");
558 return -EINVAL;
559 }
560
Josh Boyerff534f02013-03-11 17:47:42 -0400561 spin_lock_irq(&efivars->lock);
Josh Boyere887bb42013-03-11 17:48:53 -0400562
563 status = check_var_size_locked(efivars, new_var->Attributes,
564 new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
565
566 if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
567 status = efivars->ops->set_variable(new_var->VariableName,
568 &new_var->VendorGuid,
569 new_var->Attributes,
570 new_var->DataSize,
571 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Josh Boyerff534f02013-03-11 17:47:42 -0400573 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (status != EFI_SUCCESS) {
576 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
577 status);
578 return -EIO;
579 }
580
581 memcpy(&entry->var, new_var, count);
582 return count;
583}
584
585static ssize_t
586efivar_show_raw(struct efivar_entry *entry, char *buf)
587{
588 struct efi_variable *var = &entry->var;
589 efi_status_t status;
590
591 if (!entry || !buf)
592 return 0;
593
Mike Waychison4142ef12011-03-11 17:43:11 -0800594 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (status != EFI_SUCCESS)
596 return -EIO;
597
598 memcpy(buf, var, sizeof(*var));
599 return sizeof(*var);
600}
601
602/*
603 * Generic read/write functions that call the specific functions of
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200604 * the attributes...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 */
606static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
607 char *buf)
608{
609 struct efivar_entry *var = to_efivar_entry(kobj);
610 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500611 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 if (!capable(CAP_SYS_ADMIN))
614 return -EACCES;
615
616 if (efivar_attr->show) {
617 ret = efivar_attr->show(var, buf);
618 }
619 return ret;
620}
621
622static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
623 const char *buf, size_t count)
624{
625 struct efivar_entry *var = to_efivar_entry(kobj);
626 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500627 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (!capable(CAP_SYS_ADMIN))
630 return -EACCES;
631
632 if (efivar_attr->store)
633 ret = efivar_attr->store(var, buf, count);
634
635 return ret;
636}
637
Emese Revfy52cf25d2010-01-19 02:58:23 +0100638static const struct sysfs_ops efivar_attr_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 .show = efivar_attr_show,
640 .store = efivar_attr_store,
641};
642
643static void efivar_release(struct kobject *kobj)
644{
645 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 kfree(var);
647}
648
649static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
650static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
651static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
652static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
653static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
654
655static struct attribute *def_attrs[] = {
656 &efivar_attr_guid.attr,
657 &efivar_attr_size.attr,
658 &efivar_attr_attributes.attr,
659 &efivar_attr_data.attr,
660 &efivar_attr_raw_var.attr,
661 NULL,
662};
663
Greg Kroah-Hartmane89a4112007-10-11 10:47:49 -0600664static struct kobj_type efivar_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 .release = efivar_release,
666 .sysfs_ops = &efivar_attr_ops,
667 .default_attrs = def_attrs,
668};
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670static inline void
671efivar_unregister(struct efivar_entry *var)
672{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800673 kobject_put(&var->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Josh Boyere887bb42013-03-11 17:48:53 -0400676static int efi_status_to_err(efi_status_t status)
677{
678 int err;
679
680 switch (status) {
681 case EFI_INVALID_PARAMETER:
682 err = -EINVAL;
683 break;
684 case EFI_OUT_OF_RESOURCES:
685 err = -ENOSPC;
686 break;
687 case EFI_DEVICE_ERROR:
688 err = -EIO;
689 break;
690 case EFI_WRITE_PROTECTED:
691 err = -EROFS;
692 break;
693 case EFI_SECURITY_VIOLATION:
694 err = -EACCES;
695 break;
696 case EFI_NOT_FOUND:
697 err = -ENOENT;
698 break;
699 default:
700 err = -EINVAL;
701 }
702
703 return err;
704}
705
Seth Forsheef6a087a2013-03-07 11:40:17 -0600706#ifdef CONFIG_EFI_VARS_PSTORE
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400707
708static int efi_pstore_open(struct pstore_info *psi)
709{
710 struct efivars *efivars = psi->data;
711
Josh Boyerff534f02013-03-11 17:47:42 -0400712 spin_lock_irq(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400713 efivars->walk_entry = list_first_entry(&efivars->list,
714 struct efivar_entry, list);
715 return 0;
716}
717
718static int efi_pstore_close(struct pstore_info *psi)
719{
720 struct efivars *efivars = psi->data;
721
Josh Boyerff534f02013-03-11 17:47:42 -0400722 spin_unlock_irq(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400723 return 0;
724}
725
726static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
Kees Cookf6f82852011-11-17 12:58:07 -0800727 struct timespec *timespec,
728 char **buf, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400729{
730 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
731 struct efivars *efivars = psi->data;
732 char name[DUMP_NAME_LEN];
733 int i;
734 unsigned int part, size;
735 unsigned long time;
736
737 while (&efivars->walk_entry->list != &efivars->list) {
738 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
739 vendor)) {
740 for (i = 0; i < DUMP_NAME_LEN; i++) {
741 name[i] = efivars->walk_entry->var.VariableName[i];
742 }
743 if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
744 *id = part;
745 timespec->tv_sec = time;
746 timespec->tv_nsec = 0;
747 get_var_data_locked(efivars, &efivars->walk_entry->var);
748 size = efivars->walk_entry->var.DataSize;
Kees Cookf6f82852011-11-17 12:58:07 -0800749 *buf = kmalloc(size, GFP_KERNEL);
750 if (*buf == NULL)
751 return -ENOMEM;
752 memcpy(*buf, efivars->walk_entry->var.Data,
753 size);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400754 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
755 struct efivar_entry, list);
756 return size;
757 }
758 }
759 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
760 struct efivar_entry, list);
761 }
762 return 0;
763}
764
Kees Cook3d6d8d22011-11-17 13:13:29 -0800765static int efi_pstore_write(enum pstore_type_id type,
766 enum kmsg_dump_reason reason, u64 *id,
Chen Gongb238b8f2011-10-12 09:17:24 -0700767 unsigned int part, size_t size, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400768{
769 char name[DUMP_NAME_LEN];
770 char stub_name[DUMP_NAME_LEN];
771 efi_char16_t efi_name[DUMP_NAME_LEN];
772 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
773 struct efivars *efivars = psi->data;
774 struct efivar_entry *entry, *found = NULL;
Chen Gongb238b8f2011-10-12 09:17:24 -0700775 int i, ret = 0;
Seiji Aguchi8e74ecf2012-11-14 20:25:37 +0000776 efi_status_t status = EFI_NOT_FOUND;
Josh Boyerff534f02013-03-11 17:47:42 -0400777 unsigned long flags;
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400778
779 sprintf(stub_name, "dump-type%u-%u-", type, part);
780 sprintf(name, "%s%lu", stub_name, get_seconds());
781
Josh Boyerff534f02013-03-11 17:47:42 -0400782 spin_lock_irqsave(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400783
Ben Hutchings1c4e6e92013-03-23 03:49:53 +0000784 if (size) {
785 /*
786 * Check if there is a space enough to log.
787 * size: a size of logging data
788 * DUMP_NAME_LEN * 2: a maximum size of variable name
789 */
Josh Boyere887bb42013-03-11 17:48:53 -0400790
Ben Hutchings1c4e6e92013-03-23 03:49:53 +0000791 status = check_var_size_locked(efivars, PSTORE_EFI_ATTRIBUTES,
792 size + DUMP_NAME_LEN * 2);
Josh Boyere887bb42013-03-11 17:48:53 -0400793
Ben Hutchings1c4e6e92013-03-23 03:49:53 +0000794 if (status) {
795 spin_unlock_irqrestore(&efivars->lock, flags);
796 *id = part;
797 return -ENOSPC;
798 }
Seiji Aguchi8e74ecf2012-11-14 20:25:37 +0000799 }
800
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400801 for (i = 0; i < DUMP_NAME_LEN; i++)
802 efi_name[i] = stub_name[i];
803
804 /*
805 * Clean up any entries with the same name
806 */
807
808 list_for_each_entry(entry, &efivars->list, list) {
809 get_var_data_locked(efivars, &entry->var);
810
Mike Waychisonc4755942011-07-21 16:57:59 -0400811 if (efi_guidcmp(entry->var.VendorGuid, vendor))
812 continue;
813 if (utf16_strncmp(entry->var.VariableName, efi_name,
814 utf16_strlen(efi_name)))
815 continue;
816 /* Needs to be a prefix */
817 if (entry->var.VariableName[utf16_strlen(efi_name)] == 0)
818 continue;
819
820 /* found */
821 found = entry;
Mike Waychison7644c162011-07-21 16:58:00 -0400822 efivars->ops->set_variable(entry->var.VariableName,
823 &entry->var.VendorGuid,
824 PSTORE_EFI_ATTRIBUTES,
Mike Waychisonc4755942011-07-21 16:57:59 -0400825 0, NULL);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400826 }
827
828 if (found)
829 list_del(&found->list);
830
831 for (i = 0; i < DUMP_NAME_LEN; i++)
832 efi_name[i] = name[i];
833
Mike Waychison7644c162011-07-21 16:58:00 -0400834 efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400835 size, psi->buf);
836
Josh Boyerff534f02013-03-11 17:47:42 -0400837 spin_unlock_irqrestore(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400838
839 if (found)
840 efivar_unregister(found);
841
842 if (size)
Chen Gongb238b8f2011-10-12 09:17:24 -0700843 ret = efivar_create_sysfs_entry(efivars,
Mike Waychisona2940902011-07-21 16:57:57 -0400844 utf16_strsize(efi_name,
845 DUMP_NAME_LEN * 2),
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400846 efi_name, &vendor);
847
Chen Gongb238b8f2011-10-12 09:17:24 -0700848 *id = part;
849 return ret;
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400850};
851
852static int efi_pstore_erase(enum pstore_type_id type, u64 id,
853 struct pstore_info *psi)
854{
Kees Cook3d6d8d22011-11-17 13:13:29 -0800855 efi_pstore_write(type, 0, &id, (unsigned int)id, 0, psi);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400856
857 return 0;
858}
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400859
860static struct pstore_info efi_pstore_info = {
861 .owner = THIS_MODULE,
862 .name = "efi",
863 .open = efi_pstore_open,
864 .close = efi_pstore_close,
865 .read = efi_pstore_read,
866 .write = efi_pstore_write,
867 .erase = efi_pstore_erase,
868};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Seth Forsheef6a087a2013-03-07 11:40:17 -0600870static void efivar_pstore_register(struct efivars *efivars)
871{
872 efivars->efi_pstore_info = efi_pstore_info;
873 efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
874 if (efivars->efi_pstore_info.buf) {
875 efivars->efi_pstore_info.bufsize = 1024;
876 efivars->efi_pstore_info.data = efivars;
877 spin_lock_init(&efivars->efi_pstore_info.buf_lock);
878 pstore_register(&efivars->efi_pstore_info);
879 }
880}
881#else
882static void efivar_pstore_register(struct efivars *efivars)
883{
884 return;
885}
886#endif
887
Chris Wright2c3c8be2010-05-12 18:28:57 -0700888static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -0800889 struct bin_attribute *bin_attr,
890 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 struct efi_variable *new_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -0800893 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -0800894 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 efi_status_t status = EFI_NOT_FOUND;
897 int found = 0;
898
899 if (!capable(CAP_SYS_ADMIN))
900 return -EACCES;
901
Matthew Garrettfec6c202012-04-30 16:11:30 -0400902 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
903 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
904 printk(KERN_ERR "efivars: Malformed variable content\n");
905 return -EINVAL;
906 }
907
Josh Boyerff534f02013-03-11 17:47:42 -0400908 spin_lock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 /*
911 * Does this variable already exist?
912 */
Mike Waychison29422692011-03-11 17:43:00 -0800913 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -0400914 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
915 strsize2 = utf16_strsize(new_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (strsize1 == strsize2 &&
917 !memcmp(&(search_efivar->var.VariableName),
918 new_var->VariableName, strsize1) &&
919 !efi_guidcmp(search_efivar->var.VendorGuid,
920 new_var->VendorGuid)) {
921 found = 1;
922 break;
923 }
924 }
925 if (found) {
Josh Boyerff534f02013-03-11 17:47:42 -0400926 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return -EINVAL;
928 }
929
Josh Boyere887bb42013-03-11 17:48:53 -0400930 status = check_var_size_locked(efivars, new_var->Attributes,
931 new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
932
933 if (status && status != EFI_UNSUPPORTED) {
934 spin_unlock_irq(&efivars->lock);
935 return efi_status_to_err(status);
936 }
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /* now *really* create the variable via EFI */
Mike Waychison32958142011-03-11 17:43:21 -0800939 status = efivars->ops->set_variable(new_var->VariableName,
940 &new_var->VendorGuid,
941 new_var->Attributes,
942 new_var->DataSize,
943 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 if (status != EFI_SUCCESS) {
946 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
947 status);
Josh Boyerff534f02013-03-11 17:47:42 -0400948 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return -EIO;
950 }
Josh Boyerff534f02013-03-11 17:47:42 -0400951 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 /* Create the entry in sysfs. Locking is not required here */
Mike Waychison4142ef12011-03-11 17:43:11 -0800954 status = efivar_create_sysfs_entry(efivars,
Mike Waychisona2940902011-07-21 16:57:57 -0400955 utf16_strsize(new_var->VariableName,
956 1024),
Mike Waychison4142ef12011-03-11 17:43:11 -0800957 new_var->VariableName,
958 &new_var->VendorGuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (status) {
960 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
961 }
962 return count;
963}
964
Chris Wright2c3c8be2010-05-12 18:28:57 -0700965static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -0800966 struct bin_attribute *bin_attr,
967 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 struct efi_variable *del_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -0800970 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -0800971 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 efi_status_t status = EFI_NOT_FOUND;
974 int found = 0;
975
976 if (!capable(CAP_SYS_ADMIN))
977 return -EACCES;
978
Josh Boyerff534f02013-03-11 17:47:42 -0400979 spin_lock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 /*
982 * Does this variable already exist?
983 */
Mike Waychison29422692011-03-11 17:43:00 -0800984 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -0400985 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
986 strsize2 = utf16_strsize(del_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (strsize1 == strsize2 &&
988 !memcmp(&(search_efivar->var.VariableName),
989 del_var->VariableName, strsize1) &&
990 !efi_guidcmp(search_efivar->var.VendorGuid,
991 del_var->VendorGuid)) {
992 found = 1;
993 break;
994 }
995 }
996 if (!found) {
Josh Boyerff534f02013-03-11 17:47:42 -0400997 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return -EINVAL;
999 }
1000 /* force the Attributes/DataSize to 0 to ensure deletion */
1001 del_var->Attributes = 0;
1002 del_var->DataSize = 0;
1003
Mike Waychison32958142011-03-11 17:43:21 -08001004 status = efivars->ops->set_variable(del_var->VariableName,
1005 &del_var->VendorGuid,
1006 del_var->Attributes,
1007 del_var->DataSize,
1008 del_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 if (status != EFI_SUCCESS) {
1011 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1012 status);
Josh Boyerff534f02013-03-11 17:47:42 -04001013 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return -EIO;
1015 }
Matt Domsch496a0fc2007-01-26 00:57:18 -08001016 list_del(&search_efivar->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 /* We need to release this lock before unregistering. */
Josh Boyerff534f02013-03-11 17:47:42 -04001018 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 efivar_unregister(search_efivar);
1020
1021 /* It's dead Jim.... */
1022 return count;
1023}
1024
Matt Fleming4025b052013-03-07 11:59:14 +00001025static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor)
1026{
1027 struct efivar_entry *entry, *n;
1028 struct efivars *efivars = &__efivars;
1029 unsigned long strsize1, strsize2;
1030 bool found = false;
1031
1032 strsize1 = utf16_strsize(variable_name, 1024);
1033 list_for_each_entry_safe(entry, n, &efivars->list, list) {
1034 strsize2 = utf16_strsize(entry->var.VariableName, 1024);
1035 if (strsize1 == strsize2 &&
1036 !memcmp(variable_name, &(entry->var.VariableName),
1037 strsize2) &&
1038 !efi_guidcmp(entry->var.VendorGuid,
1039 *vendor)) {
1040 found = true;
1041 break;
1042 }
1043 }
1044 return found;
1045}
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047/*
Matt Fleming5c44ddd2013-03-01 14:49:12 +00001048 * Returns the size of variable_name, in bytes, including the
1049 * terminating NULL character, or variable_name_size if no NULL
1050 * character is found among the first variable_name_size bytes.
1051 */
1052static unsigned long var_name_strnsize(efi_char16_t *variable_name,
1053 unsigned long variable_name_size)
1054{
1055 unsigned long len;
1056 efi_char16_t c;
1057
1058 /*
1059 * The variable name is, by definition, a NULL-terminated
1060 * string, so make absolutely sure that variable_name_size is
1061 * the value we expect it to be. If not, return the real size.
1062 */
1063 for (len = 2; len <= variable_name_size; len += sizeof(c)) {
1064 c = variable_name[(len / sizeof(c)) - 1];
1065 if (!c)
1066 break;
1067 }
1068
1069 return min(len, variable_name_size);
1070}
1071
1072/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 * Let's not leave out systab information that snuck into
1074 * the efivars driver
1075 */
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001076static ssize_t systab_show(struct kobject *kobj,
1077 struct kobj_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
1079 char *str = buf;
1080
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001081 if (!kobj || !buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return -EINVAL;
1083
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -08001084 if (efi.mps != EFI_INVALID_TABLE_ADDR)
1085 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
1086 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
1087 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
1088 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
1089 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
1090 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
1091 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
1092 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
1093 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
1094 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
1095 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
1096 if (efi.uga != EFI_INVALID_TABLE_ADDR)
1097 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 return str - buf;
1100}
1101
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001102static struct kobj_attribute efi_attr_systab =
1103 __ATTR(systab, 0400, systab_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001105static struct attribute *efi_subsys_attrs[] = {
1106 &efi_attr_systab.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 NULL, /* maybe more in the future? */
1108};
1109
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001110static struct attribute_group efi_subsys_attr_group = {
1111 .attrs = efi_subsys_attrs,
1112};
1113
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08001114static struct kobject *efi_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116/*
1117 * efivar_create_sysfs_entry()
1118 * Requires:
1119 * variable_name_size = number of bytes required to hold
1120 * variable_name (not counting the NULL
1121 * character at the end.
Mike Waychison29422692011-03-11 17:43:00 -08001122 * efivars->lock is not held on entry or exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 * Returns 1 on failure, 0 on success
1124 */
1125static int
Mike Waychison4142ef12011-03-11 17:43:11 -08001126efivar_create_sysfs_entry(struct efivars *efivars,
1127 unsigned long variable_name_size,
1128 efi_char16_t *variable_name,
1129 efi_guid_t *vendor_guid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
1132 char *short_name;
1133 struct efivar_entry *new_efivar;
1134
Deepak Saxena9c215382005-11-07 01:01:24 -08001135 short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
1136 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 if (!short_name || !new_efivar) {
Jesper Juhl0933ad92005-06-25 14:59:15 -07001139 kfree(short_name);
1140 kfree(new_efivar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return 1;
1142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Mike Waychison4142ef12011-03-11 17:43:11 -08001144 new_efivar->efivars = efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 memcpy(new_efivar->var.VariableName, variable_name,
1146 variable_name_size);
1147 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
1148
1149 /* Convert Unicode to normal chars (assume top bits are 0),
1150 ala UTF-8 */
1151 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
1152 short_name[i] = variable_name[i] & 0xFF;
1153 }
1154 /* This is ugly, but necessary to separate one vendor's
1155 private variables from another's. */
1156
1157 *(short_name + strlen(short_name)) = '-';
1158 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
1159
Mike Waychison29422692011-03-11 17:43:00 -08001160 new_efivar->kobj.kset = efivars->kset;
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001161 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
1162 "%s", short_name);
Jeff Garzik69b21862006-10-11 01:22:23 -07001163 if (i) {
1164 kfree(short_name);
1165 kfree(new_efivar);
1166 return 1;
1167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001169 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
Jesper Juhl0933ad92005-06-25 14:59:15 -07001170 kfree(short_name);
1171 short_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Josh Boyerff534f02013-03-11 17:47:42 -04001173 spin_lock_irq(&efivars->lock);
Mike Waychison29422692011-03-11 17:43:00 -08001174 list_add(&new_efivar->list, &efivars->list);
Josh Boyerff534f02013-03-11 17:47:42 -04001175 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 return 0;
1178}
Mike Waychisond502fbb2011-03-11 17:43:06 -08001179
1180static int
1181create_efivars_bin_attributes(struct efivars *efivars)
1182{
1183 struct bin_attribute *attr;
1184 int error;
1185
1186 /* new_var */
1187 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1188 if (!attr)
1189 return -ENOMEM;
1190
1191 attr->attr.name = "new_var";
1192 attr->attr.mode = 0200;
1193 attr->write = efivar_create;
1194 attr->private = efivars;
1195 efivars->new_var = attr;
1196
1197 /* del_var */
1198 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1199 if (!attr) {
1200 error = -ENOMEM;
1201 goto out_free;
1202 }
1203 attr->attr.name = "del_var";
1204 attr->attr.mode = 0200;
1205 attr->write = efivar_delete;
1206 attr->private = efivars;
1207 efivars->del_var = attr;
1208
1209 sysfs_bin_attr_init(efivars->new_var);
1210 sysfs_bin_attr_init(efivars->del_var);
1211
1212 /* Register */
1213 error = sysfs_create_bin_file(&efivars->kset->kobj,
1214 efivars->new_var);
1215 if (error) {
1216 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
1217 " due to error %d\n", error);
1218 goto out_free;
1219 }
1220 error = sysfs_create_bin_file(&efivars->kset->kobj,
1221 efivars->del_var);
1222 if (error) {
1223 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
1224 " due to error %d\n", error);
1225 sysfs_remove_bin_file(&efivars->kset->kobj,
1226 efivars->new_var);
1227 goto out_free;
1228 }
1229
1230 return 0;
1231out_free:
Dan Carpenter051d51b2011-03-18 10:12:14 +03001232 kfree(efivars->del_var);
1233 efivars->del_var = NULL;
Mike Waychisond502fbb2011-03-11 17:43:06 -08001234 kfree(efivars->new_var);
1235 efivars->new_var = NULL;
1236 return error;
1237}
1238
Mike Waychison4fc756b2011-03-11 17:43:27 -08001239void unregister_efivars(struct efivars *efivars)
Mike Waychison76b53f72011-03-11 17:43:16 -08001240{
1241 struct efivar_entry *entry, *n;
Mike Waychison4142ef12011-03-11 17:43:11 -08001242
Mike Waychison76b53f72011-03-11 17:43:16 -08001243 list_for_each_entry_safe(entry, n, &efivars->list, list) {
Josh Boyerff534f02013-03-11 17:47:42 -04001244 spin_lock_irq(&efivars->lock);
Mike Waychison76b53f72011-03-11 17:43:16 -08001245 list_del(&entry->list);
Josh Boyerff534f02013-03-11 17:47:42 -04001246 spin_unlock_irq(&efivars->lock);
Mike Waychison76b53f72011-03-11 17:43:16 -08001247 efivar_unregister(entry);
1248 }
1249 if (efivars->new_var)
1250 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
1251 if (efivars->del_var)
1252 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
1253 kfree(efivars->new_var);
1254 kfree(efivars->del_var);
1255 kset_unregister(efivars->kset);
1256}
Mike Waychison4fc756b2011-03-11 17:43:27 -08001257EXPORT_SYMBOL_GPL(unregister_efivars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Matt Fleming4025b052013-03-07 11:59:14 +00001259/*
1260 * Print a warning when duplicate EFI variables are encountered and
1261 * disable the sysfs workqueue since the firmware is buggy.
1262 */
1263static void dup_variable_bug(efi_char16_t *s16, efi_guid_t *vendor_guid,
1264 unsigned long len16)
1265{
1266 size_t i, len8 = len16 / sizeof(efi_char16_t);
1267 char *s8;
1268
1269 s8 = kzalloc(len8, GFP_KERNEL);
1270 if (!s8)
1271 return;
1272
1273 for (i = 0; i < len8; i++)
1274 s8[i] = s16[i];
1275
1276 printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
1277 s8, vendor_guid);
1278 kfree(s8);
1279}
1280
Mike Waychison4fc756b2011-03-11 17:43:27 -08001281int register_efivars(struct efivars *efivars,
1282 const struct efivar_operations *ops,
1283 struct kobject *parent_kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 efi_status_t status = EFI_NOT_FOUND;
1286 efi_guid_t vendor_guid;
1287 efi_char16_t *variable_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 unsigned long variable_name_size = 1024;
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001289 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Deepak Saxena9c215382005-11-07 01:01:24 -08001291 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (!variable_name) {
1293 printk(KERN_ERR "efivars: Memory allocation failed.\n");
1294 return -ENOMEM;
1295 }
1296
Mike Waychison29422692011-03-11 17:43:00 -08001297 spin_lock_init(&efivars->lock);
1298 INIT_LIST_HEAD(&efivars->list);
Mike Waychison32958142011-03-11 17:43:21 -08001299 efivars->ops = ops;
Mike Waychison29422692011-03-11 17:43:00 -08001300
Mike Waychison76b53f72011-03-11 17:43:16 -08001301 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
Mike Waychison29422692011-03-11 17:43:00 -08001302 if (!efivars->kset) {
Greg Kroah-Hartman66ac8312007-11-02 13:20:40 -07001303 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
1304 error = -ENOMEM;
Mike Waychison76b53f72011-03-11 17:43:16 -08001305 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
1308 /*
1309 * Per EFI spec, the maximum storage allocated for both
1310 * the variable name and variable data is 1024 bytes.
1311 */
1312
1313 do {
1314 variable_name_size = 1024;
1315
Mike Waychison32958142011-03-11 17:43:21 -08001316 status = ops->get_next_variable(&variable_name_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 variable_name,
1318 &vendor_guid);
1319 switch (status) {
1320 case EFI_SUCCESS:
Matt Fleming5c44ddd2013-03-01 14:49:12 +00001321 variable_name_size = var_name_strnsize(variable_name,
1322 variable_name_size);
Matt Fleming4025b052013-03-07 11:59:14 +00001323
1324 /*
1325 * Some firmware implementations return the
1326 * same variable name on multiple calls to
1327 * get_next_variable(). Terminate the loop
1328 * immediately as there is no guarantee that
1329 * we'll ever see a different variable name,
1330 * and may end up looping here forever.
1331 */
1332 if (variable_is_present(variable_name, &vendor_guid)) {
1333 dup_variable_bug(variable_name, &vendor_guid,
1334 variable_name_size);
1335 status = EFI_NOT_FOUND;
1336 break;
1337 }
1338
Mike Waychison4142ef12011-03-11 17:43:11 -08001339 efivar_create_sysfs_entry(efivars,
1340 variable_name_size,
1341 variable_name,
1342 &vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 break;
1344 case EFI_NOT_FOUND:
1345 break;
1346 default:
1347 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
1348 status);
1349 status = EFI_NOT_FOUND;
1350 break;
1351 }
1352 } while (status != EFI_NOT_FOUND);
1353
Mike Waychisond502fbb2011-03-11 17:43:06 -08001354 error = create_efivars_bin_attributes(efivars);
Mike Waychison76b53f72011-03-11 17:43:16 -08001355 if (error)
1356 unregister_efivars(efivars);
1357
Seth Forshee026f0282013-03-11 16:17:50 -05001358 if (!efivars_pstore_disable)
1359 efivar_pstore_register(efivars);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001360
Mike Waychison76b53f72011-03-11 17:43:16 -08001361out:
1362 kfree(variable_name);
1363
1364 return error;
1365}
Mike Waychison4fc756b2011-03-11 17:43:27 -08001366EXPORT_SYMBOL_GPL(register_efivars);
Mike Waychison76b53f72011-03-11 17:43:16 -08001367
Mike Waychison76b53f72011-03-11 17:43:16 -08001368/*
1369 * For now we register the efi subsystem with the firmware subsystem
1370 * and the vars subsystem with the efi subsystem. In the future, it
1371 * might make sense to split off the efi subsystem into its own
1372 * driver, but for now only efivars will register with it, so just
1373 * include it here.
1374 */
1375
1376static int __init
1377efivars_init(void)
1378{
1379 int error = 0;
1380
1381 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
1382 EFIVARS_DATE);
1383
Matt Fleming73923012012-11-14 09:42:35 +00001384 if (!efi_enabled(EFI_RUNTIME_SERVICES))
Mike Waychison4fc756b2011-03-11 17:43:27 -08001385 return 0;
Mike Waychison76b53f72011-03-11 17:43:16 -08001386
1387 /* For now we'll register the efi directory at /sys/firmware/efi */
1388 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
1389 if (!efi_kobj) {
1390 printk(KERN_ERR "efivars: Firmware registration failed.\n");
1391 return -ENOMEM;
1392 }
1393
Mike Waychison32958142011-03-11 17:43:21 -08001394 ops.get_variable = efi.get_variable;
1395 ops.set_variable = efi.set_variable;
1396 ops.get_next_variable = efi.get_next_variable;
Seiji Aguchi8e74ecf2012-11-14 20:25:37 +00001397 ops.query_variable_info = efi.query_variable_info;
Mike Waychison32958142011-03-11 17:43:21 -08001398 error = register_efivars(&__efivars, &ops, efi_kobj);
Dan Carpenter3116aab2011-03-18 10:12:38 +03001399 if (error)
1400 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 /* Don't forget the systab entry */
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08001403 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
Mike Waychison76b53f72011-03-11 17:43:16 -08001404 if (error) {
1405 printk(KERN_ERR
1406 "efivars: Sysfs attribute export failed with error %d.\n",
1407 error);
Dan Carpenter3116aab2011-03-18 10:12:38 +03001408 goto err_unregister;
Mike Waychison76b53f72011-03-11 17:43:16 -08001409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Dan Carpenter3116aab2011-03-18 10:12:38 +03001411 return 0;
1412
1413err_unregister:
1414 unregister_efivars(&__efivars);
1415err_put:
1416 kobject_put(efi_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return error;
1418}
1419
1420static void __exit
1421efivars_exit(void)
1422{
Matt Fleming73923012012-11-14 09:42:35 +00001423 if (efi_enabled(EFI_RUNTIME_SERVICES)) {
Randy Dunlapaabb6e12011-05-06 13:27:41 -07001424 unregister_efivars(&__efivars);
1425 kobject_put(efi_kobj);
1426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
1429module_init(efivars_init);
1430module_exit(efivars_exit);
1431