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 8488CCCA481 for ; Thu, 30 Jun 2022 08:38:51 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 7336F843DC; Thu, 30 Jun 2022 10:38:49 +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="H4aOnqt6"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E4DC0843EA; Thu, 30 Jun 2022 10:38:48 +0200 (CEST) Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) (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 48FAC842E3 for ; Thu, 30 Jun 2022 10:38:46 +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=1656578324; bh=3G5+apK82Kfa/2UlfJTPhppLMQts6GVM8QXkCDrj7qk=; h=X-UI-Sender-Class:Date:Subject:To:Cc:References:From:In-Reply-To; b=H4aOnqt6qt90jQosGpcYaQr5cUmOpDLad4b/LzxkSNoAMvCdQWm/zqM6ZFf1haqPj 91wn8e7Ckc37HP6aIvmjHAiiKEwJYVaAjY12Eduboani+Zo/6QCnBapiXA13nuBEHc YNUYBZEJ92baNCL2VarFGSPdFy0qEYOkwxpXaAfg= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.123.67] ([62.143.94.109]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1M6lpG-1nziQ63B4I-008FtL; Thu, 30 Jun 2022 10:38:44 +0200 Message-ID: <90538c2d-2fa2-3c7d-7e4f-ec26164cf06e@gmx.de> Date: Thu, 30 Jun 2022 10:38:44 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: [PATCH v2] EFI: Fix ReadBlocks API reading incorrect sector for UCLASS_PARTITION devices Content-Language: en-US To: Paul Barbieri Cc: AKASHI Takahiro , u-boot@lists.denx.de References: <748f33b0-c77f-2c8f-48d1-6ca595420d0d@gmail.com> <20220629174413.2053-1-plb365@gmail.com> From: Heinrich Schuchardt In-Reply-To: <20220629174413.2053-1-plb365@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:4QOc1iIs0pQvN06DXeDk24PDLvQZkleEfGQasg4DozlHeVLU16J l7CSVqp/CP2yV5uuixFF7LSjUhyBeR2ZrCnnxr9O+Bl8WkG6ppUi5jX3ShYRoWJuaTCAyRD pSmUA7YPNyXWIb9iPDrbdkq+QcXVdPSD4a+vFxNNqx8QQPOTf7iKk6iyRnC+khAo6c8/1ce jrptywPd/eZlhrjfOwPFg== X-UI-Out-Filterresults: notjunk:1;V03:K0:O7M5BEqhjWw=:KQcruaEKyAryOA10pDdbyX TC2lmcyBaGZ9pmdXYHQNQyTx0Azz6fm6kg000LnFgpFfT2Z1NZ3PSvn/W3ttNQOYEvb7d21BM wz83OwCF387Xf6lDg5VOGSH5UVp+25TUgNMZpv5h1bKJOSymGZ2ciMcq2dRFlPV5cpvXX428r 4JEppdMl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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/29/22 19:44, 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. > > I verified that the test was using the correct code path by defining DEB= UG > in lib/efi_loader/efi_disk.c This showed that when the test was run, > efi_disk_read_blocks() was executed and it called efi_disk_rw_blocks(). > Adding a little extra debug, I saw that efi_disk_rw_blocks() called dev_= read(). > I also added a debug print in the read_blocks() routine of > efi_selftest_block_device.c. One thing to note, if I moved the read_bloc= ks test > before the file system test, I would see the read_blocks() routine print > in the middle of the efi_disk_rw_blocks() routine. If I put the test aft= er > the file system test, I don't see read_blocks() get called. I assume thi= s > is because dev_read() is able to get the block from the cache. > > > Signed-Off-by: Paul Barbieri > Cc: Heinrich Schuchardt > Cc: AKASHI Takahiro > --- > lib/efi_loader/efi_disk.c | 8 +------ > lib/efi_selftest/efi_selftest_block_device.c | 23 ++++++++++++++++++++ > 2 files changed, 24 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..7b50c829ad 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,25 @@ 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= )); > + ret =3D block_io_protocol->read_blocks(block_io_protocol, > + block_io_protocol->media->media_id, > + (0x5000 >> LB_BLOCK_SIZE) - 1, This code really needs a comment. E.g. In the test data * the partition starts at block 1 * the file hello.txt with 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 the block. Best regards Heinrich > + 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 |