efi_selftest_block_device.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * efi_selftest_block
  3. *
  4. * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. *
  8. * This test checks the driver for block IO devices.
  9. * A disk image is created in memory.
  10. * A handle is created for the new block IO device.
  11. * The block I/O protocol is installed on the handle.
  12. * ConnectController is used to setup partitions and to install the simple
  13. * file protocol.
  14. * A known file is read from the file system and verified.
  15. */
  16. #include <efi_selftest.h>
  17. #include "efi_selftest_disk_image.h"
  18. /* Block size of compressed disk image */
  19. #define COMPRESSED_DISK_IMAGE_BLOCK_SIZE 8
  20. /* Binary logarithm of the block size */
  21. #define LB_BLOCK_SIZE 9
  22. static struct efi_boot_services *boottime;
  23. static const efi_guid_t block_io_protocol_guid = BLOCK_IO_GUID;
  24. static const efi_guid_t guid_device_path = DEVICE_PATH_GUID;
  25. static const efi_guid_t guid_simple_file_system_protocol =
  26. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
  27. static efi_guid_t guid_vendor =
  28. EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
  29. 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xb7, 0xb8);
  30. static struct efi_device_path *dp;
  31. /* One 8 byte block of the compressed disk image */
  32. struct line {
  33. size_t addr;
  34. char *line;
  35. };
  36. /* Compressed disk image */
  37. struct compressed_disk_image {
  38. size_t length;
  39. struct line lines[];
  40. };
  41. static const struct compressed_disk_image img = EFI_ST_DISK_IMG;
  42. /* Decompressed disk image */
  43. static u8 *image;
  44. /*
  45. * Reset service of the block IO protocol.
  46. *
  47. * @this block IO protocol
  48. * @return status code
  49. */
  50. static efi_status_t EFIAPI reset(
  51. struct efi_block_io *this,
  52. char extended_verification)
  53. {
  54. return EFI_SUCCESS;
  55. }
  56. /*
  57. * Read service of the block IO protocol.
  58. *
  59. * @this block IO protocol
  60. * @media_id media id
  61. * @lba start of the read in logical blocks
  62. * @buffer_size number of bytes to read
  63. * @buffer target buffer
  64. * @return status code
  65. */
  66. static efi_status_t EFIAPI read_blocks(
  67. struct efi_block_io *this, u32 media_id, u64 lba,
  68. efi_uintn_t buffer_size, void *buffer)
  69. {
  70. u8 *start;
  71. if ((lba << LB_BLOCK_SIZE) + buffer_size > img.length)
  72. return EFI_INVALID_PARAMETER;
  73. start = image + (lba << LB_BLOCK_SIZE);
  74. boottime->copy_mem(buffer, start, buffer_size);
  75. return EFI_SUCCESS;
  76. }
  77. /*
  78. * Write service of the block IO protocol.
  79. *
  80. * @this block IO protocol
  81. * @media_id media id
  82. * @lba start of the write in logical blocks
  83. * @buffer_size number of bytes to read
  84. * @buffer source buffer
  85. * @return status code
  86. */
  87. static efi_status_t EFIAPI write_blocks(
  88. struct efi_block_io *this, u32 media_id, u64 lba,
  89. efi_uintn_t buffer_size, void *buffer)
  90. {
  91. u8 *start;
  92. if ((lba << LB_BLOCK_SIZE) + buffer_size > img.length)
  93. return EFI_INVALID_PARAMETER;
  94. start = image + (lba << LB_BLOCK_SIZE);
  95. boottime->copy_mem(start, buffer, buffer_size);
  96. return EFI_SUCCESS;
  97. }
  98. /*
  99. * Flush service of the block IO protocol.
  100. *
  101. * @this block IO protocol
  102. * @return status code
  103. */
  104. static efi_status_t EFIAPI flush_blocks(struct efi_block_io *this)
  105. {
  106. return EFI_SUCCESS;
  107. }
  108. /*
  109. * Decompress the disk image.
  110. *
  111. * @image decompressed disk image
  112. * @return status code
  113. */
  114. static efi_status_t decompress(u8 **image)
  115. {
  116. u8 *buf;
  117. size_t i;
  118. size_t addr;
  119. size_t len;
  120. efi_status_t ret;
  121. ret = boottime->allocate_pool(EFI_LOADER_DATA, img.length,
  122. (void **)&buf);
  123. if (ret != EFI_SUCCESS) {
  124. efi_st_error("Out of memory\n");
  125. return ret;
  126. }
  127. boottime->set_mem(buf, img.length, 0);
  128. for (i = 0; ; ++i) {
  129. if (!img.lines[i].line)
  130. break;
  131. addr = img.lines[i].addr;
  132. len = COMPRESSED_DISK_IMAGE_BLOCK_SIZE;
  133. if (addr + len > img.length)
  134. len = img.length - addr;
  135. boottime->copy_mem(buf + addr, img.lines[i].line, len);
  136. }
  137. *image = buf;
  138. return ret;
  139. }
  140. static struct efi_block_io_media media;
  141. static struct efi_block_io block_io = {
  142. .media = &media,
  143. .reset = reset,
  144. .read_blocks = read_blocks,
  145. .write_blocks = write_blocks,
  146. .flush_blocks = flush_blocks,
  147. };
  148. /* Handle for the block IO device */
  149. static efi_handle_t disk_handle;
  150. /*
  151. * Setup unit test.
  152. *
  153. * @handle: handle of the loaded image
  154. * @systable: system table
  155. * @return: EFI_ST_SUCCESS for success
  156. */
  157. static int setup(const efi_handle_t handle,
  158. const struct efi_system_table *systable)
  159. {
  160. efi_status_t ret;
  161. struct efi_device_path_vendor vendor_node;
  162. struct efi_device_path end_node;
  163. boottime = systable->boottime;
  164. decompress(&image);
  165. block_io.media->block_size = 1 << LB_BLOCK_SIZE;
  166. block_io.media->last_block = img.length >> LB_BLOCK_SIZE;
  167. ret = boottime->install_protocol_interface(
  168. &disk_handle, &block_io_protocol_guid,
  169. EFI_NATIVE_INTERFACE, &block_io);
  170. if (ret != EFI_SUCCESS) {
  171. efi_st_error("Failed to install block I/O protocol\n");
  172. return EFI_ST_FAILURE;
  173. }
  174. ret = boottime->allocate_pool(EFI_LOADER_DATA,
  175. sizeof(struct efi_device_path_vendor) +
  176. sizeof(struct efi_device_path),
  177. (void **)&dp);
  178. if (ret != EFI_SUCCESS) {
  179. efi_st_error("Out of memory\n");
  180. return EFI_ST_FAILURE;
  181. }
  182. vendor_node.dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
  183. vendor_node.dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
  184. vendor_node.dp.length = sizeof(struct efi_device_path_vendor);
  185. boottime->copy_mem(&vendor_node.guid, &guid_vendor,
  186. sizeof(efi_guid_t));
  187. boottime->copy_mem(dp, &vendor_node,
  188. sizeof(struct efi_device_path_vendor));
  189. end_node.type = DEVICE_PATH_TYPE_END;
  190. end_node.sub_type = DEVICE_PATH_SUB_TYPE_END;
  191. end_node.length = sizeof(struct efi_device_path);
  192. boottime->copy_mem((char *)dp + sizeof(struct efi_device_path_vendor),
  193. &end_node, sizeof(struct efi_device_path));
  194. ret = boottime->install_protocol_interface(&disk_handle,
  195. &guid_device_path,
  196. EFI_NATIVE_INTERFACE,
  197. dp);
  198. if (ret != EFI_SUCCESS) {
  199. efi_st_error("InstallProtocolInterface failed\n");
  200. return EFI_ST_FAILURE;
  201. }
  202. return EFI_ST_SUCCESS;
  203. }
  204. /*
  205. * Tear down unit test.
  206. *
  207. * @return: EFI_ST_SUCCESS for success
  208. */
  209. static int teardown(void)
  210. {
  211. efi_status_t r = EFI_ST_SUCCESS;
  212. if (disk_handle) {
  213. r = boottime->uninstall_protocol_interface(disk_handle,
  214. &guid_device_path,
  215. dp);
  216. if (r != EFI_SUCCESS) {
  217. efi_st_error("Uninstall device path failed\n");
  218. return EFI_ST_FAILURE;
  219. }
  220. r = boottime->uninstall_protocol_interface(
  221. disk_handle, &block_io_protocol_guid,
  222. &block_io);
  223. if (r != EFI_SUCCESS) {
  224. efi_st_todo(
  225. "Failed to uninstall block I/O protocol\n");
  226. return EFI_ST_SUCCESS;
  227. }
  228. }
  229. if (image) {
  230. r = efi_free_pool(image);
  231. if (r != EFI_SUCCESS) {
  232. efi_st_error("Failed to free image\n");
  233. return EFI_ST_FAILURE;
  234. }
  235. }
  236. return r;
  237. }
  238. /*
  239. * Get length of device path without end tag.
  240. *
  241. * @dp device path
  242. * @return length of device path in bytes
  243. */
  244. static efi_uintn_t dp_size(struct efi_device_path *dp)
  245. {
  246. struct efi_device_path *pos = dp;
  247. while (pos->type != DEVICE_PATH_TYPE_END)
  248. pos = (struct efi_device_path *)((char *)pos + pos->length);
  249. return (char *)pos - (char *)dp;
  250. }
  251. /*
  252. * Execute unit test.
  253. *
  254. * @return: EFI_ST_SUCCESS for success
  255. */
  256. static int execute(void)
  257. {
  258. efi_status_t ret;
  259. efi_uintn_t no_handles, i, len;
  260. efi_handle_t *handles;
  261. efi_handle_t handle_partition = NULL;
  262. struct efi_device_path *dp_partition;
  263. struct efi_simple_file_system_protocol *file_system;
  264. struct efi_file_handle *root, *file;
  265. u64 buf_size;
  266. char buf[16] __aligned(ARCH_DMA_MINALIGN);
  267. ret = boottime->connect_controller(disk_handle, NULL, NULL, 1);
  268. if (ret != EFI_SUCCESS) {
  269. efi_st_error("Failed to connect controller\n");
  270. return EFI_ST_FAILURE;
  271. }
  272. ret = boottime->locate_handle_buffer(
  273. BY_PROTOCOL, &guid_device_path, NULL,
  274. &no_handles, &handles);
  275. if (ret != EFI_SUCCESS) {
  276. efi_st_error("Failed to locate handles\n");
  277. return EFI_ST_FAILURE;
  278. }
  279. len = dp_size(dp);
  280. for (i = 0; i < no_handles; ++i) {
  281. ret = boottime->open_protocol(handles[i], &guid_device_path,
  282. (void **)&dp_partition,
  283. NULL, NULL,
  284. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  285. if (ret != EFI_SUCCESS) {
  286. efi_st_error("Failed to open device path protocol\n");
  287. return EFI_ST_FAILURE;
  288. }
  289. if (len >= dp_size(dp_partition))
  290. continue;
  291. if (efi_st_memcmp(dp, dp_partition, len))
  292. continue;
  293. handle_partition = handles[i];
  294. break;
  295. }
  296. ret = boottime->free_pool(handles);
  297. if (ret != EFI_SUCCESS) {
  298. efi_st_error("Failed to free pool memory\n");
  299. return EFI_ST_FAILURE;
  300. }
  301. if (!handle_partition) {
  302. efi_st_error("Partition handle not found\n");
  303. return EFI_ST_FAILURE;
  304. }
  305. ret = boottime->open_protocol(handle_partition,
  306. &guid_simple_file_system_protocol,
  307. (void **)&file_system, NULL, NULL,
  308. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  309. if (ret != EFI_SUCCESS) {
  310. efi_st_error("Failed to open simple file system protocol\n");
  311. return EFI_ST_FAILURE;
  312. }
  313. ret = file_system->open_volume(file_system, &root);
  314. if (ret != EFI_SUCCESS) {
  315. efi_st_error("Failed to open volume\n");
  316. return EFI_ST_FAILURE;
  317. }
  318. ret = root->open(root, &file, (s16 *)L"hello.txt", EFI_FILE_MODE_READ,
  319. 0);
  320. if (ret != EFI_SUCCESS) {
  321. efi_st_error("Failed to open file\n");
  322. return EFI_ST_FAILURE;
  323. }
  324. buf_size = sizeof(buf) - 1;
  325. ret = file->read(file, &buf_size, buf);
  326. if (ret != EFI_SUCCESS) {
  327. efi_st_error("Failed to read file\n");
  328. return EFI_ST_FAILURE;
  329. }
  330. if (efi_st_memcmp(buf, "Hello world!", 12)) {
  331. efi_st_error("Unexpected file content\n");
  332. return EFI_ST_FAILURE;
  333. }
  334. ret = file->close(file);
  335. if (ret != EFI_SUCCESS) {
  336. efi_st_error("Failed to close file\n");
  337. return EFI_ST_FAILURE;
  338. }
  339. ret = root->close(root);
  340. if (ret != EFI_SUCCESS) {
  341. efi_st_error("Failed to close volume\n");
  342. return EFI_ST_FAILURE;
  343. }
  344. return EFI_ST_SUCCESS;
  345. }
  346. EFI_UNIT_TEST(blkdev) = {
  347. .name = "block device",
  348. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  349. .setup = setup,
  350. .execute = execute,
  351. .teardown = teardown,
  352. };