Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
a9499fa7 | 2 | * Originally from efivars.c |
1da177e4 LT |
3 | * |
4 | * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com> | |
5 | * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com> | |
6 | * | |
1da177e4 LT |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
1da177e4 LT |
20 | */ |
21 | ||
c59ede7b | 22 | #include <linux/capability.h> |
1da177e4 LT |
23 | #include <linux/types.h> |
24 | #include <linux/errno.h> | |
25 | #include <linux/init.h> | |
1da177e4 LT |
26 | #include <linux/mm.h> |
27 | #include <linux/module.h> | |
28 | #include <linux/string.h> | |
29 | #include <linux/smp.h> | |
30 | #include <linux/efi.h> | |
31 | #include <linux/sysfs.h> | |
1da177e4 | 32 | #include <linux/device.h> |
5a0e3ad6 | 33 | #include <linux/slab.h> |
47f531e8 | 34 | #include <linux/ctype.h> |
a614e192 | 35 | #include <linux/ucs2_string.h> |
1da177e4 | 36 | |
4423d779 MF |
37 | /* Private pointer to registered efivars */ |
38 | static struct efivars *__efivars; | |
5d9db883 | 39 | |
e971318b | 40 | static bool efivar_wq_enabled = true; |
a9499fa7 TG |
41 | DECLARE_WORK(efivar_work, NULL); |
42 | EXPORT_SYMBOL_GPL(efivar_work); | |
a93bc0c6 | 43 | |
fec6c20b | 44 | static bool |
a5d92ad3 | 45 | validate_device_path(efi_char16_t *var_name, int match, u8 *buffer, |
54b3a4d3 | 46 | unsigned long len) |
fec6c20b MG |
47 | { |
48 | struct efi_generic_dev_path *node; | |
49 | int offset = 0; | |
50 | ||
51 | node = (struct efi_generic_dev_path *)buffer; | |
52 | ||
54b3a4d3 MG |
53 | if (len < sizeof(*node)) |
54 | return false; | |
fec6c20b | 55 | |
54b3a4d3 MG |
56 | while (offset <= len - sizeof(*node) && |
57 | node->length >= sizeof(*node) && | |
58 | node->length <= len - offset) { | |
59 | offset += node->length; | |
fec6c20b MG |
60 | |
61 | if ((node->type == EFI_DEV_END_PATH || | |
62 | node->type == EFI_DEV_END_PATH2) && | |
63 | node->sub_type == EFI_DEV_END_ENTIRE) | |
64 | return true; | |
65 | ||
66 | node = (struct efi_generic_dev_path *)(buffer + offset); | |
67 | } | |
68 | ||
69 | /* | |
70 | * If we're here then either node->length pointed past the end | |
71 | * of the buffer or we reached the end of the buffer without | |
72 | * finding a device path end node. | |
73 | */ | |
74 | return false; | |
75 | } | |
76 | ||
77 | static bool | |
a5d92ad3 | 78 | validate_boot_order(efi_char16_t *var_name, int match, u8 *buffer, |
54b3a4d3 | 79 | unsigned long len) |
fec6c20b MG |
80 | { |
81 | /* An array of 16-bit integers */ | |
82 | if ((len % 2) != 0) | |
83 | return false; | |
84 | ||
85 | return true; | |
86 | } | |
87 | ||
88 | static bool | |
a5d92ad3 | 89 | validate_load_option(efi_char16_t *var_name, int match, u8 *buffer, |
54b3a4d3 | 90 | unsigned long len) |
fec6c20b MG |
91 | { |
92 | u16 filepathlength; | |
54b3a4d3 MG |
93 | int i, desclength = 0, namelen; |
94 | ||
a5d92ad3 | 95 | namelen = ucs2_strnlen(var_name, EFI_VAR_NAME_LEN); |
fec6c20b MG |
96 | |
97 | /* Either "Boot" or "Driver" followed by four digits of hex */ | |
98 | for (i = match; i < match+4; i++) { | |
a5d92ad3 MF |
99 | if (var_name[i] > 127 || |
100 | hex_to_bin(var_name[i] & 0xff) < 0) | |
fec6c20b MG |
101 | return true; |
102 | } | |
103 | ||
54b3a4d3 MG |
104 | /* Reject it if there's 4 digits of hex and then further content */ |
105 | if (namelen > match + 4) | |
106 | return false; | |
107 | ||
108 | /* A valid entry must be at least 8 bytes */ | |
109 | if (len < 8) | |
fec6c20b MG |
110 | return false; |
111 | ||
112 | filepathlength = buffer[4] | buffer[5] << 8; | |
113 | ||
114 | /* | |
115 | * There's no stored length for the description, so it has to be | |
116 | * found by hand | |
117 | */ | |
a614e192 | 118 | desclength = ucs2_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2; |
fec6c20b MG |
119 | |
120 | /* Each boot entry must have a descriptor */ | |
121 | if (!desclength) | |
122 | return false; | |
123 | ||
124 | /* | |
125 | * If the sum of the length of the description, the claimed filepath | |
126 | * length and the original header are greater than the length of the | |
127 | * variable, it's malformed | |
128 | */ | |
129 | if ((desclength + filepathlength + 6) > len) | |
130 | return false; | |
131 | ||
132 | /* | |
133 | * And, finally, check the filepath | |
134 | */ | |
a5d92ad3 | 135 | return validate_device_path(var_name, match, buffer + desclength + 6, |
fec6c20b MG |
136 | filepathlength); |
137 | } | |
138 | ||
139 | static bool | |
a5d92ad3 | 140 | validate_uint16(efi_char16_t *var_name, int match, u8 *buffer, |
54b3a4d3 | 141 | unsigned long len) |
fec6c20b MG |
142 | { |
143 | /* A single 16-bit integer */ | |
144 | if (len != 2) | |
145 | return false; | |
146 | ||
147 | return true; | |
148 | } | |
149 | ||
150 | static bool | |
a5d92ad3 | 151 | validate_ascii_string(efi_char16_t *var_name, int match, u8 *buffer, |
54b3a4d3 | 152 | unsigned long len) |
fec6c20b MG |
153 | { |
154 | int i; | |
155 | ||
156 | for (i = 0; i < len; i++) { | |
157 | if (buffer[i] > 127) | |
158 | return false; | |
159 | ||
160 | if (buffer[i] == 0) | |
161 | return true; | |
162 | } | |
163 | ||
164 | return false; | |
165 | } | |
166 | ||
167 | struct variable_validate { | |
8282f5d9 | 168 | efi_guid_t vendor; |
fec6c20b | 169 | char *name; |
a5d92ad3 | 170 | bool (*validate)(efi_char16_t *var_name, int match, u8 *data, |
54b3a4d3 | 171 | unsigned long len); |
fec6c20b MG |
172 | }; |
173 | ||
8282f5d9 | 174 | /* |
ed8b0de5 PJ |
175 | * This is the list of variables we need to validate, as well as the |
176 | * whitelist for what we think is safe not to default to immutable. | |
8282f5d9 PJ |
177 | * |
178 | * If it has a validate() method that's not NULL, it'll go into the | |
ed8b0de5 PJ |
179 | * validation routine. If not, it is assumed valid, but still used for |
180 | * whitelisting. | |
8282f5d9 PJ |
181 | * |
182 | * Note that it's sorted by {vendor,name}, but globbed names must come after | |
183 | * any other name with the same prefix. | |
184 | */ | |
fec6c20b | 185 | static const struct variable_validate variable_validate[] = { |
8282f5d9 PJ |
186 | { EFI_GLOBAL_VARIABLE_GUID, "BootNext", validate_uint16 }, |
187 | { EFI_GLOBAL_VARIABLE_GUID, "BootOrder", validate_boot_order }, | |
188 | { EFI_GLOBAL_VARIABLE_GUID, "Boot*", validate_load_option }, | |
189 | { EFI_GLOBAL_VARIABLE_GUID, "DriverOrder", validate_boot_order }, | |
190 | { EFI_GLOBAL_VARIABLE_GUID, "Driver*", validate_load_option }, | |
191 | { EFI_GLOBAL_VARIABLE_GUID, "ConIn", validate_device_path }, | |
192 | { EFI_GLOBAL_VARIABLE_GUID, "ConInDev", validate_device_path }, | |
193 | { EFI_GLOBAL_VARIABLE_GUID, "ConOut", validate_device_path }, | |
194 | { EFI_GLOBAL_VARIABLE_GUID, "ConOutDev", validate_device_path }, | |
195 | { EFI_GLOBAL_VARIABLE_GUID, "ErrOut", validate_device_path }, | |
196 | { EFI_GLOBAL_VARIABLE_GUID, "ErrOutDev", validate_device_path }, | |
197 | { EFI_GLOBAL_VARIABLE_GUID, "Lang", validate_ascii_string }, | |
ed8b0de5 | 198 | { EFI_GLOBAL_VARIABLE_GUID, "OsIndications", NULL }, |
8282f5d9 PJ |
199 | { EFI_GLOBAL_VARIABLE_GUID, "PlatformLang", validate_ascii_string }, |
200 | { EFI_GLOBAL_VARIABLE_GUID, "Timeout", validate_uint16 }, | |
e246eb56 | 201 | { LINUX_EFI_CRASH_GUID, "*", NULL }, |
8282f5d9 | 202 | { NULL_GUID, "", NULL }, |
fec6c20b MG |
203 | }; |
204 | ||
630ba0cc LE |
205 | /* |
206 | * Check if @var_name matches the pattern given in @match_name. | |
207 | * | |
208 | * @var_name: an array of @len non-NUL characters. | |
209 | * @match_name: a NUL-terminated pattern string, optionally ending in "*". A | |
210 | * final "*" character matches any trailing characters @var_name, | |
211 | * including the case when there are none left in @var_name. | |
212 | * @match: on output, the number of non-wildcard characters in @match_name | |
213 | * that @var_name matches, regardless of the return value. | |
214 | * @return: whether @var_name fully matches @match_name. | |
215 | */ | |
ed8b0de5 PJ |
216 | static bool |
217 | variable_matches(const char *var_name, size_t len, const char *match_name, | |
218 | int *match) | |
219 | { | |
220 | for (*match = 0; ; (*match)++) { | |
221 | char c = match_name[*match]; | |
ed8b0de5 | 222 | |
630ba0cc LE |
223 | switch (c) { |
224 | case '*': | |
225 | /* Wildcard in @match_name means we've matched. */ | |
ed8b0de5 PJ |
226 | return true; |
227 | ||
630ba0cc LE |
228 | case '\0': |
229 | /* @match_name has ended. Has @var_name too? */ | |
230 | return (*match == len); | |
231 | ||
232 | default: | |
233 | /* | |
234 | * We've reached a non-wildcard char in @match_name. | |
235 | * Continue only if there's an identical character in | |
236 | * @var_name. | |
237 | */ | |
238 | if (*match < len && c == var_name[*match]) | |
239 | continue; | |
ed8b0de5 | 240 | return false; |
630ba0cc | 241 | } |
ed8b0de5 | 242 | } |
ed8b0de5 PJ |
243 | } |
244 | ||
e14ab23d | 245 | bool |
8282f5d9 PJ |
246 | efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data, |
247 | unsigned long data_size) | |
fec6c20b MG |
248 | { |
249 | int i; | |
3dcb1f55 PJ |
250 | unsigned long utf8_size; |
251 | u8 *utf8_name; | |
fec6c20b | 252 | |
3dcb1f55 PJ |
253 | utf8_size = ucs2_utf8size(var_name); |
254 | utf8_name = kmalloc(utf8_size + 1, GFP_KERNEL); | |
255 | if (!utf8_name) | |
256 | return false; | |
fec6c20b | 257 | |
3dcb1f55 PJ |
258 | ucs2_as_utf8(utf8_name, var_name, utf8_size); |
259 | utf8_name[utf8_size] = '\0'; | |
fec6c20b | 260 | |
8282f5d9 | 261 | for (i = 0; variable_validate[i].name[0] != '\0'; i++) { |
fec6c20b | 262 | const char *name = variable_validate[i].name; |
8282f5d9 | 263 | int match = 0; |
fec6c20b | 264 | |
8282f5d9 PJ |
265 | if (efi_guidcmp(vendor, variable_validate[i].vendor)) |
266 | continue; | |
fec6c20b | 267 | |
ed8b0de5 PJ |
268 | if (variable_matches(utf8_name, utf8_size+1, name, &match)) { |
269 | if (variable_validate[i].validate == NULL) | |
fec6c20b | 270 | break; |
ed8b0de5 PJ |
271 | kfree(utf8_name); |
272 | return variable_validate[i].validate(var_name, match, | |
273 | data, data_size); | |
fec6c20b MG |
274 | } |
275 | } | |
3dcb1f55 | 276 | kfree(utf8_name); |
fec6c20b MG |
277 | return true; |
278 | } | |
e14ab23d | 279 | EXPORT_SYMBOL_GPL(efivar_validate); |
fec6c20b | 280 | |
ed8b0de5 PJ |
281 | bool |
282 | efivar_variable_is_removable(efi_guid_t vendor, const char *var_name, | |
283 | size_t len) | |
284 | { | |
285 | int i; | |
286 | bool found = false; | |
287 | int match = 0; | |
288 | ||
289 | /* | |
290 | * Check if our variable is in the validated variables list | |
291 | */ | |
292 | for (i = 0; variable_validate[i].name[0] != '\0'; i++) { | |
293 | if (efi_guidcmp(variable_validate[i].vendor, vendor)) | |
294 | continue; | |
295 | ||
296 | if (variable_matches(var_name, len, | |
297 | variable_validate[i].name, &match)) { | |
298 | found = true; | |
299 | break; | |
300 | } | |
301 | } | |
302 | ||
303 | /* | |
304 | * If it's in our list, it is removable. | |
305 | */ | |
306 | return found; | |
307 | } | |
308 | EXPORT_SYMBOL_GPL(efivar_variable_is_removable); | |
309 | ||
1da177e4 | 310 | static efi_status_t |
e14ab23d | 311 | check_var_size(u32 attributes, unsigned long size) |
68d92986 | 312 | { |
e14ab23d | 313 | const struct efivar_operations *fops = __efivars->ops; |
68d92986 | 314 | |
a614e192 | 315 | if (!fops->query_variable_store) |
68d92986 MG |
316 | return EFI_UNSUPPORTED; |
317 | ||
ca0e30dc AB |
318 | return fops->query_variable_store(attributes, size, false); |
319 | } | |
320 | ||
321 | static efi_status_t | |
322 | check_var_size_nonblocking(u32 attributes, unsigned long size) | |
323 | { | |
324 | const struct efivar_operations *fops = __efivars->ops; | |
325 | ||
326 | if (!fops->query_variable_store) | |
327 | return EFI_UNSUPPORTED; | |
328 | ||
329 | return fops->query_variable_store(attributes, size, true); | |
68d92986 MG |
330 | } |
331 | ||
7253eaba MF |
332 | static int efi_status_to_err(efi_status_t status) |
333 | { | |
334 | int err; | |
335 | ||
336 | switch (status) { | |
e14ab23d MF |
337 | case EFI_SUCCESS: |
338 | err = 0; | |
339 | break; | |
7253eaba MF |
340 | case EFI_INVALID_PARAMETER: |
341 | err = -EINVAL; | |
342 | break; | |
343 | case EFI_OUT_OF_RESOURCES: | |
344 | err = -ENOSPC; | |
345 | break; | |
346 | case EFI_DEVICE_ERROR: | |
347 | err = -EIO; | |
348 | break; | |
349 | case EFI_WRITE_PROTECTED: | |
350 | err = -EROFS; | |
351 | break; | |
352 | case EFI_SECURITY_VIOLATION: | |
353 | err = -EACCES; | |
354 | break; | |
355 | case EFI_NOT_FOUND: | |
e14ab23d | 356 | err = -ENOENT; |
7253eaba MF |
357 | break; |
358 | default: | |
359 | err = -EINVAL; | |
360 | } | |
361 | ||
362 | return err; | |
363 | } | |
364 | ||
e14ab23d MF |
365 | static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor, |
366 | struct list_head *head) | |
a93bc0c6 SA |
367 | { |
368 | struct efivar_entry *entry, *n; | |
a93bc0c6 SA |
369 | unsigned long strsize1, strsize2; |
370 | bool found = false; | |
371 | ||
a614e192 | 372 | strsize1 = ucs2_strsize(variable_name, 1024); |
e14ab23d | 373 | list_for_each_entry_safe(entry, n, head, list) { |
a614e192 | 374 | strsize2 = ucs2_strsize(entry->var.VariableName, 1024); |
a93bc0c6 SA |
375 | if (strsize1 == strsize2 && |
376 | !memcmp(variable_name, &(entry->var.VariableName), | |
377 | strsize2) && | |
378 | !efi_guidcmp(entry->var.VendorGuid, | |
379 | *vendor)) { | |
380 | found = true; | |
381 | break; | |
382 | } | |
383 | } | |
384 | return found; | |
385 | } | |
386 | ||
ec50bd32 MF |
387 | /* |
388 | * Returns the size of variable_name, in bytes, including the | |
389 | * terminating NULL character, or variable_name_size if no NULL | |
390 | * character is found among the first variable_name_size bytes. | |
391 | */ | |
392 | static unsigned long var_name_strnsize(efi_char16_t *variable_name, | |
393 | unsigned long variable_name_size) | |
394 | { | |
395 | unsigned long len; | |
396 | efi_char16_t c; | |
397 | ||
398 | /* | |
399 | * The variable name is, by definition, a NULL-terminated | |
400 | * string, so make absolutely sure that variable_name_size is | |
401 | * the value we expect it to be. If not, return the real size. | |
402 | */ | |
403 | for (len = 2; len <= variable_name_size; len += sizeof(c)) { | |
404 | c = variable_name[(len / sizeof(c)) - 1]; | |
405 | if (!c) | |
406 | break; | |
407 | } | |
408 | ||
409 | return min(len, variable_name_size); | |
410 | } | |
411 | ||
e971318b MF |
412 | /* |
413 | * Print a warning when duplicate EFI variables are encountered and | |
414 | * disable the sysfs workqueue since the firmware is buggy. | |
415 | */ | |
b2fce819 | 416 | static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid, |
e971318b MF |
417 | unsigned long len16) |
418 | { | |
419 | size_t i, len8 = len16 / sizeof(efi_char16_t); | |
b2fce819 | 420 | char *str8; |
e971318b MF |
421 | |
422 | /* | |
423 | * Disable the workqueue since the algorithm it uses for | |
424 | * detecting new variables won't work with this buggy | |
425 | * implementation of GetNextVariableName(). | |
426 | */ | |
427 | efivar_wq_enabled = false; | |
428 | ||
b2fce819 MR |
429 | str8 = kzalloc(len8, GFP_KERNEL); |
430 | if (!str8) | |
e971318b MF |
431 | return; |
432 | ||
433 | for (i = 0; i < len8; i++) | |
b2fce819 | 434 | str8[i] = str16[i]; |
e971318b MF |
435 | |
436 | printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n", | |
b2fce819 MR |
437 | str8, vendor_guid); |
438 | kfree(str8); | |
e971318b MF |
439 | } |
440 | ||
e14ab23d MF |
441 | /** |
442 | * efivar_init - build the initial list of EFI variables | |
443 | * @func: callback function to invoke for every variable | |
444 | * @data: function-specific data to pass to @func | |
445 | * @atomic: do we need to execute the @func-loop atomically? | |
446 | * @duplicates: error if we encounter duplicates on @head? | |
447 | * @head: initialised head of variable list | |
448 | * | |
449 | * Get every EFI variable from the firmware and invoke @func. @func | |
450 | * should call efivar_entry_add() to build the list of variables. | |
451 | * | |
452 | * Returns 0 on success, or a kernel error code on failure. | |
453 | */ | |
454 | int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *), | |
455 | void *data, bool atomic, bool duplicates, | |
456 | struct list_head *head) | |
457 | { | |
458 | const struct efivar_operations *ops = __efivars->ops; | |
459 | unsigned long variable_name_size = 1024; | |
460 | efi_char16_t *variable_name; | |
461 | efi_status_t status; | |
462 | efi_guid_t vendor_guid; | |
463 | int err = 0; | |
464 | ||
465 | variable_name = kzalloc(variable_name_size, GFP_KERNEL); | |
466 | if (!variable_name) { | |
467 | printk(KERN_ERR "efivars: Memory allocation failed.\n"); | |
468 | return -ENOMEM; | |
605e70c7 LCY |
469 | } |
470 | ||
e14ab23d MF |
471 | spin_lock_irq(&__efivars->lock); |
472 | ||
1da177e4 LT |
473 | /* |
474 | * Per EFI spec, the maximum storage allocated for both | |
475 | * the variable name and variable data is 1024 bytes. | |
476 | */ | |
477 | ||
478 | do { | |
479 | variable_name_size = 1024; | |
480 | ||
3295814d | 481 | status = ops->get_next_variable(&variable_name_size, |
1da177e4 LT |
482 | variable_name, |
483 | &vendor_guid); | |
484 | switch (status) { | |
485 | case EFI_SUCCESS: | |
e14ab23d MF |
486 | if (!atomic) |
487 | spin_unlock_irq(&__efivars->lock); | |
488 | ||
ec50bd32 MF |
489 | variable_name_size = var_name_strnsize(variable_name, |
490 | variable_name_size); | |
e971318b MF |
491 | |
492 | /* | |
493 | * Some firmware implementations return the | |
494 | * same variable name on multiple calls to | |
495 | * get_next_variable(). Terminate the loop | |
496 | * immediately as there is no guarantee that | |
497 | * we'll ever see a different variable name, | |
498 | * and may end up looping here forever. | |
499 | */ | |
e14ab23d MF |
500 | if (duplicates && |
501 | variable_is_present(variable_name, &vendor_guid, head)) { | |
e971318b MF |
502 | dup_variable_bug(variable_name, &vendor_guid, |
503 | variable_name_size); | |
e14ab23d MF |
504 | if (!atomic) |
505 | spin_lock_irq(&__efivars->lock); | |
506 | ||
e971318b MF |
507 | status = EFI_NOT_FOUND; |
508 | break; | |
509 | } | |
510 | ||
e14ab23d MF |
511 | err = func(variable_name, vendor_guid, variable_name_size, data); |
512 | if (err) | |
513 | status = EFI_NOT_FOUND; | |
514 | ||
515 | if (!atomic) | |
516 | spin_lock_irq(&__efivars->lock); | |
517 | ||
1da177e4 LT |
518 | break; |
519 | case EFI_NOT_FOUND: | |
520 | break; | |
521 | default: | |
522 | printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n", | |
523 | status); | |
524 | status = EFI_NOT_FOUND; | |
525 | break; | |
526 | } | |
e14ab23d | 527 | |
1da177e4 LT |
528 | } while (status != EFI_NOT_FOUND); |
529 | ||
e14ab23d MF |
530 | spin_unlock_irq(&__efivars->lock); |
531 | ||
532 | kfree(variable_name); | |
533 | ||
534 | return err; | |
535 | } | |
536 | EXPORT_SYMBOL_GPL(efivar_init); | |
537 | ||
538 | /** | |
539 | * efivar_entry_add - add entry to variable list | |
540 | * @entry: entry to add to list | |
541 | * @head: list head | |
542 | */ | |
543 | void efivar_entry_add(struct efivar_entry *entry, struct list_head *head) | |
544 | { | |
545 | spin_lock_irq(&__efivars->lock); | |
546 | list_add(&entry->list, head); | |
547 | spin_unlock_irq(&__efivars->lock); | |
548 | } | |
549 | EXPORT_SYMBOL_GPL(efivar_entry_add); | |
550 | ||
551 | /** | |
552 | * efivar_entry_remove - remove entry from variable list | |
553 | * @entry: entry to remove from list | |
554 | */ | |
555 | void efivar_entry_remove(struct efivar_entry *entry) | |
556 | { | |
557 | spin_lock_irq(&__efivars->lock); | |
558 | list_del(&entry->list); | |
559 | spin_unlock_irq(&__efivars->lock); | |
560 | } | |
561 | EXPORT_SYMBOL_GPL(efivar_entry_remove); | |
562 | ||
563 | /* | |
564 | * efivar_entry_list_del_unlock - remove entry from variable list | |
565 | * @entry: entry to remove | |
566 | * | |
567 | * Remove @entry from the variable list and release the list lock. | |
568 | * | |
569 | * NOTE: slightly weird locking semantics here - we expect to be | |
570 | * called with the efivars lock already held, and we release it before | |
571 | * returning. This is because this function is usually called after | |
572 | * set_variable() while the lock is still held. | |
573 | */ | |
574 | static void efivar_entry_list_del_unlock(struct efivar_entry *entry) | |
575 | { | |
aee530cf | 576 | lockdep_assert_held(&__efivars->lock); |
e14ab23d MF |
577 | |
578 | list_del(&entry->list); | |
579 | spin_unlock_irq(&__efivars->lock); | |
580 | } | |
581 | ||
582 | /** | |
583 | * __efivar_entry_delete - delete an EFI variable | |
584 | * @entry: entry containing EFI variable to delete | |
585 | * | |
a9499fa7 TG |
586 | * Delete the variable from the firmware but leave @entry on the |
587 | * variable list. | |
e14ab23d | 588 | * |
a9499fa7 TG |
589 | * This function differs from efivar_entry_delete() because it does |
590 | * not remove @entry from the variable list. Also, it is safe to be | |
591 | * called from within a efivar_entry_iter_begin() and | |
e14ab23d MF |
592 | * efivar_entry_iter_end() region, unlike efivar_entry_delete(). |
593 | * | |
594 | * Returns 0 on success, or a converted EFI status code if | |
a9499fa7 | 595 | * set_variable() fails. |
e14ab23d MF |
596 | */ |
597 | int __efivar_entry_delete(struct efivar_entry *entry) | |
598 | { | |
599 | const struct efivar_operations *ops = __efivars->ops; | |
600 | efi_status_t status; | |
601 | ||
aee530cf | 602 | lockdep_assert_held(&__efivars->lock); |
e14ab23d MF |
603 | |
604 | status = ops->set_variable(entry->var.VariableName, | |
605 | &entry->var.VendorGuid, | |
606 | 0, 0, NULL); | |
e14ab23d | 607 | |
a9499fa7 | 608 | return efi_status_to_err(status); |
e14ab23d MF |
609 | } |
610 | EXPORT_SYMBOL_GPL(__efivar_entry_delete); | |
611 | ||
612 | /** | |
613 | * efivar_entry_delete - delete variable and remove entry from list | |
614 | * @entry: entry containing variable to delete | |
615 | * | |
616 | * Delete the variable from the firmware and remove @entry from the | |
617 | * variable list. It is the caller's responsibility to free @entry | |
618 | * once we return. | |
619 | * | |
620 | * Returns 0 on success, or a converted EFI status code if | |
621 | * set_variable() fails. | |
622 | */ | |
623 | int efivar_entry_delete(struct efivar_entry *entry) | |
624 | { | |
625 | const struct efivar_operations *ops = __efivars->ops; | |
626 | efi_status_t status; | |
627 | ||
628 | spin_lock_irq(&__efivars->lock); | |
629 | status = ops->set_variable(entry->var.VariableName, | |
630 | &entry->var.VendorGuid, | |
631 | 0, 0, NULL); | |
632 | if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) { | |
633 | spin_unlock_irq(&__efivars->lock); | |
634 | return efi_status_to_err(status); | |
635 | } | |
636 | ||
637 | efivar_entry_list_del_unlock(entry); | |
638 | return 0; | |
639 | } | |
640 | EXPORT_SYMBOL_GPL(efivar_entry_delete); | |
641 | ||
642 | /** | |
643 | * efivar_entry_set - call set_variable() | |
644 | * @entry: entry containing the EFI variable to write | |
645 | * @attributes: variable attributes | |
646 | * @size: size of @data buffer | |
647 | * @data: buffer containing variable data | |
648 | * @head: head of variable list | |
649 | * | |
650 | * Calls set_variable() for an EFI variable. If creating a new EFI | |
651 | * variable, this function is usually followed by efivar_entry_add(). | |
652 | * | |
653 | * Before writing the variable, the remaining EFI variable storage | |
654 | * space is checked to ensure there is enough room available. | |
655 | * | |
656 | * If @head is not NULL a lookup is performed to determine whether | |
657 | * the entry is already on the list. | |
658 | * | |
659 | * Returns 0 on success, -EEXIST if a lookup is performed and the entry | |
660 | * already exists on the list, or a converted EFI status code if | |
661 | * set_variable() fails. | |
662 | */ | |
663 | int efivar_entry_set(struct efivar_entry *entry, u32 attributes, | |
664 | unsigned long size, void *data, struct list_head *head) | |
665 | { | |
666 | const struct efivar_operations *ops = __efivars->ops; | |
667 | efi_status_t status; | |
668 | efi_char16_t *name = entry->var.VariableName; | |
669 | efi_guid_t vendor = entry->var.VendorGuid; | |
670 | ||
671 | spin_lock_irq(&__efivars->lock); | |
672 | ||
673 | if (head && efivar_entry_find(name, vendor, head, false)) { | |
674 | spin_unlock_irq(&__efivars->lock); | |
675 | return -EEXIST; | |
676 | } | |
677 | ||
a614e192 | 678 | status = check_var_size(attributes, size + ucs2_strsize(name, 1024)); |
e14ab23d MF |
679 | if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED) |
680 | status = ops->set_variable(name, &vendor, | |
681 | attributes, size, data); | |
682 | ||
683 | spin_unlock_irq(&__efivars->lock); | |
684 | ||
685 | return efi_status_to_err(status); | |
a9499fa7 | 686 | |
e14ab23d MF |
687 | } |
688 | EXPORT_SYMBOL_GPL(efivar_entry_set); | |
689 | ||
6d80dba1 MF |
690 | /* |
691 | * efivar_entry_set_nonblocking - call set_variable_nonblocking() | |
692 | * | |
693 | * This function is guaranteed to not block and is suitable for calling | |
694 | * from crash/panic handlers. | |
695 | * | |
696 | * Crucially, this function will not block if it cannot acquire | |
697 | * __efivars->lock. Instead, it returns -EBUSY. | |
698 | */ | |
699 | static int | |
700 | efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor, | |
701 | u32 attributes, unsigned long size, void *data) | |
702 | { | |
703 | const struct efivar_operations *ops = __efivars->ops; | |
704 | unsigned long flags; | |
705 | efi_status_t status; | |
706 | ||
707 | if (!spin_trylock_irqsave(&__efivars->lock, flags)) | |
708 | return -EBUSY; | |
709 | ||
ca0e30dc AB |
710 | status = check_var_size_nonblocking(attributes, |
711 | size + ucs2_strsize(name, 1024)); | |
6d80dba1 MF |
712 | if (status != EFI_SUCCESS) { |
713 | spin_unlock_irqrestore(&__efivars->lock, flags); | |
714 | return -ENOSPC; | |
715 | } | |
716 | ||
717 | status = ops->set_variable_nonblocking(name, &vendor, attributes, | |
718 | size, data); | |
719 | ||
720 | spin_unlock_irqrestore(&__efivars->lock, flags); | |
721 | return efi_status_to_err(status); | |
722 | } | |
723 | ||
e14ab23d MF |
724 | /** |
725 | * efivar_entry_set_safe - call set_variable() if enough space in firmware | |
726 | * @name: buffer containing the variable name | |
727 | * @vendor: variable vendor guid | |
728 | * @attributes: variable attributes | |
729 | * @block: can we block in this context? | |
730 | * @size: size of @data buffer | |
731 | * @data: buffer containing variable data | |
732 | * | |
733 | * Ensures there is enough free storage in the firmware for this variable, and | |
734 | * if so, calls set_variable(). If creating a new EFI variable, this function | |
735 | * is usually followed by efivar_entry_add(). | |
736 | * | |
737 | * Returns 0 on success, -ENOSPC if the firmware does not have enough | |
738 | * space for set_variable() to succeed, or a converted EFI status code | |
739 | * if set_variable() fails. | |
740 | */ | |
741 | int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes, | |
742 | bool block, unsigned long size, void *data) | |
743 | { | |
744 | const struct efivar_operations *ops = __efivars->ops; | |
745 | unsigned long flags; | |
746 | efi_status_t status; | |
747 | ||
a614e192 | 748 | if (!ops->query_variable_store) |
e14ab23d MF |
749 | return -ENOSYS; |
750 | ||
6d80dba1 MF |
751 | /* |
752 | * If the EFI variable backend provides a non-blocking | |
753 | * ->set_variable() operation and we're in a context where we | |
754 | * cannot block, then we need to use it to avoid live-locks, | |
755 | * since the implication is that the regular ->set_variable() | |
756 | * will block. | |
757 | * | |
758 | * If no ->set_variable_nonblocking() is provided then | |
759 | * ->set_variable() is assumed to be non-blocking. | |
760 | */ | |
761 | if (!block && ops->set_variable_nonblocking) | |
762 | return efivar_entry_set_nonblocking(name, vendor, attributes, | |
763 | size, data); | |
764 | ||
85c90716 DC |
765 | if (!block) { |
766 | if (!spin_trylock_irqsave(&__efivars->lock, flags)) | |
767 | return -EBUSY; | |
768 | } else { | |
e14ab23d | 769 | spin_lock_irqsave(&__efivars->lock, flags); |
85c90716 | 770 | } |
e14ab23d | 771 | |
a614e192 | 772 | status = check_var_size(attributes, size + ucs2_strsize(name, 1024)); |
e14ab23d MF |
773 | if (status != EFI_SUCCESS) { |
774 | spin_unlock_irqrestore(&__efivars->lock, flags); | |
775 | return -ENOSPC; | |
776 | } | |
777 | ||
778 | status = ops->set_variable(name, &vendor, attributes, size, data); | |
779 | ||
780 | spin_unlock_irqrestore(&__efivars->lock, flags); | |
781 | ||
782 | return efi_status_to_err(status); | |
783 | } | |
784 | EXPORT_SYMBOL_GPL(efivar_entry_set_safe); | |
785 | ||
786 | /** | |
787 | * efivar_entry_find - search for an entry | |
788 | * @name: the EFI variable name | |
789 | * @guid: the EFI variable vendor's guid | |
790 | * @head: head of the variable list | |
791 | * @remove: should we remove the entry from the list? | |
792 | * | |
793 | * Search for an entry on the variable list that has the EFI variable | |
794 | * name @name and vendor guid @guid. If an entry is found on the list | |
795 | * and @remove is true, the entry is removed from the list. | |
796 | * | |
797 | * The caller MUST call efivar_entry_iter_begin() and | |
798 | * efivar_entry_iter_end() before and after the invocation of this | |
799 | * function, respectively. | |
800 | * | |
801 | * Returns the entry if found on the list, %NULL otherwise. | |
802 | */ | |
803 | struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid, | |
804 | struct list_head *head, bool remove) | |
805 | { | |
806 | struct efivar_entry *entry, *n; | |
807 | int strsize1, strsize2; | |
808 | bool found = false; | |
809 | ||
aee530cf | 810 | lockdep_assert_held(&__efivars->lock); |
e14ab23d MF |
811 | |
812 | list_for_each_entry_safe(entry, n, head, list) { | |
a614e192 MF |
813 | strsize1 = ucs2_strsize(name, 1024); |
814 | strsize2 = ucs2_strsize(entry->var.VariableName, 1024); | |
e14ab23d MF |
815 | if (strsize1 == strsize2 && |
816 | !memcmp(name, &(entry->var.VariableName), strsize1) && | |
817 | !efi_guidcmp(guid, entry->var.VendorGuid)) { | |
818 | found = true; | |
819 | break; | |
820 | } | |
821 | } | |
822 | ||
823 | if (!found) | |
824 | return NULL; | |
825 | ||
e0d59733 SA |
826 | if (remove) { |
827 | if (entry->scanning) { | |
828 | /* | |
829 | * The entry will be deleted | |
830 | * after scanning is completed. | |
831 | */ | |
832 | entry->deleting = true; | |
833 | } else | |
834 | list_del(&entry->list); | |
835 | } | |
e14ab23d MF |
836 | |
837 | return entry; | |
838 | } | |
839 | EXPORT_SYMBOL_GPL(efivar_entry_find); | |
840 | ||
841 | /** | |
8a415b8c | 842 | * efivar_entry_size - obtain the size of a variable |
e14ab23d MF |
843 | * @entry: entry for this variable |
844 | * @size: location to store the variable's size | |
e14ab23d | 845 | */ |
8a415b8c | 846 | int efivar_entry_size(struct efivar_entry *entry, unsigned long *size) |
e14ab23d MF |
847 | { |
848 | const struct efivar_operations *ops = __efivars->ops; | |
849 | efi_status_t status; | |
850 | ||
e14ab23d | 851 | *size = 0; |
8a415b8c MF |
852 | |
853 | spin_lock_irq(&__efivars->lock); | |
e14ab23d MF |
854 | status = ops->get_variable(entry->var.VariableName, |
855 | &entry->var.VendorGuid, NULL, size, NULL); | |
8a415b8c MF |
856 | spin_unlock_irq(&__efivars->lock); |
857 | ||
e14ab23d MF |
858 | if (status != EFI_BUFFER_TOO_SMALL) |
859 | return efi_status_to_err(status); | |
860 | ||
861 | return 0; | |
862 | } | |
8a415b8c | 863 | EXPORT_SYMBOL_GPL(efivar_entry_size); |
e14ab23d MF |
864 | |
865 | /** | |
8a415b8c MF |
866 | * __efivar_entry_get - call get_variable() |
867 | * @entry: read data for this variable | |
868 | * @attributes: variable attributes | |
869 | * @size: size of @data buffer | |
870 | * @data: buffer to store variable data | |
871 | * | |
872 | * The caller MUST call efivar_entry_iter_begin() and | |
873 | * efivar_entry_iter_end() before and after the invocation of this | |
874 | * function, respectively. | |
e14ab23d | 875 | */ |
8a415b8c MF |
876 | int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes, |
877 | unsigned long *size, void *data) | |
e14ab23d MF |
878 | { |
879 | const struct efivar_operations *ops = __efivars->ops; | |
880 | efi_status_t status; | |
881 | ||
aee530cf | 882 | lockdep_assert_held(&__efivars->lock); |
e14ab23d | 883 | |
e14ab23d | 884 | status = ops->get_variable(entry->var.VariableName, |
8a415b8c MF |
885 | &entry->var.VendorGuid, |
886 | attributes, size, data); | |
e14ab23d | 887 | |
8a415b8c | 888 | return efi_status_to_err(status); |
e14ab23d | 889 | } |
8a415b8c | 890 | EXPORT_SYMBOL_GPL(__efivar_entry_get); |
e14ab23d MF |
891 | |
892 | /** | |
893 | * efivar_entry_get - call get_variable() | |
894 | * @entry: read data for this variable | |
895 | * @attributes: variable attributes | |
896 | * @size: size of @data buffer | |
897 | * @data: buffer to store variable data | |
898 | */ | |
899 | int efivar_entry_get(struct efivar_entry *entry, u32 *attributes, | |
900 | unsigned long *size, void *data) | |
901 | { | |
902 | const struct efivar_operations *ops = __efivars->ops; | |
903 | efi_status_t status; | |
904 | ||
905 | spin_lock_irq(&__efivars->lock); | |
906 | status = ops->get_variable(entry->var.VariableName, | |
907 | &entry->var.VendorGuid, | |
908 | attributes, size, data); | |
909 | spin_unlock_irq(&__efivars->lock); | |
910 | ||
911 | return efi_status_to_err(status); | |
912 | } | |
913 | EXPORT_SYMBOL_GPL(efivar_entry_get); | |
914 | ||
915 | /** | |
916 | * efivar_entry_set_get_size - call set_variable() and get new size (atomic) | |
917 | * @entry: entry containing variable to set and get | |
918 | * @attributes: attributes of variable to be written | |
919 | * @size: size of data buffer | |
920 | * @data: buffer containing data to write | |
921 | * @set: did the set_variable() call succeed? | |
922 | * | |
923 | * This is a pretty special (complex) function. See efivarfs_file_write(). | |
924 | * | |
925 | * Atomically call set_variable() for @entry and if the call is | |
926 | * successful, return the new size of the variable from get_variable() | |
927 | * in @size. The success of set_variable() is indicated by @set. | |
928 | * | |
929 | * Returns 0 on success, -EINVAL if the variable data is invalid, | |
930 | * -ENOSPC if the firmware does not have enough available space, or a | |
931 | * converted EFI status code if either of set_variable() or | |
932 | * get_variable() fail. | |
933 | * | |
934 | * If the EFI variable does not exist when calling set_variable() | |
935 | * (EFI_NOT_FOUND), @entry is removed from the variable list. | |
936 | */ | |
937 | int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes, | |
938 | unsigned long *size, void *data, bool *set) | |
939 | { | |
940 | const struct efivar_operations *ops = __efivars->ops; | |
941 | efi_char16_t *name = entry->var.VariableName; | |
942 | efi_guid_t *vendor = &entry->var.VendorGuid; | |
943 | efi_status_t status; | |
944 | int err; | |
945 | ||
946 | *set = false; | |
947 | ||
8282f5d9 | 948 | if (efivar_validate(*vendor, name, data, *size) == false) |
e14ab23d MF |
949 | return -EINVAL; |
950 | ||
951 | /* | |
952 | * The lock here protects the get_variable call, the conditional | |
953 | * set_variable call, and removal of the variable from the efivars | |
954 | * list (in the case of an authenticated delete). | |
955 | */ | |
956 | spin_lock_irq(&__efivars->lock); | |
957 | ||
958 | /* | |
959 | * Ensure that the available space hasn't shrunk below the safe level | |
960 | */ | |
a614e192 | 961 | status = check_var_size(attributes, *size + ucs2_strsize(name, 1024)); |
e14ab23d MF |
962 | if (status != EFI_SUCCESS) { |
963 | if (status != EFI_UNSUPPORTED) { | |
964 | err = efi_status_to_err(status); | |
965 | goto out; | |
966 | } | |
967 | ||
968 | if (*size > 65536) { | |
969 | err = -ENOSPC; | |
970 | goto out; | |
971 | } | |
972 | } | |
973 | ||
974 | status = ops->set_variable(name, vendor, attributes, *size, data); | |
975 | if (status != EFI_SUCCESS) { | |
976 | err = efi_status_to_err(status); | |
977 | goto out; | |
978 | } | |
979 | ||
980 | *set = true; | |
981 | ||
982 | /* | |
983 | * Writing to the variable may have caused a change in size (which | |
984 | * could either be an append or an overwrite), or the variable to be | |
985 | * deleted. Perform a GetVariable() so we can tell what actually | |
986 | * happened. | |
987 | */ | |
988 | *size = 0; | |
989 | status = ops->get_variable(entry->var.VariableName, | |
990 | &entry->var.VendorGuid, | |
991 | NULL, size, NULL); | |
992 | ||
993 | if (status == EFI_NOT_FOUND) | |
994 | efivar_entry_list_del_unlock(entry); | |
995 | else | |
996 | spin_unlock_irq(&__efivars->lock); | |
997 | ||
998 | if (status && status != EFI_BUFFER_TOO_SMALL) | |
999 | return efi_status_to_err(status); | |
1000 | ||
1001 | return 0; | |
1002 | ||
1003 | out: | |
1004 | spin_unlock_irq(&__efivars->lock); | |
1005 | return err; | |
1006 | ||
1007 | } | |
1008 | EXPORT_SYMBOL_GPL(efivar_entry_set_get_size); | |
1009 | ||
1010 | /** | |
1011 | * efivar_entry_iter_begin - begin iterating the variable list | |
1012 | * | |
1013 | * Lock the variable list to prevent entry insertion and removal until | |
1014 | * efivar_entry_iter_end() is called. This function is usually used in | |
1015 | * conjunction with __efivar_entry_iter() or efivar_entry_iter(). | |
1016 | */ | |
1017 | void efivar_entry_iter_begin(void) | |
1018 | { | |
1019 | spin_lock_irq(&__efivars->lock); | |
1020 | } | |
1021 | EXPORT_SYMBOL_GPL(efivar_entry_iter_begin); | |
1022 | ||
1023 | /** | |
1024 | * efivar_entry_iter_end - finish iterating the variable list | |
1025 | * | |
1026 | * Unlock the variable list and allow modifications to the list again. | |
1027 | */ | |
1028 | void efivar_entry_iter_end(void) | |
1029 | { | |
1030 | spin_unlock_irq(&__efivars->lock); | |
1031 | } | |
1032 | EXPORT_SYMBOL_GPL(efivar_entry_iter_end); | |
1033 | ||
1034 | /** | |
1035 | * __efivar_entry_iter - iterate over variable list | |
1036 | * @func: callback function | |
1037 | * @head: head of the variable list | |
1038 | * @data: function-specific data to pass to callback | |
1039 | * @prev: entry to begin iterating from | |
1040 | * | |
1041 | * Iterate over the list of EFI variables and call @func with every | |
1042 | * entry on the list. It is safe for @func to remove entries in the | |
1043 | * list via efivar_entry_delete(). | |
1044 | * | |
1045 | * You MUST call efivar_enter_iter_begin() before this function, and | |
1046 | * efivar_entry_iter_end() afterwards. | |
1047 | * | |
1048 | * It is possible to begin iteration from an arbitrary entry within | |
1049 | * the list by passing @prev. @prev is updated on return to point to | |
1050 | * the last entry passed to @func. To begin iterating from the | |
1051 | * beginning of the list @prev must be %NULL. | |
1052 | * | |
1053 | * The restrictions for @func are the same as documented for | |
1054 | * efivar_entry_iter(). | |
1055 | */ | |
1056 | int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *), | |
1057 | struct list_head *head, void *data, | |
1058 | struct efivar_entry **prev) | |
1059 | { | |
1060 | struct efivar_entry *entry, *n; | |
1061 | int err = 0; | |
1062 | ||
1063 | if (!prev || !*prev) { | |
1064 | list_for_each_entry_safe(entry, n, head, list) { | |
1065 | err = func(entry, data); | |
1066 | if (err) | |
1067 | break; | |
1068 | } | |
1069 | ||
1070 | if (prev) | |
1071 | *prev = entry; | |
1072 | ||
1073 | return err; | |
1074 | } | |
1075 | ||
1076 | ||
1077 | list_for_each_entry_safe_continue((*prev), n, head, list) { | |
1078 | err = func(*prev, data); | |
1079 | if (err) | |
1080 | break; | |
1081 | } | |
1082 | ||
1083 | return err; | |
1084 | } | |
1085 | EXPORT_SYMBOL_GPL(__efivar_entry_iter); | |
1086 | ||
1087 | /** | |
1088 | * efivar_entry_iter - iterate over variable list | |
1089 | * @func: callback function | |
1090 | * @head: head of variable list | |
1091 | * @data: function-specific data to pass to callback | |
1092 | * | |
1093 | * Iterate over the list of EFI variables and call @func with every | |
1094 | * entry on the list. It is safe for @func to remove entries in the | |
1095 | * list via efivar_entry_delete() while iterating. | |
1096 | * | |
1097 | * Some notes for the callback function: | |
1098 | * - a non-zero return value indicates an error and terminates the loop | |
1099 | * - @func is called from atomic context | |
1100 | */ | |
1101 | int efivar_entry_iter(int (*func)(struct efivar_entry *, void *), | |
1102 | struct list_head *head, void *data) | |
1103 | { | |
1104 | int err = 0; | |
1105 | ||
1106 | efivar_entry_iter_begin(); | |
1107 | err = __efivar_entry_iter(func, head, data, NULL); | |
1108 | efivar_entry_iter_end(); | |
1109 | ||
1110 | return err; | |
1111 | } | |
1112 | EXPORT_SYMBOL_GPL(efivar_entry_iter); | |
1113 | ||
1114 | /** | |
1115 | * efivars_kobject - get the kobject for the registered efivars | |
1116 | * | |
1117 | * If efivars_register() has not been called we return NULL, | |
1118 | * otherwise return the kobject used at registration time. | |
1119 | */ | |
1120 | struct kobject *efivars_kobject(void) | |
1121 | { | |
1122 | if (!__efivars) | |
1123 | return NULL; | |
1124 | ||
1125 | return __efivars->kobject; | |
1126 | } | |
1127 | EXPORT_SYMBOL_GPL(efivars_kobject); | |
1128 | ||
04851772 MF |
1129 | /** |
1130 | * efivar_run_worker - schedule the efivar worker thread | |
1131 | */ | |
1132 | void efivar_run_worker(void) | |
1133 | { | |
1134 | if (efivar_wq_enabled) | |
1135 | schedule_work(&efivar_work); | |
1136 | } | |
1137 | EXPORT_SYMBOL_GPL(efivar_run_worker); | |
1138 | ||
e14ab23d MF |
1139 | /** |
1140 | * efivars_register - register an efivars | |
1141 | * @efivars: efivars to register | |
1142 | * @ops: efivars operations | |
1143 | * @kobject: @efivars-specific kobject | |
1144 | * | |
1145 | * Only a single efivars can be registered at any time. | |
1146 | */ | |
1147 | int efivars_register(struct efivars *efivars, | |
1148 | const struct efivar_operations *ops, | |
1149 | struct kobject *kobject) | |
1150 | { | |
1151 | spin_lock_init(&efivars->lock); | |
1152 | efivars->ops = ops; | |
1153 | efivars->kobject = kobject; | |
1154 | ||
1155 | __efivars = efivars; | |
1da177e4 | 1156 | |
e14ab23d MF |
1157 | return 0; |
1158 | } | |
1159 | EXPORT_SYMBOL_GPL(efivars_register); | |
1da177e4 | 1160 | |
e14ab23d MF |
1161 | /** |
1162 | * efivars_unregister - unregister an efivars | |
1163 | * @efivars: efivars to unregister | |
1164 | * | |
1165 | * The caller must have already removed every entry from the list, | |
1166 | * failure to do so is an error. | |
1167 | */ | |
1168 | int efivars_unregister(struct efivars *efivars) | |
1169 | { | |
1170 | int rv; | |
1171 | ||
1172 | if (!__efivars) { | |
1173 | printk(KERN_ERR "efivars not registered\n"); | |
1174 | rv = -EINVAL; | |
1175 | goto out; | |
1176 | } | |
1177 | ||
1178 | if (__efivars != efivars) { | |
1179 | rv = -EINVAL; | |
1180 | goto out; | |
1181 | } | |
1182 | ||
1183 | __efivars = NULL; | |
1184 | ||
1185 | rv = 0; | |
1186 | out: | |
1187 | return rv; | |
76b53f7c | 1188 | } |
e14ab23d | 1189 | EXPORT_SYMBOL_GPL(efivars_unregister); |