vars.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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. /*
  175. * Check if @var_name matches the pattern given in @match_name.
  176. *
  177. * @var_name: an array of @len non-NUL characters.
  178. * @match_name: a NUL-terminated pattern string, optionally ending in "*". A
  179. * final "*" character matches any trailing characters @var_name,
  180. * including the case when there are none left in @var_name.
  181. * @match: on output, the number of non-wildcard characters in @match_name
  182. * that @var_name matches, regardless of the return value.
  183. * @return: whether @var_name fully matches @match_name.
  184. */
  185. static bool
  186. variable_matches(const char *var_name, size_t len, const char *match_name,
  187. int *match)
  188. {
  189. for (*match = 0; ; (*match)++) {
  190. char c = match_name[*match];
  191. switch (c) {
  192. case '*':
  193. /* Wildcard in @match_name means we've matched. */
  194. return true;
  195. case '\0':
  196. /* @match_name has ended. Has @var_name too? */
  197. return (*match == len);
  198. default:
  199. /*
  200. * We've reached a non-wildcard char in @match_name.
  201. * Continue only if there's an identical character in
  202. * @var_name.
  203. */
  204. if (*match < len && c == var_name[*match])
  205. continue;
  206. return false;
  207. }
  208. }
  209. }
  210. bool
  211. efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data,
  212. unsigned long data_size)
  213. {
  214. int i;
  215. unsigned long utf8_size;
  216. u8 *utf8_name;
  217. utf8_size = ucs2_utf8size(var_name);
  218. utf8_name = kmalloc(utf8_size + 1, GFP_KERNEL);
  219. if (!utf8_name)
  220. return false;
  221. ucs2_as_utf8(utf8_name, var_name, utf8_size);
  222. utf8_name[utf8_size] = '\0';
  223. for (i = 0; variable_validate[i].name[0] != '\0'; i++) {
  224. const char *name = variable_validate[i].name;
  225. int match = 0;
  226. if (efi_guidcmp(vendor, variable_validate[i].vendor))
  227. continue;
  228. if (variable_matches(utf8_name, utf8_size+1, name, &match)) {
  229. if (variable_validate[i].validate == NULL)
  230. break;
  231. kfree(utf8_name);
  232. return variable_validate[i].validate(var_name, match,
  233. data, data_size);
  234. }
  235. }
  236. kfree(utf8_name);
  237. return true;
  238. }
  239. EXPORT_SYMBOL_GPL(efivar_validate);
  240. bool
  241. efivar_variable_is_removable(efi_guid_t vendor, const char *var_name,
  242. size_t len)
  243. {
  244. int i;
  245. bool found = false;
  246. int match = 0;
  247. /*
  248. * Check if our variable is in the validated variables list
  249. */
  250. for (i = 0; variable_validate[i].name[0] != '\0'; i++) {
  251. if (efi_guidcmp(variable_validate[i].vendor, vendor))
  252. continue;
  253. if (variable_matches(var_name, len,
  254. variable_validate[i].name, &match)) {
  255. found = true;
  256. break;
  257. }
  258. }
  259. /*
  260. * If it's in our list, it is removable.
  261. */
  262. return found;
  263. }
  264. EXPORT_SYMBOL_GPL(efivar_variable_is_removable);
  265. static efi_status_t
  266. check_var_size(u32 attributes, unsigned long size)
  267. {
  268. const struct efivar_operations *fops = __efivars->ops;
  269. if (!fops->query_variable_store)
  270. return EFI_UNSUPPORTED;
  271. return fops->query_variable_store(attributes, size, false);
  272. }
  273. static efi_status_t
  274. check_var_size_nonblocking(u32 attributes, unsigned long size)
  275. {
  276. const struct efivar_operations *fops = __efivars->ops;
  277. if (!fops->query_variable_store)
  278. return EFI_UNSUPPORTED;
  279. return fops->query_variable_store(attributes, size, true);
  280. }
  281. static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor,
  282. struct list_head *head)
  283. {
  284. struct efivar_entry *entry, *n;
  285. unsigned long strsize1, strsize2;
  286. bool found = false;
  287. strsize1 = ucs2_strsize(variable_name, 1024);
  288. list_for_each_entry_safe(entry, n, head, list) {
  289. strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
  290. if (strsize1 == strsize2 &&
  291. !memcmp(variable_name, &(entry->var.VariableName),
  292. strsize2) &&
  293. !efi_guidcmp(entry->var.VendorGuid,
  294. *vendor)) {
  295. found = true;
  296. break;
  297. }
  298. }
  299. return found;
  300. }
  301. /*
  302. * Returns the size of variable_name, in bytes, including the
  303. * terminating NULL character, or variable_name_size if no NULL
  304. * character is found among the first variable_name_size bytes.
  305. */
  306. static unsigned long var_name_strnsize(efi_char16_t *variable_name,
  307. unsigned long variable_name_size)
  308. {
  309. unsigned long len;
  310. efi_char16_t c;
  311. /*
  312. * The variable name is, by definition, a NULL-terminated
  313. * string, so make absolutely sure that variable_name_size is
  314. * the value we expect it to be. If not, return the real size.
  315. */
  316. for (len = 2; len <= variable_name_size; len += sizeof(c)) {
  317. c = variable_name[(len / sizeof(c)) - 1];
  318. if (!c)
  319. break;
  320. }
  321. return min(len, variable_name_size);
  322. }
  323. /*
  324. * Print a warning when duplicate EFI variables are encountered and
  325. * disable the sysfs workqueue since the firmware is buggy.
  326. */
  327. static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid,
  328. unsigned long len16)
  329. {
  330. size_t i, len8 = len16 / sizeof(efi_char16_t);
  331. char *str8;
  332. /*
  333. * Disable the workqueue since the algorithm it uses for
  334. * detecting new variables won't work with this buggy
  335. * implementation of GetNextVariableName().
  336. */
  337. efivar_wq_enabled = false;
  338. str8 = kzalloc(len8, GFP_KERNEL);
  339. if (!str8)
  340. return;
  341. for (i = 0; i < len8; i++)
  342. str8[i] = str16[i];
  343. printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
  344. str8, vendor_guid);
  345. kfree(str8);
  346. }
  347. /**
  348. * efivar_init - build the initial list of EFI variables
  349. * @func: callback function to invoke for every variable
  350. * @data: function-specific data to pass to @func
  351. * @atomic: do we need to execute the @func-loop atomically?
  352. * @duplicates: error if we encounter duplicates on @head?
  353. * @head: initialised head of variable list
  354. *
  355. * Get every EFI variable from the firmware and invoke @func. @func
  356. * should call efivar_entry_add() to build the list of variables.
  357. *
  358. * Returns 0 on success, or a kernel error code on failure.
  359. */
  360. int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
  361. void *data, bool duplicates, struct list_head *head)
  362. {
  363. const struct efivar_operations *ops = __efivars->ops;
  364. unsigned long variable_name_size = 1024;
  365. efi_char16_t *variable_name;
  366. efi_status_t status;
  367. efi_guid_t vendor_guid;
  368. int err = 0;
  369. variable_name = kzalloc(variable_name_size, GFP_KERNEL);
  370. if (!variable_name) {
  371. printk(KERN_ERR "efivars: Memory allocation failed.\n");
  372. return -ENOMEM;
  373. }
  374. spin_lock_irq(&__efivars->lock);
  375. /*
  376. * Per EFI spec, the maximum storage allocated for both
  377. * the variable name and variable data is 1024 bytes.
  378. */
  379. do {
  380. variable_name_size = 1024;
  381. status = ops->get_next_variable(&variable_name_size,
  382. variable_name,
  383. &vendor_guid);
  384. switch (status) {
  385. case EFI_SUCCESS:
  386. if (duplicates)
  387. spin_unlock_irq(&__efivars->lock);
  388. variable_name_size = var_name_strnsize(variable_name,
  389. variable_name_size);
  390. /*
  391. * Some firmware implementations return the
  392. * same variable name on multiple calls to
  393. * get_next_variable(). Terminate the loop
  394. * immediately as there is no guarantee that
  395. * we'll ever see a different variable name,
  396. * and may end up looping here forever.
  397. */
  398. if (duplicates &&
  399. variable_is_present(variable_name, &vendor_guid,
  400. head)) {
  401. dup_variable_bug(variable_name, &vendor_guid,
  402. variable_name_size);
  403. status = EFI_NOT_FOUND;
  404. } else {
  405. err = func(variable_name, vendor_guid,
  406. variable_name_size, data);
  407. if (err)
  408. status = EFI_NOT_FOUND;
  409. }
  410. if (duplicates)
  411. spin_lock_irq(&__efivars->lock);
  412. break;
  413. case EFI_NOT_FOUND:
  414. break;
  415. default:
  416. printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
  417. status);
  418. status = EFI_NOT_FOUND;
  419. break;
  420. }
  421. } while (status != EFI_NOT_FOUND);
  422. spin_unlock_irq(&__efivars->lock);
  423. kfree(variable_name);
  424. return err;
  425. }
  426. EXPORT_SYMBOL_GPL(efivar_init);
  427. /**
  428. * efivar_entry_add - add entry to variable list
  429. * @entry: entry to add to list
  430. * @head: list head
  431. */
  432. void efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
  433. {
  434. spin_lock_irq(&__efivars->lock);
  435. list_add(&entry->list, head);
  436. spin_unlock_irq(&__efivars->lock);
  437. }
  438. EXPORT_SYMBOL_GPL(efivar_entry_add);
  439. /**
  440. * efivar_entry_remove - remove entry from variable list
  441. * @entry: entry to remove from list
  442. */
  443. void efivar_entry_remove(struct efivar_entry *entry)
  444. {
  445. spin_lock_irq(&__efivars->lock);
  446. list_del(&entry->list);
  447. spin_unlock_irq(&__efivars->lock);
  448. }
  449. EXPORT_SYMBOL_GPL(efivar_entry_remove);
  450. /*
  451. * efivar_entry_list_del_unlock - remove entry from variable list
  452. * @entry: entry to remove
  453. *
  454. * Remove @entry from the variable list and release the list lock.
  455. *
  456. * NOTE: slightly weird locking semantics here - we expect to be
  457. * called with the efivars lock already held, and we release it before
  458. * returning. This is because this function is usually called after
  459. * set_variable() while the lock is still held.
  460. */
  461. static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
  462. {
  463. lockdep_assert_held(&__efivars->lock);
  464. list_del(&entry->list);
  465. spin_unlock_irq(&__efivars->lock);
  466. }
  467. /**
  468. * __efivar_entry_delete - delete an EFI variable
  469. * @entry: entry containing EFI variable to delete
  470. *
  471. * Delete the variable from the firmware but leave @entry on the
  472. * variable list.
  473. *
  474. * This function differs from efivar_entry_delete() because it does
  475. * not remove @entry from the variable list. Also, it is safe to be
  476. * called from within a efivar_entry_iter_begin() and
  477. * efivar_entry_iter_end() region, unlike efivar_entry_delete().
  478. *
  479. * Returns 0 on success, or a converted EFI status code if
  480. * set_variable() fails.
  481. */
  482. int __efivar_entry_delete(struct efivar_entry *entry)
  483. {
  484. const struct efivar_operations *ops = __efivars->ops;
  485. efi_status_t status;
  486. lockdep_assert_held(&__efivars->lock);
  487. status = ops->set_variable(entry->var.VariableName,
  488. &entry->var.VendorGuid,
  489. 0, 0, NULL);
  490. return efi_status_to_err(status);
  491. }
  492. EXPORT_SYMBOL_GPL(__efivar_entry_delete);
  493. /**
  494. * efivar_entry_delete - delete variable and remove entry from list
  495. * @entry: entry containing variable to delete
  496. *
  497. * Delete the variable from the firmware and remove @entry from the
  498. * variable list. It is the caller's responsibility to free @entry
  499. * once we return.
  500. *
  501. * Returns 0 on success, or a converted EFI status code if
  502. * set_variable() fails.
  503. */
  504. int efivar_entry_delete(struct efivar_entry *entry)
  505. {
  506. const struct efivar_operations *ops = __efivars->ops;
  507. efi_status_t status;
  508. spin_lock_irq(&__efivars->lock);
  509. status = ops->set_variable(entry->var.VariableName,
  510. &entry->var.VendorGuid,
  511. 0, 0, NULL);
  512. if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
  513. spin_unlock_irq(&__efivars->lock);
  514. return efi_status_to_err(status);
  515. }
  516. efivar_entry_list_del_unlock(entry);
  517. return 0;
  518. }
  519. EXPORT_SYMBOL_GPL(efivar_entry_delete);
  520. /**
  521. * efivar_entry_set - call set_variable()
  522. * @entry: entry containing the EFI variable to write
  523. * @attributes: variable attributes
  524. * @size: size of @data buffer
  525. * @data: buffer containing variable data
  526. * @head: head of variable list
  527. *
  528. * Calls set_variable() for an EFI variable. If creating a new EFI
  529. * variable, this function is usually followed by efivar_entry_add().
  530. *
  531. * Before writing the variable, the remaining EFI variable storage
  532. * space is checked to ensure there is enough room available.
  533. *
  534. * If @head is not NULL a lookup is performed to determine whether
  535. * the entry is already on the list.
  536. *
  537. * Returns 0 on success, -EEXIST if a lookup is performed and the entry
  538. * already exists on the list, or a converted EFI status code if
  539. * set_variable() fails.
  540. */
  541. int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
  542. unsigned long size, void *data, struct list_head *head)
  543. {
  544. const struct efivar_operations *ops = __efivars->ops;
  545. efi_status_t status;
  546. efi_char16_t *name = entry->var.VariableName;
  547. efi_guid_t vendor = entry->var.VendorGuid;
  548. spin_lock_irq(&__efivars->lock);
  549. if (head && efivar_entry_find(name, vendor, head, false)) {
  550. spin_unlock_irq(&__efivars->lock);
  551. return -EEXIST;
  552. }
  553. status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
  554. if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
  555. status = ops->set_variable(name, &vendor,
  556. attributes, size, data);
  557. spin_unlock_irq(&__efivars->lock);
  558. return efi_status_to_err(status);
  559. }
  560. EXPORT_SYMBOL_GPL(efivar_entry_set);
  561. /*
  562. * efivar_entry_set_nonblocking - call set_variable_nonblocking()
  563. *
  564. * This function is guaranteed to not block and is suitable for calling
  565. * from crash/panic handlers.
  566. *
  567. * Crucially, this function will not block if it cannot acquire
  568. * __efivars->lock. Instead, it returns -EBUSY.
  569. */
  570. static int
  571. efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
  572. u32 attributes, unsigned long size, void *data)
  573. {
  574. const struct efivar_operations *ops = __efivars->ops;
  575. unsigned long flags;
  576. efi_status_t status;
  577. if (!spin_trylock_irqsave(&__efivars->lock, flags))
  578. return -EBUSY;
  579. status = check_var_size_nonblocking(attributes,
  580. size + ucs2_strsize(name, 1024));
  581. if (status != EFI_SUCCESS) {
  582. spin_unlock_irqrestore(&__efivars->lock, flags);
  583. return -ENOSPC;
  584. }
  585. status = ops->set_variable_nonblocking(name, &vendor, attributes,
  586. size, data);
  587. spin_unlock_irqrestore(&__efivars->lock, flags);
  588. return efi_status_to_err(status);
  589. }
  590. /**
  591. * efivar_entry_set_safe - call set_variable() if enough space in firmware
  592. * @name: buffer containing the variable name
  593. * @vendor: variable vendor guid
  594. * @attributes: variable attributes
  595. * @block: can we block in this context?
  596. * @size: size of @data buffer
  597. * @data: buffer containing variable data
  598. *
  599. * Ensures there is enough free storage in the firmware for this variable, and
  600. * if so, calls set_variable(). If creating a new EFI variable, this function
  601. * is usually followed by efivar_entry_add().
  602. *
  603. * Returns 0 on success, -ENOSPC if the firmware does not have enough
  604. * space for set_variable() to succeed, or a converted EFI status code
  605. * if set_variable() fails.
  606. */
  607. int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
  608. bool block, unsigned long size, void *data)
  609. {
  610. const struct efivar_operations *ops = __efivars->ops;
  611. unsigned long flags;
  612. efi_status_t status;
  613. if (!ops->query_variable_store)
  614. return -ENOSYS;
  615. /*
  616. * If the EFI variable backend provides a non-blocking
  617. * ->set_variable() operation and we're in a context where we
  618. * cannot block, then we need to use it to avoid live-locks,
  619. * since the implication is that the regular ->set_variable()
  620. * will block.
  621. *
  622. * If no ->set_variable_nonblocking() is provided then
  623. * ->set_variable() is assumed to be non-blocking.
  624. */
  625. if (!block && ops->set_variable_nonblocking)
  626. return efivar_entry_set_nonblocking(name, vendor, attributes,
  627. size, data);
  628. if (!block) {
  629. if (!spin_trylock_irqsave(&__efivars->lock, flags))
  630. return -EBUSY;
  631. } else {
  632. spin_lock_irqsave(&__efivars->lock, flags);
  633. }
  634. status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
  635. if (status != EFI_SUCCESS) {
  636. spin_unlock_irqrestore(&__efivars->lock, flags);
  637. return -ENOSPC;
  638. }
  639. status = ops->set_variable(name, &vendor, attributes, size, data);
  640. spin_unlock_irqrestore(&__efivars->lock, flags);
  641. return efi_status_to_err(status);
  642. }
  643. EXPORT_SYMBOL_GPL(efivar_entry_set_safe);
  644. /**
  645. * efivar_entry_find - search for an entry
  646. * @name: the EFI variable name
  647. * @guid: the EFI variable vendor's guid
  648. * @head: head of the variable list
  649. * @remove: should we remove the entry from the list?
  650. *
  651. * Search for an entry on the variable list that has the EFI variable
  652. * name @name and vendor guid @guid. If an entry is found on the list
  653. * and @remove is true, the entry is removed from the list.
  654. *
  655. * The caller MUST call efivar_entry_iter_begin() and
  656. * efivar_entry_iter_end() before and after the invocation of this
  657. * function, respectively.
  658. *
  659. * Returns the entry if found on the list, %NULL otherwise.
  660. */
  661. struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
  662. struct list_head *head, bool remove)
  663. {
  664. struct efivar_entry *entry, *n;
  665. int strsize1, strsize2;
  666. bool found = false;
  667. lockdep_assert_held(&__efivars->lock);
  668. list_for_each_entry_safe(entry, n, head, list) {
  669. strsize1 = ucs2_strsize(name, 1024);
  670. strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
  671. if (strsize1 == strsize2 &&
  672. !memcmp(name, &(entry->var.VariableName), strsize1) &&
  673. !efi_guidcmp(guid, entry->var.VendorGuid)) {
  674. found = true;
  675. break;
  676. }
  677. }
  678. if (!found)
  679. return NULL;
  680. if (remove) {
  681. if (entry->scanning) {
  682. /*
  683. * The entry will be deleted
  684. * after scanning is completed.
  685. */
  686. entry->deleting = true;
  687. } else
  688. list_del(&entry->list);
  689. }
  690. return entry;
  691. }
  692. EXPORT_SYMBOL_GPL(efivar_entry_find);
  693. /**
  694. * efivar_entry_size - obtain the size of a variable
  695. * @entry: entry for this variable
  696. * @size: location to store the variable's size
  697. */
  698. int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
  699. {
  700. const struct efivar_operations *ops = __efivars->ops;
  701. efi_status_t status;
  702. *size = 0;
  703. spin_lock_irq(&__efivars->lock);
  704. status = ops->get_variable(entry->var.VariableName,
  705. &entry->var.VendorGuid, NULL, size, NULL);
  706. spin_unlock_irq(&__efivars->lock);
  707. if (status != EFI_BUFFER_TOO_SMALL)
  708. return efi_status_to_err(status);
  709. return 0;
  710. }
  711. EXPORT_SYMBOL_GPL(efivar_entry_size);
  712. /**
  713. * __efivar_entry_get - call get_variable()
  714. * @entry: read data for this variable
  715. * @attributes: variable attributes
  716. * @size: size of @data buffer
  717. * @data: buffer to store variable data
  718. *
  719. * The caller MUST call efivar_entry_iter_begin() and
  720. * efivar_entry_iter_end() before and after the invocation of this
  721. * function, respectively.
  722. */
  723. int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
  724. unsigned long *size, void *data)
  725. {
  726. const struct efivar_operations *ops = __efivars->ops;
  727. efi_status_t status;
  728. lockdep_assert_held(&__efivars->lock);
  729. status = ops->get_variable(entry->var.VariableName,
  730. &entry->var.VendorGuid,
  731. attributes, size, data);
  732. return efi_status_to_err(status);
  733. }
  734. EXPORT_SYMBOL_GPL(__efivar_entry_get);
  735. /**
  736. * efivar_entry_get - call get_variable()
  737. * @entry: read data for this variable
  738. * @attributes: variable attributes
  739. * @size: size of @data buffer
  740. * @data: buffer to store variable data
  741. */
  742. int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
  743. unsigned long *size, void *data)
  744. {
  745. const struct efivar_operations *ops = __efivars->ops;
  746. efi_status_t status;
  747. spin_lock_irq(&__efivars->lock);
  748. status = ops->get_variable(entry->var.VariableName,
  749. &entry->var.VendorGuid,
  750. attributes, size, data);
  751. spin_unlock_irq(&__efivars->lock);
  752. return efi_status_to_err(status);
  753. }
  754. EXPORT_SYMBOL_GPL(efivar_entry_get);
  755. /**
  756. * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
  757. * @entry: entry containing variable to set and get
  758. * @attributes: attributes of variable to be written
  759. * @size: size of data buffer
  760. * @data: buffer containing data to write
  761. * @set: did the set_variable() call succeed?
  762. *
  763. * This is a pretty special (complex) function. See efivarfs_file_write().
  764. *
  765. * Atomically call set_variable() for @entry and if the call is
  766. * successful, return the new size of the variable from get_variable()
  767. * in @size. The success of set_variable() is indicated by @set.
  768. *
  769. * Returns 0 on success, -EINVAL if the variable data is invalid,
  770. * -ENOSPC if the firmware does not have enough available space, or a
  771. * converted EFI status code if either of set_variable() or
  772. * get_variable() fail.
  773. *
  774. * If the EFI variable does not exist when calling set_variable()
  775. * (EFI_NOT_FOUND), @entry is removed from the variable list.
  776. */
  777. int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
  778. unsigned long *size, void *data, bool *set)
  779. {
  780. const struct efivar_operations *ops = __efivars->ops;
  781. efi_char16_t *name = entry->var.VariableName;
  782. efi_guid_t *vendor = &entry->var.VendorGuid;
  783. efi_status_t status;
  784. int err;
  785. *set = false;
  786. if (efivar_validate(*vendor, name, data, *size) == false)
  787. return -EINVAL;
  788. /*
  789. * The lock here protects the get_variable call, the conditional
  790. * set_variable call, and removal of the variable from the efivars
  791. * list (in the case of an authenticated delete).
  792. */
  793. spin_lock_irq(&__efivars->lock);
  794. /*
  795. * Ensure that the available space hasn't shrunk below the safe level
  796. */
  797. status = check_var_size(attributes, *size + ucs2_strsize(name, 1024));
  798. if (status != EFI_SUCCESS) {
  799. if (status != EFI_UNSUPPORTED) {
  800. err = efi_status_to_err(status);
  801. goto out;
  802. }
  803. if (*size > 65536) {
  804. err = -ENOSPC;
  805. goto out;
  806. }
  807. }
  808. status = ops->set_variable(name, vendor, attributes, *size, data);
  809. if (status != EFI_SUCCESS) {
  810. err = efi_status_to_err(status);
  811. goto out;
  812. }
  813. *set = true;
  814. /*
  815. * Writing to the variable may have caused a change in size (which
  816. * could either be an append or an overwrite), or the variable to be
  817. * deleted. Perform a GetVariable() so we can tell what actually
  818. * happened.
  819. */
  820. *size = 0;
  821. status = ops->get_variable(entry->var.VariableName,
  822. &entry->var.VendorGuid,
  823. NULL, size, NULL);
  824. if (status == EFI_NOT_FOUND)
  825. efivar_entry_list_del_unlock(entry);
  826. else
  827. spin_unlock_irq(&__efivars->lock);
  828. if (status && status != EFI_BUFFER_TOO_SMALL)
  829. return efi_status_to_err(status);
  830. return 0;
  831. out:
  832. spin_unlock_irq(&__efivars->lock);
  833. return err;
  834. }
  835. EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
  836. /**
  837. * efivar_entry_iter_begin - begin iterating the variable list
  838. *
  839. * Lock the variable list to prevent entry insertion and removal until
  840. * efivar_entry_iter_end() is called. This function is usually used in
  841. * conjunction with __efivar_entry_iter() or efivar_entry_iter().
  842. */
  843. void efivar_entry_iter_begin(void)
  844. {
  845. spin_lock_irq(&__efivars->lock);
  846. }
  847. EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
  848. /**
  849. * efivar_entry_iter_end - finish iterating the variable list
  850. *
  851. * Unlock the variable list and allow modifications to the list again.
  852. */
  853. void efivar_entry_iter_end(void)
  854. {
  855. spin_unlock_irq(&__efivars->lock);
  856. }
  857. EXPORT_SYMBOL_GPL(efivar_entry_iter_end);
  858. /**
  859. * __efivar_entry_iter - iterate over variable list
  860. * @func: callback function
  861. * @head: head of the variable list
  862. * @data: function-specific data to pass to callback
  863. * @prev: entry to begin iterating from
  864. *
  865. * Iterate over the list of EFI variables and call @func with every
  866. * entry on the list. It is safe for @func to remove entries in the
  867. * list via efivar_entry_delete().
  868. *
  869. * You MUST call efivar_enter_iter_begin() before this function, and
  870. * efivar_entry_iter_end() afterwards.
  871. *
  872. * It is possible to begin iteration from an arbitrary entry within
  873. * the list by passing @prev. @prev is updated on return to point to
  874. * the last entry passed to @func. To begin iterating from the
  875. * beginning of the list @prev must be %NULL.
  876. *
  877. * The restrictions for @func are the same as documented for
  878. * efivar_entry_iter().
  879. */
  880. int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
  881. struct list_head *head, void *data,
  882. struct efivar_entry **prev)
  883. {
  884. struct efivar_entry *entry, *n;
  885. int err = 0;
  886. if (!prev || !*prev) {
  887. list_for_each_entry_safe(entry, n, head, list) {
  888. err = func(entry, data);
  889. if (err)
  890. break;
  891. }
  892. if (prev)
  893. *prev = entry;
  894. return err;
  895. }
  896. list_for_each_entry_safe_continue((*prev), n, head, list) {
  897. err = func(*prev, data);
  898. if (err)
  899. break;
  900. }
  901. return err;
  902. }
  903. EXPORT_SYMBOL_GPL(__efivar_entry_iter);
  904. /**
  905. * efivar_entry_iter - iterate over variable list
  906. * @func: callback function
  907. * @head: head of variable list
  908. * @data: function-specific data to pass to callback
  909. *
  910. * Iterate over the list of EFI variables and call @func with every
  911. * entry on the list. It is safe for @func to remove entries in the
  912. * list via efivar_entry_delete() while iterating.
  913. *
  914. * Some notes for the callback function:
  915. * - a non-zero return value indicates an error and terminates the loop
  916. * - @func is called from atomic context
  917. */
  918. int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
  919. struct list_head *head, void *data)
  920. {
  921. int err = 0;
  922. efivar_entry_iter_begin();
  923. err = __efivar_entry_iter(func, head, data, NULL);
  924. efivar_entry_iter_end();
  925. return err;
  926. }
  927. EXPORT_SYMBOL_GPL(efivar_entry_iter);
  928. /**
  929. * efivars_kobject - get the kobject for the registered efivars
  930. *
  931. * If efivars_register() has not been called we return NULL,
  932. * otherwise return the kobject used at registration time.
  933. */
  934. struct kobject *efivars_kobject(void)
  935. {
  936. if (!__efivars)
  937. return NULL;
  938. return __efivars->kobject;
  939. }
  940. EXPORT_SYMBOL_GPL(efivars_kobject);
  941. /**
  942. * efivar_run_worker - schedule the efivar worker thread
  943. */
  944. void efivar_run_worker(void)
  945. {
  946. if (efivar_wq_enabled)
  947. schedule_work(&efivar_work);
  948. }
  949. EXPORT_SYMBOL_GPL(efivar_run_worker);
  950. /**
  951. * efivars_register - register an efivars
  952. * @efivars: efivars to register
  953. * @ops: efivars operations
  954. * @kobject: @efivars-specific kobject
  955. *
  956. * Only a single efivars can be registered at any time.
  957. */
  958. int efivars_register(struct efivars *efivars,
  959. const struct efivar_operations *ops,
  960. struct kobject *kobject)
  961. {
  962. spin_lock_init(&efivars->lock);
  963. efivars->ops = ops;
  964. efivars->kobject = kobject;
  965. __efivars = efivars;
  966. return 0;
  967. }
  968. EXPORT_SYMBOL_GPL(efivars_register);
  969. /**
  970. * efivars_unregister - unregister an efivars
  971. * @efivars: efivars to unregister
  972. *
  973. * The caller must have already removed every entry from the list,
  974. * failure to do so is an error.
  975. */
  976. int efivars_unregister(struct efivars *efivars)
  977. {
  978. int rv;
  979. if (!__efivars) {
  980. printk(KERN_ERR "efivars not registered\n");
  981. rv = -EINVAL;
  982. goto out;
  983. }
  984. if (__efivars != efivars) {
  985. rv = -EINVAL;
  986. goto out;
  987. }
  988. __efivars = NULL;
  989. rv = 0;
  990. out:
  991. return rv;
  992. }
  993. EXPORT_SYMBOL_GPL(efivars_unregister);