blob: 6607daf5a08de47435d1e536c9b52c319ed70a54 [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
Seth Forsheeec0971b2013-03-11 16:17:50 -0500106static bool efivars_pstore_disable =
107 IS_ENABLED(EFI_VARS_PSTORE_DEFAULT_DISABLE);
108
109module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/*
112 * The maximum size of VariableName + Data = 1024
113 * Therefore, it's reasonable to save that much
114 * space in each part of the structure,
115 * and we use a page for reading/writing.
116 */
117
118struct efi_variable {
119 efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
120 efi_guid_t VendorGuid;
121 unsigned long DataSize;
122 __u8 Data[1024];
123 efi_status_t Status;
124 __u32 Attributes;
125} __attribute__((packed));
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127struct efivar_entry {
Mike Waychison4142ef12011-03-11 17:43:11 -0800128 struct efivars *efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct efi_variable var;
130 struct list_head list;
131 struct kobject kobj;
132};
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134struct efivar_attribute {
135 struct attribute attr;
136 ssize_t (*show) (struct efivar_entry *entry, char *buf);
137 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
138};
139
Matthew Garrett5d9db882012-10-05 13:54:56 +0800140static struct efivars __efivars;
141static struct efivar_operations ops;
142
Mike Waychison7644c162011-07-21 16:58:00 -0400143#define PSTORE_EFI_ATTRIBUTES \
144 (EFI_VARIABLE_NON_VOLATILE | \
145 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
146 EFI_VARIABLE_RUNTIME_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#define EFIVAR_ATTR(_name, _mode, _show, _store) \
149struct efivar_attribute efivar_attr_##_name = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900150 .attr = {.name = __stringify(_name), .mode = _mode}, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 .show = _show, \
152 .store = _store, \
153};
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
156#define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
157
158/*
159 * Prototype for sysfs creation function
160 */
161static int
Mike Waychison4142ef12011-03-11 17:43:11 -0800162efivar_create_sysfs_entry(struct efivars *efivars,
163 unsigned long variable_name_size,
164 efi_char16_t *variable_name,
165 efi_guid_t *vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Seiji Aguchia93bc0c2013-02-12 13:04:41 -0800167/*
168 * Prototype for workqueue functions updating sysfs entry
169 */
170
171static void efivar_update_sysfs_entries(struct work_struct *);
172static DECLARE_WORK(efivar_work, efivar_update_sysfs_entries);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/* Return the number of unicode characters in data */
175static unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400176utf16_strnlen(efi_char16_t *s, size_t maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 unsigned long length = 0;
179
Mike Waychisona2940902011-07-21 16:57:57 -0400180 while (*s++ != 0 && length < maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 length++;
182 return length;
183}
184
Tony Luckb728a5c2011-08-02 15:08:30 -0700185static inline unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400186utf16_strlen(efi_char16_t *s)
187{
188 return utf16_strnlen(s, ~0UL);
189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
192 * Return the number of bytes is the length of this string
193 * Note: this is NOT the same as the number of unicode characters
194 */
195static inline unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400196utf16_strsize(efi_char16_t *data, unsigned long maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Mike Waychisona2940902011-07-21 16:57:57 -0400198 return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Mike Waychison828aa1f2011-07-21 16:57:58 -0400201static inline int
202utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
203{
204 while (1) {
205 if (len == 0)
206 return 0;
207 if (*a < *b)
208 return -1;
209 if (*a > *b)
210 return 1;
211 if (*a == 0) /* implies *b == 0 */
212 return 0;
213 a++;
214 b++;
215 len--;
216 }
217}
218
Matthew Garrettfec6c202012-04-30 16:11:30 -0400219static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400220validate_device_path(struct efi_variable *var, int match, u8 *buffer,
221 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400222{
223 struct efi_generic_dev_path *node;
224 int offset = 0;
225
226 node = (struct efi_generic_dev_path *)buffer;
227
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400228 if (len < sizeof(*node))
229 return false;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400230
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400231 while (offset <= len - sizeof(*node) &&
232 node->length >= sizeof(*node) &&
233 node->length <= len - offset) {
234 offset += node->length;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400235
236 if ((node->type == EFI_DEV_END_PATH ||
237 node->type == EFI_DEV_END_PATH2) &&
238 node->sub_type == EFI_DEV_END_ENTIRE)
239 return true;
240
241 node = (struct efi_generic_dev_path *)(buffer + offset);
242 }
243
244 /*
245 * If we're here then either node->length pointed past the end
246 * of the buffer or we reached the end of the buffer without
247 * finding a device path end node.
248 */
249 return false;
250}
251
252static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400253validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
254 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400255{
256 /* An array of 16-bit integers */
257 if ((len % 2) != 0)
258 return false;
259
260 return true;
261}
262
263static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400264validate_load_option(struct efi_variable *var, int match, u8 *buffer,
265 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400266{
267 u16 filepathlength;
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400268 int i, desclength = 0, namelen;
269
270 namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
Matthew Garrettfec6c202012-04-30 16:11:30 -0400271
272 /* Either "Boot" or "Driver" followed by four digits of hex */
273 for (i = match; i < match+4; i++) {
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400274 if (var->VariableName[i] > 127 ||
275 hex_to_bin(var->VariableName[i] & 0xff) < 0)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400276 return true;
277 }
278
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400279 /* Reject it if there's 4 digits of hex and then further content */
280 if (namelen > match + 4)
281 return false;
282
283 /* A valid entry must be at least 8 bytes */
284 if (len < 8)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400285 return false;
286
287 filepathlength = buffer[4] | buffer[5] << 8;
288
289 /*
290 * There's no stored length for the description, so it has to be
291 * found by hand
292 */
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400293 desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400294
295 /* Each boot entry must have a descriptor */
296 if (!desclength)
297 return false;
298
299 /*
300 * If the sum of the length of the description, the claimed filepath
301 * length and the original header are greater than the length of the
302 * variable, it's malformed
303 */
304 if ((desclength + filepathlength + 6) > len)
305 return false;
306
307 /*
308 * And, finally, check the filepath
309 */
310 return validate_device_path(var, match, buffer + desclength + 6,
311 filepathlength);
312}
313
314static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400315validate_uint16(struct efi_variable *var, int match, u8 *buffer,
316 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400317{
318 /* A single 16-bit integer */
319 if (len != 2)
320 return false;
321
322 return true;
323}
324
325static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400326validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
327 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400328{
329 int i;
330
331 for (i = 0; i < len; i++) {
332 if (buffer[i] > 127)
333 return false;
334
335 if (buffer[i] == 0)
336 return true;
337 }
338
339 return false;
340}
341
342struct variable_validate {
343 char *name;
344 bool (*validate)(struct efi_variable *var, int match, u8 *data,
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400345 unsigned long len);
Matthew Garrettfec6c202012-04-30 16:11:30 -0400346};
347
348static const struct variable_validate variable_validate[] = {
349 { "BootNext", validate_uint16 },
350 { "BootOrder", validate_boot_order },
351 { "DriverOrder", validate_boot_order },
352 { "Boot*", validate_load_option },
353 { "Driver*", validate_load_option },
354 { "ConIn", validate_device_path },
355 { "ConInDev", validate_device_path },
356 { "ConOut", validate_device_path },
357 { "ConOutDev", validate_device_path },
358 { "ErrOut", validate_device_path },
359 { "ErrOutDev", validate_device_path },
360 { "Timeout", validate_uint16 },
361 { "Lang", validate_ascii_string },
362 { "PlatformLang", validate_ascii_string },
363 { "", NULL },
364};
365
366static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400367validate_var(struct efi_variable *var, u8 *data, unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400368{
369 int i;
370 u16 *unicode_name = var->VariableName;
371
372 for (i = 0; variable_validate[i].validate != NULL; i++) {
373 const char *name = variable_validate[i].name;
374 int match;
375
376 for (match = 0; ; match++) {
377 char c = name[match];
378 u16 u = unicode_name[match];
379
380 /* All special variables are plain ascii */
381 if (u > 127)
382 return true;
383
384 /* Wildcard in the matching name means we've matched */
385 if (c == '*')
386 return variable_validate[i].validate(var,
387 match, data, len);
388
389 /* Case sensitive match */
390 if (c != u)
391 break;
392
393 /* Reached the end of the string while matching */
394 if (!c)
395 return variable_validate[i].validate(var,
396 match, data, len);
397 }
398 }
399
400 return true;
401}
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403static efi_status_t
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400404get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 efi_status_t status;
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 var->DataSize = 1024;
Mike Waychison32958142011-03-11 17:43:21 -0800409 status = efivars->ops->get_variable(var->VariableName,
410 &var->VendorGuid,
411 &var->Attributes,
412 &var->DataSize,
413 var->Data);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400414 return status;
415}
416
417static efi_status_t
418get_var_data(struct efivars *efivars, struct efi_variable *var)
419{
420 efi_status_t status;
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800421 unsigned long flags;
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400422
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800423 spin_lock_irqsave(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400424 status = get_var_data_locked(efivars, var);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800425 spin_unlock_irqrestore(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (status != EFI_SUCCESS) {
428 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
429 status);
430 }
431 return status;
432}
433
Matthew Garrett68d92982013-03-02 19:40:17 -0500434static efi_status_t
435check_var_size_locked(struct efivars *efivars, u32 attributes,
436 unsigned long size)
437{
438 u64 storage_size, remaining_size, max_size;
439 efi_status_t status;
440 const struct efivar_operations *fops = efivars->ops;
441
442 if (!efivars->ops->query_variable_info)
443 return EFI_UNSUPPORTED;
444
445 status = fops->query_variable_info(attributes, &storage_size,
446 &remaining_size, &max_size);
447
448 if (status != EFI_SUCCESS)
449 return status;
450
451 if (!storage_size || size > remaining_size || size > max_size ||
452 (remaining_size - size) < (storage_size / 2))
453 return EFI_OUT_OF_RESOURCES;
454
455 return status;
456}
457
458
459static efi_status_t
460check_var_size(struct efivars *efivars, u32 attributes, unsigned long size)
461{
462 efi_status_t status;
463 unsigned long flags;
464
465 spin_lock_irqsave(&efivars->lock, flags);
466 status = check_var_size_locked(efivars, attributes, size);
467 spin_unlock_irqrestore(&efivars->lock, flags);
468
469 return status;
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472static ssize_t
473efivar_guid_read(struct efivar_entry *entry, char *buf)
474{
475 struct efi_variable *var = &entry->var;
476 char *str = buf;
477
478 if (!entry || !buf)
479 return 0;
480
481 efi_guid_unparse(&var->VendorGuid, str);
482 str += strlen(str);
483 str += sprintf(str, "\n");
484
485 return str - buf;
486}
487
488static ssize_t
489efivar_attr_read(struct efivar_entry *entry, char *buf)
490{
491 struct efi_variable *var = &entry->var;
492 char *str = buf;
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
Khalid Aziz70839092012-09-10 12:52:42 -0600502 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600504 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600506 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600508 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
509 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
510 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
511 str += sprintf(str,
512 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
513 if (var->Attributes &
514 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
515 str += sprintf(str,
516 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
517 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
518 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return str - buf;
520}
521
522static ssize_t
523efivar_size_read(struct efivar_entry *entry, char *buf)
524{
525 struct efi_variable *var = &entry->var;
526 char *str = buf;
527 efi_status_t status;
528
529 if (!entry || !buf)
530 return -EINVAL;
531
Mike Waychison4142ef12011-03-11 17:43:11 -0800532 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (status != EFI_SUCCESS)
534 return -EIO;
535
536 str += sprintf(str, "0x%lx\n", var->DataSize);
537 return str - buf;
538}
539
540static ssize_t
541efivar_data_read(struct efivar_entry *entry, char *buf)
542{
543 struct efi_variable *var = &entry->var;
544 efi_status_t status;
545
546 if (!entry || !buf)
547 return -EINVAL;
548
Mike Waychison4142ef12011-03-11 17:43:11 -0800549 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (status != EFI_SUCCESS)
551 return -EIO;
552
553 memcpy(buf, var->Data, var->DataSize);
554 return var->DataSize;
555}
556/*
557 * We allow each variable to be edited via rewriting the
558 * entire efi variable structure.
559 */
560static ssize_t
561efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
562{
563 struct efi_variable *new_var, *var = &entry->var;
Mike Waychison4142ef12011-03-11 17:43:11 -0800564 struct efivars *efivars = entry->efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 efi_status_t status = EFI_NOT_FOUND;
566
567 if (count != sizeof(struct efi_variable))
568 return -EINVAL;
569
570 new_var = (struct efi_variable *)buf;
571 /*
572 * If only updating the variable data, then the name
573 * and guid should remain the same
574 */
575 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
576 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
577 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
578 return -EINVAL;
579 }
580
581 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
582 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
583 return -EINVAL;
584 }
585
Matthew Garrettfec6c202012-04-30 16:11:30 -0400586 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
587 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
588 printk(KERN_ERR "efivars: Malformed variable content\n");
589 return -EINVAL;
590 }
591
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800592 spin_lock_irq(&efivars->lock);
Matthew Garrett68d92982013-03-02 19:40:17 -0500593
594 status = check_var_size_locked(efivars, new_var->Attributes,
595 new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
596
597 if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
598 status = efivars->ops->set_variable(new_var->VariableName,
599 &new_var->VendorGuid,
600 new_var->Attributes,
601 new_var->DataSize,
602 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800604 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 if (status != EFI_SUCCESS) {
607 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
608 status);
609 return -EIO;
610 }
611
612 memcpy(&entry->var, new_var, count);
613 return count;
614}
615
616static ssize_t
617efivar_show_raw(struct efivar_entry *entry, char *buf)
618{
619 struct efi_variable *var = &entry->var;
620 efi_status_t status;
621
622 if (!entry || !buf)
623 return 0;
624
Mike Waychison4142ef12011-03-11 17:43:11 -0800625 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (status != EFI_SUCCESS)
627 return -EIO;
628
629 memcpy(buf, var, sizeof(*var));
630 return sizeof(*var);
631}
632
633/*
634 * Generic read/write functions that call the specific functions of
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200635 * the attributes...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
637static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
638 char *buf)
639{
640 struct efivar_entry *var = to_efivar_entry(kobj);
641 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500642 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 if (!capable(CAP_SYS_ADMIN))
645 return -EACCES;
646
647 if (efivar_attr->show) {
648 ret = efivar_attr->show(var, buf);
649 }
650 return ret;
651}
652
653static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
654 const char *buf, size_t count)
655{
656 struct efivar_entry *var = to_efivar_entry(kobj);
657 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500658 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (!capable(CAP_SYS_ADMIN))
661 return -EACCES;
662
663 if (efivar_attr->store)
664 ret = efivar_attr->store(var, buf, count);
665
666 return ret;
667}
668
Emese Revfy52cf25d2010-01-19 02:58:23 +0100669static const struct sysfs_ops efivar_attr_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 .show = efivar_attr_show,
671 .store = efivar_attr_store,
672};
673
674static void efivar_release(struct kobject *kobj)
675{
676 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 kfree(var);
678}
679
680static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
681static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
682static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
683static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
684static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
685
686static struct attribute *def_attrs[] = {
687 &efivar_attr_guid.attr,
688 &efivar_attr_size.attr,
689 &efivar_attr_attributes.attr,
690 &efivar_attr_data.attr,
691 &efivar_attr_raw_var.attr,
692 NULL,
693};
694
Greg Kroah-Hartmane89a4112007-10-11 10:47:49 -0600695static struct kobj_type efivar_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 .release = efivar_release,
697 .sysfs_ops = &efivar_attr_ops,
698 .default_attrs = def_attrs,
699};
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701static inline void
702efivar_unregister(struct efivar_entry *var)
703{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800704 kobject_put(&var->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Matthew Garrett5d9db882012-10-05 13:54:56 +0800707static int efivarfs_file_open(struct inode *inode, struct file *file)
708{
709 file->private_data = inode->i_private;
710 return 0;
711}
712
Matt Fleming7253eab2012-10-16 15:58:07 +0100713static int efi_status_to_err(efi_status_t status)
714{
715 int err;
716
717 switch (status) {
718 case EFI_INVALID_PARAMETER:
719 err = -EINVAL;
720 break;
721 case EFI_OUT_OF_RESOURCES:
722 err = -ENOSPC;
723 break;
724 case EFI_DEVICE_ERROR:
725 err = -EIO;
726 break;
727 case EFI_WRITE_PROTECTED:
728 err = -EROFS;
729 break;
730 case EFI_SECURITY_VIOLATION:
731 err = -EACCES;
732 break;
733 case EFI_NOT_FOUND:
Matt Fleming1fa7e692013-01-16 13:47:05 +0000734 err = -EIO;
Matt Fleming7253eab2012-10-16 15:58:07 +0100735 break;
736 default:
737 err = -EINVAL;
738 }
739
740 return err;
741}
742
Matthew Garrett5d9db882012-10-05 13:54:56 +0800743static ssize_t efivarfs_file_write(struct file *file,
744 const char __user *userbuf, size_t count, loff_t *ppos)
745{
746 struct efivar_entry *var = file->private_data;
747 struct efivars *efivars;
748 efi_status_t status;
749 void *data;
750 u32 attributes;
751 struct inode *inode = file->f_mapping->host;
Matt Fleming07b1c5b2012-10-23 12:35:43 +0100752 unsigned long datasize = count - sizeof(attributes);
Matthew Garrett68d92982013-03-02 19:40:17 -0500753 unsigned long newdatasize, varsize;
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100754 ssize_t bytes = 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800755
756 if (count < sizeof(attributes))
757 return -EINVAL;
758
Matt Fleming89d16662012-11-09 21:02:56 +0000759 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
760 return -EFAULT;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800761
Matt Fleming89d16662012-11-09 21:02:56 +0000762 if (attributes & ~(EFI_VARIABLE_MASK))
763 return -EINVAL;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800764
765 efivars = var->efivars;
766
Matt Fleming89d16662012-11-09 21:02:56 +0000767 /*
768 * Ensure that the user can't allocate arbitrarily large
769 * amounts of memory. Pick a default size of 64K if
770 * QueryVariableInfo() isn't supported by the firmware.
771 */
Matt Fleming89d16662012-11-09 21:02:56 +0000772
Matthew Garrett68d92982013-03-02 19:40:17 -0500773 varsize = datasize + utf16_strsize(var->var.VariableName, 1024);
774 status = check_var_size(efivars, attributes, varsize);
Matt Fleming89d16662012-11-09 21:02:56 +0000775
776 if (status != EFI_SUCCESS) {
777 if (status != EFI_UNSUPPORTED)
778 return efi_status_to_err(status);
779
Matthew Garrett68d92982013-03-02 19:40:17 -0500780 if (datasize > 65536)
781 return -ENOSPC;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800782 }
783
Matt Fleming89d16662012-11-09 21:02:56 +0000784 data = kmalloc(datasize, GFP_KERNEL);
785 if (!data)
786 return -ENOMEM;
787
Matthew Garrett5d9db882012-10-05 13:54:56 +0800788 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100789 bytes = -EFAULT;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800790 goto out;
791 }
792
793 if (validate_var(&var->var, data, datasize) == false) {
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100794 bytes = -EINVAL;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800795 goto out;
796 }
797
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800798 /*
799 * The lock here protects the get_variable call, the conditional
800 * set_variable call, and removal of the variable from the efivars
801 * list (in the case of an authenticated delete).
802 */
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800803 spin_lock_irq(&efivars->lock);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800804
Matthew Garrett68d92982013-03-02 19:40:17 -0500805 /*
806 * Ensure that the available space hasn't shrunk below the safe level
807 */
808
809 status = check_var_size_locked(efivars, attributes, varsize);
810
811 if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED) {
812 spin_unlock_irq(&efivars->lock);
813 kfree(data);
814
815 return efi_status_to_err(status);
816 }
817
Matthew Garrett5d9db882012-10-05 13:54:56 +0800818 status = efivars->ops->set_variable(var->var.VariableName,
819 &var->var.VendorGuid,
820 attributes, datasize,
821 data);
822
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800823 if (status != EFI_SUCCESS) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800824 spin_unlock_irq(&efivars->lock);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800825 kfree(data);
826
Matt Fleming7253eab2012-10-16 15:58:07 +0100827 return efi_status_to_err(status);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800828 }
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800829
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100830 bytes = count;
831
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800832 /*
833 * Writing to the variable may have caused a change in size (which
834 * could either be an append or an overwrite), or the variable to be
835 * deleted. Perform a GetVariable() so we can tell what actually
836 * happened.
837 */
838 newdatasize = 0;
839 status = efivars->ops->get_variable(var->var.VariableName,
840 &var->var.VendorGuid,
841 NULL, &newdatasize,
842 NULL);
843
844 if (status == EFI_BUFFER_TOO_SMALL) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800845 spin_unlock_irq(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800846 mutex_lock(&inode->i_mutex);
847 i_size_write(inode, newdatasize + sizeof(attributes));
848 mutex_unlock(&inode->i_mutex);
849
850 } else if (status == EFI_NOT_FOUND) {
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800851 list_del(&var->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800852 spin_unlock_irq(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800853 efivar_unregister(var);
854 drop_nlink(inode);
Matt Fleming791eb562013-01-16 21:55:36 +0000855 d_delete(file->f_dentry);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800856 dput(file->f_dentry);
857
858 } else {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800859 spin_unlock_irq(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800860 pr_warn("efivarfs: inconsistent EFI variable implementation? "
861 "status = %lx\n", status);
862 }
863
Matthew Garrett5d9db882012-10-05 13:54:56 +0800864out:
865 kfree(data);
866
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100867 return bytes;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800868}
869
870static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
871 size_t count, loff_t *ppos)
872{
873 struct efivar_entry *var = file->private_data;
874 struct efivars *efivars = var->efivars;
875 efi_status_t status;
876 unsigned long datasize = 0;
877 u32 attributes;
878 void *data;
Andy Whitcroftd142df02012-10-11 11:32:17 +0100879 ssize_t size = 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800880
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800881 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800882 status = efivars->ops->get_variable(var->var.VariableName,
883 &var->var.VendorGuid,
884 &attributes, &datasize, NULL);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800885 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800886
887 if (status != EFI_BUFFER_TOO_SMALL)
Matt Fleming7253eab2012-10-16 15:58:07 +0100888 return efi_status_to_err(status);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800889
Matt Flemingd2923842012-10-22 15:23:29 +0100890 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800891
892 if (!data)
Matt Fleming7253eab2012-10-16 15:58:07 +0100893 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800894
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800895 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800896 status = efivars->ops->get_variable(var->var.VariableName,
897 &var->var.VendorGuid,
898 &attributes, &datasize,
Matt Flemingd2923842012-10-22 15:23:29 +0100899 (data + sizeof(attributes)));
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800900 spin_unlock_irq(&efivars->lock);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800901
Matt Fleming7253eab2012-10-16 15:58:07 +0100902 if (status != EFI_SUCCESS) {
903 size = efi_status_to_err(status);
Andy Whitcroftd142df02012-10-11 11:32:17 +0100904 goto out_free;
Matt Fleming7253eab2012-10-16 15:58:07 +0100905 }
Matthew Garrett5d9db882012-10-05 13:54:56 +0800906
Matt Flemingd2923842012-10-22 15:23:29 +0100907 memcpy(data, &attributes, sizeof(attributes));
Matthew Garrett5d9db882012-10-05 13:54:56 +0800908 size = simple_read_from_buffer(userbuf, count, ppos,
Matt Flemingd2923842012-10-22 15:23:29 +0100909 data, datasize + sizeof(attributes));
Andy Whitcroftd142df02012-10-11 11:32:17 +0100910out_free:
Matthew Garrett5d9db882012-10-05 13:54:56 +0800911 kfree(data);
912
913 return size;
914}
915
916static void efivarfs_evict_inode(struct inode *inode)
917{
918 clear_inode(inode);
919}
920
921static const struct super_operations efivarfs_ops = {
922 .statfs = simple_statfs,
923 .drop_inode = generic_delete_inode,
924 .evict_inode = efivarfs_evict_inode,
925 .show_options = generic_show_options,
926};
927
928static struct super_block *efivarfs_sb;
929
930static const struct inode_operations efivarfs_dir_inode_operations;
931
932static const struct file_operations efivarfs_file_operations = {
933 .open = efivarfs_file_open,
934 .read = efivarfs_file_read,
935 .write = efivarfs_file_write,
936 .llseek = no_llseek,
937};
938
939static struct inode *efivarfs_get_inode(struct super_block *sb,
940 const struct inode *dir, int mode, dev_t dev)
941{
942 struct inode *inode = new_inode(sb);
943
944 if (inode) {
945 inode->i_ino = get_next_ino();
Matthew Garrett5d9db882012-10-05 13:54:56 +0800946 inode->i_mode = mode;
947 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
948 switch (mode & S_IFMT) {
949 case S_IFREG:
950 inode->i_fop = &efivarfs_file_operations;
951 break;
952 case S_IFDIR:
953 inode->i_op = &efivarfs_dir_inode_operations;
954 inode->i_fop = &simple_dir_operations;
955 inc_nlink(inode);
956 break;
957 }
958 }
959 return inode;
960}
961
Matt Fleming47f531e2013-01-31 19:02:03 +0000962/*
963 * Return true if 'str' is a valid efivarfs filename of the form,
964 *
965 * VariableName-12345678-1234-1234-1234-1234567891bc
966 */
967static bool efivarfs_valid_name(const char *str, int len)
968{
969 static const char dashes[GUID_LEN] = {
970 [8] = 1, [13] = 1, [18] = 1, [23] = 1
971 };
972 const char *s = str + len - GUID_LEN;
973 int i;
974
975 /*
976 * We need a GUID, plus at least one letter for the variable name,
977 * plus the '-' separator
978 */
979 if (len < GUID_LEN + 2)
980 return false;
981
Matt Fleming123abd72013-03-05 07:40:16 +0000982 /* GUID must be preceded by a '-' */
983 if (*(s - 1) != '-')
Matt Fleming47f531e2013-01-31 19:02:03 +0000984 return false;
985
986 /*
987 * Validate that 's' is of the correct format, e.g.
988 *
989 * 12345678-1234-1234-1234-123456789abc
990 */
991 for (i = 0; i < GUID_LEN; i++) {
992 if (dashes[i]) {
993 if (*s++ != '-')
994 return false;
995 } else {
996 if (!isxdigit(*s++))
997 return false;
998 }
999 }
1000
1001 return true;
1002}
1003
Matthew Garrett5d9db882012-10-05 13:54:56 +08001004static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
1005{
1006 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
1007 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
1008 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
1009 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
1010 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
1011 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
1012 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
1013 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
1014 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
1015 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
1016 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
1017 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
1018 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
1019 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
1020 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
1021 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
1022}
1023
1024static int efivarfs_create(struct inode *dir, struct dentry *dentry,
1025 umode_t mode, bool excl)
1026{
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001027 struct inode *inode;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001028 struct efivars *efivars = &__efivars;
1029 struct efivar_entry *var;
1030 int namelen, i = 0, err = 0;
1031
Matt Fleming47f531e2013-01-31 19:02:03 +00001032 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
Matthew Garrett5d9db882012-10-05 13:54:56 +08001033 return -EINVAL;
1034
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001035 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001036 if (!inode)
Matt Flemingaeeaa8d2012-10-23 12:41:03 +01001037 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001038
1039 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001040 if (!var) {
1041 err = -ENOMEM;
1042 goto out;
1043 }
Matthew Garrett5d9db882012-10-05 13:54:56 +08001044
Jeremy Kerr310ad752012-10-19 15:16:45 +08001045 /* length of the variable name itself: remove GUID and separator */
1046 namelen = dentry->d_name.len - GUID_LEN - 1;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001047
1048 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
1049 &var->var.VendorGuid);
1050
1051 for (i = 0; i < namelen; i++)
1052 var->var.VariableName[i] = dentry->d_name.name[i];
1053
1054 var->var.VariableName[i] = '\0';
1055
1056 inode->i_private = var;
1057 var->efivars = efivars;
1058 var->kobj.kset = efivars->kset;
1059
1060 err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
1061 dentry->d_name.name);
1062 if (err)
1063 goto out;
1064
1065 kobject_uevent(&var->kobj, KOBJ_ADD);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001066 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001067 list_add(&var->list, &efivars->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001068 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001069 d_instantiate(dentry, inode);
1070 dget(dentry);
1071out:
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001072 if (err) {
Matthew Garrett5d9db882012-10-05 13:54:56 +08001073 kfree(var);
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001074 iput(inode);
1075 }
Matthew Garrett5d9db882012-10-05 13:54:56 +08001076 return err;
1077}
1078
1079static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
1080{
1081 struct efivar_entry *var = dentry->d_inode->i_private;
1082 struct efivars *efivars = var->efivars;
1083 efi_status_t status;
1084
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001085 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001086
1087 status = efivars->ops->set_variable(var->var.VariableName,
1088 &var->var.VendorGuid,
1089 0, 0, NULL);
1090
1091 if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
1092 list_del(&var->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001093 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001094 efivar_unregister(var);
Lingzhu Xiangde5fe952013-01-05 13:59:52 +08001095 drop_nlink(dentry->d_inode);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001096 dput(dentry);
1097 return 0;
1098 }
1099
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001100 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001101 return -EINVAL;
1102};
1103
Matt Flemingda27a242013-02-01 11:02:28 +00001104/*
1105 * Compare two efivarfs file names.
1106 *
1107 * An efivarfs filename is composed of two parts,
1108 *
1109 * 1. A case-sensitive variable name
1110 * 2. A case-insensitive GUID
1111 *
1112 * So we need to perform a case-sensitive match on part 1 and a
1113 * case-insensitive match on part 2.
1114 */
1115static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
1116 const struct dentry *dentry, const struct inode *inode,
1117 unsigned int len, const char *str,
1118 const struct qstr *name)
1119{
1120 int guid = len - GUID_LEN;
1121
1122 if (name->len != len)
1123 return 1;
1124
1125 /* Case-sensitive compare for the variable name */
1126 if (memcmp(str, name->name, guid))
1127 return 1;
1128
1129 /* Case-insensitive compare for the GUID */
1130 return strncasecmp(name->name + guid, str + guid, GUID_LEN);
1131}
1132
1133static int efivarfs_d_hash(const struct dentry *dentry,
1134 const struct inode *inode, struct qstr *qstr)
1135{
1136 unsigned long hash = init_name_hash();
1137 const unsigned char *s = qstr->name;
1138 unsigned int len = qstr->len;
1139
1140 if (!efivarfs_valid_name(s, len))
1141 return -EINVAL;
1142
1143 while (len-- > GUID_LEN)
1144 hash = partial_name_hash(*s++, hash);
1145
1146 /* GUID is case-insensitive. */
1147 while (len--)
1148 hash = partial_name_hash(tolower(*s++), hash);
1149
1150 qstr->hash = end_name_hash(hash);
1151 return 0;
1152}
1153
1154/*
1155 * Retaining negative dentries for an in-memory filesystem just wastes
1156 * memory and lookup time: arrange for them to be deleted immediately.
1157 */
1158static int efivarfs_delete_dentry(const struct dentry *dentry)
1159{
1160 return 1;
1161}
1162
1163static struct dentry_operations efivarfs_d_ops = {
1164 .d_compare = efivarfs_d_compare,
1165 .d_hash = efivarfs_d_hash,
1166 .d_delete = efivarfs_delete_dentry,
1167};
1168
1169static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
1170{
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001171 struct dentry *d;
Matt Flemingda27a242013-02-01 11:02:28 +00001172 struct qstr q;
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001173 int err;
Matt Flemingda27a242013-02-01 11:02:28 +00001174
1175 q.name = name;
1176 q.len = strlen(name);
1177
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001178 err = efivarfs_d_hash(NULL, NULL, &q);
1179 if (err)
1180 return ERR_PTR(err);
Matt Flemingda27a242013-02-01 11:02:28 +00001181
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001182 d = d_alloc(parent, &q);
1183 if (d)
1184 return d;
1185
1186 return ERR_PTR(-ENOMEM);
Matt Flemingda27a242013-02-01 11:02:28 +00001187}
1188
Matt Fleminge83af1f2012-11-15 20:03:16 +00001189static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
Matthew Garrett5d9db882012-10-05 13:54:56 +08001190{
1191 struct inode *inode = NULL;
1192 struct dentry *root;
1193 struct efivar_entry *entry, *n;
1194 struct efivars *efivars = &__efivars;
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001195 char *name;
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001196 int err = -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001197
1198 efivarfs_sb = sb;
1199
1200 sb->s_maxbytes = MAX_LFS_FILESIZE;
1201 sb->s_blocksize = PAGE_CACHE_SIZE;
1202 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
Matt Fleming91716322012-10-22 15:51:45 +01001203 sb->s_magic = EFIVARFS_MAGIC;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001204 sb->s_op = &efivarfs_ops;
Matt Flemingda27a242013-02-01 11:02:28 +00001205 sb->s_d_op = &efivarfs_d_ops;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001206 sb->s_time_gran = 1;
1207
1208 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +01001209 if (!inode)
1210 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001211 inode->i_op = &efivarfs_dir_inode_operations;
1212
1213 root = d_make_root(inode);
1214 sb->s_root = root;
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +01001215 if (!root)
1216 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001217
1218 list_for_each_entry_safe(entry, n, &efivars->list, list) {
Matthew Garrett5d9db882012-10-05 13:54:56 +08001219 struct dentry *dentry, *root = efivarfs_sb->s_root;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001220 unsigned long size = 0;
1221 int len, i;
1222
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001223 inode = NULL;
1224
Matthew Garrett5d9db882012-10-05 13:54:56 +08001225 len = utf16_strlen(entry->var.VariableName);
1226
Jeremy Kerr310ad752012-10-19 15:16:45 +08001227 /* name, plus '-', plus GUID, plus NUL*/
1228 name = kmalloc(len + 1 + GUID_LEN + 1, GFP_ATOMIC);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001229 if (!name)
1230 goto fail;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001231
1232 for (i = 0; i < len; i++)
1233 name[i] = entry->var.VariableName[i] & 0xFF;
1234
1235 name[len] = '-';
1236
1237 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
1238
Jeremy Kerr310ad752012-10-19 15:16:45 +08001239 name[len+GUID_LEN+1] = '\0';
Matthew Garrett5d9db882012-10-05 13:54:56 +08001240
1241 inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
1242 S_IFREG | 0644, 0);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001243 if (!inode)
1244 goto fail_name;
1245
Matt Flemingda27a242013-02-01 11:02:28 +00001246 dentry = efivarfs_alloc_dentry(root, name);
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001247 if (IS_ERR(dentry)) {
1248 err = PTR_ERR(dentry);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001249 goto fail_inode;
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001250 }
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001251
Andy Whitcroftc0359db2012-10-11 11:32:20 +01001252 /* copied by the above to local storage in the dentry. */
1253 kfree(name);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001254
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001255 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001256 efivars->ops->get_variable(entry->var.VariableName,
1257 &entry->var.VendorGuid,
1258 &entry->var.Attributes,
1259 &size,
1260 NULL);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001261 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001262
1263 mutex_lock(&inode->i_mutex);
1264 inode->i_private = entry;
Matt Fleming94a193f2013-01-11 13:30:46 +00001265 i_size_write(inode, size + sizeof(entry->var.Attributes));
Matthew Garrett5d9db882012-10-05 13:54:56 +08001266 mutex_unlock(&inode->i_mutex);
1267 d_add(dentry, inode);
1268 }
1269
1270 return 0;
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001271
1272fail_inode:
1273 iput(inode);
1274fail_name:
1275 kfree(name);
1276fail:
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001277 return err;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001278}
1279
1280static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
1281 int flags, const char *dev_name, void *data)
1282{
1283 return mount_single(fs_type, flags, data, efivarfs_fill_super);
1284}
1285
1286static void efivarfs_kill_sb(struct super_block *sb)
1287{
1288 kill_litter_super(sb);
1289 efivarfs_sb = NULL;
1290}
1291
1292static struct file_system_type efivarfs_type = {
1293 .name = "efivarfs",
1294 .mount = efivarfs_mount,
1295 .kill_sb = efivarfs_kill_sb,
1296};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001297MODULE_ALIAS_FS("efivarfs");
Matthew Garrett5d9db882012-10-05 13:54:56 +08001298
Matt Flemingda27a242013-02-01 11:02:28 +00001299/*
1300 * Handle negative dentry.
1301 */
1302static struct dentry *efivarfs_lookup(struct inode *dir, struct dentry *dentry,
1303 unsigned int flags)
1304{
1305 if (dentry->d_name.len > NAME_MAX)
1306 return ERR_PTR(-ENAMETOOLONG);
1307 d_add(dentry, NULL);
1308 return NULL;
1309}
1310
Matthew Garrett5d9db882012-10-05 13:54:56 +08001311static const struct inode_operations efivarfs_dir_inode_operations = {
Matt Flemingda27a242013-02-01 11:02:28 +00001312 .lookup = efivarfs_lookup,
Matthew Garrett5d9db882012-10-05 13:54:56 +08001313 .unlink = efivarfs_unlink,
1314 .create = efivarfs_create,
1315};
1316
Seth Forsheeed9dc8c2013-03-07 11:40:17 -06001317#ifdef CONFIG_EFI_VARS_PSTORE
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001318
1319static int efi_pstore_open(struct pstore_info *psi)
1320{
1321 struct efivars *efivars = psi->data;
1322
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001323 spin_lock_irq(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001324 efivars->walk_entry = list_first_entry(&efivars->list,
1325 struct efivar_entry, list);
1326 return 0;
1327}
1328
1329static int efi_pstore_close(struct pstore_info *psi)
1330{
1331 struct efivars *efivars = psi->data;
1332
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001333 spin_unlock_irq(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001334 return 0;
1335}
1336
1337static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001338 int *count, struct timespec *timespec,
Kees Cookf6f82852011-11-17 12:58:07 -08001339 char **buf, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001340{
1341 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1342 struct efivars *efivars = psi->data;
1343 char name[DUMP_NAME_LEN];
1344 int i;
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001345 int cnt;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001346 unsigned int part, size;
1347 unsigned long time;
1348
1349 while (&efivars->walk_entry->list != &efivars->list) {
1350 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
1351 vendor)) {
1352 for (i = 0; i < DUMP_NAME_LEN; i++) {
1353 name[i] = efivars->walk_entry->var.VariableName[i];
1354 }
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001355 if (sscanf(name, "dump-type%u-%u-%d-%lu",
1356 type, &part, &cnt, &time) == 4) {
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001357 *id = part;
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001358 *count = cnt;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001359 timespec->tv_sec = time;
1360 timespec->tv_nsec = 0;
Seiji Aguchi0f7de852012-11-14 20:28:50 +00001361 } else if (sscanf(name, "dump-type%u-%u-%lu",
1362 type, &part, &time) == 3) {
1363 /*
1364 * Check if an old format,
1365 * which doesn't support holding
1366 * multiple logs, remains.
1367 */
1368 *id = part;
1369 *count = 0;
1370 timespec->tv_sec = time;
1371 timespec->tv_nsec = 0;
1372 } else {
1373 efivars->walk_entry = list_entry(
1374 efivars->walk_entry->list.next,
1375 struct efivar_entry, list);
1376 continue;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001377 }
Seiji Aguchi0f7de852012-11-14 20:28:50 +00001378
1379 get_var_data_locked(efivars, &efivars->walk_entry->var);
1380 size = efivars->walk_entry->var.DataSize;
1381 *buf = kmalloc(size, GFP_KERNEL);
1382 if (*buf == NULL)
1383 return -ENOMEM;
1384 memcpy(*buf, efivars->walk_entry->var.Data,
1385 size);
1386 efivars->walk_entry = list_entry(
1387 efivars->walk_entry->list.next,
1388 struct efivar_entry, list);
1389 return size;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001390 }
1391 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1392 struct efivar_entry, list);
1393 }
1394 return 0;
1395}
1396
Kees Cook3d6d8d22011-11-17 13:13:29 -08001397static int efi_pstore_write(enum pstore_type_id type,
1398 enum kmsg_dump_reason reason, u64 *id,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001399 unsigned int part, int count, size_t size,
1400 struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001401{
1402 char name[DUMP_NAME_LEN];
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001403 efi_char16_t efi_name[DUMP_NAME_LEN];
1404 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1405 struct efivars *efivars = psi->data;
Chen Gongb238b8f2011-10-12 09:17:24 -07001406 int i, ret = 0;
Seiji Aguchid80a3612012-11-14 20:25:37 +00001407 efi_status_t status = EFI_NOT_FOUND;
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001408 unsigned long flags;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001409
Seiji Aguchie59310a2013-01-11 18:10:05 +00001410 if (pstore_cannot_block_path(reason)) {
1411 /*
1412 * If the lock is taken by another cpu in non-blocking path,
1413 * this driver returns without entering firmware to avoid
1414 * hanging up.
1415 */
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001416 if (!spin_trylock_irqsave(&efivars->lock, flags))
Seiji Aguchie59310a2013-01-11 18:10:05 +00001417 return -EBUSY;
1418 } else
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001419 spin_lock_irqsave(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001420
Seiji Aguchid80a3612012-11-14 20:25:37 +00001421 /*
1422 * Check if there is a space enough to log.
1423 * size: a size of logging data
1424 * DUMP_NAME_LEN * 2: a maximum size of variable name
1425 */
Matthew Garrett68d92982013-03-02 19:40:17 -05001426
1427 status = check_var_size_locked(efivars, PSTORE_EFI_ATTRIBUTES,
1428 size + DUMP_NAME_LEN * 2);
1429
1430 if (status) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001431 spin_unlock_irqrestore(&efivars->lock, flags);
Seiji Aguchid80a3612012-11-14 20:25:37 +00001432 *id = part;
1433 return -ENOSPC;
1434 }
1435
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001436 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
Seiji Aguchi96480d92012-11-14 20:26:46 +00001437 get_seconds());
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001438
1439 for (i = 0; i < DUMP_NAME_LEN; i++)
1440 efi_name[i] = name[i];
1441
Mike Waychison7644c162011-07-21 16:58:00 -04001442 efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001443 size, psi->buf);
1444
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001445 spin_unlock_irqrestore(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001446
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001447 if (reason == KMSG_DUMP_OOPS)
1448 schedule_work(&efivar_work);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001449
Chen Gongb238b8f2011-10-12 09:17:24 -07001450 *id = part;
1451 return ret;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001452};
1453
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001454static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001455 struct timespec time, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001456{
Seiji Aguchia9efd392012-11-14 20:27:28 +00001457 char name[DUMP_NAME_LEN];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001458 efi_char16_t efi_name[DUMP_NAME_LEN];
Seiji Aguchif94ec0c2012-11-14 20:29:21 +00001459 char name_old[DUMP_NAME_LEN];
1460 efi_char16_t efi_name_old[DUMP_NAME_LEN];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001461 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1462 struct efivars *efivars = psi->data;
1463 struct efivar_entry *entry, *found = NULL;
1464 int i;
1465
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001466 sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001467 time.tv_sec);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001468
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001469 spin_lock_irq(&efivars->lock);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001470
1471 for (i = 0; i < DUMP_NAME_LEN; i++)
Seiji Aguchia9efd392012-11-14 20:27:28 +00001472 efi_name[i] = name[i];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001473
1474 /*
Seiji Aguchia9efd392012-11-14 20:27:28 +00001475 * Clean up an entry with the same name
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001476 */
1477
1478 list_for_each_entry(entry, &efivars->list, list) {
1479 get_var_data_locked(efivars, &entry->var);
1480
1481 if (efi_guidcmp(entry->var.VendorGuid, vendor))
1482 continue;
1483 if (utf16_strncmp(entry->var.VariableName, efi_name,
Seiji Aguchif94ec0c2012-11-14 20:29:21 +00001484 utf16_strlen(efi_name))) {
1485 /*
1486 * Check if an old format,
1487 * which doesn't support holding
1488 * multiple logs, remains.
1489 */
1490 sprintf(name_old, "dump-type%u-%u-%lu", type,
1491 (unsigned int)id, time.tv_sec);
1492
1493 for (i = 0; i < DUMP_NAME_LEN; i++)
1494 efi_name_old[i] = name_old[i];
1495
1496 if (utf16_strncmp(entry->var.VariableName, efi_name_old,
1497 utf16_strlen(efi_name_old)))
1498 continue;
1499 }
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001500
1501 /* found */
1502 found = entry;
1503 efivars->ops->set_variable(entry->var.VariableName,
1504 &entry->var.VendorGuid,
1505 PSTORE_EFI_ATTRIBUTES,
1506 0, NULL);
Seiji Aguchia9efd392012-11-14 20:27:28 +00001507 break;
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001508 }
1509
1510 if (found)
1511 list_del(&found->list);
1512
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001513 spin_unlock_irq(&efivars->lock);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001514
1515 if (found)
1516 efivar_unregister(found);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001517
1518 return 0;
1519}
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001520
1521static struct pstore_info efi_pstore_info = {
1522 .owner = THIS_MODULE,
1523 .name = "efi",
1524 .open = efi_pstore_open,
1525 .close = efi_pstore_close,
1526 .read = efi_pstore_read,
1527 .write = efi_pstore_write,
1528 .erase = efi_pstore_erase,
1529};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Seth Forsheeed9dc8c2013-03-07 11:40:17 -06001531static void efivar_pstore_register(struct efivars *efivars)
1532{
1533 efivars->efi_pstore_info = efi_pstore_info;
1534 efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
1535 if (efivars->efi_pstore_info.buf) {
1536 efivars->efi_pstore_info.bufsize = 1024;
1537 efivars->efi_pstore_info.data = efivars;
1538 spin_lock_init(&efivars->efi_pstore_info.buf_lock);
1539 pstore_register(&efivars->efi_pstore_info);
1540 }
1541}
1542#else
1543static void efivar_pstore_register(struct efivars *efivars)
1544{
1545 return;
1546}
1547#endif
1548
Chris Wright2c3c8be2010-05-12 18:28:57 -07001549static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001550 struct bin_attribute *bin_attr,
1551 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553 struct efi_variable *new_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001554 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001555 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 efi_status_t status = EFI_NOT_FOUND;
1558 int found = 0;
1559
1560 if (!capable(CAP_SYS_ADMIN))
1561 return -EACCES;
1562
Matthew Garrettfec6c202012-04-30 16:11:30 -04001563 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
1564 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
1565 printk(KERN_ERR "efivars: Malformed variable content\n");
1566 return -EINVAL;
1567 }
1568
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001569 spin_lock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 /*
1572 * Does this variable already exist?
1573 */
Mike Waychison29422692011-03-11 17:43:00 -08001574 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -04001575 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1576 strsize2 = utf16_strsize(new_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (strsize1 == strsize2 &&
1578 !memcmp(&(search_efivar->var.VariableName),
1579 new_var->VariableName, strsize1) &&
1580 !efi_guidcmp(search_efivar->var.VendorGuid,
1581 new_var->VendorGuid)) {
1582 found = 1;
1583 break;
1584 }
1585 }
1586 if (found) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001587 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return -EINVAL;
1589 }
1590
Matthew Garrett68d92982013-03-02 19:40:17 -05001591 status = check_var_size_locked(efivars, new_var->Attributes,
1592 new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
1593
1594 if (status && status != EFI_UNSUPPORTED) {
1595 spin_unlock_irq(&efivars->lock);
1596 return efi_status_to_err(status);
1597 }
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 /* now *really* create the variable via EFI */
Mike Waychison32958142011-03-11 17:43:21 -08001600 status = efivars->ops->set_variable(new_var->VariableName,
1601 &new_var->VendorGuid,
1602 new_var->Attributes,
1603 new_var->DataSize,
1604 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 if (status != EFI_SUCCESS) {
1607 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1608 status);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001609 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return -EIO;
1611 }
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001612 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 /* Create the entry in sysfs. Locking is not required here */
Mike Waychison4142ef12011-03-11 17:43:11 -08001615 status = efivar_create_sysfs_entry(efivars,
Mike Waychisona2940902011-07-21 16:57:57 -04001616 utf16_strsize(new_var->VariableName,
1617 1024),
Mike Waychison4142ef12011-03-11 17:43:11 -08001618 new_var->VariableName,
1619 &new_var->VendorGuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (status) {
1621 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
1622 }
1623 return count;
1624}
1625
Chris Wright2c3c8be2010-05-12 18:28:57 -07001626static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001627 struct bin_attribute *bin_attr,
1628 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
1630 struct efi_variable *del_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001631 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001632 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 efi_status_t status = EFI_NOT_FOUND;
1635 int found = 0;
1636
1637 if (!capable(CAP_SYS_ADMIN))
1638 return -EACCES;
1639
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001640 spin_lock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 /*
1643 * Does this variable already exist?
1644 */
Mike Waychison29422692011-03-11 17:43:00 -08001645 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -04001646 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1647 strsize2 = utf16_strsize(del_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (strsize1 == strsize2 &&
1649 !memcmp(&(search_efivar->var.VariableName),
1650 del_var->VariableName, strsize1) &&
1651 !efi_guidcmp(search_efivar->var.VendorGuid,
1652 del_var->VendorGuid)) {
1653 found = 1;
1654 break;
1655 }
1656 }
1657 if (!found) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001658 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return -EINVAL;
1660 }
1661 /* force the Attributes/DataSize to 0 to ensure deletion */
1662 del_var->Attributes = 0;
1663 del_var->DataSize = 0;
1664
Mike Waychison32958142011-03-11 17:43:21 -08001665 status = efivars->ops->set_variable(del_var->VariableName,
1666 &del_var->VendorGuid,
1667 del_var->Attributes,
1668 del_var->DataSize,
1669 del_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 if (status != EFI_SUCCESS) {
1672 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1673 status);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001674 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return -EIO;
1676 }
Matt Domsch496a0fc2007-01-26 00:57:18 -08001677 list_del(&search_efivar->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 /* We need to release this lock before unregistering. */
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001679 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 efivar_unregister(search_efivar);
1681
1682 /* It's dead Jim.... */
1683 return count;
1684}
1685
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001686static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor)
1687{
1688 struct efivar_entry *entry, *n;
1689 struct efivars *efivars = &__efivars;
1690 unsigned long strsize1, strsize2;
1691 bool found = false;
1692
1693 strsize1 = utf16_strsize(variable_name, 1024);
1694 list_for_each_entry_safe(entry, n, &efivars->list, list) {
1695 strsize2 = utf16_strsize(entry->var.VariableName, 1024);
1696 if (strsize1 == strsize2 &&
1697 !memcmp(variable_name, &(entry->var.VariableName),
1698 strsize2) &&
1699 !efi_guidcmp(entry->var.VendorGuid,
1700 *vendor)) {
1701 found = true;
1702 break;
1703 }
1704 }
1705 return found;
1706}
1707
1708static void efivar_update_sysfs_entries(struct work_struct *work)
1709{
1710 struct efivars *efivars = &__efivars;
1711 efi_guid_t vendor;
1712 efi_char16_t *variable_name;
1713 unsigned long variable_name_size = 1024;
1714 efi_status_t status = EFI_NOT_FOUND;
1715 bool found;
1716
1717 /* Add new sysfs entries */
1718 while (1) {
1719 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
1720 if (!variable_name) {
1721 pr_err("efivars: Memory allocation failed.\n");
1722 return;
1723 }
1724
1725 spin_lock_irq(&efivars->lock);
1726 found = false;
1727 while (1) {
1728 variable_name_size = 1024;
1729 status = efivars->ops->get_next_variable(
1730 &variable_name_size,
1731 variable_name,
1732 &vendor);
1733 if (status != EFI_SUCCESS) {
1734 break;
1735 } else {
1736 if (!variable_is_present(variable_name,
1737 &vendor)) {
1738 found = true;
1739 break;
1740 }
1741 }
1742 }
1743 spin_unlock_irq(&efivars->lock);
1744
1745 if (!found) {
1746 kfree(variable_name);
1747 break;
1748 } else
1749 efivar_create_sysfs_entry(efivars,
1750 variable_name_size,
1751 variable_name, &vendor);
1752 }
1753}
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755/*
1756 * Let's not leave out systab information that snuck into
1757 * the efivars driver
1758 */
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001759static ssize_t systab_show(struct kobject *kobj,
1760 struct kobj_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 char *str = buf;
1763
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001764 if (!kobj || !buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return -EINVAL;
1766
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -08001767 if (efi.mps != EFI_INVALID_TABLE_ADDR)
1768 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
1769 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
1770 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
1771 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
1772 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
1773 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
1774 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
1775 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
1776 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
1777 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
1778 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
1779 if (efi.uga != EFI_INVALID_TABLE_ADDR)
1780 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 return str - buf;
1783}
1784
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001785static struct kobj_attribute efi_attr_systab =
1786 __ATTR(systab, 0400, systab_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001788static struct attribute *efi_subsys_attrs[] = {
1789 &efi_attr_systab.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 NULL, /* maybe more in the future? */
1791};
1792
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001793static struct attribute_group efi_subsys_attr_group = {
1794 .attrs = efi_subsys_attrs,
1795};
1796
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08001797static struct kobject *efi_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799/*
1800 * efivar_create_sysfs_entry()
1801 * Requires:
1802 * variable_name_size = number of bytes required to hold
1803 * variable_name (not counting the NULL
1804 * character at the end.
Mike Waychison29422692011-03-11 17:43:00 -08001805 * efivars->lock is not held on entry or exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 * Returns 1 on failure, 0 on success
1807 */
1808static int
Mike Waychison4142ef12011-03-11 17:43:11 -08001809efivar_create_sysfs_entry(struct efivars *efivars,
1810 unsigned long variable_name_size,
1811 efi_char16_t *variable_name,
1812 efi_guid_t *vendor_guid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Jeremy Kerr310ad752012-10-19 15:16:45 +08001814 int i, short_name_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 char *short_name;
1816 struct efivar_entry *new_efivar;
1817
Jeremy Kerr310ad752012-10-19 15:16:45 +08001818 /*
1819 * Length of the variable bytes in ASCII, plus the '-' separator,
1820 * plus the GUID, plus trailing NUL
1821 */
1822 short_name_size = variable_name_size / sizeof(efi_char16_t)
1823 + 1 + GUID_LEN + 1;
1824
1825 short_name = kzalloc(short_name_size, GFP_KERNEL);
Deepak Saxena9c215382005-11-07 01:01:24 -08001826 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 if (!short_name || !new_efivar) {
Jesper Juhl0933ad92005-06-25 14:59:15 -07001829 kfree(short_name);
1830 kfree(new_efivar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return 1;
1832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Mike Waychison4142ef12011-03-11 17:43:11 -08001834 new_efivar->efivars = efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 memcpy(new_efivar->var.VariableName, variable_name,
1836 variable_name_size);
1837 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
1838
1839 /* Convert Unicode to normal chars (assume top bits are 0),
1840 ala UTF-8 */
1841 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
1842 short_name[i] = variable_name[i] & 0xFF;
1843 }
1844 /* This is ugly, but necessary to separate one vendor's
1845 private variables from another's. */
1846
1847 *(short_name + strlen(short_name)) = '-';
1848 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
1849
Mike Waychison29422692011-03-11 17:43:00 -08001850 new_efivar->kobj.kset = efivars->kset;
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001851 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
1852 "%s", short_name);
Jeff Garzik69b21862006-10-11 01:22:23 -07001853 if (i) {
1854 kfree(short_name);
1855 kfree(new_efivar);
1856 return 1;
1857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001859 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
Jesper Juhl0933ad92005-06-25 14:59:15 -07001860 kfree(short_name);
1861 short_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001863 spin_lock_irq(&efivars->lock);
Mike Waychison29422692011-03-11 17:43:00 -08001864 list_add(&new_efivar->list, &efivars->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001865 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 return 0;
1868}
Mike Waychisond502fbb2011-03-11 17:43:06 -08001869
1870static int
1871create_efivars_bin_attributes(struct efivars *efivars)
1872{
1873 struct bin_attribute *attr;
1874 int error;
1875
1876 /* new_var */
1877 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1878 if (!attr)
1879 return -ENOMEM;
1880
1881 attr->attr.name = "new_var";
1882 attr->attr.mode = 0200;
1883 attr->write = efivar_create;
1884 attr->private = efivars;
1885 efivars->new_var = attr;
1886
1887 /* del_var */
1888 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1889 if (!attr) {
1890 error = -ENOMEM;
1891 goto out_free;
1892 }
1893 attr->attr.name = "del_var";
1894 attr->attr.mode = 0200;
1895 attr->write = efivar_delete;
1896 attr->private = efivars;
1897 efivars->del_var = attr;
1898
1899 sysfs_bin_attr_init(efivars->new_var);
1900 sysfs_bin_attr_init(efivars->del_var);
1901
1902 /* Register */
1903 error = sysfs_create_bin_file(&efivars->kset->kobj,
1904 efivars->new_var);
1905 if (error) {
1906 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
1907 " due to error %d\n", error);
1908 goto out_free;
1909 }
1910 error = sysfs_create_bin_file(&efivars->kset->kobj,
1911 efivars->del_var);
1912 if (error) {
1913 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
1914 " due to error %d\n", error);
1915 sysfs_remove_bin_file(&efivars->kset->kobj,
1916 efivars->new_var);
1917 goto out_free;
1918 }
1919
1920 return 0;
1921out_free:
Dan Carpenter051d51b2011-03-18 10:12:14 +03001922 kfree(efivars->del_var);
1923 efivars->del_var = NULL;
Mike Waychisond502fbb2011-03-11 17:43:06 -08001924 kfree(efivars->new_var);
1925 efivars->new_var = NULL;
1926 return error;
1927}
1928
Mike Waychison4fc756b2011-03-11 17:43:27 -08001929void unregister_efivars(struct efivars *efivars)
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001930{
1931 struct efivar_entry *entry, *n;
Mike Waychison4142ef12011-03-11 17:43:11 -08001932
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001933 list_for_each_entry_safe(entry, n, &efivars->list, list) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001934 spin_lock_irq(&efivars->lock);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001935 list_del(&entry->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001936 spin_unlock_irq(&efivars->lock);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001937 efivar_unregister(entry);
1938 }
1939 if (efivars->new_var)
1940 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
1941 if (efivars->del_var)
1942 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
1943 kfree(efivars->new_var);
1944 kfree(efivars->del_var);
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08001945 kobject_put(efivars->kobject);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001946 kset_unregister(efivars->kset);
1947}
Mike Waychison4fc756b2011-03-11 17:43:27 -08001948EXPORT_SYMBOL_GPL(unregister_efivars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Mike Waychison4fc756b2011-03-11 17:43:27 -08001950int register_efivars(struct efivars *efivars,
1951 const struct efivar_operations *ops,
1952 struct kobject *parent_kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
1954 efi_status_t status = EFI_NOT_FOUND;
1955 efi_guid_t vendor_guid;
1956 efi_char16_t *variable_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 unsigned long variable_name_size = 1024;
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001958 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Deepak Saxena9c215382005-11-07 01:01:24 -08001960 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (!variable_name) {
1962 printk(KERN_ERR "efivars: Memory allocation failed.\n");
1963 return -ENOMEM;
1964 }
1965
Mike Waychison29422692011-03-11 17:43:00 -08001966 spin_lock_init(&efivars->lock);
1967 INIT_LIST_HEAD(&efivars->list);
Mike Waychison32958142011-03-11 17:43:21 -08001968 efivars->ops = ops;
Mike Waychison29422692011-03-11 17:43:00 -08001969
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001970 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
Mike Waychison29422692011-03-11 17:43:00 -08001971 if (!efivars->kset) {
Greg Kroah-Hartman66ac8312007-11-02 13:20:40 -07001972 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
1973 error = -ENOMEM;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001974 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
1976
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08001977 efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
1978 if (!efivars->kobject) {
1979 pr_err("efivars: Subsystem registration failed.\n");
1980 error = -ENOMEM;
1981 kset_unregister(efivars->kset);
1982 goto out;
1983 }
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 /*
1986 * Per EFI spec, the maximum storage allocated for both
1987 * the variable name and variable data is 1024 bytes.
1988 */
1989
1990 do {
1991 variable_name_size = 1024;
1992
Mike Waychison32958142011-03-11 17:43:21 -08001993 status = ops->get_next_variable(&variable_name_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 variable_name,
1995 &vendor_guid);
1996 switch (status) {
1997 case EFI_SUCCESS:
Mike Waychison4142ef12011-03-11 17:43:11 -08001998 efivar_create_sysfs_entry(efivars,
1999 variable_name_size,
2000 variable_name,
2001 &vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 break;
2003 case EFI_NOT_FOUND:
2004 break;
2005 default:
2006 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
2007 status);
2008 status = EFI_NOT_FOUND;
2009 break;
2010 }
2011 } while (status != EFI_NOT_FOUND);
2012
Mike Waychisond502fbb2011-03-11 17:43:06 -08002013 error = create_efivars_bin_attributes(efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002014 if (error)
2015 unregister_efivars(efivars);
2016
Seth Forsheeec0971b2013-03-11 16:17:50 -05002017 if (!efivars_pstore_disable)
2018 efivar_pstore_register(efivars);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04002019
Matthew Garrett5d9db882012-10-05 13:54:56 +08002020 register_filesystem(&efivarfs_type);
2021
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002022out:
2023 kfree(variable_name);
2024
2025 return error;
2026}
Mike Waychison4fc756b2011-03-11 17:43:27 -08002027EXPORT_SYMBOL_GPL(register_efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002028
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002029/*
2030 * For now we register the efi subsystem with the firmware subsystem
2031 * and the vars subsystem with the efi subsystem. In the future, it
2032 * might make sense to split off the efi subsystem into its own
2033 * driver, but for now only efivars will register with it, so just
2034 * include it here.
2035 */
2036
2037static int __init
2038efivars_init(void)
2039{
2040 int error = 0;
2041
2042 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
2043 EFIVARS_DATE);
2044
Matt Fleming83e68182012-11-14 09:42:35 +00002045 if (!efi_enabled(EFI_RUNTIME_SERVICES))
Mike Waychison4fc756b2011-03-11 17:43:27 -08002046 return 0;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002047
2048 /* For now we'll register the efi directory at /sys/firmware/efi */
2049 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
2050 if (!efi_kobj) {
2051 printk(KERN_ERR "efivars: Firmware registration failed.\n");
2052 return -ENOMEM;
2053 }
2054
Mike Waychison32958142011-03-11 17:43:21 -08002055 ops.get_variable = efi.get_variable;
2056 ops.set_variable = efi.set_variable;
2057 ops.get_next_variable = efi.get_next_variable;
Seiji Aguchid80a3612012-11-14 20:25:37 +00002058 ops.query_variable_info = efi.query_variable_info;
Matt Fleming89d16662012-11-09 21:02:56 +00002059
Mike Waychison32958142011-03-11 17:43:21 -08002060 error = register_efivars(&__efivars, &ops, efi_kobj);
Dan Carpenter3116aab2011-03-18 10:12:38 +03002061 if (error)
2062 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 /* Don't forget the systab entry */
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08002065 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002066 if (error) {
2067 printk(KERN_ERR
2068 "efivars: Sysfs attribute export failed with error %d.\n",
2069 error);
Dan Carpenter3116aab2011-03-18 10:12:38 +03002070 goto err_unregister;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Dan Carpenter3116aab2011-03-18 10:12:38 +03002073 return 0;
2074
2075err_unregister:
2076 unregister_efivars(&__efivars);
2077err_put:
2078 kobject_put(efi_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return error;
2080}
2081
2082static void __exit
2083efivars_exit(void)
2084{
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08002085 cancel_work_sync(&efivar_work);
2086
Matt Fleming83e68182012-11-14 09:42:35 +00002087 if (efi_enabled(EFI_RUNTIME_SERVICES)) {
Randy Dunlapaabb6e12011-05-06 13:27:41 -07002088 unregister_efivars(&__efivars);
2089 kobject_put(efi_kobj);
2090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
2093module_init(efivars_init);
2094module_exit(efivars_exit);
2095