efivars.c 16 KB

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