From patchwork Sat Jul 2 11:20:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Barbieri X-Patchwork-Id: 1651657 X-Patchwork-Delegate: xypron.glpk@gmx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=Wzt0payM; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LbS861wcZz9sFw for ; Sun, 3 Jul 2022 21:56:22 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5737283E60; Sun, 3 Jul 2022 13:56:18 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Wzt0payM"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 65D2582119; Sat, 2 Jul 2022 13:21:11 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id B5156839CC for ; Sat, 2 Jul 2022 13:21:08 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=plb365@gmail.com Received: by mail-qk1-x72b.google.com with SMTP id z16so3563341qkj.7 for ; Sat, 02 Jul 2022 04:21:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6cYh84+epWZP6psokurxUE/1eiUcX7drNXTwu56b8tQ=; b=Wzt0payMw0lTXm2IXCLxevlLruCx+3VY4+41aJq8oargcw/35xFwxpPHAw/3jTMKCt 3hqqcOf+DI73igoYCTUjVcfgpYqrZGLrryb1jSGtKAvKn/Ueq/7U4WF8c58ZyCVxLUNp A8r9s/G1xnJyPBP13NIEg3eyFHHEIC+m5N/+ocY0nUJZ5PuzYb/LargAI1R14ARpIQ+S Ob7UPfNpDUONlc1FS9UWxrbbhkdswNwE0A8oI7EvoraJ+At7PwDCZcxaJliQV+V4EdXy U1Hhb60fR4C8Ad/BaObZmqd2ohOfaLkX4kiiMMJp0SWzJexme0bjJspo2vMqokSdw0YW tBLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6cYh84+epWZP6psokurxUE/1eiUcX7drNXTwu56b8tQ=; b=5AbOsBL0+Eud1VCj7jP4NSHWJ7oVxsykt2ymsbLlrQ4xvlIgiKr0MMV+YYWIvhQy6d ArMGVlGQnsvxj9DcWh0hWUkl3CZx1Y6uQOJU+eF0Fp5pTBkP1Ij1NSK2tiurNYPcja7a AGeIK78MAY0FE9emDnkOpWoOidJXutlcEuOKuMclCtSWUD2G1mn5edgq/0my6yuwcD1Y dXoA/xnx0/8wLsnxu8U5jpRwQEFa09N4xLrvEEBm/wY7WGL4Bt2Cxs7o/VEY0QAS2SDX 3H053apC6PPShLfvfr/TOfzSX4MZXncycWc/lx9rDze6HaFKE7YyrTjIsXCa1F5HG9Kz Mwcg== X-Gm-Message-State: AJIora9mHlnHiT2A6p4xhDGN8+xhJed7eGDFEKHP1YHruehMxyaz0SBT dEXQe7dJLqD/2PL212D71+JybrDcS2Q= X-Google-Smtp-Source: AGRyM1u7EZvdcRC3Wl4SCH5t8Ig4E2LDKlRtf2vrYRInR94dPVT5/5HKF/DvmRKehnZj4f0XyUKbhA== X-Received: by 2002:a05:620a:2184:b0:6ae:ed43:489f with SMTP id g4-20020a05620a218400b006aeed43489fmr13703586qka.556.1656760867109; Sat, 02 Jul 2022 04:21:07 -0700 (PDT) Received: from barbieri-MS-7C37.xts.us.baesystems.com (pool-70-109-56-67.clppva.fios.verizon.net. [70.109.56.67]) by smtp.gmail.com with ESMTPSA id f14-20020a05620a408e00b006a5d2eb58b2sm21990909qko.33.2022.07.02.04.21.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Jul 2022 04:21:06 -0700 (PDT) From: Paul Barbieri To: u-boot@lists.denx.de Cc: Paul Barbieri , Heinrich Schuchardt , AKASHI Takahiro Subject: [PATCH v4] EFI: Fix ReadBlocks API reading incorrect sector for UCLASS_PARTITION devices Date: Sat, 2 Jul 2022 07:20:05 -0400 Message-Id: <20220702112005.27961-1-plb365@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <8d674232-0bab-b76e-1817-761c2cb6e387@gmx.de> References: <8d674232-0bab-b76e-1817-761c2cb6e387@gmx.de> X-Mailman-Approved-At: Sun, 03 Jul 2022 13:56:17 +0200 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean The requsted partition disk sector incorrectly has the parition start sector added in twice for UCLASS_PARTITION devices. The efi_disk_rw_blocks() routine adds the diskobj->offset to the requested lba. When the device is a UCLASS_PARTITION, the dev_read() or dev_write() routine is called which adds part-gpt_part_info.start. This causes I/O to the wrong sector. Takahiro Akashi suggested removing the offset field from the efi_disk_obj structure since disk-uclass.c handles the partition start biasing. Device types other than UCLASS_PARTITION set the diskobj->offset field to zero which makes the field unnecessary. This change removes the offset field from the structure and removes all references from the code which is isolated to the lib/efi_loader/efi_disk.c module. This change also adds a test for the EFI ReadBlocks() API in the EFI selftest code. There is already a test for reading a FAT file. The new test uses ReadBlocks() to read the same "disk" block and compare it to the data read from the file system API. Signed-Off-by: Paul Barbieri Cc: Heinrich Schuchardt Cc: AKASHI Takahiro --- Changes for v3: - Added requested comment in test code Changes for v4: - Use aligned buffer instead of computing aligned address lib/efi_loader/efi_disk.c | 8 +------ lib/efi_selftest/efi_selftest_block_device.c | 24 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/efi_loader/efi_disk.c b/lib/efi_loader/efi_disk.c index 1e82f52dc0..1d700b2a6b 100644 --- a/lib/efi_loader/efi_disk.c +++ b/lib/efi_loader/efi_disk.c @@ -35,7 +35,6 @@ const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID; * @dp: device path to the block device * @part: partition * @volume: simple file system protocol of the partition - * @offset: offset into disk for simple partition * @dev: associated DM device */ struct efi_disk_obj { @@ -47,7 +46,6 @@ struct efi_disk_obj { struct efi_device_path *dp; unsigned int part; struct efi_simple_file_system_protocol *volume; - lbaint_t offset; struct udevice *dev; /* TODO: move it to efi_object */ }; @@ -117,7 +115,6 @@ static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this, diskobj = container_of(this, struct efi_disk_obj, ops); blksz = diskobj->media.block_size; blocks = buffer_size / blksz; - lba += diskobj->offset; EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n", blocks, lba, blksz, direction); @@ -440,13 +437,11 @@ static efi_status_t efi_disk_add_dev( diskobj->dp = efi_dp_append_node(dp_parent, node); efi_free_pool(node); - diskobj->offset = part_info->start; diskobj->media.last_block = part_info->size - 1; if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) guid = &efi_system_partition_guid; } else { diskobj->dp = efi_dp_from_part(desc, part); - diskobj->offset = 0; diskobj->media.last_block = desc->lba - 1; } diskobj->part = part; @@ -501,12 +496,11 @@ static efi_status_t efi_disk_add_dev( *disk = diskobj; EFI_PRINT("BlockIO: part %u, present %d, logical %d, removable %d" - ", offset " LBAF ", last_block %llu\n", + ", last_block %llu\n", diskobj->part, diskobj->media.media_present, diskobj->media.logical_partition, diskobj->media.removable_media, - diskobj->offset, diskobj->media.last_block); /* Store first EFI system partition */ diff --git a/lib/efi_selftest/efi_selftest_block_device.c b/lib/efi_selftest/efi_selftest_block_device.c index 60fa655766..6da923909e 100644 --- a/lib/efi_selftest/efi_selftest_block_device.c +++ b/lib/efi_selftest/efi_selftest_block_device.c @@ -11,6 +11,7 @@ * ConnectController is used to setup partitions and to install the simple * file protocol. * A known file is read from the file system and verified. + * Test that the read_blocks API correctly reads a block from the device. */ #include @@ -312,6 +313,7 @@ static int execute(void) char buf[16] __aligned(ARCH_DMA_MINALIGN); u32 part1_size; u64 pos; + char block[1 << LB_BLOCK_SIZE] __aligned(1 << LB_BLOCK_SIZE); /* Connect controller to virtual disk */ ret = boottime->connect_controller(disk_handle, NULL, NULL, 1); @@ -449,6 +451,28 @@ static int execute(void) return EFI_ST_FAILURE; } + /* Test read_blocks() can read same file data. */ + boottime->set_mem(block, sizeof(block), 0); + /* In the test data, the partition starts at block 1 and the file + hello.txt with the content 'Hello world!' is located at 0x5000 + of the disk. Here we read block 0x27 (offset 0x4e00 of the + partition) and expect the string 'Hello world!' to be at the + start of block. */ + ret = block_io_protocol->read_blocks(block_io_protocol, + block_io_protocol->media->media_id, + (0x5000 >> LB_BLOCK_SIZE) - 1, + block_io_protocol->media->block_size, + block); + if (ret != EFI_SUCCESS) { + efi_st_error("ReadBlocks failed\n"); + return EFI_ST_FAILURE; + } + + if (memcmp(&block[1], buf, 11)) { + efi_st_error("Unexpected block content\n"); + return EFI_ST_FAILURE; + } + #ifdef CONFIG_FAT_WRITE /* Write file */ ret = root->open(root, &file, u"u-boot.txt", EFI_FILE_MODE_READ |