efivars.c 26 KB

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