blob: bf15d81d74e18af739b5ea1ed0155e1b8786c268 [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{
Matthew Garrett68d92982013-03-02 19:40:17 -0500439 const struct efivar_operations *fops = efivars->ops;
440
Matt Fleminga6e4d5a2013-03-25 09:14:30 +0000441 if (!efivars->ops->query_variable_store)
Matthew Garrett68d92982013-03-02 19:40:17 -0500442 return EFI_UNSUPPORTED;
443
Matt Fleminga6e4d5a2013-03-25 09:14:30 +0000444 return fops->query_variable_store(attributes, size);
Matthew Garrett68d92982013-03-02 19:40:17 -0500445}
446
447
448static efi_status_t
449check_var_size(struct efivars *efivars, u32 attributes, unsigned long size)
450{
451 efi_status_t status;
452 unsigned long flags;
453
454 spin_lock_irqsave(&efivars->lock, flags);
455 status = check_var_size_locked(efivars, attributes, size);
456 spin_unlock_irqrestore(&efivars->lock, flags);
457
458 return status;
459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static ssize_t
462efivar_guid_read(struct efivar_entry *entry, char *buf)
463{
464 struct efi_variable *var = &entry->var;
465 char *str = buf;
466
467 if (!entry || !buf)
468 return 0;
469
470 efi_guid_unparse(&var->VendorGuid, str);
471 str += strlen(str);
472 str += sprintf(str, "\n");
473
474 return str - buf;
475}
476
477static ssize_t
478efivar_attr_read(struct efivar_entry *entry, char *buf)
479{
480 struct efi_variable *var = &entry->var;
481 char *str = buf;
482 efi_status_t status;
483
484 if (!entry || !buf)
485 return -EINVAL;
486
Mike Waychison4142ef12011-03-11 17:43:11 -0800487 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (status != EFI_SUCCESS)
489 return -EIO;
490
Khalid Aziz70839092012-09-10 12:52:42 -0600491 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600493 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600495 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
Khalid Aziz70839092012-09-10 12:52:42 -0600497 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
498 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
499 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
500 str += sprintf(str,
501 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
502 if (var->Attributes &
503 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
504 str += sprintf(str,
505 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
506 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
507 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return str - buf;
509}
510
511static ssize_t
512efivar_size_read(struct efivar_entry *entry, char *buf)
513{
514 struct efi_variable *var = &entry->var;
515 char *str = buf;
516 efi_status_t status;
517
518 if (!entry || !buf)
519 return -EINVAL;
520
Mike Waychison4142ef12011-03-11 17:43:11 -0800521 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (status != EFI_SUCCESS)
523 return -EIO;
524
525 str += sprintf(str, "0x%lx\n", var->DataSize);
526 return str - buf;
527}
528
529static ssize_t
530efivar_data_read(struct efivar_entry *entry, char *buf)
531{
532 struct efi_variable *var = &entry->var;
533 efi_status_t status;
534
535 if (!entry || !buf)
536 return -EINVAL;
537
Mike Waychison4142ef12011-03-11 17:43:11 -0800538 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (status != EFI_SUCCESS)
540 return -EIO;
541
542 memcpy(buf, var->Data, var->DataSize);
543 return var->DataSize;
544}
545/*
546 * We allow each variable to be edited via rewriting the
547 * entire efi variable structure.
548 */
549static ssize_t
550efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
551{
552 struct efi_variable *new_var, *var = &entry->var;
Mike Waychison4142ef12011-03-11 17:43:11 -0800553 struct efivars *efivars = entry->efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 efi_status_t status = EFI_NOT_FOUND;
555
556 if (count != sizeof(struct efi_variable))
557 return -EINVAL;
558
559 new_var = (struct efi_variable *)buf;
560 /*
561 * If only updating the variable data, then the name
562 * and guid should remain the same
563 */
564 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
565 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
566 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
567 return -EINVAL;
568 }
569
570 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
571 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
572 return -EINVAL;
573 }
574
Matthew Garrettfec6c202012-04-30 16:11:30 -0400575 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
576 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
577 printk(KERN_ERR "efivars: Malformed variable content\n");
578 return -EINVAL;
579 }
580
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800581 spin_lock_irq(&efivars->lock);
Matthew Garrett68d92982013-03-02 19:40:17 -0500582
583 status = check_var_size_locked(efivars, new_var->Attributes,
584 new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
585
586 if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
587 status = efivars->ops->set_variable(new_var->VariableName,
588 &new_var->VendorGuid,
589 new_var->Attributes,
590 new_var->DataSize,
591 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800593 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if (status != EFI_SUCCESS) {
596 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
597 status);
598 return -EIO;
599 }
600
601 memcpy(&entry->var, new_var, count);
602 return count;
603}
604
605static ssize_t
606efivar_show_raw(struct efivar_entry *entry, char *buf)
607{
608 struct efi_variable *var = &entry->var;
609 efi_status_t status;
610
611 if (!entry || !buf)
612 return 0;
613
Mike Waychison4142ef12011-03-11 17:43:11 -0800614 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (status != EFI_SUCCESS)
616 return -EIO;
617
618 memcpy(buf, var, sizeof(*var));
619 return sizeof(*var);
620}
621
622/*
623 * Generic read/write functions that call the specific functions of
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200624 * the attributes...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
626static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
627 char *buf)
628{
629 struct efivar_entry *var = to_efivar_entry(kobj);
630 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500631 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 if (!capable(CAP_SYS_ADMIN))
634 return -EACCES;
635
636 if (efivar_attr->show) {
637 ret = efivar_attr->show(var, buf);
638 }
639 return ret;
640}
641
642static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
643 const char *buf, size_t count)
644{
645 struct efivar_entry *var = to_efivar_entry(kobj);
646 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500647 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 if (!capable(CAP_SYS_ADMIN))
650 return -EACCES;
651
652 if (efivar_attr->store)
653 ret = efivar_attr->store(var, buf, count);
654
655 return ret;
656}
657
Emese Revfy52cf25d2010-01-19 02:58:23 +0100658static const struct sysfs_ops efivar_attr_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 .show = efivar_attr_show,
660 .store = efivar_attr_store,
661};
662
663static void efivar_release(struct kobject *kobj)
664{
665 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 kfree(var);
667}
668
669static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
670static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
671static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
672static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
673static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
674
675static struct attribute *def_attrs[] = {
676 &efivar_attr_guid.attr,
677 &efivar_attr_size.attr,
678 &efivar_attr_attributes.attr,
679 &efivar_attr_data.attr,
680 &efivar_attr_raw_var.attr,
681 NULL,
682};
683
Greg Kroah-Hartmane89a4112007-10-11 10:47:49 -0600684static struct kobj_type efivar_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 .release = efivar_release,
686 .sysfs_ops = &efivar_attr_ops,
687 .default_attrs = def_attrs,
688};
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static inline void
691efivar_unregister(struct efivar_entry *var)
692{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800693 kobject_put(&var->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Matthew Garrett5d9db882012-10-05 13:54:56 +0800696static int efivarfs_file_open(struct inode *inode, struct file *file)
697{
698 file->private_data = inode->i_private;
699 return 0;
700}
701
Matt Fleming7253eab2012-10-16 15:58:07 +0100702static int efi_status_to_err(efi_status_t status)
703{
704 int err;
705
706 switch (status) {
707 case EFI_INVALID_PARAMETER:
708 err = -EINVAL;
709 break;
710 case EFI_OUT_OF_RESOURCES:
711 err = -ENOSPC;
712 break;
713 case EFI_DEVICE_ERROR:
714 err = -EIO;
715 break;
716 case EFI_WRITE_PROTECTED:
717 err = -EROFS;
718 break;
719 case EFI_SECURITY_VIOLATION:
720 err = -EACCES;
721 break;
722 case EFI_NOT_FOUND:
Matt Fleming1fa7e692013-01-16 13:47:05 +0000723 err = -EIO;
Matt Fleming7253eab2012-10-16 15:58:07 +0100724 break;
725 default:
726 err = -EINVAL;
727 }
728
729 return err;
730}
731
Matthew Garrett5d9db882012-10-05 13:54:56 +0800732static ssize_t efivarfs_file_write(struct file *file,
733 const char __user *userbuf, size_t count, loff_t *ppos)
734{
735 struct efivar_entry *var = file->private_data;
736 struct efivars *efivars;
737 efi_status_t status;
738 void *data;
739 u32 attributes;
740 struct inode *inode = file->f_mapping->host;
Matt Fleming07b1c5b2012-10-23 12:35:43 +0100741 unsigned long datasize = count - sizeof(attributes);
Matthew Garrett68d92982013-03-02 19:40:17 -0500742 unsigned long newdatasize, varsize;
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100743 ssize_t bytes = 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800744
745 if (count < sizeof(attributes))
746 return -EINVAL;
747
Matt Fleming89d16662012-11-09 21:02:56 +0000748 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
749 return -EFAULT;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800750
Matt Fleming89d16662012-11-09 21:02:56 +0000751 if (attributes & ~(EFI_VARIABLE_MASK))
752 return -EINVAL;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800753
754 efivars = var->efivars;
755
Matt Fleming89d16662012-11-09 21:02:56 +0000756 /*
757 * Ensure that the user can't allocate arbitrarily large
758 * amounts of memory. Pick a default size of 64K if
759 * QueryVariableInfo() isn't supported by the firmware.
760 */
Matt Fleming89d16662012-11-09 21:02:56 +0000761
Matthew Garrett68d92982013-03-02 19:40:17 -0500762 varsize = datasize + utf16_strsize(var->var.VariableName, 1024);
763 status = check_var_size(efivars, attributes, varsize);
Matt Fleming89d16662012-11-09 21:02:56 +0000764
765 if (status != EFI_SUCCESS) {
766 if (status != EFI_UNSUPPORTED)
767 return efi_status_to_err(status);
768
Matthew Garrett68d92982013-03-02 19:40:17 -0500769 if (datasize > 65536)
770 return -ENOSPC;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800771 }
772
Matt Fleming89d16662012-11-09 21:02:56 +0000773 data = kmalloc(datasize, GFP_KERNEL);
774 if (!data)
775 return -ENOMEM;
776
Matthew Garrett5d9db882012-10-05 13:54:56 +0800777 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100778 bytes = -EFAULT;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800779 goto out;
780 }
781
782 if (validate_var(&var->var, data, datasize) == false) {
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100783 bytes = -EINVAL;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800784 goto out;
785 }
786
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800787 /*
788 * The lock here protects the get_variable call, the conditional
789 * set_variable call, and removal of the variable from the efivars
790 * list (in the case of an authenticated delete).
791 */
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800792 spin_lock_irq(&efivars->lock);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800793
Matthew Garrett68d92982013-03-02 19:40:17 -0500794 /*
795 * Ensure that the available space hasn't shrunk below the safe level
796 */
797
798 status = check_var_size_locked(efivars, attributes, varsize);
799
800 if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED) {
801 spin_unlock_irq(&efivars->lock);
802 kfree(data);
803
804 return efi_status_to_err(status);
805 }
806
Matthew Garrett5d9db882012-10-05 13:54:56 +0800807 status = efivars->ops->set_variable(var->var.VariableName,
808 &var->var.VendorGuid,
809 attributes, datasize,
810 data);
811
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800812 if (status != EFI_SUCCESS) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800813 spin_unlock_irq(&efivars->lock);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800814 kfree(data);
815
Matt Fleming7253eab2012-10-16 15:58:07 +0100816 return efi_status_to_err(status);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800817 }
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800818
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100819 bytes = count;
820
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800821 /*
822 * Writing to the variable may have caused a change in size (which
823 * could either be an append or an overwrite), or the variable to be
824 * deleted. Perform a GetVariable() so we can tell what actually
825 * happened.
826 */
827 newdatasize = 0;
828 status = efivars->ops->get_variable(var->var.VariableName,
829 &var->var.VendorGuid,
830 NULL, &newdatasize,
831 NULL);
832
833 if (status == EFI_BUFFER_TOO_SMALL) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800834 spin_unlock_irq(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800835 mutex_lock(&inode->i_mutex);
836 i_size_write(inode, newdatasize + sizeof(attributes));
837 mutex_unlock(&inode->i_mutex);
838
839 } else if (status == EFI_NOT_FOUND) {
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800840 list_del(&var->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800841 spin_unlock_irq(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800842 efivar_unregister(var);
843 drop_nlink(inode);
Matt Fleming791eb562013-01-16 21:55:36 +0000844 d_delete(file->f_dentry);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800845 dput(file->f_dentry);
846
847 } else {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800848 spin_unlock_irq(&efivars->lock);
Jeremy Kerr0c542ed2012-10-05 13:54:56 +0800849 pr_warn("efivarfs: inconsistent EFI variable implementation? "
850 "status = %lx\n", status);
851 }
852
Matthew Garrett5d9db882012-10-05 13:54:56 +0800853out:
854 kfree(data);
855
Matt Flemingcfcf2f12012-10-26 12:18:53 +0100856 return bytes;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800857}
858
859static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
860 size_t count, loff_t *ppos)
861{
862 struct efivar_entry *var = file->private_data;
863 struct efivars *efivars = var->efivars;
864 efi_status_t status;
865 unsigned long datasize = 0;
866 u32 attributes;
867 void *data;
Andy Whitcroftd142df02012-10-11 11:32:17 +0100868 ssize_t size = 0;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800869
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800870 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800871 status = efivars->ops->get_variable(var->var.VariableName,
872 &var->var.VendorGuid,
873 &attributes, &datasize, NULL);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800874 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800875
876 if (status != EFI_BUFFER_TOO_SMALL)
Matt Fleming7253eab2012-10-16 15:58:07 +0100877 return efi_status_to_err(status);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800878
Matt Flemingd2923842012-10-22 15:23:29 +0100879 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800880
881 if (!data)
Matt Fleming7253eab2012-10-16 15:58:07 +0100882 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +0800883
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800884 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +0800885 status = efivars->ops->get_variable(var->var.VariableName,
886 &var->var.VendorGuid,
887 &attributes, &datasize,
Matt Flemingd2923842012-10-22 15:23:29 +0100888 (data + sizeof(attributes)));
Seiji Aguchi81fa4e52013-02-12 12:59:07 -0800889 spin_unlock_irq(&efivars->lock);
Jeremy Kerrf5f6a602012-10-11 21:19:11 +0800890
Matt Fleming7253eab2012-10-16 15:58:07 +0100891 if (status != EFI_SUCCESS) {
892 size = efi_status_to_err(status);
Andy Whitcroftd142df02012-10-11 11:32:17 +0100893 goto out_free;
Matt Fleming7253eab2012-10-16 15:58:07 +0100894 }
Matthew Garrett5d9db882012-10-05 13:54:56 +0800895
Matt Flemingd2923842012-10-22 15:23:29 +0100896 memcpy(data, &attributes, sizeof(attributes));
Matthew Garrett5d9db882012-10-05 13:54:56 +0800897 size = simple_read_from_buffer(userbuf, count, ppos,
Matt Flemingd2923842012-10-22 15:23:29 +0100898 data, datasize + sizeof(attributes));
Andy Whitcroftd142df02012-10-11 11:32:17 +0100899out_free:
Matthew Garrett5d9db882012-10-05 13:54:56 +0800900 kfree(data);
901
902 return size;
903}
904
905static void efivarfs_evict_inode(struct inode *inode)
906{
907 clear_inode(inode);
908}
909
910static const struct super_operations efivarfs_ops = {
911 .statfs = simple_statfs,
912 .drop_inode = generic_delete_inode,
913 .evict_inode = efivarfs_evict_inode,
914 .show_options = generic_show_options,
915};
916
917static struct super_block *efivarfs_sb;
918
919static const struct inode_operations efivarfs_dir_inode_operations;
920
921static const struct file_operations efivarfs_file_operations = {
922 .open = efivarfs_file_open,
923 .read = efivarfs_file_read,
924 .write = efivarfs_file_write,
925 .llseek = no_llseek,
926};
927
928static struct inode *efivarfs_get_inode(struct super_block *sb,
929 const struct inode *dir, int mode, dev_t dev)
930{
931 struct inode *inode = new_inode(sb);
932
933 if (inode) {
934 inode->i_ino = get_next_ino();
Matthew Garrett5d9db882012-10-05 13:54:56 +0800935 inode->i_mode = mode;
936 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
937 switch (mode & S_IFMT) {
938 case S_IFREG:
939 inode->i_fop = &efivarfs_file_operations;
940 break;
941 case S_IFDIR:
942 inode->i_op = &efivarfs_dir_inode_operations;
943 inode->i_fop = &simple_dir_operations;
944 inc_nlink(inode);
945 break;
946 }
947 }
948 return inode;
949}
950
Matt Fleming47f531e2013-01-31 19:02:03 +0000951/*
952 * Return true if 'str' is a valid efivarfs filename of the form,
953 *
954 * VariableName-12345678-1234-1234-1234-1234567891bc
955 */
956static bool efivarfs_valid_name(const char *str, int len)
957{
958 static const char dashes[GUID_LEN] = {
959 [8] = 1, [13] = 1, [18] = 1, [23] = 1
960 };
961 const char *s = str + len - GUID_LEN;
962 int i;
963
964 /*
965 * We need a GUID, plus at least one letter for the variable name,
966 * plus the '-' separator
967 */
968 if (len < GUID_LEN + 2)
969 return false;
970
Matt Fleming123abd72013-03-05 07:40:16 +0000971 /* GUID must be preceded by a '-' */
972 if (*(s - 1) != '-')
Matt Fleming47f531e2013-01-31 19:02:03 +0000973 return false;
974
975 /*
976 * Validate that 's' is of the correct format, e.g.
977 *
978 * 12345678-1234-1234-1234-123456789abc
979 */
980 for (i = 0; i < GUID_LEN; i++) {
981 if (dashes[i]) {
982 if (*s++ != '-')
983 return false;
984 } else {
985 if (!isxdigit(*s++))
986 return false;
987 }
988 }
989
990 return true;
991}
992
Matthew Garrett5d9db882012-10-05 13:54:56 +0800993static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
994{
995 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
996 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
997 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
998 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
999 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
1000 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
1001 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
1002 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
1003 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
1004 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
1005 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
1006 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
1007 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
1008 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
1009 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
1010 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
1011}
1012
1013static int efivarfs_create(struct inode *dir, struct dentry *dentry,
1014 umode_t mode, bool excl)
1015{
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001016 struct inode *inode;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001017 struct efivars *efivars = &__efivars;
1018 struct efivar_entry *var;
1019 int namelen, i = 0, err = 0;
1020
Matt Fleming47f531e2013-01-31 19:02:03 +00001021 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
Matthew Garrett5d9db882012-10-05 13:54:56 +08001022 return -EINVAL;
1023
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001024 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001025 if (!inode)
Matt Flemingaeeaa8d2012-10-23 12:41:03 +01001026 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001027
1028 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001029 if (!var) {
1030 err = -ENOMEM;
1031 goto out;
1032 }
Matthew Garrett5d9db882012-10-05 13:54:56 +08001033
Jeremy Kerr310ad752012-10-19 15:16:45 +08001034 /* length of the variable name itself: remove GUID and separator */
1035 namelen = dentry->d_name.len - GUID_LEN - 1;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001036
1037 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
1038 &var->var.VendorGuid);
1039
1040 for (i = 0; i < namelen; i++)
1041 var->var.VariableName[i] = dentry->d_name.name[i];
1042
1043 var->var.VariableName[i] = '\0';
1044
1045 inode->i_private = var;
1046 var->efivars = efivars;
1047 var->kobj.kset = efivars->kset;
1048
1049 err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
1050 dentry->d_name.name);
1051 if (err)
1052 goto out;
1053
1054 kobject_uevent(&var->kobj, KOBJ_ADD);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001055 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001056 list_add(&var->list, &efivars->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001057 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001058 d_instantiate(dentry, inode);
1059 dget(dentry);
1060out:
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001061 if (err) {
Matthew Garrett5d9db882012-10-05 13:54:56 +08001062 kfree(var);
Andy Whitcroft45a937a2012-10-11 11:32:18 +01001063 iput(inode);
1064 }
Matthew Garrett5d9db882012-10-05 13:54:56 +08001065 return err;
1066}
1067
1068static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
1069{
1070 struct efivar_entry *var = dentry->d_inode->i_private;
1071 struct efivars *efivars = var->efivars;
1072 efi_status_t status;
1073
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001074 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001075
1076 status = efivars->ops->set_variable(var->var.VariableName,
1077 &var->var.VendorGuid,
1078 0, 0, NULL);
1079
1080 if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
1081 list_del(&var->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001082 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001083 efivar_unregister(var);
Lingzhu Xiangde5fe952013-01-05 13:59:52 +08001084 drop_nlink(dentry->d_inode);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001085 dput(dentry);
1086 return 0;
1087 }
1088
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001089 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001090 return -EINVAL;
1091};
1092
Matt Flemingda27a242013-02-01 11:02:28 +00001093/*
1094 * Compare two efivarfs file names.
1095 *
1096 * An efivarfs filename is composed of two parts,
1097 *
1098 * 1. A case-sensitive variable name
1099 * 2. A case-insensitive GUID
1100 *
1101 * So we need to perform a case-sensitive match on part 1 and a
1102 * case-insensitive match on part 2.
1103 */
1104static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
1105 const struct dentry *dentry, const struct inode *inode,
1106 unsigned int len, const char *str,
1107 const struct qstr *name)
1108{
1109 int guid = len - GUID_LEN;
1110
1111 if (name->len != len)
1112 return 1;
1113
1114 /* Case-sensitive compare for the variable name */
1115 if (memcmp(str, name->name, guid))
1116 return 1;
1117
1118 /* Case-insensitive compare for the GUID */
1119 return strncasecmp(name->name + guid, str + guid, GUID_LEN);
1120}
1121
1122static int efivarfs_d_hash(const struct dentry *dentry,
1123 const struct inode *inode, struct qstr *qstr)
1124{
1125 unsigned long hash = init_name_hash();
1126 const unsigned char *s = qstr->name;
1127 unsigned int len = qstr->len;
1128
1129 if (!efivarfs_valid_name(s, len))
1130 return -EINVAL;
1131
1132 while (len-- > GUID_LEN)
1133 hash = partial_name_hash(*s++, hash);
1134
1135 /* GUID is case-insensitive. */
1136 while (len--)
1137 hash = partial_name_hash(tolower(*s++), hash);
1138
1139 qstr->hash = end_name_hash(hash);
1140 return 0;
1141}
1142
1143/*
1144 * Retaining negative dentries for an in-memory filesystem just wastes
1145 * memory and lookup time: arrange for them to be deleted immediately.
1146 */
1147static int efivarfs_delete_dentry(const struct dentry *dentry)
1148{
1149 return 1;
1150}
1151
1152static struct dentry_operations efivarfs_d_ops = {
1153 .d_compare = efivarfs_d_compare,
1154 .d_hash = efivarfs_d_hash,
1155 .d_delete = efivarfs_delete_dentry,
1156};
1157
1158static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
1159{
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001160 struct dentry *d;
Matt Flemingda27a242013-02-01 11:02:28 +00001161 struct qstr q;
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001162 int err;
Matt Flemingda27a242013-02-01 11:02:28 +00001163
1164 q.name = name;
1165 q.len = strlen(name);
1166
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001167 err = efivarfs_d_hash(NULL, NULL, &q);
1168 if (err)
1169 return ERR_PTR(err);
Matt Flemingda27a242013-02-01 11:02:28 +00001170
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001171 d = d_alloc(parent, &q);
1172 if (d)
1173 return d;
1174
1175 return ERR_PTR(-ENOMEM);
Matt Flemingda27a242013-02-01 11:02:28 +00001176}
1177
Matt Fleminge83af1f2012-11-15 20:03:16 +00001178static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
Matthew Garrett5d9db882012-10-05 13:54:56 +08001179{
1180 struct inode *inode = NULL;
1181 struct dentry *root;
1182 struct efivar_entry *entry, *n;
1183 struct efivars *efivars = &__efivars;
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001184 char *name;
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001185 int err = -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001186
1187 efivarfs_sb = sb;
1188
1189 sb->s_maxbytes = MAX_LFS_FILESIZE;
1190 sb->s_blocksize = PAGE_CACHE_SIZE;
1191 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
Matt Fleming91716322012-10-22 15:51:45 +01001192 sb->s_magic = EFIVARFS_MAGIC;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001193 sb->s_op = &efivarfs_ops;
Matt Flemingda27a242013-02-01 11:02:28 +00001194 sb->s_d_op = &efivarfs_d_ops;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001195 sb->s_time_gran = 1;
1196
1197 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +01001198 if (!inode)
1199 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001200 inode->i_op = &efivarfs_dir_inode_operations;
1201
1202 root = d_make_root(inode);
1203 sb->s_root = root;
Andy Whitcroft5c9b50a2012-10-11 11:32:19 +01001204 if (!root)
1205 return -ENOMEM;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001206
1207 list_for_each_entry_safe(entry, n, &efivars->list, list) {
Matthew Garrett5d9db882012-10-05 13:54:56 +08001208 struct dentry *dentry, *root = efivarfs_sb->s_root;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001209 unsigned long size = 0;
1210 int len, i;
1211
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001212 inode = NULL;
1213
Matthew Garrett5d9db882012-10-05 13:54:56 +08001214 len = utf16_strlen(entry->var.VariableName);
1215
Jeremy Kerr310ad752012-10-19 15:16:45 +08001216 /* name, plus '-', plus GUID, plus NUL*/
1217 name = kmalloc(len + 1 + GUID_LEN + 1, GFP_ATOMIC);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001218 if (!name)
1219 goto fail;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001220
1221 for (i = 0; i < len; i++)
1222 name[i] = entry->var.VariableName[i] & 0xFF;
1223
1224 name[len] = '-';
1225
1226 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
1227
Jeremy Kerr310ad752012-10-19 15:16:45 +08001228 name[len+GUID_LEN+1] = '\0';
Matthew Garrett5d9db882012-10-05 13:54:56 +08001229
1230 inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
1231 S_IFREG | 0644, 0);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001232 if (!inode)
1233 goto fail_name;
1234
Matt Flemingda27a242013-02-01 11:02:28 +00001235 dentry = efivarfs_alloc_dentry(root, name);
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001236 if (IS_ERR(dentry)) {
1237 err = PTR_ERR(dentry);
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001238 goto fail_inode;
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001239 }
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001240
Andy Whitcroftc0359db2012-10-11 11:32:20 +01001241 /* copied by the above to local storage in the dentry. */
1242 kfree(name);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001243
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001244 spin_lock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001245 efivars->ops->get_variable(entry->var.VariableName,
1246 &entry->var.VendorGuid,
1247 &entry->var.Attributes,
1248 &size,
1249 NULL);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001250 spin_unlock_irq(&efivars->lock);
Matthew Garrett5d9db882012-10-05 13:54:56 +08001251
1252 mutex_lock(&inode->i_mutex);
1253 inode->i_private = entry;
Matt Fleming94a193f2013-01-11 13:30:46 +00001254 i_size_write(inode, size + sizeof(entry->var.Attributes));
Matthew Garrett5d9db882012-10-05 13:54:56 +08001255 mutex_unlock(&inode->i_mutex);
1256 d_add(dentry, inode);
1257 }
1258
1259 return 0;
Andy Whitcroft5ba6e292012-10-11 11:32:21 +01001260
1261fail_inode:
1262 iput(inode);
1263fail_name:
1264 kfree(name);
1265fail:
Matt Flemingfeff5dc2013-03-05 12:46:30 +00001266 return err;
Matthew Garrett5d9db882012-10-05 13:54:56 +08001267}
1268
1269static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
1270 int flags, const char *dev_name, void *data)
1271{
1272 return mount_single(fs_type, flags, data, efivarfs_fill_super);
1273}
1274
1275static void efivarfs_kill_sb(struct super_block *sb)
1276{
1277 kill_litter_super(sb);
1278 efivarfs_sb = NULL;
1279}
1280
1281static struct file_system_type efivarfs_type = {
1282 .name = "efivarfs",
1283 .mount = efivarfs_mount,
1284 .kill_sb = efivarfs_kill_sb,
1285};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001286MODULE_ALIAS_FS("efivarfs");
Matthew Garrett5d9db882012-10-05 13:54:56 +08001287
Matt Flemingda27a242013-02-01 11:02:28 +00001288/*
1289 * Handle negative dentry.
1290 */
1291static struct dentry *efivarfs_lookup(struct inode *dir, struct dentry *dentry,
1292 unsigned int flags)
1293{
1294 if (dentry->d_name.len > NAME_MAX)
1295 return ERR_PTR(-ENAMETOOLONG);
1296 d_add(dentry, NULL);
1297 return NULL;
1298}
1299
Matthew Garrett5d9db882012-10-05 13:54:56 +08001300static const struct inode_operations efivarfs_dir_inode_operations = {
Matt Flemingda27a242013-02-01 11:02:28 +00001301 .lookup = efivarfs_lookup,
Matthew Garrett5d9db882012-10-05 13:54:56 +08001302 .unlink = efivarfs_unlink,
1303 .create = efivarfs_create,
1304};
1305
Seth Forsheeed9dc8c2013-03-07 11:40:17 -06001306#ifdef CONFIG_EFI_VARS_PSTORE
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001307
1308static int efi_pstore_open(struct pstore_info *psi)
1309{
1310 struct efivars *efivars = psi->data;
1311
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001312 spin_lock_irq(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001313 efivars->walk_entry = list_first_entry(&efivars->list,
1314 struct efivar_entry, list);
1315 return 0;
1316}
1317
1318static int efi_pstore_close(struct pstore_info *psi)
1319{
1320 struct efivars *efivars = psi->data;
1321
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001322 spin_unlock_irq(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001323 return 0;
1324}
1325
1326static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001327 int *count, struct timespec *timespec,
Kees Cookf6f82852011-11-17 12:58:07 -08001328 char **buf, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001329{
1330 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1331 struct efivars *efivars = psi->data;
1332 char name[DUMP_NAME_LEN];
1333 int i;
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001334 int cnt;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001335 unsigned int part, size;
1336 unsigned long time;
1337
1338 while (&efivars->walk_entry->list != &efivars->list) {
1339 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
1340 vendor)) {
1341 for (i = 0; i < DUMP_NAME_LEN; i++) {
1342 name[i] = efivars->walk_entry->var.VariableName[i];
1343 }
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001344 if (sscanf(name, "dump-type%u-%u-%d-%lu",
1345 type, &part, &cnt, &time) == 4) {
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001346 *id = part;
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001347 *count = cnt;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001348 timespec->tv_sec = time;
1349 timespec->tv_nsec = 0;
Seiji Aguchi0f7de852012-11-14 20:28:50 +00001350 } else if (sscanf(name, "dump-type%u-%u-%lu",
1351 type, &part, &time) == 3) {
1352 /*
1353 * Check if an old format,
1354 * which doesn't support holding
1355 * multiple logs, remains.
1356 */
1357 *id = part;
1358 *count = 0;
1359 timespec->tv_sec = time;
1360 timespec->tv_nsec = 0;
1361 } else {
1362 efivars->walk_entry = list_entry(
1363 efivars->walk_entry->list.next,
1364 struct efivar_entry, list);
1365 continue;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001366 }
Seiji Aguchi0f7de852012-11-14 20:28:50 +00001367
1368 get_var_data_locked(efivars, &efivars->walk_entry->var);
1369 size = efivars->walk_entry->var.DataSize;
1370 *buf = kmalloc(size, GFP_KERNEL);
1371 if (*buf == NULL)
1372 return -ENOMEM;
1373 memcpy(*buf, efivars->walk_entry->var.Data,
1374 size);
1375 efivars->walk_entry = list_entry(
1376 efivars->walk_entry->list.next,
1377 struct efivar_entry, list);
1378 return size;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001379 }
1380 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1381 struct efivar_entry, list);
1382 }
1383 return 0;
1384}
1385
Kees Cook3d6d8d22011-11-17 13:13:29 -08001386static int efi_pstore_write(enum pstore_type_id type,
1387 enum kmsg_dump_reason reason, u64 *id,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001388 unsigned int part, int count, size_t size,
1389 struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001390{
1391 char name[DUMP_NAME_LEN];
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001392 efi_char16_t efi_name[DUMP_NAME_LEN];
1393 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1394 struct efivars *efivars = psi->data;
Chen Gongb238b8f2011-10-12 09:17:24 -07001395 int i, ret = 0;
Seiji Aguchid80a3612012-11-14 20:25:37 +00001396 efi_status_t status = EFI_NOT_FOUND;
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001397 unsigned long flags;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001398
Seiji Aguchie59310a2013-01-11 18:10:05 +00001399 if (pstore_cannot_block_path(reason)) {
1400 /*
1401 * If the lock is taken by another cpu in non-blocking path,
1402 * this driver returns without entering firmware to avoid
1403 * hanging up.
1404 */
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001405 if (!spin_trylock_irqsave(&efivars->lock, flags))
Seiji Aguchie59310a2013-01-11 18:10:05 +00001406 return -EBUSY;
1407 } else
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001408 spin_lock_irqsave(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001409
Seiji Aguchid80a3612012-11-14 20:25:37 +00001410 /*
1411 * Check if there is a space enough to log.
1412 * size: a size of logging data
1413 * DUMP_NAME_LEN * 2: a maximum size of variable name
1414 */
Matthew Garrett68d92982013-03-02 19:40:17 -05001415
1416 status = check_var_size_locked(efivars, PSTORE_EFI_ATTRIBUTES,
1417 size + DUMP_NAME_LEN * 2);
1418
1419 if (status) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001420 spin_unlock_irqrestore(&efivars->lock, flags);
Seiji Aguchid80a3612012-11-14 20:25:37 +00001421 *id = part;
1422 return -ENOSPC;
1423 }
1424
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001425 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
Seiji Aguchi96480d92012-11-14 20:26:46 +00001426 get_seconds());
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001427
1428 for (i = 0; i < DUMP_NAME_LEN; i++)
1429 efi_name[i] = name[i];
1430
Mike Waychison7644c162011-07-21 16:58:00 -04001431 efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001432 size, psi->buf);
1433
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001434 spin_unlock_irqrestore(&efivars->lock, flags);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001435
Matt Fleminge9713182013-03-07 11:59:14 +00001436 if (reason == KMSG_DUMP_OOPS && efivar_wq_enabled)
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001437 schedule_work(&efivar_work);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001438
Chen Gongb238b8f2011-10-12 09:17:24 -07001439 *id = part;
1440 return ret;
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001441};
1442
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001443static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001444 struct timespec time, struct pstore_info *psi)
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001445{
Seiji Aguchia9efd392012-11-14 20:27:28 +00001446 char name[DUMP_NAME_LEN];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001447 efi_char16_t efi_name[DUMP_NAME_LEN];
Seiji Aguchif94ec0c2012-11-14 20:29:21 +00001448 char name_old[DUMP_NAME_LEN];
1449 efi_char16_t efi_name_old[DUMP_NAME_LEN];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001450 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1451 struct efivars *efivars = psi->data;
1452 struct efivar_entry *entry, *found = NULL;
1453 int i;
1454
Seiji Aguchi755d4fe2012-11-26 16:07:44 -08001455 sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
Seiji Aguchia9efd392012-11-14 20:27:28 +00001456 time.tv_sec);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001457
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001458 spin_lock_irq(&efivars->lock);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001459
1460 for (i = 0; i < DUMP_NAME_LEN; i++)
Seiji Aguchia9efd392012-11-14 20:27:28 +00001461 efi_name[i] = name[i];
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001462
1463 /*
Seiji Aguchia9efd392012-11-14 20:27:28 +00001464 * Clean up an entry with the same name
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001465 */
1466
1467 list_for_each_entry(entry, &efivars->list, list) {
1468 get_var_data_locked(efivars, &entry->var);
1469
1470 if (efi_guidcmp(entry->var.VendorGuid, vendor))
1471 continue;
1472 if (utf16_strncmp(entry->var.VariableName, efi_name,
Seiji Aguchif94ec0c2012-11-14 20:29:21 +00001473 utf16_strlen(efi_name))) {
1474 /*
1475 * Check if an old format,
1476 * which doesn't support holding
1477 * multiple logs, remains.
1478 */
1479 sprintf(name_old, "dump-type%u-%u-%lu", type,
1480 (unsigned int)id, time.tv_sec);
1481
1482 for (i = 0; i < DUMP_NAME_LEN; i++)
1483 efi_name_old[i] = name_old[i];
1484
1485 if (utf16_strncmp(entry->var.VariableName, efi_name_old,
1486 utf16_strlen(efi_name_old)))
1487 continue;
1488 }
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001489
1490 /* found */
1491 found = entry;
1492 efivars->ops->set_variable(entry->var.VariableName,
1493 &entry->var.VendorGuid,
1494 PSTORE_EFI_ATTRIBUTES,
1495 0, NULL);
Seiji Aguchia9efd392012-11-14 20:27:28 +00001496 break;
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001497 }
1498
1499 if (found)
1500 list_del(&found->list);
1501
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001502 spin_unlock_irq(&efivars->lock);
Seiji Aguchidd230fe2012-11-14 20:26:21 +00001503
1504 if (found)
1505 efivar_unregister(found);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001506
1507 return 0;
1508}
Matthew Garrett5ee9c192011-07-21 16:57:56 -04001509
1510static struct pstore_info efi_pstore_info = {
1511 .owner = THIS_MODULE,
1512 .name = "efi",
1513 .open = efi_pstore_open,
1514 .close = efi_pstore_close,
1515 .read = efi_pstore_read,
1516 .write = efi_pstore_write,
1517 .erase = efi_pstore_erase,
1518};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Seth Forsheeed9dc8c2013-03-07 11:40:17 -06001520static void efivar_pstore_register(struct efivars *efivars)
1521{
1522 efivars->efi_pstore_info = efi_pstore_info;
1523 efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
1524 if (efivars->efi_pstore_info.buf) {
1525 efivars->efi_pstore_info.bufsize = 1024;
1526 efivars->efi_pstore_info.data = efivars;
1527 spin_lock_init(&efivars->efi_pstore_info.buf_lock);
1528 pstore_register(&efivars->efi_pstore_info);
1529 }
1530}
1531#else
1532static void efivar_pstore_register(struct efivars *efivars)
1533{
1534 return;
1535}
1536#endif
1537
Chris Wright2c3c8be2010-05-12 18:28:57 -07001538static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001539 struct bin_attribute *bin_attr,
1540 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 struct efi_variable *new_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001543 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001544 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 efi_status_t status = EFI_NOT_FOUND;
1547 int found = 0;
1548
1549 if (!capable(CAP_SYS_ADMIN))
1550 return -EACCES;
1551
Matthew Garrettfec6c202012-04-30 16:11:30 -04001552 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
1553 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
1554 printk(KERN_ERR "efivars: Malformed variable content\n");
1555 return -EINVAL;
1556 }
1557
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001558 spin_lock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 /*
1561 * Does this variable already exist?
1562 */
Mike Waychison29422692011-03-11 17:43:00 -08001563 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -04001564 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1565 strsize2 = utf16_strsize(new_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (strsize1 == strsize2 &&
1567 !memcmp(&(search_efivar->var.VariableName),
1568 new_var->VariableName, strsize1) &&
1569 !efi_guidcmp(search_efivar->var.VendorGuid,
1570 new_var->VendorGuid)) {
1571 found = 1;
1572 break;
1573 }
1574 }
1575 if (found) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001576 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 return -EINVAL;
1578 }
1579
Matthew Garrett68d92982013-03-02 19:40:17 -05001580 status = check_var_size_locked(efivars, new_var->Attributes,
1581 new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
1582
1583 if (status && status != EFI_UNSUPPORTED) {
1584 spin_unlock_irq(&efivars->lock);
1585 return efi_status_to_err(status);
1586 }
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /* now *really* create the variable via EFI */
Mike Waychison32958142011-03-11 17:43:21 -08001589 status = efivars->ops->set_variable(new_var->VariableName,
1590 &new_var->VendorGuid,
1591 new_var->Attributes,
1592 new_var->DataSize,
1593 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 if (status != EFI_SUCCESS) {
1596 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1597 status);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001598 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return -EIO;
1600 }
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001601 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 /* Create the entry in sysfs. Locking is not required here */
Mike Waychison4142ef12011-03-11 17:43:11 -08001604 status = efivar_create_sysfs_entry(efivars,
Mike Waychisona2940902011-07-21 16:57:57 -04001605 utf16_strsize(new_var->VariableName,
1606 1024),
Mike Waychison4142ef12011-03-11 17:43:11 -08001607 new_var->VariableName,
1608 &new_var->VendorGuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (status) {
1610 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
1611 }
1612 return count;
1613}
1614
Chris Wright2c3c8be2010-05-12 18:28:57 -07001615static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -08001616 struct bin_attribute *bin_attr,
1617 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 struct efi_variable *del_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -08001620 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -08001621 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 efi_status_t status = EFI_NOT_FOUND;
1624 int found = 0;
1625
1626 if (!capable(CAP_SYS_ADMIN))
1627 return -EACCES;
1628
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001629 spin_lock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 /*
1632 * Does this variable already exist?
1633 */
Mike Waychison29422692011-03-11 17:43:00 -08001634 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -04001635 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1636 strsize2 = utf16_strsize(del_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (strsize1 == strsize2 &&
1638 !memcmp(&(search_efivar->var.VariableName),
1639 del_var->VariableName, strsize1) &&
1640 !efi_guidcmp(search_efivar->var.VendorGuid,
1641 del_var->VendorGuid)) {
1642 found = 1;
1643 break;
1644 }
1645 }
1646 if (!found) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001647 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return -EINVAL;
1649 }
1650 /* force the Attributes/DataSize to 0 to ensure deletion */
1651 del_var->Attributes = 0;
1652 del_var->DataSize = 0;
1653
Mike Waychison32958142011-03-11 17:43:21 -08001654 status = efivars->ops->set_variable(del_var->VariableName,
1655 &del_var->VendorGuid,
1656 del_var->Attributes,
1657 del_var->DataSize,
1658 del_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 if (status != EFI_SUCCESS) {
1661 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1662 status);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001663 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return -EIO;
1665 }
Matt Domsch496a0fc2007-01-26 00:57:18 -08001666 list_del(&search_efivar->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 /* We need to release this lock before unregistering. */
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001668 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 efivar_unregister(search_efivar);
1670
1671 /* It's dead Jim.... */
1672 return count;
1673}
1674
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001675static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor)
1676{
1677 struct efivar_entry *entry, *n;
1678 struct efivars *efivars = &__efivars;
1679 unsigned long strsize1, strsize2;
1680 bool found = false;
1681
1682 strsize1 = utf16_strsize(variable_name, 1024);
1683 list_for_each_entry_safe(entry, n, &efivars->list, list) {
1684 strsize2 = utf16_strsize(entry->var.VariableName, 1024);
1685 if (strsize1 == strsize2 &&
1686 !memcmp(variable_name, &(entry->var.VariableName),
1687 strsize2) &&
1688 !efi_guidcmp(entry->var.VendorGuid,
1689 *vendor)) {
1690 found = true;
1691 break;
1692 }
1693 }
1694 return found;
1695}
1696
Matt Flemingec50bd32013-03-01 14:49:12 +00001697/*
1698 * Returns the size of variable_name, in bytes, including the
1699 * terminating NULL character, or variable_name_size if no NULL
1700 * character is found among the first variable_name_size bytes.
1701 */
1702static unsigned long var_name_strnsize(efi_char16_t *variable_name,
1703 unsigned long variable_name_size)
1704{
1705 unsigned long len;
1706 efi_char16_t c;
1707
1708 /*
1709 * The variable name is, by definition, a NULL-terminated
1710 * string, so make absolutely sure that variable_name_size is
1711 * the value we expect it to be. If not, return the real size.
1712 */
1713 for (len = 2; len <= variable_name_size; len += sizeof(c)) {
1714 c = variable_name[(len / sizeof(c)) - 1];
1715 if (!c)
1716 break;
1717 }
1718
1719 return min(len, variable_name_size);
1720}
1721
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001722static void efivar_update_sysfs_entries(struct work_struct *work)
1723{
1724 struct efivars *efivars = &__efivars;
1725 efi_guid_t vendor;
1726 efi_char16_t *variable_name;
1727 unsigned long variable_name_size = 1024;
1728 efi_status_t status = EFI_NOT_FOUND;
1729 bool found;
1730
1731 /* Add new sysfs entries */
1732 while (1) {
1733 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
1734 if (!variable_name) {
1735 pr_err("efivars: Memory allocation failed.\n");
1736 return;
1737 }
1738
1739 spin_lock_irq(&efivars->lock);
1740 found = false;
1741 while (1) {
1742 variable_name_size = 1024;
1743 status = efivars->ops->get_next_variable(
1744 &variable_name_size,
1745 variable_name,
1746 &vendor);
1747 if (status != EFI_SUCCESS) {
1748 break;
1749 } else {
1750 if (!variable_is_present(variable_name,
1751 &vendor)) {
1752 found = true;
1753 break;
1754 }
1755 }
1756 }
1757 spin_unlock_irq(&efivars->lock);
1758
1759 if (!found) {
1760 kfree(variable_name);
1761 break;
Matt Flemingec50bd32013-03-01 14:49:12 +00001762 } else {
1763 variable_name_size = var_name_strnsize(variable_name,
1764 variable_name_size);
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001765 efivar_create_sysfs_entry(efivars,
1766 variable_name_size,
1767 variable_name, &vendor);
Matt Flemingec50bd32013-03-01 14:49:12 +00001768 }
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08001769 }
1770}
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772/*
1773 * Let's not leave out systab information that snuck into
1774 * the efivars driver
1775 */
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001776static ssize_t systab_show(struct kobject *kobj,
1777 struct kobj_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
1779 char *str = buf;
1780
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001781 if (!kobj || !buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return -EINVAL;
1783
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -08001784 if (efi.mps != EFI_INVALID_TABLE_ADDR)
1785 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
1786 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
1787 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
1788 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
1789 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
1790 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
1791 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
1792 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
1793 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
1794 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
1795 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
1796 if (efi.uga != EFI_INVALID_TABLE_ADDR)
1797 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 return str - buf;
1800}
1801
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001802static struct kobj_attribute efi_attr_systab =
1803 __ATTR(systab, 0400, systab_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001805static struct attribute *efi_subsys_attrs[] = {
1806 &efi_attr_systab.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 NULL, /* maybe more in the future? */
1808};
1809
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07001810static struct attribute_group efi_subsys_attr_group = {
1811 .attrs = efi_subsys_attrs,
1812};
1813
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08001814static struct kobject *efi_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816/*
1817 * efivar_create_sysfs_entry()
1818 * Requires:
1819 * variable_name_size = number of bytes required to hold
1820 * variable_name (not counting the NULL
1821 * character at the end.
Mike Waychison29422692011-03-11 17:43:00 -08001822 * efivars->lock is not held on entry or exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 * Returns 1 on failure, 0 on success
1824 */
1825static int
Mike Waychison4142ef12011-03-11 17:43:11 -08001826efivar_create_sysfs_entry(struct efivars *efivars,
1827 unsigned long variable_name_size,
1828 efi_char16_t *variable_name,
1829 efi_guid_t *vendor_guid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Jeremy Kerr310ad752012-10-19 15:16:45 +08001831 int i, short_name_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 char *short_name;
1833 struct efivar_entry *new_efivar;
1834
Jeremy Kerr310ad752012-10-19 15:16:45 +08001835 /*
1836 * Length of the variable bytes in ASCII, plus the '-' separator,
1837 * plus the GUID, plus trailing NUL
1838 */
1839 short_name_size = variable_name_size / sizeof(efi_char16_t)
1840 + 1 + GUID_LEN + 1;
1841
1842 short_name = kzalloc(short_name_size, GFP_KERNEL);
Deepak Saxena9c215382005-11-07 01:01:24 -08001843 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 if (!short_name || !new_efivar) {
Jesper Juhl0933ad92005-06-25 14:59:15 -07001846 kfree(short_name);
1847 kfree(new_efivar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return 1;
1849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Mike Waychison4142ef12011-03-11 17:43:11 -08001851 new_efivar->efivars = efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 memcpy(new_efivar->var.VariableName, variable_name,
1853 variable_name_size);
1854 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
1855
1856 /* Convert Unicode to normal chars (assume top bits are 0),
1857 ala UTF-8 */
1858 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
1859 short_name[i] = variable_name[i] & 0xFF;
1860 }
1861 /* This is ugly, but necessary to separate one vendor's
1862 private variables from another's. */
1863
1864 *(short_name + strlen(short_name)) = '-';
1865 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
1866
Mike Waychison29422692011-03-11 17:43:00 -08001867 new_efivar->kobj.kset = efivars->kset;
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001868 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
1869 "%s", short_name);
Jeff Garzik69b21862006-10-11 01:22:23 -07001870 if (i) {
1871 kfree(short_name);
1872 kfree(new_efivar);
1873 return 1;
1874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -04001876 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
Jesper Juhl0933ad92005-06-25 14:59:15 -07001877 kfree(short_name);
1878 short_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001880 spin_lock_irq(&efivars->lock);
Mike Waychison29422692011-03-11 17:43:00 -08001881 list_add(&new_efivar->list, &efivars->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001882 spin_unlock_irq(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 return 0;
1885}
Mike Waychisond502fbb2011-03-11 17:43:06 -08001886
1887static int
1888create_efivars_bin_attributes(struct efivars *efivars)
1889{
1890 struct bin_attribute *attr;
1891 int error;
1892
1893 /* new_var */
1894 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1895 if (!attr)
1896 return -ENOMEM;
1897
1898 attr->attr.name = "new_var";
1899 attr->attr.mode = 0200;
1900 attr->write = efivar_create;
1901 attr->private = efivars;
1902 efivars->new_var = attr;
1903
1904 /* del_var */
1905 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1906 if (!attr) {
1907 error = -ENOMEM;
1908 goto out_free;
1909 }
1910 attr->attr.name = "del_var";
1911 attr->attr.mode = 0200;
1912 attr->write = efivar_delete;
1913 attr->private = efivars;
1914 efivars->del_var = attr;
1915
1916 sysfs_bin_attr_init(efivars->new_var);
1917 sysfs_bin_attr_init(efivars->del_var);
1918
1919 /* Register */
1920 error = sysfs_create_bin_file(&efivars->kset->kobj,
1921 efivars->new_var);
1922 if (error) {
1923 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
1924 " due to error %d\n", error);
1925 goto out_free;
1926 }
1927 error = sysfs_create_bin_file(&efivars->kset->kobj,
1928 efivars->del_var);
1929 if (error) {
1930 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
1931 " due to error %d\n", error);
1932 sysfs_remove_bin_file(&efivars->kset->kobj,
1933 efivars->new_var);
1934 goto out_free;
1935 }
1936
1937 return 0;
1938out_free:
Dan Carpenter051d51b2011-03-18 10:12:14 +03001939 kfree(efivars->del_var);
1940 efivars->del_var = NULL;
Mike Waychisond502fbb2011-03-11 17:43:06 -08001941 kfree(efivars->new_var);
1942 efivars->new_var = NULL;
1943 return error;
1944}
1945
Mike Waychison4fc756b2011-03-11 17:43:27 -08001946void unregister_efivars(struct efivars *efivars)
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001947{
1948 struct efivar_entry *entry, *n;
Mike Waychison4142ef12011-03-11 17:43:11 -08001949
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001950 list_for_each_entry_safe(entry, n, &efivars->list, list) {
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001951 spin_lock_irq(&efivars->lock);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001952 list_del(&entry->list);
Seiji Aguchi81fa4e52013-02-12 12:59:07 -08001953 spin_unlock_irq(&efivars->lock);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001954 efivar_unregister(entry);
1955 }
1956 if (efivars->new_var)
1957 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
1958 if (efivars->del_var)
1959 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
1960 kfree(efivars->new_var);
1961 kfree(efivars->del_var);
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08001962 kobject_put(efivars->kobject);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08001963 kset_unregister(efivars->kset);
1964}
Mike Waychison4fc756b2011-03-11 17:43:27 -08001965EXPORT_SYMBOL_GPL(unregister_efivars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Matt Fleminge9713182013-03-07 11:59:14 +00001967/*
1968 * Print a warning when duplicate EFI variables are encountered and
1969 * disable the sysfs workqueue since the firmware is buggy.
1970 */
1971static void dup_variable_bug(efi_char16_t *s16, efi_guid_t *vendor_guid,
1972 unsigned long len16)
1973{
1974 size_t i, len8 = len16 / sizeof(efi_char16_t);
1975 char *s8;
1976
1977 /*
1978 * Disable the workqueue since the algorithm it uses for
1979 * detecting new variables won't work with this buggy
1980 * implementation of GetNextVariableName().
1981 */
1982 efivar_wq_enabled = false;
1983
1984 s8 = kzalloc(len8, GFP_KERNEL);
1985 if (!s8)
1986 return;
1987
1988 for (i = 0; i < len8; i++)
1989 s8[i] = s16[i];
1990
1991 printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
1992 s8, vendor_guid);
1993 kfree(s8);
1994}
1995
Mike Waychison4fc756b2011-03-11 17:43:27 -08001996int register_efivars(struct efivars *efivars,
1997 const struct efivar_operations *ops,
1998 struct kobject *parent_kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
2000 efi_status_t status = EFI_NOT_FOUND;
2001 efi_guid_t vendor_guid;
2002 efi_char16_t *variable_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 unsigned long variable_name_size = 1024;
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -07002004 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Deepak Saxena9c215382005-11-07 01:01:24 -08002006 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (!variable_name) {
2008 printk(KERN_ERR "efivars: Memory allocation failed.\n");
2009 return -ENOMEM;
2010 }
2011
Mike Waychison29422692011-03-11 17:43:00 -08002012 spin_lock_init(&efivars->lock);
2013 INIT_LIST_HEAD(&efivars->list);
Mike Waychison32958142011-03-11 17:43:21 -08002014 efivars->ops = ops;
Mike Waychison29422692011-03-11 17:43:00 -08002015
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002016 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
Mike Waychison29422692011-03-11 17:43:00 -08002017 if (!efivars->kset) {
Greg Kroah-Hartman66ac8312007-11-02 13:20:40 -07002018 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
2019 error = -ENOMEM;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002020 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022
Lee, Chun-Yi605e70c2012-10-05 13:54:56 +08002023 efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
2024 if (!efivars->kobject) {
2025 pr_err("efivars: Subsystem registration failed.\n");
2026 error = -ENOMEM;
2027 kset_unregister(efivars->kset);
2028 goto out;
2029 }
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /*
2032 * Per EFI spec, the maximum storage allocated for both
2033 * the variable name and variable data is 1024 bytes.
2034 */
2035
2036 do {
2037 variable_name_size = 1024;
2038
Mike Waychison32958142011-03-11 17:43:21 -08002039 status = ops->get_next_variable(&variable_name_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 variable_name,
2041 &vendor_guid);
2042 switch (status) {
2043 case EFI_SUCCESS:
Matt Flemingec50bd32013-03-01 14:49:12 +00002044 variable_name_size = var_name_strnsize(variable_name,
2045 variable_name_size);
Matt Fleminge9713182013-03-07 11:59:14 +00002046
2047 /*
2048 * Some firmware implementations return the
2049 * same variable name on multiple calls to
2050 * get_next_variable(). Terminate the loop
2051 * immediately as there is no guarantee that
2052 * we'll ever see a different variable name,
2053 * and may end up looping here forever.
2054 */
2055 if (variable_is_present(variable_name, &vendor_guid)) {
2056 dup_variable_bug(variable_name, &vendor_guid,
2057 variable_name_size);
2058 status = EFI_NOT_FOUND;
2059 break;
2060 }
2061
Mike Waychison4142ef12011-03-11 17:43:11 -08002062 efivar_create_sysfs_entry(efivars,
2063 variable_name_size,
2064 variable_name,
2065 &vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 break;
2067 case EFI_NOT_FOUND:
2068 break;
2069 default:
2070 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
2071 status);
2072 status = EFI_NOT_FOUND;
2073 break;
2074 }
2075 } while (status != EFI_NOT_FOUND);
2076
Mike Waychisond502fbb2011-03-11 17:43:06 -08002077 error = create_efivars_bin_attributes(efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002078 if (error)
2079 unregister_efivars(efivars);
2080
Seth Forsheeec0971b2013-03-11 16:17:50 -05002081 if (!efivars_pstore_disable)
2082 efivar_pstore_register(efivars);
Matthew Garrett5ee9c192011-07-21 16:57:56 -04002083
Matthew Garrett5d9db882012-10-05 13:54:56 +08002084 register_filesystem(&efivarfs_type);
2085
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002086out:
2087 kfree(variable_name);
2088
2089 return error;
2090}
Mike Waychison4fc756b2011-03-11 17:43:27 -08002091EXPORT_SYMBOL_GPL(register_efivars);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002092
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002093/*
2094 * For now we register the efi subsystem with the firmware subsystem
2095 * and the vars subsystem with the efi subsystem. In the future, it
2096 * might make sense to split off the efi subsystem into its own
2097 * driver, but for now only efivars will register with it, so just
2098 * include it here.
2099 */
2100
2101static int __init
2102efivars_init(void)
2103{
2104 int error = 0;
2105
2106 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
2107 EFIVARS_DATE);
2108
Matt Fleming83e68182012-11-14 09:42:35 +00002109 if (!efi_enabled(EFI_RUNTIME_SERVICES))
Mike Waychison4fc756b2011-03-11 17:43:27 -08002110 return 0;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002111
2112 /* For now we'll register the efi directory at /sys/firmware/efi */
2113 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
2114 if (!efi_kobj) {
2115 printk(KERN_ERR "efivars: Firmware registration failed.\n");
2116 return -ENOMEM;
2117 }
2118
Mike Waychison32958142011-03-11 17:43:21 -08002119 ops.get_variable = efi.get_variable;
2120 ops.set_variable = efi.set_variable;
2121 ops.get_next_variable = efi.get_next_variable;
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00002122 ops.query_variable_store = efi_query_variable_store;
Matt Fleming89d16662012-11-09 21:02:56 +00002123
Mike Waychison32958142011-03-11 17:43:21 -08002124 error = register_efivars(&__efivars, &ops, efi_kobj);
Dan Carpenter3116aab2011-03-18 10:12:38 +03002125 if (error)
2126 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 /* Don't forget the systab entry */
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08002129 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002130 if (error) {
2131 printk(KERN_ERR
2132 "efivars: Sysfs attribute export failed with error %d.\n",
2133 error);
Dan Carpenter3116aab2011-03-18 10:12:38 +03002134 goto err_unregister;
Mike Waychison76b53f7c2011-03-11 17:43:16 -08002135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Dan Carpenter3116aab2011-03-18 10:12:38 +03002137 return 0;
2138
2139err_unregister:
2140 unregister_efivars(&__efivars);
2141err_put:
2142 kobject_put(efi_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return error;
2144}
2145
2146static void __exit
2147efivars_exit(void)
2148{
Seiji Aguchia93bc0c2013-02-12 13:04:41 -08002149 cancel_work_sync(&efivar_work);
2150
Matt Fleming83e68182012-11-14 09:42:35 +00002151 if (efi_enabled(EFI_RUNTIME_SERVICES)) {
Randy Dunlapaabb6e12011-05-06 13:27:41 -07002152 unregister_efivars(&__efivars);
2153 kobject_put(efi_kobj);
2154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
2157module_init(efivars_init);
2158module_exit(efivars_exit);
2159