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