public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH v3 0/7] MdeModulePkg: add support for dispatching foreign arch PE/COFF images
@ 2018-09-20 23:01 Ard Biesheuvel
  2018-09-20 23:01 ` [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol Ard Biesheuvel
                   ` (6 more replies)
  0 siblings, 7 replies; 22+ messages in thread
From: Ard Biesheuvel @ 2018-09-20 23:01 UTC (permalink / raw)
  To: edk2-devel
  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

Add the basic plumbing to DXE core, the PCI bus driver and the boot manager
to allow PE/COFF images to be dispatched that target an architecture that is
not native for the platform, but which is supported by one of potentially
several available emulators.

One implementation of such an emulator can be found here:
https://github.com/ardbiesheuvel/X86EmulatorPkg

This also allows us to get rid of the special treatment of EBC images in
core code. Instead, the EbcDxe driver is augmented with an implementation
of the EDK2 PE/COFF image emulator so that internal knowledge of how EBC
is implemented (I-cache flushing, thunks) is removed from the DXE core.

Changes since v2:
- incorporate feedback from Andrew Fish (delivered in person):
  * pass a device path into the IsImageSupported() protocol method so that an
    implementation can blacklist or whitelist certain devices, or implement
    other policies that depend on the device where the driver originated
  * allow the emulator to supersede the native loading of the image - this
    permits things like X86 on X86 emulators for security sandboxing or debug

Changes since v1:
- subsume the EBC handling into the EDK2 emulator protocol and abstract
  away from EBC specifics in core code.
- allow multiple emulator implementations to co-exist
- incorporate Star's review feedback

Cc: Vincent Zimmer <vincent.zimmer@intel.com>
Cc: Brian Richardson <brian.richardson@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Andrew Fish <afish@apple.com>
Cc: Leif Lindholm <leif.lindholm@linaro.org>
Cc: Star Zeng <star.zeng@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Jaben Carsey <jaben.carsey@intel.com>
Cc: Steven Shi <steven.shi@intel.com>

Ard Biesheuvel (7):
  MdeModulePkg: introduce PE/COFF image emulator protocol
  MdeModulePkg/DxeCore: invoke the emulator protocol for foreign images
  MdeModulePkg/PciBusDxe: invoke PE/COFF emulator for foreign option
    ROMs
  MdeModulePkg/UefiBootManagerLib: allow foreign Driver#### images
  MdeModulePkg/EbcDxe: implement the PE/COFF emulator protocol
  MdePkg/UefiBaseType.h: treat EBC as a non-native machine type
  MdeModulePkg/DxeCore: remove explicit EBC handling

 MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h       |   1 +
 MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf  |   1 +
 .../Bus/Pci/PciBusDxe/PciOptionRomSupport.c   |  53 +++++-
 MdeModulePkg/Core/Dxe/DxeMain.h               |   6 +-
 MdeModulePkg/Core/Dxe/DxeMain.inf             |   2 +-
 MdeModulePkg/Core/Dxe/Image/Image.c           | 178 ++++++++++++------
 MdeModulePkg/Core/Dxe/Image/Image.h           |   1 +
 .../Include/Protocol/PeCoffImageEmulator.h    | 102 ++++++++++
 .../Library/UefiBootManagerLib/BmLoadOption.c |  51 ++++-
 .../Library/UefiBootManagerLib/InternalBm.h   |   1 +
 .../UefiBootManagerLib/UefiBootManagerLib.inf |   1 +
 MdeModulePkg/MdeModulePkg.dec                 |   4 +
 MdeModulePkg/Universal/EbcDxe/EbcDxe.inf      |   3 +
 MdeModulePkg/Universal/EbcDxe/EbcInt.c        | 127 +++++++++++++
 MdeModulePkg/Universal/EbcDxe/EbcInt.h        |   3 +
 MdePkg/Include/Uefi/UefiBaseType.h            |   8 +-
 16 files changed, 478 insertions(+), 64 deletions(-)
 create mode 100644 MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h

-- 
2.17.1



^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol
  2018-09-20 23:01 [PATCH v3 0/7] MdeModulePkg: add support for dispatching foreign arch PE/COFF images Ard Biesheuvel
@ 2018-09-20 23:01 ` Ard Biesheuvel
  2018-09-26  9:58   ` Zeng, Star
  2018-09-20 23:01 ` [PATCH v3 2/7] MdeModulePkg/DxeCore: invoke the emulator protocol for foreign images Ard Biesheuvel
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 22+ messages in thread
From: Ard Biesheuvel @ 2018-09-20 23:01 UTC (permalink / raw)
  To: edk2-devel
  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

Introduce a protocol that can be invoked by the image loading services
to execute foreign architecture PE/COFF images via an emulator.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h | 102 ++++++++++++++++++++
 MdeModulePkg/MdeModulePkg.dec                       |   4 +
 2 files changed, 106 insertions(+)

diff --git a/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
new file mode 100644
index 000000000000..27bad556209c
--- /dev/null
+++ b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
@@ -0,0 +1,102 @@
+/** @file
+  Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>
+
+  This program and the accompanying materials are licensed and made available
+  under the terms and conditions of the BSD License which accompanies this
+  distribution.  The full text of the license may be found at
+  http://opensource.org/licenses/bsd-license.php
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
+#define PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
+
+#define EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID \
+  { 0x96F46153, 0x97A7, 0x4793, { 0xAC, 0xC1, 0xFA, 0x19, 0xBF, 0x78, 0xEA, 0x97 } }
+
+typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
+
+/**
+  Check whether the emulator supports executing a certain PE/COFF image
+
+  @param[in] This         This pointer for EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
+                          structure
+  @param[in] MachineType  The machine type for which the image was built
+  @param[in] ImageType    Whether the image is an application, a boot time
+                          driver or a runtime driver.
+  @param[in] DevicePath   Path to device where the image originated
+                          (e.g., a PCI option ROM)
+
+  @retval TRUE            The image is supported by the emulator
+  @retval FALSE           The image is not supported by the emulator.
+**/
+typedef
+BOOLEAN
+(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED) (
+  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
+  IN  UINT16                                  MachineType,
+  IN  UINT16                                  ImageType,
+  IN  EFI_DEVICE_PATH_PROTOCOL                *DevicePath   OPTIONAL
+  );
+
+/**
+  Register a supported PE/COFF image with the emulator. After this call
+  completes successfully, the PE/COFF image may be started as usual, and
+  it is the responsibility of the emulator implementation that any branch
+  into the code section of the image (including returns from functions called
+  from the foreign code) is executed as if it were running on the machine
+  type it was built for.
+
+  @param[in]      This          This pointer for
+                                EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL structure
+  @param[in]      ImageBase     The base address in memory of the PE/COFF image
+  @param[in]      ImageSize     The size in memory of the PE/COFF image
+  @param[in,out]  EntryPoint    The entry point of the PE/COFF image. Passed by
+                                reference so that the emulator may modify it.
+
+  @retval EFI_SUCCESS           The image was registered with the emulator and
+                                can be started as usual.
+  @retval other                 The image could not be registered.
+
+  If the PE/COFF machine type or image type are not supported by the emulator,
+  then ASSERT().
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE) (
+  IN      EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
+  IN      EFI_PHYSICAL_ADDRESS                    ImageBase,
+  IN      UINT64                                  ImageSize,
+  IN  OUT EFI_IMAGE_ENTRY_POINT                   *EntryPoint
+  );
+
+/**
+  Unregister a PE/COFF image that has been registered with the emulator.
+  This should be done before the image is unloaded from memory.
+
+  @param[in] This         This pointer for EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
+                          structure
+  @param[in] ImageBase    The base address in memory of the PE/COFF image
+
+  @retval EFI_SUCCESS     The image was unregistered with the emulator.
+  @retval other           Image could not be unloaded.
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE) (
+  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
+  IN  EFI_PHYSICAL_ADDRESS                    ImageBase
+  );
+
+typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL {
+  EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED    IsImageSupported;
+  EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE        RegisterImage;
+  EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE      UnregisterImage;
+} EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
+
+extern EFI_GUID gEdkiiPeCoffImageEmulatorProtocolGuid;
+
+#endif
diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec
index 6a6d9660edc2..be307329f901 100644
--- a/MdeModulePkg/MdeModulePkg.dec
+++ b/MdeModulePkg/MdeModulePkg.dec
@@ -617,6 +617,10 @@
 
   ## Include/Protocol/AtaAtapiPolicy.h
   gEdkiiAtaAtapiPolicyProtocolGuid = { 0xe59cd769, 0x5083, 0x4f26,{ 0x90, 0x94, 0x6c, 0x91, 0x9f, 0x91, 0x6c, 0x4e } }
+
+  ## Include/Protocol/PeCoffImageEmulator.h
+  gEdkiiPeCoffImageEmulatorProtocolGuid = { 0x96f46153, 0x97a7, 0x4793, { 0xac, 0xc1, 0xfa, 0x19, 0xbf, 0x78, 0xea, 0x97 } }
+
 #
 # [Error.gEfiMdeModulePkgTokenSpaceGuid]
 #   0x80000001 | Invalid value provided.
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 22+ messages in thread

* [PATCH v3 2/7] MdeModulePkg/DxeCore: invoke the emulator protocol for foreign images
  2018-09-20 23:01 [PATCH v3 0/7] MdeModulePkg: add support for dispatching foreign arch PE/COFF images Ard Biesheuvel
  2018-09-20 23:01 ` [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol Ard Biesheuvel
@ 2018-09-20 23:01 ` Ard Biesheuvel
  2018-09-20 23:01 ` [PATCH v3 3/7] MdeModulePkg/PciBusDxe: invoke PE/COFF emulator for foreign option ROMs Ard Biesheuvel
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 22+ messages in thread
From: Ard Biesheuvel @ 2018-09-20 23:01 UTC (permalink / raw)
  To: edk2-devel
  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

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 <ard.biesheuvel@linaro.org>
---
 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(-)

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 <Protocol/TcgService.h>
 #include <Protocol/HiiPackageList.h>
 #include <Protocol/SmmBase2.h>
+#include <Protocol/PeCoffImageEmulator.h>
 #include <Guid/MemoryTypeInformation.h>
 #include <Guid/FirmwareFileSystem2.h>
 #include <Guid/FirmwareFileSystem3.h>
@@ -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"<Unknown>";
 }
 
