vars.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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 && spin_trylock_irqsave(&__efivars->lock, flags))
  535. return -EBUSY;
  536. else
  537. spin_lock_irqsave(&__efivars->lock, flags);
  538. status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
  539. if (status != EFI_SUCCESS) {
  540. spin_unlock_irqrestore(&__efivars->lock, flags);
  541. return -ENOSPC;
  542. }
  543. status = ops->set_variable(name, &vendor, attributes, size, data);
  544. spin_unlock_irqrestore(&__efivars->lock, flags);
  545. return efi_status_to_err(status);
  546. }
  547. EXPORT_SYMBOL_GPL(efivar_entry_set_safe);
  548. /**
  549. * efivar_entry_find - search for an entry
  550. * @name: the EFI variable name
  551. * @guid: the EFI variable vendor's guid
  552. * @head: head of the variable list
  553. * @remove: should we remove the entry from the list?
  554. *
  555. * Search for an entry on the variable list that has the EFI variable
  556. * name @name and vendor guid @guid. If an entry is found on the list
  557. * and @remove is true, the entry is removed from the list.
  558. *
  559. * The caller MUST call efivar_entry_iter_begin() and
  560. * efivar_entry_iter_end() before and after the invocation of this
  561. * function, respectively.
  562. *
  563. * Returns the entry if found on the list, %NULL otherwise.
  564. */
  565. struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
  566. struct list_head *head, bool remove)
  567. {
  568. struct efivar_entry *entry, *n;
  569. int strsize1, strsize2;
  570. bool found = false;
  571. WARN_ON(!spin_is_locked(&__efivars->lock));
  572. list_for_each_entry_safe(entry, n, head, list) {
  573. strsize1 = ucs2_strsize(name, 1024);
  574. strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
  575. if (strsize1 == strsize2 &&
  576. !memcmp(name, &(entry->var.VariableName), strsize1) &&
  577. !efi_guidcmp(guid, entry->var.VendorGuid)) {
  578. found = true;
  579. break;
  580. }
  581. }
  582. if (!found)
  583. return NULL;
  584. if (remove)
  585. list_del(&entry->list);
  586. return entry;
  587. }
  588. EXPORT_SYMBOL_GPL(efivar_entry_find);
  589. /**
  590. * efivar_entry_size - obtain the size of a variable
  591. * @entry: entry for this variable
  592. * @size: location to store the variable's size
  593. */
  594. int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
  595. {
  596. const struct efivar_operations *ops = __efivars->ops;
  597. efi_status_t status;
  598. *size = 0;
  599. spin_lock_irq(&__efivars->lock);
  600. status = ops->get_variable(entry->var.VariableName,
  601. &entry->var.VendorGuid, NULL, size, NULL);
  602. spin_unlock_irq(&__efivars->lock);
  603. if (status != EFI_BUFFER_TOO_SMALL)
  604. return efi_status_to_err(status);
  605. return 0;
  606. }
  607. EXPORT_SYMBOL_GPL(efivar_entry_size);
  608. /**
  609. * __efivar_entry_get - call get_variable()
  610. * @entry: read data for this variable
  611. * @attributes: variable attributes
  612. * @size: size of @data buffer
  613. * @data: buffer to store variable data
  614. *
  615. * The caller MUST call efivar_entry_iter_begin() and
  616. * efivar_entry_iter_end() before and after the invocation of this
  617. * function, respectively.
  618. */
  619. int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
  620. unsigned long *size, void *data)
  621. {
  622. const struct efivar_operations *ops = __efivars->ops;
  623. efi_status_t status;
  624. WARN_ON(!spin_is_locked(&__efivars->lock));
  625. status = ops->get_variable(entry->var.VariableName,
  626. &entry->var.VendorGuid,
  627. attributes, size, data);
  628. return efi_status_to_err(status);
  629. }
  630. EXPORT_SYMBOL_GPL(__efivar_entry_get);
  631. /**
  632. * efivar_entry_get - call get_variable()
  633. * @entry: read data for this variable
  634. * @attributes: variable attributes
  635. * @size: size of @data buffer
  636. * @data: buffer to store variable data
  637. */
  638. int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
  639. unsigned long *size, void *data)
  640. {
  641. const struct efivar_operations *ops = __efivars->ops;
  642. efi_status_t status;
  643. spin_lock_irq(&__efivars->lock);
  644. status = ops->get_variable(entry->var.VariableName,
  645. &entry->var.VendorGuid,
  646. attributes, size, data);
  647. spin_unlock_irq(&__efivars->lock);
  648. return efi_status_to_err(status);
  649. }
  650. EXPORT_SYMBOL_GPL(efivar_entry_get);
  651. /**
  652. * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
  653. * @entry: entry containing variable to set and get
  654. * @attributes: attributes of variable to be written
  655. * @size: size of data buffer
  656. * @data: buffer containing data to write
  657. * @set: did the set_variable() call succeed?
  658. *
  659. * This is a pretty special (complex) function. See efivarfs_file_write().
  660. *
  661. * Atomically call set_variable() for @entry and if the call is
  662. * successful, return the new size of the variable from get_variable()
  663. * in @size. The success of set_variable() is indicated by @set.
  664. *
  665. * Returns 0 on success, -EINVAL if the variable data is invalid,
  666. * -ENOSPC if the firmware does not have enough available space, or a
  667. * converted EFI status code if either of set_variable() or
  668. * get_variable() fail.
  669. *
  670. * If the EFI variable does not exist when calling set_variable()
  671. * (EFI_NOT_FOUND), @entry is removed from the variable list.
  672. */
  673. int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
  674. unsigned long *size, void *data, bool *set)
  675. {
  676. const struct efivar_operations *ops = __efivars->ops;
  677. efi_char16_t *name = entry->var.VariableName;
  678. efi_guid_t *vendor = &entry->var.VendorGuid;
  679. efi_status_t status;
  680. int err;
  681. *set = false;
  682. if (efivar_validate(&entry->var, data, *size) == false)
  683. return -EINVAL;
  684. /*
  685. * The lock here protects the get_variable call, the conditional
  686. * set_variable call, and removal of the variable from the efivars
  687. * list (in the case of an authenticated delete).
  688. */
  689. spin_lock_irq(&__efivars->lock);
  690. /*
  691. * Ensure that the available space hasn't shrunk below the safe level
  692. */
  693. status = check_var_size(attributes, *size + ucs2_strsize(name, 1024));
  694. if (status != EFI_SUCCESS) {
  695. if (status != EFI_UNSUPPORTED) {
  696. err = efi_status_to_err(status);
  697. goto out;
  698. }
  699. if (*size > 65536) {
  700. err = -ENOSPC;
  701. goto out;
  702. }
  703. }
  704. status = ops->set_variable(name, vendor, attributes, *size, data);
  705. if (status != EFI_SUCCESS) {
  706. err = efi_status_to_err(status);
  707. goto out;
  708. }
  709. *set = true;
  710. /*
  711. * Writing to the variable may have caused a change in size (which
  712. * could either be an append or an overwrite), or the variable to be
  713. * deleted. Perform a GetVariable() so we can tell what actually
  714. * happened.
  715. */
  716. *size = 0;
  717. status = ops->get_variable(entry->var.VariableName,
  718. &entry->var.VendorGuid,
  719. NULL, size, NULL);
  720. if (status == EFI_NOT_FOUND)
  721. efivar_entry_list_del_unlock(entry);
  722. else
  723. spin_unlock_irq(&__efivars->lock);
  724. if (status && status != EFI_BUFFER_TOO_SMALL)
  725. return efi_status_to_err(status);
  726. return 0;
  727. out:
  728. spin_unlock_irq(&__efivars->lock);
  729. return err;
  730. }
  731. EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
  732. /**
  733. * efivar_entry_iter_begin - begin iterating the variable list
  734. *
  735. * Lock the variable list to prevent entry insertion and removal until
  736. * efivar_entry_iter_end() is called. This function is usually used in
  737. * conjunction with __efivar_entry_iter() or efivar_entry_iter().
  738. */
  739. void efivar_entry_iter_begin(void)
  740. {
  741. spin_lock_irq(&__efivars->lock);
  742. }
  743. EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
  744. /**
  745. * efivar_entry_iter_end - finish iterating the variable list
  746. *
  747. * Unlock the variable list and allow modifications to the list again.
  748. */
  749. void efivar_entry_iter_end(void)
  750. {
  751. spin_unlock_irq(&__efivars->lock);
  752. }
  753. EXPORT_SYMBOL_GPL(efivar_entry_iter_end);
  754. /**
  755. * __efivar_entry_iter - iterate over variable list
  756. * @func: callback function
  757. * @head: head of the variable list
  758. * @data: function-specific data to pass to callback
  759. * @prev: entry to begin iterating from
  760. *
  761. * Iterate over the list of EFI variables and call @func with every
  762. * entry on the list. It is safe for @func to remove entries in the
  763. * list via efivar_entry_delete().
  764. *
  765. * You MUST call efivar_enter_iter_begin() before this function, and
  766. * efivar_entry_iter_end() afterwards.
  767. *
  768. * It is possible to begin iteration from an arbitrary entry within
  769. * the list by passing @prev. @prev is updated on return to point to
  770. * the last entry passed to @func. To begin iterating from the
  771. * beginning of the list @prev must be %NULL.
  772. *
  773. * The restrictions for @func are the same as documented for
  774. * efivar_entry_iter().
  775. */
  776. int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
  777. struct list_head *head, void *data,
  778. struct efivar_entry **prev)
  779. {
  780. struct efivar_entry *entry, *n;
  781. int err = 0;
  782. if (!prev || !*prev) {
  783. list_for_each_entry_safe(entry, n, head, list) {
  784. err = func(entry, data);
  785. if (err)
  786. break;
  787. }
  788. if (prev)
  789. *prev = entry;
  790. return err;
  791. }
  792. list_for_each_entry_safe_continue((*prev), n, head, list) {
  793. err = func(*prev, data);
  794. if (err)
  795. break;
  796. }
  797. return err;
  798. }
  799. EXPORT_SYMBOL_GPL(__efivar_entry_iter);
  800. /**
  801. * efivar_entry_iter - iterate over variable list
  802. * @func: callback function
  803. * @head: head of variable list
  804. * @data: function-specific data to pass to callback
  805. *
  806. * Iterate over the list of EFI variables and call @func with every
  807. * entry on the list. It is safe for @func to remove entries in the
  808. * list via efivar_entry_delete() while iterating.
  809. *
  810. * Some notes for the callback function:
  811. * - a non-zero return value indicates an error and terminates the loop
  812. * - @func is called from atomic context
  813. */
  814. int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
  815. struct list_head *head, void *data)
  816. {
  817. int err = 0;
  818. efivar_entry_iter_begin();
  819. err = __efivar_entry_iter(func, head, data, NULL);
  820. efivar_entry_iter_end();
  821. return err;
  822. }
  823. EXPORT_SYMBOL_GPL(efivar_entry_iter);
  824. /**
  825. * efivars_kobject - get the kobject for the registered efivars
  826. *
  827. * If efivars_register() has not been called we return NULL,
  828. * otherwise return the kobject used at registration time.
  829. */
  830. struct kobject *efivars_kobject(void)
  831. {
  832. if (!__efivars)
  833. return NULL;
  834. return __efivars->kobject;
  835. }
  836. EXPORT_SYMBOL_GPL(efivars_kobject);
  837. /**
  838. * efivar_run_worker - schedule the efivar worker thread
  839. */
  840. void efivar_run_worker(void)
  841. {
  842. if (efivar_wq_enabled)
  843. schedule_work(&efivar_work);
  844. }
  845. EXPORT_SYMBOL_GPL(efivar_run_worker);
  846. /**
  847. * efivars_register - register an efivars
  848. * @efivars: efivars to register
  849. * @ops: efivars operations
  850. * @kobject: @efivars-specific kobject
  851. *
  852. * Only a single efivars can be registered at any time.
  853. */
  854. int efivars_register(struct efivars *efivars,
  855. const struct efivar_operations *ops,
  856. struct kobject *kobject)
  857. {
  858. spin_lock_init(&efivars->lock);
  859. efivars->ops = ops;
  860. efivars->kobject = kobject;
  861. __efivars = efivars;
  862. return 0;
  863. }
  864. EXPORT_SYMBOL_GPL(efivars_register);
  865. /**
  866. * efivars_unregister - unregister an efivars
  867. * @efivars: efivars to unregister
  868. *
  869. * The caller must have already removed every entry from the list,
  870. * failure to do so is an error.
  871. */
  872. int efivars_unregister(struct efivars *efivars)
  873. {
  874. int rv;
  875. if (!__efivars) {
  876. printk(KERN_ERR "efivars not registered\n");
  877. rv = -EINVAL;
  878. goto out;
  879. }
  880. if (__efivars != efivars) {
  881. rv = -EINVAL;
  882. goto out;
  883. }
  884. __efivars = NULL;
  885. rv = 0;
  886. out:
  887. return rv;
  888. }
  889. EXPORT_SYMBOL_GPL(efivars_unregister);