efivars.c 22 KB

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