From patchwork Thu Sep 20 23:01:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 147169 Delivered-To: patch@linaro.org Received: by 2002:a2e:1648:0:0:0:0:0 with SMTP id 8-v6csp99965ljw; Thu, 20 Sep 2018 16:01:57 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbPWRXDwDp4mKL4EmhJ1IXOxCpMt4oUhSefdCD1MavAAo04bp7i28aE6V5+7jR0MnLIcbY3 X-Received: by 2002:a63:3207:: with SMTP id y7-v6mr38999321pgy.101.1537484517829; Thu, 20 Sep 2018 16:01:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537484517; cv=none; d=google.com; s=arc-20160816; b=jSZTYQM6XK5qy50dmJnNn2TUaS+ex/KGR6yLzclKYceKaJyUNU0rL5LkRdAdR/O2Eb dE3zCvnWzWuS3BFhpThnEtQCBULRE5sfIxfr/e6b8QoesJZHIodThQ6c29gG1A5Un9hy 383ZRLYrCIj1JTzCXrs/qyCHzeVUutIWbTk+MtR5ciTVV1bvhqVC9EteAxnBJJ2Tf3nS N6foQR4q3XMFgXNpcqJpWjVaV5l8jtVWYT5CKkSuVqQc5tQedzquqO9trB2bR5pCeFQF b7XLtIQbciHqp15Rh5OszEre+gXICpfh098fUVpwmLPEIy3HZYZQd4EEpSxkWZ3IrR3s TEpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to; bh=YzkxBza9TP7noUwn42g+HXYxgknKZFfGzjzjFhUfp1M=; b=mRikDq7QPiMvM8AjWt0AdxDnwdmZ/WGrPQ4/2oPztum3rHWUn+y4V56KbPK/d3aCw+ jI2mv+mhnmEo2bsym4LiltBDcyI+HLkbV8Bytw2PPpISK3drec7Bm2fZB5uMkV75UQq+ e42V9UdmSBYU88Zsuz36FQFlnBhxNfulLUXWEy9pa5S3+oEQ1gZPHPOttxArRzU5Yqz9 qv4GEVGvc/xE19UFF8fe7tdlNZG2AmvxFwmyGCtAV84TApjmys5Zm990hTKFP4MsfM7y khma+Ug8mZrgxP8OP7Dm36c9gyyxnC2wZiBbK5Asa/d6EGkn3rfSR1NJPRHKC2XgA8GR q8gw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=XFUxCUYP; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ml01.01.org (ml01.01.org. [2001:19d0:306:5::1]) by mx.google.com with ESMTPS id r2-v6si23988023pgk.452.2018.09.20.16.01.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Sep 2018 16:01:57 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) client-ip=2001:19d0:306:5::1; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=XFUxCUYP; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 7B43C2114A6AB; Thu, 20 Sep 2018 16:01:54 -0700 (PDT) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2607:f8b0:4864:20::641; helo=mail-pl1-x641.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-pl1-x641.google.com (mail-pl1-x641.google.com [IPv6:2607:f8b0:4864:20::641]) (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 B8A0721148ED9 for ; Thu, 20 Sep 2018 16:01:53 -0700 (PDT) Received: by mail-pl1-x641.google.com with SMTP id w14-v6so5020917plp.6 for ; Thu, 20 Sep 2018 16:01:53 -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=0+ayhj9g+JFy2E7nqN1mtbqnYOoYL8T7VFgqH5NlwNU=; b=XFUxCUYPvcIeA2nTjMUbyZvnzKJfGyOuD5MeMAvcg8KEcYRoggK/AqCBjoWdDGr7le TJqpkZIMZ/s7onp9ZXZtWNm3braiH7bIiVnFp7CRlXgcRoRB8eK7rztxPfnVgiGtJGt2 B3cS+6lqhZDePRSP4fDGIthNSYn0e05OinipQ= 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=0+ayhj9g+JFy2E7nqN1mtbqnYOoYL8T7VFgqH5NlwNU=; b=qWYSyD8s3TVKqdhXRnsV6bE5GM6P8VL12a5JgVQkqLJzkWDTMtkildFyPM9LxCNl9Q +++KXYqTZ59shIUsdC0jQ2S62o0LBOfeH+EdTCT5MouirIifV7JK15Mf8EWJRujmqJmm XB7DGLZ3HsSwmy6janess2QnBlOCg7fPCUk/34H+N1sO6r1ZCm9BuGwZHxhcK6GHCS5k f7Y4fRzS/YEp3xj5aR9ScoMhuq2lvMk4PL6spl4cXMOCF1GktWUm/Q3EOxvIHtMLEp9l wjwlYYTXzkBDLdr2VDF4KGu7g4uzLu0Gv+lhZLFfLgUdGaSeAIP5HjZtY9yc6V+Tk8ff sW6Q== X-Gm-Message-State: APzg51ChmsMWa1B+5QKkqC1iv0V7IOaJXCkHJGaBti5mHaxA0Y2p1FE3 c4g44aenoW3MyRNP5njr0GLwnsxiSWk= X-Received: by 2002:a17:902:6907:: with SMTP id j7-v6mr41672542plk.323.1537484513054; Thu, 20 Sep 2018 16:01:53 -0700 (PDT) Received: from localhost.localdomain ([209.121.128.187]) by smtp.gmail.com with ESMTPSA id p4-v6sm37507823pfd.65.2018.09.20.16.01.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Sep 2018 16:01:51 -0700 (PDT) From: Ard Biesheuvel To: edk2-devel@lists.01.org Date: Thu, 20 Sep 2018 16:01:40 -0700 Message-Id: <20180920230145.7565-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180920230145.7565-1-ard.biesheuvel@linaro.org> References: <20180920230145.7565-1-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH v3 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: , Cc: Ruiyu Ni , Vincent Zimmer , Eric Dong , Andrew Fish , Jaben Carsey , Brian Richardson , Liming Gao , Michael D Kinney , Star Zeng MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" 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 | 139 ++++++++++++++++++-- MdeModulePkg/Core/Dxe/Image/Image.h | 1 + 4 files changed, 133 insertions(+), 11 deletions(-) -- 2.17.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel diff --git a/MdeModulePkg/Core/Dxe/DxeMain.h b/MdeModulePkg/Core/Dxe/DxeMain.h index 7ec82388a3f9..ff2418c5ae5e 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 @@ -229,6 +230,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 68fa0a01d9bd..63e650ee7c27 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.inf +++ b/MdeModulePkg/Core/Dxe/DxeMain.inf @@ -164,6 +164,7 @@ gEfiEbcProtocolGuid ## SOMETIMES_CONSUMES gEfiSmmBase2ProtocolGuid ## SOMETIMES_CONSUMES gEfiBlockIoProtocolGuid ## 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..dd987f7fcea7 100644 --- a/MdeModulePkg/Core/Dxe/Image/Image.c +++ b/MdeModulePkg/Core/Dxe/Image/Image.c @@ -29,6 +29,14 @@ LOAD_PE32_IMAGE_PRIVATE_DATA mLoadPe32PrivateData = { } }; +STATIC +EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL *mAvailableEmulators[MAX_NUM_EMULATORS]; + +STATIC +EFI_EVENT mPeCoffEmuProtocolRegistrationEvent; + +STATIC +VOID *mPeCoffEmuProtocolNotifyRegistration; // // This code is needed to build the Image handle for the DXE Core @@ -67,6 +75,7 @@ LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage = { NULL, // JumpContext 0, // Machine NULL, // Ebc + NULL, // PeCoffEmu NULL, // RuntimeData NULL // LoadedImageDevicePath }; @@ -118,6 +127,41 @@ 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 Index; + + for (Index = 0; Index < MAX_NUM_EMULATORS; Index++) { + if (mAvailableEmulators[Index] == NULL) { + break; + } + } + + // ensure that there is still room in the emulator protocol array + ASSERT (Index < MAX_NUM_EMULATORS); + + Status = CoreLocateProtocol (&gEdkiiPeCoffImageEmulatorProtocolGuid, + mPeCoffEmuProtocolNotifyRegistration, + (VOID **)&mAvailableEmulators[Index] + ); + ASSERT_EFI_ERROR (Status); +} + /** Add the Image Services to EFI Boot Services Table and install the protocol interfaces for this image. @@ -192,6 +236,28 @@ 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); + if (FeaturePcdGet (PcdFrameworkCompatibilitySupport)) { // // Export DXE Core PE Loader functionality for backward compatibility. @@ -425,6 +491,41 @@ 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 + ) +{ + UINTN Index; + EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL *Emu; + + for (Index = 0; Index < MAX_NUM_EMULATORS; Index++) { + Emu = mAvailableEmulators[Index]; + if (Emu == NULL) { + break; + } + + if (Emu->IsImageSupported (Emu, Image->ImageContext.Machine, + Image->ImageContext.ImageType, NULL)) { + Image->PeCoffEmu = Emu; + 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 +574,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 +786,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 +983,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 +1715,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. diff --git a/MdeModulePkg/Core/Dxe/Image/Image.h b/MdeModulePkg/Core/Dxe/Image/Image.h index 5cff84fee094..021d516f46e2 100644 --- a/MdeModulePkg/Core/Dxe/Image/Image.h +++ b/MdeModulePkg/Core/Dxe/Image/Image.h @@ -28,6 +28,7 @@ typedef struct { #define LOAD_PE32_IMAGE_PRIVATE_DATA_FROM_THIS(a) \ CR(a, LOAD_PE32_IMAGE_PRIVATE_DATA, Pe32Image, LOAD_PE32_IMAGE_PRIVATE_DATA_SIGNATURE) +#define MAX_NUM_EMULATORS 4 // // Private Data Types