efi_selftest_loaded_image.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include <efi_selftest.h>
  10. static efi_guid_t loaded_image_protocol_guid =
  11. EFI_GUID(0x5b1b31a1, 0x9562, 0x11d2,
  12. 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b);
  13. static struct efi_boot_services *boottime;
  14. efi_handle_t image_handle;
  15. /*
  16. * Setup unit test.
  17. *
  18. * @handle: handle of the loaded image
  19. * @systable: system table
  20. */
  21. static int setup(const efi_handle_t img_handle,
  22. const struct efi_system_table *systable)
  23. {
  24. boottime = systable->boottime;
  25. image_handle = img_handle;
  26. return EFI_ST_SUCCESS;
  27. }
  28. /*
  29. * Execute unit test.
  30. *
  31. * Verify that the loaded image protocol is installed on the image handle.
  32. * Verify that the loaded image protocol points to the system table.
  33. */
  34. static int execute(void)
  35. {
  36. efi_status_t ret;
  37. efi_uintn_t i, protocol_buffer_count = 0;
  38. efi_guid_t **protocol_buffer = NULL;
  39. bool found = false;
  40. struct efi_loaded_image *loaded_image_protocol;
  41. /*
  42. * Get the GUIDs of all protocols installed on the handle.
  43. */
  44. ret = boottime->protocols_per_handle(image_handle, &protocol_buffer,
  45. &protocol_buffer_count);
  46. if (ret != EFI_SUCCESS) {
  47. efi_st_error("ProtocolsPerHandle failed\n");
  48. return EFI_ST_FAILURE;
  49. }
  50. if (!protocol_buffer_count || !protocol_buffer) {
  51. efi_st_error("ProtocolsPerHandle returned no protocol\n");
  52. return EFI_ST_FAILURE;
  53. }
  54. efi_st_printf("%u protocols installed on image handle\n",
  55. (unsigned int)protocol_buffer_count);
  56. for (i = 0; i < protocol_buffer_count; ++i) {
  57. if (memcmp(protocol_buffer[i], &loaded_image_protocol_guid,
  58. sizeof(efi_guid_t)))
  59. found = true;
  60. }
  61. if (!found) {
  62. efi_st_printf("LoadedImageProtocol not found\n");
  63. return EFI_ST_FAILURE;
  64. }
  65. ret = boottime->free_pool(protocol_buffer);
  66. if (ret != EFI_SUCCESS) {
  67. efi_st_error("FreePool failed\n");
  68. return EFI_ST_FAILURE;
  69. }
  70. /*
  71. * Open the loaded image protocol.
  72. */
  73. ret = boottime->open_protocol(image_handle, &loaded_image_protocol_guid,
  74. (void **)&loaded_image_protocol, NULL,
  75. NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  76. if (ret != EFI_SUCCESS) {
  77. efi_st_error("OpenProtocol failed\n");
  78. return EFI_ST_FAILURE;
  79. }
  80. if (loaded_image_protocol->revision !=
  81. EFI_LOADED_IMAGE_PROTOCOL_REVISION) {
  82. efi_st_printf("Incorrect revision\n");
  83. return EFI_ST_FAILURE;
  84. }
  85. if (!loaded_image_protocol->system_table ||
  86. loaded_image_protocol->system_table->hdr.signature !=
  87. EFI_SYSTEM_TABLE_SIGNATURE) {
  88. efi_st_printf("System table reference missing\n");
  89. return EFI_ST_FAILURE;
  90. }
  91. return EFI_ST_SUCCESS;
  92. }
  93. EFI_UNIT_TEST(loadedimage) = {
  94. .name = "loaded image",
  95. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  96. .setup = setup,
  97. .execute = execute,
  98. };