From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2607:f8b0:4864:20::442; helo=mail-pf1-x442.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id B78C7211E011B for ; Sun, 31 Mar 2019 20:27:26 -0700 (PDT) Received: by mail-pf1-x442.google.com with SMTP id y13so3803982pfm.11 for ; Sun, 31 Mar 2019 20:27:26 -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=1kM/d0DCxSrkBgIGWttlTHAIeRJJEiXPzmAwpx+rgwA=; b=n6JDv5vvzEiTy5OVSlvAl19IkI0ySGfU3Lg72QlFo1bPV7Q42JOz7M6Ylghes3k3M8 TsNbj28GGJ91OBj4kV9RgC8sXwumg0tfaHeki5hpM/NZJ+ejK2eyopVyLzig/y3nHuHN aJcQSMj0z5huO/LIJZltuTJGi2WY6l937yKGtlaDBdH3wNFF5tcvuPkxBAQYTI5tOHxW NL5N2Y5iJ8zMkDjJjB7tc7ZDC65Je1A0o/N5yrmyJMpsUkWIKV+97QlUsQhtffkBuji4 313ma9oHUNU8SgfZk2ZLkLjt+PLeGZysA4bv4xzMu/GWvoqk2B6zK3FU9Oym+bUQWnSE mcoA== 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=1kM/d0DCxSrkBgIGWttlTHAIeRJJEiXPzmAwpx+rgwA=; b=i2ETJ8N9WlZGki8dr2vC4+IwTPebBhsBkTerbkjTMBDq2OHRaNdugW+WJrS1cgWkrA x+At0549vvvaFLFwonDxw7BrCHr7hxGQnSbvJhBRMoBYRNlB6b9j03VVhUPKONk3GXDL 3M3cpiJvxwdroYkod3h7Hk2PquzqOBrMwJtHa3gxSvOq9Ggd+U8BsGKFj1FrMV2+jwxg M6SXSRDFzmhjMQWSlAuOaF/p94GcLY2wt38uaqWBXkwq5eNRrH2qY/pxg+kHMxppQQvj lMgjvO6lbKXaB7/S0xMz/dbzfXoGt/goY7RMWRNvJ/luJlzYbwhQjkRHpdflVgpUMHuN wjqg== X-Gm-Message-State: APjAAAVRjJe2/Em9HCmHXI8INdVxTcRxx8RdLOjQ6e1sc+NEoGOn/5Dp 18TvWm6gtNEBjP4LLfl8MTkzFDvFK2tRlQ== X-Google-Smtp-Source: APXvYqzL/EIxSaRPNwVoJhNC84poKIxYr3O0QbBPK9jKRKOdzypr/8SWLQK6wCVlj2usq88BFgfoGw== X-Received: by 2002:a63:1a1b:: with SMTP id a27mr50939665pga.59.1554089245905; Sun, 31 Mar 2019 20:27:25 -0700 (PDT) Received: from mba13.imgcgcw.net ([147.50.13.10]) by smtp.gmail.com with ESMTPSA id 16sm12758092pfz.106.2019.03.31.20.27.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 31 Mar 2019 20:27:25 -0700 (PDT) From: Ard Biesheuvel To: edk2-devel@lists.01.org Cc: Ard Biesheuvel , Vincent Zimmer , Brian Richardson , Michael D Kinney , Andrew Fish , Leif Lindholm , Star Zeng , Eric Dong , Ruiyu Ni , Liming Gao , Jaben Carsey , Steven Shi Date: Mon, 1 Apr 2019 10:27:04 +0700 Message-Id: <20190401032709.14787-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190401032709.14787-1-ard.biesheuvel@linaro.org> References: <20190401032709.14787-1-ard.biesheuvel@linaro.org> Subject: [PATCH v4 2/7] MdeModulePkg/DxeCore: invoke the emulator protocol for foreign images X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 01 Apr 2019 03:27:26 -0000 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. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Ard Biesheuvel --- MdeModulePkg/Core/Dxe/DxeMain.h | 3 + MdeModulePkg/Core/Dxe/DxeMain.inf | 1 + MdeModulePkg/Core/Dxe/Image/Image.c | 144 ++++++++++++++++++-- 3 files changed, 137 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..cda447d23ec5 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,39 @@ 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; + EMULATOR_ENTRY *Entry; + + Entry = AllocateZeroPool (sizeof *Entry); + ASSERT (Entry != NULL); + + Status = CoreLocateProtocol (&gEdkiiPeCoffImageEmulatorProtocolGuid, + mPeCoffEmuProtocolNotifyRegistration, + (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 +235,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 +492,45 @@ 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 that 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 +**/ +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 +579,14 @@ 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_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; } // @@ -687,6 +791,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 +988,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 +1720,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