Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 3 | * Hello world EFI application |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 4 | * |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 5 | * Copyright 2020, Heinrich Schuchardt <xypron.glpk@gmx.de> |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 6 | * |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 7 | * This test program is used to test the invocation of an EFI application. |
| 8 | * It writes |
| 9 | * |
| 10 | * * a greeting |
| 11 | * * the firmware's UEFI version |
| 12 | * * the installed configuration tables |
| 13 | * * the boot device's device path and the file path |
| 14 | * |
| 15 | * to the console. |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 18 | #include <efi_api.h> |
| 19 | |
Heinrich Schuchardt | 788ad41 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 20 | static const efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID; |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 21 | static const efi_guid_t device_path_to_text_protocol_guid = |
| 22 | EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID; |
| 23 | static const efi_guid_t device_path_guid = EFI_DEVICE_PATH_PROTOCOL_GUID; |
Heinrich Schuchardt | 5a77152 | 2018-01-19 20:24:42 +0100 | [diff] [blame] | 24 | static const efi_guid_t fdt_guid = EFI_FDT_GUID; |
Bin Meng | 8e94329 | 2018-06-27 20:38:04 -0700 | [diff] [blame] | 25 | static const efi_guid_t acpi_guid = EFI_ACPI_TABLE_GUID; |
Heinrich Schuchardt | 5a77152 | 2018-01-19 20:24:42 +0100 | [diff] [blame] | 26 | static const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID; |
| 27 | |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 28 | static struct efi_system_table *systable; |
| 29 | static struct efi_boot_services *boottime; |
| 30 | static struct efi_simple_text_output_protocol *con_out; |
| 31 | |
Heinrich Schuchardt | 5b669d8 | 2018-09-24 23:53:51 +0200 | [diff] [blame] | 32 | /** |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 33 | * print_uefi_revision() - print UEFI revision number |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 34 | */ |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 35 | static void print_uefi_revision(void) |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 36 | { |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 37 | u16 rev[] = u"0.0.0"; |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 38 | |
Heinrich Schuchardt | a0a61dd | 2018-02-05 18:24:26 +0100 | [diff] [blame] | 39 | rev[0] = (systable->hdr.revision >> 16) + '0'; |
| 40 | rev[4] = systable->hdr.revision & 0xffff; |
| 41 | for (; rev[4] >= 10;) { |
| 42 | rev[4] -= 10; |
| 43 | ++rev[2]; |
| 44 | } |
| 45 | /* Third digit is only to be shown if non-zero */ |
| 46 | if (rev[4]) |
| 47 | rev[4] += '0'; |
| 48 | else |
| 49 | rev[3] = 0; |
| 50 | |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 51 | con_out->output_string(con_out, u"Running on UEFI "); |
Heinrich Schuchardt | a0a61dd | 2018-02-05 18:24:26 +0100 | [diff] [blame] | 52 | con_out->output_string(con_out, rev); |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 53 | con_out->output_string(con_out, u"\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 54 | } |
Heinrich Schuchardt | a0a61dd | 2018-02-05 18:24:26 +0100 | [diff] [blame] | 55 | |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 56 | /** |
| 57 | * print_config_tables() - print configuration tables |
| 58 | */ |
| 59 | static void print_config_tables(void) |
| 60 | { |
| 61 | efi_uintn_t i; |
| 62 | |
Heinrich Schuchardt | 5a77152 | 2018-01-19 20:24:42 +0100 | [diff] [blame] | 63 | /* Find configuration tables */ |
| 64 | for (i = 0; i < systable->nr_tables; ++i) { |
Heinrich Schuchardt | b2b186c | 2019-01-20 08:20:32 +0100 | [diff] [blame] | 65 | if (!memcmp(&systable->tables[i].guid, &fdt_guid, |
| 66 | sizeof(efi_guid_t))) |
Heinrich Schuchardt | 5b669d8 | 2018-09-24 23:53:51 +0200 | [diff] [blame] | 67 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 68 | (con_out, u"Have device tree\r\n"); |
Heinrich Schuchardt | b2b186c | 2019-01-20 08:20:32 +0100 | [diff] [blame] | 69 | if (!memcmp(&systable->tables[i].guid, &acpi_guid, |
| 70 | sizeof(efi_guid_t))) |
Heinrich Schuchardt | 5b669d8 | 2018-09-24 23:53:51 +0200 | [diff] [blame] | 71 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 72 | (con_out, u"Have ACPI 2.0 table\r\n"); |
Heinrich Schuchardt | b2b186c | 2019-01-20 08:20:32 +0100 | [diff] [blame] | 73 | if (!memcmp(&systable->tables[i].guid, &smbios_guid, |
| 74 | sizeof(efi_guid_t))) |
Heinrich Schuchardt | 5b669d8 | 2018-09-24 23:53:51 +0200 | [diff] [blame] | 75 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 76 | (con_out, u"Have SMBIOS table\r\n"); |
Heinrich Schuchardt | 5a77152 | 2018-01-19 20:24:42 +0100 | [diff] [blame] | 77 | } |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
| 81 | * print_load_options() - print load options |
| 82 | * |
| 83 | * @systable: system table |
| 84 | * @con_out: simple text output protocol |
| 85 | */ |
| 86 | void print_load_options(struct efi_loaded_image *loaded_image) |
| 87 | { |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 88 | /* Output the load options */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 89 | con_out->output_string(con_out, u"Load options: "); |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 90 | if (loaded_image->load_options_size && loaded_image->load_options) |
| 91 | con_out->output_string(con_out, |
| 92 | (u16 *)loaded_image->load_options); |
| 93 | else |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 94 | con_out->output_string(con_out, u"<none>"); |
| 95 | con_out->output_string(con_out, u"\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * print_device_path() - print device path |
| 100 | * |
| 101 | * @device_path: device path to print |
| 102 | * @dp2txt: device path to text protocol |
| 103 | */ |
| 104 | efi_status_t print_device_path(struct efi_device_path *device_path, |
| 105 | struct efi_device_path_to_text_protocol *dp2txt) |
| 106 | { |
| 107 | u16 *string; |
| 108 | efi_status_t ret; |
| 109 | |
| 110 | if (!device_path) { |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 111 | con_out->output_string(con_out, u"<none>\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 112 | return EFI_SUCCESS; |
| 113 | } |
| 114 | |
| 115 | string = dp2txt->convert_device_path_to_text(device_path, true, false); |
| 116 | if (!string) { |
| 117 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 118 | (con_out, u"Cannot convert device path to text\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 119 | return EFI_OUT_OF_RESOURCES; |
| 120 | } |
| 121 | con_out->output_string(con_out, string); |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 122 | con_out->output_string(con_out, u"\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 123 | ret = boottime->free_pool(string); |
| 124 | if (ret != EFI_SUCCESS) { |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 125 | con_out->output_string(con_out, u"Cannot free pool memory\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 126 | return ret; |
| 127 | } |
| 128 | return EFI_SUCCESS; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * efi_main() - entry point of the EFI application. |
| 133 | * |
| 134 | * @handle: handle of the loaded image |
| 135 | * @systab: system table |
Heinrich Schuchardt | fbe9021 | 2022-01-20 19:48:20 +0100 | [diff] [blame] | 136 | * Return: status code |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 137 | */ |
| 138 | efi_status_t EFIAPI efi_main(efi_handle_t handle, |
| 139 | struct efi_system_table *systab) |
| 140 | { |
| 141 | struct efi_loaded_image *loaded_image; |
| 142 | struct efi_device_path_to_text_protocol *device_path_to_text; |
| 143 | struct efi_device_path *device_path; |
| 144 | efi_status_t ret; |
| 145 | |
| 146 | systable = systab; |
| 147 | boottime = systable->boottime; |
| 148 | con_out = systable->con_out; |
| 149 | |
| 150 | /* UEFI requires CR LF */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 151 | con_out->output_string(con_out, u"Hello, world!\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 152 | |
| 153 | print_uefi_revision(); |
| 154 | print_config_tables(); |
| 155 | |
| 156 | /* Get the loaded image protocol */ |
| 157 | ret = boottime->handle_protocol(handle, &loaded_image_guid, |
| 158 | (void **)&loaded_image); |
| 159 | if (ret != EFI_SUCCESS) { |
| 160 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 161 | (con_out, u"Cannot open loaded image protocol\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 162 | goto out; |
| 163 | } |
| 164 | print_load_options(loaded_image); |
| 165 | |
| 166 | /* Get the device path to text protocol */ |
| 167 | ret = boottime->locate_protocol(&device_path_to_text_protocol_guid, |
| 168 | NULL, (void **)&device_path_to_text); |
| 169 | if (ret != EFI_SUCCESS) { |
| 170 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 171 | (con_out, u"Cannot open device path to text protocol\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 172 | goto out; |
| 173 | } |
| 174 | if (!loaded_image->device_handle) { |
| 175 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 176 | (con_out, u"Missing device handle\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 177 | goto out; |
| 178 | } |
| 179 | ret = boottime->handle_protocol(loaded_image->device_handle, |
| 180 | &device_path_guid, |
| 181 | (void **)&device_path); |
| 182 | if (ret != EFI_SUCCESS) { |
| 183 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 184 | (con_out, u"Missing device path for device handle\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 185 | goto out; |
| 186 | } |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 187 | con_out->output_string(con_out, u"Boot device: "); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 188 | ret = print_device_path(device_path, device_path_to_text); |
| 189 | if (ret != EFI_SUCCESS) |
| 190 | goto out; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 191 | con_out->output_string(con_out, u"File path: "); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 192 | ret = print_device_path(loaded_image->file_path, device_path_to_text); |
| 193 | if (ret != EFI_SUCCESS) |
| 194 | goto out; |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 195 | |
| 196 | out: |
| 197 | boottime->exit(handle, ret, 0, NULL); |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 198 | |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 199 | /* We should never arrive here */ |
| 200 | return ret; |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 201 | } |