From nobody Mon Feb 9 20:32:28 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+55385+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+55385+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1583315570; cv=none; d=zohomail.com; s=zohoarc; b=CKjEKPgu243ITHyZuy+SHQH7Bdqxzee++12nMBIcL90d5A+sXfmt155h/eT65o6RM49NZkBlBy516CEcRw0xdqxIzs1LfTyMX/6E8KkiCBQX6g6ZbdA2rAaj+InSXF8bV7VwdB8ftndMpabMkIz7tSNZ+Dc2Mq5Il0kLBI6RRQw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1583315570; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=tqgJrSp/+oACQNn9tSiS8btJ1htC7gCUGkCMPEvLp54=; b=QOoZqocH3ZvB08l52BUl79gVEwVs6MrF3S2y2OVWW5vGvU2+lngB3OjXNd1/PG/Vj7944YWqpYGDnYY/4ZjvLptwty5jVOywZiz0Loe5ys+1dphsJgFUd7uMbVYYc2CiGx5r+i7lxgJZaHlJ20J/xSR6sZmBIyLcm6PLmpK0xNM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+55385+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1583315569998265.9395854367948; Wed, 4 Mar 2020 01:52:49 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id 7PD2YY1788612xGwgIQ2htU4; Wed, 04 Mar 2020 01:52:47 -0800 X-Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by mx.groups.io with SMTP id smtpd.web10.9181.1583315567232029788 for ; Wed, 04 Mar 2020 01:52:47 -0800 X-Received: by mail-wm1-f66.google.com with SMTP id a132so1249808wme.1 for ; Wed, 04 Mar 2020 01:52:47 -0800 (PST) X-Gm-Message-State: EG1mbgovb4wAusWULIqd3viJx1787277AA= X-Google-Smtp-Source: ADFU+vtw+JWXo2zZzBn0N7eS3NOLPRH9FRLG1EbczZra7dKkKtqDAB/qd0YcOwfHygB0n2tzhiDLZQ== X-Received: by 2002:a7b:ce8b:: with SMTP id q11mr2927612wmj.100.1583315565261; Wed, 04 Mar 2020 01:52:45 -0800 (PST) X-Received: from e123331-lin.home ([2a01:cb1d:112:6f00:816e:ff0d:fb69:f613]) by smtp.gmail.com with ESMTPSA id v16sm20781095wrp.84.2020.03.04.01.52.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2020 01:52:43 -0800 (PST) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: lersek@redhat.com, Ard Biesheuvel Subject: [edk2-devel] [PATCH v2 04/14] OvmfPkg: provide a generic implementation of QemuLoadImageLib Date: Wed, 4 Mar 2020 10:52:23 +0100 Message-Id: <20200304095233.21046-5-ard.biesheuvel@linaro.org> In-Reply-To: <20200304095233.21046-1-ard.biesheuvel@linaro.org> References: <20200304095233.21046-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: 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,ard.biesheuvel@linaro.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1583315567; bh=lueDbEUEzBo7uBkbt0952v3eywQ6Z0cqqLUiJaaaUBw=; h=Cc:Content-Type:Date:From:Reply-To:Subject:To; b=K0t9EkJ5KB9OBy4om1ftFa0rl15Vmm34UyI5pOvUVJT3QaNc6rpXwJRef9gqFjRp/jx IumOpaDHuIbc7HZW2bN8BpYcmrUAW8OhfWtIg1EXJsxfiVR2UkU0dp0g3fxk4+Hvggvti ev2EFt67Zrc5bbja4C1MHoHLTfD24Z1MQT4= X-ZohoMail-DKIM: pass (identity @groups.io) Implement QemuLoadImageLib, and make it load the image provided by the QEMU_EFI_LOADER_FS_MEDIA_GUID/kernel device path that we implemented in a preceding patch in a separate DXE driver, using only the standard LoadImage and StartImage boot services. Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2566 Signed-off-by: Ard Biesheuvel Reviewed-by: Laszlo Ersek --- OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.c | 278 = ++++++++++++++++++++ OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.inf | 38 = +++ 2 files changed, 316 insertions(+) diff --git a/OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLi= b.c b/OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.c new file mode 100644 index 000000000000..f5edb43cc0b9 --- /dev/null +++ b/OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.c @@ -0,0 +1,278 @@ +/** @file + Generic implementation of QemuLoadImageLib library class interface. + + Copyright (c) 2020, ARM Ltd. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#pragma pack (1) +typedef struct { + EFI_DEVICE_PATH_PROTOCOL FilePathHeader; + CHAR16 FilePath[ARRAY_SIZE (L"kernel")]; +} KERNEL_FILE_DEVPATH; + +typedef struct { + VENDOR_DEVICE_PATH VenMediaNode; + KERNEL_FILE_DEVPATH FileNode; + EFI_DEVICE_PATH_PROTOCOL EndNode; +} KERNEL_VENMEDIA_FILE_DEVPATH; +#pragma pack () + +STATIC CONST KERNEL_VENMEDIA_FILE_DEVPATH mKernelDevicePath =3D { + { + { + MEDIA_DEVICE_PATH, MEDIA_VENDOR_DP, + { sizeof (VENDOR_DEVICE_PATH) } + }, + QEMU_KERNEL_LOADER_FS_MEDIA_GUID + }, { + { + MEDIA_DEVICE_PATH, MEDIA_FILEPATH_DP, + { sizeof (KERNEL_FILE_DEVPATH) } + }, + L"kernel", + }, { + END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, + { sizeof (EFI_DEVICE_PATH_PROTOCOL) } + } +}; + +/** + Download the kernel, the initial ramdisk, and the kernel command line fr= om + QEMU's fw_cfg. The kernel will be instructed via its command line to load + the initrd from the same Simple FileSystem where the kernel was loaded f= rom. + + @param[out] ImageHandle The image handle that was allocated for + loading the image + @param[out] LoadedImage The loaded image protocol that was install= ed + on ImageHandle by the LoadImage boot servi= ce. + + @retval EFI_SUCCESS The image was loaded successfully. + @retval EFI_NOT_FOUND Kernel image was not found. + @retval EFI_OUT_OF_RESOURCES Memory allocation failed. + @retval EFI_PROTOCOL_ERROR Unterminated kernel command line. + @retval EFI_ACCESS_DENIED The underlying LoadImage boot service call + returned EFI_SECURITY_VIOLATION, and the i= mage + was unloaded again. + + @return Error codes from any of the underlying + functions. +**/ +EFI_STATUS +EFIAPI +QemuLoadKernelImage ( + OUT EFI_HANDLE *ImageHandle + ) +{ + EFI_STATUS Status; + EFI_HANDLE KernelImageHandle; + EFI_LOADED_IMAGE_PROTOCOL *KernelLoadedImage; + UINTN CommandLineSize; + CHAR8 *CommandLine; + UINTN InitrdSize; + + // + // Load the image. This should call back into the QEMU EFI loader file s= ystem. + // + Status =3D gBS->LoadImage ( + FALSE, // BootPolicy: exact match req= uired + gImageHandle, // ParentImageHandle + (EFI_DEVICE_PATH_PROTOCOL *)&mKernelDevicePath, + NULL, // SourceBuffer + 0, // SourceSize + &KernelImageHandle + ); + switch (Status) { + case EFI_SUCCESS: + break; + + case EFI_SECURITY_VIOLATION: + // + // In this case, the image was loaded but failed to authenticate. + // + Status =3D EFI_ACCESS_DENIED; + goto UnloadImage; + + default: + DEBUG ((DEBUG_ERROR, "%a: LoadImage(): %r\n", __FUNCTION__, Status)); + return Status; + } + + // + // Construct the kernel command line. + // + Status =3D gBS->OpenProtocol ( + KernelImageHandle, + &gEfiLoadedImageProtocolGuid, + (VOID **)&KernelLoadedImage, + gImageHandle, // AgentHandle + NULL, // ControllerHandle + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + ASSERT_EFI_ERROR (Status); + + QemuFwCfgSelectItem (QemuFwCfgItemCommandLineSize); + CommandLineSize =3D (UINTN)QemuFwCfgRead32 (); + + if (CommandLineSize =3D=3D 0) { + KernelLoadedImage->LoadOptionsSize =3D 0; + } else { + CommandLine =3D AllocatePool (CommandLineSize); + if (CommandLine =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + goto UnloadImage; + } + + QemuFwCfgSelectItem (QemuFwCfgItemCommandLineData); + QemuFwCfgReadBytes (CommandLineSize, CommandLine); + + // + // Verify NUL-termination of the command line. + // + if (CommandLine[CommandLineSize - 1] !=3D '\0') { + DEBUG ((DEBUG_ERROR, "%a: kernel command line is not NUL-terminated\= n", + __FUNCTION__)); + Status =3D EFI_PROTOCOL_ERROR; + goto FreeCommandLine; + } + + // + // Drop the terminating NUL, convert to UTF-16. + // + KernelLoadedImage->LoadOptionsSize =3D (CommandLineSize - 1) * 2; + } + + QemuFwCfgSelectItem (QemuFwCfgItemInitrdSize); + InitrdSize =3D (UINTN)QemuFwCfgRead32 (); + + if (InitrdSize > 0) { + // + // Append ' initrd=3Dinitrd' in UTF-16. + // + KernelLoadedImage->LoadOptionsSize +=3D sizeof (L" initrd=3Dinitrd") -= 2; + } + + if (KernelLoadedImage->LoadOptionsSize =3D=3D 0) { + KernelLoadedImage->LoadOptions =3D NULL; + } else { + // + // NUL-terminate in UTF-16. + // + KernelLoadedImage->LoadOptionsSize +=3D 2; + + KernelLoadedImage->LoadOptions =3D AllocatePool ( + KernelLoadedImage->LoadOptionsSize); + if (KernelLoadedImage->LoadOptions =3D=3D NULL) { + KernelLoadedImage->LoadOptionsSize =3D 0; + Status =3D EFI_OUT_OF_RESOURCES; + goto FreeCommandLine; + } + + UnicodeSPrintAsciiFormat ( + KernelLoadedImage->LoadOptions, + KernelLoadedImage->LoadOptionsSize, + "%a%a", + (CommandLineSize =3D=3D 0) ? "" : CommandLine, + (InitrdSize =3D=3D 0) ? "" : " initrd=3Dinitrd" + ); + DEBUG ((DEBUG_INFO, "%a: command line: \"%s\"\n", __FUNCTION__, + (CHAR16 *)KernelLoadedImage->LoadOptions)); + } + + *ImageHandle =3D KernelImageHandle; + return EFI_SUCCESS; + +FreeCommandLine: + if (CommandLineSize > 0) { + FreePool (CommandLine); + } +UnloadImage: + gBS->UnloadImage (KernelImageHandle); + + return Status; +} + +/** + Transfer control to a kernel image loaded with QemuLoadKernelImage () + + @param[in,out] ImageHandle Handle of image to be started. May assum= e a + different value on return if the image w= as + reloaded. + + @retval EFI_INVALID_PARAMETER ImageHandle is either an invalid image h= andle + or the image has already been initialize= d with + StartImage + @retval EFI_SECURITY_VIOLATION The current platform policy specifies th= at the + image should not be started. + + @return Error codes returned by the started image +**/ +EFI_STATUS +EFIAPI +QemuStartKernelImage ( + IN OUT EFI_HANDLE *ImageHandle + ) +{ + return gBS->StartImage ( + *ImageHandle, + NULL, // ExitDataSize + NULL // ExitData + ); +} + +/** + Unloads an image loaded with QemuLoadKernelImage (). + + @param ImageHandle Handle that identifies the image to be + unloaded. + + @retval EFI_SUCCESS The image has been unloaded. + @retval EFI_UNSUPPORTED The image has been started, and does not + support unload. + @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle. + + @return Exit code from the image=E2=80=99s unloa= d function. +**/ +EFI_STATUS +EFIAPI +QemuUnloadKernelImage ( + IN EFI_HANDLE ImageHandle + ) +{ + EFI_LOADED_IMAGE_PROTOCOL *KernelLoadedImage; + EFI_STATUS Status; + + Status =3D gBS->OpenProtocol ( + ImageHandle, + &gEfiLoadedImageProtocolGuid, + (VOID **)&KernelLoadedImage, + gImageHandle, // AgentHandle + NULL, // ControllerHandle + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return EFI_INVALID_PARAMETER; + } + + if (KernelLoadedImage->LoadOptions !=3D NULL) { + FreePool (KernelLoadedImage->LoadOptions); + KernelLoadedImage->LoadOptions =3D NULL; + } + KernelLoadedImage->LoadOptionsSize =3D 0; + + return gBS->UnloadImage (ImageHandle); +} diff --git a/OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLi= b.inf b/OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.inf new file mode 100644 index 000000000000..b262cb926a4d --- /dev/null +++ b/OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.inf @@ -0,0 +1,38 @@ +## @file +# Generic implementation of QemuLoadImageLib library class interface. +# +# Copyright (c) 2020, ARM Ltd. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 1.27 + BASE_NAME =3D GenericQemuLoadImageLib + FILE_GUID =3D 9e3e28da-c7b5-4f85-841a-84e6a9a1f1a0 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D QemuLoadImageLib|DXE_DRIVER + +[Sources] + GenericQemuLoadImageLib.c + +[Packages] + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + OvmfPkg/OvmfPkg.dec + +[LibraryClasses] + DebugLib + MemoryAllocationLib + PrintLib + QemuFwCfgLib + UefiBootServicesTableLib + +[Protocols] + gEfiDevicePathProtocolGuid + gEfiLoadedImageProtocolGuid + +[Guids] + gQemuKernelLoaderFsMediaGuid --=20 2.17.1 -=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 (#55385): https://edk2.groups.io/g/devel/message/55385 Mute This Topic: https://groups.io/mt/71722797/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-