+/**
+  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
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 22+ messages in thread

* [PATCH v3 3/7] MdeModulePkg/PciBusDxe: invoke PE/COFF emulator for foreign option ROMs
  2018-09-20 23:01 [PATCH v3 0/7] MdeModulePkg: add support for dispatching foreign arch PE/COFF images Ard Biesheuvel
  2018-09-20 23:01 ` [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol Ard Biesheuvel
  2018-09-20 23:01 ` [PATCH v3 2/7] MdeModulePkg/DxeCore: invoke the emulator protocol for foreign images Ard Biesheuvel
@ 2018-09-20 23:01 ` Ard Biesheuvel
  2018-09-26 18:26   ` Kinney, Michael D
  2018-09-20 23:01 ` [PATCH v3 4/7] MdeModulePkg/UefiBootManagerLib: allow foreign Driver#### images Ard Biesheuvel
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 22+ messages in thread
From: Ard Biesheuvel @ 2018-09-20 23:01 UTC (permalink / raw)
  To: edk2-devel
  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

When enumerating option ROM images, take into account whether an emulator
exists that would allow dispatch of PE/COFF images built for foreign
architectures.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h              |  1 +
 MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf         |  1 +
 MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c | 53 +++++++++++++++++++-
 3 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h
index 55eb3a5a8070..dc57d4876c0f 100644
--- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h
+++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h
@@ -33,6 +33,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Protocol/PciOverride.h>
 #include <Protocol/PciEnumerationComplete.h>
 #include <Protocol/IoMmu.h>
+#include <Protocol/PeCoffImageEmulator.h>
 
 #include <Library/DebugLib.h>
 #include <Library/UefiDriverEntryPoint.h>
diff --git a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
index a21dd2b5edf4..c8b861093292 100644
--- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
+++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
@@ -96,6 +96,7 @@
   gEfiIncompatiblePciDeviceSupportProtocolGuid    ## SOMETIMES_CONSUMES
   gEfiLoadFile2ProtocolGuid                       ## SOMETIMES_PRODUCES
   gEdkiiIoMmuProtocolGuid                         ## SOMETIMES_CONSUMES
+  gEdkiiPeCoffImageEmulatorProtocolGuid           ## SOMETIMES_CONSUMES
   gEfiLoadedImageDevicePathProtocolGuid           ## CONSUMES
 
 [FeaturePcd]
diff --git a/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c b/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
index c2be85a906af..085bd5d571bd 100644
--- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
+++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
@@ -651,6 +651,55 @@ RomDecode (
   }
 }
 
+STATIC
+BOOLEAN
+IsImageTypeSupported (
+  IN  UINT16                    MachineType,
+  IN  UINT16                    SubSystem,
+  IN  EFI_DEVICE_PATH_PROTOCOL  *DevicePath
+  )
+{
+  EFI_STATUS                            Status;
+  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL  *Emu;
+  UINTN                                 HandleCount;
+  EFI_HANDLE                            *HandleBuffer;
+  BOOLEAN                               ReturnValue;
+  UINTN                                 Index;
+
+  if (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (MachineType)) {
+    return TRUE;
+  }
+
+  Status = gBS->LocateHandleBuffer (
+                  ByProtocol,
+                  &gEdkiiPeCoffImageEmulatorProtocolGuid,
+                  NULL,
+                  &HandleCount,
+                  &HandleBuffer
+                  );
+  if (EFI_ERROR (Status)) {
+    return FALSE;
+  }
+
+  ReturnValue = FALSE;
+  for (Index = 0; Index < HandleCount; Index++) {
+    Status = gBS->HandleProtocol (
+                    HandleBuffer[Index],
+                    &gEdkiiPeCoffImageEmulatorProtocolGuid,
+                    (VOID **)&Emu
+                    );
+    ASSERT_EFI_ERROR (Status);
+
+    if (Emu->IsImageSupported (Emu, MachineType, SubSystem, DevicePath)) {
+      ReturnValue = TRUE;
+      break;
+    }
+  }
+
+  FreePool (HandleBuffer);
+  return ReturnValue;
+}
+
 /**
   Load and start the Option Rom image.
 
@@ -715,7 +764,9 @@ ProcessOpRomImage (
     //
     // Skip the EFI PCI Option ROM image if its machine type is not supported
     //
-    if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (EfiRomHeader->EfiMachineType)) {
+    if (!IsImageTypeSupported(EfiRomHeader->EfiMachineType,
+                              EfiRomHeader->EfiSubsystem,
+                              PciDevice->DevicePath)) {
       goto NextImage;
     }
 
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 22+ messages in thread

* [PATCH v3 4/7] MdeModulePkg/UefiBootManagerLib: allow foreign Driver#### images
  2018-09-20 23:01 [PATCH v3 0/7] MdeModulePkg: add support for dispatching foreign arch PE/COFF images Ard Biesheuvel
                   ` (2 preceding siblings ...)
  2018-09-20 23:01 ` [PATCH v3 3/7] MdeModulePkg/PciBusDxe: invoke PE/COFF emulator for foreign option ROMs Ard Biesheuvel
@ 2018-09-20 23:01 ` Ard Biesheuvel
  2018-09-26 23:34   ` Kinney, Michael D
  2018-09-20 23:01 ` [PATCH v3 5/7] MdeModulePkg/EbcDxe: implement the PE/COFF emulator protocol Ard Biesheuvel
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 22+ messages in thread
From: Ard Biesheuvel @ 2018-09-20 23:01 UTC (permalink / raw)
  To: edk2-devel
  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

Allow PE/COFF images that must execute under emulation for Driver####
options, by relaxing the machine type check to include support for
machine types that is provided by an emulator.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c         | 51 +++++++++++++++++++-
 MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h           |  1 +
 MdeModulePkg/Library/UefiBootManagerLib/UefiBootManagerLib.inf |  1 +
 3 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
index 7bf96646c690..f6fda8f2c3f7 100644
--- a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
+++ b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
@@ -1185,6 +1185,54 @@ EfiBootManagerFreeLoadOptions (
   return EFI_SUCCESS;
 }
 
+STATIC
+BOOLEAN
+BmIsImageTypeSupported (
+  IN  UINT16    MachineType,
+  IN  UINT16    SubSystem
+  )
+{
+  EFI_STATUS                            Status;
+  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL  *Emu;
+  UINTN                                 HandleCount;
+  EFI_HANDLE                            *HandleBuffer;
+  BOOLEAN                               ReturnValue;
+  UINTN                                 Index;
+
+  if (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (MachineType)) {
+    return TRUE;
+  }
+
+  Status = gBS->LocateHandleBuffer (
+                  ByProtocol,
+                  &gEdkiiPeCoffImageEmulatorProtocolGuid,
+                  NULL,
+                  &HandleCount,
+                  &HandleBuffer
+                  );
+  if (EFI_ERROR (Status)) {
+    return FALSE;
+  }
+
+  ReturnValue = FALSE;
+  for (Index = 0; Index < HandleCount; Index++) {
+    Status = gBS->HandleProtocol (
+                    HandleBuffer[Index],
+                    &gEdkiiPeCoffImageEmulatorProtocolGuid,
+                    (VOID **)&Emu
+                    );
+    ASSERT_EFI_ERROR (Status);
+
+    if (Emu->IsImageSupported (Emu, MachineType, SubSystem, NULL)) {
+      ReturnValue = TRUE;
+      break;
+    }
+  }
+
+  FreePool (HandleBuffer);
+  return ReturnValue;
+}
+
 /**
   Return whether the PE header of the load option is valid or not.
 
@@ -1235,7 +1283,8 @@ BmIsLoadOptionPeHeaderValid (
       OptionalHeader = (EFI_IMAGE_OPTIONAL_HEADER32 *) &PeHeader->Pe32.OptionalHeader;
       if ((OptionalHeader->Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC ||
            OptionalHeader->Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) &&
-          EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeHeader->Pe32.FileHeader.Machine)
+          BmIsImageTypeSupported (PeHeader->Pe32.FileHeader.Machine,
+                                  OptionalHeader->Subsystem)
           ) {
         //
         // Check the Subsystem:
diff --git a/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h b/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
index 978fbff966f6..5f64ef304b87 100644
--- a/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
+++ b/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
@@ -47,6 +47,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Protocol/VariableLock.h>
 #include <Protocol/RamDisk.h>
 #include <Protocol/DeferredImageLoad.h>
+#include <Protocol/PeCoffImageEmulator.h>
 
 #include <Guid/MemoryTypeInformation.h>
 #include <Guid/FileInfo.h>
diff --git a/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManagerLib.inf b/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManagerLib.inf
index 72c5ca1cd59e..09e2134acf8e 100644
--- a/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManagerLib.inf
+++ b/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManagerLib.inf
@@ -104,6 +104,7 @@
   gEfiDevicePathProtocolGuid                    ## SOMETIMES_CONSUMES
   gEfiBootLogoProtocolGuid                      ## SOMETIMES_CONSUMES
   gEfiSimpleTextInputExProtocolGuid             ## SOMETIMES_CONSUMES
+  gEdkiiPeCoffImageEmulatorProtocolGuid         ## SOMETIMES_CONSUMES
   gEdkiiVariableLockProtocolGuid                ## SOMETIMES_CONSUMES
   gEfiGraphicsOutputProtocolGuid                ## SOMETIMES_CONSUMES
   gEfiUsbIoProtocolGuid                         ## SOMETIMES_CONSUMES
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 22+ messages in thread

* [PATCH v3 5/7] MdeModulePkg/EbcDxe: implement the PE/COFF emulator protocol
  2018-09-20 23:01 [PATCH v3 0/7] MdeModulePkg: add support for dispatching foreign arch PE/COFF images Ard Biesheuvel
                   ` (3 preceding siblings ...)
  2018-09-20 23:01 ` [PATCH v3 4/7] MdeModulePkg/UefiBootManagerLib: allow foreign Driver#### images Ard Biesheuvel
@ 2018-09-20 23:01 ` Ard Biesheuvel
  2018-09-20 23:01 ` [PATCH v3 6/7] MdePkg/UefiBaseType.h: treat EBC as a non-native machine type Ard Biesheuvel
  2018-09-20 23:01 ` [PATCH v3 7/7] MdeModulePkg/DxeCore: remove explicit EBC handling Ard Biesheuvel
  6 siblings, 0 replies; 22+ messages in thread
From: Ard Biesheuvel @ 2018-09-20 23:01 UTC (permalink / raw)
  To: edk2-devel
  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

Implement the new EDK2 PE/COFF image emulator protocol so that we can
remove the EBC specific handling in the DXE core and other places in
the core code.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 MdeModulePkg/Universal/EbcDxe/EbcDxe.inf |   3 +
 MdeModulePkg/Universal/EbcDxe/EbcInt.c   | 127 ++++++++++++++++++++
 MdeModulePkg/Universal/EbcDxe/EbcInt.h   |   3 +
 3 files changed, 133 insertions(+)

diff --git a/MdeModulePkg/Universal/EbcDxe/EbcDxe.inf b/MdeModulePkg/Universal/EbcDxe/EbcDxe.inf
index 8f128b121d0b..9cba1d0a917b 100644
--- a/MdeModulePkg/Universal/EbcDxe/EbcDxe.inf
+++ b/MdeModulePkg/Universal/EbcDxe/EbcDxe.inf
@@ -62,7 +62,9 @@
   MdeModulePkg/MdeModulePkg.dec
 
 [LibraryClasses]
+  CacheMaintenanceLib
   MemoryAllocationLib
+  PeCoffLib
   UefiBootServicesTableLib
   BaseMemoryLib
   UefiDriverEntryPoint
@@ -73,6 +75,7 @@
 [Protocols]
   gEfiDebugSupportProtocolGuid                  ## PRODUCES
   gEfiEbcProtocolGuid                           ## PRODUCES
+  gEdkiiPeCoffImageEmulatorProtocolGuid         ## PRODUCES
   gEfiEbcVmTestProtocolGuid                     ## SOMETIMES_PRODUCES
   gEfiEbcSimpleDebuggerProtocolGuid             ## SOMETIMES_CONSUMES
 
diff --git a/MdeModulePkg/Universal/EbcDxe/EbcInt.c b/MdeModulePkg/Universal/EbcDxe/EbcInt.c
index 727ba8bcae44..45e5da1823e7 100644
--- a/MdeModulePkg/Universal/EbcDxe/EbcInt.c
+++ b/MdeModulePkg/Universal/EbcDxe/EbcInt.c
@@ -349,6 +349,123 @@ UINTN                  mStackNum = 0;
 EFI_EVENT              mEbcPeriodicEvent;
 VM_CONTEXT             *mVmPtr = NULL;
 
+/**
+  Check whether the emulator supports executing a certain PE/COFF image
+
+  @param[in] This         This pointer for EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
+                          structure
+  @param[in] MachineType  The machine type for which the image was built
+  @param[in] ImageType    Whether the image is an application, a boot time
+                          driver or a runtime driver.
+  @param[in] DevicePath   Path to device where the image originated
+                          (e.g., a PCI option ROM)
+
+  @retval TRUE            The image is supported by the emulator
+  @retval FALSE           The image is not supported by the emulator.
+**/
+BOOLEAN
+EFIAPI
+EbcIsImageSupported (
+  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
+  IN  UINT16                                  MachineType,
+  IN  UINT16                                  ImageType,
+  IN  EFI_DEVICE_PATH_PROTOCOL                *DevicePath   OPTIONAL
+  )
+{
+  if (MachineType != EFI_IMAGE_MACHINE_EBC) {
+    return FALSE;
+  }
+
+  if (ImageType != EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION &&
+      ImageType != EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER) {
+    return FALSE;
+  }
+  return TRUE;
+}
+
+/**
+  Register a supported PE/COFF image with the emulator. After this call
+  completes successfully, the PE/COFF image may be started as usual, and
+  it is the responsibility of the emulator implementation that any branch
+  into the code section of the image (including returns from functions called
+  from the foreign code) is executed as if it were running on the machine
+  type it was built for.
+
+  @param[in]      This          This pointer for
+                                EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL structure
+  @param[in]      ImageBase     The base address in memory of the PE/COFF image
+  @param[in]      ImageSize     The size in memory of the PE/COFF image
+  @param[in,out]  EntryPoint    The entry point of the PE/COFF image. Passed by
+                                reference so that the emulator may modify it.
+
+  @retval EFI_SUCCESS           The image was registered with the emulator and
+                                can be started as usual.
+  @retval other                 The image could not be registered.
+
+  If the PE/COFF machine type or image type are not supported by the emulator,
+  then ASSERT().
+**/
+EFI_STATUS
+EFIAPI
+EbcRegisterImage (
+  IN      EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
+  IN      EFI_PHYSICAL_ADDRESS                    ImageBase,
+  IN      UINT64                                  ImageSize,
+  IN  OUT EFI_IMAGE_ENTRY_POINT                   *EntryPoint
+  )
+{
+  DEBUG_CODE_BEGIN ();
+    PE_COFF_LOADER_IMAGE_CONTEXT  ImageContext;
+    EFI_STATUS                    Status;
+
+    ZeroMem (&ImageContext, sizeof (ImageContext));
+
+    ImageContext.Handle    = (VOID *)(UINTN)ImageBase;
+    ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
+
+    Status = PeCoffLoaderGetImageInfo (&ImageContext);
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
+
+    ASSERT (ImageContext.Machine == EFI_IMAGE_MACHINE_EBC);
+    ASSERT (ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION ||
+            ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER);
+  DEBUG_CODE_END ();
+
+  EbcRegisterICacheFlush (NULL,
+    (EBC_ICACHE_FLUSH)InvalidateInstructionCacheRange);
+
+  return EbcCreateThunk (NULL, (VOID *)(UINTN)ImageBase, *EntryPoint,
+           (VOID **)EntryPoint);
+}
+
+/**
+  Unregister a PE/COFF image that has been registered with the emulator.
+  This should be done before the image is unloaded from memory.
+
+  @param[in] This         This pointer for EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
+                          structure
+  @param[in] ImageBase    The base address in memory of the PE/COFF image
+
+  @retval EFI_SUCCESS     The image was unregistered with the emulator.
+  @retval other           Image could not be unloaded.
+**/
+EFI_STATUS
+EFIAPI
+EbcUnregisterImage (
+  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
+  IN  EFI_PHYSICAL_ADDRESS                    ImageBase
+  )
+{
+  return EbcUnloadImage (NULL, (VOID *)(UINTN)ImageBase);
+}
+
+EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL mPeCoffEmuProtocol = {
+  EbcIsImageSupported,
+  EbcRegisterImage,
+  EbcUnregisterImage
+};
 
 /**
   Initializes the VM EFI interface.  Allocates memory for the VM interface
@@ -449,6 +566,16 @@ InitializeEbcDriver (
     }
   }
 
+  Status = gBS->InstallProtocolInterface (
+                  &ImageHandle,
+                  &gEdkiiPeCoffImageEmulatorProtocolGuid,
+                  EFI_NATIVE_INTERFACE,
+                  &mPeCoffEmuProtocol
+                  );
+  if (EFI_ERROR(Status)) {
+    goto ErrorExit;
+  }
+
   Status = InitEBCStack();
   if (EFI_ERROR(Status)) {
     goto ErrorExit;
diff --git a/MdeModulePkg/Universal/EbcDxe/EbcInt.h b/MdeModulePkg/Universal/EbcDxe/EbcInt.h
index 8aa7a4abbd63..9b25e91f951c 100644
--- a/MdeModulePkg/Universal/EbcDxe/EbcInt.h
+++ b/MdeModulePkg/Universal/EbcDxe/EbcInt.h
@@ -23,9 +23,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Protocol/Ebc.h>
 #include <Protocol/EbcVmTest.h>
 #include <Protocol/EbcSimpleDebugger.h>
+#include <Protocol/PeCoffImageEmulator.h>
 
 #include <Library/BaseLib.h>
+#include <Library/CacheMaintenanceLib.h>
 #include <Library/DebugLib.h>
+#include <Library/PeCoffLib.h>
 #include <Library/UefiDriverEntryPoint.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/UefiBootServicesTableLib.h>
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 22+ messages in thread

* [PATCH v3 6/7] MdePkg/UefiBaseType.h: treat EBC as a non-native machine type
  2018-09-20 23:01 [PATCH v3 0/7] MdeModulePkg: add support for dispatching foreign arch PE/COFF images Ard Biesheuvel
                   ` (4 preceding siblings ...)
  2018-09-20 23:01 ` [PATCH v3 5/7] MdeModulePkg/EbcDxe: implement the PE/COFF emulator protocol Ard Biesheuvel
@ 2018-09-20 23:01 ` Ard Biesheuvel
  2018-09-20 23:01 ` [PATCH v3 7/7] MdeModulePkg/DxeCore: remove explicit EBC handling Ard Biesheuvel
  6 siblings, 0 replies; 22+ messages in thread
From: Ard Biesheuvel @ 2018-09-20 23:01 UTC (permalink / raw)
  To: edk2-devel
  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

Instead of classifying EBC as a supported machine type and have special
handling in DXE core for loading EBC images, make it a foreign type and
rely on the EDK2 PE/COFF image emulator protocol to claim the image when
the DXE core finds that it cannot be supported natively.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 MdePkg/Include/Uefi/UefiBaseType.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/MdePkg/Include/Uefi/UefiBaseType.h b/MdePkg/Include/Uefi/UefiBaseType.h
index 401db7f620b3..e52121809deb 100644
--- a/MdePkg/Include/Uefi/UefiBaseType.h
+++ b/MdePkg/Include/Uefi/UefiBaseType.h
@@ -250,21 +250,21 @@ typedef union {
 #if   defined (MDE_CPU_IA32)
 
 #define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) \
-  (((Machine) == EFI_IMAGE_MACHINE_IA32) || ((Machine) == EFI_IMAGE_MACHINE_EBC))
+  ((Machine) == EFI_IMAGE_MACHINE_IA32)
 
 #define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) ((Machine) == EFI_IMAGE_MACHINE_X64)
 
 #elif defined (MDE_CPU_IPF)
 
 #define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) \
-  (((Machine) == EFI_IMAGE_MACHINE_IA64) || ((Machine) == EFI_IMAGE_MACHINE_EBC))
+  ((Machine) == EFI_IMAGE_MACHINE_IA64)
 
 #define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) (FALSE)
 
 #elif defined (MDE_CPU_X64)
 
 #define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) \
-  (((Machine) == EFI_IMAGE_MACHINE_X64) || ((Machine) == EFI_IMAGE_MACHINE_EBC))
+  ((Machine) == EFI_IMAGE_MACHINE_X64)
 
 #define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) ((Machine) == EFI_IMAGE_MACHINE_IA32)
 
@@ -277,7 +277,7 @@ typedef union {
 #elif defined (MDE_CPU_AARCH64)
 
 #define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Machine) \
-  (((Machine) == EFI_IMAGE_MACHINE_AARCH64) || ((Machine) == EFI_IMAGE_MACHINE_EBC))
+  ((Machine) == EFI_IMAGE_MACHINE_AARCH64)
 
 #define EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED(Machine) (FALSE)
 
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 22+ messages in thread

* [PATCH v3 7/7] MdeModulePkg/DxeCore: remove explicit EBC handling
  2018-09-20 23:01 [PATCH v3 0/7] MdeModulePkg: add support for dispatching foreign arch PE/COFF images Ard Biesheuvel
                   ` (5 preceding siblings ...)
  2018-09-20 23:01 ` [PATCH v3 6/7] MdePkg/UefiBaseType.h: treat EBC as a non-native machine type Ard Biesheuvel
@ 2018-09-20 23:01 ` Ard Biesheuvel
  6 siblings, 0 replies; 22+ messages in thread
From: Ard Biesheuvel @ 2018-09-20 23:01 UTC (permalink / raw)
  To: edk2-devel
  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

Now that the EBC machine type is no longer classified as a
natively supported machine type on the architectures that can
support it via the EBC interpreter, the EBC specific handling
in DXE core is no longer used and can be removed.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 MdeModulePkg/Core/Dxe/DxeMain.h     |  3 --
 MdeModulePkg/Core/Dxe/DxeMain.inf   |  1 -
 MdeModulePkg/Core/Dxe/Image/Image.c | 53 ++------------------
 3 files changed, 3 insertions(+), 54 deletions(-)

diff --git a/MdeModulePkg/Core/Dxe/DxeMain.h b/MdeModulePkg/Core/Dxe/DxeMain.h
index ff2418c5ae5e..c473006813fe 100644
--- a/MdeModulePkg/Core/Dxe/DxeMain.h
+++ b/MdeModulePkg/Core/Dxe/DxeMain.h
@@ -42,7 +42,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Protocol/LoadPe32Image.h>
 #include <Protocol/Security.h>
 #include <Protocol/Security2.h>
-#include <Protocol/Ebc.h>
 #include <Protocol/Reset.h>
 #include <Protocol/Cpu.h>
 #include <Protocol/Metronome.h>
@@ -228,8 +227,6 @@ typedef struct {
   BASE_LIBRARY_JUMP_BUFFER    *JumpContext;
   /// Machine type from PE image
   UINT16                      Machine;
-  /// EBC Protocol pointer
-  EFI_EBC_PROTOCOL            *Ebc;
   /// PE/COFF Image Emulator Protocol pointer
   EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL  *PeCoffEmu;
   /// Runtime image list
diff --git a/MdeModulePkg/Core/Dxe/DxeMain.inf b/MdeModulePkg/Core/Dxe/DxeMain.inf
index 63e650ee7c27..a969b869b331 100644
--- a/MdeModulePkg/Core/Dxe/DxeMain.inf
+++ b/MdeModulePkg/Core/Dxe/DxeMain.inf
@@ -161,7 +161,6 @@
   gEfiLoadedImageProtocolGuid                   ## PRODUCES
   gEfiLoadedImageDevicePathProtocolGuid         ## PRODUCES
   gEfiHiiPackageListProtocolGuid                ## SOMETIMES_PRODUCES
-  gEfiEbcProtocolGuid                           ## SOMETIMES_CONSUMES
   gEfiSmmBase2ProtocolGuid                      ## SOMETIMES_CONSUMES
   gEfiBlockIoProtocolGuid                       ## SOMETIMES_CONSUMES
   gEdkiiPeCoffImageEmulatorProtocolGuid         ## SOMETIMES_CONSUMES
diff --git a/MdeModulePkg/Core/Dxe/Image/Image.c b/MdeModulePkg/Core/Dxe/Image/Image.c
index dd987f7fcea7..53d526fddc7d 100644
--- a/MdeModulePkg/Core/Dxe/Image/Image.c
+++ b/MdeModulePkg/Core/Dxe/Image/Image.c
@@ -74,7 +74,6 @@ LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage  = {
   NULL,                       // JumpBuffer
   NULL,                       // JumpContext
   0,                          // Machine
-  NULL,                       // Ebc
   NULL,                       // PeCoffEmu
   NULL,                       // RuntimeData
   NULL                        // LoadedImageDevicePath
@@ -91,9 +90,6 @@ typedef struct {
   CHAR16  *MachineTypeName;
 } MACHINE_TYPE_INFO;
 
-//
-// EBC machine is not listed in this table, because EBC is in the default supported scopes of other machine type.
-//
 GLOBAL_REMOVE_IF_UNREFERENCED MACHINE_TYPE_INFO  mMachineTypeInfo[] = {
   {EFI_IMAGE_MACHINE_IA32,           L"IA32"},
   {EFI_IMAGE_MACHINE_IA64,           L"IA64"},
@@ -742,51 +738,15 @@ CoreLoadPeImage (
   InvalidateInstructionCacheRange ((VOID *)(UINTN)Image->ImageContext.ImageAddress, (UINTN)Image->ImageContext.ImageSize);
 
   //
-  // Copy the machine type from the context to the image private data. This
-  // is needed during image unload to know if we should call an EBC protocol
-  // to unload the image.
+  // Copy the machine type from the context to the image private data.
   //
   Image->Machine = Image->ImageContext.Machine;
 
   //
-  // Get the image entry point. If it's an EBC image, then call into the
-  // interpreter to create a thunk for the entry point and use the returned
-  // value for the entry point.
+  // Get the image entry point.
   //
   Image->EntryPoint   = (EFI_IMAGE_ENTRY_POINT)(UINTN)Image->ImageContext.EntryPoint;
-  if (Image->ImageContext.Machine == EFI_IMAGE_MACHINE_EBC) {
-    //
-    // Locate the EBC interpreter protocol
-    //
-    Status = CoreLocateProtocol (&gEfiEbcProtocolGuid, NULL, (VOID **)&Image->Ebc);
-    if (EFI_ERROR(Status) || Image->Ebc == NULL) {
-      DEBUG ((DEBUG_LOAD | DEBUG_ERROR, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));
-      goto Done;
-    }
-
-    //
-    // Register a callback for flushing the instruction cache so that created
-    // thunks can be flushed.
-    //
-    Status = Image->Ebc->RegisterICacheFlush (Image->Ebc, (EBC_ICACHE_FLUSH)InvalidateInstructionCacheRange);
-    if (EFI_ERROR(Status)) {
-      goto Done;
-    }
-
-    //
-    // Create a thunk for the image's entry point. This will be the new
-    // entry point for the image.
-    //
-    Status = Image->Ebc->CreateThunk (
-                           Image->Ebc,
-                           Image->Handle,
-                           (VOID *)(UINTN) Image->ImageContext.EntryPoint,
-                           (VOID **) &Image->EntryPoint
-                           );
-    if (EFI_ERROR(Status)) {
-      goto Done;
-    }
-  } else if (Image->PeCoffEmu != NULL) {
+  if (Image->PeCoffEmu != NULL) {
     Status = Image->PeCoffEmu->RegisterImage (Image->PeCoffEmu,
                                  Image->ImageBasePage,
                                  EFI_PAGES_TO_SIZE (Image->NumberOfPages),
@@ -976,13 +936,6 @@ CoreUnloadAndCloseImage (
 
   UnprotectUefiImage (&Image->Info, Image->LoadedImageDevicePath);
 
-  if (Image->Ebc != NULL) {
-    //
-    // If EBC protocol exists we must perform cleanups for this image.
-    //
-    Image->Ebc->UnloadImage (Image->Ebc, Image->Handle);
-  }
-
   if (Image->PeCoffEmu != NULL) {
     //
     // If the PE/COFF Emulator protocol exists we must unregister the image.
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 22+ messages in thread

* Re: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol
  2018-09-20 23:01 ` [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol Ard Biesheuvel
@ 2018-09-26  9:58   ` Zeng, Star
  2018-09-26 10:13     ` Ard Biesheuvel
  0 siblings, 1 reply; 22+ messages in thread
From: Zeng, Star @ 2018-09-26  9:58 UTC (permalink / raw)
  To: Ard Biesheuvel, edk2-devel@lists.01.org
  Cc: Zimmer, Vincent, Richardson, Brian, Kinney, Michael D,
	Andrew Fish, Leif Lindholm, Dong, Eric, Ni, Ruiyu, Gao, Liming,
	Carsey, Jaben, Shi, Steven, Zeng, Star

A little late feedback. Just an idea.

Do you think whether it can make the consumer simpler like below or not?

Add a new API RegisterInterfaces in the protocol and add a wrapper driver PeCoffEmulatorDxe.
The emulators can call RegisterInterfaces, and consumer will call other APIs simply, PeCoffEmulatorDxe will be a wrapper as manager.

typedef
EFI_STATUS
(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES) (
  IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
  IN EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED    IsImageSupported,
  IN EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE        RegisterImage,
  IN EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE      UnregisterImage
  );

typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL {
  EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES    RegisterInterfaces;
  EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED    IsImageSupported;
  EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE        RegisterImage;
  EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE      UnregisterImage;
} EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;


Thanks,
Star
-----Original Message-----
From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org] 
Sent: Friday, September 21, 2018 7:02 AM
To: edk2-devel@lists.01.org
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>; Zimmer, Vincent <vincent.zimmer@intel.com>; Richardson, Brian <brian.richardson@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>; Andrew Fish <afish@apple.com>; Leif Lindholm <leif.lindholm@linaro.org>; Zeng, Star <star.zeng@intel.com>; Dong, Eric <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming <liming.gao@intel.com>; Carsey, Jaben <jaben.carsey@intel.com>; Shi, Steven <steven.shi@intel.com>
Subject: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol

Introduce a protocol that can be invoked by the image loading services to execute foreign architecture PE/COFF images via an emulator.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h | 102 ++++++++++++++++++++
 MdeModulePkg/MdeModulePkg.dec                       |   4 +
 2 files changed, 106 insertions(+)

diff --git a/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
new file mode 100644
index 000000000000..27bad556209c
--- /dev/null
+++ b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
@@ -0,0 +1,102 @@
+/** @file
+  Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>
+
+  This program and the accompanying materials are licensed and made 
+ available  under the terms and conditions of the BSD License which 
+ accompanies this  distribution.  The full text of the license may be 
+ found at  http://opensource.org/licenses/bsd-license.php
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,  
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
+#define PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
+
+#define EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID \
+  { 0x96F46153, 0x97A7, 0x4793, { 0xAC, 0xC1, 0xFA, 0x19, 0xBF, 0x78, 
+0xEA, 0x97 } }
+
+typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL 
+EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
+
+/**
+  Check whether the emulator supports executing a certain PE/COFF image
+
+  @param[in] This         This pointer for EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
+                          structure
+  @param[in] MachineType  The machine type for which the image was built
+  @param[in] ImageType    Whether the image is an application, a boot time
+                          driver or a runtime driver.
+  @param[in] DevicePath   Path to device where the image originated
+                          (e.g., a PCI option ROM)
+
+  @retval TRUE            The image is supported by the emulator
+  @retval FALSE           The image is not supported by the emulator.
+**/
+typedef
+BOOLEAN
+(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED) (
+  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
+  IN  UINT16                                  MachineType,
+  IN  UINT16                                  ImageType,
+  IN  EFI_DEVICE_PATH_PROTOCOL                *DevicePath   OPTIONAL
+  );
+
+/**
+  Register a supported PE/COFF image with the emulator. After this call
+  completes successfully, the PE/COFF image may be started as usual, 
+and
+  it is the responsibility of the emulator implementation that any 
+branch
+  into the code section of the image (including returns from functions 
+called
+  from the foreign code) is executed as if it were running on the 
+machine
+  type it was built for.
+
+  @param[in]      This          This pointer for
+                                EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL structure
+  @param[in]      ImageBase     The base address in memory of the PE/COFF image
+  @param[in]      ImageSize     The size in memory of the PE/COFF image
+  @param[in,out]  EntryPoint    The entry point of the PE/COFF image. Passed by
+                                reference so that the emulator may modify it.
+
+  @retval EFI_SUCCESS           The image was registered with the emulator and
+                                can be started as usual.
+  @retval other                 The image could not be registered.
+
+  If the PE/COFF machine type or image type are not supported by the 
+emulator,
+  then ASSERT().
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE) (
+  IN      EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
+  IN      EFI_PHYSICAL_ADDRESS                    ImageBase,
+  IN      UINT64                                  ImageSize,
+  IN  OUT EFI_IMAGE_ENTRY_POINT                   *EntryPoint
+  );
+
+/**
+  Unregister a PE/COFF image that has been registered with the emulator.
+  This should be done before the image is unloaded from memory.
+
+  @param[in] This         This pointer for EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
+                          structure
+  @param[in] ImageBase    The base address in memory of the PE/COFF image
+
+  @retval EFI_SUCCESS     The image was unregistered with the emulator.
+  @retval other           Image could not be unloaded.
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE) (
+  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
+  IN  EFI_PHYSICAL_ADDRESS                    ImageBase
+  );
+
+typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL {
+  EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED    IsImageSupported;
+  EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE        RegisterImage;
+  EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE      UnregisterImage;
+} EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
+
+extern EFI_GUID gEdkiiPeCoffImageEmulatorProtocolGuid;
+
+#endif
diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index 6a6d9660edc2..be307329f901 100644
--- a/MdeModulePkg/MdeModulePkg.dec
+++ b/MdeModulePkg/MdeModulePkg.dec
@@ -617,6 +617,10 @@
 
   ## Include/Protocol/AtaAtapiPolicy.h
   gEdkiiAtaAtapiPolicyProtocolGuid = { 0xe59cd769, 0x5083, 0x4f26,{ 0x90, 0x94, 0x6c, 0x91, 0x9f, 0x91, 0x6c, 0x4e } }
+
+  ## Include/Protocol/PeCoffImageEmulator.h
+  gEdkiiPeCoffImageEmulatorProtocolGuid = { 0x96f46153, 0x97a7, 0x4793, 
+ { 0xac, 0xc1, 0xfa, 0x19, 0xbf, 0x78, 0xea, 0x97 } }
+
 #
 # [Error.gEfiMdeModulePkgTokenSpaceGuid]
 #   0x80000001 | Invalid value provided.
--
2.17.1



^ permalink raw reply related	[flat|nested] 22+ messages in thread

* Re: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol
  2018-09-26  9:58   ` Zeng, Star
@ 2018-09-26 10:13     ` Ard Biesheuvel
  2018-09-26 17:32       ` Kinney, Michael D
  0 siblings, 1 reply; 22+ messages in thread
From: Ard Biesheuvel @ 2018-09-26 10:13 UTC (permalink / raw)
  To: Zeng, Star
  Cc: edk2-devel@lists.01.org, Zimmer, Vincent, Richardson, Brian,
	Kinney, Michael D, Andrew Fish, Leif Lindholm, Eric Dong,
	Ruiyu Ni, Gao, Liming, Carsey, Jaben, Shi, Steven

On Wed, 26 Sep 2018 at 12:07, Zeng, Star <star.zeng@intel.com> wrote:
>
> A little late feedback. Just an idea.
>
> Do you think whether it can make the consumer simpler like below or not?
>
> Add a new API RegisterInterfaces in the protocol and add a wrapper driver PeCoffEmulatorDxe.
> The emulators can call RegisterInterfaces, and consumer will call other APIs simply, PeCoffEmulatorDxe will be a wrapper as manager.
>
> typedef
> EFI_STATUS
> (EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES) (
>   IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
>   IN EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED    IsImageSupported,
>   IN EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE        RegisterImage,
>   IN EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE      UnregisterImage
>   );
>
> typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL {
>   EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES    RegisterInterfaces;
>   EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED    IsImageSupported;
>   EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE        RegisterImage;
>   EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE      UnregisterImage;
> } EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
>

Hi Star,

Thanks for taking a look.

I tried to avoid introducing new drivers or library classes because it
will break existing platforms that incorporate EbcDxe. Do you think
this is not an issue?

> -----Original Message-----
> From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org]
> Sent: Friday, September 21, 2018 7:02 AM
> To: edk2-devel@lists.01.org
> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>; Zimmer, Vincent <vincent.zimmer@intel.com>; Richardson, Brian <brian.richardson@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>; Andrew Fish <afish@apple.com>; Leif Lindholm <leif.lindholm@linaro.org>; Zeng, Star <star.zeng@intel.com>; Dong, Eric <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming <liming.gao@intel.com>; Carsey, Jaben <jaben.carsey@intel.com>; Shi, Steven <steven.shi@intel.com>
> Subject: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol
>
> Introduce a protocol that can be invoked by the image loading services to execute foreign architecture PE/COFF images via an emulator.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> ---
>  MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h | 102 ++++++++++++++++++++
>  MdeModulePkg/MdeModulePkg.dec                       |   4 +
>  2 files changed, 106 insertions(+)
>
> diff --git a/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> new file mode 100644
> index 000000000000..27bad556209c
> --- /dev/null
> +++ b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> @@ -0,0 +1,102 @@
> +/** @file
> +  Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>
> +
> +  This program and the accompanying materials are licensed and made
> + available  under the terms and conditions of the BSD License which
> + accompanies this  distribution.  The full text of the license may be
> + found at  http://opensource.org/licenses/bsd-license.php
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
> +#define PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
> +
> +#define EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID \
> +  { 0x96F46153, 0x97A7, 0x4793, { 0xAC, 0xC1, 0xFA, 0x19, 0xBF, 0x78,
> +0xEA, 0x97 } }
> +
> +typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> +EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> +
> +/**
> +  Check whether the emulator supports executing a certain PE/COFF image
> +
> +  @param[in] This         This pointer for EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> +                          structure
> +  @param[in] MachineType  The machine type for which the image was built
> +  @param[in] ImageType    Whether the image is an application, a boot time
> +                          driver or a runtime driver.
> +  @param[in] DevicePath   Path to device where the image originated
> +                          (e.g., a PCI option ROM)
> +
> +  @retval TRUE            The image is supported by the emulator
> +  @retval FALSE           The image is not supported by the emulator.
> +**/
> +typedef
> +BOOLEAN
> +(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED) (
> +  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
> +  IN  UINT16                                  MachineType,
> +  IN  UINT16                                  ImageType,
> +  IN  EFI_DEVICE_PATH_PROTOCOL                *DevicePath   OPTIONAL
> +  );
> +
> +/**
> +  Register a supported PE/COFF image with the emulator. After this call
> +  completes successfully, the PE/COFF image may be started as usual,
> +and
> +  it is the responsibility of the emulator implementation that any
> +branch
> +  into the code section of the image (including returns from functions
> +called
> +  from the foreign code) is executed as if it were running on the
> +machine
> +  type it was built for.
> +
> +  @param[in]      This          This pointer for
> +                                EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL structure
> +  @param[in]      ImageBase     The base address in memory of the PE/COFF image
> +  @param[in]      ImageSize     The size in memory of the PE/COFF image
> +  @param[in,out]  EntryPoint    The entry point of the PE/COFF image. Passed by
> +                                reference so that the emulator may modify it.
> +
> +  @retval EFI_SUCCESS           The image was registered with the emulator and
> +                                can be started as usual.
> +  @retval other                 The image could not be registered.
> +
> +  If the PE/COFF machine type or image type are not supported by the
> +emulator,
> +  then ASSERT().
> +**/
> +typedef
> +EFI_STATUS
> +(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE) (
> +  IN      EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
> +  IN      EFI_PHYSICAL_ADDRESS                    ImageBase,
> +  IN      UINT64                                  ImageSize,
> +  IN  OUT EFI_IMAGE_ENTRY_POINT                   *EntryPoint
> +  );
> +
> +/**
> +  Unregister a PE/COFF image that has been registered with the emulator.
> +  This should be done before the image is unloaded from memory.
> +
> +  @param[in] This         This pointer for EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> +                          structure
> +  @param[in] ImageBase    The base address in memory of the PE/COFF image
> +
> +  @retval EFI_SUCCESS     The image was unregistered with the emulator.
> +  @retval other           Image could not be unloaded.
> +**/
> +typedef
> +EFI_STATUS
> +(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE) (
> +  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
> +  IN  EFI_PHYSICAL_ADDRESS                    ImageBase
> +  );
> +
> +typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL {
> +  EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED    IsImageSupported;
> +  EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE        RegisterImage;
> +  EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE      UnregisterImage;
> +} EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> +
> +extern EFI_GUID gEdkiiPeCoffImageEmulatorProtocolGuid;
> +
> +#endif
> diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index 6a6d9660edc2..be307329f901 100644
> --- a/MdeModulePkg/MdeModulePkg.dec
> +++ b/MdeModulePkg/MdeModulePkg.dec
> @@ -617,6 +617,10 @@
>
>    ## Include/Protocol/AtaAtapiPolicy.h
>    gEdkiiAtaAtapiPolicyProtocolGuid = { 0xe59cd769, 0x5083, 0x4f26,{ 0x90, 0x94, 0x6c, 0x91, 0x9f, 0x91, 0x6c, 0x4e } }
> +
> +  ## Include/Protocol/PeCoffImageEmulator.h
> +  gEdkiiPeCoffImageEmulatorProtocolGuid = { 0x96f46153, 0x97a7, 0x4793,
> + { 0xac, 0xc1, 0xfa, 0x19, 0xbf, 0x78, 0xea, 0x97 } }
> +
>  #
>  # [Error.gEfiMdeModulePkgTokenSpaceGuid]
>  #   0x80000001 | Invalid value provided.
> --
> 2.17.1
>


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol
  2018-09-26 10:13     ` Ard Biesheuvel
@ 2018-09-26 17:32       ` Kinney, Michael D
  2018-09-27  0:48         ` Zeng, Star
  0 siblings, 1 reply; 22+ messages in thread
From: Kinney, Michael D @ 2018-09-26 17:32 UTC (permalink / raw)
  To: Ard Biesheuvel, Zeng, Star, Kinney, Michael D
  Cc: edk2-devel@lists.01.org, Zimmer, Vincent, Richardson, Brian,
	Andrew Fish, Leif Lindholm, Dong, Eric, Ni, Ruiyu, Gao, Liming,
	Carsey, Jaben, Shi, Steven

Hi Ard,

I think this registration protocol would be a new
protocol in the MdeModulePkg and the protocol would
be produced by the DXE Core.  The emulation drivers,
including EBC would consume this protocol to register
their services.  This removes the need for the DXE
Core to do a Register Protocol Notify event for the 
emulator protocol.  The DXE Core would build a table
of registered services, so it can quickly loop 
through them to check if an emulator supports a
specific PE/COFF image or not.

Best regards,

Mike

> -----Original Message-----
> From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org]
> Sent: Wednesday, September 26, 2018 3:14 AM
> To: Zeng, Star <star.zeng@intel.com>
> Cc: edk2-devel@lists.01.org; Zimmer, Vincent
> <vincent.zimmer@intel.com>; Richardson, Brian
> <brian.richardson@intel.com>; Kinney, Michael D
> <michael.d.kinney@intel.com>; Andrew Fish
> <afish@apple.com>; Leif Lindholm
> <leif.lindholm@linaro.org>; Dong, Eric
> <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>;
> Gao, Liming <liming.gao@intel.com>; Carsey, Jaben
> <jaben.carsey@intel.com>; Shi, Steven
> <steven.shi@intel.com>
> Subject: Re: [PATCH v3 1/7] MdeModulePkg: introduce
> PE/COFF image emulator protocol
> 
> On Wed, 26 Sep 2018 at 12:07, Zeng, Star
> <star.zeng@intel.com> wrote:
> >
> > A little late feedback. Just an idea.
> >
> > Do you think whether it can make the consumer simpler
> like below or not?
> >
> > Add a new API RegisterInterfaces in the protocol and
> add a wrapper driver PeCoffEmulatorDxe.
> > The emulators can call RegisterInterfaces, and
> consumer will call other APIs simply, PeCoffEmulatorDxe
> will be a wrapper as manager.
> >
> > typedef
> > EFI_STATUS
> > (EFIAPI
> *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES) (
> >   IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
> >   IN EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
> IsImageSupported,
> >   IN EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
> RegisterImage,
> >   IN EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
> UnregisterImage
> >   );
> >
> > typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL {
> >   EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES
> RegisterInterfaces;
> >   EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
> IsImageSupported;
> >   EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
> RegisterImage;
> >   EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
> UnregisterImage;
> > } EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> >
> 
> Hi Star,
> 
> Thanks for taking a look.
> 
> I tried to avoid introducing new drivers or library
> classes because it
> will break existing platforms that incorporate EbcDxe.
> Do you think
> this is not an issue?
> 
> > -----Original Message-----
> > From: Ard Biesheuvel
> [mailto:ard.biesheuvel@linaro.org]
> > Sent: Friday, September 21, 2018 7:02 AM
> > To: edk2-devel@lists.01.org
> > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>;
> Zimmer, Vincent <vincent.zimmer@intel.com>; Richardson,
> Brian <brian.richardson@intel.com>; Kinney, Michael D
> <michael.d.kinney@intel.com>; Andrew Fish
> <afish@apple.com>; Leif Lindholm
> <leif.lindholm@linaro.org>; Zeng, Star
> <star.zeng@intel.com>; Dong, Eric <eric.dong@intel.com>;
> Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming
> <liming.gao@intel.com>; Carsey, Jaben
> <jaben.carsey@intel.com>; Shi, Steven
> <steven.shi@intel.com>
> > Subject: [PATCH v3 1/7] MdeModulePkg: introduce
> PE/COFF image emulator protocol
> >
> > Introduce a protocol that can be invoked by the image
> loading services to execute foreign architecture PE/COFF
> images via an emulator.
> >
> > Contributed-under: TianoCore Contribution Agreement
> 1.1
> > Signed-off-by: Ard Biesheuvel
> <ard.biesheuvel@linaro.org>
> > ---
> >  MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h |
> 102 ++++++++++++++++++++
> >  MdeModulePkg/MdeModulePkg.dec                       |
> 4 +
> >  2 files changed, 106 insertions(+)
> >
> > diff --git
> a/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> > new file mode 100644
> > index 000000000000..27bad556209c
> > --- /dev/null
> > +++
> b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> > @@ -0,0 +1,102 @@
> > +/** @file
> > +  Copyright (c) 2018, Linaro, Ltd. All rights
> reserved.<BR>
> > +
> > +  This program and the accompanying materials are
> licensed and made
> > + available  under the terms and conditions of the BSD
> License which
> > + accompanies this  distribution.  The full text of
> the license may be
> > + found at  http://opensource.org/licenses/bsd-
> license.php
> > +
> > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON
> AN "AS IS" BASIS,
> > + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND,
> EITHER EXPRESS OR IMPLIED.
> > +
> > +**/
> > +
> > +#ifndef PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
> > +#define PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
> > +
> > +#define EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID \
> > +  { 0x96F46153, 0x97A7, 0x4793, { 0xAC, 0xC1, 0xFA,
> 0x19, 0xBF, 0x78,
> > +0xEA, 0x97 } }
> > +
> > +typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> > +EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> > +
> > +/**
> > +  Check whether the emulator supports executing a
> certain PE/COFF image
> > +
> > +  @param[in] This         This pointer for
> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> > +                          structure
> > +  @param[in] MachineType  The machine type for which
> the image was built
> > +  @param[in] ImageType    Whether the image is an
> application, a boot time
> > +                          driver or a runtime driver.
> > +  @param[in] DevicePath   Path to device where the
> image originated
> > +                          (e.g., a PCI option ROM)
> > +
> > +  @retval TRUE            The image is supported by
> the emulator
> > +  @retval FALSE           The image is not supported
> by the emulator.
> > +**/
> > +typedef
> > +BOOLEAN
> > +(EFIAPI
> *EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED) (
> > +  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
> > +  IN  UINT16
> MachineType,
> > +  IN  UINT16
> ImageType,
> > +  IN  EFI_DEVICE_PATH_PROTOCOL
> *DevicePath   OPTIONAL
> > +  );
> > +
> > +/**
> > +  Register a supported PE/COFF image with the
> emulator. After this call
> > +  completes successfully, the PE/COFF image may be
> started as usual,
> > +and
> > +  it is the responsibility of the emulator
> implementation that any
> > +branch
> > +  into the code section of the image (including
> returns from functions
> > +called
> > +  from the foreign code) is executed as if it were
> running on the
> > +machine
> > +  type it was built for.
> > +
> > +  @param[in]      This          This pointer for
> > +
> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL structure
> > +  @param[in]      ImageBase     The base address in
> memory of the PE/COFF image
> > +  @param[in]      ImageSize     The size in memory of
> the PE/COFF image
> > +  @param[in,out]  EntryPoint    The entry point of
> the PE/COFF image. Passed by
> > +                                reference so that the
> emulator may modify it.
> > +
> > +  @retval EFI_SUCCESS           The image was
> registered with the emulator and
> > +                                can be started as
> usual.
> > +  @retval other                 The image could not
> be registered.
> > +
> > +  If the PE/COFF machine type or image type are not
> supported by the
> > +emulator,
> > +  then ASSERT().
> > +**/
> > +typedef
> > +EFI_STATUS
> > +(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE)
> (
> > +  IN      EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> *This,
> > +  IN      EFI_PHYSICAL_ADDRESS
> ImageBase,
> > +  IN      UINT64
> ImageSize,
> > +  IN  OUT EFI_IMAGE_ENTRY_POINT
> *EntryPoint
> > +  );
> > +
> > +/**
> > +  Unregister a PE/COFF image that has been registered
> with the emulator.
> > +  This should be done before the image is unloaded
> from memory.
> > +
> > +  @param[in] This         This pointer for
> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> > +                          structure
> > +  @param[in] ImageBase    The base address in memory
> of the PE/COFF image
> > +
> > +  @retval EFI_SUCCESS     The image was unregistered
> with the emulator.
> > +  @retval other           Image could not be
> unloaded.
> > +**/
> > +typedef
> > +EFI_STATUS
> > +(EFIAPI
> *EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE) (
> > +  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
> > +  IN  EFI_PHYSICAL_ADDRESS
> ImageBase
> > +  );
> > +
> > +typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> {
> > +  EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
> IsImageSupported;
> > +  EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
> RegisterImage;
> > +  EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
> UnregisterImage;
> > +} EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> > +
> > +extern EFI_GUID
> gEdkiiPeCoffImageEmulatorProtocolGuid;
> > +
> > +#endif
> > diff --git a/MdeModulePkg/MdeModulePkg.dec
> b/MdeModulePkg/MdeModulePkg.dec index
> 6a6d9660edc2..be307329f901 100644
> > --- a/MdeModulePkg/MdeModulePkg.dec
> > +++ b/MdeModulePkg/MdeModulePkg.dec
> > @@ -617,6 +617,10 @@
> >
> >    ## Include/Protocol/AtaAtapiPolicy.h
> >    gEdkiiAtaAtapiPolicyProtocolGuid = { 0xe59cd769,
> 0x5083, 0x4f26,{ 0x90, 0x94, 0x6c, 0x91, 0x9f, 0x91,
> 0x6c, 0x4e } }
> > +
> > +  ## Include/Protocol/PeCoffImageEmulator.h
> > +  gEdkiiPeCoffImageEmulatorProtocolGuid = {
> 0x96f46153, 0x97a7, 0x4793,
> > + { 0xac, 0xc1, 0xfa, 0x19, 0xbf, 0x78, 0xea, 0x97 } }
> > +
> >  #
> >  # [Error.gEfiMdeModulePkgTokenSpaceGuid]
> >  #   0x80000001 | Invalid value provided.
> > --
> > 2.17.1
> >

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v3 3/7] MdeModulePkg/PciBusDxe: invoke PE/COFF emulator for foreign option ROMs
  2018-09-20 23:01 ` [PATCH v3 3/7] MdeModulePkg/PciBusDxe: invoke PE/COFF emulator for foreign option ROMs Ard Biesheuvel
