efivars.c 21 KB

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