blob: 10666dc0f2257b814c75a234c08dbd39066d091a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassfac4ced2016-11-07 08:47:08 -07002/*
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +01003 * Hello world EFI application
Simon Glassfac4ced2016-11-07 08:47:08 -07004 *
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +01005 * Copyright 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
Simon Glassfac4ced2016-11-07 08:47:08 -07006 *
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +01007 * 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 Glassfac4ced2016-11-07 08:47:08 -070016 */
17
Simon Glassfac4ced2016-11-07 08:47:08 -070018#include <efi_api.h>
19
Heinrich Schuchardt788ad412019-04-20 07:39:11 +020020static const efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +010021static const efi_guid_t device_path_to_text_protocol_guid =
22 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
23static const efi_guid_t device_path_guid = EFI_DEVICE_PATH_PROTOCOL_GUID;
Heinrich Schuchardt5a771522018-01-19 20:24:42 +010024static const efi_guid_t fdt_guid = EFI_FDT_GUID;
Bin Meng8e943292018-06-27 20:38:04 -070025static const efi_guid_t acpi_guid = EFI_ACPI_TABLE_GUID;
Heinrich Schuchardt5a771522018-01-19 20:24:42 +010026static const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
27
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +010028static struct efi_system_table *systable;
29static struct efi_boot_services *boottime;
30static struct efi_simple_text_output_protocol *con_out;
31
Heinrich Schuchardt5b669d82018-09-24 23:53:51 +020032/**
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +010033 * print_uefi_revision() - print UEFI revision number
Heinrich Schuchardt08645802017-11-26 14:05:20 +010034 */
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +010035static void print_uefi_revision(void)
Simon Glassfac4ced2016-11-07 08:47:08 -070036{
Simon Glass90975372022-01-23 12:55:12 -070037 u16 rev[] = u"0.0.0";
Simon Glassfac4ced2016-11-07 08:47:08 -070038
Heinrich Schuchardta0a61dd2018-02-05 18:24:26 +010039 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 Glass90975372022-01-23 12:55:12 -070051 con_out->output_string(con_out, u"Running on UEFI ");
Heinrich Schuchardta0a61dd2018-02-05 18:24:26 +010052 con_out->output_string(con_out, rev);
Simon Glass90975372022-01-23 12:55:12 -070053 con_out->output_string(con_out, u"\r\n");
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +010054}
Heinrich Schuchardta0a61dd2018-02-05 18:24:26 +010055
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +010056/**
57 * print_config_tables() - print configuration tables
58 */
59static void print_config_tables(void)
60{
61 efi_uintn_t i;
62
Heinrich Schuchardt5a771522018-01-19 20:24:42 +010063 /* Find configuration tables */
64 for (i = 0; i < systable->nr_tables; ++i) {
Heinrich Schuchardtb2b186c2019-01-20 08:20:32 +010065 if (!memcmp(&systable->tables[i].guid, &fdt_guid,
66 sizeof(efi_guid_t)))
Heinrich Schuchardt5b669d82018-09-24 23:53:51 +020067 con_out->output_string
Simon Glass90975372022-01-23 12:55:12 -070068 (con_out, u"Have device tree\r\n");
Heinrich Schuchardtb2b186c2019-01-20 08:20:32 +010069 if (!memcmp(&systable->tables[i].guid, &acpi_guid,
70 sizeof(efi_guid_t)))
Heinrich Schuchardt5b669d82018-09-24 23:53:51 +020071 con_out->output_string
Simon Glass90975372022-01-23 12:55:12 -070072 (con_out, u"Have ACPI 2.0 table\r\n");
Heinrich Schuchardtb2b186c2019-01-20 08:20:32 +010073 if (!memcmp(&systable->tables[i].guid, &smbios_guid,
74 sizeof(efi_guid_t)))
Heinrich Schuchardt5b669d82018-09-24 23:53:51 +020075 con_out->output_string
Simon Glass90975372022-01-23 12:55:12 -070076 (con_out, u"Have SMBIOS table\r\n");
Heinrich Schuchardt5a771522018-01-19 20:24:42 +010077 }
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +010078}
79
80/**
81 * print_load_options() - print load options
82 *
83 * @systable: system table
84 * @con_out: simple text output protocol
85 */
86void print_load_options(struct efi_loaded_image *loaded_image)
87{
Heinrich Schuchardt08645802017-11-26 14:05:20 +010088 /* Output the load options */
Simon Glass90975372022-01-23 12:55:12 -070089 con_out->output_string(con_out, u"Load options: ");
Heinrich Schuchardt08645802017-11-26 14:05:20 +010090 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 Glass90975372022-01-23 12:55:12 -070094 con_out->output_string(con_out, u"<none>");
95 con_out->output_string(con_out, u"\r\n");
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +010096}
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 */
104efi_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 Glass90975372022-01-23 12:55:12 -0700111 con_out->output_string(con_out, u"<none>\r\n");
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +0100112 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 Glass90975372022-01-23 12:55:12 -0700118 (con_out, u"Cannot convert device path to text\r\n");
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +0100119 return EFI_OUT_OF_RESOURCES;
120 }
121 con_out->output_string(con_out, string);
Simon Glass90975372022-01-23 12:55:12 -0700122 con_out->output_string(con_out, u"\r\n");
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +0100123 ret = boottime->free_pool(string);
124 if (ret != EFI_SUCCESS) {
Simon Glass90975372022-01-23 12:55:12 -0700125 con_out->output_string(con_out, u"Cannot free pool memory\r\n");
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +0100126 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 Schuchardtfbe90212022-01-20 19:48:20 +0100136 * Return: status code
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +0100137 */
138efi_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 Glass90975372022-01-23 12:55:12 -0700151 con_out->output_string(con_out, u"Hello, world!\r\n");
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +0100152
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 Glass90975372022-01-23 12:55:12 -0700161 (con_out, u"Cannot open loaded image protocol\r\n");
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +0100162 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 Glass90975372022-01-23 12:55:12 -0700171 (con_out, u"Cannot open device path to text protocol\r\n");
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +0100172 goto out;
173 }
174 if (!loaded_image->device_handle) {
175 con_out->output_string
Simon Glass90975372022-01-23 12:55:12 -0700176 (con_out, u"Missing device handle\r\n");
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +0100177 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 Glass90975372022-01-23 12:55:12 -0700184 (con_out, u"Missing device path for device handle\r\n");
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +0100185 goto out;
186 }
Simon Glass90975372022-01-23 12:55:12 -0700187 con_out->output_string(con_out, u"Boot device: ");
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +0100188 ret = print_device_path(device_path, device_path_to_text);
189 if (ret != EFI_SUCCESS)
190 goto out;
Simon Glass90975372022-01-23 12:55:12 -0700191 con_out->output_string(con_out, u"File path: ");
Heinrich Schuchardtd9b2ff72021-01-12 12:40:11 +0100192 ret = print_device_path(loaded_image->file_path, device_path_to_text);
193 if (ret != EFI_SUCCESS)
194 goto out;
Heinrich Schuchardt08645802017-11-26 14:05:20 +0100195
196out:
197 boottime->exit(handle, ret, 0, NULL);
Simon Glassfac4ced2016-11-07 08:47:08 -0700198
Heinrich Schuchardt08645802017-11-26 14:05:20 +0100199 /* We should never arrive here */
200 return ret;
Simon Glassfac4ced2016-11-07 08:47:08 -0700201}