efivars.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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 EFI_ATTR(_name, _mode, _show, _store) \
  121. struct subsys_attribute efi_attr_##_name = { \
  122. .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE}, \
  123. .show = _show, \
  124. .store = _store, \
  125. };
  126. #define EFIVAR_ATTR(_name, _mode, _show, _store) \
  127. struct efivar_attribute efivar_attr_##_name = { \
  128. .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE}, \
  129. .show = _show, \
  130. .store = _store, \
  131. };
  132. #define VAR_SUBSYS_ATTR(_name, _mode, _show, _store) \
  133. struct subsys_attribute var_subsys_attr_##_name = { \
  134. .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE}, \
  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(unsigned long variable_name_size,
  145. efi_char16_t *variable_name,
  146. efi_guid_t *vendor_guid);
  147. /* Return the number of unicode characters in data */
  148. static unsigned long
  149. utf8_strlen(efi_char16_t *data, unsigned long maxlength)
  150. {
  151. unsigned long length = 0;
  152. while (*data++ != 0 && length < maxlength)
  153. length++;
  154. return length;
  155. }
  156. /*
  157. * Return the number of bytes is the length of this string
  158. * Note: this is NOT the same as the number of unicode characters
  159. */
  160. static inline unsigned long
  161. utf8_strsize(efi_char16_t *data, unsigned long maxlength)
  162. {
  163. return utf8_strlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
  164. }
  165. static efi_status_t
  166. get_var_data(struct efi_variable *var)
  167. {
  168. efi_status_t status;
  169. spin_lock(&efivars_lock);
  170. var->DataSize = 1024;
  171. status = efi.get_variable(var->VariableName,
  172. &var->VendorGuid,
  173. &var->Attributes,
  174. &var->DataSize,
  175. var->Data);
  176. spin_unlock(&efivars_lock);
  177. if (status != EFI_SUCCESS) {
  178. printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
  179. status);
  180. }
  181. return status;
  182. }
  183. static ssize_t
  184. efivar_guid_read(struct efivar_entry *entry, char *buf)
  185. {
  186. struct efi_variable *var = &entry->var;
  187. char *str = buf;
  188. if (!entry || !buf)
  189. return 0;
  190. efi_guid_unparse(&var->VendorGuid, str);
  191. str += strlen(str);
  192. str += sprintf(str, "\n");
  193. return str - buf;
  194. }
  195. static ssize_t
  196. efivar_attr_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(var);
  204. if (status != EFI_SUCCESS)
  205. return -EIO;
  206. if (var->Attributes & 0x1)
  207. str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
  208. if (var->Attributes & 0x2)
  209. str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
  210. if (var->Attributes & 0x4)
  211. str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
  212. return str - buf;
  213. }
  214. static ssize_t
  215. efivar_size_read(struct efivar_entry *entry, char *buf)
  216. {
  217. struct efi_variable *var = &entry->var;
  218. char *str = buf;
  219. efi_status_t status;
  220. if (!entry || !buf)
  221. return -EINVAL;
  222. status = get_var_data(var);
  223. if (status != EFI_SUCCESS)
  224. return -EIO;
  225. str += sprintf(str, "0x%lx\n", var->DataSize);
  226. return str - buf;
  227. }
  228. static ssize_t
  229. efivar_data_read(struct efivar_entry *entry, char *buf)
  230. {
  231. struct efi_variable *var = &entry->var;
  232. efi_status_t status;
  233. if (!entry || !buf)
  234. return -EINVAL;
  235. status = get_var_data(var);
  236. if (status != EFI_SUCCESS)
  237. return -EIO;
  238. memcpy(buf, var->Data, var->DataSize);
  239. return var->DataSize;
  240. }
  241. /*
  242. * We allow each variable to be edited via rewriting the
  243. * entire efi variable structure.
  244. */
  245. static ssize_t
  246. efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
  247. {
  248. struct efi_variable *new_var, *var = &entry->var;
  249. efi_status_t status = EFI_NOT_FOUND;
  250. if (count != sizeof(struct efi_variable))
  251. return -EINVAL;
  252. new_var = (struct efi_variable *)buf;
  253. /*
  254. * If only updating the variable data, then the name
  255. * and guid should remain the same
  256. */
  257. if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
  258. efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
  259. printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
  260. return -EINVAL;
  261. }
  262. if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
  263. printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
  264. return -EINVAL;
  265. }
  266. spin_lock(&efivars_lock);
  267. status = efi.set_variable(new_var->VariableName,
  268. &new_var->VendorGuid,
  269. new_var->Attributes,
  270. new_var->DataSize,
  271. new_var->Data);
  272. spin_unlock(&efivars_lock);
  273. if (status != EFI_SUCCESS) {
  274. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  275. status);
  276. return -EIO;
  277. }
  278. memcpy(&entry->var, new_var, count);
  279. return count;
  280. }
  281. static ssize_t
  282. efivar_show_raw(struct efivar_entry *entry, char *buf)
  283. {
  284. struct efi_variable *var = &entry->var;
  285. efi_status_t status;
  286. if (!entry || !buf)
  287. return 0;
  288. status = get_var_data(var);
  289. if (status != EFI_SUCCESS)
  290. return -EIO;
  291. memcpy(buf, var, sizeof(*var));
  292. return sizeof(*var);
  293. }
  294. /*
  295. * Generic read/write functions that call the specific functions of
  296. * the atttributes...
  297. */
  298. static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
  299. char *buf)
  300. {
  301. struct efivar_entry *var = to_efivar_entry(kobj);
  302. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  303. ssize_t ret = -EIO;
  304. if (!capable(CAP_SYS_ADMIN))
  305. return -EACCES;
  306. if (efivar_attr->show) {
  307. ret = efivar_attr->show(var, buf);
  308. }
  309. return ret;
  310. }
  311. static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
  312. const char *buf, size_t count)
  313. {
  314. struct efivar_entry *var = to_efivar_entry(kobj);
  315. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  316. ssize_t ret = -EIO;
  317. if (!capable(CAP_SYS_ADMIN))
  318. return -EACCES;
  319. if (efivar_attr->store)
  320. ret = efivar_attr->store(var, buf, count);
  321. return ret;
  322. }
  323. static struct sysfs_ops efivar_attr_ops = {
  324. .show = efivar_attr_show,
  325. .store = efivar_attr_store,
  326. };
  327. static void efivar_release(struct kobject *kobj)
  328. {
  329. struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
  330. kfree(var);
  331. }
  332. static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
  333. static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
  334. static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
  335. static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
  336. static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
  337. static struct attribute *def_attrs[] = {
  338. &efivar_attr_guid.attr,
  339. &efivar_attr_size.attr,
  340. &efivar_attr_attributes.attr,
  341. &efivar_attr_data.attr,
  342. &efivar_attr_raw_var.attr,
  343. NULL,
  344. };
  345. static struct kobj_type ktype_efivar = {
  346. .release = efivar_release,
  347. .sysfs_ops = &efivar_attr_ops,
  348. .default_attrs = def_attrs,
  349. };
  350. static ssize_t
  351. dummy(struct subsystem *sub, char *buf)
  352. {
  353. return -ENODEV;
  354. }
  355. static inline void
  356. efivar_unregister(struct efivar_entry *var)
  357. {
  358. kobject_unregister(&var->kobj);
  359. }
  360. static ssize_t
  361. efivar_create(struct subsystem *sub, const char *buf, size_t count)
  362. {
  363. struct efi_variable *new_var = (struct efi_variable *)buf;
  364. struct efivar_entry *search_efivar, *n;
  365. unsigned long strsize1, strsize2;
  366. efi_status_t status = EFI_NOT_FOUND;
  367. int found = 0;
  368. if (!capable(CAP_SYS_ADMIN))
  369. return -EACCES;
  370. spin_lock(&efivars_lock);
  371. /*
  372. * Does this variable already exist?
  373. */
  374. list_for_each_entry_safe(search_efivar, n, &efivar_list, list) {
  375. strsize1 = utf8_strsize(search_efivar->var.VariableName, 1024);
  376. strsize2 = utf8_strsize(new_var->VariableName, 1024);
  377. if (strsize1 == strsize2 &&
  378. !memcmp(&(search_efivar->var.VariableName),
  379. new_var->VariableName, strsize1) &&
  380. !efi_guidcmp(search_efivar->var.VendorGuid,
  381. new_var->VendorGuid)) {
  382. found = 1;
  383. break;
  384. }
  385. }
  386. if (found) {
  387. spin_unlock(&efivars_lock);
  388. return -EINVAL;
  389. }
  390. /* now *really* create the variable via EFI */
  391. status = efi.set_variable(new_var->VariableName,
  392. &new_var->VendorGuid,
  393. new_var->Attributes,
  394. new_var->DataSize,
  395. new_var->Data);
  396. if (status != EFI_SUCCESS) {
  397. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  398. status);
  399. spin_unlock(&efivars_lock);
  400. return -EIO;
  401. }
  402. spin_unlock(&efivars_lock);
  403. /* Create the entry in sysfs. Locking is not required here */
  404. status = efivar_create_sysfs_entry(utf8_strsize(new_var->VariableName,
  405. 1024), new_var->VariableName, &new_var->VendorGuid);
  406. if (status) {
  407. printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
  408. }
  409. return count;
  410. }
  411. static ssize_t
  412. efivar_delete(struct subsystem *sub, const char *buf, size_t count)
  413. {
  414. struct efi_variable *del_var = (struct efi_variable *)buf;
  415. struct efivar_entry *search_efivar, *n;
  416. unsigned long strsize1, strsize2;
  417. efi_status_t status = EFI_NOT_FOUND;
  418. int found = 0;
  419. if (!capable(CAP_SYS_ADMIN))
  420. return -EACCES;
  421. spin_lock(&efivars_lock);
  422. /*
  423. * Does this variable already exist?
  424. */
  425. list_for_each_entry_safe(search_efivar, n, &efivar_list, list) {
  426. strsize1 = utf8_strsize(search_efivar->var.VariableName, 1024);
  427. strsize2 = utf8_strsize(del_var->VariableName, 1024);
  428. if (strsize1 == strsize2 &&
  429. !memcmp(&(search_efivar->var.VariableName),
  430. del_var->VariableName, strsize1) &&
  431. !efi_guidcmp(search_efivar->var.VendorGuid,
  432. del_var->VendorGuid)) {
  433. found = 1;
  434. break;
  435. }
  436. }
  437. if (!found) {
  438. spin_unlock(&efivars_lock);
  439. return -EINVAL;
  440. }
  441. /* force the Attributes/DataSize to 0 to ensure deletion */
  442. del_var->Attributes = 0;
  443. del_var->DataSize = 0;
  444. status = efi.set_variable(del_var->VariableName,
  445. &del_var->VendorGuid,
  446. del_var->Attributes,
  447. del_var->DataSize,
  448. del_var->Data);
  449. if (status != EFI_SUCCESS) {
  450. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  451. status);
  452. spin_unlock(&efivars_lock);
  453. return -EIO;
  454. }
  455. list_del(&search_efivar->list);
  456. /* We need to release this lock before unregistering. */
  457. spin_unlock(&efivars_lock);
  458. efivar_unregister(search_efivar);
  459. /* It's dead Jim.... */
  460. return count;
  461. }
  462. static VAR_SUBSYS_ATTR(new_var, 0200, dummy, efivar_create);
  463. static VAR_SUBSYS_ATTR(del_var, 0200, dummy, efivar_delete);
  464. static struct subsys_attribute *var_subsys_attrs[] = {
  465. &var_subsys_attr_new_var,
  466. &var_subsys_attr_del_var,
  467. NULL,
  468. };
  469. /*
  470. * Let's not leave out systab information that snuck into
  471. * the efivars driver
  472. */
  473. static ssize_t
  474. systab_read(struct subsystem *entry, char *buf)
  475. {
  476. char *str = buf;
  477. if (!entry || !buf)
  478. return -EINVAL;
  479. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  480. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  481. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  482. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  483. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  484. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  485. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  486. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  487. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  488. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  489. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  490. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  491. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  492. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  493. return str - buf;
  494. }
  495. static EFI_ATTR(systab, 0400, systab_read, NULL);
  496. static struct subsys_attribute *efi_subsys_attrs[] = {
  497. &efi_attr_systab,
  498. NULL, /* maybe more in the future? */
  499. };
  500. static decl_subsys(vars, &ktype_efivar, NULL);
  501. static decl_subsys(efi, NULL, NULL);
  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(unsigned long variable_name_size,
  513. efi_char16_t *variable_name,
  514. efi_guid_t *vendor_guid)
  515. {
  516. int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
  517. char *short_name;
  518. struct efivar_entry *new_efivar;
  519. short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
  520. new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  521. if (!short_name || !new_efivar) {
  522. kfree(short_name);
  523. kfree(new_efivar);
  524. return 1;
  525. }
  526. memcpy(new_efivar->var.VariableName, variable_name,
  527. variable_name_size);
  528. memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
  529. /* Convert Unicode to normal chars (assume top bits are 0),
  530. ala UTF-8 */
  531. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  532. short_name[i] = variable_name[i] & 0xFF;
  533. }
  534. /* This is ugly, but necessary to separate one vendor's
  535. private variables from another's. */
  536. *(short_name + strlen(short_name)) = '-';
  537. efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
  538. kobject_set_name(&new_efivar->kobj, "%s", short_name);
  539. kobj_set_kset_s(new_efivar, vars_subsys);
  540. i = kobject_register(&new_efivar->kobj);
  541. if (i) {
  542. kfree(short_name);
  543. kfree(new_efivar);
  544. return 1;
  545. }
  546. kfree(short_name);
  547. short_name = NULL;
  548. spin_lock(&efivars_lock);
  549. list_add(&new_efivar->list, &efivar_list);
  550. spin_unlock(&efivars_lock);
  551. return 0;
  552. }
  553. /*
  554. * For now we register the efi subsystem with the firmware subsystem
  555. * and the vars subsystem with the efi subsystem. In the future, it
  556. * might make sense to split off the efi subsystem into its own
  557. * driver, but for now only efivars will register with it, so just
  558. * include it here.
  559. */
  560. static int __init
  561. efivars_init(void)
  562. {
  563. efi_status_t status = EFI_NOT_FOUND;
  564. efi_guid_t vendor_guid;
  565. efi_char16_t *variable_name;
  566. struct subsys_attribute *attr;
  567. unsigned long variable_name_size = 1024;
  568. int i, error = 0;
  569. if (!efi_enabled)
  570. return -ENODEV;
  571. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  572. if (!variable_name) {
  573. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  574. return -ENOMEM;
  575. }
  576. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  577. EFIVARS_DATE);
  578. /*
  579. * For now we'll register the efi subsys within this driver
  580. */
  581. error = firmware_register(&efi_subsys);
  582. if (error) {
  583. printk(KERN_ERR "efivars: Firmware registration failed with error %d.\n", error);
  584. goto out_free;
  585. }
  586. kset_set_kset_s(&vars_subsys, efi_subsys);
  587. error = subsystem_register(&vars_subsys);
  588. if (error) {
  589. printk(KERN_ERR "efivars: Subsystem registration failed with error %d.\n", error);
  590. goto out_firmware_unregister;
  591. }
  592. /*
  593. * Per EFI spec, the maximum storage allocated for both
  594. * the variable name and variable data is 1024 bytes.
  595. */
  596. do {
  597. variable_name_size = 1024;
  598. status = efi.get_next_variable(&variable_name_size,
  599. variable_name,
  600. &vendor_guid);
  601. switch (status) {
  602. case EFI_SUCCESS:
  603. efivar_create_sysfs_entry(variable_name_size,
  604. variable_name,
  605. &vendor_guid);
  606. break;
  607. case EFI_NOT_FOUND:
  608. break;
  609. default:
  610. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  611. status);
  612. status = EFI_NOT_FOUND;
  613. break;
  614. }
  615. } while (status != EFI_NOT_FOUND);
  616. /*
  617. * Now add attributes to allow creation of new vars
  618. * and deletion of existing ones...
  619. */
  620. for (i = 0; (attr = var_subsys_attrs[i]) && !error; i++) {
  621. if (attr->show && attr->store)
  622. error = subsys_create_file(&vars_subsys, attr);
  623. }
  624. /* Don't forget the systab entry */
  625. for (i = 0; (attr = efi_subsys_attrs[i]) && !error; i++) {
  626. if (attr->show)
  627. error = subsys_create_file(&efi_subsys, attr);
  628. }
  629. if (error)
  630. printk(KERN_ERR "efivars: Sysfs attribute export failed with error %d.\n", error);
  631. else
  632. goto out_free;
  633. subsystem_unregister(&vars_subsys);
  634. out_firmware_unregister:
  635. firmware_unregister(&efi_subsys);
  636. out_free:
  637. kfree(variable_name);
  638. return error;
  639. }
  640. static void __exit
  641. efivars_exit(void)
  642. {
  643. struct efivar_entry *entry, *n;
  644. list_for_each_entry_safe(entry, n, &efivar_list, list) {
  645. spin_lock(&efivars_lock);
  646. list_del(&entry->list);
  647. spin_unlock(&efivars_lock);
  648. efivar_unregister(entry);
  649. }
  650. subsystem_unregister(&vars_subsys);
  651. firmware_unregister(&efi_subsys);
  652. }
  653. module_init(efivars_init);
  654. module_exit(efivars_exit);