efi_selftest_block_device.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * efi_selftest_block
  4. *
  5. * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. *
  7. * This test checks the driver for block IO devices.
  8. * A disk image is created in memory.
  9. * A handle is created for the new block IO device.
  10. * The block I/O protocol is installed on the handle.
  11. * ConnectController is used to setup partitions and to install the simple
  12. * file protocol.
  13. * A known file is read from the file system and verified.
  14. */
  15. #include <efi_selftest.h>
  16. #include "efi_selftest_disk_image.h"
  17. /* Block size of compressed disk image */
  18. #define COMPRESSED_DISK_IMAGE_BLOCK_SIZE 8
  19. /* Binary logarithm of the block size */
  20. #define LB_BLOCK_SIZE 9
  21. static struct efi_boot_services *boottime;
  22. static const efi_guid_t block_io_protocol_guid = BLOCK_IO_GUID;
  23. static const efi_guid_t guid_device_path = DEVICE_PATH_GUID;
  24. static const efi_guid_t guid_simple_file_system_protocol =
  25. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
  26. static const efi_guid_t guid_file_system_info = EFI_FILE_SYSTEM_INFO_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. struct {
  266. struct efi_file_system_info info;
  267. u16 label[12];
  268. } system_info;
  269. efi_uintn_t buf_size;
  270. char buf[16] __aligned(ARCH_DMA_MINALIGN);
  271. /* Connect controller to virtual disk */
  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. /* Get the handle for the partition */
  278. ret = boottime->locate_handle_buffer(
  279. BY_PROTOCOL, &guid_device_path, NULL,
  280. &no_handles, &handles);
  281. if (ret != EFI_SUCCESS) {
  282. efi_st_error("Failed to locate handles\n");
  283. return EFI_ST_FAILURE;
  284. }
  285. len = dp_size(dp);
  286. for (i = 0; i < no_handles; ++i) {
  287. ret = boottime->open_protocol(handles[i], &guid_device_path,
  288. (void **)&dp_partition,
  289. NULL, NULL,
  290. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  291. if (ret != EFI_SUCCESS) {
  292. efi_st_error("Failed to open device path protocol\n");
  293. return EFI_ST_FAILURE;
  294. }
  295. if (len >= dp_size(dp_partition))
  296. continue;
  297. if (efi_st_memcmp(dp, dp_partition, len))
  298. continue;
  299. handle_partition = handles[i];
  300. break;
  301. }
  302. ret = boottime->free_pool(handles);
  303. if (ret != EFI_SUCCESS) {
  304. efi_st_error("Failed to free pool memory\n");
  305. return EFI_ST_FAILURE;
  306. }
  307. if (!handle_partition) {
  308. efi_st_error("Partition handle not found\n");
  309. return EFI_ST_FAILURE;
  310. }
  311. /* Open the simple file system protocol */
  312. ret = boottime->open_protocol(handle_partition,
  313. &guid_simple_file_system_protocol,
  314. (void **)&file_system, NULL, NULL,
  315. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  316. if (ret != EFI_SUCCESS) {
  317. efi_st_error("Failed to open simple file system protocol\n");
  318. return EFI_ST_FAILURE;
  319. }
  320. /* Open volume */
  321. ret = file_system->open_volume(file_system, &root);
  322. if (ret != EFI_SUCCESS) {
  323. efi_st_error("Failed to open volume\n");
  324. return EFI_ST_FAILURE;
  325. }
  326. buf_size = sizeof(system_info);
  327. ret = root->getinfo(root, &guid_file_system_info, &buf_size,
  328. &system_info);
  329. if (ret != EFI_SUCCESS) {
  330. efi_st_error("Failed to get file system info\n");
  331. return EFI_ST_FAILURE;
  332. }
  333. if (system_info.info.block_size != 512) {
  334. efi_st_error("Wrong block size %u, expected 512\n",
  335. system_info.info.block_size);
  336. return EFI_ST_FAILURE;
  337. }
  338. if (efi_st_strcmp_16_8(system_info.info.volume_label, "U-BOOT TEST")) {
  339. efi_st_todo(
  340. "Wrong volume label '%ps', expected 'U-BOOT TEST'\n",
  341. system_info.info.volume_label);
  342. }
  343. /* Read file */
  344. ret = root->open(root, &file, (s16 *)L"hello.txt", EFI_FILE_MODE_READ,
  345. 0);
  346. if (ret != EFI_SUCCESS) {
  347. efi_st_error("Failed to open file\n");
  348. return EFI_ST_FAILURE;
  349. }
  350. buf_size = sizeof(buf) - 1;
  351. ret = file->read(file, &buf_size, buf);
  352. if (ret != EFI_SUCCESS) {
  353. efi_st_error("Failed to read file\n");
  354. return EFI_ST_FAILURE;
  355. }
  356. if (buf_size != 13) {
  357. efi_st_error("Wrong number of bytes read: %u\n",
  358. (unsigned int)buf_size);
  359. return EFI_ST_FAILURE;
  360. }
  361. if (efi_st_memcmp(buf, "Hello world!", 12)) {
  362. efi_st_error("Unexpected file content\n");
  363. return EFI_ST_FAILURE;
  364. }
  365. ret = file->close(file);
  366. if (ret != EFI_SUCCESS) {
  367. efi_st_error("Failed to close file\n");
  368. return EFI_ST_FAILURE;
  369. }
  370. #ifdef CONFIG_FAT_WRITE
  371. /* Write file */
  372. ret = root->open(root, &file, (s16 *)L"u-boot.txt",
  373. EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0);
  374. if (ret != EFI_SUCCESS) {
  375. efi_st_error("Failed to open file\n");
  376. return EFI_ST_FAILURE;
  377. }
  378. buf_size = 7;
  379. boottime->set_mem(buf, sizeof(buf), 0);
  380. boottime->copy_mem(buf, "U-Boot", buf_size);
  381. ret = file->write(file, &buf_size, buf);
  382. if (ret != EFI_SUCCESS || buf_size != 7) {
  383. efi_st_error("Failed to write file\n");
  384. return EFI_ST_FAILURE;
  385. }
  386. ret = file->close(file);
  387. if (ret != EFI_SUCCESS) {
  388. efi_st_error("Failed to close file\n");
  389. return EFI_ST_FAILURE;
  390. }
  391. /* Verify file */
  392. boottime->set_mem(buf, sizeof(buf), 0);
  393. ret = root->open(root, &file, (s16 *)L"u-boot.txt", EFI_FILE_MODE_READ,
  394. 0);
  395. if (ret != EFI_SUCCESS) {
  396. efi_st_error("Failed to open file\n");
  397. return EFI_ST_FAILURE;
  398. }
  399. buf_size = sizeof(buf) - 1;
  400. ret = file->read(file, &buf_size, buf);
  401. if (ret != EFI_SUCCESS) {
  402. efi_st_error("Failed to read file\n");
  403. return EFI_ST_FAILURE;
  404. }
  405. if (buf_size != 7) {
  406. efi_st_error("Wrong number of bytes read: %u\n",
  407. (unsigned int)buf_size);
  408. return EFI_ST_FAILURE;
  409. }
  410. if (efi_st_memcmp(buf, "U-Boot", 7)) {
  411. efi_st_error("Unexpected file content %s\n", buf);
  412. return EFI_ST_FAILURE;
  413. }
  414. ret = file->close(file);
  415. if (ret != EFI_SUCCESS) {
  416. efi_st_error("Failed to close file\n");
  417. return EFI_ST_FAILURE;
  418. }
  419. #else
  420. efi_st_todo("CONFIG_FAT_WRITE is not set\n");
  421. #endif /* CONFIG_FAT_WRITE */
  422. /* Close volume */
  423. ret = root->close(root);
  424. if (ret != EFI_SUCCESS) {
  425. efi_st_error("Failed to close volume\n");
  426. return EFI_ST_FAILURE;
  427. }
  428. return EFI_ST_SUCCESS;
  429. }
  430. EFI_UNIT_TEST(blkdev) = {
  431. .name = "block device",
  432. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  433. .setup = setup,
  434. .execute = execute,
  435. .teardown = teardown,
  436. };