efi_setup.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI setup code
  4. *
  5. * Copyright (c) 2016-2018 Alexander Graf et al.
  6. */
  7. #include <common.h>
  8. #include <bootm.h>
  9. #include <efi_loader.h>
  10. #include <efi_variable.h>
  11. #define OBJ_LIST_NOT_INITIALIZED 1
  12. efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
  13. /*
  14. * Allow unaligned memory access.
  15. *
  16. * This routine is overridden by architectures providing this feature.
  17. */
  18. void __weak allow_unaligned(void)
  19. {
  20. }
  21. /**
  22. * efi_init_platform_lang() - define supported languages
  23. *
  24. * Set the PlatformLangCodes and PlatformLang variables.
  25. *
  26. * Return: status code
  27. */
  28. static efi_status_t efi_init_platform_lang(void)
  29. {
  30. efi_status_t ret;
  31. efi_uintn_t data_size = 0;
  32. char *lang = CONFIG_EFI_PLATFORM_LANG_CODES;
  33. char *pos;
  34. /*
  35. * Variable PlatformLangCodes defines the language codes that the
  36. * machine can support.
  37. */
  38. ret = efi_set_variable_int(L"PlatformLangCodes",
  39. &efi_global_variable_guid,
  40. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  41. EFI_VARIABLE_RUNTIME_ACCESS |
  42. EFI_VARIABLE_READ_ONLY,
  43. sizeof(CONFIG_EFI_PLATFORM_LANG_CODES),
  44. CONFIG_EFI_PLATFORM_LANG_CODES, false);
  45. if (ret != EFI_SUCCESS)
  46. goto out;
  47. /*
  48. * Variable PlatformLang defines the language that the machine has been
  49. * configured for.
  50. */
  51. ret = efi_get_variable_int(L"PlatformLang",
  52. &efi_global_variable_guid,
  53. NULL, &data_size, &pos, NULL);
  54. if (ret == EFI_BUFFER_TOO_SMALL) {
  55. /* The variable is already set. Do not change it. */
  56. ret = EFI_SUCCESS;
  57. goto out;
  58. }
  59. /*
  60. * The list of supported languages is semicolon separated. Use the first
  61. * language to initialize PlatformLang.
  62. */
  63. pos = strchr(lang, ';');
  64. if (pos)
  65. *pos = 0;
  66. ret = efi_set_variable_int(L"PlatformLang",
  67. &efi_global_variable_guid,
  68. EFI_VARIABLE_NON_VOLATILE |
  69. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  70. EFI_VARIABLE_RUNTIME_ACCESS,
  71. 1 + strlen(lang), lang, false);
  72. out:
  73. if (ret != EFI_SUCCESS)
  74. printf("EFI: cannot initialize platform language settings\n");
  75. return ret;
  76. }
  77. #ifdef CONFIG_EFI_SECURE_BOOT
  78. /**
  79. * efi_init_secure_boot - initialize secure boot state
  80. *
  81. * Return: status code
  82. */
  83. static efi_status_t efi_init_secure_boot(void)
  84. {
  85. efi_guid_t signature_types[] = {
  86. EFI_CERT_SHA256_GUID,
  87. EFI_CERT_X509_GUID,
  88. };
  89. efi_status_t ret;
  90. ret = efi_set_variable_int(L"SignatureSupport",
  91. &efi_global_variable_guid,
  92. EFI_VARIABLE_READ_ONLY |
  93. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  94. EFI_VARIABLE_RUNTIME_ACCESS,
  95. sizeof(signature_types),
  96. &signature_types, false);
  97. if (ret != EFI_SUCCESS)
  98. printf("EFI: cannot initialize SignatureSupport variable\n");
  99. return ret;
  100. }
  101. #else
  102. static efi_status_t efi_init_secure_boot(void)
  103. {
  104. return EFI_SUCCESS;
  105. }
  106. #endif /* CONFIG_EFI_SECURE_BOOT */
  107. /**
  108. * efi_init_capsule - initialize capsule update state
  109. *
  110. * Return: status code
  111. */
  112. static efi_status_t efi_init_capsule(void)
  113. {
  114. efi_status_t ret = EFI_SUCCESS;
  115. if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_UPDATE)) {
  116. ret = efi_set_variable_int(L"CapsuleMax",
  117. &efi_guid_capsule_report,
  118. EFI_VARIABLE_READ_ONLY |
  119. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  120. EFI_VARIABLE_RUNTIME_ACCESS,
  121. 22, L"CapsuleFFFF", false);
  122. if (ret != EFI_SUCCESS)
  123. printf("EFI: cannot initialize CapsuleMax variable\n");
  124. }
  125. return ret;
  126. }
  127. /**
  128. * efi_init_os_indications() - indicate supported features for OS requests
  129. *
  130. * Set the OsIndicationsSupported variable.
  131. *
  132. * Return: status code
  133. */
  134. static efi_status_t efi_init_os_indications(void)
  135. {
  136. u64 os_indications_supported = 0;
  137. if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT))
  138. os_indications_supported |=
  139. EFI_OS_INDICATIONS_CAPSULE_RESULT_VAR_SUPPORTED;
  140. if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK))
  141. os_indications_supported |=
  142. EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED;
  143. return efi_set_variable_int(L"OsIndicationsSupported",
  144. &efi_global_variable_guid,
  145. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  146. EFI_VARIABLE_RUNTIME_ACCESS |
  147. EFI_VARIABLE_READ_ONLY,
  148. sizeof(os_indications_supported),
  149. &os_indications_supported, false);
  150. }
  151. /**
  152. * efi_init_obj_list() - Initialize and populate EFI object list
  153. *
  154. * Return: status code
  155. */
  156. efi_status_t efi_init_obj_list(void)
  157. {
  158. efi_status_t ret = EFI_SUCCESS;
  159. /* Initialize once only */
  160. if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
  161. return efi_obj_list_initialized;
  162. /* Allow unaligned memory access */
  163. allow_unaligned();
  164. /* On ARM switch from EL3 or secure mode to EL2 or non-secure mode */
  165. switch_to_non_secure_mode();
  166. /* Initialize root node */
  167. ret = efi_root_node_register();
  168. if (ret != EFI_SUCCESS)
  169. goto out;
  170. ret = efi_console_register();
  171. if (ret != EFI_SUCCESS)
  172. goto out;
  173. #ifdef CONFIG_PARTITIONS
  174. ret = efi_disk_register();
  175. if (ret != EFI_SUCCESS)
  176. goto out;
  177. #endif
  178. if (IS_ENABLED(CONFIG_EFI_RNG_PROTOCOL)) {
  179. ret = efi_rng_register();
  180. if (ret != EFI_SUCCESS)
  181. goto out;
  182. }
  183. /* Initialize variable services */
  184. ret = efi_init_variables();
  185. if (ret != EFI_SUCCESS)
  186. goto out;
  187. /* Define supported languages */
  188. ret = efi_init_platform_lang();
  189. if (ret != EFI_SUCCESS)
  190. goto out;
  191. /* Indicate supported features */
  192. ret = efi_init_os_indications();
  193. if (ret != EFI_SUCCESS)
  194. goto out;
  195. /* Initialize system table */
  196. ret = efi_initialize_system_table();
  197. if (ret != EFI_SUCCESS)
  198. goto out;
  199. if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
  200. ret = efi_tcg2_register();
  201. if (ret != EFI_SUCCESS)
  202. goto out;
  203. }
  204. /* Secure boot */
  205. ret = efi_init_secure_boot();
  206. if (ret != EFI_SUCCESS)
  207. goto out;
  208. /* Indicate supported runtime services */
  209. ret = efi_init_runtime_supported();
  210. if (ret != EFI_SUCCESS)
  211. goto out;
  212. /* Initialize EFI driver uclass */
  213. ret = efi_driver_init();
  214. if (ret != EFI_SUCCESS)
  215. goto out;
  216. #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
  217. ret = efi_gop_register();
  218. if (ret != EFI_SUCCESS)
  219. goto out;
  220. #endif
  221. #ifdef CONFIG_EFI_LOAD_FILE2_INITRD
  222. ret = efi_initrd_register();
  223. if (ret != EFI_SUCCESS)
  224. goto out;
  225. #endif
  226. #ifdef CONFIG_NET
  227. ret = efi_net_register();
  228. if (ret != EFI_SUCCESS)
  229. goto out;
  230. #endif
  231. #ifdef CONFIG_GENERATE_ACPI_TABLE
  232. ret = efi_acpi_register();
  233. if (ret != EFI_SUCCESS)
  234. goto out;
  235. #endif
  236. #ifdef CONFIG_GENERATE_SMBIOS_TABLE
  237. ret = efi_smbios_register();
  238. if (ret != EFI_SUCCESS)
  239. goto out;
  240. #endif
  241. ret = efi_watchdog_register();
  242. if (ret != EFI_SUCCESS)
  243. goto out;
  244. ret = efi_init_capsule();
  245. if (ret != EFI_SUCCESS)
  246. goto out;
  247. /* Initialize EFI runtime services */
  248. ret = efi_reset_system_init();
  249. if (ret != EFI_SUCCESS)
  250. goto out;
  251. /* Execute capsules after reboot */
  252. if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK) &&
  253. !IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK_EARLY))
  254. ret = efi_launch_capsules();
  255. out:
  256. efi_obj_list_initialized = ret;
  257. return ret;
  258. }