efivars.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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,
  390. char **buf, struct pstore_info *psi)
  391. {
  392. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  393. struct efivars *efivars = psi->data;
  394. char name[DUMP_NAME_LEN];
  395. int i;
  396. unsigned int part, size;
  397. unsigned long time;
  398. while (&efivars->walk_entry->list != &efivars->list) {
  399. if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
  400. vendor)) {
  401. for (i = 0; i < DUMP_NAME_LEN; i++) {
  402. name[i] = efivars->walk_entry->var.VariableName[i];
  403. }
  404. if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
  405. *id = part;
  406. timespec->tv_sec = time;
  407. timespec->tv_nsec = 0;
  408. get_var_data_locked(efivars, &efivars->walk_entry->var);
  409. size = efivars->walk_entry->var.DataSize;
  410. *buf = kmalloc(size, GFP_KERNEL);
  411. if (*buf == NULL)
  412. return -ENOMEM;
  413. memcpy(*buf, efivars->walk_entry->var.Data,
  414. size);
  415. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  416. struct efivar_entry, list);
  417. return size;
  418. }
  419. }
  420. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  421. struct efivar_entry, list);
  422. }
  423. return 0;
  424. }
  425. static int efi_pstore_write(enum pstore_type_id type, u64 *id,
  426. unsigned int part, size_t size, struct pstore_info *psi)
  427. {
  428. char name[DUMP_NAME_LEN];
  429. char stub_name[DUMP_NAME_LEN];
  430. efi_char16_t efi_name[DUMP_NAME_LEN];
  431. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  432. struct efivars *efivars = psi->data;
  433. struct efivar_entry *entry, *found = NULL;
  434. int i, ret = 0;
  435. sprintf(stub_name, "dump-type%u-%u-", type, part);
  436. sprintf(name, "%s%lu", stub_name, get_seconds());
  437. spin_lock(&efivars->lock);
  438. for (i = 0; i < DUMP_NAME_LEN; i++)
  439. efi_name[i] = stub_name[i];
  440. /*
  441. * Clean up any entries with the same name
  442. */
  443. list_for_each_entry(entry, &efivars->list, list) {
  444. get_var_data_locked(efivars, &entry->var);
  445. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  446. continue;
  447. if (utf16_strncmp(entry->var.VariableName, efi_name,
  448. utf16_strlen(efi_name)))
  449. continue;
  450. /* Needs to be a prefix */
  451. if (entry->var.VariableName[utf16_strlen(efi_name)] == 0)
  452. continue;
  453. /* found */
  454. found = entry;
  455. efivars->ops->set_variable(entry->var.VariableName,
  456. &entry->var.VendorGuid,
  457. PSTORE_EFI_ATTRIBUTES,
  458. 0, NULL);
  459. }
  460. if (found)
  461. list_del(&found->list);
  462. for (i = 0; i < DUMP_NAME_LEN; i++)
  463. efi_name[i] = name[i];
  464. efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
  465. size, psi->buf);
  466. spin_unlock(&efivars->lock);
  467. if (found)
  468. efivar_unregister(found);
  469. if (size)
  470. ret = efivar_create_sysfs_entry(efivars,
  471. utf16_strsize(efi_name,
  472. DUMP_NAME_LEN * 2),
  473. efi_name, &vendor);
  474. *id = part;
  475. return ret;
  476. };
  477. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  478. struct pstore_info *psi)
  479. {
  480. efi_pstore_write(type, &id, (unsigned int)id, 0, psi);
  481. return 0;
  482. }
  483. #else
  484. static int efi_pstore_open(struct pstore_info *psi)
  485. {
  486. return 0;
  487. }
  488. static int efi_pstore_close(struct pstore_info *psi)
  489. {
  490. return 0;
  491. }
  492. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  493. struct timespec *time, struct pstore_info *psi)
  494. {
  495. return -1;
  496. }
  497. static int efi_pstore_write(enum pstore_type_id type, u64 *id,
  498. unsigned int part, size_t size, struct pstore_info *psi)
  499. {
  500. return 0;
  501. }
  502. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  503. struct pstore_info *psi)
  504. {
  505. return 0;
  506. }
  507. #endif
  508. static struct pstore_info efi_pstore_info = {
  509. .owner = THIS_MODULE,
  510. .name = "efi",
  511. .open = efi_pstore_open,
  512. .close = efi_pstore_close,
  513. .read = efi_pstore_read,
  514. .write = efi_pstore_write,
  515. .erase = efi_pstore_erase,
  516. };
  517. static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
  518. struct bin_attribute *bin_attr,
  519. char *buf, loff_t pos, size_t count)
  520. {
  521. struct efi_variable *new_var = (struct efi_variable *)buf;
  522. struct efivars *efivars = bin_attr->private;
  523. struct efivar_entry *search_efivar, *n;
  524. unsigned long strsize1, strsize2;
  525. efi_status_t status = EFI_NOT_FOUND;
  526. int found = 0;
  527. if (!capable(CAP_SYS_ADMIN))
  528. return -EACCES;
  529. spin_lock(&efivars->lock);
  530. /*
  531. * Does this variable already exist?
  532. */
  533. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  534. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  535. strsize2 = utf16_strsize(new_var->VariableName, 1024);
  536. if (strsize1 == strsize2 &&
  537. !memcmp(&(search_efivar->var.VariableName),
  538. new_var->VariableName, strsize1) &&
  539. !efi_guidcmp(search_efivar->var.VendorGuid,
  540. new_var->VendorGuid)) {
  541. found = 1;
  542. break;
  543. }
  544. }
  545. if (found) {
  546. spin_unlock(&efivars->lock);
  547. return -EINVAL;
  548. }
  549. /* now *really* create the variable via EFI */
  550. status = efivars->ops->set_variable(new_var->VariableName,
  551. &new_var->VendorGuid,
  552. new_var->Attributes,
  553. new_var->DataSize,
  554. new_var->Data);
  555. if (status != EFI_SUCCESS) {
  556. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  557. status);
  558. spin_unlock(&efivars->lock);
  559. return -EIO;
  560. }
  561. spin_unlock(&efivars->lock);
  562. /* Create the entry in sysfs. Locking is not required here */
  563. status = efivar_create_sysfs_entry(efivars,
  564. utf16_strsize(new_var->VariableName,
  565. 1024),
  566. new_var->VariableName,
  567. &new_var->VendorGuid);
  568. if (status) {
  569. printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
  570. }
  571. return count;
  572. }
  573. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  574. struct bin_attribute *bin_attr,
  575. char *buf, loff_t pos, size_t count)
  576. {
  577. struct efi_variable *del_var = (struct efi_variable *)buf;
  578. struct efivars *efivars = bin_attr->private;
  579. struct efivar_entry *search_efivar, *n;
  580. unsigned long strsize1, strsize2;
  581. efi_status_t status = EFI_NOT_FOUND;
  582. int found = 0;
  583. if (!capable(CAP_SYS_ADMIN))
  584. return -EACCES;
  585. spin_lock(&efivars->lock);
  586. /*
  587. * Does this variable already exist?
  588. */
  589. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  590. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  591. strsize2 = utf16_strsize(del_var->VariableName, 1024);
  592. if (strsize1 == strsize2 &&
  593. !memcmp(&(search_efivar->var.VariableName),
  594. del_var->VariableName, strsize1) &&
  595. !efi_guidcmp(search_efivar->var.VendorGuid,
  596. del_var->VendorGuid)) {
  597. found = 1;
  598. break;
  599. }
  600. }
  601. if (!found) {
  602. spin_unlock(&efivars->lock);
  603. return -EINVAL;
  604. }
  605. /* force the Attributes/DataSize to 0 to ensure deletion */
  606. del_var->Attributes = 0;
  607. del_var->DataSize = 0;
  608. status = efivars->ops->set_variable(del_var->VariableName,
  609. &del_var->VendorGuid,
  610. del_var->Attributes,
  611. del_var->DataSize,
  612. del_var->Data);
  613. if (status != EFI_SUCCESS) {
  614. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  615. status);
  616. spin_unlock(&efivars->lock);
  617. return -EIO;
  618. }
  619. list_del(&search_efivar->list);
  620. /* We need to release this lock before unregistering. */
  621. spin_unlock(&efivars->lock);
  622. efivar_unregister(search_efivar);
  623. /* It's dead Jim.... */
  624. return count;
  625. }
  626. /*
  627. * Let's not leave out systab information that snuck into
  628. * the efivars driver
  629. */
  630. static ssize_t systab_show(struct kobject *kobj,
  631. struct kobj_attribute *attr, char *buf)
  632. {
  633. char *str = buf;
  634. if (!kobj || !buf)
  635. return -EINVAL;
  636. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  637. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  638. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  639. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  640. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  641. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  642. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  643. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  644. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  645. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  646. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  647. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  648. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  649. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  650. return str - buf;
  651. }
  652. static struct kobj_attribute efi_attr_systab =
  653. __ATTR(systab, 0400, systab_show, NULL);
  654. static struct attribute *efi_subsys_attrs[] = {
  655. &efi_attr_systab.attr,
  656. NULL, /* maybe more in the future? */
  657. };
  658. static struct attribute_group efi_subsys_attr_group = {
  659. .attrs = efi_subsys_attrs,
  660. };
  661. static struct kobject *efi_kobj;
  662. /*
  663. * efivar_create_sysfs_entry()
  664. * Requires:
  665. * variable_name_size = number of bytes required to hold
  666. * variable_name (not counting the NULL
  667. * character at the end.
  668. * efivars->lock is not held on entry or exit.
  669. * Returns 1 on failure, 0 on success
  670. */
  671. static int
  672. efivar_create_sysfs_entry(struct efivars *efivars,
  673. unsigned long variable_name_size,
  674. efi_char16_t *variable_name,
  675. efi_guid_t *vendor_guid)
  676. {
  677. int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
  678. char *short_name;
  679. struct efivar_entry *new_efivar;
  680. short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
  681. new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  682. if (!short_name || !new_efivar) {
  683. kfree(short_name);
  684. kfree(new_efivar);
  685. return 1;
  686. }
  687. new_efivar->efivars = efivars;
  688. memcpy(new_efivar->var.VariableName, variable_name,
  689. variable_name_size);
  690. memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
  691. /* Convert Unicode to normal chars (assume top bits are 0),
  692. ala UTF-8 */
  693. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  694. short_name[i] = variable_name[i] & 0xFF;
  695. }
  696. /* This is ugly, but necessary to separate one vendor's
  697. private variables from another's. */
  698. *(short_name + strlen(short_name)) = '-';
  699. efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
  700. new_efivar->kobj.kset = efivars->kset;
  701. i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
  702. "%s", short_name);
  703. if (i) {
  704. kfree(short_name);
  705. kfree(new_efivar);
  706. return 1;
  707. }
  708. kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
  709. kfree(short_name);
  710. short_name = NULL;
  711. spin_lock(&efivars->lock);
  712. list_add(&new_efivar->list, &efivars->list);
  713. spin_unlock(&efivars->lock);
  714. return 0;
  715. }
  716. static int
  717. create_efivars_bin_attributes(struct efivars *efivars)
  718. {
  719. struct bin_attribute *attr;
  720. int error;
  721. /* new_var */
  722. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  723. if (!attr)
  724. return -ENOMEM;
  725. attr->attr.name = "new_var";
  726. attr->attr.mode = 0200;
  727. attr->write = efivar_create;
  728. attr->private = efivars;
  729. efivars->new_var = attr;
  730. /* del_var */
  731. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  732. if (!attr) {
  733. error = -ENOMEM;
  734. goto out_free;
  735. }
  736. attr->attr.name = "del_var";
  737. attr->attr.mode = 0200;
  738. attr->write = efivar_delete;
  739. attr->private = efivars;
  740. efivars->del_var = attr;
  741. sysfs_bin_attr_init(efivars->new_var);
  742. sysfs_bin_attr_init(efivars->del_var);
  743. /* Register */
  744. error = sysfs_create_bin_file(&efivars->kset->kobj,
  745. efivars->new_var);
  746. if (error) {
  747. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  748. " due to error %d\n", error);
  749. goto out_free;
  750. }
  751. error = sysfs_create_bin_file(&efivars->kset->kobj,
  752. efivars->del_var);
  753. if (error) {
  754. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  755. " due to error %d\n", error);
  756. sysfs_remove_bin_file(&efivars->kset->kobj,
  757. efivars->new_var);
  758. goto out_free;
  759. }
  760. return 0;
  761. out_free:
  762. kfree(efivars->del_var);
  763. efivars->del_var = NULL;
  764. kfree(efivars->new_var);
  765. efivars->new_var = NULL;
  766. return error;
  767. }
  768. void unregister_efivars(struct efivars *efivars)
  769. {
  770. struct efivar_entry *entry, *n;
  771. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  772. spin_lock(&efivars->lock);
  773. list_del(&entry->list);
  774. spin_unlock(&efivars->lock);
  775. efivar_unregister(entry);
  776. }
  777. if (efivars->new_var)
  778. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
  779. if (efivars->del_var)
  780. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
  781. kfree(efivars->new_var);
  782. kfree(efivars->del_var);
  783. kset_unregister(efivars->kset);
  784. }
  785. EXPORT_SYMBOL_GPL(unregister_efivars);
  786. int register_efivars(struct efivars *efivars,
  787. const struct efivar_operations *ops,
  788. struct kobject *parent_kobj)
  789. {
  790. efi_status_t status = EFI_NOT_FOUND;
  791. efi_guid_t vendor_guid;
  792. efi_char16_t *variable_name;
  793. unsigned long variable_name_size = 1024;
  794. int error = 0;
  795. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  796. if (!variable_name) {
  797. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  798. return -ENOMEM;
  799. }
  800. spin_lock_init(&efivars->lock);
  801. INIT_LIST_HEAD(&efivars->list);
  802. efivars->ops = ops;
  803. efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
  804. if (!efivars->kset) {
  805. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  806. error = -ENOMEM;
  807. goto out;
  808. }
  809. /*
  810. * Per EFI spec, the maximum storage allocated for both
  811. * the variable name and variable data is 1024 bytes.
  812. */
  813. do {
  814. variable_name_size = 1024;
  815. status = ops->get_next_variable(&variable_name_size,
  816. variable_name,
  817. &vendor_guid);
  818. switch (status) {
  819. case EFI_SUCCESS:
  820. efivar_create_sysfs_entry(efivars,
  821. variable_name_size,
  822. variable_name,
  823. &vendor_guid);
  824. break;
  825. case EFI_NOT_FOUND:
  826. break;
  827. default:
  828. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  829. status);
  830. status = EFI_NOT_FOUND;
  831. break;
  832. }
  833. } while (status != EFI_NOT_FOUND);
  834. error = create_efivars_bin_attributes(efivars);
  835. if (error)
  836. unregister_efivars(efivars);
  837. efivars->efi_pstore_info = efi_pstore_info;
  838. efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  839. if (efivars->efi_pstore_info.buf) {
  840. efivars->efi_pstore_info.bufsize = 1024;
  841. efivars->efi_pstore_info.data = efivars;
  842. spin_lock_init(&efivars->efi_pstore_info.buf_lock);
  843. pstore_register(&efivars->efi_pstore_info);
  844. }
  845. out:
  846. kfree(variable_name);
  847. return error;
  848. }
  849. EXPORT_SYMBOL_GPL(register_efivars);
  850. static struct efivars __efivars;
  851. static struct efivar_operations ops;
  852. /*
  853. * For now we register the efi subsystem with the firmware subsystem
  854. * and the vars subsystem with the efi subsystem. In the future, it
  855. * might make sense to split off the efi subsystem into its own
  856. * driver, but for now only efivars will register with it, so just
  857. * include it here.
  858. */
  859. static int __init
  860. efivars_init(void)
  861. {
  862. int error = 0;
  863. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  864. EFIVARS_DATE);
  865. if (!efi_enabled)
  866. return 0;
  867. /* For now we'll register the efi directory at /sys/firmware/efi */
  868. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  869. if (!efi_kobj) {
  870. printk(KERN_ERR "efivars: Firmware registration failed.\n");
  871. return -ENOMEM;
  872. }
  873. ops.get_variable = efi.get_variable;
  874. ops.set_variable = efi.set_variable;
  875. ops.get_next_variable = efi.get_next_variable;
  876. error = register_efivars(&__efivars, &ops, efi_kobj);
  877. if (error)
  878. goto err_put;
  879. /* Don't forget the systab entry */
  880. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  881. if (error) {
  882. printk(KERN_ERR
  883. "efivars: Sysfs attribute export failed with error %d.\n",
  884. error);
  885. goto err_unregister;
  886. }
  887. return 0;
  888. err_unregister:
  889. unregister_efivars(&__efivars);
  890. err_put:
  891. kobject_put(efi_kobj);
  892. return error;
  893. }
  894. static void __exit
  895. efivars_exit(void)
  896. {
  897. if (efi_enabled) {
  898. unregister_efivars(&__efivars);
  899. kobject_put(efi_kobj);
  900. }
  901. }
  902. module_init(efivars_init);
  903. module_exit(efivars_exit);