aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/firmware
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2011-03-16 18:05:40 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2011-03-16 18:05:40 -0400
commita5e6b135bdff649e4330f98e2e80dbb1984f7e77 (patch)
tree475bfb1163c59d1370fd77415255afba768f9520 /drivers/firmware
parent971f115a50afbe409825c9f3399d5a3b9aca4381 (diff)
parent9d90c8d9cde929cbc575098e825d7c29d9f45054 (diff)
Merge branch 'driver-core-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core-2.6
* 'driver-core-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core-2.6: (50 commits) printk: do not mangle valid userspace syslog prefixes efivars: Add Documentation efivars: Expose efivars functionality to external drivers. efivars: Parameterize operations. efivars: Split out variable registration efivars: parameterize efivars efivars: Make efivars bin_attributes dynamic efivars: move efivars globals into struct efivars drivers:misc: ti-st: fix debugging code kref: Fix typo in kref documentation UIO: add PRUSS UIO driver support Fix spelling mistakes in Documentation/zh_CN/SubmittingPatches firmware: Fix unaligned memory accesses in dmi-sysfs firmware: Add documentation for /sys/firmware/dmi firmware: Expose DMI type 15 System Event Log firmware: Break out system_event_log in dmi-sysfs firmware: Basic dmi-sysfs support firmware: Add DMI entry types to the headers Driver core: convert platform_{get,set}_drvdata to static inline functions Translate linux-2.6/Documentation/magic-number.txt into Chinese ...
Diffstat (limited to 'drivers/firmware')
-rw-r--r--drivers/firmware/Kconfig11
-rw-r--r--drivers/firmware/Makefile1
-rw-r--r--drivers/firmware/dmi-sysfs.c696
-rw-r--r--drivers/firmware/efivars.c343
4 files changed, 913 insertions, 138 deletions
diff --git a/drivers/firmware/Kconfig b/drivers/firmware/Kconfig
index e710424b59ea..3c56afc5eb1b 100644
--- a/drivers/firmware/Kconfig
+++ b/drivers/firmware/Kconfig
@@ -113,6 +113,17 @@ config DMIID
113 information from userspace through /sys/class/dmi/id/ or if you want 113 information from userspace through /sys/class/dmi/id/ or if you want
114 DMI-based module auto-loading. 114 DMI-based module auto-loading.
115 115
116config DMI_SYSFS
117 tristate "DMI table support in sysfs"
118 depends on SYSFS && DMI
119 default n
120 help
121 Say Y or M here to enable the exporting of the raw DMI table
122 data via sysfs. This is useful for consuming the data without
123 requiring any access to /dev/mem at all. Tables are found
124 under /sys/firmware/dmi when this option is enabled and
125 loaded.
126
116config ISCSI_IBFT_FIND 127config ISCSI_IBFT_FIND
117 bool "iSCSI Boot Firmware Table Attributes" 128 bool "iSCSI Boot Firmware Table Attributes"
118 depends on X86 129 depends on X86
diff --git a/drivers/firmware/Makefile b/drivers/firmware/Makefile
index 1c3c17343dbe..20c17fca1232 100644
--- a/drivers/firmware/Makefile
+++ b/drivers/firmware/Makefile
@@ -2,6 +2,7 @@
2# Makefile for the linux kernel. 2# Makefile for the linux kernel.
3# 3#
4obj-$(CONFIG_DMI) += dmi_scan.o 4obj-$(CONFIG_DMI) += dmi_scan.o
5obj-$(CONFIG_DMI_SYSFS) += dmi-sysfs.o
5obj-$(CONFIG_EDD) += edd.o 6obj-$(CONFIG_EDD) += edd.o
6obj-$(CONFIG_EFI_VARS) += efivars.o 7obj-$(CONFIG_EFI_VARS) += efivars.o
7obj-$(CONFIG_EFI_PCDP) += pcdp.o 8obj-$(CONFIG_EFI_PCDP) += pcdp.o
diff --git a/drivers/firmware/dmi-sysfs.c b/drivers/firmware/dmi-sysfs.c
new file mode 100644
index 000000000000..eb26d62e5188
--- /dev/null
+++ b/drivers/firmware/dmi-sysfs.c
@@ -0,0 +1,696 @@
1/*
2 * dmi-sysfs.c
3 *
4 * This module exports the DMI tables read-only to userspace through the
5 * sysfs file system.
6 *
7 * Data is currently found below
8 * /sys/firmware/dmi/...
9 *
10 * DMI attributes are presented in attribute files with names
11 * formatted using %d-%d, so that the first integer indicates the
12 * structure type (0-255), and the second field is the instance of that
13 * entry.
14 *
15 * Copyright 2011 Google, Inc.
16 */
17
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/kobject.h>
23#include <linux/dmi.h>
24#include <linux/capability.h>
25#include <linux/slab.h>
26#include <linux/list.h>
27#include <linux/io.h>
28
29#define MAX_ENTRY_TYPE 255 /* Most of these aren't used, but we consider
30 the top entry type is only 8 bits */
31
32struct dmi_sysfs_entry {
33 struct dmi_header dh;
34 struct kobject kobj;
35 int instance;
36 int position;
37 struct list_head list;
38 struct kobject *child;
39};
40
41/*
42 * Global list of dmi_sysfs_entry. Even though this should only be
43 * manipulated at setup and teardown, the lazy nature of the kobject
44 * system means we get lazy removes.
45 */
46static LIST_HEAD(entry_list);
47static DEFINE_SPINLOCK(entry_list_lock);
48
49/* dmi_sysfs_attribute - Top level attribute. used by all entries. */
50struct dmi_sysfs_attribute {
51 struct attribute attr;
52 ssize_t (*show)(struct dmi_sysfs_entry *entry, char *buf);
53};
54
55#define DMI_SYSFS_ATTR(_entry, _name) \
56struct dmi_sysfs_attribute dmi_sysfs_attr_##_entry##_##_name = { \
57 .attr = {.name = __stringify(_name), .mode = 0400}, \
58 .show = dmi_sysfs_##_entry##_##_name, \
59}
60
61/*
62 * dmi_sysfs_mapped_attribute - Attribute where we require the entry be
63 * mapped in. Use in conjunction with dmi_sysfs_specialize_attr_ops.
64 */
65struct dmi_sysfs_mapped_attribute {
66 struct attribute attr;
67 ssize_t (*show)(struct dmi_sysfs_entry *entry,
68 const struct dmi_header *dh,
69 char *buf);
70};
71
72#define DMI_SYSFS_MAPPED_ATTR(_entry, _name) \
73struct dmi_sysfs_mapped_attribute dmi_sysfs_attr_##_entry##_##_name = { \
74 .attr = {.name = __stringify(_name), .mode = 0400}, \
75 .show = dmi_sysfs_##_entry##_##_name, \
76}
77
78/*************************************************
79 * Generic DMI entry support.
80 *************************************************/
81static void dmi_entry_free(struct kobject *kobj)
82{
83 kfree(kobj);
84}
85
86static struct dmi_sysfs_entry *to_entry(struct kobject *kobj)
87{
88 return container_of(kobj, struct dmi_sysfs_entry, kobj);
89}
90
91static struct dmi_sysfs_attribute *to_attr(struct attribute *attr)
92{
93 return container_of(attr, struct dmi_sysfs_attribute, attr);
94}
95
96static ssize_t dmi_sysfs_attr_show(struct kobject *kobj,
97 struct attribute *_attr, char *buf)
98{
99 struct dmi_sysfs_entry *entry = to_entry(kobj);
100 struct dmi_sysfs_attribute *attr = to_attr(_attr);
101
102 /* DMI stuff is only ever admin visible */
103 if (!capable(CAP_SYS_ADMIN))
104 return -EACCES;
105
106 return attr->show(entry, buf);
107}
108
109static const struct sysfs_ops dmi_sysfs_attr_ops = {
110 .show = dmi_sysfs_attr_show,
111};
112
113typedef ssize_t (*dmi_callback)(struct dmi_sysfs_entry *,
114 const struct dmi_header *dh, void *);
115
116struct find_dmi_data {
117 struct dmi_sysfs_entry *entry;
118 dmi_callback callback;
119 void *private;
120 int instance_countdown;
121 ssize_t ret;
122};
123
124static void find_dmi_entry_helper(const struct dmi_header *dh,
125 void *_data)
126{
127 struct find_dmi_data *data = _data;
128 struct dmi_sysfs_entry *entry = data->entry;
129
130 /* Is this the entry we want? */
131 if (dh->type != entry->dh.type)
132 return;
133
134 if (data->instance_countdown != 0) {
135 /* try the next instance? */
136 data->instance_countdown--;
137 return;
138 }
139
140 /*
141 * Don't ever revisit the instance. Short circuit later
142 * instances by letting the instance_countdown run negative
143 */
144 data->instance_countdown--;
145
146 /* Found the entry */
147 data->ret = data->callback(entry, dh, data->private);
148}
149
150/* State for passing the read parameters through dmi_find_entry() */
151struct dmi_read_state {
152 char *buf;
153 loff_t pos;
154 size_t count;
155};
156
157static ssize_t find_dmi_entry(struct dmi_sysfs_entry *entry,
158 dmi_callback callback, void *private)
159{
160 struct find_dmi_data data = {
161 .entry = entry,
162 .callback = callback,
163 .private = private,
164 .instance_countdown = entry->instance,
165 .ret = -EIO, /* To signal the entry disappeared */
166 };
167 int ret;
168
169 ret = dmi_walk(find_dmi_entry_helper, &data);
170 /* This shouldn't happen, but just in case. */
171 if (ret)
172 return -EINVAL;
173 return data.ret;
174}
175
176/*
177 * Calculate and return the byte length of the dmi entry identified by
178 * dh. This includes both the formatted portion as well as the
179 * unformatted string space, including the two trailing nul characters.
180 */
181static size_t dmi_entry_length(const struct dmi_header *dh)
182{
183 const char *p = (const char *)dh;
184
185 p += dh->length;
186
187 while (p[0] || p[1])
188 p++;
189
190 return 2 + p - (const char *)dh;
191}
192
193/*************************************************
194 * Support bits for specialized DMI entry support
195 *************************************************/
196struct dmi_entry_attr_show_data {
197 struct attribute *attr;
198 char *buf;
199};
200
201static ssize_t dmi_entry_attr_show_helper(struct dmi_sysfs_entry *entry,
202 const struct dmi_header *dh,
203 void *_data)
204{
205 struct dmi_entry_attr_show_data *data = _data;
206 struct dmi_sysfs_mapped_attribute *attr;
207
208 attr = container_of(data->attr,
209 struct dmi_sysfs_mapped_attribute, attr);
210 return attr->show(entry, dh, data->buf);
211}
212
213static ssize_t dmi_entry_attr_show(struct kobject *kobj,
214 struct attribute *attr,
215 char *buf)
216{
217 struct dmi_entry_attr_show_data data = {
218 .attr = attr,
219 .buf = buf,
220 };
221 /* Find the entry according to our parent and call the
222 * normalized show method hanging off of the attribute */
223 return find_dmi_entry(to_entry(kobj->parent),
224 dmi_entry_attr_show_helper, &data);
225}
226
227static const struct sysfs_ops dmi_sysfs_specialize_attr_ops = {
228 .show = dmi_entry_attr_show,
229};
230
231/*************************************************
232 * Specialized DMI entry support.
233 *************************************************/
234
235/*** Type 15 - System Event Table ***/
236
237#define DMI_SEL_ACCESS_METHOD_IO8 0x00
238#define DMI_SEL_ACCESS_METHOD_IO2x8 0x01
239#define DMI_SEL_ACCESS_METHOD_IO16 0x02
240#define DMI_SEL_ACCESS_METHOD_PHYS32 0x03
241#define DMI_SEL_ACCESS_METHOD_GPNV 0x04
242
243struct dmi_system_event_log {
244 struct dmi_header header;
245 u16 area_length;
246 u16 header_start_offset;
247 u16 data_start_offset;
248 u8 access_method;
249 u8 status;
250 u32 change_token;
251 union {
252 struct {
253 u16 index_addr;
254 u16 data_addr;
255 } io;
256 u32 phys_addr32;
257 u16 gpnv_handle;
258 u32 access_method_address;
259 };
260 u8 header_format;
261 u8 type_descriptors_supported_count;
262 u8 per_log_type_descriptor_length;
263 u8 supported_log_type_descriptos[0];
264} __packed;
265
266#define DMI_SYSFS_SEL_FIELD(_field) \
267static ssize_t dmi_sysfs_sel_##_field(struct dmi_sysfs_entry *entry, \
268 const struct dmi_header *dh, \
269 char *buf) \
270{ \
271 struct dmi_system_event_log sel; \
272 if (sizeof(sel) > dmi_entry_length(dh)) \
273 return -EIO; \
274 memcpy(&sel, dh, sizeof(sel)); \
275 return sprintf(buf, "%u\n", sel._field); \
276} \
277static DMI_SYSFS_MAPPED_ATTR(sel, _field)
278
279DMI_SYSFS_SEL_FIELD(area_length);
280DMI_SYSFS_SEL_FIELD(header_start_offset);
281DMI_SYSFS_SEL_FIELD(data_start_offset);
282DMI_SYSFS_SEL_FIELD(access_method);
283DMI_SYSFS_SEL_FIELD(status);
284DMI_SYSFS_SEL_FIELD(change_token);
285DMI_SYSFS_SEL_FIELD(access_method_address);
286DMI_SYSFS_SEL_FIELD(header_format);
287DMI_SYSFS_SEL_FIELD(type_descriptors_supported_count);
288DMI_SYSFS_SEL_FIELD(per_log_type_descriptor_length);
289
290static struct attribute *dmi_sysfs_sel_attrs[] = {
291 &dmi_sysfs_attr_sel_area_length.attr,
292 &dmi_sysfs_attr_sel_header_start_offset.attr,
293 &dmi_sysfs_attr_sel_data_start_offset.attr,
294 &dmi_sysfs_attr_sel_access_method.attr,
295 &dmi_sysfs_attr_sel_status.attr,
296 &dmi_sysfs_attr_sel_change_token.attr,
297 &dmi_sysfs_attr_sel_access_method_address.attr,
298 &dmi_sysfs_attr_sel_header_format.attr,
299 &dmi_sysfs_attr_sel_type_descriptors_supported_count.attr,
300 &dmi_sysfs_attr_sel_per_log_type_descriptor_length.attr,
301 NULL,
302};
303
304
305static struct kobj_type dmi_system_event_log_ktype = {
306 .release = dmi_entry_free,
307 .sysfs_ops = &dmi_sysfs_specialize_attr_ops,
308 .default_attrs = dmi_sysfs_sel_attrs,
309};
310
311typedef u8 (*sel_io_reader)(const struct dmi_system_event_log *sel,
312 loff_t offset);
313
314static DEFINE_MUTEX(io_port_lock);
315
316static u8 read_sel_8bit_indexed_io(const struct dmi_system_event_log *sel,
317 loff_t offset)
318{
319 u8 ret;
320
321 mutex_lock(&io_port_lock);
322 outb((u8)offset, sel->io.index_addr);
323 ret = inb(sel->io.data_addr);
324 mutex_unlock(&io_port_lock);
325 return ret;
326}
327
328static u8 read_sel_2x8bit_indexed_io(const struct dmi_system_event_log *sel,
329 loff_t offset)
330{
331 u8 ret;
332
333 mutex_lock(&io_port_lock);
334 outb((u8)offset, sel->io.index_addr);
335 outb((u8)(offset >> 8), sel->io.index_addr + 1);
336 ret = inb(sel->io.data_addr);
337 mutex_unlock(&io_port_lock);
338 return ret;
339}
340
341static u8 read_sel_16bit_indexed_io(const struct dmi_system_event_log *sel,
342 loff_t offset)
343{
344 u8 ret;
345
346 mutex_lock(&io_port_lock);
347 outw((u16)offset, sel->io.index_addr);
348 ret = inb(sel->io.data_addr);
349 mutex_unlock(&io_port_lock);
350 return ret;
351}
352
353static sel_io_reader sel_io_readers[] = {
354 [DMI_SEL_ACCESS_METHOD_IO8] = read_sel_8bit_indexed_io,
355 [DMI_SEL_ACCESS_METHOD_IO2x8] = read_sel_2x8bit_indexed_io,
356 [DMI_SEL_ACCESS_METHOD_IO16] = read_sel_16bit_indexed_io,
357};
358
359static ssize_t dmi_sel_raw_read_io(struct dmi_sysfs_entry *entry,
360 const struct dmi_system_event_log *sel,
361 char *buf, loff_t pos, size_t count)
362{
363 ssize_t wrote = 0;
364
365 sel_io_reader io_reader = sel_io_readers[sel->access_method];
366
367 while (count && pos < sel->area_length) {
368 count--;
369 *(buf++) = io_reader(sel, pos++);
370 wrote++;
371 }
372
373 return wrote;
374}
375
376static ssize_t dmi_sel_raw_read_phys32(struct dmi_sysfs_entry *entry,
377 const struct dmi_system_event_log *sel,
378 char *buf, loff_t pos, size_t count)
379{
380 u8 __iomem *mapped;
381 ssize_t wrote = 0;
382
383 mapped = ioremap(sel->access_method_address, sel->area_length);
384 if (!mapped)
385 return -EIO;
386
387 while (count && pos < sel->area_length) {
388 count--;
389 *(buf++) = readb(mapped + pos++);
390 wrote++;
391 }
392
393 iounmap(mapped);
394 return wrote;
395}
396
397static ssize_t dmi_sel_raw_read_helper(struct dmi_sysfs_entry *entry,
398 const struct dmi_header *dh,
399 void *_state)
400{
401 struct dmi_read_state *state = _state;
402 struct dmi_system_event_log sel;
403
404 if (sizeof(sel) > dmi_entry_length(dh))
405 return -EIO;
406
407 memcpy(&sel, dh, sizeof(sel));
408
409 switch (sel.access_method) {
410 case DMI_SEL_ACCESS_METHOD_IO8:
411 case DMI_SEL_ACCESS_METHOD_IO2x8:
412 case DMI_SEL_ACCESS_METHOD_IO16:
413 return dmi_sel_raw_read_io(entry, &sel, state->buf,
414 state->pos, state->count);
415 case DMI_SEL_ACCESS_METHOD_PHYS32:
416 return dmi_sel_raw_read_phys32(entry, &sel, state->buf,
417 state->pos, state->count);
418 case DMI_SEL_ACCESS_METHOD_GPNV:
419 pr_info("dmi-sysfs: GPNV support missing.\n");
420 return -EIO;
421 default:
422 pr_info("dmi-sysfs: Unknown access method %02x\n",
423 sel.access_method);
424 return -EIO;
425 }
426}
427
428static ssize_t dmi_sel_raw_read(struct file *filp, struct kobject *kobj,
429 struct bin_attribute *bin_attr,
430 char *buf, loff_t pos, size_t count)
431{
432 struct dmi_sysfs_entry *entry = to_entry(kobj->parent);
433 struct dmi_read_state state = {
434 .buf = buf,
435 .pos = pos,
436 .count = count,
437 };
438
439 return find_dmi_entry(entry, dmi_sel_raw_read_helper, &state);
440}
441
442static struct bin_attribute dmi_sel_raw_attr = {
443 .attr = {.name = "raw_event_log", .mode = 0400},
444 .read = dmi_sel_raw_read,
445};
446
447static int dmi_system_event_log(struct dmi_sysfs_entry *entry)
448{
449 int ret;
450
451 entry->child = kzalloc(sizeof(*entry->child), GFP_KERNEL);
452 if (!entry->child)
453 return -ENOMEM;
454 ret = kobject_init_and_add(entry->child,
455 &dmi_system_event_log_ktype,
456 &entry->kobj,
457 "system_event_log");
458 if (ret)
459 goto out_free;
460
461 ret = sysfs_create_bin_file(entry->child, &dmi_sel_raw_attr);
462 if (ret)
463 goto out_del;
464
465 return 0;
466
467out_del:
468 kobject_del(entry->child);
469out_free:
470 kfree(entry->child);
471 return ret;
472}
473
474/*************************************************
475 * Generic DMI entry support.
476 *************************************************/
477
478static ssize_t dmi_sysfs_entry_length(struct dmi_sysfs_entry *entry, char *buf)
479{
480 return sprintf(buf, "%d\n", entry->dh.length);
481}
482
483static ssize_t dmi_sysfs_entry_handle(struct dmi_sysfs_entry *entry, char *buf)
484{
485 return sprintf(buf, "%d\n", entry->dh.handle);
486}
487
488static ssize_t dmi_sysfs_entry_type(struct dmi_sysfs_entry *entry, char *buf)
489{
490 return sprintf(buf, "%d\n", entry->dh.type);
491}
492
493static ssize_t dmi_sysfs_entry_instance(struct dmi_sysfs_entry *entry,
494 char *buf)
495{
496 return sprintf(buf, "%d\n", entry->instance);
497}
498
499static ssize_t dmi_sysfs_entry_position(struct dmi_sysfs_entry *entry,
500 char *buf)
501{
502 return sprintf(buf, "%d\n", entry->position);
503}
504
505static DMI_SYSFS_ATTR(entry, length);
506static DMI_SYSFS_ATTR(entry, handle);
507static DMI_SYSFS_ATTR(entry, type);
508static DMI_SYSFS_ATTR(entry, instance);
509static DMI_SYSFS_ATTR(entry, position);
510
511static struct attribute *dmi_sysfs_entry_attrs[] = {
512 &dmi_sysfs_attr_entry_length.attr,
513 &dmi_sysfs_attr_entry_handle.attr,
514 &dmi_sysfs_attr_entry_type.attr,
515 &dmi_sysfs_attr_entry_instance.attr,
516 &dmi_sysfs_attr_entry_position.attr,
517 NULL,
518};
519
520static ssize_t dmi_entry_raw_read_helper(struct dmi_sysfs_entry *entry,
521 const struct dmi_header *dh,
522 void *_state)
523{
524 struct dmi_read_state *state = _state;
525 size_t entry_length;
526
527 entry_length = dmi_entry_length(dh);
528
529 return memory_read_from_buffer(state->buf, state->count,
530 &state->pos, dh, entry_length);
531}
532
533static ssize_t dmi_entry_raw_read(struct file *filp,
534 struct kobject *kobj,
535 struct bin_attribute *bin_attr,
536 char *buf, loff_t pos, size_t count)
537{
538 struct dmi_sysfs_entry *entry = to_entry(kobj);
539 struct dmi_read_state state = {
540 .buf = buf,
541 .pos = pos,
542 .count = count,
543 };
544
545 return find_dmi_entry(entry, dmi_entry_raw_read_helper, &state);
546}
547
548static const struct bin_attribute dmi_entry_raw_attr = {
549 .attr = {.name = "raw", .mode = 0400},
550 .read = dmi_entry_raw_read,
551};
552
553static void dmi_sysfs_entry_release(struct kobject *kobj)
554{
555 struct dmi_sysfs_entry *entry = to_entry(kobj);
556 sysfs_remove_bin_file(&entry->kobj, &dmi_entry_raw_attr);
557 spin_lock(&entry_list_lock);
558 list_del(&entry->list);
559 spin_unlock(&entry_list_lock);
560 kfree(entry);
561}
562
563static struct kobj_type dmi_sysfs_entry_ktype = {
564 .release = dmi_sysfs_entry_release,
565 .sysfs_ops = &dmi_sysfs_attr_ops,
566 .default_attrs = dmi_sysfs_entry_attrs,
567};
568
569static struct kobject *dmi_kobj;
570static struct kset *dmi_kset;
571
572/* Global count of all instances seen. Only for setup */
573static int __initdata instance_counts[MAX_ENTRY_TYPE + 1];
574
575/* Global positional count of all entries seen. Only for setup */
576static int __initdata position_count;
577
578static void __init dmi_sysfs_register_handle(const struct dmi_header *dh,
579 void *_ret)
580{
581 struct dmi_sysfs_entry *entry;
582 int *ret = _ret;
583
584 /* If a previous entry saw an error, short circuit */
585 if (*ret)
586 return;
587
588 /* Allocate and register a new entry into the entries set */
589 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
590 if (!entry) {
591 *ret = -ENOMEM;
592 return;
593 }
594
595 /* Set the key */
596 memcpy(&entry->dh, dh, sizeof(*dh));
597 entry->instance = instance_counts[dh->type]++;
598 entry->position = position_count++;
599
600 entry->kobj.kset = dmi_kset;
601 *ret = kobject_init_and_add(&entry->kobj, &dmi_sysfs_entry_ktype, NULL,
602 "%d-%d", dh->type, entry->instance);
603
604 if (*ret) {
605 kfree(entry);
606 return;
607 }
608
609 /* Thread on the global list for cleanup */
610 spin_lock(&entry_list_lock);
611 list_add_tail(&entry->list, &entry_list);
612 spin_unlock(&entry_list_lock);
613
614 /* Handle specializations by type */
615 switch (dh->type) {
616 case DMI_ENTRY_SYSTEM_EVENT_LOG:
617 *ret = dmi_system_event_log(entry);
618 break;
619 default:
620 /* No specialization */
621 break;
622 }
623 if (*ret)
624 goto out_err;
625
626 /* Create the raw binary file to access the entry */
627 *ret = sysfs_create_bin_file(&entry->kobj, &dmi_entry_raw_attr);
628 if (*ret)
629 goto out_err;
630
631 return;
632out_err:
633 kobject_put(entry->child);
634 kobject_put(&entry->kobj);
635 return;
636}
637
638static void cleanup_entry_list(void)
639{
640 struct dmi_sysfs_entry *entry, *next;
641
642 /* No locks, we are on our way out */
643 list_for_each_entry_safe(entry, next, &entry_list, list) {
644 kobject_put(entry->child);
645 kobject_put(&entry->kobj);
646 }
647}
648
649static int __init dmi_sysfs_init(void)
650{
651 int error = -ENOMEM;
652 int val;
653
654 /* Set up our directory */
655 dmi_kobj = kobject_create_and_add("dmi", firmware_kobj);
656 if (!dmi_kobj)
657 goto err;
658
659 dmi_kset = kset_create_and_add("entries", NULL, dmi_kobj);
660 if (!dmi_kset)
661 goto err;
662
663 val = 0;
664 error = dmi_walk(dmi_sysfs_register_handle, &val);
665 if (error)
666 goto err;
667 if (val) {
668 error = val;
669 goto err;
670 }
671
672 pr_debug("dmi-sysfs: loaded.\n");
673
674 return 0;
675err:
676 cleanup_entry_list();
677 kset_unregister(dmi_kset);
678 kobject_put(dmi_kobj);
679 return error;
680}
681
682/* clean up everything. */
683static void __exit dmi_sysfs_exit(void)
684{
685 pr_debug("dmi-sysfs: unloading.\n");
686 cleanup_entry_list();
687 kset_unregister(dmi_kset);
688 kobject_put(dmi_kobj);
689}
690
691module_init(dmi_sysfs_init);
692module_exit(dmi_sysfs_exit);
693
694MODULE_AUTHOR("Mike Waychison <mikew@google.com>");
695MODULE_DESCRIPTION("DMI sysfs support");
696MODULE_LICENSE("GPL");
diff --git a/drivers/firmware/efivars.c b/drivers/firmware/efivars.c
index 2a62ec6390e0..ff0c373e3bbf 100644
--- a/drivers/firmware/efivars.c
+++ b/drivers/firmware/efivars.c
@@ -90,17 +90,6 @@ MODULE_LICENSE("GPL");
90MODULE_VERSION(EFIVARS_VERSION); 90MODULE_VERSION(EFIVARS_VERSION);
91 91
92/* 92/*
93 * efivars_lock protects two things:
94 * 1) efivar_list - adds, removals, reads, writes
95 * 2) efi.[gs]et_variable() calls.
96 * It must not be held when creating sysfs entries or calling kmalloc.
97 * efi.get_next_variable() is only called from efivars_init(),
98 * which is protected by the BKL, so that path is safe.
99 */
100static DEFINE_SPINLOCK(efivars_lock);
101static LIST_HEAD(efivar_list);
102
103/*
104 * The maximum size of VariableName + Data = 1024 93 * The maximum size of VariableName + Data = 1024
105 * Therefore, it's reasonable to save that much 94 * Therefore, it's reasonable to save that much
106 * space in each part of the structure, 95 * space in each part of the structure,
@@ -118,6 +107,7 @@ struct efi_variable {
118 107
119 108
120struct efivar_entry { 109struct efivar_entry {
110 struct efivars *efivars;
121 struct efi_variable var; 111 struct efi_variable var;
122 struct list_head list; 112 struct list_head list;
123 struct kobject kobj; 113 struct kobject kobj;
@@ -144,9 +134,10 @@ struct efivar_attribute efivar_attr_##_name = { \
144 * Prototype for sysfs creation function 134 * Prototype for sysfs creation function
145 */ 135 */
146static int 136static int
147efivar_create_sysfs_entry(unsigned long variable_name_size, 137efivar_create_sysfs_entry(struct efivars *efivars,
148 efi_char16_t *variable_name, 138 unsigned long variable_name_size,
149 efi_guid_t *vendor_guid); 139 efi_char16_t *variable_name,
140 efi_guid_t *vendor_guid);
150 141
151/* Return the number of unicode characters in data */ 142/* Return the number of unicode characters in data */
152static unsigned long 143static unsigned long
@@ -170,18 +161,18 @@ utf8_strsize(efi_char16_t *data, unsigned long maxlength)
170} 161}
171 162
172static efi_status_t 163static efi_status_t
173get_var_data(struct efi_variable *var) 164get_var_data(struct efivars *efivars, struct efi_variable *var)
174{ 165{
175 efi_status_t status; 166 efi_status_t status;
176 167
177 spin_lock(&efivars_lock); 168 spin_lock(&efivars->lock);
178 var->DataSize = 1024; 169 var->DataSize = 1024;
179 status = efi.get_variable(var->VariableName, 170 status = efivars->ops->get_variable(var->VariableName,
180 &var->VendorGuid, 171 &var->VendorGuid,
181 &var->Attributes, 172 &var->Attributes,
182 &var->DataSize, 173 &var->DataSize,
183 var->Data); 174 var->Data);
184 spin_unlock(&efivars_lock); 175 spin_unlock(&efivars->lock);
185 if (status != EFI_SUCCESS) { 176 if (status != EFI_SUCCESS) {
186 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n", 177 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
187 status); 178 status);
@@ -215,7 +206,7 @@ efivar_attr_read(struct efivar_entry *entry, char *buf)
215 if (!entry || !buf) 206 if (!entry || !buf)
216 return -EINVAL; 207 return -EINVAL;
217 208
218 status = get_var_data(var); 209 status = get_var_data(entry->efivars, var);
219 if (status != EFI_SUCCESS) 210 if (status != EFI_SUCCESS)
220 return -EIO; 211 return -EIO;
221 212
@@ -238,7 +229,7 @@ efivar_size_read(struct efivar_entry *entry, char *buf)
238 if (!entry || !buf) 229 if (!entry || !buf)
239 return -EINVAL; 230 return -EINVAL;
240 231
241 status = get_var_data(var); 232 status = get_var_data(entry->efivars, var);
242 if (status != EFI_SUCCESS) 233 if (status != EFI_SUCCESS)
243 return -EIO; 234 return -EIO;
244 235
@@ -255,7 +246,7 @@ efivar_data_read(struct efivar_entry *entry, char *buf)
255 if (!entry || !buf) 246 if (!entry || !buf)
256 return -EINVAL; 247 return -EINVAL;
257 248
258 status = get_var_data(var); 249 status = get_var_data(entry->efivars, var);
259 if (status != EFI_SUCCESS) 250 if (status != EFI_SUCCESS)
260 return -EIO; 251 return -EIO;
261 252
@@ -270,6 +261,7 @@ static ssize_t
270efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count) 261efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
271{ 262{
272 struct efi_variable *new_var, *var = &entry->var; 263 struct efi_variable *new_var, *var = &entry->var;
264 struct efivars *efivars = entry->efivars;
273 efi_status_t status = EFI_NOT_FOUND; 265 efi_status_t status = EFI_NOT_FOUND;
274 266
275 if (count != sizeof(struct efi_variable)) 267 if (count != sizeof(struct efi_variable))
@@ -291,14 +283,14 @@ efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
291 return -EINVAL; 283 return -EINVAL;
292 } 284 }
293 285
294 spin_lock(&efivars_lock); 286 spin_lock(&efivars->lock);
295 status = efi.set_variable(new_var->VariableName, 287 status = efivars->ops->set_variable(new_var->VariableName,
296 &new_var->VendorGuid, 288 &new_var->VendorGuid,
297 new_var->Attributes, 289 new_var->Attributes,
298 new_var->DataSize, 290 new_var->DataSize,
299 new_var->Data); 291 new_var->Data);
300 292
301 spin_unlock(&efivars_lock); 293 spin_unlock(&efivars->lock);
302 294
303 if (status != EFI_SUCCESS) { 295 if (status != EFI_SUCCESS) {
304 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n", 296 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
@@ -319,7 +311,7 @@ efivar_show_raw(struct efivar_entry *entry, char *buf)
319 if (!entry || !buf) 311 if (!entry || !buf)
320 return 0; 312 return 0;
321 313
322 status = get_var_data(var); 314 status = get_var_data(entry->efivars, var);
323 if (status != EFI_SUCCESS) 315 if (status != EFI_SUCCESS)
324 return -EIO; 316 return -EIO;
325 317
@@ -407,6 +399,7 @@ static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
407 char *buf, loff_t pos, size_t count) 399 char *buf, loff_t pos, size_t count)
408{ 400{
409 struct efi_variable *new_var = (struct efi_variable *)buf; 401 struct efi_variable *new_var = (struct efi_variable *)buf;
402 struct efivars *efivars = bin_attr->private;
410 struct efivar_entry *search_efivar, *n; 403 struct efivar_entry *search_efivar, *n;
411 unsigned long strsize1, strsize2; 404 unsigned long strsize1, strsize2;
412 efi_status_t status = EFI_NOT_FOUND; 405 efi_status_t status = EFI_NOT_FOUND;
@@ -415,12 +408,12 @@ static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
415 if (!capable(CAP_SYS_ADMIN)) 408 if (!capable(CAP_SYS_ADMIN))
416 return -EACCES; 409 return -EACCES;
417 410
418 spin_lock(&efivars_lock); 411 spin_lock(&efivars->lock);
419 412
420 /* 413 /*
421 * Does this variable already exist? 414 * Does this variable already exist?
422 */ 415 */
423 list_for_each_entry_safe(search_efivar, n, &efivar_list, list) { 416 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
424 strsize1 = utf8_strsize(search_efivar->var.VariableName, 1024); 417 strsize1 = utf8_strsize(search_efivar->var.VariableName, 1024);
425 strsize2 = utf8_strsize(new_var->VariableName, 1024); 418 strsize2 = utf8_strsize(new_var->VariableName, 1024);
426 if (strsize1 == strsize2 && 419 if (strsize1 == strsize2 &&
@@ -433,28 +426,31 @@ static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
433 } 426 }
434 } 427 }
435 if (found) { 428 if (found) {
436 spin_unlock(&efivars_lock); 429 spin_unlock(&efivars->lock);
437 return -EINVAL; 430 return -EINVAL;
438 } 431 }
439 432
440 /* now *really* create the variable via EFI */ 433 /* now *really* create the variable via EFI */
441 status = efi.set_variable(new_var->VariableName, 434 status = efivars->ops->set_variable(new_var->VariableName,
442 &new_var->VendorGuid, 435 &new_var->VendorGuid,
443 new_var->Attributes, 436 new_var->Attributes,
444 new_var->DataSize, 437 new_var->DataSize,
445 new_var->Data); 438 new_var->Data);
446 439
447 if (status != EFI_SUCCESS) { 440 if (status != EFI_SUCCESS) {
448 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n", 441 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
449 status); 442 status);
450 spin_unlock(&efivars_lock); 443 spin_unlock(&efivars->lock);
451 return -EIO; 444 return -EIO;
452 } 445 }
453 spin_unlock(&efivars_lock); 446 spin_unlock(&efivars->lock);
454 447
455 /* Create the entry in sysfs. Locking is not required here */ 448 /* Create the entry in sysfs. Locking is not required here */
456 status = efivar_create_sysfs_entry(utf8_strsize(new_var->VariableName, 449 status = efivar_create_sysfs_entry(efivars,
457 1024), new_var->VariableName, &new_var->VendorGuid); 450 utf8_strsize(new_var->VariableName,
451 1024),
452 new_var->VariableName,
453 &new_var->VendorGuid);
458 if (status) { 454 if (status) {
459 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n"); 455 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
460 } 456 }
@@ -466,6 +462,7 @@ static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
466 char *buf, loff_t pos, size_t count) 462 char *buf, loff_t pos, size_t count)
467{ 463{
468 struct efi_variable *del_var = (struct efi_variable *)buf; 464 struct efi_variable *del_var = (struct efi_variable *)buf;
465 struct efivars *efivars = bin_attr->private;
469 struct efivar_entry *search_efivar, *n; 466 struct efivar_entry *search_efivar, *n;
470 unsigned long strsize1, strsize2; 467 unsigned long strsize1, strsize2;
471 efi_status_t status = EFI_NOT_FOUND; 468 efi_status_t status = EFI_NOT_FOUND;
@@ -474,12 +471,12 @@ static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
474 if (!capable(CAP_SYS_ADMIN)) 471 if (!capable(CAP_SYS_ADMIN))
475 return -EACCES; 472 return -EACCES;
476 473
477 spin_lock(&efivars_lock); 474 spin_lock(&efivars->lock);
478 475
479 /* 476 /*
480 * Does this variable already exist? 477 * Does this variable already exist?
481 */ 478 */
482 list_for_each_entry_safe(search_efivar, n, &efivar_list, list) { 479 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
483 strsize1 = utf8_strsize(search_efivar->var.VariableName, 1024); 480 strsize1 = utf8_strsize(search_efivar->var.VariableName, 1024);
484 strsize2 = utf8_strsize(del_var->VariableName, 1024); 481 strsize2 = utf8_strsize(del_var->VariableName, 1024);
485 if (strsize1 == strsize2 && 482 if (strsize1 == strsize2 &&
@@ -492,44 +489,34 @@ static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
492 } 489 }
493 } 490 }
494 if (!found) { 491 if (!found) {
495 spin_unlock(&efivars_lock); 492 spin_unlock(&efivars->lock);
496 return -EINVAL; 493 return -EINVAL;
497 } 494 }
498 /* force the Attributes/DataSize to 0 to ensure deletion */ 495 /* force the Attributes/DataSize to 0 to ensure deletion */
499 del_var->Attributes = 0; 496 del_var->Attributes = 0;
500 del_var->DataSize = 0; 497 del_var->DataSize = 0;
501 498
502 status = efi.set_variable(del_var->VariableName, 499 status = efivars->ops->set_variable(del_var->VariableName,
503 &del_var->VendorGuid, 500 &del_var->VendorGuid,
504 del_var->Attributes, 501 del_var->Attributes,
505 del_var->DataSize, 502 del_var->DataSize,
506 del_var->Data); 503 del_var->Data);
507 504
508 if (status != EFI_SUCCESS) { 505 if (status != EFI_SUCCESS) {
509 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n", 506 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
510 status); 507 status);
511 spin_unlock(&efivars_lock); 508 spin_unlock(&efivars->lock);
512 return -EIO; 509 return -EIO;
513 } 510 }
514 list_del(&search_efivar->list); 511 list_del(&search_efivar->list);
515 /* We need to release this lock before unregistering. */ 512 /* We need to release this lock before unregistering. */
516 spin_unlock(&efivars_lock); 513 spin_unlock(&efivars->lock);
517 efivar_unregister(search_efivar); 514 efivar_unregister(search_efivar);
518 515
519 /* It's dead Jim.... */ 516 /* It's dead Jim.... */
520 return count; 517 return count;
521} 518}
522 519
523static struct bin_attribute var_subsys_attr_new_var = {
524 .attr = {.name = "new_var", .mode = 0200},
525 .write = efivar_create,
526};
527
528static struct bin_attribute var_subsys_attr_del_var = {
529 .attr = {.name = "del_var", .mode = 0200},
530 .write = efivar_delete,
531};
532
533/* 520/*
534 * Let's not leave out systab information that snuck into 521 * Let's not leave out systab information that snuck into
535 * the efivars driver 522 * the efivars driver
@@ -572,8 +559,6 @@ static struct attribute_group efi_subsys_attr_group = {
572 .attrs = efi_subsys_attrs, 559 .attrs = efi_subsys_attrs,
573}; 560};
574 561
575
576static struct kset *vars_kset;
577static struct kobject *efi_kobj; 562static struct kobject *efi_kobj;
578 563
579/* 564/*
@@ -582,13 +567,14 @@ static struct kobject *efi_kobj;
582 * variable_name_size = number of bytes required to hold 567 * variable_name_size = number of bytes required to hold
583 * variable_name (not counting the NULL 568 * variable_name (not counting the NULL
584 * character at the end. 569 * character at the end.
585 * efivars_lock is not held on entry or exit. 570 * efivars->lock is not held on entry or exit.
586 * Returns 1 on failure, 0 on success 571 * Returns 1 on failure, 0 on success
587 */ 572 */
588static int 573static int
589efivar_create_sysfs_entry(unsigned long variable_name_size, 574efivar_create_sysfs_entry(struct efivars *efivars,
590 efi_char16_t *variable_name, 575 unsigned long variable_name_size,
591 efi_guid_t *vendor_guid) 576 efi_char16_t *variable_name,
577 efi_guid_t *vendor_guid)
592{ 578{
593 int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38; 579 int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
594 char *short_name; 580 char *short_name;
@@ -603,6 +589,7 @@ efivar_create_sysfs_entry(unsigned long variable_name_size,
603 return 1; 589 return 1;
604 } 590 }
605 591
592 new_efivar->efivars = efivars;
606 memcpy(new_efivar->var.VariableName, variable_name, 593 memcpy(new_efivar->var.VariableName, variable_name,
607 variable_name_size); 594 variable_name_size);
608 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t)); 595 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
@@ -618,7 +605,7 @@ efivar_create_sysfs_entry(unsigned long variable_name_size,
618 *(short_name + strlen(short_name)) = '-'; 605 *(short_name + strlen(short_name)) = '-';
619 efi_guid_unparse(vendor_guid, short_name + strlen(short_name)); 606 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
620 607
621 new_efivar->kobj.kset = vars_kset; 608 new_efivar->kobj.kset = efivars->kset;
622 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL, 609 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
623 "%s", short_name); 610 "%s", short_name);
624 if (i) { 611 if (i) {
@@ -631,22 +618,95 @@ efivar_create_sysfs_entry(unsigned long variable_name_size,
631 kfree(short_name); 618 kfree(short_name);
632 short_name = NULL; 619 short_name = NULL;
633 620
634 spin_lock(&efivars_lock); 621 spin_lock(&efivars->lock);
635 list_add(&new_efivar->list, &efivar_list); 622 list_add(&new_efivar->list, &efivars->list);
636 spin_unlock(&efivars_lock); 623 spin_unlock(&efivars->lock);
637 624
638 return 0; 625 return 0;
639} 626}
640/*
641 * For now we register the efi subsystem with the firmware subsystem
642 * and the vars subsystem with the efi subsystem. In the future, it
643 * might make sense to split off the efi subsystem into its own
644 * driver, but for now only efivars will register with it, so just
645 * include it here.
646 */
647 627
648static int __init 628static int
649efivars_init(void) 629create_efivars_bin_attributes(struct efivars *efivars)
630{
631 struct bin_attribute *attr;
632 int error;
633
634 /* new_var */
635 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
636 if (!attr)
637 return -ENOMEM;
638
639 attr->attr.name = "new_var";
640 attr->attr.mode = 0200;
641 attr->write = efivar_create;
642 attr->private = efivars;
643 efivars->new_var = attr;
644
645 /* del_var */
646 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
647 if (!attr) {
648 error = -ENOMEM;
649 goto out_free;
650 }
651 attr->attr.name = "del_var";
652 attr->attr.mode = 0200;
653 attr->write = efivar_delete;
654 attr->private = efivars;
655 efivars->del_var = attr;
656
657 sysfs_bin_attr_init(efivars->new_var);
658 sysfs_bin_attr_init(efivars->del_var);
659
660 /* Register */
661 error = sysfs_create_bin_file(&efivars->kset->kobj,
662 efivars->new_var);
663 if (error) {
664 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
665 " due to error %d\n", error);
666 goto out_free;
667 }
668 error = sysfs_create_bin_file(&efivars->kset->kobj,
669 efivars->del_var);
670 if (error) {
671 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
672 " due to error %d\n", error);
673 sysfs_remove_bin_file(&efivars->kset->kobj,
674 efivars->new_var);
675 goto out_free;
676 }
677
678 return 0;
679out_free:
680 kfree(efivars->new_var);
681 efivars->new_var = NULL;
682 kfree(efivars->new_var);
683 efivars->new_var = NULL;
684 return error;
685}
686
687void unregister_efivars(struct efivars *efivars)
688{
689 struct efivar_entry *entry, *n;
690
691 list_for_each_entry_safe(entry, n, &efivars->list, list) {
692 spin_lock(&efivars->lock);
693 list_del(&entry->list);
694 spin_unlock(&efivars->lock);
695 efivar_unregister(entry);
696 }
697 if (efivars->new_var)
698 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
699 if (efivars->del_var)
700 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
701 kfree(efivars->new_var);
702 kfree(efivars->del_var);
703 kset_unregister(efivars->kset);
704}
705EXPORT_SYMBOL_GPL(unregister_efivars);
706
707int register_efivars(struct efivars *efivars,
708 const struct efivar_operations *ops,
709 struct kobject *parent_kobj)
650{ 710{
651 efi_status_t status = EFI_NOT_FOUND; 711 efi_status_t status = EFI_NOT_FOUND;
652 efi_guid_t vendor_guid; 712 efi_guid_t vendor_guid;
@@ -654,31 +714,21 @@ efivars_init(void)
654 unsigned long variable_name_size = 1024; 714 unsigned long variable_name_size = 1024;
655 int error = 0; 715 int error = 0;
656 716
657 if (!efi_enabled)
658 return -ENODEV;
659
660 variable_name = kzalloc(variable_name_size, GFP_KERNEL); 717 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
661 if (!variable_name) { 718 if (!variable_name) {
662 printk(KERN_ERR "efivars: Memory allocation failed.\n"); 719 printk(KERN_ERR "efivars: Memory allocation failed.\n");
663 return -ENOMEM; 720 return -ENOMEM;
664 } 721 }
665 722
666 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION, 723 spin_lock_init(&efivars->lock);
667 EFIVARS_DATE); 724 INIT_LIST_HEAD(&efivars->list);
725 efivars->ops = ops;
668 726
669 /* For now we'll register the efi directory at /sys/firmware/efi */ 727 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
670 efi_kobj = kobject_create_and_add("efi", firmware_kobj); 728 if (!efivars->kset) {
671 if (!efi_kobj) {
672 printk(KERN_ERR "efivars: Firmware registration failed.\n");
673 error = -ENOMEM;
674 goto out_free;
675 }
676
677 vars_kset = kset_create_and_add("vars", NULL, efi_kobj);
678 if (!vars_kset) {
679 printk(KERN_ERR "efivars: Subsystem registration failed.\n"); 729 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
680 error = -ENOMEM; 730 error = -ENOMEM;
681 goto out_firmware_unregister; 731 goto out;
682 } 732 }
683 733
684 /* 734 /*
@@ -689,14 +739,15 @@ efivars_init(void)
689 do { 739 do {
690 variable_name_size = 1024; 740 variable_name_size = 1024;
691 741
692 status = efi.get_next_variable(&variable_name_size, 742 status = ops->get_next_variable(&variable_name_size,
693 variable_name, 743 variable_name,
694 &vendor_guid); 744 &vendor_guid);
695 switch (status) { 745 switch (status) {
696 case EFI_SUCCESS: 746 case EFI_SUCCESS:
697 efivar_create_sysfs_entry(variable_name_size, 747 efivar_create_sysfs_entry(efivars,
698 variable_name, 748 variable_name_size,
699 &vendor_guid); 749 variable_name,
750 &vendor_guid);
700 break; 751 break;
701 case EFI_NOT_FOUND: 752 case EFI_NOT_FOUND:
702 break; 753 break;
@@ -708,35 +759,60 @@ efivars_init(void)
708 } 759 }
709 } while (status != EFI_NOT_FOUND); 760 } while (status != EFI_NOT_FOUND);
710 761
711 /* 762 error = create_efivars_bin_attributes(efivars);
712 * Now add attributes to allow creation of new vars
713 * and deletion of existing ones...
714 */
715 error = sysfs_create_bin_file(&vars_kset->kobj,
716 &var_subsys_attr_new_var);
717 if (error)
718 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
719 " due to error %d\n", error);
720 error = sysfs_create_bin_file(&vars_kset->kobj,
721 &var_subsys_attr_del_var);
722 if (error) 763 if (error)
723 printk(KERN_ERR "efivars: unable to create del_var sysfs file" 764 unregister_efivars(efivars);
724 " due to error %d\n", error);
725 765
726 /* Don't forget the systab entry */ 766out:
727 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group); 767 kfree(variable_name);
728 if (error)
729 printk(KERN_ERR "efivars: Sysfs attribute export failed with error %d.\n", error);
730 else
731 goto out_free;
732 768
733 kset_unregister(vars_kset); 769 return error;
770}
771EXPORT_SYMBOL_GPL(register_efivars);
734 772
735out_firmware_unregister: 773static struct efivars __efivars;
736 kobject_put(efi_kobj); 774static struct efivar_operations ops;
737 775
738out_free: 776/*
739 kfree(variable_name); 777 * For now we register the efi subsystem with the firmware subsystem
778 * and the vars subsystem with the efi subsystem. In the future, it
779 * might make sense to split off the efi subsystem into its own
780 * driver, but for now only efivars will register with it, so just
781 * include it here.
782 */
783
784static int __init
785efivars_init(void)
786{
787 int error = 0;
788
789 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
790 EFIVARS_DATE);
791
792 if (!efi_enabled)
793 return 0;
794
795 /* For now we'll register the efi directory at /sys/firmware/efi */
796 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
797 if (!efi_kobj) {
798 printk(KERN_ERR "efivars: Firmware registration failed.\n");
799 return -ENOMEM;
800 }
801
802 ops.get_variable = efi.get_variable;
803 ops.set_variable = efi.set_variable;
804 ops.get_next_variable = efi.get_next_variable;
805 error = register_efivars(&__efivars, &ops, efi_kobj);
806
807 /* Don't forget the systab entry */
808 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
809 if (error) {
810 printk(KERN_ERR
811 "efivars: Sysfs attribute export failed with error %d.\n",
812 error);
813 unregister_efivars(&__efivars);
814 kobject_put(efi_kobj);
815 }
740 816
741 return error; 817 return error;
742} 818}
@@ -744,16 +820,7 @@ out_free:
744static void __exit 820static void __exit
745efivars_exit(void) 821efivars_exit(void)
746{ 822{
747 struct efivar_entry *entry, *n; 823 unregister_efivars(&__efivars);
748
749 list_for_each_entry_safe(entry, n, &efivar_list, list) {
750 spin_lock(&efivars_lock);
751 list_del(&entry->list);
752 spin_unlock(&efivars_lock);
753 efivar_unregister(entry);
754 }
755
756 kset_unregister(vars_kset);
757 kobject_put(efi_kobj); 824 kobject_put(efi_kobj);
758} 825}
759 826