uboot/lib/efi_selftest/efi_selftest_block_device.c
<<
>>
Prefs
   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 * The same block is read via the EFI_BLOCK_IO_PROTOCOL and compared to the file
  15 * contents.
  16 */
  17
  18#include <efi_selftest.h>
  19#include "efi_selftest_disk_image.h"
  20#include <asm/cache.h>
  21
  22/* Block size of compressed disk image */
  23#define COMPRESSED_DISK_IMAGE_BLOCK_SIZE 8
  24
  25/* Binary logarithm of the block size */
  26#define LB_BLOCK_SIZE 9
  27
  28static struct efi_boot_services *boottime;
  29
  30static const efi_guid_t block_io_protocol_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
  31static const efi_guid_t guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID;
  32static const efi_guid_t guid_simple_file_system_protocol =
  33                                        EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
  34static const efi_guid_t guid_file_system_info = EFI_FILE_SYSTEM_INFO_GUID;
  35static efi_guid_t guid_vendor =
  36        EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
  37                 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xb7, 0xb8);
  38
  39static struct efi_device_path *dp;
  40
  41/* One 8 byte block of the compressed disk image */
  42struct line {
  43        size_t addr;
  44        char *line;
  45};
  46
  47/* Compressed disk image */
  48struct compressed_disk_image {
  49        size_t length;
  50        struct line lines[];
  51};
  52
  53static const struct compressed_disk_image img = EFI_ST_DISK_IMG;
  54
  55/* Decompressed disk image */
  56static u8 *image;
  57
  58/*
  59 * Reset service of the block IO protocol.
  60 *
  61 * @this        block IO protocol
  62 * Return:      status code
  63 */
  64static efi_status_t EFIAPI reset(
  65                        struct efi_block_io *this,
  66                        char extended_verification)
  67{
  68        return EFI_SUCCESS;
  69}
  70
  71/*
  72 * Read service of the block IO protocol.
  73 *
  74 * @this        block IO protocol
  75 * @media_id    media id
  76 * @lba         start of the read in logical blocks
  77 * @buffer_size number of bytes to read
  78 * @buffer      target buffer
  79 * Return:      status code
  80 */
  81static efi_status_t EFIAPI read_blocks(
  82                        struct efi_block_io *this, u32 media_id, u64 lba,
  83                        efi_uintn_t buffer_size, void *buffer)
  84{
  85        u8 *start;
  86
  87        if ((lba << LB_BLOCK_SIZE) + buffer_size > img.length)
  88                return EFI_INVALID_PARAMETER;
  89        start = image + (lba << LB_BLOCK_SIZE);
  90
  91        boottime->copy_mem(buffer, start, buffer_size);
  92
  93        return EFI_SUCCESS;
  94}
  95
  96/*
  97 * Write service of the block IO protocol.
  98 *
  99 * @this        block IO protocol
 100 * @media_id    media id
 101 * @lba         start of the write in logical blocks
 102 * @buffer_size number of bytes to read
 103 * @buffer      source buffer
 104 * Return:      status code
 105 */
 106static efi_status_t EFIAPI write_blocks(
 107                        struct efi_block_io *this, u32 media_id, u64 lba,
 108                        efi_uintn_t buffer_size, void *buffer)
 109{
 110        u8 *start;
 111
 112        if ((lba << LB_BLOCK_SIZE) + buffer_size > img.length)
 113                return EFI_INVALID_PARAMETER;
 114        start = image + (lba << LB_BLOCK_SIZE);
 115
 116        boottime->copy_mem(start, buffer, buffer_size);
 117
 118        return EFI_SUCCESS;
 119}
 120
 121/*
 122 * Flush service of the block IO protocol.
 123 *
 124 * @this        block IO protocol
 125 * Return:      status code
 126 */
 127static efi_status_t EFIAPI flush_blocks(struct efi_block_io *this)
 128{
 129        return EFI_SUCCESS;
 130}
 131
 132/*
 133 * Decompress the disk image.
 134 *
 135 * @image       decompressed disk image
 136 * Return:      status code
 137 */
 138static efi_status_t decompress(u8 **image)
 139{
 140        u8 *buf;
 141        size_t i;
 142        size_t addr;
 143        size_t len;
 144        efi_status_t ret;
 145
 146        ret = boottime->allocate_pool(EFI_LOADER_DATA, img.length,
 147                                      (void **)&buf);
 148        if (ret != EFI_SUCCESS) {
 149                efi_st_error("Out of memory\n");
 150                return ret;
 151        }
 152        boottime->set_mem(buf, img.length, 0);
 153
 154        for (i = 0; ; ++i) {
 155                if (!img.lines[i].line)
 156                        break;
 157                addr = img.lines[i].addr;
 158                len = COMPRESSED_DISK_IMAGE_BLOCK_SIZE;
 159                if (addr + len > img.length)
 160                        len = img.length - addr;
 161                boottime->copy_mem(buf + addr, img.lines[i].line, len);
 162        }
 163        *image = buf;
 164        return ret;
 165}
 166
 167static struct efi_block_io_media media;
 168
 169static struct efi_block_io block_io = {
 170        .media = &media,
 171        .reset = reset,
 172        .read_blocks = read_blocks,
 173        .write_blocks = write_blocks,
 174        .flush_blocks = flush_blocks,
 175};
 176
 177/* Handle for the block IO device */
 178static efi_handle_t disk_handle;
 179
 180/*
 181 * Setup unit test.
 182 *
 183 * @handle:     handle of the loaded image
 184 * @systable:   system table
 185 * Return:      EFI_ST_SUCCESS for success
 186 */
 187static int setup(const efi_handle_t handle,
 188                 const struct efi_system_table *systable)
 189{
 190        efi_status_t ret;
 191        struct efi_device_path_vendor vendor_node;
 192        struct efi_device_path end_node;
 193
 194        boottime = systable->boottime;
 195
 196        decompress(&image);
 197
 198        block_io.media->block_size = 1 << LB_BLOCK_SIZE;
 199        block_io.media->last_block = (img.length >> LB_BLOCK_SIZE) - 1;
 200
 201        ret = boottime->install_protocol_interface(
 202                                &disk_handle, &block_io_protocol_guid,
 203                                EFI_NATIVE_INTERFACE, &block_io);
 204        if (ret != EFI_SUCCESS) {
 205                efi_st_error("Failed to install block I/O protocol\n");
 206                return EFI_ST_FAILURE;
 207        }
 208
 209        ret = boottime->allocate_pool(EFI_LOADER_DATA,
 210                                      sizeof(struct efi_device_path_vendor) +
 211                                      sizeof(struct efi_device_path),
 212                                      (void **)&dp);
 213        if (ret != EFI_SUCCESS) {
 214                efi_st_error("Out of memory\n");
 215                return EFI_ST_FAILURE;
 216        }
 217        vendor_node.dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
 218        vendor_node.dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
 219        vendor_node.dp.length = sizeof(struct efi_device_path_vendor);
 220
 221        boottime->copy_mem(&vendor_node.guid, &guid_vendor,
 222                           sizeof(efi_guid_t));
 223        boottime->copy_mem(dp, &vendor_node,
 224                           sizeof(struct efi_device_path_vendor));
 225        end_node.type = DEVICE_PATH_TYPE_END;
 226        end_node.sub_type = DEVICE_PATH_SUB_TYPE_END;
 227        end_node.length = sizeof(struct efi_device_path);
 228
 229        boottime->copy_mem((char *)dp + sizeof(struct efi_device_path_vendor),
 230                           &end_node, sizeof(struct efi_device_path));
 231        ret = boottime->install_protocol_interface(&disk_handle,
 232                                                   &guid_device_path,
 233                                                   EFI_NATIVE_INTERFACE,
 234                                                   dp);
 235        if (ret != EFI_SUCCESS) {
 236                efi_st_error("InstallProtocolInterface failed\n");
 237                return EFI_ST_FAILURE;
 238        }
 239        return EFI_ST_SUCCESS;
 240}
 241
 242/*
 243 * Tear down unit test.
 244 *
 245 * Return:      EFI_ST_SUCCESS for success
 246 */
 247static int teardown(void)
 248{
 249        efi_status_t r = EFI_ST_SUCCESS;
 250
 251        if (disk_handle) {
 252                r = boottime->uninstall_protocol_interface(disk_handle,
 253                                                           &guid_device_path,
 254                                                           dp);
 255                if (r != EFI_SUCCESS) {
 256                        efi_st_error("Uninstall device path failed\n");
 257                        return EFI_ST_FAILURE;
 258                }
 259                r = boottime->uninstall_protocol_interface(
 260                                disk_handle, &block_io_protocol_guid,
 261                                &block_io);
 262                if (r != EFI_SUCCESS) {
 263                        efi_st_error(
 264                                "Failed to uninstall block I/O protocol\n");
 265                        return EFI_ST_FAILURE;
 266                }
 267        }
 268
 269        if (image) {
 270                r = boottime->free_pool(image);
 271                if (r != EFI_SUCCESS) {
 272                        efi_st_error("Failed to free image\n");
 273                        return EFI_ST_FAILURE;
 274                }
 275        }
 276        return r;
 277}
 278
 279/*
 280 * Get length of device path without end tag.
 281 *
 282 * @dp          device path
 283 * Return:      length of device path in bytes
 284 */
 285static efi_uintn_t dp_size(struct efi_device_path *dp)
 286{
 287        struct efi_device_path *pos = dp;
 288
 289        while (pos->type != DEVICE_PATH_TYPE_END)
 290                pos = (struct efi_device_path *)((char *)pos + pos->length);
 291        return (char *)pos - (char *)dp;
 292}
 293
 294/*
 295 * Execute unit test.
 296 *
 297 * Return:      EFI_ST_SUCCESS for success
 298 */
 299static int execute(void)
 300{
 301        efi_status_t ret;
 302        efi_uintn_t no_handles, i, len;
 303        efi_handle_t *handles;
 304        efi_handle_t handle_partition = NULL;
 305        struct efi_device_path *dp_partition;
 306        struct efi_block_io *block_io_protocol;
 307        struct efi_simple_file_system_protocol *file_system;
 308        struct efi_file_handle *root, *file;
 309        struct {
 310                struct efi_file_system_info info;
 311                u16 label[12];
 312        } system_info;
 313        efi_uintn_t buf_size;
 314        char buf[16] __aligned(ARCH_DMA_MINALIGN);
 315        u32 part1_size;
 316        u64 pos;
 317        char block_io_aligned[1 << LB_BLOCK_SIZE] __aligned(1 << LB_BLOCK_SIZE);
 318
 319        /* Connect controller to virtual disk */
 320        ret = boottime->connect_controller(disk_handle, NULL, NULL, 1);
 321        if (ret != EFI_SUCCESS) {
 322                efi_st_error("Failed to connect controller\n");
 323                return EFI_ST_FAILURE;
 324        }
 325
 326        /* Get the handle for the partition */
 327        ret = boottime->locate_handle_buffer(
 328                                BY_PROTOCOL, &guid_device_path, NULL,
 329                                &no_handles, &handles);
 330        if (ret != EFI_SUCCESS) {
 331                efi_st_error("Failed to locate handles\n");
 332                return EFI_ST_FAILURE;
 333        }
 334        len = dp_size(dp);
 335        for (i = 0; i < no_handles; ++i) {
 336                ret = boottime->open_protocol(handles[i], &guid_device_path,
 337                                              (void **)&dp_partition,
 338                                              NULL, NULL,
 339                                              EFI_OPEN_PROTOCOL_GET_PROTOCOL);
 340                if (ret != EFI_SUCCESS) {
 341                        efi_st_error("Failed to open device path protocol\n");
 342                        return EFI_ST_FAILURE;
 343                }
 344                if (len >= dp_size(dp_partition))
 345                        continue;
 346                if (memcmp(dp, dp_partition, len))
 347                        continue;
 348                handle_partition = handles[i];
 349                break;
 350        }
 351        ret = boottime->free_pool(handles);
 352        if (ret != EFI_SUCCESS) {
 353                efi_st_error("Failed to free pool memory\n");
 354                return EFI_ST_FAILURE;
 355        }
 356        if (!handle_partition) {
 357                efi_st_error("Partition handle not found\n");
 358                return EFI_ST_FAILURE;
 359        }
 360
 361        /* Open the block_io_protocol */
 362        ret = boottime->open_protocol(handle_partition,
 363                                      &block_io_protocol_guid,
 364                                      (void **)&block_io_protocol, NULL, NULL,
 365                                      EFI_OPEN_PROTOCOL_GET_PROTOCOL);
 366        if (ret != EFI_SUCCESS) {
 367                efi_st_error("Failed to open block IO protocol\n");
 368                return EFI_ST_FAILURE;
 369        }
 370        /* Get size of first MBR partition */
 371        memcpy(&part1_size, image + 0x1ca, sizeof(u32));
 372        if (block_io_protocol->media->last_block != part1_size - 1) {
 373                efi_st_error("Last LBA of partition %x, expected %x\n",
 374                             (unsigned int)block_io_protocol->media->last_block,
 375                             part1_size - 1);
 376                return EFI_ST_FAILURE;
 377        }
 378        /* Open the simple file system protocol */
 379        ret = boottime->open_protocol(handle_partition,
 380                                      &guid_simple_file_system_protocol,
 381                                      (void **)&file_system, NULL, NULL,
 382                                      EFI_OPEN_PROTOCOL_GET_PROTOCOL);
 383        if (ret != EFI_SUCCESS) {
 384                efi_st_error("Failed to open simple file system protocol\n");
 385                return EFI_ST_FAILURE;
 386        }
 387
 388        /* Open volume */
 389        ret = file_system->open_volume(file_system, &root);
 390        if (ret != EFI_SUCCESS) {
 391                efi_st_error("Failed to open volume\n");
 392                return EFI_ST_FAILURE;
 393        }
 394        buf_size = sizeof(system_info);
 395        ret = root->getinfo(root, &guid_file_system_info, &buf_size,
 396                            &system_info);
 397        if (ret != EFI_SUCCESS) {
 398                efi_st_error("Failed to get file system info\n");
 399                return EFI_ST_FAILURE;
 400        }
 401        if (system_info.info.block_size != 512) {
 402                efi_st_error("Wrong block size %u, expected 512\n",
 403                             system_info.info.block_size);
 404                return EFI_ST_FAILURE;
 405        }
 406        if (efi_st_strcmp_16_8(system_info.info.volume_label, "U-BOOT TEST")) {
 407                efi_st_todo(
 408                        "Wrong volume label '%ps', expected 'U-BOOT TEST'\n",
 409                        system_info.info.volume_label);
 410        }
 411
 412        /* Read file */
 413        ret = root->open(root, &file, u"hello.txt", EFI_FILE_MODE_READ,
 414                         0);
 415        if (ret != EFI_SUCCESS) {
 416                efi_st_error("Failed to open file\n");
 417                return EFI_ST_FAILURE;
 418        }
 419        ret = file->setpos(file, 1);
 420        if (ret != EFI_SUCCESS) {
 421                efi_st_error("SetPosition failed\n");
 422                return EFI_ST_FAILURE;
 423        }
 424        buf_size = sizeof(buf) - 1;
 425        ret = file->read(file, &buf_size, buf);
 426        if (ret != EFI_SUCCESS) {
 427                efi_st_error("Failed to read file\n");
 428                return EFI_ST_FAILURE;
 429        }
 430        if (buf_size != 12) {
 431                efi_st_error("Wrong number of bytes read: %u\n",
 432                             (unsigned int)buf_size);
 433                return EFI_ST_FAILURE;
 434        }
 435        if (memcmp(buf, "ello world!", 11)) {
 436                efi_st_error("Unexpected file content\n");
 437                return EFI_ST_FAILURE;
 438        }
 439        ret = file->getpos(file, &pos);
 440        if (ret != EFI_SUCCESS) {
 441                efi_st_error("GetPosition failed\n");
 442                return EFI_ST_FAILURE;
 443        }
 444        if (pos != 13) {
 445                efi_st_error("GetPosition returned %u, expected 13\n",
 446                             (unsigned int)pos);
 447                return EFI_ST_FAILURE;
 448        }
 449        ret = file->close(file);
 450        if (ret != EFI_SUCCESS) {
 451                efi_st_error("Failed to close file\n");
 452                return EFI_ST_FAILURE;
 453        }
 454
 455        /*
 456         * Test that read_blocks() can read same file data.
 457         *
 458         * In the test data, the partition starts at block 1 and the file
 459         * hello.txt with the content 'Hello world!' is located at 0x5000
 460         * of the disk. Here we read block 0x27 (offset 0x4e00 of the
 461         * partition) and expect the string 'Hello world!' to be at the
 462         * start of block.
 463         */
 464        ret = block_io_protocol->read_blocks(block_io_protocol,
 465                                      block_io_protocol->media->media_id,
 466                                      (0x5000 >> LB_BLOCK_SIZE) - 1,
 467                                      block_io_protocol->media->block_size,
 468                                      block_io_aligned);
 469        if (ret != EFI_SUCCESS) {
 470                efi_st_error("ReadBlocks failed\n");
 471                return EFI_ST_FAILURE;
 472        }
 473
 474        if (memcmp(block_io_aligned + 1, buf, 11)) {
 475                efi_st_error("Unexpected block content\n");
 476                return EFI_ST_FAILURE;
 477        }
 478
 479#ifdef CONFIG_FAT_WRITE
 480        /* Write file */
 481        ret = root->open(root, &file, u"u-boot.txt", EFI_FILE_MODE_READ |
 482                         EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0);
 483        if (ret != EFI_SUCCESS) {
 484                efi_st_error("Failed to open file\n");
 485                return EFI_ST_FAILURE;
 486        }
 487        buf_size = 7;
 488        boottime->set_mem(buf, sizeof(buf), 0);
 489        boottime->copy_mem(buf, "U-Boot", buf_size);
 490        ret = file->write(file, &buf_size, buf);
 491        if (ret != EFI_SUCCESS || buf_size != 7) {
 492                efi_st_error("Failed to write file\n");
 493                return EFI_ST_FAILURE;
 494        }
 495        ret = file->getpos(file, &pos);
 496        if (ret != EFI_SUCCESS) {
 497                efi_st_error("GetPosition failed\n");
 498                return EFI_ST_FAILURE;
 499        }
 500        if (pos != 7) {
 501                efi_st_error("GetPosition returned %u, expected 7\n",
 502                             (unsigned int)pos);
 503                return EFI_ST_FAILURE;
 504        }
 505        ret = file->close(file);
 506        if (ret != EFI_SUCCESS) {
 507                efi_st_error("Failed to close file\n");
 508                return EFI_ST_FAILURE;
 509        }
 510
 511        /* Verify file */
 512        boottime->set_mem(buf, sizeof(buf), 0);
 513        ret = root->open(root, &file, u"u-boot.txt", EFI_FILE_MODE_READ,
 514                         0);
 515        if (ret != EFI_SUCCESS) {
 516                efi_st_error("Failed to open file\n");
 517                return EFI_ST_FAILURE;
 518        }
 519        buf_size = sizeof(buf) - 1;
 520        ret = file->read(file, &buf_size, buf);
 521        if (ret != EFI_SUCCESS) {
 522                efi_st_error("Failed to read file\n");
 523                return EFI_ST_FAILURE;
 524        }
 525        if (buf_size != 7) {
 526                efi_st_error("Wrong number of bytes read: %u\n",
 527                             (unsigned int)buf_size);
 528                return EFI_ST_FAILURE;
 529        }
 530        if (memcmp(buf, "U-Boot", 7)) {
 531                efi_st_error("Unexpected file content %s\n", buf);
 532                return EFI_ST_FAILURE;
 533        }
 534        ret = file->close(file);
 535        if (ret != EFI_SUCCESS) {
 536                efi_st_error("Failed to close file\n");
 537                return EFI_ST_FAILURE;
 538        }
 539#else
 540        efi_st_todo("CONFIG_FAT_WRITE is not set\n");
 541#endif /* CONFIG_FAT_WRITE */
 542
 543        /* Close volume */
 544        ret = root->close(root);
 545        if (ret != EFI_SUCCESS) {
 546                efi_st_error("Failed to close volume\n");
 547                return EFI_ST_FAILURE;
 548        }
 549
 550        return EFI_ST_SUCCESS;
 551}
 552
 553EFI_UNIT_TEST(blkdev) = {
 554        .name = "block device",
 555        .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
 556        .setup = setup,
 557        .execute = execute,
 558        .teardown = teardown,
 559};
 560