vars.c 27 KB

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