efi_var_common.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * UEFI runtime variable services
  4. *
  5. * Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. * Copyright (c) 2020 Linaro Limited, Author: AKASHI Takahiro
  7. */
  8. #include <common.h>
  9. #include <efi_loader.h>
  10. #include <efi_variable.h>
  11. enum efi_secure_mode {
  12. EFI_MODE_SETUP,
  13. EFI_MODE_USER,
  14. EFI_MODE_AUDIT,
  15. EFI_MODE_DEPLOYED,
  16. };
  17. struct efi_auth_var_name_type {
  18. const u16 *name;
  19. const efi_guid_t *guid;
  20. const enum efi_auth_var_type type;
  21. };
  22. static const struct efi_auth_var_name_type name_type[] = {
  23. {u"PK", &efi_global_variable_guid, EFI_AUTH_VAR_PK},
  24. {u"KEK", &efi_global_variable_guid, EFI_AUTH_VAR_KEK},
  25. {u"db", &efi_guid_image_security_database, EFI_AUTH_VAR_DB},
  26. {u"dbx", &efi_guid_image_security_database, EFI_AUTH_VAR_DBX},
  27. /* not used yet
  28. {u"dbt", &efi_guid_image_security_database, EFI_AUTH_VAR_DBT},
  29. {u"dbr", &efi_guid_image_security_database, EFI_AUTH_VAR_DBR},
  30. */
  31. };
  32. static bool efi_secure_boot;
  33. static enum efi_secure_mode efi_secure_mode;
  34. /**
  35. * efi_efi_get_variable() - retrieve value of a UEFI variable
  36. *
  37. * This function implements the GetVariable runtime service.
  38. *
  39. * See the Unified Extensible Firmware Interface (UEFI) specification for
  40. * details.
  41. *
  42. * @variable_name: name of the variable
  43. * @vendor: vendor GUID
  44. * @attributes: attributes of the variable
  45. * @data_size: size of the buffer to which the variable value is copied
  46. * @data: buffer to which the variable value is copied
  47. * Return: status code
  48. */
  49. efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
  50. const efi_guid_t *vendor, u32 *attributes,
  51. efi_uintn_t *data_size, void *data)
  52. {
  53. efi_status_t ret;
  54. EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
  55. data_size, data);
  56. ret = efi_get_variable_int(variable_name, vendor, attributes,
  57. data_size, data, NULL);
  58. /* Remove EFI_VARIABLE_READ_ONLY flag */
  59. if (attributes)
  60. *attributes &= EFI_VARIABLE_MASK;
  61. return EFI_EXIT(ret);
  62. }
  63. /**
  64. * efi_set_variable() - set value of a UEFI variable
  65. *
  66. * This function implements the SetVariable runtime service.
  67. *
  68. * See the Unified Extensible Firmware Interface (UEFI) specification for
  69. * details.
  70. *
  71. * @variable_name: name of the variable
  72. * @vendor: vendor GUID
  73. * @attributes: attributes of the variable
  74. * @data_size: size of the buffer with the variable value
  75. * @data: buffer with the variable value
  76. * Return: status code
  77. */
  78. efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
  79. const efi_guid_t *vendor, u32 attributes,
  80. efi_uintn_t data_size, const void *data)
  81. {
  82. efi_status_t ret;
  83. EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
  84. data_size, data);
  85. /* Make sure that the EFI_VARIABLE_READ_ONLY flag is not set */
  86. if (attributes & ~(u32)EFI_VARIABLE_MASK)
  87. ret = EFI_INVALID_PARAMETER;
  88. else
  89. ret = efi_set_variable_int(variable_name, vendor, attributes,
  90. data_size, data, true);
  91. return EFI_EXIT(ret);
  92. }
  93. /**
  94. * efi_get_next_variable_name() - enumerate the current variable names
  95. *
  96. * @variable_name_size: size of variable_name buffer in byte
  97. * @variable_name: name of uefi variable's name in u16
  98. * @vendor: vendor's guid
  99. *
  100. * See the Unified Extensible Firmware Interface (UEFI) specification for
  101. * details.
  102. *
  103. * Return: status code
  104. */
  105. efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
  106. u16 *variable_name,
  107. efi_guid_t *vendor)
  108. {
  109. efi_status_t ret;
  110. EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
  111. ret = efi_get_next_variable_name_int(variable_name_size, variable_name,
  112. vendor);
  113. return EFI_EXIT(ret);
  114. }
  115. /**
  116. * efi_query_variable_info() - get information about EFI variables
  117. *
  118. * This function implements the QueryVariableInfo() runtime service.
  119. *
  120. * See the Unified Extensible Firmware Interface (UEFI) specification for
  121. * details.
  122. *
  123. * @attributes: bitmask to select variables to be
  124. * queried
  125. * @maximum_variable_storage_size: maximum size of storage area for the
  126. * selected variable types
  127. * @remaining_variable_storage_size: remaining size of storage are for the
  128. * selected variable types
  129. * @maximum_variable_size: maximum size of a variable of the
  130. * selected type
  131. * Returns: status code
  132. */
  133. efi_status_t EFIAPI efi_query_variable_info(
  134. u32 attributes, u64 *maximum_variable_storage_size,
  135. u64 *remaining_variable_storage_size,
  136. u64 *maximum_variable_size)
  137. {
  138. efi_status_t ret;
  139. EFI_ENTRY("%x %p %p %p", attributes, maximum_variable_storage_size,
  140. remaining_variable_storage_size, maximum_variable_size);
  141. ret = efi_query_variable_info_int(attributes,
  142. maximum_variable_storage_size,
  143. remaining_variable_storage_size,
  144. maximum_variable_size);
  145. return EFI_EXIT(ret);
  146. }
  147. efi_status_t __efi_runtime EFIAPI
  148. efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *guid,
  149. u32 *attributes, efi_uintn_t *data_size, void *data)
  150. {
  151. efi_status_t ret;
  152. ret = efi_get_variable_mem(variable_name, guid, attributes, data_size, data, NULL);
  153. /* Remove EFI_VARIABLE_READ_ONLY flag */
  154. if (attributes)
  155. *attributes &= EFI_VARIABLE_MASK;
  156. return ret;
  157. }
  158. efi_status_t __efi_runtime EFIAPI
  159. efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
  160. u16 *variable_name, efi_guid_t *guid)
  161. {
  162. return efi_get_next_variable_name_mem(variable_name_size, variable_name, guid);
  163. }
  164. /**
  165. * efi_set_secure_state - modify secure boot state variables
  166. * @secure_boot: value of SecureBoot
  167. * @setup_mode: value of SetupMode
  168. * @audit_mode: value of AuditMode
  169. * @deployed_mode: value of DeployedMode
  170. *
  171. * Modify secure boot status related variables as indicated.
  172. *
  173. * Return: status code
  174. */
  175. static efi_status_t efi_set_secure_state(u8 secure_boot, u8 setup_mode,
  176. u8 audit_mode, u8 deployed_mode)
  177. {
  178. efi_status_t ret;
  179. const u32 attributes_ro = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  180. EFI_VARIABLE_RUNTIME_ACCESS |
  181. EFI_VARIABLE_READ_ONLY;
  182. const u32 attributes_rw = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  183. EFI_VARIABLE_RUNTIME_ACCESS;
  184. efi_secure_boot = secure_boot;
  185. ret = efi_set_variable_int(L"SecureBoot", &efi_global_variable_guid,
  186. attributes_ro, sizeof(secure_boot),
  187. &secure_boot, false);
  188. if (ret != EFI_SUCCESS)
  189. goto err;
  190. ret = efi_set_variable_int(L"SetupMode", &efi_global_variable_guid,
  191. attributes_ro, sizeof(setup_mode),
  192. &setup_mode, false);
  193. if (ret != EFI_SUCCESS)
  194. goto err;
  195. ret = efi_set_variable_int(L"AuditMode", &efi_global_variable_guid,
  196. audit_mode || setup_mode ?
  197. attributes_ro : attributes_rw,
  198. sizeof(audit_mode), &audit_mode, false);
  199. if (ret != EFI_SUCCESS)
  200. goto err;
  201. ret = efi_set_variable_int(L"DeployedMode",
  202. &efi_global_variable_guid,
  203. audit_mode || deployed_mode || setup_mode ?
  204. attributes_ro : attributes_rw,
  205. sizeof(deployed_mode), &deployed_mode,
  206. false);
  207. err:
  208. return ret;
  209. }
  210. /**
  211. * efi_transfer_secure_state - handle a secure boot state transition
  212. * @mode: new state
  213. *
  214. * Depending on @mode, secure boot related variables are updated.
  215. * Those variables are *read-only* for users, efi_set_variable_int()
  216. * is called here.
  217. *
  218. * Return: status code
  219. */
  220. static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
  221. {
  222. efi_status_t ret;
  223. EFI_PRINT("Switching secure state from %d to %d\n", efi_secure_mode,
  224. mode);
  225. if (mode == EFI_MODE_DEPLOYED) {
  226. ret = efi_set_secure_state(1, 0, 0, 1);
  227. if (ret != EFI_SUCCESS)
  228. goto err;
  229. } else if (mode == EFI_MODE_AUDIT) {
  230. ret = efi_set_variable_int(L"PK", &efi_global_variable_guid,
  231. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  232. EFI_VARIABLE_RUNTIME_ACCESS,
  233. 0, NULL, false);
  234. if (ret != EFI_SUCCESS)
  235. goto err;
  236. ret = efi_set_secure_state(0, 1, 1, 0);
  237. if (ret != EFI_SUCCESS)
  238. goto err;
  239. } else if (mode == EFI_MODE_USER) {
  240. ret = efi_set_secure_state(1, 0, 0, 0);
  241. if (ret != EFI_SUCCESS)
  242. goto err;
  243. } else if (mode == EFI_MODE_SETUP) {
  244. ret = efi_set_secure_state(0, 1, 0, 0);
  245. if (ret != EFI_SUCCESS)
  246. goto err;
  247. } else {
  248. return EFI_INVALID_PARAMETER;
  249. }
  250. efi_secure_mode = mode;
  251. return EFI_SUCCESS;
  252. err:
  253. /* TODO: What action should be taken here? */
  254. printf("ERROR: Secure state transition failed\n");
  255. return ret;
  256. }
  257. efi_status_t efi_init_secure_state(void)
  258. {
  259. enum efi_secure_mode mode = EFI_MODE_SETUP;
  260. u8 efi_vendor_keys = 0;
  261. efi_uintn_t size = 0;
  262. efi_status_t ret;
  263. ret = efi_get_variable_int(L"PK", &efi_global_variable_guid,
  264. NULL, &size, NULL, NULL);
  265. if (ret == EFI_BUFFER_TOO_SMALL) {
  266. if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT))
  267. mode = EFI_MODE_USER;
  268. }
  269. ret = efi_transfer_secure_state(mode);
  270. if (ret != EFI_SUCCESS)
  271. return ret;
  272. /* As we do not provide vendor keys this variable is always 0. */
  273. ret = efi_set_variable_int(L"VendorKeys",
  274. &efi_global_variable_guid,
  275. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  276. EFI_VARIABLE_RUNTIME_ACCESS |
  277. EFI_VARIABLE_READ_ONLY,
  278. sizeof(efi_vendor_keys),
  279. &efi_vendor_keys, false);
  280. return ret;
  281. }
  282. /**
  283. * efi_secure_boot_enabled - return if secure boot is enabled or not
  284. *
  285. * Return: true if enabled, false if disabled
  286. */
  287. bool efi_secure_boot_enabled(void)
  288. {
  289. return efi_secure_boot;
  290. }
  291. enum efi_auth_var_type efi_auth_var_get_type(u16 *name, const efi_guid_t *guid)
  292. {
  293. for (size_t i = 0; i < ARRAY_SIZE(name_type); ++i) {
  294. if (!u16_strcmp(name, name_type[i].name) &&
  295. !guidcmp(guid, name_type[i].guid))
  296. return name_type[i].type;
  297. }
  298. return EFI_AUTH_VAR_NONE;
  299. }