There is a bug in original EdkModulePkg\Library\EdkUefiRuntimeLib\Common\RuntimeLib...
[mirror_edk2.git] / EdkModulePkg / Library / EdkUefiRuntimeLib / Common / RuntimeLib.c
1 /**@file
2 Library utility functions for Runtime 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 #include <RuntimeLibInternal.h>
16
17 ///
18 /// Driver Lib Module Globals
19 ///
20
21 STATIC EFI_EVENT mEfiVirtualNotifyEvent;
22 STATIC BOOLEAN mEfiGoneVirtual = FALSE;
23 STATIC BOOLEAN mEfiAtRuntime = FALSE;
24 EFI_RUNTIME_SERVICES *mRT;
25
26 /**
27 Set AtRuntime flag as TRUE after ExitBootServices
28
29 @param[in] Event The Event that is being processed
30 @param[in] Context Event Context
31 **/
32 VOID
33 EFIAPI
34 RuntimeDriverExitBootServices (
35 IN EFI_EVENT Event,
36 IN VOID *Context
37 )
38 {
39 //
40 // Clear out BootService globals
41 //
42 gBS = NULL;
43
44 mEfiAtRuntime = TRUE;
45 }
46
47 /**
48 Fixup internal data so that EFI can be call in virtual mode.
49 Call the passed in Child Notify event and convert any pointers in
50 lib to virtual mode.
51
52 @param[in] Event The Event that is being processed
53 @param[in] Context Event Context
54 **/
55 STATIC
56 VOID
57 EFIAPI
58 RuntimeLibVirtualNotifyEvent (
59 IN EFI_EVENT Event,
60 IN VOID *Context
61 )
62 {
63 UINTN Index;
64 EFI_EVENT_NOTIFY ChildNotifyEventHandler;
65
66 for (Index = 0;
67 _gDriverSetVirtualAddressMapEvent[Index] != NULL;
68 Index++) {
69 ChildNotifyEventHandler = _gDriverSetVirtualAddressMapEvent[Index];
70 ChildNotifyEventHandler (Event, NULL);
71 }
72
73 //
74 // Update global for Runtime Services Table and IO
75 //
76 EfiConvertPointer (0, (VOID **) &mRT);
77
78 mEfiGoneVirtual = TRUE;
79 }
80
81 /**
82 Intialize runtime Driver Lib if it has not yet been initialized.
83
84 @param[in] ImageHandle The firmware allocated handle for the EFI image.
85 @param[in] SystemTable A pointer to the EFI System Table.
86
87 @return EFI_STATUS always returns EFI_SUCCESS except EFI_ALREADY_STARTED if already started.
88 **/
89 EFI_STATUS
90 EFIAPI
91 RuntimeDriverLibConstruct (
92 IN EFI_HANDLE ImageHandle,
93 IN EFI_SYSTEM_TABLE *SystemTable
94 )
95 {
96 EFI_STATUS Status;
97
98 ASSERT (SystemTable != NULL);
99 mRT = SystemTable->RuntimeServices;
100 ASSERT (mRT != NULL);
101
102 //
103 // Register SetVirtualAddressMap () notify function
104 //
105 ASSERT (gBS != NULL);
106 Status = gBS->CreateEvent (
107 EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,
108 EFI_TPL_NOTIFY,
109 RuntimeLibVirtualNotifyEvent,
110 NULL,
111 &mEfiVirtualNotifyEvent
112 );
113
114 ASSERT_EFI_ERROR (Status);
115
116 return EFI_SUCCESS;
117 }
118
119 /**
120 This routine will free some resources which have been allocated in
121 EfiInitializeRuntimeDriverLib(). If a runtime driver exits with an error,
122 it must call this routine to free the allocated resource before the exiting.
123
124 @retval EFI_SUCCESS Shutdown the Runtime Driver Lib successfully
125 @retval EFI_UNSUPPORTED Runtime Driver lib was not initialized at all
126 **/
127 EFI_STATUS
128 EFIAPI
129 RuntimeDriverLibDeconstruct (
130 IN EFI_HANDLE ImageHandle,
131 IN EFI_SYSTEM_TABLE *SystemTable
132 )
133 {
134 EFI_STATUS Status;
135
136 //
137 // Close SetVirtualAddressMap () notify function
138 //
139 if (_gDriverSetVirtualAddressMapEvent[0] != NULL) {
140 ASSERT (gBS != NULL);
141 Status = gBS->CloseEvent (mEfiVirtualNotifyEvent);
142 ASSERT_EFI_ERROR (Status);
143 }
144
145 return EFI_SUCCESS;
146 }
147
148 /**
149 Return TRUE if ExitBootServices () has been called
150
151 @retval TRUE If ExitBootServices () has been called
152 **/
153 BOOLEAN
154 EFIAPI
155 EfiAtRuntime (
156 VOID
157 )
158 {
159 return mEfiAtRuntime;
160 }
161
162 /**
163 Return TRUE if SetVirtualAddressMap () has been called
164
165 @retval TRUE If SetVirtualAddressMap () has been called
166 **/
167 BOOLEAN
168 EFIAPI
169 EfiGoneVirtual (
170 VOID
171 )
172 {
173 return mEfiGoneVirtual;
174 }
175