@ 2018-09-26 18:26   ` Kinney, Michael D
  2018-12-27 10:13     ` Ard Biesheuvel
  0 siblings, 1 reply; 22+ messages in thread
From: Kinney, Michael D @ 2018-09-26 18:26 UTC (permalink / raw)
  To: Ard Biesheuvel, edk2-devel@lists.01.org, Kinney, Michael D
  Cc: Zimmer, Vincent, Richardson, Brian, Andrew Fish, Leif Lindholm,
	Zeng, Star, Dong, Eric, Ni, Ruiyu, Gao, Liming, Carsey, Jaben,
	Shi, Steven

Hi Ard,

I am wondering if we can simplify the PciBusDxe driver and 
remove the image type supported check and instead depend on
LoadImage().  LoadImage() will return EFI_UNSUPPORTED if the
image type is not supported.

Thanks,

Mike

> -----Original Message-----
> From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org]
> Sent: Thursday, September 20, 2018 4:02 PM
> To: edk2-devel@lists.01.org
> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>; Zimmer,
> Vincent <vincent.zimmer@intel.com>; Richardson, Brian
> <brian.richardson@intel.com>; Kinney, Michael D
> <michael.d.kinney@intel.com>; Andrew Fish
> <afish@apple.com>; Leif Lindholm
> <leif.lindholm@linaro.org>; Zeng, Star
> <star.zeng@intel.com>; Dong, Eric
> <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>;
> Gao, Liming <liming.gao@intel.com>; Carsey, Jaben
> <jaben.carsey@intel.com>; Shi, Steven
> <steven.shi@intel.com>
> Subject: [PATCH v3 3/7] MdeModulePkg/PciBusDxe: invoke
> PE/COFF emulator for foreign option ROMs
> 
> When enumerating option ROM images, take into account
> whether an emulator
> exists that would allow dispatch of PE/COFF images
> built for foreign
> architectures.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Ard Biesheuvel
> <ard.biesheuvel@linaro.org>
> ---
>  MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h              |
> 1 +
>  MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf         |
> 1 +
>  MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c |
> 53 +++++++++++++++++++-
>  3 files changed, 54 insertions(+), 1 deletion(-)
> 
> diff --git a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h
> b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h
> index 55eb3a5a8070..dc57d4876c0f 100644
> --- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h
> +++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h
> @@ -33,6 +33,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS
> OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>  #include <Protocol/PciOverride.h>
>  #include <Protocol/PciEnumerationComplete.h>
>  #include <Protocol/IoMmu.h>
> +#include <Protocol/PeCoffImageEmulator.h>
> 
>  #include <Library/DebugLib.h>
>  #include <Library/UefiDriverEntryPoint.h>
> diff --git
> a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
> b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
> index a21dd2b5edf4..c8b861093292 100644
> --- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
> +++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
> @@ -96,6 +96,7 @@
>    gEfiIncompatiblePciDeviceSupportProtocolGuid    ##
> SOMETIMES_CONSUMES
>    gEfiLoadFile2ProtocolGuid                       ##
> SOMETIMES_PRODUCES
>    gEdkiiIoMmuProtocolGuid                         ##
> SOMETIMES_CONSUMES
> +  gEdkiiPeCoffImageEmulatorProtocolGuid           ##
> SOMETIMES_CONSUMES
>    gEfiLoadedImageDevicePathProtocolGuid           ##
> CONSUMES
> 
>  [FeaturePcd]
> diff --git
> a/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
> b/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
> index c2be85a906af..085bd5d571bd 100644
> ---
> a/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
> +++
> b/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
> @@ -651,6 +651,55 @@ RomDecode (
>    }
>  }
> 
> +STATIC
> +BOOLEAN
> +IsImageTypeSupported (
> +  IN  UINT16                    MachineType,
> +  IN  UINT16                    SubSystem,
> +  IN  EFI_DEVICE_PATH_PROTOCOL  *DevicePath
> +  )
> +{
> +  EFI_STATUS                            Status;
> +  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL  *Emu;
> +  UINTN                                 HandleCount;
> +  EFI_HANDLE                            *HandleBuffer;
> +  BOOLEAN                               ReturnValue;
> +  UINTN                                 Index;
> +
> +  if (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (MachineType))
> {
> +    return TRUE;
> +  }
> +
> +  Status = gBS->LocateHandleBuffer (
> +                  ByProtocol,
> +
> &gEdkiiPeCoffImageEmulatorProtocolGuid,
> +                  NULL,
> +                  &HandleCount,
> +                  &HandleBuffer
> +                  );
> +  if (EFI_ERROR (Status)) {
> +    return FALSE;
> +  }
> +
> +  ReturnValue = FALSE;
> +  for (Index = 0; Index < HandleCount; Index++) {
> +    Status = gBS->HandleProtocol (
> +                    HandleBuffer[Index],
> +
> &gEdkiiPeCoffImageEmulatorProtocolGuid,
> +                    (VOID **)&Emu
> +                    );
> +    ASSERT_EFI_ERROR (Status);
> +
> +    if (Emu->IsImageSupported (Emu, MachineType,
> SubSystem, DevicePath)) {
> +      ReturnValue = TRUE;
> +      break;
> +    }
> +  }
> +
> +  FreePool (HandleBuffer);
> +  return ReturnValue;
> +}
> +
>  /**
>    Load and start the Option Rom image.
> 
> @@ -715,7 +764,9 @@ ProcessOpRomImage (
>      //
>      // Skip the EFI PCI Option ROM image if its
> machine type is not supported
>      //
> -    if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED
> (EfiRomHeader->EfiMachineType)) {
> +    if (!IsImageTypeSupported(EfiRomHeader-
> >EfiMachineType,
> +                              EfiRomHeader-
> >EfiSubsystem,
> +                              PciDevice->DevicePath))
> {
>        goto NextImage;
>      }
> 
> --
> 2.17.1



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v3 4/7] MdeModulePkg/UefiBootManagerLib: allow foreign Driver#### images
  2018-09-20 23:01 ` [PATCH v3 4/7] MdeModulePkg/UefiBootManagerLib: allow foreign Driver#### images Ard Biesheuvel
@ 2018-09-26 23:34   ` Kinney, Michael D
  2018-12-27 10:16     ` Ard Biesheuvel
  0 siblings, 1 reply; 22+ messages in thread
From: Kinney, Michael D @ 2018-09-26 23:34 UTC (permalink / raw)
  To: Ard Biesheuvel, edk2-devel@lists.01.org, Kinney, Michael D
  Cc: Ni, Ruiyu, Zimmer, Vincent, Dong, Eric, Andrew Fish,
	Carsey, Jaben, Richardson, Brian, Gao, Liming, Zeng, Star

Hi Ard,

Similar to my PciBusDxe feedback, it would be better if
the image supported information was determined by calling
LoadImage() and checking for an EFI_UNSUPPORTED return
value.

This also has the advantage of reducing the number of 
components that need to be aware of any new protocols
associated with emulation with the best case being the
DXE Core and the modules that provide the emulators.

Thanks,

Mike

> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-
> bounces@lists.01.org] On Behalf Of Ard Biesheuvel
> Sent: Thursday, September 20, 2018 4:02 PM
> To: edk2-devel@lists.01.org
> Cc: Ni, Ruiyu <ruiyu.ni@intel.com>; Zimmer, Vincent
> <vincent.zimmer@intel.com>; Dong, Eric
> <eric.dong@intel.com>; Andrew Fish <afish@apple.com>;
> Carsey, Jaben <jaben.carsey@intel.com>; Richardson,
> Brian <brian.richardson@intel.com>; Gao, Liming
> <liming.gao@intel.com>; Kinney, Michael D
> <michael.d.kinney@intel.com>; Zeng, Star
> <star.zeng@intel.com>
> Subject: [edk2] [PATCH v3 4/7]
> MdeModulePkg/UefiBootManagerLib: allow foreign
> Driver#### images
> 
> Allow PE/COFF images that must execute under emulation
> for Driver####
> options, by relaxing the machine type check to include
> support for
> machine types that is provided by an emulator.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Ard Biesheuvel
> <ard.biesheuvel@linaro.org>
> ---
>  MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
> | 51 +++++++++++++++++++-
>  MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
> |  1 +
> 
> MdeModulePkg/Library/UefiBootManagerLib/UefiBootManager
> Lib.inf |  1 +
>  3 files changed, 52 insertions(+), 1 deletion(-)
> 
> diff --git
> a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.
> c
> b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.
> c
> index 7bf96646c690..f6fda8f2c3f7 100644
> ---
> a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.
> c
> +++
> b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.
> c
> @@ -1185,6 +1185,54 @@ EfiBootManagerFreeLoadOptions (
>    return EFI_SUCCESS;
>  }
> 
> +STATIC
> +BOOLEAN
> +BmIsImageTypeSupported (
> +  IN  UINT16    MachineType,
> +  IN  UINT16    SubSystem
> +  )
> +{
> +  EFI_STATUS                            Status;
> +  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL  *Emu;
> +  UINTN                                 HandleCount;
> +  EFI_HANDLE                            *HandleBuffer;
> +  BOOLEAN                               ReturnValue;
> +  UINTN                                 Index;
> +
> +  if (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (MachineType))
> {
> +    return TRUE;
> +  }
> +
> +  Status = gBS->LocateHandleBuffer (
> +                  ByProtocol,
> +
> &gEdkiiPeCoffImageEmulatorProtocolGuid,
> +                  NULL,
> +                  &HandleCount,
> +                  &HandleBuffer
> +                  );
> +  if (EFI_ERROR (Status)) {
> +    return FALSE;
> +  }
> +
> +  ReturnValue = FALSE;
> +  for (Index = 0; Index < HandleCount; Index++) {
> +    Status = gBS->HandleProtocol (
> +                    HandleBuffer[Index],
> +
> &gEdkiiPeCoffImageEmulatorProtocolGuid,
> +                    (VOID **)&Emu
> +                    );
> +    ASSERT_EFI_ERROR (Status);
> +
> +    if (Emu->IsImageSupported (Emu, MachineType,
> SubSystem, NULL)) {
> +      ReturnValue = TRUE;
> +      break;
> +    }
> +  }
> +
> +  FreePool (HandleBuffer);
> +  return ReturnValue;
> +}
> +
>  /**
>    Return whether the PE header of the load option is
> valid or not.
> 
> @@ -1235,7 +1283,8 @@ BmIsLoadOptionPeHeaderValid (
>        OptionalHeader = (EFI_IMAGE_OPTIONAL_HEADER32 *)
> &PeHeader->Pe32.OptionalHeader;
>        if ((OptionalHeader->Magic ==
> EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC ||
>             OptionalHeader->Magic ==
> EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) &&
> -          EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeHeader-
> >Pe32.FileHeader.Machine)
> +          BmIsImageTypeSupported (PeHeader-
> >Pe32.FileHeader.Machine,
> +                                  OptionalHeader-
> >Subsystem)
>            ) {
>          //
>          // Check the Subsystem:
> diff --git
> a/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
> b/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
> index 978fbff966f6..5f64ef304b87 100644
> ---
> a/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
> +++
> b/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
> @@ -47,6 +47,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS
> OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>  #include <Protocol/VariableLock.h>
>  #include <Protocol/RamDisk.h>
>  #include <Protocol/DeferredImageLoad.h>
> +#include <Protocol/PeCoffImageEmulator.h>
> 
>  #include <Guid/MemoryTypeInformation.h>
>  #include <Guid/FileInfo.h>
> diff --git
> a/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManag
> erLib.inf
> b/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManag
> erLib.inf
> index 72c5ca1cd59e..09e2134acf8e 100644
> ---
> a/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManag
> erLib.inf
> +++
> b/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManag
> erLib.inf
> @@ -104,6 +104,7 @@
>    gEfiDevicePathProtocolGuid                    ##
> SOMETIMES_CONSUMES
>    gEfiBootLogoProtocolGuid                      ##
> SOMETIMES_CONSUMES
>    gEfiSimpleTextInputExProtocolGuid             ##
> SOMETIMES_CONSUMES
> +  gEdkiiPeCoffImageEmulatorProtocolGuid         ##
> SOMETIMES_CONSUMES
>    gEdkiiVariableLockProtocolGuid                ##
> SOMETIMES_CONSUMES
>    gEfiGraphicsOutputProtocolGuid                ##
> SOMETIMES_CONSUMES
>    gEfiUsbIoProtocolGuid                         ##
> SOMETIMES_CONSUMES
> --
> 2.17.1
> 
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol
  2018-09-26 17:32       ` Kinney, Michael D
@ 2018-09-27  0:48         ` Zeng, Star
  2018-09-27 10:58           ` Ard Biesheuvel
  0 siblings, 1 reply; 22+ messages in thread
From: Zeng, Star @ 2018-09-27  0:48 UTC (permalink / raw)
  To: Kinney, Michael D, Ard Biesheuvel
  Cc: edk2-devel@lists.01.org, Zimmer, Vincent, Richardson, Brian,
	Andrew Fish, Leif Lindholm, Dong, Eric, Ni, Ruiyu, Gao, Liming,
	Carsey, Jaben, Shi, Steven, Zeng, Star

Yes. This idea also came to my mind last night, then no need introduce PeCoffEmulatorDxe and no platform change is needed.

Thanks,
Star
-----Original Message-----
From: Kinney, Michael D 
Sent: Thursday, September 27, 2018 1:32 AM
To: Ard Biesheuvel <ard.biesheuvel@linaro.org>; Zeng, Star <star.zeng@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>
Cc: edk2-devel@lists.01.org; Zimmer, Vincent <vincent.zimmer@intel.com>; Richardson, Brian <brian.richardson@intel.com>; Andrew Fish <afish@apple.com>; Leif Lindholm <leif.lindholm@linaro.org>; Dong, Eric <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming <liming.gao@intel.com>; Carsey, Jaben <jaben.carsey@intel.com>; Shi, Steven <steven.shi@intel.com>
Subject: RE: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol

Hi Ard,

I think this registration protocol would be a new protocol in the MdeModulePkg and the protocol would be produced by the DXE Core.  The emulation drivers, including EBC would consume this protocol to register their services.  This removes the need for the DXE Core to do a Register Protocol Notify event for the emulator protocol.  The DXE Core would build a table of registered services, so it can quickly loop through them to check if an emulator supports a specific PE/COFF image or not.

Best regards,

Mike

> -----Original Message-----
> From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org]
> Sent: Wednesday, September 26, 2018 3:14 AM
> To: Zeng, Star <star.zeng@intel.com>
> Cc: edk2-devel@lists.01.org; Zimmer, Vincent 
> <vincent.zimmer@intel.com>; Richardson, Brian 
> <brian.richardson@intel.com>; Kinney, Michael D 
> <michael.d.kinney@intel.com>; Andrew Fish <afish@apple.com>; Leif 
> Lindholm <leif.lindholm@linaro.org>; Dong, Eric <eric.dong@intel.com>; 
> Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming <liming.gao@intel.com>; 
> Carsey, Jaben <jaben.carsey@intel.com>; Shi, Steven 
> <steven.shi@intel.com>
> Subject: Re: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image 
> emulator protocol
> 
> On Wed, 26 Sep 2018 at 12:07, Zeng, Star <star.zeng@intel.com> wrote:
> >
> > A little late feedback. Just an idea.
> >
> > Do you think whether it can make the consumer simpler
> like below or not?
> >
> > Add a new API RegisterInterfaces in the protocol and
> add a wrapper driver PeCoffEmulatorDxe.
> > The emulators can call RegisterInterfaces, and
> consumer will call other APIs simply, PeCoffEmulatorDxe will be a 
> wrapper as manager.
> >
> > typedef
> > EFI_STATUS
> > (EFIAPI
> *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES) (
> >   IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
> >   IN EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
> IsImageSupported,
> >   IN EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
> RegisterImage,
> >   IN EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
> UnregisterImage
> >   );
> >
> > typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL {
> >   EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES
> RegisterInterfaces;
> >   EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
> IsImageSupported;
> >   EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
> RegisterImage;
> >   EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
> UnregisterImage;
> > } EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> >
> 
> Hi Star,
> 
> Thanks for taking a look.
> 
> I tried to avoid introducing new drivers or library classes because it 
> will break existing platforms that incorporate EbcDxe.
> Do you think
> this is not an issue?
> 
> > -----Original Message-----
> > From: Ard Biesheuvel
> [mailto:ard.biesheuvel@linaro.org]
> > Sent: Friday, September 21, 2018 7:02 AM
> > To: edk2-devel@lists.01.org
> > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>;
> Zimmer, Vincent <vincent.zimmer@intel.com>; Richardson, Brian 
> <brian.richardson@intel.com>; Kinney, Michael D 
> <michael.d.kinney@intel.com>; Andrew Fish <afish@apple.com>; Leif 
> Lindholm <leif.lindholm@linaro.org>; Zeng, Star <star.zeng@intel.com>; 
> Dong, Eric <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, 
> Liming <liming.gao@intel.com>; Carsey, Jaben <jaben.carsey@intel.com>; 
> Shi, Steven <steven.shi@intel.com>
> > Subject: [PATCH v3 1/7] MdeModulePkg: introduce
> PE/COFF image emulator protocol
> >
> > Introduce a protocol that can be invoked by the image
> loading services to execute foreign architecture PE/COFF images via an 
> emulator.
> >
> > Contributed-under: TianoCore Contribution Agreement
> 1.1
> > Signed-off-by: Ard Biesheuvel
> <ard.biesheuvel@linaro.org>
> > ---
> >  MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h |
> 102 ++++++++++++++++++++
> >  MdeModulePkg/MdeModulePkg.dec                       |
> 4 +
> >  2 files changed, 106 insertions(+)
> >
> > diff --git
> a/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> > new file mode 100644
> > index 000000000000..27bad556209c
> > --- /dev/null
> > +++
> b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> > @@ -0,0 +1,102 @@
> > +/** @file
> > +  Copyright (c) 2018, Linaro, Ltd. All rights
> reserved.<BR>
> > +
> > +  This program and the accompanying materials are
> licensed and made
> > + available  under the terms and conditions of the BSD
> License which
> > + accompanies this  distribution.  The full text of
> the license may be
> > + found at  http://opensource.org/licenses/bsd-
> license.php
> > +
> > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON
> AN "AS IS" BASIS,
> > + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND,
> EITHER EXPRESS OR IMPLIED.
> > +
> > +**/
> > +
> > +#ifndef PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
> > +#define PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
> > +
> > +#define EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID \
> > +  { 0x96F46153, 0x97A7, 0x4793, { 0xAC, 0xC1, 0xFA,
> 0x19, 0xBF, 0x78,
> > +0xEA, 0x97 } }
> > +
> > +typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> > +EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> > +
> > +/**
> > +  Check whether the emulator supports executing a
> certain PE/COFF image
> > +
> > +  @param[in] This         This pointer for
> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> > +                          structure  @param[in] MachineType  The 
> > + machine type for which
> the image was built
> > +  @param[in] ImageType    Whether the image is an
> application, a boot time
> > +                          driver or a runtime driver.
> > +  @param[in] DevicePath   Path to device where the
> image originated
> > +                          (e.g., a PCI option ROM)
> > +
> > +  @retval TRUE            The image is supported by
> the emulator
> > +  @retval FALSE           The image is not supported
> by the emulator.
> > +**/
> > +typedef
> > +BOOLEAN
> > +(EFIAPI
> *EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED) (
> > +  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
> > +  IN  UINT16
> MachineType,
> > +  IN  UINT16
> ImageType,
> > +  IN  EFI_DEVICE_PATH_PROTOCOL
> *DevicePath   OPTIONAL
> > +  );
> > +
> > +/**
> > +  Register a supported PE/COFF image with the
> emulator. After this call
> > +  completes successfully, the PE/COFF image may be
> started as usual,
> > +and
> > +  it is the responsibility of the emulator
> implementation that any
> > +branch
> > +  into the code section of the image (including
> returns from functions
> > +called
> > +  from the foreign code) is executed as if it were
> running on the
> > +machine
> > +  type it was built for.
> > +
> > +  @param[in]      This          This pointer for
> > +
> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL structure
> > +  @param[in]      ImageBase     The base address in
> memory of the PE/COFF image
> > +  @param[in]      ImageSize     The size in memory of
> the PE/COFF image
> > +  @param[in,out]  EntryPoint    The entry point of
> the PE/COFF image. Passed by
> > +                                reference so that the
> emulator may modify it.
> > +
> > +  @retval EFI_SUCCESS           The image was
> registered with the emulator and
> > +                                can be started as
> usual.
> > +  @retval other                 The image could not
> be registered.
> > +
> > +  If the PE/COFF machine type or image type are not
> supported by the
> > +emulator,
> > +  then ASSERT().
> > +**/
> > +typedef
> > +EFI_STATUS
> > +(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE)
> (
> > +  IN      EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> *This,
> > +  IN      EFI_PHYSICAL_ADDRESS
> ImageBase,
> > +  IN      UINT64
> ImageSize,
> > +  IN  OUT EFI_IMAGE_ENTRY_POINT
> *EntryPoint
> > +  );
> > +
> > +/**
> > +  Unregister a PE/COFF image that has been registered
> with the emulator.
> > +  This should be done before the image is unloaded
> from memory.
> > +
> > +  @param[in] This         This pointer for
> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> > +                          structure
> > +  @param[in] ImageBase    The base address in memory
> of the PE/COFF image
> > +
> > +  @retval EFI_SUCCESS     The image was unregistered
> with the emulator.
> > +  @retval other           Image could not be
> unloaded.
> > +**/
> > +typedef
> > +EFI_STATUS
> > +(EFIAPI
> *EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE) (
> > +  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
> > +  IN  EFI_PHYSICAL_ADDRESS
> ImageBase
> > +  );
> > +
> > +typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> {
> > +  EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
> IsImageSupported;
> > +  EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
> RegisterImage;
> > +  EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
> UnregisterImage;
> > +} EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> > +
> > +extern EFI_GUID
> gEdkiiPeCoffImageEmulatorProtocolGuid;
> > +
> > +#endif
> > diff --git a/MdeModulePkg/MdeModulePkg.dec
> b/MdeModulePkg/MdeModulePkg.dec index
> 6a6d9660edc2..be307329f901 100644
> > --- a/MdeModulePkg/MdeModulePkg.dec
> > +++ b/MdeModulePkg/MdeModulePkg.dec
> > @@ -617,6 +617,10 @@
> >
> >    ## Include/Protocol/AtaAtapiPolicy.h
> >    gEdkiiAtaAtapiPolicyProtocolGuid = { 0xe59cd769,
> 0x5083, 0x4f26,{ 0x90, 0x94, 0x6c, 0x91, 0x9f, 0x91, 0x6c, 0x4e } }
> > +
> > +  ## Include/Protocol/PeCoffImageEmulator.h
> > +  gEdkiiPeCoffImageEmulatorProtocolGuid = {
> 0x96f46153, 0x97a7, 0x4793,
> > + { 0xac, 0xc1, 0xfa, 0x19, 0xbf, 0x78, 0xea, 0x97 } }
> > +
> >  #
> >  # [Error.gEfiMdeModulePkgTokenSpaceGuid]
> >  #   0x80000001 | Invalid value provided.
> > --
> > 2.17.1
> >

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol
  2018-09-27  0:48         ` Zeng, Star
@ 2018-09-27 10:58           ` Ard Biesheuvel
  2018-09-27 15:36             ` Kinney, Michael D
  0 siblings, 1 reply; 22+ messages in thread
From: Ard Biesheuvel @ 2018-09-27 10:58 UTC (permalink / raw)
  To: Zeng, Star
  Cc: Kinney, Michael D, edk2-devel@lists.01.org, Zimmer, Vincent,
	Richardson, Brian, Andrew Fish, Leif Lindholm, Dong, Eric,
	Ni, Ruiyu, Gao, Liming, Carsey, Jaben, Shi, Steven

On 27 September 2018 at 02:48, Zeng, Star <star.zeng@intel.com> wrote:
> Yes. This idea also came to my mind last night, then no need introduce PeCoffEmulatorDxe and no platform change is needed.
>

The only problem with this approach is that we cannot keep track of
which emulator returned TRUE for IsSupported(), and so RegisterImage()
and also UnregisterImage() will have to do some internal bookkeeping
in the 'manager' protocol implementation that duplicates code in the
emulators.

So could we flesh this out a bit before I dive into the code again and
turn everything upside down?

- the PCI bus driver no longer checks the machine type
- the BDS no longer checks the machine type for Driver#### images
- CoreLoadImage() checks IsSupported() to establish whether the
emulator manager has an emulator available that is willing to take
charge of the image
- what happens next? CoreLoadImage() calls RegisterImage() on what?
Should IsSupported() return some kind of handle?

Also, perhaps we should simply stick with one emulator per machine
type (which doesn't preclude an implementation from registering itself
for multiple ones). That simplifies the IsSupported vs RegisterImage
issue above as well (i.e., it is unambiguous *which* RegisterImage()
should be called)



> -----Original Message-----
> From: Kinney, Michael D
> Sent: Thursday, September 27, 2018 1:32 AM
> To: Ard Biesheuvel <ard.biesheuvel@linaro.org>; Zeng, Star <star.zeng@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>
> Cc: edk2-devel@lists.01.org; Zimmer, Vincent <vincent.zimmer@intel.com>; Richardson, Brian <brian.richardson@intel.com>; Andrew Fish <afish@apple.com>; Leif Lindholm <leif.lindholm@linaro.org>; Dong, Eric <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming <liming.gao@intel.com>; Carsey, Jaben <jaben.carsey@intel.com>; Shi, Steven <steven.shi@intel.com>
> Subject: RE: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol
>
> Hi Ard,
>
> I think this registration protocol would be a new protocol in the MdeModulePkg and the protocol would be produced by the DXE Core.  The emulation drivers, including EBC would consume this protocol to register their services.  This removes the need for the DXE Core to do a Register Protocol Notify event for the emulator protocol.  The DXE Core would build a table of registered services, so it can quickly loop through them to check if an emulator supports a specific PE/COFF image or not.
>
> Best regards,
>
> Mike
>
>> -----Original Message-----
>> From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org]
>> Sent: Wednesday, September 26, 2018 3:14 AM
>> To: Zeng, Star <star.zeng@intel.com>
>> Cc: edk2-devel@lists.01.org; Zimmer, Vincent
>> <vincent.zimmer@intel.com>; Richardson, Brian
>> <brian.richardson@intel.com>; Kinney, Michael D
>> <michael.d.kinney@intel.com>; Andrew Fish <afish@apple.com>; Leif
>> Lindholm <leif.lindholm@linaro.org>; Dong, Eric <eric.dong@intel.com>;
>> Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming <liming.gao@intel.com>;
>> Carsey, Jaben <jaben.carsey@intel.com>; Shi, Steven
>> <steven.shi@intel.com>
>> Subject: Re: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image
>> emulator protocol
>>
>> On Wed, 26 Sep 2018 at 12:07, Zeng, Star <star.zeng@intel.com> wrote:
>> >
>> > A little late feedback. Just an idea.
>> >
>> > Do you think whether it can make the consumer simpler
>> like below or not?
>> >
>> > Add a new API RegisterInterfaces in the protocol and
>> add a wrapper driver PeCoffEmulatorDxe.
>> > The emulators can call RegisterInterfaces, and
>> consumer will call other APIs simply, PeCoffEmulatorDxe will be a
>> wrapper as manager.
>> >
>> > typedef
>> > EFI_STATUS
>> > (EFIAPI
>> *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES) (
>> >   IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
>> >   IN EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
>> IsImageSupported,
>> >   IN EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
>> RegisterImage,
>> >   IN EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
>> UnregisterImage
>> >   );
>> >
>> > typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL {
>> >   EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES
>> RegisterInterfaces;
>> >   EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
>> IsImageSupported;
>> >   EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
>> RegisterImage;
>> >   EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
>> UnregisterImage;
>> > } EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
>> >
>>
>> Hi Star,
>>
>> Thanks for taking a look.
>>
>> I tried to avoid introducing new drivers or library classes because it
>> will break existing platforms that incorporate EbcDxe.
>> Do you think
>> this is not an issue?
>>
>> > -----Original Message-----
>> > From: Ard Biesheuvel
>> [mailto:ard.biesheuvel@linaro.org]
>> > Sent: Friday, September 21, 2018 7:02 AM
>> > To: edk2-devel@lists.01.org
>> > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>;
>> Zimmer, Vincent <vincent.zimmer@intel.com>; Richardson, Brian
>> <brian.richardson@intel.com>; Kinney, Michael D
>> <michael.d.kinney@intel.com>; Andrew Fish <afish@apple.com>; Leif
>> Lindholm <leif.lindholm@linaro.org>; Zeng, Star <star.zeng@intel.com>;
>> Dong, Eric <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; Gao,
>> Liming <liming.gao@intel.com>; Carsey, Jaben <jaben.carsey@intel.com>;
>> Shi, Steven <steven.shi@intel.com>
>> > Subject: [PATCH v3 1/7] MdeModulePkg: introduce
>> PE/COFF image emulator protocol
>> >
>> > Introduce a protocol that can be invoked by the image
>> loading services to execute foreign architecture PE/COFF images via an
>> emulator.
>> >
>> > Contributed-under: TianoCore Contribution Agreement
>> 1.1
>> > Signed-off-by: Ard Biesheuvel
>> <ard.biesheuvel@linaro.org>
>> > ---
>> >  MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h |
>> 102 ++++++++++++++++++++
>> >  MdeModulePkg/MdeModulePkg.dec                       |
>> 4 +
>> >  2 files changed, 106 insertions(+)
>> >
>> > diff --git
>> a/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
>> b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
>> > new file mode 100644
>> > index 000000000000..27bad556209c
>> > --- /dev/null
>> > +++
>> b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
>> > @@ -0,0 +1,102 @@
>> > +/** @file
>> > +  Copyright (c) 2018, Linaro, Ltd. All rights
>> reserved.<BR>
>> > +
>> > +  This program and the accompanying materials are
>> licensed and made
>> > + available  under the terms and conditions of the BSD
>> License which
>> > + accompanies this  distribution.  The full text of
>> the license may be
>> > + found at  http://opensource.org/licenses/bsd-
>> license.php
>> > +
>> > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON
>> AN "AS IS" BASIS,
>> > + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND,
>> EITHER EXPRESS OR IMPLIED.
>> > +
>> > +**/
>> > +
>> > +#ifndef PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
>> > +#define PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
>> > +
>> > +#define EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID \
>> > +  { 0x96F46153, 0x97A7, 0x4793, { 0xAC, 0xC1, 0xFA,
>> 0x19, 0xBF, 0x78,
>> > +0xEA, 0x97 } }
>> > +
>> > +typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
>> > +EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
>> > +
>> > +/**
>> > +  Check whether the emulator supports executing a
>> certain PE/COFF image
>> > +
>> > +  @param[in] This         This pointer for
>> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
>> > +                          structure  @param[in] MachineType  The
>> > + machine type for which
>> the image was built
>> > +  @param[in] ImageType    Whether the image is an
>> application, a boot time
>> > +                          driver or a runtime driver.
>> > +  @param[in] DevicePath   Path to device where the
>> image originated
>> > +                          (e.g., a PCI option ROM)
>> > +
>> > +  @retval TRUE            The image is supported by
>> the emulator
>> > +  @retval FALSE           The image is not supported
>> by the emulator.
>> > +**/
>> > +typedef
>> > +BOOLEAN
>> > +(EFIAPI
>> *EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED) (
>> > +  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
>> > +  IN  UINT16
>> MachineType,
>> > +  IN  UINT16
>> ImageType,
>> > +  IN  EFI_DEVICE_PATH_PROTOCOL
>> *DevicePath   OPTIONAL
>> > +  );
>> > +
>> > +/**
>> > +  Register a supported PE/COFF image with the
>> emulator. After this call
>> > +  completes successfully, the PE/COFF image may be
>> started as usual,
>> > +and
>> > +  it is the responsibility of the emulator
>> implementation that any
>> > +branch
>> > +  into the code section of the image (including
>> returns from functions
>> > +called
>> > +  from the foreign code) is executed as if it were
>> running on the
>> > +machine
>> > +  type it was built for.
>> > +
>> > +  @param[in]      This          This pointer for
>> > +
>> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL structure
>> > +  @param[in]      ImageBase     The base address in
>> memory of the PE/COFF image
>> > +  @param[in]      ImageSize     The size in memory of
>> the PE/COFF image
>> > +  @param[in,out]  EntryPoint    The entry point of
>> the PE/COFF image. Passed by
>> > +                                reference so that the
>> emulator may modify it.
>> > +
>> > +  @retval EFI_SUCCESS           The image was
>> registered with the emulator and
>> > +                                can be started as
>> usual.
>> > +  @retval other                 The image could not
>> be registered.
>> > +
>> > +  If the PE/COFF machine type or image type are not
>> supported by the
>> > +emulator,
>> > +  then ASSERT().
>> > +**/
>> > +typedef
>> > +EFI_STATUS
>> > +(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE)
>> (
>> > +  IN      EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
>> *This,
>> > +  IN      EFI_PHYSICAL_ADDRESS
>> ImageBase,
>> > +  IN      UINT64
>> ImageSize,
>> > +  IN  OUT EFI_IMAGE_ENTRY_POINT
>> *EntryPoint
>> > +  );
>> > +
>> > +/**
>> > +  Unregister a PE/COFF image that has been registered
>> with the emulator.
>> > +  This should be done before the image is unloaded
>> from memory.
>> > +
>> > +  @param[in] This         This pointer for
>> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
>> > +                          structure
>> > +  @param[in] ImageBase    The base address in memory
>> of the PE/COFF image
>> > +
>> > +  @retval EFI_SUCCESS     The image was unregistered
>> with the emulator.
>> > +  @retval other           Image could not be
>> unloaded.
>> > +**/
>> > +typedef
>> > +EFI_STATUS
>> > +(EFIAPI
>> *EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE) (
>> > +  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL    *This,
>> > +  IN  EFI_PHYSICAL_ADDRESS
>> ImageBase
>> > +  );
>> > +
>> > +typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
>> {
>> > +  EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
>> IsImageSupported;
>> > +  EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
>> RegisterImage;
>> > +  EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
>> UnregisterImage;
>> > +} EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
>> > +
>> > +extern EFI_GUID
>> gEdkiiPeCoffImageEmulatorProtocolGuid;
>> > +
>> > +#endif
>> > diff --git a/MdeModulePkg/MdeModulePkg.dec
>> b/MdeModulePkg/MdeModulePkg.dec index
>> 6a6d9660edc2..be307329f901 100644
>> > --- a/MdeModulePkg/MdeModulePkg.dec
>> > +++ b/MdeModulePkg/MdeModulePkg.dec
>> > @@ -617,6 +617,10 @@
>> >
>> >    ## Include/Protocol/AtaAtapiPolicy.h
>> >    gEdkiiAtaAtapiPolicyProtocolGuid = { 0xe59cd769,
>> 0x5083, 0x4f26,{ 0x90, 0x94, 0x6c, 0x91, 0x9f, 0x91, 0x6c, 0x4e } }
>> > +
>> > +  ## Include/Protocol/PeCoffImageEmulator.h
>> > +  gEdkiiPeCoffImageEmulatorProtocolGuid = {
>> 0x96f46153, 0x97a7, 0x4793,
>> > + { 0xac, 0xc1, 0xfa, 0x19, 0xbf, 0x78, 0xea, 0x97 } }
>> > +
>> >  #
>> >  # [Error.gEfiMdeModulePkgTokenSpaceGuid]
>> >  #   0x80000001 | Invalid value provided.
>> > --
>> > 2.17.1
>> >


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol
  2018-09-27 10:58           ` Ard Biesheuvel
