efi_selftest_miniapp_exit.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * efi_selftest_miniapp_exit
  4. *
  5. * Copyright (c) 2018 Heinrich Schuchardt
  6. *
  7. * This EFI application is run by the StartImage selftest.
  8. * It uses the Exit boot service to return.
  9. */
  10. #include <common.h>
  11. #include <efi_selftest.h>
  12. static efi_guid_t loaded_image_protocol_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
  13. /**
  14. * check_loaded_image_protocol() - check image_base/image_size
  15. *
  16. * Try to open the loaded image protocol. Check that this function is located
  17. * between image_base and image_base + image_size.
  18. *
  19. * @image_handle: handle of the loaded image
  20. * @systable: system table
  21. * @return: status code
  22. */
  23. static efi_status_t EFIAPI check_loaded_image_protocol
  24. (efi_handle_t image_handle, struct efi_system_table *systable)
  25. {
  26. struct efi_simple_text_output_protocol *cout = systable->con_out;
  27. struct efi_boot_services *boottime = systable->boottime;
  28. struct efi_loaded_image *loaded_image_protocol;
  29. efi_status_t ret;
  30. /*
  31. * Open the loaded image protocol.
  32. */
  33. ret = boottime->open_protocol
  34. (image_handle, &loaded_image_protocol_guid,
  35. (void **)&loaded_image_protocol, NULL,
  36. NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  37. if (ret != EFI_SUCCESS) {
  38. cout->output_string(cout,
  39. L"Could not open loaded image protocol");
  40. return ret;
  41. }
  42. if ((void *)check_loaded_image_protocol <
  43. loaded_image_protocol->image_base ||
  44. (void *)check_loaded_image_protocol >=
  45. loaded_image_protocol->image_base +
  46. loaded_image_protocol->image_size) {
  47. cout->output_string(cout,
  48. L"Incorrect image_base or image_size\n");
  49. return EFI_NOT_FOUND;
  50. }
  51. return EFI_SUCCESS;
  52. }
  53. /**
  54. * Entry point of the EFI application.
  55. *
  56. * @handle: handle of the loaded image
  57. * @systable: system table
  58. * @return: status code
  59. */
  60. efi_status_t EFIAPI efi_main(efi_handle_t handle,
  61. struct efi_system_table *systable)
  62. {
  63. struct efi_simple_text_output_protocol *con_out = systable->con_out;
  64. efi_status_t ret;
  65. u16 text[] = EFI_ST_SUCCESS_STR;
  66. con_out->output_string(con_out, L"EFI application calling Exit\n");
  67. if (check_loaded_image_protocol(handle, systable) != EFI_SUCCESS) {
  68. con_out->output_string(con_out,
  69. L"Loaded image protocol missing\n");
  70. ret = EFI_NOT_FOUND;
  71. goto out;
  72. }
  73. /* This return value is expected by the calling test */
  74. ret = EFI_UNSUPPORTED;
  75. out:
  76. systable->boottime->exit(handle, ret, sizeof(text), text);
  77. /*
  78. * This statement should not be reached.
  79. * To enable testing use a different return value.
  80. */
  81. return EFI_SUCCESS;
  82. }