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