@ 2018-09-27 15:36             ` Kinney, Michael D
  2018-09-28  3:05               ` Zeng, Star
  0 siblings, 1 reply; 22+ messages in thread
From: Kinney, Michael D @ 2018-09-27 15:36 UTC (permalink / raw)
  To: Ard Biesheuvel, Zeng, Star, Kinney, Michael D
  Cc: edk2-devel@lists.01.org, Zimmer, Vincent, Richardson, Brian,
	Andrew Fish, Leif Lindholm, Dong, Eric, Ni, Ruiyu, Gao, Liming,
	Carsey, Jaben, Shi, Steven

Hi Ard,

Yes.  I think it is simpler if an emulator module registers
one machine type at a time.  If an emulator module wants
to do a single registration and that registration supports
multiple machine types, then the emulator implementation
is more complex, but it is still feasible.

I think I see where things got confused.  The proposal from
Star for a new protocol produced by the DXE Core has some issues.
A refined proposal is shown below.  It also supports the idea that
an emulator module can be loaded and unloaded to support testing
from environments like the UEFI Shell to soft load an emulator,
test some images, and unload it.

Also, by having a protocol produced by the DXE Core, the emulator
module can tell if the DXE Core supports emulators or not 
by checking for the presence of this protocol.  The emulator
module can fail gracefully with some DEBUG() message and unload
if DXE Core does not support emulators.  This also allows the
option to update the DXE Core module to optionally support 
emulators using a PCD Feature Flag and remove some logic if
emulators are not required on a specific platform.

