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