Linux Audio

Check our new training course

Loading...
v6.2
  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/types.h>
 10#include <linux/sizes.h>
 11#include <linux/errno.h>
 12#include <linux/init.h>
 
 13#include <linux/module.h>
 14#include <linux/string.h>
 15#include <linux/smp.h>
 16#include <linux/efi.h>
 
 
 
 
 17#include <linux/ucs2_string.h>
 18
 19/* Private pointer to registered efivars */
 20static struct efivars *__efivars;
 21
 
 
 
 
 
 
 22static DEFINE_SEMAPHORE(efivars_lock);
 23
 24static efi_status_t check_var_size(bool nonblocking, u32 attributes,
 25				   unsigned long size)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 26{
 27	const struct efivar_operations *fops;
 28	efi_status_t status;
 
 
 29
 30	fops = __efivars->ops;
 31
 32	if (!fops->query_variable_store)
 33		status = EFI_UNSUPPORTED;
 34	else
 35		status = fops->query_variable_store(attributes, size,
 36						    nonblocking);
 37	if (status == EFI_UNSUPPORTED)
 38		return (size <= SZ_64K) ? EFI_SUCCESS : EFI_OUT_OF_RESOURCES;
 39	return status;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 40}
 41
 42/**
 43 * efivars_kobject - get the kobject for the registered efivars
 
 
 
 
 44 *
 45 * If efivars_register() has not been called we return NULL,
 46 * otherwise return the kobject used at registration time.
 
 
 47 */
 48struct kobject *efivars_kobject(void)
 
 49{
 
 
 
 
 
 
 
 50	if (!__efivars)
 51		return NULL;
 52
 53	return __efivars->kobject;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 54}
 55EXPORT_SYMBOL_GPL(efivars_kobject);
 56
 57/**
 58 * efivars_register - register an efivars
 59 * @efivars: efivars to register
 60 * @ops: efivars operations
 61 * @kobject: @efivars-specific kobject
 62 *
 63 * Only a single efivars can be registered at any time.
 64 */
 65int efivars_register(struct efivars *efivars,
 66		     const struct efivar_operations *ops,
 67		     struct kobject *kobject)
 68{
 69	if (down_interruptible(&efivars_lock))
 70		return -EINTR;
 
 
 71
 72	efivars->ops = ops;
 73	efivars->kobject = kobject;
 
 74
 75	__efivars = efivars;
 
 
 
 
 
 
 
 
 
 
 
 76
 77	pr_info("Registered efivars operations\n");
 
 
 78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 79	up(&efivars_lock);
 
 80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 81	return 0;
 82}
 83EXPORT_SYMBOL_GPL(efivars_register);
 84
 85/**
 86 * efivars_unregister - unregister an efivars
 87 * @efivars: efivars to unregister
 
 
 
 
 88 *
 89 * The caller must have already removed every entry from the list,
 90 * failure to do so is an error.
 
 
 
 
 
 
 
 
 
 
 91 */
 92int efivars_unregister(struct efivars *efivars)
 
 93{
 94	int rv;
 
 
 
 95
 96	if (down_interruptible(&efivars_lock))
 97		return -EINTR;
 98
 99	if (!__efivars) {
100		printk(KERN_ERR "efivars not registered\n");
101		rv = -EINVAL;
102		goto out;
 
 
 
 
103	}
104
105	if (__efivars != efivars) {
106		rv = -EINVAL;
107		goto out;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108	}
109
110	pr_info("Unregistered efivars operations\n");
111	__efivars = NULL;
 
 
 
 
 
 
 
 
112
113	rv = 0;
114out:
115	up(&efivars_lock);
116	return rv;
117}
118EXPORT_SYMBOL_GPL(efivars_unregister);
119
120int efivar_supports_writes(void)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121{
122	return __efivars && __efivars->ops->set_variable;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123}
124EXPORT_SYMBOL_GPL(efivar_supports_writes);
125
126/*
127 * efivar_lock() - obtain the efivar lock, wait for it if needed
128 * @return 0 on success, error code on failure
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129 */
130int efivar_lock(void)
131{
 
 
 
 
 
132	if (down_interruptible(&efivars_lock))
133		return -EINTR;
134	if (!__efivars->ops) {
135		up(&efivars_lock);
136		return -ENODEV;
137	}
 
 
 
 
 
 
 
 
138	return 0;
139}
140EXPORT_SYMBOL_NS_GPL(efivar_lock, EFIVAR);
141
142/*
143 * efivar_lock() - obtain the efivar lock if it is free
144 * @return 0 on success, error code on failure
 
 
 
 
 
 
 
145 */
146int efivar_trylock(void)
 