typedef
EFI_STATUS
(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_ADD_EMULATOR) (
  IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL            *This,
  IN EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED  IsImageSupported,
  IN EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE      RegisterImage,
  IN EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE    UnregisterImage
  );

typedef
EFI_STATUS
(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_REMOVE_EMULATOR) (
  IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL            *This,
  IN EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED  IsImageSupported,
  IN EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE      RegisterImage,
  IN EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE    UnregisterImage
  );

typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL {
  EDKII_PECOFF_IMAGE_EMULATOR_ADD_EMULATOR     AddEmulator;
  EDKII_PECOFF_IMAGE_EMULATOR_REMOVE_EMULATOR  RemoveEmulator;
} EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL

The IsImageSupported(), RegisterImage(), and UnRegisterImage()
are added in a set in a single call to AddEmulator() from the
emulator module.  In LoadImage(), if IsImageSupported() returns
TRUE, LoadImage() uses the RegisterImage() and UnRegisterImage()
APIs that were added with the IsImageSupported() API in the
call to AddEmulator().

Thanks,

Mike

> -----Original Message-----
> From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org]
> Sent: Thursday, September 27, 2018 3:59 AM
> To: Zeng, Star <star.zeng@intel.com>
> Cc: Kinney, Michael D <michael.d.kinney@intel.com>;
> edk2-devel@lists.01.org; Zimmer, Vincent
> <vincent.zimmer@intel.com>; Richardson, Brian
> <brian.richardson@intel.com>; Andrew Fish
> <afish@apple.com>; Leif Lindholm
> <leif.lindholm@linaro.org>; Dong, Eric
> <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>;
> Gao, Liming <liming.gao@intel.com>; Carsey, Jaben
> <jaben.carsey@intel.com>; Shi, Steven
> <steven.shi@intel.com>
> Subject: Re: [PATCH v3 1/7] MdeModulePkg: introduce
> PE/COFF image emulator protocol
> 
> On 27 September 2018 at 02:48, Zeng, Star
> <star.zeng@intel.com> wrote:
> > Yes. This idea also came to my mind last night, then
> no need introduce PeCoffEmulatorDxe and no platform
> change is needed.
> >
> 
> The only problem with this approach is that we cannot
> keep track of
> which emulator returned TRUE for IsSupported(), and so
> RegisterImage()
> and also UnregisterImage() will have to do some
> internal bookkeeping
> in the 'manager' protocol implementation that
> duplicates code in the
> emulators.
> 
> So could we flesh this out a bit before I dive into the
> code again and
> turn everything upside down?
> 
> - the PCI bus driver no longer checks the machine type
> - the BDS no longer checks the machine type for
> Driver#### images
> - CoreLoadImage() checks IsSupported() to establish
> whether the
> emulator manager has an emulator available that is
> willing to take
> charge of the image
> - what happens next? CoreLoadImage() calls
> RegisterImage() on what?
> Should IsSupported() return some kind of handle?
> 
> Also, perhaps we should simply stick with one emulator
> per machine
> type (which doesn't preclude an implementation from
> registering itself
> for multiple ones). That simplifies the IsSupported vs
> RegisterImage
> issue above as well (i.e., it is unambiguous *which*
> RegisterImage()
> should be called)
> 
> 
> 
> > -----Original Message-----
> > From: Kinney, Michael D
> > Sent: Thursday, September 27, 2018 1:32 AM
> > To: Ard Biesheuvel <ard.biesheuvel@linaro.org>; Zeng,
> Star <star.zeng@intel.com>; Kinney, Michael D
> <michael.d.kinney@intel.com>
> > Cc: edk2-devel@lists.01.org; Zimmer, Vincent
> <vincent.zimmer@intel.com>; Richardson, Brian
> <brian.richardson@intel.com>; Andrew Fish
> <afish@apple.com>; Leif Lindholm
> <leif.lindholm@linaro.org>; Dong, Eric
> <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>;
> Gao, Liming <liming.gao@intel.com>; Carsey, Jaben
> <jaben.carsey@intel.com>; Shi, Steven
> <steven.shi@intel.com>
> > Subject: RE: [PATCH v3 1/7] MdeModulePkg: introduce
> PE/COFF image emulator protocol
> >
> > Hi Ard,
> >
> > I think this registration protocol would be a new
> protocol in the MdeModulePkg and the protocol would be
> produced by the DXE Core.  The emulation drivers,
> including EBC would consume this protocol to register
> their services.  This removes the need for the DXE Core
> to do a Register Protocol Notify event for the emulator
> protocol.  The DXE Core would build a table of
> registered services, so it can quickly loop through
> them to check if an emulator supports a specific
> PE/COFF image or not.
> >
> > Best regards,
> >
> > Mike
> >
> >> -----Original Message-----
> >> From: Ard Biesheuvel
> [mailto:ard.biesheuvel@linaro.org]
> >> Sent: Wednesday, September 26, 2018 3:14 AM
> >> To: Zeng, Star <star.zeng@intel.com>
> >> Cc: edk2-devel@lists.01.org; Zimmer, Vincent
> >> <vincent.zimmer@intel.com>; Richardson, Brian
> >> <brian.richardson@intel.com>; Kinney, Michael D
> >> <michael.d.kinney@intel.com>; Andrew Fish
> <afish@apple.com>; Leif
> >> Lindholm <leif.lindholm@linaro.org>; Dong, Eric
> <eric.dong@intel.com>;
> >> Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming
> <liming.gao@intel.com>;
> >> Carsey, Jaben <jaben.carsey@intel.com>; Shi, Steven
> >> <steven.shi@intel.com>
> >> Subject: Re: [PATCH v3 1/7] MdeModulePkg: introduce
> PE/COFF image
> >> emulator protocol
> >>
> >> On Wed, 26 Sep 2018 at 12:07, Zeng, Star
> <star.zeng@intel.com> wrote:
> >> >
> >> > A little late feedback. Just an idea.
> >> >
> >> > Do you think whether it can make the consumer
> simpler
> >> like below or not?
> >> >
> >> > Add a new API RegisterInterfaces in the protocol
> and
> >> add a wrapper driver PeCoffEmulatorDxe.
> >> > The emulators can call RegisterInterfaces, and
> >> consumer will call other APIs simply,
> PeCoffEmulatorDxe will be a
> >> wrapper as manager.
> >> >
> >> > typedef
> >> > EFI_STATUS
> >> > (EFIAPI
> >> *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES) (
> >> >   IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> *This,
> >> >   IN
> EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
> >> IsImageSupported,
> >> >   IN EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
> >> RegisterImage,
> >> >   IN EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
> >> UnregisterImage
> >> >   );
> >> >
> >> > typedef struct
> _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL {
> >> >   EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES
> >> RegisterInterfaces;
> >> >   EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
> >> IsImageSupported;
> >> >   EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
> >> RegisterImage;
> >> >   EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
> >> UnregisterImage;
> >> > } EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> >> >
> >>
> >> Hi Star,
> >>
> >> Thanks for taking a look.
> >>
> >> I tried to avoid introducing new drivers or library
> classes because it
> >> will break existing platforms that incorporate
> EbcDxe.
> >> Do you think
> >> this is not an issue?
> >>
> >> > -----Original Message-----
> >> > From: Ard Biesheuvel
> >> [mailto:ard.biesheuvel@linaro.org]
> >> > Sent: Friday, September 21, 2018 7:02 AM
> >> > To: edk2-devel@lists.01.org
> >> > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>;
> >> Zimmer, Vincent <vincent.zimmer@intel.com>;
> Richardson, Brian
> >> <brian.richardson@intel.com>; Kinney, Michael D
> >> <michael.d.kinney@intel.com>; Andrew Fish
> <afish@apple.com>; Leif
> >> Lindholm <leif.lindholm@linaro.org>; Zeng, Star
> <star.zeng@intel.com>;
> >> Dong, Eric <eric.dong@intel.com>; Ni, Ruiyu
> <ruiyu.ni@intel.com>; Gao,
> >> Liming <liming.gao@intel.com>; Carsey, Jaben
> <jaben.carsey@intel.com>;
> >> Shi, Steven <steven.shi@intel.com>
> >> > Subject: [PATCH v3 1/7] MdeModulePkg: introduce
> >> PE/COFF image emulator protocol
> >> >
> >> > Introduce a protocol that can be invoked by the
> image
> >> loading services to execute foreign architecture
> PE/COFF images via an
> >> emulator.
> >> >
> >> > Contributed-under: TianoCore Contribution
> Agreement
> >> 1.1
> >> > Signed-off-by: Ard Biesheuvel
> >> <ard.biesheuvel@linaro.org>
> >> > ---
> >> >
> MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h |
> >> 102 ++++++++++++++++++++
> >> >  MdeModulePkg/MdeModulePkg.dec
> |
> >> 4 +
> >> >  2 files changed, 106 insertions(+)
> >> >
> >> > diff --git
> >>
> a/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> >>
> b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> >> > new file mode 100644
> >> > index 000000000000..27bad556209c
> >> > --- /dev/null
> >> > +++
> >>
> b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> >> > @@ -0,0 +1,102 @@
> >> > +/** @file
> >> > +  Copyright (c) 2018, Linaro, Ltd. All rights
> >> reserved.<BR>
> >> > +
> >> > +  This program and the accompanying materials are
> >> licensed and made
> >> > + available  under the terms and conditions of the
> BSD
> >> License which
> >> > + accompanies this  distribution.  The full text
> of
> >> the license may be
> >> > + found at  http://opensource.org/licenses/bsd-
> >> license.php
> >> > +
> >> > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD
> LICENSE ON
> >> AN "AS IS" BASIS,
> >> > + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY
> KIND,
> >> EITHER EXPRESS OR IMPLIED.
> >> > +
> >> > +**/
> >> > +
> >> > +#ifndef PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
> >> > +#define PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
> >> > +
> >> > +#define EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID
> \
> >> > +  { 0x96F46153, 0x97A7, 0x4793, { 0xAC, 0xC1,
> 0xFA,
> >> 0x19, 0xBF, 0x78,
> >> > +0xEA, 0x97 } }
> >> > +
> >> > +typedef struct
> _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> >> > +EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> >> > +
> >> > +/**
> >> > +  Check whether the emulator supports executing a
> >> certain PE/COFF image
> >> > +
> >> > +  @param[in] This         This pointer for
> >> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> >> > +                          structure  @param[in]
> MachineType  The
> >> > + machine type for which
> >> the image was built
> >> > +  @param[in] ImageType    Whether the image is an
> >> application, a boot time
> >> > +                          driver or a runtime
> driver.
> >> > +  @param[in] DevicePath   Path to device where
> the
> >> image originated
> >> > +                          (e.g., a PCI option
> ROM)
> >> > +
> >> > +  @retval TRUE            The image is supported
> by
> >> the emulator
> >> > +  @retval FALSE           The image is not
> supported
> >> by the emulator.
> >> > +**/
> >> > +typedef
> >> > +BOOLEAN
> >> > +(EFIAPI
> >> *EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED) (
> >> > +  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> *This,
> >> > +  IN  UINT16
> >> MachineType,
> >> > +  IN  UINT16
> >> ImageType,
> >> > +  IN  EFI_DEVICE_PATH_PROTOCOL
> >> *DevicePath   OPTIONAL
> >> > +  );
> >> > +
> >> > +/**
> >> > +  Register a supported PE/COFF image with the
> >> emulator. After this call
> >> > +  completes successfully, the PE/COFF image may
> be
> >> started as usual,
> >> > +and
> >> > +  it is the responsibility of the emulator
> >> implementation that any
> >> > +branch
> >> > +  into the code section of the image (including
> >> returns from functions
> >> > +called
> >> > +  from the foreign code) is executed as if it
> were
> >> running on the
> >> > +machine
> >> > +  type it was built for.
> >> > +
> >> > +  @param[in]      This          This pointer for
> >> > +
> >> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL structure
> >> > +  @param[in]      ImageBase     The base address
> in
> >> memory of the PE/COFF image
> >> > +  @param[in]      ImageSize     The size in
> memory of
> >> the PE/COFF image
> >> > +  @param[in,out]  EntryPoint    The entry point
> of
> >> the PE/COFF image. Passed by
> >> > +                                reference so that
> the
> >> emulator may modify it.
> >> > +
> >> > +  @retval EFI_SUCCESS           The image was
> >> registered with the emulator and
> >> > +                                can be started as
> >> usual.
> >> > +  @retval other                 The image could
> not
> >> be registered.
> >> > +
> >> > +  If the PE/COFF machine type or image type are
> not
> >> supported by the
> >> > +emulator,
> >> > +  then ASSERT().
> >> > +**/
> >> > +typedef
> >> > +EFI_STATUS
> >> > +(EFIAPI
> *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE)
> >> (
> >> > +  IN      EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> >> *This,
> >> > +  IN      EFI_PHYSICAL_ADDRESS
> >> ImageBase,
> >> > +  IN      UINT64
> >> ImageSize,
> >> > +  IN  OUT EFI_IMAGE_ENTRY_POINT
> >> *EntryPoint
> >> > +  );
> >> > +
> >> > +/**
> >> > +  Unregister a PE/COFF image that has been
> registered
> >> with the emulator.
> >> > +  This should be done before the image is
> unloaded
> >> from memory.
> >> > +
> >> > +  @param[in] This         This pointer for
> >> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> >> > +                          structure
> >> > +  @param[in] ImageBase    The base address in
> memory
> >> of the PE/COFF image
> >> > +
> >> > +  @retval EFI_SUCCESS     The image was
> unregistered
> >> with the emulator.
> >> > +  @retval other           Image could not be
> >> unloaded.
> >> > +**/
> >> > +typedef
> >> > +EFI_STATUS
> >> > +(EFIAPI
> >> *EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE) (
> >> > +  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> *This,
> >> > +  IN  EFI_PHYSICAL_ADDRESS
> >> ImageBase
> >> > +  );
> >> > +
> >> > +typedef struct
> _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> >> {
> >> > +  EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
> >> IsImageSupported;
> >> > +  EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
> >> RegisterImage;
> >> > +  EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
> >> UnregisterImage;
> >> > +} EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> >> > +
> >> > +extern EFI_GUID
> >> gEdkiiPeCoffImageEmulatorProtocolGuid;
> >> > +
> >> > +#endif
> >> > diff --git a/MdeModulePkg/MdeModulePkg.dec
> >> b/MdeModulePkg/MdeModulePkg.dec index
> >> 6a6d9660edc2..be307329f901 100644
> >> > --- a/MdeModulePkg/MdeModulePkg.dec
> >> > +++ b/MdeModulePkg/MdeModulePkg.dec
> >> > @@ -617,6 +617,10 @@
> >> >
> >> >    ## Include/Protocol/AtaAtapiPolicy.h
> >> >    gEdkiiAtaAtapiPolicyProtocolGuid = {
> 0xe59cd769,
> >> 0x5083, 0x4f26,{ 0x90, 0x94, 0x6c, 0x91, 0x9f, 0x91,
> 0x6c, 0x4e } }
> >> > +
> >> > +  ## Include/Protocol/PeCoffImageEmulator.h
> >> > +  gEdkiiPeCoffImageEmulatorProtocolGuid = {
> >> 0x96f46153, 0x97a7, 0x4793,
> >> > + { 0xac, 0xc1, 0xfa, 0x19, 0xbf, 0x78, 0xea, 0x97
> } }
> >> > +
> >> >  #
> >> >  # [Error.gEfiMdeModulePkgTokenSpaceGuid]
> >> >  #   0x80000001 | Invalid value provided.
> >> > --
> >> > 2.17.1
> >> >

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol
  2018-09-27 15:36             ` Kinney, Michael D
@ 2018-09-28  3:05               ` Zeng, Star
  2018-09-28  3:08                 ` Zeng, Star
  0 siblings, 1 reply; 22+ messages in thread
From: Zeng, Star @ 2018-09-28  3:05 UTC (permalink / raw)
  To: Kinney, Michael D, Ard Biesheuvel
  Cc: edk2-devel@lists.01.org, Zimmer, Vincent, Richardson, Brian,
	Andrew Fish, Leif Lindholm, Dong, Eric, Ni, Ruiyu, Gao, Liming,
	Carsey, Jaben, Shi, Steven, Zeng, Star

Mike,

Good idea.
You prefer to introduce a new feature PCD for this with default value = TRUE?

Could we group the emulator APIs to one structure like below? And add a Version field for potential further extension?

typedef struct {
  UINTN                                           Version;
  EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED  IsImageSupported;
  EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE      RegisterImage;
  EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE    UnregisterImage;
} PECOFF_IMAGE_EMULATOR;


Thanks,
Star
-----Original Message-----
From: Kinney, Michael D 
Sent: Thursday, September 27, 2018 11:37 PM
To: Ard Biesheuvel <ard.biesheuvel@linaro.org>; Zeng, Star <star.zeng@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>
Cc: edk2-devel@lists.01.org; Zimmer, Vincent <vincent.zimmer@intel.com>; Richardson, Brian <brian.richardson@intel.com>; Andrew Fish <afish@apple.com>; Leif Lindholm <leif.lindholm@linaro.org>; Dong, Eric <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming <liming.gao@intel.com>; Carsey, Jaben <jaben.carsey@intel.com>; Shi, Steven <steven.shi@intel.com>
Subject: RE: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol

Hi Ard,

Yes.  I think it is simpler if an emulator module registers one machine type at a time.  If an emulator module wants to do a single registration and that registration supports multiple machine types, then the emulator implementation is more complex, but it is still feasible.

I think I see where things got confused.  The proposal from Star for a new protocol produced by the DXE Core has some issues.
A refined proposal is shown below.  It also supports the idea that an emulator module can be loaded and unloaded to support testing from environments like the UEFI Shell to soft load an emulator, test some images, and unload it.

Also, by having a protocol produced by the DXE Core, the emulator module can tell if the DXE Core supports emulators or not by checking for the presence of this protocol.  The emulator module can fail gracefully with some DEBUG() message and unload if DXE Core does not support emulators.  This also allows the option to update the DXE Core module to optionally support emulators using a PCD Feature Flag and remove some logic if emulators are not required on a specific platform.

typedef
EFI_STATUS
(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_ADD_EMULATOR) (
  IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL            *This,
  IN EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED  IsImageSupported,
  IN EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE      RegisterImage,
  IN EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE    UnregisterImage
  );

typedef
EFI_STATUS
(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_REMOVE_EMULATOR) (
  IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL            *This,
  IN EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED  IsImageSupported,
  IN EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE      RegisterImage,
  IN EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE    UnregisterImage
  );

typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL {
  EDKII_PECOFF_IMAGE_EMULATOR_ADD_EMULATOR     AddEmulator;
  EDKII_PECOFF_IMAGE_EMULATOR_REMOVE_EMULATOR  RemoveEmulator; } EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL

The IsImageSupported(), RegisterImage(), and UnRegisterImage() are added in a set in a single call to AddEmulator() from the emulator module.  In LoadImage(), if IsImageSupported() returns TRUE, LoadImage() uses the RegisterImage() and UnRegisterImage() APIs that were added with the IsImageSupported() API in the call to AddEmulator().

Thanks,

Mike

> -----Original Message-----
> From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org]
> Sent: Thursday, September 27, 2018 3:59 AM
> To: Zeng, Star <star.zeng@intel.com>
> Cc: Kinney, Michael D <michael.d.kinney@intel.com>; 
> edk2-devel@lists.01.org; Zimmer, Vincent <vincent.zimmer@intel.com>; 
> Richardson, Brian <brian.richardson@intel.com>; Andrew Fish 
> <afish@apple.com>; Leif Lindholm <leif.lindholm@linaro.org>; Dong, 
> Eric <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, 
> Liming <liming.gao@intel.com>; Carsey, Jaben <jaben.carsey@intel.com>; 
> Shi, Steven <steven.shi@intel.com>
> Subject: Re: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image 
> emulator protocol
> 
> On 27 September 2018 at 02:48, Zeng, Star <star.zeng@intel.com> wrote:
> > Yes. This idea also came to my mind last night, then
> no need introduce PeCoffEmulatorDxe and no platform change is needed.
> >
> 
> The only problem with this approach is that we cannot keep track of 
> which emulator returned TRUE for IsSupported(), and so
> RegisterImage()
> and also UnregisterImage() will have to do some internal bookkeeping 
> in the 'manager' protocol implementation that duplicates code in the 
> emulators.
> 
> So could we flesh this out a bit before I dive into the code again and 
> turn everything upside down?
> 
> - the PCI bus driver no longer checks the machine type
> - the BDS no longer checks the machine type for Driver#### images
> - CoreLoadImage() checks IsSupported() to establish whether the 
> emulator manager has an emulator available that is willing to take 
> charge of the image
> - what happens next? CoreLoadImage() calls
> RegisterImage() on what?
> Should IsSupported() return some kind of handle?
> 
> Also, perhaps we should simply stick with one emulator per machine 
> type (which doesn't preclude an implementation from registering itself 
> for multiple ones). That simplifies the IsSupported vs RegisterImage 
> issue above as well (i.e., it is unambiguous *which*
> RegisterImage()
> should be called)
> 
> 
> 
> > -----Original Message-----
> > From: Kinney, Michael D
> > Sent: Thursday, September 27, 2018 1:32 AM
> > To: Ard Biesheuvel <ard.biesheuvel@linaro.org>; Zeng,
> Star <star.zeng@intel.com>; Kinney, Michael D
> <michael.d.kinney@intel.com>
> > Cc: edk2-devel@lists.01.org; Zimmer, Vincent
> <vincent.zimmer@intel.com>; Richardson, Brian
> <brian.richardson@intel.com>; Andrew Fish
> <afish@apple.com>; Leif Lindholm
> <leif.lindholm@linaro.org>; Dong, Eric
> <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>;
> Gao, Liming <liming.gao@intel.com>; Carsey, Jaben
> <jaben.carsey@intel.com>; Shi, Steven
> <steven.shi@intel.com>
> > Subject: RE: [PATCH v3 1/7] MdeModulePkg: introduce
> PE/COFF image emulator protocol
> >
> > Hi Ard,
> >
> > I think this registration protocol would be a new
> protocol in the MdeModulePkg and the protocol would be
> produced by the DXE Core.  The emulation drivers,
> including EBC would consume this protocol to register
> their services.  This removes the need for the DXE Core
> to do a Register Protocol Notify event for the emulator
> protocol.  The DXE Core would build a table of
> registered services, so it can quickly loop through
> them to check if an emulator supports a specific
> PE/COFF image or not.
> >
> > Best regards,
> >
> > Mike
> >
> >> -----Original Message-----
> >> From: Ard Biesheuvel
> [mailto:ard.biesheuvel@linaro.org]
> >> Sent: Wednesday, September 26, 2018 3:14 AM
> >> To: Zeng, Star <star.zeng@intel.com>
> >> Cc: edk2-devel@lists.01.org; Zimmer, Vincent
> >> <vincent.zimmer@intel.com>; Richardson, Brian
> >> <brian.richardson@intel.com>; Kinney, Michael D
> >> <michael.d.kinney@intel.com>; Andrew Fish
> <afish@apple.com>; Leif
> >> Lindholm <leif.lindholm@linaro.org>; Dong, Eric
> <eric.dong@intel.com>;
> >> Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming
> <liming.gao@intel.com>;
> >> Carsey, Jaben <jaben.carsey@intel.com>; Shi, Steven
> >> <steven.shi@intel.com>
> >> Subject: Re: [PATCH v3 1/7] MdeModulePkg: introduce
> PE/COFF image
> >> emulator protocol
> >>
> >> On Wed, 26 Sep 2018 at 12:07, Zeng, Star
> <star.zeng@intel.com> wrote:
> >> >
> >> > A little late feedback. Just an idea.
> >> >
> >> > Do you think whether it can make the consumer
> simpler
> >> like below or not?
> >> >
> >> > Add a new API RegisterInterfaces in the protocol
> and
> >> add a wrapper driver PeCoffEmulatorDxe.
> >> > The emulators can call RegisterInterfaces, and
> >> consumer will call other APIs simply,
> PeCoffEmulatorDxe will be a
> >> wrapper as manager.
> >> >
> >> > typedef
> >> > EFI_STATUS
> >> > (EFIAPI
> >> *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES) (
> >> >   IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> *This,
> >> >   IN
> EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
> >> IsImageSupported,
> >> >   IN EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
> >> RegisterImage,
> >> >   IN EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
> >> UnregisterImage
> >> >   );
> >> >
> >> > typedef struct
> _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL {
> >> >   EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES
> >> RegisterInterfaces;
> >> >   EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
> >> IsImageSupported;
> >> >   EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
> >> RegisterImage;
> >> >   EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
> >> UnregisterImage;
> >> > } EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> >> >
> >>
> >> Hi Star,
> >>
> >> Thanks for taking a look.
> >>
> >> I tried to avoid introducing new drivers or library
> classes because it
> >> will break existing platforms that incorporate
> EbcDxe.
> >> Do you think
> >> this is not an issue?
> >>
> >> > -----Original Message-----
> >> > From: Ard Biesheuvel
> >> [mailto:ard.biesheuvel@linaro.org]
> >> > Sent: Friday, September 21, 2018 7:02 AM
> >> > To: edk2-devel@lists.01.org
> >> > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>;
> >> Zimmer, Vincent <vincent.zimmer@intel.com>;
> Richardson, Brian
> >> <brian.richardson@intel.com>; Kinney, Michael D
> >> <michael.d.kinney@intel.com>; Andrew Fish
> <afish@apple.com>; Leif
> >> Lindholm <leif.lindholm@linaro.org>; Zeng, Star
> <star.zeng@intel.com>;
> >> Dong, Eric <eric.dong@intel.com>; Ni, Ruiyu
> <ruiyu.ni@intel.com>; Gao,
> >> Liming <liming.gao@intel.com>; Carsey, Jaben
> <jaben.carsey@intel.com>;
> >> Shi, Steven <steven.shi@intel.com>
> >> > Subject: [PATCH v3 1/7] MdeModulePkg: introduce
> >> PE/COFF image emulator protocol
> >> >
> >> > Introduce a protocol that can be invoked by the
> image
> >> loading services to execute foreign architecture
> PE/COFF images via an
> >> emulator.
> >> >
> >> > Contributed-under: TianoCore Contribution
> Agreement
> >> 1.1
> >> > Signed-off-by: Ard Biesheuvel
> >> <ard.biesheuvel@linaro.org>
> >> > ---
> >> >
> MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h |
> >> 102 ++++++++++++++++++++
> >> >  MdeModulePkg/MdeModulePkg.dec
> |
> >> 4 +
> >> >  2 files changed, 106 insertions(+)
> >> >
> >> > diff --git
> >>
> a/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> >>
> b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> >> > new file mode 100644
> >> > index 000000000000..27bad556209c
> >> > --- /dev/null
> >> > +++
> >>
> b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> >> > @@ -0,0 +1,102 @@
> >> > +/** @file
> >> > +  Copyright (c) 2018, Linaro, Ltd. All rights
> >> reserved.<BR>
> >> > +
> >> > +  This program and the accompanying materials are
> >> licensed and made
> >> > + available  under the terms and conditions of the
> BSD
> >> License which
> >> > + accompanies this  distribution.  The full text
> of
> >> the license may be
> >> > + found at  http://opensource.org/licenses/bsd-
> >> license.php
> >> > +
> >> > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD
> LICENSE ON
> >> AN "AS IS" BASIS,
> >> > + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY
> KIND,
> >> EITHER EXPRESS OR IMPLIED.
> >> > +
> >> > +**/
> >> > +
> >> > +#ifndef PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
> >> > +#define PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
> >> > +
> >> > +#define EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID
> \
> >> > +  { 0x96F46153, 0x97A7, 0x4793, { 0xAC, 0xC1,
> 0xFA,
> >> 0x19, 0xBF, 0x78,
> >> > +0xEA, 0x97 } }
> >> > +
> >> > +typedef struct
> _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> >> > +EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> >> > +
> >> > +/**
> >> > +  Check whether the emulator supports executing a
> >> certain PE/COFF image
> >> > +
> >> > +  @param[in] This         This pointer for
> >> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> >> > +                          structure  @param[in]
> MachineType  The
> >> > + machine type for which
> >> the image was built
> >> > +  @param[in] ImageType    Whether the image is an
> >> application, a boot time
> >> > +                          driver or a runtime
> driver.
> >> > +  @param[in] DevicePath   Path to device where
> the
> >> image originated
> >> > +                          (e.g., a PCI option
> ROM)
> >> > +
> >> > +  @retval TRUE            The image is supported
> by
> >> the emulator
> >> > +  @retval FALSE           The image is not
> supported
> >> by the emulator.
> >> > +**/
> >> > +typedef
> >> > +BOOLEAN
> >> > +(EFIAPI
> >> *EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED) (
> >> > +  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> *This,
> >> > +  IN  UINT16
> >> MachineType,
> >> > +  IN  UINT16
> >> ImageType,
> >> > +  IN  EFI_DEVICE_PATH_PROTOCOL
> >> *DevicePath   OPTIONAL
> >> > +  );
> >> > +
> >> > +/**
> >> > +  Register a supported PE/COFF image with the
> >> emulator. After this call
> >> > +  completes successfully, the PE/COFF image may
> be
> >> started as usual,
> >> > +and
> >> > +  it is the responsibility of the emulator
> >> implementation that any
> >> > +branch
> >> > +  into the code section of the image (including
> >> returns from functions
> >> > +called
> >> > +  from the foreign code) is executed as if it
> were
> >> running on the
> >> > +machine
> >> > +  type it was built for.
> >> > +
> >> > +  @param[in]      This          This pointer for
> >> > +
> >> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL structure
> >> > +  @param[in]      ImageBase     The base address
> in
> >> memory of the PE/COFF image
> >> > +  @param[in]      ImageSize     The size in
> memory of
> >> the PE/COFF image
> >> > +  @param[in,out]  EntryPoint    The entry point
> of
> >> the PE/COFF image. Passed by
> >> > +                                reference so that
> the
> >> emulator may modify it.
> >> > +
> >> > +  @retval EFI_SUCCESS           The image was
> >> registered with the emulator and
> >> > +                                can be started as
> >> usual.
> >> > +  @retval other                 The image could
> not
> >> be registered.
> >> > +
> >> > +  If the PE/COFF machine type or image type are
> not
> >> supported by the
> >> > +emulator,
> >> > +  then ASSERT().
> >> > +**/
> >> > +typedef
> >> > +EFI_STATUS
> >> > +(EFIAPI
> *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE)
> >> (
> >> > +  IN      EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> >> *This,
> >> > +  IN      EFI_PHYSICAL_ADDRESS
> >> ImageBase,
> >> > +  IN      UINT64
> >> ImageSize,
> >> > +  IN  OUT EFI_IMAGE_ENTRY_POINT
> >> *EntryPoint
> >> > +  );
> >> > +
> >> > +/**
> >> > +  Unregister a PE/COFF image that has been
> registered
> >> with the emulator.
> >> > +  This should be done before the image is
> unloaded
> >> from memory.
> >> > +
> >> > +  @param[in] This         This pointer for
> >> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> >> > +                          structure
> >> > +  @param[in] ImageBase    The base address in
> memory
> >> of the PE/COFF image
> >> > +
> >> > +  @retval EFI_SUCCESS     The image was
> unregistered
> >> with the emulator.
> >> > +  @retval other           Image could not be
> >> unloaded.
> >> > +**/
> >> > +typedef
> >> > +EFI_STATUS
> >> > +(EFIAPI
> >> *EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE) (
> >> > +  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> *This,
> >> > +  IN  EFI_PHYSICAL_ADDRESS
> >> ImageBase
> >> > +  );
> >> > +
> >> > +typedef struct
> _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> >> {
> >> > +  EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
> >> IsImageSupported;
> >> > +  EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
> >> RegisterImage;
> >> > +  EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
> >> UnregisterImage;
> >> > +} EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> >> > +
> >> > +extern EFI_GUID
> >> gEdkiiPeCoffImageEmulatorProtocolGuid;
> >> > +
> >> > +#endif
> >> > diff --git a/MdeModulePkg/MdeModulePkg.dec
> >> b/MdeModulePkg/MdeModulePkg.dec index
> >> 6a6d9660edc2..be307329f901 100644
> >> > --- a/MdeModulePkg/MdeModulePkg.dec
> >> > +++ b/MdeModulePkg/MdeModulePkg.dec
> >> > @@ -617,6 +617,10 @@
> >> >
> >> >    ## Include/Protocol/AtaAtapiPolicy.h
> >> >    gEdkiiAtaAtapiPolicyProtocolGuid = {
> 0xe59cd769,
> >> 0x5083, 0x4f26,{ 0x90, 0x94, 0x6c, 0x91, 0x9f, 0x91,
> 0x6c, 0x4e } }
> >> > +
> >> > +  ## Include/Protocol/PeCoffImageEmulator.h
> >> > +  gEdkiiPeCoffImageEmulatorProtocolGuid = {
> >> 0x96f46153, 0x97a7, 0x4793,
> >> > + { 0xac, 0xc1, 0xfa, 0x19, 0xbf, 0x78, 0xea, 0x97
> } }
> >> > +
> >> >  #
> >> >  # [Error.gEfiMdeModulePkgTokenSpaceGuid]
> >> >  #   0x80000001 | Invalid value provided.
> >> > --
> >> > 2.17.1
> >> >

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol
  2018-09-28  3:05               ` Zeng, Star
