efivars.c 26 KB

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