1 // SPDX-License-Identifier: GPL-2.0+
3 * efi_selftest_loaded_image
7 * This unit test checks the Loaded Image Protocol.
10 #include <efi_selftest.h>
12 static efi_guid_t loaded_image_protocol_guid =
13 EFI_GUID(0x5b1b31a1, 0x9562, 0x11d2,
14 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b);
15 static struct efi_boot_services *boottime;
16 efi_handle_t image_handle;
21 * @handle: handle of the loaded image
22 * @systable: system table
24 static int setup(const efi_handle_t img_handle,
25 const struct efi_system_table *systable)
27 boottime = systable->boottime;
28 image_handle = img_handle;
30 return EFI_ST_SUCCESS;
36 * Verify that the loaded image protocol is installed on the image handle.
37 * Verify that the loaded image protocol points to the system table.
39 static int execute(void)
42 efi_uintn_t i, protocol_buffer_count = 0;
43 efi_guid_t **protocol_buffer = NULL;
45 struct efi_loaded_image *loaded_image_protocol;
48 * Get the GUIDs of all protocols installed on the handle.
50 ret = boottime->protocols_per_handle(image_handle, &protocol_buffer,
51 &protocol_buffer_count);
52 if (ret != EFI_SUCCESS) {
53 efi_st_error("ProtocolsPerHandle failed\n");
54 return EFI_ST_FAILURE;
56 if (!protocol_buffer_count || !protocol_buffer) {
57 efi_st_error("ProtocolsPerHandle returned no protocol\n");
58 return EFI_ST_FAILURE;
60 efi_st_printf("%u protocols installed on image handle\n",
61 (unsigned int)protocol_buffer_count);
62 for (i = 0; i < protocol_buffer_count; ++i) {
63 if (memcmp(protocol_buffer[i], &loaded_image_protocol_guid,
68 efi_st_printf("LoadedImageProtocol not found\n");
69 return EFI_ST_FAILURE;
71 ret = boottime->free_pool(protocol_buffer);
72 if (ret != EFI_SUCCESS) {
73 efi_st_error("FreePool failed\n");
74 return EFI_ST_FAILURE;
78 * Open the loaded image protocol.
80 ret = boottime->open_protocol(image_handle, &loaded_image_protocol_guid,
81 (void **)&loaded_image_protocol, NULL,
82 NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
83 if (ret != EFI_SUCCESS) {
84 efi_st_error("OpenProtocol failed\n");
85 return EFI_ST_FAILURE;
87 if (loaded_image_protocol->revision !=
88 EFI_LOADED_IMAGE_PROTOCOL_REVISION) {
89 efi_st_printf("Incorrect revision\n");
90 return EFI_ST_FAILURE;
92 if (!loaded_image_protocol->system_table ||
93 loaded_image_protocol->system_table->hdr.signature !=
94 EFI_SYSTEM_TABLE_SIGNATURE) {
95 efi_st_printf("System table reference missing\n");
96 return EFI_ST_FAILURE;
99 return EFI_ST_SUCCESS;
102 EFI_UNIT_TEST(loadedimage) = {
103 .name = "loaded image",
104 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,