@ 2018-09-28  3:08                 ` Zeng, Star
  2018-09-28  6:34                   ` Ni, Ruiyu
  0 siblings, 1 reply; 22+ messages in thread
From: Zeng, Star @ 2018-09-28  3:08 UTC (permalink / raw)
  To: Kinney, Michael D, Ard Biesheuvel
  Cc: edk2-devel@lists.01.org, Zimmer, Vincent, Richardson, Brian,
	Andrew Fish, Leif Lindholm, Dong, Eric, Ni, Ruiyu, Gao, Liming,
	Carsey, Jaben, Shi, Steven, Zeng, Star

Then AddEmulator and RemoveEmulator will be like below.

typedef
EFI_STATUS
(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_ADD_EMULATOR) (
  IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL            *This,
  IN PECOFF_IMAGE_EMULATOR                                                  *Emulator
  );

typedef
EFI_STATUS
(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_REMOVE_EMULATOR) (
  IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL            *This,
  IN PECOFF_IMAGE_EMULATOR                                                  *Emulator
  );

Thanks,
Star
-----Original Message-----
From: Zeng, Star 
Sent: Friday, September 28, 2018 11:06 AM
To: Kinney, Michael D <michael.d.kinney@intel.com>; Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: edk2-devel@lists.01.org; Zimmer, Vincent <vincent.zimmer@intel.com>; Richardson, Brian <brian.richardson@intel.com>; Andrew Fish <afish@apple.com>; Leif Lindholm <leif.lindholm@linaro.org>; Dong, Eric <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming <liming.gao@intel.com>; Carsey, Jaben <jaben.carsey@intel.com>; Shi, Steven <steven.shi@intel.com>; Zeng, Star <star.zeng@intel.com>
Subject: RE: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol

