fdtdec: support multiple phandles in memory carveout
[oweals/u-boot.git] / lib / efi_selftest / efi_selftest_block_device.c
index 9e4b93d9a618e0ddd77cd49c154ced9c09dc3301..d98a854e6d1a81d97762ce6d2f9e8508a291c8ec 100644 (file)
@@ -1,10 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * efi_selftest_block
  *
  * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
  *
- * SPDX-License-Identifier:     GPL-2.0+
- *
  * This test checks the driver for block IO devices.
  * A disk image is created in memory.
  * A handle is created for the new block IO device.
 
 static struct efi_boot_services *boottime;
 
-static const efi_guid_t block_io_protocol_guid = BLOCK_IO_GUID;
-static const efi_guid_t guid_device_path = DEVICE_PATH_GUID;
+static const efi_guid_t block_io_protocol_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
+static const efi_guid_t guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID;
 static const efi_guid_t guid_simple_file_system_protocol =
                                        EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
+static const efi_guid_t guid_file_system_info = EFI_FILE_SYSTEM_INFO_GUID;
 static efi_guid_t guid_vendor =
        EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
                 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xb7, 0xb8);
@@ -257,14 +257,14 @@ static int teardown(void)
                                disk_handle, &block_io_protocol_guid,
                                &block_io);
                if (r != EFI_SUCCESS) {
-                       efi_st_todo(
+                       efi_st_error(
                                "Failed to uninstall block I/O protocol\n");
-                       return EFI_ST_SUCCESS;
+                       return EFI_ST_FAILURE;
                }
        }
 
        if (image) {
-               r = efi_free_pool(image);
+               r = boottime->free_pool(image);
                if (r != EFI_SUCCESS) {
                        efi_st_error("Failed to free image\n");
                        return EFI_ST_FAILURE;
@@ -302,14 +302,22 @@ static int execute(void)
        struct efi_device_path *dp_partition;
        struct efi_simple_file_system_protocol *file_system;
        struct efi_file_handle *root, *file;
-       u64 buf_size;
+       struct {
+               struct efi_file_system_info info;
+               u16 label[12];
+       } system_info;
+       efi_uintn_t buf_size;
        char buf[16] __aligned(ARCH_DMA_MINALIGN);
+       u64 pos;
 
+       /* Connect controller to virtual disk */
        ret = boottime->connect_controller(disk_handle, NULL, NULL, 1);
        if (ret != EFI_SUCCESS) {
                efi_st_error("Failed to connect controller\n");
                return EFI_ST_FAILURE;
        }
+
+       /* Get the handle for the partition */
        ret = boottime->locate_handle_buffer(
                                BY_PROTOCOL, &guid_device_path, NULL,
                                &no_handles, &handles);
@@ -329,7 +337,7 @@ static int execute(void)
                }
                if (len >= dp_size(dp_partition))
                        continue;
-               if (efi_st_memcmp(dp, dp_partition, len))
+               if (memcmp(dp, dp_partition, len))
                        continue;
                handle_partition = handles[i];
                break;
@@ -343,6 +351,8 @@ static int execute(void)
                efi_st_error("Partition handle not found\n");
                return EFI_ST_FAILURE;
        }
+
+       /* Open the simple file system protocol */
        ret = boottime->open_protocol(handle_partition,
                                      &guid_simple_file_system_protocol,
                                      (void **)&file_system, NULL, NULL,
@@ -351,32 +361,139 @@ static int execute(void)
                efi_st_error("Failed to open simple file system protocol\n");
                return EFI_ST_FAILURE;
        }
+
+       /* Open volume */
        ret = file_system->open_volume(file_system, &root);
        if (ret != EFI_SUCCESS) {
                efi_st_error("Failed to open volume\n");
                return EFI_ST_FAILURE;
        }
