efivars.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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 & 0x1)
  372. str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
  373. if (var->Attributes & 0x2)
  374. str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
  375. if (var->Attributes & 0x4)
  376. str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
  377. return str - buf;
  378. }
  379. static ssize_t
  380. efivar_size_read(struct efivar_entry *entry, char *buf)
  381. {
  382. struct efi_variable *var = &entry->var;
  383. char *str = buf;
  384. efi_status_t status;
  385. if (!entry || !buf)
  386. return -EINVAL;
  387. status = get_var_data(entry->efivars, var);
  388. if (status != EFI_SUCCESS)
  389. return -EIO;
  390. str += sprintf(str, "0x%lx\n", var->DataSize);
  391. return str - buf;
  392. }
  393. static ssize_t
  394. efivar_data_read(struct efivar_entry *entry, char *buf)
  395. {
  396. struct efi_variable *var = &entry->var;
  397. efi_status_t status;
  398. if (!entry || !buf)
  399. return -EINVAL;
  400. status = get_var_data(entry->efivars, var);
  401. if (status != EFI_SUCCESS)
  402. return -EIO;
  403. memcpy(buf, var->Data, var->DataSize);
  404. return var->DataSize;
  405. }
  406. /*
  407. * We allow each variable to be edited via rewriting the
  408. * entire efi variable structure.
  409. */
  410. static ssize_t
  411. efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
  412. {
  413. struct efi_variable *new_var, *var = &entry->var;
  414. struct efivars *efivars = entry->efivars;
  415. efi_status_t status = EFI_NOT_FOUND;
  416. if (count != sizeof(struct efi_variable))
  417. return -EINVAL;
  418. new_var = (struct efi_variable *)buf;
  419. /*
  420. * If only updating the variable data, then the name
  421. * and guid should remain the same
  422. */
  423. if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
  424. efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
  425. printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
  426. return -EINVAL;
  427. }
  428. if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
  429. printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
  430. return -EINVAL;
  431. }
  432. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  433. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  434. printk(KERN_ERR "efivars: Malformed variable content\n");
  435. return -EINVAL;
  436. }
  437. spin_lock(&efivars->lock);
  438. status = efivars->ops->set_variable(new_var->VariableName,
  439. &new_var->VendorGuid,
  440. new_var->Attributes,
  441. new_var->DataSize,
  442. new_var->Data);
  443. spin_unlock(&efivars->lock);
  444. if (status != EFI_SUCCESS) {
  445. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  446. status);
  447. return -EIO;
  448. }
  449. memcpy(&entry->var, new_var, count);
  450. return count;
  451. }
  452. static ssize_t
  453. efivar_show_raw(struct efivar_entry *entry, char *buf)
  454. {
  455. struct efi_variable *var = &entry->var;
  456. efi_status_t status;
  457. if (!entry || !buf)
  458. return 0;
  459. status = get_var_data(entry->efivars, var);
  460. if (status != EFI_SUCCESS)
  461. return -EIO;
  462. memcpy(buf, var, sizeof(*var));
  463. return sizeof(*var);
  464. }
  465. /*
  466. * Generic read/write functions that call the specific functions of
  467. * the attributes...
  468. */
  469. static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
  470. char *buf)
  471. {
  472. struct efivar_entry *var = to_efivar_entry(kobj);
  473. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  474. ssize_t ret = -EIO;
  475. if (!capable(CAP_SYS_ADMIN))
  476. return -EACCES;
  477. if (efivar_attr->show) {
  478. ret = efivar_attr->show(var, buf);
  479. }
  480. return ret;
  481. }
  482. static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
  483. const char *buf, size_t count)
  484. {
  485. struct efivar_entry *var = to_efivar_entry(kobj);
  486. struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
  487. ssize_t ret = -EIO;
  488. if (!capable(CAP_SYS_ADMIN))
  489. return -EACCES;
  490. if (efivar_attr->store)
  491. ret = efivar_attr->store(var, buf, count);
  492. return ret;
  493. }
  494. static const struct sysfs_ops efivar_attr_ops = {
  495. .show = efivar_attr_show,
  496. .store = efivar_attr_store,
  497. };
  498. static void efivar_release(struct kobject *kobj)
  499. {
  500. struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
  501. kfree(var);
  502. }
  503. static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
  504. static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
  505. static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
  506. static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
  507. static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
  508. static struct attribute *def_attrs[] = {
  509. &efivar_attr_guid.attr,
  510. &efivar_attr_size.attr,
  511. &efivar_attr_attributes.attr,
  512. &efivar_attr_data.attr,
  513. &efivar_attr_raw_var.attr,
  514. NULL,
  515. };
  516. static struct kobj_type efivar_ktype = {
  517. .release = efivar_release,
  518. .sysfs_ops = &efivar_attr_ops,
  519. .default_attrs = def_attrs,
  520. };
  521. static struct pstore_info efi_pstore_info;
  522. static inline void
  523. efivar_unregister(struct efivar_entry *var)
  524. {
  525. kobject_put(&var->kobj);
  526. }
  527. #ifdef CONFIG_PSTORE
  528. static int efi_pstore_open(struct pstore_info *psi)
  529. {
  530. struct efivars *efivars = psi->data;
  531. spin_lock(&efivars->lock);
  532. efivars->walk_entry = list_first_entry(&efivars->list,
  533. struct efivar_entry, list);
  534. return 0;
  535. }
  536. static int efi_pstore_close(struct pstore_info *psi)
  537. {
  538. struct efivars *efivars = psi->data;
  539. spin_unlock(&efivars->lock);
  540. return 0;
  541. }
  542. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  543. struct timespec *timespec,
  544. char **buf, struct pstore_info *psi)
  545. {
  546. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  547. struct efivars *efivars = psi->data;
  548. char name[DUMP_NAME_LEN];
  549. int i;
  550. unsigned int part, size;
  551. unsigned long time;
  552. while (&efivars->walk_entry->list != &efivars->list) {
  553. if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
  554. vendor)) {
  555. for (i = 0; i < DUMP_NAME_LEN; i++) {
  556. name[i] = efivars->walk_entry->var.VariableName[i];
  557. }
  558. if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
  559. *id = part;
  560. timespec->tv_sec = time;
  561. timespec->tv_nsec = 0;
  562. get_var_data_locked(efivars, &efivars->walk_entry->var);
  563. size = efivars->walk_entry->var.DataSize;
  564. *buf = kmalloc(size, GFP_KERNEL);
  565. if (*buf == NULL)
  566. return -ENOMEM;
  567. memcpy(*buf, efivars->walk_entry->var.Data,
  568. size);
  569. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  570. struct efivar_entry, list);
  571. return size;
  572. }
  573. }
  574. efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
  575. struct efivar_entry, list);
  576. }
  577. return 0;
  578. }
  579. static int efi_pstore_write(enum pstore_type_id type,
  580. enum kmsg_dump_reason reason, u64 *id,
  581. unsigned int part, size_t size, struct pstore_info *psi)
  582. {
  583. char name[DUMP_NAME_LEN];
  584. char stub_name[DUMP_NAME_LEN];
  585. efi_char16_t efi_name[DUMP_NAME_LEN];
  586. efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
  587. struct efivars *efivars = psi->data;
  588. struct efivar_entry *entry, *found = NULL;
  589. int i, ret = 0;
  590. sprintf(stub_name, "dump-type%u-%u-", type, part);
  591. sprintf(name, "%s%lu", stub_name, get_seconds());
  592. spin_lock(&efivars->lock);
  593. for (i = 0; i < DUMP_NAME_LEN; i++)
  594. efi_name[i] = stub_name[i];
  595. /*
  596. * Clean up any entries with the same name
  597. */
  598. list_for_each_entry(entry, &efivars->list, list) {
  599. get_var_data_locked(efivars, &entry->var);
  600. if (efi_guidcmp(entry->var.VendorGuid, vendor))
  601. continue;
  602. if (utf16_strncmp(entry->var.VariableName, efi_name,
  603. utf16_strlen(efi_name)))
  604. continue;
  605. /* Needs to be a prefix */
  606. if (entry->var.VariableName[utf16_strlen(efi_name)] == 0)
  607. continue;
  608. /* found */
  609. found = entry;
  610. efivars->ops->set_variable(entry->var.VariableName,
  611. &entry->var.VendorGuid,
  612. PSTORE_EFI_ATTRIBUTES,
  613. 0, NULL);
  614. }
  615. if (found)
  616. list_del(&found->list);
  617. for (i = 0; i < DUMP_NAME_LEN; i++)
  618. efi_name[i] = name[i];
  619. efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
  620. size, psi->buf);
  621. spin_unlock(&efivars->lock);
  622. if (found)
  623. efivar_unregister(found);
  624. if (size)
  625. ret = efivar_create_sysfs_entry(efivars,
  626. utf16_strsize(efi_name,
  627. DUMP_NAME_LEN * 2),
  628. efi_name, &vendor);
  629. *id = part;
  630. return ret;
  631. };
  632. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  633. struct pstore_info *psi)
  634. {
  635. efi_pstore_write(type, 0, &id, (unsigned int)id, 0, psi);
  636. return 0;
  637. }
  638. #else
  639. static int efi_pstore_open(struct pstore_info *psi)
  640. {
  641. return 0;
  642. }
  643. static int efi_pstore_close(struct pstore_info *psi)
  644. {
  645. return 0;
  646. }
  647. static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
  648. struct timespec *timespec,
  649. char **buf, struct pstore_info *psi)
  650. {
  651. return -1;
  652. }
  653. static int efi_pstore_write(enum pstore_type_id type,
  654. enum kmsg_dump_reason reason, u64 *id,
  655. unsigned int part, size_t size, struct pstore_info *psi)
  656. {
  657. return 0;
  658. }
  659. static int efi_pstore_erase(enum pstore_type_id type, u64 id,
  660. struct pstore_info *psi)
  661. {
  662. return 0;
  663. }
  664. #endif
  665. static struct pstore_info efi_pstore_info = {
  666. .owner = THIS_MODULE,
  667. .name = "efi",
  668. .open = efi_pstore_open,
  669. .close = efi_pstore_close,
  670. .read = efi_pstore_read,
  671. .write = efi_pstore_write,
  672. .erase = efi_pstore_erase,
  673. };
  674. static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
  675. struct bin_attribute *bin_attr,
  676. char *buf, loff_t pos, size_t count)
  677. {
  678. struct efi_variable *new_var = (struct efi_variable *)buf;
  679. struct efivars *efivars = bin_attr->private;
  680. struct efivar_entry *search_efivar, *n;
  681. unsigned long strsize1, strsize2;
  682. efi_status_t status = EFI_NOT_FOUND;
  683. int found = 0;
  684. if (!capable(CAP_SYS_ADMIN))
  685. return -EACCES;
  686. if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
  687. validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
  688. printk(KERN_ERR "efivars: Malformed variable content\n");
  689. return -EINVAL;
  690. }
  691. spin_lock(&efivars->lock);
  692. /*
  693. * Does this variable already exist?
  694. */
  695. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  696. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  697. strsize2 = utf16_strsize(new_var->VariableName, 1024);
  698. if (strsize1 == strsize2 &&
  699. !memcmp(&(search_efivar->var.VariableName),
  700. new_var->VariableName, strsize1) &&
  701. !efi_guidcmp(search_efivar->var.VendorGuid,
  702. new_var->VendorGuid)) {
  703. found = 1;
  704. break;
  705. }
  706. }
  707. if (found) {
  708. spin_unlock(&efivars->lock);
  709. return -EINVAL;
  710. }
  711. /* now *really* create the variable via EFI */
  712. status = efivars->ops->set_variable(new_var->VariableName,
  713. &new_var->VendorGuid,
  714. new_var->Attributes,
  715. new_var->DataSize,
  716. new_var->Data);
  717. if (status != EFI_SUCCESS) {
  718. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  719. status);
  720. spin_unlock(&efivars->lock);
  721. return -EIO;
  722. }
  723. spin_unlock(&efivars->lock);
  724. /* Create the entry in sysfs. Locking is not required here */
  725. status = efivar_create_sysfs_entry(efivars,
  726. utf16_strsize(new_var->VariableName,
  727. 1024),
  728. new_var->VariableName,
  729. &new_var->VendorGuid);
  730. if (status) {
  731. printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
  732. }
  733. return count;
  734. }
  735. static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
  736. struct bin_attribute *bin_attr,
  737. char *buf, loff_t pos, size_t count)
  738. {
  739. struct efi_variable *del_var = (struct efi_variable *)buf;
  740. struct efivars *efivars = bin_attr->private;
  741. struct efivar_entry *search_efivar, *n;
  742. unsigned long strsize1, strsize2;
  743. efi_status_t status = EFI_NOT_FOUND;
  744. int found = 0;
  745. if (!capable(CAP_SYS_ADMIN))
  746. return -EACCES;
  747. spin_lock(&efivars->lock);
  748. /*
  749. * Does this variable already exist?
  750. */
  751. list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
  752. strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
  753. strsize2 = utf16_strsize(del_var->VariableName, 1024);
  754. if (strsize1 == strsize2 &&
  755. !memcmp(&(search_efivar->var.VariableName),
  756. del_var->VariableName, strsize1) &&
  757. !efi_guidcmp(search_efivar->var.VendorGuid,
  758. del_var->VendorGuid)) {
  759. found = 1;
  760. break;
  761. }
  762. }
  763. if (!found) {
  764. spin_unlock(&efivars->lock);
  765. return -EINVAL;
  766. }
  767. /* force the Attributes/DataSize to 0 to ensure deletion */
  768. del_var->Attributes = 0;
  769. del_var->DataSize = 0;
  770. status = efivars->ops->set_variable(del_var->VariableName,
  771. &del_var->VendorGuid,
  772. del_var->Attributes,
  773. del_var->DataSize,
  774. del_var->Data);
  775. if (status != EFI_SUCCESS) {
  776. printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
  777. status);
  778. spin_unlock(&efivars->lock);
  779. return -EIO;
  780. }
  781. list_del(&search_efivar->list);
  782. /* We need to release this lock before unregistering. */
  783. spin_unlock(&efivars->lock);
  784. efivar_unregister(search_efivar);
  785. /* It's dead Jim.... */
  786. return count;
  787. }
  788. /*
  789. * Let's not leave out systab information that snuck into
  790. * the efivars driver
  791. */
  792. static ssize_t systab_show(struct kobject *kobj,
  793. struct kobj_attribute *attr, char *buf)
  794. {
  795. char *str = buf;
  796. if (!kobj || !buf)
  797. return -EINVAL;
  798. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  799. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  800. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  801. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  802. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  803. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  804. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  805. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  806. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  807. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  808. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  809. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  810. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  811. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  812. return str - buf;
  813. }
  814. static struct kobj_attribute efi_attr_systab =
  815. __ATTR(systab, 0400, systab_show, NULL);
  816. static struct attribute *efi_subsys_attrs[] = {
  817. &efi_attr_systab.attr,
  818. NULL, /* maybe more in the future? */
  819. };
  820. static struct attribute_group efi_subsys_attr_group = {
  821. .attrs = efi_subsys_attrs,
  822. };
  823. static struct kobject *efi_kobj;
  824. /*
  825. * efivar_create_sysfs_entry()
  826. * Requires:
  827. * variable_name_size = number of bytes required to hold
  828. * variable_name (not counting the NULL
  829. * character at the end.
  830. * efivars->lock is not held on entry or exit.
  831. * Returns 1 on failure, 0 on success
  832. */
  833. static int
  834. efivar_create_sysfs_entry(struct efivars *efivars,
  835. unsigned long variable_name_size,
  836. efi_char16_t *variable_name,
  837. efi_guid_t *vendor_guid)
  838. {
  839. int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
  840. char *short_name;
  841. struct efivar_entry *new_efivar;
  842. short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
  843. new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  844. if (!short_name || !new_efivar) {
  845. kfree(short_name);
  846. kfree(new_efivar);
  847. return 1;
  848. }
  849. new_efivar->efivars = efivars;
  850. memcpy(new_efivar->var.VariableName, variable_name,
  851. variable_name_size);
  852. memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
  853. /* Convert Unicode to normal chars (assume top bits are 0),
  854. ala UTF-8 */
  855. for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
  856. short_name[i] = variable_name[i] & 0xFF;
  857. }
  858. /* This is ugly, but necessary to separate one vendor's
  859. private variables from another's. */
  860. *(short_name + strlen(short_name)) = '-';
  861. efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
  862. new_efivar->kobj.kset = efivars->kset;
  863. i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
  864. "%s", short_name);
  865. if (i) {
  866. kfree(short_name);
  867. kfree(new_efivar);
  868. return 1;
  869. }
  870. kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
  871. kfree(short_name);
  872. short_name = NULL;
  873. spin_lock(&efivars->lock);
  874. list_add(&new_efivar->list, &efivars->list);
  875. spin_unlock(&efivars->lock);
  876. return 0;
  877. }
  878. static int
  879. create_efivars_bin_attributes(struct efivars *efivars)
  880. {
  881. struct bin_attribute *attr;
  882. int error;
  883. /* new_var */
  884. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  885. if (!attr)
  886. return -ENOMEM;
  887. attr->attr.name = "new_var";
  888. attr->attr.mode = 0200;
  889. attr->write = efivar_create;
  890. attr->private = efivars;
  891. efivars->new_var = attr;
  892. /* del_var */
  893. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  894. if (!attr) {
  895. error = -ENOMEM;
  896. goto out_free;
  897. }
  898. attr->attr.name = "del_var";
  899. attr->attr.mode = 0200;
  900. attr->write = efivar_delete;
  901. attr->private = efivars;
  902. efivars->del_var = attr;
  903. sysfs_bin_attr_init(efivars->new_var);
  904. sysfs_bin_attr_init(efivars->del_var);
  905. /* Register */
  906. error = sysfs_create_bin_file(&efivars->kset->kobj,
  907. efivars->new_var);
  908. if (error) {
  909. printk(KERN_ERR "efivars: unable to create new_var sysfs file"
  910. " due to error %d\n", error);
  911. goto out_free;
  912. }
  913. error = sysfs_create_bin_file(&efivars->kset->kobj,
  914. efivars->del_var);
  915. if (error) {
  916. printk(KERN_ERR "efivars: unable to create del_var sysfs file"
  917. " due to error %d\n", error);
  918. sysfs_remove_bin_file(&efivars->kset->kobj,
  919. efivars->new_var);
  920. goto out_free;
  921. }
  922. return 0;
  923. out_free:
  924. kfree(efivars->del_var);
  925. efivars->del_var = NULL;
  926. kfree(efivars->new_var);
  927. efivars->new_var = NULL;
  928. return error;
  929. }
  930. void unregister_efivars(struct efivars *efivars)
  931. {
  932. struct efivar_entry *entry, *n;
  933. list_for_each_entry_safe(entry, n, &efivars->list, list) {
  934. spin_lock(&efivars->lock);
  935. list_del(&entry->list);
  936. spin_unlock(&efivars->lock);
  937. efivar_unregister(entry);
  938. }
  939. if (efivars->new_var)
  940. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
  941. if (efivars->del_var)
  942. sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
  943. kfree(efivars->new_var);
  944. kfree(efivars->del_var);
  945. kset_unregister(efivars->kset);
  946. }
  947. EXPORT_SYMBOL_GPL(unregister_efivars);
  948. int register_efivars(struct efivars *efivars,
  949. const struct efivar_operations *ops,
  950. struct kobject *parent_kobj)
  951. {
  952. efi_status_t status = EFI_NOT_FOUND;
  953. efi_guid_t vendor_guid;
  954. efi_char16_t *variable_name;
  955. unsigned long variable_name_size = 1024;
  956. int error = 0;
  957. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  958. if (!variable_name) {
  959. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  960. return -ENOMEM;
  961. }
  962. spin_lock_init(&efivars->lock);
  963. INIT_LIST_HEAD(&efivars->list);
  964. efivars->ops = ops;
  965. efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
  966. if (!efivars->kset) {
  967. printk(KERN_ERR "efivars: Subsystem registration failed.\n");
  968. error = -ENOMEM;
  969. goto out;
  970. }
  971. /*
  972. * Per EFI spec, the maximum storage allocated for both
  973. * the variable name and variable data is 1024 bytes.
  974. */
  975. do {
  976. variable_name_size = 1024;
  977. status = ops->get_next_variable(&variable_name_size,
  978. variable_name,
  979. &vendor_guid);
  980. switch (status) {
  981. case EFI_SUCCESS:
  982. efivar_create_sysfs_entry(efivars,
  983. variable_name_size,
  984. variable_name,
  985. &vendor_guid);
  986. break;
  987. case EFI_NOT_FOUND:
  988. break;
  989. default:
  990. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  991. status);
  992. status = EFI_NOT_FOUND;
  993. break;
  994. }
  995. } while (status != EFI_NOT_FOUND);
  996. error = create_efivars_bin_attributes(efivars);
  997. if (error)
  998. unregister_efivars(efivars);
  999. efivars->efi_pstore_info = efi_pstore_info;
  1000. efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
  1001. if (efivars->efi_pstore_info.buf) {
  1002. efivars->efi_pstore_info.bufsize = 1024;
  1003. efivars->efi_pstore_info.data = efivars;
  1004. spin_lock_init(&efivars->efi_pstore_info.buf_lock);
  1005. pstore_register(&efivars->efi_pstore_info);
  1006. }
  1007. out:
  1008. kfree(variable_name);
  1009. return error;
  1010. }
  1011. EXPORT_SYMBOL_GPL(register_efivars);
  1012. static struct efivars __efivars;
  1013. static struct efivar_operations ops;
  1014. /*
  1015. * For now we register the efi subsystem with the firmware subsystem
  1016. * and the vars subsystem with the efi subsystem. In the future, it
  1017. * might make sense to split off the efi subsystem into its own
  1018. * driver, but for now only efivars will register with it, so just
  1019. * include it here.
  1020. */
  1021. static int __init
  1022. efivars_init(void)
  1023. {
  1024. int error = 0;
  1025. printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
  1026. EFIVARS_DATE);
  1027. if (!efi_enabled)
  1028. return 0;
  1029. /* For now we'll register the efi directory at /sys/firmware/efi */
  1030. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  1031. if (!efi_kobj) {
  1032. printk(KERN_ERR "efivars: Firmware registration failed.\n");
  1033. return -ENOMEM;
  1034. }
  1035. ops.get_variable = efi.get_variable;
  1036. ops.set_variable = efi.set_variable;
  1037. ops.get_next_variable = efi.get_next_variable;
  1038. error = register_efivars(&__efivars, &ops, efi_kobj);
  1039. if (error)
  1040. goto err_put;
  1041. /* Don't forget the systab entry */
  1042. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  1043. if (error) {
  1044. printk(KERN_ERR
  1045. "efivars: Sysfs attribute export failed with error %d.\n",
  1046. error);
  1047. goto err_unregister;
  1048. }
  1049. return 0;
  1050. err_unregister:
  1051. unregister_efivars(&__efivars);
  1052. err_put:
  1053. kobject_put(efi_kobj);
  1054. return error;
  1055. }
  1056. static void __exit
  1057. efivars_exit(void)
  1058. {
  1059. if (efi_enabled) {
  1060. unregister_efivars(&__efivars);
  1061. kobject_put(efi_kobj);
  1062. }
  1063. }
  1064. module_init(efivars_init);
  1065. module_exit(efivars_exit);