efivars.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * EFI Variables - 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/capability.h>
  68. #include <linux/types.h>
  69. #include <linux/errno.h>
  70. #include <linux/init.h>
  71. #include <linux/mm.h>
  72. #include <linux/module.h>
  73. #include <linux/string.h>
  74. #include <linux/smp.h>
  75. #include <linux/efi.h>
  76. #include <linux/sysfs.h>
  77. #include <linux/kobject.h>
  78. #include <linux/device.h>
  79. #include <linux/slab.h>
  80. #include <linux/pstore.h>
  81. #include <asm/uaccess.h>
  82. #define EFIVARS_VERSION "0.08"
  83. #define EFIVARS_DATE "2004-May-17"
  84. MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
  85. MODULE_DESCRIPTION("sysfs interface to EFI Variables");
  86. MODULE_LICENSE("GPL");
  87. MODULE_VERSION(EFIVARS_VERSION);
  88. #define DUMP_NAME_LEN 52
  89. /*
  90. * The maximum size of VariableName + Data = 1024
  91. * Therefore, it's reasonable to save that much
  92. * space in each part of the structure,
  93. * and we use a page for reading/writing.
  94. */
  95. struct efi_variable {
  96. efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
  97. efi_guid_t VendorGuid;
  98. unsigned long DataSize;
  99. __u8 Data[1024];
  100. efi_status_t Status;
  101. __u32 Attributes;
  102. } __attribute__((packed));
  103. struct efivar_entry {
  104. struct efivars *efivars;
  105. struct efi_variable var;
  106. struct list_head list;
  107. struct kobject kobj;
  108. };
  109. struct efivar_attribute {
  110. struct attribute attr;
  111. ssize_t (*show) (struct efivar_entry *entry, char *buf);
  112. ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
  113. };
  114. #define EFIVAR_ATTR(_name, _mode, _show, _store) \
  115. struct efivar_attribute efivar_attr_##_name = { \
  116. .attr = {.name = __stringify(_name), .mode = _mode}, \
  117. .show = _show, \
  118. .store = _store, \
  119. };
  120. #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
  121. #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
  122. /*
  123. * Prototype for sysfs creation function
  124. */
  125. static int
  126. efivar_create_sysfs_entry(struct efivars *efivars,
  127. unsigned long variable_name_size,
  128. efi_char16_t *variable_name,
  129. efi_guid_t *vendor_guid);
  130. /* Return the number of unicode characters in data */
  131. static unsigned long
  132. utf8_strlen(efi_char16_t *data, unsigned long maxlength)
  133. {
  134. unsigned long length = 0;
  135. while (*data++ != 0 && length < maxlength)
  136. length++;
  137. return length;
  138. }
  139. /*
  140. * Return the number of bytes is the length of this string
  141. * Note: this is NOT the same as the number of unicode characters
  142. */
  143. static inline unsigned long
  144. utf8_strsize(efi_char16_t *data, unsigned long maxlength)
  145. {
  146. return utf8_strlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
  147. }
  148. static efi_status_t
  149. get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
  150. {
  151. efi_status_t status;
  152. var->DataSize = 1024;
  153. status = efivars->ops->get_variable(var->VariableName,
  154. &var->VendorGuid,
  155. &var->Attributes,
  156. &var->DataSize,
  157. var->Data);
  158. return status;
  159. }
  160. static efi_status_t
  161. get_var_data(struct efivars *efivars, struct efi_variable *var)
  162. {
  163. efi_status_t status;
  164. spin_lock(&efivars->lock);
  165. status = get_var_data_locked(efivars, var);
  166. spin_unlock(&efivars->lock);
  167. if (status != EFI_SUCCESS) {
  168. printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
  169. status);
  170. }
  171. return status;
  172. }
  173. static ssize_t
  174. efivar_guid_read(struct efivar_entry *entry, char *buf)
  175. {
  176. struct efi_variable *var = &entry->var;
  177. char *str = buf;
  178. if (!entry || !buf)
  179. return 0;
  180. efi_guid_unparse(&var->VendorGuid, str);
  181. str += strlen(str);
  182. str += sprintf(str, "\n");
  183. return str - buf;
  184. }
  185. static ssize_t
  186. efivar_attr_read(struct efivar_entry *entry, char *buf)
  187. {
  188. struct efi_variable *var = &entry->var;
  189. char *str = buf;
  190. efi_status_t status;
  191. if (!entry || !buf)
  192. return -EINVAL;
  193. status = get_var_data(entry->efivars, var);
  194. if (status != EFI_SUCCESS)
  195. return -EIO;
  196. if (var->Attributes & 0x1)
  197. str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
  198. if (var->Attributes & 0x2)
  199. str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
  200. if (var->Attributes & 0x4)
  201. str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
  202. return str - buf;
  203. }
  204. static ssize_t
  205. efivar_size_read(struct efivar_entry *entry, char *buf)
  206. {
  207. struct efi_variable *var = &entry->var;
  208. char *str = buf;
  209. efi_status_t status;
  210. if (!entry || !buf)
  211. return -EINVAL;
  212. status = get_var_data(entry->efivars, var);
  213. if (status != EFI_SUCCESS)
  214. return -EIO;
  215. str += sprintf(str, "0x%lx\n", var->DataSize);
  216. return str - buf;
  217. }
  218. static ssize_t
  219. efivar_data_read(struct efivar_entry *entry, char *buf)
  220. {
  221. struct efi_variable *var = &entry->var;
  222. efi_status_t status;
  223. if (!entry || !buf)
  224. return -EINVAL;
  225. status = get_var_data(entry->efivars, var);
  226. if (status != EFI_SUCCESS)
  227. return -EIO;
  228. memcpy(buf, var->Data, var->DataSize);
  229. return var->DataSize;
  230. }
  231. /*
  232. * We allow each variable to be edited via rewriting the
  233. * entire efi variable structure.
  234. */
  235. static ssize_t
  236. efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
  237. {
  238. struct efi_variable *new_var, *var = &entry->var;
  239. struct efivars *efivars = entry->efivars;
  240. efi_status_t status = EFI_NOT_FOUND;
  241. if (count != sizeof(struct efi_variable))
  242. return -EINVAL;
  243. new_var = (struct efi_variable *)buf;
  244. /*
  245. * If only updating the variable data, then the name
  246. * and guid should remain the same
  247. */
  248. if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
  249. efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
  250. printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
  251. return -EINVAL;
  252. }
  253. if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
  254. printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
  255. return -EINVAL;
  256. }
  257. spin_lock(&efivars->lock);
  258. status = efivars->ops->set_variable(new_var->VariableName,
  259. &new_var->VendorGuid,
  260. new_var->Attributes,
  261. new_var->DataSize,
  262. new_var->Data);
  263. spin_unlock(&efivars->lock);
  264. if (status != EFI_SUCCESS) {
  265. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  266. status);
  267. return -EIO;
  268. }
  269. memcpy(&entry->var, new_var, count);
  270. return count;
  271. }
  272. static ssize_t
  273. efivar_show_raw(struct efivar_entry *entry, char *buf)
  274. {
  275. struct efi_variable *var = &entry->var;
  276. efi_status_t status;
  277. if (!entry || !buf)
  278. return 0;
  279. status = get_var_data(entry->efivars, var);
  280. if (status != EFI_SUCCESS)
  281. return -EIO;
  282. memcpy(buf, var, sizeof(*var));
  283. return sizeof(*var);
  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 = container_of(kobj, struct 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 struct pstore_info efi_pstore_info;
  342. static inline void
  343. efivar_unregister(struct efivar_entry *var)
  344. {
  345. kobject_put(&var->kobj);
  346. }
  347. #ifdef CONFIG_PSTORE
  348. static int efi_pstore_open(struct pstore_info *psi)
  349. {
  350. struct efivars *efivars = psi->data;
  351. spin_lock(&efivars->lock);
  352. efivars->walk_entry = list_first_entry(&efivars->list,
  353. struct efivar_entry, list);
  354. return 0;
  355. }
  356. static int efi_pstore_close(struct pstore_info *psi)
  357. {
  358. struct efivars *efivars = psi->data;
  359. spin_unlock(&efivars->lock);
  360. return 0;
  361. }
  362. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  363. struct timespec *timespec, struct pstore_info *psi)
  364. {
  365. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  366. struct efivars *efivars = psi->data;
  367. char name[DUMP_NAME_LEN];
  368. int i;
  369. unsigned int part, size;
  370. unsigned long time;
  371. while (&efivars->walk_entry->list != &efivars->list) {
  372. if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
  373. vendor)) {
  374. for (i = 0; i < DUMP_NAME_LEN; i++) {
  375. name[i] = efivars->walk_entry->var.VariableName[i];
  376. }
  377. if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
  378. *id = part;
  379. timespec->tv_sec = time;
  380. timespec->tv_nsec = 0;
  381. get_var_data_locked(efivars, &efivars->walk_entry->var);
  382. size = efivars->walk_entry->var.DataSize;
  383. memcpy(psi->buf, efivars->walk_entry->var.Data, size);
  384. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  385. struct efivar_entry, list);
  386. return size;
  387. }
  388. }
  389. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  390. struct efivar_entry, list);
  391. }
  392. return 0;
  393. }
  394. static u64 efi_pstore_write(enum pstore_type_id type, unsigned int part,
  395. size_t size, struct pstore_info *psi)
  396. {
  397. char name[DUMP_NAME_LEN];
  398. char stub_name[DUMP_NAME_LEN];
  399. efi_char16_t efi_name[DUMP_NAME_LEN];
  400. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  401. struct efivars *efivars = psi->data;
  402. struct efivar_entry *entry, *found = NULL;
  403. int i;
  404. sprintf(stub_name, "dump-type%u-%u-", type, part);
  405. sprintf(name, "%s%lu", stub_name, get_seconds());
  406. spin_lock(&efivars->lock);
  407. for (i = 0; i < DUMP_NAME_LEN; i++)
  408. efi_name[i] = stub_name[i];
  409. /*
  410. * Clean up any entries with the same name
  411. */
  412. list_for_each_entry(entry, &efivars->list, list) {
  413. get_var_data_locked(efivars, &entry->var);
  414. for (i = 0; i < DUMP_NAME_LEN; i++) {
  415. if (efi_name[i] == 0) {
  416. found = entry;
  417. efivars->ops->set_variable(entry->var.VariableName,
  418. &entry->var.VendorGuid,
  419. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
  420. 0, NULL);
  421. break;
  422. } else if (efi_name[i] != entry->var.VariableName[i]) {
  423. break;
  424. }
  425. }
  426. }
  427. if (found)
  428. list_del(&found->list);
  429. for (i = 0; i < DUMP_NAME_LEN; i++)
  430. efi_name[i] = name[i];
  431. efivars->ops->set_variable(efi_name, &vendor,
  432. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
  433. size, psi->buf);
  434. spin_unlock(&efivars->lock);
  435. if (found)
  436. efivar_unregister(found);
  437. if (size)
  438. efivar_create_sysfs_entry(efivars, utf8_strsize(efi_name, DUMP_NAME_LEN * 2),
  439. efi_name, &vendor);
  440. return part;
  441. };
  442. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  443. struct pstore_info *psi)
  444. {
  445. efi_pstore_write(type, id, 0, psi);
  446. return 0;
  447. }
  448. #else
  449. static int efi_pstore_open(struct pstore_info *psi)
  450. {
  451. return 0;
  452. }
  453. static int efi_pstore_close(struct pstore_info *psi)
  454. {
  455. return 0;
  456. }
  457. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  458. struct timespec *time, struct pstore_info *psi)
  459. {
  460. return -1;
  461. }
  462. static u64 efi_pstore_write(enum pstore_type_id type, int part, size_t size,
  463. struct pstore_info *psi)
  464. {
  465. return 0;
  466. }
  467. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  468. struct pstore_info *psi)
  469. {
  470. return 0;
  471. }
  472. #endif
  473. static struct pstore_info efi_pstore_info = {
  474. .owner = THIS_MODULE,
  475. .name = "efi",
  476. .open = efi_pstore_open,
  477. .close = efi_pstore_close,
  478. .read = efi_pstore_read,
  479. .write = efi_pstore_write,
  480. .erase = efi_pstore_erase,
  481. };
  482. static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
  483. struct bin_attribute *bin_attr,
  484. char *buf, loff_t pos, size_t count)
  485. {
  486. struct efi_variable *new_var = (struct efi_variable *)buf;
  487. struct efivars *efivars = bin_attr->private;
  488. struct efivar_entry *search_efivar, *n;
  489. unsigned long strsize1, strsize2;
  490. efi_status_t status = EFI_NOT_FOUND;
  491. int found = 0;
  492. if (!capable(CAP_SYS_ADMIN))
  493. return -EACCES;
  494. spin_lock(&efivars->lock);
  495. /*
  496. * Does this variable already exist?
  497. */
  498. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  499. strsize1 = utf8_strsize(search_efivar->var.VariableName, 1024);
  500. strsize2 = utf8_strsize(new_var->VariableName, 1024);
  501. if (strsize1 == strsize2 &&
  502. !memcmp(&(search_efivar->var.VariableName),
  503. new_var->VariableName, strsize1) &&
  504. !efi_guidcmp(search_efivar->var.VendorGuid,
  505. new_var->VendorGuid)) {
  506. found = 1;
  507. break;
  508. }
  509. }
  510. if (found) {
  511. spin_unlock(&efivars->lock);
  512. return -EINVAL;
  513. }
  514. /* now *really* create the variable via EFI */
  515. status = efivars->ops->set_variable(new_var->VariableName,
  516. &new_var->VendorGuid,
  517. new_var->Attributes,
  518. new_var->DataSize,
  519. new_var->Data);
  520. if (status != EFI_SUCCESS) {
  521. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  522. status);
  523. spin_unlock(&efivars->lock);
  524. return -EIO;
  525. }
  526. spin_unlock(&efivars->lock);
  527. /* Create the entry in sysfs. Locking is not required here */
  528. status = efivar_create_sysfs_entry(efivars,
  529. utf8_strsize(new_var->VariableName,
  530. 1024),
  531. new_var->VariableName,
  532. &new_var->VendorGuid);
  533. if (status) {
  534. printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
  535. }
  536. return count;
  537. }
  538. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  539. struct bin_attribute *bin_attr,
  540. char *buf, loff_t pos, size_t count)
  541. {
  542. struct efi_variable *del_var = (struct efi_variable *)buf;
  543. struct efivars *efivars = bin_attr->private;
  544. struct efivar_entry *search_efivar, *n;
  545. unsigned long strsize1, strsize2;
  546. efi_status_t status = EFI_NOT_FOUND;
  547. int found = 0;
  548. if (!capable(CAP_SYS_ADMIN))
  549. return -EACCES;
  550. spin_lock(&efivars->lock);
  551. /*
  552. * Does this variable already exist?
  553. */
  554. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  555. strsize1 = utf8_strsize(search_efivar->var.VariableName, 1024);
  556. strsize2 = utf8_strsize(del_var->VariableName, 1024);
  557. if (strsize1 == strsize2 &&
  558. !memcmp(&(search_efivar->var.VariableName),
  559. del_var->VariableName, strsize1) &&
  560. !efi_guidcmp(search_efivar->var.VendorGuid,
  561. del_var->VendorGuid)) {
  562. found = 1;
  563. break;
  564. }
  565. }
  566. if (!found) {
  567. spin_unlock(&efivars->lock);
  568. return -EINVAL;
  569. }
  570. /* force the Attributes/DataSize to 0 to ensure deletion */
  571. del_var->Attributes = 0;
  572. del_var->DataSize = 0;
  573. status = efivars->ops->set_variable(del_var->VariableName,
  574. &del_var->VendorGuid,
  575. del_var->Attributes,
  576. del_var->DataSize,
  577. del_var->Data);
  578. if (status != EFI_SUCCESS) {
  579. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  580. status);
  581. spin_unlock(&efivars->lock);
  582. return -EIO;
  583. }
  584. list_del(&search_efivar->list);
  585. /* We need to release this lock before unregistering. */
  586. spin_unlock(&efivars->lock);
  587. efivar_unregister(search_efivar);
  588. /* It's dead Jim.... */
  589. return count;
  590. }
  591. /*
  592. * Let's not leave out systab information that snuck into
  593. * the efivars driver
  594. */
  595. static ssize_t systab_show(struct kobject *kobj,
  596. struct kobj_attribute *attr, char *buf)
  597. {
  598. char *str = buf;
  599. if (!kobj || !buf)
  600. return -EINVAL;
  601. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  602. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  603. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  604. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  605. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  606. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  607. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  608. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  609. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  610. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  611. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  612. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  613. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  614. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  615. return str - buf;
  616. }
  617. static struct kobj_attribute efi_attr_systab =
  618. __ATTR(systab, 0400, systab_show, NULL);
  619. static struct attribute *efi_subsys_attrs[] = {
  620. &efi_attr_systab.attr,
  621. NULL, /* maybe more in the future? */
  622. };
  623. static struct attribute_group efi_subsys_attr_group = {
  624. .attrs = efi_subsys_attrs,
  625. };
  626. static struct kobject *efi_kobj;
  627. /*
  628. * efivar_create_sysfs_entry()
  629. * Requires:
  630. * variable_name_size = number of bytes required to hold
  631. * variable_name (not counting the NULL
  632. * character at the end.
  633. * efivars->lock is not held on entry or exit.
  634. * Returns 1 on failure, 0 on success
  635. */
  636. static int
  637. efivar_create_sysfs_entry(struct efivars *efivars,
  638. unsigned long variable_name_size,
  639. efi_char16_t *variable_name,
  640. efi_guid_t *vendor_guid)
  641. {
  642. int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
  643. char *short_name;
  644. struct efivar_entry *new_efivar;
  645. short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
  646. new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  647. if (!short_name || !new_efivar) {
  648. kfree(short_name);
  649. kfree(new_efivar);
  650. return 1;
  651. }
  652. new_efivar->efivars = efivars;
  653. memcpy(new_efivar->var.VariableName, variable_name,
  654. variable_name_size);
  655. memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
  656. /* Convert Unicode to normal chars (assume top bits are 0),
  657. ala UTF-8 */
  658. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  659. short_name[i] = variable_name[i] & 0xFF;
  660. }
  661. /* This is ugly, but necessary to separate one vendor's
  662. private variables from another's. */
  663. *(short_name + strlen(short_name)) = '-';
  664. efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
  665. new_efivar->kobj.kset = efivars->kset;
  666. i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
  667. "%s", short_name);
  668. if (i) {
  669. kfree(short_name);
  670. kfree(new_efivar);
  671. return 1;
  672. }
  673. kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
  674. kfree(short_name);
  675. short_name = NULL;
  676. spin_lock(&efivars->lock);
  677. list_add(&new_efivar->list, &efivars->list);
  678. spin_unlock(&efivars->lock);
  679. return 0;
  680. }
  681. static int
  682. create_efivars_bin_attributes(struct efivars *efivars)
  683. {
  684. struct bin_attribute *attr;
  685. int error;
  686. /* new_var */
  687. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  688. if (!attr)
  689. return -ENOMEM;
  690. attr->attr.name = "new_var";
  691. attr->attr.mode = 0200;
  692. attr->write = efivar_create;
  693. attr->private = efivars;
  694. efivars->new_var = attr;
  695. /* del_var */
  696. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  697. if (!attr) {
  698. error = -ENOMEM;
  699. goto out_free;
  700. }
  701. attr->attr.name = "del_var";
  702. attr->attr.mode = 0200;
  703. attr->write = efivar_delete;
  704. attr->private = efivars;
  705. efivars->del_var = attr;
  706. sysfs_bin_attr_init(efivars->new_var);
  707. sysfs_bin_attr_init(efivars->del_var);
  708. /* Register */
  709. error = sysfs_create_bin_file(&efivars->kset->kobj,
  710. efivars->new_var);
  711. if (error) {
  712. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  713. " due to error %d\n", error);
  714. goto out_free;
  715. }
  716. error = sysfs_create_bin_file(&efivars->kset->kobj,
  717. efivars->del_var);
  718. if (error) {
  719. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  720. " due to error %d\n", error);
  721. sysfs_remove_bin_file(&efivars->kset->kobj,
  722. efivars->new_var);
  723. goto out_free;
  724. }
  725. return 0;
  726. out_free:
  727. kfree(efivars->del_var);
  728. efivars->del_var = NULL;
  729. kfree(efivars->new_var);
  730. efivars->new_var = NULL;
  731. return error;
  732. }
  733. void unregister_efivars(struct efivars *efivars)
  734. {
  735. struct efivar_entry *entry, *n;
  736. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  737. spin_lock(&efivars->lock);
  738. list_del(&entry->list);
  739. spin_unlock(&efivars->lock);
  740. efivar_unregister(entry);
  741. }
  742. if (efivars->new_var)
  743. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
  744. if (efivars->del_var)
  745. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
  746. kfree(efivars->new_var);
  747. kfree(efivars->del_var);
  748. kset_unregister(efivars->kset);
  749. }
  750. EXPORT_SYMBOL_GPL(unregister_efivars);
  751. int register_efivars(struct efivars *efivars,
  752. const struct efivar_operations *ops,
  753. struct kobject *parent_kobj)
  754. {
  755. efi_status_t status = EFI_NOT_FOUND;
  756. efi_guid_t vendor_guid;
  757. efi_char16_t *variable_name;
  758. unsigned long variable_name_size = 1024;
  759. int error = 0;
  760. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  761. if (!variable_name) {
  762. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  763. return -ENOMEM;
  764. }
  765. spin_lock_init(&efivars->lock);
  766. INIT_LIST_HEAD(&efivars->list);
  767. efivars->ops = ops;
  768. efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
  769. if (!efivars->kset) {
  770. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  771. error = -ENOMEM;
  772. goto out;
  773. }
  774. /*
  775. * Per EFI spec, the maximum storage allocated for both
  776. * the variable name and variable data is 1024 bytes.
  777. */
  778. do {
  779. variable_name_size = 1024;
  780. status = ops->get_next_variable(&variable_name_size,
  781. variable_name,
  782. &vendor_guid);
  783. switch (status) {
  784. case EFI_SUCCESS:
  785. efivar_create_sysfs_entry(efivars,
  786. variable_name_size,
  787. variable_name,
  788. &vendor_guid);
  789. break;
  790. case EFI_NOT_FOUND:
  791. break;
  792. default:
  793. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  794. status);
  795. status = EFI_NOT_FOUND;
  796. break;
  797. }
  798. } while (status != EFI_NOT_FOUND);
  799. error = create_efivars_bin_attributes(efivars);
  800. if (error)
  801. unregister_efivars(efivars);
  802. efivars->efi_pstore_info = efi_pstore_info;
  803. efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  804. if (efivars->efi_pstore_info.buf) {
  805. efivars->efi_pstore_info.bufsize = 1024;
  806. efivars->efi_pstore_info.data = efivars;
  807. mutex_init(&efivars->efi_pstore_info.buf_mutex);
  808. pstore_register(&efivars->efi_pstore_info);
  809. }
  810. out:
  811. kfree(variable_name);
  812. return error;
  813. }
  814. EXPORT_SYMBOL_GPL(register_efivars);
  815. static struct efivars __efivars;
  816. static struct efivar_operations ops;
  817. /*
  818. * For now we register the efi subsystem with the firmware subsystem
  819. * and the vars subsystem with the efi subsystem. In the future, it
  820. * might make sense to split off the efi subsystem into its own
  821. * driver, but for now only efivars will register with it, so just
  822. * include it here.
  823. */
  824. static int __init
  825. efivars_init(void)
  826. {
  827. int error = 0;
  828. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  829. EFIVARS_DATE);
  830. if (!efi_enabled)
  831. return 0;
  832. /* For now we'll register the efi directory at /sys/firmware/efi */
  833. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  834. if (!efi_kobj) {
  835. printk(KERN_ERR "efivars: Firmware registration failed.\n");
  836. return -ENOMEM;
  837. }
  838. ops.get_variable = efi.get_variable;
  839. ops.set_variable = efi.set_variable;
  840. ops.get_next_variable = efi.get_next_variable;
  841. error = register_efivars(&__efivars, &ops, efi_kobj);
  842. if (error)
  843. goto err_put;
  844. /* Don't forget the systab entry */
  845. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  846. if (error) {
  847. printk(KERN_ERR
  848. "efivars: Sysfs attribute export failed with error %d.\n",
  849. error);
  850. goto err_unregister;
  851. }
  852. return 0;
  853. err_unregister:
  854. unregister_efivars(&__efivars);
  855. err_put:
  856. kobject_put(efi_kobj);
  857. return error;
  858. }
  859. static void __exit
  860. efivars_exit(void)
  861. {
  862. if (efi_enabled) {
  863. unregister_efivars(&__efivars);
  864. kobject_put(efi_kobj);
  865. }
  866. }
  867. module_init(efivars_init);
  868. module_exit(efivars_exit);