1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
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
13 * A known file is read from the file system and verified.
16 #include <efi_selftest.h>
17 #include "efi_selftest_disk_image.h"
18 #include <asm/cache.h>
20 /* Block size of compressed disk image */
21 #define COMPRESSED_DISK_IMAGE_BLOCK_SIZE 8
23 /* Binary logarithm of the block size */
24 #define LB_BLOCK_SIZE 9
26 static struct efi_boot_services *boottime;
28 static const efi_guid_t block_io_protocol_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
29 static const efi_guid_t guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID;
30 static const efi_guid_t guid_simple_file_system_protocol =
31 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
32 static const efi_guid_t guid_file_system_info = EFI_FILE_SYSTEM_INFO_GUID;
33 static efi_guid_t guid_vendor =
34 EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
35 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xb7, 0xb8);
37 static struct efi_device_path *dp;
39 /* One 8 byte block of the compressed disk image */
45 /* Compressed disk image */
46 struct compressed_disk_image {
51 static const struct compressed_disk_image img = EFI_ST_DISK_IMG;
53 /* Decompressed disk image */
57 * Reset service of the block IO protocol.
59 * @this block IO protocol
62 static efi_status_t EFIAPI reset(
63 struct efi_block_io *this,
64 char extended_verification)
70 * Read service of the block IO protocol.
72 * @this block IO protocol
74 * @lba start of the read in logical blocks
75 * @buffer_size number of bytes to read
76 * @buffer target buffer
79 static efi_status_t EFIAPI read_blocks(
80 struct efi_block_io *this, u32 media_id, u64 lba,
81 efi_uintn_t buffer_size, void *buffer)
85 if ((lba << LB_BLOCK_SIZE) + buffer_size > img.length)
86 return EFI_INVALID_PARAMETER;
87 start = image + (lba << LB_BLOCK_SIZE);
89 boottime->copy_mem(buffer, start, buffer_size);
95 * Write service of the block IO protocol.
97 * @this block IO protocol
99 * @lba start of the write in logical blocks
100 * @buffer_size number of bytes to read
101 * @buffer source buffer
102 * @return status code
104 static efi_status_t EFIAPI write_blocks(
105 struct efi_block_io *this, u32 media_id, u64 lba,
106 efi_uintn_t buffer_size, void *buffer)
110 if ((lba << LB_BLOCK_SIZE) + buffer_size > img.length)
111 return EFI_INVALID_PARAMETER;
112 start = image + (lba << LB_BLOCK_SIZE);
114 boottime->copy_mem(start, buffer, buffer_size);
120 * Flush service of the block IO protocol.
122 * @this block IO protocol
123 * @return status code
125 static efi_status_t EFIAPI flush_blocks(struct efi_block_io *this)
131 * Decompress the disk image.
133 * @image decompressed disk image
134 * @return status code
136 static efi_status_t decompress(u8 **image)
144 ret = boottime->allocate_pool(EFI_LOADER_DATA, img.length,
146 if (ret != EFI_SUCCESS) {
147 efi_st_error("Out of memory\n");
150 boottime->set_mem(buf, img.length, 0);
153 if (!img.lines[i].line)
155 addr = img.lines[i].addr;
156 len = COMPRESSED_DISK_IMAGE_BLOCK_SIZE;
157 if (addr + len > img.length)
158 len = img.length - addr;
159 boottime->copy_mem(buf + addr, img.lines[i].line, len);
165 static struct efi_block_io_media media;
167 static struct efi_block_io block_io = {
170 .read_blocks = read_blocks,
171 .write_blocks = write_blocks,
172 .flush_blocks = flush_blocks,
175 /* Handle for the block IO device */
176 static efi_handle_t disk_handle;
181 * @handle: handle of the loaded image
182 * @systable: system table
183 * @return: EFI_ST_SUCCESS for success
185 static int setup(const efi_handle_t handle,
186 const struct efi_system_table *systable)
189 struct efi_device_path_vendor vendor_node;
190 struct efi_device_path end_node;
192 boottime = systable->boottime;
196 block_io.media->block_size = 1 << LB_BLOCK_SIZE;
197 block_io.media->last_block = img.length >> LB_BLOCK_SIZE;
199 ret = boottime->install_protocol_interface(
200 &disk_handle, &block_io_protocol_guid,
201 EFI_NATIVE_INTERFACE, &block_io);
202 if (ret != EFI_SUCCESS) {
203 efi_st_error("Failed to install block I/O protocol\n");
204 return EFI_ST_FAILURE;
207 ret = boottime->allocate_pool(EFI_LOADER_DATA,
208 sizeof(struct efi_device_path_vendor) +
209 sizeof(struct efi_device_path),
211 if (ret != EFI_SUCCESS) {
212 efi_st_error("Out of memory\n");
213 return EFI_ST_FAILURE;
215 vendor_node.dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
216 vendor_node.dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
217 vendor_node.dp.length = sizeof(struct efi_device_path_vendor);
219 boottime->copy_mem(&vendor_node.guid, &guid_vendor,
221 boottime->copy_mem(dp, &vendor_node,
222 sizeof(struct efi_device_path_vendor));
223 end_node.type = DEVICE_PATH_TYPE_END;
224 end_node.sub_type = DEVICE_PATH_SUB_TYPE_END;
225 end_node.length = sizeof(struct efi_device_path);
227 boottime->copy_mem((char *)dp + sizeof(struct efi_device_path_vendor),
228 &end_node, sizeof(struct efi_device_path));
229 ret = boottime->install_protocol_interface(&disk_handle,
231 EFI_NATIVE_INTERFACE,
233 if (ret != EFI_SUCCESS) {
234 efi_st_error("InstallProtocolInterface failed\n");
235 return EFI_ST_FAILURE;
237 return EFI_ST_SUCCESS;
241 * Tear down unit test.
243 * @return: EFI_ST_SUCCESS for success
245 static int teardown(void)
247 efi_status_t r = EFI_ST_SUCCESS;
250 r = boottime->uninstall_protocol_interface(disk_handle,
253 if (r != EFI_SUCCESS) {
254 efi_st_error("Uninstall device path failed\n");
255 return EFI_ST_FAILURE;
257 r = boottime->uninstall_protocol_interface(
258 disk_handle, &block_io_protocol_guid,
260 if (r != EFI_SUCCESS) {
262 "Failed to uninstall block I/O protocol\n");
263 return EFI_ST_FAILURE;
268 r = boottime->free_pool(image);
269 if (r != EFI_SUCCESS) {
270 efi_st_error("Failed to free image\n");
271 return EFI_ST_FAILURE;
278 * Get length of device path without end tag.
281 * @return length of device path in bytes
283 static efi_uintn_t dp_size(struct efi_device_path *dp)
285 struct efi_device_path *pos = dp;
287 while (pos->type != DEVICE_PATH_TYPE_END)
288 pos = (struct efi_device_path *)((char *)pos + pos->length);
289 return (char *)pos - (char *)dp;
295 * @return: EFI_ST_SUCCESS for success
297 static int execute(void)
300 efi_uintn_t no_handles, i, len;
301 efi_handle_t *handles;
302 efi_handle_t handle_partition = NULL;
303 struct efi_device_path *dp_partition;
304 struct efi_simple_file_system_protocol *file_system;
305 struct efi_file_handle *root, *file;
307 struct efi_file_system_info info;
310 efi_uintn_t buf_size;
311 char buf[16] __aligned(ARCH_DMA_MINALIGN);
314 /* Connect controller to virtual disk */
315 ret = boottime->connect_controller(disk_handle, NULL, NULL, 1);
316 if (ret != EFI_SUCCESS) {
317 efi_st_error("Failed to connect controller\n");
318 return EFI_ST_FAILURE;
321 /* Get the handle for the partition */
322 ret = boottime->locate_handle_buffer(
323 BY_PROTOCOL, &guid_device_path, NULL,
324 &no_handles, &handles);
325 if (ret != EFI_SUCCESS) {
326 efi_st_error("Failed to locate handles\n");
327 return EFI_ST_FAILURE;
330 for (i = 0; i < no_handles; ++i) {
331 ret = boottime->open_protocol(handles[i], &guid_device_path,
332 (void **)&dp_partition,
334 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
335 if (ret != EFI_SUCCESS) {
336 efi_st_error("Failed to open device path protocol\n");
337 return EFI_ST_FAILURE;
339 if (len >= dp_size(dp_partition))
341 if (memcmp(dp, dp_partition, len))
343 handle_partition = handles[i];
346 ret = boottime->free_pool(handles);
347 if (ret != EFI_SUCCESS) {
348 efi_st_error("Failed to free pool memory\n");
349 return EFI_ST_FAILURE;
351 if (!handle_partition) {
352 efi_st_error("Partition handle not found\n");
353 return EFI_ST_FAILURE;
356 /* Open the simple file system protocol */
357 ret = boottime->open_protocol(handle_partition,
358 &guid_simple_file_system_protocol,
359 (void **)&file_system, NULL, NULL,
360 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
361 if (ret != EFI_SUCCESS) {
362 efi_st_error("Failed to open simple file system protocol\n");
363 return EFI_ST_FAILURE;
367 ret = file_system->open_volume(file_system, &root);
368 if (ret != EFI_SUCCESS) {
369 efi_st_error("Failed to open volume\n");
370 return EFI_ST_FAILURE;
372 buf_size = sizeof(system_info);
373 ret = root->getinfo(root, &guid_file_system_info, &buf_size,
375 if (ret != EFI_SUCCESS) {
376 efi_st_error("Failed to get file system info\n");
377 return EFI_ST_FAILURE;
379 if (system_info.info.block_size != 512) {
380 efi_st_error("Wrong block size %u, expected 512\n",
381 system_info.info.block_size);
382 return EFI_ST_FAILURE;
384 if (efi_st_strcmp_16_8(system_info.info.volume_label, "U-BOOT TEST")) {
386 "Wrong volume label '%ps', expected 'U-BOOT TEST'\n",
387 system_info.info.volume_label);
391 ret = root->open(root, &file, L"hello.txt", EFI_FILE_MODE_READ,
393 if (ret != EFI_SUCCESS) {
394 efi_st_error("Failed to open file\n");
395 return EFI_ST_FAILURE;
397 ret = file->setpos(file, 1);
398 if (ret != EFI_SUCCESS) {
399 efi_st_error("SetPosition failed\n");
400 return EFI_ST_FAILURE;
402 buf_size = sizeof(buf) - 1;
403 ret = file->read(file, &buf_size, buf);
404 if (ret != EFI_SUCCESS) {
405 efi_st_error("Failed to read file\n");
406 return EFI_ST_FAILURE;
408 if (buf_size != 12) {
409 efi_st_error("Wrong number of bytes read: %u\n",
410 (unsigned int)buf_size);
411 return EFI_ST_FAILURE;
413 if (memcmp(buf, "ello world!", 11)) {
414 efi_st_error("Unexpected file content\n");
415 return EFI_ST_FAILURE;
417 ret = file->getpos(file, &pos);
418 if (ret != EFI_SUCCESS) {
419 efi_st_error("GetPosition failed\n");
420 return EFI_ST_FAILURE;
423 efi_st_error("GetPosition returned %u, expected 13\n",
425 return EFI_ST_FAILURE;
427 ret = file->close(file);
428 if (ret != EFI_SUCCESS) {
429 efi_st_error("Failed to close file\n");
430 return EFI_ST_FAILURE;
433 #ifdef CONFIG_FAT_WRITE
435 ret = root->open(root, &file, L"u-boot.txt", EFI_FILE_MODE_READ |
436 EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0);
437 if (ret != EFI_SUCCESS) {
438 efi_st_error("Failed to open file\n");
439 return EFI_ST_FAILURE;
442 boottime->set_mem(buf, sizeof(buf), 0);
443 boottime->copy_mem(buf, "U-Boot", buf_size);
444 ret = file->write(file, &buf_size, buf);
445 if (ret != EFI_SUCCESS || buf_size != 7) {
446 efi_st_error("Failed to write file\n");
447 return EFI_ST_FAILURE;
449 ret = file->getpos(file, &pos);
450 if (ret != EFI_SUCCESS) {
451 efi_st_error("GetPosition failed\n");
452 return EFI_ST_FAILURE;
455 efi_st_error("GetPosition returned %u, expected 7\n",
457 return EFI_ST_FAILURE;
459 ret = file->close(file);
460 if (ret != EFI_SUCCESS) {
461 efi_st_error("Failed to close file\n");
462 return EFI_ST_FAILURE;
466 boottime->set_mem(buf, sizeof(buf), 0);
467 ret = root->open(root, &file, L"u-boot.txt", EFI_FILE_MODE_READ,
469 if (ret != EFI_SUCCESS) {
470 efi_st_error("Failed to open file\n");
471 return EFI_ST_FAILURE;
473 buf_size = sizeof(buf) - 1;
474 ret = file->read(file, &buf_size, buf);
475 if (ret != EFI_SUCCESS) {
476 efi_st_error("Failed to read file\n");
477 return EFI_ST_FAILURE;
480 efi_st_error("Wrong number of bytes read: %u\n",
481 (unsigned int)buf_size);
482 return EFI_ST_FAILURE;
484 if (memcmp(buf, "U-Boot", 7)) {
485 efi_st_error("Unexpected file content %s\n", buf);
486 return EFI_ST_FAILURE;
488 ret = file->close(file);
489 if (ret != EFI_SUCCESS) {
490 efi_st_error("Failed to close file\n");
491 return EFI_ST_FAILURE;
494 efi_st_todo("CONFIG_FAT_WRITE is not set\n");
495 #endif /* CONFIG_FAT_WRITE */
498 ret = root->close(root);
499 if (ret != EFI_SUCCESS) {
500 efi_st_error("Failed to close volume\n");
501 return EFI_ST_FAILURE;
504 return EFI_ST_SUCCESS;
507 EFI_UNIT_TEST(blkdev) = {
508 .name = "block device",
509 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
512 .teardown = teardown,