]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdePkg / Library / StandaloneMmDriverEntryPoint / StandaloneMmDriverEntryPoint.c
1 /** @file
2 Entry point to a Standalone MM driver.
3
4 Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2016 - 2018, ARM Ltd. All rights reserved.<BR>
6 Copyright (c) 2018, Linaro, Limited. All rights reserved.<BR>
7
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
12 #include <PiMm.h>
13
14 #include <Protocol/LoadedImage.h>
15 #include <Library/BaseLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/MmServicesTableLib.h>
18 #include <Library/StandaloneMmDriverEntryPoint.h>
19
20 /**
21 Unloads an image from memory.
22
23 This function is a callback that a driver registers to do cleanup
24 when the UnloadImage boot service function is called.
25
26 @param ImageHandle The handle to the image to unload.
27
28 @return Status returned by all unload().
29
30 **/
31 EFI_STATUS
32 EFIAPI
33 _DriverUnloadHandler (
34 EFI_HANDLE ImageHandle
35 )
36 {
37 EFI_STATUS Status;
38
39 //
40 // If an UnloadImage() handler is specified, then call it
41 //
42 Status = ProcessModuleUnloadList (ImageHandle);
43
44 //
45 // If the driver specific unload handler does not return an error, then call all of the
46 // library destructors. If the unload handler returned an error, then the driver can not be
47 // unloaded, and the library destructors should not be called
48 //
49 if (!EFI_ERROR (Status)) {
50 ProcessLibraryDestructorList (ImageHandle, gMmst);
51 }
52
53 //
54 // Return the status from the driver specific unload handler
55 //
56 return Status;
57 }
58
59 /**
60 The entry point of PE/COFF Image for a Standalone MM Driver.
61
62 This function is the entry point for a Standalone MM Driver.
63 This function must call ProcessLibraryConstructorList() and
64 ProcessModuleEntryPointList().
65 If the return status from ProcessModuleEntryPointList()
66 is an error status, then ProcessLibraryDestructorList() must be called.
67 The return value from ProcessModuleEntryPointList() is returned.
68 If _gMmRevision is not zero and SystemTable->Hdr.Revision is
69 less than _gMmRevision, then return EFI_INCOMPATIBLE_VERSION.
70
71 @param ImageHandle The image handle of the Standalone MM Driver.
72 @param MmSystemTable A pointer to the MM System Table.
73
74 @retval EFI_SUCCESS The Standalone MM Driver exited normally.
75 @retval EFI_INCOMPATIBLE_VERSION _gMmRevision is greater than
76 MmSystemTable->Hdr.Revision.
77 @retval Other Return value from
78 ProcessModuleEntryPointList().
79
80 **/
81 EFI_STATUS
82 EFIAPI
83 _ModuleEntryPoint (
84 IN EFI_HANDLE ImageHandle,
85 IN IN EFI_MM_SYSTEM_TABLE *MmSystemTable
86 )
87 {
88 EFI_STATUS Status;
89 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
90
91 if (_gMmRevision != 0) {
92 //
93 // Make sure that the MM spec revision of the platform
94 // is >= MM spec revision of the driver
95 //
96 if (MmSystemTable->Hdr.Revision < _gMmRevision) {
97 return EFI_INCOMPATIBLE_VERSION;
98 }
99 }
100
101 //
102 // Call constructor for all libraries
103 //
104 ProcessLibraryConstructorList (ImageHandle, MmSystemTable);
105
106 //
107 // Install unload handler...
108 //
109 if (_gDriverUnloadImageCount != 0) {
110 Status = gMmst->MmHandleProtocol (
111 ImageHandle,
112 &gEfiLoadedImageProtocolGuid,
113 (VOID **)&LoadedImage
114 );
115 ASSERT_EFI_ERROR (Status);
116 LoadedImage->Unload = _DriverUnloadHandler;
117 }
118
119 //
120 // Call the driver entry point
121 //
122 Status = ProcessModuleEntryPointList (ImageHandle, MmSystemTable);
123
124 //
125 // If all of the drivers returned errors, then invoke all of the library destructors
126 //
127 if (EFI_ERROR (Status)) {
128 ProcessLibraryDestructorList (ImageHandle, MmSystemTable);
129 }
130
131 //
132 // Return the cumulative return status code from all of the driver entry points
133 //
134 return Status;
135 }