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