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