blob: b10b7d7bd7de8976a092efcd6957dd7c3aea9320 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82#include <asm/uaccess.h>
83
84#define EFIVARS_VERSION "0.08"
85#define EFIVARS_DATE "2004-May-17"
86
87MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
88MODULE_DESCRIPTION("sysfs interface to EFI Variables");
89MODULE_LICENSE("GPL");
90MODULE_VERSION(EFIVARS_VERSION);
91
Mike Waychison32958142011-03-11 17:43:21 -080092struct efivar_operations {
93 efi_get_variable_t *get_variable;
94 efi_get_next_variable_t *get_next_variable;
95 efi_set_variable_t *set_variable;
96};
97
Mike Waychison29422692011-03-11 17:43:00 -080098struct efivars {
99 /*
100 * ->lock protects two things:
101 * 1) ->list - adds, removals, reads, writes
Mike Waychison32958142011-03-11 17:43:21 -0800102 * 2) ops.[gs]et_variable() calls.
Mike Waychison29422692011-03-11 17:43:00 -0800103 * It must not be held when creating sysfs entries or calling kmalloc.
Mike Waychison32958142011-03-11 17:43:21 -0800104 * ops.get_next_variable() is only called from register_efivars(),
Mike Waychison29422692011-03-11 17:43:00 -0800105 * which is protected by the BKL, so that path is safe.
106 */
107 spinlock_t lock;
108 struct list_head list;
109 struct kset *kset;
Mike Waychisond502fbb2011-03-11 17:43:06 -0800110 struct bin_attribute *new_var, *del_var;
Mike Waychison32958142011-03-11 17:43:21 -0800111 const struct efivar_operations *ops;
Mike Waychison29422692011-03-11 17:43:00 -0800112};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/*
115 * The maximum size of VariableName + Data = 1024
116 * Therefore, it's reasonable to save that much
117 * space in each part of the structure,
118 * and we use a page for reading/writing.
119 */
120
121struct efi_variable {
122 efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
123 efi_guid_t VendorGuid;
124 unsigned long DataSize;
125 __u8 Data[1024];
126 efi_status_t Status;
127 __u32 Attributes;
128} __attribute__((packed));
129
130
131struct efivar_entry {
Mike Waychison4142ef12011-03-11 17:43:11 -0800132 struct efivars *efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct efi_variable var;
134 struct list_head list;
135 struct kobject kobj;
136};
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138struct efivar_attribute {
139 struct attribute attr;
140 ssize_t (*show) (struct efivar_entry *entry, char *buf);
141 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
142};
143
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define EFIVAR_ATTR(_name, _mode, _show, _store) \
146struct efivar_attribute efivar_attr_##_name = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900147 .attr = {.name = __stringify(_name), .mode = _mode}, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .show = _show, \
149 .store = _store, \
150};
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
153#define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
154
155/*
156 * Prototype for sysfs creation function
157 */
158static int
Mike Waychison4142ef12011-03-11 17:43:11 -0800159efivar_create_sysfs_entry(struct efivars *efivars,
160 unsigned long variable_name_size,
161 efi_char16_t *variable_name,
162 efi_guid_t *vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/* Return the number of unicode characters in data */
165static unsigned long
166utf8_strlen(efi_char16_t *data, unsigned long maxlength)
167{
168 unsigned long length = 0;
169
170 while (*data++ != 0 && length < maxlength)
171 length++;
172 return length;
173}
174
175/*
176 * Return the number of bytes is the length of this string
177 * Note: this is NOT the same as the number of unicode characters
178 */
179static inline unsigned long
180utf8_strsize(efi_char16_t *data, unsigned long maxlength)
181{
182 return utf8_strlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
183}
184
185static efi_status_t
Mike Waychison4142ef12011-03-11 17:43:11 -0800186get_var_data(struct efivars *efivars, struct efi_variable *var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 efi_status_t status;
189
Mike Waychison29422692011-03-11 17:43:00 -0800190 spin_lock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 var->DataSize = 1024;
Mike Waychison32958142011-03-11 17:43:21 -0800192 status = efivars->ops->get_variable(var->VariableName,
193 &var->VendorGuid,
194 &var->Attributes,
195 &var->DataSize,
196 var->Data);
Mike Waychison29422692011-03-11 17:43:00 -0800197 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (status != EFI_SUCCESS) {
199 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
200 status);
201 }
202 return status;
203}
204
205static ssize_t
206efivar_guid_read(struct efivar_entry *entry, char *buf)
207{
208 struct efi_variable *var = &entry->var;
209 char *str = buf;
210
211 if (!entry || !buf)
212 return 0;
213
214 efi_guid_unparse(&var->VendorGuid, str);
215 str += strlen(str);
216 str += sprintf(str, "\n");
217
218 return str - buf;
219}
220
221static ssize_t
222efivar_attr_read(struct efivar_entry *entry, char *buf)
223{
224 struct efi_variable *var = &entry->var;
225 char *str = buf;
226 efi_status_t status;
227
228 if (!entry || !buf)
229 return -EINVAL;
230
Mike Waychison4142ef12011-03-11 17:43:11 -0800231 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (status != EFI_SUCCESS)
233 return -EIO;
234
235 if (var->Attributes & 0x1)
236 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
237 if (var->Attributes & 0x2)
238 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
239 if (var->Attributes & 0x4)
240 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
241 return str - buf;
242}
243
244static ssize_t
245efivar_size_read(struct efivar_entry *entry, char *buf)
246{
247 struct efi_variable *var = &entry->var;
248 char *str = buf;
249 efi_status_t status;
250
251 if (!entry || !buf)
252 return -EINVAL;
253
Mike Waychison4142ef12011-03-11 17:43:11 -0800254 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (status != EFI_SUCCESS)
256 return -EIO;
257
258 str += sprintf(str, "0x%lx\n", var->DataSize);
259 return str - buf;
260}
261
262static ssize_t
263efivar_data_read(struct efivar_entry *entry, char *buf)
264{
265 struct efi_variable *var = &entry->var;
266 efi_status_t status;
267
268 if (!entry || !buf)
269 return -EINVAL;
270
Mike Waychison4142ef12011-03-11 17:43:11 -0800271 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (status != EFI_SUCCESS)
273 return -EIO;
274
275 memcpy(buf, var->Data, var->DataSize);
276 return var->DataSize;
277}
278/*
279 * We allow each variable to be edited via rewriting the
280 * entire efi variable structure.
281 */
282static ssize_t
283efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
284{
285 struct efi_variable *new_var, *var = &entry->var;
Mike Waychison4142ef12011-03-11 17:43:11 -0800286 struct efivars *efivars = entry->efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 efi_status_t status = EFI_NOT_FOUND;
288
289 if (count != sizeof(struct efi_variable))
290 return -EINVAL;
291
292 new_var = (struct efi_variable *)buf;
293 /*
294 * If only updating the variable data, then the name
295 * and guid should remain the same
296 */
297 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
298 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
299 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
300 return -EINVAL;
301 }
302
303 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
304 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
305 return -EINVAL;
306 }
307
Mike Waychison29422692011-03-11 17:43:00 -0800308 spin_lock(&efivars->lock);
Mike Waychison32958142011-03-11 17:43:21 -0800309 status = efivars->ops->set_variable(new_var->VariableName,
310 &new_var->VendorGuid,
311 new_var->Attributes,
312 new_var->DataSize,
313 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Mike Waychison29422692011-03-11 17:43:00 -0800315 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 if (status != EFI_SUCCESS) {
318 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
319 status);
320 return -EIO;
321 }
322
323 memcpy(&entry->var, new_var, count);
324 return count;
325}
326
327static ssize_t
328efivar_show_raw(struct efivar_entry *entry, char *buf)
329{
330 struct efi_variable *var = &entry->var;
331 efi_status_t status;
332
333 if (!entry || !buf)
334 return 0;
335
Mike Waychison4142ef12011-03-11 17:43:11 -0800336 status = get_var_data(entry->efivars, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (status != EFI_SUCCESS)
338 return -EIO;
339
340 memcpy(buf, var, sizeof(*var));
341 return sizeof(*var);
342}
343
344/*
345 * Generic read/write functions that call the specific functions of
346 * the atttributes...
347 */
348static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
349 char *buf)
350{
351 struct efivar_entry *var = to_efivar_entry(kobj);
352 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500353 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 if (!capable(CAP_SYS_ADMIN))
356 return -EACCES;
357
358 if (efivar_attr->show) {
359 ret = efivar_attr->show(var, buf);
360 }
361 return ret;
362}
363
364static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
365 const char *buf, size_t count)
366{
367 struct efivar_entry *var = to_efivar_entry(kobj);
368 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500369 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 if (!capable(CAP_SYS_ADMIN))
372 return -EACCES;
373
374 if (efivar_attr->store)
375 ret = efivar_attr->store(var, buf, count);
376
377 return ret;
378}
379
Emese Revfy52cf25d2010-01-19 02:58:23 +0100380static const struct sysfs_ops efivar_attr_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 .show = efivar_attr_show,
382 .store = efivar_attr_store,
383};
384
385static void efivar_release(struct kobject *kobj)
386{
387 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 kfree(var);
389}
390
391static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
392static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
393static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
394static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
395static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
396
397static struct attribute *def_attrs[] = {
398 &efivar_attr_guid.attr,
399 &efivar_attr_size.attr,
400 &efivar_attr_attributes.attr,
401 &efivar_attr_data.attr,
402 &efivar_attr_raw_var.attr,
403 NULL,
404};
405
Greg Kroah-Hartmane89a4112007-10-11 10:47:49 -0600406static struct kobj_type efivar_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 .release = efivar_release,
408 .sysfs_ops = &efivar_attr_ops,
409 .default_attrs = def_attrs,
410};
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412static inline void
413efivar_unregister(struct efivar_entry *var)
414{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800415 kobject_put(&var->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
418
Chris Wright2c3c8be2010-05-12 18:28:57 -0700419static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -0800420 struct bin_attribute *bin_attr,
421 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct efi_variable *new_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -0800424 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -0800425 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 efi_status_t status = EFI_NOT_FOUND;
428 int found = 0;
429
430 if (!capable(CAP_SYS_ADMIN))
431 return -EACCES;
432
Mike Waychison29422692011-03-11 17:43:00 -0800433 spin_lock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /*
436 * Does this variable already exist?
437 */
Mike Waychison29422692011-03-11 17:43:00 -0800438 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 strsize1 = utf8_strsize(search_efivar->var.VariableName, 1024);
440 strsize2 = utf8_strsize(new_var->VariableName, 1024);
441 if (strsize1 == strsize2 &&
442 !memcmp(&(search_efivar->var.VariableName),
443 new_var->VariableName, strsize1) &&
444 !efi_guidcmp(search_efivar->var.VendorGuid,
445 new_var->VendorGuid)) {
446 found = 1;
447 break;
448 }
449 }
450 if (found) {
Mike Waychison29422692011-03-11 17:43:00 -0800451 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return -EINVAL;
453 }
454
455 /* now *really* create the variable via EFI */
Mike Waychison32958142011-03-11 17:43:21 -0800456 status = efivars->ops->set_variable(new_var->VariableName,
457 &new_var->VendorGuid,
458 new_var->Attributes,
459 new_var->DataSize,
460 new_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 if (status != EFI_SUCCESS) {
463 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
464 status);
Mike Waychison29422692011-03-11 17:43:00 -0800465 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return -EIO;
467 }
Mike Waychison29422692011-03-11 17:43:00 -0800468 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 /* Create the entry in sysfs. Locking is not required here */
Mike Waychison4142ef12011-03-11 17:43:11 -0800471 status = efivar_create_sysfs_entry(efivars,
472 utf8_strsize(new_var->VariableName,
473 1024),
474 new_var->VariableName,
475 &new_var->VendorGuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (status) {
477 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
478 }
479 return count;
480}
481
Chris Wright2c3c8be2010-05-12 18:28:57 -0700482static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
Greg Kroah-Hartman97fa5bb2007-11-07 13:56:19 -0800483 struct bin_attribute *bin_attr,
484 char *buf, loff_t pos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct efi_variable *del_var = (struct efi_variable *)buf;
Mike Waychison4142ef12011-03-11 17:43:11 -0800487 struct efivars *efivars = bin_attr->private;
Matt Domsch496a0fc2007-01-26 00:57:18 -0800488 struct efivar_entry *search_efivar, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 unsigned long strsize1, strsize2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 efi_status_t status = EFI_NOT_FOUND;
491 int found = 0;
492
493 if (!capable(CAP_SYS_ADMIN))
494 return -EACCES;
495
Mike Waychison29422692011-03-11 17:43:00 -0800496 spin_lock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /*
499 * Does this variable already exist?
500 */
Mike Waychison29422692011-03-11 17:43:00 -0800501 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 strsize1 = utf8_strsize(search_efivar->var.VariableName, 1024);
503 strsize2 = utf8_strsize(del_var->VariableName, 1024);
504 if (strsize1 == strsize2 &&
505 !memcmp(&(search_efivar->var.VariableName),
506 del_var->VariableName, strsize1) &&
507 !efi_guidcmp(search_efivar->var.VendorGuid,
508 del_var->VendorGuid)) {
509 found = 1;
510 break;
511 }
512 }
513 if (!found) {
Mike Waychison29422692011-03-11 17:43:00 -0800514 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -EINVAL;
516 }
517 /* force the Attributes/DataSize to 0 to ensure deletion */
518 del_var->Attributes = 0;
519 del_var->DataSize = 0;
520
Mike Waychison32958142011-03-11 17:43:21 -0800521 status = efivars->ops->set_variable(del_var->VariableName,
522 &del_var->VendorGuid,
523 del_var->Attributes,
524 del_var->DataSize,
525 del_var->Data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 if (status != EFI_SUCCESS) {
528 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
529 status);
Mike Waychison29422692011-03-11 17:43:00 -0800530 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return -EIO;
532 }
Matt Domsch496a0fc2007-01-26 00:57:18 -0800533 list_del(&search_efivar->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* We need to release this lock before unregistering. */
Mike Waychison29422692011-03-11 17:43:00 -0800535 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 efivar_unregister(search_efivar);
537
538 /* It's dead Jim.... */
539 return count;
540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542/*
543 * Let's not leave out systab information that snuck into
544 * the efivars driver
545 */
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -0700546static ssize_t systab_show(struct kobject *kobj,
547 struct kobj_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 char *str = buf;
550
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -0700551 if (!kobj || !buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return -EINVAL;
553
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800554 if (efi.mps != EFI_INVALID_TABLE_ADDR)
555 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
556 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
557 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
558 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
559 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
560 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
561 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
562 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
563 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
564 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
565 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
566 if (efi.uga != EFI_INVALID_TABLE_ADDR)
567 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 return str - buf;
570}
571
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -0700572static struct kobj_attribute efi_attr_systab =
573 __ATTR(systab, 0400, systab_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -0700575static struct attribute *efi_subsys_attrs[] = {
576 &efi_attr_systab.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 NULL, /* maybe more in the future? */
578};
579
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -0700580static struct attribute_group efi_subsys_attr_group = {
581 .attrs = efi_subsys_attrs,
582};
583
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -0800584static struct kobject *efi_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586/*
587 * efivar_create_sysfs_entry()
588 * Requires:
589 * variable_name_size = number of bytes required to hold
590 * variable_name (not counting the NULL
591 * character at the end.
Mike Waychison29422692011-03-11 17:43:00 -0800592 * efivars->lock is not held on entry or exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 * Returns 1 on failure, 0 on success
594 */
595static int
Mike Waychison4142ef12011-03-11 17:43:11 -0800596efivar_create_sysfs_entry(struct efivars *efivars,
597 unsigned long variable_name_size,
598 efi_char16_t *variable_name,
599 efi_guid_t *vendor_guid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
602 char *short_name;
603 struct efivar_entry *new_efivar;
604
Deepak Saxena9c215382005-11-07 01:01:24 -0800605 short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
606 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 if (!short_name || !new_efivar) {
Jesper Juhl0933ad92005-06-25 14:59:15 -0700609 kfree(short_name);
610 kfree(new_efivar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return 1;
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Mike Waychison4142ef12011-03-11 17:43:11 -0800614 new_efivar->efivars = efivars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 memcpy(new_efivar->var.VariableName, variable_name,
616 variable_name_size);
617 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
618
619 /* Convert Unicode to normal chars (assume top bits are 0),
620 ala UTF-8 */
621 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
622 short_name[i] = variable_name[i] & 0xFF;
623 }
624 /* This is ugly, but necessary to separate one vendor's
625 private variables from another's. */
626
627 *(short_name + strlen(short_name)) = '-';
628 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
629
Mike Waychison29422692011-03-11 17:43:00 -0800630 new_efivar->kobj.kset = efivars->kset;
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -0400631 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
632 "%s", short_name);
Jeff Garzik69b21862006-10-11 01:22:23 -0700633 if (i) {
634 kfree(short_name);
635 kfree(new_efivar);
636 return 1;
637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Greg Kroah-Hartmand6d292c2007-12-17 15:54:39 -0400639 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
Jesper Juhl0933ad92005-06-25 14:59:15 -0700640 kfree(short_name);
641 short_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Mike Waychison29422692011-03-11 17:43:00 -0800643 spin_lock(&efivars->lock);
644 list_add(&new_efivar->list, &efivars->list);
645 spin_unlock(&efivars->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 return 0;
648}
Mike Waychisond502fbb2011-03-11 17:43:06 -0800649
650static int
651create_efivars_bin_attributes(struct efivars *efivars)
652{
653 struct bin_attribute *attr;
654 int error;
655
656 /* new_var */
657 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
658 if (!attr)
659 return -ENOMEM;
660
661 attr->attr.name = "new_var";
662 attr->attr.mode = 0200;
663 attr->write = efivar_create;
664 attr->private = efivars;
665 efivars->new_var = attr;
666
667 /* del_var */
668 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
669 if (!attr) {
670 error = -ENOMEM;
671 goto out_free;
672 }
673 attr->attr.name = "del_var";
674 attr->attr.mode = 0200;
675 attr->write = efivar_delete;
676 attr->private = efivars;
677 efivars->del_var = attr;
678
679 sysfs_bin_attr_init(efivars->new_var);
680 sysfs_bin_attr_init(efivars->del_var);
681
682 /* Register */
683 error = sysfs_create_bin_file(&efivars->kset->kobj,
684 efivars->new_var);
685 if (error) {
686 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
687 " due to error %d\n", error);
688 goto out_free;
689 }
690 error = sysfs_create_bin_file(&efivars->kset->kobj,
691 efivars->del_var);
692 if (error) {
693 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
694 " due to error %d\n", error);
695 sysfs_remove_bin_file(&efivars->kset->kobj,
696 efivars->new_var);
697 goto out_free;
698 }
699
700 return 0;
701out_free:
702 kfree(efivars->new_var);
703 efivars->new_var = NULL;
704 kfree(efivars->new_var);
705 efivars->new_var = NULL;
706 return error;
707}
708
Mike Waychison76b53f72011-03-11 17:43:16 -0800709static void unregister_efivars(struct efivars *efivars)
710{
711 struct efivar_entry *entry, *n;
Mike Waychison4142ef12011-03-11 17:43:11 -0800712
Mike Waychison76b53f72011-03-11 17:43:16 -0800713 list_for_each_entry_safe(entry, n, &efivars->list, list) {
714 spin_lock(&efivars->lock);
715 list_del(&entry->list);
716 spin_unlock(&efivars->lock);
717 efivar_unregister(entry);
718 }
719 if (efivars->new_var)
720 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
721 if (efivars->del_var)
722 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
723 kfree(efivars->new_var);
724 kfree(efivars->del_var);
725 kset_unregister(efivars->kset);
726}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Mike Waychison76b53f72011-03-11 17:43:16 -0800728static int register_efivars(struct efivars *efivars,
Mike Waychison32958142011-03-11 17:43:21 -0800729 const struct efivar_operations *ops,
Mike Waychison76b53f72011-03-11 17:43:16 -0800730 struct kobject *parent_kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 efi_status_t status = EFI_NOT_FOUND;
733 efi_guid_t vendor_guid;
734 efi_char16_t *variable_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 unsigned long variable_name_size = 1024;
Greg Kroah-Hartman334c6302007-11-02 13:20:40 -0700736 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Deepak Saxena9c215382005-11-07 01:01:24 -0800738 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (!variable_name) {
740 printk(KERN_ERR "efivars: Memory allocation failed.\n");
741 return -ENOMEM;
742 }
743
Mike Waychison29422692011-03-11 17:43:00 -0800744 spin_lock_init(&efivars->lock);
745 INIT_LIST_HEAD(&efivars->list);
Mike Waychison32958142011-03-11 17:43:21 -0800746 efivars->ops = ops;
Mike Waychison29422692011-03-11 17:43:00 -0800747
Mike Waychison76b53f72011-03-11 17:43:16 -0800748 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
Mike Waychison29422692011-03-11 17:43:00 -0800749 if (!efivars->kset) {
Greg Kroah-Hartman66ac8312007-11-02 13:20:40 -0700750 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
751 error = -ENOMEM;
Mike Waychison76b53f72011-03-11 17:43:16 -0800752 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
755 /*
756 * Per EFI spec, the maximum storage allocated for both
757 * the variable name and variable data is 1024 bytes.
758 */
759
760 do {
761 variable_name_size = 1024;
762
Mike Waychison32958142011-03-11 17:43:21 -0800763 status = ops->get_next_variable(&variable_name_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 variable_name,
765 &vendor_guid);
766 switch (status) {
767 case EFI_SUCCESS:
Mike Waychison4142ef12011-03-11 17:43:11 -0800768 efivar_create_sysfs_entry(efivars,
769 variable_name_size,
770 variable_name,
771 &vendor_guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 break;
773 case EFI_NOT_FOUND:
774 break;
775 default:
776 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
777 status);
778 status = EFI_NOT_FOUND;
779 break;
780 }
781 } while (status != EFI_NOT_FOUND);
782
Mike Waychisond502fbb2011-03-11 17:43:06 -0800783 error = create_efivars_bin_attributes(efivars);
Mike Waychison76b53f72011-03-11 17:43:16 -0800784 if (error)
785 unregister_efivars(efivars);
786
787out:
788 kfree(variable_name);
789
790 return error;
791}
792
793static struct efivars __efivars;
Mike Waychison32958142011-03-11 17:43:21 -0800794static struct efivar_operations ops;
Mike Waychison76b53f72011-03-11 17:43:16 -0800795
796/*
797 * For now we register the efi subsystem with the firmware subsystem
798 * and the vars subsystem with the efi subsystem. In the future, it
799 * might make sense to split off the efi subsystem into its own
800 * driver, but for now only efivars will register with it, so just
801 * include it here.
802 */
803
804static int __init
805efivars_init(void)
806{
807 int error = 0;
808
809 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
810 EFIVARS_DATE);
811
812 if (!efi_enabled)
813 return -ENODEV;
814
815 /* For now we'll register the efi directory at /sys/firmware/efi */
816 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
817 if (!efi_kobj) {
818 printk(KERN_ERR "efivars: Firmware registration failed.\n");
819 return -ENOMEM;
820 }
821
Mike Waychison32958142011-03-11 17:43:21 -0800822 ops.get_variable = efi.get_variable;
823 ops.set_variable = efi.set_variable;
824 ops.get_next_variable = efi.get_next_variable;
825 error = register_efivars(&__efivars, &ops, efi_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /* Don't forget the systab entry */
Greg Kroah-Hartmanbc87d2f2007-11-07 13:56:19 -0800828 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
Mike Waychison76b53f72011-03-11 17:43:16 -0800829 if (error) {
830 printk(KERN_ERR
831 "efivars: Sysfs attribute export failed with error %d.\n",
832 error);
833 unregister_efivars(&__efivars);
834 kobject_put(efi_kobj);
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 return error;
838}
839
840static void __exit
841efivars_exit(void)
842{
Mike Waychison76b53f72011-03-11 17:43:16 -0800843 unregister_efivars(&__efivars);
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800844 kobject_put(efi_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847module_init(efivars_init);
848module_exit(efivars_exit);
849