From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by mx.groups.io with SMTP id smtpd.web10.11203.1583134187837182821 for ; Sun, 01 Mar 2020 23:29:48 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@linaro.org header.s=google header.b=QzcO2ipD; spf=pass (domain: linaro.org, ip: 209.85.221.66, mailfrom: ard.biesheuvel@linaro.org) Received: by mail-wr1-f66.google.com with SMTP id v2so11048192wrp.12 for ; Sun, 01 Mar 2020 23:29:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ENqf1UTfABq91DmdAY2PYELhO8RXAOQ20s5TReMs8QY=; b=QzcO2ipDDeTer1Dt1ItS7dBz4GNHDljJbNPklvCfPOVQ2J9KnHUjnBm915JX4WiAB1 gRKgkl1ooVDMPonuofkcivjVqLSBo/6tznKL+x0AgRqZTZ4WHo9Qg7YhPEh626PwJxaw 2Ob4p3DF8K3Qb1/g+SEIECRRV+CvD1nD0DUItTMFzOJd55yznWs05rz6UyrBPceTfgCi subKCwrwBUQBh4pTzer0xG7eQvNJweCmA4KqoQxKxNmmwkv+1iWRkDiA9ZiGgt48RcK1 ugHOnfZpteIHrNqKCA9GA4A3JLd5BmFNq6Id8+D6pxdgGhRoDJqka0hdrjG7YM2/71Co B1MA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ENqf1UTfABq91DmdAY2PYELhO8RXAOQ20s5TReMs8QY=; b=VuYlUtYENhZoV2pNoLTAJohKZKu+1Cs0K2aC6pkbeMiLc9aIYbZNI7UNRZPA6KFxyD OjmZqXnx6it54av5x4blIgfqhEltPwT+Y+i04qozjxa3KNqvQgB/gBJfrfgPNz8SvfkS 7TdmXYEx4DjZT/hHRfmDAwZHRd9RrpuwOyzRWKwt8mI55WwXFjKeBL5SXo3UefzrvWjh CycnbgI5Vf5jUmjymr3DRt7YBSp1tPtFQkogWAQgXlnzzh7XdGyInWv4zQ61RMjq9I0e xBC6kIyaaQ2dWsFlnHxip2cx7oUHo1bqmOWq9JgCXtY5WO5NZrciY+pQTJ1hJvY+gq2t a7vw== X-Gm-Message-State: APjAAAU4qIZCyBT0qgtyQuKK1c5/+/wpODZcIXsaiycketUhWnt1fq4h yxEfv86UpiMnksXjB41JFmGhgAIWmdXYyA== X-Google-Smtp-Source: APXvYqzm9Px15ULMrQnktAk3GTJ0c8kIB3EkR6OhORp+/Tr7COjvpStqbi76anFablXWpQHUX2oP4w== X-Received: by 2002:a5d:494c:: with SMTP id r12mr19401410wrs.50.1583134185854; Sun, 01 Mar 2020 23:29:45 -0800 (PST) Return-Path: Received: from e123331-lin.home ([2a01:cb1d:112:6f00:816e:ff0d:fb69:f613]) by smtp.gmail.com with ESMTPSA id z131sm6347153wmg.25.2020.03.01.23.29.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Mar 2020 23:29:45 -0800 (PST) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: lersek@redhat.com, Ard Biesheuvel Subject: [PATCH 04/13] OvmfPkg: provide a generic implementation of QemuLoadImageLib Date: Mon, 2 Mar 2020 08:29:27 +0100 Message-Id: <20200302072936.29221-5-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200302072936.29221-1-ard.biesheuvel@linaro.org> References: <20200302072936.29221-1-ard.biesheuvel@linaro.org> 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=2566 Signed-off-by: Ard Biesheuvel --- OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.c | 253 ++++++++++++++++++++ OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.inf | 39 +++ 2 files changed, 292 insertions(+) diff --git a/OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.c b/OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.c new file mode 100644 index 000000000000..c48c7a88dd91 --- /dev/null +++ b/OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.c @@ -0,0 +1,253 @@ +/** @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 + +#pragma pack (1) +typedef struct { + EFI_DEVICE_PATH_PROTOCOL FilePathHeader; + CHAR16 FilePath[sizeof (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 = { + { + { + 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 from + QEMU's fw_cfg. The kernel will be instructed via its command line to load + the initrd from the same Simple FileSystem. + + @param[out] ImageHandle The image handle that was allocated for + loading the image + @param[out] LoadedImage The loaded image protocol that was installed + on ImageHandle by the LoadImage boot service. + + @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. + + @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 system. + // + Status = gBS->LoadImage ( + FALSE, // BootPolicy: exact match required + gImageHandle, // ParentImageHandle + (EFI_DEVICE_PATH_PROTOCOL *)&mKernelDevicePath, + NULL, // SourceBuffer + 0, // SourceSize + &KernelImageHandle + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: LoadImage(): %r\n", __FUNCTION__, Status)); + return Status; + } + + // + // Construct the kernel command line. + // + Status = gBS->OpenProtocol ( + KernelImageHandle, + &gEfiLoadedImageProtocolGuid, + (VOID **)&KernelLoadedImage, + gImageHandle, // AgentHandle + NULL, // ControllerHandle + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + ASSERT_EFI_ERROR (Status); + + QemuFwCfgSelectItem (QemuFwCfgItemCommandLineSize); + CommandLineSize = (UINTN) QemuFwCfgRead64 (); + + if (CommandLineSize == 0) { + KernelLoadedImage->LoadOptionsSize = 0; + } else { + CommandLine = AllocatePool (CommandLineSize); + ASSERT (CommandLine != NULL); + + QemuFwCfgSelectItem (QemuFwCfgItemCommandLineData); + QemuFwCfgReadBytes (CommandLineSize, CommandLine); + + // + // Verify NUL-termination of the command line. + // + if (CommandLine[CommandLineSize - 1] != '\0') { + DEBUG ((DEBUG_ERROR, "%a: kernel command line is not NUL-terminated\n", + __FUNCTION__)); + Status = EFI_PROTOCOL_ERROR; + goto FreeCommandLine; + } + + // + // Drop the terminating NUL, convert to UTF-16. + // + KernelLoadedImage->LoadOptionsSize = (CommandLineSize - 1) * 2; + } + + QemuFwCfgSelectItem (QemuFwCfgItemInitrdSize); + InitrdSize = (UINTN) QemuFwCfgRead64 (); + + if (InitrdSize > 0) { + // + // Append ' initrd=initrd' in UTF-16. + // + KernelLoadedImage->LoadOptionsSize += sizeof (L" initrd=initrd") - 2; + } + + if (KernelLoadedImage->LoadOptionsSize == 0) { + KernelLoadedImage->LoadOptions = NULL; + } else { + // + // NUL-terminate in UTF-16. + // + KernelLoadedImage->LoadOptionsSize += 2; + + KernelLoadedImage->LoadOptions = AllocatePool ( + KernelLoadedImage->LoadOptionsSize); + if (KernelLoadedImage->LoadOptions == NULL) { + KernelLoadedImage->LoadOptionsSize = 0; + Status = EFI_OUT_OF_RESOURCES; + goto FreeCommandLine; + } + + UnicodeSPrintAsciiFormat ( + KernelLoadedImage->LoadOptions, + KernelLoadedImage->LoadOptionsSize, + "%a%a", + (CommandLineSize == 0) ? "" : CommandLine, + (InitrdSize == 0) ? "" : " initrd=initrd" + ); + DEBUG ((DEBUG_INFO, "%a: command line: \"%s\"\n", __FUNCTION__, + (CHAR16 *)KernelLoadedImage->LoadOptions)); + } + + *ImageHandle = KernelImageHandle; + return EFI_SUCCESS; + +FreeCommandLine: + FreePool (CommandLine); + gBS->UnloadImage (KernelImageHandle); + + return Status; +} + +/** + Transfer control to a kernel image loaded with QemuLoadKernelImage () + + @param[in] ImageHandle Handle of image to be started. + + @retval EFI_INVALID_PARAMETER ImageHandle is either an invalid image handle + or the image has already been initialized with + StartImage + @retval EFI_SECURITY_VIOLATION The current platform policy specifies that the + image should not be started. + + @return Error codes returned by the started image +**/ +EFI_STATUS +EFIAPI +QemuStartKernelImage ( + IN 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. + +**/ +EFI_STATUS +EFIAPI +QemuUnloadKernelImage ( + IN EFI_HANDLE ImageHandle + ) +{ + EFI_LOADED_IMAGE_PROTOCOL *KernelLoadedImage; + EFI_STATUS Status; + + Status = 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 != NULL) { + FreePool (KernelLoadedImage->LoadOptions); + KernelLoadedImage->LoadOptions = NULL; + } + KernelLoadedImage->LoadOptionsSize = 0; + + return gBS->UnloadImage (ImageHandle); +} diff --git a/OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.inf b/OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.inf new file mode 100644 index 000000000000..cbd40e6290e0 --- /dev/null +++ b/OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.inf @@ -0,0 +1,39 @@ +## @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 = 1.27 + BASE_NAME = GenericQemuLoadImageLib + FILE_GUID = 9e3e28da-c7b5-4f85-841a-84e6a9a1f1a0 + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = QemuLoadImageLib|DXE_DRIVER + +[Sources] + GenericQemuLoadImageLib.c + +[Packages] + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + OvmfPkg/OvmfPkg.dec + +[LibraryClasses] + DebugLib + MemoryAllocationLib + PrintLib + QemuFwCfgLib + ReportStatusCodeLib + UefiBootServicesTableLib + +[Protocols] + gEfiDevicePathProtocolGuid + gEfiLoadedImageProtocolGuid + +[Guids] + gQemuKernelLoaderFsMediaGuid -- 2.17.1