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