blob: 7acafb80fd4c79b19b1c53551bb5e7e8612af707 [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 =
Ben Hutchingsca0ba262013-03-22 19:56:51 +0000107 IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
Seth Forsheeec0971b2013-03-11 16:17:50 -0500108
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);
Matt Fleminge9713182013-03-07 11:59:14 +0000173static bool efivar_wq_enabled = true;
Seiji Aguchia93bc0c2013-02-12 13:04:41 -0800174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/* Return the number of unicode characters in data */
176static unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400177utf16_strnlen(efi_char16_t *s, size_t maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 unsigned long length = 0;
180
Mike Waychisona2940902011-07-21 16:57:57 -0400181 while (*s++ != 0 && length < maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 length++;
183 return length;
184}
185
Tony Luckb728a5c2011-08-02 15:08:30 -0700186static inline unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400187utf16_strlen(efi_char16_t *s)
188{
189 return utf16_strnlen(s, ~0UL);
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
193 * Return the number of bytes is the length of this string
194 * Note: this is NOT the same as the number of unicode characters
195 */
196static inline unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400197utf16_strsize(efi_char16_t *data, unsigned long maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Mike Waychisona2940902011-07-21 16:57:57 -0400199 return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Mike Waychison828aa1f2011-07-21 16:57:58 -0400202static inline int
203utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
204{
205 while (1) {
206 if (len == 0)
207 return 0;
208 if (*a < *b)
209 return -1;
210 if (*a > *b)
211 return 1;
212 if (*a == 0) /* implies *b == 0 */
213 return 0;
214 a++;
215 b++;
216 len--;
217 }
218}
219
Matthew Garrettfec6c202012-04-30 16:11:30 -0400220static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400221validate_device_path(struct efi_variable *var, int match, u8 *buffer,
222 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400223{
224 struct efi_generic_dev_path *node;
225 int offset = 0;
226
227 node = (struct efi_generic_dev_path *)buffer;
228
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400229 if (len < sizeof(*node))
230 return false;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400231
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400232 while (offset <= len - sizeof(*node) &&
233 node->length >= sizeof(*node) &&
234 node->length <= len - offset) {
235 offset += node->length;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400236
237 if ((node->type == EFI_DEV_END_PATH ||
238 node->type == EFI_DEV_END_PATH2) &&
239 node->sub_type == EFI_DEV_END_ENTIRE)
240 return true;
241
242 node = (struct efi_generic_dev_path *)(buffer + offset);
243 }
244
245 /*
246 * If we're here then either node->length pointed past the end
247 * of the buffer or we reached the end of the buffer without
248 * finding a device path end node.
249 */
250 return false;
251}
252
253static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400254validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
255 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400256{
257 /* An array of 16-bit integers */
258 if ((len % 2) != 0)
259 return false;
260
261 return true;
262}
263
264static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400265validate_load_option(struct efi_variable *var, int match, u8 *buffer,
266 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400267{
268 u16 filepathlength;
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400269 int i, desclength = 0, namelen;
270
271 namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
Matthew Garrettfec6c202012-04-30 16:11:30 -0400272
273 /* Either "Boot" or "Driver" followed by four digits of hex */
274 for (i = match; i < match+4; i++) {
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400275 if (var->VariableName[i] > 127 ||
276 hex_to_bin(var->VariableName[i] & 0xff) < 0)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400277 return true;
278 }
279
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400280 /* Reject it if there's 4 digits of hex and then further content */
281 if (namelen > match + 4)
282 return false;
283
284 /* A valid entry must be at least 8 bytes */
285 if (len < 8)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400286 return false;
287
288 filepathlength = buffer[4] | buffer[5] << 8;
289
290 /*
291 * There's no stored length for the description, so it has to be
292 * found by hand
293 */
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400294 desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400295
296 /* Each boot entry must have a descriptor */
297 if (!desclength)
298 return false;
299
300 /*
301 * If the sum of the length of the description, the claimed filepath
302 * length and the original header are greater than the length of the
303 * variable, it's malformed
304 */
305 if ((desclength + filepathlength + 6) > len)
306 return false;
307
308 /*
309 * And, finally, check the filepath
310 */
311 return validate_device_path(var, match, buffer + desclength + 6,
312 filepathlength);
313}
314
315static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400316validate_uint16(struct efi_variable *var, int match, u8 *buffer,
317 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400318{
319 /* A single 16-bit integer */
320 if (len != 2)
321 return false;
322
323 return true;
324}
325
326static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400327validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
328 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400329{
330 int i;
331
332 for (i = 0; i < len; i++) {
333 if (buffer[i] > 127)
334 return false;
335
336 if (buffer[i] == 0)
337 return true;
338 }
339
340 return false;
341}
342
343struct variable_validate {
344 char *name;
345 bool (*validate)(struct efi_variable *var, int match, u8 *data,
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400346 unsigned long len);
Matthew Garrettfec6c202012-04-30 16:11:30 -0400347};
348
349static const struct variable_validate variable_validate[] = {
350 { "BootNext", validate_uint16 },
351 { "BootOrder", validate_boot_order },
352 { "DriverOrder", validate_boot_order },
353 { "Boot*", validate_load_option },
354 { "Driver*", validate_load_option },
355 { "ConIn", validate_device_path },
356 { "ConInDev", validate_device_path },
357 { "ConOut", validate_device_path },
358 { "ConOutDev", validate_device_path },
359 { "ErrOut", validate_device_path },
360 { "ErrOutDev", validate_device_path },
361 { "Timeout", validate_uint16 },
362 { "Lang", validate_ascii_string },
363 { "PlatformLang", validate_ascii_string },
364 { "", NULL },
365};
366
367static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400368validate_var(struct efi_variable *var, u8 *data, unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400369{
370 int i;
371 u16 *unicode_name = var->VariableName;
372
373 for (i = 0; variable_validate[i].validate != NULL; i++) {
374 const char *name = variable_validate[i].name;
375 int match;
376
377 for (match = 0; ; match++) {
378 char c = name[match];
379 u16 u = unicode_name[match];
380
381 /* All special variables are plain ascii */
382 if (u > 127)
383 return true;
384
385 /* Wildcard in the matching name means we've matched */
386 if (c == '*')
387 return variable_validate[i].validate(var,
388 match, data, len);
389
390 /* Case sensitive match */
391 if (c != u)
392 break;
393
394 /* Reached the end of the string while matching */
395 if (!c)
396 return variable_validate[i].validate(var,
397 match, data, len);
398 }
399 }
400
401 return true;
402}
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404static efi_status_t
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400405get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 efi_status_t status;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 var->DataSize = 1024;
Mike Waychison32958142011-03-11 17:43:21 -0800410 status = efivars->ops->get_variable(var->VariableName,
411 &var->VendorGuid,
412 &var->Attributes,
413 &var->DataSize,
414 var->Data);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400415 return status;
416}
417
418static efi_status_t
419get_var_data(struct efivars *efivars, struct efi_variable *var)
420{
421 efi_status_t status;
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800422 unsigned long flags;
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400423
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800424 spin_lock_irqsave(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400425 status = get_var_data_locked(efivars, var);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800426 spin_unlock_irqrestore(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (status != EFI_SUCCESS) {
429 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
430 status);
431 }
432 return status;
433}
434
Matthew Garrett68d92982013-03-02 19:40:17 -0500435static efi_status_t
436check_var_size_locked(struct efivars *efivars, u32 attributes,
437 unsigned long size)
438{
439 u64 storage_size, remaining_size, max_size;
440 efi_status_t status;
441 const struct efivar_operations *fops = efivars->ops;
442
443 if (!efivars->ops->query_variable_info)
444 return EFI_UNSUPPORTED;
445
446 status = fops->query_variable_info(attributes, &storage_size,
447 &remaining_size, &max_size);
448
449 if (status != EFI_SUCCESS)
450 return status;
451
452 if (!storage_size || size > remaining_size || size > max_size ||
453 (remaining_size - size) < (storage_size / 2))
454 return EFI_OUT_OF_RESOURCES;
455
456 return status;
457}
458
459
460static efi_status_t
461check_var_size(struct efivars *efivars, u32 attributes, unsigned long size)
462{
463 efi_status_t status;
464 unsigned long flags;
465
466 spin_lock_irqsave(&efivars->lock, flags);
467 status = check_var_size_locked(efivars, attributes, size);
468 spin_unlock_irqrestore(&efivars->lock, flags);
469
470 return status;
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473static ssize_t
474efivar_guid_read(struct efivar_entry *entry, char *buf)
475{
476 struct efi_variable *var = &entry->var;
477 char *str = buf;
478
479 if (!entry || !buf)
480 return 0;
481
482 efi_guid_unparse(&var->VendorGuid, str);
483 str += strlen(str);
484 str += sprintf(str, "\n");
485
486 return str - buf;
487}
488
489static ssize_t
490efivar_attr_read(struct efivar_entry *entry, char *buf)
491{
492 struct efi_variable *var = &entry->var;
493 char *str = buf;
494 efi_status_t status;
495
496 if (!entry || !buf)
497 return -EINVAL;
498
Mike Waychison4142ef12011-03-11 17:43:11 -0800499 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (status != EFI_SUCCESS)
501 return -EIO;
502
Khalid Aziz70839092012-09-10 12:52:42 -0600503 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600505 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600507 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600509 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
510 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
511 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
512 str += sprintf(str,
513 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
514 if (var->Attributes &
515 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
516 str += sprintf(str,
517 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
518 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
519 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return str - buf;
521}
522
523static ssize_t
524efivar_size_read(struct efivar_entry *entry, char *buf)
525{
526 struct efi_variable *var = &entry->var;
527 char *str = buf;
528 efi_status_t status;
529
530 if (!entry || !buf)
531 return -EINVAL;
532
Mike Waychison4142ef12011-03-11 17:43:11 -0800533 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (status != EFI_SUCCESS)
535 return -EIO;
536
537 str += sprintf(str, "0x%lx\n", var->DataSize);
538 return str - buf;
539}
540
541static ssize_t
542efivar_data_read(struct efivar_entry *entry, char *buf)
543{
544 struct efi_variable *var = &entry->var;
545 efi_status_t status;
546
547 if (!entry || !buf)
548 return -EINVAL;
549
Mike Waychison4142ef12011-03-11 17:43:11 -0800550 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (status != EFI_SUCCESS)
552 return -EIO;
553
554 memcpy(buf, var->Data, var->DataSize);
555 return var->DataSize;
556}
557/*
558 * We allow each variable to be edited via rewriting the
559 * entire efi variable structure.
560 */
561static ssize_t
562efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
563{
564 struct efi_variable *new_var, *var = &entry->var;
Mike Waychison4142ef12011-03-11 17:43:11 -0800565 struct efivars *efivars = entry->efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 efi_status_t status = EFI_NOT_FOUND;
567
568 if (count != sizeof(struct efi_variable))
569 return -EINVAL;
570
571 new_var = (struct efi_variable *)buf;
572 /*
573 * If only updating the variable data, then the name
574 * and guid should remain the same
575 */
576 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
577 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
578 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
579 return -EINVAL;
580 }
581
582 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
583 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
584 return -EINVAL;
585 }
586
Matthew Garrettfec6c202012-04-30 16:11:30 -0400587 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
588 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
589 printk(KERN_ERR "efivars: Malformed variable content\n");
590 return -EINVAL;
591 }
592
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800593 spin_lock_irq(&efivars->lock);
Matthew Garrett68d92982013-03-02 19:40:17 -0500594
595 status = check_var_size_locked(efivars, new_var->Attributes,
596 new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
597
598 if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
599 status = efivars->ops->set_variable(new_var->VariableName,
600 &new_var->VendorGuid,
601 new_var->Attributes,
602 new_var->DataSize,
603 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800605 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 if (status != EFI_SUCCESS) {
608 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
609 status);
610 return -EIO;
611 }
612
613 memcpy(&entry->var, new_var, count);
614 return count;
615}
616
617static ssize_t
618efivar_show_raw(struct efivar_entry *entry, char *buf)
619{
620 struct efi_variable *var = &entry->var;
621 efi_status_t status;
622
623 if (!entry || !buf)
624 return 0;
625
Mike Waychison4142ef12011-03-11 17:43:11 -0800626 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (status != EFI_SUCCESS)
628 return -EIO;
629
630 memcpy(buf, var, sizeof(*var));
631 return sizeof(*var);
632}
633
634/*
635 * Generic read/write functions that call the specific functions of
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200636 * the attributes...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
638static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
639 char *buf)
640{
641 struct efivar_entry *var = to_efivar_entry(kobj);
642 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500643 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 if (!capable(CAP_SYS_ADMIN))
646 return -EACCES;
647
648 if (efivar_attr->show) {
649 ret = efivar_attr->show(var, buf);
650 }
651 return ret;
652}
653
654static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
655 const char *buf, size_t count)
656{
657 struct efivar_entry *var = to_efivar_entry(kobj);
658 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500659 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 if (!capable(CAP_SYS_ADMIN))
662 return -EACCES;
663
664 if (efivar_attr->store)
665 ret = efivar_attr->store(var, buf, count);
666
667 return ret;
668}
669
Emese Revfy52cf25d2010-01-19 02:58:23 +0100670static const struct sysfs_ops efivar_attr_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 .show = efivar_attr_show,
672 .store = efivar_attr_store,
673};
674
675static void efivar_release(struct kobject *kobj)
676{
677 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 kfree(var);
679}
680
681static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
682static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
683static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
684static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
685static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
686
687static struct attribute *def_attrs[] = {
688 &efivar_attr_guid.attr,
689 &efivar_attr_size.attr,
690 &efivar_attr_attributes.attr,
691 &efivar_attr_data.attr,
692 &efivar_attr_raw_var.attr,
693 NULL,
694};
695
Greg Kroah-Hartmane89a4112007-10-11 10:47:49 -0600696static struct kobj_type efivar_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 .release = efivar_release,
698 .sysfs_ops = &efivar_attr_ops,
699 .default_attrs = def_attrs,
700};
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702static inline void
703efivar_unregister(struct efivar_entry *var)
704{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800705 kobject_put(&var->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Matthew Garrett5d9db882012-10-05 13:54:56 +0800708static int efivarfs_file_open(struct inode *inode, struct file *file)
709{
710 file->private_data = inode->i_private;
711 return 0;
712}
713
Matt Fleming7253eab2012-10-16 15:58:07 +0100714static int efi_status_to_err(efi_status_t status)
715{
716 int err;
717
718 switch (status) {
719 case EFI_INVALID_PARAMETER:
720 err = -EINVAL;
721 break;
722 case EFI_OUT_OF_RESOURCES:
723 err = -ENOSPC;
724 break;
725 case EFI_DEVICE_ERROR:
726 err = -EIO;
727 break;
728 case EFI_WRITE_PROTECTED:
729 err = -EROFS;
730 break;
731 case EFI_SECURITY_VIOLATION:
732 err = -EACCES;
733 break;
734 case EFI_NOT_FOUND:
Matt Fleming1fa7e692013-01-16 13:47:05 +0000735 err = -EIO;
Matt Fleming7253eab2012-10-16 15:58:07 +0100736 break;
737 default:
738 err = -EINVAL;
739 }
740
741 return err;
742}
743
Matthew Garrett5d9db882012-10-05 13:54:56 +0800744static ssize_t efivarfs_file_write(struct file *file,
745 const char __user *userbuf, size_t count, loff_t *ppos)
746{
747 struct efivar_entry *var = file->private_data;
748 struct efivars *efivars;
749 efi_status_t status;
750 void *data;
751 u32 attributes;
752 struct inode *inode = file->f_mapping->host;
Matt Fleming07b1c5b2012-10-23 12:35:43 +0100753 unsigned long datasize = count - sizeof(attributes);
Matthew Garrett68d92982013-03-02 19:40:17 -0500754 unsigned long newdatasize, varsize;
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100755 ssize_t bytes = 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800756
757 if (count < sizeof(attributes))
758 return -EINVAL;
759
Matt Fleming89d16662012-11-09 21:02:56 +0000760 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
761 return -EFAULT;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800762
Matt Fleming89d16662012-11-09 21:02:56 +0000763 if (attributes & ~(EFI_VARIABLE_MASK))
764 return -EINVAL;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800765
766 efivars = var->efivars;
767
Matt Fleming89d16662012-11-09 21:02:56 +0000768 /*
769 * Ensure that the user can't allocate arbitrarily large
770 * amounts of memory. Pick a default size of 64K if
771 * QueryVariableInfo() isn't supported by the firmware.
772 */
Matt Fleming89d16662012-11-09 21:02:56 +0000773
Matthew Garrett68d92982013-03-02 19:40:17 -0500774 varsize = datasize + utf16_strsize(var->var.VariableName, 1024);
775 status = check_var_size(efivars, attributes, varsize);
Matt Fleming89d16662012-11-09 21:02:56 +0000776
777 if (status != EFI_SUCCESS) {
778 if (status != EFI_UNSUPPORTED)
779 return efi_status_to_err(status);
780
Matthew Garrett68d92982013-03-02 19:40:17 -0500781 if (datasize > 65536)
782 return -ENOSPC;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800783 }
784
Matt Fleming89d16662012-11-09 21:02:56 +0000785 data = kmalloc(datasize, GFP_KERNEL);
786 if (!data)
787 return -ENOMEM;
788
Matthew Garrett5d9db882012-10-05 13:54:56 +0800789 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100790 bytes = -EFAULT;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800791 goto out;
792 }
793
794 if (validate_var(&var->var, data, datasize) == false) {
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100795 bytes = -EINVAL;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800796 goto out;
797 }
798
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800799 /*
800 * The lock here protects the get_variable call, the conditional
801 * set_variable call, and removal of the variable from the efivars
802 * list (in the case of an authenticated delete).
803 */
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800804 spin_lock_irq(&efivars->lock);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800805
Matthew Garrett68d92982013-03-02 19:40:17 -0500806 /*
807 * Ensure that the available space hasn't shrunk below the safe level
808 */
809
810 status = check_var_size_locked(efivars, attributes, varsize);
811
812 if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED) {
813 spin_unlock_irq(&efivars->lock);
814 kfree(data);
815
816 return efi_status_to_err(status);
817 }
818
Matthew Garrett5d9db882012-10-05 13:54:56 +0800819 status = efivars->ops->set_variable(var->var.VariableName,
820 &var->var.VendorGuid,
821 attributes, datasize,
822 data);
823
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800824 if (status != EFI_SUCCESS) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800825 spin_unlock_irq(&efivars->lock);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800826 kfree(data);
827
Matt Fleming7253eab2012-10-16 15:58:07 +0100828 return efi_status_to_err(status);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800829 }
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800830
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100831 bytes = count;
832
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800833 /*
834 * Writing to the variable may have caused a change in size (which
835 * could either be an append or an overwrite), or the variable to be
836 * deleted. Perform a GetVariable() so we can tell what actually
837 * happened.
838 */
839 newdatasize = 0;
840 status = efivars->ops->get_variable(var->var.VariableName,
841 &var->var.VendorGuid,
842 NULL, &newdatasize,
843 NULL);
844
845 if (status == EFI_BUFFER_TOO_SMALL) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800846 spin_unlock_irq(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800847 mutex_lock(&inode->i_mutex);
848 i_size_write(inode, newdatasize + sizeof(attributes));
849 mutex_unlock(&inode->i_mutex);
850
851 } else if (status == EFI_NOT_FOUND) {
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800852 list_del(&var->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800853 spin_unlock_irq(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800854 efivar_unregister(var);
855 drop_nlink(inode);
Matt Fleming791eb562013-01-16 21:55:36 +0000856 d_delete(file->f_dentry);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800857 dput(file->f_dentry);
858
859 } else {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800860 spin_unlock_irq(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800861 pr_warn("efivarfs: inconsistent EFI variable implementation? "
862 "status = %lx\n", status);
863 }
864
Matthew Garrett5d9db882012-10-05 13:54:56 +0800865out:
866 kfree(data);
867
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100868 return bytes;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800869}
870
871static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
872 size_t count, loff_t *ppos)
873{
874 struct efivar_entry *var = file->private_data;
875 struct efivars *efivars = var->efivars;
876 efi_status_t status;
877 unsigned long datasize = 0;
878 u32 attributes;
879 void *data;
Andy Whitcroftd142df02012-10-11 11:32:17 +0100880 ssize_t size = 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800881
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800882 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800883 status = efivars->ops->get_variable(var->var.VariableName,
884 &var->var.VendorGuid,
885 &attributes, &datasize, NULL);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800886 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800887
888 if (status != EFI_BUFFER_TOO_SMALL)
Matt Fleming7253eab2012-10-16 15:58:07 +0100889 return efi_status_to_err(status);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800890
Matt Flemingd2923842012-10-22 15:23:29 +0100891 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800892
893 if (!data)
Matt Fleming7253eab2012-10-16 15:58:07 +0100894 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800895
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800896 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800897 status = efivars->ops->get_variable(var->var.VariableName,
898 &var->var.VendorGuid,
899 &attributes, &datasize,
Matt Flemingd2923842012-10-22 15:23:29 +0100900 (data + sizeof(attributes)));
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800901 spin_unlock_irq(&efivars->lock);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800902
Matt Fleming7253eab2012-10-16 15:58:07 +0100903 if (status != EFI_SUCCESS) {
904 size = efi_status_to_err(status);
Andy Whitcroftd142df02012-10-11 11:32:17 +0100905 goto out_free;
Matt Fleming7253eab2012-10-16 15:58:07 +0100906 }
Matthew Garrett5d9db882012-10-05 13:54:56 +0800907
Matt Flemingd2923842012-10-22 15:23:29 +0100908 memcpy(data, &attributes, sizeof(attributes));
Matthew Garrett5d9db882012-10-05 13:54:56 +0800909 size = simple_read_from_buffer(userbuf, count, ppos,
Matt Flemingd2923842012-10-22 15:23:29 +0100910 data, datasize + sizeof(attributes));
Andy Whitcroftd142df02012-10-11 11:32:17 +0100911out_free:
Matthew Garrett5d9db882012-10-05 13:54:56 +0800912 kfree(data);
913
914 return size;
915}
916
917static void efivarfs_evict_inode(struct inode *inode)
918{
919 clear_inode(inode);
920}
921
922static const struct super_operations efivarfs_ops = {
923 .statfs = simple_statfs,
924 .drop_inode = generic_delete_inode,
925 .evict_inode = efivarfs_evict_inode,
926 .show_options = generic_show_options,
927};
928
929static struct super_block *efivarfs_sb;
930
931static const struct inode_operations efivarfs_dir_inode_operations;
932
933static const struct file_operations efivarfs_file_operations = {
934 .open = efivarfs_file_open,
935 .read = efivarfs_file_read,
936 .write = efivarfs_file_write,
937 .llseek = no_llseek,
938};
939
940static struct inode *efivarfs_get_inode(struct super_block *sb,
941 const struct inode *dir, int mode, dev_t dev)
942{
943 struct inode *inode = new_inode(sb);
944
945 if (inode) {
946 inode->i_ino = get_next_ino();
Matthew Garrett5d9db882012-10-05 13:54:56 +0800947 inode->i_mode = mode;
948 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
949 switch (mode & S_IFMT) {
950 case S_IFREG:
951 inode->i_fop = &efivarfs_file_operations;
952 break;
953 case S_IFDIR:
954 inode->i_op = &efivarfs_dir_inode_operations;
955 inode->i_fop = &simple_dir_operations;
956 inc_nlink(inode);
957 break;
958 }
959 }
960 return inode;
961}
962
Matt Fleming47f531e2013-01-31 19:02:03 +0000963/*
964 * Return true if 'str' is a valid efivarfs filename of the form,
965 *
966 * VariableName-12345678-1234-1234-1234-1234567891bc
967 */
968static bool efivarfs_valid_name(const char *str, int len)
969{
970 static const char dashes[GUID_LEN] = {
971 [8] = 1, [13] = 1, [18] = 1, [23] = 1
972 };
973 const char *s = str + len - GUID_LEN;
974 int i;
975
976 /*
977 * We need a GUID, plus at least one letter for the variable name,
978 * plus the '-' separator
979 */
980 if (len < GUID_LEN + 2)
981 return false;
982
Matt Fleming123abd72013-03-05 07:40:16 +0000983 /* GUID must be preceded by a '-' */
984 if (*(s - 1) != '-')
Matt Fleming47f531e2013-01-31 19:02:03 +0000985 return false;
986
987 /*
988 * Validate that 's' is of the correct format, e.g.
989 *
990 * 12345678-1234-1234-1234-123456789abc
991 */
992 for (i = 0; i < GUID_LEN; i++) {
993 if (dashes[i]) {
994 if (*s++ != '-')
995 return false;
996 } else {
997 if (!isxdigit(*s++))
998 return false;
999 }
1000 }
1001
1002 return true;
1003}
1004
Matthew Garrett5d9db882012-10-05 13:54:56 +08001005static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
1006{
1007 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
1008 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
1009 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
1010 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
1011 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
1012 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
1013 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
1014 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
1015 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
1016 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
1017 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
1018 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
1019 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
1020 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
1021 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
1022 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
1023}
1024
1025static int efivarfs_create(struct inode *dir, struct dentry *dentry,
1026 umode_t mode, bool excl)
1027{
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001028 struct inode *inode;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001029 struct efivars *efivars = &__efivars;
1030 struct efivar_entry *var;
1031 int namelen, i = 0, err = 0;
1032
Matt Fleming47f531e2013-01-31 19:02:03 +00001033 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
Matthew Garrett5d9db882012-10-05 13:54:56 +08001034 return -EINVAL;
1035
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001036 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001037 if (!inode)
Matt Flemingaeeaa8d2012-10-23 12:41:03 +01001038 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001039
1040 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001041 if (!var) {
1042 err = -ENOMEM;
1043 goto out;
1044 }
Matthew Garrett5d9db882012-10-05 13:54:56 +08001045
Jeremy Kerr310ad752012-10-19 15:16:45 +08001046 /* length of the variable name itself: remove GUID and separator */
1047 namelen = dentry->d_name.len - GUID_LEN - 1;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001048
1049 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
1050 &var->var.VendorGuid);
1051
1052 for (i = 0; i < namelen; i++)
1053 var->var.VariableName[i] = dentry->d_name.name[i];
1054
1055 var->var.VariableName[i] = '\0';
1056
1057 inode->i_private = var;
1058 var->efivars = efivars;
1059 var->kobj.kset = efivars->kset;
1060
1061 err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
1062 dentry->d_name.name);
1063 if (err)
1064 goto out;
1065
1066 kobject_uevent(&var->kobj, KOBJ_ADD);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001067 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001068 list_add(&var->list, &efivars->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001069 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001070 d_instantiate(dentry, inode);
1071 dget(dentry);
1072out:
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001073 if (err) {
Matthew Garrett5d9db882012-10-05 13:54:56 +08001074 kfree(var);
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001075 iput(inode);
1076 }
Matthew Garrett5d9db882012-10-05 13:54:56 +08001077 return err;
1078}
1079
1080static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
1081{
1082 struct efivar_entry *var = dentry->d_inode->i_private;
1083 struct efivars *efivars = var->efivars;
1084 efi_status_t status;
1085
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001086 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001087
1088 status = efivars->ops->set_variable(var->var.VariableName,
1089 &var->var.VendorGuid,
1090 0, 0, NULL);
1091
1092 if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
1093 list_del(&var->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001094 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001095 efivar_unregister(var);
Lingzhu Xiangde5fe952013-01-05 13:59:52 +08001096 drop_nlink(dentry->d_inode);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001097 dput(dentry);
1098 return 0;
1099 }
1100
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001101 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001102 return -EINVAL;
1103};
1104
Matt Flemingda27a242013-02-01 11:02:28 +00001105/*
1106 * Compare two efivarfs file names.
1107 *
1108 * An efivarfs filename is composed of two parts,
1109 *
1110 * 1. A case-sensitive variable name
1111 * 2. A case-insensitive GUID
1112 *
1113 * So we need to perform a case-sensitive match on part 1 and a
1114 * case-insensitive match on part 2.
1115 */
1116static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
1117 const struct dentry *dentry, const struct inode *inode,
1118 unsigned int len, const char *str,
1119 const struct qstr *name)
1120{
1121 int guid = len - GUID_LEN;
1122
1123 if (name->len != len)
1124 return 1;
1125
1126 /* Case-sensitive compare for the variable name */
1127 if (memcmp(str, name->name, guid))
1128 return 1;
1129
1130 /* Case-insensitive compare for the GUID */
1131 return strncasecmp(name->name + guid, str + guid, GUID_LEN);
1132}
1133
1134static int efivarfs_d_hash(const struct dentry *dentry,
1135 const struct inode *inode, struct qstr *qstr)
1136{
1137 unsigned long hash = init_name_hash();
1138 const unsigned char *s = qstr->name;
1139 unsigned int len = qstr->len;
1140
1141 if (!efivarfs_valid_name(s, len))
1142 return -EINVAL;
1143
1144 while (len-- > GUID_LEN)
1145 hash = partial_name_hash(*s++, hash);
1146
1147 /* GUID is case-insensitive. */
1148 while (len--)
1149 hash = partial_name_hash(tolower(*s++), hash);
1150
1151 qstr->hash = end_name_hash(hash);
1152 return 0;
1153}
1154
1155/*
1156 * Retaining negative dentries for an in-memory filesystem just wastes
1157 * memory and lookup time: arrange for them to be deleted immediately.
1158 */
1159static int efivarfs_delete_dentry(const struct dentry *dentry)
1160{
1161 return 1;
1162}
1163
1164static struct dentry_operations efivarfs_d_ops = {
1165 .d_compare = efivarfs_d_compare,
1166 .d_hash = efivarfs_d_hash,
1167 .d_delete = efivarfs_delete_dentry,
1168};
1169
1170static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
1171{
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001172 struct dentry *d;
Matt Flemingda27a242013-02-01 11:02:28 +00001173 struct qstr q;
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001174 int err;
Matt Flemingda27a242013-02-01 11:02:28 +00001175
1176 q.name = name;
1177 q.len = strlen(name);
1178
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001179 err = efivarfs_d_hash(NULL, NULL, &q);
1180 if (err)
1181 return ERR_PTR(err);
Matt Flemingda27a242013-02-01 11:02:28 +00001182
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001183 d = d_alloc(parent, &q);
1184 if (d)
1185 return d;
1186
1187 return ERR_PTR(-ENOMEM);
Matt Flemingda27a242013-02-01 11:02:28 +00001188}
1189
Matt Fleminge83af1f2012-11-15 20:03:16 +00001190static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
Matthew Garrett5d9db882012-10-05 13:54:56 +08001191{
1192 struct inode *inode = NULL;
1193 struct dentry *root;
1194 struct efivar_entry *entry, *n;
1195 struct efivars *efivars = &__efivars;
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001196 char *name;
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001197 int err = -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001198
1199 efivarfs_sb = sb;
1200
1201 sb->s_maxbytes = MAX_LFS_FILESIZE;
1202 sb->s_blocksize = PAGE_CACHE_SIZE;
1203 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
Matt Fleming91716322012-10-22 15:51:45 +01001204 sb->s_magic = EFIVARFS_MAGIC;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001205 sb->s_op = &efivarfs_ops;
Matt Flemingda27a242013-02-01 11:02:28 +00001206 sb->s_d_op = &efivarfs_d_ops;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001207 sb->s_time_gran = 1;
1208
1209 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +01001210 if (!inode)
1211 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001212 inode->i_op = &efivarfs_dir_inode_operations;
1213
1214 root = d_make_root(inode);
1215 sb->s_root = root;
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +01001216 if (!root)
1217 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001218
1219 list_for_each_entry_safe(entry, n, &efivars->list, list) {
Matthew Garrett5d9db882012-10-05 13:54:56 +08001220 struct dentry *dentry, *root = efivarfs_sb->s_root;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001221 unsigned long size = 0;
1222 int len, i;
1223
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001224 inode = NULL;
1225
Matthew Garrett5d9db882012-10-05 13:54:56 +08001226 len = utf16_strlen(entry->var.VariableName);
1227
Jeremy Kerr310ad752012-10-19 15:16:45 +08001228 /* name, plus '-', plus GUID, plus NUL*/
1229 name = kmalloc(len + 1 + GUID_LEN + 1, GFP_ATOMIC);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001230 if (!name)
1231 goto fail;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001232
1233 for (i = 0; i < len; i++)
1234 name[i] = entry->var.VariableName[i] & 0xFF;
1235
1236 name[len] = '-';
1237
1238 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
1239
Jeremy Kerr310ad752012-10-19 15:16:45 +08001240 name[len+GUID_LEN+1] = '\0';
Matthew Garrett5d9db882012-10-05 13:54:56 +08001241
1242 inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
1243 S_IFREG | 0644, 0);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001244 if (!inode)
1245 goto fail_name;
1246
Matt Flemingda27a242013-02-01 11:02:28 +00001247 dentry = efivarfs_alloc_dentry(root, name);
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001248 if (IS_ERR(dentry)) {
1249 err = PTR_ERR(dentry);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001250 goto fail_inode;
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001251 }
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001252
Andy Whitcroftc0359db2012-10-11 11:32:20 +01001253 /* copied by the above to local storage in the dentry. */
1254 kfree(name);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001255
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001256 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001257 efivars->ops->get_variable(entry->var.VariableName,
1258 &entry->var.VendorGuid,
1259 &entry->var.Attributes,
1260 &size,
1261 NULL);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001262 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001263
1264 mutex_lock(&inode->i_mutex);
1265 inode->i_private = entry;
Matt Fleming94a193f2013-01-11 13:30:46 +00001266 i_size_write(inode, size + sizeof(entry->var.Attributes));
Matthew Garrett5d9db882012-10-05 13:54:56 +08001267 mutex_unlock(&inode->i_mutex);
1268 d_add(dentry, inode);
1269 }
1270
1271 return 0;
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001272
1273fail_inode:
1274 iput(inode);
1275fail_name:
1276 kfree(name);
1277fail:
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001278 return err;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001279}
1280
1281static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
1282 int flags, const char *dev_name, void *data)
1283{
1284 return mount_single(fs_type, flags, data, efivarfs_fill_super);
1285}
1286
1287static void efivarfs_kill_sb(struct super_block *sb)
1288{
1289 kill_litter_super(sb);
1290 efivarfs_sb = NULL;
1291}
1292
1293static struct file_system_type efivarfs_type = {
1294 .name = "efivarfs",
1295 .mount = efivarfs_mount,
1296 .kill_sb = efivarfs_kill_sb,
1297};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001298MODULE_ALIAS_FS("efivarfs");
Matthew Garrett5d9db882012-10-05 13:54:56 +08001299
Matt Flemingda27a242013-02-01 11:02:28 +00001300/*
1301 * Handle negative dentry.
1302 */
1303static struct dentry *efivarfs_lookup(struct inode *dir, struct dentry *dentry,
1304 unsigned int flags)
1305{
1306 if (dentry->d_name.len > NAME_MAX)
1307 return ERR_PTR(-ENAMETOOLONG);
1308 d_add(dentry, NULL);
1309 return NULL;
1310}
1311
Matthew Garrett5d9db882012-10-05 13:54:56 +08001312static const struct inode_operations efivarfs_dir_inode_operations = {
Matt Flemingda27a242013-02-01 11:02:28 +00001313 .lookup = efivarfs_lookup,
Matthew Garrett5d9db882012-10-05 13:54:56 +08001314 .unlink = efivarfs_unlink,
1315 .create = efivarfs_create,
1316};
1317
Seth Forsheeed9dc8c2013-03-07 11:40:17 -06001318#ifdef CONFIG_EFI_VARS_PSTORE
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001319
1320static int efi_pstore_open(struct pstore_info *psi)
1321{
1322 struct efivars *efivars = psi->data;
1323
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001324 spin_lock_irq(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001325 efivars->walk_entry = list_first_entry(&efivars->list,
1326 struct efivar_entry, list);
1327 return 0;
1328}
1329
1330static int efi_pstore_close(struct pstore_info *psi)
1331{
1332 struct efivars *efivars = psi->data;
1333
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001334 spin_unlock_irq(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001335 return 0;
1336}
1337
1338static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001339 int *count, struct timespec *timespec,
Kees Cookf6f82852011-11-17 12:58:07 -08001340 char **buf, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001341{
1342 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1343 struct efivars *efivars = psi->data;
1344 char name[DUMP_NAME_LEN];
1345 int i;
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001346 int cnt;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001347 unsigned int part, size;
1348 unsigned long time;
1349
1350 while (&efivars->walk_entry->list != &efivars->list) {
1351 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
1352 vendor)) {
1353 for (i = 0; i < DUMP_NAME_LEN; i++) {
1354 name[i] = efivars->walk_entry->var.VariableName[i];
1355 }
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001356 if (sscanf(name, "dump-type%u-%u-%d-%lu",
1357 type, &part, &cnt, &time) == 4) {
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001358 *id = part;
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001359 *count = cnt;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001360 timespec->tv_sec = time;
1361 timespec->tv_nsec = 0;
Seiji Aguchi0f7de852012-11-14 20:28:50 +00001362 } else if (sscanf(name, "dump-type%u-%u-%lu",
1363 type, &part, &time) == 3) {
1364 /*
1365 * Check if an old format,
1366 * which doesn't support holding
1367 * multiple logs, remains.
1368 */
1369 *id = part;
1370 *count = 0;
1371 timespec->tv_sec = time;
1372 timespec->tv_nsec = 0;
1373 } else {
1374 efivars->walk_entry = list_entry(
1375 efivars->walk_entry->list.next,
1376 struct efivar_entry, list);
1377 continue;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001378 }
Seiji Aguchi0f7de852012-11-14 20:28:50 +00001379
1380 get_var_data_locked(efivars, &efivars->walk_entry->var);
1381 size = efivars->walk_entry->var.DataSize;
1382 *buf = kmalloc(size, GFP_KERNEL);
1383 if (*buf == NULL)
1384 return -ENOMEM;
1385 memcpy(*buf, efivars->walk_entry->var.Data,
1386 size);
1387 efivars->walk_entry = list_entry(
1388 efivars->walk_entry->list.next,
1389 struct efivar_entry, list);
1390 return size;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001391 }
1392 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1393 struct efivar_entry, list);
1394 }
1395 return 0;
1396}
1397
Kees Cook3d6d8d22011-11-17 13:13:29 -08001398static int efi_pstore_write(enum pstore_type_id type,
1399 enum kmsg_dump_reason reason, u64 *id,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001400 unsigned int part, int count, size_t size,
1401 struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001402{
1403 char name[DUMP_NAME_LEN];
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001404 efi_char16_t efi_name[DUMP_NAME_LEN];
1405 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1406 struct efivars *efivars = psi->data;
Chen Gongb238b8f2011-10-12 09:17:24 -07001407 int i, ret = 0;
Seiji Aguchid80a3612012-11-14 20:25:37 +00001408 efi_status_t status = EFI_NOT_FOUND;
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001409 unsigned long flags;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001410
Seiji Aguchie59310a2013-01-11 18:10:05 +00001411 if (pstore_cannot_block_path(reason)) {
1412 /*
1413 * If the lock is taken by another cpu in non-blocking path,
1414 * this driver returns without entering firmware to avoid
1415 * hanging up.
1416 */
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001417 if (!spin_trylock_irqsave(&efivars->lock, flags))
Seiji Aguchie59310a2013-01-11 18:10:05 +00001418 return -EBUSY;
1419 } else
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001420 spin_lock_irqsave(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001421
Seiji Aguchid80a3612012-11-14 20:25:37 +00001422 /*
1423 * Check if there is a space enough to log.
1424 * size: a size of logging data
1425 * DUMP_NAME_LEN * 2: a maximum size of variable name
1426 */
Matthew Garrett68d92982013-03-02 19:40:17 -05001427
1428 status = check_var_size_locked(efivars, PSTORE_EFI_ATTRIBUTES,
1429 size + DUMP_NAME_LEN * 2);
1430
1431 if (status) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001432 spin_unlock_irqrestore(&efivars->lock, flags);
Seiji Aguchid80a3612012-11-14 20:25:37 +00001433 *id = part;
1434 return -ENOSPC;
1435 }
1436
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001437 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
Seiji Aguchi96480d92012-11-14 20:26:46 +00001438 get_seconds());
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001439
1440 for (i = 0; i < DUMP_NAME_LEN; i++)
1441 efi_name[i] = name[i];
1442
Mike Waychison7644c162011-07-21 16:58:00 -04001443 efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001444 size, psi->buf);
1445
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001446 spin_unlock_irqrestore(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001447
Matt Fleminge9713182013-03-07 11:59:14 +00001448 if (reason == KMSG_DUMP_OOPS && efivar_wq_enabled)
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001449 schedule_work(&efivar_work);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001450
Chen Gongb238b8f2011-10-12 09:17:24 -07001451 *id = part;
1452 return ret;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001453};
1454
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001455static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001456 struct timespec time, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001457{
Seiji Aguchia9efd392012-11-14 20:27:28 +00001458 char name[DUMP_NAME_LEN];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001459 efi_char16_t efi_name[DUMP_NAME_LEN];
Seiji Aguchif94ec0c2012-11-14 20:29:21 +00001460 char name_old[DUMP_NAME_LEN];
1461 efi_char16_t efi_name_old[DUMP_NAME_LEN];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001462 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1463 struct efivars *efivars = psi->data;
1464 struct efivar_entry *entry, *found = NULL;
1465 int i;
1466
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001467 sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001468 time.tv_sec);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001469
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001470 spin_lock_irq(&efivars->lock);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001471
1472 for (i = 0; i < DUMP_NAME_LEN; i++)
Seiji Aguchia9efd392012-11-14 20:27:28 +00001473 efi_name[i] = name[i];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001474
1475 /*
Seiji Aguchia9efd392012-11-14 20:27:28 +00001476 * Clean up an entry with the same name
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001477 */
1478
1479 list_for_each_entry(entry, &efivars->list, list) {
1480 get_var_data_locked(efivars, &entry->var);
1481
1482 if (efi_guidcmp(entry->var.VendorGuid, vendor))
1483 continue;
1484 if (utf16_strncmp(entry->var.VariableName, efi_name,
Seiji Aguchif94ec0c2012-11-14 20:29:21 +00001485 utf16_strlen(efi_name))) {
1486 /*
1487 * Check if an old format,
1488 * which doesn't support holding
1489 * multiple logs, remains.
1490 */
1491 sprintf(name_old, "dump-type%u-%u-%lu", type,
1492 (unsigned int)id, time.tv_sec);
1493
1494 for (i = 0; i < DUMP_NAME_LEN; i++)
1495 efi_name_old[i] = name_old[i];
1496
1497 if (utf16_strncmp(entry->var.VariableName, efi_name_old,
1498 utf16_strlen(efi_name_old)))
1499 continue;
1500 }
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001501
1502 /* found */
1503 found = entry;
1504 efivars->ops->set_variable(entry->var.VariableName,
1505 &entry->var.VendorGuid,
1506 PSTORE_EFI_ATTRIBUTES,
1507 0, NULL);
Seiji Aguchia9efd392012-11-14 20:27:28 +00001508 break;
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001509 }
1510
1511 if (found)
1512 list_del(&found->list);
1513
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001514 spin_unlock_irq(&efivars->lock);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001515
1516 if (found)
1517 efivar_unregister(found);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001518
1519 return 0;
1520}
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001521
1522static struct pstore_info efi_pstore_info = {
1523 .owner = THIS_MODULE,
1524 .name = "efi",
1525 .open = efi_pstore_open,
1526 .close = efi_pstore_close,
1527 .read = efi_pstore_read,
1528 .write = efi_pstore_write,
1529 .erase = efi_pstore_erase,
1530};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Seth Forsheeed9dc8c2013-03-07 11:40:17 -06001532static void efivar_pstore_register(struct efivars *efivars)
1533{
1534 efivars->efi_pstore_info = efi_pstore_info;
1535 efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
1536 if (efivars->efi_pstore_info.buf) {
1537 efivars->efi_pstore_info.bufsize = 1024;
1538 efivars->efi_pstore_info.data = efivars;
1539 spin_lock_init(&efivars->efi_pstore_info.buf_lock);
1540 pstore_register(&efivars->efi_pstore_info);
1541 }
1542}
1543#else
1544static void efivar_pstore_register(struct efivars *efivars)
1545{
1546 return;
1547}
1548#endif
1549
Chris Wright2c3c8be2010-05-12 18:28:57 -07001550static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001551 struct bin_attribute *bin_attr,
1552 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 struct efi_variable *new_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001555 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001556 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 efi_status_t status = EFI_NOT_FOUND;
1559 int found = 0;
1560
1561 if (!capable(CAP_SYS_ADMIN))
1562 return -EACCES;
1563
Matthew Garrettfec6c202012-04-30 16:11:30 -04001564 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
1565 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
1566 printk(KERN_ERR "efivars: Malformed variable content\n");
1567 return -EINVAL;
1568 }
1569
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001570 spin_lock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 /*
1573 * Does this variable already exist?
1574 */
Mike Waychison29422692011-03-11 17:43:00 -08001575 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -04001576 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1577 strsize2 = utf16_strsize(new_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (strsize1 == strsize2 &&
1579 !memcmp(&(search_efivar->var.VariableName),
1580 new_var->VariableName, strsize1) &&
1581 !efi_guidcmp(search_efivar->var.VendorGuid,
1582 new_var->VendorGuid)) {
1583 found = 1;
1584 break;
1585 }
1586 }
1587 if (found) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001588 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return -EINVAL;
1590 }
1591
Matthew Garrett68d92982013-03-02 19:40:17 -05001592 status = check_var_size_locked(efivars, new_var->Attributes,
1593 new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
1594
1595 if (status && status != EFI_UNSUPPORTED) {
1596 spin_unlock_irq(&efivars->lock);
1597 return efi_status_to_err(status);
1598 }
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 /* now *really* create the variable via EFI */
Mike Waychison32958142011-03-11 17:43:21 -08001601 status = efivars->ops->set_variable(new_var->VariableName,
1602 &new_var->VendorGuid,
1603 new_var->Attributes,
1604 new_var->DataSize,
1605 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 if (status != EFI_SUCCESS) {
1608 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1609 status);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001610 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return -EIO;
1612 }
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001613 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 /* Create the entry in sysfs. Locking is not required here */
Mike Waychison4142ef12011-03-11 17:43:11 -08001616 status = efivar_create_sysfs_entry(efivars,
Mike Waychisona2940902011-07-21 16:57:57 -04001617 utf16_strsize(new_var->VariableName,
1618 1024),
Mike Waychison4142ef12011-03-11 17:43:11 -08001619 new_var->VariableName,
1620 &new_var->VendorGuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (status) {
1622 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
1623 }
1624 return count;
1625}
1626
Chris Wright2c3c8be2010-05-12 18:28:57 -07001627static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001628 struct bin_attribute *bin_attr,
1629 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
1631 struct efi_variable *del_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001632 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001633 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 efi_status_t status = EFI_NOT_FOUND;
1636 int found = 0;
1637
1638 if (!capable(CAP_SYS_ADMIN))
1639 return -EACCES;
1640
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001641 spin_lock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 /*
1644 * Does this variable already exist?
1645 */
Mike Waychison29422692011-03-11 17:43:00 -08001646 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -04001647 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1648 strsize2 = utf16_strsize(del_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 if (strsize1 == strsize2 &&
1650 !memcmp(&(search_efivar->var.VariableName),
1651 del_var->VariableName, strsize1) &&
1652 !efi_guidcmp(search_efivar->var.VendorGuid,
1653 del_var->VendorGuid)) {
1654 found = 1;
1655 break;
1656 }
1657 }
1658 if (!found) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001659 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return -EINVAL;
1661 }
1662 /* force the Attributes/DataSize to 0 to ensure deletion */
1663 del_var->Attributes = 0;
1664 del_var->DataSize = 0;
1665
Mike Waychison32958142011-03-11 17:43:21 -08001666 status = efivars->ops->set_variable(del_var->VariableName,
1667 &del_var->VendorGuid,
1668 del_var->Attributes,
1669 del_var->DataSize,
1670 del_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 if (status != EFI_SUCCESS) {
1673 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1674 status);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001675 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return -EIO;
1677 }
Matt Domsch496a0fc2007-01-26 00:57:18 -08001678 list_del(&search_efivar->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 /* We need to release this lock before unregistering. */
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001680 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 efivar_unregister(search_efivar);
1682
1683 /* It's dead Jim.... */
1684 return count;
1685}
1686
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001687static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor)
1688{
1689 struct efivar_entry *entry, *n;
1690 struct efivars *efivars = &__efivars;
1691 unsigned long strsize1, strsize2;
1692 bool found = false;
1693
1694 strsize1 = utf16_strsize(variable_name, 1024);
1695 list_for_each_entry_safe(entry, n, &efivars->list, list) {
1696 strsize2 = utf16_strsize(entry->var.VariableName, 1024);
1697 if (strsize1 == strsize2 &&
1698 !memcmp(variable_name, &(entry->var.VariableName),
1699 strsize2) &&
1700 !efi_guidcmp(entry->var.VendorGuid,
1701 *vendor)) {
1702 found = true;
1703 break;
1704 }
1705 }
1706 return found;
1707}
1708
Matt Flemingec50bd32013-03-01 14:49:12 +00001709/*
1710 * Returns the size of variable_name, in bytes, including the
1711 * terminating NULL character, or variable_name_size if no NULL
1712 * character is found among the first variable_name_size bytes.
1713 */
1714static unsigned long var_name_strnsize(efi_char16_t *variable_name,
1715 unsigned long variable_name_size)
1716{
1717 unsigned long len;
1718 efi_char16_t c;
1719
1720 /*
1721 * The variable name is, by definition, a NULL-terminated
1722 * string, so make absolutely sure that variable_name_size is
1723 * the value we expect it to be. If not, return the real size.
1724 */
1725 for (len = 2; len <= variable_name_size; len += sizeof(c)) {
1726 c = variable_name[(len / sizeof(c)) - 1];
1727 if (!c)
1728 break;
1729 }
1730
1731 return min(len, variable_name_size);
1732}
1733
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001734static void efivar_update_sysfs_entries(struct work_struct *work)
1735{
1736 struct efivars *efivars = &__efivars;
1737 efi_guid_t vendor;
1738 efi_char16_t *variable_name;
1739 unsigned long variable_name_size = 1024;
1740 efi_status_t status = EFI_NOT_FOUND;
1741 bool found;
1742
1743 /* Add new sysfs entries */
1744 while (1) {
1745 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
1746 if (!variable_name) {
1747 pr_err("efivars: Memory allocation failed.\n");
1748 return;
1749 }
1750
1751 spin_lock_irq(&efivars->lock);
1752 found = false;
1753 while (1) {
1754 variable_name_size = 1024;
1755 status = efivars->ops->get_next_variable(
1756 &variable_name_size,
1757 variable_name,
1758 &vendor);
1759 if (status != EFI_SUCCESS) {
1760 break;
1761 } else {
1762 if (!variable_is_present(variable_name,
1763 &vendor)) {
1764 found = true;
1765 break;
1766 }
1767 }
1768 }
1769 spin_unlock_irq(&efivars->lock);
1770
1771 if (!found) {
1772 kfree(variable_name);
1773 break;
Matt Flemingec50bd32013-03-01 14:49:12 +00001774 } else {
1775 variable_name_size = var_name_strnsize(variable_name,
1776 variable_name_size);
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001777 efivar_create_sysfs_entry(efivars,
1778 variable_name_size,
1779 variable_name, &vendor);
Matt Flemingec50bd32013-03-01 14:49:12 +00001780 }
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001781 }
1782}
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784/*
1785 * Let's not leave out systab information that snuck into
1786 * the efivars driver
1787 */
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001788static ssize_t systab_show(struct kobject *kobj,
1789 struct kobj_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 char *str = buf;
1792
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001793 if (!kobj || !buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return -EINVAL;
1795
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -08001796 if (efi.mps != EFI_INVALID_TABLE_ADDR)
1797 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
1798 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
1799 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
1800 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
1801 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
1802 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
1803 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
1804 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
1805 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
1806 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
1807 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
1808 if (efi.uga != EFI_INVALID_TABLE_ADDR)
1809 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 return str - buf;
1812}
1813
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001814static struct kobj_attribute efi_attr_systab =
1815 __ATTR(systab, 0400, systab_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001817static struct attribute *efi_subsys_attrs[] = {
1818 &efi_attr_systab.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 NULL, /* maybe more in the future? */
1820};
1821
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001822static struct attribute_group efi_subsys_attr_group = {
1823 .attrs = efi_subsys_attrs,
1824};
1825
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08001826static struct kobject *efi_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828/*
1829 * efivar_create_sysfs_entry()
1830 * Requires:
1831 * variable_name_size = number of bytes required to hold
1832 * variable_name (not counting the NULL
1833 * character at the end.
Mike Waychison29422692011-03-11 17:43:00 -08001834 * efivars->lock is not held on entry or exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 * Returns 1 on failure, 0 on success
1836 */
1837static int
Mike Waychison4142ef12011-03-11 17:43:11 -08001838efivar_create_sysfs_entry(struct efivars *efivars,
1839 unsigned long variable_name_size,
1840 efi_char16_t *variable_name,
1841 efi_guid_t *vendor_guid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Jeremy Kerr310ad752012-10-19 15:16:45 +08001843 int i, short_name_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 char *short_name;
1845 struct efivar_entry *new_efivar;
1846
Jeremy Kerr310ad752012-10-19 15:16:45 +08001847 /*
1848 * Length of the variable bytes in ASCII, plus the '-' separator,
1849 * plus the GUID, plus trailing NUL
1850 */
1851 short_name_size = variable_name_size / sizeof(efi_char16_t)
1852 + 1 + GUID_LEN + 1;
1853
1854 short_name = kzalloc(short_name_size, GFP_KERNEL);
Deepak Saxena9c215382005-11-07 01:01:24 -08001855 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 if (!short_name || !new_efivar) {
Jesper Juhl0933ad92005-06-25 14:59:15 -07001858 kfree(short_name);
1859 kfree(new_efivar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return 1;
1861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Mike Waychison4142ef12011-03-11 17:43:11 -08001863 new_efivar->efivars = efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 memcpy(new_efivar->var.VariableName, variable_name,
1865 variable_name_size);
1866 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
1867
1868 /* Convert Unicode to normal chars (assume top bits are 0),
1869 ala UTF-8 */
1870 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
1871 short_name[i] = variable_name[i] & 0xFF;
1872 }
1873 /* This is ugly, but necessary to separate one vendor's
1874 private variables from another's. */
1875
1876 *(short_name + strlen(short_name)) = '-';
1877 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
1878
Mike Waychison29422692011-03-11 17:43:00 -08001879 new_efivar->kobj.kset = efivars->kset;
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001880 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
1881 "%s", short_name);
Jeff Garzik69b21862006-10-11 01:22:23 -07001882 if (i) {
1883 kfree(short_name);
1884 kfree(new_efivar);
1885 return 1;
1886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001888 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
Jesper Juhl0933ad92005-06-25 14:59:15 -07001889 kfree(short_name);
1890 short_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001892 spin_lock_irq(&efivars->lock);
Mike Waychison29422692011-03-11 17:43:00 -08001893 list_add(&new_efivar->list, &efivars->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001894 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 return 0;
1897}
Mike Waychisond502fbb2011-03-11 17:43:06 -08001898
1899static int
1900create_efivars_bin_attributes(struct efivars *efivars)
1901{
1902 struct bin_attribute *attr;
1903 int error;
1904
1905 /* new_var */
1906 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1907 if (!attr)
1908 return -ENOMEM;
1909
1910 attr->attr.name = "new_var";
1911 attr->attr.mode = 0200;
1912 attr->write = efivar_create;
1913 attr->private = efivars;
1914 efivars->new_var = attr;
1915
1916 /* del_var */
1917 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1918 if (!attr) {
1919 error = -ENOMEM;
1920 goto out_free;
1921 }
1922 attr->attr.name = "del_var";
1923 attr->attr.mode = 0200;
1924 attr->write = efivar_delete;
1925 attr->private = efivars;
1926 efivars->del_var = attr;
1927
1928 sysfs_bin_attr_init(efivars->new_var);
1929 sysfs_bin_attr_init(efivars->del_var);
1930
1931 /* Register */
1932 error = sysfs_create_bin_file(&efivars->kset->kobj,
1933 efivars->new_var);
1934 if (error) {
1935 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
1936 " due to error %d\n", error);
1937 goto out_free;
1938 }
1939 error = sysfs_create_bin_file(&efivars->kset->kobj,
1940 efivars->del_var);
1941 if (error) {
1942 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
1943 " due to error %d\n", error);
1944 sysfs_remove_bin_file(&efivars->kset->kobj,
1945 efivars->new_var);
1946 goto out_free;
1947 }
1948
1949 return 0;
1950out_free:
Dan Carpenter051d51b2011-03-18 10:12:14 +03001951 kfree(efivars->del_var);
1952 efivars->del_var = NULL;
Mike Waychisond502fbb2011-03-11 17:43:06 -08001953 kfree(efivars->new_var);
1954 efivars->new_var = NULL;
1955 return error;
1956}
1957
Mike Waychison4fc756bd2011-03-11 17:43:27 -08001958void unregister_efivars(struct efivars *efivars)
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001959{
1960 struct efivar_entry *entry, *n;
Mike Waychison4142ef12011-03-11 17:43:11 -08001961
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001962 list_for_each_entry_safe(entry, n, &efivars->list, list) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001963 spin_lock_irq(&efivars->lock);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001964 list_del(&entry->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001965 spin_unlock_irq(&efivars->lock);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001966 efivar_unregister(entry);
1967 }
1968 if (efivars->new_var)
1969 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
1970 if (efivars->del_var)
1971 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
1972 kfree(efivars->new_var);
1973 kfree(efivars->del_var);
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08001974 kobject_put(efivars->kobject);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001975 kset_unregister(efivars->kset);
1976}
Mike Waychison4fc756bd2011-03-11 17:43:27 -08001977EXPORT_SYMBOL_GPL(unregister_efivars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Matt Fleminge9713182013-03-07 11:59:14 +00001979/*
1980 * Print a warning when duplicate EFI variables are encountered and
1981 * disable the sysfs workqueue since the firmware is buggy.
1982 */
1983static void dup_variable_bug(efi_char16_t *s16, efi_guid_t *vendor_guid,
1984 unsigned long len16)
1985{
1986 size_t i, len8 = len16 / sizeof(efi_char16_t);
1987 char *s8;
1988
1989 /*
1990 * Disable the workqueue since the algorithm it uses for
1991 * detecting new variables won't work with this buggy
1992 * implementation of GetNextVariableName().
1993 */
1994 efivar_wq_enabled = false;
1995
1996 s8 = kzalloc(len8, GFP_KERNEL);
1997 if (!s8)
1998 return;
1999
2000 for (i = 0; i < len8; i++)
2001 s8[i] = s16[i];
2002
2003 printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
2004 s8, vendor_guid);
2005 kfree(s8);
2006}
2007
Mike Waychison4fc756bd2011-03-11 17:43:27 -08002008int register_efivars(struct efivars *efivars,
2009 const struct efivar_operations *ops,
2010 struct kobject *parent_kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
2012 efi_status_t status = EFI_NOT_FOUND;
2013 efi_guid_t vendor_guid;
2014 efi_char16_t *variable_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 unsigned long variable_name_size = 1024;
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07002016 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Deepak Saxena9c215382005-11-07 01:01:24 -08002018 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (!variable_name) {
2020 printk(KERN_ERR "efivars: Memory allocation failed.\n");
2021 return -ENOMEM;
2022 }
2023
Mike Waychison29422692011-03-11 17:43:00 -08002024 spin_lock_init(&efivars->lock);
2025 INIT_LIST_HEAD(&efivars->list);
Mike Waychison32958142011-03-11 17:43:21 -08002026 efivars->ops = ops;
Mike Waychison29422692011-03-11 17:43:00 -08002027
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002028 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
Mike Waychison29422692011-03-11 17:43:00 -08002029 if (!efivars->kset) {
Greg Kroah-Hartman66ac8312007-11-02 13:20:40 -07002030 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
2031 error = -ENOMEM;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002032 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
2034
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08002035 efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
2036 if (!efivars->kobject) {
2037 pr_err("efivars: Subsystem registration failed.\n");
2038 error = -ENOMEM;
2039 kset_unregister(efivars->kset);
2040 goto out;
2041 }
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 /*
2044 * Per EFI spec, the maximum storage allocated for both
2045 * the variable name and variable data is 1024 bytes.
2046 */
2047
2048 do {
2049 variable_name_size = 1024;
2050
Mike Waychison32958142011-03-11 17:43:21 -08002051 status = ops->get_next_variable(&variable_name_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 variable_name,
2053 &vendor_guid);
2054 switch (status) {
2055 case EFI_SUCCESS:
Matt Flemingec50bd32013-03-01 14:49:12 +00002056 variable_name_size = var_name_strnsize(variable_name,
2057 variable_name_size);
Matt Fleminge9713182013-03-07 11:59:14 +00002058
2059 /*
2060 * Some firmware implementations return the
2061 * same variable name on multiple calls to
2062 * get_next_variable(). Terminate the loop
2063 * immediately as there is no guarantee that
2064 * we'll ever see a different variable name,
2065 * and may end up looping here forever.
2066 */
2067 if (variable_is_present(variable_name, &vendor_guid)) {
2068 dup_variable_bug(variable_name, &vendor_guid,
2069 variable_name_size);
2070 status = EFI_NOT_FOUND;
2071 break;
2072 }
2073
Mike Waychison4142ef12011-03-11 17:43:11 -08002074 efivar_create_sysfs_entry(efivars,
2075 variable_name_size,
2076 variable_name,
2077 &vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 break;
2079 case EFI_NOT_FOUND:
2080 break;
2081 default:
2082 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
2083 status);
2084 status = EFI_NOT_FOUND;
2085 break;
2086 }
2087 } while (status != EFI_NOT_FOUND);
2088
Mike Waychisond502fbb2011-03-11 17:43:06 -08002089 error = create_efivars_bin_attributes(efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002090 if (error)
2091 unregister_efivars(efivars);
2092
Seth Forsheeec0971b2013-03-11 16:17:50 -05002093 if (!efivars_pstore_disable)
2094 efivar_pstore_register(efivars);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04002095
Matthew Garrett5d9db882012-10-05 13:54:56 +08002096 register_filesystem(&efivarfs_type);
2097
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002098out:
2099 kfree(variable_name);
2100
2101 return error;
2102}
Mike Waychison4fc756bd2011-03-11 17:43:27 -08002103EXPORT_SYMBOL_GPL(register_efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002104
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002105/*
2106 * For now we register the efi subsystem with the firmware subsystem
2107 * and the vars subsystem with the efi subsystem. In the future, it
2108 * might make sense to split off the efi subsystem into its own
2109 * driver, but for now only efivars will register with it, so just
2110 * include it here.
2111 */
2112
2113static int __init
2114efivars_init(void)
2115{
2116 int error = 0;
2117
2118 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
2119 EFIVARS_DATE);
2120
Matt Fleming83e68182012-11-14 09:42:35 +00002121 if (!efi_enabled(EFI_RUNTIME_SERVICES))
Mike Waychison4fc756bd2011-03-11 17:43:27 -08002122 return 0;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002123
2124 /* For now we'll register the efi directory at /sys/firmware/efi */
2125 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
2126 if (!efi_kobj) {
2127 printk(KERN_ERR "efivars: Firmware registration failed.\n");
2128 return -ENOMEM;
2129 }
2130
Mike Waychison32958142011-03-11 17:43:21 -08002131 ops.get_variable = efi.get_variable;
2132 ops.set_variable = efi.set_variable;
2133 ops.get_next_variable = efi.get_next_variable;
Seiji Aguchid80a3612012-11-14 20:25:37 +00002134 ops.query_variable_info = efi.query_variable_info;
Matt Fleming89d16662012-11-09 21:02:56 +00002135
Mike Waychison32958142011-03-11 17:43:21 -08002136 error = register_efivars(&__efivars, &ops, efi_kobj);
Dan Carpenter3116aab2011-03-18 10:12:38 +03002137 if (error)
2138 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 /* Don't forget the systab entry */
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08002141 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002142 if (error) {
2143 printk(KERN_ERR
2144 "efivars: Sysfs attribute export failed with error %d.\n",
2145 error);
Dan Carpenter3116aab2011-03-18 10:12:38 +03002146 goto err_unregister;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Dan Carpenter3116aab2011-03-18 10:12:38 +03002149 return 0;
2150
2151err_unregister:
2152 unregister_efivars(&__efivars);
2153err_put:
2154 kobject_put(efi_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return error;
2156}
2157
2158static void __exit
2159efivars_exit(void)
2160{
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08002161 cancel_work_sync(&efivar_work);
2162
Matt Fleming83e68182012-11-14 09:42:35 +00002163 if (efi_enabled(EFI_RUNTIME_SERVICES)) {
Randy Dunlapaabb6e12011-05-06 13:27:41 -07002164 unregister_efivars(&__efivars);
2165 kobject_put(efi_kobj);
2166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
2169module_init(efivars_init);
2170module_exit(efivars_exit);
2171