blob: fe62aa3922398ebc52e7f61b586a71814a4b9dfe [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
Seiji Aguchia93bc0c2013-02-12 13:04:41 -0800162/*
163 * Prototype for workqueue functions updating sysfs entry
164 */
165
166static void efivar_update_sysfs_entries(struct work_struct *);
167static DECLARE_WORK(efivar_work, efivar_update_sysfs_entries);
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/* Return the number of unicode characters in data */
170static unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400171utf16_strnlen(efi_char16_t *s, size_t maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 unsigned long length = 0;
174
Mike Waychisona2940902011-07-21 16:57:57 -0400175 while (*s++ != 0 && length < maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 length++;
177 return length;
178}
179
Tony Luckb728a5c2011-08-02 15:08:30 -0700180static inline unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400181utf16_strlen(efi_char16_t *s)
182{
183 return utf16_strnlen(s, ~0UL);
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
187 * Return the number of bytes is the length of this string
188 * Note: this is NOT the same as the number of unicode characters
189 */
190static inline unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400191utf16_strsize(efi_char16_t *data, unsigned long maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Mike Waychisona2940902011-07-21 16:57:57 -0400193 return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Mike Waychison828aa1f2011-07-21 16:57:58 -0400196static inline int
197utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
198{
199 while (1) {
200 if (len == 0)
201 return 0;
202 if (*a < *b)
203 return -1;
204 if (*a > *b)
205 return 1;
206 if (*a == 0) /* implies *b == 0 */
207 return 0;
208 a++;
209 b++;
210 len--;
211 }
212}
213
Matthew Garrettfec6c202012-04-30 16:11:30 -0400214static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400215validate_device_path(struct efi_variable *var, int match, u8 *buffer,
216 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400217{
218 struct efi_generic_dev_path *node;
219 int offset = 0;
220
221 node = (struct efi_generic_dev_path *)buffer;
222
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400223 if (len < sizeof(*node))
224 return false;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400225
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400226 while (offset <= len - sizeof(*node) &&
227 node->length >= sizeof(*node) &&
228 node->length <= len - offset) {
229 offset += node->length;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400230
231 if ((node->type == EFI_DEV_END_PATH ||
232 node->type == EFI_DEV_END_PATH2) &&
233 node->sub_type == EFI_DEV_END_ENTIRE)
234 return true;
235
236 node = (struct efi_generic_dev_path *)(buffer + offset);
237 }
238
239 /*
240 * If we're here then either node->length pointed past the end
241 * of the buffer or we reached the end of the buffer without
242 * finding a device path end node.
243 */
244 return false;
245}
246
247static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400248validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
249 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400250{
251 /* An array of 16-bit integers */
252 if ((len % 2) != 0)
253 return false;
254
255 return true;
256}
257
258static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400259validate_load_option(struct efi_variable *var, int match, u8 *buffer,
260 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400261{
262 u16 filepathlength;
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400263 int i, desclength = 0, namelen;
264
265 namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
Matthew Garrettfec6c202012-04-30 16:11:30 -0400266
267 /* Either "Boot" or "Driver" followed by four digits of hex */
268 for (i = match; i < match+4; i++) {
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400269 if (var->VariableName[i] > 127 ||
270 hex_to_bin(var->VariableName[i] & 0xff) < 0)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400271 return true;
272 }
273
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400274 /* Reject it if there's 4 digits of hex and then further content */
275 if (namelen > match + 4)
276 return false;
277
278 /* A valid entry must be at least 8 bytes */
279 if (len < 8)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400280 return false;
281
282 filepathlength = buffer[4] | buffer[5] << 8;
283
284 /*
285 * There's no stored length for the description, so it has to be
286 * found by hand
287 */
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400288 desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400289
290 /* Each boot entry must have a descriptor */
291 if (!desclength)
292 return false;
293
294 /*
295 * If the sum of the length of the description, the claimed filepath
296 * length and the original header are greater than the length of the
297 * variable, it's malformed
298 */
299 if ((desclength + filepathlength + 6) > len)
300 return false;
301
302 /*
303 * And, finally, check the filepath
304 */
305 return validate_device_path(var, match, buffer + desclength + 6,
306 filepathlength);
307}
308
309static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400310validate_uint16(struct efi_variable *var, int match, u8 *buffer,
311 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400312{
313 /* A single 16-bit integer */
314 if (len != 2)
315 return false;
316
317 return true;
318}
319
320static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400321validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
322 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400323{
324 int i;
325
326 for (i = 0; i < len; i++) {
327 if (buffer[i] > 127)
328 return false;
329
330 if (buffer[i] == 0)
331 return true;
332 }
333
334 return false;
335}
336
337struct variable_validate {
338 char *name;
339 bool (*validate)(struct efi_variable *var, int match, u8 *data,
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400340 unsigned long len);
Matthew Garrettfec6c202012-04-30 16:11:30 -0400341};
342
343static const struct variable_validate variable_validate[] = {
344 { "BootNext", validate_uint16 },
345 { "BootOrder", validate_boot_order },
346 { "DriverOrder", validate_boot_order },
347 { "Boot*", validate_load_option },
348 { "Driver*", validate_load_option },
349 { "ConIn", validate_device_path },
350 { "ConInDev", validate_device_path },
351 { "ConOut", validate_device_path },
352 { "ConOutDev", validate_device_path },
353 { "ErrOut", validate_device_path },
354 { "ErrOutDev", validate_device_path },
355 { "Timeout", validate_uint16 },
356 { "Lang", validate_ascii_string },
357 { "PlatformLang", validate_ascii_string },
358 { "", NULL },
359};
360
361static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400362validate_var(struct efi_variable *var, u8 *data, unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400363{
364 int i;
365 u16 *unicode_name = var->VariableName;
366
367 for (i = 0; variable_validate[i].validate != NULL; i++) {
368 const char *name = variable_validate[i].name;
369 int match;
370
371 for (match = 0; ; match++) {
372 char c = name[match];
373 u16 u = unicode_name[match];
374
375 /* All special variables are plain ascii */
376 if (u > 127)
377 return true;
378
379 /* Wildcard in the matching name means we've matched */
380 if (c == '*')
381 return variable_validate[i].validate(var,
382 match, data, len);
383
384 /* Case sensitive match */
385 if (c != u)
386 break;
387
388 /* Reached the end of the string while matching */
389 if (!c)
390 return variable_validate[i].validate(var,
391 match, data, len);
392 }
393 }
394
395 return true;
396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static efi_status_t
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400399get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 efi_status_t status;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 var->DataSize = 1024;
Mike Waychison32958142011-03-11 17:43:21 -0800404 status = efivars->ops->get_variable(var->VariableName,
405 &var->VendorGuid,
406 &var->Attributes,
407 &var->DataSize,
408 var->Data);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400409 return status;
410}
411
412static efi_status_t
413get_var_data(struct efivars *efivars, struct efi_variable *var)
414{
415 efi_status_t status;
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800416 unsigned long flags;
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400417
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800418 spin_lock_irqsave(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400419 status = get_var_data_locked(efivars, var);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800420 spin_unlock_irqrestore(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (status != EFI_SUCCESS) {
423 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
424 status);
425 }
426 return status;
427}
428
Matthew Garrett68d92982013-03-02 19:40:17 -0500429static efi_status_t
430check_var_size_locked(struct efivars *efivars, u32 attributes,
431 unsigned long size)
432{
433 u64 storage_size, remaining_size, max_size;
434 efi_status_t status;
435 const struct efivar_operations *fops = efivars->ops;
436
437 if (!efivars->ops->query_variable_info)
438 return EFI_UNSUPPORTED;
439
440 status = fops->query_variable_info(attributes, &storage_size,
441 &remaining_size, &max_size);
442
443 if (status != EFI_SUCCESS)
444 return status;
445
446 if (!storage_size || size > remaining_size || size > max_size ||
447 (remaining_size - size) < (storage_size / 2))
448 return EFI_OUT_OF_RESOURCES;
449
450 return status;
451}
452
453
454static efi_status_t
455check_var_size(struct efivars *efivars, u32 attributes, unsigned long size)
456{
457 efi_status_t status;
458 unsigned long flags;
459
460 spin_lock_irqsave(&efivars->lock, flags);
461 status = check_var_size_locked(efivars, attributes, size);
462 spin_unlock_irqrestore(&efivars->lock, flags);
463
464 return status;
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467static ssize_t
468efivar_guid_read(struct efivar_entry *entry, char *buf)
469{
470 struct efi_variable *var = &entry->var;
471 char *str = buf;
472
473 if (!entry || !buf)
474 return 0;
475
476 efi_guid_unparse(&var->VendorGuid, str);
477 str += strlen(str);
478 str += sprintf(str, "\n");
479
480 return str - buf;
481}
482
483static ssize_t
484efivar_attr_read(struct efivar_entry *entry, char *buf)
485{
486 struct efi_variable *var = &entry->var;
487 char *str = buf;
488 efi_status_t status;
489
490 if (!entry || !buf)
491 return -EINVAL;
492
Mike Waychison4142ef12011-03-11 17:43:11 -0800493 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (status != EFI_SUCCESS)
495 return -EIO;
496
Khalid Aziz70839092012-09-10 12:52:42 -0600497 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600499 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600501 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600503 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
504 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
505 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
506 str += sprintf(str,
507 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
508 if (var->Attributes &
509 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
510 str += sprintf(str,
511 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
512 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
513 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return str - buf;
515}
516
517static ssize_t
518efivar_size_read(struct efivar_entry *entry, char *buf)
519{
520 struct efi_variable *var = &entry->var;
521 char *str = buf;
522 efi_status_t status;
523
524 if (!entry || !buf)
525 return -EINVAL;
526
Mike Waychison4142ef12011-03-11 17:43:11 -0800527 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (status != EFI_SUCCESS)
529 return -EIO;
530
531 str += sprintf(str, "0x%lx\n", var->DataSize);
532 return str - buf;
533}
534
535static ssize_t
536efivar_data_read(struct efivar_entry *entry, char *buf)
537{
538 struct efi_variable *var = &entry->var;
539 efi_status_t status;
540
541 if (!entry || !buf)
542 return -EINVAL;
543
Mike Waychison4142ef12011-03-11 17:43:11 -0800544 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (status != EFI_SUCCESS)
546 return -EIO;
547
548 memcpy(buf, var->Data, var->DataSize);
549 return var->DataSize;
550}
551/*
552 * We allow each variable to be edited via rewriting the
553 * entire efi variable structure.
554 */
555static ssize_t
556efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
557{
558 struct efi_variable *new_var, *var = &entry->var;
Mike Waychison4142ef12011-03-11 17:43:11 -0800559 struct efivars *efivars = entry->efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 efi_status_t status = EFI_NOT_FOUND;
561
562 if (count != sizeof(struct efi_variable))
563 return -EINVAL;
564
565 new_var = (struct efi_variable *)buf;
566 /*
567 * If only updating the variable data, then the name
568 * and guid should remain the same
569 */
570 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
571 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
572 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
573 return -EINVAL;
574 }
575
576 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
577 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
578 return -EINVAL;
579 }
580
Matthew Garrettfec6c202012-04-30 16:11:30 -0400581 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
582 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
583 printk(KERN_ERR "efivars: Malformed variable content\n");
584 return -EINVAL;
585 }
586
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800587 spin_lock_irq(&efivars->lock);
Matthew Garrett68d92982013-03-02 19:40:17 -0500588
589 status = check_var_size_locked(efivars, new_var->Attributes,
590 new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
591
592 if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
593 status = efivars->ops->set_variable(new_var->VariableName,
594 &new_var->VendorGuid,
595 new_var->Attributes,
596 new_var->DataSize,
597 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800599 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 if (status != EFI_SUCCESS) {
602 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
603 status);
604 return -EIO;
605 }
606
607 memcpy(&entry->var, new_var, count);
608 return count;
609}
610
611static ssize_t
612efivar_show_raw(struct efivar_entry *entry, char *buf)
613{
614 struct efi_variable *var = &entry->var;
615 efi_status_t status;
616
617 if (!entry || !buf)
618 return 0;
619
Mike Waychison4142ef12011-03-11 17:43:11 -0800620 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (status != EFI_SUCCESS)
622 return -EIO;
623
624 memcpy(buf, var, sizeof(*var));
625 return sizeof(*var);
626}
627
628/*
629 * Generic read/write functions that call the specific functions of
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200630 * the attributes...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
632static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
633 char *buf)
634{
635 struct efivar_entry *var = to_efivar_entry(kobj);
636 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500637 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (!capable(CAP_SYS_ADMIN))
640 return -EACCES;
641
642 if (efivar_attr->show) {
643 ret = efivar_attr->show(var, buf);
644 }
645 return ret;
646}
647
648static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
649 const char *buf, size_t count)
650{
651 struct efivar_entry *var = to_efivar_entry(kobj);
652 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500653 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 if (!capable(CAP_SYS_ADMIN))
656 return -EACCES;
657
658 if (efivar_attr->store)
659 ret = efivar_attr->store(var, buf, count);
660
661 return ret;
662}
663
Emese Revfy52cf25d2010-01-19 02:58:23 +0100664static const struct sysfs_ops efivar_attr_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 .show = efivar_attr_show,
666 .store = efivar_attr_store,
667};
668
669static void efivar_release(struct kobject *kobj)
670{
671 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 kfree(var);
673}
674
675static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
676static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
677static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
678static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
679static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
680
681static struct attribute *def_attrs[] = {
682 &efivar_attr_guid.attr,
683 &efivar_attr_size.attr,
684 &efivar_attr_attributes.attr,
685 &efivar_attr_data.attr,
686 &efivar_attr_raw_var.attr,
687 NULL,
688};
689
Greg Kroah-Hartmane89a4112007-10-11 10:47:49 -0600690static struct kobj_type efivar_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 .release = efivar_release,
692 .sysfs_ops = &efivar_attr_ops,
693 .default_attrs = def_attrs,
694};
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696static inline void
697efivar_unregister(struct efivar_entry *var)
698{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800699 kobject_put(&var->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Matthew Garrett5d9db882012-10-05 13:54:56 +0800702static int efivarfs_file_open(struct inode *inode, struct file *file)
703{
704 file->private_data = inode->i_private;
705 return 0;
706}
707
Matt Fleming7253eab2012-10-16 15:58:07 +0100708static int efi_status_to_err(efi_status_t status)
709{
710 int err;
711
712 switch (status) {
713 case EFI_INVALID_PARAMETER:
714 err = -EINVAL;
715 break;
716 case EFI_OUT_OF_RESOURCES:
717 err = -ENOSPC;
718 break;
719 case EFI_DEVICE_ERROR:
720 err = -EIO;
721 break;
722 case EFI_WRITE_PROTECTED:
723 err = -EROFS;
724 break;
725 case EFI_SECURITY_VIOLATION:
726 err = -EACCES;
727 break;
728 case EFI_NOT_FOUND:
Matt Fleming1fa7e692013-01-16 13:47:05 +0000729 err = -EIO;
Matt Fleming7253eab2012-10-16 15:58:07 +0100730 break;
731 default:
732 err = -EINVAL;
733 }
734
735 return err;
736}
737
Matthew Garrett5d9db882012-10-05 13:54:56 +0800738static ssize_t efivarfs_file_write(struct file *file,
739 const char __user *userbuf, size_t count, loff_t *ppos)
740{
741 struct efivar_entry *var = file->private_data;
742 struct efivars *efivars;
743 efi_status_t status;
744 void *data;
745 u32 attributes;
746 struct inode *inode = file->f_mapping->host;
Matt Fleming07b1c5b2012-10-23 12:35:43 +0100747 unsigned long datasize = count - sizeof(attributes);
Matthew Garrett68d92982013-03-02 19:40:17 -0500748 unsigned long newdatasize, varsize;
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100749 ssize_t bytes = 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800750
751 if (count < sizeof(attributes))
752 return -EINVAL;
753
Matt Fleming89d16662012-11-09 21:02:56 +0000754 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
755 return -EFAULT;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800756
Matt Fleming89d16662012-11-09 21:02:56 +0000757 if (attributes & ~(EFI_VARIABLE_MASK))
758 return -EINVAL;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800759
760 efivars = var->efivars;
761
Matt Fleming89d16662012-11-09 21:02:56 +0000762 /*
763 * Ensure that the user can't allocate arbitrarily large
764 * amounts of memory. Pick a default size of 64K if
765 * QueryVariableInfo() isn't supported by the firmware.
766 */
Matt Fleming89d16662012-11-09 21:02:56 +0000767
Matthew Garrett68d92982013-03-02 19:40:17 -0500768 varsize = datasize + utf16_strsize(var->var.VariableName, 1024);
769 status = check_var_size(efivars, attributes, varsize);
Matt Fleming89d16662012-11-09 21:02:56 +0000770
771 if (status != EFI_SUCCESS) {
772 if (status != EFI_UNSUPPORTED)
773 return efi_status_to_err(status);
774
Matthew Garrett68d92982013-03-02 19:40:17 -0500775 if (datasize > 65536)
776 return -ENOSPC;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800777 }
778
Matt Fleming89d16662012-11-09 21:02:56 +0000779 data = kmalloc(datasize, GFP_KERNEL);
780 if (!data)
781 return -ENOMEM;
782
Matthew Garrett5d9db882012-10-05 13:54:56 +0800783 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100784 bytes = -EFAULT;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800785 goto out;
786 }
787
788 if (validate_var(&var->var, data, datasize) == false) {
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100789 bytes = -EINVAL;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800790 goto out;
791 }
792
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800793 /*
794 * The lock here protects the get_variable call, the conditional
795 * set_variable call, and removal of the variable from the efivars
796 * list (in the case of an authenticated delete).
797 */
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800798 spin_lock_irq(&efivars->lock);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800799
Matthew Garrett68d92982013-03-02 19:40:17 -0500800 /*
801 * Ensure that the available space hasn't shrunk below the safe level
802 */
803
804 status = check_var_size_locked(efivars, attributes, varsize);
805
806 if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED) {
807 spin_unlock_irq(&efivars->lock);
808 kfree(data);
809
810 return efi_status_to_err(status);
811 }
812
Matthew Garrett5d9db882012-10-05 13:54:56 +0800813 status = efivars->ops->set_variable(var->var.VariableName,
814 &var->var.VendorGuid,
815 attributes, datasize,
816 data);
817
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800818 if (status != EFI_SUCCESS) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800819 spin_unlock_irq(&efivars->lock);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800820 kfree(data);
821
Matt Fleming7253eab2012-10-16 15:58:07 +0100822 return efi_status_to_err(status);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800823 }
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800824
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100825 bytes = count;
826
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800827 /*
828 * Writing to the variable may have caused a change in size (which
829 * could either be an append or an overwrite), or the variable to be
830 * deleted. Perform a GetVariable() so we can tell what actually
831 * happened.
832 */
833 newdatasize = 0;
834 status = efivars->ops->get_variable(var->var.VariableName,
835 &var->var.VendorGuid,
836 NULL, &newdatasize,
837 NULL);
838
839 if (status == EFI_BUFFER_TOO_SMALL) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800840 spin_unlock_irq(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800841 mutex_lock(&inode->i_mutex);
842 i_size_write(inode, newdatasize + sizeof(attributes));
843 mutex_unlock(&inode->i_mutex);
844
845 } else if (status == EFI_NOT_FOUND) {
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800846 list_del(&var->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800847 spin_unlock_irq(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800848 efivar_unregister(var);
849 drop_nlink(inode);
Matt Fleming791eb562013-01-16 21:55:36 +0000850 d_delete(file->f_dentry);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800851 dput(file->f_dentry);
852
853 } else {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800854 spin_unlock_irq(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800855 pr_warn("efivarfs: inconsistent EFI variable implementation? "
856 "status = %lx\n", status);
857 }
858
Matthew Garrett5d9db882012-10-05 13:54:56 +0800859out:
860 kfree(data);
861
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100862 return bytes;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800863}
864
865static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
866 size_t count, loff_t *ppos)
867{
868 struct efivar_entry *var = file->private_data;
869 struct efivars *efivars = var->efivars;
870 efi_status_t status;
871 unsigned long datasize = 0;
872 u32 attributes;
873 void *data;
Andy Whitcroftd142df02012-10-11 11:32:17 +0100874 ssize_t size = 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800875
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800876 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800877 status = efivars->ops->get_variable(var->var.VariableName,
878 &var->var.VendorGuid,
879 &attributes, &datasize, NULL);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800880 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800881
882 if (status != EFI_BUFFER_TOO_SMALL)
Matt Fleming7253eab2012-10-16 15:58:07 +0100883 return efi_status_to_err(status);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800884
Matt Flemingd2923842012-10-22 15:23:29 +0100885 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800886
887 if (!data)
Matt Fleming7253eab2012-10-16 15:58:07 +0100888 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800889
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800890 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800891 status = efivars->ops->get_variable(var->var.VariableName,
892 &var->var.VendorGuid,
893 &attributes, &datasize,
Matt Flemingd2923842012-10-22 15:23:29 +0100894 (data + sizeof(attributes)));
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800895 spin_unlock_irq(&efivars->lock);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800896
Matt Fleming7253eab2012-10-16 15:58:07 +0100897 if (status != EFI_SUCCESS) {
898 size = efi_status_to_err(status);
Andy Whitcroftd142df02012-10-11 11:32:17 +0100899 goto out_free;
Matt Fleming7253eab2012-10-16 15:58:07 +0100900 }
Matthew Garrett5d9db882012-10-05 13:54:56 +0800901
Matt Flemingd2923842012-10-22 15:23:29 +0100902 memcpy(data, &attributes, sizeof(attributes));
Matthew Garrett5d9db882012-10-05 13:54:56 +0800903 size = simple_read_from_buffer(userbuf, count, ppos,
Matt Flemingd2923842012-10-22 15:23:29 +0100904 data, datasize + sizeof(attributes));
Andy Whitcroftd142df02012-10-11 11:32:17 +0100905out_free:
Matthew Garrett5d9db882012-10-05 13:54:56 +0800906 kfree(data);
907
908 return size;
909}
910
911static void efivarfs_evict_inode(struct inode *inode)
912{
913 clear_inode(inode);
914}
915
916static const struct super_operations efivarfs_ops = {
917 .statfs = simple_statfs,
918 .drop_inode = generic_delete_inode,
919 .evict_inode = efivarfs_evict_inode,
920 .show_options = generic_show_options,
921};
922
923static struct super_block *efivarfs_sb;
924
925static const struct inode_operations efivarfs_dir_inode_operations;
926
927static const struct file_operations efivarfs_file_operations = {
928 .open = efivarfs_file_open,
929 .read = efivarfs_file_read,
930 .write = efivarfs_file_write,
931 .llseek = no_llseek,
932};
933
934static struct inode *efivarfs_get_inode(struct super_block *sb,
935 const struct inode *dir, int mode, dev_t dev)
936{
937 struct inode *inode = new_inode(sb);
938
939 if (inode) {
940 inode->i_ino = get_next_ino();
Matthew Garrett5d9db882012-10-05 13:54:56 +0800941 inode->i_mode = mode;
942 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
943 switch (mode & S_IFMT) {
944 case S_IFREG:
945 inode->i_fop = &efivarfs_file_operations;
946 break;
947 case S_IFDIR:
948 inode->i_op = &efivarfs_dir_inode_operations;
949 inode->i_fop = &simple_dir_operations;
950 inc_nlink(inode);
951 break;
952 }
953 }
954 return inode;
955}
956
Matt Fleming47f531e2013-01-31 19:02:03 +0000957/*
958 * Return true if 'str' is a valid efivarfs filename of the form,
959 *
960 * VariableName-12345678-1234-1234-1234-1234567891bc
961 */
962static bool efivarfs_valid_name(const char *str, int len)
963{
964 static const char dashes[GUID_LEN] = {
965 [8] = 1, [13] = 1, [18] = 1, [23] = 1
966 };
967 const char *s = str + len - GUID_LEN;
968 int i;
969
970 /*
971 * We need a GUID, plus at least one letter for the variable name,
972 * plus the '-' separator
973 */
974 if (len < GUID_LEN + 2)
975 return false;
976
Matt Fleming123abd72013-03-05 07:40:16 +0000977 /* GUID must be preceded by a '-' */
978 if (*(s - 1) != '-')
Matt Fleming47f531e2013-01-31 19:02:03 +0000979 return false;
980
981 /*
982 * Validate that 's' is of the correct format, e.g.
983 *
984 * 12345678-1234-1234-1234-123456789abc
985 */
986 for (i = 0; i < GUID_LEN; i++) {
987 if (dashes[i]) {
988 if (*s++ != '-')
989 return false;
990 } else {
991 if (!isxdigit(*s++))
992 return false;
993 }
994 }
995
996 return true;
997}
998
Matthew Garrett5d9db882012-10-05 13:54:56 +0800999static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
1000{
1001 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
1002 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
1003 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
1004 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
1005 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
1006 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
1007 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
1008 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
1009 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
1010 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
1011 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
1012 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
1013 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
1014 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
1015 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
1016 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
1017}
1018
1019static int efivarfs_create(struct inode *dir, struct dentry *dentry,
1020 umode_t mode, bool excl)
1021{
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001022 struct inode *inode;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001023 struct efivars *efivars = &__efivars;
1024 struct efivar_entry *var;
1025 int namelen, i = 0, err = 0;
1026
Matt Fleming47f531e2013-01-31 19:02:03 +00001027 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
Matthew Garrett5d9db882012-10-05 13:54:56 +08001028 return -EINVAL;
1029
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001030 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001031 if (!inode)
Matt Flemingaeeaa8d2012-10-23 12:41:03 +01001032 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001033
1034 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001035 if (!var) {
1036 err = -ENOMEM;
1037 goto out;
1038 }
Matthew Garrett5d9db882012-10-05 13:54:56 +08001039
Jeremy Kerr310ad752012-10-19 15:16:45 +08001040 /* length of the variable name itself: remove GUID and separator */
1041 namelen = dentry->d_name.len - GUID_LEN - 1;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001042
1043 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
1044 &var->var.VendorGuid);
1045
1046 for (i = 0; i < namelen; i++)
1047 var->var.VariableName[i] = dentry->d_name.name[i];
1048
1049 var->var.VariableName[i] = '\0';
1050
1051 inode->i_private = var;
1052 var->efivars = efivars;
1053 var->kobj.kset = efivars->kset;
1054
1055 err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
1056 dentry->d_name.name);
1057 if (err)
1058 goto out;
1059
1060 kobject_uevent(&var->kobj, KOBJ_ADD);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001061 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001062 list_add(&var->list, &efivars->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001063 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001064 d_instantiate(dentry, inode);
1065 dget(dentry);
1066out:
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001067 if (err) {
Matthew Garrett5d9db882012-10-05 13:54:56 +08001068 kfree(var);
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001069 iput(inode);
1070 }
Matthew Garrett5d9db882012-10-05 13:54:56 +08001071 return err;
1072}
1073
1074static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
1075{
1076 struct efivar_entry *var = dentry->d_inode->i_private;
1077 struct efivars *efivars = var->efivars;
1078 efi_status_t status;
1079
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001080 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001081
1082 status = efivars->ops->set_variable(var->var.VariableName,
1083 &var->var.VendorGuid,
1084 0, 0, NULL);
1085
1086 if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
1087 list_del(&var->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001088 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001089 efivar_unregister(var);
Lingzhu Xiangde5fe952013-01-05 13:59:52 +08001090 drop_nlink(dentry->d_inode);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001091 dput(dentry);
1092 return 0;
1093 }
1094
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001095 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001096 return -EINVAL;
1097};
1098
Matt Flemingda27a242013-02-01 11:02:28 +00001099/*
1100 * Compare two efivarfs file names.
1101 *
1102 * An efivarfs filename is composed of two parts,
1103 *
1104 * 1. A case-sensitive variable name
1105 * 2. A case-insensitive GUID
1106 *
1107 * So we need to perform a case-sensitive match on part 1 and a
1108 * case-insensitive match on part 2.
1109 */
1110static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
1111 const struct dentry *dentry, const struct inode *inode,
1112 unsigned int len, const char *str,
1113 const struct qstr *name)
1114{
1115 int guid = len - GUID_LEN;
1116
1117 if (name->len != len)
1118 return 1;
1119
1120 /* Case-sensitive compare for the variable name */
1121 if (memcmp(str, name->name, guid))
1122 return 1;
1123
1124 /* Case-insensitive compare for the GUID */
1125 return strncasecmp(name->name + guid, str + guid, GUID_LEN);
1126}
1127
1128static int efivarfs_d_hash(const struct dentry *dentry,
1129 const struct inode *inode, struct qstr *qstr)
1130{
1131 unsigned long hash = init_name_hash();
1132 const unsigned char *s = qstr->name;
1133 unsigned int len = qstr->len;
1134
1135 if (!efivarfs_valid_name(s, len))
1136 return -EINVAL;
1137
1138 while (len-- > GUID_LEN)
1139 hash = partial_name_hash(*s++, hash);
1140
1141 /* GUID is case-insensitive. */
1142 while (len--)
1143 hash = partial_name_hash(tolower(*s++), hash);
1144
1145 qstr->hash = end_name_hash(hash);
1146 return 0;
1147}
1148
1149/*
1150 * Retaining negative dentries for an in-memory filesystem just wastes
1151 * memory and lookup time: arrange for them to be deleted immediately.
1152 */
1153static int efivarfs_delete_dentry(const struct dentry *dentry)
1154{
1155 return 1;
1156}
1157
1158static struct dentry_operations efivarfs_d_ops = {
1159 .d_compare = efivarfs_d_compare,
1160 .d_hash = efivarfs_d_hash,
1161 .d_delete = efivarfs_delete_dentry,
1162};
1163
1164static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
1165{
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001166 struct dentry *d;
Matt Flemingda27a242013-02-01 11:02:28 +00001167 struct qstr q;
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001168 int err;
Matt Flemingda27a242013-02-01 11:02:28 +00001169
1170 q.name = name;
1171 q.len = strlen(name);
1172
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001173 err = efivarfs_d_hash(NULL, NULL, &q);
1174 if (err)
1175 return ERR_PTR(err);
Matt Flemingda27a242013-02-01 11:02:28 +00001176
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001177 d = d_alloc(parent, &q);
1178 if (d)
1179 return d;
1180
1181 return ERR_PTR(-ENOMEM);
Matt Flemingda27a242013-02-01 11:02:28 +00001182}
1183
Matt Fleminge83af1f2012-11-15 20:03:16 +00001184static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
Matthew Garrett5d9db882012-10-05 13:54:56 +08001185{
1186 struct inode *inode = NULL;
1187 struct dentry *root;
1188 struct efivar_entry *entry, *n;
1189 struct efivars *efivars = &__efivars;
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001190 char *name;
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001191 int err = -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001192
1193 efivarfs_sb = sb;
1194
1195 sb->s_maxbytes = MAX_LFS_FILESIZE;
1196 sb->s_blocksize = PAGE_CACHE_SIZE;
1197 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
Matt Fleming91716322012-10-22 15:51:45 +01001198 sb->s_magic = EFIVARFS_MAGIC;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001199 sb->s_op = &efivarfs_ops;
Matt Flemingda27a242013-02-01 11:02:28 +00001200 sb->s_d_op = &efivarfs_d_ops;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001201 sb->s_time_gran = 1;
1202
1203 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +01001204 if (!inode)
1205 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001206 inode->i_op = &efivarfs_dir_inode_operations;
1207
1208 root = d_make_root(inode);
1209 sb->s_root = root;
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +01001210 if (!root)
1211 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001212
1213 list_for_each_entry_safe(entry, n, &efivars->list, list) {
Matthew Garrett5d9db882012-10-05 13:54:56 +08001214 struct dentry *dentry, *root = efivarfs_sb->s_root;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001215 unsigned long size = 0;
1216 int len, i;
1217
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001218 inode = NULL;
1219
Matthew Garrett5d9db882012-10-05 13:54:56 +08001220 len = utf16_strlen(entry->var.VariableName);
1221
Jeremy Kerr310ad752012-10-19 15:16:45 +08001222 /* name, plus '-', plus GUID, plus NUL*/
1223 name = kmalloc(len + 1 + GUID_LEN + 1, GFP_ATOMIC);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001224 if (!name)
1225 goto fail;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001226
1227 for (i = 0; i < len; i++)
1228 name[i] = entry->var.VariableName[i] & 0xFF;
1229
1230 name[len] = '-';
1231
1232 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
1233
Jeremy Kerr310ad752012-10-19 15:16:45 +08001234 name[len+GUID_LEN+1] = '\0';
Matthew Garrett5d9db882012-10-05 13:54:56 +08001235
1236 inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
1237 S_IFREG | 0644, 0);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001238 if (!inode)
1239 goto fail_name;
1240
Matt Flemingda27a242013-02-01 11:02:28 +00001241 dentry = efivarfs_alloc_dentry(root, name);
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001242 if (IS_ERR(dentry)) {
1243 err = PTR_ERR(dentry);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001244 goto fail_inode;
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001245 }
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001246
Andy Whitcroftc0359db2012-10-11 11:32:20 +01001247 /* copied by the above to local storage in the dentry. */
1248 kfree(name);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001249
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001250 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001251 efivars->ops->get_variable(entry->var.VariableName,
1252 &entry->var.VendorGuid,
1253 &entry->var.Attributes,
1254 &size,
1255 NULL);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001256 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001257
1258 mutex_lock(&inode->i_mutex);
1259 inode->i_private = entry;
Matt Fleming94a193f2013-01-11 13:30:46 +00001260 i_size_write(inode, size + sizeof(entry->var.Attributes));
Matthew Garrett5d9db882012-10-05 13:54:56 +08001261 mutex_unlock(&inode->i_mutex);
1262 d_add(dentry, inode);
1263 }
1264
1265 return 0;
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001266
1267fail_inode:
1268 iput(inode);
1269fail_name:
1270 kfree(name);
1271fail:
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001272 return err;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001273}
1274
1275static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
1276 int flags, const char *dev_name, void *data)
1277{
1278 return mount_single(fs_type, flags, data, efivarfs_fill_super);
1279}
1280
1281static void efivarfs_kill_sb(struct super_block *sb)
1282{
1283 kill_litter_super(sb);
1284 efivarfs_sb = NULL;
1285}
1286
1287static struct file_system_type efivarfs_type = {
1288 .name = "efivarfs",
1289 .mount = efivarfs_mount,
1290 .kill_sb = efivarfs_kill_sb,
1291};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001292MODULE_ALIAS_FS("efivarfs");
Matthew Garrett5d9db882012-10-05 13:54:56 +08001293
Matt Flemingda27a242013-02-01 11:02:28 +00001294/*
1295 * Handle negative dentry.
1296 */
1297static struct dentry *efivarfs_lookup(struct inode *dir, struct dentry *dentry,
1298 unsigned int flags)
1299{
1300 if (dentry->d_name.len > NAME_MAX)
1301 return ERR_PTR(-ENAMETOOLONG);
1302 d_add(dentry, NULL);
1303 return NULL;
1304}
1305
Matthew Garrett5d9db882012-10-05 13:54:56 +08001306static const struct inode_operations efivarfs_dir_inode_operations = {
Matt Flemingda27a242013-02-01 11:02:28 +00001307 .lookup = efivarfs_lookup,
Matthew Garrett5d9db882012-10-05 13:54:56 +08001308 .unlink = efivarfs_unlink,
1309 .create = efivarfs_create,
1310};
1311
1312static struct pstore_info efi_pstore_info;
1313
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001314#ifdef CONFIG_PSTORE
1315
1316static int efi_pstore_open(struct pstore_info *psi)
1317{
1318 struct efivars *efivars = psi->data;
1319
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001320 spin_lock_irq(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001321 efivars->walk_entry = list_first_entry(&efivars->list,
1322 struct efivar_entry, list);
1323 return 0;
1324}
1325
1326static int efi_pstore_close(struct pstore_info *psi)
1327{
1328 struct efivars *efivars = psi->data;
1329
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001330 spin_unlock_irq(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001331 return 0;
1332}
1333
1334static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001335 int *count, struct timespec *timespec,
Kees Cookf6f82852011-11-17 12:58:07 -08001336 char **buf, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001337{
1338 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1339 struct efivars *efivars = psi->data;
1340 char name[DUMP_NAME_LEN];
1341 int i;
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001342 int cnt;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001343 unsigned int part, size;
1344 unsigned long time;
1345
1346 while (&efivars->walk_entry->list != &efivars->list) {
1347 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
1348 vendor)) {
1349 for (i = 0; i < DUMP_NAME_LEN; i++) {
1350 name[i] = efivars->walk_entry->var.VariableName[i];
1351 }
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001352 if (sscanf(name, "dump-type%u-%u-%d-%lu",
1353 type, &part, &cnt, &time) == 4) {
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001354 *id = part;
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001355 *count = cnt;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001356 timespec->tv_sec = time;
1357 timespec->tv_nsec = 0;
Seiji Aguchi0f7de852012-11-14 20:28:50 +00001358 } else if (sscanf(name, "dump-type%u-%u-%lu",
1359 type, &part, &time) == 3) {
1360 /*
1361 * Check if an old format,
1362 * which doesn't support holding
1363 * multiple logs, remains.
1364 */
1365 *id = part;
1366 *count = 0;
1367 timespec->tv_sec = time;
1368 timespec->tv_nsec = 0;
1369 } else {
1370 efivars->walk_entry = list_entry(
1371 efivars->walk_entry->list.next,
1372 struct efivar_entry, list);
1373 continue;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001374 }
Seiji Aguchi0f7de852012-11-14 20:28:50 +00001375
1376 get_var_data_locked(efivars, &efivars->walk_entry->var);
1377 size = efivars->walk_entry->var.DataSize;
1378 *buf = kmalloc(size, GFP_KERNEL);
1379 if (*buf == NULL)
1380 return -ENOMEM;
1381 memcpy(*buf, efivars->walk_entry->var.Data,
1382 size);
1383 efivars->walk_entry = list_entry(
1384 efivars->walk_entry->list.next,
1385 struct efivar_entry, list);
1386 return size;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001387 }
1388 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1389 struct efivar_entry, list);
1390 }
1391 return 0;
1392}
1393
Kees Cook3d6d8d22011-11-17 13:13:29 -08001394static int efi_pstore_write(enum pstore_type_id type,
1395 enum kmsg_dump_reason reason, u64 *id,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001396 unsigned int part, int count, size_t size,
1397 struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001398{
1399 char name[DUMP_NAME_LEN];
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001400 efi_char16_t efi_name[DUMP_NAME_LEN];
1401 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1402 struct efivars *efivars = psi->data;
Chen Gongb238b8f2011-10-12 09:17:24 -07001403 int i, ret = 0;
Seiji Aguchid80a3612012-11-14 20:25:37 +00001404 efi_status_t status = EFI_NOT_FOUND;
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001405 unsigned long flags;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001406
Seiji Aguchie59310a2013-01-11 18:10:05 +00001407 if (pstore_cannot_block_path(reason)) {
1408 /*
1409 * If the lock is taken by another cpu in non-blocking path,
1410 * this driver returns without entering firmware to avoid
1411 * hanging up.
1412 */
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001413 if (!spin_trylock_irqsave(&efivars->lock, flags))
Seiji Aguchie59310a2013-01-11 18:10:05 +00001414 return -EBUSY;
1415 } else
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001416 spin_lock_irqsave(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001417
Seiji Aguchid80a3612012-11-14 20:25:37 +00001418 /*
1419 * Check if there is a space enough to log.
1420 * size: a size of logging data
1421 * DUMP_NAME_LEN * 2: a maximum size of variable name
1422 */
Matthew Garrett68d92982013-03-02 19:40:17 -05001423
1424 status = check_var_size_locked(efivars, PSTORE_EFI_ATTRIBUTES,
1425 size + DUMP_NAME_LEN * 2);
1426
1427 if (status) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001428 spin_unlock_irqrestore(&efivars->lock, flags);
Seiji Aguchid80a3612012-11-14 20:25:37 +00001429 *id = part;
1430 return -ENOSPC;
1431 }
1432
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001433 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
Seiji Aguchi96480d92012-11-14 20:26:46 +00001434 get_seconds());
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001435
1436 for (i = 0; i < DUMP_NAME_LEN; i++)
1437 efi_name[i] = name[i];
1438
Mike Waychison7644c162011-07-21 16:58:00 -04001439 efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001440 size, psi->buf);
1441
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001442 spin_unlock_irqrestore(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001443
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001444 if (reason == KMSG_DUMP_OOPS)
1445 schedule_work(&efivar_work);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001446
Chen Gongb238b8f2011-10-12 09:17:24 -07001447 *id = part;
1448 return ret;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001449};
1450
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001451static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001452 struct timespec time, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001453{
Seiji Aguchia9efd392012-11-14 20:27:28 +00001454 char name[DUMP_NAME_LEN];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001455 efi_char16_t efi_name[DUMP_NAME_LEN];
Seiji Aguchif94ec0c2012-11-14 20:29:21 +00001456 char name_old[DUMP_NAME_LEN];
1457 efi_char16_t efi_name_old[DUMP_NAME_LEN];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001458 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1459 struct efivars *efivars = psi->data;
1460 struct efivar_entry *entry, *found = NULL;
1461 int i;
1462
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001463 sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001464 time.tv_sec);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001465
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001466 spin_lock_irq(&efivars->lock);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001467
1468 for (i = 0; i < DUMP_NAME_LEN; i++)
Seiji Aguchia9efd392012-11-14 20:27:28 +00001469 efi_name[i] = name[i];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001470
1471 /*
Seiji Aguchia9efd392012-11-14 20:27:28 +00001472 * Clean up an entry with the same name
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001473 */
1474
1475 list_for_each_entry(entry, &efivars->list, list) {
1476 get_var_data_locked(efivars, &entry->var);
1477
1478 if (efi_guidcmp(entry->var.VendorGuid, vendor))
1479 continue;
1480 if (utf16_strncmp(entry->var.VariableName, efi_name,
Seiji Aguchif94ec0c2012-11-14 20:29:21 +00001481 utf16_strlen(efi_name))) {
1482 /*
1483 * Check if an old format,
1484 * which doesn't support holding
1485 * multiple logs, remains.
1486 */
1487 sprintf(name_old, "dump-type%u-%u-%lu", type,
1488 (unsigned int)id, time.tv_sec);
1489
1490 for (i = 0; i < DUMP_NAME_LEN; i++)
1491 efi_name_old[i] = name_old[i];
1492
1493 if (utf16_strncmp(entry->var.VariableName, efi_name_old,
1494 utf16_strlen(efi_name_old)))
1495 continue;
1496 }
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001497
1498 /* found */
1499 found = entry;
1500 efivars->ops->set_variable(entry->var.VariableName,
1501 &entry->var.VendorGuid,
1502 PSTORE_EFI_ATTRIBUTES,
1503 0, NULL);
Seiji Aguchia9efd392012-11-14 20:27:28 +00001504 break;
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001505 }
1506
1507 if (found)
1508 list_del(&found->list);
1509
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001510 spin_unlock_irq(&efivars->lock);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001511
1512 if (found)
1513 efivar_unregister(found);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001514
1515 return 0;
1516}
1517#else
1518static int efi_pstore_open(struct pstore_info *psi)
1519{
1520 return 0;
1521}
1522
1523static int efi_pstore_close(struct pstore_info *psi)
1524{
1525 return 0;
1526}
1527
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001528static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type, int *count,
Christoph Fritzeee628d2011-11-28 23:49:33 +01001529 struct timespec *timespec,
1530 char **buf, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001531{
1532 return -1;
1533}
1534
Kees Cook3d6d8d22011-11-17 13:13:29 -08001535static int efi_pstore_write(enum pstore_type_id type,
1536 enum kmsg_dump_reason reason, u64 *id,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001537 unsigned int part, int count, size_t size,
1538 struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001539{
1540 return 0;
1541}
1542
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001543static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001544 struct timespec time, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001545{
1546 return 0;
1547}
1548#endif
1549
1550static struct pstore_info efi_pstore_info = {
1551 .owner = THIS_MODULE,
1552 .name = "efi",
1553 .open = efi_pstore_open,
1554 .close = efi_pstore_close,
1555 .read = efi_pstore_read,
1556 .write = efi_pstore_write,
1557 .erase = efi_pstore_erase,
1558};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Chris Wright2c3c8be2010-05-12 18:28:57 -07001560static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001561 struct bin_attribute *bin_attr,
1562 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
1564 struct efi_variable *new_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001565 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001566 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 efi_status_t status = EFI_NOT_FOUND;
1569 int found = 0;
1570
1571 if (!capable(CAP_SYS_ADMIN))
1572 return -EACCES;
1573
Matthew Garrettfec6c202012-04-30 16:11:30 -04001574 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
1575 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
1576 printk(KERN_ERR "efivars: Malformed variable content\n");
1577 return -EINVAL;
1578 }
1579
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001580 spin_lock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 /*
1583 * Does this variable already exist?
1584 */
Mike Waychison29422692011-03-11 17:43:00 -08001585 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -04001586 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1587 strsize2 = utf16_strsize(new_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (strsize1 == strsize2 &&
1589 !memcmp(&(search_efivar->var.VariableName),
1590 new_var->VariableName, strsize1) &&
1591 !efi_guidcmp(search_efivar->var.VendorGuid,
1592 new_var->VendorGuid)) {
1593 found = 1;
1594 break;
1595 }
1596 }
1597 if (found) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001598 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return -EINVAL;
1600 }
1601
Matthew Garrett68d92982013-03-02 19:40:17 -05001602 status = check_var_size_locked(efivars, new_var->Attributes,
1603 new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
1604
1605 if (status && status != EFI_UNSUPPORTED) {
1606 spin_unlock_irq(&efivars->lock);
1607 return efi_status_to_err(status);
1608 }
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /* now *really* create the variable via EFI */
Mike Waychison32958142011-03-11 17:43:21 -08001611 status = efivars->ops->set_variable(new_var->VariableName,
1612 &new_var->VendorGuid,
1613 new_var->Attributes,
1614 new_var->DataSize,
1615 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 if (status != EFI_SUCCESS) {
1618 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1619 status);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001620 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 return -EIO;
1622 }
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001623 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 /* Create the entry in sysfs. Locking is not required here */
Mike Waychison4142ef12011-03-11 17:43:11 -08001626 status = efivar_create_sysfs_entry(efivars,
Mike Waychisona2940902011-07-21 16:57:57 -04001627 utf16_strsize(new_var->VariableName,
1628 1024),
Mike Waychison4142ef12011-03-11 17:43:11 -08001629 new_var->VariableName,
1630 &new_var->VendorGuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (status) {
1632 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
1633 }
1634 return count;
1635}
1636
Chris Wright2c3c8be2010-05-12 18:28:57 -07001637static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001638 struct bin_attribute *bin_attr,
1639 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 struct efi_variable *del_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001642 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001643 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 efi_status_t status = EFI_NOT_FOUND;
1646 int found = 0;
1647
1648 if (!capable(CAP_SYS_ADMIN))
1649 return -EACCES;
1650
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001651 spin_lock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 /*
1654 * Does this variable already exist?
1655 */
Mike Waychison29422692011-03-11 17:43:00 -08001656 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -04001657 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1658 strsize2 = utf16_strsize(del_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (strsize1 == strsize2 &&
1660 !memcmp(&(search_efivar->var.VariableName),
1661 del_var->VariableName, strsize1) &&
1662 !efi_guidcmp(search_efivar->var.VendorGuid,
1663 del_var->VendorGuid)) {
1664 found = 1;
1665 break;
1666 }
1667 }
1668 if (!found) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001669 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return -EINVAL;
1671 }
1672 /* force the Attributes/DataSize to 0 to ensure deletion */
1673 del_var->Attributes = 0;
1674 del_var->DataSize = 0;
1675
Mike Waychison32958142011-03-11 17:43:21 -08001676 status = efivars->ops->set_variable(del_var->VariableName,
1677 &del_var->VendorGuid,
1678 del_var->Attributes,
1679 del_var->DataSize,
1680 del_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 if (status != EFI_SUCCESS) {
1683 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1684 status);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001685 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return -EIO;
1687 }
Matt Domsch496a0fc2007-01-26 00:57:18 -08001688 list_del(&search_efivar->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 /* We need to release this lock before unregistering. */
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001690 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 efivar_unregister(search_efivar);
1692
1693 /* It's dead Jim.... */
1694 return count;
1695}
1696
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001697static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor)
1698{
1699 struct efivar_entry *entry, *n;
1700 struct efivars *efivars = &__efivars;
1701 unsigned long strsize1, strsize2;
1702 bool found = false;
1703
1704 strsize1 = utf16_strsize(variable_name, 1024);
1705 list_for_each_entry_safe(entry, n, &efivars->list, list) {
1706 strsize2 = utf16_strsize(entry->var.VariableName, 1024);
1707 if (strsize1 == strsize2 &&
1708 !memcmp(variable_name, &(entry->var.VariableName),
1709 strsize2) &&
1710 !efi_guidcmp(entry->var.VendorGuid,
1711 *vendor)) {
1712 found = true;
1713 break;
1714 }
1715 }
1716 return found;
1717}
1718
1719static void efivar_update_sysfs_entries(struct work_struct *work)
1720{
1721 struct efivars *efivars = &__efivars;
1722 efi_guid_t vendor;
1723 efi_char16_t *variable_name;
1724 unsigned long variable_name_size = 1024;
1725 efi_status_t status = EFI_NOT_FOUND;
1726 bool found;
1727
1728 /* Add new sysfs entries */
1729 while (1) {
1730 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
1731 if (!variable_name) {
1732 pr_err("efivars: Memory allocation failed.\n");
1733 return;
1734 }
1735
1736 spin_lock_irq(&efivars->lock);
1737 found = false;
1738 while (1) {
1739 variable_name_size = 1024;
1740 status = efivars->ops->get_next_variable(
1741 &variable_name_size,
1742 variable_name,
1743 &vendor);
1744 if (status != EFI_SUCCESS) {
1745 break;
1746 } else {
1747 if (!variable_is_present(variable_name,
1748 &vendor)) {
1749 found = true;
1750 break;
1751 }
1752 }
1753 }
1754 spin_unlock_irq(&efivars->lock);
1755
1756 if (!found) {
1757 kfree(variable_name);
1758 break;
1759 } else
1760 efivar_create_sysfs_entry(efivars,
1761 variable_name_size,
1762 variable_name, &vendor);
1763 }
1764}
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766/*
1767 * Let's not leave out systab information that snuck into
1768 * the efivars driver
1769 */
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001770static ssize_t systab_show(struct kobject *kobj,
1771 struct kobj_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 char *str = buf;
1774
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001775 if (!kobj || !buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return -EINVAL;
1777
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -08001778 if (efi.mps != EFI_INVALID_TABLE_ADDR)
1779 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
1780 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
1781 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
1782 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
1783 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
1784 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
1785 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
1786 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
1787 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
1788 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
1789 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
1790 if (efi.uga != EFI_INVALID_TABLE_ADDR)
1791 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 return str - buf;
1794}
1795
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001796static struct kobj_attribute efi_attr_systab =
1797 __ATTR(systab, 0400, systab_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001799static struct attribute *efi_subsys_attrs[] = {
1800 &efi_attr_systab.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 NULL, /* maybe more in the future? */
1802};
1803
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001804static struct attribute_group efi_subsys_attr_group = {
1805 .attrs = efi_subsys_attrs,
1806};
1807
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08001808static struct kobject *efi_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810/*
1811 * efivar_create_sysfs_entry()
1812 * Requires:
1813 * variable_name_size = number of bytes required to hold
1814 * variable_name (not counting the NULL
1815 * character at the end.
Mike Waychison29422692011-03-11 17:43:00 -08001816 * efivars->lock is not held on entry or exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 * Returns 1 on failure, 0 on success
1818 */
1819static int
Mike Waychison4142ef12011-03-11 17:43:11 -08001820efivar_create_sysfs_entry(struct efivars *efivars,
1821 unsigned long variable_name_size,
1822 efi_char16_t *variable_name,
1823 efi_guid_t *vendor_guid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Jeremy Kerr310ad752012-10-19 15:16:45 +08001825 int i, short_name_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 char *short_name;
1827 struct efivar_entry *new_efivar;
1828
Jeremy Kerr310ad752012-10-19 15:16:45 +08001829 /*
1830 * Length of the variable bytes in ASCII, plus the '-' separator,
1831 * plus the GUID, plus trailing NUL
1832 */
1833 short_name_size = variable_name_size / sizeof(efi_char16_t)
1834 + 1 + GUID_LEN + 1;
1835
1836 short_name = kzalloc(short_name_size, GFP_KERNEL);
Deepak Saxena9c215382005-11-07 01:01:24 -08001837 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 if (!short_name || !new_efivar) {
Jesper Juhl0933ad92005-06-25 14:59:15 -07001840 kfree(short_name);
1841 kfree(new_efivar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return 1;
1843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Mike Waychison4142ef12011-03-11 17:43:11 -08001845 new_efivar->efivars = efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 memcpy(new_efivar->var.VariableName, variable_name,
1847 variable_name_size);
1848 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
1849
1850 /* Convert Unicode to normal chars (assume top bits are 0),
1851 ala UTF-8 */
1852 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
1853 short_name[i] = variable_name[i] & 0xFF;
1854 }
1855 /* This is ugly, but necessary to separate one vendor's
1856 private variables from another's. */
1857
1858 *(short_name + strlen(short_name)) = '-';
1859 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
1860
Mike Waychison29422692011-03-11 17:43:00 -08001861 new_efivar->kobj.kset = efivars->kset;
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001862 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
1863 "%s", short_name);
Jeff Garzik69b21862006-10-11 01:22:23 -07001864 if (i) {
1865 kfree(short_name);
1866 kfree(new_efivar);
1867 return 1;
1868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001870 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
Jesper Juhl0933ad92005-06-25 14:59:15 -07001871 kfree(short_name);
1872 short_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001874 spin_lock_irq(&efivars->lock);
Mike Waychison29422692011-03-11 17:43:00 -08001875 list_add(&new_efivar->list, &efivars->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001876 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 return 0;
1879}
Mike Waychisond502fbb2011-03-11 17:43:06 -08001880
1881static int
1882create_efivars_bin_attributes(struct efivars *efivars)
1883{
1884 struct bin_attribute *attr;
1885 int error;
1886
1887 /* new_var */
1888 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1889 if (!attr)
1890 return -ENOMEM;
1891
1892 attr->attr.name = "new_var";
1893 attr->attr.mode = 0200;
1894 attr->write = efivar_create;
1895 attr->private = efivars;
1896 efivars->new_var = attr;
1897
1898 /* del_var */
1899 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1900 if (!attr) {
1901 error = -ENOMEM;
1902 goto out_free;
1903 }
1904 attr->attr.name = "del_var";
1905 attr->attr.mode = 0200;
1906 attr->write = efivar_delete;
1907 attr->private = efivars;
1908 efivars->del_var = attr;
1909
1910 sysfs_bin_attr_init(efivars->new_var);
1911 sysfs_bin_attr_init(efivars->del_var);
1912
1913 /* Register */
1914 error = sysfs_create_bin_file(&efivars->kset->kobj,
1915 efivars->new_var);
1916 if (error) {
1917 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
1918 " due to error %d\n", error);
1919 goto out_free;
1920 }
1921 error = sysfs_create_bin_file(&efivars->kset->kobj,
1922 efivars->del_var);
1923 if (error) {
1924 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
1925 " due to error %d\n", error);
1926 sysfs_remove_bin_file(&efivars->kset->kobj,
1927 efivars->new_var);
1928 goto out_free;
1929 }
1930
1931 return 0;
1932out_free:
Dan Carpenter051d51b2011-03-18 10:12:14 +03001933 kfree(efivars->del_var);
1934 efivars->del_var = NULL;
Mike Waychisond502fbb2011-03-11 17:43:06 -08001935 kfree(efivars->new_var);
1936 efivars->new_var = NULL;
1937 return error;
1938}
1939
Mike Waychison4fc756b2011-03-11 17:43:27 -08001940void unregister_efivars(struct efivars *efivars)
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001941{
1942 struct efivar_entry *entry, *n;
Mike Waychison4142ef12011-03-11 17:43:11 -08001943
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001944 list_for_each_entry_safe(entry, n, &efivars->list, list) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001945 spin_lock_irq(&efivars->lock);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001946 list_del(&entry->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001947 spin_unlock_irq(&efivars->lock);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001948 efivar_unregister(entry);
1949 }
1950 if (efivars->new_var)
1951 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
1952 if (efivars->del_var)
1953 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
1954 kfree(efivars->new_var);
1955 kfree(efivars->del_var);
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08001956 kobject_put(efivars->kobject);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001957 kset_unregister(efivars->kset);
1958}
Mike Waychison4fc756b2011-03-11 17:43:27 -08001959EXPORT_SYMBOL_GPL(unregister_efivars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Mike Waychison4fc756b2011-03-11 17:43:27 -08001961int register_efivars(struct efivars *efivars,
1962 const struct efivar_operations *ops,
1963 struct kobject *parent_kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 efi_status_t status = EFI_NOT_FOUND;
1966 efi_guid_t vendor_guid;
1967 efi_char16_t *variable_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 unsigned long variable_name_size = 1024;
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001969 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Deepak Saxena9c215382005-11-07 01:01:24 -08001971 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (!variable_name) {
1973 printk(KERN_ERR "efivars: Memory allocation failed.\n");
1974 return -ENOMEM;
1975 }
1976
Mike Waychison29422692011-03-11 17:43:00 -08001977 spin_lock_init(&efivars->lock);
1978 INIT_LIST_HEAD(&efivars->list);
Mike Waychison32958142011-03-11 17:43:21 -08001979 efivars->ops = ops;
Mike Waychison29422692011-03-11 17:43:00 -08001980
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001981 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
Mike Waychison29422692011-03-11 17:43:00 -08001982 if (!efivars->kset) {
Greg Kroah-Hartman66ac8312007-11-02 13:20:40 -07001983 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
1984 error = -ENOMEM;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001985 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08001988 efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
1989 if (!efivars->kobject) {
1990 pr_err("efivars: Subsystem registration failed.\n");
1991 error = -ENOMEM;
1992 kset_unregister(efivars->kset);
1993 goto out;
1994 }
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 /*
1997 * Per EFI spec, the maximum storage allocated for both
1998 * the variable name and variable data is 1024 bytes.
1999 */
2000
2001 do {
2002 variable_name_size = 1024;
2003
Mike Waychison32958142011-03-11 17:43:21 -08002004 status = ops->get_next_variable(&variable_name_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 variable_name,
2006 &vendor_guid);
2007 switch (status) {
2008 case EFI_SUCCESS:
Mike Waychison4142ef12011-03-11 17:43:11 -08002009 efivar_create_sysfs_entry(efivars,
2010 variable_name_size,
2011 variable_name,
2012 &vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 break;
2014 case EFI_NOT_FOUND:
2015 break;
2016 default:
2017 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
2018 status);
2019 status = EFI_NOT_FOUND;
2020 break;
2021 }
2022 } while (status != EFI_NOT_FOUND);
2023
Mike Waychisond502fbb2011-03-11 17:43:06 -08002024 error = create_efivars_bin_attributes(efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002025 if (error)
2026 unregister_efivars(efivars);
2027
Matthew Garrett5ee9c192011-07-21 16:57:56 -04002028 efivars->efi_pstore_info = efi_pstore_info;
2029
2030 efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
2031 if (efivars->efi_pstore_info.buf) {
2032 efivars->efi_pstore_info.bufsize = 1024;
2033 efivars->efi_pstore_info.data = efivars;
Don Zickusabd4d552011-08-12 10:54:51 -07002034 spin_lock_init(&efivars->efi_pstore_info.buf_lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04002035 pstore_register(&efivars->efi_pstore_info);
2036 }
2037
Matthew Garrett5d9db882012-10-05 13:54:56 +08002038 register_filesystem(&efivarfs_type);
2039
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002040out:
2041 kfree(variable_name);
2042
2043 return error;
2044}
Mike Waychison4fc756b2011-03-11 17:43:27 -08002045EXPORT_SYMBOL_GPL(register_efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002046
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002047/*
2048 * For now we register the efi subsystem with the firmware subsystem
2049 * and the vars subsystem with the efi subsystem. In the future, it
2050 * might make sense to split off the efi subsystem into its own
2051 * driver, but for now only efivars will register with it, so just
2052 * include it here.
2053 */
2054
2055static int __init
2056efivars_init(void)
2057{
2058 int error = 0;
2059
2060 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
2061 EFIVARS_DATE);
2062
Matt Fleming83e68182012-11-14 09:42:35 +00002063 if (!efi_enabled(EFI_RUNTIME_SERVICES))
Mike Waychison4fc756b2011-03-11 17:43:27 -08002064 return 0;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002065
2066 /* For now we'll register the efi directory at /sys/firmware/efi */
2067 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
2068 if (!efi_kobj) {
2069 printk(KERN_ERR "efivars: Firmware registration failed.\n");
2070 return -ENOMEM;
2071 }
2072
Mike Waychison32958142011-03-11 17:43:21 -08002073 ops.get_variable = efi.get_variable;
2074 ops.set_variable = efi.set_variable;
2075 ops.get_next_variable = efi.get_next_variable;
Seiji Aguchid80a3612012-11-14 20:25:37 +00002076 ops.query_variable_info = efi.query_variable_info;
Matt Fleming89d16662012-11-09 21:02:56 +00002077
Mike Waychison32958142011-03-11 17:43:21 -08002078 error = register_efivars(&__efivars, &ops, efi_kobj);
Dan Carpenter3116aab2011-03-18 10:12:38 +03002079 if (error)
2080 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 /* Don't forget the systab entry */
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08002083 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002084 if (error) {
2085 printk(KERN_ERR
2086 "efivars: Sysfs attribute export failed with error %d.\n",
2087 error);
Dan Carpenter3116aab2011-03-18 10:12:38 +03002088 goto err_unregister;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Dan Carpenter3116aab2011-03-18 10:12:38 +03002091 return 0;
2092
2093err_unregister:
2094 unregister_efivars(&__efivars);
2095err_put:
2096 kobject_put(efi_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return error;
2098}
2099
2100static void __exit
2101efivars_exit(void)
2102{
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08002103 cancel_work_sync(&efivar_work);
2104
Matt Fleming83e68182012-11-14 09:42:35 +00002105 if (efi_enabled(EFI_RUNTIME_SERVICES)) {
Randy Dunlapaabb6e12011-05-06 13:27:41 -07002106 unregister_efivars(&__efivars);
2107 kobject_put(efi_kobj);
2108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
2111module_init(efivars_init);
2112module_exit(efivars_exit);
2113