efivars.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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 inline bool is_compat(void)
  201. {
  202. if (IS_ENABLED(CONFIG_COMPAT) && in_compat_syscall())
  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 = to_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(new_var->VendorGuid, name, data,
  379. size) == false) {
  380. printk(KERN_ERR "efivars: Malformed variable content\n");
  381. return -EINVAL;
  382. }
  383. new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL);
  384. if (!new_entry)
  385. return -ENOMEM;
  386. if (need_compat)
  387. copy_out_compat(&new_entry->var, compat);
  388. else
  389. memcpy(&new_entry->var, new_var, sizeof(*new_var));
  390. err = efivar_entry_set(new_entry, attributes, size,
  391. data, &efivar_sysfs_list);
  392. if (err) {
  393. if (err == -EEXIST)
  394. err = -EINVAL;
  395. goto out;
  396. }
  397. if (efivar_create_sysfs_entry(new_entry)) {
  398. printk(KERN_WARNING "efivars: failed to create sysfs entry.\n");
  399. kfree(new_entry);
  400. }
  401. return count;
  402. out:
  403. kfree(new_entry);
  404. return err;
  405. }
  406. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  407. struct bin_attribute *bin_attr,
  408. char *buf, loff_t pos, size_t count)
  409. {
  410. struct efi_variable *del_var = (struct efi_variable *)buf;
  411. struct compat_efi_variable *compat;
  412. struct efivar_entry *entry;
  413. efi_char16_t *name;
  414. efi_guid_t vendor;
  415. int err = 0;
  416. if (!capable(CAP_SYS_ADMIN))
  417. return -EACCES;
  418. if (is_compat()) {
  419. if (count != sizeof(*compat))
  420. return -EINVAL;
  421. compat = (struct compat_efi_variable *)buf;
  422. name = compat->VariableName;
  423. vendor = compat->VendorGuid;
  424. } else {
  425. if (count != sizeof(*del_var))
  426. return -EINVAL;
  427. name = del_var->VariableName;
  428. vendor = del_var->VendorGuid;
  429. }
  430. if (efivar_entry_iter_begin())
  431. return -EINTR;
  432. entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true);
  433. if (!entry)
  434. err = -EINVAL;
  435. else if (__efivar_entry_delete(entry))
  436. err = -EIO;
  437. if (err) {
  438. efivar_entry_iter_end();
  439. return err;
  440. }
  441. if (!entry->scanning) {
  442. efivar_entry_iter_end();
  443. efivar_unregister(entry);
  444. } else
  445. efivar_entry_iter_end();
  446. /* It's dead Jim.... */
  447. return count;
  448. }
  449. /**
  450. * efivar_create_sysfs_entry - create a new entry in sysfs
  451. * @new_var: efivar entry to create
  452. *
  453. * Returns 0 on success, negative error code on failure
  454. */
  455. static int
  456. efivar_create_sysfs_entry(struct efivar_entry *new_var)
  457. {
  458. int short_name_size;
  459. char *short_name;
  460. unsigned long utf8_name_size;
  461. efi_char16_t *variable_name = new_var->var.VariableName;
  462. int ret;
  463. /*
  464. * Length of the variable bytes in UTF8, plus the '-' separator,
  465. * plus the GUID, plus trailing NUL
  466. */
  467. utf8_name_size = ucs2_utf8size(variable_name);
  468. short_name_size = utf8_name_size + 1 + EFI_VARIABLE_GUID_LEN + 1;
  469. short_name = kmalloc(short_name_size, GFP_KERNEL);
  470. if (!short_name)
  471. return -ENOMEM;
  472. ucs2_as_utf8(short_name, variable_name, short_name_size);
  473. /* This is ugly, but necessary to separate one vendor's
  474. private variables from another's. */
  475. short_name[utf8_name_size] = '-';
  476. efi_guid_to_str(&new_var->var.VendorGuid,
  477. short_name + utf8_name_size + 1);
  478. new_var->kobj.kset = efivars_kset;
  479. ret = kobject_init_and_add(&new_var->kobj, &efivar_ktype,
  480. NULL, "%s", short_name);
  481. kfree(short_name);
  482. if (ret)
  483. return ret;
  484. kobject_uevent(&new_var->kobj, KOBJ_ADD);
  485. if (efivar_entry_add(new_var, &efivar_sysfs_list)) {
  486. efivar_unregister(new_var);
  487. return -EINTR;
  488. }
  489. return 0;
  490. }
  491. static int
  492. create_efivars_bin_attributes(void)
  493. {
  494. struct bin_attribute *attr;
  495. int error;
  496. /* new_var */
  497. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  498. if (!attr)
  499. return -ENOMEM;
  500. attr->attr.name = "new_var";
  501. attr->attr.mode = 0200;
  502. attr->write = efivar_create;
  503. efivars_new_var = attr;
  504. /* del_var */
  505. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  506. if (!attr) {
  507. error = -ENOMEM;
  508. goto out_free;
  509. }
  510. attr->attr.name = "del_var";
  511. attr->attr.mode = 0200;
  512. attr->write = efivar_delete;
  513. efivars_del_var = attr;
  514. sysfs_bin_attr_init(efivars_new_var);
  515. sysfs_bin_attr_init(efivars_del_var);
  516. /* Register */
  517. error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_new_var);
  518. if (error) {
  519. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  520. " due to error %d\n", error);
  521. goto out_free;
  522. }
  523. error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_del_var);
  524. if (error) {
  525. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  526. " due to error %d\n", error);
  527. sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
  528. goto out_free;
  529. }
  530. return 0;
  531. out_free:
  532. kfree(efivars_del_var);
  533. efivars_del_var = NULL;
  534. kfree(efivars_new_var);
  535. efivars_new_var = NULL;
  536. return error;
  537. }
  538. static int efivar_update_sysfs_entry(efi_char16_t *name, efi_guid_t vendor,
  539. unsigned long name_size, void *data)
  540. {
  541. struct efivar_entry *entry = data;
  542. if (efivar_entry_find(name, vendor, &efivar_sysfs_list, false))
  543. return 0;
  544. memcpy(entry->var.VariableName, name, name_size);
  545. memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
  546. return 1;
  547. }
  548. static void efivar_update_sysfs_entries(struct work_struct *work)
  549. {
  550. struct efivar_entry *entry;
  551. int err;
  552. /* Add new sysfs entries */
  553. while (1) {
  554. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  555. if (!entry)
  556. return;
  557. err = efivar_init(efivar_update_sysfs_entry, entry,
  558. false, &efivar_sysfs_list);
  559. if (!err)
  560. break;
  561. efivar_create_sysfs_entry(entry);
  562. }
  563. kfree(entry);
  564. }
  565. static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
  566. unsigned long name_size, void *data)
  567. {
  568. struct efivar_entry *entry;
  569. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  570. if (!entry)
  571. return -ENOMEM;
  572. memcpy(entry->var.VariableName, name, name_size);
  573. memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
  574. efivar_create_sysfs_entry(entry);
  575. return 0;
  576. }
  577. static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
  578. {
  579. int err = efivar_entry_remove(entry);
  580. if (err)
  581. return err;
  582. efivar_unregister(entry);
  583. return 0;
  584. }
  585. static void efivars_sysfs_exit(void)
  586. {
  587. /* Remove all entries and destroy */
  588. int err;
  589. err = __efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list,
  590. NULL, NULL);
  591. if (err) {
  592. pr_err("efivars: Failed to destroy sysfs entries\n");
  593. return;
  594. }
  595. if (efivars_new_var)
  596. sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
  597. if (efivars_del_var)
  598. sysfs_remove_bin_file(&efivars_kset->kobj, efivars_del_var);
  599. kfree(efivars_new_var);
  600. kfree(efivars_del_var);
  601. kset_unregister(efivars_kset);
  602. }
  603. int efivars_sysfs_init(void)
  604. {
  605. struct kobject *parent_kobj = efivars_kobject();
  606. int error = 0;
  607. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  608. return -ENODEV;
  609. /* No efivars has been registered yet */
  610. if (!parent_kobj)
  611. return 0;
  612. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  613. EFIVARS_DATE);
  614. efivars_kset = kset_create_and_add("vars", NULL, parent_kobj);
  615. if (!efivars_kset) {
  616. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  617. return -ENOMEM;
  618. }
  619. efivar_init(efivars_sysfs_callback, NULL, true, &efivar_sysfs_list);
  620. error = create_efivars_bin_attributes();
  621. if (error) {
  622. efivars_sysfs_exit();
  623. return error;
  624. }
  625. INIT_WORK(&efivar_work, efivar_update_sysfs_entries);
  626. return 0;
  627. }
  628. EXPORT_SYMBOL_GPL(efivars_sysfs_init);
  629. module_init(efivars_sysfs_init);
  630. module_exit(efivars_sysfs_exit);