efivars.c 19 KB

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