147{
148	if (down_trylock(&efivars_lock))
149		 return -EBUSY;
150	if (!__efivars->ops) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151		up(&efivars_lock);
152		return -ENODEV;
153	}
154	return 0;
 
 
 
 
 
 
155}
156EXPORT_SYMBOL_NS_GPL(efivar_trylock, EFIVAR);
157
158/*
159 * efivar_unlock() - release the efivar lock
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160 */
161void efivar_unlock(void)
 
162{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163	up(&efivars_lock);
 
 
164}
165EXPORT_SYMBOL_NS_GPL(efivar_unlock, EFIVAR);
166
167/*
168 * efivar_get_variable() - retrieve a variable identified by name/vendor
169 *
170 * Must be called with efivars_lock held.
 
 
171 */
172efi_status_t efivar_get_variable(efi_char16_t *name, efi_guid_t *vendor,
173				 u32 *attr, unsigned long *size, void *data)
174{
175	return __efivars->ops->get_variable(name, vendor, attr, size, data);
176}
177EXPORT_SYMBOL_NS_GPL(efivar_get_variable, EFIVAR);
178
179/*
180 * efivar_get_next_variable() - enumerate the next name/vendor pair
181 *
182 * Must be called with efivars_lock held.
183 */
184efi_status_t efivar_get_next_variable(unsigned long *name_size,
185				      efi_char16_t *name, efi_guid_t *vendor)
186{
187	return __efivars->ops->get_next_variable(name_size, name, vendor);
188}
189EXPORT_SYMBOL_NS_GPL(efivar_get_next_variable, EFIVAR);
190
191/*
192 * efivar_set_variable_locked() - set a variable identified by name/vendor
 
 
 
 
193 *
194 * Must be called with efivars_lock held. If @nonblocking is set, it will use
195 * non-blocking primitives so it is guaranteed not to sleep.
 
 
 
 
 
 
 
 
 
 
 
 
196 */
197efi_status_t efivar_set_variable_locked(efi_char16_t *name, efi_guid_t *vendor,
198					u32 attr, unsigned long data_size,
199					void *data, bool nonblocking)
200{
201	efi_set_variable_t *setvar;
202	efi_status_t status;
203
204	if (data_size > 0) {
205		status = check_var_size(nonblocking, attr,
206					data_size + ucs2_strsize(name, 1024));
207		if (status != EFI_SUCCESS)
208			return status;
 
 
 
 
 
 
209	}
210
211	/*
212	 * If no _nonblocking variant exists, the ordinary one
213	 * is assumed to be non-blocking.
214	 */
215	setvar = __efivars->ops->set_variable_nonblocking;
216	if (!setvar || !nonblocking)
217		 setvar = __efivars->ops->set_variable;
218
219	return setvar(name, vendor, attr, data_size, data);
 
 
 
 
 
 
220}
221EXPORT_SYMBOL_NS_GPL(efivar_set_variable_locked, EFIVAR);
222
223/*
224 * efivar_set_variable() - set a variable identified by name/vendor
 
 
 
 
 
 
 
225 *
226 * Can be called without holding the efivars_lock. Will sleep on obtaining the
227 * lock, or on obtaining other locks that are needed in order to complete the
228 * call.
229 */
230efi_status_t efivar_set_variable(efi_char16_t *name, efi_guid_t *vendor,
231				 u32 attr, unsigned long data_size, void *data)
232{
233	efi_status_t status;
234
235	if (efivar_lock())
236		return EFI_ABORTED;
 
 
 
237
238	status = efivar_set_variable_locked(name, vendor, attr, data_size,
239					    data, false);
240	efivar_unlock();
241	return status;
242}
243EXPORT_SYMBOL_NS_GPL(efivar_set_variable, EFIVAR);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
v5.14.15
   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 */
  25static 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 */
  33static DEFINE_SEMAPHORE(efivars_lock);
  34
  35static bool
  36validate_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
  68static bool
  69validate_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
  79static bool
  80validate_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
 130static bool
 131validate_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
 141static bool
 142validate_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
 158struct 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 */
 176static 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 */
 207static bool
 208variable_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
 236bool
 237efivar_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}
 270EXPORT_SYMBOL_GPL(efivar_validate);
 271
 272bool
 273efivar_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}
 299EXPORT_SYMBOL_GPL(efivar_variable_is_removable);
 300
 301static efi_status_t
 302check_var_size(u32 attributes, unsigned long size)
 303{
 304	const struct efivar_operations *fops;
 305
 306	if (!__efivars)
 307		return EFI_UNSUPPORTED;
 308
 309	fops = __efivars->ops;
 310
 311	if (!fops->query_variable_store)
 312		return EFI_UNSUPPORTED;
 313
 314	return fops->query_variable_store(attributes, size, false);
 315}
 316
 317static efi_status_t
 318check_var_size_nonblocking(u32 attributes, unsigned long size)
 319{
 320	const struct efivar_operations *fops;
 321
 322	if (!__efivars)
 323		return EFI_UNSUPPORTED;
 324
 325	fops = __efivars->ops;
 326
 327	if (!fops->query_variable_store)
 328		return EFI_UNSUPPORTED;
 329
 330	return fops->query_variable_store(attributes, size, true);
 331}
 332
 333static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor,
 334				struct list_head *head)
 335{
 336	struct efivar_entry *entry, *n;
 337	unsigned long strsize1, strsize2;
 338	bool found = false;
 339
 340	strsize1 = ucs2_strsize(variable_name, 1024);
 341	list_for_each_entry_safe(entry, n, head, list) {
 342		strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
 343		if (strsize1 == strsize2 &&
 344			!memcmp(variable_name, &(entry->var.VariableName),
 345				strsize2) &&
 346			!efi_guidcmp(entry->var.VendorGuid,
 347				*vendor)) {
 348			found = true;
 349			break;
 350		}
 351	}
 352	return found;
 353}
 354
 355/*
 356 * Returns the size of variable_name, in bytes, including the
 357 * terminating NULL character, or variable_name_size if no NULL
 358 * character is found among the first variable_name_size bytes.
 359 */
 360static unsigned long var_name_strnsize(efi_char16_t *variable_name,
 361				       unsigned long variable_name_size)
 362{
 363	unsigned long len;
 364	efi_char16_t c;
 365
 366	/*
 367	 * The variable name is, by definition, a NULL-terminated
 368	 * string, so make absolutely sure that variable_name_size is
 369	 * the value we expect it to be. If not, return the real size.
 370	 */
 371	for (len = 2; len <= variable_name_size; len += sizeof(c)) {
 372		c = variable_name[(len / sizeof(c)) - 1];
 373		if (!c)
 374			break;
 375	}
 376
 377	return min(len, variable_name_size);
 378}
 379
 380/*
 381 * Print a warning when duplicate EFI variables are encountered and
 382 * disable the sysfs workqueue since the firmware is buggy.
 383 */
 384static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid,
 385			     unsigned long len16)
 386{
 387	size_t i, len8 = len16 / sizeof(efi_char16_t);
 388	char *str8;
 389
 390	str8 = kzalloc(len8, GFP_KERNEL);
 391	if (!str8)
 392		return;
 393
 394	for (i = 0; i < len8; i++)
 395		str8[i] = str16[i];
 396
 397	printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
 398	       str8, vendor_guid);
 399	kfree(str8);
 400}
 401
 402/**
 403 * efivar_init - build the initial list of EFI variables
 404 * @func: callback function to invoke for every variable
 405 * @data: function-specific data to pass to @func
 406 * @duplicates: error if we encounter duplicates on @head?
 407 * @head: initialised head of variable list
 408 *
 409 * Get every EFI variable from the firmware and invoke @func. @func
 410 * should call efivar_entry_add() to build the list of variables.
 411 *
 412 * Returns 0 on success, or a kernel error code on failure.
 413 */
 414int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 415		void *data, bool duplicates, struct list_head *head)
 416{
 417	const struct efivar_operations *ops;
 418	unsigned long variable_name_size = 1024;
 419	efi_char16_t *variable_name;
 420	efi_status_t status;
 421	efi_guid_t vendor_guid;
 422	int err = 0;
 423
 424	if (!__efivars)
 425		return -EFAULT;
 426
 427	ops = __efivars->ops;
 428
 429	variable_name = kzalloc(variable_name_size, GFP_KERNEL);
 430	if (!variable_name) {
 431		printk(KERN_ERR "efivars: Memory allocation failed.\n");
 432		return -ENOMEM;
 433	}
 434
 435	if (down_interruptible(&efivars_lock)) {
 436		err = -EINTR;
 437		goto free;
 438	}
 439
 440	/*
 441	 * Per EFI spec, the maximum storage allocated for both
 442	 * the variable name and variable data is 1024 bytes.
 443	 */
 444
 445	do {
 446		variable_name_size = 1024;
 447
 448		status = ops->get_next_variable(&variable_name_size,
 449						variable_name,
 450						&vendor_guid);
 451		switch (status) {
 452		case EFI_SUCCESS:
 453			if (duplicates)
 454				up(&efivars_lock);
 455
 456			variable_name_size = var_name_strnsize(variable_name,
 457							       variable_name_size);
 458
 459			/*
 460			 * Some firmware implementations return the
 461			 * same variable name on multiple calls to
 462			 * get_next_variable(). Terminate the loop
 463			 * immediately as there is no guarantee that
 464			 * we'll ever see a different variable name,
 465			 * and may end up looping here forever.
 466			 */
 467			if (duplicates &&
 468			    variable_is_present(variable_name, &vendor_guid,
 469						head)) {
 470				dup_variable_bug(variable_name, &vendor_guid,
 471						 variable_name_size);
 472				status = EFI_NOT_FOUND;
 473			} else {
 474				err = func(variable_name, vendor_guid,
 475					   variable_name_size, data);
 476				if (err)
 477					status = EFI_NOT_FOUND;
 478			}
 479
 480			if (duplicates) {
 481				if (down_interruptible(&efivars_lock)) {
 482					err = -EINTR;
 483					goto free;
 484				}
 485			}
 486
 487			break;
 488		case EFI_UNSUPPORTED:
 489			err = -EOPNOTSUPP;
 490			status = EFI_NOT_FOUND;
 491			break;
 492		case EFI_NOT_FOUND:
 493			break;
 494		default:
 495			printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
 496				status);
 497			status = EFI_NOT_FOUND;
 498			break;
 499		}
 500
 501	} while (status != EFI_NOT_FOUND);
 502
 503	up(&efivars_lock);
 504free:
 505	kfree(variable_name);
 506
 507	return err;
 508}
 509EXPORT_SYMBOL_GPL(efivar_init);
 510
 511/**
 512 * efivar_entry_add - add entry to variable list
 513 * @entry: entry to add to list
 514 * @head: list head
 
 515 *
 516 * Returns 0 on success, or a kernel error code on failure.
 517 */
 518int efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
 
 
 519{
 520	if (down_interruptible(&efivars_lock))
 521		return -EINTR;
 522	list_add(&entry->list, head);
 523	up(&efivars_lock);
 524
 525	return 0;
 526}
 527EXPORT_SYMBOL_GPL(efivar_entry_add);
 528
 529/**
 530 * efivar_entry_remove - remove entry from variable list
 531 * @entry: entry to remove from list
 532 *
 533 * Returns 0 on success, or a kernel error code on failure.
 534 */
 535int efivar_entry_remove(struct efivar_entry *entry)
 536{
 537	if (down_interruptible(&efivars_lock))
 538		return -EINTR;
 539	list_del(&entry->list);
 540	up(&efivars_lock);
 541
 542	return 0;
 543}
 544EXPORT_SYMBOL_GPL(efivar_entry_remove);
 545
 546/*
 547 * efivar_entry_list_del_unlock - remove entry from variable list
 548 * @entry: entry to remove
 549 *
 550 * Remove @entry from the variable list and release the list lock.
 551 *
 552 * NOTE: slightly weird locking semantics here - we expect to be
 553 * called with the efivars lock already held, and we release it before
 554 * returning. This is because this function is usually called after
 555 * set_variable() while the lock is still held.
 556 */
 557static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
 558{
 559	list_del(&entry->list);
 560	up(&efivars_lock);
 561}
 562
 563/**
 564 * __efivar_entry_delete - delete an EFI variable
 565 * @entry: entry containing EFI variable to delete
 566 *
 567 * Delete the variable from the firmware but leave @entry on the
 568 * variable list.
 569 *
 570 * This function differs from efivar_entry_delete() because it does
 571 * not remove @entry from the variable list. Also, it is safe to be
 572 * called from within a efivar_entry_iter_begin() and
 573 * efivar_entry_iter_end() region, unlike efivar_entry_delete().
 574 *
 575 * Returns 0 on success, or a converted EFI status code if
 576 * set_variable() fails.
 577 */
 578int __efivar_entry_delete(struct efivar_entry *entry)
 579{
 580	efi_status_t status;
 581
 582	if (!__efivars)
 583		return -EINVAL;
 584
 585	status = __efivars->ops->set_variable(entry->var.VariableName,
 586					      &entry->var.VendorGuid,
 587					      0, 0, NULL);
 588
 589	return efi_status_to_err(status);
 590}
 591EXPORT_SYMBOL_GPL(__efivar_entry_delete);
 592
 593/**
 594 * efivar_entry_delete - delete variable and remove entry from list
 595 * @entry: entry containing variable to delete
 596 *
 597 * Delete the variable from the firmware and remove @entry from the
 598 * variable list. It is the caller's responsibility to free @entry
 599 * once we return.
 600 *
 601 * Returns 0 on success, -EINTR if we can't grab the semaphore,
 602 * converted EFI status code if set_variable() fails.
 603 */
 604int efivar_entry_delete(struct efivar_entry *entry)
 605{
 606	const struct efivar_operations *ops;
 607	efi_status_t status;
 608
 609	if (down_interruptible(&efivars_lock))
 610		return -EINTR;
 611
 612	if (!__efivars) {
 613		up(&efivars_lock);
 614		return -EINVAL;
 615	}
 616	ops = __efivars->ops;
 617	status = ops->set_variable(entry->var.VariableName,
 618				   &entry->var.VendorGuid,
 619				   0, 0, NULL);
 620	if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
 621		up(&efivars_lock);
 622		return efi_status_to_err(status);
 623	}
 624
 625	efivar_entry_list_del_unlock(entry);
 626	return 0;
 627}
 628EXPORT_SYMBOL_GPL(efivar_entry_delete);
 629
 630/**
 631 * efivar_entry_set - call set_variable()
 632 * @entry: entry containing the EFI variable to write
 633 * @attributes: variable attributes
 634 * @size: size of @data buffer
 635 * @data: buffer containing variable data
 636 * @head: head of variable list
 637 *
 638 * Calls set_variable() for an EFI variable. If creating a new EFI
 639 * variable, this function is usually followed by efivar_entry_add().
 640 *
 641 * Before writing the variable, the remaining EFI variable storage
 642 * space is checked to ensure there is enough room available.
 643 *
 644 * If @head is not NULL a lookup is performed to determine whether
 645 * the entry is already on the list.
 646 *
 647 * Returns 0 on success, -EINTR if we can't grab the semaphore,
 648 * -EEXIST if a lookup is performed and the entry already exists on
 649 * the list, or a converted EFI status code if set_variable() fails.
 650 */
 651int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
 652		     unsigned long size, void *data, struct list_head *head)
 653{
 654	const struct efivar_operations *ops;
 655	efi_status_t status;
 656	efi_char16_t *name = entry->var.VariableName;
 657	efi_guid_t vendor = entry->var.VendorGuid;
 658
 659	if (down_interruptible(&efivars_lock))
 660		return -EINTR;
 661
 662	if (!__efivars) {
 663		up(&efivars_lock);
 664		return -EINVAL;
 665	}
 666	ops = __efivars->ops;
 667	if (head && efivar_entry_find(name, vendor, head, false)) {
 668		up(&efivars_lock);
 669		return -EEXIST;
 670	}
 671
 672	status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
 673	if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
 674		status = ops->set_variable(name, &vendor,
 675					   attributes, size, data);
 676
 677	up(&efivars_lock);
 678
 679	return efi_status_to_err(status);
 680
 681}
 682EXPORT_SYMBOL_GPL(efivar_entry_set);
 683
 684/*
 685 * efivar_entry_set_nonblocking - call set_variable_nonblocking()
 686 *
 687 * This function is guaranteed to not block and is suitable for calling
 688 * from crash/panic handlers.
 689 *
 690 * Crucially, this function will not block if it cannot acquire
 691 * efivars_lock. Instead, it returns -EBUSY.
 692 */
 693static int
 694efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
 695			     u32 attributes, unsigned long size, void *data)
 696{
 697	const struct efivar_operations *ops;
 698	efi_status_t status;
 699
 700	if (down_trylock(&efivars_lock))
 701		return -EBUSY;
 702
 703	if (!__efivars) {
 704		up(&efivars_lock);
 705		return -EINVAL;
 706	}
 707
 708	status = check_var_size_nonblocking(attributes,
 709					    size + ucs2_strsize(name, 1024));
 710	if (status != EFI_SUCCESS) {
 711		up(&efivars_lock);
 712		return -ENOSPC;
 713	}
 714
 715	ops = __efivars->ops;
 716	status = ops->set_variable_nonblocking(name, &vendor, attributes,
 717					       size, data);
 718
 
 
 719	up(&efivars_lock);
 720	return efi_status_to_err(status);
 721}
 
 722
 723/**
 724 * efivar_entry_set_safe - call set_variable() if enough space in firmware
 725 * @name: buffer containing the variable name
 726 * @vendor: variable vendor guid
 727 * @attributes: variable attributes
 728 * @block: can we block in this context?
 729 * @size: size of @data buffer
 730 * @data: buffer containing variable data
 731 *
 732 * Ensures there is enough free storage in the firmware for this variable, and
 733 * if so, calls set_variable(). If creating a new EFI variable, this function
 734 * is usually followed by efivar_entry_add().
 735 *
 736 * Returns 0 on success, -ENOSPC if the firmware does not have enough
 737 * space for set_variable() to succeed, or a converted EFI status code
 738 * if set_variable() fails.
 739 */
 740int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
 741			  bool block, unsigned long size, void *data)
 742{
 743	const struct efivar_operations *ops;
 744	efi_status_t status;
 745
 746	if (!__efivars)
 747		return -EINVAL;
 748
 749	ops = __efivars->ops;
 750	if (!ops->query_variable_store)
 751		return -ENOSYS;
 752
 753	/*
 754	 * If the EFI variable backend provides a non-blocking
 755	 * ->set_variable() operation and we're in a context where we
 756	 * cannot block, then we need to use it to avoid live-locks,
 757	 * since the implication is that the regular ->set_variable()
 758	 * will block.
 759	 *
 760	 * If no ->set_variable_nonblocking() is provided then
 761	 * ->set_variable() is assumed to be non-blocking.
 762	 */
 763	if (!block && ops->set_variable_nonblocking)
 764		return efivar_entry_set_nonblocking(name, vendor, attributes,
 765						    size, data);
 766
 767	if (!block) {
 768		if (down_trylock(&efivars_lock))
 769			return -EBUSY;
 770	} else {
 771		if (down_interruptible(&efivars_lock))
 772			return -EINTR;
 773	}
 774
 775	status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
 776	if (status != EFI_SUCCESS) {
 777		up(&efivars_lock);
 778		return -ENOSPC;
 779	}
 780
 781	status = ops->set_variable(name, &vendor, attributes, size, data);
 782
 783	up(&efivars_lock);
 784
 785	return efi_status_to_err(status);
 786}
 787EXPORT_SYMBOL_GPL(efivar_entry_set_safe);
 788
 789/**
 790 * efivar_entry_find - search for an entry
 791 * @name: the EFI variable name
 792 * @guid: the EFI variable vendor's guid
 793 * @head: head of the variable list
 794 * @remove: should we remove the entry from the list?
 795 *
 796 * Search for an entry on the variable list that has the EFI variable
 797 * name @name and vendor guid @guid. If an entry is found on the list
 798 * and @remove is true, the entry is removed from the list.
 799 *
 800 * The caller MUST call efivar_entry_iter_begin() and
 801 * efivar_entry_iter_end() before and after the invocation of this
 802 * function, respectively.
 803 *
 804 * Returns the entry if found on the list, %NULL otherwise.
 805 */
 806struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
 807				       struct list_head *head, bool remove)
 808{
 809	struct efivar_entry *entry, *n;
 810	int strsize1, strsize2;
 811	bool found = false;
 812
 813	list_for_each_entry_safe(entry, n, head, list) {
 814		strsize1 = ucs2_strsize(name, 1024);
 815		strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
 816		if (strsize1 == strsize2 &&
 817		    !memcmp(name, &(entry->var.VariableName), strsize1) &&
 818		    !efi_guidcmp(guid, entry->var.VendorGuid)) {
 819			found = true;
 820			break;
 821		}
 822	}
 823
 824	if (!found)
 825		return NULL;
 826
 827	if (remove) {
 828		if (entry->scanning) {
 829			/*
 830			 * The entry will be deleted
 831			 * after scanning is completed.
 832			 */
 833			entry->deleting = true;
 834		} else
 835			list_del(&entry->list);
 836	}
 837
 838	return entry;
 839}
 840EXPORT_SYMBOL_GPL(efivar_entry_find);
 841
 842/**
 843 * efivar_entry_size - obtain the size of a variable
 844 * @entry: entry for this variable
 845 * @size: location to store the variable's size
 846 */
 847int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
 848{
 849	const struct efivar_operations *ops;
 850	efi_status_t status;
 851
 852	*size = 0;
 853
 854	if (down_interruptible(&efivars_lock))
 855		return -EINTR;
 856	if (!__efivars) {
 857		up(&efivars_lock);
 858		return -EINVAL;
 859	}
 860	ops = __efivars->ops;
 861	status = ops->get_variable(entry->var.VariableName,
 862				   &entry->var.VendorGuid, NULL, size, NULL);
 863	up(&efivars_lock);
 864
 865	if (status != EFI_BUFFER_TOO_SMALL)
 866		return efi_status_to_err(status);
 867
 868	return 0;
 869}
 870EXPORT_SYMBOL_GPL(efivar_entry_size);
 871
 872/**
 873 * __efivar_entry_get - call get_variable()
 874 * @entry: read data for this variable
 875 * @attributes: variable attributes
 876 * @size: size of @data buffer
 877 * @data: buffer to store variable data
 878 *
 879 * The caller MUST call efivar_entry_iter_begin() and
 880 * efivar_entry_iter_end() before and after the invocation of this
 881 * function, respectively.
 882 */
 883int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
 884		       unsigned long *size, void *data)
 885{
 886	efi_status_t status;
 887
 888	if (!__efivars)
 889		return -EINVAL;
 890
 891	status = __efivars->ops->get_variable(entry->var.VariableName,
 892					      &entry->var.VendorGuid,
 893					      attributes, size, data);
 894
 895	return efi_status_to_err(status);
 896}
 897EXPORT_SYMBOL_GPL(__efivar_entry_get);
 898
 899/**
 900 * efivar_entry_get - call get_variable()
 901 * @entry: read data for this variable
 902 * @attributes: variable attributes
 903 * @size: size of @data buffer
 904 * @data: buffer to store variable data
 905 */
 906int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
 907		     unsigned long *size, void *data)
 908{
 909	efi_status_t status;
 910
 911	if (down_interruptible(&efivars_lock))
 912		return -EINTR;
 913
 914	if (!__efivars) {
 915		up(&efivars_lock);
 916		return -EINVAL;
 917	}
 918
 919	status = __efivars->ops->get_variable(entry->var.VariableName,
 920					      &entry->var.VendorGuid,
 921					      attributes, size, data);
 922	up(&efivars_lock);
 923
 924	return efi_status_to_err(status);
 925}
 926EXPORT_SYMBOL_GPL(efivar_entry_get);
 927
 928/**
 929 * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
 930 * @entry: entry containing variable to set and get
 931 * @attributes: attributes of variable to be written
 932 * @size: size of data buffer
 933 * @data: buffer containing data to write
 934 * @set: did the set_variable() call succeed?
 935 *
 936 * This is a pretty special (complex) function. See efivarfs_file_write().
 937 *
 938 * Atomically call set_variable() for @entry and if the call is
 939 * successful, return the new size of the variable from get_variable()
 940 * in @size. The success of set_variable() is indicated by @set.
 941 *
 942 * Returns 0 on success, -EINVAL if the variable data is invalid,
 943 * -ENOSPC if the firmware does not have enough available space, or a
 944 * converted EFI status code if either of set_variable() or
 945 * get_variable() fail.
 946 *
 947 * If the EFI variable does not exist when calling set_variable()
 948 * (EFI_NOT_FOUND), @entry is removed from the variable list.
 949 */
 950int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
 951			      unsigned long *size, void *data, bool *set)
 952{
 953	const struct efivar_operations *ops;
 954	efi_char16_t *name = entry->var.VariableName;
 955	efi_guid_t *vendor = &entry->var.VendorGuid;
 956	efi_status_t status;
 957	int err;
 958
 959	*set = false;
 960
 961	if (efivar_validate(*vendor, name, data, *size) == false)
 962		return -EINVAL;
 963
 964	/*
 965	 * The lock here protects the get_variable call, the conditional
 966	 * set_variable call, and removal of the variable from the efivars
 967	 * list (in the case of an authenticated delete).
 968	 */
 969	if (down_interruptible(&efivars_lock))
 970		return -EINTR;
 971
 972	if (!__efivars) {
 973		err = -EINVAL;
 974		goto out;
 975	}
 976
 977	/*
 978	 * Ensure that the available space hasn't shrunk below the safe level
 979	 */
 980	status = check_var_size(attributes, *size + ucs2_strsize(name, 1024));
 981	if (status != EFI_SUCCESS) {
 982		if (status != EFI_UNSUPPORTED) {
 983			err = efi_status_to_err(status);
 984			goto out;
 985		}
 986
 987		if (*size > 65536) {
 988			err = -ENOSPC;
 989			goto out;
 990		}
 991	}
 992
 993	ops = __efivars->ops;
 994
 995	status = ops->set_variable(name, vendor, attributes, *size, data);
 996	if (status != EFI_SUCCESS) {
 997		err = efi_status_to_err(status);
 998		goto out;
 999	}
1000
1001	*set = true;
1002
1003	/*
1004	 * Writing to the variable may have caused a change in size (which
1005	 * could either be an append or an overwrite), or the variable to be
1006	 * deleted. Perform a GetVariable() so we can tell what actually
1007	 * happened.
1008	 */
1009	*size = 0;
1010	status = ops->get_variable(entry->var.VariableName,
1011				   &entry->var.VendorGuid,
1012				   NULL, size, NULL);
1013
1014	if (status == EFI_NOT_FOUND)
1015		efivar_entry_list_del_unlock(entry);
1016	else
1017		up(&efivars_lock);
1018
1019	if (status && status != EFI_BUFFER_TOO_SMALL)
1020		return efi_status_to_err(status);
1021
1022	return 0;
1023
1024out:
1025	up(&efivars_lock);
1026	return err;
1027
1028}
1029EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
1030
1031/**
1032 * efivar_entry_iter_begin - begin iterating the variable list
1033 *
1034 * Lock the variable list to prevent entry insertion and removal until
1035 * efivar_entry_iter_end() is called. This function is usually used in
1036 * conjunction with __efivar_entry_iter() or efivar_entry_iter().
1037 */
1038int efivar_entry_iter_begin(void)
 
