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 | * |
Simon Glass | cba7378 | 2024-09-01 19:18:13 -0600 | [diff] [blame] | 5 | * Copyright (c) 2016 Google, Inc |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | * |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 8 | * Copyright 2020, Heinrich Schuchardt <xypron.glpk@gmx.de> |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 9 | * |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 10 | * This test program is used to test the invocation of an EFI application. |
| 11 | * It writes |
| 12 | * |
| 13 | * * a greeting |
| 14 | * * the firmware's UEFI version |
| 15 | * * the installed configuration tables |
| 16 | * * the boot device's device path and the file path |
| 17 | * |
| 18 | * to the console. |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 21 | #include <efi_api.h> |
| 22 | |
Heinrich Schuchardt | 788ad41 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 23 | 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] | 24 | static const efi_guid_t device_path_to_text_protocol_guid = |
| 25 | EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID; |
| 26 | 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] | 27 | static const efi_guid_t fdt_guid = EFI_FDT_GUID; |
Bin Meng | 8e94329 | 2018-06-27 20:38:04 -0700 | [diff] [blame] | 28 | static const efi_guid_t acpi_guid = EFI_ACPI_TABLE_GUID; |
Heinrich Schuchardt | 5a77152 | 2018-01-19 20:24:42 +0100 | [diff] [blame] | 29 | static const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID; |
| 30 | |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 31 | static struct efi_system_table *systable; |
| 32 | static struct efi_boot_services *boottime; |
| 33 | static struct efi_simple_text_output_protocol *con_out; |
| 34 | |
Heinrich Schuchardt | 6507164 | 2022-09-01 22:23:47 +0200 | [diff] [blame] | 35 | /* |
| 36 | * Print an unsigned 32bit value as decimal number to an u16 string |
| 37 | * |
| 38 | * @value: value to be printed |
| 39 | * @buf: pointer to buffer address |
| 40 | * on return position of terminating zero word |
| 41 | */ |
| 42 | static void uint2dec(u32 value, u16 **buf) |
| 43 | { |
| 44 | u16 *pos = *buf; |
| 45 | int i; |
| 46 | u16 c; |
| 47 | u64 f; |
| 48 | |
| 49 | /* |
| 50 | * Increment by .5 and multiply with |
| 51 | * (2 << 60) / 1,000,000,000 = 0x44B82FA0.9B5A52CC |
| 52 | * to move the first digit to bit 60-63. |
| 53 | */ |
| 54 | f = 0x225C17D0; |
| 55 | f += (0x9B5A52DULL * value) >> 28; |
| 56 | f += 0x44B82FA0ULL * value; |
| 57 | |
| 58 | for (i = 0; i < 10; ++i) { |
| 59 | /* Write current digit */ |
| 60 | c = f >> 60; |
| 61 | if (c || pos != *buf) |
| 62 | *pos++ = c + '0'; |
| 63 | /* Eliminate current digit */ |
| 64 | f &= 0xfffffffffffffff; |
| 65 | /* Get next digit */ |
| 66 | f *= 0xaULL; |
| 67 | } |
| 68 | if (pos == *buf) |
| 69 | *pos++ = '0'; |
| 70 | *pos = 0; |
| 71 | *buf = pos; |
| 72 | } |
| 73 | |
Heinrich Schuchardt | 5b669d8 | 2018-09-24 23:53:51 +0200 | [diff] [blame] | 74 | /** |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 75 | * print_uefi_revision() - print UEFI revision number |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 76 | */ |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 77 | static void print_uefi_revision(void) |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 78 | { |
Heinrich Schuchardt | 6507164 | 2022-09-01 22:23:47 +0200 | [diff] [blame] | 79 | u16 rev[13] = {0}; |
| 80 | u16 *buf = rev; |
| 81 | u16 digit; |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 82 | |
Heinrich Schuchardt | 6507164 | 2022-09-01 22:23:47 +0200 | [diff] [blame] | 83 | uint2dec(systable->hdr.revision >> 16, &buf); |
| 84 | *buf++ = '.'; |
| 85 | uint2dec(systable->hdr.revision & 0xffff, &buf); |
| 86 | |
| 87 | /* Minor revision is only to be shown if non-zero */ |
| 88 | digit = *--buf; |
| 89 | if (digit == '0') { |
| 90 | *buf = 0; |
| 91 | } else { |
| 92 | *buf++ = '.'; |
| 93 | *buf = digit; |
Heinrich Schuchardt | a0a61dd | 2018-02-05 18:24:26 +0100 | [diff] [blame] | 94 | } |
Heinrich Schuchardt | a0a61dd | 2018-02-05 18:24:26 +0100 | [diff] [blame] | 95 | |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 96 | con_out->output_string(con_out, u"Running on UEFI "); |
Heinrich Schuchardt | a0a61dd | 2018-02-05 18:24:26 +0100 | [diff] [blame] | 97 | con_out->output_string(con_out, rev); |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 98 | con_out->output_string(con_out, u"\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 99 | } |
Heinrich Schuchardt | a0a61dd | 2018-02-05 18:24:26 +0100 | [diff] [blame] | 100 | |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 101 | /** |
| 102 | * print_config_tables() - print configuration tables |
| 103 | */ |
| 104 | static void print_config_tables(void) |
| 105 | { |
| 106 | efi_uintn_t i; |
| 107 | |
Heinrich Schuchardt | 5a77152 | 2018-01-19 20:24:42 +0100 | [diff] [blame] | 108 | /* Find configuration tables */ |
| 109 | for (i = 0; i < systable->nr_tables; ++i) { |
Heinrich Schuchardt | b2b186c | 2019-01-20 08:20:32 +0100 | [diff] [blame] | 110 | if (!memcmp(&systable->tables[i].guid, &fdt_guid, |
| 111 | sizeof(efi_guid_t))) |
Heinrich Schuchardt | 5b669d8 | 2018-09-24 23:53:51 +0200 | [diff] [blame] | 112 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 113 | (con_out, u"Have device tree\r\n"); |
Heinrich Schuchardt | b2b186c | 2019-01-20 08:20:32 +0100 | [diff] [blame] | 114 | if (!memcmp(&systable->tables[i].guid, &acpi_guid, |
| 115 | sizeof(efi_guid_t))) |
Heinrich Schuchardt | 5b669d8 | 2018-09-24 23:53:51 +0200 | [diff] [blame] | 116 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 117 | (con_out, u"Have ACPI 2.0 table\r\n"); |
Heinrich Schuchardt | b2b186c | 2019-01-20 08:20:32 +0100 | [diff] [blame] | 118 | if (!memcmp(&systable->tables[i].guid, &smbios_guid, |
| 119 | sizeof(efi_guid_t))) |
Heinrich Schuchardt | 5b669d8 | 2018-09-24 23:53:51 +0200 | [diff] [blame] | 120 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 121 | (con_out, u"Have SMBIOS table\r\n"); |
Heinrich Schuchardt | 5a77152 | 2018-01-19 20:24:42 +0100 | [diff] [blame] | 122 | } |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
| 126 | * print_load_options() - print load options |
| 127 | * |
| 128 | * @systable: system table |
| 129 | * @con_out: simple text output protocol |
| 130 | */ |
Heinrich Schuchardt | fc7d342 | 2023-02-10 08:45:38 +0100 | [diff] [blame] | 131 | static void print_load_options(struct efi_loaded_image *loaded_image) |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 132 | { |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 133 | /* Output the load options */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 134 | con_out->output_string(con_out, u"Load options: "); |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 135 | if (loaded_image->load_options_size && loaded_image->load_options) |
| 136 | con_out->output_string(con_out, |
| 137 | (u16 *)loaded_image->load_options); |
| 138 | else |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 139 | con_out->output_string(con_out, u"<none>"); |
| 140 | con_out->output_string(con_out, u"\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
| 144 | * print_device_path() - print device path |
| 145 | * |
| 146 | * @device_path: device path to print |
| 147 | * @dp2txt: device path to text protocol |
| 148 | */ |
Heinrich Schuchardt | fc7d342 | 2023-02-10 08:45:38 +0100 | [diff] [blame] | 149 | static |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 150 | efi_status_t print_device_path(struct efi_device_path *device_path, |
| 151 | struct efi_device_path_to_text_protocol *dp2txt) |
| 152 | { |
| 153 | u16 *string; |
| 154 | efi_status_t ret; |
| 155 | |
| 156 | if (!device_path) { |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 157 | con_out->output_string(con_out, u"<none>\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 158 | return EFI_SUCCESS; |
| 159 | } |
| 160 | |
| 161 | string = dp2txt->convert_device_path_to_text(device_path, true, false); |
| 162 | if (!string) { |
| 163 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 164 | (con_out, u"Cannot convert device path to text\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 165 | return EFI_OUT_OF_RESOURCES; |
| 166 | } |
| 167 | con_out->output_string(con_out, string); |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 168 | con_out->output_string(con_out, u"\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 169 | ret = boottime->free_pool(string); |
| 170 | if (ret != EFI_SUCCESS) { |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 171 | 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] | 172 | return ret; |
| 173 | } |
| 174 | return EFI_SUCCESS; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * efi_main() - entry point of the EFI application. |
| 179 | * |
| 180 | * @handle: handle of the loaded image |
| 181 | * @systab: system table |
Heinrich Schuchardt | fbe9021 | 2022-01-20 19:48:20 +0100 | [diff] [blame] | 182 | * Return: status code |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 183 | */ |
| 184 | efi_status_t EFIAPI efi_main(efi_handle_t handle, |
| 185 | struct efi_system_table *systab) |
| 186 | { |
| 187 | struct efi_loaded_image *loaded_image; |
| 188 | struct efi_device_path_to_text_protocol *device_path_to_text; |
| 189 | struct efi_device_path *device_path; |
| 190 | efi_status_t ret; |
| 191 | |
| 192 | systable = systab; |
| 193 | boottime = systable->boottime; |
| 194 | con_out = systable->con_out; |
| 195 | |
| 196 | /* UEFI requires CR LF */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 197 | con_out->output_string(con_out, u"Hello, world!\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 198 | |
| 199 | print_uefi_revision(); |
| 200 | print_config_tables(); |
| 201 | |
| 202 | /* Get the loaded image protocol */ |
Heinrich Schuchardt | 5aaea01 | 2023-04-04 07:17:48 +0200 | [diff] [blame] | 203 | ret = boottime->open_protocol(handle, &loaded_image_guid, |
| 204 | (void **)&loaded_image, NULL, NULL, |
| 205 | EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 206 | |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 207 | if (ret != EFI_SUCCESS) { |
| 208 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 209 | (con_out, u"Cannot open loaded image protocol\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 210 | goto out; |
| 211 | } |
| 212 | print_load_options(loaded_image); |
| 213 | |
| 214 | /* Get the device path to text protocol */ |
| 215 | ret = boottime->locate_protocol(&device_path_to_text_protocol_guid, |
| 216 | NULL, (void **)&device_path_to_text); |
| 217 | if (ret != EFI_SUCCESS) { |
| 218 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 219 | (con_out, u"Cannot open device path to text protocol\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 220 | goto out; |
| 221 | } |
Heinrich Schuchardt | c4ff7c0 | 2023-05-12 21:23:15 +0200 | [diff] [blame] | 222 | con_out->output_string(con_out, u"File path: "); |
| 223 | ret = print_device_path(loaded_image->file_path, device_path_to_text); |
| 224 | if (ret != EFI_SUCCESS) |
| 225 | goto out; |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 226 | if (!loaded_image->device_handle) { |
| 227 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 228 | (con_out, u"Missing device handle\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 229 | goto out; |
| 230 | } |
Heinrich Schuchardt | 5aaea01 | 2023-04-04 07:17:48 +0200 | [diff] [blame] | 231 | ret = boottime->open_protocol(loaded_image->device_handle, |
| 232 | &device_path_guid, |
| 233 | (void **)&device_path, NULL, NULL, |
| 234 | EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 235 | if (ret != EFI_SUCCESS) { |
| 236 | con_out->output_string |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 237 | (con_out, u"Missing device path for device handle\r\n"); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 238 | goto out; |
| 239 | } |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 240 | con_out->output_string(con_out, u"Boot device: "); |
Heinrich Schuchardt | d9b2ff7 | 2021-01-12 12:40:11 +0100 | [diff] [blame] | 241 | ret = print_device_path(device_path, device_path_to_text); |
| 242 | if (ret != EFI_SUCCESS) |
| 243 | goto out; |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 244 | |
| 245 | out: |
| 246 | boottime->exit(handle, ret, 0, NULL); |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 247 | |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 248 | /* We should never arrive here */ |
| 249 | return ret; |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 250 | } |