From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2a00:1450:4864:20::536; helo=mail-ed1-x536.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) (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 C7F18211350E5 for ; Sat, 15 Sep 2018 06:29:09 -0700 (PDT) Received: by mail-ed1-x536.google.com with SMTP id y20-v6so9522684edq.2 for ; Sat, 15 Sep 2018 06:29:09 -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=Igh65ZMfpEHInhjqoH+ggt0oB+Sf8yTvKsI17Jb6Ndc=; b=jUdxIqNr4ee+Eabh5595+Pv2uF9osqOAV93nrDjD+G2pMM4uhEXS8H3/7Lj67wML9x GSmZfh7LybR1SAHTd5uh8/eQQ8XG2zdboMA8yM6DBGcJ5H6GMQti8PWJQ7KmerEIwhRP 77iUMqDj3iqVPFOWZD23/jCl4RxXuWEv39HVQ= 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=Igh65ZMfpEHInhjqoH+ggt0oB+Sf8yTvKsI17Jb6Ndc=; b=BbDbK+tdw0jWJyWRCgC3eshou1s/Kh0xeM63d3fJMoR2AKGKH5GANV8nMHDC/Yi+xc qc7Wo7eEoN3Rb4t4XsKLgKTaw/O3QuatufWji0v4C0POMkBCtH8solKeVXT9IN3FJ9e4 KPbwK/ZfcQqidqIkntxBqd0lh1vru3VCvIAOgFkBGYD/7PZkqLgLEUGrqPyVJt4/xUbY ANG5BKUxpctlMY8D1BVVtUBUjNSMlhTnrUKrE+vHTcXYXoKYWoHdG6XS/CoNTvP69IcZ VM6XK23stiXmDiybu1yymC4sRaqxrwtsgHwnSSXMuISTCGwLPbOKOllIhVGeuBge7fJa QwDg== X-Gm-Message-State: APzg51AqoF6wsX59+F23VE6OFRmHomA5hh6PbTySz2ZBBli65ZnmMAC7 blPaTIoZlO9DA+nbJL7c8by8CiCd9gPVvQ== X-Google-Smtp-Source: ANB0VdZdgm5BpwkAZSWIKmA+s0xRkKfthM5chKKNXD/BTJPRB4saMze/ZEn6jn5Gf5pRGTRfGX7FeQ== X-Received: by 2002:a50:fa0a:: with SMTP id b10-v6mr27883570edq.138.1537018147935; Sat, 15 Sep 2018 06:29:07 -0700 (PDT) Received: from mba13.kuq.prv ([194.138.39.100]) by smtp.gmail.com with ESMTPSA id j23-v6sm4449262edh.29.2018.09.15.06.29.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 15 Sep 2018 06:29:07 -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: Sat, 15 Sep 2018 15:28:54 +0200 Message-Id: <20180915132859.25727-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180915132859.25727-1-ard.biesheuvel@linaro.org> References: <20180915132859.25727-1-ard.biesheuvel@linaro.org> Subject: [PATCH v2 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: Sat, 15 Sep 2018 13:29:10 -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.0 Signed-off-by: Ard Biesheuvel --- MdeModulePkg/Core/Dxe/DxeMain.h | 3 + MdeModulePkg/Core/Dxe/DxeMain.inf | 1 + MdeModulePkg/Core/Dxe/Image/Image.c | 102 +++++++++++++++++--- 3 files changed, 95 insertions(+), 11 deletions(-) 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..0a4bb3644af0 100644 --- a/MdeModulePkg/Core/Dxe/Image/Image.c +++ b/MdeModulePkg/Core/Dxe/Image/Image.c @@ -67,6 +67,7 @@ LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage = { NULL, // JumpContext 0, // Machine NULL, // Ebc + NULL, // PeCoffEmu NULL, // RuntimeData NULL // LoadedImageDevicePath }; @@ -425,6 +426,69 @@ 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 + ) +{ + EFI_STATUS Status; + UINTN HandleCount; + EFI_HANDLE *HandleBuffer; + BOOLEAN ReturnValue; + UINTN Index; + EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL *Emu; + + if (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->ImageContext.Machine) || + EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Image->ImageContext.Machine)) { + return TRUE; + } + + // + // Locate the emulator protocol to check whether it supports this + // image. + // + Status = CoreLocateHandleBuffer ( + ByProtocol, + &gEdkiiPeCoffImageEmulatorProtocolGuid, + NULL, + &HandleCount, + &HandleBuffer + ); + if (EFI_ERROR (Status)) { + return FALSE; + } + + ReturnValue = FALSE; + for (Index = 0; Index < HandleCount; Index++) { + Status = CoreHandleProtocol ( + HandleBuffer[Index], + &gEdkiiPeCoffImageEmulatorProtocolGuid, + (VOID **)&Emu + ); + ASSERT_EFI_ERROR (Status); + + if (Emu->IsImageSupported (Emu, Image->ImageContext.Machine, + Image->ImageContext.ImageType)) { + ReturnValue = TRUE; + Image->PeCoffEmu = Emu; + break; + } + } + + FreePool (HandleBuffer); + return ReturnValue; +} + /** Loads, relocates, and invokes a PE/COFF image @@ -473,16 +537,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 +749,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 +946,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 +1678,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