1039{
1040	return down_interruptible(&efivars_lock);
1041}
1042EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
1043
1044/**
1045 * efivar_entry_iter_end - finish iterating the variable list
1046 *
1047 * Unlock the variable list and allow modifications to the list again.
1048 */
1049void efivar_entry_iter_end(void)
 
1050{
1051	up(&efivars_lock);
1052}
1053EXPORT_SYMBOL_GPL(efivar_entry_iter_end);
1054
1055/**
1056 * __efivar_entry_iter - iterate over variable list
1057 * @func: callback function
1058 * @head: head of the variable list
1059 * @data: function-specific data to pass to callback
1060 * @prev: entry to begin iterating from
1061 *
1062 * Iterate over the list of EFI variables and call @func with every
1063 * entry on the list. It is safe for @func to remove entries in the
1064 * list via efivar_entry_delete().
1065 *
1066 * You MUST call efivar_entry_iter_begin() before this function, and
1067 * efivar_entry_iter_end() afterwards.
1068 *
1069 * It is possible to begin iteration from an arbitrary entry within
1070 * the list by passing @prev. @prev is updated on return to point to
1071 * the last entry passed to @func. To begin iterating from the
1072 * beginning of the list @prev must be %NULL.
1073 *
1074 * The restrictions for @func are the same as documented for
1075 * efivar_entry_iter().
1076 */
1077int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
1078			struct list_head *head, void *data,
1079			struct efivar_entry **prev)
1080{
1081	struct efivar_entry *entry, *n;
1082	int err = 0;
1083
1084	if (!prev || !*prev) {
1085		list_for_each_entry_safe(entry, n, head, list) {
1086			err = func(entry, data);
1087			if (err)
1088				break;
1089		}
1090
1091		if (prev)
1092			*prev = entry;
1093
1094		return err;
1095	}
1096
 
 
 
 
 
 
 
1097
1098	list_for_each_entry_safe_continue((*prev), n, head, list) {
1099		err = func(*prev, data);
1100		if (err)
1101			break;
1102	}
1103
1104	return err;
1105}
1106EXPORT_SYMBOL_GPL(__efivar_entry_iter);
1107
1108/**
1109 * efivar_entry_iter - iterate over variable list
1110 * @func: callback function
1111 * @head: head of variable list
1112 * @data: function-specific data to pass to callback
1113 *
1114 * Iterate over the list of EFI variables and call @func with every
1115 * entry on the list. It is safe for @func to remove entries in the
1116 * list via efivar_entry_delete() while iterating.
1117 *
1118 * Some notes for the callback function:
1119 *  - a non-zero return value indicates an error and terminates the loop
1120 *  - @func is called from atomic context
1121 */
1122int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
1123		      struct list_head *head, void *data)
1124{
1125	int err = 0;
1126
1127	err = efivar_entry_iter_begin();
1128	if (err)
1129		return err;
1130	err = __efivar_entry_iter(func, head, data, NULL);
1131	efivar_entry_iter_end();
1132
1133	return err;
 
 
 
1134}
1135EXPORT_SYMBOL_GPL(efivar_entry_iter);
1136
1137/**
1138 * efivars_kobject - get the kobject for the registered efivars
1139 *
1140 * If efivars_register() has not been called we return NULL,
1141 * otherwise return the kobject used at registration time.
1142 */
1143struct kobject *efivars_kobject(void)
1144{
1145	if (!__efivars)
1146		return NULL;
1147
1148	return __efivars->kobject;
1149}
1150EXPORT_SYMBOL_GPL(efivars_kobject);
1151
1152/**
1153 * efivars_register - register an efivars
1154 * @efivars: efivars to register
1155 * @ops: efivars operations
1156 * @kobject: @efivars-specific kobject
1157 *
1158 * Only a single efivars can be registered at any time.
1159 */
1160int efivars_register(struct efivars *efivars,
1161		     const struct efivar_operations *ops,
1162		     struct kobject *kobject)
1163{
1164	if (down_interruptible(&efivars_lock))
1165		return -EINTR;
1166
1167	efivars->ops = ops;
1168	efivars->kobject = kobject;
1169
1170	__efivars = efivars;
1171
1172	pr_info("Registered efivars operations\n");
1173
1174	up(&efivars_lock);
1175
1176	return 0;
1177}
1178EXPORT_SYMBOL_GPL(efivars_register);
1179
1180/**
1181 * efivars_unregister - unregister an efivars
1182 * @efivars: efivars to unregister
1183 *
1184 * The caller must have already removed every entry from the list,
1185 * failure to do so is an error.
1186 */
1187int efivars_unregister(struct efivars *efivars)
1188{
1189	int rv;
1190
1191	if (down_interruptible(&efivars_lock))
1192		return -EINTR;
1193
1194	if (!__efivars) {
1195		printk(KERN_ERR "efivars not registered\n");
1196		rv = -EINVAL;
1197		goto out;
1198	}
1199
1200	if (__efivars != efivars) {
1201		rv = -EINVAL;
1202		goto out;
1203	}
1204
1205	pr_info("Unregistered efivars operations\n");
1206	__efivars = NULL;
1207
1208	rv = 0;
1209out:
1210	up(&efivars_lock);
1211	return rv;
1212}
1213EXPORT_SYMBOL_GPL(efivars_unregister);
1214
1215int efivar_supports_writes(void)
1216{
1217	return __efivars && __efivars->ops->set_variable;
1218}
1219EXPORT_SYMBOL_GPL(efivar_supports_writes);