vars.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Originally from efivars.c
  4. *
  5. * Copyright (C) 2001,2003,2004 Dell <[email protected]>
  6. * Copyright (C) 2004 Intel Corporation <[email protected]>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/sizes.h>
  10. #include <linux/errno.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/string.h>
  14. #include <linux/smp.h>
  15. #include <linux/efi.h>
  16. #include <linux/ucs2_string.h>
  17. /* Private pointer to registered efivars */
  18. static struct efivars *__efivars;
  19. static DEFINE_SEMAPHORE(efivars_lock);
  20. static efi_status_t check_var_size(bool nonblocking, u32 attributes,
  21. unsigned long size)
  22. {
  23. const struct efivar_operations *fops;
  24. efi_status_t status;
  25. fops = __efivars->ops;
  26. if (!fops->query_variable_store)
  27. status = EFI_UNSUPPORTED;
  28. else
  29. status = fops->query_variable_store(attributes, size,
  30. nonblocking);
  31. if (status == EFI_UNSUPPORTED)
  32. return (size <= SZ_64K) ? EFI_SUCCESS : EFI_OUT_OF_RESOURCES;
  33. return status;
  34. }
  35. /**
  36. * efivars_kobject - get the kobject for the registered efivars
  37. *
  38. * If efivars_register() has not been called we return NULL,
  39. * otherwise return the kobject used at registration time.
  40. */
  41. struct kobject *efivars_kobject(void)
  42. {
  43. if (!__efivars)
  44. return NULL;
  45. return __efivars->kobject;
  46. }
  47. EXPORT_SYMBOL_GPL(efivars_kobject);
  48. /**
  49. * efivars_register - register an efivars
  50. * @efivars: efivars to register
  51. * @ops: efivars operations
  52. * @kobject: @efivars-specific kobject
  53. *
  54. * Only a single efivars can be registered at any time.
  55. */
  56. int efivars_register(struct efivars *efivars,
  57. const struct efivar_operations *ops,
  58. struct kobject *kobject)
  59. {
  60. if (down_interruptible(&efivars_lock))
  61. return -EINTR;
  62. efivars->ops = ops;
  63. efivars->kobject = kobject;
  64. __efivars = efivars;
  65. pr_info("Registered efivars operations\n");
  66. up(&efivars_lock);
  67. return 0;
  68. }
  69. EXPORT_SYMBOL_GPL(efivars_register);
  70. /**
  71. * efivars_unregister - unregister an efivars
  72. * @efivars: efivars to unregister
  73. *
  74. * The caller must have already removed every entry from the list,
  75. * failure to do so is an error.
  76. */
  77. int efivars_unregister(struct efivars *efivars)
  78. {
  79. int rv;
  80. if (down_interruptible(&efivars_lock))
  81. return -EINTR;
  82. if (!__efivars) {
  83. printk(KERN_ERR "efivars not registered\n");
  84. rv = -EINVAL;
  85. goto out;
  86. }
  87. if (__efivars != efivars) {
  88. rv = -EINVAL;
  89. goto out;
  90. }
  91. pr_info("Unregistered efivars operations\n");
  92. __efivars = NULL;
  93. rv = 0;
  94. out:
  95. up(&efivars_lock);
  96. return rv;
  97. }
  98. EXPORT_SYMBOL_GPL(efivars_unregister);
  99. int efivar_supports_writes(void)
  100. {
  101. return __efivars && __efivars->ops->set_variable;
  102. }
  103. EXPORT_SYMBOL_GPL(efivar_supports_writes);
  104. /*
  105. * efivar_lock() - obtain the efivar lock, wait for it if needed
  106. * @return 0 on success, error code on failure
  107. */
  108. int efivar_lock(void)
  109. {
  110. if (down_interruptible(&efivars_lock))
  111. return -EINTR;
  112. if (!__efivars->ops) {
  113. up(&efivars_lock);
  114. return -ENODEV;
  115. }
  116. return 0;
  117. }
  118. EXPORT_SYMBOL_NS_GPL(efivar_lock, EFIVAR);
  119. /*
  120. * efivar_lock() - obtain the efivar lock if it is free
  121. * @return 0 on success, error code on failure
  122. */
  123. int efivar_trylock(void)
  124. {
  125. if (down_trylock(&efivars_lock))
  126. return -EBUSY;
  127. if (!__efivars->ops) {
  128. up(&efivars_lock);
  129. return -ENODEV;
  130. }
  131. return 0;
  132. }
  133. EXPORT_SYMBOL_NS_GPL(efivar_trylock, EFIVAR);
  134. /*
  135. * efivar_unlock() - release the efivar lock
  136. */
  137. void efivar_unlock(void)
  138. {
  139. up(&efivars_lock);
  140. }
  141. EXPORT_SYMBOL_NS_GPL(efivar_unlock, EFIVAR);
  142. /*
  143. * efivar_get_variable() - retrieve a variable identified by name/vendor
  144. *
  145. * Must be called with efivars_lock held.
  146. */
  147. efi_status_t efivar_get_variable(efi_char16_t *name, efi_guid_t *vendor,
  148. u32 *attr, unsigned long *size, void *data)
  149. {
  150. return __efivars->ops->get_variable(name, vendor, attr, size, data);
  151. }
  152. EXPORT_SYMBOL_NS_GPL(efivar_get_variable, EFIVAR);
  153. /*
  154. * efivar_get_next_variable() - enumerate the next name/vendor pair
  155. *
  156. * Must be called with efivars_lock held.
  157. */
  158. efi_status_t efivar_get_next_variable(unsigned long *name_size,
  159. efi_char16_t *name, efi_guid_t *vendor)
  160. {
  161. return __efivars->ops->get_next_variable(name_size, name, vendor);
  162. }
  163. EXPORT_SYMBOL_NS_GPL(efivar_get_next_variable, EFIVAR);
  164. /*
  165. * efivar_set_variable_locked() - set a variable identified by name/vendor
  166. *
  167. * Must be called with efivars_lock held. If @nonblocking is set, it will use
  168. * non-blocking primitives so it is guaranteed not to sleep.
  169. */
  170. efi_status_t efivar_set_variable_locked(efi_char16_t *name, efi_guid_t *vendor,
  171. u32 attr, unsigned long data_size,
  172. void *data, bool nonblocking)
  173. {
  174. efi_set_variable_t *setvar;
  175. efi_status_t status;
  176. if (data_size > 0) {
  177. status = check_var_size(nonblocking, attr,
  178. data_size + ucs2_strsize(name, 1024));
  179. if (status != EFI_SUCCESS)
  180. return status;
  181. }
  182. /*
  183. * If no _nonblocking variant exists, the ordinary one
  184. * is assumed to be non-blocking.
  185. */
  186. setvar = __efivars->ops->set_variable_nonblocking;
  187. if (!setvar || !nonblocking)
  188. setvar = __efivars->ops->set_variable;
  189. return setvar(name, vendor, attr, data_size, data);
  190. }
  191. EXPORT_SYMBOL_NS_GPL(efivar_set_variable_locked, EFIVAR);
  192. /*
  193. * efivar_set_variable() - set a variable identified by name/vendor
  194. *
  195. * Can be called without holding the efivars_lock. Will sleep on obtaining the
  196. * lock, or on obtaining other locks that are needed in order to complete the
  197. * call.
  198. */
  199. efi_status_t efivar_set_variable(efi_char16_t *name, efi_guid_t *vendor,
  200. u32 attr, unsigned long data_size, void *data)
  201. {
  202. efi_status_t status;
  203. if (efivar_lock())
  204. return EFI_ABORTED;
  205. status = efivar_set_variable_locked(name, vendor, attr, data_size,
  206. data, false);
  207. efivar_unlock();
  208. return status;
  209. }
  210. EXPORT_SYMBOL_NS_GPL(efivar_set_variable, EFIVAR);