efivars.c 21 KB

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