adddf933e2d26e0feb534593931d3faf9cb6eb6f
[mirror_edk2.git] / EdkModulePkg / Library / EdkUefiRuntimeLib / Common / RuntimeLib.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 RuntimeLib.c
15
16 Abstract:
17
18 Light weight lib to support Tiano drivers.
19
20 --*/
21
22 #include <RuntimeLibInternal.h>
23
24 //
25 // Driver Lib Module Globals
26 //
27 STATIC EFI_EVENT mRuntimeNotifyEvent;
28 STATIC EFI_EVENT mEfiVirtualNotifyEvent;
29 STATIC BOOLEAN mEfiGoneVirtual = FALSE;
30 STATIC BOOLEAN mEfiAtRuntime = FALSE;
31
32 EFI_RUNTIME_SERVICES *mRT = NULL;
33
34 VOID
35 EFIAPI
36 RuntimeDriverExitBootServices (
37 IN EFI_EVENT Event,
38 IN VOID *Context
39 )
40 /*++
41
42 Routine Description:
43
44 Set AtRuntime flag as TRUE after ExitBootServices
45
46 Arguments:
47
48 Event - The Event that is being processed
49
50 Context - Event Context
51
52 Returns:
53
54 None
55
56 --*/
57 {
58 EFI_EVENT_NOTIFY ChildNotifyEventHandler;
59 UINTN Index;
60
61 for (Index = 0;
62 _gDriverExitBootServicesEvent[Index] != NULL;
63 Index++) {
64 ChildNotifyEventHandler = _gDriverExitBootServicesEvent[Index];
65 ChildNotifyEventHandler (Event, NULL);
66 }
67
68 mEfiAtRuntime = TRUE;
69 }
70
71 STATIC
72 VOID
73 EFIAPI
74 RuntimeLibVirtualNotifyEvent (
75 IN EFI_EVENT Event,
76 IN VOID *Context
77 )
78 /*++
79
80 Routine Description:
81
82 Fixup internal data so that EFI can be call in virtual mode.
83 Call the passed in Child Notify event and convert any pointers in
84 lib to virtual mode.
85
86 Arguments:
87
88 Event - The Event that is being processed
89
90 Context - Event Context
91
92 Returns:
93
94 None
95
96 --*/
97 {
98 UINTN Index;
99 EFI_EVENT_NOTIFY ChildNotifyEventHandler;
100
101 for (Index = 0;
102 _gDriverSetVirtualAddressMapEvent[Index] != NULL;
103 Index++) {
104 ChildNotifyEventHandler = _gDriverSetVirtualAddressMapEvent[Index];
105 ChildNotifyEventHandler (Event, NULL);
106 }
107
108 //
109 // Update global for Runtime Services Table and IO
110 //
111 EfiConvertInternalPointer ((VOID **) &mRT);
112
113 //
114 // Clear out BootService globals
115 //
116 gBS = NULL;
117 gST = NULL;
118 mEfiGoneVirtual = TRUE;
119 }
120
121 EFI_STATUS
122 RuntimeDriverLibConstruct (
123 IN EFI_HANDLE ImageHandle,
124 IN EFI_SYSTEM_TABLE *SystemTable
125 )
126 /*++
127
128 Routine Description:
129
130 Intialize runtime Driver Lib if it has not yet been initialized.
131
132 Arguments:
133
134 ImageHandle - The firmware allocated handle for the EFI image.
135
136 SystemTable - A pointer to the EFI System Table.
137
138 GoVirtualChildEvent - Caller can register a virtual notification event.
139
140 Returns:
141
142 EFI_STATUS always returns EFI_SUCCESS except EFI_ALREADY_STARTED if already started.
143
144 --*/
145 {
146 EFI_STATUS Status;
147
148 mRT = SystemTable->RuntimeServices;
149
150 //
151 // Register our ExitBootServices () notify function
152 //
153 Status = gBS->CreateEvent (
154 EFI_EVENT_SIGNAL_EXIT_BOOT_SERVICES,
155 EFI_TPL_NOTIFY,
156 RuntimeDriverExitBootServices,
157 NULL,
158 &mRuntimeNotifyEvent
159 );
160
161 ASSERT_EFI_ERROR (Status);
162
163 //
164 // Register SetVirtualAddressMap () notify function
165 //
166 Status = gBS->CreateEvent (
167 EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,
168 EFI_TPL_NOTIFY,
169 RuntimeLibVirtualNotifyEvent,
170 NULL,
171 &mEfiVirtualNotifyEvent
172 );
173
174 ASSERT_EFI_ERROR (Status);
175
176 return EFI_SUCCESS;
177 }
178
179 EFI_STATUS
180 RuntimeDriverLibDeconstruct (
181 VOID
182 )
183 /*++
184
185 Routine Description:
186
187 This routine will free some resources which have been allocated in
188 EfiInitializeRuntimeDriverLib(). If a runtime driver exits with an error,
189 it must call this routine to free the allocated resource before the exiting.
190
191 Arguments:
192
193 None
194
195 Returns:
196
197 EFI_SUCCESS - Shotdown the Runtime Driver Lib successfully
198 EFI_UNSUPPORTED - Runtime Driver lib was not initialized at all
199
200 --*/
201 {
202 EFI_STATUS Status;
203
204 //
205 // Close our ExitBootServices () notify function
206 //
207 Status = gBS->CloseEvent (mRuntimeNotifyEvent);
208 ASSERT_EFI_ERROR (Status);
209
210 //
211 // Close SetVirtualAddressMap () notify function
212 //
213 Status = gBS->CloseEvent (mEfiVirtualNotifyEvent);
214 ASSERT_EFI_ERROR (Status);
215
216 return EFI_SUCCESS;
217 }
218
219 BOOLEAN
220 EfiAtRuntime (
221 VOID
222 )
223 /*++
224
225 Routine Description:
226 Return TRUE if ExitBootServices () has been called
227
228 Arguments:
229 NONE
230
231 Returns:
232 TRUE - If ExitBootServices () has been called
233
234 --*/
235 {
236 return mEfiAtRuntime;
237 }
238
239 BOOLEAN
240 EfiGoneVirtual (
241 VOID
242 )
243 /*++
244
245 Routine Description:
246 Return TRUE if SetVirtualAddressMap () has been called
247
248 Arguments:
249 NONE
250
251 Returns:
252 TRUE - If SetVirtualAddressMap () has been called
253
254 --*/
255 {
256 return mEfiGoneVirtual;
257 }
258