2 Implementation of Reset2, ResetFilter and ResetHandler PPIs.
4 Copyright (c) 2017 - 2019, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "ResetSystem.h"
12 GLOBAL_REMOVE_IF_UNREFERENCED CHAR16
*mResetTypeStr
[] = {
13 L
"Cold", L
"Warm", L
"Shutdown", L
"PlatformSpecific"
16 EFI_PEI_RESET2_PPI mPpiReset2
= {
20 EFI_GUID
*mProcessingOrder
[] = {
21 &gEdkiiPlatformSpecificResetFilterPpiGuid
,
22 &gEdkiiPlatformSpecificResetNotificationPpiGuid
,
23 &gEdkiiPlatformSpecificResetHandlerPpiGuid
26 RESET_FILTER_INSTANCE mResetFilter
= {
31 &gEdkiiPlatformSpecificResetFilterPpiGuid
34 RESET_FILTER_INSTANCE mResetNotification
= {
39 &gEdkiiPlatformSpecificResetNotificationPpiGuid
42 RESET_FILTER_INSTANCE mResetHandler
= {
47 &gEdkiiPlatformSpecificResetHandlerPpiGuid
50 EFI_PEI_PPI_DESCRIPTOR mPpiListReset
[] = {
52 EFI_PEI_PPI_DESCRIPTOR_PPI
,
53 &gEfiPeiReset2PpiGuid
,
57 EFI_PEI_PPI_DESCRIPTOR_PPI
,
58 &gEdkiiPlatformSpecificResetFilterPpiGuid
,
59 &mResetFilter
.ResetFilter
62 EFI_PEI_PPI_DESCRIPTOR_PPI
,
63 &gEdkiiPlatformSpecificResetNotificationPpiGuid
,
64 &mResetNotification
.ResetFilter
67 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
68 &gEdkiiPlatformSpecificResetHandlerPpiGuid
,
69 &mResetHandler
.ResetFilter
74 Register a notification function to be called when ResetSystem() is called.
76 The RegisterResetNotify() function registers a notification function that is called when
77 ResetSystem() is called and prior to completing the reset of the platform.
78 The registered functions must not perform a platform reset themselves. These
79 notifications are intended only for the notification of components which may need some
80 special-purpose maintenance prior to the platform resetting.
81 The list of registered reset notification functions are processed if ResetSystem()is called
82 before ExitBootServices(). The list of registered reset notification functions is ignored if
83 ResetSystem() is called after ExitBootServices().
85 @param[in] This A pointer to the EFI_RESET_NOTIFICATION_PROTOCOL instance.
86 @param[in] ResetFunction Points to the function to be called when a ResetSystem() is executed.
88 @retval EFI_SUCCESS The reset notification function was successfully registered.
89 @retval EFI_INVALID_PARAMETER ResetFunction is NULL.
90 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to register the reset notification function.
91 @retval EFI_ALREADY_STARTED The reset notification function specified by ResetFunction has already been registered.
97 IN EDKII_PLATFORM_SPECIFIC_RESET_FILTER_PPI
*This
,
98 IN EFI_RESET_SYSTEM ResetFunction
101 RESET_FILTER_INSTANCE
*ResetFilter
;
102 RESET_FILTER_LIST
*List
;
106 if (ResetFunction
== NULL
) {
107 return EFI_INVALID_PARAMETER
;
110 ResetFilter
= (RESET_FILTER_INSTANCE
*) This
;
111 ASSERT (CompareGuid (ResetFilter
->Guid
, &gEdkiiPlatformSpecificResetFilterPpiGuid
) ||
112 CompareGuid (ResetFilter
->Guid
, &gEdkiiPlatformSpecificResetNotificationPpiGuid
) ||
113 CompareGuid (ResetFilter
->Guid
, &gEdkiiPlatformSpecificResetHandlerPpiGuid
)
116 Hob
= GetFirstGuidHob (ResetFilter
->Guid
);
119 // When the GUIDed HOB doesn't exist, create it.
121 List
= (RESET_FILTER_LIST
*)BuildGuidHob (
123 sizeof (RESET_FILTER_LIST
) + sizeof (EFI_RESET_SYSTEM
) * PcdGet32 (PcdMaximumPeiResetNotifies
)
126 return EFI_OUT_OF_RESOURCES
;
128 List
->Signature
= RESET_FILTER_LIST_SIGNATURE
;
129 List
->Count
= PcdGet32 (PcdMaximumPeiResetNotifies
);
130 ZeroMem (List
->ResetFilters
, sizeof (EFI_RESET_SYSTEM
) * List
->Count
);
131 List
->ResetFilters
[0] = ResetFunction
;
134 List
= (RESET_FILTER_LIST
*)GET_GUID_HOB_DATA (Hob
);
135 ASSERT (List
->Signature
== RESET_FILTER_LIST_SIGNATURE
);
137 // Firstly check whether the ResetFunction is already registerred.
139 for (Index
= 0; Index
< List
->Count
; Index
++) {
140 if (List
->ResetFilters
[Index
] == ResetFunction
) {
144 if (Index
!= List
->Count
) {
145 return EFI_ALREADY_STARTED
;
149 // Secondly find the first free slot.
151 for (Index
= 0; Index
< List
->Count
; Index
++) {
152 if (List
->ResetFilters
[Index
] == NULL
) {
157 if (Index
== List
->Count
) {
158 return EFI_OUT_OF_RESOURCES
;
160 List
->ResetFilters
[Index
] = ResetFunction
;
166 Unregister a notification function.
168 The UnregisterResetNotify() function removes the previously registered
169 notification using RegisterResetNotify().
171 @param[in] This A pointer to the EFI_RESET_NOTIFICATION_PROTOCOL instance.
172 @param[in] ResetFunction The pointer to the ResetFunction being unregistered.
174 @retval EFI_SUCCESS The reset notification function was unregistered.
175 @retval EFI_INVALID_PARAMETER ResetFunction is NULL.
176 @retval EFI_INVALID_PARAMETER The reset notification function specified by ResetFunction was not previously
177 registered using RegisterResetNotify().
182 UnregisterResetNotify (
183 IN EDKII_PLATFORM_SPECIFIC_RESET_FILTER_PPI
*This
,
184 IN EFI_RESET_SYSTEM ResetFunction
188 RESET_FILTER_INSTANCE
*ResetFilter
;
189 RESET_FILTER_LIST
*List
;
193 if (ResetFunction
== NULL
) {
194 return EFI_INVALID_PARAMETER
;
197 ResetFilter
= (RESET_FILTER_INSTANCE
*)This
;
198 ASSERT (CompareGuid (ResetFilter
->Guid
, &gEdkiiPlatformSpecificResetFilterPpiGuid
) ||
199 CompareGuid (ResetFilter
->Guid
, &gEdkiiPlatformSpecificResetNotificationPpiGuid
) ||
200 CompareGuid (ResetFilter
->Guid
, &gEdkiiPlatformSpecificResetHandlerPpiGuid
)
203 Hob
= GetFirstGuidHob (ResetFilter
->Guid
);
205 return EFI_INVALID_PARAMETER
;
208 List
= (RESET_FILTER_LIST
*)GET_GUID_HOB_DATA (Hob
);
209 ASSERT (List
->Signature
== RESET_FILTER_LIST_SIGNATURE
);
210 for (Index
= 0; Index
< List
->Count
; Index
++) {
211 if (List
->ResetFilters
[Index
] == ResetFunction
) {
216 if (Index
== List
->Count
) {
217 return EFI_INVALID_PARAMETER
;
220 List
->ResetFilters
[Index
] = NULL
;
226 The PEIM's entry point.
228 It initializes the Reset2, ResetFilter and ResetHandler PPIs.
230 @param[in] FileHandle Handle of the file being invoked.
231 @param[in] PeiServices Describes the list of possible PEI Services.
233 @retval EFI_SUCCESS The entry point is executed successfully.
234 @retval EFI_ALREADY_STARTED The Reset2 PPI was already installed.
235 @retval others Status code returned from PeiServicesInstallPpi().
240 InitializeResetSystem (
241 IN EFI_PEI_FILE_HANDLE FileHandle
,
242 IN CONST EFI_PEI_SERVICES
**PeiServices
248 Status
= PeiServicesLocatePpi (&gEfiPeiReset2PpiGuid
, 0, NULL
, (VOID
**)&Ppi
);
249 if (Status
!= EFI_NOT_FOUND
) {
250 return EFI_ALREADY_STARTED
;
253 PeiServicesInstallPpi (mPpiListReset
);
259 Resets the entire platform.
261 @param[in] ResetType The type of reset to perform.
262 @param[in] ResetStatus The status code for the reset.
263 @param[in] DataSize The size, in bytes, of ResetData.
264 @param[in] ResetData For a ResetType of EfiResetCold, EfiResetWarm, or
265 EfiResetShutdown the data buffer starts with a Null-terminated
266 string, optionally followed by additional binary data.
267 The string is a description that the caller may use to further
268 indicate the reason for the system reset.
269 For a ResetType of EfiResetPlatformSpecific the data buffer
270 also starts with a Null-terminated string that is followed
271 by an EFI_GUID that describes the specific type of reset to perform.
276 IN EFI_RESET_TYPE ResetType
,
277 IN EFI_STATUS ResetStatus
,
279 IN VOID
*ResetData OPTIONAL
284 RESET_FILTER_LIST
*List
;
286 UINT8 RecursionDepth
;
287 UINT8
*RecursionDepthPointer
;
290 // The recursion depth is stored in GUIDed HOB using gEfiCallerIdGuid.
292 Hob
= GetFirstGuidHob (&gEfiCallerIdGuid
);
295 RecursionDepthPointer
= BuildGuidDataHob (&gEfiCallerIdGuid
, &RecursionDepth
, sizeof (RecursionDepth
));
297 RecursionDepthPointer
= (UINT8
*)GET_GUID_HOB_DATA (Hob
);
300 // Only do REPORT_STATUS_CODE() on first call to ResetSystem()
302 if (*RecursionDepthPointer
== 0) {
304 // Indicate reset system PEI service is called.
306 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_PEI_SERVICE
| EFI_SW_PS_PC_RESET_SYSTEM
));
310 // Increase the call depth
312 (*RecursionDepthPointer
)++;
313 DEBUG ((DEBUG_INFO
, "PEI ResetSystem2: Reset call depth = %d.\n", *RecursionDepthPointer
));
315 if (*RecursionDepthPointer
<= MAX_RESET_NOTIFY_DEPTH
) {
317 // Iteratively call Reset Filters and Reset Handlers.
319 for (OrderIndex
= 0; OrderIndex
< ARRAY_SIZE (mProcessingOrder
); OrderIndex
++) {
320 Hob
= GetFirstGuidHob (mProcessingOrder
[OrderIndex
]);
322 List
= (RESET_FILTER_LIST
*)GET_GUID_HOB_DATA (Hob
);
323 ASSERT (List
->Signature
== RESET_FILTER_LIST_SIGNATURE
);
325 for (Index
= 0; Index
< List
->Count
; Index
++) {
326 if (List
->ResetFilters
[Index
] != NULL
) {
327 List
->ResetFilters
[Index
] (ResetType
, ResetStatus
, DataSize
, ResetData
);
333 ASSERT (ResetType
< ARRAY_SIZE (mResetTypeStr
));
334 DEBUG ((DEBUG_ERROR
, "PEI ResetSystem2: Maximum reset call depth is met. Use the current reset type: %s!\n", mResetTypeStr
[ResetType
]));
346 case EfiResetShutdown
:
350 case EfiResetPlatformSpecific
:
351 ResetPlatformSpecific (DataSize
, ResetData
);
359 // Given we should have reset getting here would be bad