From nobody Mon Apr 29 02:13:07 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1487277707303881.4509983218148; Thu, 16 Feb 2017 12:41:47 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 0B20D820FD; Thu, 16 Feb 2017 12:41:46 -0800 (PST) Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 32B9781F3A for ; Thu, 16 Feb 2017 12:41:44 -0800 (PST) Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C7288D1FD8; Thu, 16 Feb 2017 20:41:44 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-116-72.phx2.redhat.com [10.3.116.72]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1GKfgnY011444; Thu, 16 Feb 2017 15:41:44 -0500 X-Original-To: edk2-devel@ml01.01.org From: Laszlo Ersek To: edk2-devel-01 Date: Thu, 16 Feb 2017 21:41:33 +0100 Message-Id: <20170216204137.30221-2-lersek@redhat.com> In-Reply-To: <20170216204137.30221-1-lersek@redhat.com> References: <20170216204137.30221-1-lersek@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 16 Feb 2017 20:41:44 +0000 (UTC) Subject: [edk2] [PATCH 1/5] OvmfPkg/AcpiPlatformDxe: prepare for QEMU_LOADER_WRITE_POINTER definitions X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jordan Justen MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" No functional changes in this patch, just prepare the grounds with some reformatting (trailing comma after the last enumeration constant, horizontal whitespace insertion) so that the next patch can be cleaner. Cc: Jordan Justen Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D359 Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek --- OvmfPkg/AcpiPlatformDxe/QemuLoader.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/OvmfPkg/AcpiPlatformDxe/QemuLoader.h b/OvmfPkg/AcpiPlatformDxe= /QemuLoader.h index 84dec06422d6..b29944378d76 100644 --- a/OvmfPkg/AcpiPlatformDxe/QemuLoader.h +++ b/OvmfPkg/AcpiPlatformDxe/QemuLoader.h @@ -25,11 +25,11 @@ #define QEMU_LOADER_FNAME_SIZE QEMU_FW_CFG_FNAME_SIZE =20 typedef enum { QemuLoaderCmdAllocate =3D 1, QemuLoaderCmdAddPointer, - QemuLoaderCmdAddChecksum + QemuLoaderCmdAddChecksum, } QEMU_LOADER_COMMAND_TYPE; =20 typedef enum { QemuLoaderAllocHigh =3D 1, QemuLoaderAllocFSeg @@ -73,14 +73,14 @@ typedef struct { } QEMU_LOADER_ADD_CHECKSUM; =20 typedef struct { UINT32 Type; // QEMU_LOADER_COMMAND_TYPE val= ues union { - QEMU_LOADER_ALLOCATE Allocate; - QEMU_LOADER_ADD_POINTER AddPointer; - QEMU_LOADER_ADD_CHECKSUM AddChecksum; - UINT8 Padding[124]; + QEMU_LOADER_ALLOCATE Allocate; + QEMU_LOADER_ADD_POINTER AddPointer; + QEMU_LOADER_ADD_CHECKSUM AddChecksum; + UINT8 Padding[124]; } Command; } QEMU_LOADER_ENTRY; #pragma pack () =20 #endif --=20 2.9.3 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Mon Apr 29 02:13:07 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 148727771343554.258020372621445; Thu, 16 Feb 2017 12:41:53 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 3BF39820FC; Thu, 16 Feb 2017 12:41:52 -0800 (PST) Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 6246B81F3A for ; Thu, 16 Feb 2017 12:41:50 -0800 (PST) Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F29D761BA8; Thu, 16 Feb 2017 20:41:50 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-116-72.phx2.redhat.com [10.3.116.72]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1GKfgnZ011444; Thu, 16 Feb 2017 15:41:50 -0500 X-Original-To: edk2-devel@ml01.01.org From: Laszlo Ersek To: edk2-devel-01 Date: Thu, 16 Feb 2017 21:41:34 +0100 Message-Id: <20170216204137.30221-3-lersek@redhat.com> In-Reply-To: <20170216204137.30221-1-lersek@redhat.com> References: <20170216204137.30221-1-lersek@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Thu, 16 Feb 2017 20:41:51 +0000 (UTC) Subject: [edk2] [PATCH 2/5] OvmfPkg/AcpiPlatformDxe: add QEMU_LOADER_WRITE_POINTER definitions X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jordan Justen MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Cc: Jordan Justen Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D359 Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek --- OvmfPkg/AcpiPlatformDxe/QemuLoader.h | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/OvmfPkg/AcpiPlatformDxe/QemuLoader.h b/OvmfPkg/AcpiPlatformDxe= /QemuLoader.h index b29944378d76..437776d86d9a 100644 --- a/OvmfPkg/AcpiPlatformDxe/QemuLoader.h +++ b/OvmfPkg/AcpiPlatformDxe/QemuLoader.h @@ -26,10 +26,11 @@ =20 typedef enum { QemuLoaderCmdAllocate =3D 1, QemuLoaderCmdAddPointer, QemuLoaderCmdAddChecksum, + QemuLoaderCmdWritePointer, } QEMU_LOADER_COMMAND_TYPE; =20 typedef enum { QemuLoaderAllocHigh =3D 1, QemuLoaderAllocFSeg @@ -70,16 +71,41 @@ typedef struct { UINT32 ResultOffset; UINT32 Start; UINT32 Length; } QEMU_LOADER_ADD_CHECKSUM; =20 +// +// QemuLoaderCmdWritePointer: the bytes at +// [PointerOffset..PointerOffset+PointerSize) in the writeable fw_cfg file +// PointerFile are to receive the absolute address of PointeeFile, as allo= cated +// and downloaded by the firmware, incremented by the value of PointeeOffs= et. +// Store the sum of (a) the base address of where PointeeFile's contents h= ave +// been placed (when QemuLoaderCmdAllocate has been executed for PointeeFi= le) +// and (b) PointeeOffset, to this portion of PointerFile. +// +// This command is similar to QemuLoaderCmdAddPointer; the difference is t= hat +// the "pointer to patch" does not exist in guest-physical address space, = only +// in "fw_cfg file space". In addition, the "pointer to patch" is not +// initialized by QEMU in-place with a possibly nonzero offset value: the +// relative offset into PointeeFile comes from the explicit PointeeOffset +// field. +// +typedef struct { + UINT8 PointerFile[QEMU_LOADER_FNAME_SIZE]; // NUL-terminated + UINT8 PointeeFile[QEMU_LOADER_FNAME_SIZE]; // NUL-terminated + UINT32 PointerOffset; + UINT32 PointeeOffset; + UINT8 PointerSize; // one of 1, 2, 4, 8 +} QEMU_LOADER_WRITE_POINTER; + typedef struct { UINT32 Type; // QEMU_LOADER_COMMAND_TYPE val= ues union { QEMU_LOADER_ALLOCATE Allocate; QEMU_LOADER_ADD_POINTER AddPointer; QEMU_LOADER_ADD_CHECKSUM AddChecksum; + QEMU_LOADER_WRITE_POINTER WritePointer; UINT8 Padding[124]; } Command; } QEMU_LOADER_ENTRY; #pragma pack () =20 --=20 2.9.3 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Mon Apr 29 02:13:07 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1487277715634322.047804943192; Thu, 16 Feb 2017 12:41:55 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 6BE5682108; Thu, 16 Feb 2017 12:41:52 -0800 (PST) Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id B687A81F3A for ; Thu, 16 Feb 2017 12:41:51 -0800 (PST) Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 56403D1FEF; Thu, 16 Feb 2017 20:41:52 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-116-72.phx2.redhat.com [10.3.116.72]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1GKfgna011444; Thu, 16 Feb 2017 15:41:51 -0500 X-Original-To: edk2-devel@ml01.01.org From: Laszlo Ersek To: edk2-devel-01 Date: Thu, 16 Feb 2017 21:41:35 +0100 Message-Id: <20170216204137.30221-4-lersek@redhat.com> In-Reply-To: <20170216204137.30221-1-lersek@redhat.com> References: <20170216204137.30221-1-lersek@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 16 Feb 2017 20:41:52 +0000 (UTC) Subject: [edk2] [PATCH 3/5] OvmfPkg/AcpiPlatformDxe: rewrap license block in "QemuFwCfgAcpi.c" X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jordan Justen MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The longest line is currently 84 characters long. Cc: Jordan Justen Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D359 Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek --- OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c b/OvmfPkg/AcpiPlatform= Dxe/QemuFwCfgAcpi.c index deaf14c6b0dc..404589cad0b7 100644 --- a/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c +++ b/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c @@ -2,17 +2,17 @@ OVMF ACPI support using QEMU's fw-cfg interface =20 Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.
Copyright (C) 2012-2014, Red Hat, Inc. =20 - This program and the accompanying materials - are licensed and made available under the terms and conditions of the BS= D License - which accompanies this distribution. The full text of the license may b= e found at + This program and the accompanying materials are licensed and made availa= ble + under the terms and conditions of the BSD License which accompanies this + distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php =20 - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, - WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMP= LIED. + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WI= THOUT + WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. =20 **/ =20 #include "AcpiPlatform.h" #include "QemuLoader.h" --=20 2.9.3 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Mon Apr 29 02:13:07 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1487277716908620.521795194854; Thu, 16 Feb 2017 12:41:56 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id A5888820FA; Thu, 16 Feb 2017 12:41:55 -0800 (PST) Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id E0A338210B for ; Thu, 16 Feb 2017 12:41:52 -0800 (PST) Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 79AF07FB70; Thu, 16 Feb 2017 20:41:53 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-116-72.phx2.redhat.com [10.3.116.72]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1GKfgnb011444; Thu, 16 Feb 2017 15:41:52 -0500 X-Original-To: edk2-devel@ml01.01.org From: Laszlo Ersek To: edk2-devel-01 Date: Thu, 16 Feb 2017 21:41:36 +0100 Message-Id: <20170216204137.30221-5-lersek@redhat.com> In-Reply-To: <20170216204137.30221-1-lersek@redhat.com> References: <20170216204137.30221-1-lersek@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Thu, 16 Feb 2017 20:41:53 +0000 (UTC) Subject: [edk2] [PATCH 4/5] OvmfPkg/AcpiPlatformDxe: implement the QEMU_LOADER_WRITE_POINTER command X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jordan Justen MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The QEMU_LOADER_WRITE_POINTER command instructs the firmware to write the address of a field within a previously allocated/downloaded fw_cfg blob into another (writeable) fw_cfg file at a specific offset. Put differently, QEMU_LOADER_WRITE_POINTER propagates, to QEMU, the address that QEMU_LOADER_ALLOCATE placed the designated fw_cfg blob at, as adjusted for the given field inside the allocated blob. The implementation is similar to that of QEMU_LOADER_ADD_POINTER. Since here we "patch" a pointer object in "fw_cfg file space", not guest memory space, we utilize the QemuFwCfgSkipBytes() and QemuFwCfgWriteBytes() APIs completed in commit range 465663e9f128..7fcb73541299. An interesting aspect is that QEMU_LOADER_WRITE_POINTER creates a host-level reference to a guest memory location. Therefore, if we fail to process the linker/loader script for any reason, we have to clear out those references first, before we release the guest memory allocations in response to the error. Cc: Jordan Justen Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D359 Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek --- OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c | 171 +++++++++++++++++++- 1 file changed, 168 insertions(+), 3 deletions(-) diff --git a/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c b/OvmfPkg/AcpiPlatform= Dxe/QemuFwCfgAcpi.c index 404589cad0b7..de827c2df204 100644 --- a/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c +++ b/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c @@ -350,10 +350,147 @@ ProcessCmdAddChecksum ( AddChecksum->ResultOffset, AddChecksum->Start, AddChecksum->Length)); return EFI_SUCCESS; } =20 =20 +/** + Process a QEMU_LOADER_WRITE_POINTER command. + + @param[in] WritePointer The QEMU_LOADER_WRITE_POINTER command to proce= ss. + + @param[in] Tracker The ORDERED_COLLECTION tracking the BLOB user + structures created thus far. + + @retval EFI_PROTOCOL_ERROR Malformed fw_cfg file name(s) have been foun= d in + WritePointer. Or, the WritePointer command + references a file unknown to Tracker or the + fw_cfg directory. Or, the pointer object to + rewrite has invalid location, size, or initi= al + relative value. Or, the pointer value to sto= re + does not fit in the given pointer size. + + @retval EFI_SUCCESS The pointer object inside the writeable fw_c= fg + file has been written. +**/ +STATIC +EFI_STATUS +ProcessCmdWritePointer ( + IN CONST QEMU_LOADER_WRITE_POINTER *WritePointer, + IN CONST ORDERED_COLLECTION *Tracker + ) +{ + RETURN_STATUS Status; + FIRMWARE_CONFIG_ITEM PointerItem; + UINTN PointerItemSize; + ORDERED_COLLECTION_ENTRY *PointeeEntry; + BLOB *PointeeBlob; + UINT64 PointerValue; + + if (WritePointer->PointerFile[QEMU_LOADER_FNAME_SIZE - 1] !=3D '\0' || + WritePointer->PointeeFile[QEMU_LOADER_FNAME_SIZE - 1] !=3D '\0') { + DEBUG ((DEBUG_ERROR, "%a: malformed file name\n", __FUNCTION__)); + return EFI_PROTOCOL_ERROR; + } + + Status =3D QemuFwCfgFindFile ((CONST CHAR8 *)WritePointer->PointerFile, + &PointerItem, &PointerItemSize); + PointeeEntry =3D OrderedCollectionFind (Tracker, WritePointer->PointeeFi= le); + if (RETURN_ERROR (Status) || PointeeEntry =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, + "%a: invalid fw_cfg file or blob reference \"%a\" / \"%a\"\n", + __FUNCTION__, WritePointer->PointerFile, WritePointer->PointeeFile)); + return EFI_PROTOCOL_ERROR; + } + + if ((WritePointer->PointerSize !=3D 1 && WritePointer->PointerSize !=3D = 2 && + WritePointer->PointerSize !=3D 4 && WritePointer->PointerSize !=3D = 8) || + (PointerItemSize < WritePointer->PointerSize) || + (PointerItemSize - WritePointer->PointerSize < + WritePointer->PointerOffset)) { + DEBUG ((DEBUG_ERROR, "%a: invalid pointer location or size in \"%a\"\n= ", + __FUNCTION__, WritePointer->PointerFile)); + return EFI_PROTOCOL_ERROR; + } + + PointeeBlob =3D OrderedCollectionUserStruct (PointeeEntry); + PointerValue =3D WritePointer->PointeeOffset; + if (PointerValue >=3D PointeeBlob->Size) { + DEBUG ((DEBUG_ERROR, "%a: invalid PointeeOffset\n", __FUNCTION__)); + return EFI_PROTOCOL_ERROR; + } + + // + // The memory allocation system ensures that the address of the byte pas= t the + // last byte of any allocated object is expressible (no wraparound). + // + ASSERT ((UINTN)PointeeBlob->Base <=3D MAX_ADDRESS - PointeeBlob->Size); + + PointerValue +=3D (UINT64)(UINTN)PointeeBlob->Base; + if (RShiftU64 ( + RShiftU64 (PointerValue, WritePointer->PointerSize * 8 - 1), 1) != =3D 0) { + DEBUG ((DEBUG_ERROR, "%a: pointer value unrepresentable in \"%a\"\n", + __FUNCTION__, WritePointer->PointerFile)); + return EFI_PROTOCOL_ERROR; + } + + QemuFwCfgSelectItem (PointerItem); + QemuFwCfgSkipBytes (WritePointer->PointerOffset); + QemuFwCfgWriteBytes (WritePointer->PointerSize, &PointerValue); + + // + // Because QEMU has now learned PointeeBlob->Base, we must mark PointeeB= lob + // as unreleasable, for the case when the whole linker/loader script is + // handled successfully. + // + PointeeBlob->HostsOnlyTableData =3D FALSE; + + DEBUG ((DEBUG_VERBOSE, "%a: PointerFile=3D\"%a\" PointeeFile=3D\"%a\" " + "PointerOffset=3D0x%x PointeeOffset=3D0x%x PointerSize=3D%d\n", __FUNC= TION__, + WritePointer->PointerFile, WritePointer->PointeeFile, + WritePointer->PointerOffset, WritePointer->PointeeOffset, + WritePointer->PointerSize)); + return EFI_SUCCESS; +} + + +/** + Undo a QEMU_LOADER_WRITE_POINTER command. + + This function revokes (zeroes out) a guest memory reference communicated= to + QEMU earlier. The caller is responsible for invoking this function only = on + such QEMU_LOADER_WRITE_POINTER commands that have been successfully proc= essed + by ProcessCmdWritePointer(). + + @param[in] WritePointer The QEMU_LOADER_WRITE_POINTER command to undo. +**/ +STATIC +VOID +UndoCmdWritePointer ( + IN CONST QEMU_LOADER_WRITE_POINTER *WritePointer + ) +{ + RETURN_STATUS Status; + FIRMWARE_CONFIG_ITEM PointerItem; + UINTN PointerItemSize; + UINT64 PointerValue; + + Status =3D QemuFwCfgFindFile ((CONST CHAR8 *)WritePointer->PointerFile, + &PointerItem, &PointerItemSize); + ASSERT_RETURN_ERROR (Status); + + PointerValue =3D 0; + QemuFwCfgSelectItem (PointerItem); + QemuFwCfgSkipBytes (WritePointer->PointerOffset); + QemuFwCfgWriteBytes (WritePointer->PointerSize, &PointerValue); + + DEBUG ((DEBUG_VERBOSE, + "%a: PointerFile=3D\"%a\" PointerOffset=3D0x%x PointerSize=3D%d\n", __= FUNCTION__, + WritePointer->PointerFile, WritePointer->PointerOffset, + WritePointer->PointerSize)); +} + + // // We'll be saving the keys of installed tables so that we can roll them b= ack // in case of failure. 128 tables should be enough for anyone (TM). // #define INSTALLED_TABLES_MAX 128 @@ -559,10 +696,11 @@ InstallQemuFwCfgTables ( EFI_STATUS Status; FIRMWARE_CONFIG_ITEM FwCfgItem; UINTN FwCfgSize; QEMU_LOADER_ENTRY *LoaderStart; CONST QEMU_LOADER_ENTRY *LoaderEntry, *LoaderEnd; + CONST QEMU_LOADER_ENTRY *WritePointerSubsetEnd; ORIGINAL_ATTRIBUTES *OriginalPciAttributes; UINTN OriginalPciAttributesCount; ORDERED_COLLECTION *Tracker; UINTN *InstalledKey; INT32 Installed; @@ -595,10 +733,15 @@ InstallQemuFwCfgTables ( } =20 // // first pass: process the commands // + // "WritePointerSubsetEnd" points one past the last successful + // QEMU_LOADER_WRITE_POINTER command. Now when we're about to start the = first + // pass, no such command has been encountered yet. + // + WritePointerSubsetEnd =3D LoaderStart; for (LoaderEntry =3D LoaderStart; LoaderEntry < LoaderEnd; ++LoaderEntry= ) { switch (LoaderEntry->Type) { case QemuLoaderCmdAllocate: Status =3D ProcessCmdAllocate (&LoaderEntry->Command.Allocate, Track= er); break; @@ -611,25 +754,33 @@ InstallQemuFwCfgTables ( case QemuLoaderCmdAddChecksum: Status =3D ProcessCmdAddChecksum (&LoaderEntry->Command.AddChecksum, Tracker); break; =20 + case QemuLoaderCmdWritePointer: + Status =3D ProcessCmdWritePointer (&LoaderEntry->Command.WritePoin= ter, + Tracker); + if (!EFI_ERROR (Status)) { + WritePointerSubsetEnd =3D LoaderEntry + 1; + } + break; + default: DEBUG ((EFI_D_VERBOSE, "%a: unknown loader command: 0x%x\n", __FUNCTION__, LoaderEntry->Type)); break; } =20 if (EFI_ERROR (Status)) { - goto FreeTracker; + goto RollbackWritePointersAndFreeTracker; } } =20 InstalledKey =3D AllocatePool (INSTALLED_TABLES_MAX * sizeof *InstalledK= ey); if (InstalledKey =3D=3D NULL) { Status =3D EFI_OUT_OF_RESOURCES; - goto FreeTracker; + goto RollbackWritePointersAndFreeTracker; } =20 // // second pass: identify and install ACPI tables // @@ -656,11 +807,25 @@ InstallQemuFwCfgTables ( DEBUG ((EFI_D_INFO, "%a: installed %d tables\n", __FUNCTION__, Install= ed)); } =20 FreePool (InstalledKey); =20 -FreeTracker: +RollbackWritePointersAndFreeTracker: + // + // In case of failure, revoke any allocation addresses that were communi= cated + // to QEMU previously, before we release all the blobs. + // + if (EFI_ERROR (Status)) { + LoaderEntry =3D WritePointerSubsetEnd; + while (LoaderEntry > LoaderStart) { + --LoaderEntry; + if (LoaderEntry->Type =3D=3D QemuLoaderCmdWritePointer) { + UndoCmdWritePointer (&LoaderEntry->Command.WritePointer); + } + } + } + // // Tear down the tracker infrastructure. Each fw_cfg blob will be left in // place only if we're exiting with success and the blob hosts data that= is // not directly part of some ACPI table. // --=20 2.9.3 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Mon Apr 29 02:13:07 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1487277720258299.4887667136288; Thu, 16 Feb 2017 12:42:00 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id E32C98211A; Thu, 16 Feb 2017 12:41:55 -0800 (PST) Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 3A96F81F3A for ; Thu, 16 Feb 2017 12:41:54 -0800 (PST) Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CBA0D7E9E0; Thu, 16 Feb 2017 20:41:54 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-116-72.phx2.redhat.com [10.3.116.72]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1GKfgnc011444; Thu, 16 Feb 2017 15:41:53 -0500 X-Original-To: edk2-devel@ml01.01.org From: Laszlo Ersek To: edk2-devel-01 Date: Thu, 16 Feb 2017 21:41:37 +0100 Message-Id: <20170216204137.30221-6-lersek@redhat.com> In-Reply-To: <20170216204137.30221-1-lersek@redhat.com> References: <20170216204137.30221-1-lersek@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 16 Feb 2017 20:41:54 +0000 (UTC) Subject: [edk2] [PATCH 5/5] OvmfPkg/AcpiPlatformDxe: replay QEMU_LOADER_WRITE_POINTER commands at S3 X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jordan Justen MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Ultimately, each QEMU_LOADER_WRITE_POINTER command creates a guest memory reference in some QEMU device. When the virtual machine is reset, the device willfully forgets the guest address, since the guest memory is wholly invalidated during platform reset. ... Unless the reset is part of S3 resume. Then the guest memory is preserved intact, and the firmware must reprogram those devices with the original guest memory allocation addresses. This patch accumulates the fw_cfg select, skip and write operations of ProcessCmdWritePointer() in a validated / condensed form, and turns them into an ACPI S3 Boot Script fragment at the very end of InstallQemuFwCfgTables(). Cc: Jordan Justen Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D359 Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek Reviewed-by: Jordan Justen --- OvmfPkg/AcpiPlatformDxe/AcpiPlatformDxe.inf | 2 + OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpiPlatformDxe.inf | 2 + OvmfPkg/AcpiPlatformDxe/AcpiPlatform.h | 27 ++ OvmfPkg/AcpiPlatformDxe/BootScript.c | 414 +++++++++++++++= +++++ OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c | 70 +++- 5 files changed, 510 insertions(+), 5 deletions(-) diff --git a/OvmfPkg/AcpiPlatformDxe/AcpiPlatformDxe.inf b/OvmfPkg/AcpiPlat= formDxe/AcpiPlatformDxe.inf index 654d3a03905d..bb5f14e0fc7a 100644 --- a/OvmfPkg/AcpiPlatformDxe/AcpiPlatformDxe.inf +++ b/OvmfPkg/AcpiPlatformDxe/AcpiPlatformDxe.inf @@ -31,10 +31,11 @@ [Sources] Qemu.c QemuFwCfgAcpi.c Xen.c EntryPoint.c PciDecoding.c + BootScript.c =20 [Packages] MdePkg/MdePkg.dec MdeModulePkg/MdeModulePkg.dec OvmfPkg/OvmfPkg.dec @@ -57,10 +58,11 @@ [LibraryClasses] OrderedCollectionLib =20 [Protocols] gEfiAcpiTableProtocolGuid # PROTOCOL ALWAYS_CONSUMED gEfiPciIoProtocolGuid # PROTOCOL SOMETIMES_CONSU= MED + gEfiS3SaveStateProtocolGuid # PROTOCOL SOMETIMES_CONSU= MED =20 [Guids] gEfiXenInfoGuid gRootBridgesConnectedEventGroupGuid =20 diff --git a/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpiPlatformDxe.inf b/OvmfPkg= /AcpiPlatformDxe/QemuFwCfgAcpiPlatformDxe.inf index d99f2d5a95c7..e550ff5a4714 100644 --- a/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpiPlatformDxe.inf +++ b/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpiPlatformDxe.inf @@ -29,10 +29,11 @@ [Defines] [Sources] QemuFwCfgAcpiPlatform.c QemuFwCfgAcpi.c EntryPoint.c PciDecoding.c + BootScript.c =20 [Packages] MdePkg/MdePkg.dec MdeModulePkg/MdeModulePkg.dec OvmfPkg/OvmfPkg.dec @@ -47,10 +48,11 @@ [LibraryClasses] UefiDriverEntryPoint =20 [Protocols] gEfiAcpiTableProtocolGuid # PROTOCOL ALWAYS_CONSUMED gEfiPciIoProtocolGuid # PROTOCOL SOMETIMES_CONSU= MED + gEfiS3SaveStateProtocolGuid # PROTOCOL SOMETIMES_CONSU= MED =20 [Guids] gRootBridgesConnectedEventGroupGuid =20 [Pcd] diff --git a/OvmfPkg/AcpiPlatformDxe/AcpiPlatform.h b/OvmfPkg/AcpiPlatformD= xe/AcpiPlatform.h index 08dd7f8f7dd7..0f035a0d5751 100644 --- a/OvmfPkg/AcpiPlatformDxe/AcpiPlatform.h +++ b/OvmfPkg/AcpiPlatformDxe/AcpiPlatform.h @@ -31,10 +31,12 @@ typedef struct { EFI_PCI_IO_PROTOCOL *PciIo; UINT64 PciAttributes; } ORIGINAL_ATTRIBUTES; =20 +typedef struct S3_CONTEXT S3_CONTEXT; + EFI_STATUS EFIAPI InstallAcpiTable ( IN EFI_ACPI_TABLE_PROTOCOL *AcpiProtocol, IN VOID *AcpiTableBuffer, @@ -89,7 +91,32 @@ VOID RestorePciDecoding ( IN ORIGINAL_ATTRIBUTES *OriginalAttributes, IN UINTN Count ); =20 +EFI_STATUS +AllocateS3Context ( + OUT S3_CONTEXT **S3Context, + IN UINTN WritePointerCount + ); + +VOID +ReleaseS3Context ( + IN S3_CONTEXT *S3Context + ); + +EFI_STATUS +SaveCondensedWritePointerToS3Context ( + IN OUT S3_CONTEXT *S3Context, + IN UINT16 PointerItem, + IN UINT8 PointerSize, + IN UINT32 PointerOffset, + IN UINT64 PointerValue + ); + +EFI_STATUS +TransferS3ContextToBootScript ( + IN CONST S3_CONTEXT *S3Context + ); + #endif =20 diff --git a/OvmfPkg/AcpiPlatformDxe/BootScript.c b/OvmfPkg/AcpiPlatformDxe= /BootScript.c new file mode 100644 index 000000000000..b7a7f270f223 --- /dev/null +++ b/OvmfPkg/AcpiPlatformDxe/BootScript.c @@ -0,0 +1,414 @@ +/** @file + Append an ACPI S3 Boot Script fragment from the QEMU_LOADER_WRITE_POINTER + commands of QEMU's fully processed table linker/loader script. + + Copyright (C) 2017, Red Hat, Inc. + + This program and the accompanying materials are licensed and made availa= ble + under the terms and conditions of the BSD License which accompanies this + distribution. The full text of the license may be found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WI= THOUT + WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +**/ + +#include +#include +#include + +#include "AcpiPlatform.h" + + +// +// Condensed structure for capturing the fw_cfg operations -- select, skip, +// write -- inherent in executing a QEMU_LOADER_WRITE_POINTER command. +// +typedef struct { + UINT16 PointerItem; // resolved from QEMU_LOADER_WRITE_POINTER.Pointer= File + UINT8 PointerSize; // copied as-is from QEMU_LOADER_WRITE_POINTER + UINT32 PointerOffset; // copied as-is from QEMU_LOADER_WRITE_POINTER + UINT64 PointerValue; // resolved from QEMU_LOADER_WRITE_POINTER.Pointee= File +} CONDENSED_WRITE_POINTER; + + +// +// Context structure to accumulate CONDENSED_WRITE_POINTER objects from +// QEMU_LOADER_WRITE_POINTER commands. +// +// Any pointers in this structure own the pointed-to objects; that is, whe= n the +// context structure is released, all pointed-to objects must be released = too. +// +struct S3_CONTEXT { + CONDENSED_WRITE_POINTER *WritePointers; // one array element per process= ed + // QEMU_LOADER_WRITE_POINTER + // command + UINTN Allocated; // number of elements allocated = for + // WritePointers + UINTN Used; // number of elements populated = in + // WritePointers +}; + + +// +// Scratch buffer, allocated in EfiReservedMemoryType type memory, for the= ACPI +// S3 Boot Script opcodes to work on. We use the buffer to compose and to +// replay several fw_cfg select+skip and write operations, using the DMA a= ccess +// method. The fw_cfg operations will implement the actions dictated by +// CONDENSED_WRITE_POINTER objects. +// +#pragma pack (1) +typedef struct { + FW_CFG_DMA_ACCESS Access; // filled in from + // CONDENSED_WRITE_POINTER.PointerItem, + // CONDENSED_WRITE_POINTER.PointerSize, + // CONDENSED_WRITE_POINTER.PointerOffs= et + UINT64 PointerValue; // filled in from + // CONDENSED_WRITE_POINTER.PointerValue +} SCRATCH_BUFFER; +#pragma pack () + + +/** + Allocate an S3_CONTEXT object. + + @param[out] S3Context The allocated S3_CONTEXT object is returned + through this parameter. + + @param[in] WritePointerCount Number of CONDENSED_WRITE_POINTER elements= to + allocate room for. WritePointerCount must = be + positive. + + @retval EFI_SUCCESS Allocation successful. + + @retval EFI_OUT_OF_RESOURCES Out of memory. + + @retval EFI_INVALID_PARAMETER WritePointerCount is zero. +**/ +EFI_STATUS +AllocateS3Context ( + OUT S3_CONTEXT **S3Context, + IN UINTN WritePointerCount + ) +{ + EFI_STATUS Status; + S3_CONTEXT *Context; + + if (WritePointerCount =3D=3D 0) { + return EFI_INVALID_PARAMETER; + } + + Context =3D AllocateZeroPool (sizeof *Context); + if (Context =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Context->WritePointers =3D AllocatePool (WritePointerCount * + sizeof *Context->WritePointers); + if (Context->WritePointers =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + goto FreeContext; + } + + Context->Allocated =3D WritePointerCount; + *S3Context =3D Context; + return EFI_SUCCESS; + +FreeContext: + FreePool (Context); + + return Status; +} + + +/** + Release an S3_CONTEXT object. + + @param[in] S3Context The object to release. +**/ +VOID +ReleaseS3Context ( + IN S3_CONTEXT *S3Context + ) +{ + FreePool (S3Context->WritePointers); + FreePool (S3Context); +} + + +/** + Save the information necessary to replicate a QEMU_LOADER_WRITE_POINTER + command during S3 resume, in condensed format. + + This function is to be called from ProcessCmdWritePointer(), after all t= he + sanity checks have passed, and before the fw_cfg operations are performe= d. + + @param[in,out] S3Context The S3_CONTEXT object into which the caller wa= nts + to save the information that was derived from + QEMU_LOADER_WRITE_POINTER. + + @param[in] PointerItem The FIRMWARE_CONFIG_ITEM that + QEMU_LOADER_WRITE_POINTER.PointerFile was reso= lved + to, expressed as a UINT16 value. + + @param[in] PointerSize Copied directly from + QEMU_LOADER_WRITE_POINTER.PointerSize. + + @param[in] PointerOffset Copied directly from + QEMU_LOADER_WRITE_POINTER.PointerOffset. + + @param[in] PointerValue The base address of the allocated / downloaded + fw_cfg blob that is identified by + QEMU_LOADER_WRITE_POINTER.PointeeFile. + + @retval EFI_SUCCESS The information derived from + QEMU_LOADER_WRITE_POINTER has been success= fully + absorbed into S3Context. + + @retval EFI_OUT_OF_RESOURCES No room available in S3Context. +**/ +EFI_STATUS +SaveCondensedWritePointerToS3Context ( + IN OUT S3_CONTEXT *S3Context, + IN UINT16 PointerItem, + IN UINT8 PointerSize, + IN UINT32 PointerOffset, + IN UINT64 PointerValue + ) +{ + CONDENSED_WRITE_POINTER *Condensed; + + if (S3Context->Used =3D=3D S3Context->Allocated) { + return EFI_OUT_OF_RESOURCES; + } + Condensed =3D S3Context->WritePointers + S3Context->Used; + Condensed->PointerItem =3D PointerItem; + Condensed->PointerSize =3D PointerSize; + Condensed->PointerOffset =3D PointerOffset; + Condensed->PointerValue =3D PointerValue; + DEBUG ((DEBUG_VERBOSE, "%a: 0x%04x/[0x%08x+%d] :=3D 0x%Lx (%Lu)\n", + __FUNCTION__, PointerItem, PointerOffset, PointerSize, PointerValue, + (UINT64)S3Context->Used)); + ++S3Context->Used; + return EFI_SUCCESS; +} + + +/** + Translate and append the information from an S3_CONTEXT object to the AC= PI S3 + Boot Script. + + The effects of a successful call to this function cannot be undone. + + @param[in] S3Context The S3_CONTEXT object to translate to ACPI S3 Boot + Script opcodes. + + @retval EFI_OUT_OF_RESOURCES Out of memory. + + @retval EFI_SUCCESS The translation of S3Context to ACPI S3 Bo= ot + Script opcodes has been successful. + + @return Error codes from underlying functions. +**/ +EFI_STATUS +TransferS3ContextToBootScript ( + IN CONST S3_CONTEXT *S3Context + ) +{ + EFI_STATUS Status; + EFI_S3_SAVE_STATE_PROTOCOL *S3SaveState; + SCRATCH_BUFFER *ScratchBuffer; + FW_CFG_DMA_ACCESS *Access; + UINT64 BigEndianAddressOfAccess; + UINT32 ControlPollData; + UINT32 ControlPollMask; + UINTN Index; + + // + // If the following protocol lookup fails, it shall not happen due to an + // unexpected DXE driver dispatch order. + // + // Namely, this function is only invoked on QEMU. Therefore it is only + // reached after Platform BDS signals gRootBridgesConnectedEventGroupGuid + // (see OnRootBridgesConnected() in "EntryPoint.c"). Hence, because + // TransferS3ContextToBootScript() is invoked in BDS, all DXE drivers, + // including S3SaveStateDxe (producing EFI_S3_SAVE_STATE_PROTOCOL), have= been + // dispatched by the time we get here. (S3SaveStateDxe is not expected to + // have any stricter-than-TRUE DEPEX -- not a DEPEX that gets unblocked = only + // within BDS anyway.) + // + // Reaching this function also depends on QemuFwCfgS3Enabled(). That imp= lies + // S3SaveStateDxe has not exited immediately due to S3 being disabled. T= hus + // EFI_S3_SAVE_STATE_PROTOCOL can only be missing for genuinely unforese= eable + // reasons. + // + Status =3D gBS->LocateProtocol (&gEfiS3SaveStateProtocolGuid, + NULL /* Registration */, (VOID **)&S3SaveState); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: LocateProtocol(): %r\n", __FUNCTION__, Statu= s)); + return Status; + } + + ScratchBuffer =3D AllocateReservedPool (sizeof *ScratchBuffer); + if (ScratchBuffer =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Set up helper variables that we'll use identically for all + // CONDENSED_WRITE_POINTER elements. + // + Access =3D &ScratchBuffer->Access; + BigEndianAddressOfAccess =3D SwapBytes64 ((UINTN)Access); + ControlPollData =3D 0; + ControlPollMask =3D MAX_UINT32; + + // + // For each CONDENSED_WRITE_POINTER, we need six ACPI S3 Boot Script opc= odes: + // (1) restore an FW_CFG_DMA_ACCESS object in reserved memory that selec= ts + // the writeable fw_cfg file PointerFile (through PointerItem), and = skips + // to PointerOffset in it, + // (2) call QEMU with the FW_CFG_DMA_ACCESS object, + // (3) wait for the select+skip to finish, + // (4) restore a SCRATCH_BUFFER object in reserved memory that writes + // PointerValue (base address of the allocated / downloaded PointeeF= ile), + // of size PointerSize, into the fw_cfg file selected in (1), at the + // offset sought to in (1), + // (5) call QEMU with the FW_CFG_DMA_ACCESS object, + // (6) wait for the write to finish. + // + // EFI_S3_SAVE_STATE_PROTOCOL does not allow rolling back opcode additio= ns, + // therefore we treat any failure here as fatal. + // + for (Index =3D 0; Index < S3Context->Used; ++Index) { + CONST CONDENSED_WRITE_POINTER *Condensed; + + Condensed =3D &S3Context->WritePointers[Index]; + + // + // (1) restore an FW_CFG_DMA_ACCESS object in reserved memory that sel= ects + // the writeable fw_cfg file PointerFile (through PointerItem), and + // skips to PointerOffset in it, + // + Access->Control =3D SwapBytes32 ((UINT32)Condensed->PointerItem << 16 | + FW_CFG_DMA_CTL_SELECT | FW_CFG_DMA_CTL_SKIP); + Access->Length =3D SwapBytes32 (Condensed->PointerOffset); + Access->Address =3D 0; + Status =3D S3SaveState->Write ( + S3SaveState, // This + EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE, // OpCode + EfiBootScriptWidthUint8, // Width + (UINT64)(UINTN)Access, // Address + sizeof *Access, // Count + Access // Buffer + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 1: %r\n", __FUNCTION__, + (UINT64)Index, Status)); + goto FatalError; + } + + // + // (2) call QEMU with the FW_CFG_DMA_ACCESS object, + // + Status =3D S3SaveState->Write ( + S3SaveState, // This + EFI_BOOT_SCRIPT_IO_WRITE_OPCODE, // OpCode + EfiBootScriptWidthUint32, // Width + (UINT64)0x514, // Address + (UINTN)2, // Count + &BigEndianAddressOfAccess // Buffer + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 2: %r\n", __FUNCTION__, + (UINT64)Index, Status)); + goto FatalError; + } + + // + // (3) wait for the select+skip to finish, + // + Status =3D S3SaveState->Write ( + S3SaveState, // This + EFI_BOOT_SCRIPT_MEM_POLL_OPCODE, // OpCode + EfiBootScriptWidthUint32, // Width + (UINT64)(UINTN)&Access->Control, // Address + &ControlPollData, // Data + &ControlPollMask, // DataMask + MAX_UINT64 // Delay + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 3: %r\n", __FUNCTION__, + (UINT64)Index, Status)); + goto FatalError; + } + + // + // (4) restore a SCRATCH_BUFFER object in reserved memory that writes + // PointerValue (base address of the allocated / downloaded + // PointeeFile), of size PointerSize, into the fw_cfg file selecte= d in + // (1), at the offset sought to in (1), + // + Access->Control =3D SwapBytes32 (FW_CFG_DMA_CTL_WRITE); + Access->Length =3D SwapBytes32 (Condensed->PointerSize); + Access->Address =3D SwapBytes64 ((UINTN)&ScratchBuffer->PointerValue); + ScratchBuffer->PointerValue =3D Condensed->PointerValue; + Status =3D S3SaveState->Write ( + S3SaveState, // This + EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE, // OpCode + EfiBootScriptWidthUint8, // Width + (UINT64)(UINTN)ScratchBuffer, // Address + sizeof *ScratchBuffer, // Count + ScratchBuffer // Buffer + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 4: %r\n", __FUNCTION__, + (UINT64)Index, Status)); + goto FatalError; + } + + // + // (5) call QEMU with the FW_CFG_DMA_ACCESS object, + // + Status =3D S3SaveState->Write ( + S3SaveState, // This + EFI_BOOT_SCRIPT_IO_WRITE_OPCODE, // OpCode + EfiBootScriptWidthUint32, // Width + (UINT64)0x514, // Address + (UINTN)2, // Count + &BigEndianAddressOfAccess // Buffer + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 5: %r\n", __FUNCTION__, + (UINT64)Index, Status)); + goto FatalError; + } + + // + // (6) wait for the write to finish. + // + Status =3D S3SaveState->Write ( + S3SaveState, // This + EFI_BOOT_SCRIPT_MEM_POLL_OPCODE, // OpCode + EfiBootScriptWidthUint32, // Width + (UINT64)(UINTN)&Access->Control, // Address + &ControlPollData, // Data + &ControlPollMask, // DataMask + MAX_UINT64 // Delay + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 6: %r\n", __FUNCTION__, + (UINT64)Index, Status)); + goto FatalError; + } + } + + DEBUG ((DEBUG_VERBOSE, "%a: boot script fragment saved, ScratchBuffer=3D= %p\n", + __FUNCTION__, (VOID *)ScratchBuffer)); + return EFI_SUCCESS; + +FatalError: + ASSERT (FALSE); + CpuDeadLoop (); + return Status; +} diff --git a/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c b/OvmfPkg/AcpiPlatform= Dxe/QemuFwCfgAcpi.c index de827c2df204..eadd690bef4e 100644 --- a/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c +++ b/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c @@ -358,26 +358,39 @@ ProcessCmdAddChecksum ( @param[in] WritePointer The QEMU_LOADER_WRITE_POINTER command to proce= ss. =20 @param[in] Tracker The ORDERED_COLLECTION tracking the BLOB user structures created thus far. =20 + @param[in,out] S3Context The S3_CONTEXT object capturing the fw_cfg act= ions + of successfully processed QEMU_LOADER_WRITE_PO= INTER + commands, to be replayed at S3 resume. S3Conte= xt + may be NULL if S3 is disabled. + @retval EFI_PROTOCOL_ERROR Malformed fw_cfg file name(s) have been foun= d in WritePointer. Or, the WritePointer command references a file unknown to Tracker or the fw_cfg directory. Or, the pointer object to rewrite has invalid location, size, or initi= al relative value. Or, the pointer value to sto= re does not fit in the given pointer size. =20 @retval EFI_SUCCESS The pointer object inside the writeable fw_c= fg - file has been written. + file has been written. If S3Context is not N= ULL, + then WritePointer has been condensed into + S3Context. + + @return Error codes propagated from + SaveCondensedWritePointerToS3Context(). The + pointer object inside the writeable fw_cfg f= ile + has not been written. **/ STATIC EFI_STATUS ProcessCmdWritePointer ( IN CONST QEMU_LOADER_WRITE_POINTER *WritePointer, - IN CONST ORDERED_COLLECTION *Tracker + IN CONST ORDERED_COLLECTION *Tracker, + IN OUT S3_CONTEXT *S3Context OPTIONAL ) { RETURN_STATUS Status; FIRMWARE_CONFIG_ITEM PointerItem; UINTN PointerItemSize; @@ -430,10 +443,29 @@ ProcessCmdWritePointer ( DEBUG ((DEBUG_ERROR, "%a: pointer value unrepresentable in \"%a\"\n", __FUNCTION__, WritePointer->PointerFile)); return EFI_PROTOCOL_ERROR; } =20 + // + // If S3 is enabled, we have to capture the below fw_cfg actions in cond= ensed + // form, to be replayed during S3 resume. + // + if (S3Context !=3D NULL) { + EFI_STATUS SaveStatus; + + SaveStatus =3D SaveCondensedWritePointerToS3Context ( + S3Context, + (UINT16)PointerItem, + WritePointer->PointerSize, + WritePointer->PointerOffset, + PointerValue + ); + if (EFI_ERROR (SaveStatus)) { + return SaveStatus; + } + } + QemuFwCfgSelectItem (PointerItem); QemuFwCfgSkipBytes (WritePointer->PointerOffset); QemuFwCfgWriteBytes (WritePointer->PointerSize, &PointerValue); =20 // @@ -699,10 +731,11 @@ InstallQemuFwCfgTables ( QEMU_LOADER_ENTRY *LoaderStart; CONST QEMU_LOADER_ENTRY *LoaderEntry, *LoaderEnd; CONST QEMU_LOADER_ENTRY *WritePointerSubsetEnd; ORIGINAL_ATTRIBUTES *OriginalPciAttributes; UINTN OriginalPciAttributesCount; + S3_CONTEXT *S3Context; ORDERED_COLLECTION *Tracker; UINTN *InstalledKey; INT32 Installed; ORDERED_COLLECTION_ENTRY *TrackerEntry, *TrackerEntry2; =20 @@ -724,14 +757,26 @@ InstallQemuFwCfgTables ( QemuFwCfgSelectItem (FwCfgItem); QemuFwCfgReadBytes (FwCfgSize, LoaderStart); RestorePciDecoding (OriginalPciAttributes, OriginalPciAttributesCount); LoaderEnd =3D LoaderStart + FwCfgSize / sizeof *LoaderEntry; =20 + S3Context =3D NULL; + if (QemuFwCfgS3Enabled ()) { + // + // Size the allocation pessimistically, assuming that all commands in = the + // script are QEMU_LOADER_WRITE_POINTER commands. + // + Status =3D AllocateS3Context (&S3Context, LoaderEnd - LoaderStart); + if (EFI_ERROR (Status)) { + goto FreeLoader; + } + } + Tracker =3D OrderedCollectionInit (BlobCompare, BlobKeyCompare); if (Tracker =3D=3D NULL) { Status =3D EFI_OUT_OF_RESOURCES; - goto FreeLoader; + goto FreeS3Context; } =20 // // first pass: process the commands // @@ -756,11 +801,11 @@ InstallQemuFwCfgTables ( Tracker); break; =20 case QemuLoaderCmdWritePointer: Status =3D ProcessCmdWritePointer (&LoaderEntry->Command.WritePoin= ter, - Tracker); + Tracker, S3Context); if (!EFI_ERROR (Status)) { WritePointerSubsetEnd =3D LoaderEntry + 1; } break; =20 @@ -788,15 +833,25 @@ InstallQemuFwCfgTables ( for (LoaderEntry =3D LoaderStart; LoaderEntry < LoaderEnd; ++LoaderEntry= ) { if (LoaderEntry->Type =3D=3D QemuLoaderCmdAddPointer) { Status =3D Process2ndPassCmdAddPointer (&LoaderEntry->Command.AddPoi= nter, Tracker, AcpiProtocol, InstalledKey, &Installed); if (EFI_ERROR (Status)) { - break; + goto UninstallAcpiTables; } } } =20 + // + // Translating the condensed QEMU_LOADER_WRITE_POINTER commands to ACPI = S3 + // Boot Script opcodes has to be the last operation in this function, be= cause + // if it succeeds, it cannot be undone. + // + if (S3Context !=3D NULL) { + Status =3D TransferS3ContextToBootScript (S3Context); + } + +UninstallAcpiTables: if (EFI_ERROR (Status)) { // // roll back partial installation // while (Installed > 0) { @@ -845,10 +900,15 @@ RollbackWritePointersAndFreeTracker: } FreePool (Blob); } OrderedCollectionUninit (Tracker); =20 +FreeS3Context: + if (S3Context !=3D NULL) { + ReleaseS3Context (S3Context); + } + FreeLoader: FreePool (LoaderStart); =20 return Status; } --=20 2.9.3 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel