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