vars.c 32 KB

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