efi_selftest_loadimage.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 L"app.efi"
  20. #define VOLUME_NAME L"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 open_volume
  127. (struct efi_simple_file_system_protocol *this,
  128. struct efi_file_handle **root);
  129. static efi_status_t EFIAPI 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 close(struct efi_file_handle *this);
  134. static efi_status_t EFIAPI delete(struct efi_file_handle *this);
  135. static efi_status_t EFIAPI read
  136. (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer);
  137. static efi_status_t EFIAPI write
  138. (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer);
  139. static efi_status_t EFIAPI getpos(struct efi_file_handle *this, u64 *pos);
  140. static efi_status_t EFIAPI setpos(struct efi_file_handle *this, u64 pos);
  141. static efi_status_t EFIAPI getinfo
  142. (struct efi_file_handle *this, const efi_guid_t *info_type,
  143. efi_uintn_t *buffer_size, void *buffer);
  144. static efi_status_t EFIAPI setinfo
  145. (struct efi_file_handle *this, const efi_guid_t *info_type,
  146. efi_uintn_t buffer_size, void *buffer);
  147. static efi_status_t EFIAPI flush(struct efi_file_handle *this);
  148. /* Internal information about status of file system */
  149. static struct {
  150. /* Difference of volume open count minus volume close count */
  151. int volume_open_count;
  152. /* Difference of file open count minus file close count */
  153. int file_open_count;
  154. /* File size */
  155. u64 file_size;
  156. /* Current position in file */
  157. u64 file_pos;
  158. } priv;
  159. /* EFI_FILE_PROTOCOL for file */
  160. static struct efi_file_handle file = {
  161. .rev = 0x00010000,
  162. .open = open,
  163. .close = close,
  164. .delete = delete,
  165. .read = read,
  166. .write = write,
  167. .getpos = getpos,
  168. .setpos = setpos,
  169. .getinfo = getinfo,
  170. .setinfo = setinfo,
  171. .flush = flush,
  172. };
  173. /* EFI_FILE_PROTOCOL for root directory */
  174. static struct efi_file_handle volume = {
  175. .rev = 0x00010000,
  176. .open = open,
  177. .close = close,
  178. .delete = delete,
  179. .read = read,
  180. .write = write,
  181. .getpos = getpos,
  182. .setpos = setpos,
  183. .getinfo = getinfo,
  184. .setinfo = setinfo,
  185. .flush = flush,
  186. };
  187. /* EFI_SIMPLE_FILE_SYSTEM_PROTOCOL of the block device */
  188. struct efi_simple_file_system_protocol file_system = {
  189. .rev = 0x00010000,
  190. .open_volume = open_volume,
  191. };
  192. static efi_status_t EFIAPI open_volume
  193. (struct efi_simple_file_system_protocol *this,
  194. struct efi_file_handle **root)
  195. {
  196. if (this != &file_system || !root)
  197. return EFI_INVALID_PARAMETER;
  198. *root = &volume;
  199. priv.volume_open_count++;
  200. return EFI_SUCCESS;
  201. }
  202. static efi_status_t EFIAPI open
  203. (struct efi_file_handle *this,
  204. struct efi_file_handle **new_handle,
  205. u16 *file_name, u64 open_mode, u64 attributes)
  206. {
  207. if (this != &volume)
  208. return EFI_INVALID_PARAMETER;
  209. *new_handle = &file;
  210. priv.file_pos = 0;
  211. priv.file_open_count++;
  212. return EFI_SUCCESS;
  213. }
  214. static efi_status_t EFIAPI close(struct efi_file_handle *this)
  215. {
  216. if (this == &file)
  217. priv.file_open_count--;
  218. else if (this == &volume)
  219. priv.volume_open_count--;
  220. else
  221. return EFI_INVALID_PARAMETER;
  222. return EFI_SUCCESS;
  223. }
  224. static efi_status_t EFIAPI delete(struct efi_file_handle *this)
  225. {
  226. if (this != &file)
  227. return EFI_INVALID_PARAMETER;
  228. return EFI_UNSUPPORTED;
  229. }
  230. static efi_status_t EFIAPI read
  231. (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer)
  232. {
  233. if (this != &file)
  234. return EFI_INVALID_PARAMETER;
  235. if (priv.file_pos >= img.length)
  236. *buffer_size = 0;
  237. else if (priv.file_pos + *buffer_size > img.length)
  238. *buffer_size = img.length - priv.file_pos;
  239. boottime->copy_mem(buffer, &image[priv.file_pos], *buffer_size);
  240. priv.file_pos += *buffer_size;
  241. return EFI_SUCCESS;
  242. }
  243. static efi_status_t EFIAPI write
  244. (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer)
  245. {
  246. if (this != &file)
  247. return EFI_INVALID_PARAMETER;
  248. return EFI_UNSUPPORTED;
  249. }
  250. static efi_status_t EFIAPI getpos(struct efi_file_handle *this, u64 *pos)
  251. {
  252. if (this != &file)
  253. return EFI_INVALID_PARAMETER;
  254. *pos = priv.file_pos;
  255. return EFI_SUCCESS;
  256. }
  257. static efi_status_t EFIAPI setpos(struct efi_file_handle *this, u64 pos)
  258. {
  259. if (this != &file)
  260. return EFI_INVALID_PARAMETER;
  261. priv.file_pos = pos;
  262. return EFI_SUCCESS;
  263. }
  264. static efi_status_t EFIAPI getinfo
  265. (struct efi_file_handle *this, const efi_guid_t *info_type,
  266. efi_uintn_t *buffer_size, void *buffer)
  267. {
  268. if (this == &file) {
  269. if (memcmp(info_type, &guid_file_info, sizeof(efi_guid_t)))
  270. return EFI_INVALID_PARAMETER;
  271. if (*buffer_size >= sizeof(struct file_info)) {
  272. boottime->copy_mem(buffer, file_info,
  273. sizeof(struct file_info));
  274. } else {
  275. *buffer_size = sizeof(struct file_info);
  276. return EFI_BUFFER_TOO_SMALL;
  277. }
  278. } else if (this == &volume) {
  279. if (memcmp(info_type, &guid_file_system_info,
  280. sizeof(efi_guid_t)))
  281. return EFI_INVALID_PARAMETER;
  282. if (*buffer_size >= sizeof(struct file_system_info)) {
  283. boottime->copy_mem(buffer, file_system_info,
  284. sizeof(struct file_system_info));
  285. } else {
  286. *buffer_size = sizeof(struct file_system_info);
  287. return EFI_BUFFER_TOO_SMALL;
  288. }
  289. } else {
  290. return EFI_INVALID_PARAMETER;
  291. }
  292. return EFI_SUCCESS;
  293. }
  294. static efi_status_t EFIAPI setinfo
  295. (struct efi_file_handle *this, const efi_guid_t *info_type,
  296. efi_uintn_t buffer_size, void *buffer)
  297. {
  298. if (this != &file)
  299. return EFI_INVALID_PARAMETER;
  300. return EFI_UNSUPPORTED;
  301. }
  302. static efi_status_t EFIAPI flush(struct efi_file_handle *this)
  303. {
  304. if (this != &file)
  305. return EFI_INVALID_PARAMETER;
  306. return EFI_UNSUPPORTED;
  307. }
  308. /*
  309. * Decompress the disk image.
  310. *
  311. * @image decompressed disk image
  312. * @return status code
  313. */
  314. static efi_status_t decompress(u8 **image)
  315. {
  316. u8 *buf;
  317. size_t i;
  318. size_t addr;
  319. size_t len;
  320. efi_status_t ret;
  321. ret = boottime->allocate_pool(EFI_LOADER_DATA, img.length,
  322. (void **)&buf);
  323. if (ret != EFI_SUCCESS) {
  324. efi_st_error("Out of memory\n");
  325. return ret;
  326. }
  327. boottime->set_mem(buf, img.length, 0);
  328. for (i = 0; ; ++i) {
  329. if (!img.lines[i].line)
  330. break;
  331. addr = img.lines[i].addr;
  332. len = COMPRESSED_DISK_IMAGE_BLOCK_SIZE;
  333. if (addr + len > img.length)
  334. len = img.length - addr;
  335. boottime->copy_mem(buf + addr, img.lines[i].line, len);
  336. }
  337. *image = buf;
  338. priv.file_size = img.length;
  339. file_info->file_size = img.length;
  340. return ret;
  341. }
  342. /*
  343. * Setup unit test.
  344. *
  345. * Decompress application image and provide a handle for the in memory block
  346. * device.
  347. *
  348. * @handle: handle of the loaded image
  349. * @systable: system table
  350. * @return: EFI_ST_SUCCESS for success
  351. */
  352. static int setup(const efi_handle_t handle,
  353. const struct efi_system_table *systable)
  354. {
  355. efi_status_t ret;
  356. handle_image = handle;
  357. boottime = systable->boottime;
  358. /* Load the application image into memory */
  359. decompress(&image);
  360. ret = boottime->install_protocol_interface
  361. (&handle_volume, &guid_device_path, EFI_NATIVE_INTERFACE,
  362. &dp_volume);
  363. if (ret != EFI_SUCCESS) {
  364. efi_st_error("Failed to install device path\n");
  365. return EFI_ST_FAILURE;
  366. }
  367. ret = boottime->install_protocol_interface
  368. (&handle_volume, &guid_simple_file_system_protocol,
  369. EFI_NATIVE_INTERFACE, &file_system);
  370. if (ret != EFI_SUCCESS) {
  371. efi_st_error("Failed to install simple file system protocol\n");
  372. return EFI_ST_FAILURE;
  373. }
  374. return EFI_ST_SUCCESS;
  375. }
  376. /*
  377. * Tear down unit test.
  378. *
  379. * Uninstall protocols and free memory.
  380. *
  381. * @return: EFI_ST_SUCCESS for success
  382. */
  383. static int teardown(void)
  384. {
  385. efi_status_t ret = EFI_ST_SUCCESS;
  386. if (handle_volume) {
  387. ret = boottime->uninstall_protocol_interface
  388. (handle_volume, &guid_simple_file_system_protocol,
  389. &file_system);
  390. if (ret != EFI_SUCCESS) {
  391. efi_st_error
  392. ("Failed to uninstall simple file system protocol\n");
  393. return EFI_ST_FAILURE;
  394. }
  395. ret = boottime->uninstall_protocol_interface
  396. (handle_volume, &guid_device_path, &dp_volume);
  397. if (ret != EFI_SUCCESS) {
  398. efi_st_error
  399. ("Failed to uninstall device path protocol\n");
  400. return EFI_ST_FAILURE;
  401. }
  402. }
  403. if (image) {
  404. ret = boottime->free_pool(image);
  405. if (ret != EFI_SUCCESS) {
  406. efi_st_error("Failed to free image\n");
  407. return EFI_ST_FAILURE;
  408. }
  409. }
  410. return ret;
  411. }
  412. /*
  413. * Execute unit test.
  414. *
  415. * Load and start the application image.
  416. *
  417. * @return: EFI_ST_SUCCESS for success
  418. */
  419. static int execute(void)
  420. {
  421. efi_status_t ret;
  422. efi_handle_t handle;
  423. ret = boottime->load_image(false, handle_image, &dp_file.vendor.dp,
  424. NULL, 0, &handle);
  425. if (ret != EFI_SUCCESS) {
  426. efi_st_error("Failed to load image\n");
  427. return EFI_ST_FAILURE;
  428. }
  429. ret = boottime->start_image(handle, NULL, NULL);
  430. if (ret != EFI_UNSUPPORTED) {
  431. efi_st_error("Wrong return value from application\n");
  432. return EFI_ST_FAILURE;
  433. }
  434. if (priv.file_open_count) {
  435. efi_st_error("File open count = %d, expected 0\n",
  436. priv.file_open_count);
  437. return EFI_ST_FAILURE;
  438. }
  439. if (priv.volume_open_count) {
  440. efi_st_error("Volume open count = %d, expected 0\n",
  441. priv.volume_open_count);
  442. return EFI_ST_FAILURE;
  443. }
  444. return EFI_ST_SUCCESS;
  445. }
  446. EFI_UNIT_TEST(loadimage) = {
  447. .name = "load image from file",
  448. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  449. .setup = setup,
  450. .execute = execute,
  451. .teardown = teardown,
  452. };