mtd: sf: Make sf_mtd.c more robust
[platform/kernel/u-boot.git] / lib / efi_selftest / efi_selftest_loaded_image.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * efi_selftest_loaded_image
4  *
5  * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
6  *
7  * This unit test checks the Loaded Image Protocol.
8  */
9
10 #include <efi_selftest.h>
11
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;
17
18 /*
19  * Setup unit test.
20  *
21  * @handle:     handle of the loaded image
22  * @systable:   system table
23  */
24 static int setup(const efi_handle_t img_handle,
25                  const struct efi_system_table *systable)
26 {
27         boottime = systable->boottime;
28         image_handle = img_handle;
29
30         return EFI_ST_SUCCESS;
31 }
32
33 /*
34  * Execute unit test.
35  *
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.
38  */
39 static int execute(void)
40 {
41         efi_status_t ret;
42         efi_uintn_t i, protocol_buffer_count = 0;
43         efi_guid_t **protocol_buffer = NULL;
44         bool found = false;
45         struct efi_loaded_image *loaded_image_protocol;
46
47         /*
48          * Get the GUIDs of all protocols installed on the handle.
49          */
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;
55         }
56         if (!protocol_buffer_count | !protocol_buffer) {
57                 efi_st_error("ProtocolsPerHandle returned no protocol\n");
58                 return EFI_ST_FAILURE;
59         }
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 (efi_st_memcmp(protocol_buffer[i],
64                                   &loaded_image_protocol_guid,
65                                   sizeof(efi_guid_t)))
66                         found = true;
67         }
68         if (!found) {
69                 efi_st_printf("LoadedImageProtocol not found\n");
70                 return EFI_ST_FAILURE;
71         }
72         ret = boottime->free_pool(protocol_buffer);
73         if (ret != EFI_SUCCESS) {
74                 efi_st_error("FreePool failed\n");
75                 return EFI_ST_FAILURE;
76         }
77
78         /*
79          * Open the loaded image protocol.
80          */
81         ret = boottime->open_protocol(image_handle, &loaded_image_protocol_guid,
82                                       (void **)&loaded_image_protocol, NULL,
83                                       NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
84         if (ret != EFI_SUCCESS) {
85                 efi_st_error("OpenProtocol failed\n");
86                 return EFI_ST_FAILURE;
87         }
88         if (loaded_image_protocol->revision !=
89             EFI_LOADED_IMAGE_PROTOCOL_REVISION) {
90                 efi_st_printf("Incorrect revision\n");
91                 return EFI_ST_FAILURE;
92         }
93         if (!loaded_image_protocol->system_table ||
94             loaded_image_protocol->system_table->hdr.signature !=
95             EFI_SYSTEM_TABLE_SIGNATURE) {
96                 efi_st_printf("System table reference missing\n");
97                 return EFI_ST_FAILURE;
98         }
99
100         return EFI_ST_SUCCESS;
101 }
102
103 EFI_UNIT_TEST(loadedimage) = {
104         .name = "loaded image",
105         .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
106         .setup = setup,
107         .execute = execute,
108 };