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