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