efivars.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  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 bool
  175. validate_device_path(struct efi_variable *var, int match, u8 *buffer,
  176. unsigned long len)
  177. {
  178. struct efi_generic_dev_path *node;
  179. int offset = 0;
  180. node = (struct efi_generic_dev_path *)buffer;
  181. if (len < sizeof(*node))
  182. return false;
  183. while (offset <= len - sizeof(*node) &&
  184. node->length >= sizeof(*node) &&
  185. node->length <= len - offset) {
  186. offset += node->length;
  187. if ((node->type == EFI_DEV_END_PATH ||
  188. node->type == EFI_DEV_END_PATH2) &&
  189. node->sub_type == EFI_DEV_END_ENTIRE)
  190. return true;
  191. node = (struct efi_generic_dev_path *)(buffer + offset);
  192. }
  193. /*
  194. * If we're here then either node->length pointed past the end
  195. * of the buffer or we reached the end of the buffer without
  196. * finding a device path end node.
  197. */
  198. return false;
  199. }
  200. static bool
  201. validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
  202. unsigned long len)
  203. {
  204. /* An array of 16-bit integers */
  205. if ((len % 2) != 0)
  206. return false;
  207. return true;
  208. }
  209. static bool
  210. validate_load_option(struct efi_variable *var, int match, u8 *buffer,
  211. unsigned long len)
  212. {
  213. u16 filepathlength;
  214. int i, desclength = 0, namelen;
  215. namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
  216. /* Either "Boot" or "Driver" followed by four digits of hex */
  217. for (i = match; i < match+4; i++) {
  218. if (var->VariableName[i] > 127 ||
  219. hex_to_bin(var->VariableName[i] & 0xff) < 0)
  220. return true;
  221. }
  222. /* Reject it if there's 4 digits of hex and then further content */
  223. if (namelen > match + 4)
  224. return false;
  225. /* A valid entry must be at least 8 bytes */
  226. if (len < 8)
  227. return false;
  228. filepathlength = buffer[4] | buffer[5] << 8;
  229. /*
  230. * There's no stored length for the description, so it has to be
  231. * found by hand
  232. */
  233. desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
  234. /* Each boot entry must have a descriptor */
  235. if (!desclength)
  236. return false;
  237. /*
  238. * If the sum of the length of the description, the claimed filepath
  239. * length and the original header are greater than the length of the
  240. * variable, it's malformed
  241. */
  242. if ((desclength + filepathlength + 6) > len)
  243. return false;
  244. /*
  245. * And, finally, check the filepath
  246. */
  247. return validate_device_path(var, match, buffer + desclength + 6,
  248. filepathlength);
  249. }
  250. static bool
  251. validate_uint16(struct efi_variable *var, int match, u8 *buffer,
  252. unsigned long len)
  253. {
  254. /* A single 16-bit integer */
  255. if (len != 2)
  256. return false;
  257. return true;
  258. }
  259. static bool
  260. validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
  261. unsigned long len)
  262. {
  263. int i;
  264. for (i = 0; i < len; i++) {
  265. if (buffer[i] > 127)
  266. return false;
  267. if (buffer[i] == 0)
  268. return true;
  269. }
  270. return false;
  271. }
  272. struct variable_validate {
  273. char *name;
  274. bool (*validate)(struct efi_variable *var, int match, u8 *data,
  275. unsigned long len);
  276. };
  277. static const struct variable_validate variable_validate[] = {
  278. { "BootNext", validate_uint16 },
  279. { "BootOrder", validate_boot_order },
  280. { "DriverOrder", validate_boot_order },
  281. { "Boot*", validate_load_option },
  282. { "Driver*", validate_load_option },
  283. { "ConIn", validate_device_path },
  284. { "ConInDev", validate_device_path },
  285. { "ConOut", validate_device_path },
  286. { "ConOutDev", validate_device_path },
  287. { "ErrOut", validate_device_path },
  288. { "ErrOutDev", validate_device_path },
  289. { "Timeout", validate_uint16 },
  290. { "Lang", validate_ascii_string },
  291. { "PlatformLang", validate_ascii_string },
  292. { "", NULL },
  293. };
  294. static bool
  295. validate_var(struct efi_variable *var, u8 *data, unsigned long len)
  296. {
  297. int i;
  298. u16 *unicode_name = var->VariableName;
  299. for (i = 0; variable_validate[i].validate != NULL; i++) {
  300. const char *name = variable_validate[i].name;
  301. int match;
  302. for (match = 0; ; match++) {
  303. char c = name[match];
  304. u16 u = unicode_name[match];
  305. /* All special variables are plain ascii */
  306. if (u > 127)
  307. return true;
  308. /* Wildcard in the matching name means we've matched */
  309. if (c == '*')
  310. return variable_validate[i].validate(var,
  311. match, data, len);
  312. /* Case sensitive match */
  313. if (c != u)
  314. break;
  315. /* Reached the end of the string while matching */
  316. if (!c)
  317. return variable_validate[i].validate(var,
  318. match, data, len);
  319. }
  320. }
  321. return true;
  322. }
  323. static efi_status_t
  324. get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
  325. {
  326. efi_status_t status;
  327. var->DataSize = 1024;
  328. status = efivars->ops->get_variable(var->VariableName,
  329. &var->VendorGuid,
  330. &var->Attributes,
  331. &var->DataSize,
  332. var->Data);
  333. return status;
  334. }
  335. static efi_status_t
  336. get_var_data(struct efivars *efivars, struct efi_variable *var)
  337. {
  338. efi_status_t status;
  339. spin_lock(&efivars->lock);
  340. status = get_var_data_locked(efivars, var);
  341. spin_unlock(&efivars->lock);
  342. if (status != EFI_SUCCESS) {
  343. printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
  344. status);
  345. }
  346. return status;
  347. }
  348. static ssize_t
  349. efivar_guid_read(struct efivar_entry *entry, char *buf)
  350. {
  351. struct efi_variable *var = &entry->var;
  352. char *str = buf;
  353. if (!entry || !buf)
  354. return 0;
  355. efi_guid_unparse(&var->VendorGuid, str);
  356. str += strlen(str);
  357. str += sprintf(str, "\n");
  358. return str - buf;
  359. }
  360. static ssize_t
  361. efivar_attr_read(struct efivar_entry *entry, char *buf)
  362. {
  363. struct efi_variable *var = &entry->var;
  364. char *str = buf;
  365. efi_status_t status;
  366. if (!entry || !buf)
  367. return -EINVAL;
  368. status = get_var_data(entry->efivars, var);
  369. if (status != EFI_SUCCESS)
  370. return -EIO;
  371. if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
  372. str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
  373. if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
  374. str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
  375. if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
  376. str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
  377. if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
  378. str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
  379. if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
  380. str += sprintf(str,
  381. "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
  382. if (var->Attributes &
  383. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
  384. str += sprintf(str,
  385. "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
  386. if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
  387. str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
  388. return str - buf;
  389. }
  390. static ssize_t
  391. efivar_size_read(struct efivar_entry *entry, char *buf)
  392. {
  393. struct efi_variable *var = &entry->var;
  394. char *str = buf;
  395. efi_status_t status;
  396. if (!entry || !buf)
  397. return -EINVAL;
  398. status = get_var_data(entry->efivars, var);
  399. if (status != EFI_SUCCESS)
  400. return -EIO;
  401. str += sprintf(str, "0x%lx\n", var->DataSize);
  402. return str - buf;
  403. }
  404. static ssize_t
  405. efivar_data_read(struct efivar_entry *entry, char *buf)
  406. {
  407. struct efi_variable *var = &entry->var;
  408. efi_status_t status;
  409. if (!entry || !buf)
  410. return -EINVAL;
  411. status = get_var_data(entry->efivars, var);
  412. if (status != EFI_SUCCESS)
  413. return -EIO;
  414. memcpy(buf, var->Data, var->DataSize);
  415. return var->DataSize;
  416. }
  417. /*
  418. * We allow each variable to be edited via rewriting the
  419. * entire efi variable structure.
  420. */
  421. static ssize_t
  422. efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
  423. {
  424. struct efi_variable *new_var, *var = &entry->var;
  425. struct efivars *efivars = entry->efivars;
  426. efi_status_t status = EFI_NOT_FOUND;
  427. if (count != sizeof(struct efi_variable))
  428. return -EINVAL;
  429. new_var = (struct efi_variable *)buf;
  430. /*
  431. * If only updating the variable data, then the name
  432. * and guid should remain the same
  433. */
  434. if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
  435. efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
  436. printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
  437. return -EINVAL;
  438. }
  439. if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
  440. printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
  441. return -EINVAL;
  442. }
  443. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  444. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  445. printk(KERN_ERR "efivars: Malformed variable content\n");
  446. return -EINVAL;
  447. }
  448. spin_lock(&efivars->lock);
  449. status = efivars->ops->set_variable(new_var->VariableName,
  450. &new_var->VendorGuid,
  451. new_var->Attributes,
  452. new_var->DataSize,
  453. new_var->Data);
  454. spin_unlock(&efivars->lock);
  455. if (status != EFI_SUCCESS) {
  456. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  457. status);
  458. return -EIO;
  459. }
  460. memcpy(&entry->var, new_var, count);
  461. return count;
  462. }
  463. static ssize_t
  464. efivar_show_raw(struct efivar_entry *entry, char *buf)
  465. {
  466. struct efi_variable *var = &entry->var;
  467. efi_status_t status;
  468. if (!entry || !buf)
  469. return 0;
  470. status = get_var_data(entry->efivars, var);
  471. if (status != EFI_SUCCESS)
  472. return -EIO;
  473. memcpy(buf, var, sizeof(*var));
  474. return sizeof(*var);
  475. }
  476. /*
  477. * Generic read/write functions that call the specific functions of
  478. * the attributes...
  479. */
  480. static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
  481. char *buf)
  482. {
  483. struct efivar_entry *var = to_efivar_entry(kobj);
  484. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  485. ssize_t ret = -EIO;
  486. if (!capable(CAP_SYS_ADMIN))
  487. return -EACCES;
  488. if (efivar_attr->show) {
  489. ret = efivar_attr->show(var, buf);
  490. }
  491. return ret;
  492. }
  493. static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
  494. const char *buf, size_t count)
  495. {
  496. struct efivar_entry *var = to_efivar_entry(kobj);
  497. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  498. ssize_t ret = -EIO;
  499. if (!capable(CAP_SYS_ADMIN))
  500. return -EACCES;
  501. if (efivar_attr->store)
  502. ret = efivar_attr->store(var, buf, count);
  503. return ret;
  504. }
  505. static const struct sysfs_ops efivar_attr_ops = {
  506. .show = efivar_attr_show,
  507. .store = efivar_attr_store,
  508. };
  509. static void efivar_release(struct kobject *kobj)
  510. {
  511. struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
  512. kfree(var);
  513. }
  514. static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
  515. static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
  516. static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
  517. static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
  518. static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
  519. static struct attribute *def_attrs[] = {
  520. &efivar_attr_guid.attr,
  521. &efivar_attr_size.attr,
  522. &efivar_attr_attributes.attr,
  523. &efivar_attr_data.attr,
  524. &efivar_attr_raw_var.attr,
  525. NULL,
  526. };
  527. static struct kobj_type efivar_ktype = {
  528. .release = efivar_release,
  529. .sysfs_ops = &efivar_attr_ops,
  530. .default_attrs = def_attrs,
  531. };
  532. static struct pstore_info efi_pstore_info;
  533. static inline void
  534. efivar_unregister(struct efivar_entry *var)
  535. {
  536. kobject_put(&var->kobj);
  537. }
  538. #ifdef CONFIG_PSTORE
  539. static int efi_pstore_open(struct pstore_info *psi)
  540. {
  541. struct efivars *efivars = psi->data;
  542. spin_lock(&efivars->lock);
  543. efivars->walk_entry = list_first_entry(&efivars->list,
  544. struct efivar_entry, list);
  545. return 0;
  546. }
  547. static int efi_pstore_close(struct pstore_info *psi)
  548. {
  549. struct efivars *efivars = psi->data;
  550. spin_unlock(&efivars->lock);
  551. return 0;
  552. }
  553. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  554. struct timespec *timespec,
  555. char **buf, struct pstore_info *psi)
  556. {
  557. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  558. struct efivars *efivars = psi->data;
  559. char name[DUMP_NAME_LEN];
  560. int i;
  561. unsigned int part, size;
  562. unsigned long time;
  563. while (&efivars->walk_entry->list != &efivars->list) {
  564. if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
  565. vendor)) {
  566. for (i = 0; i < DUMP_NAME_LEN; i++) {
  567. name[i] = efivars->walk_entry->var.VariableName[i];
  568. }
  569. if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
  570. *id = part;
  571. timespec->tv_sec = time;
  572. timespec->tv_nsec = 0;
  573. get_var_data_locked(efivars, &efivars->walk_entry->var);
  574. size = efivars->walk_entry->var.DataSize;
  575. *buf = kmalloc(size, GFP_KERNEL);
  576. if (*buf == NULL)
  577. return -ENOMEM;
  578. memcpy(*buf, efivars->walk_entry->var.Data,
  579. size);
  580. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  581. struct efivar_entry, list);
  582. return size;
  583. }
  584. }
  585. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  586. struct efivar_entry, list);
  587. }
  588. return 0;
  589. }
  590. static int efi_pstore_write(enum pstore_type_id type,
  591. enum kmsg_dump_reason reason, u64 *id,
  592. unsigned int part, size_t size, struct pstore_info *psi)
  593. {
  594. char name[DUMP_NAME_LEN];
  595. char stub_name[DUMP_NAME_LEN];
  596. efi_char16_t efi_name[DUMP_NAME_LEN];
  597. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  598. struct efivars *efivars = psi->data;
  599. struct efivar_entry *entry, *found = NULL;
  600. int i, ret = 0;
  601. sprintf(stub_name, "dump-type%u-%u-", type, part);
  602. sprintf(name, "%s%lu", stub_name, get_seconds());
  603. spin_lock(&efivars->lock);
  604. for (i = 0; i < DUMP_NAME_LEN; i++)
  605. efi_name[i] = stub_name[i];
  606. /*
  607. * Clean up any entries with the same name
  608. */
  609. list_for_each_entry(entry, &efivars->list, list) {
  610. get_var_data_locked(efivars, &entry->var);
  611. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  612. continue;
  613. if (utf16_strncmp(entry->var.VariableName, efi_name,
  614. utf16_strlen(efi_name)))
  615. continue;
  616. /* Needs to be a prefix */
  617. if (entry->var.VariableName[utf16_strlen(efi_name)] == 0)
  618. continue;
  619. /* found */
  620. found = entry;
  621. efivars->ops->set_variable(entry->var.VariableName,
  622. &entry->var.VendorGuid,
  623. PSTORE_EFI_ATTRIBUTES,
  624. 0, NULL);
  625. }
  626. if (found)
  627. list_del(&found->list);
  628. for (i = 0; i < DUMP_NAME_LEN; i++)
  629. efi_name[i] = name[i];
  630. efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
  631. size, psi->buf);
  632. spin_unlock(&efivars->lock);
  633. if (found)
  634. efivar_unregister(found);
  635. if (size)
  636. ret = efivar_create_sysfs_entry(efivars,
  637. utf16_strsize(efi_name,
  638. DUMP_NAME_LEN * 2),
  639. efi_name, &vendor);
  640. *id = part;
  641. return ret;
  642. };
  643. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  644. struct pstore_info *psi)
  645. {
  646. efi_pstore_write(type, 0, &id, (unsigned int)id, 0, psi);
  647. return 0;
  648. }
  649. #else
  650. static int efi_pstore_open(struct pstore_info *psi)
  651. {
  652. return 0;
  653. }
  654. static int efi_pstore_close(struct pstore_info *psi)
  655. {
  656. return 0;
  657. }
  658. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  659. struct timespec *timespec,
  660. char **buf, struct pstore_info *psi)
  661. {
  662. return -1;
  663. }
  664. static int efi_pstore_write(enum pstore_type_id type,
  665. enum kmsg_dump_reason reason, u64 *id,
  666. unsigned int part, size_t size, struct pstore_info *psi)
  667. {
  668. return 0;
  669. }
  670. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  671. struct pstore_info *psi)
  672. {
  673. return 0;
  674. }
  675. #endif
  676. static struct pstore_info efi_pstore_info = {
  677. .owner = THIS_MODULE,
  678. .name = "efi",
  679. .open = efi_pstore_open,
  680. .close = efi_pstore_close,
  681. .read = efi_pstore_read,
  682. .write = efi_pstore_write,
  683. .erase = efi_pstore_erase,
  684. };
  685. static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
  686. struct bin_attribute *bin_attr,
  687. char *buf, loff_t pos, size_t count)
  688. {
  689. struct efi_variable *new_var = (struct efi_variable *)buf;
  690. struct efivars *efivars = bin_attr->private;
  691. struct efivar_entry *search_efivar, *n;
  692. unsigned long strsize1, strsize2;
  693. efi_status_t status = EFI_NOT_FOUND;
  694. int found = 0;
  695. if (!capable(CAP_SYS_ADMIN))
  696. return -EACCES;
  697. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  698. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  699. printk(KERN_ERR "efivars: Malformed variable content\n");
  700. return -EINVAL;
  701. }
  702. spin_lock(&efivars->lock);
  703. /*
  704. * Does this variable already exist?
  705. */
  706. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  707. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  708. strsize2 = utf16_strsize(new_var->VariableName, 1024);
  709. if (strsize1 == strsize2 &&
  710. !memcmp(&(search_efivar->var.VariableName),
  711. new_var->VariableName, strsize1) &&
  712. !efi_guidcmp(search_efivar->var.VendorGuid,
  713. new_var->VendorGuid)) {
  714. found = 1;
  715. break;
  716. }
  717. }
  718. if (found) {
  719. spin_unlock(&efivars->lock);
  720. return -EINVAL;
  721. }
  722. /* now *really* create the variable via EFI */
  723. status = efivars->ops->set_variable(new_var->VariableName,
  724. &new_var->VendorGuid,
  725. new_var->Attributes,
  726. new_var->DataSize,
  727. new_var->Data);
  728. if (status != EFI_SUCCESS) {
  729. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  730. status);
  731. spin_unlock(&efivars->lock);
  732. return -EIO;
  733. }
  734. spin_unlock(&efivars->lock);
  735. /* Create the entry in sysfs. Locking is not required here */
  736. status = efivar_create_sysfs_entry(efivars,
  737. utf16_strsize(new_var->VariableName,
  738. 1024),
  739. new_var->VariableName,
  740. &new_var->VendorGuid);
  741. if (status) {
  742. printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
  743. }
  744. return count;
  745. }
  746. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  747. struct bin_attribute *bin_attr,
  748. char *buf, loff_t pos, size_t count)
  749. {
  750. struct efi_variable *del_var = (struct efi_variable *)buf;
  751. struct efivars *efivars = bin_attr->private;
  752. struct efivar_entry *search_efivar, *n;
  753. unsigned long strsize1, strsize2;
  754. efi_status_t status = EFI_NOT_FOUND;
  755. int found = 0;
  756. if (!capable(CAP_SYS_ADMIN))
  757. return -EACCES;
  758. spin_lock(&efivars->lock);
  759. /*
  760. * Does this variable already exist?
  761. */
  762. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  763. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  764. strsize2 = utf16_strsize(del_var->VariableName, 1024);
  765. if (strsize1 == strsize2 &&
  766. !memcmp(&(search_efivar->var.VariableName),
  767. del_var->VariableName, strsize1) &&
  768. !efi_guidcmp(search_efivar->var.VendorGuid,
  769. del_var->VendorGuid)) {
  770. found = 1;
  771. break;
  772. }
  773. }
  774. if (!found) {
  775. spin_unlock(&efivars->lock);
  776. return -EINVAL;
  777. }
  778. /* force the Attributes/DataSize to 0 to ensure deletion */
  779. del_var->Attributes = 0;
  780. del_var->DataSize = 0;
  781. status = efivars->ops->set_variable(del_var->VariableName,
  782. &del_var->VendorGuid,
  783. del_var->Attributes,
  784. del_var->DataSize,
  785. del_var->Data);
  786. if (status != EFI_SUCCESS) {
  787. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  788. status);
  789. spin_unlock(&efivars->lock);
  790. return -EIO;
  791. }
  792. list_del(&search_efivar->list);
  793. /* We need to release this lock before unregistering. */
  794. spin_unlock(&efivars->lock);
  795. efivar_unregister(search_efivar);
  796. /* It's dead Jim.... */
  797. return count;
  798. }
  799. /*
  800. * Let's not leave out systab information that snuck into
  801. * the efivars driver
  802. */
  803. static ssize_t systab_show(struct kobject *kobj,
  804. struct kobj_attribute *attr, char *buf)
  805. {
  806. char *str = buf;
  807. if (!kobj || !buf)
  808. return -EINVAL;
  809. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  810. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  811. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  812. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  813. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  814. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  815. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  816. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  817. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  818. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  819. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  820. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  821. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  822. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  823. return str - buf;
  824. }
  825. static struct kobj_attribute efi_attr_systab =
  826. __ATTR(systab, 0400, systab_show, NULL);
  827. static struct attribute *efi_subsys_attrs[] = {
  828. &efi_attr_systab.attr,
  829. NULL, /* maybe more in the future? */
  830. };
  831. static struct attribute_group efi_subsys_attr_group = {
  832. .attrs = efi_subsys_attrs,
  833. };
  834. static struct kobject *efi_kobj;
  835. /*
  836. * efivar_create_sysfs_entry()
  837. * Requires:
  838. * variable_name_size = number of bytes required to hold
  839. * variable_name (not counting the NULL
  840. * character at the end.
  841. * efivars->lock is not held on entry or exit.
  842. * Returns 1 on failure, 0 on success
  843. */
  844. static int
  845. efivar_create_sysfs_entry(struct efivars *efivars,
  846. unsigned long variable_name_size,
  847. efi_char16_t *variable_name,
  848. efi_guid_t *vendor_guid)
  849. {
  850. int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
  851. char *short_name;
  852. struct efivar_entry *new_efivar;
  853. short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
  854. new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  855. if (!short_name || !new_efivar) {
  856. kfree(short_name);
  857. kfree(new_efivar);
  858. return 1;
  859. }
  860. new_efivar->efivars = efivars;
  861. memcpy(new_efivar->var.VariableName, variable_name,
  862. variable_name_size);
  863. memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
  864. /* Convert Unicode to normal chars (assume top bits are 0),
  865. ala UTF-8 */
  866. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  867. short_name[i] = variable_name[i] & 0xFF;
  868. }
  869. /* This is ugly, but necessary to separate one vendor's
  870. private variables from another's. */
  871. *(short_name + strlen(short_name)) = '-';
  872. efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
  873. new_efivar->kobj.kset = efivars->kset;
  874. i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
  875. "%s", short_name);
  876. if (i) {
  877. kfree(short_name);
  878. kfree(new_efivar);
  879. return 1;
  880. }
  881. kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
  882. kfree(short_name);
  883. short_name = NULL;
  884. spin_lock(&efivars->lock);
  885. list_add(&new_efivar->list, &efivars->list);
  886. spin_unlock(&efivars->lock);
  887. return 0;
  888. }
  889. static int
  890. create_efivars_bin_attributes(struct efivars *efivars)
  891. {
  892. struct bin_attribute *attr;
  893. int error;
  894. /* new_var */
  895. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  896. if (!attr)
  897. return -ENOMEM;
  898. attr->attr.name = "new_var";
  899. attr->attr.mode = 0200;
  900. attr->write = efivar_create;
  901. attr->private = efivars;
  902. efivars->new_var = attr;
  903. /* del_var */
  904. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  905. if (!attr) {
  906. error = -ENOMEM;
  907. goto out_free;
  908. }
  909. attr->attr.name = "del_var";
  910. attr->attr.mode = 0200;
  911. attr->write = efivar_delete;
  912. attr->private = efivars;
  913. efivars->del_var = attr;
  914. sysfs_bin_attr_init(efivars->new_var);
  915. sysfs_bin_attr_init(efivars->del_var);
  916. /* Register */
  917. error = sysfs_create_bin_file(&efivars->kset->kobj,
  918. efivars->new_var);
  919. if (error) {
  920. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  921. " due to error %d\n", error);
  922. goto out_free;
  923. }
  924. error = sysfs_create_bin_file(&efivars->kset->kobj,
  925. efivars->del_var);
  926. if (error) {
  927. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  928. " due to error %d\n", error);
  929. sysfs_remove_bin_file(&efivars->kset->kobj,
  930. efivars->new_var);
  931. goto out_free;
  932. }
  933. return 0;
  934. out_free:
  935. kfree(efivars->del_var);
  936. efivars->del_var = NULL;
  937. kfree(efivars->new_var);
  938. efivars->new_var = NULL;
  939. return error;
  940. }
  941. void unregister_efivars(struct efivars *efivars)
  942. {
  943. struct efivar_entry *entry, *n;
  944. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  945. spin_lock(&efivars->lock);
  946. list_del(&entry->list);
  947. spin_unlock(&efivars->lock);
  948. efivar_unregister(entry);
  949. }
  950. if (efivars->new_var)
  951. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
  952. if (efivars->del_var)
  953. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
  954. kfree(efivars->new_var);
  955. kfree(efivars->del_var);
  956. kset_unregister(efivars->kset);
  957. }
  958. EXPORT_SYMBOL_GPL(unregister_efivars);
  959. int register_efivars(struct efivars *efivars,
  960. const struct efivar_operations *ops,
  961. struct kobject *parent_kobj)
  962. {
  963. efi_status_t status = EFI_NOT_FOUND;
  964. efi_guid_t vendor_guid;
  965. efi_char16_t *variable_name;
  966. unsigned long variable_name_size = 1024;
  967. int error = 0;
  968. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  969. if (!variable_name) {
  970. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  971. return -ENOMEM;
  972. }
  973. spin_lock_init(&efivars->lock);
  974. INIT_LIST_HEAD(&efivars->list);
  975. efivars->ops = ops;
  976. efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
  977. if (!efivars->kset) {
  978. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  979. error = -ENOMEM;
  980. goto out;
  981. }
  982. /*
  983. * Per EFI spec, the maximum storage allocated for both
  984. * the variable name and variable data is 1024 bytes.
  985. */
  986. do {
  987. variable_name_size = 1024;
  988. status = ops->get_next_variable(&variable_name_size,
  989. variable_name,
  990. &vendor_guid);
  991. switch (status) {
  992. case EFI_SUCCESS:
  993. efivar_create_sysfs_entry(efivars,
  994. variable_name_size,
  995. variable_name,
  996. &vendor_guid);
  997. break;
  998. case EFI_NOT_FOUND:
  999. break;
  1000. default:
  1001. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  1002. status);
  1003. status = EFI_NOT_FOUND;
  1004. break;
  1005. }
  1006. } while (status != EFI_NOT_FOUND);
  1007. error = create_efivars_bin_attributes(efivars);
  1008. if (error)
  1009. unregister_efivars(efivars);
  1010. efivars->efi_pstore_info = efi_pstore_info;
  1011. efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  1012. if (efivars->efi_pstore_info.buf) {
  1013. efivars->efi_pstore_info.bufsize = 1024;
  1014. efivars->efi_pstore_info.data = efivars;
  1015. spin_lock_init(&efivars->efi_pstore_info.buf_lock);
  1016. pstore_register(&efivars->efi_pstore_info);
  1017. }
  1018. out:
  1019. kfree(variable_name);
  1020. return error;
  1021. }
  1022. EXPORT_SYMBOL_GPL(register_efivars);
  1023. static struct efivars __efivars;
  1024. static struct efivar_operations ops;
  1025. /*
  1026. * For now we register the efi subsystem with the firmware subsystem
  1027. * and the vars subsystem with the efi subsystem. In the future, it
  1028. * might make sense to split off the efi subsystem into its own
  1029. * driver, but for now only efivars will register with it, so just
  1030. * include it here.
  1031. */
  1032. static int __init
  1033. efivars_init(void)
  1034. {
  1035. int error = 0;
  1036. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  1037. EFIVARS_DATE);
  1038. if (!efi_enabled)
  1039. return 0;
  1040. /* For now we'll register the efi directory at /sys/firmware/efi */
  1041. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  1042. if (!efi_kobj) {
  1043. printk(KERN_ERR "efivars: Firmware registration failed.\n");
  1044. return -ENOMEM;
  1045. }
  1046. ops.get_variable = efi.get_variable;
  1047. ops.set_variable = efi.set_variable;
  1048. ops.get_next_variable = efi.get_next_variable;
  1049. error = register_efivars(&__efivars, &ops, efi_kobj);
  1050. if (error)
  1051. goto err_put;
  1052. /* Don't forget the systab entry */
  1053. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  1054. if (error) {
  1055. printk(KERN_ERR
  1056. "efivars: Sysfs attribute export failed with error %d.\n",
  1057. error);
  1058. goto err_unregister;
  1059. }
  1060. return 0;
  1061. err_unregister:
  1062. unregister_efivars(&__efivars);
  1063. err_put:
  1064. kobject_put(efi_kobj);
  1065. return error;
  1066. }
  1067. static void __exit
  1068. efivars_exit(void)
  1069. {
  1070. if (efi_enabled) {
  1071. unregister_efivars(&__efivars);
  1072. kobject_put(efi_kobj);
  1073. }
  1074. }
  1075. module_init(efivars_init);
  1076. module_exit(efivars_exit);