uboot/lib/efi_selftest/efi_selftest_loaded_image.c
<<
>>
Prefs
   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
  12static efi_guid_t loaded_image_protocol_guid =
  13        EFI_GUID(0x5b1b31a1, 0x9562, 0x11d2,
  14                 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b);
  15static struct efi_boot_services *boottime;
  16efi_handle_t image_handle;
  17
  18/*
  19 * Setup unit test.
  20 *
  21 * @handle:     handle of the loaded image
  22 * @systable:   system table
  23 */
  24static 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 */
  39static 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 (memcmp(protocol_buffer[i], &loaded_image_protocol_guid,
  64                           sizeof(efi_guid_t)))
  65                        found = true;
  66        }
  67        if (!found) {
  68                efi_st_printf("LoadedImageProtocol not found\n");
  69                return EFI_ST_FAILURE;
  70        }
  71        ret = boottime->free_pool(protocol_buffer);
  72        if (ret != EFI_SUCCESS) {
  73                efi_st_error("FreePool failed\n");
  74                return EFI_ST_FAILURE;
  75        }
  76
  77        /*
  78         * Open the loaded image protocol.
  79         */
  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;
  86        }
  87        if (loaded_image_protocol->revision !=
  88            EFI_LOADED_IMAGE_PROTOCOL_REVISION) {
  89                efi_st_printf("Incorrect revision\n");
  90                return EFI_ST_FAILURE;
  91        }
  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;
  97        }
  98
  99        return EFI_ST_SUCCESS;
 100}
 101
 102EFI_UNIT_TEST(loadedimage) = {
 103        .name = "loaded image",
 104        .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
 105        .setup = setup,
 106        .execute = execute,
 107};
 108