efi_selftest_exception.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * efi_selftest_exception
  4. *
  5. * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. *
  7. * This test checks the handling of exceptions.
  8. *
  9. * The efi_selftest_miniapp_exception.efi application is loaded into memory
  10. * and started.
  11. */
  12. #include <efi_selftest.h>
  13. /* Include containing the UEFI application */
  14. #include "efi_miniapp_file_image_exception.h"
  15. /* Block size of compressed disk image */
  16. #define COMPRESSED_DISK_IMAGE_BLOCK_SIZE 8
  17. /* Binary logarithm of the block size */
  18. #define LB_BLOCK_SIZE 9
  19. /* File device path for LoadImage() */
  20. static struct {
  21. struct efi_device_path dp;
  22. u16 filename[8];
  23. struct efi_device_path end;
  24. } dp = {
  25. {
  26. DEVICE_PATH_TYPE_MEDIA_DEVICE,
  27. DEVICE_PATH_SUB_TYPE_FILE_PATH,
  28. sizeof(dp.dp) + sizeof(dp.filename),
  29. },
  30. u"bug.efi",
  31. {
  32. DEVICE_PATH_TYPE_END,
  33. DEVICE_PATH_SUB_TYPE_END,
  34. sizeof(dp.end),
  35. }
  36. };
  37. static efi_handle_t image_handle;
  38. static struct efi_boot_services *boottime;
  39. /* One 8 byte block of the compressed disk image */
  40. struct line {
  41. size_t addr;
  42. char *line;
  43. };
  44. /* Compressed file image */
  45. struct compressed_file_image {
  46. size_t length;
  47. struct line lines[];
  48. };
  49. static struct compressed_file_image img = EFI_ST_DISK_IMG;
  50. /* Decompressed file image */
  51. static u8 *image;
  52. /*
  53. * Decompress the disk image.
  54. *
  55. * @image decompressed disk image
  56. * Return: status code
  57. */
  58. static efi_status_t decompress(u8 **image)
  59. {
  60. u8 *buf;
  61. size_t i;
  62. size_t addr;
  63. size_t len;
  64. efi_status_t ret;
  65. ret = boottime->allocate_pool(EFI_LOADER_DATA, img.length,
  66. (void **)&buf);
  67. if (ret != EFI_SUCCESS) {
  68. efi_st_error("Out of memory\n");
  69. return ret;
  70. }
  71. boottime->set_mem(buf, img.length, 0);
  72. for (i = 0; ; ++i) {
  73. if (!img.lines[i].line)
  74. break;
  75. addr = img.lines[i].addr;
  76. len = COMPRESSED_DISK_IMAGE_BLOCK_SIZE;
  77. if (addr + len > img.length)
  78. len = img.length - addr;
  79. boottime->copy_mem(buf + addr, img.lines[i].line, len);
  80. }
  81. *image = buf;
  82. return ret;
  83. }
  84. /*
  85. * Setup unit test.
  86. *
  87. * @handle: handle of the loaded image
  88. * @systable: system table
  89. * Return: EFI_ST_SUCCESS for success
  90. */
  91. static int setup(const efi_handle_t handle,
  92. const struct efi_system_table *systable)
  93. {
  94. image_handle = handle;
  95. boottime = systable->boottime;
  96. /* Load the application image into memory */
  97. decompress(&image);
  98. return EFI_ST_SUCCESS;
  99. }
  100. /*
  101. * Execute unit test.
  102. *
  103. * Load and start the application image.
  104. *
  105. * Return: EFI_ST_SUCCESS for success
  106. */
  107. static int execute(void)
  108. {
  109. efi_status_t ret;
  110. efi_handle_t handle;
  111. ret = boottime->load_image(false, image_handle, &dp.dp, image,
  112. img.length, &handle);
  113. if (ret != EFI_SUCCESS) {
  114. efi_st_error("Failed to load image\n");
  115. return EFI_ST_FAILURE;
  116. }
  117. ret = boottime->start_image(handle, NULL, NULL);
  118. efi_st_error("Exception not triggered\n");
  119. return EFI_ST_FAILURE;
  120. }
  121. EFI_UNIT_TEST(exception) = {
  122. .name = "exception",
  123. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  124. .setup = setup,
  125. .execute = execute,
  126. .on_request = true,
  127. };