vars.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /*
  2. * Originally from 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 program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/capability.h>
  22. #include <linux/types.h>
  23. #include <linux/errno.h>
  24. #include <linux/init.h>
  25. #include <linux/mm.h>
  26. #include <linux/module.h>
  27. #include <linux/string.h>
  28. #include <linux/smp.h>
  29. #include <linux/efi.h>
  30. #include <linux/sysfs.h>
  31. #include <linux/device.h>
  32. #include <linux/slab.h>
  33. #include <linux/ctype.h>
  34. #include <linux/ucs2_string.h>
  35. /* Private pointer to registered efivars */
  36. static struct efivars *__efivars;
  37. static bool efivar_wq_enabled = true;
  38. DECLARE_WORK(efivar_work, NULL);
  39. EXPORT_SYMBOL_GPL(efivar_work);
  40. static bool
  41. validate_device_path(efi_char16_t *var_name, int match, u8 *buffer,
  42. unsigned long len)
  43. {
  44. struct efi_generic_dev_path *node;
  45. int offset = 0;
  46. node = (struct efi_generic_dev_path *)buffer;
  47. if (len < sizeof(*node))
  48. return false;
  49. while (offset <= len - sizeof(*node) &&
  50. node->length >= sizeof(*node) &&
  51. node->length <= len - offset) {
  52. offset += node->length;
  53. if ((node->type == EFI_DEV_END_PATH ||
  54. node->type == EFI_DEV_END_PATH2) &&
  55. node->sub_type == EFI_DEV_END_ENTIRE)
  56. return true;
  57. node = (struct efi_generic_dev_path *)(buffer + offset);
  58. }
  59. /*
  60. * If we're here then either node->length pointed past the end
  61. * of the buffer or we reached the end of the buffer without
  62. * finding a device path end node.
  63. */
  64. return false;
  65. }
  66. static bool
  67. validate_boot_order(efi_char16_t *var_name, int match, u8 *buffer,
  68. unsigned long len)
  69. {
  70. /* An array of 16-bit integers */
  71. if ((len % 2) != 0)
  72. return false;
  73. return true;
  74. }
  75. static bool
  76. validate_load_option(efi_char16_t *var_name, int match, u8 *buffer,
  77. unsigned long len)
  78. {
  79. u16 filepathlength;
  80. int i, desclength = 0, namelen;
  81. namelen = ucs2_strnlen(var_name, EFI_VAR_NAME_LEN);
  82. /* Either "Boot" or "Driver" followed by four digits of hex */
  83. for (i = match; i < match+4; i++) {
  84. if (var_name[i] > 127 ||
  85. hex_to_bin(var_name[i] & 0xff) < 0)
  86. return true;
  87. }
  88. /* Reject it if there's 4 digits of hex and then further content */
  89. if (namelen > match + 4)
  90. return false;
  91. /* A valid entry must be at least 8 bytes */
  92. if (len < 8)
  93. return false;
  94. filepathlength = buffer[4] | buffer[5] << 8;
  95. /*
  96. * There's no stored length for the description, so it has to be
  97. * found by hand
  98. */
  99. desclength = ucs2_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
  100. /* Each boot entry must have a descriptor */
  101. if (!desclength)
  102. return false;
  103. /*
  104. * If the sum of the length of the description, the claimed filepath
  105. * length and the original header are greater than the length of the
  106. * variable, it's malformed
  107. */
  108. if ((desclength + filepathlength + 6) > len)
  109. return false;
  110. /*
  111. * And, finally, check the filepath
  112. */
  113. return validate_device_path(var_name, match, buffer + desclength + 6,
  114. filepathlength);
  115. }
  116. static bool
  117. validate_uint16(efi_char16_t *var_name, int match, u8 *buffer,
  118. unsigned long len)
  119. {
  120. /* A single 16-bit integer */
  121. if (len != 2)
  122. return false;
  123. return true;
  124. }
  125. static bool
  126. validate_ascii_string(efi_char16_t *var_name, int match, u8 *buffer,
  127. unsigned long len)
  128. {
  129. int i;
  130. for (i = 0; i < len; i++) {
  131. if (buffer[i] > 127)
  132. return false;
  133. if (buffer[i] == 0)
  134. return true;
  135. }
  136. return false;
  137. }
  138. struct variable_validate {
  139. efi_guid_t vendor;
  140. char *name;
  141. bool (*validate)(efi_char16_t *var_name, int match, u8 *data,
  142. unsigned long len);
  143. };
  144. /*
  145. * This is the list of variables we need to validate, as well as the
  146. * whitelist for what we think is safe not to default to immutable.
  147. *
  148. * If it has a validate() method that's not NULL, it'll go into the
  149. * validation routine. If not, it is assumed valid, but still used for
  150. * whitelisting.
  151. *
  152. * Note that it's sorted by {vendor,name}, but globbed names must come after
  153. * any other name with the same prefix.
  154. */
  155. static const struct variable_validate variable_validate[] = {
  156. { EFI_GLOBAL_VARIABLE_GUID, "BootNext", validate_uint16 },
  157. { EFI_GLOBAL_VARIABLE_GUID, "BootOrder", validate_boot_order },
  158. { EFI_GLOBAL_VARIABLE_GUID, "Boot*", validate_load_option },
  159. { EFI_GLOBAL_VARIABLE_GUID, "DriverOrder", validate_boot_order },
  160. { EFI_GLOBAL_VARIABLE_GUID, "Driver*", validate_load_option },
  161. { EFI_GLOBAL_VARIABLE_GUID, "ConIn", validate_device_path },
  162. { EFI_GLOBAL_VARIABLE_GUID, "ConInDev", validate_device_path },
  163. { EFI_GLOBAL_VARIABLE_GUID, "ConOut", validate_device_path },
  164. { EFI_GLOBAL_VARIABLE_GUID, "ConOutDev", validate_device_path },
  165. { EFI_GLOBAL_VARIABLE_GUID, "ErrOut", validate_device_path },
  166. { EFI_GLOBAL_VARIABLE_GUID, "ErrOutDev", validate_device_path },
  167. { EFI_GLOBAL_VARIABLE_GUID, "Lang", validate_ascii_string },
  168. { EFI_GLOBAL_VARIABLE_GUID, "OsIndications", NULL },
  169. { EFI_GLOBAL_VARIABLE_GUID, "PlatformLang", validate_ascii_string },
  170. { EFI_GLOBAL_VARIABLE_GUID, "Timeout", validate_uint16 },
  171. { LINUX_EFI_CRASH_GUID, "*", NULL },
  172. { NULL_GUID, "", NULL },
  173. };
  174. static bool
  175. variable_matches(const char *var_name, size_t len, const char *match_name,
  176. int *match)
  177. {
  178. for (*match = 0; ; (*match)++) {
  179. char c = match_name[*match];
  180. char u = var_name[*match];
  181. /* Wildcard in the matching name means we've matched */
  182. if (c == '*')
  183. return true;
  184. /* Case sensitive match */
  185. if (!c && *match == len)
  186. return true;
  187. if (c != u)
  188. return false;
  189. if (!c)
  190. return true;
  191. }
  192. return true;
  193. }
  194. bool
  195. efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data,
  196. unsigned long data_size)
  197. {
  198. int i;
  199. unsigned long utf8_size;
  200. u8 *utf8_name;
  201. utf8_size = ucs2_utf8size(var_name);
  202. utf8_name = kmalloc(utf8_size + 1, GFP_KERNEL);
  203. if (!utf8_name)
  204. return false;
  205. ucs2_as_utf8(utf8_name, var_name, utf8_size);
  206. utf8_name[utf8_size] = '\0';
  207. for (i = 0; variable_validate[i].name[0] != '\0'; i++) {
  208. const char *name = variable_validate[i].name;
  209. int match = 0;
  210. if (efi_guidcmp(vendor, variable_validate[i].vendor))
  211. continue;
  212. if (variable_matches(utf8_name, utf8_size+1, name, &match)) {
  213. if (variable_validate[i].validate == NULL)
  214. break;
  215. kfree(utf8_name);
  216. return variable_validate[i].validate(var_name, match,
  217. data, data_size);
  218. }
  219. }
  220. kfree(utf8_name);
  221. return true;
  222. }
  223. EXPORT_SYMBOL_GPL(efivar_validate);
  224. bool
  225. efivar_variable_is_removable(efi_guid_t vendor, const char *var_name,
  226. size_t len)
  227. {
  228. int i;
  229. bool found = false;
  230. int match = 0;
  231. /*
  232. * Check if our variable is in the validated variables list
  233. */
  234. for (i = 0; variable_validate[i].name[0] != '\0'; i++) {
  235. if (efi_guidcmp(variable_validate[i].vendor, vendor))
  236. continue;
  237. if (variable_matches(var_name, len,
  238. variable_validate[i].name, &match)) {
  239. found = true;
  240. break;
  241. }
  242. }
  243. /*
  244. * If it's in our list, it is removable.
  245. */
  246. return found;
  247. }
  248. EXPORT_SYMBOL_GPL(efivar_variable_is_removable);
  249. static efi_status_t
  250. check_var_size(u32 attributes, unsigned long size)
  251. {
  252. const struct efivar_operations *fops = __efivars->ops;
  253. if (!fops->query_variable_store)
  254. return EFI_UNSUPPORTED;
  255. return fops->query_variable_store(attributes, size, false);
  256. }
  257. static efi_status_t
  258. check_var_size_nonblocking(u32 attributes, unsigned long size)
  259. {
  260. const struct efivar_operations *fops = __efivars->ops;
  261. if (!fops->query_variable_store)
  262. return EFI_UNSUPPORTED;
  263. return fops->query_variable_store(attributes, size, true);
  264. }
  265. static int efi_status_to_err(efi_status_t status)
  266. {
  267. int err;
  268. switch (status) {
  269. case EFI_SUCCESS:
  270. err = 0;
  271. break;
  272. case EFI_INVALID_PARAMETER:
  273. err = -EINVAL;
  274. break;
  275. case EFI_OUT_OF_RESOURCES:
  276. err = -ENOSPC;
  277. break;
  278. case EFI_DEVICE_ERROR:
  279. err = -EIO;
  280. break;
  281. case EFI_WRITE_PROTECTED:
  282. err = -EROFS;
  283. break;
  284. case EFI_SECURITY_VIOLATION:
  285. err = -EACCES;
  286. break;
  287. case EFI_NOT_FOUND:
  288. err = -ENOENT;
  289. break;
  290. default:
  291. err = -EINVAL;
  292. }
  293. return err;
  294. }
  295. static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor,
  296. struct list_head *head)
  297. {
  298. struct efivar_entry *entry, *n;
  299. unsigned long strsize1, strsize2;
  300. bool found = false;
  301. strsize1 = ucs2_strsize(variable_name, 1024);
  302. list_for_each_entry_safe(entry, n, head, list) {
  303. strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
  304. if (strsize1 == strsize2 &&
  305. !memcmp(variable_name, &(entry->var.VariableName),
  306. strsize2) &&
  307. !efi_guidcmp(entry->var.VendorGuid,
  308. *vendor)) {
  309. found = true;
  310. break;
  311. }
  312. }
  313. return found;
  314. }
  315. /*
  316. * Returns the size of variable_name, in bytes, including the
  317. * terminating NULL character, or variable_name_size if no NULL
  318. * character is found among the first variable_name_size bytes.
  319. */
  320. static unsigned long var_name_strnsize(efi_char16_t *variable_name,
  321. unsigned long variable_name_size)
  322. {
  323. unsigned long len;
  324. efi_char16_t c;
  325. /*
  326. * The variable name is, by definition, a NULL-terminated
  327. * string, so make absolutely sure that variable_name_size is
  328. * the value we expect it to be. If not, return the real size.
  329. */
  330. for (len = 2; len <= variable_name_size; len += sizeof(c)) {
  331. c = variable_name[(len / sizeof(c)) - 1];
  332. if (!c)
  333. break;
  334. }
  335. return min(len, variable_name_size);
  336. }
  337. /*
  338. * Print a warning when duplicate EFI variables are encountered and
  339. * disable the sysfs workqueue since the firmware is buggy.
  340. */
  341. static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid,
  342. unsigned long len16)
  343. {
  344. size_t i, len8 = len16 / sizeof(efi_char16_t);
  345. char *str8;
  346. /*
  347. * Disable the workqueue since the algorithm it uses for
  348. * detecting new variables won't work with this buggy
  349. * implementation of GetNextVariableName().
  350. */
  351. efivar_wq_enabled = false;
  352. str8 = kzalloc(len8, GFP_KERNEL);
  353. if (!str8)
  354. return;
  355. for (i = 0; i < len8; i++)
  356. str8[i] = str16[i];
  357. printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
  358. str8, vendor_guid);
  359. kfree(str8);
  360. }
  361. /**
  362. * efivar_init - build the initial list of EFI variables
  363. * @func: callback function to invoke for every variable
  364. * @data: function-specific data to pass to @func
  365. * @atomic: do we need to execute the @func-loop atomically?
  366. * @duplicates: error if we encounter duplicates on @head?
  367. * @head: initialised head of variable list
  368. *
  369. * Get every EFI variable from the firmware and invoke @func. @func
  370. * should call efivar_entry_add() to build the list of variables.
  371. *
  372. * Returns 0 on success, or a kernel error code on failure.
  373. */
  374. int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
  375. void *data, bool atomic, bool duplicates,
  376. struct list_head *head)
  377. {
  378. const struct efivar_operations *ops = __efivars->ops;
  379. unsigned long variable_name_size = 1024;
  380. efi_char16_t *variable_name;
  381. efi_status_t status;
  382. efi_guid_t vendor_guid;
  383. int err = 0;
  384. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  385. if (!variable_name) {
  386. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  387. return -ENOMEM;
  388. }
  389. spin_lock_irq(&__efivars->lock);
  390. /*
  391. * Per EFI spec, the maximum storage allocated for both
  392. * the variable name and variable data is 1024 bytes.
  393. */
  394. do {
  395. variable_name_size = 1024;
  396. status = ops->get_next_variable(&variable_name_size,
  397. variable_name,
  398. &vendor_guid);
  399. switch (status) {
  400. case EFI_SUCCESS:
  401. if (!atomic)
  402. spin_unlock_irq(&__efivars->lock);
  403. variable_name_size = var_name_strnsize(variable_name,
  404. variable_name_size);
  405. /*
  406. * Some firmware implementations return the
  407. * same variable name on multiple calls to
  408. * get_next_variable(). Terminate the loop
  409. * immediately as there is no guarantee that
  410. * we'll ever see a different variable name,
  411. * and may end up looping here forever.
  412. */
  413. if (duplicates &&
  414. variable_is_present(variable_name, &vendor_guid, head)) {
  415. dup_variable_bug(variable_name, &vendor_guid,
  416. variable_name_size);
  417. if (!atomic)
  418. spin_lock_irq(&__efivars->lock);
  419. status = EFI_NOT_FOUND;
  420. break;
  421. }
  422. err = func(variable_name, vendor_guid, variable_name_size, data);
  423. if (err)
  424. status = EFI_NOT_FOUND;
  425. if (!atomic)
  426. spin_lock_irq(&__efivars->lock);
  427. break;
  428. case EFI_NOT_FOUND:
  429. break;
  430. default:
  431. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  432. status);
  433. status = EFI_NOT_FOUND;
  434. break;
  435. }
  436. } while (status != EFI_NOT_FOUND);
  437. spin_unlock_irq(&__efivars->lock);
  438. kfree(variable_name);
  439. return err;
  440. }
  441. EXPORT_SYMBOL_GPL(efivar_init);
  442. /**
  443. * efivar_entry_add - add entry to variable list
  444. * @entry: entry to add to list
  445. * @head: list head
  446. */
  447. void efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
  448. {
  449. spin_lock_irq(&__efivars->lock);
  450. list_add(&entry->list, head);
  451. spin_unlock_irq(&__efivars->lock);
  452. }
  453. EXPORT_SYMBOL_GPL(efivar_entry_add);
  454. /**
  455. * efivar_entry_remove - remove entry from variable list
  456. * @entry: entry to remove from list
  457. */
  458. void efivar_entry_remove(struct efivar_entry *entry)
  459. {
  460. spin_lock_irq(&__efivars->lock);
  461. list_del(&entry->list);
  462. spin_unlock_irq(&__efivars->lock);
  463. }
  464. EXPORT_SYMBOL_GPL(efivar_entry_remove);
  465. /*
  466. * efivar_entry_list_del_unlock - remove entry from variable list
  467. * @entry: entry to remove
  468. *
  469. * Remove @entry from the variable list and release the list lock.
  470. *
  471. * NOTE: slightly weird locking semantics here - we expect to be
  472. * called with the efivars lock already held, and we release it before
  473. * returning. This is because this function is usually called after
  474. * set_variable() while the lock is still held.
  475. */
  476. static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
  477. {
  478. lockdep_assert_held(&__efivars->lock);
  479. list_del(&entry->list);
  480. spin_unlock_irq(&__efivars->lock);
  481. }
  482. /**
  483. * __efivar_entry_delete - delete an EFI variable
  484. * @entry: entry containing EFI variable to delete
  485. *
  486. * Delete the variable from the firmware but leave @entry on the
  487. * variable list.
  488. *
  489. * This function differs from efivar_entry_delete() because it does
  490. * not remove @entry from the variable list. Also, it is safe to be
  491. * called from within a efivar_entry_iter_begin() and
  492. * efivar_entry_iter_end() region, unlike efivar_entry_delete().
  493. *
  494. * Returns 0 on success, or a converted EFI status code if
  495. * set_variable() fails.
  496. */
  497. int __efivar_entry_delete(struct efivar_entry *entry)
  498. {
  499. const struct efivar_operations *ops = __efivars->ops;
  500. efi_status_t status;
  501. lockdep_assert_held(&__efivars->lock);
  502. status = ops->set_variable(entry->var.VariableName,
  503. &entry->var.VendorGuid,
  504. 0, 0, NULL);
  505. return efi_status_to_err(status);
  506. }
  507. EXPORT_SYMBOL_GPL(__efivar_entry_delete);
  508. /**
  509. * efivar_entry_delete - delete variable and remove entry from list
  510. * @entry: entry containing variable to delete
  511. *
  512. * Delete the variable from the firmware and remove @entry from the
  513. * variable list. It is the caller's responsibility to free @entry
  514. * once we return.
  515. *
  516. * Returns 0 on success, or a converted EFI status code if
  517. * set_variable() fails.
  518. */
  519. int efivar_entry_delete(struct efivar_entry *entry)
  520. {
  521. const struct efivar_operations *ops = __efivars->ops;
  522. efi_status_t status;
  523. spin_lock_irq(&__efivars->lock);
  524. status = ops->set_variable(entry->var.VariableName,
  525. &entry->var.VendorGuid,
  526. 0, 0, NULL);
  527. if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
  528. spin_unlock_irq(&__efivars->lock);
  529. return efi_status_to_err(status);
  530. }
  531. efivar_entry_list_del_unlock(entry);
  532. return 0;
  533. }
  534. EXPORT_SYMBOL_GPL(efivar_entry_delete);
  535. /**
  536. * efivar_entry_set - call set_variable()
  537. * @entry: entry containing the EFI variable to write
  538. * @attributes: variable attributes
  539. * @size: size of @data buffer
  540. * @data: buffer containing variable data
  541. * @head: head of variable list
  542. *
  543. * Calls set_variable() for an EFI variable. If creating a new EFI
  544. * variable, this function is usually followed by efivar_entry_add().
  545. *
  546. * Before writing the variable, the remaining EFI variable storage
  547. * space is checked to ensure there is enough room available.
  548. *
  549. * If @head is not NULL a lookup is performed to determine whether
  550. * the entry is already on the list.
  551. *
  552. * Returns 0 on success, -EEXIST if a lookup is performed and the entry
  553. * already exists on the list, or a converted EFI status code if
  554. * set_variable() fails.
  555. */
  556. int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
  557. unsigned long size, void *data, struct list_head *head)
  558. {
  559. const struct efivar_operations *ops = __efivars->ops;
  560. efi_status_t status;
  561. efi_char16_t *name = entry->var.VariableName;
  562. efi_guid_t vendor = entry->var.VendorGuid;
  563. spin_lock_irq(&__efivars->lock);
  564. if (head && efivar_entry_find(name, vendor, head, false)) {
  565. spin_unlock_irq(&__efivars->lock);
  566. return -EEXIST;
  567. }
  568. status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
  569. if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
  570. status = ops->set_variable(name, &vendor,
  571. attributes, size, data);
  572. spin_unlock_irq(&__efivars->lock);
  573. return efi_status_to_err(status);
  574. }
  575. EXPORT_SYMBOL_GPL(efivar_entry_set);
  576. /*
  577. * efivar_entry_set_nonblocking - call set_variable_nonblocking()
  578. *
  579. * This function is guaranteed to not block and is suitable for calling
  580. * from crash/panic handlers.
  581. *
  582. * Crucially, this function will not block if it cannot acquire
  583. * __efivars->lock. Instead, it returns -EBUSY.
  584. */
  585. static int
  586. efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
  587. u32 attributes, unsigned long size, void *data)
  588. {
  589. const struct efivar_operations *ops = __efivars->ops;
  590. unsigned long flags;
  591. efi_status_t status;
  592. if (!spin_trylock_irqsave(&__efivars->lock, flags))
  593. return -EBUSY;
  594. status = check_var_size_nonblocking(attributes,
  595. size + ucs2_strsize(name, 1024));
  596. if (status != EFI_SUCCESS) {
  597. spin_unlock_irqrestore(&__efivars->lock, flags);
  598. return -ENOSPC;
  599. }
  600. status = ops->set_variable_nonblocking(name, &vendor, attributes,
  601. size, data);
  602. spin_unlock_irqrestore(&__efivars->lock, flags);
  603. return efi_status_to_err(status);
  604. }
  605. /**
  606. * efivar_entry_set_safe - call set_variable() if enough space in firmware
  607. * @name: buffer containing the variable name
  608. * @vendor: variable vendor guid
  609. * @attributes: variable attributes
  610. * @block: can we block in this context?
  611. * @size: size of @data buffer
  612. * @data: buffer containing variable data
  613. *
  614. * Ensures there is enough free storage in the firmware for this variable, and
  615. * if so, calls set_variable(). If creating a new EFI variable, this function
  616. * is usually followed by efivar_entry_add().
  617. *
  618. * Returns 0 on success, -ENOSPC if the firmware does not have enough
  619. * space for set_variable() to succeed, or a converted EFI status code
  620. * if set_variable() fails.
  621. */
  622. int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
  623. bool block, unsigned long size, void *data)
  624. {
  625. const struct efivar_operations *ops = __efivars->ops;
  626. unsigned long flags;
  627. efi_status_t status;
  628. if (!ops->query_variable_store)
  629. return -ENOSYS;
  630. /*
  631. * If the EFI variable backend provides a non-blocking
  632. * ->set_variable() operation and we're in a context where we
  633. * cannot block, then we need to use it to avoid live-locks,
  634. * since the implication is that the regular ->set_variable()
  635. * will block.
  636. *
  637. * If no ->set_variable_nonblocking() is provided then
  638. * ->set_variable() is assumed to be non-blocking.
  639. */
  640. if (!block && ops->set_variable_nonblocking)
  641. return efivar_entry_set_nonblocking(name, vendor, attributes,
  642. size, data);
  643. if (!block) {
  644. if (!spin_trylock_irqsave(&__efivars->lock, flags))
  645. return -EBUSY;
  646. } else {
  647. spin_lock_irqsave(&__efivars->lock, flags);
  648. }
  649. status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
  650. if (status != EFI_SUCCESS) {
  651. spin_unlock_irqrestore(&__efivars->lock, flags);
  652. return -ENOSPC;
  653. }
  654. status = ops->set_variable(name, &vendor, attributes, size, data);
  655. spin_unlock_irqrestore(&__efivars->lock, flags);
  656. return efi_status_to_err(status);
  657. }
  658. EXPORT_SYMBOL_GPL(efivar_entry_set_safe);
  659. /**
  660. * efivar_entry_find - search for an entry
  661. * @name: the EFI variable name
  662. * @guid: the EFI variable vendor's guid
  663. * @head: head of the variable list
  664. * @remove: should we remove the entry from the list?
  665. *
  666. * Search for an entry on the variable list that has the EFI variable
  667. * name @name and vendor guid @guid. If an entry is found on the list
  668. * and @remove is true, the entry is removed from the list.
  669. *
  670. * The caller MUST call efivar_entry_iter_begin() and
  671. * efivar_entry_iter_end() before and after the invocation of this
  672. * function, respectively.
  673. *
  674. * Returns the entry if found on the list, %NULL otherwise.
  675. */
  676. struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
  677. struct list_head *head, bool remove)
  678. {
  679. struct efivar_entry *entry, *n;
  680. int strsize1, strsize2;
  681. bool found = false;
  682. lockdep_assert_held(&__efivars->lock);
  683. list_for_each_entry_safe(entry, n, head, list) {
  684. strsize1 = ucs2_strsize(name, 1024);
  685. strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
  686. if (strsize1 == strsize2 &&
  687. !memcmp(name, &(entry->var.VariableName), strsize1) &&
  688. !efi_guidcmp(guid, entry->var.VendorGuid)) {
  689. found = true;
  690. break;
  691. }
  692. }
  693. if (!found)
  694. return NULL;
  695. if (remove) {
  696. if (entry->scanning) {
  697. /*
  698. * The entry will be deleted
  699. * after scanning is completed.
  700. */
  701. entry->deleting = true;
  702. } else
  703. list_del(&entry->list);
  704. }
  705. return entry;
  706. }
  707. EXPORT_SYMBOL_GPL(efivar_entry_find);
  708. /**
  709. * efivar_entry_size - obtain the size of a variable
  710. * @entry: entry for this variable
  711. * @size: location to store the variable's size
  712. */
  713. int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
  714. {
  715. const struct efivar_operations *ops = __efivars->ops;
  716. efi_status_t status;
  717. *size = 0;
  718. spin_lock_irq(&__efivars->lock);
  719. status = ops->get_variable(entry->var.VariableName,
  720. &entry->var.VendorGuid, NULL, size, NULL);
  721. spin_unlock_irq(&__efivars->lock);
  722. if (status != EFI_BUFFER_TOO_SMALL)
  723. return efi_status_to_err(status);
  724. return 0;
  725. }
  726. EXPORT_SYMBOL_GPL(efivar_entry_size);
  727. /**
  728. * __efivar_entry_get - call get_variable()
  729. * @entry: read data for this variable
  730. * @attributes: variable attributes
  731. * @size: size of @data buffer
  732. * @data: buffer to store variable data
  733. *
  734. * The caller MUST call efivar_entry_iter_begin() and
  735. * efivar_entry_iter_end() before and after the invocation of this
  736. * function, respectively.
  737. */
  738. int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
  739. unsigned long *size, void *data)
  740. {
  741. const struct efivar_operations *ops = __efivars->ops;
  742. efi_status_t status;
  743. lockdep_assert_held(&__efivars->lock);
  744. status = ops->get_variable(entry->var.VariableName,
  745. &entry->var.VendorGuid,
  746. attributes, size, data);
  747. return efi_status_to_err(status);
  748. }
  749. EXPORT_SYMBOL_GPL(__efivar_entry_get);
  750. /**
  751. * efivar_entry_get - call get_variable()
  752. * @entry: read data for this variable
  753. * @attributes: variable attributes
  754. * @size: size of @data buffer
  755. * @data: buffer to store variable data
  756. */
  757. int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
  758. unsigned long *size, void *data)
  759. {
  760. const struct efivar_operations *ops = __efivars->ops;
  761. efi_status_t status;
  762. spin_lock_irq(&__efivars->lock);
  763. status = ops->get_variable(entry->var.VariableName,
  764. &entry->var.VendorGuid,
  765. attributes, size, data);
  766. spin_unlock_irq(&__efivars->lock);
  767. return efi_status_to_err(status);
  768. }
  769. EXPORT_SYMBOL_GPL(efivar_entry_get);
  770. /**
  771. * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
  772. * @entry: entry containing variable to set and get
  773. * @attributes: attributes of variable to be written
  774. * @size: size of data buffer
  775. * @data: buffer containing data to write
  776. * @set: did the set_variable() call succeed?
  777. *
  778. * This is a pretty special (complex) function. See efivarfs_file_write().
  779. *
  780. * Atomically call set_variable() for @entry and if the call is
  781. * successful, return the new size of the variable from get_variable()
  782. * in @size. The success of set_variable() is indicated by @set.
  783. *
  784. * Returns 0 on success, -EINVAL if the variable data is invalid,
  785. * -ENOSPC if the firmware does not have enough available space, or a
  786. * converted EFI status code if either of set_variable() or
  787. * get_variable() fail.
  788. *
  789. * If the EFI variable does not exist when calling set_variable()
  790. * (EFI_NOT_FOUND), @entry is removed from the variable list.
  791. */
  792. int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
  793. unsigned long *size, void *data, bool *set)
  794. {
  795. const struct efivar_operations *ops = __efivars->ops;
  796. efi_char16_t *name = entry->var.VariableName;
  797. efi_guid_t *vendor = &entry->var.VendorGuid;
  798. efi_status_t status;
  799. int err;
  800. *set = false;
  801. if (efivar_validate(*vendor, name, data, *size) == false)
  802. return -EINVAL;
  803. /*
  804. * The lock here protects the get_variable call, the conditional
  805. * set_variable call, and removal of the variable from the efivars
  806. * list (in the case of an authenticated delete).
  807. */
  808. spin_lock_irq(&__efivars->lock);
  809. /*
  810. * Ensure that the available space hasn't shrunk below the safe level
  811. */
  812. status = check_var_size(attributes, *size + ucs2_strsize(name, 1024));
  813. if (status != EFI_SUCCESS) {
  814. if (status != EFI_UNSUPPORTED) {
  815. err = efi_status_to_err(status);
  816. goto out;
  817. }
  818. if (*size > 65536) {
  819. err = -ENOSPC;
  820. goto out;
  821. }
  822. }
  823. status = ops->set_variable(name, vendor, attributes, *size, data);
  824. if (status != EFI_SUCCESS) {
  825. err = efi_status_to_err(status);
  826. goto out;
  827. }
  828. *set = true;
  829. /*
  830. * Writing to the variable may have caused a change in size (which
  831. * could either be an append or an overwrite), or the variable to be
  832. * deleted. Perform a GetVariable() so we can tell what actually
  833. * happened.
  834. */
  835. *size = 0;
  836. status = ops->get_variable(entry->var.VariableName,
  837. &entry->var.VendorGuid,
  838. NULL, size, NULL);
  839. if (status == EFI_NOT_FOUND)
  840. efivar_entry_list_del_unlock(entry);
  841. else
  842. spin_unlock_irq(&__efivars->lock);
  843. if (status && status != EFI_BUFFER_TOO_SMALL)
  844. return efi_status_to_err(status);
  845. return 0;
  846. out:
  847. spin_unlock_irq(&__efivars->lock);
  848. return err;
  849. }
  850. EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
  851. /**
  852. * efivar_entry_iter_begin - begin iterating the variable list
  853. *
  854. * Lock the variable list to prevent entry insertion and removal until
  855. * efivar_entry_iter_end() is called. This function is usually used in
  856. * conjunction with __efivar_entry_iter() or efivar_entry_iter().
  857. */
  858. void efivar_entry_iter_begin(void)
  859. {
  860. spin_lock_irq(&__efivars->lock);
  861. }
  862. EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
  863. /**
  864. * efivar_entry_iter_end - finish iterating the variable list
  865. *
  866. * Unlock the variable list and allow modifications to the list again.
  867. */
  868. void efivar_entry_iter_end(void)
  869. {
  870. spin_unlock_irq(&__efivars->lock);
  871. }
  872. EXPORT_SYMBOL_GPL(efivar_entry_iter_end);
  873. /**
  874. * __efivar_entry_iter - iterate over variable list
  875. * @func: callback function
  876. * @head: head of the variable list
  877. * @data: function-specific data to pass to callback
  878. * @prev: entry to begin iterating from
  879. *
  880. * Iterate over the list of EFI variables and call @func with every
  881. * entry on the list. It is safe for @func to remove entries in the
  882. * list via efivar_entry_delete().
  883. *
  884. * You MUST call efivar_enter_iter_begin() before this function, and
  885. * efivar_entry_iter_end() afterwards.
  886. *
  887. * It is possible to begin iteration from an arbitrary entry within
  888. * the list by passing @prev. @prev is updated on return to point to
  889. * the last entry passed to @func. To begin iterating from the
  890. * beginning of the list @prev must be %NULL.
  891. *
  892. * The restrictions for @func are the same as documented for
  893. * efivar_entry_iter().
  894. */
  895. int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
  896. struct list_head *head, void *data,
  897. struct efivar_entry **prev)
  898. {
  899. struct efivar_entry *entry, *n;
  900. int err = 0;
  901. if (!prev || !*prev) {
  902. list_for_each_entry_safe(entry, n, head, list) {
  903. err = func(entry, data);
  904. if (err)
  905. break;
  906. }
  907. if (prev)
  908. *prev = entry;
  909. return err;
  910. }
  911. list_for_each_entry_safe_continue((*prev), n, head, list) {
  912. err = func(*prev, data);
  913. if (err)
  914. break;
  915. }
  916. return err;
  917. }
  918. EXPORT_SYMBOL_GPL(__efivar_entry_iter);
  919. /**
  920. * efivar_entry_iter - iterate over variable list
  921. * @func: callback function
  922. * @head: head of variable list
  923. * @data: function-specific data to pass to callback
  924. *
  925. * Iterate over the list of EFI variables and call @func with every
  926. * entry on the list. It is safe for @func to remove entries in the
  927. * list via efivar_entry_delete() while iterating.
  928. *
  929. * Some notes for the callback function:
  930. * - a non-zero return value indicates an error and terminates the loop
  931. * - @func is called from atomic context
  932. */
  933. int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
  934. struct list_head *head, void *data)
  935. {
  936. int err = 0;
  937. efivar_entry_iter_begin();
  938. err = __efivar_entry_iter(func, head, data, NULL);
  939. efivar_entry_iter_end();
  940. return err;
  941. }
  942. EXPORT_SYMBOL_GPL(efivar_entry_iter);
  943. /**
  944. * efivars_kobject - get the kobject for the registered efivars
  945. *
  946. * If efivars_register() has not been called we return NULL,
  947. * otherwise return the kobject used at registration time.
  948. */
  949. struct kobject *efivars_kobject(void)
  950. {
  951. if (!__efivars)
  952. return NULL;
  953. return __efivars->kobject;
  954. }
  955. EXPORT_SYMBOL_GPL(efivars_kobject);
  956. /**
  957. * efivar_run_worker - schedule the efivar worker thread
  958. */
  959. void efivar_run_worker(void)
  960. {
  961. if (efivar_wq_enabled)
  962. schedule_work(&efivar_work);
  963. }
  964. EXPORT_SYMBOL_GPL(efivar_run_worker);
  965. /**
  966. * efivars_register - register an efivars
  967. * @efivars: efivars to register
  968. * @ops: efivars operations
  969. * @kobject: @efivars-specific kobject
  970. *
  971. * Only a single efivars can be registered at any time.
  972. */
  973. int efivars_register(struct efivars *efivars,
  974. const struct efivar_operations *ops,
  975. struct kobject *kobject)
  976. {
  977. spin_lock_init(&efivars->lock);
  978. efivars->ops = ops;
  979. efivars->kobject = kobject;
  980. __efivars = efivars;
  981. return 0;
  982. }
  983. EXPORT_SYMBOL_GPL(efivars_register);
  984. /**
  985. * efivars_unregister - unregister an efivars
  986. * @efivars: efivars to unregister
  987. *
  988. * The caller must have already removed every entry from the list,
  989. * failure to do so is an error.
  990. */
  991. int efivars_unregister(struct efivars *efivars)
  992. {
  993. int rv;
  994. if (!__efivars) {
  995. printk(KERN_ERR "efivars not registered\n");
  996. rv = -EINVAL;
  997. goto out;
  998. }
  999. if (__efivars != efivars) {
  1000. rv = -EINVAL;
  1001. goto out;
  1002. }
  1003. __efivars = NULL;
  1004. rv = 0;
  1005. out:
  1006. return rv;
  1007. }
  1008. EXPORT_SYMBOL_GPL(efivars_unregister);