efi_selftest_block_device.c 10 KB

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