efi_setup.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI setup code
  4. *
  5. * Copyright (c) 2016-2018 Alexander Graf et al.
  6. */
  7. #define LOG_CATEGORY LOGC_EFI
  8. #include <common.h>
  9. #include <efi_loader.h>
  10. #include <efi_variable.h>
  11. #include <log.h>
  12. #include <asm-generic/unaligned.h>
  13. #define OBJ_LIST_NOT_INITIALIZED 1
  14. efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
  15. /*
  16. * Allow unaligned memory access.
  17. *
  18. * This routine is overridden by architectures providing this feature.
  19. */
  20. void __weak allow_unaligned(void)
  21. {
  22. }
  23. /**
  24. * efi_init_platform_lang() - define supported languages
  25. *
  26. * Set the PlatformLangCodes and PlatformLang variables.
  27. *
  28. * Return: status code
  29. */
  30. static efi_status_t efi_init_platform_lang(void)
  31. {
  32. efi_status_t ret;
  33. efi_uintn_t data_size = 0;
  34. char *lang = CONFIG_EFI_PLATFORM_LANG_CODES;
  35. char *pos;
  36. /*
  37. * Variable PlatformLangCodes defines the language codes that the
  38. * machine can support.
  39. */
  40. ret = efi_set_variable_int(u"PlatformLangCodes",
  41. &efi_global_variable_guid,
  42. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  43. EFI_VARIABLE_RUNTIME_ACCESS |
  44. EFI_VARIABLE_READ_ONLY,
  45. sizeof(CONFIG_EFI_PLATFORM_LANG_CODES),
  46. CONFIG_EFI_PLATFORM_LANG_CODES, false);
  47. if (ret != EFI_SUCCESS)
  48. goto out;
  49. /*
  50. * Variable PlatformLang defines the language that the machine has been
  51. * configured for.
  52. */
  53. ret = efi_get_variable_int(u"PlatformLang",
  54. &efi_global_variable_guid,
  55. NULL, &data_size, &pos, NULL);
  56. if (ret == EFI_BUFFER_TOO_SMALL) {
  57. /* The variable is already set. Do not change it. */
  58. ret = EFI_SUCCESS;
  59. goto out;
  60. }
  61. /*
  62. * The list of supported languages is semicolon separated. Use the first
  63. * language to initialize PlatformLang.
  64. */
  65. pos = strchr(lang, ';');
  66. if (pos)
  67. *pos = 0;
  68. ret = efi_set_variable_int(u"PlatformLang",
  69. &efi_global_variable_guid,
  70. EFI_VARIABLE_NON_VOLATILE |
  71. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  72. EFI_VARIABLE_RUNTIME_ACCESS,
  73. 1 + strlen(lang), lang, false);
  74. out:
  75. if (ret != EFI_SUCCESS)
  76. printf("EFI: cannot initialize platform language settings\n");
  77. return ret;
  78. }
  79. #ifdef CONFIG_EFI_SECURE_BOOT
  80. /**
  81. * efi_init_secure_boot - initialize secure boot state
  82. *
  83. * Return: status code
  84. */
  85. static efi_status_t efi_init_secure_boot(void)
  86. {
  87. efi_guid_t signature_types[] = {
  88. EFI_CERT_SHA256_GUID,
  89. EFI_CERT_X509_GUID,
  90. };
  91. efi_status_t ret;
  92. ret = efi_set_variable_int(u"SignatureSupport",
  93. &efi_global_variable_guid,
  94. EFI_VARIABLE_READ_ONLY |
  95. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  96. EFI_VARIABLE_RUNTIME_ACCESS,
  97. sizeof(signature_types),
  98. &signature_types, false);
  99. if (ret != EFI_SUCCESS)
  100. printf("EFI: cannot initialize SignatureSupport variable\n");
  101. return ret;
  102. }
  103. #else
  104. static efi_status_t efi_init_secure_boot(void)
  105. {
  106. return EFI_SUCCESS;
  107. }
  108. #endif /* CONFIG_EFI_SECURE_BOOT */
  109. /**
  110. * efi_init_capsule - initialize capsule update state
  111. *
  112. * Return: status code
  113. */
  114. static efi_status_t efi_init_capsule(void)
  115. {
  116. efi_status_t ret = EFI_SUCCESS;
  117. if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)) {
  118. u16 var_name16[12];
  119. efi_create_indexed_name(var_name16, sizeof(var_name16),
  120. "Capsule", CONFIG_EFI_CAPSULE_MAX);
  121. ret = efi_set_variable_int(u"CapsuleMax",
  122. &efi_guid_capsule_report,
  123. EFI_VARIABLE_READ_ONLY |
  124. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  125. EFI_VARIABLE_RUNTIME_ACCESS,
  126. 22, var_name16, false);
  127. if (ret != EFI_SUCCESS)
  128. printf("EFI: cannot initialize CapsuleMax variable\n");
  129. }
  130. return ret;
  131. }
  132. /**
  133. * efi_init_os_indications() - indicate supported features for OS requests
  134. *
  135. * Set the OsIndicationsSupported variable.
  136. *
  137. * Return: status code
  138. */
  139. static efi_status_t efi_init_os_indications(void)
  140. {
  141. u64 os_indications_supported = 0;
  142. if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT))
  143. os_indications_supported |=
  144. EFI_OS_INDICATIONS_CAPSULE_RESULT_VAR_SUPPORTED;
  145. if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK))
  146. os_indications_supported |=
  147. EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED;
  148. if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT))
  149. os_indications_supported |=
  150. EFI_OS_INDICATIONS_FMP_CAPSULE_SUPPORTED;
  151. return efi_set_variable_int(u"OsIndicationsSupported",
  152. &efi_global_variable_guid,
  153. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  154. EFI_VARIABLE_RUNTIME_ACCESS |
  155. EFI_VARIABLE_READ_ONLY,
  156. sizeof(os_indications_supported),
  157. &os_indications_supported, false);
  158. }
  159. /**
  160. * efi_init_early() - handle initialization at early stage
  161. *
  162. * expected to be called in board_init_r().
  163. *
  164. * Return: status code
  165. */
  166. int efi_init_early(void)
  167. {
  168. efi_status_t ret;
  169. /* Allow unaligned memory access */
  170. allow_unaligned();
  171. /* Initialize root node */
  172. ret = efi_root_node_register();
  173. if (ret != EFI_SUCCESS)
  174. goto out;
  175. ret = efi_console_register();
  176. if (ret != EFI_SUCCESS)
  177. goto out;
  178. /* Initialize EFI driver uclass */
  179. ret = efi_driver_init();
  180. if (ret != EFI_SUCCESS)
  181. goto out;
  182. return 0;
  183. out:
  184. /* never re-init UEFI subsystem */
  185. efi_obj_list_initialized = ret;
  186. return -1;
  187. }
  188. /**
  189. * efi_init_obj_list() - Initialize and populate EFI object list
  190. *
  191. * Return: status code
  192. */
  193. efi_status_t efi_init_obj_list(void)
  194. {
  195. efi_status_t ret = EFI_SUCCESS;
  196. /* Initialize once only */
  197. if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
  198. return efi_obj_list_initialized;
  199. /* Set up console modes */
  200. efi_setup_console_size();
  201. /*
  202. * Probe block devices to find the ESP.
  203. * efi_disks_register() must be called before efi_init_variables().
  204. */
  205. ret = efi_disks_register();
  206. if (ret != EFI_SUCCESS)
  207. goto out;
  208. /* Initialize variable services */
  209. ret = efi_init_variables();
  210. if (ret != EFI_SUCCESS)
  211. goto out;
  212. /* Define supported languages */
  213. ret = efi_init_platform_lang();
  214. if (ret != EFI_SUCCESS)
  215. goto out;
  216. /* Indicate supported features */
  217. ret = efi_init_os_indications();
  218. if (ret != EFI_SUCCESS)
  219. goto out;
  220. /* Initialize system table */
  221. ret = efi_initialize_system_table();
  222. if (ret != EFI_SUCCESS)
  223. goto out;
  224. if (IS_ENABLED(CONFIG_EFI_ECPT)) {
  225. ret = efi_ecpt_register();
  226. if (ret != EFI_SUCCESS)
  227. goto out;
  228. }
  229. if (IS_ENABLED(CONFIG_EFI_ESRT)) {
  230. ret = efi_esrt_register();
  231. if (ret != EFI_SUCCESS)
  232. goto out;
  233. }
  234. if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
  235. ret = efi_tcg2_register();
  236. if (ret != EFI_SUCCESS)
  237. goto out;
  238. ret = efi_tcg2_do_initial_measurement();
  239. if (ret == EFI_SECURITY_VIOLATION)
  240. goto out;
  241. }
  242. /* Install EFI_RNG_PROTOCOL */
  243. if (IS_ENABLED(CONFIG_EFI_RNG_PROTOCOL)) {
  244. ret = efi_rng_register();
  245. if (ret != EFI_SUCCESS)
  246. goto out;
  247. }
  248. if (IS_ENABLED(CONFIG_EFI_RISCV_BOOT_PROTOCOL)) {
  249. ret = efi_riscv_register();
  250. if (ret != EFI_SUCCESS)
  251. goto out;
  252. }
  253. /* Secure boot */
  254. ret = efi_init_secure_boot();
  255. if (ret != EFI_SUCCESS)
  256. goto out;
  257. /* Indicate supported runtime services */
  258. ret = efi_init_runtime_supported();
  259. if (ret != EFI_SUCCESS)
  260. goto out;
  261. if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)) {
  262. ret = efi_load_capsule_drivers();
  263. if (ret != EFI_SUCCESS)
  264. goto out;
  265. }
  266. if (IS_ENABLED(CONFIG_VIDEO)) {
  267. ret = efi_gop_register();
  268. if (ret != EFI_SUCCESS)
  269. goto out;
  270. }
  271. #ifdef CONFIG_NETDEVICES
  272. ret = efi_net_register();
  273. if (ret != EFI_SUCCESS)
  274. goto out;
  275. #endif
  276. #ifdef CONFIG_GENERATE_ACPI_TABLE
  277. ret = efi_acpi_register();
  278. if (ret != EFI_SUCCESS)
  279. goto out;
  280. #endif
  281. #ifdef CONFIG_GENERATE_SMBIOS_TABLE
  282. ret = efi_smbios_register();
  283. if (ret != EFI_SUCCESS)
  284. goto out;
  285. #endif
  286. ret = efi_watchdog_register();
  287. if (ret != EFI_SUCCESS)
  288. goto out;
  289. ret = efi_init_capsule();
  290. if (ret != EFI_SUCCESS)
  291. goto out;
  292. /* Initialize EFI runtime services */
  293. ret = efi_reset_system_init();
  294. if (ret != EFI_SUCCESS)
  295. goto out;
  296. /* Execute capsules after reboot */
  297. if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK) &&
  298. !IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK_EARLY))
  299. ret = efi_launch_capsules();
  300. out:
  301. efi_obj_list_initialized = ret;
  302. return ret;
  303. }