efi: Handle deletions and size changes in efivarfs_write_file
[deliverable/linux.git] / drivers / firmware / efivars.c
CommitLineData
1da177e4
LT
1/*
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
c59ede7b 68#include <linux/capability.h>
1da177e4
LT
69#include <linux/types.h>
70#include <linux/errno.h>
71#include <linux/init.h>
1da177e4
LT
72#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>
5a0e3ad6 80#include <linux/slab.h>
5ee9c198 81#include <linux/pstore.h>
1da177e4 82
5d9db883
MG
83#include <linux/fs.h>
84#include <linux/ramfs.h>
85#include <linux/pagemap.h>
86
1da177e4
LT
87#include <asm/uaccess.h>
88
89#define EFIVARS_VERSION "0.08"
90#define EFIVARS_DATE "2004-May-17"
91
92MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
93MODULE_DESCRIPTION("sysfs interface to EFI Variables");
94MODULE_LICENSE("GPL");
95MODULE_VERSION(EFIVARS_VERSION);
96
5ee9c198 97#define DUMP_NAME_LEN 52
5d9db883 98#define GUID_LEN 37
5ee9c198 99
1da177e4
LT
100/*
101 * The maximum size of VariableName + Data = 1024
102 * Therefore, it's reasonable to save that much
103 * space in each part of the structure,
104 * and we use a page for reading/writing.
105 */
106
107struct efi_variable {
108 efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
109 efi_guid_t VendorGuid;
110 unsigned long DataSize;
111 __u8 Data[1024];
112 efi_status_t Status;
113 __u32 Attributes;
114} __attribute__((packed));
115
1da177e4 116struct efivar_entry {
4142ef14 117 struct efivars *efivars;
1da177e4
LT
118 struct efi_variable var;
119 struct list_head list;
120 struct kobject kobj;
121};
122
1da177e4
LT
123struct efivar_attribute {
124 struct attribute attr;
125 ssize_t (*show) (struct efivar_entry *entry, char *buf);
126 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
127};
128
5d9db883
MG
129static struct efivars __efivars;
130static struct efivar_operations ops;
131
7644c16c
MW
132#define PSTORE_EFI_ATTRIBUTES \
133 (EFI_VARIABLE_NON_VOLATILE | \
134 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
135 EFI_VARIABLE_RUNTIME_ACCESS)
1da177e4 136
1da177e4
LT
137#define EFIVAR_ATTR(_name, _mode, _show, _store) \
138struct efivar_attribute efivar_attr_##_name = { \
7b595756 139 .attr = {.name = __stringify(_name), .mode = _mode}, \
1da177e4
LT
140 .show = _show, \
141 .store = _store, \
142};
143
1da177e4
LT
144#define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
145#define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
146
147/*
148 * Prototype for sysfs creation function
149 */
150static int
4142ef14
MW
151efivar_create_sysfs_entry(struct efivars *efivars,
152 unsigned long variable_name_size,
153 efi_char16_t *variable_name,
154 efi_guid_t *vendor_guid);
1da177e4
LT
155
156/* Return the number of unicode characters in data */
157static unsigned long
a2940908 158utf16_strnlen(efi_char16_t *s, size_t maxlength)
1da177e4
LT
159{
160 unsigned long length = 0;
161
a2940908 162 while (*s++ != 0 && length < maxlength)
1da177e4
LT
163 length++;
164 return length;
165}
166
b728a5c8 167static inline unsigned long
a2940908
MW
168utf16_strlen(efi_char16_t *s)
169{
170 return utf16_strnlen(s, ~0UL);
171}
172
1da177e4
LT
173/*
174 * Return the number of bytes is the length of this string
175 * Note: this is NOT the same as the number of unicode characters
176 */
177static inline unsigned long
a2940908 178utf16_strsize(efi_char16_t *data, unsigned long maxlength)
1da177e4 179{
a2940908 180 return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
1da177e4
LT
181}
182
828aa1f0
MW
183static inline int
184utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
185{
186 while (1) {
187 if (len == 0)
188 return 0;
189 if (*a < *b)
190 return -1;
191 if (*a > *b)
192 return 1;
193 if (*a == 0) /* implies *b == 0 */
194 return 0;
195 a++;
196 b++;
197 len--;
198 }
199}
200
fec6c20b 201static bool
54b3a4d3
MG
202validate_device_path(struct efi_variable *var, int match, u8 *buffer,
203 unsigned long len)
fec6c20b
MG
204{
205 struct efi_generic_dev_path *node;
206 int offset = 0;
207
208 node = (struct efi_generic_dev_path *)buffer;
209
54b3a4d3
MG
210 if (len < sizeof(*node))
211 return false;
fec6c20b 212
54b3a4d3
MG
213 while (offset <= len - sizeof(*node) &&
214 node->length >= sizeof(*node) &&
215 node->length <= len - offset) {
216 offset += node->length;
fec6c20b
MG
217
218 if ((node->type == EFI_DEV_END_PATH ||
219 node->type == EFI_DEV_END_PATH2) &&
220 node->sub_type == EFI_DEV_END_ENTIRE)
221 return true;
222
223 node = (struct efi_generic_dev_path *)(buffer + offset);
224 }
225
226 /*
227 * If we're here then either node->length pointed past the end
228 * of the buffer or we reached the end of the buffer without
229 * finding a device path end node.
230 */
231 return false;
232}
233
234static bool
54b3a4d3
MG
235validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
236 unsigned long len)
fec6c20b
MG
237{
238 /* An array of 16-bit integers */
239 if ((len % 2) != 0)
240 return false;
241
242 return true;
243}
244
245static bool
54b3a4d3
MG
246validate_load_option(struct efi_variable *var, int match, u8 *buffer,
247 unsigned long len)
fec6c20b
MG
248{
249 u16 filepathlength;
54b3a4d3
MG
250 int i, desclength = 0, namelen;
251
252 namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
fec6c20b
MG
253
254 /* Either "Boot" or "Driver" followed by four digits of hex */
255 for (i = match; i < match+4; i++) {
54b3a4d3
MG
256 if (var->VariableName[i] > 127 ||
257 hex_to_bin(var->VariableName[i] & 0xff) < 0)
fec6c20b
MG
258 return true;
259 }
260
54b3a4d3
MG
261 /* Reject it if there's 4 digits of hex and then further content */
262 if (namelen > match + 4)
263 return false;
264
265 /* A valid entry must be at least 8 bytes */
266 if (len < 8)
fec6c20b
MG
267 return false;
268
269 filepathlength = buffer[4] | buffer[5] << 8;
270
271 /*
272 * There's no stored length for the description, so it has to be
273 * found by hand
274 */
54b3a4d3 275 desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
fec6c20b
MG
276
277 /* Each boot entry must have a descriptor */
278 if (!desclength)
279 return false;
280
281 /*
282 * If the sum of the length of the description, the claimed filepath
283 * length and the original header are greater than the length of the
284 * variable, it's malformed
285 */
286 if ((desclength + filepathlength + 6) > len)
287 return false;
288
289 /*
290 * And, finally, check the filepath
291 */
292 return validate_device_path(var, match, buffer + desclength + 6,
293 filepathlength);
294}
295
296static bool
54b3a4d3
MG
297validate_uint16(struct efi_variable *var, int match, u8 *buffer,
298 unsigned long len)
fec6c20b
MG
299{
300 /* A single 16-bit integer */
301 if (len != 2)
302 return false;
303
304 return true;
305}
306
307static bool
54b3a4d3
MG
308validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
309 unsigned long len)
fec6c20b
MG
310{
311 int i;
312
313 for (i = 0; i < len; i++) {
314 if (buffer[i] > 127)
315 return false;
316
317 if (buffer[i] == 0)
318 return true;
319 }
320
321 return false;
322}
323
324struct variable_validate {
325 char *name;
326 bool (*validate)(struct efi_variable *var, int match, u8 *data,
54b3a4d3 327 unsigned long len);
fec6c20b
MG
328};
329
330static const struct variable_validate variable_validate[] = {
331 { "BootNext", validate_uint16 },
332 { "BootOrder", validate_boot_order },
333 { "DriverOrder", validate_boot_order },
334 { "Boot*", validate_load_option },
335 { "Driver*", validate_load_option },
336 { "ConIn", validate_device_path },
337 { "ConInDev", validate_device_path },
338 { "ConOut", validate_device_path },
339 { "ConOutDev", validate_device_path },
340 { "ErrOut", validate_device_path },
341 { "ErrOutDev", validate_device_path },
342 { "Timeout", validate_uint16 },
343 { "Lang", validate_ascii_string },
344 { "PlatformLang", validate_ascii_string },
345 { "", NULL },
346};
347
348static bool
54b3a4d3 349validate_var(struct efi_variable *var, u8 *data, unsigned long len)
fec6c20b
MG
350{
351 int i;
352 u16 *unicode_name = var->VariableName;
353
354 for (i = 0; variable_validate[i].validate != NULL; i++) {
355 const char *name = variable_validate[i].name;
356 int match;
357
358 for (match = 0; ; match++) {
359 char c = name[match];
360 u16 u = unicode_name[match];
361
362 /* All special variables are plain ascii */
363 if (u > 127)
364 return true;
365
366 /* Wildcard in the matching name means we've matched */
367 if (c == '*')
368 return variable_validate[i].validate(var,
369 match, data, len);
370
371 /* Case sensitive match */
372 if (c != u)
373 break;
374
375 /* Reached the end of the string while matching */
376 if (!c)
377 return variable_validate[i].validate(var,
378 match, data, len);
379 }
380 }
381
382 return true;
383}
384
1da177e4 385static efi_status_t
5ee9c198 386get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
1da177e4
LT
387{
388 efi_status_t status;
389
1da177e4 390 var->DataSize = 1024;
3295814d
MW
391 status = efivars->ops->get_variable(var->VariableName,
392 &var->VendorGuid,
393 &var->Attributes,
394 &var->DataSize,
395 var->Data);
5ee9c198
MG
396 return status;
397}
398
399static efi_status_t
400get_var_data(struct efivars *efivars, struct efi_variable *var)
401{
402 efi_status_t status;
403
404 spin_lock(&efivars->lock);
405 status = get_var_data_locked(efivars, var);
29422693 406 spin_unlock(&efivars->lock);
5ee9c198 407
1da177e4
LT
408 if (status != EFI_SUCCESS) {
409 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
410 status);
411 }
412 return status;
413}
414
415static ssize_t
416efivar_guid_read(struct efivar_entry *entry, char *buf)
417{
418 struct efi_variable *var = &entry->var;
419 char *str = buf;
420
421 if (!entry || !buf)
422 return 0;
423
424 efi_guid_unparse(&var->VendorGuid, str);
425 str += strlen(str);
426 str += sprintf(str, "\n");
427
428 return str - buf;
429}
430
431static ssize_t
432efivar_attr_read(struct efivar_entry *entry, char *buf)
433{
434 struct efi_variable *var = &entry->var;
435 char *str = buf;
436 efi_status_t status;
437
438 if (!entry || !buf)
439 return -EINVAL;
440
4142ef14 441 status = get_var_data(entry->efivars, var);
1da177e4
LT
442 if (status != EFI_SUCCESS)
443 return -EIO;
444
70839090 445 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
1da177e4 446 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
70839090 447 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
1da177e4 448 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
70839090 449 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
1da177e4 450 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
70839090
KA
451 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
452 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
453 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
454 str += sprintf(str,
455 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
456 if (var->Attributes &
457 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
458 str += sprintf(str,
459 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
460 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
461 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
1da177e4
LT
462 return str - buf;
463}
464
465static ssize_t
466efivar_size_read(struct efivar_entry *entry, char *buf)
467{
468 struct efi_variable *var = &entry->var;
469 char *str = buf;
470 efi_status_t status;
471
472 if (!entry || !buf)
473 return -EINVAL;
474
4142ef14 475 status = get_var_data(entry->efivars, var);
1da177e4
LT
476 if (status != EFI_SUCCESS)
477 return -EIO;
478
479 str += sprintf(str, "0x%lx\n", var->DataSize);
480 return str - buf;
481}
482
483static ssize_t
484efivar_data_read(struct efivar_entry *entry, char *buf)
485{
486 struct efi_variable *var = &entry->var;
487 efi_status_t status;
488
489 if (!entry || !buf)
490 return -EINVAL;
491
4142ef14 492 status = get_var_data(entry->efivars, var);
1da177e4
LT
493 if (status != EFI_SUCCESS)
494 return -EIO;
495
496 memcpy(buf, var->Data, var->DataSize);
497 return var->DataSize;
498}
499/*
500 * We allow each variable to be edited via rewriting the
501 * entire efi variable structure.
502 */
503static ssize_t
504efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
505{
506 struct efi_variable *new_var, *var = &entry->var;
4142ef14 507 struct efivars *efivars = entry->efivars;
1da177e4
LT
508 efi_status_t status = EFI_NOT_FOUND;
509
510 if (count != sizeof(struct efi_variable))
511 return -EINVAL;
512
513 new_var = (struct efi_variable *)buf;
514 /*
515 * If only updating the variable data, then the name
516 * and guid should remain the same
517 */
518 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
519 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
520 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
521 return -EINVAL;
522 }
523
524 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
525 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
526 return -EINVAL;
527 }
528
fec6c20b
MG
529 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
530 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
531 printk(KERN_ERR "efivars: Malformed variable content\n");
532 return -EINVAL;
533 }
534
29422693 535 spin_lock(&efivars->lock);
3295814d
MW
536 status = efivars->ops->set_variable(new_var->VariableName,
537 &new_var->VendorGuid,
538 new_var->Attributes,
539 new_var->DataSize,
540 new_var->Data);
1da177e4 541
29422693 542 spin_unlock(&efivars->lock);
1da177e4
LT
543
544 if (status != EFI_SUCCESS) {
545 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
546 status);
547 return -EIO;
548 }
549
550 memcpy(&entry->var, new_var, count);
551 return count;
552}
553
554static ssize_t
555efivar_show_raw(struct efivar_entry *entry, char *buf)
556{
557 struct efi_variable *var = &entry->var;
558 efi_status_t status;
559
560 if (!entry || !buf)
561 return 0;
562
4142ef14 563 status = get_var_data(entry->efivars, var);
1da177e4
LT
564 if (status != EFI_SUCCESS)
565 return -EIO;
566
567 memcpy(buf, var, sizeof(*var));
568 return sizeof(*var);
569}
570
571/*
572 * Generic read/write functions that call the specific functions of
70f23fd6 573 * the attributes...
1da177e4
LT
574 */
575static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
576 char *buf)
577{
578 struct efivar_entry *var = to_efivar_entry(kobj);
579 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
70f2817a 580 ssize_t ret = -EIO;
1da177e4
LT
581
582 if (!capable(CAP_SYS_ADMIN))
583 return -EACCES;
584
585 if (efivar_attr->show) {
586 ret = efivar_attr->show(var, buf);
587 }
588 return ret;
589}
590
591static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
592 const char *buf, size_t count)
593{
594 struct efivar_entry *var = to_efivar_entry(kobj);
595 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
70f2817a 596 ssize_t ret = -EIO;
1da177e4
LT
597
598 if (!capable(CAP_SYS_ADMIN))
599 return -EACCES;
600
601 if (efivar_attr->store)
602 ret = efivar_attr->store(var, buf, count);
603
604 return ret;
605}
606
52cf25d0 607static const struct sysfs_ops efivar_attr_ops = {
1da177e4
LT
608 .show = efivar_attr_show,
609 .store = efivar_attr_store,
610};
611
612static void efivar_release(struct kobject *kobj)
613{
614 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
1da177e4
LT
615 kfree(var);
616}
617
618static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
619static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
620static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
621static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
622static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
623
624static struct attribute *def_attrs[] = {
625 &efivar_attr_guid.attr,
626 &efivar_attr_size.attr,
627 &efivar_attr_attributes.attr,
628 &efivar_attr_data.attr,
629 &efivar_attr_raw_var.attr,
630 NULL,
631};
632
e89a4116 633static struct kobj_type efivar_ktype = {
1da177e4
LT
634 .release = efivar_release,
635 .sysfs_ops = &efivar_attr_ops,
636 .default_attrs = def_attrs,
637};
638
1da177e4
LT
639static inline void
640efivar_unregister(struct efivar_entry *var)
641{
c10997f6 642 kobject_put(&var->kobj);
1da177e4
LT
643}
644
5d9db883
MG
645static int efivarfs_file_open(struct inode *inode, struct file *file)
646{
647 file->private_data = inode->i_private;
648 return 0;
649}
650
651static ssize_t efivarfs_file_write(struct file *file,
652 const char __user *userbuf, size_t count, loff_t *ppos)
653{
654 struct efivar_entry *var = file->private_data;
655 struct efivars *efivars;
656 efi_status_t status;
657 void *data;
658 u32 attributes;
659 struct inode *inode = file->f_mapping->host;
660 int datasize = count - sizeof(attributes);
0c542edd 661 unsigned long newdatasize;
5d9db883
MG
662
663 if (count < sizeof(attributes))
664 return -EINVAL;
665
666 data = kmalloc(datasize, GFP_KERNEL);
667
668 if (!data)
669 return -ENOMEM;
670
671 efivars = var->efivars;
672
673 if (copy_from_user(&attributes, userbuf, sizeof(attributes))) {
674 count = -EFAULT;
675 goto out;
676 }
677
678 if (attributes & ~(EFI_VARIABLE_MASK)) {
679 count = -EINVAL;
680 goto out;
681 }
682
683 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
684 count = -EFAULT;
685 goto out;
686 }
687
688 if (validate_var(&var->var, data, datasize) == false) {
689 count = -EINVAL;
690 goto out;
691 }
692
693 status = efivars->ops->set_variable(var->var.VariableName,
694 &var->var.VendorGuid,
695 attributes, datasize,
696 data);
697
698 switch (status) {
699 case EFI_SUCCESS:
5d9db883
MG
700 break;
701 case EFI_INVALID_PARAMETER:
702 count = -EINVAL;
0c542edd 703 goto out;
5d9db883
MG
704 case EFI_OUT_OF_RESOURCES:
705 count = -ENOSPC;
0c542edd 706 goto out;
5d9db883
MG
707 case EFI_DEVICE_ERROR:
708 count = -EIO;
0c542edd 709 goto out;
5d9db883
MG
710 case EFI_WRITE_PROTECTED:
711 count = -EROFS;
0c542edd 712 goto out;
5d9db883
MG
713 case EFI_SECURITY_VIOLATION:
714 count = -EACCES;
0c542edd 715 goto out;
5d9db883
MG
716 case EFI_NOT_FOUND:
717 count = -ENOENT;
0c542edd 718 goto out;
5d9db883
MG
719 default:
720 count = -EINVAL;
0c542edd 721 goto out;
5d9db883 722 }
0c542edd
JK
723
724 /*
725 * Writing to the variable may have caused a change in size (which
726 * could either be an append or an overwrite), or the variable to be
727 * deleted. Perform a GetVariable() so we can tell what actually
728 * happened.
729 */
730 newdatasize = 0;
731 status = efivars->ops->get_variable(var->var.VariableName,
732 &var->var.VendorGuid,
733 NULL, &newdatasize,
734 NULL);
735
736 if (status == EFI_BUFFER_TOO_SMALL) {
737 mutex_lock(&inode->i_mutex);
738 i_size_write(inode, newdatasize + sizeof(attributes));
739 mutex_unlock(&inode->i_mutex);
740
741 } else if (status == EFI_NOT_FOUND) {
742 spin_lock(&efivars->lock);
743 list_del(&var->list);
744 spin_unlock(&efivars->lock);
745 efivar_unregister(var);
746 drop_nlink(inode);
747 dput(file->f_dentry);
748
749 } else {
750 pr_warn("efivarfs: inconsistent EFI variable implementation? "
751 "status = %lx\n", status);
752 }
753
5d9db883
MG
754out:
755 kfree(data);
756
757 return count;
758}
759
760static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
761 size_t count, loff_t *ppos)
762{
763 struct efivar_entry *var = file->private_data;
764 struct efivars *efivars = var->efivars;
765 efi_status_t status;
766 unsigned long datasize = 0;
767 u32 attributes;
768 void *data;
769 ssize_t size;
770
771 status = efivars->ops->get_variable(var->var.VariableName,
772 &var->var.VendorGuid,
773 &attributes, &datasize, NULL);
774
775 if (status != EFI_BUFFER_TOO_SMALL)
776 return 0;
777
778 data = kmalloc(datasize + 4, GFP_KERNEL);
779
780 if (!data)
781 return 0;
782
783 status = efivars->ops->get_variable(var->var.VariableName,
784 &var->var.VendorGuid,
785 &attributes, &datasize,
786 (data + 4));
787
788 if (status != EFI_SUCCESS)
789 return 0;
790
791 memcpy(data, &attributes, 4);
792 size = simple_read_from_buffer(userbuf, count, ppos,
793 data, datasize + 4);
794 kfree(data);
795
796 return size;
797}
798
799static void efivarfs_evict_inode(struct inode *inode)
800{
801 clear_inode(inode);
802}
803
804static const struct super_operations efivarfs_ops = {
805 .statfs = simple_statfs,
806 .drop_inode = generic_delete_inode,
807 .evict_inode = efivarfs_evict_inode,
808 .show_options = generic_show_options,
809};
810
811static struct super_block *efivarfs_sb;
812
813static const struct inode_operations efivarfs_dir_inode_operations;
814
815static const struct file_operations efivarfs_file_operations = {
816 .open = efivarfs_file_open,
817 .read = efivarfs_file_read,
818 .write = efivarfs_file_write,
819 .llseek = no_llseek,
820};
821
822static struct inode *efivarfs_get_inode(struct super_block *sb,
823 const struct inode *dir, int mode, dev_t dev)
824{
825 struct inode *inode = new_inode(sb);
826
827 if (inode) {
828 inode->i_ino = get_next_ino();
829 inode->i_uid = inode->i_gid = 0;
830 inode->i_mode = mode;
831 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
832 switch (mode & S_IFMT) {
833 case S_IFREG:
834 inode->i_fop = &efivarfs_file_operations;
835 break;
836 case S_IFDIR:
837 inode->i_op = &efivarfs_dir_inode_operations;
838 inode->i_fop = &simple_dir_operations;
839 inc_nlink(inode);
840 break;
841 }
842 }
843 return inode;
844}
845
846static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
847{
848 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
849 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
850 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
851 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
852 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
853 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
854 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
855 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
856 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
857 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
858 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
859 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
860 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
861 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
862 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
863 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
864}
865
866static int efivarfs_create(struct inode *dir, struct dentry *dentry,
867 umode_t mode, bool excl)
868{
869 struct inode *inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
870 struct efivars *efivars = &__efivars;
871 struct efivar_entry *var;
872 int namelen, i = 0, err = 0;
873
874 if (dentry->d_name.len < 38)
875 return -EINVAL;
876
877 if (!inode)
878 return -ENOSPC;
879
880 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
881
882 if (!var)
883 return -ENOMEM;
884
885 namelen = dentry->d_name.len - GUID_LEN;
886
887 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
888 &var->var.VendorGuid);
889
890 for (i = 0; i < namelen; i++)
891 var->var.VariableName[i] = dentry->d_name.name[i];
892
893 var->var.VariableName[i] = '\0';
894
895 inode->i_private = var;
896 var->efivars = efivars;
897 var->kobj.kset = efivars->kset;
898
899 err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
900 dentry->d_name.name);
901 if (err)
902 goto out;
903
904 kobject_uevent(&var->kobj, KOBJ_ADD);
905 spin_lock(&efivars->lock);
906 list_add(&var->list, &efivars->list);
907 spin_unlock(&efivars->lock);
908 d_instantiate(dentry, inode);
909 dget(dentry);
910out:
911 if (err)
912 kfree(var);
913 return err;
914}
915
916static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
917{
918 struct efivar_entry *var = dentry->d_inode->i_private;
919 struct efivars *efivars = var->efivars;
920 efi_status_t status;
921
922 spin_lock(&efivars->lock);
923
924 status = efivars->ops->set_variable(var->var.VariableName,
925 &var->var.VendorGuid,
926 0, 0, NULL);
927
928 if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
929 list_del(&var->list);
930 spin_unlock(&efivars->lock);
931 efivar_unregister(var);
932 drop_nlink(dir);
933 dput(dentry);
934 return 0;
935 }
936
937 spin_unlock(&efivars->lock);
938 return -EINVAL;
939};
940
941int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
942{
943 struct inode *inode = NULL;
944 struct dentry *root;
945 struct efivar_entry *entry, *n;
946 struct efivars *efivars = &__efivars;
947 int err;
948
949 efivarfs_sb = sb;
950
951 sb->s_maxbytes = MAX_LFS_FILESIZE;
952 sb->s_blocksize = PAGE_CACHE_SIZE;
953 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
954 sb->s_magic = PSTOREFS_MAGIC;
955 sb->s_op = &efivarfs_ops;
956 sb->s_time_gran = 1;
957
958 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
959 if (!inode) {
960 err = -ENOMEM;
961 goto fail;
962 }
963 inode->i_op = &efivarfs_dir_inode_operations;
964
965 root = d_make_root(inode);
966 sb->s_root = root;
967 if (!root) {
968 err = -ENOMEM;
969 goto fail;
970 }
971
972 list_for_each_entry_safe(entry, n, &efivars->list, list) {
973 struct inode *inode;
974 struct dentry *dentry, *root = efivarfs_sb->s_root;
975 char *name;
976 unsigned long size = 0;
977 int len, i;
978
979 len = utf16_strlen(entry->var.VariableName);
980
981 /* GUID plus trailing NULL */
982 name = kmalloc(len + 38, GFP_ATOMIC);
983
984 for (i = 0; i < len; i++)
985 name[i] = entry->var.VariableName[i] & 0xFF;
986
987 name[len] = '-';
988
989 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
990
991 name[len+GUID_LEN] = '\0';
992
993 inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
994 S_IFREG | 0644, 0);
995 dentry = d_alloc_name(root, name);
996
997 efivars->ops->get_variable(entry->var.VariableName,
998 &entry->var.VendorGuid,
999 &entry->var.Attributes,
1000 &size,
1001 NULL);
1002
1003 mutex_lock(&inode->i_mutex);
1004 inode->i_private = entry;
1005 i_size_write(inode, size+4);
1006 mutex_unlock(&inode->i_mutex);
1007 d_add(dentry, inode);
1008 }
1009
1010 return 0;
1011fail:
1012 iput(inode);
1013 return err;
1014}
1015
1016static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
1017 int flags, const char *dev_name, void *data)
1018{
1019 return mount_single(fs_type, flags, data, efivarfs_fill_super);
1020}
1021
1022static void efivarfs_kill_sb(struct super_block *sb)
1023{
1024 kill_litter_super(sb);
1025 efivarfs_sb = NULL;
1026}
1027
1028static struct file_system_type efivarfs_type = {
1029 .name = "efivarfs",
1030 .mount = efivarfs_mount,
1031 .kill_sb = efivarfs_kill_sb,
1032};
1033
1034static const struct inode_operations efivarfs_dir_inode_operations = {
1035 .lookup = simple_lookup,
1036 .unlink = efivarfs_unlink,
1037 .create = efivarfs_create,
1038};
1039
1040static struct pstore_info efi_pstore_info;
1041
5ee9c198
MG
1042#ifdef CONFIG_PSTORE
1043
1044static int efi_pstore_open(struct pstore_info *psi)
1045{
1046 struct efivars *efivars = psi->data;
1047
1048 spin_lock(&efivars->lock);
1049 efivars->walk_entry = list_first_entry(&efivars->list,
1050 struct efivar_entry, list);
1051 return 0;
1052}
1053
1054static int efi_pstore_close(struct pstore_info *psi)
1055{
1056 struct efivars *efivars = psi->data;
1057
1058 spin_unlock(&efivars->lock);
1059 return 0;
1060}
1061
1062static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
f6f82851
KC
1063 struct timespec *timespec,
1064 char **buf, struct pstore_info *psi)
5ee9c198
MG
1065{
1066 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1067 struct efivars *efivars = psi->data;
1068 char name[DUMP_NAME_LEN];
1069 int i;
1070 unsigned int part, size;
1071 unsigned long time;
1072
1073 while (&efivars->walk_entry->list != &efivars->list) {
1074 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
1075 vendor)) {
1076 for (i = 0; i < DUMP_NAME_LEN; i++) {
1077 name[i] = efivars->walk_entry->var.VariableName[i];
1078 }
1079 if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
1080 *id = part;
1081 timespec->tv_sec = time;
1082 timespec->tv_nsec = 0;
1083 get_var_data_locked(efivars, &efivars->walk_entry->var);
1084 size = efivars->walk_entry->var.DataSize;
f6f82851
KC
1085 *buf = kmalloc(size, GFP_KERNEL);
1086 if (*buf == NULL)
1087 return -ENOMEM;
1088 memcpy(*buf, efivars->walk_entry->var.Data,
1089 size);
5ee9c198
MG
1090 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1091 struct efivar_entry, list);
1092 return size;
1093 }
1094 }
1095 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1096 struct efivar_entry, list);
1097 }
1098 return 0;
1099}
1100
3d6d8d20
KC
1101static int efi_pstore_write(enum pstore_type_id type,
1102 enum kmsg_dump_reason reason, u64 *id,
b238b8fa 1103 unsigned int part, size_t size, struct pstore_info *psi)
5ee9c198
MG
1104{
1105 char name[DUMP_NAME_LEN];
1106 char stub_name[DUMP_NAME_LEN];
1107 efi_char16_t efi_name[DUMP_NAME_LEN];
1108 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1109 struct efivars *efivars = psi->data;
1110 struct efivar_entry *entry, *found = NULL;
b238b8fa 1111 int i, ret = 0;
5ee9c198
MG
1112
1113 sprintf(stub_name, "dump-type%u-%u-", type, part);
1114 sprintf(name, "%s%lu", stub_name, get_seconds());
1115
1116 spin_lock(&efivars->lock);
1117
1118 for (i = 0; i < DUMP_NAME_LEN; i++)
1119 efi_name[i] = stub_name[i];
1120
1121 /*
1122 * Clean up any entries with the same name
1123 */
1124
1125 list_for_each_entry(entry, &efivars->list, list) {
1126 get_var_data_locked(efivars, &entry->var);
1127
c475594d
MW
1128 if (efi_guidcmp(entry->var.VendorGuid, vendor))
1129 continue;
1130 if (utf16_strncmp(entry->var.VariableName, efi_name,
1131 utf16_strlen(efi_name)))
1132 continue;
1133 /* Needs to be a prefix */
1134 if (entry->var.VariableName[utf16_strlen(efi_name)] == 0)
1135 continue;
1136
1137 /* found */
1138 found = entry;
7644c16c
MW
1139 efivars->ops->set_variable(entry->var.VariableName,
1140 &entry->var.VendorGuid,
1141 PSTORE_EFI_ATTRIBUTES,
c475594d 1142 0, NULL);
5ee9c198
MG
1143 }
1144
1145 if (found)
1146 list_del(&found->list);
1147
1148 for (i = 0; i < DUMP_NAME_LEN; i++)
1149 efi_name[i] = name[i];
1150
7644c16c 1151 efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
5ee9c198
MG
1152 size, psi->buf);
1153
1154 spin_unlock(&efivars->lock);
1155
1156 if (found)
1157 efivar_unregister(found);
1158
1159 if (size)
b238b8fa 1160 ret = efivar_create_sysfs_entry(efivars,
a2940908
MW
1161 utf16_strsize(efi_name,
1162 DUMP_NAME_LEN * 2),
5ee9c198
MG
1163 efi_name, &vendor);
1164
b238b8fa
CG
1165 *id = part;
1166 return ret;
5ee9c198
MG
1167};
1168
1169static int efi_pstore_erase(enum pstore_type_id type, u64 id,
1170 struct pstore_info *psi)
1171{
3d6d8d20 1172 efi_pstore_write(type, 0, &id, (unsigned int)id, 0, psi);
5ee9c198
MG
1173
1174 return 0;
1175}
1176#else
1177static int efi_pstore_open(struct pstore_info *psi)
1178{
1179 return 0;
1180}
1181
1182static int efi_pstore_close(struct pstore_info *psi)
1183{
1184 return 0;
1185}
1186
1187static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
eee628da
CF
1188 struct timespec *timespec,
1189 char **buf, struct pstore_info *psi)
5ee9c198
MG
1190{
1191 return -1;
1192}
1193
3d6d8d20
KC
1194static int efi_pstore_write(enum pstore_type_id type,
1195 enum kmsg_dump_reason reason, u64 *id,
b238b8fa 1196 unsigned int part, size_t size, struct pstore_info *psi)
5ee9c198
MG
1197{
1198 return 0;
1199}
1200
1201static int efi_pstore_erase(enum pstore_type_id type, u64 id,
1202 struct pstore_info *psi)
1203{
1204 return 0;
1205}
1206#endif
1207
1208static struct pstore_info efi_pstore_info = {
1209 .owner = THIS_MODULE,
1210 .name = "efi",
1211 .open = efi_pstore_open,
1212 .close = efi_pstore_close,
1213 .read = efi_pstore_read,
1214 .write = efi_pstore_write,
1215 .erase = efi_pstore_erase,
1216};
1da177e4 1217
2c3c8bea 1218static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
97fa5bb7
GKH
1219 struct bin_attribute *bin_attr,
1220 char *buf, loff_t pos, size_t count)
1da177e4
LT
1221{
1222 struct efi_variable *new_var = (struct efi_variable *)buf;
4142ef14 1223 struct efivars *efivars = bin_attr->private;
496a0fc8 1224 struct efivar_entry *search_efivar, *n;
1da177e4 1225 unsigned long strsize1, strsize2;
1da177e4
LT
1226 efi_status_t status = EFI_NOT_FOUND;
1227 int found = 0;
1228
1229 if (!capable(CAP_SYS_ADMIN))
1230 return -EACCES;
1231
fec6c20b
MG
1232 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
1233 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
1234 printk(KERN_ERR "efivars: Malformed variable content\n");
1235 return -EINVAL;
1236 }
1237
29422693 1238 spin_lock(&efivars->lock);
1da177e4
LT
1239
1240 /*
1241 * Does this variable already exist?
1242 */
29422693 1243 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
a2940908
MW
1244 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1245 strsize2 = utf16_strsize(new_var->VariableName, 1024);
1da177e4
LT
1246 if (strsize1 == strsize2 &&
1247 !memcmp(&(search_efivar->var.VariableName),
1248 new_var->VariableName, strsize1) &&
1249 !efi_guidcmp(search_efivar->var.VendorGuid,
1250 new_var->VendorGuid)) {
1251 found = 1;
1252 break;
1253 }
1254 }
1255 if (found) {
29422693 1256 spin_unlock(&efivars->lock);
1da177e4
LT
1257 return -EINVAL;
1258 }
1259
1260 /* now *really* create the variable via EFI */
3295814d
MW
1261 status = efivars->ops->set_variable(new_var->VariableName,
1262 &new_var->VendorGuid,
1263 new_var->Attributes,
1264 new_var->DataSize,
1265 new_var->Data);
1da177e4
LT
1266
1267 if (status != EFI_SUCCESS) {
1268 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1269 status);
29422693 1270 spin_unlock(&efivars->lock);
1da177e4
LT
1271 return -EIO;
1272 }
29422693 1273 spin_unlock(&efivars->lock);
1da177e4
LT
1274
1275 /* Create the entry in sysfs. Locking is not required here */
4142ef14 1276 status = efivar_create_sysfs_entry(efivars,
a2940908
MW
1277 utf16_strsize(new_var->VariableName,
1278 1024),
4142ef14
MW
1279 new_var->VariableName,
1280 &new_var->VendorGuid);
1da177e4
LT
1281 if (status) {
1282 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
1283 }
1284 return count;
1285}
1286
2c3c8bea 1287static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
97fa5bb7
GKH
1288 struct bin_attribute *bin_attr,
1289 char *buf, loff_t pos, size_t count)
1da177e4
LT
1290{
1291 struct efi_variable *del_var = (struct efi_variable *)buf;
4142ef14 1292 struct efivars *efivars = bin_attr->private;
496a0fc8 1293 struct efivar_entry *search_efivar, *n;
1da177e4 1294 unsigned long strsize1, strsize2;
1da177e4
LT
1295 efi_status_t status = EFI_NOT_FOUND;
1296 int found = 0;
1297
1298 if (!capable(CAP_SYS_ADMIN))
1299 return -EACCES;
1300
29422693 1301 spin_lock(&efivars->lock);
1da177e4
LT
1302
1303 /*
1304 * Does this variable already exist?
1305 */
29422693 1306 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
a2940908
MW
1307 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1308 strsize2 = utf16_strsize(del_var->VariableName, 1024);
1da177e4
LT
1309 if (strsize1 == strsize2 &&
1310 !memcmp(&(search_efivar->var.VariableName),
1311 del_var->VariableName, strsize1) &&
1312 !efi_guidcmp(search_efivar->var.VendorGuid,
1313 del_var->VendorGuid)) {
1314 found = 1;
1315 break;
1316 }
1317 }
1318 if (!found) {
29422693 1319 spin_unlock(&efivars->lock);
1da177e4
LT
1320 return -EINVAL;
1321 }
1322 /* force the Attributes/DataSize to 0 to ensure deletion */
1323 del_var->Attributes = 0;
1324 del_var->DataSize = 0;
1325
3295814d
MW
1326 status = efivars->ops->set_variable(del_var->VariableName,
1327 &del_var->VendorGuid,
1328 del_var->Attributes,
1329 del_var->DataSize,
1330 del_var->Data);
1da177e4
LT
1331
1332 if (status != EFI_SUCCESS) {
1333 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1334 status);
29422693 1335 spin_unlock(&efivars->lock);
1da177e4
LT
1336 return -EIO;
1337 }
496a0fc8 1338 list_del(&search_efivar->list);
1da177e4 1339 /* We need to release this lock before unregistering. */
29422693 1340 spin_unlock(&efivars->lock);
1da177e4
LT
1341 efivar_unregister(search_efivar);
1342
1343 /* It's dead Jim.... */
1344 return count;
1345}
1346
1da177e4
LT
1347/*
1348 * Let's not leave out systab information that snuck into
1349 * the efivars driver
1350 */
334c6307
GKH
1351static ssize_t systab_show(struct kobject *kobj,
1352 struct kobj_attribute *attr, char *buf)
1da177e4
LT
1353{
1354 char *str = buf;
1355
334c6307 1356 if (!kobj || !buf)
1da177e4
LT
1357 return -EINVAL;
1358
b2c99e3c
BH
1359 if (efi.mps != EFI_INVALID_TABLE_ADDR)
1360 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
1361 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
1362 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
1363 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
1364 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
1365 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
1366 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
1367 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
1368 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
1369 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
1370 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
1371 if (efi.uga != EFI_INVALID_TABLE_ADDR)
1372 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
1da177e4
LT
1373
1374 return str - buf;
1375}
1376
334c6307
GKH
1377static struct kobj_attribute efi_attr_systab =
1378 __ATTR(systab, 0400, systab_show, NULL);
1da177e4 1379
334c6307
GKH
1380static struct attribute *efi_subsys_attrs[] = {
1381 &efi_attr_systab.attr,
1da177e4
LT
1382 NULL, /* maybe more in the future? */
1383};
1384
334c6307
GKH
1385static struct attribute_group efi_subsys_attr_group = {
1386 .attrs = efi_subsys_attrs,
1387};
1388
bc87d2fe 1389static struct kobject *efi_kobj;
1da177e4
LT
1390
1391/*
1392 * efivar_create_sysfs_entry()
1393 * Requires:
1394 * variable_name_size = number of bytes required to hold
1395 * variable_name (not counting the NULL
1396 * character at the end.
29422693 1397 * efivars->lock is not held on entry or exit.
1da177e4
LT
1398 * Returns 1 on failure, 0 on success
1399 */
1400static int
4142ef14
MW
1401efivar_create_sysfs_entry(struct efivars *efivars,
1402 unsigned long variable_name_size,
1403 efi_char16_t *variable_name,
1404 efi_guid_t *vendor_guid)
1da177e4
LT
1405{
1406 int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
1407 char *short_name;
1408 struct efivar_entry *new_efivar;
1409
9c215384
DS
1410 short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
1411 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
1da177e4
LT
1412
1413 if (!short_name || !new_efivar) {
0933ad9c
JJ
1414 kfree(short_name);
1415 kfree(new_efivar);
1da177e4
LT
1416 return 1;
1417 }
1da177e4 1418
4142ef14 1419 new_efivar->efivars = efivars;
1da177e4
LT
1420 memcpy(new_efivar->var.VariableName, variable_name,
1421 variable_name_size);
1422 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
1423
1424 /* Convert Unicode to normal chars (assume top bits are 0),
1425 ala UTF-8 */
1426 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
1427 short_name[i] = variable_name[i] & 0xFF;
1428 }
1429 /* This is ugly, but necessary to separate one vendor's
1430 private variables from another's. */
1431
1432 *(short_name + strlen(short_name)) = '-';
1433 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
1434
29422693 1435 new_efivar->kobj.kset = efivars->kset;
d6d292c4
GKH
1436 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
1437 "%s", short_name);
69b2186c
JG
1438 if (i) {
1439 kfree(short_name);
1440 kfree(new_efivar);
1441 return 1;
1442 }
1da177e4 1443
d6d292c4 1444 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
0933ad9c
JJ
1445 kfree(short_name);
1446 short_name = NULL;
1da177e4 1447
29422693
MW
1448 spin_lock(&efivars->lock);
1449 list_add(&new_efivar->list, &efivars->list);
1450 spin_unlock(&efivars->lock);
1da177e4
LT
1451
1452 return 0;
1453}
d502fbb0
MW
1454
1455static int
1456create_efivars_bin_attributes(struct efivars *efivars)
1457{
1458 struct bin_attribute *attr;
1459 int error;
1460
1461 /* new_var */
1462 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1463 if (!attr)
1464 return -ENOMEM;
1465
1466 attr->attr.name = "new_var";
1467 attr->attr.mode = 0200;
1468 attr->write = efivar_create;
1469 attr->private = efivars;
1470 efivars->new_var = attr;
1471
1472 /* del_var */
1473 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1474 if (!attr) {
1475 error = -ENOMEM;
1476 goto out_free;
1477 }
1478 attr->attr.name = "del_var";
1479 attr->attr.mode = 0200;
1480 attr->write = efivar_delete;
1481 attr->private = efivars;
1482 efivars->del_var = attr;
1483
1484 sysfs_bin_attr_init(efivars->new_var);
1485 sysfs_bin_attr_init(efivars->del_var);
1486
1487 /* Register */
1488 error = sysfs_create_bin_file(&efivars->kset->kobj,
1489 efivars->new_var);
1490 if (error) {
1491 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
1492 " due to error %d\n", error);
1493 goto out_free;
1494 }
1495 error = sysfs_create_bin_file(&efivars->kset->kobj,
1496 efivars->del_var);
1497 if (error) {
1498 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
1499 " due to error %d\n", error);
1500 sysfs_remove_bin_file(&efivars->kset->kobj,
1501 efivars->new_var);
1502 goto out_free;
1503 }
1504
1505 return 0;
1506out_free:
051d51bc
DC
1507 kfree(efivars->del_var);
1508 efivars->del_var = NULL;
d502fbb0
MW
1509 kfree(efivars->new_var);
1510 efivars->new_var = NULL;
1511 return error;
1512}
1513
4fc756bd 1514void unregister_efivars(struct efivars *efivars)
76b53f7c
MW
1515{
1516 struct efivar_entry *entry, *n;
4142ef14 1517
76b53f7c
MW
1518 list_for_each_entry_safe(entry, n, &efivars->list, list) {
1519 spin_lock(&efivars->lock);
1520 list_del(&entry->list);
1521 spin_unlock(&efivars->lock);
1522 efivar_unregister(entry);
1523 }
1524 if (efivars->new_var)
1525 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
1526 if (efivars->del_var)
1527 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
1528 kfree(efivars->new_var);
1529 kfree(efivars->del_var);
1530 kset_unregister(efivars->kset);
1531}
4fc756bd 1532EXPORT_SYMBOL_GPL(unregister_efivars);
1da177e4 1533
4fc756bd
MW
1534int register_efivars(struct efivars *efivars,
1535 const struct efivar_operations *ops,
1536 struct kobject *parent_kobj)
1da177e4
LT
1537{
1538 efi_status_t status = EFI_NOT_FOUND;
1539 efi_guid_t vendor_guid;
1540 efi_char16_t *variable_name;
1da177e4 1541 unsigned long variable_name_size = 1024;
334c6307 1542 int error = 0;
1da177e4 1543
9c215384 1544 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
1da177e4
LT
1545 if (!variable_name) {
1546 printk(KERN_ERR "efivars: Memory allocation failed.\n");
1547 return -ENOMEM;
1548 }
1549
29422693
MW
1550 spin_lock_init(&efivars->lock);
1551 INIT_LIST_HEAD(&efivars->list);
3295814d 1552 efivars->ops = ops;
29422693 1553
76b53f7c 1554 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
29422693 1555 if (!efivars->kset) {
66ac831e
GKH
1556 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
1557 error = -ENOMEM;
76b53f7c 1558 goto out;
1da177e4
LT
1559 }
1560
1561 /*
1562 * Per EFI spec, the maximum storage allocated for both
1563 * the variable name and variable data is 1024 bytes.
1564 */
1565
1566 do {
1567 variable_name_size = 1024;
1568
3295814d 1569 status = ops->get_next_variable(&variable_name_size,
1da177e4
LT
1570 variable_name,
1571 &vendor_guid);
1572 switch (status) {
1573 case EFI_SUCCESS:
4142ef14
MW
1574 efivar_create_sysfs_entry(efivars,
1575 variable_name_size,
1576 variable_name,
1577 &vendor_guid);
1da177e4
LT
1578 break;
1579 case EFI_NOT_FOUND:
1580 break;
1581 default:
1582 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
1583 status);
1584 status = EFI_NOT_FOUND;
1585 break;
1586 }
1587 } while (status != EFI_NOT_FOUND);
1588
d502fbb0 1589 error = create_efivars_bin_attributes(efivars);
1da177e4 1590 if (error)
76b53f7c 1591 unregister_efivars(efivars);
1da177e4 1592
5ee9c198
MG
1593 efivars->efi_pstore_info = efi_pstore_info;
1594
1595 efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
1596 if (efivars->efi_pstore_info.buf) {
1597 efivars->efi_pstore_info.bufsize = 1024;
1598 efivars->efi_pstore_info.data = efivars;
abd4d558 1599 spin_lock_init(&efivars->efi_pstore_info.buf_lock);
5ee9c198
MG
1600 pstore_register(&efivars->efi_pstore_info);
1601 }
1602
5d9db883
MG
1603 register_filesystem(&efivarfs_type);
1604
76b53f7c
MW
1605out:
1606 kfree(variable_name);
1da177e4 1607
76b53f7c
MW
1608 return error;
1609}
4fc756bd 1610EXPORT_SYMBOL_GPL(register_efivars);
1da177e4 1611
76b53f7c
MW
1612/*
1613 * For now we register the efi subsystem with the firmware subsystem
1614 * and the vars subsystem with the efi subsystem. In the future, it
1615 * might make sense to split off the efi subsystem into its own
1616 * driver, but for now only efivars will register with it, so just
1617 * include it here.
1618 */
1619
1620static int __init
1621efivars_init(void)
1622{
1623 int error = 0;
1624
1625 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
1626 EFIVARS_DATE);
1627
1628 if (!efi_enabled)
4fc756bd 1629 return 0;
76b53f7c
MW
1630
1631 /* For now we'll register the efi directory at /sys/firmware/efi */
1632 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
1633 if (!efi_kobj) {
1634 printk(KERN_ERR "efivars: Firmware registration failed.\n");
1635 return -ENOMEM;
1636 }
1637
3295814d
MW
1638 ops.get_variable = efi.get_variable;
1639 ops.set_variable = efi.set_variable;
1640 ops.get_next_variable = efi.get_next_variable;
1641 error = register_efivars(&__efivars, &ops, efi_kobj);
3116aabc
DC
1642 if (error)
1643 goto err_put;
76b53f7c
MW
1644
1645 /* Don't forget the systab entry */
1646 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
1647 if (error) {
1648 printk(KERN_ERR
1649 "efivars: Sysfs attribute export failed with error %d.\n",
1650 error);
3116aabc 1651 goto err_unregister;
76b53f7c 1652 }
1da177e4 1653
3116aabc
DC
1654 return 0;
1655
1656err_unregister:
1657 unregister_efivars(&__efivars);
1658err_put:
1659 kobject_put(efi_kobj);
1da177e4
LT
1660 return error;
1661}
1662
1663static void __exit
1664efivars_exit(void)
1665{
aabb6e15
RD
1666 if (efi_enabled) {
1667 unregister_efivars(&__efivars);
1668 kobject_put(efi_kobj);
1669 }
1da177e4
LT
1670}
1671
1672module_init(efivars_init);
1673module_exit(efivars_exit);
1674
This page took 0.79625 seconds and 5 git commands to generate.