From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 5C4B09419DC for ; Tue, 16 Jan 2024 12:49:08 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=ZwNnHW2X7963Tl/hUmWwiew4f4JA0eLlsRLyh+fmwl4=; c=relaxed/simple; d=groups.io; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From:In-Reply-To:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Language:Content-Type:Content-Transfer-Encoding; s=20140610; t=1705409347; v=1; b=DI0eyU+h76vDjE+8MgHLhxqxh5sDmRq/EZI+bMg/5tEZHmFj99m1tEwwYC7yIi9ha4mLbRYJ wIdvQFEE14JcyTG+Qq5SgBpCOt3A99s6TtA5aSRGd65xJkdp3CEXpAprIm+dBtIXPunyvIMSMXB VDKv1TIZS5FDv4A7kCGn1Za4= X-Received: by 127.0.0.2 with SMTP id aKCfYY7687511x5dReCaQ0GT; Tue, 16 Jan 2024 04:49:07 -0800 X-Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.groups.io with SMTP id smtpd.web10.12721.1705409346227127241 for ; Tue, 16 Jan 2024 04:49:06 -0800 X-Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-179-xjW3DAMAMPWTYl8t9wwuHg-1; Tue, 16 Jan 2024 07:48:58 -0500 X-MC-Unique: xjW3DAMAMPWTYl8t9wwuHg-1 X-Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 476E728C976A for ; Tue, 16 Jan 2024 12:48:58 +0000 (UTC) X-Received: from [10.39.194.248] (unknown [10.39.194.248]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 93838C15E6C; Tue, 16 Jan 2024 12:48:57 +0000 (UTC) Message-ID: <6f16a713-2e13-83ff-c34b-996892d49e1d@redhat.com> Date: Tue, 16 Jan 2024 13:48:56 +0100 MIME-Version: 1.0 Subject: Re: [edk2-devel] [PATCH v2 2/6] OvmfPkg/VirtNorFlashDxe: clarify block write logic & fix shadowbuffer reads To: devel@edk2.groups.io, kraxel@redhat.com Cc: oliver@redhat.com References: <20240115155948.136499-1-kraxel@redhat.com> <20240115155948.136499-3-kraxel@redhat.com> From: "Laszlo Ersek" In-Reply-To: <20240115155948.136499-3-kraxel@redhat.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,lersek@redhat.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: yAwyHI4xFAZjHs7frjFcPTbfx7686176AA= Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=DI0eyU+h; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=redhat.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io On 1/15/24 16:59, Gerd Hoffmann wrote: > Introduce 'Start' and 'End' variables to make it easier to follow the > logic and code flow. Also add a ascii art diagram (based on a > suggestion by Laszlo). > > This also fixes the 'Size' calculation for the NorFlashRead() call. > Without this patch the code will read only one instead of two > P30_MAX_BUFFER_SIZE_IN_BYTES blocks in case '*NumBytes' is smaller than > P30_MAX_BUFFER_SIZE_IN_BYTES but 'Offset + *NumBytes' is not, i.e. the > update range crosses a P30_MAX_BUFFER_SIZE_IN_BYTES boundary. > > Signed-off-by: Gerd Hoffmann > --- > OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c | 35 ++++++++++++++++++++------ > 1 file changed, 27 insertions(+), 8 deletions(-) > > diff --git a/OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c b/OvmfPkg/VirtNorFlas= hDxe/VirtNorFlash.c > index 7f4743b00399..54251633d0ee 100644 > --- a/OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c > +++ b/OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c > @@ -520,6 +520,7 @@ NorFlashWriteSingleBlock ( > UINTN BlockSize; > UINTN BlockAddress; > UINT8 *OrigData; > + UINTN Start, End; > > DEBUG ((DEBUG_BLKIO, "NorFlashWriteSingleBlock(Parameters: Lba=3D%ld, = Offset=3D0x%x, *NumBytes=3D0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumByte= s, Buffer)); > > @@ -555,7 +556,27 @@ NorFlashWriteSingleBlock ( > // To avoid pathological cases were a 2 byte write is disregarded beca= use it > // occurs right at a 128 byte buffered write alignment boundary, permi= t up to > // twice the max buffer size, and perform two writes if needed. > - if ((*NumBytes + (Offset & BOUNDARY_OF_32_WORDS)) <=3D (2 * P30_MAX_BU= FFER_SIZE_IN_BYTES)) { > + // > + // 0 128 256 > + // [----------------|----------------] > + // ^ ^ ^ ^ > + // | | | | > + // | | | End, the next "word" boundary b= eyond > + // | | | the (logical) update > + // | | | > + // | | (Offset & 0x7F) + NumBytes; i.e., the Offset ins= ide > + // | | (or just past) the *double-word* such that Offse= t is > + // | | the *exclusive* end of the (logical) update. Obviously, when I proposed this diagram, I messed up this text. Clearly, there's no better time for making a mistake in a comment than when complaining about comments... :) Two warts: - 0x7F has not been replaced with BOUNDARY_OF_32_WORDS - the uppercase "Offset" identifier (=3D proper noun), from my original proposal, is misleading here. The common noun "offset" is what's need. So I suggest refreshing it as: // | | (Offset & BOUNDARY_OF_32_WORDS) + NumBytes; // | | i.e., the relative offset inside (or just past) // | | the *double-word* such that it is the // | | *exclusive* end of the (logical) update. With that comment update: Reviewed-by: Laszlo Ersek Thanks! Laszlo > + // | | > + // | Offset & BOUNDARY_OF_32_WORDS; i.e., Offset within the= "word"; > + // | this is where the (logical) update is supposed to star= t > + // | > + // Start =3D Offset & ~BOUNDARY_OF_32_WORDS; i.e., Offset truncated= to "word" boundary > + > + Start =3D Offset & ~BOUNDARY_OF_32_WORDS; > + End =3D ALIGN_VALUE (Offset + *NumBytes, P30_MAX_BUFFER_SIZE_IN_BYTE= S); > + > + if ((End - Start) <=3D (2 * P30_MAX_BUFFER_SIZE_IN_BYTES)) { > // Check to see if we need to erase before programming the data into= NOR. > // If the destination bits are only changing from 1s to 0s we can ju= st write. > // After a block is erased all bits in the block is set to 1. > @@ -565,8 +586,8 @@ NorFlashWriteSingleBlock ( > Status =3D NorFlashRead ( > Instance, > Lba, > - Offset & ~BOUNDARY_OF_32_WORDS, > - (*NumBytes | BOUNDARY_OF_32_WORDS) + 1, > + Start, > + End - Start, > Instance->ShadowBuffer > ); > if (EFI_ERROR (Status)) { > @@ -601,7 +622,7 @@ NorFlashWriteSingleBlock ( > > Status =3D NorFlashWriteBuffer ( > Instance, > - BlockAddress + (Offset & ~BOUNDARY_OF_32_WORDS), > + BlockAddress + Start, > P30_MAX_BUFFER_SIZE_IN_BYTES, > Instance->ShadowBuffer > ); > @@ -609,12 +630,10 @@ NorFlashWriteSingleBlock ( > goto Exit; > } > > - if ((*NumBytes + (Offset & BOUNDARY_OF_32_WORDS)) > P30_MAX_BUFFER_S= IZE_IN_BYTES) { > - BlockAddress +=3D P30_MAX_BUFFER_SIZE_IN_BYTES; > - > + if ((End - Start) > P30_MAX_BUFFER_SIZE_IN_BYTES) { > Status =3D NorFlashWriteBuffer ( > Instance, > - BlockAddress + (Offset & ~BOUNDARY_OF_32_WORDS), > + BlockAddress + Start + P30_MAX_BUFFER_SIZE_IN_BYTES, > P30_MAX_BUFFER_SIZE_IN_BYTES, > Instance->ShadowBuffer + P30_MAX_BUFFER_SIZE_IN_BYTES > ); -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#113893): https://edk2.groups.io/g/devel/message/113893 Mute This Topic: https://groups.io/mt/103741669/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/leave/12367111/7686176/19134562= 12/xyzzy [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-