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