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 |
44f21ed1 | 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 | |
44f21ed1 | 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 | |
44f21ed1 | 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 | ||
44f21ed1 | 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++) { | |
44f21ed1 PJ |
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 | */ | |
44f21ed1 | 135 | return validate_device_path(var_name, match, buffer + desclength + 6, |
fec6c20b MG |
136 | filepathlength); |
137 | } | |
138 | ||
139 | static bool | |
44f21ed1 | 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 | |
44f21ed1 | 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 { | |
d591b6da | 168 | efi_guid_t vendor; |
fec6c20b | 169 | char *name; |
44f21ed1 | 170 | bool (*validate)(efi_char16_t *var_name, int match, u8 *data, |
54b3a4d3 | 171 | unsigned long len); |
fec6c20b MG |
172 | }; |
173 | ||
d591b6da | 174 | /* |
7b35014c 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. | |
d591b6da PJ |
177 | * |
178 | * If it has a validate() method that's not NULL, it'll go into the | |
7b35014c PJ |
179 | * validation routine. If not, it is assumed valid, but still used for |
180 | * whitelisting. | |
d591b6da 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[] = { |
d591b6da 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 }, | |
7b35014c | 198 | { EFI_GLOBAL_VARIABLE_GUID, "OsIndications", NULL }, |
d591b6da PJ |
199 | { EFI_GLOBAL_VARIABLE_GUID, "PlatformLang", validate_ascii_string }, |
200 | { EFI_GLOBAL_VARIABLE_GUID, "Timeout", validate_uint16 }, | |
15b988cf | 201 | { LINUX_EFI_CRASH_GUID, "*", NULL }, |
d591b6da | 202 | { NULL_GUID, "", NULL }, |
fec6c20b MG |
203 | }; |
204 | ||
e2d8aa41 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 | */ | |
7b35014c 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]; | |
7b35014c | 222 | |
e2d8aa41 LE |
223 | switch (c) { |
224 | case '*': | |
225 | /* Wildcard in @match_name means we've matched. */ | |
7b35014c PJ |
226 | return true; |
227 | ||
e2d8aa41 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; | |
7b35014c | 240 | return false; |
e2d8aa41 | 241 | } |
7b35014c | 242 | } |
7b35014c PJ |
243 | } |
244 | ||
e14ab23d | 245 | bool |
d591b6da 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; | |
44f21ed1 PJ |
250 | unsigned long utf8_size; |
251 | u8 *utf8_name; | |
252 | ||
253 | utf8_size = ucs2_utf8size(var_name); | |
254 | utf8_name = kmalloc(utf8_size + 1, GFP_KERNEL); | |
255 | if (!utf8_name) | |
256 | return false; | |
257 | ||
258 | ucs2_as_utf8(utf8_name, var_name, utf8_size); | |
259 | utf8_name[utf8_size] = '\0'; | |
fec6c20b | 260 | |
d591b6da | 261 | for (i = 0; variable_validate[i].name[0] != '\0'; i++) { |
fec6c20b | 262 | const char *name = variable_validate[i].name; |
d591b6da PJ |
263 | int match = 0; |
264 | ||
265 | if (efi_guidcmp(vendor, variable_validate[i].vendor)) | |
266 | continue; | |
fec6c20b | 267 | |
7b35014c PJ |
268 | if (variable_matches(utf8_name, utf8_size+1, name, &match)) { |
269 | if (variable_validate[i].validate == NULL) | |
fec6c20b | 270 | break; |
7b35014c PJ |
271 | kfree(utf8_name); |
272 | return variable_validate[i].validate(var_name, match, | |
273 | data, data_size); | |
fec6c20b MG |
274 | } |
275 | } | |
44f21ed1 | 276 | kfree(utf8_name); |
fec6c20b MG |
277 | return true; |
278 | } | |
e14ab23d | 279 | EXPORT_SYMBOL_GPL(efivar_validate); |
fec6c20b | 280 | |
7b35014c 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 | ||
a614e192 | 318 | return fops->query_variable_store(attributes, size); |
68d92986 MG |
319 | } |
320 | ||
7253eaba MF |
321 | static int efi_status_to_err(efi_status_t status) |
322 | { | |
323 | int err; | |
324 | ||
325 | switch (status) { | |
e14ab23d MF |
326 | case EFI_SUCCESS: |
327 | err = 0; | |
328 | break; | |
7253eaba MF |
329 | case EFI_INVALID_PARAMETER: |
330 | err = -EINVAL; | |
331 | break; | |
332 | case EFI_OUT_OF_RESOURCES: | |
333 | err = -ENOSPC; | |
334 | break; | |
335 | case EFI_DEVICE_ERROR: | |
336 | err = -EIO; | |
337 | break; | |
338 | case EFI_WRITE_PROTECTED: | |
339 | err = -EROFS; | |
340 | break; | |
341 | case EFI_SECURITY_VIOLATION: | |
342 | err = -EACCES; | |
343 | break; | |
344 | case EFI_NOT_FOUND: | |
e14ab23d | 345 | err = -ENOENT; |
7253eaba MF |
346 | break; |
347 | default: | |
348 | err = -EINVAL; | |
349 | } | |
350 | ||
351 | return err; | |
352 | } | |
353 | ||
e14ab23d MF |
354 | static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor, |
355 | struct list_head *head) | |
a93bc0c6 SA |
356 | { |
357 | struct efivar_entry *entry, *n; | |
a93bc0c6 SA |
358 | unsigned long strsize1, strsize2; |
359 | bool found = false; | |
360 | ||
a614e192 | 361 | strsize1 = ucs2_strsize(variable_name, 1024); |
e14ab23d | 362 | list_for_each_entry_safe(entry, n, head, list) { |
a614e192 | 363 | strsize2 = ucs2_strsize(entry->var.VariableName, 1024); |
a93bc0c6 SA |
364 | if (strsize1 == strsize2 && |
365 | !memcmp(variable_name, &(entry->var.VariableName), | |
366 | strsize2) && | |
367 | !efi_guidcmp(entry->var.VendorGuid, | |
368 | *vendor)) { | |
369 | found = true; | |
370 | break; | |
371 | } | |
372 | } | |
373 | return found; | |
374 | } | |
375 | ||
ec50bd32 MF |
376 | /* |
377 | * Returns the size of variable_name, in bytes, including the | |
378 | * terminating NULL character, or variable_name_size if no NULL | |
379 | * character is found among the first variable_name_size bytes. | |
380 | */ | |
381 | static unsigned long var_name_strnsize(efi_char16_t *variable_name, | |
382 | unsigned long variable_name_size) | |
383 | { | |
384 | unsigned long len; | |
385 | efi_char16_t c; | |
386 | ||
387 | /* | |
388 | * The variable name is, by definition, a NULL-terminated | |
389 | * string, so make absolutely sure that variable_name_size is | |
390 | * the value we expect it to be. If not, return the real size. | |
391 | */ | |
392 | for (len = 2; len <= variable_name_size; len += sizeof(c)) { | |
393 | c = variable_name[(len / sizeof(c)) - 1]; | |
394 | if (!c) | |
395 | break; | |
396 | } | |
397 | ||
398 | return min(len, variable_name_size); | |
399 | } | |
400 | ||
e971318b MF |
401 | /* |
402 | * Print a warning when duplicate EFI variables are encountered and | |
403 | * disable the sysfs workqueue since the firmware is buggy. | |
404 | */ | |
405 | static void dup_variable_bug(efi_char16_t *s16, efi_guid_t *vendor_guid, | |
406 | unsigned long len16) | |
407 | { | |
408 | size_t i, len8 = len16 / sizeof(efi_char16_t); | |
409 | char *s8; | |
410 | ||
411 | /* | |
412 | * Disable the workqueue since the algorithm it uses for | |
413 | * detecting new variables won't work with this buggy | |
414 | * implementation of GetNextVariableName(). | |
415 | */ | |
416 | efivar_wq_enabled = false; | |
417 | ||
418 | s8 = kzalloc(len8, GFP_KERNEL); | |
419 | if (!s8) | |
420 | return; | |
421 | ||
422 | for (i = 0; i < len8; i++) | |
423 | s8[i] = s16[i]; | |
424 | ||
425 | printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n", | |
426 | s8, vendor_guid); | |
427 | kfree(s8); | |
428 | } | |
429 | ||
e14ab23d MF |
430 | /** |
431 | * efivar_init - build the initial list of EFI variables | |
432 | * @func: callback function to invoke for every variable | |
433 | * @data: function-specific data to pass to @func | |
434 | * @atomic: do we need to execute the @func-loop atomically? | |
435 | * @duplicates: error if we encounter duplicates on @head? | |
436 | * @head: initialised head of variable list | |
437 | * | |
438 | * Get every EFI variable from the firmware and invoke @func. @func | |
439 | * should call efivar_entry_add() to build the list of variables. | |
440 | * | |
441 | * Returns 0 on success, or a kernel error code on failure. | |
442 | */ | |
443 | int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *), | |
444 | void *data, bool atomic, bool duplicates, | |
445 | struct list_head *head) | |
446 | { | |
447 | const struct efivar_operations *ops = __efivars->ops; | |
448 | unsigned long variable_name_size = 1024; | |
449 | efi_char16_t *variable_name; | |
450 | efi_status_t status; | |
451 | efi_guid_t vendor_guid; | |
452 | int err = 0; | |
453 | ||
454 | variable_name = kzalloc(variable_name_size, GFP_KERNEL); | |
455 | if (!variable_name) { | |
456 | printk(KERN_ERR "efivars: Memory allocation failed.\n"); | |
457 | return -ENOMEM; | |
605e70c7 LCY |
458 | } |
459 | ||
e14ab23d MF |
460 | spin_lock_irq(&__efivars->lock); |
461 | ||
1da177e4 LT |
462 | /* |
463 | * Per EFI spec, the maximum storage allocated for both | |
464 | * the variable name and variable data is 1024 bytes. | |
465 | */ | |
466 | ||
467 | do { | |
468 | variable_name_size = 1024; | |
469 | ||
3295814d | 470 | status = ops->get_next_variable(&variable_name_size, |
1da177e4 LT |
471 | variable_name, |
472 | &vendor_guid); | |
473 | switch (status) { | |
474 | case EFI_SUCCESS: | |
e14ab23d MF |
475 | if (!atomic) |
476 | spin_unlock_irq(&__efivars->lock); | |
477 | ||
ec50bd32 MF |
478 | variable_name_size = var_name_strnsize(variable_name, |
479 | variable_name_size); | |
e971318b MF |
480 | |
481 | /* | |
482 | * Some firmware implementations return the | |
483 | * same variable name on multiple calls to | |
484 | * get_next_variable(). Terminate the loop | |
485 | * immediately as there is no guarantee that | |
486 | * we'll ever see a different variable name, | |
487 | * and may end up looping here forever. | |
488 | */ | |
e14ab23d MF |
489 | if (duplicates && |
490 | variable_is_present(variable_name, &vendor_guid, head)) { | |
e971318b MF |
491 | dup_variable_bug(variable_name, &vendor_guid, |
492 | variable_name_size); | |
e14ab23d MF |
493 | if (!atomic) |
494 | spin_lock_irq(&__efivars->lock); | |
495 | ||
e971318b MF |
496 | status = EFI_NOT_FOUND; |
497 | break; | |
498 | } | |
499 | ||
e14ab23d MF |
500 | err = func(variable_name, vendor_guid, variable_name_size, data); |
501 | if (err) | |
502 | status = EFI_NOT_FOUND; | |
503 | ||
504 | if (!atomic) | |
505 | spin_lock_irq(&__efivars->lock); | |
506 | ||
1da177e4 LT |
507 | break; |
508 | case EFI_NOT_FOUND: | |
509 | break; | |
510 | default: | |
511 | printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n", | |
512 | status); | |
513 | status = EFI_NOT_FOUND; | |
514 | break; | |
515 | } | |
e14ab23d | 516 | |
1da177e4 LT |
517 | } while (status != EFI_NOT_FOUND); |
518 | ||
e14ab23d MF |
519 | spin_unlock_irq(&__efivars->lock); |
520 | ||
521 | kfree(variable_name); | |
522 | ||
523 | return err; | |
524 | } | |
525 | EXPORT_SYMBOL_GPL(efivar_init); | |
526 | ||
527 | /** | |
528 | * efivar_entry_add - add entry to variable list | |
529 | * @entry: entry to add to list | |
530 | * @head: list head | |
531 | */ | |
532 | void efivar_entry_add(struct efivar_entry *entry, struct list_head *head) | |
533 | { | |
534 | spin_lock_irq(&__efivars->lock); | |
535 | list_add(&entry->list, head); | |
536 | spin_unlock_irq(&__efivars->lock); | |
537 | } | |
538 | EXPORT_SYMBOL_GPL(efivar_entry_add); | |
539 | ||
540 | /** | |
541 | * efivar_entry_remove - remove entry from variable list | |
542 | * @entry: entry to remove from list | |
543 | */ | |
544 | void efivar_entry_remove(struct efivar_entry *entry) | |
545 | { | |
546 | spin_lock_irq(&__efivars->lock); | |
547 | list_del(&entry->list); | |
548 | spin_unlock_irq(&__efivars->lock); | |
549 | } | |
550 | EXPORT_SYMBOL_GPL(efivar_entry_remove); | |
551 | ||
552 | /* | |
553 | * efivar_entry_list_del_unlock - remove entry from variable list | |
554 | * @entry: entry to remove | |
555 | * | |
556 | * Remove @entry from the variable list and release the list lock. | |
557 | * | |
558 | * NOTE: slightly weird locking semantics here - we expect to be | |
559 | * called with the efivars lock already held, and we release it before | |
560 | * returning. This is because this function is usually called after | |
561 | * set_variable() while the lock is still held. | |
562 | */ | |
563 | static void efivar_entry_list_del_unlock(struct efivar_entry *entry) | |
564 | { | |
79943632 | 565 | lockdep_assert_held(&__efivars->lock); |
e14ab23d MF |
566 | |
567 | list_del(&entry->list); | |
568 | spin_unlock_irq(&__efivars->lock); | |
569 | } | |
570 | ||
571 | /** | |
572 | * __efivar_entry_delete - delete an EFI variable | |
573 | * @entry: entry containing EFI variable to delete | |
574 | * | |
a9499fa7 TG |
575 | * Delete the variable from the firmware but leave @entry on the |
576 | * variable list. | |
e14ab23d | 577 | * |
a9499fa7 TG |
578 | * This function differs from efivar_entry_delete() because it does |
579 | * not remove @entry from the variable list. Also, it is safe to be | |
580 | * called from within a efivar_entry_iter_begin() and | |
e14ab23d MF |
581 | * efivar_entry_iter_end() region, unlike efivar_entry_delete(). |
582 | * | |
583 | * Returns 0 on success, or a converted EFI status code if | |
a9499fa7 | 584 | * set_variable() fails. |
e14ab23d MF |
585 | */ |
586 | int __efivar_entry_delete(struct efivar_entry *entry) | |
587 | { | |
588 | const struct efivar_operations *ops = __efivars->ops; | |
589 | efi_status_t status; | |
590 | ||
79943632 | 591 | lockdep_assert_held(&__efivars->lock); |
e14ab23d MF |
592 | |
593 | status = ops->set_variable(entry->var.VariableName, | |
594 | &entry->var.VendorGuid, | |
595 | 0, 0, NULL); | |
e14ab23d | 596 | |
a9499fa7 | 597 | return efi_status_to_err(status); |
e14ab23d MF |
598 | } |
599 | EXPORT_SYMBOL_GPL(__efivar_entry_delete); | |
600 | ||
601 | /** | |
602 | * efivar_entry_delete - delete variable and remove entry from list | |
603 | * @entry: entry containing variable to delete | |
604 | * | |
605 | * Delete the variable from the firmware and remove @entry from the | |
606 | * variable list. It is the caller's responsibility to free @entry | |
607 | * once we return. | |
608 | * | |
609 | * Returns 0 on success, or a converted EFI status code if | |
610 | * set_variable() fails. | |
611 | */ | |
612 | int efivar_entry_delete(struct efivar_entry *entry) | |
613 | { | |
614 | const struct efivar_operations *ops = __efivars->ops; | |
615 | efi_status_t status; | |
616 | ||
617 | spin_lock_irq(&__efivars->lock); | |
618 | status = ops->set_variable(entry->var.VariableName, | |
619 | &entry->var.VendorGuid, | |
620 | 0, 0, NULL); | |
621 | if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) { | |
622 | spin_unlock_irq(&__efivars->lock); | |
623 | return efi_status_to_err(status); | |
624 | } | |
625 | ||
626 | efivar_entry_list_del_unlock(entry); | |
627 | return 0; | |
628 | } | |
629 | EXPORT_SYMBOL_GPL(efivar_entry_delete); | |
630 | ||
631 | /** | |
632 | * efivar_entry_set - call set_variable() | |
633 | * @entry: entry containing the EFI variable to write | |
634 | * @attributes: variable attributes | |
635 | * @size: size of @data buffer | |
636 | * @data: buffer containing variable data | |
637 | * @head: head of variable list | |
638 | * | |
639 | * Calls set_variable() for an EFI variable. If creating a new EFI | |
640 | * variable, this function is usually followed by efivar_entry_add(). | |
641 | * | |
642 | * Before writing the variable, the remaining EFI variable storage | |
643 | * space is checked to ensure there is enough room available. | |
644 | * | |
645 | * If @head is not NULL a lookup is performed to determine whether | |
646 | * the entry is already on the list. | |
647 | * | |
648 | * Returns 0 on success, -EEXIST if a lookup is performed and the entry | |
649 | * already exists on the list, or a converted EFI status code if | |
650 | * set_variable() fails. | |
651 | */ | |
652 | int efivar_entry_set(struct efivar_entry *entry, u32 attributes, | |
653 | unsigned long size, void *data, struct list_head *head) | |
654 | { | |
655 | const struct efivar_operations *ops = __efivars->ops; | |
656 | efi_status_t status; | |
657 | efi_char16_t *name = entry->var.VariableName; | |
658 | efi_guid_t vendor = entry->var.VendorGuid; | |
659 | ||
660 | spin_lock_irq(&__efivars->lock); | |
661 | ||
662 | if (head && efivar_entry_find(name, vendor, head, false)) { | |
663 | spin_unlock_irq(&__efivars->lock); | |
664 | return -EEXIST; | |
665 | } | |
666 | ||
a614e192 | 667 | status = check_var_size(attributes, size + ucs2_strsize(name, 1024)); |
e14ab23d MF |
668 | if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED) |
669 | status = ops->set_variable(name, &vendor, | |
670 | attributes, size, data); | |
671 | ||
672 | spin_unlock_irq(&__efivars->lock); | |
673 | ||
674 | return efi_status_to_err(status); | |
a9499fa7 | 675 | |
e14ab23d MF |
676 | } |
677 | EXPORT_SYMBOL_GPL(efivar_entry_set); | |
678 | ||
679 | /** | |
680 | * efivar_entry_set_safe - call set_variable() if enough space in firmware | |
681 | * @name: buffer containing the variable name | |
682 | * @vendor: variable vendor guid | |
683 | * @attributes: variable attributes | |
684 | * @block: can we block in this context? | |
685 | * @size: size of @data buffer | |
686 | * @data: buffer containing variable data | |
687 | * | |
688 | * Ensures there is enough free storage in the firmware for this variable, and | |
689 | * if so, calls set_variable(). If creating a new EFI variable, this function | |
690 | * is usually followed by efivar_entry_add(). | |
691 | * | |
692 | * Returns 0 on success, -ENOSPC if the firmware does not have enough | |
693 | * space for set_variable() to succeed, or a converted EFI status code | |
694 | * if set_variable() fails. | |
695 | */ | |
696 | int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes, | |
697 | bool block, unsigned long size, void *data) | |
698 | { | |
699 | const struct efivar_operations *ops = __efivars->ops; | |
700 | unsigned long flags; | |
701 | efi_status_t status; | |
702 | ||
a614e192 | 703 | if (!ops->query_variable_store) |
e14ab23d MF |
704 | return -ENOSYS; |
705 | ||
85c90716 DC |
706 | if (!block) { |
707 | if (!spin_trylock_irqsave(&__efivars->lock, flags)) | |
708 | return -EBUSY; | |
709 | } else { | |
e14ab23d | 710 | spin_lock_irqsave(&__efivars->lock, flags); |
85c90716 | 711 | } |
e14ab23d | 712 | |
a614e192 | 713 | status = check_var_size(attributes, size + ucs2_strsize(name, 1024)); |
e14ab23d MF |
714 | if (status != EFI_SUCCESS) { |
715 | spin_unlock_irqrestore(&__efivars->lock, flags); | |
716 | return -ENOSPC; | |
717 | } | |
718 | ||
719 | status = ops->set_variable(name, &vendor, attributes, size, data); | |
720 | ||
721 | spin_unlock_irqrestore(&__efivars->lock, flags); | |
722 | ||
723 | return efi_status_to_err(status); | |
724 | } | |
725 | EXPORT_SYMBOL_GPL(efivar_entry_set_safe); | |
726 | ||
727 | /** | |
728 | * efivar_entry_find - search for an entry | |
729 | * @name: the EFI variable name | |
730 | * @guid: the EFI variable vendor's guid | |
731 | * @head: head of the variable list | |
732 | * @remove: should we remove the entry from the list? | |
733 | * | |
734 | * Search for an entry on the variable list that has the EFI variable | |
735 | * name @name and vendor guid @guid. If an entry is found on the list | |
736 | * and @remove is true, the entry is removed from the list. | |
737 | * | |
738 | * The caller MUST call efivar_entry_iter_begin() and | |
739 | * efivar_entry_iter_end() before and after the invocation of this | |
740 | * function, respectively. | |
741 | * | |
742 | * Returns the entry if found on the list, %NULL otherwise. | |
743 | */ | |
744 | struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid, | |
745 | struct list_head *head, bool remove) | |
746 | { | |
747 | struct efivar_entry *entry, *n; | |
748 | int strsize1, strsize2; | |
749 | bool found = false; | |
750 | ||
79943632 | 751 | lockdep_assert_held(&__efivars->lock); |
e14ab23d MF |
752 | |
753 | list_for_each_entry_safe(entry, n, head, list) { | |
a614e192 MF |
754 | strsize1 = ucs2_strsize(name, 1024); |
755 | strsize2 = ucs2_strsize(entry->var.VariableName, 1024); | |
e14ab23d MF |
756 | if (strsize1 == strsize2 && |
757 | !memcmp(name, &(entry->var.VariableName), strsize1) && | |
758 | !efi_guidcmp(guid, entry->var.VendorGuid)) { | |
759 | found = true; | |
760 | break; | |
761 | } | |
762 | } | |
763 | ||
764 | if (!found) | |
765 | return NULL; | |
766 | ||
767 | if (remove) | |
768 | list_del(&entry->list); | |
769 | ||
770 | return entry; | |
771 | } | |
772 | EXPORT_SYMBOL_GPL(efivar_entry_find); | |
773 | ||
774 | /** | |
8a415b8c | 775 | * efivar_entry_size - obtain the size of a variable |
e14ab23d MF |
776 | * @entry: entry for this variable |
777 | * @size: location to store the variable's size | |
e14ab23d | 778 | */ |
8a415b8c | 779 | int efivar_entry_size(struct efivar_entry *entry, unsigned long *size) |
e14ab23d MF |
780 | { |
781 | const struct efivar_operations *ops = __efivars->ops; | |
782 | efi_status_t status; | |
783 | ||
e14ab23d | 784 | *size = 0; |
8a415b8c MF |
785 | |
786 | spin_lock_irq(&__efivars->lock); | |
e14ab23d MF |
787 | status = ops->get_variable(entry->var.VariableName, |
788 | &entry->var.VendorGuid, NULL, size, NULL); | |
8a415b8c MF |
789 | spin_unlock_irq(&__efivars->lock); |
790 | ||
e14ab23d MF |
791 | if (status != EFI_BUFFER_TOO_SMALL) |
792 | return efi_status_to_err(status); | |
793 | ||
794 | return 0; | |
795 | } | |
8a415b8c | 796 | EXPORT_SYMBOL_GPL(efivar_entry_size); |
e14ab23d MF |
797 | |
798 | /** | |
8a415b8c MF |
799 | * __efivar_entry_get - call get_variable() |
800 | * @entry: read data for this variable | |
801 | * @attributes: variable attributes | |
802 | * @size: size of @data buffer | |
803 | * @data: buffer to store variable data | |
804 | * | |
805 | * The caller MUST call efivar_entry_iter_begin() and | |
806 | * efivar_entry_iter_end() before and after the invocation of this | |
807 | * function, respectively. | |
e14ab23d | 808 | */ |
8a415b8c MF |
809 | int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes, |
810 | unsigned long *size, void *data) | |
e14ab23d MF |
811 | { |
812 | const struct efivar_operations *ops = __efivars->ops; | |
813 | efi_status_t status; | |
814 | ||
79943632 | 815 | lockdep_assert_held(&__efivars->lock); |
e14ab23d | 816 | |
e14ab23d | 817 | status = ops->get_variable(entry->var.VariableName, |
8a415b8c MF |
818 | &entry->var.VendorGuid, |
819 | attributes, size, data); | |
e14ab23d | 820 | |
8a415b8c | 821 | return efi_status_to_err(status); |
e14ab23d | 822 | } |
8a415b8c | 823 | EXPORT_SYMBOL_GPL(__efivar_entry_get); |
e14ab23d MF |
824 | |
825 | /** | |
826 | * efivar_entry_get - call get_variable() | |
827 | * @entry: read data for this variable | |
828 | * @attributes: variable attributes | |
829 | * @size: size of @data buffer | |
830 | * @data: buffer to store variable data | |
831 | */ | |
832 | int efivar_entry_get(struct efivar_entry *entry, u32 *attributes, | |
833 | unsigned long *size, void *data) | |
834 | { | |
835 | const struct efivar_operations *ops = __efivars->ops; | |
836 | efi_status_t status; | |
837 | ||
838 | spin_lock_irq(&__efivars->lock); | |
839 | status = ops->get_variable(entry->var.VariableName, | |
840 | &entry->var.VendorGuid, | |
841 | attributes, size, data); | |
842 | spin_unlock_irq(&__efivars->lock); | |
843 | ||
844 | return efi_status_to_err(status); | |
845 | } | |
846 | EXPORT_SYMBOL_GPL(efivar_entry_get); | |
847 | ||
848 | /** | |
849 | * efivar_entry_set_get_size - call set_variable() and get new size (atomic) | |
850 | * @entry: entry containing variable to set and get | |
851 | * @attributes: attributes of variable to be written | |
852 | * @size: size of data buffer | |
853 | * @data: buffer containing data to write | |
854 | * @set: did the set_variable() call succeed? | |
855 | * | |
856 | * This is a pretty special (complex) function. See efivarfs_file_write(). | |
857 | * | |
858 | * Atomically call set_variable() for @entry and if the call is | |
859 | * successful, return the new size of the variable from get_variable() | |
860 | * in @size. The success of set_variable() is indicated by @set. | |
861 | * | |
862 | * Returns 0 on success, -EINVAL if the variable data is invalid, | |
863 | * -ENOSPC if the firmware does not have enough available space, or a | |
864 | * converted EFI status code if either of set_variable() or | |
865 | * get_variable() fail. | |
866 | * | |
867 | * If the EFI variable does not exist when calling set_variable() | |
868 | * (EFI_NOT_FOUND), @entry is removed from the variable list. | |
869 | */ | |
870 | int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes, | |
871 | unsigned long *size, void *data, bool *set) | |
872 | { | |
873 | const struct efivar_operations *ops = __efivars->ops; | |
874 | efi_char16_t *name = entry->var.VariableName; | |
875 | efi_guid_t *vendor = &entry->var.VendorGuid; | |
876 | efi_status_t status; | |
877 | int err; | |
878 | ||
879 | *set = false; | |
880 | ||
d591b6da | 881 | if (efivar_validate(*vendor, name, data, *size) == false) |
e14ab23d MF |
882 | return -EINVAL; |
883 | ||
884 | /* | |
885 | * The lock here protects the get_variable call, the conditional | |
886 | * set_variable call, and removal of the variable from the efivars | |
887 | * list (in the case of an authenticated delete). | |
888 | */ | |
889 | spin_lock_irq(&__efivars->lock); | |
890 | ||
891 | /* | |
892 | * Ensure that the available space hasn't shrunk below the safe level | |
893 | */ | |
a614e192 | 894 | status = check_var_size(attributes, *size + ucs2_strsize(name, 1024)); |
e14ab23d MF |
895 | if (status != EFI_SUCCESS) { |
896 | if (status != EFI_UNSUPPORTED) { | |
897 | err = efi_status_to_err(status); | |
898 | goto out; | |
899 | } | |
900 | ||
901 | if (*size > 65536) { | |
902 | err = -ENOSPC; | |
903 | goto out; | |
904 | } | |
905 | } | |
906 | ||
907 | status = ops->set_variable(name, vendor, attributes, *size, data); | |
908 | if (status != EFI_SUCCESS) { | |
909 | err = efi_status_to_err(status); | |
910 | goto out; | |
911 | } | |
912 | ||
913 | *set = true; | |
914 | ||
915 | /* | |
916 | * Writing to the variable may have caused a change in size (which | |
917 | * could either be an append or an overwrite), or the variable to be | |
918 | * deleted. Perform a GetVariable() so we can tell what actually | |
919 | * happened. | |
920 | */ | |
921 | *size = 0; | |
922 | status = ops->get_variable(entry->var.VariableName, | |
923 | &entry->var.VendorGuid, | |
924 | NULL, size, NULL); | |
925 | ||
926 | if (status == EFI_NOT_FOUND) | |
927 | efivar_entry_list_del_unlock(entry); | |
928 | else | |
929 | spin_unlock_irq(&__efivars->lock); | |
930 | ||
931 | if (status && status != EFI_BUFFER_TOO_SMALL) | |
932 | return efi_status_to_err(status); | |
933 | ||
934 | return 0; | |
935 | ||
936 | out: | |
937 | spin_unlock_irq(&__efivars->lock); | |
938 | return err; | |
939 | ||
940 | } | |
941 | EXPORT_SYMBOL_GPL(efivar_entry_set_get_size); | |
942 | ||
943 | /** | |
944 | * efivar_entry_iter_begin - begin iterating the variable list | |
945 | * | |
946 | * Lock the variable list to prevent entry insertion and removal until | |
947 | * efivar_entry_iter_end() is called. This function is usually used in | |
948 | * conjunction with __efivar_entry_iter() or efivar_entry_iter(). | |
949 | */ | |
950 | void efivar_entry_iter_begin(void) | |
951 | { | |
952 | spin_lock_irq(&__efivars->lock); | |
953 | } | |
954 | EXPORT_SYMBOL_GPL(efivar_entry_iter_begin); | |
955 | ||
956 | /** | |
957 | * efivar_entry_iter_end - finish iterating the variable list | |
958 | * | |
959 | * Unlock the variable list and allow modifications to the list again. | |
960 | */ | |
961 | void efivar_entry_iter_end(void) | |
962 | { | |
963 | spin_unlock_irq(&__efivars->lock); | |
964 | } | |
965 | EXPORT_SYMBOL_GPL(efivar_entry_iter_end); | |
966 | ||
967 | /** | |
968 | * __efivar_entry_iter - iterate over variable list | |
969 | * @func: callback function | |
970 | * @head: head of the variable list | |
971 | * @data: function-specific data to pass to callback | |
972 | * @prev: entry to begin iterating from | |
973 | * | |
974 | * Iterate over the list of EFI variables and call @func with every | |
975 | * entry on the list. It is safe for @func to remove entries in the | |
976 | * list via efivar_entry_delete(). | |
977 | * | |
978 | * You MUST call efivar_enter_iter_begin() before this function, and | |
979 | * efivar_entry_iter_end() afterwards. | |
980 | * | |
981 | * It is possible to begin iteration from an arbitrary entry within | |
982 | * the list by passing @prev. @prev is updated on return to point to | |
983 | * the last entry passed to @func. To begin iterating from the | |
984 | * beginning of the list @prev must be %NULL. | |
985 | * | |
986 | * The restrictions for @func are the same as documented for | |
987 | * efivar_entry_iter(). | |
988 | */ | |
989 | int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *), | |
990 | struct list_head *head, void *data, | |
991 | struct efivar_entry **prev) | |
992 | { | |
993 | struct efivar_entry *entry, *n; | |
994 | int err = 0; | |
995 | ||
996 | if (!prev || !*prev) { | |
997 | list_for_each_entry_safe(entry, n, head, list) { | |
998 | err = func(entry, data); | |
999 | if (err) | |
1000 | break; | |
1001 | } | |
1002 | ||
1003 | if (prev) | |
1004 | *prev = entry; | |
1005 | ||
1006 | return err; | |
1007 | } | |
1008 | ||
1009 | ||
1010 | list_for_each_entry_safe_continue((*prev), n, head, list) { | |
1011 | err = func(*prev, data); | |
1012 | if (err) | |
1013 | break; | |
1014 | } | |
1015 | ||
1016 | return err; | |
1017 | } | |
1018 | EXPORT_SYMBOL_GPL(__efivar_entry_iter); | |
1019 | ||
1020 | /** | |
1021 | * efivar_entry_iter - iterate over variable list | |
1022 | * @func: callback function | |
1023 | * @head: head of variable list | |
1024 | * @data: function-specific data to pass to callback | |
1025 | * | |
1026 | * Iterate over the list of EFI variables and call @func with every | |
1027 | * entry on the list. It is safe for @func to remove entries in the | |
1028 | * list via efivar_entry_delete() while iterating. | |
1029 | * | |
1030 | * Some notes for the callback function: | |
1031 | * - a non-zero return value indicates an error and terminates the loop | |
1032 | * - @func is called from atomic context | |
1033 | */ | |
1034 | int efivar_entry_iter(int (*func)(struct efivar_entry *, void *), | |
1035 | struct list_head *head, void *data) | |
1036 | { | |
1037 | int err = 0; | |
1038 | ||
1039 | efivar_entry_iter_begin(); | |
1040 | err = __efivar_entry_iter(func, head, data, NULL); | |
1041 | efivar_entry_iter_end(); | |
1042 | ||
1043 | return err; | |
1044 | } | |
1045 | EXPORT_SYMBOL_GPL(efivar_entry_iter); | |
1046 | ||
1047 | /** | |
1048 | * efivars_kobject - get the kobject for the registered efivars | |
1049 | * | |
1050 | * If efivars_register() has not been called we return NULL, | |
1051 | * otherwise return the kobject used at registration time. | |
1052 | */ | |
1053 | struct kobject *efivars_kobject(void) | |
1054 | { | |
1055 | if (!__efivars) | |
1056 | return NULL; | |
1057 | ||
1058 | return __efivars->kobject; | |
1059 | } | |
1060 | EXPORT_SYMBOL_GPL(efivars_kobject); | |
1061 | ||
04851772 MF |
1062 | /** |
1063 | * efivar_run_worker - schedule the efivar worker thread | |
1064 | */ | |
1065 | void efivar_run_worker(void) | |
1066 | { | |
1067 | if (efivar_wq_enabled) | |
1068 | schedule_work(&efivar_work); | |
1069 | } | |
1070 | EXPORT_SYMBOL_GPL(efivar_run_worker); | |
1071 | ||
e14ab23d MF |
1072 | /** |
1073 | * efivars_register - register an efivars | |
1074 | * @efivars: efivars to register | |
1075 | * @ops: efivars operations | |
1076 | * @kobject: @efivars-specific kobject | |
1077 | * | |
1078 | * Only a single efivars can be registered at any time. | |
1079 | */ | |
1080 | int efivars_register(struct efivars *efivars, | |
1081 | const struct efivar_operations *ops, | |
1082 | struct kobject *kobject) | |
1083 | { | |
1084 | spin_lock_init(&efivars->lock); | |
1085 | efivars->ops = ops; | |
1086 | efivars->kobject = kobject; | |
1087 | ||
1088 | __efivars = efivars; | |
1da177e4 | 1089 | |
e14ab23d MF |
1090 | return 0; |
1091 | } | |
1092 | EXPORT_SYMBOL_GPL(efivars_register); | |
1da177e4 | 1093 | |
e14ab23d MF |
1094 | /** |
1095 | * efivars_unregister - unregister an efivars | |
1096 | * @efivars: efivars to unregister | |
1097 | * | |
1098 | * The caller must have already removed every entry from the list, | |
1099 | * failure to do so is an error. | |
1100 | */ | |
1101 | int efivars_unregister(struct efivars *efivars) | |
1102 | { | |
1103 | int rv; | |
1104 | ||
1105 | if (!__efivars) { | |
1106 | printk(KERN_ERR "efivars not registered\n"); | |
1107 | rv = -EINVAL; | |
1108 | goto out; | |
1109 | } | |
1110 | ||
1111 | if (__efivars != efivars) { | |
1112 | rv = -EINVAL; | |
1113 | goto out; | |
1114 | } | |
1115 | ||
1116 | __efivars = NULL; | |
1117 | ||
1118 | rv = 0; | |
1119 | out: | |
1120 | return rv; | |
76b53f7c | 1121 | } |
e14ab23d | 1122 | EXPORT_SYMBOL_GPL(efivars_unregister); |