efi.c 2.4 KB

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