efivars.c 26 KB

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