blob: 868cea5cd4b8fcb7b35652afb99864b8aaa26460 [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>
Matt Fleming47f531e2013-01-31 19:02:03 +000082#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Matthew Garrett5d9db882012-10-05 13:54:56 +080084#include <linux/fs.h>
85#include <linux/ramfs.h>
86#include <linux/pagemap.h>
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <asm/uaccess.h>
89
90#define EFIVARS_VERSION "0.08"
91#define EFIVARS_DATE "2004-May-17"
92
93MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
94MODULE_DESCRIPTION("sysfs interface to EFI Variables");
95MODULE_LICENSE("GPL");
96MODULE_VERSION(EFIVARS_VERSION);
97
Matthew Garrett5ee9c192011-07-21 16:57:56 -040098#define DUMP_NAME_LEN 52
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
Jeremy Kerr310ad752012-10-19 15:16:45 +0800101 * Length of a GUID string (strlen("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))
102 * not including trailing NUL
103 */
104#define GUID_LEN 36
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/*
107 * The maximum size of VariableName + Data = 1024
108 * Therefore, it's reasonable to save that much
109 * space in each part of the structure,
110 * and we use a page for reading/writing.
111 */
112
113struct efi_variable {
114 efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
115 efi_guid_t VendorGuid;
116 unsigned long DataSize;
117 __u8 Data[1024];
118 efi_status_t Status;
119 __u32 Attributes;
120} __attribute__((packed));
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122struct efivar_entry {
Mike Waychison4142ef12011-03-11 17:43:11 -0800123 struct efivars *efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 struct efi_variable var;
125 struct list_head list;
126 struct kobject kobj;
127};
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129struct efivar_attribute {
130 struct attribute attr;
131 ssize_t (*show) (struct efivar_entry *entry, char *buf);
132 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
133};
134
Matthew Garrett5d9db882012-10-05 13:54:56 +0800135static struct efivars __efivars;
136static struct efivar_operations ops;
137
Mike Waychison7644c162011-07-21 16:58:00 -0400138#define PSTORE_EFI_ATTRIBUTES \
139 (EFI_VARIABLE_NON_VOLATILE | \
140 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
141 EFI_VARIABLE_RUNTIME_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#define EFIVAR_ATTR(_name, _mode, _show, _store) \
144struct efivar_attribute efivar_attr_##_name = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900145 .attr = {.name = __stringify(_name), .mode = _mode}, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .show = _show, \
147 .store = _store, \
148};
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
151#define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
152
153/*
154 * Prototype for sysfs creation function
155 */
156static int
Mike Waychison4142ef12011-03-11 17:43:11 -0800157efivar_create_sysfs_entry(struct efivars *efivars,
158 unsigned long variable_name_size,
159 efi_char16_t *variable_name,
160 efi_guid_t *vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162/* Return the number of unicode characters in data */
163static unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400164utf16_strnlen(efi_char16_t *s, size_t maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 unsigned long length = 0;
167
Mike Waychisona2940902011-07-21 16:57:57 -0400168 while (*s++ != 0 && length < maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 length++;
170 return length;
171}
172
Tony Luckb728a5c2011-08-02 15:08:30 -0700173static inline unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400174utf16_strlen(efi_char16_t *s)
175{
176 return utf16_strnlen(s, ~0UL);
177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*
180 * Return the number of bytes is the length of this string
181 * Note: this is NOT the same as the number of unicode characters
182 */
183static inline unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400184utf16_strsize(efi_char16_t *data, unsigned long maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Mike Waychisona2940902011-07-21 16:57:57 -0400186 return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Mike Waychison828aa1f2011-07-21 16:57:58 -0400189static inline int
190utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
191{
192 while (1) {
193 if (len == 0)
194 return 0;
195 if (*a < *b)
196 return -1;
197 if (*a > *b)
198 return 1;
199 if (*a == 0) /* implies *b == 0 */
200 return 0;
201 a++;
202 b++;
203 len--;
204 }
205}
206
Matthew Garrettfec6c202012-04-30 16:11:30 -0400207static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400208validate_device_path(struct efi_variable *var, int match, u8 *buffer,
209 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400210{
211 struct efi_generic_dev_path *node;
212 int offset = 0;
213
214 node = (struct efi_generic_dev_path *)buffer;
215
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400216 if (len < sizeof(*node))
217 return false;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400218
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400219 while (offset <= len - sizeof(*node) &&
220 node->length >= sizeof(*node) &&
221 node->length <= len - offset) {
222 offset += node->length;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400223
224 if ((node->type == EFI_DEV_END_PATH ||
225 node->type == EFI_DEV_END_PATH2) &&
226 node->sub_type == EFI_DEV_END_ENTIRE)
227 return true;
228
229 node = (struct efi_generic_dev_path *)(buffer + offset);
230 }
231
232 /*
233 * If we're here then either node->length pointed past the end
234 * of the buffer or we reached the end of the buffer without
235 * finding a device path end node.
236 */
237 return false;
238}
239
240static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400241validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
242 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400243{
244 /* An array of 16-bit integers */
245 if ((len % 2) != 0)
246 return false;
247
248 return true;
249}
250
251static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400252validate_load_option(struct efi_variable *var, int match, u8 *buffer,
253 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400254{
255 u16 filepathlength;
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400256 int i, desclength = 0, namelen;
257
258 namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
Matthew Garrettfec6c202012-04-30 16:11:30 -0400259
260 /* Either "Boot" or "Driver" followed by four digits of hex */
261 for (i = match; i < match+4; i++) {
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400262 if (var->VariableName[i] > 127 ||
263 hex_to_bin(var->VariableName[i] & 0xff) < 0)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400264 return true;
265 }
266
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400267 /* Reject it if there's 4 digits of hex and then further content */
268 if (namelen > match + 4)
269 return false;
270
271 /* A valid entry must be at least 8 bytes */
272 if (len < 8)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400273 return false;
274
275 filepathlength = buffer[4] | buffer[5] << 8;
276
277 /*
278 * There's no stored length for the description, so it has to be
279 * found by hand
280 */
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400281 desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400282
283 /* Each boot entry must have a descriptor */
284 if (!desclength)
285 return false;
286
287 /*
288 * If the sum of the length of the description, the claimed filepath
289 * length and the original header are greater than the length of the
290 * variable, it's malformed
291 */
292 if ((desclength + filepathlength + 6) > len)
293 return false;
294
295 /*
296 * And, finally, check the filepath
297 */
298 return validate_device_path(var, match, buffer + desclength + 6,
299 filepathlength);
300}
301
302static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400303validate_uint16(struct efi_variable *var, int match, u8 *buffer,
304 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400305{
306 /* A single 16-bit integer */
307 if (len != 2)
308 return false;
309
310 return true;
311}
312
313static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400314validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
315 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400316{
317 int i;
318
319 for (i = 0; i < len; i++) {
320 if (buffer[i] > 127)
321 return false;
322
323 if (buffer[i] == 0)
324 return true;
325 }
326
327 return false;
328}
329
330struct variable_validate {
331 char *name;
332 bool (*validate)(struct efi_variable *var, int match, u8 *data,
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400333 unsigned long len);
Matthew Garrettfec6c202012-04-30 16:11:30 -0400334};
335
336static const struct variable_validate variable_validate[] = {
337 { "BootNext", validate_uint16 },
338 { "BootOrder", validate_boot_order },
339 { "DriverOrder", validate_boot_order },
340 { "Boot*", validate_load_option },
341 { "Driver*", validate_load_option },
342 { "ConIn", validate_device_path },
343 { "ConInDev", validate_device_path },
344 { "ConOut", validate_device_path },
345 { "ConOutDev", validate_device_path },
346 { "ErrOut", validate_device_path },
347 { "ErrOutDev", validate_device_path },
348 { "Timeout", validate_uint16 },
349 { "Lang", validate_ascii_string },
350 { "PlatformLang", validate_ascii_string },
351 { "", NULL },
352};
353
354static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400355validate_var(struct efi_variable *var, u8 *data, unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400356{
357 int i;
358 u16 *unicode_name = var->VariableName;
359
360 for (i = 0; variable_validate[i].validate != NULL; i++) {
361 const char *name = variable_validate[i].name;
362 int match;
363
364 for (match = 0; ; match++) {
365 char c = name[match];
366 u16 u = unicode_name[match];
367
368 /* All special variables are plain ascii */
369 if (u > 127)
370 return true;
371
372 /* Wildcard in the matching name means we've matched */
373 if (c == '*')
374 return variable_validate[i].validate(var,
375 match, data, len);
376
377 /* Case sensitive match */
378 if (c != u)
379 break;
380
381 /* Reached the end of the string while matching */
382 if (!c)
383 return variable_validate[i].validate(var,
384 match, data, len);
385 }
386 }
387
388 return true;
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391static efi_status_t
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400392get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 efi_status_t status;
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 var->DataSize = 1024;
Mike Waychison32958142011-03-11 17:43:21 -0800397 status = efivars->ops->get_variable(var->VariableName,
398 &var->VendorGuid,
399 &var->Attributes,
400 &var->DataSize,
401 var->Data);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400402 return status;
403}
404
405static efi_status_t
406get_var_data(struct efivars *efivars, struct efi_variable *var)
407{
408 efi_status_t status;
409
410 spin_lock(&efivars->lock);
411 status = get_var_data_locked(efivars, var);
Mike Waychison29422692011-03-11 17:43:00 -0800412 spin_unlock(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (status != EFI_SUCCESS) {
415 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
416 status);
417 }
418 return status;
419}
420
421static ssize_t
422efivar_guid_read(struct efivar_entry *entry, char *buf)
423{
424 struct efi_variable *var = &entry->var;
425 char *str = buf;
426
427 if (!entry || !buf)
428 return 0;
429
430 efi_guid_unparse(&var->VendorGuid, str);
431 str += strlen(str);
432 str += sprintf(str, "\n");
433
434 return str - buf;
435}
436
437static ssize_t
438efivar_attr_read(struct efivar_entry *entry, char *buf)
439{
440 struct efi_variable *var = &entry->var;
441 char *str = buf;
442 efi_status_t status;
443
444 if (!entry || !buf)
445 return -EINVAL;
446
Mike Waychison4142ef12011-03-11 17:43:11 -0800447 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (status != EFI_SUCCESS)
449 return -EIO;
450
Khalid Aziz70839092012-09-10 12:52:42 -0600451 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600453 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600455 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600457 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
458 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
459 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
460 str += sprintf(str,
461 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
462 if (var->Attributes &
463 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
464 str += sprintf(str,
465 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
466 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
467 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return str - buf;
469}
470
471static ssize_t
472efivar_size_read(struct efivar_entry *entry, char *buf)
473{
474 struct efi_variable *var = &entry->var;
475 char *str = buf;
476 efi_status_t status;
477
478 if (!entry || !buf)
479 return -EINVAL;
480
Mike Waychison4142ef12011-03-11 17:43:11 -0800481 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (status != EFI_SUCCESS)
483 return -EIO;
484
485 str += sprintf(str, "0x%lx\n", var->DataSize);
486 return str - buf;
487}
488
489static ssize_t
490efivar_data_read(struct efivar_entry *entry, char *buf)
491{
492 struct efi_variable *var = &entry->var;
493 efi_status_t status;
494
495 if (!entry || !buf)
496 return -EINVAL;
497
Mike Waychison4142ef12011-03-11 17:43:11 -0800498 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (status != EFI_SUCCESS)
500 return -EIO;
501
502 memcpy(buf, var->Data, var->DataSize);
503 return var->DataSize;
504}
505/*
506 * We allow each variable to be edited via rewriting the
507 * entire efi variable structure.
508 */
509static ssize_t
510efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
511{
512 struct efi_variable *new_var, *var = &entry->var;
Mike Waychison4142ef12011-03-11 17:43:11 -0800513 struct efivars *efivars = entry->efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 efi_status_t status = EFI_NOT_FOUND;
515
516 if (count != sizeof(struct efi_variable))
517 return -EINVAL;
518
519 new_var = (struct efi_variable *)buf;
520 /*
521 * If only updating the variable data, then the name
522 * and guid should remain the same
523 */
524 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
525 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
526 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
527 return -EINVAL;
528 }
529
530 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
531 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
532 return -EINVAL;
533 }
534
Matthew Garrettfec6c202012-04-30 16:11:30 -0400535 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
536 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
537 printk(KERN_ERR "efivars: Malformed variable content\n");
538 return -EINVAL;
539 }
540
Mike Waychison29422692011-03-11 17:43:00 -0800541 spin_lock(&efivars->lock);
Mike Waychison32958142011-03-11 17:43:21 -0800542 status = efivars->ops->set_variable(new_var->VariableName,
543 &new_var->VendorGuid,
544 new_var->Attributes,
545 new_var->DataSize,
546 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Mike Waychison29422692011-03-11 17:43:00 -0800548 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 if (status != EFI_SUCCESS) {
551 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
552 status);
553 return -EIO;
554 }
555
556 memcpy(&entry->var, new_var, count);
557 return count;
558}
559
560static ssize_t
561efivar_show_raw(struct efivar_entry *entry, char *buf)
562{
563 struct efi_variable *var = &entry->var;
564 efi_status_t status;
565
566 if (!entry || !buf)
567 return 0;
568
Mike Waychison4142ef12011-03-11 17:43:11 -0800569 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (status != EFI_SUCCESS)
571 return -EIO;
572
573 memcpy(buf, var, sizeof(*var));
574 return sizeof(*var);
575}
576
577/*
578 * Generic read/write functions that call the specific functions of
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200579 * the attributes...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 */
581static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
582 char *buf)
583{
584 struct efivar_entry *var = to_efivar_entry(kobj);
585 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500586 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (!capable(CAP_SYS_ADMIN))
589 return -EACCES;
590
591 if (efivar_attr->show) {
592 ret = efivar_attr->show(var, buf);
593 }
594 return ret;
595}
596
597static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
598 const char *buf, size_t count)
599{
600 struct efivar_entry *var = to_efivar_entry(kobj);
601 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500602 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (!capable(CAP_SYS_ADMIN))
605 return -EACCES;
606
607 if (efivar_attr->store)
608 ret = efivar_attr->store(var, buf, count);
609
610 return ret;
611}
612
Emese Revfy52cf25d2010-01-19 02:58:23 +0100613static const struct sysfs_ops efivar_attr_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 .show = efivar_attr_show,
615 .store = efivar_attr_store,
616};
617
618static void efivar_release(struct kobject *kobj)
619{
620 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 kfree(var);
622}
623
624static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
625static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
626static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
627static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
628static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
629
630static struct attribute *def_attrs[] = {
631 &efivar_attr_guid.attr,
632 &efivar_attr_size.attr,
633 &efivar_attr_attributes.attr,
634 &efivar_attr_data.attr,
635 &efivar_attr_raw_var.attr,
636 NULL,
637};
638
Greg Kroah-Hartmane89a4112007-10-11 10:47:49 -0600639static struct kobj_type efivar_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 .release = efivar_release,
641 .sysfs_ops = &efivar_attr_ops,
642 .default_attrs = def_attrs,
643};
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645static inline void
646efivar_unregister(struct efivar_entry *var)
647{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800648 kobject_put(&var->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Matthew Garrett5d9db882012-10-05 13:54:56 +0800651static int efivarfs_file_open(struct inode *inode, struct file *file)
652{
653 file->private_data = inode->i_private;
654 return 0;
655}
656
Matt Fleming7253eab2012-10-16 15:58:07 +0100657static int efi_status_to_err(efi_status_t status)
658{
659 int err;
660
661 switch (status) {
662 case EFI_INVALID_PARAMETER:
663 err = -EINVAL;
664 break;
665 case EFI_OUT_OF_RESOURCES:
666 err = -ENOSPC;
667 break;
668 case EFI_DEVICE_ERROR:
669 err = -EIO;
670 break;
671 case EFI_WRITE_PROTECTED:
672 err = -EROFS;
673 break;
674 case EFI_SECURITY_VIOLATION:
675 err = -EACCES;
676 break;
677 case EFI_NOT_FOUND:
Matt Fleming1fa7e692013-01-16 13:47:05 +0000678 err = -EIO;
Matt Fleming7253eab2012-10-16 15:58:07 +0100679 break;
680 default:
681 err = -EINVAL;
682 }
683
684 return err;
685}
686
Matthew Garrett5d9db882012-10-05 13:54:56 +0800687static ssize_t efivarfs_file_write(struct file *file,
688 const char __user *userbuf, size_t count, loff_t *ppos)
689{
690 struct efivar_entry *var = file->private_data;
691 struct efivars *efivars;
692 efi_status_t status;
693 void *data;
694 u32 attributes;
695 struct inode *inode = file->f_mapping->host;
Matt Fleming07b1c5b2012-10-23 12:35:43 +0100696 unsigned long datasize = count - sizeof(attributes);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800697 unsigned long newdatasize;
Matt Fleming89d16662012-11-09 21:02:56 +0000698 u64 storage_size, remaining_size, max_size;
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100699 ssize_t bytes = 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800700
701 if (count < sizeof(attributes))
702 return -EINVAL;
703
Matt Fleming89d16662012-11-09 21:02:56 +0000704 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
705 return -EFAULT;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800706
Matt Fleming89d16662012-11-09 21:02:56 +0000707 if (attributes & ~(EFI_VARIABLE_MASK))
708 return -EINVAL;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800709
710 efivars = var->efivars;
711
Matt Fleming89d16662012-11-09 21:02:56 +0000712 /*
713 * Ensure that the user can't allocate arbitrarily large
714 * amounts of memory. Pick a default size of 64K if
715 * QueryVariableInfo() isn't supported by the firmware.
716 */
717 spin_lock(&efivars->lock);
718
719 if (!efivars->ops->query_variable_info)
720 status = EFI_UNSUPPORTED;
721 else {
722 const struct efivar_operations *fops = efivars->ops;
723 status = fops->query_variable_info(attributes, &storage_size,
724 &remaining_size, &max_size);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800725 }
726
Matt Fleming89d16662012-11-09 21:02:56 +0000727 spin_unlock(&efivars->lock);
728
729 if (status != EFI_SUCCESS) {
730 if (status != EFI_UNSUPPORTED)
731 return efi_status_to_err(status);
732
733 remaining_size = 65536;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800734 }
735
Matt Fleming89d16662012-11-09 21:02:56 +0000736 if (datasize > remaining_size)
737 return -ENOSPC;
738
739 data = kmalloc(datasize, GFP_KERNEL);
740 if (!data)
741 return -ENOMEM;
742
Matthew Garrett5d9db882012-10-05 13:54:56 +0800743 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100744 bytes = -EFAULT;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800745 goto out;
746 }
747
748 if (validate_var(&var->var, data, datasize) == false) {
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100749 bytes = -EINVAL;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800750 goto out;
751 }
752
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800753 /*
754 * The lock here protects the get_variable call, the conditional
755 * set_variable call, and removal of the variable from the efivars
756 * list (in the case of an authenticated delete).
757 */
758 spin_lock(&efivars->lock);
759
Matthew Garrett5d9db882012-10-05 13:54:56 +0800760 status = efivars->ops->set_variable(var->var.VariableName,
761 &var->var.VendorGuid,
762 attributes, datasize,
763 data);
764
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800765 if (status != EFI_SUCCESS) {
766 spin_unlock(&efivars->lock);
767 kfree(data);
768
Matt Fleming7253eab2012-10-16 15:58:07 +0100769 return efi_status_to_err(status);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800770 }
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800771
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100772 bytes = count;
773
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800774 /*
775 * Writing to the variable may have caused a change in size (which
776 * could either be an append or an overwrite), or the variable to be
777 * deleted. Perform a GetVariable() so we can tell what actually
778 * happened.
779 */
780 newdatasize = 0;
781 status = efivars->ops->get_variable(var->var.VariableName,
782 &var->var.VendorGuid,
783 NULL, &newdatasize,
784 NULL);
785
786 if (status == EFI_BUFFER_TOO_SMALL) {
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800787 spin_unlock(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800788 mutex_lock(&inode->i_mutex);
789 i_size_write(inode, newdatasize + sizeof(attributes));
790 mutex_unlock(&inode->i_mutex);
791
792 } else if (status == EFI_NOT_FOUND) {
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800793 list_del(&var->list);
794 spin_unlock(&efivars->lock);
795 efivar_unregister(var);
796 drop_nlink(inode);
Matt Fleming791eb562013-01-16 21:55:36 +0000797 d_delete(file->f_dentry);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800798 dput(file->f_dentry);
799
800 } else {
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800801 spin_unlock(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800802 pr_warn("efivarfs: inconsistent EFI variable implementation? "
803 "status = %lx\n", status);
804 }
805
Matthew Garrett5d9db882012-10-05 13:54:56 +0800806out:
807 kfree(data);
808
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100809 return bytes;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800810}
811
812static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
813 size_t count, loff_t *ppos)
814{
815 struct efivar_entry *var = file->private_data;
816 struct efivars *efivars = var->efivars;
817 efi_status_t status;
818 unsigned long datasize = 0;
819 u32 attributes;
820 void *data;
Andy Whitcroftd142df02012-10-11 11:32:17 +0100821 ssize_t size = 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800822
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800823 spin_lock(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800824 status = efivars->ops->get_variable(var->var.VariableName,
825 &var->var.VendorGuid,
826 &attributes, &datasize, NULL);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800827 spin_unlock(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800828
829 if (status != EFI_BUFFER_TOO_SMALL)
Matt Fleming7253eab2012-10-16 15:58:07 +0100830 return efi_status_to_err(status);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800831
Matt Flemingd2923842012-10-22 15:23:29 +0100832 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800833
834 if (!data)
Matt Fleming7253eab2012-10-16 15:58:07 +0100835 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800836
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800837 spin_lock(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800838 status = efivars->ops->get_variable(var->var.VariableName,
839 &var->var.VendorGuid,
840 &attributes, &datasize,
Matt Flemingd2923842012-10-22 15:23:29 +0100841 (data + sizeof(attributes)));
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800842 spin_unlock(&efivars->lock);
843
Matt Fleming7253eab2012-10-16 15:58:07 +0100844 if (status != EFI_SUCCESS) {
845 size = efi_status_to_err(status);
Andy Whitcroftd142df02012-10-11 11:32:17 +0100846 goto out_free;
Matt Fleming7253eab2012-10-16 15:58:07 +0100847 }
Matthew Garrett5d9db882012-10-05 13:54:56 +0800848
Matt Flemingd2923842012-10-22 15:23:29 +0100849 memcpy(data, &attributes, sizeof(attributes));
Matthew Garrett5d9db882012-10-05 13:54:56 +0800850 size = simple_read_from_buffer(userbuf, count, ppos,
Matt Flemingd2923842012-10-22 15:23:29 +0100851 data, datasize + sizeof(attributes));
Andy Whitcroftd142df02012-10-11 11:32:17 +0100852out_free:
Matthew Garrett5d9db882012-10-05 13:54:56 +0800853 kfree(data);
854
855 return size;
856}
857
858static void efivarfs_evict_inode(struct inode *inode)
859{
860 clear_inode(inode);
861}
862
863static const struct super_operations efivarfs_ops = {
864 .statfs = simple_statfs,
865 .drop_inode = generic_delete_inode,
866 .evict_inode = efivarfs_evict_inode,
867 .show_options = generic_show_options,
868};
869
870static struct super_block *efivarfs_sb;
871
872static const struct inode_operations efivarfs_dir_inode_operations;
873
874static const struct file_operations efivarfs_file_operations = {
875 .open = efivarfs_file_open,
876 .read = efivarfs_file_read,
877 .write = efivarfs_file_write,
878 .llseek = no_llseek,
879};
880
881static struct inode *efivarfs_get_inode(struct super_block *sb,
882 const struct inode *dir, int mode, dev_t dev)
883{
884 struct inode *inode = new_inode(sb);
885
886 if (inode) {
887 inode->i_ino = get_next_ino();
Matthew Garrett5d9db882012-10-05 13:54:56 +0800888 inode->i_mode = mode;
889 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
890 switch (mode & S_IFMT) {
891 case S_IFREG:
892 inode->i_fop = &efivarfs_file_operations;
893 break;
894 case S_IFDIR:
895 inode->i_op = &efivarfs_dir_inode_operations;
896 inode->i_fop = &simple_dir_operations;
897 inc_nlink(inode);
898 break;
899 }
900 }
901 return inode;
902}
903
Matt Fleming47f531e2013-01-31 19:02:03 +0000904/*
905 * Return true if 'str' is a valid efivarfs filename of the form,
906 *
907 * VariableName-12345678-1234-1234-1234-1234567891bc
908 */
909static bool efivarfs_valid_name(const char *str, int len)
910{
911 static const char dashes[GUID_LEN] = {
912 [8] = 1, [13] = 1, [18] = 1, [23] = 1
913 };
914 const char *s = str + len - GUID_LEN;
915 int i;
916
917 /*
918 * We need a GUID, plus at least one letter for the variable name,
919 * plus the '-' separator
920 */
921 if (len < GUID_LEN + 2)
922 return false;
923
924 /* GUID should be right after the first '-' */
925 if (s - 1 != strchr(str, '-'))
926 return false;
927
928 /*
929 * Validate that 's' is of the correct format, e.g.
930 *
931 * 12345678-1234-1234-1234-123456789abc
932 */
933 for (i = 0; i < GUID_LEN; i++) {
934 if (dashes[i]) {
935 if (*s++ != '-')
936 return false;
937 } else {
938 if (!isxdigit(*s++))
939 return false;
940 }
941 }
942
943 return true;
944}
945
Matthew Garrett5d9db882012-10-05 13:54:56 +0800946static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
947{
948 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
949 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
950 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
951 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
952 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
953 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
954 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
955 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
956 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
957 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
958 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
959 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
960 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
961 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
962 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
963 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
964}
965
966static int efivarfs_create(struct inode *dir, struct dentry *dentry,
967 umode_t mode, bool excl)
968{
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100969 struct inode *inode;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800970 struct efivars *efivars = &__efivars;
971 struct efivar_entry *var;
972 int namelen, i = 0, err = 0;
973
Matt Fleming47f531e2013-01-31 19:02:03 +0000974 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
Matthew Garrett5d9db882012-10-05 13:54:56 +0800975 return -EINVAL;
976
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100977 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800978 if (!inode)
Matt Flemingaeeaa8d2012-10-23 12:41:03 +0100979 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800980
981 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100982 if (!var) {
983 err = -ENOMEM;
984 goto out;
985 }
Matthew Garrett5d9db882012-10-05 13:54:56 +0800986
Jeremy Kerr310ad752012-10-19 15:16:45 +0800987 /* length of the variable name itself: remove GUID and separator */
988 namelen = dentry->d_name.len - GUID_LEN - 1;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800989
990 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
991 &var->var.VendorGuid);
992
993 for (i = 0; i < namelen; i++)
994 var->var.VariableName[i] = dentry->d_name.name[i];
995
996 var->var.VariableName[i] = '\0';
997
998 inode->i_private = var;
999 var->efivars = efivars;
1000 var->kobj.kset = efivars->kset;
1001
1002 err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
1003 dentry->d_name.name);
1004 if (err)
1005 goto out;
1006
1007 kobject_uevent(&var->kobj, KOBJ_ADD);
1008 spin_lock(&efivars->lock);
1009 list_add(&var->list, &efivars->list);
1010 spin_unlock(&efivars->lock);
1011 d_instantiate(dentry, inode);
1012 dget(dentry);
1013out:
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001014 if (err) {
Matthew Garrett5d9db882012-10-05 13:54:56 +08001015 kfree(var);
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001016 iput(inode);
1017 }
Matthew Garrett5d9db882012-10-05 13:54:56 +08001018 return err;
1019}
1020
1021static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
1022{
1023 struct efivar_entry *var = dentry->d_inode->i_private;
1024 struct efivars *efivars = var->efivars;
1025 efi_status_t status;
1026
1027 spin_lock(&efivars->lock);
1028
1029 status = efivars->ops->set_variable(var->var.VariableName,
1030 &var->var.VendorGuid,
1031 0, 0, NULL);
1032
1033 if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
1034 list_del(&var->list);
1035 spin_unlock(&efivars->lock);
1036 efivar_unregister(var);
Lingzhu Xiangde5fe952013-01-05 13:59:52 +08001037 drop_nlink(dentry->d_inode);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001038 dput(dentry);
1039 return 0;
1040 }
1041
1042 spin_unlock(&efivars->lock);
1043 return -EINVAL;
1044};
1045
Matt Fleminge83af1f2012-11-15 20:03:16 +00001046static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
Matthew Garrett5d9db882012-10-05 13:54:56 +08001047{
1048 struct inode *inode = NULL;
1049 struct dentry *root;
1050 struct efivar_entry *entry, *n;
1051 struct efivars *efivars = &__efivars;
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001052 char *name;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001053
1054 efivarfs_sb = sb;
1055
1056 sb->s_maxbytes = MAX_LFS_FILESIZE;
1057 sb->s_blocksize = PAGE_CACHE_SIZE;
1058 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
Matt Fleming91716322012-10-22 15:51:45 +01001059 sb->s_magic = EFIVARFS_MAGIC;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001060 sb->s_op = &efivarfs_ops;
1061 sb->s_time_gran = 1;
1062
1063 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +01001064 if (!inode)
1065 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001066 inode->i_op = &efivarfs_dir_inode_operations;
1067
1068 root = d_make_root(inode);
1069 sb->s_root = root;
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +01001070 if (!root)
1071 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001072
1073 list_for_each_entry_safe(entry, n, &efivars->list, list) {
Matthew Garrett5d9db882012-10-05 13:54:56 +08001074 struct dentry *dentry, *root = efivarfs_sb->s_root;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001075 unsigned long size = 0;
1076 int len, i;
1077
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001078 inode = NULL;
1079
Matthew Garrett5d9db882012-10-05 13:54:56 +08001080 len = utf16_strlen(entry->var.VariableName);
1081
Jeremy Kerr310ad752012-10-19 15:16:45 +08001082 /* name, plus '-', plus GUID, plus NUL*/
1083 name = kmalloc(len + 1 + GUID_LEN + 1, GFP_ATOMIC);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001084 if (!name)
1085 goto fail;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001086
1087 for (i = 0; i < len; i++)
1088 name[i] = entry->var.VariableName[i] & 0xFF;
1089
1090 name[len] = '-';
1091
1092 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
1093
Jeremy Kerr310ad752012-10-19 15:16:45 +08001094 name[len+GUID_LEN+1] = '\0';
Matthew Garrett5d9db882012-10-05 13:54:56 +08001095
1096 inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
1097 S_IFREG | 0644, 0);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001098 if (!inode)
1099 goto fail_name;
1100
Matthew Garrett5d9db882012-10-05 13:54:56 +08001101 dentry = d_alloc_name(root, name);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001102 if (!dentry)
1103 goto fail_inode;
1104
Andy Whitcroftc0359db2012-10-11 11:32:20 +01001105 /* copied by the above to local storage in the dentry. */
1106 kfree(name);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001107
Jeremy Kerrf5f6a602012-10-11 21:19:11 +08001108 spin_lock(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001109 efivars->ops->get_variable(entry->var.VariableName,
1110 &entry->var.VendorGuid,
1111 &entry->var.Attributes,
1112 &size,
1113 NULL);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +08001114 spin_unlock(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001115
1116 mutex_lock(&inode->i_mutex);
1117 inode->i_private = entry;
Matt Fleming94a193f2013-01-11 13:30:46 +00001118 i_size_write(inode, size + sizeof(entry->var.Attributes));
Matthew Garrett5d9db882012-10-05 13:54:56 +08001119 mutex_unlock(&inode->i_mutex);
1120 d_add(dentry, inode);
1121 }
1122
1123 return 0;
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001124
1125fail_inode:
1126 iput(inode);
1127fail_name:
1128 kfree(name);
1129fail:
1130 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001131}
1132
1133static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
1134 int flags, const char *dev_name, void *data)
1135{
1136 return mount_single(fs_type, flags, data, efivarfs_fill_super);
1137}
1138
1139static void efivarfs_kill_sb(struct super_block *sb)
1140{
1141 kill_litter_super(sb);
1142 efivarfs_sb = NULL;
1143}
1144
1145static struct file_system_type efivarfs_type = {
1146 .name = "efivarfs",
1147 .mount = efivarfs_mount,
1148 .kill_sb = efivarfs_kill_sb,
1149};
1150
1151static const struct inode_operations efivarfs_dir_inode_operations = {
1152 .lookup = simple_lookup,
1153 .unlink = efivarfs_unlink,
1154 .create = efivarfs_create,
1155};
1156
1157static struct pstore_info efi_pstore_info;
1158
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001159#ifdef CONFIG_PSTORE
1160
1161static int efi_pstore_open(struct pstore_info *psi)
1162{
1163 struct efivars *efivars = psi->data;
1164
1165 spin_lock(&efivars->lock);
1166 efivars->walk_entry = list_first_entry(&efivars->list,
1167 struct efivar_entry, list);
1168 return 0;
1169}
1170
1171static int efi_pstore_close(struct pstore_info *psi)
1172{
1173 struct efivars *efivars = psi->data;
1174
1175 spin_unlock(&efivars->lock);
1176 return 0;
1177}
1178
1179static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001180 int *count, struct timespec *timespec,
Kees Cookf6f82852011-11-17 12:58:07 -08001181 char **buf, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001182{
1183 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1184 struct efivars *efivars = psi->data;
1185 char name[DUMP_NAME_LEN];
1186 int i;
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001187 int cnt;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001188 unsigned int part, size;
1189 unsigned long time;
1190
1191 while (&efivars->walk_entry->list != &efivars->list) {
1192 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
1193 vendor)) {
1194 for (i = 0; i < DUMP_NAME_LEN; i++) {
1195 name[i] = efivars->walk_entry->var.VariableName[i];
1196 }
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001197 if (sscanf(name, "dump-type%u-%u-%d-%lu",
1198 type, &part, &cnt, &time) == 4) {
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001199 *id = part;
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001200 *count = cnt;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001201 timespec->tv_sec = time;
1202 timespec->tv_nsec = 0;
Seiji Aguchi0f7de852012-11-14 20:28:50 +00001203 } else if (sscanf(name, "dump-type%u-%u-%lu",
1204 type, &part, &time) == 3) {
1205 /*
1206 * Check if an old format,
1207 * which doesn't support holding
1208 * multiple logs, remains.
1209 */
1210 *id = part;
1211 *count = 0;
1212 timespec->tv_sec = time;
1213 timespec->tv_nsec = 0;
1214 } else {
1215 efivars->walk_entry = list_entry(
1216 efivars->walk_entry->list.next,
1217 struct efivar_entry, list);
1218 continue;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001219 }
Seiji Aguchi0f7de852012-11-14 20:28:50 +00001220
1221 get_var_data_locked(efivars, &efivars->walk_entry->var);
1222 size = efivars->walk_entry->var.DataSize;
1223 *buf = kmalloc(size, GFP_KERNEL);
1224 if (*buf == NULL)
1225 return -ENOMEM;
1226 memcpy(*buf, efivars->walk_entry->var.Data,
1227 size);
1228 efivars->walk_entry = list_entry(
1229 efivars->walk_entry->list.next,
1230 struct efivar_entry, list);
1231 return size;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001232 }
1233 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1234 struct efivar_entry, list);
1235 }
1236 return 0;
1237}
1238
Kees Cook3d6d8d22011-11-17 13:13:29 -08001239static int efi_pstore_write(enum pstore_type_id type,
1240 enum kmsg_dump_reason reason, u64 *id,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001241 unsigned int part, int count, size_t size,
1242 struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001243{
1244 char name[DUMP_NAME_LEN];
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001245 efi_char16_t efi_name[DUMP_NAME_LEN];
1246 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1247 struct efivars *efivars = psi->data;
Chen Gongb238b8f2011-10-12 09:17:24 -07001248 int i, ret = 0;
Seiji Aguchid80a3612012-11-14 20:25:37 +00001249 u64 storage_space, remaining_space, max_variable_size;
1250 efi_status_t status = EFI_NOT_FOUND;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001251
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001252 spin_lock(&efivars->lock);
1253
Seiji Aguchid80a3612012-11-14 20:25:37 +00001254 /*
1255 * Check if there is a space enough to log.
1256 * size: a size of logging data
1257 * DUMP_NAME_LEN * 2: a maximum size of variable name
1258 */
1259 status = efivars->ops->query_variable_info(PSTORE_EFI_ATTRIBUTES,
1260 &storage_space,
1261 &remaining_space,
1262 &max_variable_size);
1263 if (status || remaining_space < size + DUMP_NAME_LEN * 2) {
1264 spin_unlock(&efivars->lock);
1265 *id = part;
1266 return -ENOSPC;
1267 }
1268
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001269 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
Seiji Aguchi96480d92012-11-14 20:26:46 +00001270 get_seconds());
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001271
1272 for (i = 0; i < DUMP_NAME_LEN; i++)
1273 efi_name[i] = name[i];
1274
Mike Waychison7644c162011-07-21 16:58:00 -04001275 efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001276 size, psi->buf);
1277
1278 spin_unlock(&efivars->lock);
1279
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001280 if (size)
Chen Gongb238b8f2011-10-12 09:17:24 -07001281 ret = efivar_create_sysfs_entry(efivars,
Mike Waychisona2940902011-07-21 16:57:57 -04001282 utf16_strsize(efi_name,
1283 DUMP_NAME_LEN * 2),
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001284 efi_name, &vendor);
1285
Chen Gongb238b8f2011-10-12 09:17:24 -07001286 *id = part;
1287 return ret;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001288};
1289
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001290static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001291 struct timespec time, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001292{
Seiji Aguchia9efd392012-11-14 20:27:28 +00001293 char name[DUMP_NAME_LEN];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001294 efi_char16_t efi_name[DUMP_NAME_LEN];
Seiji Aguchif94ec0c2012-11-14 20:29:21 +00001295 char name_old[DUMP_NAME_LEN];
1296 efi_char16_t efi_name_old[DUMP_NAME_LEN];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001297 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1298 struct efivars *efivars = psi->data;
1299 struct efivar_entry *entry, *found = NULL;
1300 int i;
1301
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001302 sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001303 time.tv_sec);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001304
1305 spin_lock(&efivars->lock);
1306
1307 for (i = 0; i < DUMP_NAME_LEN; i++)
Seiji Aguchia9efd392012-11-14 20:27:28 +00001308 efi_name[i] = name[i];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001309
1310 /*
Seiji Aguchia9efd392012-11-14 20:27:28 +00001311 * Clean up an entry with the same name
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001312 */
1313
1314 list_for_each_entry(entry, &efivars->list, list) {
1315 get_var_data_locked(efivars, &entry->var);
1316
1317 if (efi_guidcmp(entry->var.VendorGuid, vendor))
1318 continue;
1319 if (utf16_strncmp(entry->var.VariableName, efi_name,
Seiji Aguchif94ec0c2012-11-14 20:29:21 +00001320 utf16_strlen(efi_name))) {
1321 /*
1322 * Check if an old format,
1323 * which doesn't support holding
1324 * multiple logs, remains.
1325 */
1326 sprintf(name_old, "dump-type%u-%u-%lu", type,
1327 (unsigned int)id, time.tv_sec);
1328
1329 for (i = 0; i < DUMP_NAME_LEN; i++)
1330 efi_name_old[i] = name_old[i];
1331
1332 if (utf16_strncmp(entry->var.VariableName, efi_name_old,
1333 utf16_strlen(efi_name_old)))
1334 continue;
1335 }
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001336
1337 /* found */
1338 found = entry;
1339 efivars->ops->set_variable(entry->var.VariableName,
1340 &entry->var.VendorGuid,
1341 PSTORE_EFI_ATTRIBUTES,
1342 0, NULL);
Seiji Aguchia9efd392012-11-14 20:27:28 +00001343 break;
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001344 }
1345
1346 if (found)
1347 list_del(&found->list);
1348
1349 spin_unlock(&efivars->lock);
1350
1351 if (found)
1352 efivar_unregister(found);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001353
1354 return 0;
1355}
1356#else
1357static int efi_pstore_open(struct pstore_info *psi)
1358{
1359 return 0;
1360}
1361
1362static int efi_pstore_close(struct pstore_info *psi)
1363{
1364 return 0;
1365}
1366
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001367static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type, int *count,
Christoph Fritzeee628d2011-11-28 23:49:33 +01001368 struct timespec *timespec,
1369 char **buf, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001370{
1371 return -1;
1372}
1373
Kees Cook3d6d8d22011-11-17 13:13:29 -08001374static int efi_pstore_write(enum pstore_type_id type,
1375 enum kmsg_dump_reason reason, u64 *id,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001376 unsigned int part, int count, size_t size,
1377 struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001378{
1379 return 0;
1380}
1381
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001382static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001383 struct timespec time, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001384{
1385 return 0;
1386}
1387#endif
1388
1389static struct pstore_info efi_pstore_info = {
1390 .owner = THIS_MODULE,
1391 .name = "efi",
1392 .open = efi_pstore_open,
1393 .close = efi_pstore_close,
1394 .read = efi_pstore_read,
1395 .write = efi_pstore_write,
1396 .erase = efi_pstore_erase,
1397};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Chris Wright2c3c8be2010-05-12 18:28:57 -07001399static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001400 struct bin_attribute *bin_attr,
1401 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
1403 struct efi_variable *new_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001404 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001405 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 efi_status_t status = EFI_NOT_FOUND;
1408 int found = 0;
1409
1410 if (!capable(CAP_SYS_ADMIN))
1411 return -EACCES;
1412
Matthew Garrettfec6c202012-04-30 16:11:30 -04001413 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
1414 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
1415 printk(KERN_ERR "efivars: Malformed variable content\n");
1416 return -EINVAL;
1417 }
1418
Mike Waychison29422692011-03-11 17:43:00 -08001419 spin_lock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 /*
1422 * Does this variable already exist?
1423 */
Mike Waychison29422692011-03-11 17:43:00 -08001424 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -04001425 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1426 strsize2 = utf16_strsize(new_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (strsize1 == strsize2 &&
1428 !memcmp(&(search_efivar->var.VariableName),
1429 new_var->VariableName, strsize1) &&
1430 !efi_guidcmp(search_efivar->var.VendorGuid,
1431 new_var->VendorGuid)) {
1432 found = 1;
1433 break;
1434 }
1435 }
1436 if (found) {
Mike Waychison29422692011-03-11 17:43:00 -08001437 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return -EINVAL;
1439 }
1440
1441 /* now *really* create the variable via EFI */
Mike Waychison32958142011-03-11 17:43:21 -08001442 status = efivars->ops->set_variable(new_var->VariableName,
1443 &new_var->VendorGuid,
1444 new_var->Attributes,
1445 new_var->DataSize,
1446 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 if (status != EFI_SUCCESS) {
1449 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1450 status);
Mike Waychison29422692011-03-11 17:43:00 -08001451 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return -EIO;
1453 }
Mike Waychison29422692011-03-11 17:43:00 -08001454 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 /* Create the entry in sysfs. Locking is not required here */
Mike Waychison4142ef12011-03-11 17:43:11 -08001457 status = efivar_create_sysfs_entry(efivars,
Mike Waychisona2940902011-07-21 16:57:57 -04001458 utf16_strsize(new_var->VariableName,
1459 1024),
Mike Waychison4142ef12011-03-11 17:43:11 -08001460 new_var->VariableName,
1461 &new_var->VendorGuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (status) {
1463 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
1464 }
1465 return count;
1466}
1467
Chris Wright2c3c8be2010-05-12 18:28:57 -07001468static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001469 struct bin_attribute *bin_attr,
1470 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 struct efi_variable *del_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001473 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001474 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 efi_status_t status = EFI_NOT_FOUND;
1477 int found = 0;
1478
1479 if (!capable(CAP_SYS_ADMIN))
1480 return -EACCES;
1481
Mike Waychison29422692011-03-11 17:43:00 -08001482 spin_lock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 /*
1485 * Does this variable already exist?
1486 */
Mike Waychison29422692011-03-11 17:43:00 -08001487 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -04001488 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1489 strsize2 = utf16_strsize(del_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 if (strsize1 == strsize2 &&
1491 !memcmp(&(search_efivar->var.VariableName),
1492 del_var->VariableName, strsize1) &&
1493 !efi_guidcmp(search_efivar->var.VendorGuid,
1494 del_var->VendorGuid)) {
1495 found = 1;
1496 break;
1497 }
1498 }
1499 if (!found) {
Mike Waychison29422692011-03-11 17:43:00 -08001500 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return -EINVAL;
1502 }
1503 /* force the Attributes/DataSize to 0 to ensure deletion */
1504 del_var->Attributes = 0;
1505 del_var->DataSize = 0;
1506
Mike Waychison32958142011-03-11 17:43:21 -08001507 status = efivars->ops->set_variable(del_var->VariableName,
1508 &del_var->VendorGuid,
1509 del_var->Attributes,
1510 del_var->DataSize,
1511 del_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (status != EFI_SUCCESS) {
1514 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1515 status);
Mike Waychison29422692011-03-11 17:43:00 -08001516 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return -EIO;
1518 }
Matt Domsch496a0fc2007-01-26 00:57:18 -08001519 list_del(&search_efivar->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 /* We need to release this lock before unregistering. */
Mike Waychison29422692011-03-11 17:43:00 -08001521 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 efivar_unregister(search_efivar);
1523
1524 /* It's dead Jim.... */
1525 return count;
1526}
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528/*
1529 * Let's not leave out systab information that snuck into
1530 * the efivars driver
1531 */
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001532static ssize_t systab_show(struct kobject *kobj,
1533 struct kobj_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 char *str = buf;
1536
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001537 if (!kobj || !buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return -EINVAL;
1539
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -08001540 if (efi.mps != EFI_INVALID_TABLE_ADDR)
1541 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
1542 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
1543 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
1544 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
1545 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
1546 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
1547 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
1548 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
1549 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
1550 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
1551 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
1552 if (efi.uga != EFI_INVALID_TABLE_ADDR)
1553 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 return str - buf;
1556}
1557
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001558static struct kobj_attribute efi_attr_systab =
1559 __ATTR(systab, 0400, systab_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001561static struct attribute *efi_subsys_attrs[] = {
1562 &efi_attr_systab.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 NULL, /* maybe more in the future? */
1564};
1565
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001566static struct attribute_group efi_subsys_attr_group = {
1567 .attrs = efi_subsys_attrs,
1568};
1569
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08001570static struct kobject *efi_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572/*
1573 * efivar_create_sysfs_entry()
1574 * Requires:
1575 * variable_name_size = number of bytes required to hold
1576 * variable_name (not counting the NULL
1577 * character at the end.
Mike Waychison29422692011-03-11 17:43:00 -08001578 * efivars->lock is not held on entry or exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 * Returns 1 on failure, 0 on success
1580 */
1581static int
Mike Waychison4142ef12011-03-11 17:43:11 -08001582efivar_create_sysfs_entry(struct efivars *efivars,
1583 unsigned long variable_name_size,
1584 efi_char16_t *variable_name,
1585 efi_guid_t *vendor_guid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Jeremy Kerr310ad752012-10-19 15:16:45 +08001587 int i, short_name_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 char *short_name;
1589 struct efivar_entry *new_efivar;
1590
Jeremy Kerr310ad752012-10-19 15:16:45 +08001591 /*
1592 * Length of the variable bytes in ASCII, plus the '-' separator,
1593 * plus the GUID, plus trailing NUL
1594 */
1595 short_name_size = variable_name_size / sizeof(efi_char16_t)
1596 + 1 + GUID_LEN + 1;
1597
1598 short_name = kzalloc(short_name_size, GFP_KERNEL);
Deepak Saxena9c215382005-11-07 01:01:24 -08001599 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 if (!short_name || !new_efivar) {
Jesper Juhl0933ad92005-06-25 14:59:15 -07001602 kfree(short_name);
1603 kfree(new_efivar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return 1;
1605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Mike Waychison4142ef12011-03-11 17:43:11 -08001607 new_efivar->efivars = efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 memcpy(new_efivar->var.VariableName, variable_name,
1609 variable_name_size);
1610 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
1611
1612 /* Convert Unicode to normal chars (assume top bits are 0),
1613 ala UTF-8 */
1614 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
1615 short_name[i] = variable_name[i] & 0xFF;
1616 }
1617 /* This is ugly, but necessary to separate one vendor's
1618 private variables from another's. */
1619
1620 *(short_name + strlen(short_name)) = '-';
1621 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
1622
Mike Waychison29422692011-03-11 17:43:00 -08001623 new_efivar->kobj.kset = efivars->kset;
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001624 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
1625 "%s", short_name);
Jeff Garzik69b21862006-10-11 01:22:23 -07001626 if (i) {
1627 kfree(short_name);
1628 kfree(new_efivar);
1629 return 1;
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001632 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
Jesper Juhl0933ad92005-06-25 14:59:15 -07001633 kfree(short_name);
1634 short_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Mike Waychison29422692011-03-11 17:43:00 -08001636 spin_lock(&efivars->lock);
1637 list_add(&new_efivar->list, &efivars->list);
1638 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 return 0;
1641}
Mike Waychisond502fbb2011-03-11 17:43:06 -08001642
1643static int
1644create_efivars_bin_attributes(struct efivars *efivars)
1645{
1646 struct bin_attribute *attr;
1647 int error;
1648
1649 /* new_var */
1650 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1651 if (!attr)
1652 return -ENOMEM;
1653
1654 attr->attr.name = "new_var";
1655 attr->attr.mode = 0200;
1656 attr->write = efivar_create;
1657 attr->private = efivars;
1658 efivars->new_var = attr;
1659
1660 /* del_var */
1661 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1662 if (!attr) {
1663 error = -ENOMEM;
1664 goto out_free;
1665 }
1666 attr->attr.name = "del_var";
1667 attr->attr.mode = 0200;
1668 attr->write = efivar_delete;
1669 attr->private = efivars;
1670 efivars->del_var = attr;
1671
1672 sysfs_bin_attr_init(efivars->new_var);
1673 sysfs_bin_attr_init(efivars->del_var);
1674
1675 /* Register */
1676 error = sysfs_create_bin_file(&efivars->kset->kobj,
1677 efivars->new_var);
1678 if (error) {
1679 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
1680 " due to error %d\n", error);
1681 goto out_free;
1682 }
1683 error = sysfs_create_bin_file(&efivars->kset->kobj,
1684 efivars->del_var);
1685 if (error) {
1686 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
1687 " due to error %d\n", error);
1688 sysfs_remove_bin_file(&efivars->kset->kobj,
1689 efivars->new_var);
1690 goto out_free;
1691 }
1692
1693 return 0;
1694out_free:
Dan Carpenter051d51b2011-03-18 10:12:14 +03001695 kfree(efivars->del_var);
1696 efivars->del_var = NULL;
Mike Waychisond502fbb2011-03-11 17:43:06 -08001697 kfree(efivars->new_var);
1698 efivars->new_var = NULL;
1699 return error;
1700}
1701
Mike Waychison4fc756b2011-03-11 17:43:27 -08001702void unregister_efivars(struct efivars *efivars)
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001703{
1704 struct efivar_entry *entry, *n;
Mike Waychison4142ef12011-03-11 17:43:11 -08001705
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001706 list_for_each_entry_safe(entry, n, &efivars->list, list) {
1707 spin_lock(&efivars->lock);
1708 list_del(&entry->list);
1709 spin_unlock(&efivars->lock);
1710 efivar_unregister(entry);
1711 }
1712 if (efivars->new_var)
1713 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
1714 if (efivars->del_var)
1715 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
1716 kfree(efivars->new_var);
1717 kfree(efivars->del_var);
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08001718 kobject_put(efivars->kobject);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001719 kset_unregister(efivars->kset);
1720}
Mike Waychison4fc756b2011-03-11 17:43:27 -08001721EXPORT_SYMBOL_GPL(unregister_efivars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Mike Waychison4fc756b2011-03-11 17:43:27 -08001723int register_efivars(struct efivars *efivars,
1724 const struct efivar_operations *ops,
1725 struct kobject *parent_kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
1727 efi_status_t status = EFI_NOT_FOUND;
1728 efi_guid_t vendor_guid;
1729 efi_char16_t *variable_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 unsigned long variable_name_size = 1024;
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001731 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Deepak Saxena9c215382005-11-07 01:01:24 -08001733 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (!variable_name) {
1735 printk(KERN_ERR "efivars: Memory allocation failed.\n");
1736 return -ENOMEM;
1737 }
1738
Mike Waychison29422692011-03-11 17:43:00 -08001739 spin_lock_init(&efivars->lock);
1740 INIT_LIST_HEAD(&efivars->list);
Mike Waychison32958142011-03-11 17:43:21 -08001741 efivars->ops = ops;
Mike Waychison29422692011-03-11 17:43:00 -08001742
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001743 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
Mike Waychison29422692011-03-11 17:43:00 -08001744 if (!efivars->kset) {
Greg Kroah-Hartman66ac8312007-11-02 13:20:40 -07001745 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
1746 error = -ENOMEM;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001747 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08001750 efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
1751 if (!efivars->kobject) {
1752 pr_err("efivars: Subsystem registration failed.\n");
1753 error = -ENOMEM;
1754 kset_unregister(efivars->kset);
1755 goto out;
1756 }
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 /*
1759 * Per EFI spec, the maximum storage allocated for both
1760 * the variable name and variable data is 1024 bytes.
1761 */
1762
1763 do {
1764 variable_name_size = 1024;
1765
Mike Waychison32958142011-03-11 17:43:21 -08001766 status = ops->get_next_variable(&variable_name_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 variable_name,
1768 &vendor_guid);
1769 switch (status) {
1770 case EFI_SUCCESS:
Mike Waychison4142ef12011-03-11 17:43:11 -08001771 efivar_create_sysfs_entry(efivars,
1772 variable_name_size,
1773 variable_name,
1774 &vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 break;
1776 case EFI_NOT_FOUND:
1777 break;
1778 default:
1779 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
1780 status);
1781 status = EFI_NOT_FOUND;
1782 break;
1783 }
1784 } while (status != EFI_NOT_FOUND);
1785
Mike Waychisond502fbb2011-03-11 17:43:06 -08001786 error = create_efivars_bin_attributes(efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001787 if (error)
1788 unregister_efivars(efivars);
1789
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001790 efivars->efi_pstore_info = efi_pstore_info;
1791
1792 efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
1793 if (efivars->efi_pstore_info.buf) {
1794 efivars->efi_pstore_info.bufsize = 1024;
1795 efivars->efi_pstore_info.data = efivars;
Don Zickusabd4d552011-08-12 10:54:51 -07001796 spin_lock_init(&efivars->efi_pstore_info.buf_lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001797 pstore_register(&efivars->efi_pstore_info);
1798 }
1799
Matthew Garrett5d9db882012-10-05 13:54:56 +08001800 register_filesystem(&efivarfs_type);
1801
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001802out:
1803 kfree(variable_name);
1804
1805 return error;
1806}
Mike Waychison4fc756b2011-03-11 17:43:27 -08001807EXPORT_SYMBOL_GPL(register_efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001808
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001809/*
1810 * For now we register the efi subsystem with the firmware subsystem
1811 * and the vars subsystem with the efi subsystem. In the future, it
1812 * might make sense to split off the efi subsystem into its own
1813 * driver, but for now only efivars will register with it, so just
1814 * include it here.
1815 */
1816
1817static int __init
1818efivars_init(void)
1819{
1820 int error = 0;
1821
1822 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
1823 EFIVARS_DATE);
1824
Matt Fleming83e68182012-11-14 09:42:35 +00001825 if (!efi_enabled(EFI_RUNTIME_SERVICES))
Mike Waychison4fc756b2011-03-11 17:43:27 -08001826 return 0;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001827
1828 /* For now we'll register the efi directory at /sys/firmware/efi */
1829 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
1830 if (!efi_kobj) {
1831 printk(KERN_ERR "efivars: Firmware registration failed.\n");
1832 return -ENOMEM;
1833 }
1834
Mike Waychison32958142011-03-11 17:43:21 -08001835 ops.get_variable = efi.get_variable;
1836 ops.set_variable = efi.set_variable;
1837 ops.get_next_variable = efi.get_next_variable;
Seiji Aguchid80a3612012-11-14 20:25:37 +00001838 ops.query_variable_info = efi.query_variable_info;
Matt Fleming89d16662012-11-09 21:02:56 +00001839
Mike Waychison32958142011-03-11 17:43:21 -08001840 error = register_efivars(&__efivars, &ops, efi_kobj);
Dan Carpenter3116aab2011-03-18 10:12:38 +03001841 if (error)
1842 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 /* Don't forget the systab entry */
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08001845 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001846 if (error) {
1847 printk(KERN_ERR
1848 "efivars: Sysfs attribute export failed with error %d.\n",
1849 error);
Dan Carpenter3116aab2011-03-18 10:12:38 +03001850 goto err_unregister;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Dan Carpenter3116aab2011-03-18 10:12:38 +03001853 return 0;
1854
1855err_unregister:
1856 unregister_efivars(&__efivars);
1857err_put:
1858 kobject_put(efi_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return error;
1860}
1861
1862static void __exit
1863efivars_exit(void)
1864{
Matt Fleming83e68182012-11-14 09:42:35 +00001865 if (efi_enabled(EFI_RUNTIME_SERVICES)) {
Randy Dunlapaabb6e12011-05-06 13:27:41 -07001866 unregister_efivars(&__efivars);
1867 kobject_put(efi_kobj);
1868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
1871module_init(efivars_init);
1872module_exit(efivars_exit);
1873