blob: 4202a31704673a9b938de84be1a8b581e3427067 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83#include <asm/uaccess.h>
84
85#define EFIVARS_VERSION "0.08"
86#define EFIVARS_DATE "2004-May-17"
87
88MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
89MODULE_DESCRIPTION("sysfs interface to EFI Variables");
90MODULE_LICENSE("GPL");
91MODULE_VERSION(EFIVARS_VERSION);
92
Matthew Garrett5ee9c192011-07-21 16:57:56 -040093#define DUMP_NAME_LEN 52
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
96 * The maximum size of VariableName + Data = 1024
97 * Therefore, it's reasonable to save that much
98 * space in each part of the structure,
99 * and we use a page for reading/writing.
100 */
101
102struct efi_variable {
103 efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
104 efi_guid_t VendorGuid;
105 unsigned long DataSize;
106 __u8 Data[1024];
107 efi_status_t Status;
108 __u32 Attributes;
109} __attribute__((packed));
110
111
112struct efivar_entry {
Mike Waychison4142ef12011-03-11 17:43:11 -0800113 struct efivars *efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 struct efi_variable var;
115 struct list_head list;
116 struct kobject kobj;
117};
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119struct efivar_attribute {
120 struct attribute attr;
121 ssize_t (*show) (struct efivar_entry *entry, char *buf);
122 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
123};
124
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#define EFIVAR_ATTR(_name, _mode, _show, _store) \
127struct efivar_attribute efivar_attr_##_name = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900128 .attr = {.name = __stringify(_name), .mode = _mode}, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .show = _show, \
130 .store = _store, \
131};
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
134#define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
135
136/*
137 * Prototype for sysfs creation function
138 */
139static int
Mike Waychison4142ef12011-03-11 17:43:11 -0800140efivar_create_sysfs_entry(struct efivars *efivars,
141 unsigned long variable_name_size,
142 efi_char16_t *variable_name,
143 efi_guid_t *vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145/* Return the number of unicode characters in data */
146static unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400147utf16_strnlen(efi_char16_t *s, size_t maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 unsigned long length = 0;
150
Mike Waychisona2940902011-07-21 16:57:57 -0400151 while (*s++ != 0 && length < maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 length++;
153 return length;
154}
155
Mike Waychisona2940902011-07-21 16:57:57 -0400156static unsigned long
157utf16_strlen(efi_char16_t *s)
158{
159 return utf16_strnlen(s, ~0UL);
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/*
163 * Return the number of bytes is the length of this string
164 * Note: this is NOT the same as the number of unicode characters
165 */
166static inline unsigned long
Mike Waychisona2940902011-07-21 16:57:57 -0400167utf16_strsize(efi_char16_t *data, unsigned long maxlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Mike Waychisona2940902011-07-21 16:57:57 -0400169 return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172static efi_status_t
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400173get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 efi_status_t status;
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 var->DataSize = 1024;
Mike Waychison32958142011-03-11 17:43:21 -0800178 status = efivars->ops->get_variable(var->VariableName,
179 &var->VendorGuid,
180 &var->Attributes,
181 &var->DataSize,
182 var->Data);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400183 return status;
184}
185
186static efi_status_t
187get_var_data(struct efivars *efivars, struct efi_variable *var)
188{
189 efi_status_t status;
190
191 spin_lock(&efivars->lock);
192 status = get_var_data_locked(efivars, var);
Mike Waychison29422692011-03-11 17:43:00 -0800193 spin_unlock(&efivars->lock);
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (status != EFI_SUCCESS) {
196 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
197 status);
198 }
199 return status;
200}
201
202static ssize_t
203efivar_guid_read(struct efivar_entry *entry, char *buf)
204{
205 struct efi_variable *var = &entry->var;
206 char *str = buf;
207
208 if (!entry || !buf)
209 return 0;
210
211 efi_guid_unparse(&var->VendorGuid, str);
212 str += strlen(str);
213 str += sprintf(str, "\n");
214
215 return str - buf;
216}
217
218static ssize_t
219efivar_attr_read(struct efivar_entry *entry, char *buf)
220{
221 struct efi_variable *var = &entry->var;
222 char *str = buf;
223 efi_status_t status;
224
225 if (!entry || !buf)
226 return -EINVAL;
227
Mike Waychison4142ef12011-03-11 17:43:11 -0800228 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (status != EFI_SUCCESS)
230 return -EIO;
231
232 if (var->Attributes & 0x1)
233 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
234 if (var->Attributes & 0x2)
235 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
236 if (var->Attributes & 0x4)
237 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
238 return str - buf;
239}
240
241static ssize_t
242efivar_size_read(struct efivar_entry *entry, char *buf)
243{
244 struct efi_variable *var = &entry->var;
245 char *str = buf;
246 efi_status_t status;
247
248 if (!entry || !buf)
249 return -EINVAL;
250
Mike Waychison4142ef12011-03-11 17:43:11 -0800251 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (status != EFI_SUCCESS)
253 return -EIO;
254
255 str += sprintf(str, "0x%lx\n", var->DataSize);
256 return str - buf;
257}
258
259static ssize_t
260efivar_data_read(struct efivar_entry *entry, char *buf)
261{
262 struct efi_variable *var = &entry->var;
263 efi_status_t status;
264
265 if (!entry || !buf)
266 return -EINVAL;
267
Mike Waychison4142ef12011-03-11 17:43:11 -0800268 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (status != EFI_SUCCESS)
270 return -EIO;
271
272 memcpy(buf, var->Data, var->DataSize);
273 return var->DataSize;
274}
275/*
276 * We allow each variable to be edited via rewriting the
277 * entire efi variable structure.
278 */
279static ssize_t
280efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
281{
282 struct efi_variable *new_var, *var = &entry->var;
Mike Waychison4142ef12011-03-11 17:43:11 -0800283 struct efivars *efivars = entry->efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 efi_status_t status = EFI_NOT_FOUND;
285
286 if (count != sizeof(struct efi_variable))
287 return -EINVAL;
288
289 new_var = (struct efi_variable *)buf;
290 /*
291 * If only updating the variable data, then the name
292 * and guid should remain the same
293 */
294 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
295 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
296 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
297 return -EINVAL;
298 }
299
300 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
301 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
302 return -EINVAL;
303 }
304
Mike Waychison29422692011-03-11 17:43:00 -0800305 spin_lock(&efivars->lock);
Mike Waychison32958142011-03-11 17:43:21 -0800306 status = efivars->ops->set_variable(new_var->VariableName,
307 &new_var->VendorGuid,
308 new_var->Attributes,
309 new_var->DataSize,
310 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Mike Waychison29422692011-03-11 17:43:00 -0800312 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 if (status != EFI_SUCCESS) {
315 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
316 status);
317 return -EIO;
318 }
319
320 memcpy(&entry->var, new_var, count);
321 return count;
322}
323
324static ssize_t
325efivar_show_raw(struct efivar_entry *entry, char *buf)
326{
327 struct efi_variable *var = &entry->var;
328 efi_status_t status;
329
330 if (!entry || !buf)
331 return 0;
332
Mike Waychison4142ef12011-03-11 17:43:11 -0800333 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (status != EFI_SUCCESS)
335 return -EIO;
336
337 memcpy(buf, var, sizeof(*var));
338 return sizeof(*var);
339}
340
341/*
342 * Generic read/write functions that call the specific functions of
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200343 * the attributes...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 */
345static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
346 char *buf)
347{
348 struct efivar_entry *var = to_efivar_entry(kobj);
349 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500350 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (!capable(CAP_SYS_ADMIN))
353 return -EACCES;
354
355 if (efivar_attr->show) {
356 ret = efivar_attr->show(var, buf);
357 }
358 return ret;
359}
360
361static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
362 const char *buf, size_t count)
363{
364 struct efivar_entry *var = to_efivar_entry(kobj);
365 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500366 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 if (!capable(CAP_SYS_ADMIN))
369 return -EACCES;
370
371 if (efivar_attr->store)
372 ret = efivar_attr->store(var, buf, count);
373
374 return ret;
375}
376
Emese Revfy52cf25d2010-01-19 02:58:23 +0100377static const struct sysfs_ops efivar_attr_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 .show = efivar_attr_show,
379 .store = efivar_attr_store,
380};
381
382static void efivar_release(struct kobject *kobj)
383{
384 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 kfree(var);
386}
387
388static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
389static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
390static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
391static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
392static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
393
394static struct attribute *def_attrs[] = {
395 &efivar_attr_guid.attr,
396 &efivar_attr_size.attr,
397 &efivar_attr_attributes.attr,
398 &efivar_attr_data.attr,
399 &efivar_attr_raw_var.attr,
400 NULL,
401};
402
Greg Kroah-Hartmane89a4112007-10-11 10:47:49 -0600403static struct kobj_type efivar_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 .release = efivar_release,
405 .sysfs_ops = &efivar_attr_ops,
406 .default_attrs = def_attrs,
407};
408
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400409static struct pstore_info efi_pstore_info;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411static inline void
412efivar_unregister(struct efivar_entry *var)
413{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800414 kobject_put(&var->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400417#ifdef CONFIG_PSTORE
418
419static int efi_pstore_open(struct pstore_info *psi)
420{
421 struct efivars *efivars = psi->data;
422
423 spin_lock(&efivars->lock);
424 efivars->walk_entry = list_first_entry(&efivars->list,
425 struct efivar_entry, list);
426 return 0;
427}
428
429static int efi_pstore_close(struct pstore_info *psi)
430{
431 struct efivars *efivars = psi->data;
432
433 spin_unlock(&efivars->lock);
434 return 0;
435}
436
437static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
438 struct timespec *timespec, struct pstore_info *psi)
439{
440 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
441 struct efivars *efivars = psi->data;
442 char name[DUMP_NAME_LEN];
443 int i;
444 unsigned int part, size;
445 unsigned long time;
446
447 while (&efivars->walk_entry->list != &efivars->list) {
448 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
449 vendor)) {
450 for (i = 0; i < DUMP_NAME_LEN; i++) {
451 name[i] = efivars->walk_entry->var.VariableName[i];
452 }
453 if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
454 *id = part;
455 timespec->tv_sec = time;
456 timespec->tv_nsec = 0;
457 get_var_data_locked(efivars, &efivars->walk_entry->var);
458 size = efivars->walk_entry->var.DataSize;
459 memcpy(psi->buf, efivars->walk_entry->var.Data, size);
460 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
461 struct efivar_entry, list);
462 return size;
463 }
464 }
465 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
466 struct efivar_entry, list);
467 }
468 return 0;
469}
470
471static u64 efi_pstore_write(enum pstore_type_id type, unsigned int part,
472 size_t size, struct pstore_info *psi)
473{
474 char name[DUMP_NAME_LEN];
475 char stub_name[DUMP_NAME_LEN];
476 efi_char16_t efi_name[DUMP_NAME_LEN];
477 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
478 struct efivars *efivars = psi->data;
479 struct efivar_entry *entry, *found = NULL;
480 int i;
481
482 sprintf(stub_name, "dump-type%u-%u-", type, part);
483 sprintf(name, "%s%lu", stub_name, get_seconds());
484
485 spin_lock(&efivars->lock);
486
487 for (i = 0; i < DUMP_NAME_LEN; i++)
488 efi_name[i] = stub_name[i];
489
490 /*
491 * Clean up any entries with the same name
492 */
493
494 list_for_each_entry(entry, &efivars->list, list) {
495 get_var_data_locked(efivars, &entry->var);
496
497 for (i = 0; i < DUMP_NAME_LEN; i++) {
498 if (efi_name[i] == 0) {
499 found = entry;
500 efivars->ops->set_variable(entry->var.VariableName,
501 &entry->var.VendorGuid,
502 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
503 0, NULL);
504 break;
505 } else if (efi_name[i] != entry->var.VariableName[i]) {
506 break;
507 }
508 }
509 }
510
511 if (found)
512 list_del(&found->list);
513
514 for (i = 0; i < DUMP_NAME_LEN; i++)
515 efi_name[i] = name[i];
516
517 efivars->ops->set_variable(efi_name, &vendor,
518 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
519 size, psi->buf);
520
521 spin_unlock(&efivars->lock);
522
523 if (found)
524 efivar_unregister(found);
525
526 if (size)
Mike Waychisona2940902011-07-21 16:57:57 -0400527 efivar_create_sysfs_entry(efivars,
528 utf16_strsize(efi_name,
529 DUMP_NAME_LEN * 2),
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400530 efi_name, &vendor);
531
532 return part;
533};
534
535static int efi_pstore_erase(enum pstore_type_id type, u64 id,
536 struct pstore_info *psi)
537{
538 efi_pstore_write(type, id, 0, psi);
539
540 return 0;
541}
542#else
543static int efi_pstore_open(struct pstore_info *psi)
544{
545 return 0;
546}
547
548static int efi_pstore_close(struct pstore_info *psi)
549{
550 return 0;
551}
552
553static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
554 struct timespec *time, struct pstore_info *psi)
555{
556 return -1;
557}
558
559static u64 efi_pstore_write(enum pstore_type_id type, int part, size_t size,
560 struct pstore_info *psi)
561{
562 return 0;
563}
564
565static int efi_pstore_erase(enum pstore_type_id type, u64 id,
566 struct pstore_info *psi)
567{
568 return 0;
569}
570#endif
571
572static struct pstore_info efi_pstore_info = {
573 .owner = THIS_MODULE,
574 .name = "efi",
575 .open = efi_pstore_open,
576 .close = efi_pstore_close,
577 .read = efi_pstore_read,
578 .write = efi_pstore_write,
579 .erase = efi_pstore_erase,
580};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Chris Wright2c3c8be2010-05-12 18:28:57 -0700582static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -0800583 struct bin_attribute *bin_attr,
584 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct efi_variable *new_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -0800587 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -0800588 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 efi_status_t status = EFI_NOT_FOUND;
591 int found = 0;
592
593 if (!capable(CAP_SYS_ADMIN))
594 return -EACCES;
595
Mike Waychison29422692011-03-11 17:43:00 -0800596 spin_lock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /*
599 * Does this variable already exist?
600 */
Mike Waychison29422692011-03-11 17:43:00 -0800601 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -0400602 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
603 strsize2 = utf16_strsize(new_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (strsize1 == strsize2 &&
605 !memcmp(&(search_efivar->var.VariableName),
606 new_var->VariableName, strsize1) &&
607 !efi_guidcmp(search_efivar->var.VendorGuid,
608 new_var->VendorGuid)) {
609 found = 1;
610 break;
611 }
612 }
613 if (found) {
Mike Waychison29422692011-03-11 17:43:00 -0800614 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return -EINVAL;
616 }
617
618 /* now *really* create the variable via EFI */
Mike Waychison32958142011-03-11 17:43:21 -0800619 status = efivars->ops->set_variable(new_var->VariableName,
620 &new_var->VendorGuid,
621 new_var->Attributes,
622 new_var->DataSize,
623 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 if (status != EFI_SUCCESS) {
626 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
627 status);
Mike Waychison29422692011-03-11 17:43:00 -0800628 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -EIO;
630 }
Mike Waychison29422692011-03-11 17:43:00 -0800631 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 /* Create the entry in sysfs. Locking is not required here */
Mike Waychison4142ef12011-03-11 17:43:11 -0800634 status = efivar_create_sysfs_entry(efivars,
Mike Waychisona2940902011-07-21 16:57:57 -0400635 utf16_strsize(new_var->VariableName,
636 1024),
Mike Waychison4142ef12011-03-11 17:43:11 -0800637 new_var->VariableName,
638 &new_var->VendorGuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (status) {
640 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
641 }
642 return count;
643}
644
Chris Wright2c3c8be2010-05-12 18:28:57 -0700645static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -0800646 struct bin_attribute *bin_attr,
647 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct efi_variable *del_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -0800650 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -0800651 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 efi_status_t status = EFI_NOT_FOUND;
654 int found = 0;
655
656 if (!capable(CAP_SYS_ADMIN))
657 return -EACCES;
658
Mike Waychison29422692011-03-11 17:43:00 -0800659 spin_lock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 /*
662 * Does this variable already exist?
663 */
Mike Waychison29422692011-03-11 17:43:00 -0800664 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Mike Waychisona2940902011-07-21 16:57:57 -0400665 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
666 strsize2 = utf16_strsize(del_var->VariableName, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (strsize1 == strsize2 &&
668 !memcmp(&(search_efivar->var.VariableName),
669 del_var->VariableName, strsize1) &&
670 !efi_guidcmp(search_efivar->var.VendorGuid,
671 del_var->VendorGuid)) {
672 found = 1;
673 break;
674 }
675 }
676 if (!found) {
Mike Waychison29422692011-03-11 17:43:00 -0800677 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return -EINVAL;
679 }
680 /* force the Attributes/DataSize to 0 to ensure deletion */
681 del_var->Attributes = 0;
682 del_var->DataSize = 0;
683
Mike Waychison32958142011-03-11 17:43:21 -0800684 status = efivars->ops->set_variable(del_var->VariableName,
685 &del_var->VendorGuid,
686 del_var->Attributes,
687 del_var->DataSize,
688 del_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (status != EFI_SUCCESS) {
691 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
692 status);
Mike Waychison29422692011-03-11 17:43:00 -0800693 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -EIO;
695 }
Matt Domsch496a0fc2007-01-26 00:57:18 -0800696 list_del(&search_efivar->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /* We need to release this lock before unregistering. */
Mike Waychison29422692011-03-11 17:43:00 -0800698 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 efivar_unregister(search_efivar);
700
701 /* It's dead Jim.... */
702 return count;
703}
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705/*
706 * Let's not leave out systab information that snuck into
707 * the efivars driver
708 */
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -0700709static ssize_t systab_show(struct kobject *kobj,
710 struct kobj_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 char *str = buf;
713
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -0700714 if (!kobj || !buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return -EINVAL;
716
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800717 if (efi.mps != EFI_INVALID_TABLE_ADDR)
718 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
719 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
720 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
721 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
722 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
723 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
724 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
725 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
726 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
727 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
728 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
729 if (efi.uga != EFI_INVALID_TABLE_ADDR)
730 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 return str - buf;
733}
734
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -0700735static struct kobj_attribute efi_attr_systab =
736 __ATTR(systab, 0400, systab_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -0700738static struct attribute *efi_subsys_attrs[] = {
739 &efi_attr_systab.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 NULL, /* maybe more in the future? */
741};
742
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -0700743static struct attribute_group efi_subsys_attr_group = {
744 .attrs = efi_subsys_attrs,
745};
746
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -0800747static struct kobject *efi_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749/*
750 * efivar_create_sysfs_entry()
751 * Requires:
752 * variable_name_size = number of bytes required to hold
753 * variable_name (not counting the NULL
754 * character at the end.
Mike Waychison29422692011-03-11 17:43:00 -0800755 * efivars->lock is not held on entry or exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * Returns 1 on failure, 0 on success
757 */
758static int
Mike Waychison4142ef12011-03-11 17:43:11 -0800759efivar_create_sysfs_entry(struct efivars *efivars,
760 unsigned long variable_name_size,
761 efi_char16_t *variable_name,
762 efi_guid_t *vendor_guid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
765 char *short_name;
766 struct efivar_entry *new_efivar;
767
Deepak Saxena9c215382005-11-07 01:01:24 -0800768 short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
769 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 if (!short_name || !new_efivar) {
Jesper Juhl0933ad92005-06-25 14:59:15 -0700772 kfree(short_name);
773 kfree(new_efivar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return 1;
775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Mike Waychison4142ef12011-03-11 17:43:11 -0800777 new_efivar->efivars = efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 memcpy(new_efivar->var.VariableName, variable_name,
779 variable_name_size);
780 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
781
782 /* Convert Unicode to normal chars (assume top bits are 0),
783 ala UTF-8 */
784 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
785 short_name[i] = variable_name[i] & 0xFF;
786 }
787 /* This is ugly, but necessary to separate one vendor's
788 private variables from another's. */
789
790 *(short_name + strlen(short_name)) = '-';
791 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
792
Mike Waychison29422692011-03-11 17:43:00 -0800793 new_efivar->kobj.kset = efivars->kset;
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -0400794 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
795 "%s", short_name);
Jeff Garzik69b21862006-10-11 01:22:23 -0700796 if (i) {
797 kfree(short_name);
798 kfree(new_efivar);
799 return 1;
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -0400802 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
Jesper Juhl0933ad92005-06-25 14:59:15 -0700803 kfree(short_name);
804 short_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Mike Waychison29422692011-03-11 17:43:00 -0800806 spin_lock(&efivars->lock);
807 list_add(&new_efivar->list, &efivars->list);
808 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 return 0;
811}
Mike Waychisond502fbb2011-03-11 17:43:06 -0800812
813static int
814create_efivars_bin_attributes(struct efivars *efivars)
815{
816 struct bin_attribute *attr;
817 int error;
818
819 /* new_var */
820 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
821 if (!attr)
822 return -ENOMEM;
823
824 attr->attr.name = "new_var";
825 attr->attr.mode = 0200;
826 attr->write = efivar_create;
827 attr->private = efivars;
828 efivars->new_var = attr;
829
830 /* del_var */
831 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
832 if (!attr) {
833 error = -ENOMEM;
834 goto out_free;
835 }
836 attr->attr.name = "del_var";
837 attr->attr.mode = 0200;
838 attr->write = efivar_delete;
839 attr->private = efivars;
840 efivars->del_var = attr;
841
842 sysfs_bin_attr_init(efivars->new_var);
843 sysfs_bin_attr_init(efivars->del_var);
844
845 /* Register */
846 error = sysfs_create_bin_file(&efivars->kset->kobj,
847 efivars->new_var);
848 if (error) {
849 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
850 " due to error %d\n", error);
851 goto out_free;
852 }
853 error = sysfs_create_bin_file(&efivars->kset->kobj,
854 efivars->del_var);
855 if (error) {
856 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
857 " due to error %d\n", error);
858 sysfs_remove_bin_file(&efivars->kset->kobj,
859 efivars->new_var);
860 goto out_free;
861 }
862
863 return 0;
864out_free:
Dan Carpenter051d51b2011-03-18 10:12:14 +0300865 kfree(efivars->del_var);
866 efivars->del_var = NULL;
Mike Waychisond502fbb2011-03-11 17:43:06 -0800867 kfree(efivars->new_var);
868 efivars->new_var = NULL;
869 return error;
870}
871
Mike Waychison4fc756b2011-03-11 17:43:27 -0800872void unregister_efivars(struct efivars *efivars)
Mike Waychison76b53f72011-03-11 17:43:16 -0800873{
874 struct efivar_entry *entry, *n;
Mike Waychison4142ef12011-03-11 17:43:11 -0800875
Mike Waychison76b53f72011-03-11 17:43:16 -0800876 list_for_each_entry_safe(entry, n, &efivars->list, list) {
877 spin_lock(&efivars->lock);
878 list_del(&entry->list);
879 spin_unlock(&efivars->lock);
880 efivar_unregister(entry);
881 }
882 if (efivars->new_var)
883 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
884 if (efivars->del_var)
885 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
886 kfree(efivars->new_var);
887 kfree(efivars->del_var);
888 kset_unregister(efivars->kset);
889}
Mike Waychison4fc756b2011-03-11 17:43:27 -0800890EXPORT_SYMBOL_GPL(unregister_efivars);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Mike Waychison4fc756b2011-03-11 17:43:27 -0800892int register_efivars(struct efivars *efivars,
893 const struct efivar_operations *ops,
894 struct kobject *parent_kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 efi_status_t status = EFI_NOT_FOUND;
897 efi_guid_t vendor_guid;
898 efi_char16_t *variable_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 unsigned long variable_name_size = 1024;
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -0700900 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Deepak Saxena9c215382005-11-07 01:01:24 -0800902 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (!variable_name) {
904 printk(KERN_ERR "efivars: Memory allocation failed.\n");
905 return -ENOMEM;
906 }
907
Mike Waychison29422692011-03-11 17:43:00 -0800908 spin_lock_init(&efivars->lock);
909 INIT_LIST_HEAD(&efivars->list);
Mike Waychison32958142011-03-11 17:43:21 -0800910 efivars->ops = ops;
Mike Waychison29422692011-03-11 17:43:00 -0800911
Mike Waychison76b53f72011-03-11 17:43:16 -0800912 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
Mike Waychison29422692011-03-11 17:43:00 -0800913 if (!efivars->kset) {
Greg Kroah-Hartman66ac8312007-11-02 13:20:40 -0700914 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
915 error = -ENOMEM;
Mike Waychison76b53f72011-03-11 17:43:16 -0800916 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918
919 /*
920 * Per EFI spec, the maximum storage allocated for both
921 * the variable name and variable data is 1024 bytes.
922 */
923
924 do {
925 variable_name_size = 1024;
926
Mike Waychison32958142011-03-11 17:43:21 -0800927 status = ops->get_next_variable(&variable_name_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 variable_name,
929 &vendor_guid);
930 switch (status) {
931 case EFI_SUCCESS:
Mike Waychison4142ef12011-03-11 17:43:11 -0800932 efivar_create_sysfs_entry(efivars,
933 variable_name_size,
934 variable_name,
935 &vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 break;
937 case EFI_NOT_FOUND:
938 break;
939 default:
940 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
941 status);
942 status = EFI_NOT_FOUND;
943 break;
944 }
945 } while (status != EFI_NOT_FOUND);
946
Mike Waychisond502fbb2011-03-11 17:43:06 -0800947 error = create_efivars_bin_attributes(efivars);
Mike Waychison76b53f72011-03-11 17:43:16 -0800948 if (error)
949 unregister_efivars(efivars);
950
Matthew Garrett5ee9c192011-07-21 16:57:56 -0400951 efivars->efi_pstore_info = efi_pstore_info;
952
953 efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
954 if (efivars->efi_pstore_info.buf) {
955 efivars->efi_pstore_info.bufsize = 1024;
956 efivars->efi_pstore_info.data = efivars;
957 mutex_init(&efivars->efi_pstore_info.buf_mutex);
958 pstore_register(&efivars->efi_pstore_info);
959 }
960
Mike Waychison76b53f72011-03-11 17:43:16 -0800961out:
962 kfree(variable_name);
963
964 return error;
965}
Mike Waychison4fc756b2011-03-11 17:43:27 -0800966EXPORT_SYMBOL_GPL(register_efivars);
Mike Waychison76b53f72011-03-11 17:43:16 -0800967
968static struct efivars __efivars;
Mike Waychison32958142011-03-11 17:43:21 -0800969static struct efivar_operations ops;
Mike Waychison76b53f72011-03-11 17:43:16 -0800970
971/*
972 * For now we register the efi subsystem with the firmware subsystem
973 * and the vars subsystem with the efi subsystem. In the future, it
974 * might make sense to split off the efi subsystem into its own
975 * driver, but for now only efivars will register with it, so just
976 * include it here.
977 */
978
979static int __init
980efivars_init(void)
981{
982 int error = 0;
983
984 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
985 EFIVARS_DATE);
986
987 if (!efi_enabled)
Mike Waychison4fc756b2011-03-11 17:43:27 -0800988 return 0;
Mike Waychison76b53f72011-03-11 17:43:16 -0800989
990 /* For now we'll register the efi directory at /sys/firmware/efi */
991 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
992 if (!efi_kobj) {
993 printk(KERN_ERR "efivars: Firmware registration failed.\n");
994 return -ENOMEM;
995 }
996
Mike Waychison32958142011-03-11 17:43:21 -0800997 ops.get_variable = efi.get_variable;
998 ops.set_variable = efi.set_variable;
999 ops.get_next_variable = efi.get_next_variable;
1000 error = register_efivars(&__efivars, &ops, efi_kobj);
Dan Carpenter3116aab2011-03-18 10:12:38 +03001001 if (error)
1002 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 /* Don't forget the systab entry */
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -08001005 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
Mike Waychison76b53f72011-03-11 17:43:16 -08001006 if (error) {
1007 printk(KERN_ERR
1008 "efivars: Sysfs attribute export failed with error %d.\n",
1009 error);
Dan Carpenter3116aab2011-03-18 10:12:38 +03001010 goto err_unregister;
Mike Waychison76b53f72011-03-11 17:43:16 -08001011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Dan Carpenter3116aab2011-03-18 10:12:38 +03001013 return 0;
1014
1015err_unregister:
1016 unregister_efivars(&__efivars);
1017err_put:
1018 kobject_put(efi_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return error;
1020}
1021
1022static void __exit
1023efivars_exit(void)
1024{
Randy Dunlapaabb6e12011-05-06 13:27:41 -07001025 if (efi_enabled) {
1026 unregister_efivars(&__efivars);
1027 kobject_put(efi_kobj);
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
1031module_init(efivars_init);
1032module_exit(efivars_exit);
1033