efivars.c 31 KB

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