From nobody Fri Dec 19 22:04:09 2025 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+113916+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+113916+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1705425073; cv=none; d=zohomail.com; s=zohoarc; b=QXb3UYlXfG6XgP3ymB1FiDJvNvZw3jRUQEnTu6+Z+ydEkM1+Nc7XuScyZR0AQGr4vmqwj7If+ilvdamdn+a7VdKbzH9N9Q2vzn2yu1nTmMBfcp689Vz6upFxNeMqYmpvaSmmlC1SJISIW+SuKtdMw+crLj7N7yfa9crgT9yEUDQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1705425073; 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=Oe7dNLwfwj+2aSh6T+XG7NQH1wmGR4M+eC7cnP16y1A=; b=i8Y1pqlh0TpzsXRDI1A+SRsXIyFvSG8Ndpbt83eOYJm6VzDkv7vs15OQH/OUwVNT2KCGJNv555jzLxaQ6iAlFgq8j47+8S4BWCbU1pDfBbFwtgUEAPIrNyLa9LAFet+ad0pbn9cEgFNA+P+RLsGiFbIKtEdC6oYWnBWrwnvMXP8= 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+113916+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 1705425073027646.7689612898291; Tue, 16 Jan 2024 09:11:13 -0800 (PST) Return-Path: DKIM-Signature: a=rsa-sha256; bh=tPscBqxnXh+9GaXJp++u5QU6i/kILyY+iYBnZjAj85Y=; 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=1705425072; v=1; b=kA2+E8OomoZsFebtFCeVITipoKFllkBidu0Wu2E9a+yvjt8TQtj2M32WBglyDvhwFQRIe04Q dc3wwF9RxRUVCsRnVHHSYxP1m0A+j9DEkppS1LNmO+weqThL2zG78+xJZMXQbTNUgWWBA/lKlWn NWqIZIMoT14z4hJ9gLiPq5rk= X-Received: by 127.0.0.2 with SMTP id b3gCYY1788612xQ7xmshGfRN; Tue, 16 Jan 2024 09:11:12 -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.web11.22109.1705425071617794164 for ; Tue, 16 Jan 2024 09:11:11 -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-455-kAQBXSiSNwK2ytT8SNAEag-1; Tue, 16 Jan 2024 12:11:09 -0500 X-MC-Unique: kAQBXSiSNwK2ytT8SNAEag-1 X-Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 8C72B28240F2 for ; Tue, 16 Jan 2024 17:11:08 +0000 (UTC) X-Received: from dobby.home.kraxel.org (unknown [10.39.192.35]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DD47340C6EB9; Tue, 16 Jan 2024 17:11:07 +0000 (UTC) X-Received: by dobby.home.kraxel.org (Postfix, from userid 1000) id 5E498AEDBC; Tue, 16 Jan 2024 18:11:05 +0100 (CET) From: "Gerd Hoffmann" To: devel@edk2.groups.io Cc: oliver@redhat.com, Gerd Hoffmann , Laszlo Ersek Subject: [edk2-devel] [PATCH v3 2/6] OvmfPkg/VirtNorFlashDxe: clarify block write logic & fix shadowbuffer reads Date: Tue, 16 Jan 2024 18:11:01 +0100 Message-ID: <20240116171105.37831-3-kraxel@redhat.com> In-Reply-To: <20240116171105.37831-1-kraxel@redhat.com> References: <20240116171105.37831-1-kraxel@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 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: e70uSvWlaW4ZLyFfZR6iCmlTx1787277AA= Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1705425073444100005 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 | 36 ++++++++++++++++++++------ 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c b/OvmfPkg/VirtNorFlashD= xe/VirtNorFlash.c index 7f4743b00399..88a4d2c23fc0 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,28 @@ 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 & 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. + // | | + // | 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 +587,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 +623,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 +631,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 (#113916): https://edk2.groups.io/g/devel/message/113916 Mute This Topic: https://groups.io/mt/103766774/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-