]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/ResetSystemRuntimeDxe/ResetSystem.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Universal / ResetSystemRuntimeDxe / ResetSystem.c
1 /** @file
2 Reset Architectural and Reset Notification protocols implementation.
3
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "ResetSystem.h"
11
12 GLOBAL_REMOVE_IF_UNREFERENCED CHAR16 *mResetTypeStr[] = {
13 L"Cold", L"Warm", L"Shutdown", L"PlatformSpecific"
14 };
15
16 //
17 // The current ResetSystem() notification recursion depth
18 //
19 UINTN mResetNotifyDepth = 0;
20
21 /**
22 Register a notification function to be called when ResetSystem() is called.
23
24 The RegisterResetNotify() function registers a notification function that is called when
25 ResetSystem()is called and prior to completing the reset of the platform.
26 The registered functions must not perform a platform reset themselves. These
27 notifications are intended only for the notification of components which may need some
28 special-purpose maintenance prior to the platform resetting.
29 The list of registered reset notification functions are processed if ResetSystem()is called
30 before ExitBootServices(). The list of registered reset notification functions is ignored if
31 ResetSystem()is called after ExitBootServices().
32
33 @param[in] This A pointer to the EFI_RESET_NOTIFICATION_PROTOCOL instance.
34 @param[in] ResetFunction Points to the function to be called when a ResetSystem() is executed.
35
36 @retval EFI_SUCCESS The reset notification function was successfully registered.
37 @retval EFI_INVALID_PARAMETER ResetFunction is NULL.
38 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to register the reset notification function.
39 @retval EFI_ALREADY_STARTED The reset notification function specified by ResetFunction has already been registered.
40
41 **/
42 EFI_STATUS
43 EFIAPI
44 RegisterResetNotify (
45 IN EFI_RESET_NOTIFICATION_PROTOCOL *This,
46 IN EFI_RESET_SYSTEM ResetFunction
47 )
48 {
49 RESET_NOTIFICATION_INSTANCE *Instance;
50 LIST_ENTRY *Link;
51 RESET_NOTIFY_ENTRY *Entry;
52
53 if (ResetFunction == NULL) {
54 return EFI_INVALID_PARAMETER;
55 }
56
57 Instance = RESET_NOTIFICATION_INSTANCE_FROM_THIS (This);
58
59 for ( Link = GetFirstNode (&Instance->ResetNotifies)
60 ; !IsNull (&Instance->ResetNotifies, Link)
61 ; Link = GetNextNode (&Instance->ResetNotifies, Link)
62 ) {
63 Entry = RESET_NOTIFY_ENTRY_FROM_LINK (Link);
64 if (Entry->ResetNotify == ResetFunction) {
65 return EFI_ALREADY_STARTED;
66 }
67 }
68
69 ASSERT (IsNull (&Instance->ResetNotifies, Link));
70 Entry = AllocatePool (sizeof (*Entry));
71 if (Entry == NULL) {
72 return EFI_OUT_OF_RESOURCES;
73 }
74 Entry->Signature = RESET_NOTIFY_ENTRY_SIGNATURE;
75 Entry->ResetNotify = ResetFunction;
76 InsertTailList (&Instance->ResetNotifies, &Entry->Link);
77 return EFI_SUCCESS;
78 }
79
80 /**
81 Unregister a notification function.
82
83 The UnregisterResetNotify() function removes the previously registered
84 notification using RegisterResetNotify().
85
86 @param[in] This A pointer to the EFI_RESET_NOTIFICATION_PROTOCOL instance.
87 @param[in] ResetFunction The pointer to the ResetFunction being unregistered.
88
89 @retval EFI_SUCCESS The reset notification function was unregistered.
90 @retval EFI_INVALID_PARAMETER ResetFunction is NULL.
91 @retval EFI_INVALID_PARAMETER The reset notification function specified by ResetFunction was not previously
92 registered using RegisterResetNotify().
93
94 **/
95 EFI_STATUS
96 EFIAPI
97 UnregisterResetNotify (
98 IN EFI_RESET_NOTIFICATION_PROTOCOL *This,
99 IN EFI_RESET_SYSTEM ResetFunction
100 )
101 {
102 RESET_NOTIFICATION_INSTANCE *Instance;
103 LIST_ENTRY *Link;
104 RESET_NOTIFY_ENTRY *Entry;
105
106 if (ResetFunction == NULL) {
107 return EFI_INVALID_PARAMETER;
108 }
109
110 Instance = RESET_NOTIFICATION_INSTANCE_FROM_THIS (This);
111
112 for ( Link = GetFirstNode (&Instance->ResetNotifies)
113 ; !IsNull (&Instance->ResetNotifies, Link)
114 ; Link = GetNextNode (&Instance->ResetNotifies, Link)
115 ) {
116 Entry = RESET_NOTIFY_ENTRY_FROM_LINK (Link);
117 if (Entry->ResetNotify == ResetFunction) {
118 RemoveEntryList (&Entry->Link);
119 FreePool (Entry);
120 return EFI_SUCCESS;
121 }
122 }
123
124 return EFI_INVALID_PARAMETER;
125 }
126
127 RESET_NOTIFICATION_INSTANCE mResetNotification = {
128 RESET_NOTIFICATION_INSTANCE_SIGNATURE,
129 {
130 RegisterResetNotify,
131 UnregisterResetNotify
132 },
133 INITIALIZE_LIST_HEAD_VARIABLE (mResetNotification.ResetNotifies)
134 };
135
136 RESET_NOTIFICATION_INSTANCE mPlatformSpecificResetFilter = {
137 RESET_NOTIFICATION_INSTANCE_SIGNATURE,
138 {
139 RegisterResetNotify,
140 UnregisterResetNotify
141 },
142 INITIALIZE_LIST_HEAD_VARIABLE (mPlatformSpecificResetFilter.ResetNotifies)
143 };
144
145 RESET_NOTIFICATION_INSTANCE mPlatformSpecificResetHandler = {
146 RESET_NOTIFICATION_INSTANCE_SIGNATURE,
147 {
148 RegisterResetNotify,
149 UnregisterResetNotify
150 },
151 INITIALIZE_LIST_HEAD_VARIABLE (mPlatformSpecificResetHandler.ResetNotifies)
152 };
153
154 /**
155 The driver's entry point.
156
157 It initializes the Reset Architectural Protocol.
158
159 @param[in] ImageHandle The firmware allocated handle for the EFI image.
160 @param[in] SystemTable A pointer to the EFI System Table.
161
162 @retval EFI_SUCCESS The entry point is executed successfully.
163 @retval other Cannot install ResetArch protocol.
164
165 **/
166 EFI_STATUS
167 EFIAPI
168 InitializeResetSystem (
169 IN EFI_HANDLE ImageHandle,
170 IN EFI_SYSTEM_TABLE *SystemTable
171 )
172 {
173 EFI_STATUS Status;
174 EFI_HANDLE Handle;
175
176 //
177 // Make sure the Reset Architectural Protocol is not already installed in the system
178 //
179 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiResetArchProtocolGuid);
180
181 //
182 // Hook the runtime service table
183 //
184 gRT->ResetSystem = RuntimeServiceResetSystem;
185
186 //
187 // Now install the Reset RT AP on a new handle
188 //
189 Handle = NULL;
190 Status = gBS->InstallMultipleProtocolInterfaces (
191 &Handle,
192 &gEfiResetArchProtocolGuid, NULL,
193 &gEfiResetNotificationProtocolGuid, &mResetNotification.ResetNotification,
194 &gEdkiiPlatformSpecificResetFilterProtocolGuid, &mPlatformSpecificResetFilter.ResetNotification,
195 &gEdkiiPlatformSpecificResetHandlerProtocolGuid, &mPlatformSpecificResetHandler.ResetNotification,
196 NULL
197 );
198 ASSERT_EFI_ERROR (Status);
199
200 return Status;
201 }
202
203 /**
204 Resets the entire platform.
205
206 @param[in] ResetType The type of reset to perform.
207 @param[in] ResetStatus The status code for the reset.
208 @param[in] DataSize The size, in bytes, of ResetData.
209 @param[in] ResetData For a ResetType of EfiResetCold, EfiResetWarm, or
210 EfiResetShutdown the data buffer starts with a Null-terminated
211 string, optionally followed by additional binary data.
212 The string is a description that the caller may use to further
213 indicate the reason for the system reset. ResetData is only
214 valid if ResetStatus is something other than EFI_SUCCESS
215 unless the ResetType is EfiResetPlatformSpecific
216 where a minimum amount of ResetData is always required.
217 For a ResetType of EfiResetPlatformSpecific the data buffer
218 also starts with a Null-terminated string that is followed
219 by an EFI_GUID that describes the specific type of reset to perform.
220 **/
221 VOID
222 EFIAPI
223 RuntimeServiceResetSystem (
224 IN EFI_RESET_TYPE ResetType,
225 IN EFI_STATUS ResetStatus,
226 IN UINTN DataSize,
227 IN VOID *ResetData OPTIONAL
228 )
229 {
230 LIST_ENTRY *Link;
231 RESET_NOTIFY_ENTRY *Entry;
232
233 //
234 // Only do REPORT_STATUS_CODE() on first call to RuntimeServiceResetSystem()
235 //
236 if (mResetNotifyDepth == 0) {
237 //
238 // Indicate reset system runtime service is called.
239 //
240 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_EFI_RUNTIME_SERVICE | EFI_SW_RS_PC_RESET_SYSTEM));
241 }
242
243 mResetNotifyDepth++;
244 DEBUG ((
245 DEBUG_INFO, "DXE ResetSystem2: ResetType %s, Call Depth = %d.\n",
246 mResetTypeStr[ResetType], mResetNotifyDepth
247 ));
248
249 if (mResetNotifyDepth <= MAX_RESET_NOTIFY_DEPTH) {
250 if (!EfiAtRuntime ()) {
251 //
252 // Call reset notification functions registered through the
253 // EDKII_PLATFORM_SPECIFIC_RESET_FILTER_PROTOCOL.
254 //
255 for ( Link = GetFirstNode (&mPlatformSpecificResetFilter.ResetNotifies)
256 ; !IsNull (&mPlatformSpecificResetFilter.ResetNotifies, Link)
257 ; Link = GetNextNode (&mPlatformSpecificResetFilter.ResetNotifies, Link)
258 ) {
259 Entry = RESET_NOTIFY_ENTRY_FROM_LINK (Link);
260 Entry->ResetNotify (ResetType, ResetStatus, DataSize, ResetData);
261 }
262 //
263 // Call reset notification functions registered through the
264 // EFI_RESET_NOTIFICATION_PROTOCOL.
265 //
266 for ( Link = GetFirstNode (&mResetNotification.ResetNotifies)
267 ; !IsNull (&mResetNotification.ResetNotifies, Link)
268 ; Link = GetNextNode (&mResetNotification.ResetNotifies, Link)
269 ) {
270 Entry = RESET_NOTIFY_ENTRY_FROM_LINK (Link);
271 Entry->ResetNotify (ResetType, ResetStatus, DataSize, ResetData);
272 }
273 //
274 // call reset notification functions registered through the
275 // EDKII_PLATFORM_SPECIFIC_RESET_HANDLER_PROTOCOL.
276 //
277 for ( Link = GetFirstNode (&mPlatformSpecificResetHandler.ResetNotifies)
278 ; !IsNull (&mPlatformSpecificResetHandler.ResetNotifies, Link)
279 ; Link = GetNextNode (&mPlatformSpecificResetHandler.ResetNotifies, Link)
280 ) {
281 Entry = RESET_NOTIFY_ENTRY_FROM_LINK (Link);
282 Entry->ResetNotify (ResetType, ResetStatus, DataSize, ResetData);
283 }
284 }
285 } else {
286 ASSERT (ResetType < ARRAY_SIZE (mResetTypeStr));
287 DEBUG ((DEBUG_ERROR, "DXE ResetSystem2: Maximum reset call depth is met. Use the current reset type: %s!\n", mResetTypeStr[ResetType]));
288 }
289
290 switch (ResetType) {
291 case EfiResetWarm:
292
293 ResetWarm ();
294 break;
295
296 case EfiResetCold:
297 ResetCold ();
298 break;
299
300 case EfiResetShutdown:
301 ResetShutdown ();
302 return ;
303
304 case EfiResetPlatformSpecific:
305 ResetPlatformSpecific (DataSize, ResetData);
306 return;
307
308 default:
309 return ;
310 }
311
312 //
313 // Given we should have reset getting here would be bad
314 //
315 ASSERT (FALSE);
316 }