• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Originally from 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_to_str()
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 
68 #include <linux/efi.h>
69 #include <linux/module.h>
70 #include <linux/slab.h>
71 #include <linux/ucs2_string.h>
72 #include <linux/compat.h>
73 
74 #define EFIVARS_VERSION "0.08"
75 #define EFIVARS_DATE "2004-May-17"
76 
77 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
78 MODULE_DESCRIPTION("sysfs interface to EFI Variables");
79 MODULE_LICENSE("GPL");
80 MODULE_VERSION(EFIVARS_VERSION);
81 MODULE_ALIAS("platform:efivars");
82 
83 LIST_HEAD(efivar_sysfs_list);
84 EXPORT_SYMBOL_GPL(efivar_sysfs_list);
85 
86 static struct kset *efivars_kset;
87 
88 static struct bin_attribute *efivars_new_var;
89 static struct bin_attribute *efivars_del_var;
90 
91 struct compat_efi_variable {
92 	efi_char16_t  VariableName[EFI_VAR_NAME_LEN/sizeof(efi_char16_t)];
93 	efi_guid_t    VendorGuid;
94 	__u32         DataSize;
95 	__u8          Data[1024];
96 	__u32         Status;
97 	__u32         Attributes;
98 } __packed;
99 
100 struct efivar_attribute {
101 	struct attribute attr;
102 	ssize_t (*show) (struct efivar_entry *entry, char *buf);
103 	ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
104 };
105 
106 #define EFIVAR_ATTR(_name, _mode, _show, _store) \
107 struct efivar_attribute efivar_attr_##_name = { \
108 	.attr = {.name = __stringify(_name), .mode = _mode}, \
109 	.show = _show, \
110 	.store = _store, \
111 };
112 
113 #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
114 #define to_efivar_entry(obj)  container_of(obj, struct efivar_entry, kobj)
115 
116 /*
117  * Prototype for sysfs creation function
118  */
119 static int
120 efivar_create_sysfs_entry(struct efivar_entry *new_var);
121 
122 static ssize_t
efivar_guid_read(struct efivar_entry * entry,char * buf)123 efivar_guid_read(struct efivar_entry *entry, char *buf)
124 {
125 	struct efi_variable *var = &entry->var;
126 	char *str = buf;
127 
128 	if (!entry || !buf)
129 		return 0;
130 
131 	efi_guid_to_str(&var->VendorGuid, str);
132 	str += strlen(str);
133 	str += sprintf(str, "\n");
134 
135 	return str - buf;
136 }
137 
138 static ssize_t
efivar_attr_read(struct efivar_entry * entry,char * buf)139 efivar_attr_read(struct efivar_entry *entry, char *buf)
140 {
141 	struct efi_variable *var = &entry->var;
142 	unsigned long size = sizeof(var->Data);
143 	char *str = buf;
144 	int ret;
145 
146 	if (!entry || !buf)
147 		return -EINVAL;
148 
149 	ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
150 	var->DataSize = size;
151 	if (ret)
152 		return -EIO;
153 
154 	if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
155 		str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
156 	if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
157 		str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
158 	if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
159 		str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
160 	if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
161 		str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
162 	if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
163 		str += sprintf(str,
164 			"EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
165 	if (var->Attributes &
166 			EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
167 		str += sprintf(str,
168 			"EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
169 	if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
170 		str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
171 	return str - buf;
172 }
173 
174 static ssize_t
efivar_size_read(struct efivar_entry * entry,char * buf)175 efivar_size_read(struct efivar_entry *entry, char *buf)
176 {
177 	struct efi_variable *var = &entry->var;
178 	unsigned long size = sizeof(var->Data);
179 	char *str = buf;
180 	int ret;
181 
182 	if (!entry || !buf)
183 		return -EINVAL;
184 
185 	ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
186 	var->DataSize = size;
187 	if (ret)
188 		return -EIO;
189 
190 	str += sprintf(str, "0x%lx\n", var->DataSize);
191 	return str - buf;
192 }
193 
194 static ssize_t
efivar_data_read(struct efivar_entry * entry,char * buf)195 efivar_data_read(struct efivar_entry *entry, char *buf)
196 {
197 	struct efi_variable *var = &entry->var;
198 	unsigned long size = sizeof(var->Data);
199 	int ret;
200 
201 	if (!entry || !buf)
202 		return -EINVAL;
203 
204 	ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
205 	var->DataSize = size;
206 	if (ret)
207 		return -EIO;
208 
209 	memcpy(buf, var->Data, var->DataSize);
210 	return var->DataSize;
211 }
212 
213 static inline int
sanity_check(struct efi_variable * var,efi_char16_t * name,efi_guid_t vendor,unsigned long size,u32 attributes,u8 * data)214 sanity_check(struct efi_variable *var, efi_char16_t *name, efi_guid_t vendor,
215 	     unsigned long size, u32 attributes, u8 *data)
216 {
217 	/*
218 	 * If only updating the variable data, then the name
219 	 * and guid should remain the same
220 	 */
221 	if (memcmp(name, var->VariableName, sizeof(var->VariableName)) ||
222 		efi_guidcmp(vendor, var->VendorGuid)) {
223 		printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
224 		return -EINVAL;
225 	}
226 
227 	if ((size <= 0) || (attributes == 0)){
228 		printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
229 		return -EINVAL;
230 	}
231 
232 	if ((attributes & ~EFI_VARIABLE_MASK) != 0 ||
233 	    efivar_validate(vendor, name, data, size) == false) {
234 		printk(KERN_ERR "efivars: Malformed variable content\n");
235 		return -EINVAL;
236 	}
237 
238 	return 0;
239 }
240 
is_compat(void)241 static inline bool is_compat(void)
242 {
243 	if (IS_ENABLED(CONFIG_COMPAT) && in_compat_syscall())
244 		return true;
245 
246 	return false;
247 }
248 
249 static void
copy_out_compat(struct efi_variable * dst,struct compat_efi_variable * src)250 copy_out_compat(struct efi_variable *dst, struct compat_efi_variable *src)
251 {
252 	memcpy(dst->VariableName, src->VariableName, EFI_VAR_NAME_LEN);
253 	memcpy(dst->Data, src->Data, sizeof(src->Data));
254 
255 	dst->VendorGuid = src->VendorGuid;
256 	dst->DataSize = src->DataSize;
257 	dst->Attributes = src->Attributes;
258 }
259 
260 /*
261  * We allow each variable to be edited via rewriting the
262  * entire efi variable structure.
263  */
264 static ssize_t
efivar_store_raw(struct efivar_entry * entry,const char * buf,size_t count)265 efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
266 {
267 	struct efi_variable *new_var, *var = &entry->var;
268 	efi_char16_t *name;
269 	unsigned long size;
270 	efi_guid_t vendor;
271 	u32 attributes;
272 	u8 *data;
273 	int err;
274 
275 	if (!entry || !buf)
276 		return -EINVAL;
277 
278 	if (is_compat()) {
279 		struct compat_efi_variable *compat;
280 
281 		if (count != sizeof(*compat))
282 			return -EINVAL;
283 
284 		compat = (struct compat_efi_variable *)buf;
285 		attributes = compat->Attributes;
286 		vendor = compat->VendorGuid;
287 		name = compat->VariableName;
288 		size = compat->DataSize;
289 		data = compat->Data;
290 
291 		err = sanity_check(var, name, vendor, size, attributes, data);
292 		if (err)
293 			return err;
294 
295 		copy_out_compat(&entry->var, compat);
296 	} else {
297 		if (count != sizeof(struct efi_variable))
298 			return -EINVAL;
299 
300 		new_var = (struct efi_variable *)buf;
301 
302 		attributes = new_var->Attributes;
303 		vendor = new_var->VendorGuid;
304 		name = new_var->VariableName;
305 		size = new_var->DataSize;
306 		data = new_var->Data;
307 
308 		err = sanity_check(var, name, vendor, size, attributes, data);
309 		if (err)
310 			return err;
311 
312 		memcpy(&entry->var, new_var, count);
313 	}
314 
315 	err = efivar_entry_set(entry, attributes, size, data, NULL);
316 	if (err) {
317 		printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err);
318 		return -EIO;
319 	}
320 
321 	return count;
322 }
323 
324 static ssize_t
efivar_show_raw(struct efivar_entry * entry,char * buf)325 efivar_show_raw(struct efivar_entry *entry, char *buf)
326 {
327 	struct efi_variable *var = &entry->var;
328 	struct compat_efi_variable *compat;
329 	unsigned long datasize = sizeof(var->Data);
330 	size_t size;
331 	int ret;
332 
333 	if (!entry || !buf)
334 		return 0;
335 
336 	ret = efivar_entry_get(entry, &var->Attributes, &datasize, var->Data);
337 	var->DataSize = datasize;
338 	if (ret)
339 		return -EIO;
340 
341 	if (is_compat()) {
342 		compat = (struct compat_efi_variable *)buf;
343 
344 		size = sizeof(*compat);
345 		memcpy(compat->VariableName, var->VariableName,
346 			EFI_VAR_NAME_LEN);
347 		memcpy(compat->Data, var->Data, sizeof(compat->Data));
348 
349 		compat->VendorGuid = var->VendorGuid;
350 		compat->DataSize = var->DataSize;
351 		compat->Attributes = var->Attributes;
352 	} else {
353 		size = sizeof(*var);
354 		memcpy(buf, var, size);
355 	}
356 
357 	return size;
358 }
359 
360 /*
361  * Generic read/write functions that call the specific functions of
362  * the attributes...
363  */
efivar_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)364 static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
365 				char *buf)
366 {
367 	struct efivar_entry *var = to_efivar_entry(kobj);
368 	struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
369 	ssize_t ret = -EIO;
370 
371 	if (!capable(CAP_SYS_ADMIN))
372 		return -EACCES;
373 
374 	if (efivar_attr->show) {
375 		ret = efivar_attr->show(var, buf);
376 	}
377 	return ret;
378 }
379 
efivar_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)380 static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
381 				const char *buf, size_t count)
382 {
383 	struct efivar_entry *var = to_efivar_entry(kobj);
384 	struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
385 	ssize_t ret = -EIO;
386 
387 	if (!capable(CAP_SYS_ADMIN))
388 		return -EACCES;
389 
390 	if (efivar_attr->store)
391 		ret = efivar_attr->store(var, buf, count);
392 
393 	return ret;
394 }
395 
396 static const struct sysfs_ops efivar_attr_ops = {
397 	.show = efivar_attr_show,
398 	.store = efivar_attr_store,
399 };
400 
efivar_release(struct kobject * kobj)401 static void efivar_release(struct kobject *kobj)
402 {
403 	struct efivar_entry *var = to_efivar_entry(kobj);
404 	kfree(var);
405 }
406 
407 static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
408 static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
409 static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
410 static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
411 static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
412 
413 static struct attribute *def_attrs[] = {
414 	&efivar_attr_guid.attr,
415 	&efivar_attr_size.attr,
416 	&efivar_attr_attributes.attr,
417 	&efivar_attr_data.attr,
418 	&efivar_attr_raw_var.attr,
419 	NULL,
420 };
421 
422 static struct kobj_type efivar_ktype = {
423 	.release = efivar_release,
424 	.sysfs_ops = &efivar_attr_ops,
425 	.default_attrs = def_attrs,
426 };
427 
efivar_create(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t pos,size_t count)428 static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
429 			     struct bin_attribute *bin_attr,
430 			     char *buf, loff_t pos, size_t count)
431 {
432 	struct compat_efi_variable *compat = (struct compat_efi_variable *)buf;
433 	struct efi_variable *new_var = (struct efi_variable *)buf;
434 	struct efivar_entry *new_entry;
435 	bool need_compat = is_compat();
436 	efi_char16_t *name;
437 	unsigned long size;
438 	u32 attributes;
439 	u8 *data;
440 	int err;
441 
442 	if (!capable(CAP_SYS_ADMIN))
443 		return -EACCES;
444 
445 	if (need_compat) {
446 		if (count != sizeof(*compat))
447 			return -EINVAL;
448 
449 		attributes = compat->Attributes;
450 		name = compat->VariableName;
451 		size = compat->DataSize;
452 		data = compat->Data;
453 	} else {
454 		if (count != sizeof(*new_var))
455 			return -EINVAL;
456 
457 		attributes = new_var->Attributes;
458 		name = new_var->VariableName;
459 		size = new_var->DataSize;
460 		data = new_var->Data;
461 	}
462 
463 	if ((attributes & ~EFI_VARIABLE_MASK) != 0 ||
464 	    efivar_validate(new_var->VendorGuid, name, data,
465 			    size) == false) {
466 		printk(KERN_ERR "efivars: Malformed variable content\n");
467 		return -EINVAL;
468 	}
469 
470 	new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL);
471 	if (!new_entry)
472 		return -ENOMEM;
473 
474 	if (need_compat)
475 		copy_out_compat(&new_entry->var, compat);
476 	else
477 		memcpy(&new_entry->var, new_var, sizeof(*new_var));
478 
479 	err = efivar_entry_set(new_entry, attributes, size,
480 			       data, &efivar_sysfs_list);
481 	if (err) {
482 		if (err == -EEXIST)
483 			err = -EINVAL;
484 		goto out;
485 	}
486 
487 	if (efivar_create_sysfs_entry(new_entry)) {
488 		printk(KERN_WARNING "efivars: failed to create sysfs entry.\n");
489 		kfree(new_entry);
490 	}
491 	return count;
492 
493 out:
494 	kfree(new_entry);
495 	return err;
496 }
497 
efivar_delete(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t pos,size_t count)498 static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
499 			     struct bin_attribute *bin_attr,
500 			     char *buf, loff_t pos, size_t count)
501 {
502 	struct efi_variable *del_var = (struct efi_variable *)buf;
503 	struct compat_efi_variable *compat;
504 	struct efivar_entry *entry;
505 	efi_char16_t *name;
506 	efi_guid_t vendor;
507 	int err = 0;
508 
509 	if (!capable(CAP_SYS_ADMIN))
510 		return -EACCES;
511 
512 	if (is_compat()) {
513 		if (count != sizeof(*compat))
514 			return -EINVAL;
515 
516 		compat = (struct compat_efi_variable *)buf;
517 		name = compat->VariableName;
518 		vendor = compat->VendorGuid;
519 	} else {
520 		if (count != sizeof(*del_var))
521 			return -EINVAL;
522 
523 		name = del_var->VariableName;
524 		vendor = del_var->VendorGuid;
525 	}
526 
527 	if (efivar_entry_iter_begin())
528 		return -EINTR;
529 	entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true);
530 	if (!entry)
531 		err = -EINVAL;
532 	else if (__efivar_entry_delete(entry))
533 		err = -EIO;
534 
535 	if (err) {
536 		efivar_entry_iter_end();
537 		return err;
538 	}
539 
540 	if (!entry->scanning) {
541 		efivar_entry_iter_end();
542 		efivar_unregister(entry);
543 	} else
544 		efivar_entry_iter_end();
545 
546 	/* It's dead Jim.... */
547 	return count;
548 }
549 
550 /**
551  * efivar_create_sysfs_entry - create a new entry in sysfs
552  * @new_var: efivar entry to create
553  *
554  * Returns 0 on success, negative error code on failure
555  */
556 static int
efivar_create_sysfs_entry(struct efivar_entry * new_var)557 efivar_create_sysfs_entry(struct efivar_entry *new_var)
558 {
559 	int short_name_size;
560 	char *short_name;
561 	unsigned long utf8_name_size;
562 	efi_char16_t *variable_name = new_var->var.VariableName;
563 	int ret;
564 
565 	/*
566 	 * Length of the variable bytes in UTF8, plus the '-' separator,
567 	 * plus the GUID, plus trailing NUL
568 	 */
569 	utf8_name_size = ucs2_utf8size(variable_name);
570 	short_name_size = utf8_name_size + 1 + EFI_VARIABLE_GUID_LEN + 1;
571 
572 	short_name = kmalloc(short_name_size, GFP_KERNEL);
573 	if (!short_name)
574 		return -ENOMEM;
575 
576 	ucs2_as_utf8(short_name, variable_name, short_name_size);
577 
578 	/* This is ugly, but necessary to separate one vendor's
579 	   private variables from another's.         */
580 	short_name[utf8_name_size] = '-';
581 	efi_guid_to_str(&new_var->var.VendorGuid,
582 			 short_name + utf8_name_size + 1);
583 
584 	new_var->kobj.kset = efivars_kset;
585 
586 	ret = kobject_init_and_add(&new_var->kobj, &efivar_ktype,
587 				   NULL, "%s", short_name);
588 	kfree(short_name);
589 	if (ret)
590 		return ret;
591 
592 	kobject_uevent(&new_var->kobj, KOBJ_ADD);
593 	if (efivar_entry_add(new_var, &efivar_sysfs_list)) {
594 		efivar_unregister(new_var);
595 		return -EINTR;
596 	}
597 
598 	return 0;
599 }
600 
601 static int
create_efivars_bin_attributes(void)602 create_efivars_bin_attributes(void)
603 {
604 	struct bin_attribute *attr;
605 	int error;
606 
607 	/* new_var */
608 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
609 	if (!attr)
610 		return -ENOMEM;
611 
612 	attr->attr.name = "new_var";
613 	attr->attr.mode = 0200;
614 	attr->write = efivar_create;
615 	efivars_new_var = attr;
616 
617 	/* del_var */
618 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
619 	if (!attr) {
620 		error = -ENOMEM;
621 		goto out_free;
622 	}
623 	attr->attr.name = "del_var";
624 	attr->attr.mode = 0200;
625 	attr->write = efivar_delete;
626 	efivars_del_var = attr;
627 
628 	sysfs_bin_attr_init(efivars_new_var);
629 	sysfs_bin_attr_init(efivars_del_var);
630 
631 	/* Register */
632 	error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_new_var);
633 	if (error) {
634 		printk(KERN_ERR "efivars: unable to create new_var sysfs file"
635 			" due to error %d\n", error);
636 		goto out_free;
637 	}
638 
639 	error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_del_var);
640 	if (error) {
641 		printk(KERN_ERR "efivars: unable to create del_var sysfs file"
642 			" due to error %d\n", error);
643 		sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
644 		goto out_free;
645 	}
646 
647 	return 0;
648 out_free:
649 	kfree(efivars_del_var);
650 	efivars_del_var = NULL;
651 	kfree(efivars_new_var);
652 	efivars_new_var = NULL;
653 	return error;
654 }
655 
efivar_update_sysfs_entry(efi_char16_t * name,efi_guid_t vendor,unsigned long name_size,void * data)656 static int efivar_update_sysfs_entry(efi_char16_t *name, efi_guid_t vendor,
657 				     unsigned long name_size, void *data)
658 {
659 	struct efivar_entry *entry = data;
660 
661 	if (efivar_entry_find(name, vendor, &efivar_sysfs_list, false))
662 		return 0;
663 
664 	memcpy(entry->var.VariableName, name, name_size);
665 	memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
666 
667 	return 1;
668 }
669 
efivar_update_sysfs_entries(struct work_struct * work)670 static void efivar_update_sysfs_entries(struct work_struct *work)
671 {
672 	struct efivar_entry *entry;
673 	int err;
674 
675 	/* Add new sysfs entries */
676 	while (1) {
677 		entry = kzalloc(sizeof(*entry), GFP_KERNEL);
678 		if (!entry)
679 			return;
680 
681 		err = efivar_init(efivar_update_sysfs_entry, entry,
682 				  false, &efivar_sysfs_list);
683 		if (!err)
684 			break;
685 
686 		efivar_create_sysfs_entry(entry);
687 	}
688 
689 	kfree(entry);
690 }
691 
efivars_sysfs_callback(efi_char16_t * name,efi_guid_t vendor,unsigned long name_size,void * data)692 static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
693 				  unsigned long name_size, void *data)
694 {
695 	struct efivar_entry *entry;
696 
697 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
698 	if (!entry)
699 		return -ENOMEM;
700 
701 	memcpy(entry->var.VariableName, name, name_size);
702 	memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
703 
704 	efivar_create_sysfs_entry(entry);
705 
706 	return 0;
707 }
708 
efivar_sysfs_destroy(struct efivar_entry * entry,void * data)709 static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
710 {
711 	int err = efivar_entry_remove(entry);
712 
713 	if (err)
714 		return err;
715 	efivar_unregister(entry);
716 	return 0;
717 }
718 
efivars_sysfs_exit(void)719 static void efivars_sysfs_exit(void)
720 {
721 	/* Remove all entries and destroy */
722 	int err;
723 
724 	err = __efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list,
725 				  NULL, NULL);
726 	if (err) {
727 		pr_err("efivars: Failed to destroy sysfs entries\n");
728 		return;
729 	}
730 
731 	if (efivars_new_var)
732 		sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
733 	if (efivars_del_var)
734 		sysfs_remove_bin_file(&efivars_kset->kobj, efivars_del_var);
735 	kfree(efivars_new_var);
736 	kfree(efivars_del_var);
737 	kset_unregister(efivars_kset);
738 }
739 
efivars_sysfs_init(void)740 int efivars_sysfs_init(void)
741 {
742 	struct kobject *parent_kobj = efivars_kobject();
743 	int error = 0;
744 
745 	if (!efi_enabled(EFI_RUNTIME_SERVICES))
746 		return -ENODEV;
747 
748 	/* No efivars has been registered yet */
749 	if (!parent_kobj)
750 		return 0;
751 
752 	printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
753 	       EFIVARS_DATE);
754 
755 	efivars_kset = kset_create_and_add("vars", NULL, parent_kobj);
756 	if (!efivars_kset) {
757 		printk(KERN_ERR "efivars: Subsystem registration failed.\n");
758 		return -ENOMEM;
759 	}
760 
761 	efivar_init(efivars_sysfs_callback, NULL, true, &efivar_sysfs_list);
762 
763 	error = create_efivars_bin_attributes();
764 	if (error) {
765 		efivars_sysfs_exit();
766 		return error;
767 	}
768 
769 	INIT_WORK(&efivar_work, efivar_update_sysfs_entries);
770 
771 	return 0;
772 }
773 EXPORT_SYMBOL_GPL(efivars_sysfs_init);
774 
775 module_init(efivars_sysfs_init);
776 module_exit(efivars_sysfs_exit);
777