efi_selftest_load_file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * efi_selftest_load_file
  4. *
  5. * Copyright (c) 2020 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. *
  7. * This test checks the handling of the LOAD_FILE and the LOAD_FILE2 protocol
  8. * by the LoadImage() service.
  9. */
  10. #include <efi_selftest.h>
  11. /* Include containing the miniapp.efi application */
  12. #include "efi_miniapp_file_image_exit.h"
  13. /* Block size of compressed disk image */
  14. #define COMPRESSED_DISK_IMAGE_BLOCK_SIZE 8
  15. /* Binary logarithm of the block size */
  16. #define LB_BLOCK_SIZE 9
  17. #define GUID_VENDOR \
  18. EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d, \
  19. 0x08, 0x72, 0x81, 0x9c, 0x65, 0xfc, 0xbb, 0xd1)
  20. #define GUID_VENDOR2 \
  21. EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d, \
  22. 0x08, 0x72, 0x81, 0x9c, 0x65, 0xfc, 0xbb, 0xd2)
  23. #define FILE_NAME_SIZE 16
  24. static const efi_guid_t efi_st_guid_load_file_protocol =
  25. EFI_LOAD_FILE_PROTOCOL_GUID;
  26. static const efi_guid_t efi_st_guid_load_file2_protocol =
  27. EFI_LOAD_FILE2_PROTOCOL_GUID;
  28. static const efi_guid_t efi_st_guid_device_path =
  29. EFI_DEVICE_PATH_PROTOCOL_GUID;
  30. static efi_handle_t image_handle;
  31. static struct efi_boot_services *boottime;
  32. static efi_handle_t handle_lf;
  33. static efi_handle_t handle_lf2;
  34. /* One 8 byte block of the compressed disk image */
  35. struct line {
  36. size_t addr;
  37. char *line;
  38. };
  39. /* Compressed file image */
  40. struct compressed_file_image {
  41. size_t length;
  42. struct line lines[];
  43. };
  44. static struct compressed_file_image img = EFI_ST_DISK_IMG;
  45. static int load_file_call_count;
  46. static int load_file2_call_count;
  47. /* Decompressed file image */
  48. static u8 *image;
  49. static struct {
  50. struct efi_device_path_vendor v;
  51. struct efi_device_path d;
  52. } dp_lf_prot = {
  53. {
  54. {
  55. DEVICE_PATH_TYPE_HARDWARE_DEVICE,
  56. DEVICE_PATH_SUB_TYPE_VENDOR,
  57. sizeof(struct efi_device_path_vendor),
  58. },
  59. GUID_VENDOR,
  60. },
  61. {
  62. DEVICE_PATH_TYPE_END,
  63. DEVICE_PATH_SUB_TYPE_END,
  64. sizeof(struct efi_device_path),
  65. },
  66. };
  67. static struct {
  68. struct efi_device_path_vendor v;
  69. struct efi_device_path_file_path f;
  70. u16 file_name[FILE_NAME_SIZE];
  71. struct efi_device_path e;
  72. } dp_lf_file = {
  73. {
  74. {
  75. DEVICE_PATH_TYPE_HARDWARE_DEVICE,
  76. DEVICE_PATH_SUB_TYPE_VENDOR,
  77. sizeof(struct efi_device_path_vendor),
  78. },
  79. GUID_VENDOR,
  80. },
  81. {
  82. {
  83. DEVICE_PATH_TYPE_MEDIA_DEVICE,
  84. DEVICE_PATH_SUB_TYPE_FILE_PATH,
  85. sizeof(struct efi_device_path_file_path) +
  86. FILE_NAME_SIZE * sizeof(u16),
  87. }
  88. },
  89. u"\\lf.efi",
  90. {
  91. DEVICE_PATH_TYPE_END,
  92. DEVICE_PATH_SUB_TYPE_END,
  93. sizeof(struct efi_device_path),
  94. },
  95. };
  96. struct efi_device_path *dp_lf_file_remainder = &dp_lf_file.f.dp;
  97. static struct {
  98. struct efi_device_path_vendor v;
  99. struct efi_device_path d;
  100. } dp_lf2_prot = {
  101. {
  102. {
  103. DEVICE_PATH_TYPE_HARDWARE_DEVICE,
  104. DEVICE_PATH_SUB_TYPE_VENDOR,
  105. sizeof(struct efi_device_path_vendor),
  106. },
  107. GUID_VENDOR2,
  108. },
  109. {
  110. DEVICE_PATH_TYPE_END,
  111. DEVICE_PATH_SUB_TYPE_END,
  112. sizeof(struct efi_device_path),
  113. },
  114. };
  115. static struct {
  116. struct efi_device_path_vendor v;
  117. struct efi_device_path_file_path f;
  118. u16 file_name[FILE_NAME_SIZE];
  119. struct efi_device_path e;
  120. } dp_lf2_file = {
  121. {
  122. {
  123. DEVICE_PATH_TYPE_HARDWARE_DEVICE,
  124. DEVICE_PATH_SUB_TYPE_VENDOR,
  125. sizeof(struct efi_device_path_vendor),
  126. },
  127. GUID_VENDOR2,
  128. },
  129. {
  130. {
  131. DEVICE_PATH_TYPE_MEDIA_DEVICE,
  132. DEVICE_PATH_SUB_TYPE_FILE_PATH,
  133. sizeof(struct efi_device_path_file_path) +
  134. FILE_NAME_SIZE * sizeof(u16),
  135. }
  136. },
  137. u"\\lf2.efi",
  138. {
  139. DEVICE_PATH_TYPE_END,
  140. DEVICE_PATH_SUB_TYPE_END,
  141. sizeof(struct efi_device_path),
  142. },
  143. };
  144. struct efi_device_path *dp_lf2_file_remainder = &dp_lf2_file.f.dp;
  145. /*
  146. * Decompress the disk image.
  147. *
  148. * @image decompressed disk image
  149. * Return: status code
  150. */
  151. static efi_status_t decompress(u8 **image)
  152. {
  153. u8 *buf;
  154. size_t i;
  155. size_t addr;
  156. size_t len;
  157. efi_status_t ret;
  158. ret = boottime->allocate_pool(EFI_LOADER_DATA, img.length,
  159. (void **)&buf);
  160. if (ret != EFI_SUCCESS) {
  161. efi_st_error("Out of memory\n");
  162. return ret;
  163. }
  164. boottime->set_mem(buf, img.length, 0);
  165. for (i = 0; ; ++i) {
  166. if (!img.lines[i].line)
  167. break;
  168. addr = img.lines[i].addr;
  169. len = COMPRESSED_DISK_IMAGE_BLOCK_SIZE;
  170. if (addr + len > img.length)
  171. len = img.length - addr;
  172. boottime->copy_mem(buf + addr, img.lines[i].line, len);
  173. }
  174. *image = buf;
  175. return ret;
  176. }
  177. /*
  178. * load_file() - LoadFile() service of a EFI_LOAD_FILE_PROTOCOL
  179. *
  180. * @this: instance of EFI_LOAD_FILE_PROTOCOL
  181. * @file_path: remaining device path
  182. * @boot_policy: true if called by boot manager
  183. * @buffer_size: (required) buffer size
  184. * @buffer: buffer to which the file is to be loaded
  185. */
  186. static efi_status_t EFIAPI load_file(struct efi_load_file_protocol *this,
  187. struct efi_device_path *file_path,
  188. bool boot_policy,
  189. efi_uintn_t *buffer_size,
  190. void *buffer)
  191. {
  192. ++load_file_call_count;
  193. if (memcmp(file_path, dp_lf_file_remainder,
  194. sizeof(struct efi_device_path_file_path) +
  195. FILE_NAME_SIZE * sizeof(u16) +
  196. sizeof(struct efi_device_path))) {
  197. efi_st_error("Wrong remaining device path\n");
  198. return EFI_NOT_FOUND;
  199. }
  200. if (this->load_file != load_file) {
  201. efi_st_error("wrong this\n");
  202. return EFI_INVALID_PARAMETER;
  203. }
  204. if (*buffer_size < img.length) {
  205. *buffer_size = img.length;
  206. return EFI_BUFFER_TOO_SMALL;
  207. }
  208. memcpy(buffer, image, img.length);
  209. *buffer_size = img.length;
  210. return EFI_SUCCESS;
  211. }
  212. /*
  213. * load_file2() - LoadFile() service of a EFI_LOAD_FILE2_PROTOCOL
  214. *
  215. * @this: instance of EFI_LOAD_FILE2_PROTOCOL
  216. * @file_path: remaining device path
  217. * @boot_policy: true if called by boot manager
  218. * @buffer_size: (required) buffer size
  219. * @buffer: buffer to which the file is to be loaded
  220. */
  221. static efi_status_t EFIAPI load_file2(struct efi_load_file_protocol *this,
  222. struct efi_device_path *file_path,
  223. bool boot_policy,
  224. efi_uintn_t *buffer_size,
  225. void *buffer)
  226. {
  227. ++load_file2_call_count;
  228. if (memcmp(file_path, dp_lf2_file_remainder,
  229. sizeof(struct efi_device_path_file_path) +
  230. FILE_NAME_SIZE * sizeof(u16) +
  231. sizeof(struct efi_device_path))) {
  232. efi_st_error("Wrong remaining device path\n");
  233. return EFI_NOT_FOUND;
  234. }
  235. if (this->load_file != load_file2) {
  236. efi_st_error("wrong this\n");
  237. return EFI_INVALID_PARAMETER;
  238. }
  239. if (boot_policy) {
  240. efi_st_error("LOAD_FILE2 called with boot_policy = true");
  241. return EFI_INVALID_PARAMETER;
  242. }
  243. if (*buffer_size < img.length) {
  244. *buffer_size = img.length;
  245. return EFI_BUFFER_TOO_SMALL;
  246. }
  247. memcpy(buffer, image, img.length);
  248. *buffer_size = img.length;
  249. return EFI_SUCCESS;
  250. }
  251. static struct efi_load_file_protocol lf_prot = {load_file};
  252. static struct efi_load_file_protocol lf2_prot = {load_file2};
  253. /*
  254. * Setup unit test.
  255. *
  256. * Install an EFI_LOAD_FILE_PROTOCOL and an EFI_LOAD_FILE2_PROTOCOL.
  257. *
  258. * @handle: handle of the loaded image
  259. * @systable: system table
  260. * Return: EFI_ST_SUCCESS for success
  261. */
  262. static int efi_st_load_file_setup(const efi_handle_t handle,
  263. const struct efi_system_table *systable)
  264. {
  265. efi_status_t ret;
  266. image_handle = handle;
  267. boottime = systable->boottime;
  268. /* Load the application image into memory */
  269. decompress(&image);
  270. ret = boottime->install_multiple_protocol_interfaces(
  271. &handle_lf,
  272. &efi_st_guid_device_path,
  273. &dp_lf_prot,
  274. &efi_st_guid_load_file_protocol,
  275. &lf_prot,
  276. NULL);
  277. if (ret != EFI_SUCCESS) {
  278. efi_st_error("InstallMultipleProtocolInterfaces failed\n");
  279. return EFI_ST_FAILURE;
  280. }
  281. ret = boottime->install_multiple_protocol_interfaces(
  282. &handle_lf2,
  283. &efi_st_guid_device_path,
  284. &dp_lf2_prot,
  285. &efi_st_guid_load_file2_protocol,
  286. &lf2_prot,
  287. NULL);
  288. if (ret != EFI_SUCCESS) {
  289. efi_st_error("InstallMultipleProtocolInterfaces failed\n");
  290. return EFI_ST_FAILURE;
  291. }
  292. return EFI_ST_SUCCESS;
  293. }
  294. /*
  295. * Tear down unit test.
  296. *
  297. * Return: EFI_ST_SUCCESS for success
  298. */
  299. static int efi_st_load_file_teardown(void)
  300. {
  301. efi_status_t ret = EFI_ST_SUCCESS;
  302. if (handle_lf) {
  303. ret = boottime->uninstall_multiple_protocol_interfaces(
  304. handle_lf,
  305. &efi_st_guid_device_path,
  306. &dp_lf_prot,
  307. &efi_st_guid_load_file_protocol,
  308. &lf_prot,
  309. NULL);
  310. if (ret != EFI_SUCCESS) {
  311. efi_st_error(
  312. "UninstallMultipleProtocolInterfaces failed\n");
  313. return EFI_ST_FAILURE;
  314. }
  315. }
  316. if (handle_lf2) {
  317. ret = boottime->uninstall_multiple_protocol_interfaces(
  318. handle_lf2,
  319. &efi_st_guid_device_path,
  320. &dp_lf2_prot,
  321. &efi_st_guid_load_file2_protocol,
  322. &lf2_prot,
  323. NULL);
  324. if (ret != EFI_SUCCESS) {
  325. efi_st_error(
  326. "UninstallMultipleProtocolInterfaces failed\n");
  327. return EFI_ST_FAILURE;
  328. }
  329. }
  330. if (image) {
  331. ret = boottime->free_pool(image);
  332. if (ret != EFI_SUCCESS) {
  333. efi_st_error("Failed to free image\n");
  334. return EFI_ST_FAILURE;
  335. }
  336. }
  337. return ret;
  338. }
  339. /*
  340. * Execute unit test.
  341. *
  342. * Try loading an image via the EFI_LOAD_FILE_PROTOCOL and the
  343. * EFI_LOAD_FILE2_PROTOCOL. Finally execute the image.
  344. *
  345. * Return: EFI_ST_SUCCESS for success
  346. */
  347. static int efi_st_load_file_execute(void)
  348. {
  349. efi_status_t ret;
  350. efi_handle_t handle;
  351. efi_uintn_t exit_data_size = 0;
  352. u16 *exit_data = NULL;
  353. u16 expected_text[] = EFI_ST_SUCCESS_STR;
  354. load_file_call_count = 0;
  355. load_file2_call_count = 0;
  356. handle = NULL;
  357. ret = boottime->load_image(true, image_handle, &dp_lf_file.v.dp, NULL,
  358. 0, &handle);
  359. if (ret != EFI_SUCCESS) {
  360. efi_st_error("Failed to load image\n");
  361. return EFI_ST_FAILURE;
  362. }
  363. if (load_file2_call_count || !load_file_call_count) {
  364. efi_st_error("Wrong image loaded\n");
  365. return EFI_ST_FAILURE;
  366. }
  367. ret = boottime->unload_image(handle);
  368. if (ret != EFI_SUCCESS) {
  369. efi_st_error("Failed to unload image\n");
  370. return EFI_ST_FAILURE;
  371. }
  372. load_file_call_count = 0;
  373. load_file2_call_count = 0;
  374. handle = NULL;
  375. ret = boottime->load_image(false, image_handle, &dp_lf_file.v.dp, NULL,
  376. 0, &handle);
  377. if (ret != EFI_SUCCESS) {
  378. efi_st_error("Failed to load image\n");
  379. return EFI_ST_FAILURE;
  380. }
  381. if (load_file2_call_count || !load_file_call_count) {
  382. efi_st_error("Wrong image loaded\n");
  383. return EFI_ST_FAILURE;
  384. }
  385. ret = boottime->unload_image(handle);
  386. if (ret != EFI_SUCCESS) {
  387. efi_st_error("Failed to unload image\n");
  388. return EFI_ST_FAILURE;
  389. }
  390. ret = boottime->load_image(true, image_handle, &dp_lf2_file.v.dp, NULL,
  391. 0, &handle);
  392. if (ret != EFI_NOT_FOUND) {
  393. efi_st_error(
  394. "Boot manager should not use LOAD_FILE2_PROTOCOL\n");
  395. return EFI_ST_FAILURE;
  396. }
  397. load_file_call_count = 0;
  398. load_file2_call_count = 0;
  399. handle = NULL;
  400. ret = boottime->load_image(false, image_handle, &dp_lf2_file.v.dp, NULL,
  401. 0, &handle);
  402. if (ret != EFI_SUCCESS) {
  403. efi_st_error("Failed to load image\n");
  404. return EFI_ST_FAILURE;
  405. }
  406. if (!load_file2_call_count || load_file_call_count) {
  407. efi_st_error("Wrong image loaded\n");
  408. return EFI_ST_FAILURE;
  409. }
  410. ret = boottime->start_image(handle, &exit_data_size, &exit_data);
  411. if (ret != EFI_UNSUPPORTED) {
  412. efi_st_error("Wrong return value from application\n");
  413. return EFI_ST_FAILURE;
  414. }
  415. if (!exit_data || exit_data_size != sizeof(expected_text) ||
  416. memcmp(exit_data, expected_text, sizeof(expected_text))) {
  417. efi_st_error("Incorrect exit data\n");
  418. return EFI_ST_FAILURE;
  419. }
  420. ret = boottime->free_pool(exit_data);
  421. if (ret != EFI_SUCCESS) {
  422. efi_st_error("Failed to free exit data\n");
  423. return EFI_ST_FAILURE;
  424. }
  425. return EFI_ST_SUCCESS;
  426. }
  427. EFI_UNIT_TEST(load_file_protocol) = {
  428. .name = "load file protocol",
  429. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  430. .setup = efi_st_load_file_setup,
  431. .execute = efi_st_load_file_execute,
  432. .teardown = efi_st_load_file_teardown,
  433. };