Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * EFI Variables - 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 code takes all variables accessible from EFI runtime and |
| 8 | * exports them via sysfs |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | * |
| 24 | * Changelog: |
| 25 | * |
| 26 | * 17 May 2004 - Matt Domsch <Matt_Domsch@dell.com> |
| 27 | * remove check for efi_enabled in exit |
| 28 | * add MODULE_VERSION |
| 29 | * |
| 30 | * 26 Apr 2004 - Matt Domsch <Matt_Domsch@dell.com> |
| 31 | * minor bug fixes |
| 32 | * |
| 33 | * 21 Apr 2004 - Matt Tolentino <matthew.e.tolentino@intel.com) |
| 34 | * converted driver to export variable information via sysfs |
| 35 | * and moved to drivers/firmware directory |
| 36 | * bumped revision number to v0.07 to reflect conversion & move |
| 37 | * |
| 38 | * 10 Dec 2002 - Matt Domsch <Matt_Domsch@dell.com> |
| 39 | * fix locking per Peter Chubb's findings |
| 40 | * |
| 41 | * 25 Mar 2002 - Matt Domsch <Matt_Domsch@dell.com> |
| 42 | * move uuid_unparse() to include/asm-ia64/efi.h:efi_guid_unparse() |
| 43 | * |
| 44 | * 12 Feb 2002 - Matt Domsch <Matt_Domsch@dell.com> |
| 45 | * use list_for_each_safe when deleting vars. |
| 46 | * remove ifdef CONFIG_SMP around include <linux/smp.h> |
| 47 | * v0.04 release to linux-ia64@linuxia64.org |
| 48 | * |
| 49 | * 20 April 2001 - Matt Domsch <Matt_Domsch@dell.com> |
| 50 | * Moved vars from /proc/efi to /proc/efi/vars, and made |
| 51 | * efi.c own the /proc/efi directory. |
| 52 | * v0.03 release to linux-ia64@linuxia64.org |
| 53 | * |
| 54 | * 26 March 2001 - Matt Domsch <Matt_Domsch@dell.com> |
| 55 | * At the request of Stephane, moved ownership of /proc/efi |
| 56 | * to efi.c, and now efivars lives under /proc/efi/vars. |
| 57 | * |
| 58 | * 12 March 2001 - Matt Domsch <Matt_Domsch@dell.com> |
| 59 | * Feedback received from Stephane Eranian incorporated. |
| 60 | * efivar_write() checks copy_from_user() return value. |
| 61 | * efivar_read/write() returns proper errno. |
| 62 | * v0.02 release to linux-ia64@linuxia64.org |
| 63 | * |
| 64 | * 26 February 2001 - Matt Domsch <Matt_Domsch@dell.com> |
| 65 | * v0.01 release to linux-ia64@linuxia64.org |
| 66 | */ |
| 67 | |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 68 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | #include <linux/types.h> |
| 70 | #include <linux/errno.h> |
| 71 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #include <linux/mm.h> |
| 73 | #include <linux/module.h> |
| 74 | #include <linux/string.h> |
| 75 | #include <linux/smp.h> |
| 76 | #include <linux/efi.h> |
| 77 | #include <linux/sysfs.h> |
| 78 | #include <linux/kobject.h> |
| 79 | #include <linux/device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 80 | #include <linux/slab.h> |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 81 | #include <linux/pstore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Matthew Garrett | 5d9db88 | 2012-10-05 13:54:56 +0800 | [diff] [blame^] | 83 | #include <linux/fs.h> |
| 84 | #include <linux/ramfs.h> |
| 85 | #include <linux/pagemap.h> |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | #include <asm/uaccess.h> |
| 88 | |
| 89 | #define EFIVARS_VERSION "0.08" |
| 90 | #define EFIVARS_DATE "2004-May-17" |
| 91 | |
| 92 | MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>"); |
| 93 | MODULE_DESCRIPTION("sysfs interface to EFI Variables"); |
| 94 | MODULE_LICENSE("GPL"); |
| 95 | MODULE_VERSION(EFIVARS_VERSION); |
| 96 | |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 97 | #define DUMP_NAME_LEN 52 |
Matthew Garrett | 5d9db88 | 2012-10-05 13:54:56 +0800 | [diff] [blame^] | 98 | #define GUID_LEN 37 |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | /* |
| 101 | * The maximum size of VariableName + Data = 1024 |
| 102 | * Therefore, it's reasonable to save that much |
| 103 | * space in each part of the structure, |
| 104 | * and we use a page for reading/writing. |
| 105 | */ |
| 106 | |
| 107 | struct efi_variable { |
| 108 | efi_char16_t VariableName[1024/sizeof(efi_char16_t)]; |
| 109 | efi_guid_t VendorGuid; |
| 110 | unsigned long DataSize; |
| 111 | __u8 Data[1024]; |
| 112 | efi_status_t Status; |
| 113 | __u32 Attributes; |
| 114 | } __attribute__((packed)); |
| 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | struct efivar_entry { |
Mike Waychison | 4142ef1 | 2011-03-11 17:43:11 -0800 | [diff] [blame] | 117 | struct efivars *efivars; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | struct efi_variable var; |
| 119 | struct list_head list; |
| 120 | struct kobject kobj; |
| 121 | }; |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | struct efivar_attribute { |
| 124 | struct attribute attr; |
| 125 | ssize_t (*show) (struct efivar_entry *entry, char *buf); |
| 126 | ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count); |
| 127 | }; |
| 128 | |
Matthew Garrett | 5d9db88 | 2012-10-05 13:54:56 +0800 | [diff] [blame^] | 129 | static struct efivars __efivars; |
| 130 | static struct efivar_operations ops; |
| 131 | |
Mike Waychison | 7644c16 | 2011-07-21 16:58:00 -0400 | [diff] [blame] | 132 | #define PSTORE_EFI_ATTRIBUTES \ |
| 133 | (EFI_VARIABLE_NON_VOLATILE | \ |
| 134 | EFI_VARIABLE_BOOTSERVICE_ACCESS | \ |
| 135 | EFI_VARIABLE_RUNTIME_ACCESS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | #define EFIVAR_ATTR(_name, _mode, _show, _store) \ |
| 138 | struct efivar_attribute efivar_attr_##_name = { \ |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 139 | .attr = {.name = __stringify(_name), .mode = _mode}, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | .show = _show, \ |
| 141 | .store = _store, \ |
| 142 | }; |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr) |
| 145 | #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj) |
| 146 | |
| 147 | /* |
| 148 | * Prototype for sysfs creation function |
| 149 | */ |
| 150 | static int |
Mike Waychison | 4142ef1 | 2011-03-11 17:43:11 -0800 | [diff] [blame] | 151 | efivar_create_sysfs_entry(struct efivars *efivars, |
| 152 | unsigned long variable_name_size, |
| 153 | efi_char16_t *variable_name, |
| 154 | efi_guid_t *vendor_guid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | /* Return the number of unicode characters in data */ |
| 157 | static unsigned long |
Mike Waychison | a294090 | 2011-07-21 16:57:57 -0400 | [diff] [blame] | 158 | utf16_strnlen(efi_char16_t *s, size_t maxlength) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
| 160 | unsigned long length = 0; |
| 161 | |
Mike Waychison | a294090 | 2011-07-21 16:57:57 -0400 | [diff] [blame] | 162 | while (*s++ != 0 && length < maxlength) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | length++; |
| 164 | return length; |
| 165 | } |
| 166 | |
Tony Luck | b728a5c | 2011-08-02 15:08:30 -0700 | [diff] [blame] | 167 | static inline unsigned long |
Mike Waychison | a294090 | 2011-07-21 16:57:57 -0400 | [diff] [blame] | 168 | utf16_strlen(efi_char16_t *s) |
| 169 | { |
| 170 | return utf16_strnlen(s, ~0UL); |
| 171 | } |
| 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | /* |
| 174 | * Return the number of bytes is the length of this string |
| 175 | * Note: this is NOT the same as the number of unicode characters |
| 176 | */ |
| 177 | static inline unsigned long |
Mike Waychison | a294090 | 2011-07-21 16:57:57 -0400 | [diff] [blame] | 178 | utf16_strsize(efi_char16_t *data, unsigned long maxlength) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { |
Mike Waychison | a294090 | 2011-07-21 16:57:57 -0400 | [diff] [blame] | 180 | return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Mike Waychison | 828aa1f | 2011-07-21 16:57:58 -0400 | [diff] [blame] | 183 | static inline int |
| 184 | utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len) |
| 185 | { |
| 186 | while (1) { |
| 187 | if (len == 0) |
| 188 | return 0; |
| 189 | if (*a < *b) |
| 190 | return -1; |
| 191 | if (*a > *b) |
| 192 | return 1; |
| 193 | if (*a == 0) /* implies *b == 0 */ |
| 194 | return 0; |
| 195 | a++; |
| 196 | b++; |
| 197 | len--; |
| 198 | } |
| 199 | } |
| 200 | |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 201 | static bool |
Matthew Garrett | 54b3a4d | 2012-05-03 16:50:46 -0400 | [diff] [blame] | 202 | validate_device_path(struct efi_variable *var, int match, u8 *buffer, |
| 203 | unsigned long len) |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 204 | { |
| 205 | struct efi_generic_dev_path *node; |
| 206 | int offset = 0; |
| 207 | |
| 208 | node = (struct efi_generic_dev_path *)buffer; |
| 209 | |
Matthew Garrett | 54b3a4d | 2012-05-03 16:50:46 -0400 | [diff] [blame] | 210 | if (len < sizeof(*node)) |
| 211 | return false; |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 212 | |
Matthew Garrett | 54b3a4d | 2012-05-03 16:50:46 -0400 | [diff] [blame] | 213 | while (offset <= len - sizeof(*node) && |
| 214 | node->length >= sizeof(*node) && |
| 215 | node->length <= len - offset) { |
| 216 | offset += node->length; |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 217 | |
| 218 | if ((node->type == EFI_DEV_END_PATH || |
| 219 | node->type == EFI_DEV_END_PATH2) && |
| 220 | node->sub_type == EFI_DEV_END_ENTIRE) |
| 221 | return true; |
| 222 | |
| 223 | node = (struct efi_generic_dev_path *)(buffer + offset); |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * If we're here then either node->length pointed past the end |
| 228 | * of the buffer or we reached the end of the buffer without |
| 229 | * finding a device path end node. |
| 230 | */ |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | static bool |
Matthew Garrett | 54b3a4d | 2012-05-03 16:50:46 -0400 | [diff] [blame] | 235 | validate_boot_order(struct efi_variable *var, int match, u8 *buffer, |
| 236 | unsigned long len) |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 237 | { |
| 238 | /* An array of 16-bit integers */ |
| 239 | if ((len % 2) != 0) |
| 240 | return false; |
| 241 | |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | static bool |
Matthew Garrett | 54b3a4d | 2012-05-03 16:50:46 -0400 | [diff] [blame] | 246 | validate_load_option(struct efi_variable *var, int match, u8 *buffer, |
| 247 | unsigned long len) |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 248 | { |
| 249 | u16 filepathlength; |
Matthew Garrett | 54b3a4d | 2012-05-03 16:50:46 -0400 | [diff] [blame] | 250 | int i, desclength = 0, namelen; |
| 251 | |
| 252 | namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName)); |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 253 | |
| 254 | /* Either "Boot" or "Driver" followed by four digits of hex */ |
| 255 | for (i = match; i < match+4; i++) { |
Matthew Garrett | 54b3a4d | 2012-05-03 16:50:46 -0400 | [diff] [blame] | 256 | if (var->VariableName[i] > 127 || |
| 257 | hex_to_bin(var->VariableName[i] & 0xff) < 0) |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 258 | return true; |
| 259 | } |
| 260 | |
Matthew Garrett | 54b3a4d | 2012-05-03 16:50:46 -0400 | [diff] [blame] | 261 | /* Reject it if there's 4 digits of hex and then further content */ |
| 262 | if (namelen > match + 4) |
| 263 | return false; |
| 264 | |
| 265 | /* A valid entry must be at least 8 bytes */ |
| 266 | if (len < 8) |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 267 | return false; |
| 268 | |
| 269 | filepathlength = buffer[4] | buffer[5] << 8; |
| 270 | |
| 271 | /* |
| 272 | * There's no stored length for the description, so it has to be |
| 273 | * found by hand |
| 274 | */ |
Matthew Garrett | 54b3a4d | 2012-05-03 16:50:46 -0400 | [diff] [blame] | 275 | desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2; |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 276 | |
| 277 | /* Each boot entry must have a descriptor */ |
| 278 | if (!desclength) |
| 279 | return false; |
| 280 | |
| 281 | /* |
| 282 | * If the sum of the length of the description, the claimed filepath |
| 283 | * length and the original header are greater than the length of the |
| 284 | * variable, it's malformed |
| 285 | */ |
| 286 | if ((desclength + filepathlength + 6) > len) |
| 287 | return false; |
| 288 | |
| 289 | /* |
| 290 | * And, finally, check the filepath |
| 291 | */ |
| 292 | return validate_device_path(var, match, buffer + desclength + 6, |
| 293 | filepathlength); |
| 294 | } |
| 295 | |
| 296 | static bool |
Matthew Garrett | 54b3a4d | 2012-05-03 16:50:46 -0400 | [diff] [blame] | 297 | validate_uint16(struct efi_variable *var, int match, u8 *buffer, |
| 298 | unsigned long len) |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 299 | { |
| 300 | /* A single 16-bit integer */ |
| 301 | if (len != 2) |
| 302 | return false; |
| 303 | |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | static bool |
Matthew Garrett | 54b3a4d | 2012-05-03 16:50:46 -0400 | [diff] [blame] | 308 | validate_ascii_string(struct efi_variable *var, int match, u8 *buffer, |
| 309 | unsigned long len) |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 310 | { |
| 311 | int i; |
| 312 | |
| 313 | for (i = 0; i < len; i++) { |
| 314 | if (buffer[i] > 127) |
| 315 | return false; |
| 316 | |
| 317 | if (buffer[i] == 0) |
| 318 | return true; |
| 319 | } |
| 320 | |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | struct variable_validate { |
| 325 | char *name; |
| 326 | bool (*validate)(struct efi_variable *var, int match, u8 *data, |
Matthew Garrett | 54b3a4d | 2012-05-03 16:50:46 -0400 | [diff] [blame] | 327 | unsigned long len); |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 328 | }; |
| 329 | |
| 330 | static const struct variable_validate variable_validate[] = { |
| 331 | { "BootNext", validate_uint16 }, |
| 332 | { "BootOrder", validate_boot_order }, |
| 333 | { "DriverOrder", validate_boot_order }, |
| 334 | { "Boot*", validate_load_option }, |
| 335 | { "Driver*", validate_load_option }, |
| 336 | { "ConIn", validate_device_path }, |
| 337 | { "ConInDev", validate_device_path }, |
| 338 | { "ConOut", validate_device_path }, |
| 339 | { "ConOutDev", validate_device_path }, |
| 340 | { "ErrOut", validate_device_path }, |
| 341 | { "ErrOutDev", validate_device_path }, |
| 342 | { "Timeout", validate_uint16 }, |
| 343 | { "Lang", validate_ascii_string }, |
| 344 | { "PlatformLang", validate_ascii_string }, |
| 345 | { "", NULL }, |
| 346 | }; |
| 347 | |
| 348 | static bool |
Matthew Garrett | 54b3a4d | 2012-05-03 16:50:46 -0400 | [diff] [blame] | 349 | validate_var(struct efi_variable *var, u8 *data, unsigned long len) |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 350 | { |
| 351 | int i; |
| 352 | u16 *unicode_name = var->VariableName; |
| 353 | |
| 354 | for (i = 0; variable_validate[i].validate != NULL; i++) { |
| 355 | const char *name = variable_validate[i].name; |
| 356 | int match; |
| 357 | |
| 358 | for (match = 0; ; match++) { |
| 359 | char c = name[match]; |
| 360 | u16 u = unicode_name[match]; |
| 361 | |
| 362 | /* All special variables are plain ascii */ |
| 363 | if (u > 127) |
| 364 | return true; |
| 365 | |
| 366 | /* Wildcard in the matching name means we've matched */ |
| 367 | if (c == '*') |
| 368 | return variable_validate[i].validate(var, |
| 369 | match, data, len); |
| 370 | |
| 371 | /* Case sensitive match */ |
| 372 | if (c != u) |
| 373 | break; |
| 374 | |
| 375 | /* Reached the end of the string while matching */ |
| 376 | if (!c) |
| 377 | return variable_validate[i].validate(var, |
| 378 | match, data, len); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | return true; |
| 383 | } |
| 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | static efi_status_t |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 386 | get_var_data_locked(struct efivars *efivars, struct efi_variable *var) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
| 388 | efi_status_t status; |
| 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | var->DataSize = 1024; |
Mike Waychison | 3295814 | 2011-03-11 17:43:21 -0800 | [diff] [blame] | 391 | status = efivars->ops->get_variable(var->VariableName, |
| 392 | &var->VendorGuid, |
| 393 | &var->Attributes, |
| 394 | &var->DataSize, |
| 395 | var->Data); |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 396 | return status; |
| 397 | } |
| 398 | |
| 399 | static efi_status_t |
| 400 | get_var_data(struct efivars *efivars, struct efi_variable *var) |
| 401 | { |
| 402 | efi_status_t status; |
| 403 | |
| 404 | spin_lock(&efivars->lock); |
| 405 | status = get_var_data_locked(efivars, var); |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 406 | spin_unlock(&efivars->lock); |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | if (status != EFI_SUCCESS) { |
| 409 | printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n", |
| 410 | status); |
| 411 | } |
| 412 | return status; |
| 413 | } |
| 414 | |
| 415 | static ssize_t |
| 416 | efivar_guid_read(struct efivar_entry *entry, char *buf) |
| 417 | { |
| 418 | struct efi_variable *var = &entry->var; |
| 419 | char *str = buf; |
| 420 | |
| 421 | if (!entry || !buf) |
| 422 | return 0; |
| 423 | |
| 424 | efi_guid_unparse(&var->VendorGuid, str); |
| 425 | str += strlen(str); |
| 426 | str += sprintf(str, "\n"); |
| 427 | |
| 428 | return str - buf; |
| 429 | } |
| 430 | |
| 431 | static ssize_t |
| 432 | efivar_attr_read(struct efivar_entry *entry, char *buf) |
| 433 | { |
| 434 | struct efi_variable *var = &entry->var; |
| 435 | char *str = buf; |
| 436 | efi_status_t status; |
| 437 | |
| 438 | if (!entry || !buf) |
| 439 | return -EINVAL; |
| 440 | |
Mike Waychison | 4142ef1 | 2011-03-11 17:43:11 -0800 | [diff] [blame] | 441 | status = get_var_data(entry->efivars, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | if (status != EFI_SUCCESS) |
| 443 | return -EIO; |
| 444 | |
Khalid Aziz | 7083909 | 2012-09-10 12:52:42 -0600 | [diff] [blame] | 445 | if (var->Attributes & EFI_VARIABLE_NON_VOLATILE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n"); |
Khalid Aziz | 7083909 | 2012-09-10 12:52:42 -0600 | [diff] [blame] | 447 | if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n"); |
Khalid Aziz | 7083909 | 2012-09-10 12:52:42 -0600 | [diff] [blame] | 449 | if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n"); |
Khalid Aziz | 7083909 | 2012-09-10 12:52:42 -0600 | [diff] [blame] | 451 | if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) |
| 452 | str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n"); |
| 453 | if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) |
| 454 | str += sprintf(str, |
| 455 | "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n"); |
| 456 | if (var->Attributes & |
| 457 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) |
| 458 | str += sprintf(str, |
| 459 | "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n"); |
| 460 | if (var->Attributes & EFI_VARIABLE_APPEND_WRITE) |
| 461 | str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | return str - buf; |
| 463 | } |
| 464 | |
| 465 | static ssize_t |
| 466 | efivar_size_read(struct efivar_entry *entry, char *buf) |
| 467 | { |
| 468 | struct efi_variable *var = &entry->var; |
| 469 | char *str = buf; |
| 470 | efi_status_t status; |
| 471 | |
| 472 | if (!entry || !buf) |
| 473 | return -EINVAL; |
| 474 | |
Mike Waychison | 4142ef1 | 2011-03-11 17:43:11 -0800 | [diff] [blame] | 475 | status = get_var_data(entry->efivars, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | if (status != EFI_SUCCESS) |
| 477 | return -EIO; |
| 478 | |
| 479 | str += sprintf(str, "0x%lx\n", var->DataSize); |
| 480 | return str - buf; |
| 481 | } |
| 482 | |
| 483 | static ssize_t |
| 484 | efivar_data_read(struct efivar_entry *entry, char *buf) |
| 485 | { |
| 486 | struct efi_variable *var = &entry->var; |
| 487 | efi_status_t status; |
| 488 | |
| 489 | if (!entry || !buf) |
| 490 | return -EINVAL; |
| 491 | |
Mike Waychison | 4142ef1 | 2011-03-11 17:43:11 -0800 | [diff] [blame] | 492 | status = get_var_data(entry->efivars, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | if (status != EFI_SUCCESS) |
| 494 | return -EIO; |
| 495 | |
| 496 | memcpy(buf, var->Data, var->DataSize); |
| 497 | return var->DataSize; |
| 498 | } |
| 499 | /* |
| 500 | * We allow each variable to be edited via rewriting the |
| 501 | * entire efi variable structure. |
| 502 | */ |
| 503 | static ssize_t |
| 504 | efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count) |
| 505 | { |
| 506 | struct efi_variable *new_var, *var = &entry->var; |
Mike Waychison | 4142ef1 | 2011-03-11 17:43:11 -0800 | [diff] [blame] | 507 | struct efivars *efivars = entry->efivars; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | efi_status_t status = EFI_NOT_FOUND; |
| 509 | |
| 510 | if (count != sizeof(struct efi_variable)) |
| 511 | return -EINVAL; |
| 512 | |
| 513 | new_var = (struct efi_variable *)buf; |
| 514 | /* |
| 515 | * If only updating the variable data, then the name |
| 516 | * and guid should remain the same |
| 517 | */ |
| 518 | if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) || |
| 519 | efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) { |
| 520 | printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n"); |
| 521 | return -EINVAL; |
| 522 | } |
| 523 | |
| 524 | if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){ |
| 525 | printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n"); |
| 526 | return -EINVAL; |
| 527 | } |
| 528 | |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 529 | if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 || |
| 530 | validate_var(new_var, new_var->Data, new_var->DataSize) == false) { |
| 531 | printk(KERN_ERR "efivars: Malformed variable content\n"); |
| 532 | return -EINVAL; |
| 533 | } |
| 534 | |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 535 | spin_lock(&efivars->lock); |
Mike Waychison | 3295814 | 2011-03-11 17:43:21 -0800 | [diff] [blame] | 536 | status = efivars->ops->set_variable(new_var->VariableName, |
| 537 | &new_var->VendorGuid, |
| 538 | new_var->Attributes, |
| 539 | new_var->DataSize, |
| 540 | new_var->Data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 542 | spin_unlock(&efivars->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
| 544 | if (status != EFI_SUCCESS) { |
| 545 | printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n", |
| 546 | status); |
| 547 | return -EIO; |
| 548 | } |
| 549 | |
| 550 | memcpy(&entry->var, new_var, count); |
| 551 | return count; |
| 552 | } |
| 553 | |
| 554 | static ssize_t |
| 555 | efivar_show_raw(struct efivar_entry *entry, char *buf) |
| 556 | { |
| 557 | struct efi_variable *var = &entry->var; |
| 558 | efi_status_t status; |
| 559 | |
| 560 | if (!entry || !buf) |
| 561 | return 0; |
| 562 | |
Mike Waychison | 4142ef1 | 2011-03-11 17:43:11 -0800 | [diff] [blame] | 563 | status = get_var_data(entry->efivars, var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | if (status != EFI_SUCCESS) |
| 565 | return -EIO; |
| 566 | |
| 567 | memcpy(buf, var, sizeof(*var)); |
| 568 | return sizeof(*var); |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * Generic read/write functions that call the specific functions of |
Justin P. Mattock | 70f23fd | 2011-05-10 10:16:21 +0200 | [diff] [blame] | 573 | * the attributes... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | */ |
| 575 | static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr, |
| 576 | char *buf) |
| 577 | { |
| 578 | struct efivar_entry *var = to_efivar_entry(kobj); |
| 579 | struct efivar_attribute *efivar_attr = to_efivar_attr(attr); |
Dmitry Torokhov | 70f2817 | 2005-04-29 01:27:34 -0500 | [diff] [blame] | 580 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | if (!capable(CAP_SYS_ADMIN)) |
| 583 | return -EACCES; |
| 584 | |
| 585 | if (efivar_attr->show) { |
| 586 | ret = efivar_attr->show(var, buf); |
| 587 | } |
| 588 | return ret; |
| 589 | } |
| 590 | |
| 591 | static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr, |
| 592 | const char *buf, size_t count) |
| 593 | { |
| 594 | struct efivar_entry *var = to_efivar_entry(kobj); |
| 595 | struct efivar_attribute *efivar_attr = to_efivar_attr(attr); |
Dmitry Torokhov | 70f2817 | 2005-04-29 01:27:34 -0500 | [diff] [blame] | 596 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
| 598 | if (!capable(CAP_SYS_ADMIN)) |
| 599 | return -EACCES; |
| 600 | |
| 601 | if (efivar_attr->store) |
| 602 | ret = efivar_attr->store(var, buf, count); |
| 603 | |
| 604 | return ret; |
| 605 | } |
| 606 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 607 | static const struct sysfs_ops efivar_attr_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | .show = efivar_attr_show, |
| 609 | .store = efivar_attr_store, |
| 610 | }; |
| 611 | |
| 612 | static void efivar_release(struct kobject *kobj) |
| 613 | { |
| 614 | struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | kfree(var); |
| 616 | } |
| 617 | |
| 618 | static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL); |
| 619 | static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL); |
| 620 | static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL); |
| 621 | static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL); |
| 622 | static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw); |
| 623 | |
| 624 | static struct attribute *def_attrs[] = { |
| 625 | &efivar_attr_guid.attr, |
| 626 | &efivar_attr_size.attr, |
| 627 | &efivar_attr_attributes.attr, |
| 628 | &efivar_attr_data.attr, |
| 629 | &efivar_attr_raw_var.attr, |
| 630 | NULL, |
| 631 | }; |
| 632 | |
Greg Kroah-Hartman | e89a411 | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 633 | static struct kobj_type efivar_ktype = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | .release = efivar_release, |
| 635 | .sysfs_ops = &efivar_attr_ops, |
| 636 | .default_attrs = def_attrs, |
| 637 | }; |
| 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | static inline void |
| 640 | efivar_unregister(struct efivar_entry *var) |
| 641 | { |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 642 | kobject_put(&var->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Matthew Garrett | 5d9db88 | 2012-10-05 13:54:56 +0800 | [diff] [blame^] | 645 | static int efivarfs_file_open(struct inode *inode, struct file *file) |
| 646 | { |
| 647 | file->private_data = inode->i_private; |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | static ssize_t efivarfs_file_write(struct file *file, |
| 652 | const char __user *userbuf, size_t count, loff_t *ppos) |
| 653 | { |
| 654 | struct efivar_entry *var = file->private_data; |
| 655 | struct efivars *efivars; |
| 656 | efi_status_t status; |
| 657 | void *data; |
| 658 | u32 attributes; |
| 659 | struct inode *inode = file->f_mapping->host; |
| 660 | int datasize = count - sizeof(attributes); |
| 661 | |
| 662 | if (count < sizeof(attributes)) |
| 663 | return -EINVAL; |
| 664 | |
| 665 | data = kmalloc(datasize, GFP_KERNEL); |
| 666 | |
| 667 | if (!data) |
| 668 | return -ENOMEM; |
| 669 | |
| 670 | efivars = var->efivars; |
| 671 | |
| 672 | if (copy_from_user(&attributes, userbuf, sizeof(attributes))) { |
| 673 | count = -EFAULT; |
| 674 | goto out; |
| 675 | } |
| 676 | |
| 677 | if (attributes & ~(EFI_VARIABLE_MASK)) { |
| 678 | count = -EINVAL; |
| 679 | goto out; |
| 680 | } |
| 681 | |
| 682 | if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) { |
| 683 | count = -EFAULT; |
| 684 | goto out; |
| 685 | } |
| 686 | |
| 687 | if (validate_var(&var->var, data, datasize) == false) { |
| 688 | count = -EINVAL; |
| 689 | goto out; |
| 690 | } |
| 691 | |
| 692 | status = efivars->ops->set_variable(var->var.VariableName, |
| 693 | &var->var.VendorGuid, |
| 694 | attributes, datasize, |
| 695 | data); |
| 696 | |
| 697 | switch (status) { |
| 698 | case EFI_SUCCESS: |
| 699 | mutex_lock(&inode->i_mutex); |
| 700 | i_size_write(inode, count); |
| 701 | mutex_unlock(&inode->i_mutex); |
| 702 | break; |
| 703 | case EFI_INVALID_PARAMETER: |
| 704 | count = -EINVAL; |
| 705 | break; |
| 706 | case EFI_OUT_OF_RESOURCES: |
| 707 | count = -ENOSPC; |
| 708 | break; |
| 709 | case EFI_DEVICE_ERROR: |
| 710 | count = -EIO; |
| 711 | break; |
| 712 | case EFI_WRITE_PROTECTED: |
| 713 | count = -EROFS; |
| 714 | break; |
| 715 | case EFI_SECURITY_VIOLATION: |
| 716 | count = -EACCES; |
| 717 | break; |
| 718 | case EFI_NOT_FOUND: |
| 719 | count = -ENOENT; |
| 720 | break; |
| 721 | default: |
| 722 | count = -EINVAL; |
| 723 | break; |
| 724 | } |
| 725 | out: |
| 726 | kfree(data); |
| 727 | |
| 728 | return count; |
| 729 | } |
| 730 | |
| 731 | static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf, |
| 732 | size_t count, loff_t *ppos) |
| 733 | { |
| 734 | struct efivar_entry *var = file->private_data; |
| 735 | struct efivars *efivars = var->efivars; |
| 736 | efi_status_t status; |
| 737 | unsigned long datasize = 0; |
| 738 | u32 attributes; |
| 739 | void *data; |
| 740 | ssize_t size; |
| 741 | |
| 742 | status = efivars->ops->get_variable(var->var.VariableName, |
| 743 | &var->var.VendorGuid, |
| 744 | &attributes, &datasize, NULL); |
| 745 | |
| 746 | if (status != EFI_BUFFER_TOO_SMALL) |
| 747 | return 0; |
| 748 | |
| 749 | data = kmalloc(datasize + 4, GFP_KERNEL); |
| 750 | |
| 751 | if (!data) |
| 752 | return 0; |
| 753 | |
| 754 | status = efivars->ops->get_variable(var->var.VariableName, |
| 755 | &var->var.VendorGuid, |
| 756 | &attributes, &datasize, |
| 757 | (data + 4)); |
| 758 | |
| 759 | if (status != EFI_SUCCESS) |
| 760 | return 0; |
| 761 | |
| 762 | memcpy(data, &attributes, 4); |
| 763 | size = simple_read_from_buffer(userbuf, count, ppos, |
| 764 | data, datasize + 4); |
| 765 | kfree(data); |
| 766 | |
| 767 | return size; |
| 768 | } |
| 769 | |
| 770 | static void efivarfs_evict_inode(struct inode *inode) |
| 771 | { |
| 772 | clear_inode(inode); |
| 773 | } |
| 774 | |
| 775 | static const struct super_operations efivarfs_ops = { |
| 776 | .statfs = simple_statfs, |
| 777 | .drop_inode = generic_delete_inode, |
| 778 | .evict_inode = efivarfs_evict_inode, |
| 779 | .show_options = generic_show_options, |
| 780 | }; |
| 781 | |
| 782 | static struct super_block *efivarfs_sb; |
| 783 | |
| 784 | static const struct inode_operations efivarfs_dir_inode_operations; |
| 785 | |
| 786 | static const struct file_operations efivarfs_file_operations = { |
| 787 | .open = efivarfs_file_open, |
| 788 | .read = efivarfs_file_read, |
| 789 | .write = efivarfs_file_write, |
| 790 | .llseek = no_llseek, |
| 791 | }; |
| 792 | |
| 793 | static struct inode *efivarfs_get_inode(struct super_block *sb, |
| 794 | const struct inode *dir, int mode, dev_t dev) |
| 795 | { |
| 796 | struct inode *inode = new_inode(sb); |
| 797 | |
| 798 | if (inode) { |
| 799 | inode->i_ino = get_next_ino(); |
| 800 | inode->i_uid = inode->i_gid = 0; |
| 801 | inode->i_mode = mode; |
| 802 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 803 | switch (mode & S_IFMT) { |
| 804 | case S_IFREG: |
| 805 | inode->i_fop = &efivarfs_file_operations; |
| 806 | break; |
| 807 | case S_IFDIR: |
| 808 | inode->i_op = &efivarfs_dir_inode_operations; |
| 809 | inode->i_fop = &simple_dir_operations; |
| 810 | inc_nlink(inode); |
| 811 | break; |
| 812 | } |
| 813 | } |
| 814 | return inode; |
| 815 | } |
| 816 | |
| 817 | static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid) |
| 818 | { |
| 819 | guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]); |
| 820 | guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]); |
| 821 | guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]); |
| 822 | guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]); |
| 823 | guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]); |
| 824 | guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]); |
| 825 | guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]); |
| 826 | guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]); |
| 827 | guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]); |
| 828 | guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]); |
| 829 | guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]); |
| 830 | guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]); |
| 831 | guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]); |
| 832 | guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]); |
| 833 | guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]); |
| 834 | guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]); |
| 835 | } |
| 836 | |
| 837 | static int efivarfs_create(struct inode *dir, struct dentry *dentry, |
| 838 | umode_t mode, bool excl) |
| 839 | { |
| 840 | struct inode *inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0); |
| 841 | struct efivars *efivars = &__efivars; |
| 842 | struct efivar_entry *var; |
| 843 | int namelen, i = 0, err = 0; |
| 844 | |
| 845 | if (dentry->d_name.len < 38) |
| 846 | return -EINVAL; |
| 847 | |
| 848 | if (!inode) |
| 849 | return -ENOSPC; |
| 850 | |
| 851 | var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL); |
| 852 | |
| 853 | if (!var) |
| 854 | return -ENOMEM; |
| 855 | |
| 856 | namelen = dentry->d_name.len - GUID_LEN; |
| 857 | |
| 858 | efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1, |
| 859 | &var->var.VendorGuid); |
| 860 | |
| 861 | for (i = 0; i < namelen; i++) |
| 862 | var->var.VariableName[i] = dentry->d_name.name[i]; |
| 863 | |
| 864 | var->var.VariableName[i] = '\0'; |
| 865 | |
| 866 | inode->i_private = var; |
| 867 | var->efivars = efivars; |
| 868 | var->kobj.kset = efivars->kset; |
| 869 | |
| 870 | err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s", |
| 871 | dentry->d_name.name); |
| 872 | if (err) |
| 873 | goto out; |
| 874 | |
| 875 | kobject_uevent(&var->kobj, KOBJ_ADD); |
| 876 | spin_lock(&efivars->lock); |
| 877 | list_add(&var->list, &efivars->list); |
| 878 | spin_unlock(&efivars->lock); |
| 879 | d_instantiate(dentry, inode); |
| 880 | dget(dentry); |
| 881 | out: |
| 882 | if (err) |
| 883 | kfree(var); |
| 884 | return err; |
| 885 | } |
| 886 | |
| 887 | static int efivarfs_unlink(struct inode *dir, struct dentry *dentry) |
| 888 | { |
| 889 | struct efivar_entry *var = dentry->d_inode->i_private; |
| 890 | struct efivars *efivars = var->efivars; |
| 891 | efi_status_t status; |
| 892 | |
| 893 | spin_lock(&efivars->lock); |
| 894 | |
| 895 | status = efivars->ops->set_variable(var->var.VariableName, |
| 896 | &var->var.VendorGuid, |
| 897 | 0, 0, NULL); |
| 898 | |
| 899 | if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) { |
| 900 | list_del(&var->list); |
| 901 | spin_unlock(&efivars->lock); |
| 902 | efivar_unregister(var); |
| 903 | drop_nlink(dir); |
| 904 | dput(dentry); |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | spin_unlock(&efivars->lock); |
| 909 | return -EINVAL; |
| 910 | }; |
| 911 | |
| 912 | int efivarfs_fill_super(struct super_block *sb, void *data, int silent) |
| 913 | { |
| 914 | struct inode *inode = NULL; |
| 915 | struct dentry *root; |
| 916 | struct efivar_entry *entry, *n; |
| 917 | struct efivars *efivars = &__efivars; |
| 918 | int err; |
| 919 | |
| 920 | efivarfs_sb = sb; |
| 921 | |
| 922 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
| 923 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 924 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 925 | sb->s_magic = PSTOREFS_MAGIC; |
| 926 | sb->s_op = &efivarfs_ops; |
| 927 | sb->s_time_gran = 1; |
| 928 | |
| 929 | inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0); |
| 930 | if (!inode) { |
| 931 | err = -ENOMEM; |
| 932 | goto fail; |
| 933 | } |
| 934 | inode->i_op = &efivarfs_dir_inode_operations; |
| 935 | |
| 936 | root = d_make_root(inode); |
| 937 | sb->s_root = root; |
| 938 | if (!root) { |
| 939 | err = -ENOMEM; |
| 940 | goto fail; |
| 941 | } |
| 942 | |
| 943 | list_for_each_entry_safe(entry, n, &efivars->list, list) { |
| 944 | struct inode *inode; |
| 945 | struct dentry *dentry, *root = efivarfs_sb->s_root; |
| 946 | char *name; |
| 947 | unsigned long size = 0; |
| 948 | int len, i; |
| 949 | |
| 950 | len = utf16_strlen(entry->var.VariableName); |
| 951 | |
| 952 | /* GUID plus trailing NULL */ |
| 953 | name = kmalloc(len + 38, GFP_ATOMIC); |
| 954 | |
| 955 | for (i = 0; i < len; i++) |
| 956 | name[i] = entry->var.VariableName[i] & 0xFF; |
| 957 | |
| 958 | name[len] = '-'; |
| 959 | |
| 960 | efi_guid_unparse(&entry->var.VendorGuid, name + len + 1); |
| 961 | |
| 962 | name[len+GUID_LEN] = '\0'; |
| 963 | |
| 964 | inode = efivarfs_get_inode(efivarfs_sb, root->d_inode, |
| 965 | S_IFREG | 0644, 0); |
| 966 | dentry = d_alloc_name(root, name); |
| 967 | |
| 968 | efivars->ops->get_variable(entry->var.VariableName, |
| 969 | &entry->var.VendorGuid, |
| 970 | &entry->var.Attributes, |
| 971 | &size, |
| 972 | NULL); |
| 973 | |
| 974 | mutex_lock(&inode->i_mutex); |
| 975 | inode->i_private = entry; |
| 976 | i_size_write(inode, size+4); |
| 977 | mutex_unlock(&inode->i_mutex); |
| 978 | d_add(dentry, inode); |
| 979 | } |
| 980 | |
| 981 | return 0; |
| 982 | fail: |
| 983 | iput(inode); |
| 984 | return err; |
| 985 | } |
| 986 | |
| 987 | static struct dentry *efivarfs_mount(struct file_system_type *fs_type, |
| 988 | int flags, const char *dev_name, void *data) |
| 989 | { |
| 990 | return mount_single(fs_type, flags, data, efivarfs_fill_super); |
| 991 | } |
| 992 | |
| 993 | static void efivarfs_kill_sb(struct super_block *sb) |
| 994 | { |
| 995 | kill_litter_super(sb); |
| 996 | efivarfs_sb = NULL; |
| 997 | } |
| 998 | |
| 999 | static struct file_system_type efivarfs_type = { |
| 1000 | .name = "efivarfs", |
| 1001 | .mount = efivarfs_mount, |
| 1002 | .kill_sb = efivarfs_kill_sb, |
| 1003 | }; |
| 1004 | |
| 1005 | static const struct inode_operations efivarfs_dir_inode_operations = { |
| 1006 | .lookup = simple_lookup, |
| 1007 | .unlink = efivarfs_unlink, |
| 1008 | .create = efivarfs_create, |
| 1009 | }; |
| 1010 | |
| 1011 | static struct pstore_info efi_pstore_info; |
| 1012 | |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 1013 | #ifdef CONFIG_PSTORE |
| 1014 | |
| 1015 | static int efi_pstore_open(struct pstore_info *psi) |
| 1016 | { |
| 1017 | struct efivars *efivars = psi->data; |
| 1018 | |
| 1019 | spin_lock(&efivars->lock); |
| 1020 | efivars->walk_entry = list_first_entry(&efivars->list, |
| 1021 | struct efivar_entry, list); |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
| 1025 | static int efi_pstore_close(struct pstore_info *psi) |
| 1026 | { |
| 1027 | struct efivars *efivars = psi->data; |
| 1028 | |
| 1029 | spin_unlock(&efivars->lock); |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type, |
Kees Cook | f6f8285 | 2011-11-17 12:58:07 -0800 | [diff] [blame] | 1034 | struct timespec *timespec, |
| 1035 | char **buf, struct pstore_info *psi) |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 1036 | { |
| 1037 | efi_guid_t vendor = LINUX_EFI_CRASH_GUID; |
| 1038 | struct efivars *efivars = psi->data; |
| 1039 | char name[DUMP_NAME_LEN]; |
| 1040 | int i; |
| 1041 | unsigned int part, size; |
| 1042 | unsigned long time; |
| 1043 | |
| 1044 | while (&efivars->walk_entry->list != &efivars->list) { |
| 1045 | if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid, |
| 1046 | vendor)) { |
| 1047 | for (i = 0; i < DUMP_NAME_LEN; i++) { |
| 1048 | name[i] = efivars->walk_entry->var.VariableName[i]; |
| 1049 | } |
| 1050 | if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) { |
| 1051 | *id = part; |
| 1052 | timespec->tv_sec = time; |
| 1053 | timespec->tv_nsec = 0; |
| 1054 | get_var_data_locked(efivars, &efivars->walk_entry->var); |
| 1055 | size = efivars->walk_entry->var.DataSize; |
Kees Cook | f6f8285 | 2011-11-17 12:58:07 -0800 | [diff] [blame] | 1056 | *buf = kmalloc(size, GFP_KERNEL); |
| 1057 | if (*buf == NULL) |
| 1058 | return -ENOMEM; |
| 1059 | memcpy(*buf, efivars->walk_entry->var.Data, |
| 1060 | size); |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 1061 | efivars->walk_entry = list_entry(efivars->walk_entry->list.next, |
| 1062 | struct efivar_entry, list); |
| 1063 | return size; |
| 1064 | } |
| 1065 | } |
| 1066 | efivars->walk_entry = list_entry(efivars->walk_entry->list.next, |
| 1067 | struct efivar_entry, list); |
| 1068 | } |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
Kees Cook | 3d6d8d2 | 2011-11-17 13:13:29 -0800 | [diff] [blame] | 1072 | static int efi_pstore_write(enum pstore_type_id type, |
| 1073 | enum kmsg_dump_reason reason, u64 *id, |
Chen Gong | b238b8f | 2011-10-12 09:17:24 -0700 | [diff] [blame] | 1074 | unsigned int part, size_t size, struct pstore_info *psi) |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 1075 | { |
| 1076 | char name[DUMP_NAME_LEN]; |
| 1077 | char stub_name[DUMP_NAME_LEN]; |
| 1078 | efi_char16_t efi_name[DUMP_NAME_LEN]; |
| 1079 | efi_guid_t vendor = LINUX_EFI_CRASH_GUID; |
| 1080 | struct efivars *efivars = psi->data; |
| 1081 | struct efivar_entry *entry, *found = NULL; |
Chen Gong | b238b8f | 2011-10-12 09:17:24 -0700 | [diff] [blame] | 1082 | int i, ret = 0; |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 1083 | |
| 1084 | sprintf(stub_name, "dump-type%u-%u-", type, part); |
| 1085 | sprintf(name, "%s%lu", stub_name, get_seconds()); |
| 1086 | |
| 1087 | spin_lock(&efivars->lock); |
| 1088 | |
| 1089 | for (i = 0; i < DUMP_NAME_LEN; i++) |
| 1090 | efi_name[i] = stub_name[i]; |
| 1091 | |
| 1092 | /* |
| 1093 | * Clean up any entries with the same name |
| 1094 | */ |
| 1095 | |
| 1096 | list_for_each_entry(entry, &efivars->list, list) { |
| 1097 | get_var_data_locked(efivars, &entry->var); |
| 1098 | |
Mike Waychison | c475594 | 2011-07-21 16:57:59 -0400 | [diff] [blame] | 1099 | if (efi_guidcmp(entry->var.VendorGuid, vendor)) |
| 1100 | continue; |
| 1101 | if (utf16_strncmp(entry->var.VariableName, efi_name, |
| 1102 | utf16_strlen(efi_name))) |
| 1103 | continue; |
| 1104 | /* Needs to be a prefix */ |
| 1105 | if (entry->var.VariableName[utf16_strlen(efi_name)] == 0) |
| 1106 | continue; |
| 1107 | |
| 1108 | /* found */ |
| 1109 | found = entry; |
Mike Waychison | 7644c16 | 2011-07-21 16:58:00 -0400 | [diff] [blame] | 1110 | efivars->ops->set_variable(entry->var.VariableName, |
| 1111 | &entry->var.VendorGuid, |
| 1112 | PSTORE_EFI_ATTRIBUTES, |
Mike Waychison | c475594 | 2011-07-21 16:57:59 -0400 | [diff] [blame] | 1113 | 0, NULL); |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | if (found) |
| 1117 | list_del(&found->list); |
| 1118 | |
| 1119 | for (i = 0; i < DUMP_NAME_LEN; i++) |
| 1120 | efi_name[i] = name[i]; |
| 1121 | |
Mike Waychison | 7644c16 | 2011-07-21 16:58:00 -0400 | [diff] [blame] | 1122 | efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES, |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 1123 | size, psi->buf); |
| 1124 | |
| 1125 | spin_unlock(&efivars->lock); |
| 1126 | |
| 1127 | if (found) |
| 1128 | efivar_unregister(found); |
| 1129 | |
| 1130 | if (size) |
Chen Gong | b238b8f | 2011-10-12 09:17:24 -0700 | [diff] [blame] | 1131 | ret = efivar_create_sysfs_entry(efivars, |
Mike Waychison | a294090 | 2011-07-21 16:57:57 -0400 | [diff] [blame] | 1132 | utf16_strsize(efi_name, |
| 1133 | DUMP_NAME_LEN * 2), |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 1134 | efi_name, &vendor); |
| 1135 | |
Chen Gong | b238b8f | 2011-10-12 09:17:24 -0700 | [diff] [blame] | 1136 | *id = part; |
| 1137 | return ret; |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 1138 | }; |
| 1139 | |
| 1140 | static int efi_pstore_erase(enum pstore_type_id type, u64 id, |
| 1141 | struct pstore_info *psi) |
| 1142 | { |
Kees Cook | 3d6d8d2 | 2011-11-17 13:13:29 -0800 | [diff] [blame] | 1143 | efi_pstore_write(type, 0, &id, (unsigned int)id, 0, psi); |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 1144 | |
| 1145 | return 0; |
| 1146 | } |
| 1147 | #else |
| 1148 | static int efi_pstore_open(struct pstore_info *psi) |
| 1149 | { |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | static int efi_pstore_close(struct pstore_info *psi) |
| 1154 | { |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
| 1158 | static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type, |
Christoph Fritz | eee628d | 2011-11-28 23:49:33 +0100 | [diff] [blame] | 1159 | struct timespec *timespec, |
| 1160 | char **buf, struct pstore_info *psi) |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 1161 | { |
| 1162 | return -1; |
| 1163 | } |
| 1164 | |
Kees Cook | 3d6d8d2 | 2011-11-17 13:13:29 -0800 | [diff] [blame] | 1165 | static int efi_pstore_write(enum pstore_type_id type, |
| 1166 | enum kmsg_dump_reason reason, u64 *id, |
Chen Gong | b238b8f | 2011-10-12 09:17:24 -0700 | [diff] [blame] | 1167 | unsigned int part, size_t size, struct pstore_info *psi) |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 1168 | { |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | static int efi_pstore_erase(enum pstore_type_id type, u64 id, |
| 1173 | struct pstore_info *psi) |
| 1174 | { |
| 1175 | return 0; |
| 1176 | } |
| 1177 | #endif |
| 1178 | |
| 1179 | static struct pstore_info efi_pstore_info = { |
| 1180 | .owner = THIS_MODULE, |
| 1181 | .name = "efi", |
| 1182 | .open = efi_pstore_open, |
| 1183 | .close = efi_pstore_close, |
| 1184 | .read = efi_pstore_read, |
| 1185 | .write = efi_pstore_write, |
| 1186 | .erase = efi_pstore_erase, |
| 1187 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 1189 | static ssize_t efivar_create(struct file *filp, struct kobject *kobj, |
Greg Kroah-Hartman | 97fa5bb | 2007-11-07 13:56:19 -0800 | [diff] [blame] | 1190 | struct bin_attribute *bin_attr, |
| 1191 | char *buf, loff_t pos, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | { |
| 1193 | struct efi_variable *new_var = (struct efi_variable *)buf; |
Mike Waychison | 4142ef1 | 2011-03-11 17:43:11 -0800 | [diff] [blame] | 1194 | struct efivars *efivars = bin_attr->private; |
Matt Domsch | 496a0fc | 2007-01-26 00:57:18 -0800 | [diff] [blame] | 1195 | struct efivar_entry *search_efivar, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | unsigned long strsize1, strsize2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | efi_status_t status = EFI_NOT_FOUND; |
| 1198 | int found = 0; |
| 1199 | |
| 1200 | if (!capable(CAP_SYS_ADMIN)) |
| 1201 | return -EACCES; |
| 1202 | |
Matthew Garrett | fec6c20 | 2012-04-30 16:11:30 -0400 | [diff] [blame] | 1203 | if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 || |
| 1204 | validate_var(new_var, new_var->Data, new_var->DataSize) == false) { |
| 1205 | printk(KERN_ERR "efivars: Malformed variable content\n"); |
| 1206 | return -EINVAL; |
| 1207 | } |
| 1208 | |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1209 | spin_lock(&efivars->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | |
| 1211 | /* |
| 1212 | * Does this variable already exist? |
| 1213 | */ |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1214 | list_for_each_entry_safe(search_efivar, n, &efivars->list, list) { |
Mike Waychison | a294090 | 2011-07-21 16:57:57 -0400 | [diff] [blame] | 1215 | strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024); |
| 1216 | strsize2 = utf16_strsize(new_var->VariableName, 1024); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | if (strsize1 == strsize2 && |
| 1218 | !memcmp(&(search_efivar->var.VariableName), |
| 1219 | new_var->VariableName, strsize1) && |
| 1220 | !efi_guidcmp(search_efivar->var.VendorGuid, |
| 1221 | new_var->VendorGuid)) { |
| 1222 | found = 1; |
| 1223 | break; |
| 1224 | } |
| 1225 | } |
| 1226 | if (found) { |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1227 | spin_unlock(&efivars->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | return -EINVAL; |
| 1229 | } |
| 1230 | |
| 1231 | /* now *really* create the variable via EFI */ |
Mike Waychison | 3295814 | 2011-03-11 17:43:21 -0800 | [diff] [blame] | 1232 | status = efivars->ops->set_variable(new_var->VariableName, |
| 1233 | &new_var->VendorGuid, |
| 1234 | new_var->Attributes, |
| 1235 | new_var->DataSize, |
| 1236 | new_var->Data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | |
| 1238 | if (status != EFI_SUCCESS) { |
| 1239 | printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n", |
| 1240 | status); |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1241 | spin_unlock(&efivars->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | return -EIO; |
| 1243 | } |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1244 | spin_unlock(&efivars->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | |
| 1246 | /* Create the entry in sysfs. Locking is not required here */ |
Mike Waychison | 4142ef1 | 2011-03-11 17:43:11 -0800 | [diff] [blame] | 1247 | status = efivar_create_sysfs_entry(efivars, |
Mike Waychison | a294090 | 2011-07-21 16:57:57 -0400 | [diff] [blame] | 1248 | utf16_strsize(new_var->VariableName, |
| 1249 | 1024), |
Mike Waychison | 4142ef1 | 2011-03-11 17:43:11 -0800 | [diff] [blame] | 1250 | new_var->VariableName, |
| 1251 | &new_var->VendorGuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | if (status) { |
| 1253 | printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n"); |
| 1254 | } |
| 1255 | return count; |
| 1256 | } |
| 1257 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 1258 | static ssize_t efivar_delete(struct file *filp, struct kobject *kobj, |
Greg Kroah-Hartman | 97fa5bb | 2007-11-07 13:56:19 -0800 | [diff] [blame] | 1259 | struct bin_attribute *bin_attr, |
| 1260 | char *buf, loff_t pos, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | { |
| 1262 | struct efi_variable *del_var = (struct efi_variable *)buf; |
Mike Waychison | 4142ef1 | 2011-03-11 17:43:11 -0800 | [diff] [blame] | 1263 | struct efivars *efivars = bin_attr->private; |
Matt Domsch | 496a0fc | 2007-01-26 00:57:18 -0800 | [diff] [blame] | 1264 | struct efivar_entry *search_efivar, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | unsigned long strsize1, strsize2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | efi_status_t status = EFI_NOT_FOUND; |
| 1267 | int found = 0; |
| 1268 | |
| 1269 | if (!capable(CAP_SYS_ADMIN)) |
| 1270 | return -EACCES; |
| 1271 | |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1272 | spin_lock(&efivars->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | |
| 1274 | /* |
| 1275 | * Does this variable already exist? |
| 1276 | */ |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1277 | list_for_each_entry_safe(search_efivar, n, &efivars->list, list) { |
Mike Waychison | a294090 | 2011-07-21 16:57:57 -0400 | [diff] [blame] | 1278 | strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024); |
| 1279 | strsize2 = utf16_strsize(del_var->VariableName, 1024); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | if (strsize1 == strsize2 && |
| 1281 | !memcmp(&(search_efivar->var.VariableName), |
| 1282 | del_var->VariableName, strsize1) && |
| 1283 | !efi_guidcmp(search_efivar->var.VendorGuid, |
| 1284 | del_var->VendorGuid)) { |
| 1285 | found = 1; |
| 1286 | break; |
| 1287 | } |
| 1288 | } |
| 1289 | if (!found) { |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1290 | spin_unlock(&efivars->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | return -EINVAL; |
| 1292 | } |
| 1293 | /* force the Attributes/DataSize to 0 to ensure deletion */ |
| 1294 | del_var->Attributes = 0; |
| 1295 | del_var->DataSize = 0; |
| 1296 | |
Mike Waychison | 3295814 | 2011-03-11 17:43:21 -0800 | [diff] [blame] | 1297 | status = efivars->ops->set_variable(del_var->VariableName, |
| 1298 | &del_var->VendorGuid, |
| 1299 | del_var->Attributes, |
| 1300 | del_var->DataSize, |
| 1301 | del_var->Data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | |
| 1303 | if (status != EFI_SUCCESS) { |
| 1304 | printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n", |
| 1305 | status); |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1306 | spin_unlock(&efivars->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | return -EIO; |
| 1308 | } |
Matt Domsch | 496a0fc | 2007-01-26 00:57:18 -0800 | [diff] [blame] | 1309 | list_del(&search_efivar->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | /* We need to release this lock before unregistering. */ |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1311 | spin_unlock(&efivars->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | efivar_unregister(search_efivar); |
| 1313 | |
| 1314 | /* It's dead Jim.... */ |
| 1315 | return count; |
| 1316 | } |
| 1317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | /* |
| 1319 | * Let's not leave out systab information that snuck into |
| 1320 | * the efivars driver |
| 1321 | */ |
Greg Kroah-Hartman | 334c630 | 2007-11-02 13:20:40 -0700 | [diff] [blame] | 1322 | static ssize_t systab_show(struct kobject *kobj, |
| 1323 | struct kobj_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | { |
| 1325 | char *str = buf; |
| 1326 | |
Greg Kroah-Hartman | 334c630 | 2007-11-02 13:20:40 -0700 | [diff] [blame] | 1327 | if (!kobj || !buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | return -EINVAL; |
| 1329 | |
Bjorn Helgaas | b2c99e3 | 2006-03-26 01:37:08 -0800 | [diff] [blame] | 1330 | if (efi.mps != EFI_INVALID_TABLE_ADDR) |
| 1331 | str += sprintf(str, "MPS=0x%lx\n", efi.mps); |
| 1332 | if (efi.acpi20 != EFI_INVALID_TABLE_ADDR) |
| 1333 | str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20); |
| 1334 | if (efi.acpi != EFI_INVALID_TABLE_ADDR) |
| 1335 | str += sprintf(str, "ACPI=0x%lx\n", efi.acpi); |
| 1336 | if (efi.smbios != EFI_INVALID_TABLE_ADDR) |
| 1337 | str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios); |
| 1338 | if (efi.hcdp != EFI_INVALID_TABLE_ADDR) |
| 1339 | str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp); |
| 1340 | if (efi.boot_info != EFI_INVALID_TABLE_ADDR) |
| 1341 | str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info); |
| 1342 | if (efi.uga != EFI_INVALID_TABLE_ADDR) |
| 1343 | str += sprintf(str, "UGA=0x%lx\n", efi.uga); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | |
| 1345 | return str - buf; |
| 1346 | } |
| 1347 | |
Greg Kroah-Hartman | 334c630 | 2007-11-02 13:20:40 -0700 | [diff] [blame] | 1348 | static struct kobj_attribute efi_attr_systab = |
| 1349 | __ATTR(systab, 0400, systab_show, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
Greg Kroah-Hartman | 334c630 | 2007-11-02 13:20:40 -0700 | [diff] [blame] | 1351 | static struct attribute *efi_subsys_attrs[] = { |
| 1352 | &efi_attr_systab.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | NULL, /* maybe more in the future? */ |
| 1354 | }; |
| 1355 | |
Greg Kroah-Hartman | 334c630 | 2007-11-02 13:20:40 -0700 | [diff] [blame] | 1356 | static struct attribute_group efi_subsys_attr_group = { |
| 1357 | .attrs = efi_subsys_attrs, |
| 1358 | }; |
| 1359 | |
Greg Kroah-Hartman | bc87d2f | 2007-11-07 13:56:19 -0800 | [diff] [blame] | 1360 | static struct kobject *efi_kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | |
| 1362 | /* |
| 1363 | * efivar_create_sysfs_entry() |
| 1364 | * Requires: |
| 1365 | * variable_name_size = number of bytes required to hold |
| 1366 | * variable_name (not counting the NULL |
| 1367 | * character at the end. |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1368 | * efivars->lock is not held on entry or exit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | * Returns 1 on failure, 0 on success |
| 1370 | */ |
| 1371 | static int |
Mike Waychison | 4142ef1 | 2011-03-11 17:43:11 -0800 | [diff] [blame] | 1372 | efivar_create_sysfs_entry(struct efivars *efivars, |
| 1373 | unsigned long variable_name_size, |
| 1374 | efi_char16_t *variable_name, |
| 1375 | efi_guid_t *vendor_guid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | { |
| 1377 | int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38; |
| 1378 | char *short_name; |
| 1379 | struct efivar_entry *new_efivar; |
| 1380 | |
Deepak Saxena | 9c21538 | 2005-11-07 01:01:24 -0800 | [diff] [blame] | 1381 | short_name = kzalloc(short_name_size + 1, GFP_KERNEL); |
| 1382 | new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | |
| 1384 | if (!short_name || !new_efivar) { |
Jesper Juhl | 0933ad9 | 2005-06-25 14:59:15 -0700 | [diff] [blame] | 1385 | kfree(short_name); |
| 1386 | kfree(new_efivar); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | return 1; |
| 1388 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | |
Mike Waychison | 4142ef1 | 2011-03-11 17:43:11 -0800 | [diff] [blame] | 1390 | new_efivar->efivars = efivars; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | memcpy(new_efivar->var.VariableName, variable_name, |
| 1392 | variable_name_size); |
| 1393 | memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t)); |
| 1394 | |
| 1395 | /* Convert Unicode to normal chars (assume top bits are 0), |
| 1396 | ala UTF-8 */ |
| 1397 | for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) { |
| 1398 | short_name[i] = variable_name[i] & 0xFF; |
| 1399 | } |
| 1400 | /* This is ugly, but necessary to separate one vendor's |
| 1401 | private variables from another's. */ |
| 1402 | |
| 1403 | *(short_name + strlen(short_name)) = '-'; |
| 1404 | efi_guid_unparse(vendor_guid, short_name + strlen(short_name)); |
| 1405 | |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1406 | new_efivar->kobj.kset = efivars->kset; |
Greg Kroah-Hartman | d6d292c | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 1407 | i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL, |
| 1408 | "%s", short_name); |
Jeff Garzik | 69b2186 | 2006-10-11 01:22:23 -0700 | [diff] [blame] | 1409 | if (i) { |
| 1410 | kfree(short_name); |
| 1411 | kfree(new_efivar); |
| 1412 | return 1; |
| 1413 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | |
Greg Kroah-Hartman | d6d292c | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 1415 | kobject_uevent(&new_efivar->kobj, KOBJ_ADD); |
Jesper Juhl | 0933ad9 | 2005-06-25 14:59:15 -0700 | [diff] [blame] | 1416 | kfree(short_name); |
| 1417 | short_name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1419 | spin_lock(&efivars->lock); |
| 1420 | list_add(&new_efivar->list, &efivars->list); |
| 1421 | spin_unlock(&efivars->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | |
| 1423 | return 0; |
| 1424 | } |
Mike Waychison | d502fbb | 2011-03-11 17:43:06 -0800 | [diff] [blame] | 1425 | |
| 1426 | static int |
| 1427 | create_efivars_bin_attributes(struct efivars *efivars) |
| 1428 | { |
| 1429 | struct bin_attribute *attr; |
| 1430 | int error; |
| 1431 | |
| 1432 | /* new_var */ |
| 1433 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); |
| 1434 | if (!attr) |
| 1435 | return -ENOMEM; |
| 1436 | |
| 1437 | attr->attr.name = "new_var"; |
| 1438 | attr->attr.mode = 0200; |
| 1439 | attr->write = efivar_create; |
| 1440 | attr->private = efivars; |
| 1441 | efivars->new_var = attr; |
| 1442 | |
| 1443 | /* del_var */ |
| 1444 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); |
| 1445 | if (!attr) { |
| 1446 | error = -ENOMEM; |
| 1447 | goto out_free; |
| 1448 | } |
| 1449 | attr->attr.name = "del_var"; |
| 1450 | attr->attr.mode = 0200; |
| 1451 | attr->write = efivar_delete; |
| 1452 | attr->private = efivars; |
| 1453 | efivars->del_var = attr; |
| 1454 | |
| 1455 | sysfs_bin_attr_init(efivars->new_var); |
| 1456 | sysfs_bin_attr_init(efivars->del_var); |
| 1457 | |
| 1458 | /* Register */ |
| 1459 | error = sysfs_create_bin_file(&efivars->kset->kobj, |
| 1460 | efivars->new_var); |
| 1461 | if (error) { |
| 1462 | printk(KERN_ERR "efivars: unable to create new_var sysfs file" |
| 1463 | " due to error %d\n", error); |
| 1464 | goto out_free; |
| 1465 | } |
| 1466 | error = sysfs_create_bin_file(&efivars->kset->kobj, |
| 1467 | efivars->del_var); |
| 1468 | if (error) { |
| 1469 | printk(KERN_ERR "efivars: unable to create del_var sysfs file" |
| 1470 | " due to error %d\n", error); |
| 1471 | sysfs_remove_bin_file(&efivars->kset->kobj, |
| 1472 | efivars->new_var); |
| 1473 | goto out_free; |
| 1474 | } |
| 1475 | |
| 1476 | return 0; |
| 1477 | out_free: |
Dan Carpenter | 051d51b | 2011-03-18 10:12:14 +0300 | [diff] [blame] | 1478 | kfree(efivars->del_var); |
| 1479 | efivars->del_var = NULL; |
Mike Waychison | d502fbb | 2011-03-11 17:43:06 -0800 | [diff] [blame] | 1480 | kfree(efivars->new_var); |
| 1481 | efivars->new_var = NULL; |
| 1482 | return error; |
| 1483 | } |
| 1484 | |
Mike Waychison | 4fc756b | 2011-03-11 17:43:27 -0800 | [diff] [blame] | 1485 | void unregister_efivars(struct efivars *efivars) |
Mike Waychison | 76b53f7c | 2011-03-11 17:43:16 -0800 | [diff] [blame] | 1486 | { |
| 1487 | struct efivar_entry *entry, *n; |
Mike Waychison | 4142ef1 | 2011-03-11 17:43:11 -0800 | [diff] [blame] | 1488 | |
Mike Waychison | 76b53f7c | 2011-03-11 17:43:16 -0800 | [diff] [blame] | 1489 | list_for_each_entry_safe(entry, n, &efivars->list, list) { |
| 1490 | spin_lock(&efivars->lock); |
| 1491 | list_del(&entry->list); |
| 1492 | spin_unlock(&efivars->lock); |
| 1493 | efivar_unregister(entry); |
| 1494 | } |
| 1495 | if (efivars->new_var) |
| 1496 | sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var); |
| 1497 | if (efivars->del_var) |
| 1498 | sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var); |
| 1499 | kfree(efivars->new_var); |
| 1500 | kfree(efivars->del_var); |
| 1501 | kset_unregister(efivars->kset); |
| 1502 | } |
Mike Waychison | 4fc756b | 2011-03-11 17:43:27 -0800 | [diff] [blame] | 1503 | EXPORT_SYMBOL_GPL(unregister_efivars); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | |
Mike Waychison | 4fc756b | 2011-03-11 17:43:27 -0800 | [diff] [blame] | 1505 | int register_efivars(struct efivars *efivars, |
| 1506 | const struct efivar_operations *ops, |
| 1507 | struct kobject *parent_kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | { |
| 1509 | efi_status_t status = EFI_NOT_FOUND; |
| 1510 | efi_guid_t vendor_guid; |
| 1511 | efi_char16_t *variable_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | unsigned long variable_name_size = 1024; |
Greg Kroah-Hartman | 334c630 | 2007-11-02 13:20:40 -0700 | [diff] [blame] | 1513 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | |
Deepak Saxena | 9c21538 | 2005-11-07 01:01:24 -0800 | [diff] [blame] | 1515 | variable_name = kzalloc(variable_name_size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | if (!variable_name) { |
| 1517 | printk(KERN_ERR "efivars: Memory allocation failed.\n"); |
| 1518 | return -ENOMEM; |
| 1519 | } |
| 1520 | |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1521 | spin_lock_init(&efivars->lock); |
| 1522 | INIT_LIST_HEAD(&efivars->list); |
Mike Waychison | 3295814 | 2011-03-11 17:43:21 -0800 | [diff] [blame] | 1523 | efivars->ops = ops; |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1524 | |
Mike Waychison | 76b53f7c | 2011-03-11 17:43:16 -0800 | [diff] [blame] | 1525 | efivars->kset = kset_create_and_add("vars", NULL, parent_kobj); |
Mike Waychison | 2942269 | 2011-03-11 17:43:00 -0800 | [diff] [blame] | 1526 | if (!efivars->kset) { |
Greg Kroah-Hartman | 66ac831 | 2007-11-02 13:20:40 -0700 | [diff] [blame] | 1527 | printk(KERN_ERR "efivars: Subsystem registration failed.\n"); |
| 1528 | error = -ENOMEM; |
Mike Waychison | 76b53f7c | 2011-03-11 17:43:16 -0800 | [diff] [blame] | 1529 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | /* |
| 1533 | * Per EFI spec, the maximum storage allocated for both |
| 1534 | * the variable name and variable data is 1024 bytes. |
| 1535 | */ |
| 1536 | |
| 1537 | do { |
| 1538 | variable_name_size = 1024; |
| 1539 | |
Mike Waychison | 3295814 | 2011-03-11 17:43:21 -0800 | [diff] [blame] | 1540 | status = ops->get_next_variable(&variable_name_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | variable_name, |
| 1542 | &vendor_guid); |
| 1543 | switch (status) { |
| 1544 | case EFI_SUCCESS: |
Mike Waychison | 4142ef1 | 2011-03-11 17:43:11 -0800 | [diff] [blame] | 1545 | efivar_create_sysfs_entry(efivars, |
| 1546 | variable_name_size, |
| 1547 | variable_name, |
| 1548 | &vendor_guid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | break; |
| 1550 | case EFI_NOT_FOUND: |
| 1551 | break; |
| 1552 | default: |
| 1553 | printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n", |
| 1554 | status); |
| 1555 | status = EFI_NOT_FOUND; |
| 1556 | break; |
| 1557 | } |
| 1558 | } while (status != EFI_NOT_FOUND); |
| 1559 | |
Mike Waychison | d502fbb | 2011-03-11 17:43:06 -0800 | [diff] [blame] | 1560 | error = create_efivars_bin_attributes(efivars); |
Mike Waychison | 76b53f7c | 2011-03-11 17:43:16 -0800 | [diff] [blame] | 1561 | if (error) |
| 1562 | unregister_efivars(efivars); |
| 1563 | |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 1564 | efivars->efi_pstore_info = efi_pstore_info; |
| 1565 | |
| 1566 | efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL); |
| 1567 | if (efivars->efi_pstore_info.buf) { |
| 1568 | efivars->efi_pstore_info.bufsize = 1024; |
| 1569 | efivars->efi_pstore_info.data = efivars; |
Don Zickus | abd4d55 | 2011-08-12 10:54:51 -0700 | [diff] [blame] | 1570 | spin_lock_init(&efivars->efi_pstore_info.buf_lock); |
Matthew Garrett | 5ee9c19 | 2011-07-21 16:57:56 -0400 | [diff] [blame] | 1571 | pstore_register(&efivars->efi_pstore_info); |
| 1572 | } |
| 1573 | |
Matthew Garrett | 5d9db88 | 2012-10-05 13:54:56 +0800 | [diff] [blame^] | 1574 | register_filesystem(&efivarfs_type); |
| 1575 | |
Mike Waychison | 76b53f7c | 2011-03-11 17:43:16 -0800 | [diff] [blame] | 1576 | out: |
| 1577 | kfree(variable_name); |
| 1578 | |
| 1579 | return error; |
| 1580 | } |
Mike Waychison | 4fc756b | 2011-03-11 17:43:27 -0800 | [diff] [blame] | 1581 | EXPORT_SYMBOL_GPL(register_efivars); |
Mike Waychison | 76b53f7c | 2011-03-11 17:43:16 -0800 | [diff] [blame] | 1582 | |
Mike Waychison | 76b53f7c | 2011-03-11 17:43:16 -0800 | [diff] [blame] | 1583 | /* |
| 1584 | * For now we register the efi subsystem with the firmware subsystem |
| 1585 | * and the vars subsystem with the efi subsystem. In the future, it |
| 1586 | * might make sense to split off the efi subsystem into its own |
| 1587 | * driver, but for now only efivars will register with it, so just |
| 1588 | * include it here. |
| 1589 | */ |
| 1590 | |
| 1591 | static int __init |
| 1592 | efivars_init(void) |
| 1593 | { |
| 1594 | int error = 0; |
| 1595 | |
| 1596 | printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION, |
| 1597 | EFIVARS_DATE); |
| 1598 | |
| 1599 | if (!efi_enabled) |
Mike Waychison | 4fc756b | 2011-03-11 17:43:27 -0800 | [diff] [blame] | 1600 | return 0; |
Mike Waychison | 76b53f7c | 2011-03-11 17:43:16 -0800 | [diff] [blame] | 1601 | |
| 1602 | /* For now we'll register the efi directory at /sys/firmware/efi */ |
| 1603 | efi_kobj = kobject_create_and_add("efi", firmware_kobj); |
| 1604 | if (!efi_kobj) { |
| 1605 | printk(KERN_ERR "efivars: Firmware registration failed.\n"); |
| 1606 | return -ENOMEM; |
| 1607 | } |
| 1608 | |
Mike Waychison | 3295814 | 2011-03-11 17:43:21 -0800 | [diff] [blame] | 1609 | ops.get_variable = efi.get_variable; |
| 1610 | ops.set_variable = efi.set_variable; |
| 1611 | ops.get_next_variable = efi.get_next_variable; |
| 1612 | error = register_efivars(&__efivars, &ops, efi_kobj); |
Dan Carpenter | 3116aab | 2011-03-18 10:12:38 +0300 | [diff] [blame] | 1613 | if (error) |
| 1614 | goto err_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | |
| 1616 | /* Don't forget the systab entry */ |
Greg Kroah-Hartman | bc87d2f | 2007-11-07 13:56:19 -0800 | [diff] [blame] | 1617 | error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group); |
Mike Waychison | 76b53f7c | 2011-03-11 17:43:16 -0800 | [diff] [blame] | 1618 | if (error) { |
| 1619 | printk(KERN_ERR |
| 1620 | "efivars: Sysfs attribute export failed with error %d.\n", |
| 1621 | error); |
Dan Carpenter | 3116aab | 2011-03-18 10:12:38 +0300 | [diff] [blame] | 1622 | goto err_unregister; |
Mike Waychison | 76b53f7c | 2011-03-11 17:43:16 -0800 | [diff] [blame] | 1623 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | |
Dan Carpenter | 3116aab | 2011-03-18 10:12:38 +0300 | [diff] [blame] | 1625 | return 0; |
| 1626 | |
| 1627 | err_unregister: |
| 1628 | unregister_efivars(&__efivars); |
| 1629 | err_put: |
| 1630 | kobject_put(efi_kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | return error; |
| 1632 | } |
| 1633 | |
| 1634 | static void __exit |
| 1635 | efivars_exit(void) |
| 1636 | { |
Randy Dunlap | aabb6e1 | 2011-05-06 13:27:41 -0700 | [diff] [blame] | 1637 | if (efi_enabled) { |
| 1638 | unregister_efivars(&__efivars); |
| 1639 | kobject_put(efi_kobj); |
| 1640 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | module_init(efivars_init); |
| 1644 | module_exit(efivars_exit); |
| 1645 | |