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