Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 5 | * EFI information obtained here: |
| 6 | * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES |
| 7 | * |
| 8 | * Common EFI functions |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <debug_uart.h> |
| 13 | #include <errno.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <malloc.h> |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 15 | #include <linux/err.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <efi.h> |
| 18 | #include <efi_api.h> |
| 19 | |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 20 | /* |
Simon Glass | 86ed9c5 | 2021-11-03 21:09:08 -0600 | [diff] [blame] | 21 | * Global declaration of gd. |
| 22 | * |
| 23 | * As we write to it before relocation we have to make sure it is not put into |
| 24 | * a .bss section which may overlap a .rela section. Initialization forces it |
| 25 | * into a .data section which cannot overlap any .rela section. |
| 26 | */ |
| 27 | struct global_data *global_data_ptr = (struct global_data *)~0; |
| 28 | |
| 29 | /* |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 30 | * Unfortunately we cannot access any code outside what is built especially |
| 31 | * for the stub. lib/string.c is already being built for the U-Boot payload |
| 32 | * so it uses the wrong compiler flags. Add our own memset() here. |
| 33 | */ |
| 34 | static void efi_memset(void *ptr, int ch, int size) |
| 35 | { |
| 36 | char *dest = ptr; |
| 37 | |
| 38 | while (size-- > 0) |
| 39 | *dest++ = ch; |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | * Since the EFI stub cannot access most of the U-Boot code, add our own |
| 44 | * simple console output functions here. The EFI app will not use these since |
| 45 | * it can use the normal console. |
| 46 | */ |
| 47 | void efi_putc(struct efi_priv *priv, const char ch) |
| 48 | { |
| 49 | struct efi_simple_text_output_protocol *con = priv->sys_table->con_out; |
| 50 | uint16_t ucode[2]; |
| 51 | |
| 52 | ucode[0] = ch; |
| 53 | ucode[1] = '\0'; |
| 54 | con->output_string(con, ucode); |
| 55 | } |
| 56 | |
| 57 | void efi_puts(struct efi_priv *priv, const char *str) |
| 58 | { |
| 59 | while (*str) |
| 60 | efi_putc(priv, *str++); |
| 61 | } |
| 62 | |
| 63 | int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image, |
| 64 | struct efi_system_table *sys_table) |
| 65 | { |
Heinrich Schuchardt | 788ad41 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 66 | efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID; |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 67 | struct efi_boot_services *boot = sys_table->boottime; |
| 68 | struct efi_loaded_image *loaded_image; |
| 69 | int ret; |
| 70 | |
| 71 | efi_memset(priv, '\0', sizeof(*priv)); |
| 72 | priv->sys_table = sys_table; |
| 73 | priv->boot = sys_table->boottime; |
| 74 | priv->parent_image = image; |
| 75 | priv->run = sys_table->runtime; |
| 76 | |
| 77 | efi_puts(priv, "U-Boot EFI "); |
| 78 | efi_puts(priv, banner); |
| 79 | efi_putc(priv, ' '); |
| 80 | |
| 81 | ret = boot->open_protocol(priv->parent_image, &loaded_image_guid, |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 82 | (void **)&loaded_image, priv->parent_image, |
Simon Glass | 9539e69 | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 83 | NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 84 | if (ret) { |
| 85 | efi_puts(priv, "Failed to get loaded image protocol\n"); |
| 86 | return ret; |
| 87 | } |
| 88 | priv->image_data_type = loaded_image->image_data_type; |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp) |
| 94 | { |
| 95 | struct efi_boot_services *boot = priv->boot; |
| 96 | void *buf = NULL; |
| 97 | |
| 98 | *retp = boot->allocate_pool(priv->image_data_type, size, &buf); |
| 99 | |
| 100 | return buf; |
| 101 | } |
| 102 | |
| 103 | void efi_free(struct efi_priv *priv, void *ptr) |
| 104 | { |
| 105 | struct efi_boot_services *boot = priv->boot; |
| 106 | |
| 107 | boot->free_pool(ptr); |
| 108 | } |