efivars.c 19 KB

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