xref: /openbmc/linux/drivers/firmware/efi/vars.c (revision 0f5b2c69)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Originally from efivars.c
4  *
5  * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
6  * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
7  */
8 
9 #include <linux/capability.h>
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/init.h>
13 #include <linux/mm.h>
14 #include <linux/module.h>
15 #include <linux/string.h>
16 #include <linux/smp.h>
17 #include <linux/efi.h>
18 #include <linux/sysfs.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/ctype.h>
22 #include <linux/ucs2_string.h>
23 
24 /* Private pointer to registered efivars */
25 static struct efivars *__efivars;
26 
27 /*
28  * efivars_lock protects three things:
29  * 1) efivarfs_list and efivars_sysfs_list
30  * 2) ->ops calls
31  * 3) (un)registration of __efivars
32  */
33 static DEFINE_SEMAPHORE(efivars_lock);
34 
35 static bool
36 validate_device_path(efi_char16_t *var_name, int match, u8 *buffer,
37 		     unsigned long len)
38 {
39 	struct efi_generic_dev_path *node;
40 	int offset = 0;
41 
42 	node = (struct efi_generic_dev_path *)buffer;
43 
44 	if (len < sizeof(*node))
45 		return false;
46 
47 	while (offset <= len - sizeof(*node) &&
48 	       node->length >= sizeof(*node) &&
49 		node->length <= len - offset) {
50 		offset += node->length;
51 
52 		if ((node->type == EFI_DEV_END_PATH ||
53 		     node->type == EFI_DEV_END_PATH2) &&
54 		    node->sub_type == EFI_DEV_END_ENTIRE)
55 			return true;
56 
57 		node = (struct efi_generic_dev_path *)(buffer + offset);
58 	}
59 
60 	/*
61 	 * If we're here then either node->length pointed past the end
62 	 * of the buffer or we reached the end of the buffer without
63 	 * finding a device path end node.
64 	 */
65 	return false;
66 }
67 
68 static bool
69 validate_boot_order(efi_char16_t *var_name, int match, u8 *buffer,
70 		    unsigned long len)
71 {
72 	/* An array of 16-bit integers */
73 	if ((len % 2) != 0)
74 		return false;
75 
76 	return true;
77 }
78 
79 static bool
80 validate_load_option(efi_char16_t *var_name, int match, u8 *buffer,
81 		     unsigned long len)
82 {
83 	u16 filepathlength;
84 	int i, desclength = 0, namelen;
85 
86 	namelen = ucs2_strnlen(var_name, EFI_VAR_NAME_LEN);
87 
88 	/* Either "Boot" or "Driver" followed by four digits of hex */
89 	for (i = match; i < match+4; i++) {
90 		if (var_name[i] > 127 ||
91 		    hex_to_bin(var_name[i] & 0xff) < 0)
92 			return true;
93 	}
94 
95 	/* Reject it if there's 4 digits of hex and then further content */
96 	if (namelen > match + 4)
97 		return false;
98 
99 	/* A valid entry must be at least 8 bytes */
100 	if (len < 8)
101 		return false;
102 
103 	filepathlength = buffer[4] | buffer[5] << 8;
104 
105 	/*
106 	 * There's no stored length for the description, so it has to be
107 	 * found by hand
108 	 */
109 	desclength = ucs2_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
110 
111 	/* Each boot entry must have a descriptor */
112 	if (!desclength)
113 		return false;
114 
115 	/*
116 	 * If the sum of the length of the description, the claimed filepath
117 	 * length and the original header are greater than the length of the
118 	 * variable, it's malformed
119 	 */
120 	if ((desclength + filepathlength + 6) > len)
121 		return false;
122 
123 	/*
124 	 * And, finally, check the filepath
125 	 */
126 	return validate_device_path(var_name, match, buffer + desclength + 6,
127 				    filepathlength);
128 }
129 
130 static bool
131 validate_uint16(efi_char16_t *var_name, int match, u8 *buffer,
132 		unsigned long len)
133 {
134 	/* A single 16-bit integer */
135 	if (len != 2)
136 		return false;
137 
138 	return true;
139 }
140 
141 static bool
142 validate_ascii_string(efi_char16_t *var_name, int match, u8 *buffer,
143 		      unsigned long len)
144 {
145 	int i;
146 
147 	for (i = 0; i < len; i++) {
148 		if (buffer[i] > 127)
149 			return false;
150 
151 		if (buffer[i] == 0)
152 			return true;
153 	}
154 
155 	return false;
156 }
157 
158 struct variable_validate {
159 	efi_guid_t vendor;
160 	char *name;
161 	bool (*validate)(efi_char16_t *var_name, int match, u8 *data,
162 			 unsigned long len);
163 };
164 
165 /*
166  * This is the list of variables we need to validate, as well as the
167  * whitelist for what we think is safe not to default to immutable.
168  *
169  * If it has a validate() method that's not NULL, it'll go into the
170  * validation routine.  If not, it is assumed valid, but still used for
171  * whitelisting.
172  *
173  * Note that it's sorted by {vendor,name}, but globbed names must come after
174  * any other name with the same prefix.
175  */
176 static const struct variable_validate variable_validate[] = {
177 	{ EFI_GLOBAL_VARIABLE_GUID, "BootNext", validate_uint16 },
178 	{ EFI_GLOBAL_VARIABLE_GUID, "BootOrder", validate_boot_order },
179 	{ EFI_GLOBAL_VARIABLE_GUID, "Boot*", validate_load_option },
180 	{ EFI_GLOBAL_VARIABLE_GUID, "DriverOrder", validate_boot_order },
181 	{ EFI_GLOBAL_VARIABLE_GUID, "Driver*", validate_load_option },
182 	{ EFI_GLOBAL_VARIABLE_GUID, "ConIn", validate_device_path },
183 	{ EFI_GLOBAL_VARIABLE_GUID, "ConInDev", validate_device_path },
184 	{ EFI_GLOBAL_VARIABLE_GUID, "ConOut", validate_device_path },
185 	{ EFI_GLOBAL_VARIABLE_GUID, "ConOutDev", validate_device_path },
186 	{ EFI_GLOBAL_VARIABLE_GUID, "ErrOut", validate_device_path },
187 	{ EFI_GLOBAL_VARIABLE_GUID, "ErrOutDev", validate_device_path },
188 	{ EFI_GLOBAL_VARIABLE_GUID, "Lang", validate_ascii_string },
189 	{ EFI_GLOBAL_VARIABLE_GUID, "OsIndications", NULL },
190 	{ EFI_GLOBAL_VARIABLE_GUID, "PlatformLang", validate_ascii_string },
191 	{ EFI_GLOBAL_VARIABLE_GUID, "Timeout", validate_uint16 },
192 	{ LINUX_EFI_CRASH_GUID, "*", NULL },
193 	{ NULL_GUID, "", NULL },
194 };
195 
196 /*
197  * Check if @var_name matches the pattern given in @match_name.
198  *
199  * @var_name: an array of @len non-NUL characters.
200  * @match_name: a NUL-terminated pattern string, optionally ending in "*". A
201  *              final "*" character matches any trailing characters @var_name,
202  *              including the case when there are none left in @var_name.
203  * @match: on output, the number of non-wildcard characters in @match_name
204  *         that @var_name matches, regardless of the return value.
205  * @return: whether @var_name fully matches @match_name.
206  */
207 static bool
208 variable_matches(const char *var_name, size_t len, const char *match_name,
209 		 int *match)
210 {
211 	for (*match = 0; ; (*match)++) {
212 		char c = match_name[*match];
213 
214 		switch (c) {
215 		case '*':
216 			/* Wildcard in @match_name means we've matched. */
217 			return true;
218 
219 		case '\0':
220 			/* @match_name has ended. Has @var_name too? */
221 			return (*match == len);
222 
223 		default:
224 			/*
225 			 * We've reached a non-wildcard char in @match_name.
226 			 * Continue only if there's an identical character in
227 			 * @var_name.
228 			 */
229 			if (*match < len && c == var_name[*match])
230 				continue;
231 			return false;
232 		}
233 	}
234 }
235 
236 bool
237 efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data,
238 		unsigned long data_size)
239 {
240 	int i;
241 	unsigned long utf8_size;
242 	u8 *utf8_name;
243 
244 	utf8_size = ucs2_utf8size(var_name);
245 	utf8_name = kmalloc(utf8_size + 1, GFP_KERNEL);
246 	if (!utf8_name)
247 		return false;
248 
249 	ucs2_as_utf8(utf8_name, var_name, utf8_size);
250 	utf8_name[utf8_size] = '\0';
251 
252 	for (i = 0; variable_validate[i].name[0] != '\0'; i++) {
253 		const char *name = variable_validate[i].name;
254 		int match = 0;
255 
256 		if (efi_guidcmp(vendor, variable_validate[i].vendor))
257 			continue;
258 
259 		if (variable_matches(utf8_name, utf8_size+1, name, &match)) {
260 			if (variable_validate[i].validate == NULL)
261 				break;
262 			kfree(utf8_name);
263 			return variable_validate[i].validate(var_name, match,
264 							     data, data_size);
265 		}
266 	}
267 	kfree(utf8_name);
268 	return true;
269 }
270 EXPORT_SYMBOL_GPL(efivar_validate);
271 
272 bool
273 efivar_variable_is_removable(efi_guid_t vendor, const char *var_name,
274 			     size_t len)
275 {
276 	int i;
277 	bool found = false;
278 	int match = 0;
279 
280 	/*
281 	 * Check if our variable is in the validated variables list
282 	 */
283 	for (i = 0; variable_validate[i].name[0] != '\0'; i++) {
284 		if (efi_guidcmp(variable_validate[i].vendor, vendor))
285 			continue;
286 
287 		if (variable_matches(var_name, len,
288 				     variable_validate[i].name, &match)) {
289 			found = true;
290 			break;
291 		}
292 	}
293 
294 	/*
295 	 * If it's in our list, it is removable.
296 	 */
297 	return found;
298 }
299 EXPORT_SYMBOL_GPL(efivar_variable_is_removable);
300 
301 efi_status_t check_var_size(u32 attributes, unsigned long size)
302 {
303 	const struct efivar_operations *fops;
304 
305 	fops = __efivars->ops;
306 
307 	if (!fops->query_variable_store)
308 		return EFI_UNSUPPORTED;
309 
310 	return fops->query_variable_store(attributes, size, false);
311 }
312 EXPORT_SYMBOL_NS_GPL(check_var_size, EFIVAR);
313 
314 efi_status_t check_var_size_nonblocking(u32 attributes, unsigned long size)
315 {
316 	const struct efivar_operations *fops;
317 
318 	fops = __efivars->ops;
319 
320 	if (!fops->query_variable_store)
321 		return EFI_UNSUPPORTED;
322 
323 	return fops->query_variable_store(attributes, size, true);
324 }
325 EXPORT_SYMBOL_NS_GPL(check_var_size_nonblocking, EFIVAR);
326 
327 static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor,
328 				struct list_head *head)
329 {
330 	struct efivar_entry *entry, *n;
331 	unsigned long strsize1, strsize2;
332 	bool found = false;
333 
334 	strsize1 = ucs2_strsize(variable_name, 1024);
335 	list_for_each_entry_safe(entry, n, head, list) {
336 		strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
337 		if (strsize1 == strsize2 &&
338 			!memcmp(variable_name, &(entry->var.VariableName),
339 				strsize2) &&
340 			!efi_guidcmp(entry->var.VendorGuid,
341 				*vendor)) {
342 			found = true;
343 			break;
344 		}
345 	}
346 	return found;
347 }
348 
349 /*
350  * Returns the size of variable_name, in bytes, including the
351  * terminating NULL character, or variable_name_size if no NULL
352  * character is found among the first variable_name_size bytes.
353  */
354 static unsigned long var_name_strnsize(efi_char16_t *variable_name,
355 				       unsigned long variable_name_size)
356 {
357 	unsigned long len;
358 	efi_char16_t c;
359 
360 	/*
361 	 * The variable name is, by definition, a NULL-terminated
362 	 * string, so make absolutely sure that variable_name_size is
363 	 * the value we expect it to be. If not, return the real size.
364 	 */
365 	for (len = 2; len <= variable_name_size; len += sizeof(c)) {
366 		c = variable_name[(len / sizeof(c)) - 1];
367 		if (!c)
368 			break;
369 	}
370 
371 	return min(len, variable_name_size);
372 }
373 
374 /*
375  * Print a warning when duplicate EFI variables are encountered and
376  * disable the sysfs workqueue since the firmware is buggy.
377  */
378 static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid,
379 			     unsigned long len16)
380 {
381 	size_t i, len8 = len16 / sizeof(efi_char16_t);
382 	char *str8;
383 
384 	str8 = kzalloc(len8, GFP_KERNEL);
385 	if (!str8)
386 		return;
387 
388 	for (i = 0; i < len8; i++)
389 		str8[i] = str16[i];
390 
391 	printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
392 	       str8, vendor_guid);
393 	kfree(str8);
394 }
395 
396 /**
397  * efivar_init - build the initial list of EFI variables
398  * @func: callback function to invoke for every variable
399  * @data: function-specific data to pass to @func
400  * @duplicates: error if we encounter duplicates on @head?
401  * @head: initialised head of variable list
402  *
403  * Get every EFI variable from the firmware and invoke @func. @func
404  * should call efivar_entry_add() to build the list of variables.
405  *
406  * Returns 0 on success, or a kernel error code on failure.
407  */
408 int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
409 		void *data, bool duplicates, struct list_head *head)
410 {
411 	const struct efivar_operations *ops;
412 	unsigned long variable_name_size = 1024;
413 	efi_char16_t *variable_name;
414 	efi_status_t status;
415 	efi_guid_t vendor_guid;
416 	int err = 0;
417 
418 	if (!__efivars)
419 		return -EFAULT;
420 
421 	ops = __efivars->ops;
422 
423 	variable_name = kzalloc(variable_name_size, GFP_KERNEL);
424 	if (!variable_name) {
425 		printk(KERN_ERR "efivars: Memory allocation failed.\n");
426 		return -ENOMEM;
427 	}
428 
429 	if (down_interruptible(&efivars_lock)) {
430 		err = -EINTR;
431 		goto free;
432 	}
433 
434 	/*
435 	 * Per EFI spec, the maximum storage allocated for both
436 	 * the variable name and variable data is 1024 bytes.
437 	 */
438 
439 	do {
440 		variable_name_size = 1024;
441 
442 		status = ops->get_next_variable(&variable_name_size,
443 						variable_name,
444 						&vendor_guid);
445 		switch (status) {
446 		case EFI_SUCCESS:
447 			variable_name_size = var_name_strnsize(variable_name,
448 							       variable_name_size);
449 
450 			/*
451 			 * Some firmware implementations return the
452 			 * same variable name on multiple calls to
453 			 * get_next_variable(). Terminate the loop
454 			 * immediately as there is no guarantee that
455 			 * we'll ever see a different variable name,
456 			 * and may end up looping here forever.
457 			 */
458 			if (duplicates &&
459 			    variable_is_present(variable_name, &vendor_guid,
460 						head)) {
461 				dup_variable_bug(variable_name, &vendor_guid,
462 						 variable_name_size);
463 				status = EFI_NOT_FOUND;
464 			} else {
465 				err = func(variable_name, vendor_guid,
466 					   variable_name_size, data);
467 				if (err)
468 					status = EFI_NOT_FOUND;
469 			}
470 			break;
471 		case EFI_UNSUPPORTED:
472 			err = -EOPNOTSUPP;
473 			status = EFI_NOT_FOUND;
474 			break;
475 		case EFI_NOT_FOUND:
476 			break;
477 		default:
478 			printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
479 				status);
480 			status = EFI_NOT_FOUND;
481 			break;
482 		}
483 
484 	} while (status != EFI_NOT_FOUND);
485 
486 	up(&efivars_lock);
487 free:
488 	kfree(variable_name);
489 
490 	return err;
491 }
492 EXPORT_SYMBOL_GPL(efivar_init);
493 
494 /**
495  * efivar_entry_add - add entry to variable list
496  * @entry: entry to add to list
497  * @head: list head
498  *
499  * Returns 0 on success, or a kernel error code on failure.
500  */
501 int efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
502 {
503 	if (down_interruptible(&efivars_lock))
504 		return -EINTR;
505 	list_add(&entry->list, head);
506 	up(&efivars_lock);
507 
508 	return 0;
509 }
510 EXPORT_SYMBOL_GPL(efivar_entry_add);
511 
512 /**
513  * __efivar_entry_add - add entry to variable list
514  * @entry: entry to add to list
515  * @head: list head
516  */
517 void __efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
518 {
519 	list_add(&entry->list, head);
520 }
521 EXPORT_SYMBOL_GPL(__efivar_entry_add);
522 
523 /**
524  * efivar_entry_remove - remove entry from variable list
525  * @entry: entry to remove from list
526  */
527 void efivar_entry_remove(struct efivar_entry *entry)
528 {
529 	list_del(&entry->list);
530 }
531 EXPORT_SYMBOL_GPL(efivar_entry_remove);
532 
533 /*
534  * efivar_entry_list_del_unlock - remove entry from variable list
535  * @entry: entry to remove
536  *
537  * Remove @entry from the variable list and release the list lock.
538  *
539  * NOTE: slightly weird locking semantics here - we expect to be
540  * called with the efivars lock already held, and we release it before
541  * returning. This is because this function is usually called after
542  * set_variable() while the lock is still held.
543  */
544 static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
545 {
546 	list_del(&entry->list);
547 	up(&efivars_lock);
548 }
549 
550 /**
551  * efivar_entry_delete - delete variable and remove entry from list
552  * @entry: entry containing variable to delete
553  *
554  * Delete the variable from the firmware and remove @entry from the
555  * variable list. It is the caller's responsibility to free @entry
556  * once we return.
557  *
558  * Returns 0 on success, -EINTR if we can't grab the semaphore,
559  * converted EFI status code if set_variable() fails.
560  */
561 int efivar_entry_delete(struct efivar_entry *entry)
562 {
563 	const struct efivar_operations *ops;
564 	efi_status_t status;
565 
566 	if (down_interruptible(&efivars_lock))
567 		return -EINTR;
568 
569 	if (!__efivars) {
570 		up(&efivars_lock);
571 		return -EINVAL;
572 	}
573 	ops = __efivars->ops;
574 	status = ops->set_variable(entry->var.VariableName,
575 				   &entry->var.VendorGuid,
576 				   0, 0, NULL);
577 	if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
578 		up(&efivars_lock);
579 		return efi_status_to_err(status);
580 	}
581 
582 	efivar_entry_list_del_unlock(entry);
583 	return 0;
584 }
585 EXPORT_SYMBOL_GPL(efivar_entry_delete);
586 
587 /**
588  * efivar_entry_size - obtain the size of a variable
589  * @entry: entry for this variable
590  * @size: location to store the variable's size
591  */
592 int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
593 {
594 	const struct efivar_operations *ops;
595 	efi_status_t status;
596 
597 	*size = 0;
598 
599 	if (down_interruptible(&efivars_lock))
600 		return -EINTR;
601 	if (!__efivars) {
602 		up(&efivars_lock);
603 		return -EINVAL;
604 	}
605 	ops = __efivars->ops;
606 	status = ops->get_variable(entry->var.VariableName,
607 				   &entry->var.VendorGuid, NULL, size, NULL);
608 	up(&efivars_lock);
609 
610 	if (status != EFI_BUFFER_TOO_SMALL)
611 		return efi_status_to_err(status);
612 
613 	return 0;
614 }
615 EXPORT_SYMBOL_GPL(efivar_entry_size);
616 
617 /**
618  * __efivar_entry_get - call get_variable()
619  * @entry: read data for this variable
620  * @attributes: variable attributes
621  * @size: size of @data buffer
622  * @data: buffer to store variable data
623  *
624  * The caller MUST call efivar_entry_iter_begin() and
625  * efivar_entry_iter_end() before and after the invocation of this
626  * function, respectively.
627  */
628 int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
629 		       unsigned long *size, void *data)
630 {
631 	efi_status_t status;
632 
633 	if (!__efivars)
634 		return -EINVAL;
635 
636 	status = __efivars->ops->get_variable(entry->var.VariableName,
637 					      &entry->var.VendorGuid,
638 					      attributes, size, data);
639 
640 	return efi_status_to_err(status);
641 }
642 EXPORT_SYMBOL_GPL(__efivar_entry_get);
643 
644 /**
645  * efivar_entry_get - call get_variable()
646  * @entry: read data for this variable
647  * @attributes: variable attributes
648  * @size: size of @data buffer
649  * @data: buffer to store variable data
650  */
651 int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
652 		     unsigned long *size, void *data)
653 {
654 	efi_status_t status;
655 
656 	if (down_interruptible(&efivars_lock))
657 		return -EINTR;
658 
659 	if (!__efivars) {
660 		up(&efivars_lock);
661 		return -EINVAL;
662 	}
663 
664 	status = __efivars->ops->get_variable(entry->var.VariableName,
665 					      &entry->var.VendorGuid,
666 					      attributes, size, data);
667 	up(&efivars_lock);
668 
669 	return efi_status_to_err(status);
670 }
671 EXPORT_SYMBOL_GPL(efivar_entry_get);
672 
673 /**
674  * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
675  * @entry: entry containing variable to set and get
676  * @attributes: attributes of variable to be written
677  * @size: size of data buffer
678  * @data: buffer containing data to write
679  * @set: did the set_variable() call succeed?
680  *
681  * This is a pretty special (complex) function. See efivarfs_file_write().
682  *
683  * Atomically call set_variable() for @entry and if the call is
684  * successful, return the new size of the variable from get_variable()
685  * in @size. The success of set_variable() is indicated by @set.
686  *
687  * Returns 0 on success, -EINVAL if the variable data is invalid,
688  * -ENOSPC if the firmware does not have enough available space, or a
689  * converted EFI status code if either of set_variable() or
690  * get_variable() fail.
691  *
692  * If the EFI variable does not exist when calling set_variable()
693  * (EFI_NOT_FOUND), @entry is removed from the variable list.
694  */
695 int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
696 			      unsigned long *size, void *data, bool *set)
697 {
698 	const struct efivar_operations *ops;
699 	efi_char16_t *name = entry->var.VariableName;
700 	efi_guid_t *vendor = &entry->var.VendorGuid;
701 	efi_status_t status;
702 	int err;
703 
704 	*set = false;
705 
706 	if (efivar_validate(*vendor, name, data, *size) == false)
707 		return -EINVAL;
708 
709 	/*
710 	 * The lock here protects the get_variable call, the conditional
711 	 * set_variable call, and removal of the variable from the efivars
712 	 * list (in the case of an authenticated delete).
713 	 */
714 	if (down_interruptible(&efivars_lock))
715 		return -EINTR;
716 
717 	if (!__efivars) {
718 		err = -EINVAL;
719 		goto out;
720 	}
721 
722 	/*
723 	 * Ensure that the available space hasn't shrunk below the safe level
724 	 */
725 	status = check_var_size(attributes, *size + ucs2_strsize(name, 1024));
726 	if (status != EFI_SUCCESS) {
727 		if (status != EFI_UNSUPPORTED) {
728 			err = efi_status_to_err(status);
729 			goto out;
730 		}
731 
732 		if (*size > 65536) {
733 			err = -ENOSPC;
734 			goto out;
735 		}
736 	}
737 
738 	ops = __efivars->ops;
739 
740 	status = ops->set_variable(name, vendor, attributes, *size, data);
741 	if (status != EFI_SUCCESS) {
742 		err = efi_status_to_err(status);
743 		goto out;
744 	}
745 
746 	*set = true;
747 
748 	/*
749 	 * Writing to the variable may have caused a change in size (which
750 	 * could either be an append or an overwrite), or the variable to be
751 	 * deleted. Perform a GetVariable() so we can tell what actually
752 	 * happened.
753 	 */
754 	*size = 0;
755 	status = ops->get_variable(entry->var.VariableName,
756 				   &entry->var.VendorGuid,
757 				   NULL, size, NULL);
758 
759 	if (status == EFI_NOT_FOUND)
760 		efivar_entry_list_del_unlock(entry);
761 	else
762 		up(&efivars_lock);
763 
764 	if (status && status != EFI_BUFFER_TOO_SMALL)
765 		return efi_status_to_err(status);
766 
767 	return 0;
768 
769 out:
770 	up(&efivars_lock);
771 	return err;
772 
773 }
774 EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
775 
776 /**
777  * efivar_entry_iter - iterate over variable list
778  * @func: callback function
779  * @head: head of variable list
780  * @data: function-specific data to pass to callback
781  *
782  * Iterate over the list of EFI variables and call @func with every
783  * entry on the list. It is safe for @func to remove entries in the
784  * list via efivar_entry_delete() while iterating.
785  *
786  * Some notes for the callback function:
787  *  - a non-zero return value indicates an error and terminates the loop
788  *  - @func is called from atomic context
789  */
790 int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
791 		      struct list_head *head, void *data)
792 {
793 	struct efivar_entry *entry, *n;
794 	int err = 0;
795 
796 	err = down_interruptible(&efivars_lock);
797 	if (err)
798 		return err;
799 
800 	list_for_each_entry_safe(entry, n, head, list) {
801 		err = func(entry, data);
802 		if (err)
803 			break;
804 	}
805 	up(&efivars_lock);
806 
807 	return err;
808 }
809 EXPORT_SYMBOL_GPL(efivar_entry_iter);
810 
811 /**
812  * efivars_kobject - get the kobject for the registered efivars
813  *
814  * If efivars_register() has not been called we return NULL,
815  * otherwise return the kobject used at registration time.
816  */
817 struct kobject *efivars_kobject(void)
818 {
819 	if (!__efivars)
820 		return NULL;
821 
822 	return __efivars->kobject;
823 }
824 EXPORT_SYMBOL_GPL(efivars_kobject);
825 
826 /**
827  * efivars_register - register an efivars
828  * @efivars: efivars to register
829  * @ops: efivars operations
830  * @kobject: @efivars-specific kobject
831  *
832  * Only a single efivars can be registered at any time.
833  */
834 int efivars_register(struct efivars *efivars,
835 		     const struct efivar_operations *ops,
836 		     struct kobject *kobject)
837 {
838 	if (down_interruptible(&efivars_lock))
839 		return -EINTR;
840 
841 	efivars->ops = ops;
842 	efivars->kobject = kobject;
843 
844 	__efivars = efivars;
845 
846 	pr_info("Registered efivars operations\n");
847 
848 	up(&efivars_lock);
849 
850 	return 0;
851 }
852 EXPORT_SYMBOL_GPL(efivars_register);
853 
854 /**
855  * efivars_unregister - unregister an efivars
856  * @efivars: efivars to unregister
857  *
858  * The caller must have already removed every entry from the list,
859  * failure to do so is an error.
860  */
861 int efivars_unregister(struct efivars *efivars)
862 {
863 	int rv;
864 
865 	if (down_interruptible(&efivars_lock))
866 		return -EINTR;
867 
868 	if (!__efivars) {
869 		printk(KERN_ERR "efivars not registered\n");
870 		rv = -EINVAL;
871 		goto out;
872 	}
873 
874 	if (__efivars != efivars) {
875 		rv = -EINVAL;
876 		goto out;
877 	}
878 
879 	pr_info("Unregistered efivars operations\n");
880 	__efivars = NULL;
881 
882 	rv = 0;
883 out:
884 	up(&efivars_lock);
885 	return rv;
886 }
887 EXPORT_SYMBOL_GPL(efivars_unregister);
888 
889 int efivar_supports_writes(void)
890 {
891 	return __efivars && __efivars->ops->set_variable;
892 }
893 EXPORT_SYMBOL_GPL(efivar_supports_writes);
894 
895 /*
896  * efivar_lock() - obtain the efivar lock, wait for it if needed
897  * @return 0 on success, error code on failure
898  */
899 int efivar_lock(void)
900 {
901 	if (down_interruptible(&efivars_lock))
902 		return -EINTR;
903 	if (!__efivars->ops) {
904 		up(&efivars_lock);
905 		return -ENODEV;
906 	}
907 	return 0;
908 }
909 EXPORT_SYMBOL_NS_GPL(efivar_lock, EFIVAR);
910 
911 /*
912  * efivar_lock() - obtain the efivar lock if it is free
913  * @return 0 on success, error code on failure
914  */
915 int efivar_trylock(void)
916 {
917 	if (down_trylock(&efivars_lock))
918 		 return -EBUSY;
919 	if (!__efivars->ops) {
920 		up(&efivars_lock);
921 		return -ENODEV;
922 	}
923 	return 0;
924 }
925 EXPORT_SYMBOL_NS_GPL(efivar_trylock, EFIVAR);
926 
927 /*
928  * efivar_unlock() - release the efivar lock
929  */
930 void efivar_unlock(void)
931 {
932 	up(&efivars_lock);
933 }
934 EXPORT_SYMBOL_NS_GPL(efivar_unlock, EFIVAR);
935 
936 /*
937  * efivar_get_variable() - retrieve a variable identified by name/vendor
938  *
939  * Must be called with efivars_lock held.
940  */
941 efi_status_t efivar_get_variable(efi_char16_t *name, efi_guid_t *vendor,
942 				 u32 *attr, unsigned long *size, void *data)
943 {
944 	return __efivars->ops->get_variable(name, vendor, attr, size, data);
945 }
946 EXPORT_SYMBOL_NS_GPL(efivar_get_variable, EFIVAR);
947 
948 /*
949  * efivar_get_next_variable() - enumerate the next name/vendor pair
950  *
951  * Must be called with efivars_lock held.
952  */
953 efi_status_t efivar_get_next_variable(unsigned long *name_size,
954 				      efi_char16_t *name, efi_guid_t *vendor)
955 {
956 	return __efivars->ops->get_next_variable(name_size, name, vendor);
957 }
958 EXPORT_SYMBOL_NS_GPL(efivar_get_next_variable, EFIVAR);
959 
960 /*
961  * efivar_set_variable_blocking() - local helper function for set_variable
962  *
963  * Must be called with efivars_lock held.
964  */
965 static efi_status_t
966 efivar_set_variable_blocking(efi_char16_t *name, efi_guid_t *vendor,
967 			     u32 attr, unsigned long data_size, void *data)
968 {
969 	efi_status_t status;
970 
971 	if (data_size > 0) {
972 		status = check_var_size(attr, data_size +
973 					      ucs2_strsize(name, 1024));
974 		if (status != EFI_SUCCESS)
975 			return status;
976 	}
977 	return __efivars->ops->set_variable(name, vendor, attr, data_size, data);
978 }
979 
980 /*
981  * efivar_set_variable_locked() - set a variable identified by name/vendor
982  *
983  * Must be called with efivars_lock held. If @nonblocking is set, it will use
984  * non-blocking primitives so it is guaranteed not to sleep.
985  */
986 efi_status_t efivar_set_variable_locked(efi_char16_t *name, efi_guid_t *vendor,
987 					u32 attr, unsigned long data_size,
988 					void *data, bool nonblocking)
989 {
990 	efi_set_variable_t *setvar;
991 	efi_status_t status;
992 
993 	if (!nonblocking)
994 		return efivar_set_variable_blocking(name, vendor, attr,
995 						    data_size, data);
996 
997 	/*
998 	 * If no _nonblocking variant exists, the ordinary one
999 	 * is assumed to be non-blocking.
1000 	 */
1001 	setvar = __efivars->ops->set_variable_nonblocking ?:
1002 		 __efivars->ops->set_variable;
1003 
1004 	if (data_size > 0) {
1005 		status = check_var_size_nonblocking(attr, data_size +
1006 							  ucs2_strsize(name, 1024));
1007 		if (status != EFI_SUCCESS)
1008 			return status;
1009 	}
1010 	return setvar(name, vendor, attr, data_size, data);
1011 }
1012 EXPORT_SYMBOL_NS_GPL(efivar_set_variable_locked, EFIVAR);
1013 
1014 /*
1015  * efivar_set_variable() - set a variable identified by name/vendor
1016  *
1017  * Can be called without holding the efivars_lock. Will sleep on obtaining the
1018  * lock, or on obtaining other locks that are needed in order to complete the
1019  * call.
1020  */
1021 efi_status_t efivar_set_variable(efi_char16_t *name, efi_guid_t *vendor,
1022 				 u32 attr, unsigned long data_size, void *data)
1023 {
1024 	efi_status_t status;
1025 
1026 	if (efivar_lock())
1027 		return EFI_ABORTED;
1028 
1029 	status = efivar_set_variable_blocking(name, vendor, attr, data_size, data);
1030 	efivar_unlock();
1031 	return status;
1032 }
1033 EXPORT_SYMBOL_NS_GPL(efivar_set_variable, EFIVAR);
1034