xref: /openbmc/linux/drivers/firmware/efi/vars.c (revision bbc6d2c6)
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 	unsigned long variable_name_size = 1024;
412 	efi_char16_t *variable_name;
413 	efi_status_t status;
414 	efi_guid_t vendor_guid;
415 	int err = 0;
416 
417 	variable_name = kzalloc(variable_name_size, GFP_KERNEL);
418 	if (!variable_name) {
419 		printk(KERN_ERR "efivars: Memory allocation failed.\n");
420 		return -ENOMEM;
421 	}
422 
423 	err = efivar_lock();
424 	if (err)
425 		goto free;
426 
427 	/*
428 	 * Per EFI spec, the maximum storage allocated for both
429 	 * the variable name and variable data is 1024 bytes.
430 	 */
431 
432 	do {
433 		variable_name_size = 1024;
434 
435 		status = efivar_get_next_variable(&variable_name_size,
436 						  variable_name,
437 						  &vendor_guid);
438 		switch (status) {
439 		case EFI_SUCCESS:
440 			variable_name_size = var_name_strnsize(variable_name,
441 							       variable_name_size);
442 
443 			/*
444 			 * Some firmware implementations return the
445 			 * same variable name on multiple calls to
446 			 * get_next_variable(). Terminate the loop
447 			 * immediately as there is no guarantee that
448 			 * we'll ever see a different variable name,
449 			 * and may end up looping here forever.
450 			 */
451 			if (duplicates &&
452 			    variable_is_present(variable_name, &vendor_guid,
453 						head)) {
454 				dup_variable_bug(variable_name, &vendor_guid,
455 						 variable_name_size);
456 				status = EFI_NOT_FOUND;
457 			} else {
458 				err = func(variable_name, vendor_guid,
459 					   variable_name_size, data);
460 				if (err)
461 					status = EFI_NOT_FOUND;
462 			}
463 			break;
464 		case EFI_UNSUPPORTED:
465 			err = -EOPNOTSUPP;
466 			status = EFI_NOT_FOUND;
467 			break;
468 		case EFI_NOT_FOUND:
469 			break;
470 		default:
471 			printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
472 				status);
473 			status = EFI_NOT_FOUND;
474 			break;
475 		}
476 
477 	} while (status != EFI_NOT_FOUND);
478 
479 	efivar_unlock();
480 free:
481 	kfree(variable_name);
482 
483 	return err;
484 }
485 EXPORT_SYMBOL_GPL(efivar_init);
486 
487 /**
488  * efivar_entry_add - add entry to variable list
489  * @entry: entry to add to list
490  * @head: list head
491  *
492  * Returns 0 on success, or a kernel error code on failure.
493  */
494 int efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
495 {
496 	int err;
497 
498 	err = efivar_lock();
499 	if (err)
500 		return err;
501 	list_add(&entry->list, head);
502 	efivar_unlock();
503 
504 	return 0;
505 }
506 EXPORT_SYMBOL_GPL(efivar_entry_add);
507 
508 /**
509  * __efivar_entry_add - add entry to variable list
510  * @entry: entry to add to list
511  * @head: list head
512  */
513 void __efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
514 {
515 	list_add(&entry->list, head);
516 }
517 EXPORT_SYMBOL_GPL(__efivar_entry_add);
518 
519 /**
520  * efivar_entry_remove - remove entry from variable list
521  * @entry: entry to remove from list
522  */
523 void efivar_entry_remove(struct efivar_entry *entry)
524 {
525 	list_del(&entry->list);
526 }
527 EXPORT_SYMBOL_GPL(efivar_entry_remove);
528 
529 /*
530  * efivar_entry_list_del_unlock - remove entry from variable list
531  * @entry: entry to remove
532  *
533  * Remove @entry from the variable list and release the list lock.
534  *
535  * NOTE: slightly weird locking semantics here - we expect to be
536  * called with the efivars lock already held, and we release it before
537  * returning. This is because this function is usually called after
538  * set_variable() while the lock is still held.
539  */
540 static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
541 {
542 	list_del(&entry->list);
543 	efivar_unlock();
544 }
545 
546 /**
547  * efivar_entry_delete - delete variable and remove entry from list
548  * @entry: entry containing variable to delete
549  *
550  * Delete the variable from the firmware and remove @entry from the
551  * variable list. It is the caller's responsibility to free @entry
552  * once we return.
553  *
554  * Returns 0 on success, -EINTR if we can't grab the semaphore,
555  * converted EFI status code if set_variable() fails.
556  */
557 int efivar_entry_delete(struct efivar_entry *entry)
558 {
559 	efi_status_t status;
560 	int err;
561 
562 	err = efivar_lock();
563 	if (err)
564 		return err;
565 
566 	status = efivar_set_variable_locked(entry->var.VariableName,
567 					    &entry->var.VendorGuid,
568 					    0, 0, NULL, false);
569 	if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
570 		efivar_unlock();
571 		return efi_status_to_err(status);
572 	}
573 
574 	efivar_entry_list_del_unlock(entry);
575 	return 0;
576 }
577 EXPORT_SYMBOL_GPL(efivar_entry_delete);
578 
579 /**
580  * efivar_entry_size - obtain the size of a variable
581  * @entry: entry for this variable
582  * @size: location to store the variable's size
583  */
584 int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
585 {
586 	efi_status_t status;
587 	int err;
588 
589 	*size = 0;
590 
591 	err = efivar_lock();
592 	if (err)
593 		return err;
594 
595 	status = efivar_get_variable(entry->var.VariableName,
596 				     &entry->var.VendorGuid, NULL, size, NULL);
597 	efivar_unlock();
598 
599 	if (status != EFI_BUFFER_TOO_SMALL)
600 		return efi_status_to_err(status);
601 
602 	return 0;
603 }
604 EXPORT_SYMBOL_GPL(efivar_entry_size);
605 
606 /**
607  * __efivar_entry_get - call get_variable()
608  * @entry: read data for this variable
609  * @attributes: variable attributes
610  * @size: size of @data buffer
611  * @data: buffer to store variable data
612  *
613  * The caller MUST hold the efivar lock when calling this function.
614  */
615 int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
616 		       unsigned long *size, void *data)
617 {
618 	efi_status_t status;
619 
620 	status = efivar_get_variable(entry->var.VariableName,
621 				     &entry->var.VendorGuid,
622 				     attributes, size, data);
623 
624 	return efi_status_to_err(status);
625 }
626 EXPORT_SYMBOL_GPL(__efivar_entry_get);
627 
628 /**
629  * efivar_entry_get - call get_variable()
630  * @entry: read data for this variable
631  * @attributes: variable attributes
632  * @size: size of @data buffer
633  * @data: buffer to store variable data
634  */
635 int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
636 		     unsigned long *size, void *data)
637 {
638 	int err;
639 
640 	err = efivar_lock();
641 	if (err)
642 		return err;
643 	err = __efivar_entry_get(entry, attributes, size, data);
644 	efivar_unlock();
645 
646 	return err;
647 }
648 EXPORT_SYMBOL_GPL(efivar_entry_get);
649 
650 /**
651  * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
652  * @entry: entry containing variable to set and get
653  * @attributes: attributes of variable to be written
654  * @size: size of data buffer
655  * @data: buffer containing data to write
656  * @set: did the set_variable() call succeed?
657  *
658  * This is a pretty special (complex) function. See efivarfs_file_write().
659  *
660  * Atomically call set_variable() for @entry and if the call is
661  * successful, return the new size of the variable from get_variable()
662  * in @size. The success of set_variable() is indicated by @set.
663  *
664  * Returns 0 on success, -EINVAL if the variable data is invalid,
665  * -ENOSPC if the firmware does not have enough available space, or a
666  * converted EFI status code if either of set_variable() or
667  * get_variable() fail.
668  *
669  * If the EFI variable does not exist when calling set_variable()
670  * (EFI_NOT_FOUND), @entry is removed from the variable list.
671  */
672 int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
673 			      unsigned long *size, void *data, bool *set)
674 {
675 	efi_char16_t *name = entry->var.VariableName;
676 	efi_guid_t *vendor = &entry->var.VendorGuid;
677 	efi_status_t status;
678 	int err;
679 
680 	*set = false;
681 
682 	if (efivar_validate(*vendor, name, data, *size) == false)
683 		return -EINVAL;
684 
685 	/*
686 	 * The lock here protects the get_variable call, the conditional
687 	 * set_variable call, and removal of the variable from the efivars
688 	 * list (in the case of an authenticated delete).
689 	 */
690 	err = efivar_lock();
691 	if (err)
692 		return err;
693 
694 	/*
695 	 * Ensure that the available space hasn't shrunk below the safe level
696 	 */
697 	status = check_var_size(attributes, *size + ucs2_strsize(name, 1024));
698 	if (status != EFI_SUCCESS) {
699 		if (status != EFI_UNSUPPORTED) {
700 			err = efi_status_to_err(status);
701 			goto out;
702 		}
703 
704 		if (*size > 65536) {
705 			err = -ENOSPC;
706 			goto out;
707 		}
708 	}
709 
710 	status = efivar_set_variable_locked(name, vendor, attributes, *size,
711 					    data, false);
712 	if (status != EFI_SUCCESS) {
713 		err = efi_status_to_err(status);
714 		goto out;
715 	}
716 
717 	*set = true;
718 
719 	/*
720 	 * Writing to the variable may have caused a change in size (which
721 	 * could either be an append or an overwrite), or the variable to be
722 	 * deleted. Perform a GetVariable() so we can tell what actually
723 	 * happened.
724 	 */
725 	*size = 0;
726 	status = efivar_get_variable(entry->var.VariableName,
727 				    &entry->var.VendorGuid,
728 				    NULL, size, NULL);
729 
730 	if (status == EFI_NOT_FOUND)
731 		efivar_entry_list_del_unlock(entry);
732 	else
733 		efivar_unlock();
734 
735 	if (status && status != EFI_BUFFER_TOO_SMALL)
736 		return efi_status_to_err(status);
737 
738 	return 0;
739 
740 out:
741 	efivar_unlock();
742 	return err;
743 
744 }
745 EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
746 
747 /**
748  * efivar_entry_iter - iterate over variable list
749  * @func: callback function
750  * @head: head of variable list
751  * @data: function-specific data to pass to callback
752  *
753  * Iterate over the list of EFI variables and call @func with every
754  * entry on the list. It is safe for @func to remove entries in the
755  * list via efivar_entry_delete() while iterating.
756  *
757  * Some notes for the callback function:
758  *  - a non-zero return value indicates an error and terminates the loop
759  *  - @func is called from atomic context
760  */
761 int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
762 		      struct list_head *head, void *data)
763 {
764 	struct efivar_entry *entry, *n;
765 	int err = 0;
766 
767 	err = efivar_lock();
768 	if (err)
769 		return err;
770 
771 	list_for_each_entry_safe(entry, n, head, list) {
772 		err = func(entry, data);
773 		if (err)
774 			break;
775 	}
776 	efivar_unlock();
777 
778 	return err;
779 }
780 EXPORT_SYMBOL_GPL(efivar_entry_iter);
781 
782 /**
783  * efivars_kobject - get the kobject for the registered efivars
784  *
785  * If efivars_register() has not been called we return NULL,
786  * otherwise return the kobject used at registration time.
787  */
788 struct kobject *efivars_kobject(void)
789 {
790 	if (!__efivars)
791 		return NULL;
792 
793 	return __efivars->kobject;
794 }
795 EXPORT_SYMBOL_GPL(efivars_kobject);
796 
797 /**
798  * efivars_register - register an efivars
799  * @efivars: efivars to register
800  * @ops: efivars operations
801  * @kobject: @efivars-specific kobject
802  *
803  * Only a single efivars can be registered at any time.
804  */
805 int efivars_register(struct efivars *efivars,
806 		     const struct efivar_operations *ops,
807 		     struct kobject *kobject)
808 {
809 	if (down_interruptible(&efivars_lock))
810 		return -EINTR;
811 
812 	efivars->ops = ops;
813 	efivars->kobject = kobject;
814 
815 	__efivars = efivars;
816 
817 	pr_info("Registered efivars operations\n");
818 
819 	up(&efivars_lock);
820 
821 	return 0;
822 }
823 EXPORT_SYMBOL_GPL(efivars_register);
824 
825 /**
826  * efivars_unregister - unregister an efivars
827  * @efivars: efivars to unregister
828  *
829  * The caller must have already removed every entry from the list,
830  * failure to do so is an error.
831  */
832 int efivars_unregister(struct efivars *efivars)
833 {
834 	int rv;
835 
836 	if (down_interruptible(&efivars_lock))
837 		return -EINTR;
838 
839 	if (!__efivars) {
840 		printk(KERN_ERR "efivars not registered\n");
841 		rv = -EINVAL;
842 		goto out;
843 	}
844 
845 	if (__efivars != efivars) {
846 		rv = -EINVAL;
847 		goto out;
848 	}
849 
850 	pr_info("Unregistered efivars operations\n");
851 	__efivars = NULL;
852 
853 	rv = 0;
854 out:
855 	up(&efivars_lock);
856 	return rv;
857 }
858 EXPORT_SYMBOL_GPL(efivars_unregister);
859 
860 int efivar_supports_writes(void)
861 {
862 	return __efivars && __efivars->ops->set_variable;
863 }
864 EXPORT_SYMBOL_GPL(efivar_supports_writes);
865 
866 /*
867  * efivar_lock() - obtain the efivar lock, wait for it if needed
868  * @return 0 on success, error code on failure
869  */
870 int efivar_lock(void)
871 {
872 	if (down_interruptible(&efivars_lock))
873 		return -EINTR;
874 	if (!__efivars->ops) {
875 		up(&efivars_lock);
876 		return -ENODEV;
877 	}
878 	return 0;
879 }
880 EXPORT_SYMBOL_NS_GPL(efivar_lock, EFIVAR);
881 
882 /*
883  * efivar_lock() - obtain the efivar lock if it is free
884  * @return 0 on success, error code on failure
885  */
886 int efivar_trylock(void)
887 {
888 	if (down_trylock(&efivars_lock))
889 		 return -EBUSY;
890 	if (!__efivars->ops) {
891 		up(&efivars_lock);
892 		return -ENODEV;
893 	}
894 	return 0;
895 }
896 EXPORT_SYMBOL_NS_GPL(efivar_trylock, EFIVAR);
897 
898 /*
899  * efivar_unlock() - release the efivar lock
900  */
901 void efivar_unlock(void)
902 {
903 	up(&efivars_lock);
904 }
905 EXPORT_SYMBOL_NS_GPL(efivar_unlock, EFIVAR);
906 
907 /*
908  * efivar_get_variable() - retrieve a variable identified by name/vendor
909  *
910  * Must be called with efivars_lock held.
911  */
912 efi_status_t efivar_get_variable(efi_char16_t *name, efi_guid_t *vendor,
913 				 u32 *attr, unsigned long *size, void *data)
914 {
915 	return __efivars->ops->get_variable(name, vendor, attr, size, data);
916 }
917 EXPORT_SYMBOL_NS_GPL(efivar_get_variable, EFIVAR);
918 
919 /*
920  * efivar_get_next_variable() - enumerate the next name/vendor pair
921  *
922  * Must be called with efivars_lock held.
923  */
924 efi_status_t efivar_get_next_variable(unsigned long *name_size,
925 				      efi_char16_t *name, efi_guid_t *vendor)
926 {
927 	return __efivars->ops->get_next_variable(name_size, name, vendor);
928 }
929 EXPORT_SYMBOL_NS_GPL(efivar_get_next_variable, EFIVAR);
930 
931 /*
932  * efivar_set_variable_blocking() - local helper function for set_variable
933  *
934  * Must be called with efivars_lock held.
935  */
936 static efi_status_t
937 efivar_set_variable_blocking(efi_char16_t *name, efi_guid_t *vendor,
938 			     u32 attr, unsigned long data_size, void *data)
939 {
940 	efi_status_t status;
941 
942 	if (data_size > 0) {
943 		status = check_var_size(attr, data_size +
944 					      ucs2_strsize(name, 1024));
945 		if (status != EFI_SUCCESS)
946 			return status;
947 	}
948 	return __efivars->ops->set_variable(name, vendor, attr, data_size, data);
949 }
950 
951 /*
952  * efivar_set_variable_locked() - set a variable identified by name/vendor
953  *
954  * Must be called with efivars_lock held. If @nonblocking is set, it will use
955  * non-blocking primitives so it is guaranteed not to sleep.
956  */
957 efi_status_t efivar_set_variable_locked(efi_char16_t *name, efi_guid_t *vendor,
958 					u32 attr, unsigned long data_size,
959 					void *data, bool nonblocking)
960 {
961 	efi_set_variable_t *setvar;
962 	efi_status_t status;
963 
964 	if (!nonblocking)
965 		return efivar_set_variable_blocking(name, vendor, attr,
966 						    data_size, data);
967 
968 	/*
969 	 * If no _nonblocking variant exists, the ordinary one
970 	 * is assumed to be non-blocking.
971 	 */
972 	setvar = __efivars->ops->set_variable_nonblocking ?:
973 		 __efivars->ops->set_variable;
974 
975 	if (data_size > 0) {
976 		status = check_var_size_nonblocking(attr, data_size +
977 							  ucs2_strsize(name, 1024));
978 		if (status != EFI_SUCCESS)
979 			return status;
980 	}
981 	return setvar(name, vendor, attr, data_size, data);
982 }
983 EXPORT_SYMBOL_NS_GPL(efivar_set_variable_locked, EFIVAR);
984 
985 /*
986  * efivar_set_variable() - set a variable identified by name/vendor
987  *
988  * Can be called without holding the efivars_lock. Will sleep on obtaining the
989  * lock, or on obtaining other locks that are needed in order to complete the
990  * call.
991  */
992 efi_status_t efivar_set_variable(efi_char16_t *name, efi_guid_t *vendor,
993 				 u32 attr, unsigned long data_size, void *data)
994 {
995 	efi_status_t status;
996 
997 	if (efivar_lock())
998 		return EFI_ABORTED;
999 
1000 	status = efivar_set_variable_blocking(name, vendor, attr, data_size, data);
1001 	efivar_unlock();
1002 	return status;
1003 }
1004 EXPORT_SYMBOL_NS_GPL(efivar_set_variable, EFIVAR);
1005