efi.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * Common EFI functions
  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. /*
  20. * Unfortunately we cannot access any code outside what is built especially
  21. * for the stub. lib/string.c is already being built for the U-Boot payload
  22. * so it uses the wrong compiler flags. Add our own memset() here.
  23. */
  24. static void efi_memset(void *ptr, int ch, int size)
  25. {
  26. char *dest = ptr;
  27. while (size-- > 0)
  28. *dest++ = ch;
  29. }
  30. /*
  31. * Since the EFI stub cannot access most of the U-Boot code, add our own
  32. * simple console output functions here. The EFI app will not use these since
  33. * it can use the normal console.
  34. */
  35. void efi_putc(struct efi_priv *priv, const char ch)
  36. {
  37. struct efi_simple_text_output_protocol *con = priv->sys_table->con_out;
  38. uint16_t ucode[2];
  39. ucode[0] = ch;
  40. ucode[1] = '\0';
  41. con->output_string(con, ucode);
  42. }
  43. void efi_puts(struct efi_priv *priv, const char *str)
  44. {
  45. while (*str)
  46. efi_putc(priv, *str++);
  47. }
  48. int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image,
  49. struct efi_system_table *sys_table)
  50. {
  51. efi_guid_t loaded_image_guid = LOADED_IMAGE_PROTOCOL_GUID;
  52. struct efi_boot_services *boot = sys_table->boottime;
  53. struct efi_loaded_image *loaded_image;
  54. int ret;
  55. efi_memset(priv, '\0', sizeof(*priv));
  56. priv->sys_table = sys_table;
  57. priv->boot = sys_table->boottime;
  58. priv->parent_image = image;
  59. priv->run = sys_table->runtime;
  60. efi_puts(priv, "U-Boot EFI ");
  61. efi_puts(priv, banner);
  62. efi_putc(priv, ' ');
  63. ret = boot->open_protocol(priv->parent_image, &loaded_image_guid,
  64. (void **)&loaded_image, &priv->parent_image,
  65. NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  66. if (ret) {
  67. efi_puts(priv, "Failed to get loaded image protocol\n");
  68. return ret;
  69. }
  70. priv->image_data_type = loaded_image->image_data_type;
  71. return 0;
  72. }
  73. void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp)
  74. {
  75. struct efi_boot_services *boot = priv->boot;
  76. void *buf = NULL;
  77. *retp = boot->allocate_pool(priv->image_data_type, size, &buf);
  78. return buf;
  79. }
  80. void efi_free(struct efi_priv *priv, void *ptr)
  81. {
  82. struct efi_boot_services *boot = priv->boot;
  83. boot->free_pool(ptr);
  84. }