efi_selftest_block_device.c 13 KB

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