1 /** @file
2   Provides services to perform additional actions to relocate and unload
3   PE/Coff image for Emu environment specific purpose such as souce level debug.
4   This version only works for PEI phase
5 
6 Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>
7 Portions copyright (c) 2008 - 2011, Apple Inc. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9 
10 **/
11 #include <PiPei.h>
12 #include <Ppi/EmuThunk.h>
13 #include <Protocol/EmuThunk.h>
14 
15 #include <Library/PeCoffLib.h>
16 #include <Library/PeiServicesLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/BaseLib.h>
19 #include <Library/PeCoffExtraActionLib.h>
20 #include <Library/EmuMagicPageLib.h>
21 
22 //
23 // Cache of UnixThunk protocol
24 //
25 EMU_THUNK_PROTOCOL   *mThunk = NULL;
26 
27 /**
28   The function caches the pointer of the Unix thunk functions
29   It will ASSERT() if Unix thunk ppi is not installed.
30 
31   @retval EFI_SUCCESS   WinNT thunk protocol is found and cached.
32 
33 **/
34 EFI_STATUS
35 EFIAPI
EmuPeCoffGetThunkStucture()36 EmuPeCoffGetThunkStucture (
37   )
38 {
39   EMU_THUNK_PPI     *ThunkPpi;
40   EFI_STATUS        Status;
41 
42 
43   //
44   // Locate Unix ThunkPpi for retrieving standard output handle
45   //
46   Status = PeiServicesLocatePpi (
47               &gEmuThunkPpiGuid,
48               0,
49               NULL,
50               (VOID **) &ThunkPpi
51               );
52   ASSERT_EFI_ERROR (Status);
53 
54   EMU_MAGIC_PAGE()->Thunk = (EMU_THUNK_PROTOCOL *) ThunkPpi->Thunk ();
55 
56   return EFI_SUCCESS;
57 }
58 
59 /**
60   Performs additional actions after a PE/COFF image has been loaded and relocated.
61 
62   If ImageContext is NULL, then ASSERT().
63 
64   @param  ImageContext  Pointer to the image context structure that describes the
65                         PE/COFF image that has already been loaded and relocated.
66 
67 **/
68 VOID
69 EFIAPI
PeCoffLoaderRelocateImageExtraAction(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT * ImageContext)70 PeCoffLoaderRelocateImageExtraAction (
71   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext
72   )
73 {
74   if (EMU_MAGIC_PAGE()->Thunk == NULL) {
75     EmuPeCoffGetThunkStucture ();
76   }
77     EMU_MAGIC_PAGE()->Thunk->PeCoffRelocateImageExtraAction (ImageContext);
78   }
79 
80 
81 /**
82   Performs additional actions just before a PE/COFF image is unloaded.  Any resources
83   that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
84 
85   If ImageContext is NULL, then ASSERT().
86 
87   @param  ImageContext  Pointer to the image context structure that describes the
88                         PE/COFF image that is being unloaded.
89 
90 **/
91 VOID
92 EFIAPI
PeCoffLoaderUnloadImageExtraAction(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT * ImageContext)93 PeCoffLoaderUnloadImageExtraAction (
94   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext
95   )
96 {
97   if (EMU_MAGIC_PAGE()->Thunk == NULL) {
98     EmuPeCoffGetThunkStucture ();
99   }
100   EMU_MAGIC_PAGE()->Thunk->PeCoffUnloadImageExtraAction (ImageContext);
101 }
102