/kern_2.6.32/drivers/firmware/efivars.c

http://omnia2droid.googlecode.com/ · C · 761 lines · 501 code · 115 blank · 145 comment · 81 complexity · e2f361f4a362cb547fa47b9e55fea3c7 MD5 · raw file

  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. #include <linux/capability.h>
  68. #include <linux/types.h>
  69. #include <linux/errno.h>
  70. #include <linux/init.h>
  71. #include <linux/mm.h>
  72. #include <linux/module.h>
  73. #include <linux/string.h>
  74. #include <linux/smp.h>
  75. #include <linux/efi.h>
  76. #include <linux/sysfs.h>
  77. #include <linux/kobject.h>
  78. #include <linux/device.h>
  79. #include <asm/uaccess.h>
  80. #define EFIVARS_VERSION "0.08"
  81. #define EFIVARS_DATE "2004-May-17"
  82. MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
  83. MODULE_DESCRIPTION("sysfs interface to EFI Variables");
  84. MODULE_LICENSE("GPL");
  85. MODULE_VERSION(EFIVARS_VERSION);
  86. /*
  87. * efivars_lock protects two things:
  88. * 1) efivar_list - adds, removals, reads, writes
  89. * 2) efi.[gs]et_variable() calls.
  90. * It must not be held when creating sysfs entries or calling kmalloc.
  91. * efi.get_next_variable() is only called from efivars_init(),
  92. * which is protected by the BKL, so that path is safe.
  93. */
  94. static DEFINE_SPINLOCK(efivars_lock);
  95. static LIST_HEAD(efivar_list);
  96. /*
  97. * The maximum size of VariableName + Data = 1024
  98. * Therefore, it's reasonable to save that much
  99. * space in each part of the structure,
  100. * and we use a page for reading/writing.
  101. */
  102. struct efi_variable {
  103. efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
  104. efi_guid_t VendorGuid;
  105. unsigned long DataSize;
  106. __u8 Data[1024];
  107. efi_status_t Status;
  108. __u32 Attributes;
  109. } __attribute__((packed));
  110. struct efivar_entry {
  111. struct efi_variable var;
  112. struct list_head list;
  113. struct kobject kobj;
  114. };
  115. struct efivar_attribute {
  116. struct attribute attr;
  117. ssize_t (*show) (struct efivar_entry *entry, char *buf);
  118. ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
  119. };
  120. #define EFIVAR_ATTR(_name, _mode, _show, _store) \
  121. struct efivar_attribute efivar_attr_##_name = { \
  122. .attr = {.name = __stringify(_name), .mode = _mode}, \
  123. .show = _show, \
  124. .store = _store, \
  125. };
  126. #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
  127. #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
  128. /*
  129. * Prototype for sysfs creation function
  130. */
  131. static int
  132. efivar_create_sysfs_entry(unsigned long variable_name_size,
  133. efi_char16_t *variable_name,
  134. efi_guid_t *vendor_guid);
  135. /* Return the number of unicode characters in data */
  136. static unsigned long
  137. utf8_strlen(efi_char16_t *data, unsigned long maxlength)
  138. {
  139. unsigned long length = 0;
  140. while (*data++ != 0 && length < maxlength)
  141. length++;
  142. return length;
  143. }
  144. /*
  145. * Return the number of bytes is the length of this string
  146. * Note: this is NOT the same as the number of unicode characters
  147. */
  148. static inline unsigned long
  149. utf8_strsize(efi_char16_t *data, unsigned long maxlength)
  150. {
  151. return utf8_strlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
  152. }
  153. static efi_status_t
  154. get_var_data(struct efi_variable *var)
  155. {
  156. efi_status_t status;
  157. spin_lock(&efivars_lock);
  158. var->DataSize = 1024;
  159. status = efi.get_variable(var->VariableName,
  160. &var->VendorGuid,
  161. &var->Attributes,
  162. &var->DataSize,
  163. var->Data);
  164. spin_unlock(&efivars_lock);
  165. if (status != EFI_SUCCESS) {
  166. printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
  167. status);
  168. }
  169. return status;
  170. }
  171. static ssize_t
  172. efivar_guid_read(struct efivar_entry *entry, char *buf)
  173. {
  174. struct efi_variable *var = &entry->var;
  175. char *str = buf;
  176. if (!entry || !buf)
  177. return 0;
  178. efi_guid_unparse(&var->VendorGuid, str);
  179. str += strlen(str);
  180. str += sprintf(str, "\n");
  181. return str - buf;
  182. }
  183. static ssize_t
  184. efivar_attr_read(struct efivar_entry *entry, char *buf)
  185. {
  186. struct efi_variable *var = &entry->var;
  187. char *str = buf;
  188. efi_status_t status;
  189. if (!entry || !buf)
  190. return -EINVAL;
  191. status = get_var_data(var);
  192. if (status != EFI_SUCCESS)
  193. return -EIO;
  194. if (var->Attributes & 0x1)
  195. str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
  196. if (var->Attributes & 0x2)
  197. str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
  198. if (var->Attributes & 0x4)
  199. str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
  200. return str - buf;
  201. }
  202. static ssize_t
  203. efivar_size_read(struct efivar_entry *entry, char *buf)
  204. {
  205. struct efi_variable *var = &entry->var;
  206. char *str = buf;
  207. efi_status_t status;
  208. if (!entry || !buf)
  209. return -EINVAL;
  210. status = get_var_data(var);
  211. if (status != EFI_SUCCESS)
  212. return -EIO;
  213. str += sprintf(str, "0x%lx\n", var->DataSize);
  214. return str - buf;
  215. }
  216. static ssize_t
  217. efivar_data_read(struct efivar_entry *entry, char *buf)
  218. {
  219. struct efi_variable *var = &entry->var;
  220. efi_status_t status;
  221. if (!entry || !buf)
  222. return -EINVAL;
  223. status = get_var_data(var);
  224. if (status != EFI_SUCCESS)
  225. return -EIO;
  226. memcpy(buf, var->Data, var->DataSize);
  227. return var->DataSize;
  228. }
  229. /*
  230. * We allow each variable to be edited via rewriting the
  231. * entire efi variable structure.
  232. */
  233. static ssize_t
  234. efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
  235. {
  236. struct efi_variable *new_var, *var = &entry->var;
  237. efi_status_t status = EFI_NOT_FOUND;
  238. if (count != sizeof(struct efi_variable))
  239. return -EINVAL;
  240. new_var = (struct efi_variable *)buf;
  241. /*
  242. * If only updating the variable data, then the name
  243. * and guid should remain the same
  244. */
  245. if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
  246. efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
  247. printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
  248. return -EINVAL;
  249. }
  250. if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
  251. printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
  252. return -EINVAL;
  253. }
  254. spin_lock(&efivars_lock);
  255. status = efi.set_variable(new_var->VariableName,
  256. &new_var->VendorGuid,
  257. new_var->Attributes,
  258. new_var->DataSize,
  259. new_var->Data);
  260. spin_unlock(&efivars_lock);
  261. if (status != EFI_SUCCESS) {
  262. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  263. status);
  264. return -EIO;
  265. }
  266. memcpy(&entry->var, new_var, count);
  267. return count;
  268. }
  269. static ssize_t
  270. efivar_show_raw(struct efivar_entry *entry, char *buf)
  271. {
  272. struct efi_variable *var = &entry->var;
  273. efi_status_t status;
  274. if (!entry || !buf)
  275. return 0;
  276. status = get_var_data(var);
  277. if (status != EFI_SUCCESS)
  278. return -EIO;
  279. memcpy(buf, var, sizeof(*var));
  280. return sizeof(*var);
  281. }
  282. /*
  283. * Generic read/write functions that call the specific functions of
  284. * the atttributes...
  285. */
  286. static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
  287. char *buf)
  288. {
  289. struct efivar_entry *var = to_efivar_entry(kobj);
  290. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  291. ssize_t ret = -EIO;
  292. if (!capable(CAP_SYS_ADMIN))
  293. return -EACCES;
  294. if (efivar_attr->show) {
  295. ret = efivar_attr->show(var, buf);
  296. }
  297. return ret;
  298. }
  299. static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
  300. const char *buf, size_t count)
  301. {
  302. struct efivar_entry *var = to_efivar_entry(kobj);
  303. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  304. ssize_t ret = -EIO;
  305. if (!capable(CAP_SYS_ADMIN))
  306. return -EACCES;
  307. if (efivar_attr->store)
  308. ret = efivar_attr->store(var, buf, count);
  309. return ret;
  310. }
  311. static struct sysfs_ops efivar_attr_ops = {
  312. .show = efivar_attr_show,
  313. .store = efivar_attr_store,
  314. };
  315. static void efivar_release(struct kobject *kobj)
  316. {
  317. struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
  318. kfree(var);
  319. }
  320. static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
  321. static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
  322. static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
  323. static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
  324. static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
  325. static struct attribute *def_attrs[] = {
  326. &efivar_attr_guid.attr,
  327. &efivar_attr_size.attr,
  328. &efivar_attr_attributes.attr,
  329. &efivar_attr_data.attr,
  330. &efivar_attr_raw_var.attr,
  331. NULL,
  332. };
  333. static struct kobj_type efivar_ktype = {
  334. .release = efivar_release,
  335. .sysfs_ops = &efivar_attr_ops,
  336. .default_attrs = def_attrs,
  337. };
  338. static inline void
  339. efivar_unregister(struct efivar_entry *var)
  340. {
  341. kobject_put(&var->kobj);
  342. }
  343. static ssize_t efivar_create(struct kobject *kobj,
  344. struct bin_attribute *bin_attr,
  345. char *buf, loff_t pos, size_t count)
  346. {
  347. struct efi_variable *new_var = (struct efi_variable *)buf;
  348. struct efivar_entry *search_efivar, *n;
  349. unsigned long strsize1, strsize2;
  350. efi_status_t status = EFI_NOT_FOUND;
  351. int found = 0;
  352. if (!capable(CAP_SYS_ADMIN))
  353. return -EACCES;
  354. spin_lock(&efivars_lock);
  355. /*
  356. * Does this variable already exist?
  357. */
  358. list_for_each_entry_safe(search_efivar, n, &efivar_list, list) {
  359. strsize1 = utf8_strsize(search_efivar->var.VariableName, 1024);
  360. strsize2 = utf8_strsize(new_var->VariableName, 1024);
  361. if (strsize1 == strsize2 &&
  362. !memcmp(&(search_efivar->var.VariableName),
  363. new_var->VariableName, strsize1) &&
  364. !efi_guidcmp(search_efivar->var.VendorGuid,
  365. new_var->VendorGuid)) {
  366. found = 1;
  367. break;
  368. }
  369. }
  370. if (found) {
  371. spin_unlock(&efivars_lock);
  372. return -EINVAL;
  373. }
  374. /* now *really* create the variable via EFI */
  375. status = efi.set_variable(new_var->VariableName,
  376. &new_var->VendorGuid,
  377. new_var->Attributes,
  378. new_var->DataSize,
  379. new_var->Data);
  380. if (status != EFI_SUCCESS) {
  381. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  382. status);
  383. spin_unlock(&efivars_lock);
  384. return -EIO;
  385. }
  386. spin_unlock(&efivars_lock);
  387. /* Create the entry in sysfs. Locking is not required here */
  388. status = efivar_create_sysfs_entry(utf8_strsize(new_var->VariableName,
  389. 1024), new_var->VariableName, &new_var->VendorGuid);
  390. if (status) {
  391. printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
  392. }
  393. return count;
  394. }
  395. static ssize_t efivar_delete(struct kobject *kobj,
  396. struct bin_attribute *bin_attr,
  397. char *buf, loff_t pos, size_t count)
  398. {
  399. struct efi_variable *del_var = (struct efi_variable *)buf;
  400. struct efivar_entry *search_efivar, *n;
  401. unsigned long strsize1, strsize2;
  402. efi_status_t status = EFI_NOT_FOUND;
  403. int found = 0;
  404. if (!capable(CAP_SYS_ADMIN))
  405. return -EACCES;
  406. spin_lock(&efivars_lock);
  407. /*
  408. * Does this variable already exist?
  409. */
  410. list_for_each_entry_safe(search_efivar, n, &efivar_list, list) {
  411. strsize1 = utf8_strsize(search_efivar->var.VariableName, 1024);
  412. strsize2 = utf8_strsize(del_var->VariableName, 1024);
  413. if (strsize1 == strsize2 &&
  414. !memcmp(&(search_efivar->var.VariableName),
  415. del_var->VariableName, strsize1) &&
  416. !efi_guidcmp(search_efivar->var.VendorGuid,
  417. del_var->VendorGuid)) {
  418. found = 1;
  419. break;
  420. }
  421. }
  422. if (!found) {
  423. spin_unlock(&efivars_lock);
  424. return -EINVAL;
  425. }
  426. /* force the Attributes/DataSize to 0 to ensure deletion */
  427. del_var->Attributes = 0;
  428. del_var->DataSize = 0;
  429. status = efi.set_variable(del_var->VariableName,
  430. &del_var->VendorGuid,
  431. del_var->Attributes,
  432. del_var->DataSize,
  433. del_var->Data);
  434. if (status != EFI_SUCCESS) {
  435. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  436. status);
  437. spin_unlock(&efivars_lock);
  438. return -EIO;
  439. }
  440. list_del(&search_efivar->list);
  441. /* We need to release this lock before unregistering. */
  442. spin_unlock(&efivars_lock);
  443. efivar_unregister(search_efivar);
  444. /* It's dead Jim.... */
  445. return count;
  446. }
  447. static struct bin_attribute var_subsys_attr_new_var = {
  448. .attr = {.name = "new_var", .mode = 0200},
  449. .write = efivar_create,
  450. };
  451. static struct bin_attribute var_subsys_attr_del_var = {
  452. .attr = {.name = "del_var", .mode = 0200},
  453. .write = efivar_delete,
  454. };
  455. /*
  456. * Let's not leave out systab information that snuck into
  457. * the efivars driver
  458. */
  459. static ssize_t systab_show(struct kobject *kobj,
  460. struct kobj_attribute *attr, char *buf)
  461. {
  462. char *str = buf;
  463. if (!kobj || !buf)
  464. return -EINVAL;
  465. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  466. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  467. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  468. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  469. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  470. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  471. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  472. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  473. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  474. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  475. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  476. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  477. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  478. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  479. return str - buf;
  480. }
  481. static struct kobj_attribute efi_attr_systab =
  482. __ATTR(systab, 0400, systab_show, NULL);
  483. static struct attribute *efi_subsys_attrs[] = {
  484. &efi_attr_systab.attr,
  485. NULL, /* maybe more in the future? */
  486. };
  487. static struct attribute_group efi_subsys_attr_group = {
  488. .attrs = efi_subsys_attrs,
  489. };
  490. static struct kset *vars_kset;
  491. static struct kobject *efi_kobj;
  492. /*
  493. * efivar_create_sysfs_entry()
  494. * Requires:
  495. * variable_name_size = number of bytes required to hold
  496. * variable_name (not counting the NULL
  497. * character at the end.
  498. * efivars_lock is not held on entry or exit.
  499. * Returns 1 on failure, 0 on success
  500. */
  501. static int
  502. efivar_create_sysfs_entry(unsigned long variable_name_size,
  503. efi_char16_t *variable_name,
  504. efi_guid_t *vendor_guid)
  505. {
  506. int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
  507. char *short_name;
  508. struct efivar_entry *new_efivar;
  509. short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
  510. new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  511. if (!short_name || !new_efivar) {
  512. kfree(short_name);
  513. kfree(new_efivar);
  514. return 1;
  515. }
  516. memcpy(new_efivar->var.VariableName, variable_name,
  517. variable_name_size);
  518. memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
  519. /* Convert Unicode to normal chars (assume top bits are 0),
  520. ala UTF-8 */
  521. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  522. short_name[i] = variable_name[i] & 0xFF;
  523. }
  524. /* This is ugly, but necessary to separate one vendor's
  525. private variables from another's. */
  526. *(short_name + strlen(short_name)) = '-';
  527. efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
  528. new_efivar->kobj.kset = vars_kset;
  529. i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
  530. "%s", short_name);
  531. if (i) {
  532. kfree(short_name);
  533. kfree(new_efivar);
  534. return 1;
  535. }
  536. kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
  537. kfree(short_name);
  538. short_name = NULL;
  539. spin_lock(&efivars_lock);
  540. list_add(&new_efivar->list, &efivar_list);
  541. spin_unlock(&efivars_lock);
  542. return 0;
  543. }
  544. /*
  545. * For now we register the efi subsystem with the firmware subsystem
  546. * and the vars subsystem with the efi subsystem. In the future, it
  547. * might make sense to split off the efi subsystem into its own
  548. * driver, but for now only efivars will register with it, so just
  549. * include it here.
  550. */
  551. static int __init
  552. efivars_init(void)
  553. {
  554. efi_status_t status = EFI_NOT_FOUND;
  555. efi_guid_t vendor_guid;
  556. efi_char16_t *variable_name;
  557. unsigned long variable_name_size = 1024;
  558. int error = 0;
  559. if (!efi_enabled)
  560. return -ENODEV;
  561. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  562. if (!variable_name) {
  563. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  564. return -ENOMEM;
  565. }
  566. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  567. EFIVARS_DATE);
  568. /* For now we'll register the efi directory at /sys/firmware/efi */
  569. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  570. if (!efi_kobj) {
  571. printk(KERN_ERR "efivars: Firmware registration failed.\n");
  572. error = -ENOMEM;
  573. goto out_free;
  574. }
  575. vars_kset = kset_create_and_add("vars", NULL, efi_kobj);
  576. if (!vars_kset) {
  577. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  578. error = -ENOMEM;
  579. goto out_firmware_unregister;
  580. }
  581. /*
  582. * Per EFI spec, the maximum storage allocated for both
  583. * the variable name and variable data is 1024 bytes.
  584. */
  585. do {
  586. variable_name_size = 1024;
  587. status = efi.get_next_variable(&variable_name_size,
  588. variable_name,
  589. &vendor_guid);
  590. switch (status) {
  591. case EFI_SUCCESS:
  592. efivar_create_sysfs_entry(variable_name_size,
  593. variable_name,
  594. &vendor_guid);
  595. break;
  596. case EFI_NOT_FOUND:
  597. break;
  598. default:
  599. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  600. status);
  601. status = EFI_NOT_FOUND;
  602. break;
  603. }
  604. } while (status != EFI_NOT_FOUND);
  605. /*
  606. * Now add attributes to allow creation of new vars
  607. * and deletion of existing ones...
  608. */
  609. error = sysfs_create_bin_file(&vars_kset->kobj,
  610. &var_subsys_attr_new_var);
  611. if (error)
  612. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  613. " due to error %d\n", error);
  614. error = sysfs_create_bin_file(&vars_kset->kobj,
  615. &var_subsys_attr_del_var);
  616. if (error)
  617. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  618. " due to error %d\n", error);
  619. /* Don't forget the systab entry */
  620. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  621. if (error)
  622. printk(KERN_ERR "efivars: Sysfs attribute export failed with error %d.\n", error);
  623. else
  624. goto out_free;
  625. kset_unregister(vars_kset);
  626. out_firmware_unregister:
  627. kobject_put(efi_kobj);
  628. out_free:
  629. kfree(variable_name);
  630. return error;
  631. }
  632. static void __exit
  633. efivars_exit(void)
  634. {
  635. struct efivar_entry *entry, *n;
  636. list_for_each_entry_safe(entry, n, &efivar_list, list) {
  637. spin_lock(&efivars_lock);
  638. list_del(&entry->list);
  639. spin_unlock(&efivars_lock);
  640. efivar_unregister(entry);
  641. }
  642. kset_unregister(vars_kset);
  643. kobject_put(efi_kobj);
  644. }
  645. module_init(efivars_init);
  646. module_exit(efivars_exit);