efi_app.c 3.7 KB

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