blob: f4baa11d3644d80377c3bdc34d86550d1c2eaeef [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>
Matthew Garrett0635eb82013-04-15 13:09:45 -070083#include <linux/ucs2_string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Matthew Garrett5d9db882012-10-05 13:54:56 +080085#include <linux/fs.h>
86#include <linux/ramfs.h>
87#include <linux/pagemap.h>
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <asm/uaccess.h>
90
91#define EFIVARS_VERSION "0.08"
92#define EFIVARS_DATE "2004-May-17"
93
94MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
95MODULE_DESCRIPTION("sysfs interface to EFI Variables");
96MODULE_LICENSE("GPL");
97MODULE_VERSION(EFIVARS_VERSION);
98
Matthew Garrett5ee9c192011-07-21 16:57:56 -040099#define DUMP_NAME_LEN 52
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
Jeremy Kerr310ad752012-10-19 15:16:45 +0800102 * Length of a GUID string (strlen("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))
103 * not including trailing NUL
104 */
105#define GUID_LEN 36
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Seth Forsheeec0971b2013-03-11 16:17:50 -0500107static bool efivars_pstore_disable =
Ben Hutchingsca0ba262013-03-22 19:56:51 +0000108 IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
Seth Forsheeec0971b2013-03-11 16:17:50 -0500109
110module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*
113 * The maximum size of VariableName + Data = 1024
114 * Therefore, it's reasonable to save that much
115 * space in each part of the structure,
116 * and we use a page for reading/writing.
117 */
118
119struct efi_variable {
120 efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
121 efi_guid_t VendorGuid;
122 unsigned long DataSize;
123 __u8 Data[1024];
124 efi_status_t Status;
125 __u32 Attributes;
126} __attribute__((packed));
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128struct efivar_entry {
Mike Waychison4142ef12011-03-11 17:43:11 -0800129 struct efivars *efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct efi_variable var;
131 struct list_head list;
132 struct kobject kobj;
133};
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135struct efivar_attribute {
136 struct attribute attr;
137 ssize_t (*show) (struct efivar_entry *entry, char *buf);
138 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
139};
140
Matthew Garrett5d9db882012-10-05 13:54:56 +0800141static struct efivars __efivars;
142static struct efivar_operations ops;
143
Mike Waychison7644c162011-07-21 16:58:00 -0400144#define PSTORE_EFI_ATTRIBUTES \
145 (EFI_VARIABLE_NON_VOLATILE | \
146 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
147 EFI_VARIABLE_RUNTIME_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#define EFIVAR_ATTR(_name, _mode, _show, _store) \
150struct efivar_attribute efivar_attr_##_name = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900151 .attr = {.name = __stringify(_name), .mode = _mode}, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 .show = _show, \
153 .store = _store, \
154};
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
157#define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
158
159/*
160 * Prototype for sysfs creation function
161 */
162static int
Mike Waychison4142ef12011-03-11 17:43:11 -0800163efivar_create_sysfs_entry(struct efivars *efivars,
164 unsigned long variable_name_size,
165 efi_char16_t *variable_name,
166 efi_guid_t *vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Seiji Aguchia93bc0c2013-02-12 13:04:41 -0800168/*
169 * Prototype for workqueue functions updating sysfs entry
170 */
171
172static void efivar_update_sysfs_entries(struct work_struct *);
173static DECLARE_WORK(efivar_work, efivar_update_sysfs_entries);
Matt Fleminge9713182013-03-07 11:59:14 +0000174static bool efivar_wq_enabled = true;
Seiji Aguchia93bc0c2013-02-12 13:04:41 -0800175
Matthew Garrettfec6c202012-04-30 16:11:30 -0400176static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400177validate_device_path(struct efi_variable *var, int match, u8 *buffer,
178 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400179{
180 struct efi_generic_dev_path *node;
181 int offset = 0;
182
183 node = (struct efi_generic_dev_path *)buffer;
184
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400185 if (len < sizeof(*node))
186 return false;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400187
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400188 while (offset <= len - sizeof(*node) &&
189 node->length >= sizeof(*node) &&
190 node->length <= len - offset) {
191 offset += node->length;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400192
193 if ((node->type == EFI_DEV_END_PATH ||
194 node->type == EFI_DEV_END_PATH2) &&
195 node->sub_type == EFI_DEV_END_ENTIRE)
196 return true;
197
198 node = (struct efi_generic_dev_path *)(buffer + offset);
199 }
200
201 /*
202 * If we're here then either node->length pointed past the end
203 * of the buffer or we reached the end of the buffer without
204 * finding a device path end node.
205 */
206 return false;
207}
208
209static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400210validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
211 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400212{
213 /* An array of 16-bit integers */
214 if ((len % 2) != 0)
215 return false;
216
217 return true;
218}
219
220static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400221validate_load_option(struct efi_variable *var, int match, u8 *buffer,
222 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400223{
224 u16 filepathlength;
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400225 int i, desclength = 0, namelen;
226
Matthew Garrett0635eb82013-04-15 13:09:45 -0700227 namelen = ucs2_strnlen(var->VariableName, sizeof(var->VariableName));
Matthew Garrettfec6c202012-04-30 16:11:30 -0400228
229 /* Either "Boot" or "Driver" followed by four digits of hex */
230 for (i = match; i < match+4; i++) {
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400231 if (var->VariableName[i] > 127 ||
232 hex_to_bin(var->VariableName[i] & 0xff) < 0)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400233 return true;
234 }
235
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400236 /* Reject it if there's 4 digits of hex and then further content */
237 if (namelen > match + 4)
238 return false;
239
240 /* A valid entry must be at least 8 bytes */
241 if (len < 8)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400242 return false;
243
244 filepathlength = buffer[4] | buffer[5] << 8;
245
246 /*
247 * There's no stored length for the description, so it has to be
248 * found by hand
249 */
Matthew Garrett0635eb82013-04-15 13:09:45 -0700250 desclength = ucs2_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
Matthew Garrettfec6c202012-04-30 16:11:30 -0400251
252 /* Each boot entry must have a descriptor */
253 if (!desclength)
254 return false;
255
256 /*
257 * If the sum of the length of the description, the claimed filepath
258 * length and the original header are greater than the length of the
259 * variable, it's malformed
260 */
261 if ((desclength + filepathlength + 6) > len)
262 return false;
263
264 /*
265 * And, finally, check the filepath
266 */
267 return validate_device_path(var, match, buffer + desclength + 6,
268 filepathlength);
269}
270
271static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400272validate_uint16(struct efi_variable *var, int match, u8 *buffer,
273 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400274{
275 /* A single 16-bit integer */
276 if (len != 2)
277 return false;
278
279 return true;
280}
281
282static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400283validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
284 unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400285{
286 int i;
287
288 for (i = 0; i < len; i++) {
289 if (buffer[i] > 127)
290 return false;
291
292 if (buffer[i] == 0)
293 return true;
294 }
295
296 return false;
297}
298
299struct variable_validate {
300 char *name;
301 bool (*validate)(struct efi_variable *var, int match, u8 *data,
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400302 unsigned long len);
Matthew Garrettfec6c202012-04-30 16:11:30 -0400303};
304
305static const struct variable_validate variable_validate[] = {
306 { "BootNext", validate_uint16 },
307 { "BootOrder", validate_boot_order },
308 { "DriverOrder", validate_boot_order },
309 { "Boot*", validate_load_option },
310 { "Driver*", validate_load_option },
311 { "ConIn", validate_device_path },
312 { "ConInDev", validate_device_path },
313 { "ConOut", validate_device_path },
314 { "ConOutDev", validate_device_path },
315 { "ErrOut", validate_device_path },
316 { "ErrOutDev", validate_device_path },
317 { "Timeout", validate_uint16 },
318 { "Lang", validate_ascii_string },
319 { "PlatformLang", validate_ascii_string },
320 { "", NULL },
321};
322
323static bool
Matthew Garrett54b3a4d2012-05-03 16:50:46 -0400324validate_var(struct efi_variable *var, u8 *data, unsigned long len)
Matthew Garrettfec6c202012-04-30 16:11:30 -0400325{
326 int i;
327 u16 *unicode_name = var->VariableName;
328
329 for (i = 0; variable_validate[i].validate != NULL; i++) {
330 const char *name = variable_validate[i].name;
331 int match;
332
333 for (match = 0; ; match++) {
334 char c = name[match];
335 u16 u = unicode_name[match];
336
337 /* All special variables are plain ascii */
338 if (u > 127)
339 return true;
340
341 /* Wildcard in the matching name means we've matched */
342 if (c == '*')
343 return variable_validate[i].validate(var,
344 match, data, len);
345
346 /* Case sensitive match */
347 if (c != u)
348 break;
349
350 /* Reached the end of the string while matching */
351 if (!c)
352 return variable_validate[i].validate(var,
353 match, data, len);
354 }
355 }
356
357 return true;
358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360static efi_status_t
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400361get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 efi_status_t status;
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 var->DataSize = 1024;
Mike Waychison32958142011-03-11 17:43:21 -0800366 status = efivars->ops->get_variable(var->VariableName,
367 &var->VendorGuid,
368 &var->Attributes,
369 &var->DataSize,
370 var->Data);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400371 return status;
372}
373
374static efi_status_t
375get_var_data(struct efivars *efivars, struct efi_variable *var)
376{
377 efi_status_t status;
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800378 unsigned long flags;
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400379
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800380 spin_lock_irqsave(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400381 status = get_var_data_locked(efivars, var);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800382 spin_unlock_irqrestore(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (status != EFI_SUCCESS) {
385 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
386 status);
387 }
388 return status;
389}
390
Matthew Garrett68d92982013-03-02 19:40:17 -0500391static efi_status_t
392check_var_size_locked(struct efivars *efivars, u32 attributes,
393 unsigned long size)
394{
Matthew Garrett68d92982013-03-02 19:40:17 -0500395 const struct efivar_operations *fops = efivars->ops;
396
Matt Fleminga6e4d5a2013-03-25 09:14:30 +0000397 if (!efivars->ops->query_variable_store)
Matthew Garrett68d92982013-03-02 19:40:17 -0500398 return EFI_UNSUPPORTED;
399
Matt Fleminga6e4d5a2013-03-25 09:14:30 +0000400 return fops->query_variable_store(attributes, size);
Matthew Garrett68d92982013-03-02 19:40:17 -0500401}
402
403
404static efi_status_t
405check_var_size(struct efivars *efivars, u32 attributes, unsigned long size)
406{
407 efi_status_t status;
408 unsigned long flags;
409
410 spin_lock_irqsave(&efivars->lock, flags);
411 status = check_var_size_locked(efivars, attributes, size);
412 spin_unlock_irqrestore(&efivars->lock, flags);
413
414 return status;
415}
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417static ssize_t
418efivar_guid_read(struct efivar_entry *entry, char *buf)
419{
420 struct efi_variable *var = &entry->var;
421 char *str = buf;
422
423 if (!entry || !buf)
424 return 0;
425
426 efi_guid_unparse(&var->VendorGuid, str);
427 str += strlen(str);
428 str += sprintf(str, "\n");
429
430 return str - buf;
431}
432
433static ssize_t
434efivar_attr_read(struct efivar_entry *entry, char *buf)
435{
436 struct efi_variable *var = &entry->var;
437 char *str = buf;
438 efi_status_t status;
439
440 if (!entry || !buf)
441 return -EINVAL;
442
Mike Waychison4142ef12011-03-11 17:43:11 -0800443 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (status != EFI_SUCCESS)
445 return -EIO;
446
Khalid Aziz70839092012-09-10 12:52:42 -0600447 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600449 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600451 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600453 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
454 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
455 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
456 str += sprintf(str,
457 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
458 if (var->Attributes &
459 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
460 str += sprintf(str,
461 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
462 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
463 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return str - buf;
465}
466
467static ssize_t
468efivar_size_read(struct efivar_entry *entry, char *buf)
469{
470 struct efi_variable *var = &entry->var;
471 char *str = buf;
472 efi_status_t status;
473
474 if (!entry || !buf)
475 return -EINVAL;
476
Mike Waychison4142ef12011-03-11 17:43:11 -0800477 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (status != EFI_SUCCESS)
479 return -EIO;
480
481 str += sprintf(str, "0x%lx\n", var->DataSize);
482 return str - buf;
483}
484
485static ssize_t
486efivar_data_read(struct efivar_entry *entry, char *buf)
487{
488 struct efi_variable *var = &entry->var;
489 efi_status_t status;
490
491 if (!entry || !buf)
492 return -EINVAL;
493
Mike Waychison4142ef12011-03-11 17:43:11 -0800494 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (status != EFI_SUCCESS)
496 return -EIO;
497
498 memcpy(buf, var->Data, var->DataSize);
499 return var->DataSize;
500}
501/*
502 * We allow each variable to be edited via rewriting the
503 * entire efi variable structure.
504 */
505static ssize_t
506efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
507{
508 struct efi_variable *new_var, *var = &entry->var;
Mike Waychison4142ef12011-03-11 17:43:11 -0800509 struct efivars *efivars = entry->efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 efi_status_t status = EFI_NOT_FOUND;
511
512 if (count != sizeof(struct efi_variable))
513 return -EINVAL;
514
515 new_var = (struct efi_variable *)buf;
516 /*
517 * If only updating the variable data, then the name
518 * and guid should remain the same
519 */
520 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
521 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
522 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
523 return -EINVAL;
524 }
525
526 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
527 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
528 return -EINVAL;
529 }
530
Matthew Garrettfec6c202012-04-30 16:11:30 -0400531 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
532 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
533 printk(KERN_ERR "efivars: Malformed variable content\n");
534 return -EINVAL;
535 }
536
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800537 spin_lock_irq(&efivars->lock);
Matthew Garrett68d92982013-03-02 19:40:17 -0500538
539 status = check_var_size_locked(efivars, new_var->Attributes,
Matthew Garrett0635eb82013-04-15 13:09:45 -0700540 new_var->DataSize + ucs2_strsize(new_var->VariableName, 1024));
Matthew Garrett68d92982013-03-02 19:40:17 -0500541
542 if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
543 status = efivars->ops->set_variable(new_var->VariableName,
544 &new_var->VendorGuid,
545 new_var->Attributes,
546 new_var->DataSize,
547 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800549 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if (status != EFI_SUCCESS) {
552 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
553 status);
554 return -EIO;
555 }
556
557 memcpy(&entry->var, new_var, count);
558 return count;
559}
560
561static ssize_t
562efivar_show_raw(struct efivar_entry *entry, char *buf)
563{
564 struct efi_variable *var = &entry->var;
565 efi_status_t status;
566
567 if (!entry || !buf)
568 return 0;
569
Mike Waychison4142ef12011-03-11 17:43:11 -0800570 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (status != EFI_SUCCESS)
572 return -EIO;
573
574 memcpy(buf, var, sizeof(*var));
575 return sizeof(*var);
576}
577
578/*
579 * Generic read/write functions that call the specific functions of
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200580 * the attributes...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
582static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
583 char *buf)
584{
585 struct efivar_entry *var = to_efivar_entry(kobj);
586 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500587 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 if (!capable(CAP_SYS_ADMIN))
590 return -EACCES;
591
592 if (efivar_attr->show) {
593 ret = efivar_attr->show(var, buf);
594 }
595 return ret;
596}
597
598static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
599 const char *buf, size_t count)
600{
601 struct efivar_entry *var = to_efivar_entry(kobj);
602 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500603 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (!capable(CAP_SYS_ADMIN))
606 return -EACCES;
607
608 if (efivar_attr->store)
609 ret = efivar_attr->store(var, buf, count);
610
611 return ret;
612}
613
Emese Revfy52cf25d2010-01-19 02:58:23 +0100614static const struct sysfs_ops efivar_attr_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 .show = efivar_attr_show,
616 .store = efivar_attr_store,
617};
618
619static void efivar_release(struct kobject *kobj)
620{
621 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 kfree(var);
623}
624
625static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
626static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
627static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
628static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
629static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
630
631static struct attribute *def_attrs[] = {
632 &efivar_attr_guid.attr,
633 &efivar_attr_size.attr,
634 &efivar_attr_attributes.attr,
635 &efivar_attr_data.attr,
636 &efivar_attr_raw_var.attr,
637 NULL,
638};
639
Greg Kroah-Hartmane89a4112007-10-11 10:47:49 -0600640static struct kobj_type efivar_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 .release = efivar_release,
642 .sysfs_ops = &efivar_attr_ops,
643 .default_attrs = def_attrs,
644};
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646static inline void
647efivar_unregister(struct efivar_entry *var)
648{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800649 kobject_put(&var->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Matthew Garrett5d9db882012-10-05 13:54:56 +0800652static int efivarfs_file_open(struct inode *inode, struct file *file)
653{
654 file->private_data = inode->i_private;
655 return 0;
656}
657
Matt Fleming7253eab2012-10-16 15:58:07 +0100658static int efi_status_to_err(efi_status_t status)
659{
660 int err;
661
662 switch (status) {
663 case EFI_INVALID_PARAMETER:
664 err = -EINVAL;
665 break;
666 case EFI_OUT_OF_RESOURCES:
667 err = -ENOSPC;
668 break;
669 case EFI_DEVICE_ERROR:
670 err = -EIO;
671 break;
672 case EFI_WRITE_PROTECTED:
673 err = -EROFS;
674 break;
675 case EFI_SECURITY_VIOLATION:
676 err = -EACCES;
677 break;
678 case EFI_NOT_FOUND:
Matt Fleming1fa7e692013-01-16 13:47:05 +0000679 err = -EIO;
Matt Fleming7253eab2012-10-16 15:58:07 +0100680 break;
681 default:
682 err = -EINVAL;
683 }
684
685 return err;
686}
687
Matthew Garrett5d9db882012-10-05 13:54:56 +0800688static ssize_t efivarfs_file_write(struct file *file,
689 const char __user *userbuf, size_t count, loff_t *ppos)
690{
691 struct efivar_entry *var = file->private_data;
692 struct efivars *efivars;
693 efi_status_t status;
694 void *data;
695 u32 attributes;
696 struct inode *inode = file->f_mapping->host;
Matt Fleming07b1c5b2012-10-23 12:35:43 +0100697 unsigned long datasize = count - sizeof(attributes);
Matthew Garrett68d92982013-03-02 19:40:17 -0500698 unsigned long newdatasize, varsize;
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100699 ssize_t bytes = 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800700
701 if (count < sizeof(attributes))
702 return -EINVAL;
703
Matt Fleming89d16662012-11-09 21:02:56 +0000704 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
705 return -EFAULT;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800706
Matt Fleming89d16662012-11-09 21:02:56 +0000707 if (attributes & ~(EFI_VARIABLE_MASK))
708 return -EINVAL;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800709
710 efivars = var->efivars;
711
Matt Fleming89d16662012-11-09 21:02:56 +0000712 /*
713 * Ensure that the user can't allocate arbitrarily large
714 * amounts of memory. Pick a default size of 64K if
715 * QueryVariableInfo() isn't supported by the firmware.
716 */
Matt Fleming89d16662012-11-09 21:02:56 +0000717
Matthew Garrett0635eb82013-04-15 13:09:45 -0700718 varsize = datasize + ucs2_strsize(var->var.VariableName, 1024);
Matthew Garrett68d92982013-03-02 19:40:17 -0500719 status = check_var_size(efivars, attributes, varsize);
Matt Fleming89d16662012-11-09 21:02:56 +0000720
721 if (status != EFI_SUCCESS) {
722 if (status != EFI_UNSUPPORTED)
723 return efi_status_to_err(status);
724
Matthew Garrett68d92982013-03-02 19:40:17 -0500725 if (datasize > 65536)
726 return -ENOSPC;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800727 }
728
Matt Fleming89d16662012-11-09 21:02:56 +0000729 data = kmalloc(datasize, GFP_KERNEL);
730 if (!data)
731 return -ENOMEM;
732
Matthew Garrett5d9db882012-10-05 13:54:56 +0800733 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100734 bytes = -EFAULT;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800735 goto out;
736 }
737
738 if (validate_var(&var->var, data, datasize) == false) {
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100739 bytes = -EINVAL;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800740 goto out;
741 }
742
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800743 /*
744 * The lock here protects the get_variable call, the conditional
745 * set_variable call, and removal of the variable from the efivars
746 * list (in the case of an authenticated delete).
747 */
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800748 spin_lock_irq(&efivars->lock);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800749
Matthew Garrett68d92982013-03-02 19:40:17 -0500750 /*
751 * Ensure that the available space hasn't shrunk below the safe level
752 */
753
754 status = check_var_size_locked(efivars, attributes, varsize);
755
756 if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED) {
757 spin_unlock_irq(&efivars->lock);
758 kfree(data);
759
760 return efi_status_to_err(status);
761 }
762
Matthew Garrett5d9db882012-10-05 13:54:56 +0800763 status = efivars->ops->set_variable(var->var.VariableName,
764 &var->var.VendorGuid,
765 attributes, datasize,
766 data);
767
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800768 if (status != EFI_SUCCESS) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800769 spin_unlock_irq(&efivars->lock);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800770 kfree(data);
771
Matt Fleming7253eab2012-10-16 15:58:07 +0100772 return efi_status_to_err(status);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800773 }
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800774
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100775 bytes = count;
776
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800777 /*
778 * Writing to the variable may have caused a change in size (which
779 * could either be an append or an overwrite), or the variable to be
780 * deleted. Perform a GetVariable() so we can tell what actually
781 * happened.
782 */
783 newdatasize = 0;
784 status = efivars->ops->get_variable(var->var.VariableName,
785 &var->var.VendorGuid,
786 NULL, &newdatasize,
787 NULL);
788
789 if (status == EFI_BUFFER_TOO_SMALL) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800790 spin_unlock_irq(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800791 mutex_lock(&inode->i_mutex);
792 i_size_write(inode, newdatasize + sizeof(attributes));
793 mutex_unlock(&inode->i_mutex);
794
795 } else if (status == EFI_NOT_FOUND) {
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800796 list_del(&var->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800797 spin_unlock_irq(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800798 efivar_unregister(var);
799 drop_nlink(inode);
Matt Fleming791eb562013-01-16 21:55:36 +0000800 d_delete(file->f_dentry);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800801 dput(file->f_dentry);
802
803 } else {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800804 spin_unlock_irq(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800805 pr_warn("efivarfs: inconsistent EFI variable implementation? "
806 "status = %lx\n", status);
807 }
808
Matthew Garrett5d9db882012-10-05 13:54:56 +0800809out:
810 kfree(data);
811
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100812 return bytes;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800813}
814
815static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
816 size_t count, loff_t *ppos)
817{
818 struct efivar_entry *var = file->private_data;
819 struct efivars *efivars = var->efivars;
820 efi_status_t status;
821 unsigned long datasize = 0;
822 u32 attributes;
823 void *data;
Andy Whitcroftd142df02012-10-11 11:32:17 +0100824 ssize_t size = 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800825
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800826 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800827 status = efivars->ops->get_variable(var->var.VariableName,
828 &var->var.VendorGuid,
829 &attributes, &datasize, NULL);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800830 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800831
832 if (status != EFI_BUFFER_TOO_SMALL)
Matt Fleming7253eab2012-10-16 15:58:07 +0100833 return efi_status_to_err(status);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800834
Matt Flemingd2923842012-10-22 15:23:29 +0100835 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800836
837 if (!data)
Matt Fleming7253eab2012-10-16 15:58:07 +0100838 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800839
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800840 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800841 status = efivars->ops->get_variable(var->var.VariableName,
842 &var->var.VendorGuid,
843 &attributes, &datasize,
Matt Flemingd2923842012-10-22 15:23:29 +0100844 (data + sizeof(attributes)));
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800845 spin_unlock_irq(&efivars->lock);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800846
Matt Fleming7253eab2012-10-16 15:58:07 +0100847 if (status != EFI_SUCCESS) {
848 size = efi_status_to_err(status);
Andy Whitcroftd142df02012-10-11 11:32:17 +0100849 goto out_free;
Matt Fleming7253eab2012-10-16 15:58:07 +0100850 }
Matthew Garrett5d9db882012-10-05 13:54:56 +0800851
Matt Flemingd2923842012-10-22 15:23:29 +0100852 memcpy(data, &attributes, sizeof(attributes));
Matthew Garrett5d9db882012-10-05 13:54:56 +0800853 size = simple_read_from_buffer(userbuf, count, ppos,
Matt Flemingd2923842012-10-22 15:23:29 +0100854 data, datasize + sizeof(attributes));
Andy Whitcroftd142df02012-10-11 11:32:17 +0100855out_free:
Matthew Garrett5d9db882012-10-05 13:54:56 +0800856 kfree(data);
857
858 return size;
859}
860
861static void efivarfs_evict_inode(struct inode *inode)
862{
863 clear_inode(inode);
864}
865
866static const struct super_operations efivarfs_ops = {
867 .statfs = simple_statfs,
868 .drop_inode = generic_delete_inode,
869 .evict_inode = efivarfs_evict_inode,
870 .show_options = generic_show_options,
871};
872
873static struct super_block *efivarfs_sb;
874
875static const struct inode_operations efivarfs_dir_inode_operations;
876
877static const struct file_operations efivarfs_file_operations = {
878 .open = efivarfs_file_open,
879 .read = efivarfs_file_read,
880 .write = efivarfs_file_write,
881 .llseek = no_llseek,
882};
883
884static struct inode *efivarfs_get_inode(struct super_block *sb,
885 const struct inode *dir, int mode, dev_t dev)
886{
887 struct inode *inode = new_inode(sb);
888
889 if (inode) {
890 inode->i_ino = get_next_ino();
Matthew Garrett5d9db882012-10-05 13:54:56 +0800891 inode->i_mode = mode;
892 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
893 switch (mode & S_IFMT) {
894 case S_IFREG:
895 inode->i_fop = &efivarfs_file_operations;
896 break;
897 case S_IFDIR:
898 inode->i_op = &efivarfs_dir_inode_operations;
899 inode->i_fop = &simple_dir_operations;
900 inc_nlink(inode);
901 break;
902 }
903 }
904 return inode;
905}
906
Matt Fleming47f531e2013-01-31 19:02:03 +0000907/*
908 * Return true if 'str' is a valid efivarfs filename of the form,
909 *
910 * VariableName-12345678-1234-1234-1234-1234567891bc
911 */
912static bool efivarfs_valid_name(const char *str, int len)
913{
914 static const char dashes[GUID_LEN] = {
915 [8] = 1, [13] = 1, [18] = 1, [23] = 1
916 };
917 const char *s = str + len - GUID_LEN;
918 int i;
919
920 /*
921 * We need a GUID, plus at least one letter for the variable name,
922 * plus the '-' separator
923 */
924 if (len < GUID_LEN + 2)
925 return false;
926
Matt Fleming123abd72013-03-05 07:40:16 +0000927 /* GUID must be preceded by a '-' */
928 if (*(s - 1) != '-')
Matt Fleming47f531e2013-01-31 19:02:03 +0000929 return false;
930
931 /*
932 * Validate that 's' is of the correct format, e.g.
933 *
934 * 12345678-1234-1234-1234-123456789abc
935 */
936 for (i = 0; i < GUID_LEN; i++) {
937 if (dashes[i]) {
938 if (*s++ != '-')
939 return false;
940 } else {
941 if (!isxdigit(*s++))
942 return false;
943 }
944 }
945
946 return true;
947}
948
Matthew Garrett5d9db882012-10-05 13:54:56 +0800949static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
950{
951 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
952 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
953 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
954 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
955 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
956 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
957 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
958 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
959 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
960 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
961 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
962 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
963 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
964 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
965 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
966 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
967}
968
969static int efivarfs_create(struct inode *dir, struct dentry *dentry,
970 umode_t mode, bool excl)
971{
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100972 struct inode *inode;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800973 struct efivars *efivars = &__efivars;
974 struct efivar_entry *var;
975 int namelen, i = 0, err = 0;
976
Matt Fleming47f531e2013-01-31 19:02:03 +0000977 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
Matthew Garrett5d9db882012-10-05 13:54:56 +0800978 return -EINVAL;
979
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100980 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800981 if (!inode)
Matt Flemingaeeaa8d2012-10-23 12:41:03 +0100982 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800983
984 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Andy Whitcroft45a937a2012-10-11 11:32:18 +0100985 if (!var) {
986 err = -ENOMEM;
987 goto out;
988 }
Matthew Garrett5d9db882012-10-05 13:54:56 +0800989
Jeremy Kerr310ad752012-10-19 15:16:45 +0800990 /* length of the variable name itself: remove GUID and separator */
991 namelen = dentry->d_name.len - GUID_LEN - 1;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800992
993 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
994 &var->var.VendorGuid);
995
996 for (i = 0; i < namelen; i++)
997 var->var.VariableName[i] = dentry->d_name.name[i];
998
999 var->var.VariableName[i] = '\0';
1000
1001 inode->i_private = var;
1002 var->efivars = efivars;
1003 var->kobj.kset = efivars->kset;
1004
1005 err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
1006 dentry->d_name.name);
1007 if (err)
1008 goto out;
1009
1010 kobject_uevent(&var->kobj, KOBJ_ADD);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001011 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001012 list_add(&var->list, &efivars->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001013 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001014 d_instantiate(dentry, inode);
1015 dget(dentry);
1016out:
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001017 if (err) {
Matthew Garrett5d9db882012-10-05 13:54:56 +08001018 kfree(var);
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001019 iput(inode);
1020 }
Matthew Garrett5d9db882012-10-05 13:54:56 +08001021 return err;
1022}
1023
1024static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
1025{
1026 struct efivar_entry *var = dentry->d_inode->i_private;
1027 struct efivars *efivars = var->efivars;
1028 efi_status_t status;
1029
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001030 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001031
1032 status = efivars->ops->set_variable(var->var.VariableName,
1033 &var->var.VendorGuid,
1034 0, 0, NULL);
1035
1036 if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
1037 list_del(&var->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001038 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001039 efivar_unregister(var);
Lingzhu Xiangde5fe952013-01-05 13:59:52 +08001040 drop_nlink(dentry->d_inode);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001041 dput(dentry);
1042 return 0;
1043 }
1044
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001045 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001046 return -EINVAL;
1047};
1048
Matt Flemingda27a242013-02-01 11:02:28 +00001049/*
1050 * Compare two efivarfs file names.
1051 *
1052 * An efivarfs filename is composed of two parts,
1053 *
1054 * 1. A case-sensitive variable name
1055 * 2. A case-insensitive GUID
1056 *
1057 * So we need to perform a case-sensitive match on part 1 and a
1058 * case-insensitive match on part 2.
1059 */
1060static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
1061 const struct dentry *dentry, const struct inode *inode,
1062 unsigned int len, const char *str,
1063 const struct qstr *name)
1064{
1065 int guid = len - GUID_LEN;
1066
1067 if (name->len != len)
1068 return 1;
1069
1070 /* Case-sensitive compare for the variable name */
1071 if (memcmp(str, name->name, guid))
1072 return 1;
1073
1074 /* Case-insensitive compare for the GUID */
1075 return strncasecmp(name->name + guid, str + guid, GUID_LEN);
1076}
1077
1078static int efivarfs_d_hash(const struct dentry *dentry,
1079 const struct inode *inode, struct qstr *qstr)
1080{
1081 unsigned long hash = init_name_hash();
1082 const unsigned char *s = qstr->name;
1083 unsigned int len = qstr->len;
1084
1085 if (!efivarfs_valid_name(s, len))
1086 return -EINVAL;
1087
1088 while (len-- > GUID_LEN)
1089 hash = partial_name_hash(*s++, hash);
1090
1091 /* GUID is case-insensitive. */
1092 while (len--)
1093 hash = partial_name_hash(tolower(*s++), hash);
1094
1095 qstr->hash = end_name_hash(hash);
1096 return 0;
1097}
1098
1099/*
1100 * Retaining negative dentries for an in-memory filesystem just wastes
1101 * memory and lookup time: arrange for them to be deleted immediately.
1102 */
1103static int efivarfs_delete_dentry(const struct dentry *dentry)
1104{
1105 return 1;
1106}
1107
1108static struct dentry_operations efivarfs_d_ops = {
1109 .d_compare = efivarfs_d_compare,
1110 .d_hash = efivarfs_d_hash,
1111 .d_delete = efivarfs_delete_dentry,
1112};
1113
1114static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
1115{
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001116 struct dentry *d;
Matt Flemingda27a242013-02-01 11:02:28 +00001117 struct qstr q;
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001118 int err;
Matt Flemingda27a242013-02-01 11:02:28 +00001119
1120 q.name = name;
1121 q.len = strlen(name);
1122
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001123 err = efivarfs_d_hash(NULL, NULL, &q);
1124 if (err)
1125 return ERR_PTR(err);
Matt Flemingda27a242013-02-01 11:02:28 +00001126
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001127 d = d_alloc(parent, &q);
1128 if (d)
1129 return d;
1130
1131 return ERR_PTR(-ENOMEM);
Matt Flemingda27a242013-02-01 11:02:28 +00001132}
1133
Matt Fleminge83af1f2012-11-15 20:03:16 +00001134static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
Matthew Garrett5d9db882012-10-05 13:54:56 +08001135{
1136 struct inode *inode = NULL;
1137 struct dentry *root;
1138 struct efivar_entry *entry, *n;
1139 struct efivars *efivars = &__efivars;
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001140 char *name;
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001141 int err = -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001142
1143 efivarfs_sb = sb;
1144
1145 sb->s_maxbytes = MAX_LFS_FILESIZE;
1146 sb->s_blocksize = PAGE_CACHE_SIZE;
1147 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
Matt Fleming91716322012-10-22 15:51:45 +01001148 sb->s_magic = EFIVARFS_MAGIC;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001149 sb->s_op = &efivarfs_ops;
Matt Flemingda27a242013-02-01 11:02:28 +00001150 sb->s_d_op = &efivarfs_d_ops;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001151 sb->s_time_gran = 1;
1152
1153 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +01001154 if (!inode)
1155 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001156 inode->i_op = &efivarfs_dir_inode_operations;
1157
1158 root = d_make_root(inode);
1159 sb->s_root = root;
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +01001160 if (!root)
1161 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001162
1163 list_for_each_entry_safe(entry, n, &efivars->list, list) {
Matthew Garrett5d9db882012-10-05 13:54:56 +08001164 struct dentry *dentry, *root = efivarfs_sb->s_root;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001165 unsigned long size = 0;
1166 int len, i;
1167
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001168 inode = NULL;
1169
Matthew Garrett0635eb82013-04-15 13:09:45 -07001170 len = ucs2_strlen(entry->var.VariableName);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001171
Jeremy Kerr310ad752012-10-19 15:16:45 +08001172 /* name, plus '-', plus GUID, plus NUL*/
1173 name = kmalloc(len + 1 + GUID_LEN + 1, GFP_ATOMIC);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001174 if (!name)
1175 goto fail;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001176
1177 for (i = 0; i < len; i++)
1178 name[i] = entry->var.VariableName[i] & 0xFF;
1179
1180 name[len] = '-';
1181
1182 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
1183
Jeremy Kerr310ad752012-10-19 15:16:45 +08001184 name[len+GUID_LEN+1] = '\0';
Matthew Garrett5d9db882012-10-05 13:54:56 +08001185
1186 inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
1187 S_IFREG | 0644, 0);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001188 if (!inode)
1189 goto fail_name;
1190
Matt Flemingda27a242013-02-01 11:02:28 +00001191 dentry = efivarfs_alloc_dentry(root, name);
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001192 if (IS_ERR(dentry)) {
1193 err = PTR_ERR(dentry);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001194 goto fail_inode;
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001195 }
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001196
Andy Whitcroftc0359db2012-10-11 11:32:20 +01001197 /* copied by the above to local storage in the dentry. */
1198 kfree(name);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001199
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001200 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001201 efivars->ops->get_variable(entry->var.VariableName,
1202 &entry->var.VendorGuid,
1203 &entry->var.Attributes,
1204 &size,
1205 NULL);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001206 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001207
1208 mutex_lock(&inode->i_mutex);
1209 inode->i_private = entry;
Matt Fleming94a193f2013-01-11 13:30:46 +00001210 i_size_write(inode, size + sizeof(entry->var.Attributes));
Matthew Garrett5d9db882012-10-05 13:54:56 +08001211 mutex_unlock(&inode->i_mutex);
1212 d_add(dentry, inode);
1213 }
1214
1215 return 0;
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001216
1217fail_inode:
1218 iput(inode);
1219fail_name:
1220 kfree(name);
1221fail:
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001222 return err;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001223}
1224
1225static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
1226 int flags, const char *dev_name, void *data)
1227{
1228 return mount_single(fs_type, flags, data, efivarfs_fill_super);
1229}
1230
1231static void efivarfs_kill_sb(struct super_block *sb)
1232{
1233 kill_litter_super(sb);
1234 efivarfs_sb = NULL;
1235}
1236
1237static struct file_system_type efivarfs_type = {
1238 .name = "efivarfs",
1239 .mount = efivarfs_mount,
1240 .kill_sb = efivarfs_kill_sb,
1241};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001242MODULE_ALIAS_FS("efivarfs");
Matthew Garrett5d9db882012-10-05 13:54:56 +08001243
Matt Flemingda27a242013-02-01 11:02:28 +00001244/*
1245 * Handle negative dentry.
1246 */
1247static struct dentry *efivarfs_lookup(struct inode *dir, struct dentry *dentry,
1248 unsigned int flags)
1249{
1250 if (dentry->d_name.len > NAME_MAX)
1251 return ERR_PTR(-ENAMETOOLONG);
1252 d_add(dentry, NULL);
1253 return NULL;
1254}
1255
Matthew Garrett5d9db882012-10-05 13:54:56 +08001256static const struct inode_operations efivarfs_dir_inode_operations = {
Matt Flemingda27a242013-02-01 11:02:28 +00001257 .lookup = efivarfs_lookup,
Matthew Garrett5d9db882012-10-05 13:54:56 +08001258 .unlink = efivarfs_unlink,
1259 .create = efivarfs_create,
1260};
1261
Seth Forsheeed9dc8c2013-03-07 11:40:17 -06001262#ifdef CONFIG_EFI_VARS_PSTORE
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001263
1264static int efi_pstore_open(struct pstore_info *psi)
1265{
1266 struct efivars *efivars = psi->data;
1267
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001268 spin_lock_irq(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001269 efivars->walk_entry = list_first_entry(&efivars->list,
1270 struct efivar_entry, list);
1271 return 0;
1272}
1273
1274static int efi_pstore_close(struct pstore_info *psi)
1275{
1276 struct efivars *efivars = psi->data;
1277
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001278 spin_unlock_irq(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001279 return 0;
1280}
1281
1282static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001283 int *count, struct timespec *timespec,
Kees Cookf6f82852011-11-17 12:58:07 -08001284 char **buf, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001285{
1286 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1287 struct efivars *efivars = psi->data;
1288 char name[DUMP_NAME_LEN];
1289 int i;
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001290 int cnt;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001291 unsigned int part, size;
1292 unsigned long time;
1293
1294 while (&efivars->walk_entry->list != &efivars->list) {
1295 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
1296 vendor)) {
1297 for (i = 0; i < DUMP_NAME_LEN; i++) {
1298 name[i] = efivars->walk_entry->var.VariableName[i];
1299 }
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001300 if (sscanf(name, "dump-type%u-%u-%d-%lu",
1301 type, &part, &cnt, &time) == 4) {
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001302 *id = part;
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001303 *count = cnt;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001304 timespec->tv_sec = time;
1305 timespec->tv_nsec = 0;
Seiji Aguchi0f7de852012-11-14 20:28:50 +00001306 } else if (sscanf(name, "dump-type%u-%u-%lu",
1307 type, &part, &time) == 3) {
1308 /*
1309 * Check if an old format,
1310 * which doesn't support holding
1311 * multiple logs, remains.
1312 */
1313 *id = part;
1314 *count = 0;
1315 timespec->tv_sec = time;
1316 timespec->tv_nsec = 0;
1317 } else {
1318 efivars->walk_entry = list_entry(
1319 efivars->walk_entry->list.next,
1320 struct efivar_entry, list);
1321 continue;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001322 }
Seiji Aguchi0f7de852012-11-14 20:28:50 +00001323
1324 get_var_data_locked(efivars, &efivars->walk_entry->var);
1325 size = efivars->walk_entry->var.DataSize;
1326 *buf = kmalloc(size, GFP_KERNEL);
1327 if (*buf == NULL)
1328 return -ENOMEM;
1329 memcpy(*buf, efivars->walk_entry->var.Data,
1330 size);
1331 efivars->walk_entry = list_entry(
1332 efivars->walk_entry->list.next,
1333 struct efivar_entry, list);
1334 return size;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001335 }
1336 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1337 struct efivar_entry, list);
1338 }
1339 return 0;
1340}
1341
Kees Cook3d6d8d22011-11-17 13:13:29 -08001342static int efi_pstore_write(enum pstore_type_id type,
1343 enum kmsg_dump_reason reason, u64 *id,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001344 unsigned int part, int count, size_t size,
1345 struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001346{
1347 char name[DUMP_NAME_LEN];
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001348 efi_char16_t efi_name[DUMP_NAME_LEN];
1349 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1350 struct efivars *efivars = psi->data;
Chen Gongb238b8f2011-10-12 09:17:24 -07001351 int i, ret = 0;
Seiji Aguchid80a3612012-11-14 20:25:37 +00001352 efi_status_t status = EFI_NOT_FOUND;
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001353 unsigned long flags;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001354
Seiji Aguchie59310a2013-01-11 18:10:05 +00001355 if (pstore_cannot_block_path(reason)) {
1356 /*
1357 * If the lock is taken by another cpu in non-blocking path,
1358 * this driver returns without entering firmware to avoid
1359 * hanging up.
1360 */
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001361 if (!spin_trylock_irqsave(&efivars->lock, flags))
Seiji Aguchie59310a2013-01-11 18:10:05 +00001362 return -EBUSY;
1363 } else
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001364 spin_lock_irqsave(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001365
Seiji Aguchid80a3612012-11-14 20:25:37 +00001366 /*
1367 * Check if there is a space enough to log.
1368 * size: a size of logging data
1369 * DUMP_NAME_LEN * 2: a maximum size of variable name
1370 */
Matthew Garrett68d92982013-03-02 19:40:17 -05001371
1372 status = check_var_size_locked(efivars, PSTORE_EFI_ATTRIBUTES,
1373 size + DUMP_NAME_LEN * 2);
1374
1375 if (status) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001376 spin_unlock_irqrestore(&efivars->lock, flags);
Seiji Aguchid80a3612012-11-14 20:25:37 +00001377 *id = part;
1378 return -ENOSPC;
1379 }
1380
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001381 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
Seiji Aguchi96480d92012-11-14 20:26:46 +00001382 get_seconds());
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001383
1384 for (i = 0; i < DUMP_NAME_LEN; i++)
1385 efi_name[i] = name[i];
1386
Mike Waychison7644c162011-07-21 16:58:00 -04001387 efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001388 size, psi->buf);
1389
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001390 spin_unlock_irqrestore(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001391
Matt Fleminge9713182013-03-07 11:59:14 +00001392 if (reason == KMSG_DUMP_OOPS && efivar_wq_enabled)
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001393 schedule_work(&efivar_work);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001394
Chen Gongb238b8f2011-10-12 09:17:24 -07001395 *id = part;
1396 return ret;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001397};
1398
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001399static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001400 struct timespec time, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001401{
Seiji Aguchia9efd392012-11-14 20:27:28 +00001402 char name[DUMP_NAME_LEN];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001403 efi_char16_t efi_name[DUMP_NAME_LEN];
Seiji Aguchif94ec0c2012-11-14 20:29:21 +00001404 char name_old[DUMP_NAME_LEN];
1405 efi_char16_t efi_name_old[DUMP_NAME_LEN];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001406 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1407 struct efivars *efivars = psi->data;
1408 struct efivar_entry *entry, *found = NULL;
1409 int i;
1410
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001411 sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001412 time.tv_sec);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001413
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001414 spin_lock_irq(&efivars->lock);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001415
1416 for (i = 0; i < DUMP_NAME_LEN; i++)
Seiji Aguchia9efd392012-11-14 20:27:28 +00001417 efi_name[i] = name[i];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001418
1419 /*
Seiji Aguchia9efd392012-11-14 20:27:28 +00001420 * Clean up an entry with the same name
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001421 */
1422
1423 list_for_each_entry(entry, &efivars->list, list) {
1424 get_var_data_locked(efivars, &entry->var);
1425
1426 if (efi_guidcmp(entry->var.VendorGuid, vendor))
1427 continue;
Matthew Garrett0635eb82013-04-15 13:09:45 -07001428 if (ucs2_strncmp(entry->var.VariableName, efi_name,
1429 ucs2_strlen(efi_name))) {
Seiji Aguchif94ec0c2012-11-14 20:29:21 +00001430 /*
1431 * Check if an old format,
1432 * which doesn't support holding
1433 * multiple logs, remains.
1434 */
1435 sprintf(name_old, "dump-type%u-%u-%lu", type,
1436 (unsigned int)id, time.tv_sec);
1437
1438 for (i = 0; i < DUMP_NAME_LEN; i++)
1439 efi_name_old[i] = name_old[i];
1440
Matthew Garrett0635eb82013-04-15 13:09:45 -07001441 if (ucs2_strncmp(entry->var.VariableName, efi_name_old,
1442 ucs2_strlen(efi_name_old)))
Seiji Aguchif94ec0c2012-11-14 20:29:21 +00001443 continue;
1444 }
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001445
1446 /* found */
1447 found = entry;
1448 efivars->ops->set_variable(entry->var.VariableName,
1449 &entry->var.VendorGuid,
1450 PSTORE_EFI_ATTRIBUTES,
1451 0, NULL);
Seiji Aguchia9efd392012-11-14 20:27:28 +00001452 break;
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001453 }
1454
1455 if (found)
1456 list_del(&found->list);
1457
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001458 spin_unlock_irq(&efivars->lock);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001459
1460 if (found)
1461 efivar_unregister(found);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001462
1463 return 0;
1464}
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001465
1466static struct pstore_info efi_pstore_info = {
1467 .owner = THIS_MODULE,
1468 .name = "efi",
1469 .open = efi_pstore_open,
1470 .close = efi_pstore_close,
1471 .read = efi_pstore_read,
1472 .write = efi_pstore_write,
1473 .erase = efi_pstore_erase,
1474};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Seth Forsheeed9dc8c2013-03-07 11:40:17 -06001476static void efivar_pstore_register(struct efivars *efivars)
1477{
1478 efivars->efi_pstore_info = efi_pstore_info;
1479 efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
1480 if (efivars->efi_pstore_info.buf) {
1481 efivars->efi_pstore_info.bufsize = 1024;
1482 efivars->efi_pstore_info.data = efivars;
1483 spin_lock_init(&efivars->efi_pstore_info.buf_lock);
1484 pstore_register(&efivars->efi_pstore_info);
1485 }
1486}
1487#else
1488static void efivar_pstore_register(struct efivars *efivars)
1489{
1490 return;
1491}
1492#endif
1493
Chris Wright2c3c8be2010-05-12 18:28:57 -07001494static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001495 struct bin_attribute *bin_attr,
1496 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 struct efi_variable *new_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001499 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001500 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 efi_status_t status = EFI_NOT_FOUND;
1503 int found = 0;
1504
1505 if (!capable(CAP_SYS_ADMIN))
1506 return -EACCES;
1507
Matthew Garrettfec6c202012-04-30 16:11:30 -04001508 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
1509 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
1510 printk(KERN_ERR "efivars: Malformed variable content\n");
1511 return -EINVAL;
1512 }
1513
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001514 spin_lock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 /*
1517 * Does this variable already exist?
1518 */
Mike Waychison29422692011-03-11 17:43:00 -08001519 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Matthew Garrett0635eb82013-04-15 13:09:45 -07001520 strsize1 = ucs2_strsize(search_efivar->var.VariableName, 1024);
1521 strsize2 = ucs2_strsize(new_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (strsize1 == strsize2 &&
1523 !memcmp(&(search_efivar->var.VariableName),
1524 new_var->VariableName, strsize1) &&
1525 !efi_guidcmp(search_efivar->var.VendorGuid,
1526 new_var->VendorGuid)) {
1527 found = 1;
1528 break;
1529 }
1530 }
1531 if (found) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001532 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return -EINVAL;
1534 }
1535
Matthew Garrett68d92982013-03-02 19:40:17 -05001536 status = check_var_size_locked(efivars, new_var->Attributes,
Matthew Garrett0635eb82013-04-15 13:09:45 -07001537 new_var->DataSize + ucs2_strsize(new_var->VariableName, 1024));
Matthew Garrett68d92982013-03-02 19:40:17 -05001538
1539 if (status && status != EFI_UNSUPPORTED) {
1540 spin_unlock_irq(&efivars->lock);
1541 return efi_status_to_err(status);
1542 }
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /* now *really* create the variable via EFI */
Mike Waychison32958142011-03-11 17:43:21 -08001545 status = efivars->ops->set_variable(new_var->VariableName,
1546 &new_var->VendorGuid,
1547 new_var->Attributes,
1548 new_var->DataSize,
1549 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 if (status != EFI_SUCCESS) {
1552 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1553 status);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001554 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return -EIO;
1556 }
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001557 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 /* Create the entry in sysfs. Locking is not required here */
Mike Waychison4142ef12011-03-11 17:43:11 -08001560 status = efivar_create_sysfs_entry(efivars,
Matthew Garrett0635eb82013-04-15 13:09:45 -07001561 ucs2_strsize(new_var->VariableName,
Mike Waychisona2940902011-07-21 16:57:57 -04001562 1024),
Mike Waychison4142ef12011-03-11 17:43:11 -08001563 new_var->VariableName,
1564 &new_var->VendorGuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (status) {
1566 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
1567 }
1568 return count;
1569}
1570
Chris Wright2c3c8be2010-05-12 18:28:57 -07001571static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001572 struct bin_attribute *bin_attr,
1573 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 struct efi_variable *del_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001576 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001577 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 efi_status_t status = EFI_NOT_FOUND;
1580 int found = 0;
1581
1582 if (!capable(CAP_SYS_ADMIN))
1583 return -EACCES;
1584
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001585 spin_lock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 /*
1588 * Does this variable already exist?
1589 */
Mike Waychison29422692011-03-11 17:43:00 -08001590 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Matthew Garrett0635eb82013-04-15 13:09:45 -07001591 strsize1 = ucs2_strsize(search_efivar->var.VariableName, 1024);
1592 strsize2 = ucs2_strsize(del_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (strsize1 == strsize2 &&
1594 !memcmp(&(search_efivar->var.VariableName),
1595 del_var->VariableName, strsize1) &&
1596 !efi_guidcmp(search_efivar->var.VendorGuid,
1597 del_var->VendorGuid)) {
1598 found = 1;
1599 break;
1600 }
1601 }
1602 if (!found) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001603 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return -EINVAL;
1605 }
1606 /* force the Attributes/DataSize to 0 to ensure deletion */
1607 del_var->Attributes = 0;
1608 del_var->DataSize = 0;
1609
Mike Waychison32958142011-03-11 17:43:21 -08001610 status = efivars->ops->set_variable(del_var->VariableName,
1611 &del_var->VendorGuid,
1612 del_var->Attributes,
1613 del_var->DataSize,
1614 del_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 if (status != EFI_SUCCESS) {
1617 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1618 status);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001619 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return -EIO;
1621 }
Matt Domsch496a0fc2007-01-26 00:57:18 -08001622 list_del(&search_efivar->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 /* We need to release this lock before unregistering. */
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001624 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 efivar_unregister(search_efivar);
1626
1627 /* It's dead Jim.... */
1628 return count;
1629}
1630
Matt Flemingf4642462013-04-26 10:10:55 +01001631static bool variable_is_present(struct efivars *efivars,
1632 efi_char16_t *variable_name,
1633 efi_guid_t *vendor)
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001634{
1635 struct efivar_entry *entry, *n;
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001636 unsigned long strsize1, strsize2;
1637 bool found = false;
1638
Matthew Garrett0635eb82013-04-15 13:09:45 -07001639 strsize1 = ucs2_strsize(variable_name, 1024);
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001640 list_for_each_entry_safe(entry, n, &efivars->list, list) {
Matthew Garrett0635eb82013-04-15 13:09:45 -07001641 strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001642 if (strsize1 == strsize2 &&
1643 !memcmp(variable_name, &(entry->var.VariableName),
1644 strsize2) &&
1645 !efi_guidcmp(entry->var.VendorGuid,
1646 *vendor)) {
1647 found = true;
1648 break;
1649 }
1650 }
1651 return found;
1652}
1653
Matt Flemingec50bd32013-03-01 14:49:12 +00001654/*
1655 * Returns the size of variable_name, in bytes, including the
1656 * terminating NULL character, or variable_name_size if no NULL
1657 * character is found among the first variable_name_size bytes.
1658 */
1659static unsigned long var_name_strnsize(efi_char16_t *variable_name,
1660 unsigned long variable_name_size)
1661{
1662 unsigned long len;
1663 efi_char16_t c;
1664
1665 /*
1666 * The variable name is, by definition, a NULL-terminated
1667 * string, so make absolutely sure that variable_name_size is
1668 * the value we expect it to be. If not, return the real size.
1669 */
1670 for (len = 2; len <= variable_name_size; len += sizeof(c)) {
1671 c = variable_name[(len / sizeof(c)) - 1];
1672 if (!c)
1673 break;
1674 }
1675
1676 return min(len, variable_name_size);
1677}
1678
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001679static void efivar_update_sysfs_entries(struct work_struct *work)
1680{
1681 struct efivars *efivars = &__efivars;
1682 efi_guid_t vendor;
1683 efi_char16_t *variable_name;
1684 unsigned long variable_name_size = 1024;
1685 efi_status_t status = EFI_NOT_FOUND;
1686 bool found;
1687
1688 /* Add new sysfs entries */
1689 while (1) {
1690 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
1691 if (!variable_name) {
1692 pr_err("efivars: Memory allocation failed.\n");
1693 return;
1694 }
1695
1696 spin_lock_irq(&efivars->lock);
1697 found = false;
1698 while (1) {
1699 variable_name_size = 1024;
1700 status = efivars->ops->get_next_variable(
1701 &variable_name_size,
1702 variable_name,
1703 &vendor);
1704 if (status != EFI_SUCCESS) {
1705 break;
1706 } else {
Matt Flemingf4642462013-04-26 10:10:55 +01001707 if (!variable_is_present(efivars,
1708 variable_name, &vendor)) {
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001709 found = true;
1710 break;
1711 }
1712 }
1713 }
1714 spin_unlock_irq(&efivars->lock);
1715
1716 if (!found) {
1717 kfree(variable_name);
1718 break;
Matt Flemingec50bd32013-03-01 14:49:12 +00001719 } else {
1720 variable_name_size = var_name_strnsize(variable_name,
1721 variable_name_size);
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001722 efivar_create_sysfs_entry(efivars,
1723 variable_name_size,
1724 variable_name, &vendor);
Matt Flemingec50bd32013-03-01 14:49:12 +00001725 }
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001726 }
1727}
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729/*
1730 * Let's not leave out systab information that snuck into
1731 * the efivars driver
1732 */
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001733static ssize_t systab_show(struct kobject *kobj,
1734 struct kobj_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 char *str = buf;
1737
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001738 if (!kobj || !buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return -EINVAL;
1740
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -08001741 if (efi.mps != EFI_INVALID_TABLE_ADDR)
1742 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
1743 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
1744 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
1745 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
1746 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
1747 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
1748 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
1749 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
1750 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
1751 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
1752 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
1753 if (efi.uga != EFI_INVALID_TABLE_ADDR)
1754 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 return str - buf;
1757}
1758
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001759static struct kobj_attribute efi_attr_systab =
1760 __ATTR(systab, 0400, systab_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001762static struct attribute *efi_subsys_attrs[] = {
1763 &efi_attr_systab.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 NULL, /* maybe more in the future? */
1765};
1766
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001767static struct attribute_group efi_subsys_attr_group = {
1768 .attrs = efi_subsys_attrs,
1769};
1770
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08001771static struct kobject *efi_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773/*
1774 * efivar_create_sysfs_entry()
1775 * Requires:
1776 * variable_name_size = number of bytes required to hold
1777 * variable_name (not counting the NULL
1778 * character at the end.
Mike Waychison29422692011-03-11 17:43:00 -08001779 * efivars->lock is not held on entry or exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 * Returns 1 on failure, 0 on success
1781 */
1782static int
Mike Waychison4142ef12011-03-11 17:43:11 -08001783efivar_create_sysfs_entry(struct efivars *efivars,
1784 unsigned long variable_name_size,
1785 efi_char16_t *variable_name,
1786 efi_guid_t *vendor_guid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Jeremy Kerr310ad752012-10-19 15:16:45 +08001788 int i, short_name_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 char *short_name;
1790 struct efivar_entry *new_efivar;
1791
Jeremy Kerr310ad752012-10-19 15:16:45 +08001792 /*
1793 * Length of the variable bytes in ASCII, plus the '-' separator,
1794 * plus the GUID, plus trailing NUL
1795 */
1796 short_name_size = variable_name_size / sizeof(efi_char16_t)
1797 + 1 + GUID_LEN + 1;
1798
1799 short_name = kzalloc(short_name_size, GFP_KERNEL);
Deepak Saxena9c215382005-11-07 01:01:24 -08001800 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 if (!short_name || !new_efivar) {
Jesper Juhl0933ad92005-06-25 14:59:15 -07001803 kfree(short_name);
1804 kfree(new_efivar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return 1;
1806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Mike Waychison4142ef12011-03-11 17:43:11 -08001808 new_efivar->efivars = efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 memcpy(new_efivar->var.VariableName, variable_name,
1810 variable_name_size);
1811 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
1812
1813 /* Convert Unicode to normal chars (assume top bits are 0),
1814 ala UTF-8 */
1815 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
1816 short_name[i] = variable_name[i] & 0xFF;
1817 }
1818 /* This is ugly, but necessary to separate one vendor's
1819 private variables from another's. */
1820
1821 *(short_name + strlen(short_name)) = '-';
1822 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
1823
Mike Waychison29422692011-03-11 17:43:00 -08001824 new_efivar->kobj.kset = efivars->kset;
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001825 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
1826 "%s", short_name);
Jeff Garzik69b21862006-10-11 01:22:23 -07001827 if (i) {
1828 kfree(short_name);
1829 kfree(new_efivar);
1830 return 1;
1831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001833 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
Jesper Juhl0933ad92005-06-25 14:59:15 -07001834 kfree(short_name);
1835 short_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001837 spin_lock_irq(&efivars->lock);
Mike Waychison29422692011-03-11 17:43:00 -08001838 list_add(&new_efivar->list, &efivars->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001839 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 return 0;
1842}
Mike Waychisond502fbb2011-03-11 17:43:06 -08001843
1844static int
1845create_efivars_bin_attributes(struct efivars *efivars)
1846{
1847 struct bin_attribute *attr;
1848 int error;
1849
1850 /* new_var */
1851 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1852 if (!attr)
1853 return -ENOMEM;
1854
1855 attr->attr.name = "new_var";
1856 attr->attr.mode = 0200;
1857 attr->write = efivar_create;
1858 attr->private = efivars;
1859 efivars->new_var = attr;
1860
1861 /* del_var */
1862 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1863 if (!attr) {
1864 error = -ENOMEM;
1865 goto out_free;
1866 }
1867 attr->attr.name = "del_var";
1868 attr->attr.mode = 0200;
1869 attr->write = efivar_delete;
1870 attr->private = efivars;
1871 efivars->del_var = attr;
1872
1873 sysfs_bin_attr_init(efivars->new_var);
1874 sysfs_bin_attr_init(efivars->del_var);
1875
1876 /* Register */
1877 error = sysfs_create_bin_file(&efivars->kset->kobj,
1878 efivars->new_var);
1879 if (error) {
1880 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
1881 " due to error %d\n", error);
1882 goto out_free;
1883 }
1884 error = sysfs_create_bin_file(&efivars->kset->kobj,
1885 efivars->del_var);
1886 if (error) {
1887 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
1888 " due to error %d\n", error);
1889 sysfs_remove_bin_file(&efivars->kset->kobj,
1890 efivars->new_var);
1891 goto out_free;
1892 }
1893
1894 return 0;
1895out_free:
Dan Carpenter051d51b2011-03-18 10:12:14 +03001896 kfree(efivars->del_var);
1897 efivars->del_var = NULL;
Mike Waychisond502fbb2011-03-11 17:43:06 -08001898 kfree(efivars->new_var);
1899 efivars->new_var = NULL;
1900 return error;
1901}
1902
Mike Waychison4fc756b2011-03-11 17:43:27 -08001903void unregister_efivars(struct efivars *efivars)
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001904{
1905 struct efivar_entry *entry, *n;
Mike Waychison4142ef12011-03-11 17:43:11 -08001906
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001907 list_for_each_entry_safe(entry, n, &efivars->list, list) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001908 spin_lock_irq(&efivars->lock);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001909 list_del(&entry->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001910 spin_unlock_irq(&efivars->lock);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001911 efivar_unregister(entry);
1912 }
1913 if (efivars->new_var)
1914 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
1915 if (efivars->del_var)
1916 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
1917 kfree(efivars->new_var);
1918 kfree(efivars->del_var);
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08001919 kobject_put(efivars->kobject);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001920 kset_unregister(efivars->kset);
1921}
Mike Waychison4fc756b2011-03-11 17:43:27 -08001922EXPORT_SYMBOL_GPL(unregister_efivars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Matt Fleminge9713182013-03-07 11:59:14 +00001924/*
1925 * Print a warning when duplicate EFI variables are encountered and
1926 * disable the sysfs workqueue since the firmware is buggy.
1927 */
1928static void dup_variable_bug(efi_char16_t *s16, efi_guid_t *vendor_guid,
1929 unsigned long len16)
1930{
1931 size_t i, len8 = len16 / sizeof(efi_char16_t);
1932 char *s8;
1933
1934 /*
1935 * Disable the workqueue since the algorithm it uses for
1936 * detecting new variables won't work with this buggy
1937 * implementation of GetNextVariableName().
1938 */
1939 efivar_wq_enabled = false;
1940
1941 s8 = kzalloc(len8, GFP_KERNEL);
1942 if (!s8)
1943 return;
1944
1945 for (i = 0; i < len8; i++)
1946 s8[i] = s16[i];
1947
1948 printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
1949 s8, vendor_guid);
1950 kfree(s8);
1951}
1952
Mike Waychison4fc756b2011-03-11 17:43:27 -08001953int register_efivars(struct efivars *efivars,
1954 const struct efivar_operations *ops,
1955 struct kobject *parent_kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 efi_status_t status = EFI_NOT_FOUND;
1958 efi_guid_t vendor_guid;
1959 efi_char16_t *variable_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 unsigned long variable_name_size = 1024;
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001961 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Deepak Saxena9c215382005-11-07 01:01:24 -08001963 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (!variable_name) {
1965 printk(KERN_ERR "efivars: Memory allocation failed.\n");
1966 return -ENOMEM;
1967 }
1968
Mike Waychison29422692011-03-11 17:43:00 -08001969 spin_lock_init(&efivars->lock);
1970 INIT_LIST_HEAD(&efivars->list);
Mike Waychison32958142011-03-11 17:43:21 -08001971 efivars->ops = ops;
Mike Waychison29422692011-03-11 17:43:00 -08001972
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001973 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
Mike Waychison29422692011-03-11 17:43:00 -08001974 if (!efivars->kset) {
Greg Kroah-Hartman66ac8312007-11-02 13:20:40 -07001975 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
1976 error = -ENOMEM;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08001980 efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
1981 if (!efivars->kobject) {
1982 pr_err("efivars: Subsystem registration failed.\n");
1983 error = -ENOMEM;
1984 kset_unregister(efivars->kset);
1985 goto out;
1986 }
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /*
1989 * Per EFI spec, the maximum storage allocated for both
1990 * the variable name and variable data is 1024 bytes.
1991 */
1992
1993 do {
1994 variable_name_size = 1024;
1995
Mike Waychison32958142011-03-11 17:43:21 -08001996 status = ops->get_next_variable(&variable_name_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 variable_name,
1998 &vendor_guid);
1999 switch (status) {
2000 case EFI_SUCCESS:
Matt Flemingec50bd32013-03-01 14:49:12 +00002001 variable_name_size = var_name_strnsize(variable_name,
2002 variable_name_size);
Matt Fleminge9713182013-03-07 11:59:14 +00002003
2004 /*
2005 * Some firmware implementations return the
2006 * same variable name on multiple calls to
2007 * get_next_variable(). Terminate the loop
2008 * immediately as there is no guarantee that
2009 * we'll ever see a different variable name,
2010 * and may end up looping here forever.
2011 */
Matt Flemingf4642462013-04-26 10:10:55 +01002012 if (variable_is_present(efivars, variable_name,
2013 &vendor_guid)) {
Matt Fleminge9713182013-03-07 11:59:14 +00002014 dup_variable_bug(variable_name, &vendor_guid,
2015 variable_name_size);
2016 status = EFI_NOT_FOUND;
2017 break;
2018 }
2019
Mike Waychison4142ef12011-03-11 17:43:11 -08002020 efivar_create_sysfs_entry(efivars,
2021 variable_name_size,
2022 variable_name,
2023 &vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 break;
2025 case EFI_NOT_FOUND:
2026 break;
2027 default:
2028 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
2029 status);
2030 status = EFI_NOT_FOUND;
2031 break;
2032 }
2033 } while (status != EFI_NOT_FOUND);
2034
Mike Waychisond502fbb2011-03-11 17:43:06 -08002035 error = create_efivars_bin_attributes(efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002036 if (error)
2037 unregister_efivars(efivars);
2038
Seth Forsheeec0971b2013-03-11 16:17:50 -05002039 if (!efivars_pstore_disable)
2040 efivar_pstore_register(efivars);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04002041
Matthew Garrett5d9db882012-10-05 13:54:56 +08002042 register_filesystem(&efivarfs_type);
2043
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002044out:
2045 kfree(variable_name);
2046
2047 return error;
2048}
Mike Waychison4fc756b2011-03-11 17:43:27 -08002049EXPORT_SYMBOL_GPL(register_efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002050
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002051/*
2052 * For now we register the efi subsystem with the firmware subsystem
2053 * and the vars subsystem with the efi subsystem. In the future, it
2054 * might make sense to split off the efi subsystem into its own
2055 * driver, but for now only efivars will register with it, so just
2056 * include it here.
2057 */
2058
2059static int __init
2060efivars_init(void)
2061{
2062 int error = 0;
2063
2064 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
2065 EFIVARS_DATE);
2066
Matt Fleming83e68182012-11-14 09:42:35 +00002067 if (!efi_enabled(EFI_RUNTIME_SERVICES))
Mike Waychison4fc756b2011-03-11 17:43:27 -08002068 return 0;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002069
2070 /* For now we'll register the efi directory at /sys/firmware/efi */
2071 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
2072 if (!efi_kobj) {
2073 printk(KERN_ERR "efivars: Firmware registration failed.\n");
2074 return -ENOMEM;
2075 }
2076
Mike Waychison32958142011-03-11 17:43:21 -08002077 ops.get_variable = efi.get_variable;
2078 ops.set_variable = efi.set_variable;
2079 ops.get_next_variable = efi.get_next_variable;
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00002080 ops.query_variable_store = efi_query_variable_store;
Matt Fleming89d16662012-11-09 21:02:56 +00002081
Mike Waychison32958142011-03-11 17:43:21 -08002082 error = register_efivars(&__efivars, &ops, efi_kobj);
Dan Carpenter3116aab2011-03-18 10:12:38 +03002083 if (error)
2084 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 /* Don't forget the systab entry */
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08002087 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002088 if (error) {
2089 printk(KERN_ERR
2090 "efivars: Sysfs attribute export failed with error %d.\n",
2091 error);
Dan Carpenter3116aab2011-03-18 10:12:38 +03002092 goto err_unregister;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Dan Carpenter3116aab2011-03-18 10:12:38 +03002095 return 0;
2096
2097err_unregister:
2098 unregister_efivars(&__efivars);
2099err_put:
2100 kobject_put(efi_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return error;
2102}
2103
2104static void __exit
2105efivars_exit(void)
2106{
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08002107 cancel_work_sync(&efivar_work);
2108
Matt Fleming83e68182012-11-14 09:42:35 +00002109 if (efi_enabled(EFI_RUNTIME_SERVICES)) {
Randy Dunlapaabb6e12011-05-06 13:27:41 -07002110 unregister_efivars(&__efivars);
2111 kobject_put(efi_kobj);
2112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
2115module_init(efivars_init);
2116module_exit(efivars_exit);
2117