efi_selftest_loadimage.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * efi_selftest_loadimage
  4. *
  5. * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. *
  7. * This test checks the LoadImage and StartImage boot service.
  8. *
  9. * The efi_selftest_miniapp_exit.efi application is loaded via a file device
  10. * path and started.
  11. */
  12. #include <efi_selftest.h>
  13. /* Include containing the efi_selftest_miniapp_exit.efi application */
  14. #include "efi_miniapp_file_image_exit.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. #define FILE_NAME u"app.efi"
  20. #define VOLUME_NAME u"EfiDisk"
  21. static struct efi_boot_services *boottime;
  22. static efi_handle_t handle_image;
  23. static efi_handle_t handle_volume;
  24. static const efi_guid_t guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID;
  25. static const efi_guid_t guid_simple_file_system_protocol =
  26. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
  27. static const efi_guid_t guid_file_info = EFI_FILE_INFO_GUID;
  28. static const efi_guid_t guid_file_system_info = EFI_FILE_SYSTEM_INFO_GUID;
  29. /* One 8 byte block of the compressed disk image */
  30. struct line {
  31. size_t addr;
  32. char *line;
  33. };
  34. /* Compressed file image */
  35. struct compressed_file_image {
  36. size_t length;
  37. struct line lines[];
  38. };
  39. /* File info including file name */
  40. struct file_info {
  41. struct efi_file_info info;
  42. u16 file_name[sizeof(FILE_NAME)];
  43. };
  44. /* File system info including volume name */
  45. struct file_system_info {
  46. struct efi_file_system_info info;
  47. u16 file_name[sizeof(VOLUME_NAME)];
  48. };
  49. /* Compressed file image */
  50. static struct compressed_file_image img = EFI_ST_DISK_IMG;
  51. /* Pointer to decompressed file image */
  52. static u8 *image;
  53. /* File info */
  54. static struct file_info priv_file_info = {
  55. {
  56. .size = sizeof(struct file_info),
  57. .attribute = EFI_FILE_READ_ONLY,
  58. },
  59. FILE_NAME,
  60. };
  61. /* Pointer to file info */
  62. struct efi_file_info *file_info = &priv_file_info.info;
  63. /* Volume device path */
  64. static struct {
  65. struct efi_device_path_vendor vendor;
  66. struct efi_device_path end;
  67. } __packed dp_volume = {
  68. .vendor = {
  69. .dp = {
  70. .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
  71. .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
  72. .length = sizeof(struct efi_device_path_vendor),
  73. },
  74. .guid = EFI_GUID(0x4f9a0ebf, 0xa179, 0x88a6, 0x25, 0x68,
  75. 0x10, 0x72, 0xb1, 0x93, 0x51, 0x71),
  76. },
  77. .end = {
  78. .type = DEVICE_PATH_TYPE_END,
  79. .sub_type = DEVICE_PATH_SUB_TYPE_END,
  80. .length = sizeof(struct efi_device_path),
  81. }
  82. };
  83. /* File device path */
  84. static struct {
  85. struct efi_device_path_vendor vendor;
  86. struct efi_device_path path;
  87. u16 file[sizeof(FILE_NAME)];
  88. struct efi_device_path end;
  89. } __packed dp_file = {
  90. .vendor = {
  91. .dp = {
  92. .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
  93. .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
  94. .length = sizeof(struct efi_device_path_vendor),
  95. },
  96. .guid = EFI_GUID(0x4f9a0ebf, 0xa179, 0x88a6, 0x25, 0x68,
  97. 0x10, 0x72, 0xb1, 0x93, 0x51, 0x71),
  98. },
  99. .path = {
  100. .type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
  101. .sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
  102. .length = sizeof(struct efi_device_path) + sizeof(dp_file.file),
  103. },
  104. .file = FILE_NAME,
  105. .end = {
  106. .type = DEVICE_PATH_TYPE_END,
  107. .sub_type = DEVICE_PATH_SUB_TYPE_END,
  108. .length = sizeof(struct efi_device_path),
  109. }
  110. };
  111. /* File system info */
  112. static struct file_system_info priv_file_system_info = {
  113. {
  114. .size = sizeof(struct file_system_info),
  115. .read_only = true,
  116. .volume_size = 0x100000,
  117. .free_space = 0x0,
  118. .block_size = 0x200,
  119. },
  120. VOLUME_NAME
  121. };
  122. /* Pointer to file system info */
  123. static struct efi_file_system_info *file_system_info =
  124. &priv_file_system_info.info;
  125. /* Forward definitions of file and file system functions */
  126. static efi_status_t EFIAPI efi_st_open_volume
  127. (struct efi_simple_file_system_protocol *this,
  128. struct efi_file_handle **root);
  129. static efi_status_t EFIAPI efi_st_open
  130. (struct efi_file_handle *this,
  131. struct efi_file_handle **new_handle,
  132. u16 *file_name, u64 open_mode, u64 attributes);
  133. static efi_status_t EFIAPI efi_st_close(struct efi_file_handle *this);
  134. static efi_status_t EFIAPI efi_st_delete(struct efi_file_handle *this);
  135. static efi_status_t EFIAPI efi_st_read
  136. (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer);
  137. static efi_status_t EFIAPI efi_st_write
  138. (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer);
  139. static efi_status_t EFIAPI efi_st_getpos(struct efi_file_handle *this,
  140. u64 *pos);
  141. static efi_status_t EFIAPI efi_st_setpos(struct efi_file_handle *this, u64 pos);
  142. static efi_status_t EFIAPI efi_st_getinfo
  143. (struct efi_file_handle *this, const efi_guid_t *info_type,
  144. efi_uintn_t *buffer_size, void *buffer);
  145. static efi_status_t EFIAPI efi_st_setinfo
  146. (struct efi_file_handle *this, const efi_guid_t *info_type,
  147. efi_uintn_t buffer_size, void *buffer);
  148. static efi_status_t EFIAPI efi_st_flush(struct efi_file_handle *this);
  149. /* Internal information about status of file system */
  150. static struct {
  151. /* Difference of volume open count minus volume close count */
  152. int volume_open_count;
  153. /* Difference of file open count minus file close count */
  154. int file_open_count;
  155. /* File size */
  156. u64 file_size;
  157. /* Current position in file */
  158. u64 file_pos;
  159. } priv;
  160. /* EFI_FILE_PROTOCOL for file */
  161. static struct efi_file_handle file = {
  162. .rev = 0x00010000,
  163. .open = efi_st_open,
  164. .close = efi_st_close,
  165. .delete = efi_st_delete,
  166. .read = efi_st_read,
  167. .write = efi_st_write,
  168. .getpos = efi_st_getpos,
  169. .setpos = efi_st_setpos,
  170. .getinfo = efi_st_getinfo,
  171. .setinfo = efi_st_setinfo,
  172. .flush = efi_st_flush,
  173. };
  174. /* EFI_FILE_PROTOCOL for root directory */
  175. static struct efi_file_handle volume = {
  176. .rev = 0x00010000,
  177. .open = efi_st_open,
  178. .close = efi_st_close,
  179. .delete = efi_st_delete,
  180. .read = efi_st_read,
  181. .write = efi_st_write,
  182. .getpos = efi_st_getpos,
  183. .setpos = efi_st_setpos,
  184. .getinfo = efi_st_getinfo,
  185. .setinfo = efi_st_setinfo,
  186. .flush = efi_st_flush,
  187. };
  188. /* EFI_SIMPLE_FILE_SYSTEM_PROTOCOL of the block device */
  189. struct efi_simple_file_system_protocol file_system = {
  190. .rev = 0x00010000,
  191. .open_volume = efi_st_open_volume,
  192. };
  193. static efi_status_t EFIAPI efi_st_open_volume
  194. (struct efi_simple_file_system_protocol *this,
  195. struct efi_file_handle **root)
  196. {
  197. if (this != &file_system || !root)
  198. return EFI_INVALID_PARAMETER;
  199. *root = &volume;
  200. priv.volume_open_count++;
  201. return EFI_SUCCESS;
  202. }
  203. static efi_status_t EFIAPI efi_st_open
  204. (struct efi_file_handle *this,
  205. struct efi_file_handle **new_handle,
  206. u16 *file_name, u64 open_mode, u64 attributes)
  207. {
  208. if (this != &volume)
  209. return EFI_INVALID_PARAMETER;
  210. *new_handle = &file;
  211. priv.file_pos = 0;
  212. priv.file_open_count++;
  213. return EFI_SUCCESS;
  214. }
  215. static efi_status_t EFIAPI efi_st_close(struct efi_file_handle *this)
  216. {
  217. if (this == &file)
  218. priv.file_open_count--;
  219. else if (this == &volume)
  220. priv.volume_open_count--;
  221. else
  222. return EFI_INVALID_PARAMETER;
  223. return EFI_SUCCESS;
  224. }
  225. static efi_status_t EFIAPI efi_st_delete(struct efi_file_handle *this)
  226. {
  227. if (this != &file)
  228. return EFI_INVALID_PARAMETER;
  229. return EFI_UNSUPPORTED;
  230. }
  231. static efi_status_t EFIAPI efi_st_read
  232. (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer)
  233. {
  234. if (this != &file)
  235. return EFI_INVALID_PARAMETER;
  236. if (priv.file_pos >= img.length)
  237. *buffer_size = 0;
  238. else if (priv.file_pos + *buffer_size > img.length)
  239. *buffer_size = img.length - priv.file_pos;
  240. boottime->copy_mem(buffer, &image[priv.file_pos], *buffer_size);
  241. priv.file_pos += *buffer_size;
  242. return EFI_SUCCESS;
  243. }
  244. static efi_status_t EFIAPI efi_st_write
  245. (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer)
  246. {
  247. if (this != &file)
  248. return EFI_INVALID_PARAMETER;
  249. return EFI_UNSUPPORTED;
  250. }
  251. static efi_status_t EFIAPI efi_st_getpos(struct efi_file_handle *this, u64 *pos)
  252. {
  253. if (this != &file)
  254. return EFI_INVALID_PARAMETER;
  255. *pos = priv.file_pos;
  256. return EFI_SUCCESS;
  257. }
  258. static efi_status_t EFIAPI efi_st_setpos(struct efi_file_handle *this, u64 pos)
  259. {
  260. if (this != &file)
  261. return EFI_INVALID_PARAMETER;
  262. priv.file_pos = pos;
  263. return EFI_SUCCESS;
  264. }
  265. static efi_status_t EFIAPI efi_st_getinfo
  266. (struct efi_file_handle *this, const efi_guid_t *info_type,
  267. efi_uintn_t *buffer_size, void *buffer)
  268. {
  269. if (this == &file) {
  270. if (memcmp(info_type, &guid_file_info, sizeof(efi_guid_t)))
  271. return EFI_INVALID_PARAMETER;
  272. if (*buffer_size >= sizeof(struct file_info)) {
  273. boottime->copy_mem(buffer, file_info,
  274. sizeof(struct file_info));
  275. } else {
  276. *buffer_size = sizeof(struct file_info);
  277. return EFI_BUFFER_TOO_SMALL;
  278. }
  279. } else if (this == &volume) {
  280. if (memcmp(info_type, &guid_file_system_info,
  281. sizeof(efi_guid_t)))
  282. return EFI_INVALID_PARAMETER;
  283. if (*buffer_size >= sizeof(struct file_system_info)) {
  284. boottime->copy_mem(buffer, file_system_info,
  285. sizeof(struct file_system_info));
  286. } else {
  287. *buffer_size = sizeof(struct file_system_info);
  288. return EFI_BUFFER_TOO_SMALL;
  289. }
  290. } else {
  291. return EFI_INVALID_PARAMETER;
  292. }
  293. return EFI_SUCCESS;
  294. }
  295. static efi_status_t EFIAPI efi_st_setinfo
  296. (struct efi_file_handle *this, const efi_guid_t *info_type,
  297. efi_uintn_t buffer_size, void *buffer)
  298. {
  299. if (this != &file)
  300. return EFI_INVALID_PARAMETER;
  301. return EFI_UNSUPPORTED;
  302. }
  303. static efi_status_t EFIAPI efi_st_flush(struct efi_file_handle *this)
  304. {
  305. if (this != &file)
  306. return EFI_INVALID_PARAMETER;
  307. return EFI_UNSUPPORTED;
  308. }
  309. /*
  310. * Decompress the disk image.
  311. *
  312. * @image decompressed disk image
  313. * Return: status code
  314. */
  315. static efi_status_t decompress(u8 **image)
  316. {
  317. u8 *buf;
  318. size_t i;
  319. size_t addr;
  320. size_t len;
  321. efi_status_t ret;
  322. ret = boottime->allocate_pool(EFI_LOADER_DATA, img.length,
  323. (void **)&buf);
  324. if (ret != EFI_SUCCESS) {
  325. efi_st_error("Out of memory\n");
  326. return ret;
  327. }
  328. boottime->set_mem(buf, img.length, 0);
  329. for (i = 0; ; ++i) {
  330. if (!img.lines[i].line)
  331. break;
  332. addr = img.lines[i].addr;
  333. len = COMPRESSED_DISK_IMAGE_BLOCK_SIZE;
  334. if (addr + len > img.length)
  335. len = img.length - addr;
  336. boottime->copy_mem(buf + addr, img.lines[i].line, len);
  337. }
  338. *image = buf;
  339. priv.file_size = img.length;
  340. file_info->file_size = img.length;
  341. return ret;
  342. }
  343. /*
  344. * Setup unit test.
  345. *
  346. * Decompress application image and provide a handle for the in memory block
  347. * device.
  348. *
  349. * @handle: handle of the loaded image
  350. * @systable: system table
  351. * Return: EFI_ST_SUCCESS for success
  352. */
  353. static int setup(const efi_handle_t handle,
  354. const struct efi_system_table *systable)
  355. {
  356. efi_status_t ret;
  357. handle_image = handle;
  358. boottime = systable->boottime;
  359. /* Load the application image into memory */
  360. decompress(&image);
  361. ret = boottime->install_protocol_interface
  362. (&handle_volume, &guid_device_path, EFI_NATIVE_INTERFACE,
  363. &dp_volume);
  364. if (ret != EFI_SUCCESS) {
  365. efi_st_error("Failed to install device path\n");
  366. return EFI_ST_FAILURE;
  367. }
  368. ret = boottime->install_protocol_interface
  369. (&handle_volume, &guid_simple_file_system_protocol,
  370. EFI_NATIVE_INTERFACE, &file_system);
  371. if (ret != EFI_SUCCESS) {
  372. efi_st_error("Failed to install simple file system protocol\n");
  373. return EFI_ST_FAILURE;
  374. }
  375. return EFI_ST_SUCCESS;
  376. }
  377. /*
  378. * Tear down unit test.
  379. *
  380. * Uninstall protocols and free memory.
  381. *
  382. * Return: EFI_ST_SUCCESS for success
  383. */
  384. static int teardown(void)
  385. {
  386. efi_status_t ret = EFI_ST_SUCCESS;
  387. if (handle_volume) {
  388. ret = boottime->uninstall_protocol_interface
  389. (handle_volume, &guid_simple_file_system_protocol,
  390. &file_system);
  391. if (ret != EFI_SUCCESS) {
  392. efi_st_error
  393. ("Failed to uninstall simple file system protocol\n");
  394. return EFI_ST_FAILURE;
  395. }
  396. ret = boottime->uninstall_protocol_interface
  397. (handle_volume, &guid_device_path, &dp_volume);
  398. if (ret != EFI_SUCCESS) {
  399. efi_st_error
  400. ("Failed to uninstall device path protocol\n");
  401. return EFI_ST_FAILURE;
  402. }
  403. }
  404. if (image) {
  405. ret = boottime->free_pool(image);
  406. if (ret != EFI_SUCCESS) {
  407. efi_st_error("Failed to free image\n");
  408. return EFI_ST_FAILURE;
  409. }
  410. }
  411. return ret;
  412. }
  413. /*
  414. * Execute unit test.
  415. *
  416. * Load and start the application image.
  417. *
  418. * Return: EFI_ST_SUCCESS for success
  419. */
  420. static int execute(void)
  421. {
  422. efi_status_t ret;
  423. efi_handle_t handle;
  424. ret = boottime->load_image(false, handle_image, &dp_file.vendor.dp,
  425. NULL, 0, &handle);
  426. if (ret != EFI_SUCCESS) {
  427. efi_st_error("Failed to load image\n");
  428. return EFI_ST_FAILURE;
  429. }
  430. ret = boottime->start_image(handle, NULL, NULL);
  431. if (ret != EFI_UNSUPPORTED) {
  432. efi_st_error("Wrong return value from application\n");
  433. return EFI_ST_FAILURE;
  434. }
  435. if (priv.file_open_count) {
  436. efi_st_error("File open count = %d, expected 0\n",
  437. priv.file_open_count);
  438. return EFI_ST_FAILURE;
  439. }
  440. if (priv.volume_open_count) {
  441. efi_st_error("Volume open count = %d, expected 0\n",
  442. priv.volume_open_count);
  443. return EFI_ST_FAILURE;
  444. }
  445. return EFI_ST_SUCCESS;
  446. }
  447. EFI_UNIT_TEST(loadimage) = {
  448. .name = "load image from file",
  449. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  450. .setup = setup,
  451. .execute = execute,
  452. .teardown = teardown,
  453. };