Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 867a6ac | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * |
Simon Glass | 867a6ac | 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 | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame^] | 14 | #include <malloc.h> |
Simon Glass | 867a6ac | 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 | 867a6ac | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 20 | /* |
| 21 | * Unfortunately we cannot access any code outside what is built especially |
| 22 | * for the stub. lib/string.c is already being built for the U-Boot payload |
| 23 | * so it uses the wrong compiler flags. Add our own memset() here. |
| 24 | */ |
| 25 | static void efi_memset(void *ptr, int ch, int size) |
| 26 | { |
| 27 | char *dest = ptr; |
| 28 | |
| 29 | while (size-- > 0) |
| 30 | *dest++ = ch; |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * Since the EFI stub cannot access most of the U-Boot code, add our own |
| 35 | * simple console output functions here. The EFI app will not use these since |
| 36 | * it can use the normal console. |
| 37 | */ |
| 38 | void efi_putc(struct efi_priv *priv, const char ch) |
| 39 | { |
| 40 | struct efi_simple_text_output_protocol *con = priv->sys_table->con_out; |
| 41 | uint16_t ucode[2]; |
| 42 | |
| 43 | ucode[0] = ch; |
| 44 | ucode[1] = '\0'; |
| 45 | con->output_string(con, ucode); |
| 46 | } |
| 47 | |
| 48 | void efi_puts(struct efi_priv *priv, const char *str) |
| 49 | { |
| 50 | while (*str) |
| 51 | efi_putc(priv, *str++); |
| 52 | } |
| 53 | |
| 54 | int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image, |
| 55 | struct efi_system_table *sys_table) |
| 56 | { |
Heinrich Schuchardt | dec88e4 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 57 | efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID; |
Simon Glass | 867a6ac | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 58 | struct efi_boot_services *boot = sys_table->boottime; |
| 59 | struct efi_loaded_image *loaded_image; |
| 60 | int ret; |
| 61 | |
| 62 | efi_memset(priv, '\0', sizeof(*priv)); |
| 63 | priv->sys_table = sys_table; |
| 64 | priv->boot = sys_table->boottime; |
| 65 | priv->parent_image = image; |
| 66 | priv->run = sys_table->runtime; |
| 67 | |
| 68 | efi_puts(priv, "U-Boot EFI "); |
| 69 | efi_puts(priv, banner); |
| 70 | efi_putc(priv, ' '); |
| 71 | |
| 72 | ret = boot->open_protocol(priv->parent_image, &loaded_image_guid, |
Heinrich Schuchardt | faea104 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 73 | (void **)&loaded_image, priv->parent_image, |
Simon Glass | 867a6ac | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 74 | NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 75 | if (ret) { |
| 76 | efi_puts(priv, "Failed to get loaded image protocol\n"); |
| 77 | return ret; |
| 78 | } |
| 79 | priv->image_data_type = loaded_image->image_data_type; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp) |
| 85 | { |
| 86 | struct efi_boot_services *boot = priv->boot; |
| 87 | void *buf = NULL; |
| 88 | |
| 89 | *retp = boot->allocate_pool(priv->image_data_type, size, &buf); |
| 90 | |
| 91 | return buf; |
| 92 | } |
| 93 | |
| 94 | void efi_free(struct efi_priv *priv, void *ptr) |
| 95 | { |
| 96 | struct efi_boot_services *boot = priv->boot; |
| 97 | |
| 98 | boot->free_pool(ptr); |
| 99 | } |