Mike,

Good idea.
You prefer to introduce a new feature PCD for this with default value = TRUE?

Could we group the emulator APIs to one structure like below? And add a Version field for potential further extension?

typedef struct {
  UINTN                                           Version;
  EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED  IsImageSupported;
  EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE      RegisterImage;
  EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE    UnregisterImage;
} PECOFF_IMAGE_EMULATOR;


Thanks,
Star
-----Original Message-----
From: Kinney, Michael D
Sent: Thursday, September 27, 2018 11:37 PM
To: Ard Biesheuvel <ard.biesheuvel@linaro.org>; Zeng, Star <star.zeng@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>
Cc: edk2-devel@lists.01.org; Zimmer, Vincent <vincent.zimmer@intel.com>; Richardson, Brian <brian.richardson@intel.com>; Andrew Fish <afish@apple.com>; Leif Lindholm <leif.lindholm@linaro.org>; Dong, Eric <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming <liming.gao@intel.com>; Carsey, Jaben <jaben.carsey@intel.com>; Shi, Steven <steven.shi@intel.com>
Subject: RE: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol

Hi Ard,

Yes.  I think it is simpler if an emulator module registers one machine type at a time.  If an emulator module wants to do a single registration and that registration supports multiple machine types, then the emulator implementation is more complex, but it is still feasible.

I think I see where things got confused.  The proposal from Star for a new protocol produced by the DXE Core has some issues.
A refined proposal is shown below.  It also supports the idea that an emulator module can be loaded and unloaded to support testing from environments like the UEFI Shell to soft load an emulator, test some images, and unload it.

Also, by having a protocol produced by the DXE Core, the emulator module can tell if the DXE Core supports emulators or not by checking for the presence of this protocol.  The emulator module can fail gracefully with some DEBUG() message and unload if DXE Core does not support emulators.  This also allows the option to update the DXE Core module to optionally support emulators using a PCD Feature Flag and remove some logic if emulators are not required on a specific platform.

typedef
EFI_STATUS
(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_ADD_EMULATOR) (
  IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL            *This,
  IN EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED  IsImageSupported,
  IN EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE      RegisterImage,
  IN EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE    UnregisterImage
  );

typedef
EFI_STATUS
(EFIAPI *EDKII_PECOFF_IMAGE_EMULATOR_REMOVE_EMULATOR) (
  IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL            *This,
  IN EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED  IsImageSupported,
  IN EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE      RegisterImage,
  IN EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE    UnregisterImage
  );

typedef struct _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL {
  EDKII_PECOFF_IMAGE_EMULATOR_ADD_EMULATOR     AddEmulator;
  EDKII_PECOFF_IMAGE_EMULATOR_REMOVE_EMULATOR  RemoveEmulator; } EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL

The IsImageSupported(), RegisterImage(), and UnRegisterImage() are added in a set in a single call to AddEmulator() from the emulator module.  In LoadImage(), if IsImageSupported() returns TRUE, LoadImage() uses the RegisterImage() and UnRegisterImage() APIs that were added with the IsImageSupported() API in the call to AddEmulator().

Thanks,

Mike

> -----Original Message-----
> From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org]
> Sent: Thursday, September 27, 2018 3:59 AM
> To: Zeng, Star <star.zeng@intel.com>
> Cc: Kinney, Michael D <michael.d.kinney@intel.com>; 
> edk2-devel@lists.01.org; Zimmer, Vincent <vincent.zimmer@intel.com>; 
> Richardson, Brian <brian.richardson@intel.com>; Andrew Fish 
> <afish@apple.com>; Leif Lindholm <leif.lindholm@linaro.org>; Dong, 
> Eric <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, 
> Liming <liming.gao@intel.com>; Carsey, Jaben <jaben.carsey@intel.com>; 
> Shi, Steven <steven.shi@intel.com>
> Subject: Re: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image 
> emulator protocol
> 
> On 27 September 2018 at 02:48, Zeng, Star <star.zeng@intel.com> wrote:
> > Yes. This idea also came to my mind last night, then
> no need introduce PeCoffEmulatorDxe and no platform change is needed.
> >
> 
> The only problem with this approach is that we cannot keep track of 
> which emulator returned TRUE for IsSupported(), and so
> RegisterImage()
> and also UnregisterImage() will have to do some internal bookkeeping 
> in the 'manager' protocol implementation that duplicates code in the 
> emulators.
> 
> So could we flesh this out a bit before I dive into the code again and 
> turn everything upside down?
> 
> - the PCI bus driver no longer checks the machine type
> - the BDS no longer checks the machine type for Driver#### images
> - CoreLoadImage() checks IsSupported() to establish whether the 
> emulator manager has an emulator available that is willing to take 
> charge of the image
> - what happens next? CoreLoadImage() calls
> RegisterImage() on what?
> Should IsSupported() return some kind of handle?
> 
> Also, perhaps we should simply stick with one emulator per machine 
> type (which doesn't preclude an implementation from registering itself 
> for multiple ones). That simplifies the IsSupported vs RegisterImage 
> issue above as well (i.e., it is unambiguous *which*
> RegisterImage()
> should be called)
> 
> 
> 
> > -----Original Message-----
> > From: Kinney, Michael D
> > Sent: Thursday, September 27, 2018 1:32 AM
> > To: Ard Biesheuvel <ard.biesheuvel@linaro.org>; Zeng,
> Star <star.zeng@intel.com>; Kinney, Michael D 
> <michael.d.kinney@intel.com>
> > Cc: edk2-devel@lists.01.org; Zimmer, Vincent
> <vincent.zimmer@intel.com>; Richardson, Brian 
> <brian.richardson@intel.com>; Andrew Fish <afish@apple.com>; Leif 
> Lindholm <leif.lindholm@linaro.org>; Dong, Eric <eric.dong@intel.com>; 
> Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming <liming.gao@intel.com>; 
> Carsey, Jaben <jaben.carsey@intel.com>; Shi, Steven 
> <steven.shi@intel.com>
> > Subject: RE: [PATCH v3 1/7] MdeModulePkg: introduce
> PE/COFF image emulator protocol
> >
> > Hi Ard,
> >
> > I think this registration protocol would be a new
> protocol in the MdeModulePkg and the protocol would be produced by the 
> DXE Core.  The emulation drivers, including EBC would consume this 
> protocol to register their services.  This removes the need for the 
> DXE Core to do a Register Protocol Notify event for the emulator 
> protocol.  The DXE Core would build a table of registered services, so 
> it can quickly loop through them to check if an emulator supports a 
> specific PE/COFF image or not.
> >
> > Best regards,
> >
> > Mike
> >
> >> -----Original Message-----
> >> From: Ard Biesheuvel
> [mailto:ard.biesheuvel@linaro.org]
> >> Sent: Wednesday, September 26, 2018 3:14 AM
> >> To: Zeng, Star <star.zeng@intel.com>
> >> Cc: edk2-devel@lists.01.org; Zimmer, Vincent 
> >> <vincent.zimmer@intel.com>; Richardson, Brian 
> >> <brian.richardson@intel.com>; Kinney, Michael D 
> >> <michael.d.kinney@intel.com>; Andrew Fish
> <afish@apple.com>; Leif
> >> Lindholm <leif.lindholm@linaro.org>; Dong, Eric
> <eric.dong@intel.com>;
> >> Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming
> <liming.gao@intel.com>;
> >> Carsey, Jaben <jaben.carsey@intel.com>; Shi, Steven 
> >> <steven.shi@intel.com>
> >> Subject: Re: [PATCH v3 1/7] MdeModulePkg: introduce
> PE/COFF image
> >> emulator protocol
> >>
> >> On Wed, 26 Sep 2018 at 12:07, Zeng, Star
> <star.zeng@intel.com> wrote:
> >> >
> >> > A little late feedback. Just an idea.
> >> >
> >> > Do you think whether it can make the consumer
> simpler
> >> like below or not?
> >> >
> >> > Add a new API RegisterInterfaces in the protocol
> and
> >> add a wrapper driver PeCoffEmulatorDxe.
> >> > The emulators can call RegisterInterfaces, and
> >> consumer will call other APIs simply,
> PeCoffEmulatorDxe will be a
> >> wrapper as manager.
> >> >
> >> > typedef
> >> > EFI_STATUS
> >> > (EFIAPI
> >> *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES) (
> >> >   IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> *This,
> >> >   IN
> EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
> >> IsImageSupported,
> >> >   IN EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
> >> RegisterImage,
> >> >   IN EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
> >> UnregisterImage
> >> >   );
> >> >
> >> > typedef struct
> _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL {
> >> >   EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_INTERFACES
> >> RegisterInterfaces;
> >> >   EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
> >> IsImageSupported;
> >> >   EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
> >> RegisterImage;
> >> >   EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
> >> UnregisterImage;
> >> > } EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> >> >
> >>
> >> Hi Star,
> >>
> >> Thanks for taking a look.
> >>
> >> I tried to avoid introducing new drivers or library
> classes because it
> >> will break existing platforms that incorporate
> EbcDxe.
> >> Do you think
> >> this is not an issue?
> >>
> >> > -----Original Message-----
> >> > From: Ard Biesheuvel
> >> [mailto:ard.biesheuvel@linaro.org]
> >> > Sent: Friday, September 21, 2018 7:02 AM
> >> > To: edk2-devel@lists.01.org
> >> > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>;
> >> Zimmer, Vincent <vincent.zimmer@intel.com>;
> Richardson, Brian
> >> <brian.richardson@intel.com>; Kinney, Michael D 
> >> <michael.d.kinney@intel.com>; Andrew Fish
> <afish@apple.com>; Leif
> >> Lindholm <leif.lindholm@linaro.org>; Zeng, Star
> <star.zeng@intel.com>;
> >> Dong, Eric <eric.dong@intel.com>; Ni, Ruiyu
> <ruiyu.ni@intel.com>; Gao,
> >> Liming <liming.gao@intel.com>; Carsey, Jaben
> <jaben.carsey@intel.com>;
> >> Shi, Steven <steven.shi@intel.com>
> >> > Subject: [PATCH v3 1/7] MdeModulePkg: introduce
> >> PE/COFF image emulator protocol
> >> >
> >> > Introduce a protocol that can be invoked by the
> image
> >> loading services to execute foreign architecture
> PE/COFF images via an
> >> emulator.
> >> >
> >> > Contributed-under: TianoCore Contribution
> Agreement
> >> 1.1
> >> > Signed-off-by: Ard Biesheuvel
> >> <ard.biesheuvel@linaro.org>
> >> > ---
> >> >
> MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h |
> >> 102 ++++++++++++++++++++
> >> >  MdeModulePkg/MdeModulePkg.dec
> |
> >> 4 +
> >> >  2 files changed, 106 insertions(+)
> >> >
> >> > diff --git
> >>
> a/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> >>
> b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> >> > new file mode 100644
> >> > index 000000000000..27bad556209c
> >> > --- /dev/null
> >> > +++
> >>
> b/MdeModulePkg/Include/Protocol/PeCoffImageEmulator.h
> >> > @@ -0,0 +1,102 @@
> >> > +/** @file
> >> > +  Copyright (c) 2018, Linaro, Ltd. All rights
> >> reserved.<BR>
> >> > +
> >> > +  This program and the accompanying materials are
> >> licensed and made
> >> > + available  under the terms and conditions of the
> BSD
> >> License which
> >> > + accompanies this  distribution.  The full text
> of
> >> the license may be
> >> > + found at  http://opensource.org/licenses/bsd-
> >> license.php
> >> > +
> >> > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD
> LICENSE ON
> >> AN "AS IS" BASIS,
> >> > + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY
> KIND,
> >> EITHER EXPRESS OR IMPLIED.
> >> > +
> >> > +**/
> >> > +
> >> > +#ifndef PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
> >> > +#define PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID_H
> >> > +
> >> > +#define EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL_GUID
> \
> >> > +  { 0x96F46153, 0x97A7, 0x4793, { 0xAC, 0xC1,
> 0xFA,
> >> 0x19, 0xBF, 0x78,
> >> > +0xEA, 0x97 } }
> >> > +
> >> > +typedef struct
> _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> >> > +EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> >> > +
> >> > +/**
> >> > +  Check whether the emulator supports executing a
> >> certain PE/COFF image
> >> > +
> >> > +  @param[in] This         This pointer for
> >> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> >> > +                          structure  @param[in]
> MachineType  The
> >> > + machine type for which
> >> the image was built
> >> > +  @param[in] ImageType    Whether the image is an
> >> application, a boot time
> >> > +                          driver or a runtime
> driver.
> >> > +  @param[in] DevicePath   Path to device where
> the
> >> image originated
> >> > +                          (e.g., a PCI option
> ROM)
> >> > +
> >> > +  @retval TRUE            The image is supported
> by
> >> the emulator
> >> > +  @retval FALSE           The image is not
> supported
> >> by the emulator.
> >> > +**/
> >> > +typedef
> >> > +BOOLEAN
> >> > +(EFIAPI
> >> *EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED) (
> >> > +  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> *This,
> >> > +  IN  UINT16
> >> MachineType,
> >> > +  IN  UINT16
> >> ImageType,
> >> > +  IN  EFI_DEVICE_PATH_PROTOCOL
> >> *DevicePath   OPTIONAL
> >> > +  );
> >> > +
> >> > +/**
> >> > +  Register a supported PE/COFF image with the
> >> emulator. After this call
> >> > +  completes successfully, the PE/COFF image may
> be
> >> started as usual,
> >> > +and
> >> > +  it is the responsibility of the emulator
> >> implementation that any
> >> > +branch
> >> > +  into the code section of the image (including
> >> returns from functions
> >> > +called
> >> > +  from the foreign code) is executed as if it
> were
> >> running on the
> >> > +machine
> >> > +  type it was built for.
> >> > +
> >> > +  @param[in]      This          This pointer for
> >> > +
> >> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL structure
> >> > +  @param[in]      ImageBase     The base address
> in
> >> memory of the PE/COFF image
> >> > +  @param[in]      ImageSize     The size in
> memory of
> >> the PE/COFF image
> >> > +  @param[in,out]  EntryPoint    The entry point
> of
> >> the PE/COFF image. Passed by
> >> > +                                reference so that
> the
> >> emulator may modify it.
> >> > +
> >> > +  @retval EFI_SUCCESS           The image was
> >> registered with the emulator and
> >> > +                                can be started as
> >> usual.
> >> > +  @retval other                 The image could
> not
> >> be registered.
> >> > +
> >> > +  If the PE/COFF machine type or image type are
> not
> >> supported by the
> >> > +emulator,
> >> > +  then ASSERT().
> >> > +**/
> >> > +typedef
> >> > +EFI_STATUS
> >> > +(EFIAPI
> *EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE)
> >> (
> >> > +  IN      EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> >> *This,
> >> > +  IN      EFI_PHYSICAL_ADDRESS
> >> ImageBase,
> >> > +  IN      UINT64
> >> ImageSize,
> >> > +  IN  OUT EFI_IMAGE_ENTRY_POINT
> >> *EntryPoint
> >> > +  );
> >> > +
> >> > +/**
> >> > +  Unregister a PE/COFF image that has been
> registered
> >> with the emulator.
> >> > +  This should be done before the image is
> unloaded
> >> from memory.
> >> > +
> >> > +  @param[in] This         This pointer for
> >> EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> >> > +                          structure
> >> > +  @param[in] ImageBase    The base address in
> memory
> >> of the PE/COFF image
> >> > +
> >> > +  @retval EFI_SUCCESS     The image was
> unregistered
> >> with the emulator.
> >> > +  @retval other           Image could not be
> >> unloaded.
> >> > +**/
> >> > +typedef
> >> > +EFI_STATUS
> >> > +(EFIAPI
> >> *EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE) (
> >> > +  IN  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> *This,
> >> > +  IN  EFI_PHYSICAL_ADDRESS
> >> ImageBase
> >> > +  );
> >> > +
> >> > +typedef struct
> _EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL
> >> {
> >> > +  EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED
> >> IsImageSupported;
> >> > +  EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE
> >> RegisterImage;
> >> > +  EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE
> >> UnregisterImage;
> >> > +} EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL;
> >> > +
> >> > +extern EFI_GUID
> >> gEdkiiPeCoffImageEmulatorProtocolGuid;
> >> > +
> >> > +#endif
> >> > diff --git a/MdeModulePkg/MdeModulePkg.dec
> >> b/MdeModulePkg/MdeModulePkg.dec index
> >> 6a6d9660edc2..be307329f901 100644
> >> > --- a/MdeModulePkg/MdeModulePkg.dec
> >> > +++ b/MdeModulePkg/MdeModulePkg.dec
> >> > @@ -617,6 +617,10 @@
> >> >
> >> >    ## Include/Protocol/AtaAtapiPolicy.h
> >> >    gEdkiiAtaAtapiPolicyProtocolGuid = {
> 0xe59cd769,
> >> 0x5083, 0x4f26,{ 0x90, 0x94, 0x6c, 0x91, 0x9f, 0x91,
> 0x6c, 0x4e } }
> >> > +
> >> > +  ## Include/Protocol/PeCoffImageEmulator.h
> >> > +  gEdkiiPeCoffImageEmulatorProtocolGuid = {
> >> 0x96f46153, 0x97a7, 0x4793,
> >> > + { 0xac, 0xc1, 0xfa, 0x19, 0xbf, 0x78, 0xea, 0x97
> } }
> >> > +
> >> >  #
> >> >  # [Error.gEfiMdeModulePkgTokenSpaceGuid]
> >> >  #   0x80000001 | Invalid value provided.
> >> > --
> >> > 2.17.1
> >> >

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol
  2018-09-28  3:08                 ` Zeng, Star
@ 2018-09-28  6:34                   ` Ni, Ruiyu
  2018-09-28  7:02                     ` Zeng, Star
  0 siblings, 1 reply; 22+ messages in thread
From: Ni, Ruiyu @ 2018-09-28  6:34 UTC (permalink / raw)
  To: Zeng, Star, Kinney, Michael D, Ard Biesheuvel
  Cc: edk2-devel@lists.01.org, Zimmer, Vincent, Richardson, Brian,
	Andrew Fish, Leif Lindholm, Dong, Eric, Gao, Liming,
	Carsey, Jaben, Shi, Steven

On 9/28/2018 11:08 AM, Zeng, Star wrote:
> Good idea.
> You prefer to introduce a new feature PCD for this with default value = TRUE?
> 
> Could we group the emulator APIs to one structure like below? And add a Version field for potential further extension?
> 
> typedef struct {
>    UINTN                                           Version;
>    EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED  IsImageSupported;
>    EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE      RegisterImage;
>    EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE    UnregisterImage;
> } PECOFF_IMAGE_EMULATOR;

Star,
What's the reason for the "Version" field? Can you explain the usage case?

And why do we need the PCD to control?

-- 
Thanks,
Ray


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol
  2018-09-28  6:34                   ` Ni, Ruiyu
