From nobody Tue Aug 25 01:34:08 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+113833+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+113833+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1705334399; cv=none; d=zohomail.com; s=zohoarc; b=kRpFATthw1pNSwKEjdEYFljXFPMoW1N7aCDMO/mju4ygKleHgZoc+HahZw+zV5j8cqJGDqu9ul43a7pj9BTiDdxRzHdecAQ3SZQraQ5ILJTtDmyFlVra5u3Urd1s31MVHkS2WOuErcqg9cfB+QkG5GwDay/+/ZyyVHiIcco68Ek= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1705334399; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=dGvEj4x11imUGPdcGotsajfxadcKACWDIXehfCvQq6A=; b=Azsq616uP3cGub1SFmOLodXN6eUVeqd7Sq2BkMhYC+IB60WCuSDyh6th6jSQ+RTrmx0Sh4OJs9Johanlb58tiEwgIDG1yEGaTXgcjG2duB5SqH40fTi+KWwznc9Ka5Xk2Un5xy9R9eS8dv/oMCDV729G+JwZMIpwyzSMIyVyanU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+113833+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1705334399894628.0945282515396; Mon, 15 Jan 2024 07:59:59 -0800 (PST) Return-Path: DKIM-Signature: a=rsa-sha256; bh=StK1Aav6+javWogciEZFIzmL9JTWjR22lHtg+tUuPSE=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding:Content-Type; s=20140610; t=1705334399; v=1; b=XApZ+ILy/jO3jYH3/vujQ+4nL0d2fnT+4Vsc5XNMreZMxbrjgQns9DBUb96XShll89lvEIXm mgYwCeIzSP8fH62UvINnltM/BCCQZpxddb2BtuZH//SB/PN1lweysJ1QWQw043rNvj7Qi0uSbDd RDJh1eHB02YIuqChzMxleGPo= X-Received: by 127.0.0.2 with SMTP id Dd97YY1788612x9Yi2g1qQHQ; Mon, 15 Jan 2024 07:59:59 -0800 X-Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.groups.io with SMTP id smtpd.web11.84616.1705334398958410449 for ; Mon, 15 Jan 2024 07:59:59 -0800 X-Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-32-DqWPYZWdNIWSV3roUtD8KQ-1; Mon, 15 Jan 2024 10:59:51 -0500 X-MC-Unique: DqWPYZWdNIWSV3roUtD8KQ-1 X-Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (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 74CD5863064 for ; Mon, 15 Jan 2024 15:59:51 +0000 (UTC) X-Received: from dobby.home.kraxel.org (unknown [10.39.193.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 25E5F492BFA; Mon, 15 Jan 2024 15:59:51 +0000 (UTC) X-Received: by dobby.home.kraxel.org (Postfix, from userid 1000) id D35BBAC986; Mon, 15 Jan 2024 16:59:48 +0100 (CET) From: "Gerd Hoffmann" To: devel@edk2.groups.io Cc: oliver@redhat.com, Gerd Hoffmann Subject: [edk2-devel] [PATCH v2 2/6] OvmfPkg/VirtNorFlashDxe: clarify block write logic & fix shadowbuffer reads Date: Mon, 15 Jan 2024 16:59:44 +0100 Message-ID: <20240115155948.136499-3-kraxel@redhat.com> In-Reply-To: <20240115155948.136499-1-kraxel@redhat.com> References: <20240115155948.136499-1-kraxel@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 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,kraxel@redhat.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: 1zsuIS9Z86nHplmEXreaOawwx1787277AA= Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1705334401887100027 Content-Type: text/plain; charset="utf-8"; x-default="true" 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 Reviewed-by: Laszlo Ersek --- OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c | 35 ++++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c b/OvmfPkg/VirtNorFlashD= xe/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; =20 DEBUG ((DEBUG_BLKIO, "NorFlashWriteSingleBlock(Parameters: Lba=3D%ld, Of= fset=3D0x%x, *NumBytes=3D0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes,= Buffer)); =20 @@ -555,7 +556,27 @@ NorFlashWriteSingleBlock ( // To avoid pathological cases were a 2 byte write is disregarded becaus= e it // occurs right at a 128 byte buffered write alignment boundary, permit = up to // twice the max buffer size, and perform two writes if needed. - if ((*NumBytes + (Offset & BOUNDARY_OF_32_WORDS)) <=3D (2 * P30_MAX_BUFF= ER_SIZE_IN_BYTES)) { + // + // 0 128 256 + // [----------------|----------------] + // ^ ^ ^ ^ + // | | | | + // | | | End, the next "word" boundary bey= ond + // | | | the (logical) update + // | | | + // | | (Offset & 0x7F) + NumBytes; i.e., the Offset inside + // | | (or just past) the *double-word* such that Offset = is + // | | the *exclusive* end of the (logical) update. + // | | + // | Offset & BOUNDARY_OF_32_WORDS; i.e., Offset within the "= word"; + // | this is where the (logical) update is supposed to start + // | + // Start =3D Offset & ~BOUNDARY_OF_32_WORDS; i.e., Offset truncated t= o "word" boundary + + Start =3D Offset & ~BOUNDARY_OF_32_WORDS; + End =3D ALIGN_VALUE (Offset + *NumBytes, P30_MAX_BUFFER_SIZE_IN_BYTES); + + if ((End - Start) <=3D (2 * P30_MAX_BUFFER_SIZE_IN_BYTES)) { // Check to see if we need to erase before programming the data into N= OR. // If the destination bits are only changing from 1s to 0s we can just= 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 ( =20 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; } =20 - if ((*NumBytes + (Offset & BOUNDARY_OF_32_WORDS)) > P30_MAX_BUFFER_SIZ= E_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 ); --=20 2.43.0 -=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 (#113833): https://edk2.groups.io/g/devel/message/113833 Mute This Topic: https://groups.io/mt/103741669/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-