efi_app.c 3.1 KB

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