vars.c 31 KB

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