efi_selftest_loaded_image.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 (efi_st_memcmp(protocol_buffer[i],
  58. &loaded_image_protocol_guid,
  59. sizeof(efi_guid_t)))
  60. found = true;
  61. }
  62. if (!found) {
  63. efi_st_printf("LoadedImageProtocol not found\n");
  64. return EFI_ST_FAILURE;
  65. }
  66. ret = boottime->free_pool(protocol_buffer);
  67. if (ret != EFI_SUCCESS) {
  68. efi_st_error("FreePool failed\n");
  69. return EFI_ST_FAILURE;
  70. }
  71. /*
  72. * Open the loaded image protocol.
  73. */
  74. ret = boottime->open_protocol(image_handle, &loaded_image_protocol_guid,
  75. (void **)&loaded_image_protocol, NULL,
  76. NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  77. if (ret != EFI_SUCCESS) {
  78. efi_st_error("OpenProtocol failed\n");
  79. return EFI_ST_FAILURE;
  80. }
  81. if (loaded_image_protocol->revision !=
  82. EFI_LOADED_IMAGE_PROTOCOL_REVISION) {
  83. efi_st_printf("Incorrect revision\n");
  84. return EFI_ST_FAILURE;
  85. }
  86. if (!loaded_image_protocol->system_table ||
  87. loaded_image_protocol->system_table->hdr.signature !=
  88. EFI_SYSTEM_TABLE_SIGNATURE) {
  89. efi_st_printf("System table reference missing\n");
  90. return EFI_ST_FAILURE;
  91. }
  92. return EFI_ST_SUCCESS;
  93. }
  94. EFI_UNIT_TEST(loadedimage) = {
  95. .name = "loaded image",
  96. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  97. .setup = setup,
  98. .execute = execute,
  99. };