@ 2018-09-28  7:02                     ` Zeng, Star
  0 siblings, 0 replies; 22+ messages in thread
From: Zeng, Star @ 2018-09-28  7:02 UTC (permalink / raw)
  To: Ni, Ruiyu, Kinney, Michael D, Ard Biesheuvel
  Cc: edk2-devel@lists.01.org, Zimmer, Vincent, Richardson, Brian,
	Andrew Fish, Leif Lindholm, Dong, Eric, Gao, Liming,
	Carsey, Jaben, Shi, Steven, Zeng, Star

Good questions.

1. The emulator needs three interfaces currently. The Version I mentioned is for the case that the emulator may need extra interface later in future for some new consideration, then we can just update the Version value and extend the structure with new interface, and no new protocol2 needs to be introduced.

2. I just tried to confirm Mike's idea as Mike replied to Ard with " This also allows the option to update the DXE Core module to optionally support emulators using a PCD Feature Flag and remove some logic if emulators are not required on a specific platform. ".


Thanks,
Star
-----Original Message-----
From: Ni, Ruiyu 
Sent: Friday, September 28, 2018 2:34 PM
To: Zeng, Star <star.zeng@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>; Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: edk2-devel@lists.01.org; Zimmer, Vincent <vincent.zimmer@intel.com>; Richardson, Brian <brian.richardson@intel.com>; Andrew Fish <afish@apple.com>; Leif Lindholm <leif.lindholm@linaro.org>; Dong, Eric <eric.dong@intel.com>; Gao, Liming <liming.gao@intel.com>; Carsey, Jaben <jaben.carsey@intel.com>; Shi, Steven <steven.shi@intel.com>
Subject: Re: [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol

On 9/28/2018 11:08 AM, Zeng, Star wrote:
> Good idea.
> You prefer to introduce a new feature PCD for this with default value = TRUE?
> 
> Could we group the emulator APIs to one structure like below? And add a Version field for potential further extension?
> 
> typedef struct {
>    UINTN                                           Version;
>    EDKII_PECOFF_IMAGE_EMULATOR_IS_IMAGE_SUPPORTED  IsImageSupported;
>    EDKII_PECOFF_IMAGE_EMULATOR_REGISTER_IMAGE      RegisterImage;
>    EDKII_PECOFF_IMAGE_EMULATOR_UNREGISTER_IMAGE    UnregisterImage;
> } PECOFF_IMAGE_EMULATOR;

Star,
What's the reason for the "Version" field? Can you explain the usage case?

And why do we need the PCD to control?

-- 
Thanks,
Ray

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v3 3/7] MdeModulePkg/PciBusDxe: invoke PE/COFF emulator for foreign option ROMs
  2018-09-26 18:26   ` Kinney, Michael D
@ 2018-12-27 10:13     ` Ard Biesheuvel
  0 siblings, 0 replies; 22+ messages in thread
From: Ard Biesheuvel @ 2018-12-27 10:13 UTC (permalink / raw)
  To: Kinney, Michael D
  Cc: edk2-devel@lists.01.org, Zimmer, Vincent, Richardson, Brian,
	Andrew Fish, Leif Lindholm, Zeng, Star, Dong, Eric, Ni, Ruiyu,
	Gao, Liming, Carsey, Jaben, Shi, Steven

(digging up this old thread)

On Wed, 26 Sep 2018 at 20:26, Kinney, Michael D
<michael.d.kinney@intel.com> wrote:
>
> Hi Ard,
>
> I am wondering if we can simplify the PciBusDxe driver and
> remove the image type supported check and instead depend on
> LoadImage().  LoadImage() will return EFI_UNSUPPORTED if the
> image type is not supported.
>

I don't think so. A failure of gBS->LoadImage() is treated in a special way:

    if (EFI_ERROR (Status)) {
      //
      // Record the Option ROM Image device path when LoadImage fails.
      // PciOverride.GetDriver() will try to look for the Image Handle
using the device path later.
      //
      AddDriver (PciDevice, NULL, PciOptionRomImageDevicePath);

and other drivers in the same ROM image are disregarded. So it is
probably better to leave this logic alone, given that the override
implementation could be platform specific, and so we have no idea what
it might do.

>
> > -----Original Message-----
> > From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org]
> > Sent: Thursday, September 20, 2018 4:02 PM
> > To: edk2-devel@lists.01.org
> > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>; Zimmer,
> > Vincent <vincent.zimmer@intel.com>; Richardson, Brian
> > <brian.richardson@intel.com>; Kinney, Michael D
> > <michael.d.kinney@intel.com>; Andrew Fish
> > <afish@apple.com>; Leif Lindholm
> > <leif.lindholm@linaro.org>; Zeng, Star
> > <star.zeng@intel.com>; Dong, Eric
> > <eric.dong@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>;
> > Gao, Liming <liming.gao@intel.com>; Carsey, Jaben
> > <jaben.carsey@intel.com>; Shi, Steven
> > <steven.shi@intel.com>
> > Subject: [PATCH v3 3/7] MdeModulePkg/PciBusDxe: invoke
> > PE/COFF emulator for foreign option ROMs
> >
> > When enumerating option ROM images, take into account
> > whether an emulator
> > exists that would allow dispatch of PE/COFF images
> > built for foreign
> > architectures.
> >
> > Contributed-under: TianoCore Contribution Agreement 1.1
> > Signed-off-by: Ard Biesheuvel
> > <ard.biesheuvel@linaro.org>
> > ---
> >  MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h              |
> > 1 +
> >  MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf         |
> > 1 +
> >  MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c |
> > 53 +++++++++++++++++++-
> >  3 files changed, 54 insertions(+), 1 deletion(-)
> >
> > diff --git a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h
> > b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h
> > index 55eb3a5a8070..dc57d4876c0f 100644
> > --- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h
> > +++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h
> > @@ -33,6 +33,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS
> > OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >  #include <Protocol/PciOverride.h>
> >  #include <Protocol/PciEnumerationComplete.h>
> >  #include <Protocol/IoMmu.h>
> > +#include <Protocol/PeCoffImageEmulator.h>
> >
> >  #include <Library/DebugLib.h>
> >  #include <Library/UefiDriverEntryPoint.h>
> > diff --git
> > a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
> > b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
> > index a21dd2b5edf4..c8b861093292 100644
> > --- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
> > +++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
> > @@ -96,6 +96,7 @@
> >    gEfiIncompatiblePciDeviceSupportProtocolGuid    ##
> > SOMETIMES_CONSUMES
> >    gEfiLoadFile2ProtocolGuid                       ##
> > SOMETIMES_PRODUCES
> >    gEdkiiIoMmuProtocolGuid                         ##
> > SOMETIMES_CONSUMES
> > +  gEdkiiPeCoffImageEmulatorProtocolGuid           ##
> > SOMETIMES_CONSUMES
> >    gEfiLoadedImageDevicePathProtocolGuid           ##
> > CONSUMES
> >
> >  [FeaturePcd]
> > diff --git
> > a/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
> > b/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
> > index c2be85a906af..085bd5d571bd 100644
> > ---
> > a/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
> > +++
> > b/MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
> > @@ -651,6 +651,55 @@ RomDecode (
> >    }
> >  }
> >
> > +STATIC
> > +BOOLEAN
> > +IsImageTypeSupported (
> > +  IN  UINT16                    MachineType,
> > +  IN  UINT16                    SubSystem,
> > +  IN  EFI_DEVICE_PATH_PROTOCOL  *DevicePath
> > +  )
> > +{
> > +  EFI_STATUS                            Status;
> > +  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL  *Emu;
> > +  UINTN                                 HandleCount;
> > +  EFI_HANDLE                            *HandleBuffer;
> > +  BOOLEAN                               ReturnValue;
> > +  UINTN                                 Index;
> > +
> > +  if (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (MachineType))
> > {
> > +    return TRUE;
> > +  }
> > +
> > +  Status = gBS->LocateHandleBuffer (
> > +                  ByProtocol,
> > +
> > &gEdkiiPeCoffImageEmulatorProtocolGuid,
> > +                  NULL,
> > +                  &HandleCount,
> > +                  &HandleBuffer
> > +                  );
> > +  if (EFI_ERROR (Status)) {
> > +    return FALSE;
> > +  }
> > +
> > +  ReturnValue = FALSE;
> > +  for (Index = 0; Index < HandleCount; Index++) {
> > +    Status = gBS->HandleProtocol (
> > +                    HandleBuffer[Index],
> > +
> > &gEdkiiPeCoffImageEmulatorProtocolGuid,
> > +                    (VOID **)&Emu
> > +                    );
> > +    ASSERT_EFI_ERROR (Status);
> > +
> > +    if (Emu->IsImageSupported (Emu, MachineType,
> > SubSystem, DevicePath)) {
> > +      ReturnValue = TRUE;
> > +      break;
> > +    }
> > +  }
> > +
> > +  FreePool (HandleBuffer);
> > +  return ReturnValue;
> > +}
> > +
> >  /**
> >    Load and start the Option Rom image.
> >
> > @@ -715,7 +764,9 @@ ProcessOpRomImage (
> >      //
> >      // Skip the EFI PCI Option ROM image if its
> > machine type is not supported
> >      //
> > -    if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED
> > (EfiRomHeader->EfiMachineType)) {
> > +    if (!IsImageTypeSupported(EfiRomHeader-
> > >EfiMachineType,
> > +                              EfiRomHeader-
> > >EfiSubsystem,
> > +                              PciDevice->DevicePath))
> > {
> >        goto NextImage;
> >      }
> >
> > --
> > 2.17.1
>


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH v3 4/7] MdeModulePkg/UefiBootManagerLib: allow foreign Driver#### images
  2018-09-26 23:34   ` Kinney, Michael D
@ 2018-12-27 10:16     ` Ard Biesheuvel
  0 siblings, 0 replies; 22+ messages in thread
From: Ard Biesheuvel @ 2018-12-27 10:16 UTC (permalink / raw)
  To: Kinney, Michael D
  Cc: edk2-devel@lists.01.org, Ni, Ruiyu, Zimmer, Vincent, Dong, Eric,
	Andrew Fish, Carsey, Jaben, Richardson, Brian, Gao, Liming,
	Zeng, Star

On Thu, 27 Sep 2018 at 01:34, Kinney, Michael D
<michael.d.kinney@intel.com> wrote:
>
> Hi Ard,
>
> Similar to my PciBusDxe feedback, it would be better if
> the image supported information was determined by calling
> LoadImage() and checking for an EFI_UNSUPPORTED return
> value.
>
> This also has the advantage of reducing the number of
> components that need to be aware of any new protocols
> associated with emulation with the best case being the
> DXE Core and the modules that provide the emulators.
>

In this case, yes, I think we can drop the machine type check.


> > -----Original Message-----
> > From: edk2-devel [mailto:edk2-devel-
> > bounces@lists.01.org] On Behalf Of Ard Biesheuvel
> > Sent: Thursday, September 20, 2018 4:02 PM
> > To: edk2-devel@lists.01.org
> > Cc: Ni, Ruiyu <ruiyu.ni@intel.com>; Zimmer, Vincent
> > <vincent.zimmer@intel.com>; Dong, Eric
> > <eric.dong@intel.com>; Andrew Fish <afish@apple.com>;
> > Carsey, Jaben <jaben.carsey@intel.com>; Richardson,
> > Brian <brian.richardson@intel.com>; Gao, Liming
> > <liming.gao@intel.com>; Kinney, Michael D
> > <michael.d.kinney@intel.com>; Zeng, Star
> > <star.zeng@intel.com>
> > Subject: [edk2] [PATCH v3 4/7]
> > MdeModulePkg/UefiBootManagerLib: allow foreign
> > Driver#### images
> >
> > Allow PE/COFF images that must execute under emulation
> > for Driver####
> > options, by relaxing the machine type check to include
> > support for
> > machine types that is provided by an emulator.
> >
> > Contributed-under: TianoCore Contribution Agreement 1.1
> > Signed-off-by: Ard Biesheuvel
> > <ard.biesheuvel@linaro.org>
> > ---
> >  MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
> > | 51 +++++++++++++++++++-
> >  MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
> > |  1 +
> >
> > MdeModulePkg/Library/UefiBootManagerLib/UefiBootManager
> > Lib.inf |  1 +
> >  3 files changed, 52 insertions(+), 1 deletion(-)
> >
> > diff --git
> > a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.
> > c
> > b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.
> > c
> > index 7bf96646c690..f6fda8f2c3f7 100644
> > ---
> > a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.
> > c
> > +++
> > b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.
> > c
> > @@ -1185,6 +1185,54 @@ EfiBootManagerFreeLoadOptions (
> >    return EFI_SUCCESS;
> >  }
> >
> > +STATIC
> > +BOOLEAN
> > +BmIsImageTypeSupported (
> > +  IN  UINT16    MachineType,
> > +  IN  UINT16    SubSystem
> > +  )
> > +{
> > +  EFI_STATUS                            Status;
> > +  EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL  *Emu;
> > +  UINTN                                 HandleCount;
> > +  EFI_HANDLE                            *HandleBuffer;
> > +  BOOLEAN                               ReturnValue;
> > +  UINTN                                 Index;
> > +
> > +  if (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (MachineType))
> > {
> > +    return TRUE;
> > +  }
> > +
> > +  Status = gBS->LocateHandleBuffer (
> > +                  ByProtocol,
> > +
> > &gEdkiiPeCoffImageEmulatorProtocolGuid,
> > +                  NULL,
> > +                  &HandleCount,
> > +                  &HandleBuffer
> > +                  );
> > +  if (EFI_ERROR (Status)) {
> > +    return FALSE;
> > +  }
> > +
> > +  ReturnValue = FALSE;
> > +  for (Index = 0; Index < HandleCount; Index++) {
> > +    Status = gBS->HandleProtocol (
> > +                    HandleBuffer[Index],
> > +
> > &gEdkiiPeCoffImageEmulatorProtocolGuid,
> > +                    (VOID **)&Emu
> > +                    );
> > +    ASSERT_EFI_ERROR (Status);
> > +
> > +    if (Emu->IsImageSupported (Emu, MachineType,
> > SubSystem, NULL)) {
> > +      ReturnValue = TRUE;
> > +      break;
> > +    }
> > +  }
> > +
> > +  FreePool (HandleBuffer);
> > +  return ReturnValue;
> > +}
> > +
> >  /**
> >    Return whether the PE header of the load option is
> > valid or not.
> >
> > @@ -1235,7 +1283,8 @@ BmIsLoadOptionPeHeaderValid (
> >        OptionalHeader = (EFI_IMAGE_OPTIONAL_HEADER32 *)
> > &PeHeader->Pe32.OptionalHeader;
> >        if ((OptionalHeader->Magic ==
> > EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC ||
> >             OptionalHeader->Magic ==
> > EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) &&
> > -          EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeHeader-
> > >Pe32.FileHeader.Machine)
> > +          BmIsImageTypeSupported (PeHeader-
> > >Pe32.FileHeader.Machine,
> > +                                  OptionalHeader-
> > >Subsystem)
> >            ) {
> >          //
> >          // Check the Subsystem:
> > diff --git
> > a/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
> > b/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
> > index 978fbff966f6..5f64ef304b87 100644
> > ---
> > a/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
> > +++
> > b/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
> > @@ -47,6 +47,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS
> > OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >  #include <Protocol/VariableLock.h>
> >  #include <Protocol/RamDisk.h>
> >  #include <Protocol/DeferredImageLoad.h>
> > +#include <Protocol/PeCoffImageEmulator.h>
> >
> >  #include <Guid/MemoryTypeInformation.h>
> >  #include <Guid/FileInfo.h>
> > diff --git
> > a/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManag
> > erLib.inf
> > b/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManag
> > erLib.inf
> > index 72c5ca1cd59e..09e2134acf8e 100644
> > ---
> > a/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManag
> > erLib.inf
> > +++
> > b/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManag
> > erLib.inf
> > @@ -104,6 +104,7 @@
> >    gEfiDevicePathProtocolGuid                    ##
> > SOMETIMES_CONSUMES
> >    gEfiBootLogoProtocolGuid                      ##
> > SOMETIMES_CONSUMES
> >    gEfiSimpleTextInputExProtocolGuid             ##
> > SOMETIMES_CONSUMES
> > +  gEdkiiPeCoffImageEmulatorProtocolGuid         ##
> > SOMETIMES_CONSUMES
> >    gEdkiiVariableLockProtocolGuid                ##
> > SOMETIMES_CONSUMES
> >    gEfiGraphicsOutputProtocolGuid                ##
> > SOMETIMES_CONSUMES
> >    gEfiUsbIoProtocolGuid                         ##
> > SOMETIMES_CONSUMES
> > --
> > 2.17.1
> >
> > _______________________________________________
> > edk2-devel mailing list
> > edk2-devel@lists.01.org
> > https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2018-12-27 10:16 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-09-20 23:01 [PATCH v3 0/7] MdeModulePkg: add support for dispatching foreign arch PE/COFF images Ard Biesheuvel
2018-09-20 23:01 ` [PATCH v3 1/7] MdeModulePkg: introduce PE/COFF image emulator protocol Ard Biesheuvel
2018-09-26  9:58   ` Zeng, Star
2018-09-26 10:13     ` Ard Biesheuvel
2018-09-26 17:32       ` Kinney, Michael D
2018-09-27  0:48         ` Zeng, Star
2018-09-27 10:58           ` Ard Biesheuvel
2018-09-27 15:36             ` Kinney, Michael D
2018-09-28  3:05               ` Zeng, Star
2018-09-28  3:08                 ` Zeng, Star
2018-09-28  6:34                   ` Ni, Ruiyu
2018-09-28  7:02                     ` Zeng, Star
2018-09-20 23:01 ` [PATCH v3 2/7] MdeModulePkg/DxeCore: invoke the emulator protocol for foreign images Ard Biesheuvel
2018-09-20 23:01 ` [PATCH v3 3/7] MdeModulePkg/PciBusDxe: invoke PE/COFF emulator for foreign option ROMs Ard Biesheuvel
2018-09-26 18:26   ` Kinney, Michael D
2018-12-27 10:13     ` Ard Biesheuvel
2018-09-20 23:01 ` [PATCH v3 4/7] MdeModulePkg/UefiBootManagerLib: allow foreign Driver#### images Ard Biesheuvel
2018-09-26 23:34   ` Kinney, Michael D
2018-12-27 10:16     ` Ard Biesheuvel
2018-09-20 23:01 ` [PATCH v3 5/7] MdeModulePkg/EbcDxe: implement the PE/COFF emulator protocol Ard Biesheuvel
2018-09-20 23:01 ` [PATCH v3 6/7] MdePkg/UefiBaseType.h: treat EBC as a non-native machine type Ard Biesheuvel
2018-09-20 23:01 ` [PATCH v3 7/7] MdeModulePkg/DxeCore: remove explicit EBC handling Ard Biesheuvel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox