]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/UefiDriverEntryPoint/DriverEntryPoint.c
Move registration and processing of ExitBootServices() events into UefiDriverEntryPoi...
[mirror_edk2.git] / MdePkg / Library / UefiDriverEntryPoint / DriverEntryPoint.c
1 /** @file
2 Entry point to a EFI/DXE driver.
3
4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 /**
16 Unload function that is registered in the LoadImage protocol. It un-installs
17 protocols produced and deallocates pool used by the driver. Called by the core
18 when unloading the driver.
19
20 @param ImageHandle
21
22 @retval EFI_SUCCESS
23
24 **/
25
26 EFI_EVENT _mDriverExitBootServicesNotifyEvent;
27
28 EFI_STATUS
29 EFIAPI
30 _DriverUnloadHandler (
31 EFI_HANDLE ImageHandle
32 )
33 {
34 EFI_STATUS Status;
35
36 //
37 // If an UnloadImage() handler is specified, then call it
38 //
39 Status = ProcessModuleUnloadList (ImageHandle);
40
41 //
42 // If the driver specific unload handler does not return an error, then call all of the
43 // library destructors. If the unload handler returned an error, then the driver can not be
44 // unloaded, and the library destructors should not be called
45 //
46 if (!EFI_ERROR (Status)) {
47 //
48 // Close our ExitBootServices () notify function
49 //
50 if (_gDriverExitBootServicesEvent[0] != NULL) {
51 Status = gBS->CloseEvent (_mDriverExitBootServicesNotifyEvent);
52 ASSERT_EFI_ERROR (Status);
53 }
54
55 ProcessLibraryDestructorList (ImageHandle, gST);
56 }
57
58 //
59 // Return the status from the driver specific unload handler
60 //
61 return Status;
62 }
63
64 VOID
65 EFIAPI
66 _DriverExitBootServices (
67 IN EFI_EVENT Event,
68 IN VOID *Context
69 )
70 /*++
71
72 Routine Description:
73
74 Set AtRuntime flag as TRUE after ExitBootServices
75
76 Arguments:
77
78 Event - The Event that is being processed
79
80 Context - Event Context
81
82 Returns:
83
84 None
85
86 --*/
87 {
88 EFI_EVENT_NOTIFY ChildNotifyEventHandler;
89 UINTN Index;
90
91 for (Index = 0; _gDriverExitBootServicesEvent[Index] != NULL; Index++) {
92 ChildNotifyEventHandler = _gDriverExitBootServicesEvent[Index];
93 ChildNotifyEventHandler (Event, NULL);
94 }
95 }
96
97 /**
98 Enrty point to DXE Driver.
99
100 @param ImageHandle ImageHandle of the loaded driver.
101 @param SystemTable Pointer to the EFI System Table.
102
103 @retval EFI_SUCCESS One or more of the drivers returned a success code.
104 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
105
106 **/
107 EFI_STATUS
108 EFIAPI
109 _ModuleEntryPoint (
110 IN EFI_HANDLE ImageHandle,
111 IN EFI_SYSTEM_TABLE *SystemTable
112 )
113 {
114 EFI_STATUS Status;
115 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
116
117 if (_gUefiDriverRevision != 0) {
118 //
119 // Make sure that the EFI/UEFI spec revision of the platform is >= EFI/UEFI spec revision of the driver
120 //
121 if (SystemTable->Hdr.Revision < _gUefiDriverRevision) {
122 return EFI_INCOMPATIBLE_VERSION;
123 }
124 }
125
126 //
127 // Call constructor for all libraries
128 //
129 ProcessLibraryConstructorList (ImageHandle, SystemTable);
130
131 //
132 // Register our ExitBootServices () notify function
133 //
134 if (_gDriverExitBootServicesEvent[0] != NULL) {
135 Status = gBS->CreateEvent (
136 EFI_EVENT_SIGNAL_EXIT_BOOT_SERVICES,
137 EFI_TPL_NOTIFY,
138 _DriverExitBootServices,
139 NULL,
140 &_mDriverExitBootServicesNotifyEvent
141 );
142
143 ASSERT_EFI_ERROR (Status);
144 }
145
146 //
147 // Install unload handler...
148 //
149 if (_gDriverUnloadImageCount != 0) {
150 Status = gBS->HandleProtocol (
151 ImageHandle,
152 &gEfiLoadedImageProtocolGuid,
153 (VOID **)&LoadedImage
154 );
155 ASSERT_EFI_ERROR (Status);
156 LoadedImage->Unload = _DriverUnloadHandler;
157 }
158
159 //
160 // Call the driver entry point
161 //
162 Status = ProcessModuleEntryPointList (ImageHandle, SystemTable);
163
164 //
165 // If all of the drivers returned errors, then invoke all of the library destructors
166 //
167 if (EFI_ERROR (Status)) {
168 //
169 // Close our ExitBootServices () notify function
170 //
171 if (_gDriverExitBootServicesEvent[0] != NULL) {
172 Status = gBS->CloseEvent (_mDriverExitBootServicesNotifyEvent);
173 ASSERT_EFI_ERROR (Status);
174 }
175
176 ProcessLibraryDestructorList (ImageHandle, SystemTable);
177 }
178
179 //
180 // Return the cummalative return status code from all of the driver entry points
181 //
182 return Status;
183 }
184
185
186 /**
187 Enrty point wrapper of DXE Driver.
188
189 @param ImageHandle ImageHandle of the loaded driver.
190 @param SystemTable Pointer to the EFI System Table.
191
192 @retval EFI_SUCCESS One or more of the drivers returned a success code.
193 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
194
195 **/
196 EFI_STATUS
197 EFIAPI
198 EfiMain (
199 IN EFI_HANDLE ImageHandle,
200 IN EFI_SYSTEM_TABLE *SystemTable
201 )
202 {
203 return _ModuleEntryPoint (ImageHandle, SystemTable);
204 }