Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7F5BEC433EF for ; Sat, 2 Jul 2022 08:19:07 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 1E0C48440C; Sat, 2 Jul 2022 10:19:05 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.b="eFqv3g9r"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id BBF9B820E1; Sat, 2 Jul 2022 10:19:02 +0200 (CEST) Received: from mout.gmx.net (mout.gmx.net [212.227.15.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id C1781820E1 for ; Sat, 2 Jul 2022 10:18:59 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=xypron.glpk@gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1656749938; bh=90gtq27tkzmz+jQgjwnaSg46Qpsyj5WHrwGF754n0HI=; h=X-UI-Sender-Class:Date:Subject:To:Cc:References:From:In-Reply-To; b=eFqv3g9rKs//C0N+guyn6aGDxQHCgM+IALDAIZge9qXWJRO6v2w1qOl337Pwvq1zE iMWZid+X+KWhKpPJ/s0Cs6mW52J/gqKJPhKb5J/7+fLEVNUqB0FzBV2oRXIKCAF/3/ oAvBDFNYF+1SWhZeWLcZaqdb38yaep800AKAFREI= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.123.94] ([62.143.94.109]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MaJ3n-1oAS3g3wZh-00WCQW; Sat, 02 Jul 2022 10:18:58 +0200 Message-ID: <8d674232-0bab-b76e-1817-761c2cb6e387@gmx.de> Date: Sat, 2 Jul 2022 10:18:51 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.0 Subject: Re: [PATCH v3] EFI: Fix ReadBlocks API reading incorrect sector for UCLASS_PARTITION devices To: Paul Barbieri Cc: AKASHI Takahiro , u-boot@lists.denx.de References: <90538c2d-2fa2-3c7d-7e4f-ec26164cf06e@gmx.de> <20220630110204.19160-1-plb365@gmail.com> Content-Language: en-US From: Heinrich Schuchardt In-Reply-To: <20220630110204.19160-1-plb365@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:ZeM0BxBeKo+PoYW5bOJJJQaZVxmIRqAHLacGBja4BrjpxQ3kzBJ sgLVZ9T4ytEsnGH4pJhQ65XA7rGLDIkCF43w+oSEg6Y19IUywTRqPldRFpXko6nFHFHX4FQ AtRAsiqbC0FaD9xXyyYXpb+1blRMUj2TuZDDDIw0WOV75w5VLQSANmhzEaxBDbISEnTMazy rxl5gRfkBDweBJYh7f+Xg== X-UI-Out-Filterresults: notjunk:1;V03:K0:P+W2bdczhsI=:vLTN7Ymv8r81A6jS6tSGo2 NVWTuYzkSvKvu4ifsGuy3+huLncf8eikPvcBr1zLR15kIZeg3/HHkeaZXEcDFHteRY1NJYchG c7nefspNKuJLoYevW1Yu2FGZIxp64IlLbDxd41mVwwPjHp2d9I8LPD94CiSZFh82S6KLaBih6 q4gIuLWTSGjjjtEuSuTm97xHtjE8THIxfxTOzrxhnnYazMqMMx2BCRLV8yEVPEAVDBD3khJDw NPYkA646tlqXLwbIDLacqGzjRD13I2nRlo/Pjb4GqKM5uIJ3P43zPfygzVM09hbrczjAUbIYC U82xmJaTankYpTgpDvDRJD8v11H1qbfcnxCWrdXZ/gzQbcRyHT+cXHlbmuqcK33quW4ddy/0r OYQBitvnuXKLU8D2sjkyGJrfDD5OvRl18sEIBjo+30h5NvAnVGchJg2NRFlPyDrRI5vpjIzp/ c+Wdd5BtMsAwGyqVrQtXj0GYK4QwNP1arZnxHkqqtfo5EIwsHnZf3aJe0aaPlqvN3NeMfGyca uct1A3Zh1jrDZAjeiCPrz9Vob5BXL8NrxqkDcUniQ4pe5LzNNc4aE0XC7M5Ru8PYKFwSmuV+5 ym4XwBn4bb8jHHcUp/xarom8XzugthV60GbmZSrM3SJJDEjZX8rXbKvIseB3xulop3XnBKkFq tI0DJAxtHaib9D44xYByPcZkouR/Ew3mgcfRcz/3EVewPrhkJ2akz4jcuFucj21valQ9gHdM7 x/ayxUbgizDS6B6hH41zQlEvgLfEZx1HQme1jjvH0Y/WLb5LPpDNfpLNq+8Fyfq3jAux2oZBS AqpMZXYL78yL0UqIvL8y992L++BFql7SnKn3Rcj8aO6LVJA8pH1KRPDhH2VqdwXHCKI+7RjJp vUfC98izVvPXyp7dmk05lXy9owAHd6KadvxfW7AHY4enptjD9GQBlSdtaS6rVmjdV/rQZjjgZ 8vXk4A8n6A3fRgvoSuh3sk5G3uMZ0gyrKMD5qZwuWbGT6vqR5e9sgGy4mmSeozql1F4hMq+vv ucZVcs4AuTqHpno6SC/xVfVaEg2Id6YA+j/C5UauWHL8j9WDNVLKTA3/lYtbPTqNs7x9TKoM8 g4DB1dbBtfD/zduvY/OyELludPdJSGzWK6smImI/szyA0xe0P8O5/q+Wg== 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 On 6/30/22 13:02, Paul Barbieri wrote: > The requsted partition disk sector incorrectly has the parition start > sector added in twice for UCLASS_PARTITION devices. The efi_disk_rw_bloc= ks() > 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_ob= j > structure since disk-uclass.c handles the partition start biasing. Devic= e > 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 > > lib/efi_loader/efi_disk.c | 8 +----- > lib/efi_selftest/efi_selftest_block_device.c | 28 ++++++++++++++++++++ > 2 files changed, 29 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 =3D PARTITI= ON_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_bl= ock_io *this, > diskobj =3D container_of(this, struct efi_disk_obj, ops); > blksz =3D diskobj->media.block_size; > blocks =3D buffer_size / blksz; > - lba +=3D diskobj->offset; > > EFI_PRINT("blocks=3D%x lba=3D%llx blksz=3D%x dir=3D%d\n", > blocks, lba, blksz, direction); > @@ -440,13 +437,11 @@ static efi_status_t efi_disk_add_dev( > > diskobj->dp =3D efi_dp_append_node(dp_parent, node); > efi_free_pool(node); > - diskobj->offset =3D part_info->start; > diskobj->media.last_block =3D part_info->size - 1; > if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) > guid =3D &efi_system_partition_guid; > } else { > diskobj->dp =3D efi_dp_from_part(desc, part); > - diskobj->offset =3D 0; > diskobj->media.last_block =3D desc->lba - 1; > } > diskobj->part =3D part; > @@ -501,12 +496,11 @@ static efi_status_t efi_disk_add_dev( > *disk =3D 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_self= test/efi_selftest_block_device.c > index 60fa655766..d11f673148 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 si= mple > * 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 devic= e. > */ > > #include > @@ -312,6 +313,9 @@ static int execute(void) > char buf[16] __aligned(ARCH_DMA_MINALIGN); > u32 part1_size; > u64 pos; > + char block[2 * (1 << LB_BLOCK_SIZE)]; > + char *block_io_aligned; > + u32 align; > > /* Connect controller to virtual disk */ > ret =3D boottime->connect_controller(disk_handle, NULL, NULL, 1); > @@ -449,6 +453,30 @@ static int execute(void) > return EFI_ST_FAILURE; > } > > + /* Test read_blocks() can read same file data. */ > + boottime->set_mem(block, sizeof(block), 0); > + align =3D block_io_protocol->media->io_align; > + block_io_aligned =3D (char *)(((uintptr_t)block + align-1) & ~(align-1= )); Align is a 32 bit value but the pointer may be a 64 bit address: Running the test on the sandbox results in a crash: align: 512 block: 0x00007ffe78df28b8 block_io_aligned: 0x0000000078df2a00 Segmentation fault (core dumped) IoAlign =3D 0 is an allowable value. Your code would set the last 32 bits to zero in this case. Keep it simple. Just define block_io_align as an LB_BLOCK_SIZE aligned array of size LB_BLOCK_SIZE. Anyway the block driver in this unit test does not care about alignment. read_blocks() only executes memcpy(). Best regards Heinrich > + /* 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 =3D 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_io_aligned); > + if (ret !=3D EFI_SUCCESS) { > + efi_st_error("ReadBlocks failed\n"); > + return EFI_ST_FAILURE; > + } > + > + if (memcmp(&block_io_aligned[1], buf, 11)) { > + efi_st_error("Unexpected block content\n"); > + return EFI_ST_FAILURE; > + } > + > #ifdef CONFIG_FAT_WRITE > /* Write file */ > ret =3D root->open(root, &file, u"u-boot.txt", EFI_FILE_MODE_READ |