2 Implementation of Reset2, ResetFilter and ResetHandler PPIs.
4 Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>
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
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.
16 #include "ResetSystem.h"
18 GLOBAL_REMOVE_IF_UNREFERENCED CHAR16
*mResetTypeStr
[] = {
19 L
"Cold", L
"Warm", L
"Shutdown", L
"PlatformSpecific"
22 EFI_PEI_RESET2_PPI mPpiReset2
= {
26 EFI_GUID
*mProcessingOrder
[] = {
27 &gEdkiiPlatformSpecificResetFilterPpiGuid
,
28 &gEdkiiPlatformSpecificResetHandlerPpiGuid
31 RESET_FILTER_INSTANCE mResetFilter
= {
36 &gEdkiiPlatformSpecificResetFilterPpiGuid
39 RESET_FILTER_INSTANCE mResetHandler
= {
44 &gEdkiiPlatformSpecificResetHandlerPpiGuid
47 EFI_PEI_PPI_DESCRIPTOR mPpiListReset
[] = {
49 EFI_PEI_PPI_DESCRIPTOR_PPI
,
50 &gEfiPeiReset2PpiGuid
,
54 EFI_PEI_PPI_DESCRIPTOR_PPI
,
55 &gEdkiiPlatformSpecificResetFilterPpiGuid
,
56 &mResetFilter
.ResetFilter
59 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
60 &gEdkiiPlatformSpecificResetHandlerPpiGuid
,
61 &mResetHandler
.ResetFilter
66 Register a notification function to be called when ResetSystem() is called.
68 The RegisterResetNotify() function registers a notification function that is called when
69 ResetSystem() is called and prior to completing the reset of the platform.
70 The registered functions must not perform a platform reset themselves. These
71 notifications are intended only for the notification of components which may need some
72 special-purpose maintenance prior to the platform resetting.
73 The list of registered reset notification functions are processed if ResetSystem()is called
74 before ExitBootServices(). The list of registered reset notification functions is ignored if
75 ResetSystem() is called after ExitBootServices().
77 @param[in] This A pointer to the EFI_RESET_NOTIFICATION_PROTOCOL instance.
78 @param[in] ResetFunction Points to the function to be called when a ResetSystem() is executed.
80 @retval EFI_SUCCESS The reset notification function was successfully registered.
81 @retval EFI_INVALID_PARAMETER ResetFunction is NULL.
82 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to register the reset notification function.
83 @retval EFI_ALREADY_STARTED The reset notification function specified by ResetFunction has already been registered.
89 IN EDKII_PLATFORM_SPECIFIC_RESET_FILTER_PPI
*This
,
90 IN EFI_RESET_SYSTEM ResetFunction
93 RESET_FILTER_INSTANCE
*ResetFilter
;
94 RESET_FILTER_LIST
*List
;
98 if (ResetFunction
== NULL
) {
99 return EFI_INVALID_PARAMETER
;
102 ResetFilter
= (RESET_FILTER_INSTANCE
*) This
;
103 ASSERT (CompareGuid (ResetFilter
->Guid
, &gEdkiiPlatformSpecificResetFilterPpiGuid
) ||
104 CompareGuid (ResetFilter
->Guid
, &gEdkiiPlatformSpecificResetHandlerPpiGuid
)
107 Hob
= GetFirstGuidHob (ResetFilter
->Guid
);
110 // When the GUIDed HOB doesn't exist, create it.
112 List
= (RESET_FILTER_LIST
*)BuildGuidHob (
114 sizeof (RESET_FILTER_LIST
) + sizeof (EFI_RESET_SYSTEM
) * PcdGet32 (PcdMaximumPeiResetNotifies
)
117 return EFI_OUT_OF_RESOURCES
;
119 List
->Signature
= RESET_FILTER_LIST_SIGNATURE
;
120 List
->Count
= PcdGet32 (PcdMaximumPeiResetNotifies
);
121 ZeroMem (List
->ResetFilters
, sizeof (EFI_RESET_SYSTEM
) * List
->Count
);
122 List
->ResetFilters
[0] = ResetFunction
;
125 List
= (RESET_FILTER_LIST
*)GET_GUID_HOB_DATA (Hob
);
126 ASSERT (List
->Signature
== RESET_FILTER_LIST_SIGNATURE
);
128 // Firstly check whether the ResetFunction is already registerred.
130 for (Index
= 0; Index
< List
->Count
; Index
++) {
131 if (List
->ResetFilters
[Index
] == ResetFunction
) {
135 if (Index
!= List
->Count
) {
136 return EFI_ALREADY_STARTED
;
140 // Secondly find the first free slot.
142 for (Index
= 0; Index
< List
->Count
; Index
++) {
143 if (List
->ResetFilters
[Index
] == NULL
) {
148 if (Index
== List
->Count
) {
149 return EFI_OUT_OF_RESOURCES
;
151 List
->ResetFilters
[Index
] = ResetFunction
;
157 Unregister a notification function.
159 The UnregisterResetNotify() function removes the previously registered
160 notification using RegisterResetNotify().
162 @param[in] This A pointer to the EFI_RESET_NOTIFICATION_PROTOCOL instance.
163 @param[in] ResetFunction The pointer to the ResetFunction being unregistered.
165 @retval EFI_SUCCESS The reset notification function was unregistered.
166 @retval EFI_INVALID_PARAMETER ResetFunction is NULL.
167 @retval EFI_INVALID_PARAMETER The reset notification function specified by ResetFunction was not previously
168 registered using RegisterResetNotify().
173 UnregisterResetNotify (
174 IN EDKII_PLATFORM_SPECIFIC_RESET_FILTER_PPI
*This
,
175 IN EFI_RESET_SYSTEM ResetFunction
179 RESET_FILTER_INSTANCE
*ResetFilter
;
180 RESET_FILTER_LIST
*List
;
184 if (ResetFunction
== NULL
) {
185 return EFI_INVALID_PARAMETER
;
188 ResetFilter
= (RESET_FILTER_INSTANCE
*)This
;
189 ASSERT (CompareGuid (ResetFilter
->Guid
, &gEdkiiPlatformSpecificResetFilterPpiGuid
) ||
190 CompareGuid (ResetFilter
->Guid
, &gEdkiiPlatformSpecificResetHandlerPpiGuid
)
193 Hob
= GetFirstGuidHob (ResetFilter
->Guid
);
195 return EFI_INVALID_PARAMETER
;
198 List
= (RESET_FILTER_LIST
*)GET_GUID_HOB_DATA (Hob
);
199 ASSERT (List
->Signature
== RESET_FILTER_LIST_SIGNATURE
);
200 for (Index
= 0; Index
< List
->Count
; Index
++) {
201 if (List
->ResetFilters
[Index
] == ResetFunction
) {
206 if (Index
== List
->Count
) {
207 return EFI_INVALID_PARAMETER
;
210 List
->ResetFilters
[Index
] = NULL
;
216 The PEIM's entry point.
218 It initializes the Reset2, ResetFilter and ResetHandler PPIs.
220 @param[in] FileHandle Handle of the file being invoked.
221 @param[in] PeiServices Describes the list of possible PEI Services.
223 @retval EFI_SUCCESS The entry point is executed successfully.
224 @retval EFI_ALREADY_STARTED The Reset2 PPI was already installed.
225 @retval others Status code returned from PeiServicesInstallPpi().
230 InitializeResetSystem (
231 IN EFI_PEI_FILE_HANDLE FileHandle
,
232 IN CONST EFI_PEI_SERVICES
**PeiServices
238 Status
= PeiServicesLocatePpi (&gEfiPeiReset2PpiGuid
, 0, NULL
, (VOID
**)&Ppi
);
239 if (Status
!= EFI_NOT_FOUND
) {
240 return EFI_ALREADY_STARTED
;
243 PeiServicesInstallPpi (mPpiListReset
);
249 Resets the entire platform.
251 @param[in] ResetType The type of reset to perform.
252 @param[in] ResetStatus The status code for the reset.
253 @param[in] DataSize The size, in bytes, of ResetData.
254 @param[in] ResetData For a ResetType of EfiResetCold, EfiResetWarm, or
255 EfiResetShutdown the data buffer starts with a Null-terminated
256 string, optionally followed by additional binary data.
257 The string is a description that the caller may use to further
258 indicate the reason for the system reset. ResetData is only
259 valid if ResetStatus is something other than EFI_SUCCESS
260 unless the ResetType is EfiResetPlatformSpecific
261 where a minimum amount of ResetData is always required.
262 For a ResetType of EfiResetPlatformSpecific the data buffer
263 also starts with a Null-terminated string that is followed
264 by an EFI_GUID that describes the specific type of reset to perform.
269 IN EFI_RESET_TYPE ResetType
,
270 IN EFI_STATUS ResetStatus
,
272 IN VOID
*ResetData OPTIONAL
277 RESET_FILTER_LIST
*List
;
279 UINT8 RecursionDepth
;
280 UINT8
*RecursionDepthPointer
;
283 // The recursion depth is stored in GUIDed HOB using gEfiCallerIdGuid.
285 Hob
= GetFirstGuidHob (&gEfiCallerIdGuid
);
288 RecursionDepthPointer
= BuildGuidDataHob (&gEfiCallerIdGuid
, &RecursionDepth
, sizeof (RecursionDepth
));
290 RecursionDepthPointer
= (UINT8
*)GET_GUID_HOB_DATA (Hob
);
293 // Only do REPORT_STATUS_CODE() on first call to ResetSystem()
295 if (*RecursionDepthPointer
== 0) {
297 // Indicate reset system PEI service is called.
299 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_PEI_SERVICE
| EFI_SW_PS_PC_RESET_SYSTEM
));
303 // Increase the call depth
305 (*RecursionDepthPointer
)++;
306 DEBUG ((DEBUG_INFO
, "PEI ResetSystem2: Reset call depth = %d.\n", *RecursionDepthPointer
));
308 if (*RecursionDepthPointer
<= MAX_RESET_NOTIFY_DEPTH
) {
310 // Iteratively call Reset Filters and Reset Handlers.
312 for (OrderIndex
= 0; OrderIndex
< ARRAY_SIZE (mProcessingOrder
); OrderIndex
++) {
313 Hob
= GetFirstGuidHob (mProcessingOrder
[OrderIndex
]);
315 List
= (RESET_FILTER_LIST
*)GET_GUID_HOB_DATA (Hob
);
316 ASSERT (List
->Signature
== RESET_FILTER_LIST_SIGNATURE
);
318 for (Index
= 0; Index
< List
->Count
; Index
++) {
319 if (List
->ResetFilters
[Index
] != NULL
) {
320 List
->ResetFilters
[Index
] (ResetType
, ResetStatus
, DataSize
, ResetData
);
326 ASSERT (ResetType
< ARRAY_SIZE (mResetTypeStr
));
327 DEBUG ((DEBUG_ERROR
, "PEI ResetSystem2: Maximum reset call depth is met. Use the current reset type: %s!\n", mResetTypeStr
[ResetType
]));
339 case EfiResetShutdown
:
343 case EfiResetPlatformSpecific
:
344 ResetPlatformSpecific (DataSize
, ResetData
);
352 // Given we should have reset getting here would be bad