efi.c 2.3 KB

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