efi_app.c 3.1 KB

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