-       ret = root->open(root, &file, (s16 *)L"hello.txt", EFI_FILE_MODE_READ,
+       buf_size = sizeof(system_info);
+       ret = root->getinfo(root, &guid_file_system_info, &buf_size,
+                           &system_info);
+       if (ret != EFI_SUCCESS) {
+               efi_st_error("Failed to get file system info\n");
+               return EFI_ST_FAILURE;
+       }
+       if (system_info.info.block_size != 512) {
+               efi_st_error("Wrong block size %u, expected 512\n",
+                            system_info.info.block_size);
+               return EFI_ST_FAILURE;
+       }
+       if (efi_st_strcmp_16_8(system_info.info.volume_label, "U-BOOT TEST")) {
+               efi_st_todo(
+                       "Wrong volume label '%ps', expected 'U-BOOT TEST'\n",
+                       system_info.info.volume_label);
+       }
+
+       /* Read file */
+       ret = root->open(root, &file, L"hello.txt", EFI_FILE_MODE_READ,
                         0);
        if (ret != EFI_SUCCESS) {
                efi_st_error("Failed to open file\n");
                return EFI_ST_FAILURE;
        }
+       ret = file->setpos(file, 1);
+       if (ret != EFI_SUCCESS) {
+               efi_st_error("SetPosition failed\n");
+               return EFI_ST_FAILURE;
+       }
        buf_size = sizeof(buf) - 1;
        ret = file->read(file, &buf_size, buf);
        if (ret != EFI_SUCCESS) {
                efi_st_error("Failed to read file\n");
                return EFI_ST_FAILURE;
        }
-       if (efi_st_memcmp(buf, "Hello world!", 12)) {
+       if (buf_size != 12) {
+               efi_st_error("Wrong number of bytes read: %u\n",
+                            (unsigned int)buf_size);
+               return EFI_ST_FAILURE;
+       }
+       if (memcmp(buf, "ello world!", 11)) {
                efi_st_error("Unexpected file content\n");
                return EFI_ST_FAILURE;
        }
+       ret = file->getpos(file, &pos);
+       if (ret != EFI_SUCCESS) {
+               efi_st_error("GetPosition failed\n");
+               return EFI_ST_FAILURE;
+       }
+       if (pos != 13) {
+               efi_st_error("GetPosition returned %u, expected 13\n",
+                            (unsigned int)pos);
+               return EFI_ST_FAILURE;
+       }
        ret = file->close(file);
        if (ret != EFI_SUCCESS) {
                efi_st_error("Failed to close file\n");
                return EFI_ST_FAILURE;
        }
+
+#ifdef CONFIG_FAT_WRITE
+       /* Write file */
+       ret = root->open(root, &file, L"u-boot.txt", EFI_FILE_MODE_READ |
+                        EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0);
+       if (ret != EFI_SUCCESS) {
+               efi_st_error("Failed to open file\n");
+               return EFI_ST_FAILURE;
+       }
+       buf_size = 7;
+       boottime->set_mem(buf, sizeof(buf), 0);
+       boottime->copy_mem(buf, "U-Boot", buf_size);
+       ret = file->write(file, &buf_size, buf);
+       if (ret != EFI_SUCCESS || buf_size != 7) {
+               efi_st_error("Failed to write file\n");
+               return EFI_ST_FAILURE;
+       }
+       ret = file->getpos(file, &pos);
+       if (ret != EFI_SUCCESS) {
+               efi_st_error("GetPosition failed\n");
+               return EFI_ST_FAILURE;
+       }
+       if (pos != 7) {
+               efi_st_error("GetPosition returned %u, expected 7\n",
+                            (unsigned int)pos);
+               return EFI_ST_FAILURE;
+       }
+       ret = file->close(file);
+       if (ret != EFI_SUCCESS) {
+               efi_st_error("Failed to close file\n");
+               return EFI_ST_FAILURE;
+       }
+
+       /* Verify file */
+       boottime->set_mem(buf, sizeof(buf), 0);
+       ret = root->open(root, &file, L"u-boot.txt", EFI_FILE_MODE_READ,
+                        0);
+       if (ret != EFI_SUCCESS) {
+               efi_st_error("Failed to open file\n");
+               return EFI_ST_FAILURE;
+       }
+       buf_size = sizeof(buf) - 1;
+       ret = file->read(file, &buf_size, buf);
+       if (ret != EFI_SUCCESS) {
+               efi_st_error("Failed to read file\n");
+               return EFI_ST_FAILURE;
+       }
+       if (buf_size != 7) {
+               efi_st_error("Wrong number of bytes read: %u\n",
+                            (unsigned int)buf_size);
+               return EFI_ST_FAILURE;
+       }
+       if (memcmp(buf, "U-Boot", 7)) {
+               efi_st_error("Unexpected file content %s\n", buf);
+               return EFI_ST_FAILURE;
+       }
+       ret = file->close(file);
+       if (ret != EFI_SUCCESS) {
+               efi_st_error("Failed to close file\n");
+               return EFI_ST_FAILURE;
+       }
+#else
+       efi_st_todo("CONFIG_FAT_WRITE is not set\n");
+#endif /* CONFIG_FAT_WRITE */
+
+       /* Close volume */
        ret = root->close(root);
        if (ret != EFI_SUCCESS) {
                efi_st_error("Failed to close volume\n");