efivars.c 16 KB

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