efivars.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  2. * Originally from 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_to_str()
  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/efi.h>
  68. #include <linux/module.h>
  69. #include <linux/slab.h>
  70. #include <linux/ucs2_string.h>
  71. #include <linux/compat.h>
  72. #define EFIVARS_VERSION "0.08"
  73. #define EFIVARS_DATE "2004-May-17"
  74. MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
  75. MODULE_DESCRIPTION("sysfs interface to EFI Variables");
  76. MODULE_LICENSE("GPL");
  77. MODULE_VERSION(EFIVARS_VERSION);
  78. MODULE_ALIAS("platform:efivars");
  79. LIST_HEAD(efivar_sysfs_list);
  80. EXPORT_SYMBOL_GPL(efivar_sysfs_list);
  81. static struct kset *efivars_kset;
  82. static struct bin_attribute *efivars_new_var;
  83. static struct bin_attribute *efivars_del_var;
  84. struct compat_efi_variable {
  85. efi_char16_t VariableName[EFI_VAR_NAME_LEN/sizeof(efi_char16_t)];
  86. efi_guid_t VendorGuid;
  87. __u32 DataSize;
  88. __u8 Data[1024];
  89. __u32 Status;
  90. __u32 Attributes;
  91. } __packed;
  92. struct efivar_attribute {
  93. struct attribute attr;
  94. ssize_t (*show) (struct efivar_entry *entry, char *buf);
  95. ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
  96. };
  97. #define EFIVAR_ATTR(_name, _mode, _show, _store) \
  98. struct efivar_attribute efivar_attr_##_name = { \
  99. .attr = {.name = __stringify(_name), .mode = _mode}, \
  100. .show = _show, \
  101. .store = _store, \
  102. };
  103. #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
  104. #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
  105. /*
  106. * Prototype for sysfs creation function
  107. */
  108. static int
  109. efivar_create_sysfs_entry(struct efivar_entry *new_var);
  110. static ssize_t
  111. efivar_guid_read(struct efivar_entry *entry, char *buf)
  112. {
  113. struct efi_variable *var = &entry->var;
  114. char *str = buf;
  115. if (!entry || !buf)
  116. return 0;
  117. efi_guid_to_str(&var->VendorGuid, str);
  118. str += strlen(str);
  119. str += sprintf(str, "\n");
  120. return str - buf;
  121. }
  122. static ssize_t
  123. efivar_attr_read(struct efivar_entry *entry, char *buf)
  124. {
  125. struct efi_variable *var = &entry->var;
  126. char *str = buf;
  127. if (!entry || !buf)
  128. return -EINVAL;
  129. var->DataSize = 1024;
  130. if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
  131. return -EIO;
  132. if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
  133. str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
  134. if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
  135. str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
  136. if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
  137. str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
  138. if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
  139. str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
  140. if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
  141. str += sprintf(str,
  142. "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
  143. if (var->Attributes &
  144. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
  145. str += sprintf(str,
  146. "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
  147. if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
  148. str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
  149. return str - buf;
  150. }
  151. static ssize_t
  152. efivar_size_read(struct efivar_entry *entry, char *buf)
  153. {
  154. struct efi_variable *var = &entry->var;
  155. char *str = buf;
  156. if (!entry || !buf)
  157. return -EINVAL;
  158. var->DataSize = 1024;
  159. if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
  160. return -EIO;
  161. str += sprintf(str, "0x%lx\n", var->DataSize);
  162. return str - buf;
  163. }
  164. static ssize_t
  165. efivar_data_read(struct efivar_entry *entry, char *buf)
  166. {
  167. struct efi_variable *var = &entry->var;
  168. if (!entry || !buf)
  169. return -EINVAL;
  170. var->DataSize = 1024;
  171. if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
  172. return -EIO;
  173. memcpy(buf, var->Data, var->DataSize);
  174. return var->DataSize;
  175. }
  176. static inline int
  177. sanity_check(struct efi_variable *var, efi_char16_t *name, efi_guid_t vendor,
  178. unsigned long size, u32 attributes, u8 *data)
  179. {
  180. /*
  181. * If only updating the variable data, then the name
  182. * and guid should remain the same
  183. */
  184. if (memcmp(name, var->VariableName, sizeof(var->VariableName)) ||
  185. efi_guidcmp(vendor, var->VendorGuid)) {
  186. printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
  187. return -EINVAL;
  188. }
  189. if ((size <= 0) || (attributes == 0)){
  190. printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
  191. return -EINVAL;
  192. }
  193. if ((attributes & ~EFI_VARIABLE_MASK) != 0 ||
  194. efivar_validate(name, data, size) == false) {
  195. printk(KERN_ERR "efivars: Malformed variable content\n");
  196. return -EINVAL;
  197. }
  198. return 0;
  199. }
  200. static inline bool is_compat(void)
  201. {
  202. if (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())
  203. return true;
  204. return false;
  205. }
  206. static void
  207. copy_out_compat(struct efi_variable *dst, struct compat_efi_variable *src)
  208. {
  209. memcpy(dst->VariableName, src->VariableName, EFI_VAR_NAME_LEN);
  210. memcpy(dst->Data, src->Data, sizeof(src->Data));
  211. dst->VendorGuid = src->VendorGuid;
  212. dst->DataSize = src->DataSize;
  213. dst->Attributes = src->Attributes;
  214. }
  215. /*
  216. * We allow each variable to be edited via rewriting the
  217. * entire efi variable structure.
  218. */
  219. static ssize_t
  220. efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
  221. {
  222. struct efi_variable *new_var, *var = &entry->var;
  223. efi_char16_t *name;
  224. unsigned long size;
  225. efi_guid_t vendor;
  226. u32 attributes;
  227. u8 *data;
  228. int err;
  229. if (is_compat()) {
  230. struct compat_efi_variable *compat;
  231. if (count != sizeof(*compat))
  232. return -EINVAL;
  233. compat = (struct compat_efi_variable *)buf;
  234. attributes = compat->Attributes;
  235. vendor = compat->VendorGuid;
  236. name = compat->VariableName;
  237. size = compat->DataSize;
  238. data = compat->Data;
  239. err = sanity_check(var, name, vendor, size, attributes, data);
  240. if (err)
  241. return err;
  242. copy_out_compat(&entry->var, compat);
  243. } else {
  244. if (count != sizeof(struct efi_variable))
  245. return -EINVAL;
  246. new_var = (struct efi_variable *)buf;
  247. attributes = new_var->Attributes;
  248. vendor = new_var->VendorGuid;
  249. name = new_var->VariableName;
  250. size = new_var->DataSize;
  251. data = new_var->Data;
  252. err = sanity_check(var, name, vendor, size, attributes, data);
  253. if (err)
  254. return err;
  255. memcpy(&entry->var, new_var, count);
  256. }
  257. err = efivar_entry_set(entry, attributes, size, data, NULL);
  258. if (err) {
  259. printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err);
  260. return -EIO;
  261. }
  262. return count;
  263. }
  264. static ssize_t
  265. efivar_show_raw(struct efivar_entry *entry, char *buf)
  266. {
  267. struct efi_variable *var = &entry->var;
  268. struct compat_efi_variable *compat;
  269. size_t size;
  270. if (!entry || !buf)
  271. return 0;
  272. var->DataSize = 1024;
  273. if (efivar_entry_get(entry, &entry->var.Attributes,
  274. &entry->var.DataSize, entry->var.Data))
  275. return -EIO;
  276. if (is_compat()) {
  277. compat = (struct compat_efi_variable *)buf;
  278. size = sizeof(*compat);
  279. memcpy(compat->VariableName, var->VariableName,
  280. EFI_VAR_NAME_LEN);
  281. memcpy(compat->Data, var->Data, sizeof(compat->Data));
  282. compat->VendorGuid = var->VendorGuid;
  283. compat->DataSize = var->DataSize;
  284. compat->Attributes = var->Attributes;
  285. } else {
  286. size = sizeof(*var);
  287. memcpy(buf, var, size);
  288. }
  289. return size;
  290. }
  291. /*
  292. * Generic read/write functions that call the specific functions of
  293. * the attributes...
  294. */
  295. static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
  296. char *buf)
  297. {
  298. struct efivar_entry *var = to_efivar_entry(kobj);
  299. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  300. ssize_t ret = -EIO;
  301. if (!capable(CAP_SYS_ADMIN))
  302. return -EACCES;
  303. if (efivar_attr->show) {
  304. ret = efivar_attr->show(var, buf);
  305. }
  306. return ret;
  307. }
  308. static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
  309. const char *buf, size_t count)
  310. {
  311. struct efivar_entry *var = to_efivar_entry(kobj);
  312. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  313. ssize_t ret = -EIO;
  314. if (!capable(CAP_SYS_ADMIN))
  315. return -EACCES;
  316. if (efivar_attr->store)
  317. ret = efivar_attr->store(var, buf, count);
  318. return ret;
  319. }
  320. static const struct sysfs_ops efivar_attr_ops = {
  321. .show = efivar_attr_show,
  322. .store = efivar_attr_store,
  323. };
  324. static void efivar_release(struct kobject *kobj)
  325. {
  326. struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
  327. kfree(var);
  328. }
  329. static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
  330. static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
  331. static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
  332. static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
  333. static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
  334. static struct attribute *def_attrs[] = {
  335. &efivar_attr_guid.attr,
  336. &efivar_attr_size.attr,
  337. &efivar_attr_attributes.attr,
  338. &efivar_attr_data.attr,
  339. &efivar_attr_raw_var.attr,
  340. NULL,
  341. };
  342. static struct kobj_type efivar_ktype = {
  343. .release = efivar_release,
  344. .sysfs_ops = &efivar_attr_ops,
  345. .default_attrs = def_attrs,
  346. };
  347. static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
  348. struct bin_attribute *bin_attr,
  349. char *buf, loff_t pos, size_t count)
  350. {
  351. struct compat_efi_variable *compat = (struct compat_efi_variable *)buf;
  352. struct efi_variable *new_var = (struct efi_variable *)buf;
  353. struct efivar_entry *new_entry;
  354. bool need_compat = is_compat();
  355. efi_char16_t *name;
  356. unsigned long size;
  357. u32 attributes;
  358. u8 *data;
  359. int err;
  360. if (!capable(CAP_SYS_ADMIN))
  361. return -EACCES;
  362. if (need_compat) {
  363. if (count != sizeof(*compat))
  364. return -EINVAL;
  365. attributes = compat->Attributes;
  366. name = compat->VariableName;
  367. size = compat->DataSize;
  368. data = compat->Data;
  369. } else {
  370. if (count != sizeof(*new_var))
  371. return -EINVAL;
  372. attributes = new_var->Attributes;
  373. name = new_var->VariableName;
  374. size = new_var->DataSize;
  375. data = new_var->Data;
  376. }
  377. if ((attributes & ~EFI_VARIABLE_MASK) != 0 ||
  378. efivar_validate(name, data, size) == false) {
  379. printk(KERN_ERR "efivars: Malformed variable content\n");
  380. return -EINVAL;
  381. }
  382. new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL);
  383. if (!new_entry)
  384. return -ENOMEM;
  385. if (need_compat)
  386. copy_out_compat(&new_entry->var, compat);
  387. else
  388. memcpy(&new_entry->var, new_var, sizeof(*new_var));
  389. err = efivar_entry_set(new_entry, attributes, size,
  390. data, &efivar_sysfs_list);
  391. if (err) {
  392. if (err == -EEXIST)
  393. err = -EINVAL;
  394. goto out;
  395. }
  396. if (efivar_create_sysfs_entry(new_entry)) {
  397. printk(KERN_WARNING "efivars: failed to create sysfs entry.\n");
  398. kfree(new_entry);
  399. }
  400. return count;
  401. out:
  402. kfree(new_entry);
  403. return err;
  404. }
  405. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  406. struct bin_attribute *bin_attr,
  407. char *buf, loff_t pos, size_t count)
  408. {
  409. struct efi_variable *del_var = (struct efi_variable *)buf;
  410. struct compat_efi_variable *compat;
  411. struct efivar_entry *entry;
  412. efi_char16_t *name;
  413. efi_guid_t vendor;
  414. int err = 0;
  415. if (!capable(CAP_SYS_ADMIN))
  416. return -EACCES;
  417. if (is_compat()) {
  418. if (count != sizeof(*compat))
  419. return -EINVAL;
  420. compat = (struct compat_efi_variable *)buf;
  421. name = compat->VariableName;
  422. vendor = compat->VendorGuid;
  423. } else {
  424. if (count != sizeof(*del_var))
  425. return -EINVAL;
  426. name = del_var->VariableName;
  427. vendor = del_var->VendorGuid;
  428. }
  429. efivar_entry_iter_begin();
  430. entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true);
  431. if (!entry)
  432. err = -EINVAL;
  433. else if (__efivar_entry_delete(entry))
  434. err = -EIO;
  435. if (err) {
  436. efivar_entry_iter_end();
  437. return err;
  438. }
  439. if (!entry->scanning) {
  440. efivar_entry_iter_end();
  441. efivar_unregister(entry);
  442. } else
  443. efivar_entry_iter_end();
  444. /* It's dead Jim.... */
  445. return count;
  446. }
  447. /**
  448. * efivar_create_sysfs_entry - create a new entry in sysfs
  449. * @new_var: efivar entry to create
  450. *
  451. * Returns 0 on success, negative error code on failure
  452. */
  453. static int
  454. efivar_create_sysfs_entry(struct efivar_entry *new_var)
  455. {
  456. int i, short_name_size;
  457. char *short_name;
  458. unsigned long variable_name_size;
  459. efi_char16_t *variable_name;
  460. int ret;
  461. variable_name = new_var->var.VariableName;
  462. variable_name_size = ucs2_strlen(variable_name) * sizeof(efi_char16_t);
  463. /*
  464. * Length of the variable bytes in ASCII, plus the '-' separator,
  465. * plus the GUID, plus trailing NUL
  466. */
  467. short_name_size = variable_name_size / sizeof(efi_char16_t)
  468. + 1 + EFI_VARIABLE_GUID_LEN + 1;
  469. short_name = kzalloc(short_name_size, GFP_KERNEL);
  470. if (!short_name)
  471. return -ENOMEM;
  472. /* Convert Unicode to normal chars (assume top bits are 0),
  473. ala UTF-8 */
  474. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  475. short_name[i] = variable_name[i] & 0xFF;
  476. }
  477. /* This is ugly, but necessary to separate one vendor's
  478. private variables from another's. */
  479. *(short_name + strlen(short_name)) = '-';
  480. efi_guid_to_str(&new_var->var.VendorGuid,
  481. short_name + strlen(short_name));
  482. new_var->kobj.kset = efivars_kset;
  483. ret = kobject_init_and_add(&new_var->kobj, &efivar_ktype,
  484. NULL, "%s", short_name);
  485. kfree(short_name);
  486. if (ret)
  487. return ret;
  488. kobject_uevent(&new_var->kobj, KOBJ_ADD);
  489. efivar_entry_add(new_var, &efivar_sysfs_list);
  490. return 0;
  491. }
  492. static int
  493. create_efivars_bin_attributes(void)
  494. {
  495. struct bin_attribute *attr;
  496. int error;
  497. /* new_var */
  498. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  499. if (!attr)
  500. return -ENOMEM;
  501. attr->attr.name = "new_var";
  502. attr->attr.mode = 0200;
  503. attr->write = efivar_create;
  504. efivars_new_var = attr;
  505. /* del_var */
  506. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  507. if (!attr) {
  508. error = -ENOMEM;
  509. goto out_free;
  510. }
  511. attr->attr.name = "del_var";
  512. attr->attr.mode = 0200;
  513. attr->write = efivar_delete;
  514. efivars_del_var = attr;
  515. sysfs_bin_attr_init(efivars_new_var);
  516. sysfs_bin_attr_init(efivars_del_var);
  517. /* Register */
  518. error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_new_var);
  519. if (error) {
  520. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  521. " due to error %d\n", error);
  522. goto out_free;
  523. }
  524. error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_del_var);
  525. if (error) {
  526. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  527. " due to error %d\n", error);
  528. sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
  529. goto out_free;
  530. }
  531. return 0;
  532. out_free:
  533. kfree(efivars_del_var);
  534. efivars_del_var = NULL;
  535. kfree(efivars_new_var);
  536. efivars_new_var = NULL;
  537. return error;
  538. }
  539. static int efivar_update_sysfs_entry(efi_char16_t *name, efi_guid_t vendor,
  540. unsigned long name_size, void *data)
  541. {
  542. struct efivar_entry *entry = data;
  543. if (efivar_entry_find(name, vendor, &efivar_sysfs_list, false))
  544. return 0;
  545. memcpy(entry->var.VariableName, name, name_size);
  546. memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
  547. return 1;
  548. }
  549. static void efivar_update_sysfs_entries(struct work_struct *work)
  550. {
  551. struct efivar_entry *entry;
  552. int err;
  553. /* Add new sysfs entries */
  554. while (1) {
  555. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  556. if (!entry)
  557. return;
  558. err = efivar_init(efivar_update_sysfs_entry, entry,
  559. true, false, &efivar_sysfs_list);
  560. if (!err)
  561. break;
  562. efivar_create_sysfs_entry(entry);
  563. }
  564. kfree(entry);
  565. }
  566. static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
  567. unsigned long name_size, void *data)
  568. {
  569. struct efivar_entry *entry;
  570. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  571. if (!entry)
  572. return -ENOMEM;
  573. memcpy(entry->var.VariableName, name, name_size);
  574. memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
  575. efivar_create_sysfs_entry(entry);
  576. return 0;
  577. }
  578. static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
  579. {
  580. efivar_entry_remove(entry);
  581. efivar_unregister(entry);
  582. return 0;
  583. }
  584. static void efivars_sysfs_exit(void)
  585. {
  586. /* Remove all entries and destroy */
  587. __efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list, NULL, NULL);
  588. if (efivars_new_var)
  589. sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
  590. if (efivars_del_var)
  591. sysfs_remove_bin_file(&efivars_kset->kobj, efivars_del_var);
  592. kfree(efivars_new_var);
  593. kfree(efivars_del_var);
  594. kset_unregister(efivars_kset);
  595. }
  596. int efivars_sysfs_init(void)
  597. {
  598. struct kobject *parent_kobj = efivars_kobject();
  599. int error = 0;
  600. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  601. return -ENODEV;
  602. /* No efivars has been registered yet */
  603. if (!parent_kobj)
  604. return 0;
  605. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  606. EFIVARS_DATE);
  607. efivars_kset = kset_create_and_add("vars", NULL, parent_kobj);
  608. if (!efivars_kset) {
  609. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  610. return -ENOMEM;
  611. }
  612. efivar_init(efivars_sysfs_callback, NULL, false,
  613. true, &efivar_sysfs_list);
  614. error = create_efivars_bin_attributes();
  615. if (error) {
  616. efivars_sysfs_exit();
  617. return error;
  618. }
  619. INIT_WORK(&efivar_work, efivar_update_sysfs_entries);
  620. return 0;
  621. }
  622. EXPORT_SYMBOL_GPL(efivars_sysfs_init);
  623. module_init(efivars_sysfs_init);
  624. module_exit(efivars_sysfs_exit);