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