efi_app.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2015 Google, Inc
  4. *
  5. * EFI information obtained here:
  6. * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES
  7. *
  8. * This file implements U-Boot running as an EFI application.
  9. */
  10. #include <common.h>
  11. #include <cpu_func.h>
  12. #include <debug_uart.h>
  13. #include <dm.h>
  14. #include <errno.h>
  15. #include <init.h>
  16. #include <malloc.h>
  17. #include <linux/err.h>
  18. #include <linux/types.h>
  19. #include <efi.h>
  20. #include <efi_api.h>
  21. #include <sysreset.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. static struct efi_priv *global_priv;
  24. struct efi_system_table *efi_get_sys_table(void)
  25. {
  26. return global_priv->sys_table;
  27. }
  28. unsigned long efi_get_ram_base(void)
  29. {
  30. return global_priv->ram_base;
  31. }
  32. static efi_status_t setup_memory(struct efi_priv *priv)
  33. {
  34. struct efi_boot_services *boot = priv->boot;
  35. efi_physical_addr_t addr;
  36. efi_status_t ret;
  37. int pages;
  38. /*
  39. * Use global_data_ptr instead of gd since it is an assignment. There
  40. * are very few assignments to global_data in U-Boot and this makes
  41. * it easier to find them.
  42. */
  43. global_data_ptr = efi_malloc(priv, sizeof(struct global_data), &ret);
  44. if (!global_data_ptr)
  45. return ret;
  46. memset(gd, '\0', sizeof(*gd));
  47. gd->malloc_base = (ulong)efi_malloc(priv, CONFIG_VAL(SYS_MALLOC_F_LEN),
  48. &ret);
  49. if (!gd->malloc_base)
  50. return ret;
  51. pages = CONFIG_EFI_RAM_SIZE >> 12;
  52. /*
  53. * Don't allocate any memory above 4GB. U-Boot is a 32-bit application
  54. * so we want it to load below 4GB.
  55. */
  56. addr = 1ULL << 32;
  57. ret = boot->allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
  58. priv->image_data_type, pages, &addr);
  59. if (ret) {
  60. printf("(using pool %lx) ", ret);
  61. priv->ram_base = (ulong)efi_malloc(priv, CONFIG_EFI_RAM_SIZE,
  62. &ret);
  63. if (!priv->ram_base)
  64. return ret;
  65. priv->use_pool_for_malloc = true;
  66. } else {
  67. priv->ram_base = addr;
  68. }
  69. gd->ram_size = pages << 12;
  70. return 0;
  71. }
  72. static void free_memory(struct efi_priv *priv)
  73. {
  74. struct efi_boot_services *boot = priv->boot;
  75. if (priv->use_pool_for_malloc)
  76. efi_free(priv, (void *)priv->ram_base);
  77. else
  78. boot->free_pages(priv->ram_base, gd->ram_size >> 12);
  79. efi_free(priv, (void *)gd->malloc_base);
  80. efi_free(priv, gd);
  81. global_data_ptr = NULL;
  82. }
  83. /**
  84. * efi_main() - Start an EFI image
  85. *
  86. * This function is called by our EFI start-up code. It handles running
  87. * U-Boot. If it returns, EFI will continue. Another way to get back to EFI
  88. * is via reset_cpu().
  89. */
  90. efi_status_t EFIAPI efi_main(efi_handle_t image,
  91. struct efi_system_table *sys_table)
  92. {
  93. struct efi_priv local_priv, *priv = &local_priv;
  94. efi_status_t ret;
  95. /* Set up access to EFI data structures */
  96. efi_init(priv, "App", image, sys_table);
  97. global_priv = priv;
  98. /*
  99. * Set up the EFI debug UART so that printf() works. This is
  100. * implemented in the EFI serial driver, serial_efi.c. The application
  101. * can use printf() freely.
  102. */
  103. debug_uart_init();
  104. ret = setup_memory(priv);
  105. if (ret) {
  106. printf("Failed to set up memory: ret=%lx\n", ret);
  107. return ret;
  108. }
  109. printf("starting\n");
  110. board_init_f(GD_FLG_SKIP_RELOC);
  111. board_init_r(NULL, 0);
  112. free_memory(priv);
  113. return EFI_SUCCESS;
  114. }
  115. static void efi_exit(void)
  116. {
  117. struct efi_priv *priv = global_priv;
  118. free_memory(priv);
  119. printf("U-Boot EFI exiting\n");
  120. priv->boot->exit(priv->parent_image, EFI_SUCCESS, 0, NULL);
  121. }
  122. static int efi_sysreset_request(struct udevice *dev, enum sysreset_t type)
  123. {
  124. efi_exit();
  125. return -EINPROGRESS;
  126. }
  127. static const struct udevice_id efi_sysreset_ids[] = {
  128. { .compatible = "efi,reset" },
  129. { }
  130. };
  131. static struct sysreset_ops efi_sysreset_ops = {
  132. .request = efi_sysreset_request,
  133. };
  134. U_BOOT_DRIVER(efi_sysreset) = {
  135. .name = "efi-sysreset",
  136. .id = UCLASS_SYSRESET,
  137. .of_match = efi_sysreset_ids,
  138. .ops = &efi_sysreset_ops,
  139. };