efi_selftest_block_device.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. #include <asm/cache.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 = EFI_BLOCK_IO_PROTOCOL_GUID;
  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_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_error(
  226. "Failed to uninstall block I/O protocol\n");
  227. return EFI_ST_FAILURE;
  228. }
  229. }
  230. if (image) {
  231. r = boottime->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. u64 pos;
  273. /* Connect controller to virtual disk */
  274. ret = boottime->connect_controller(disk_handle, NULL, NULL, 1);
  275. if (ret != EFI_SUCCESS) {
  276. efi_st_error("Failed to connect controller\n");
  277. return EFI_ST_FAILURE;
  278. }
  279. /* Get the handle for the partition */
  280. ret = boottime->locate_handle_buffer(
  281. BY_PROTOCOL, &guid_device_path, NULL,
  282. &no_handles, &handles);
  283. if (ret != EFI_SUCCESS) {
  284. efi_st_error("Failed to locate handles\n");
  285. return EFI_ST_FAILURE;
  286. }
  287. len = dp_size(dp);
  288. for (i = 0; i < no_handles; ++i) {
  289. ret = boottime->open_protocol(handles[i], &guid_device_path,
  290. (void **)&dp_partition,
  291. NULL, NULL,
  292. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  293. if (ret != EFI_SUCCESS) {
  294. efi_st_error("Failed to open device path protocol\n");
  295. return EFI_ST_FAILURE;
  296. }
  297. if (len >= dp_size(dp_partition))
  298. continue;
  299. if (memcmp(dp, dp_partition, len))
  300. continue;
  301. handle_partition = handles[i];
  302. break;
  303. }
  304. ret = boottime->free_pool(handles);
  305. if (ret != EFI_SUCCESS) {
  306. efi_st_error("Failed to free pool memory\n");
  307. return EFI_ST_FAILURE;
  308. }
  309. if (!handle_partition) {
  310. efi_st_error("Partition handle not found\n");
  311. return EFI_ST_FAILURE;
  312. }
  313. /* Open the simple file system protocol */
  314. ret = boottime->open_protocol(handle_partition,
  315. &guid_simple_file_system_protocol,
  316. (void **)&file_system, NULL, NULL,
  317. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  318. if (ret != EFI_SUCCESS) {
  319. efi_st_error("Failed to open simple file system protocol\n");
  320. return EFI_ST_FAILURE;
  321. }
  322. /* Open volume */
  323. ret = file_system->open_volume(file_system, &root);
  324. if (ret != EFI_SUCCESS) {
  325. efi_st_error("Failed to open volume\n");
  326. return EFI_ST_FAILURE;
  327. }
  328. buf_size = sizeof(system_info);
  329. ret = root->getinfo(root, &guid_file_system_info, &buf_size,
  330. &system_info);
  331. if (ret != EFI_SUCCESS) {
  332. efi_st_error("Failed to get file system info\n");
  333. return EFI_ST_FAILURE;
  334. }
  335. if (system_info.info.block_size != 512) {
  336. efi_st_error("Wrong block size %u, expected 512\n",
  337. system_info.info.block_size);
  338. return EFI_ST_FAILURE;
  339. }
  340. if (efi_st_strcmp_16_8(system_info.info.volume_label, "U-BOOT TEST")) {
  341. efi_st_todo(
  342. "Wrong volume label '%ps', expected 'U-BOOT TEST'\n",
  343. system_info.info.volume_label);
  344. }
  345. /* Read file */
  346. ret = root->open(root, &file, L"hello.txt", EFI_FILE_MODE_READ,
  347. 0);
  348. if (ret != EFI_SUCCESS) {
  349. efi_st_error("Failed to open file\n");
  350. return EFI_ST_FAILURE;
  351. }
  352. ret = file->setpos(file, 1);
  353. if (ret != EFI_SUCCESS) {
  354. efi_st_error("SetPosition failed\n");
  355. return EFI_ST_FAILURE;
  356. }
  357. buf_size = sizeof(buf) - 1;
  358. ret = file->read(file, &buf_size, buf);
  359. if (ret != EFI_SUCCESS) {
  360. efi_st_error("Failed to read file\n");
  361. return EFI_ST_FAILURE;
  362. }
  363. if (buf_size != 12) {
  364. efi_st_error("Wrong number of bytes read: %u\n",
  365. (unsigned int)buf_size);
  366. return EFI_ST_FAILURE;
  367. }
  368. if (memcmp(buf, "ello world!", 11)) {
  369. efi_st_error("Unexpected file content\n");
  370. return EFI_ST_FAILURE;
  371. }
  372. ret = file->getpos(file, &pos);
  373. if (ret != EFI_SUCCESS) {
  374. efi_st_error("GetPosition failed\n");
  375. return EFI_ST_FAILURE;
  376. }
  377. if (pos != 13) {
  378. efi_st_error("GetPosition returned %u, expected 13\n",
  379. (unsigned int)pos);
  380. return EFI_ST_FAILURE;
  381. }
  382. ret = file->close(file);
  383. if (ret != EFI_SUCCESS) {
  384. efi_st_error("Failed to close file\n");
  385. return EFI_ST_FAILURE;
  386. }
  387. #ifdef CONFIG_FAT_WRITE
  388. /* Write file */
  389. ret = root->open(root, &file, L"u-boot.txt", EFI_FILE_MODE_READ |
  390. EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0);
  391. if (ret != EFI_SUCCESS) {
  392. efi_st_error("Failed to open file\n");
  393. return EFI_ST_FAILURE;
  394. }
  395. buf_size = 7;
  396. boottime->set_mem(buf, sizeof(buf), 0);
  397. boottime->copy_mem(buf, "U-Boot", buf_size);
  398. ret = file->write(file, &buf_size, buf);
  399. if (ret != EFI_SUCCESS || buf_size != 7) {
  400. efi_st_error("Failed to write file\n");
  401. return EFI_ST_FAILURE;
  402. }
  403. ret = file->getpos(file, &pos);
  404. if (ret != EFI_SUCCESS) {
  405. efi_st_error("GetPosition failed\n");
  406. return EFI_ST_FAILURE;
  407. }
  408. if (pos != 7) {
  409. efi_st_error("GetPosition returned %u, expected 7\n",
  410. (unsigned int)pos);
  411. return EFI_ST_FAILURE;
  412. }
  413. ret = file->close(file);
  414. if (ret != EFI_SUCCESS) {
  415. efi_st_error("Failed to close file\n");
  416. return EFI_ST_FAILURE;
  417. }
  418. /* Verify file */
  419. boottime->set_mem(buf, sizeof(buf), 0);
  420. ret = root->open(root, &file, L"u-boot.txt", EFI_FILE_MODE_READ,
  421. 0);
  422. if (ret != EFI_SUCCESS) {
  423. efi_st_error("Failed to open file\n");
  424. return EFI_ST_FAILURE;
  425. }
  426. buf_size = sizeof(buf) - 1;
  427. ret = file->read(file, &buf_size, buf);
  428. if (ret != EFI_SUCCESS) {
  429. efi_st_error("Failed to read file\n");
  430. return EFI_ST_FAILURE;
  431. }
  432. if (buf_size != 7) {
  433. efi_st_error("Wrong number of bytes read: %u\n",
  434. (unsigned int)buf_size);
  435. return EFI_ST_FAILURE;
  436. }
  437. if (memcmp(buf, "U-Boot", 7)) {
  438. efi_st_error("Unexpected file content %s\n", buf);
  439. return EFI_ST_FAILURE;
  440. }
  441. ret = file->close(file);
  442. if (ret != EFI_SUCCESS) {
  443. efi_st_error("Failed to close file\n");
  444. return EFI_ST_FAILURE;
  445. }
  446. #else
  447. efi_st_todo("CONFIG_FAT_WRITE is not set\n");
  448. #endif /* CONFIG_FAT_WRITE */
  449. /* Close volume */
  450. ret = root->close(root);
  451. if (ret != EFI_SUCCESS) {
  452. efi_st_error("Failed to close volume\n");
  453. return EFI_ST_FAILURE;
  454. }
  455. return EFI_ST_SUCCESS;
  456. }
  457. EFI_UNIT_TEST(blkdev) = {
  458. .name = "block device",
  459. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  460. .setup = setup,
  461. .execute = execute,
  462. .teardown = teardown,
  463. };