vars.c 31 KB

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