From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=pass header.i=@linaro.org header.s=google header.b=N3uIXFNF; spf=pass (domain: linaro.org, ip: 209.85.214.193, mailfrom: ard.biesheuvel@linaro.org) Received: from mail-pl1-f193.google.com (mail-pl1-f193.google.com [209.85.214.193]) by groups.io with SMTP; Sun, 14 Apr 2019 12:52:55 -0700 Received: by mail-pl1-f193.google.com with SMTP id ck15so7513799plb.3 for ; Sun, 14 Apr 2019 12:52:55 -0700 (PDT) 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=fghCYKGrvN+SMjnLR3SsNVAEHygggYW1K5FcxeeLSTY=; b=N3uIXFNFmiVl13sfbGO2zGU6s4fP4icmqtebyt1fBgDo4SMqAtQLYWx4VzscmkGD+H YaGXmCrFmwG6gEMuFFXb9rGukz1+X+JYVNtrWx7XIAPAieSqe6GHWs0Pwq23i9fPtk5Z n0DFOPpsSUOm8pId50VHzCl49S3wvzU1PlRDWX5soH8+hu6aU7terdrESSro5srptB7E +kWfoaS0isr6XUNKDY9Gl9WNfpJVNAzt5mZt+7jWDroLVJU6Ej3PnJbAn2SCt8WJh/Aj geCflhyhRDqGXmaPDibWhH3+uL/1lw+L8vPAW82Z6CjUgzcuxFYeX+WvflItO/lDa63M 4wiA== 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=fghCYKGrvN+SMjnLR3SsNVAEHygggYW1K5FcxeeLSTY=; b=mtlKn0fJ63faevv+TQ3J8lrSqlhwqFehem0NH3y2v9Gg4ekzvb8p5v+rVOlUMsv0bW 8G9JaKV9KllkywNzjnwTRMYVHVch7qM1YXfQlOmH1PSpGrhZ5HBVfdgNOpKEzK1quPVa MOSO0DHJNW+qSPOq/G+rXISzKm5DdvNHYGH1zaTqmwrTosRK/qMTqQ7LeNM5gUKo/ZIc 6OrO8SN7BqBp0EiZXyvFftN9V6niEDeoTxoR1eRBNaRzSXYvbghkQE74pc0FHE179NGK N3R4rrC1fWmGewDNZfxATUnUFk9vrBSi9thznzayC73BjQDofe2XxAWwrT+HBtePRmy4 E5HA== X-Gm-Message-State: APjAAAVh1m+q0OZwD9Y5QBND+UknJJ9/XvQhCpM3n9A8okMZcd8nPkNY ZHuJKL0/En5QXV7mdPy1lb/jV5wWSLP+HqMt X-Google-Smtp-Source: APXvYqzYZunr5YtE3hl+0FLkkwoKOB4R8PRNdwQ6YJZ+9x3ZuafNaRv9Pu+G4Iw8Whx0pE0HiveYjQ== X-Received: by 2002:a17:902:1007:: with SMTP id b7mr47044100pla.48.1555271574569; Sun, 14 Apr 2019 12:52:54 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([50.236.184.206]) by smtp.gmail.com with ESMTPSA id j16sm62388977pfi.58.2019.04.14.12.52.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 14 Apr 2019 12:52:53 -0700 (PDT) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Michael D Kinney , Andrew Fish , Leif Lindholm , Star Zeng , Eric Dong , Ruiyu Ni , Liming Gao , Jaben Carsey , Steven Shi , Jian J Wang , Hao Wu Subject: [PATCH v6 2/7] MdeModulePkg/DxeCore: invoke the emulator protocol for foreign images Date: Sun, 14 Apr 2019 12:52:28 -0700 Message-Id: <20190414195233.30045-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190414195233.30045-1-ard.biesheuvel@linaro.org> References: <20190414195233.30045-1-ard.biesheuvel@linaro.org> When encountering PE/COFF images that cannot be supported natively, attempt to locate an instance of the PE/COFF image emulator protocol, and if it supports the image, proceed with loading it and register it with the emulator. Signed-off-by: Ard Biesheuvel Reviewed-by: Michael D Kinney --- MdeModulePkg/Core/Dxe/DxeMain.h | 3 + MdeModulePkg/Core/Dxe/DxeMain.inf | 1 + MdeModulePkg/Core/Dxe/Image/Image.c | 171 ++++++++++++++++++-- 3 files changed, 164 insertions(+), 11 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/DxeMain.h b/MdeModulePkg/Core/Dxe/DxeMain.h index 2dec9da5e35b..48ec30a48aa2 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.h +++ b/MdeModulePkg/Core/Dxe/DxeMain.h @@ -53,6 +53,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include #include +#include #include #include #include @@ -228,6 +229,8 @@ typedef struct { UINT16 Machine; /// EBC Protocol pointer EFI_EBC_PROTOCOL *Ebc; + /// PE/COFF Image Emulator Protocol pointer + EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL *PeCoffEmu; /// Runtime image list EFI_RUNTIME_IMAGE_ENTRY *RuntimeData; /// Pointer to Loaded Image Device Path Protocol diff --git a/MdeModulePkg/Core/Dxe/DxeMain.inf b/MdeModulePkg/Core/Dxe/DxeMain.inf index 10375443c0f4..ce6fc19be5e4 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.inf +++ b/MdeModulePkg/Core/Dxe/DxeMain.inf @@ -162,6 +162,7 @@ gEfiHiiPackageListProtocolGuid ## SOMETIMES_PRODUCES gEfiEbcProtocolGuid ## SOMETIMES_CONSUMES gEfiSmmBase2ProtocolGuid ## SOMETIMES_CONSUMES + gEdkiiPeCoffImageEmulatorProtocolGuid ## SOMETIMES_CONSUMES # Arch Protocols gEfiBdsArchProtocolGuid ## CONSUMES diff --git a/MdeModulePkg/Core/Dxe/Image/Image.c b/MdeModulePkg/Core/Dxe/Image/Image.c index eddca140ee1a..57330636b5f3 100644 --- a/MdeModulePkg/Core/Dxe/Image/Image.c +++ b/MdeModulePkg/Core/Dxe/Image/Image.c @@ -29,6 +29,15 @@ LOAD_PE32_IMAGE_PRIVATE_DATA mLoadPe32PrivateData = { } }; +typedef struct { + LIST_ENTRY Link; + EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL *Emulator; + UINT16 MachineType; +} EMULATOR_ENTRY; + +STATIC LIST_ENTRY mAvailableEmulators; +STATIC EFI_EVENT mPeCoffEmuProtocolRegistrationEvent; +STATIC VOID *mPeCoffEmuProtocolNotifyRegistration; // // This code is needed to build the Image handle for the DXE Core @@ -67,6 +76,7 @@ LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage = { NULL, // JumpContext 0, // Machine NULL, // Ebc + NULL, // PeCoffEmu NULL, // RuntimeData NULL // LoadedImageDevicePath }; @@ -118,6 +128,61 @@ GetMachineTypeName ( return L""; } +/** + Notification event handler registered by CoreInitializeImageServices () to + keep track of which PE/COFF image emulators are available. + + @param Event The Event that is being processed, not used. + @param Context Event Context, not used. + +**/ +STATIC +VOID +EFIAPI +PeCoffEmuProtocolNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EFI_STATUS Status; + UINTN BufferSize; + EFI_HANDLE EmuHandle; + EMULATOR_ENTRY *Entry; + + EmuHandle = NULL; + + while (TRUE) { + BufferSize = sizeof (EmuHandle); + Status = CoreLocateHandle ( + ByRegisterNotify, + NULL, + mPeCoffEmuProtocolNotifyRegistration, + &BufferSize, + &EmuHandle + ); + if (EFI_ERROR (Status)) { + // + // If no more notification events exit + // + return; + } + + Entry = AllocateZeroPool (sizeof (*Entry)); + ASSERT (Entry != NULL); + + Status = CoreHandleProtocol ( + EmuHandle, + &gEdkiiPeCoffImageEmulatorProtocolGuid, + (VOID **)&Entry->Emulator + ); + ASSERT_EFI_ERROR (Status); + + Entry->MachineType = Entry->Emulator->MachineType; + + InsertTailList (&mAvailableEmulators, &Entry->Link); + } +} + /** Add the Image Services to EFI Boot Services Table and install the protocol interfaces for this image. @@ -192,6 +257,30 @@ CoreInitializeImageServices ( gDxeCoreImageHandle = Image->Handle; gDxeCoreLoadedImage = &Image->Info; + // + // Create the PE/COFF emulator protocol registration event + // + Status = CoreCreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + PeCoffEmuProtocolNotify, + NULL, + &mPeCoffEmuProtocolRegistrationEvent + ); + ASSERT_EFI_ERROR(Status); + + // + // Register for protocol notifications on this event + // + Status = CoreRegisterProtocolNotify ( + &gEdkiiPeCoffImageEmulatorProtocolGuid, + mPeCoffEmuProtocolRegistrationEvent, + &mPeCoffEmuProtocolNotifyRegistration + ); + ASSERT_EFI_ERROR(Status); + + InitializeListHead (&mAvailableEmulators); + if (FeaturePcdGet (PcdFrameworkCompatibilitySupport)) { // // Export DXE Core PE Loader functionality for backward compatibility. @@ -425,6 +514,49 @@ GetPeCoffImageFixLoadingAssignedAddress( DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address 0x%11p. Status = %r \n", (VOID *)(UINTN)(ImageContext->ImageAddress), Status)); return Status; } + +/** + Decides whether a PE/COFF image can execute on this system, either natively + or via emulation/interpretation. In the latter case, the PeCoffEmu member + of the LOADED_IMAGE_PRIVATE_DATA struct pointer is populated with a pointer + to the emulator protocol that supports this image. + + @param[in] Image LOADED_IMAGE_PRIVATE_DATA struct pointer + + @retval TRUE The image is supported + @retval FALSE The image is not supported + +**/ +STATIC +BOOLEAN +CoreIsImageTypeSupported ( + IN OUT LOADED_IMAGE_PRIVATE_DATA *Image + ) +{ + LIST_ENTRY *Link; + EMULATOR_ENTRY *Entry; + + for (Link = GetFirstNode (&mAvailableEmulators); + !IsNull (&mAvailableEmulators, Link); + Link = GetNextNode (&mAvailableEmulators, Link)) { + + Entry = BASE_CR (Link, EMULATOR_ENTRY, Link); + if (Entry->MachineType != Image->ImageContext.Machine) { + continue; + } + + if (Entry->Emulator->IsImageSupported (Entry->Emulator, + Image->ImageContext.ImageType, + Image->Info.FilePath)) { + Image->PeCoffEmu = Entry->Emulator; + return TRUE; + } + } + + return EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->ImageContext.Machine) || + EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image->ImageContext.Machine); +} + /** Loads, relocates, and invokes a PE/COFF image @@ -473,16 +605,15 @@ CoreLoadPeImage ( return Status; } - if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->ImageContext.Machine)) { - if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image->ImageContext.Machine)) { - // - // The PE/COFF loader can support loading image types that can be executed. - // If we loaded an image type that we can not execute return EFI_UNSUPORTED. - // - DEBUG ((EFI_D_ERROR, "Image type %s can't be loaded ", GetMachineTypeName(Image->ImageContext.Machine))); - DEBUG ((EFI_D_ERROR, "on %s UEFI system.\n", GetMachineTypeName(mDxeCoreImageMachineType))); - return EFI_UNSUPPORTED; - } + if (!CoreIsImageTypeSupported (Image)) { + // + // The PE/COFF loader can support loading image types that can be executed. + // If we loaded an image type that we can not execute return EFI_UNSUPPORTED. + // + DEBUG ((DEBUG_ERROR, "Image type %s can't be loaded on %s UEFI system.\n", + GetMachineTypeName (Image->ImageContext.Machine), + GetMachineTypeName (mDxeCoreImageMachineType))); + return EFI_UNSUPPORTED; } // @@ -687,6 +818,16 @@ CoreLoadPeImage ( if (EFI_ERROR(Status)) { goto Done; } + } else if (Image->PeCoffEmu != NULL) { + Status = Image->PeCoffEmu->RegisterImage (Image->PeCoffEmu, + Image->ImageBasePage, + EFI_PAGES_TO_SIZE (Image->NumberOfPages), + &Image->EntryPoint); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_LOAD | DEBUG_ERROR, + "CoreLoadPeImage: Failed to register foreign image with emulator.\n")); + goto Done; + } } // @@ -874,6 +1015,13 @@ CoreUnloadAndCloseImage ( Image->Ebc->UnloadImage (Image->Ebc, Image->Handle); } + if (Image->PeCoffEmu != NULL) { + // + // If the PE/COFF Emulator protocol exists we must unregister the image. + // + Image->PeCoffEmu->UnregisterImage (Image->PeCoffEmu, Image->ImageBasePage); + } + // // Unload image, free Image->ImageContext->ModHandle // @@ -1599,7 +1747,8 @@ CoreStartImage ( // // The image to be started must have the machine type supported by DxeCore. // - if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->Machine)) { + if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->Machine) && + Image->PeCoffEmu == NULL) { // // Do not ASSERT here, because image might be loaded via EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED // But it can not be started. -- 2.17.1