From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from fb.mail.gandi.net (spool8.mail.gandi.net [217.70.178.217]) by nmboxes7-ms7.sd4.0x35.net (Postfix) with ESMTPS id C9C0961930 for ; Tue, 16 Jan 2024 05:26:40 +0000 (UTC) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by fb.mail.gandi.net (Postfix) with ESMTPS id 57FF860377 for ; Tue, 16 Jan 2024 05:26:40 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=fI3QN0UqY0PU+FjpaNPyIoNIJpY3rgpoT6ujvRdbTDk=; 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=1705382799; v=1; b=w4vOeSG2hbgFGcNgL2t/WkbCG8a0k3OlhLCVjsIScxO7Mlu5QFTdN6RhI3uBc7AzLVCQbzGp tmNQsMkMiRm1ssrhAWVRMB0KacivgH51pBu/mUGR9pqjkp6KYqsebD4gFXhTil+54omzJcdWcOa 3RIKEE/TQ8iIT6yOdRqTYc4I= X-Received: by 127.0.0.2 with SMTP id qJf0YY7687511xScV0JGmesf; Mon, 15 Jan 2024 21:26:39 -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: 7Z49qayXshfB5VL5TLQ8uRz2x7686176AA= Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-GND-Status: LEGIT Authentication-Results: fb.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=w4vOeSG2; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=redhat.com (policy=none); spf=pass (fb.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io 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/VirtNorFlashDxe/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=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer)); @@ -555,7 +556,27 @@ NorFlashWriteSingleBlock ( // To avoid pathological cases were a 2 byte write is disregarded because 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)) <= (2 * P30_MAX_BUFFER_SIZE_IN_BYTES)) { + // + // 0 128 256 + // [----------------|----------------] + // ^ ^ ^ ^ + // | | | | + // | | | End, the next "word" boundary beyond + // | | | 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 = Offset & ~BOUNDARY_OF_32_WORDS; i.e., Offset truncated to "word" boundary + + Start = Offset & ~BOUNDARY_OF_32_WORDS; + End = ALIGN_VALUE (Offset + *NumBytes, P30_MAX_BUFFER_SIZE_IN_BYTES); + + if ((End - Start) <= (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 just write. // After a block is erased all bits in the block is set to 1. @@ -565,8 +586,8 @@ NorFlashWriteSingleBlock ( Status = 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 = 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_SIZE_IN_BYTES) { - BlockAddress += P30_MAX_BUFFER_SIZE_IN_BYTES; - + if ((End - Start) > P30_MAX_BUFFER_SIZE_IN_BYTES) { Status = 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 ); -- 2.43.0 -=-=-=-=-=-=-=-=-=-=-=- 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/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-