]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Universal / ReportStatusCodeRouter / Smm / ReportStatusCodeRouterSmm.c
1 /** @file
2 Report Status Code Router Driver which produces SMM Report Stataus Code Handler Protocol
3 and SMM Status Code Protocol.
4
5 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "ReportStatusCodeRouterSmm.h"
11
12 LIST_ENTRY mCallbackListHead = INITIALIZE_LIST_HEAD_VARIABLE (mCallbackListHead);
13
14 //
15 // Report operation nest status.
16 // If it is set, then the report operation has nested.
17 //
18 UINT32 mStatusCodeNestStatus = 0;
19
20 EFI_SMM_STATUS_CODE_PROTOCOL mSmmStatusCodeProtocol = {
21 ReportDispatcher
22 };
23
24 EFI_SMM_RSC_HANDLER_PROTOCOL mSmmRscHandlerProtocol = {
25 Register,
26 Unregister
27 };
28
29 /**
30 Register the callback function for ReportStatusCode() notification.
31
32 When this function is called the function pointer is added to an internal list and any future calls to
33 ReportStatusCode() will be forwarded to the Callback function.
34
35 @param[in] Callback A pointer to a function of type EFI_PEI_RSC_HANDLER_CALLBACK that is called
36 when a call to ReportStatusCode() occurs.
37
38 @retval EFI_SUCCESS Function was successfully registered.
39 @retval EFI_INVALID_PARAMETER The callback function was NULL.
40 @retval EFI_OUT_OF_RESOURCES The internal buffer ran out of space. No more functions can be
41 registered.
42 @retval EFI_ALREADY_STARTED The function was already registered. It can't be registered again.
43
44 **/
45 EFI_STATUS
46 EFIAPI
47 Register (
48 IN EFI_SMM_RSC_HANDLER_CALLBACK Callback
49 )
50 {
51 LIST_ENTRY *Link;
52 SMM_RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
53
54 if (Callback == NULL) {
55 return EFI_INVALID_PARAMETER;
56 }
57
58 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
59 CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node, SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
60 if (CallbackEntry->RscHandlerCallback == Callback) {
61 //
62 // If the function was already registered. It can't be registered again.
63 //
64 return EFI_ALREADY_STARTED;
65 }
66 }
67
68 CallbackEntry = (SMM_RSC_HANDLER_CALLBACK_ENTRY *)AllocatePool (sizeof (SMM_RSC_HANDLER_CALLBACK_ENTRY));
69 ASSERT (CallbackEntry != NULL);
70
71 CallbackEntry->Signature = SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE;
72 CallbackEntry->RscHandlerCallback = Callback;
73
74 InsertTailList (&mCallbackListHead, &CallbackEntry->Node);
75
76 return EFI_SUCCESS;
77 }
78
79 /**
80 Remove a previously registered callback function from the notification list.
81
82 ReportStatusCode() messages will no longer be forwarded to the Callback function.
83
84 @param[in] Callback A pointer to a function of type EFI_PEI_RSC_HANDLER_CALLBACK that is to be
85 unregistered.
86
87 @retval EFI_SUCCESS The function was successfully unregistered.
88 @retval EFI_INVALID_PARAMETER The callback function was NULL.
89 @retval EFI_NOT_FOUND The callback function was not found to be unregistered.
90
91 **/
92 EFI_STATUS
93 EFIAPI
94 Unregister (
95 IN EFI_SMM_RSC_HANDLER_CALLBACK Callback
96 )
97 {
98 LIST_ENTRY *Link;
99 SMM_RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
100
101 if (Callback == NULL) {
102 return EFI_INVALID_PARAMETER;
103 }
104
105 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
106 CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node, SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
107 if (CallbackEntry->RscHandlerCallback == Callback) {
108 //
109 // If the function is found in list, delete it and return.
110 //
111 RemoveEntryList (&CallbackEntry->Node);
112 FreePool (CallbackEntry);
113 return EFI_SUCCESS;
114 }
115 }
116
117 return EFI_NOT_FOUND;
118 }
119
120
121 /**
122 Provides an interface that a software module can call to report a status code.
123
124 @param This EFI_SMM_STATUS_CODE_PROTOCOL instance.
125 @param Type Indicates the type of status code being reported.
126 @param Value Describes the current status of a hardware or software entity.
127 This included information about the class and subclass that is used to
128 classify the entity as well as an operation.
129 @param Instance The enumeration of a hardware or software entity within
130 the system. Valid instance numbers start with 1.
131 @param CallerId This optional parameter may be used to identify the caller.
132 This parameter allows the status code driver to apply different rules to
133 different callers.
134 @param Data This optional parameter may be used to pass additional data.
135
136 @retval EFI_SUCCESS The function completed successfully
137 @retval EFI_DEVICE_ERROR The function should not be completed due to a device error.
138
139 **/
140 EFI_STATUS
141 EFIAPI
142 ReportDispatcher (
143 IN CONST EFI_SMM_STATUS_CODE_PROTOCOL *This,
144 IN EFI_STATUS_CODE_TYPE Type,
145 IN EFI_STATUS_CODE_VALUE Value,
146 IN UINT32 Instance,
147 IN CONST EFI_GUID *CallerId OPTIONAL,
148 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
149 )
150 {
151 LIST_ENTRY *Link;
152 SMM_RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
153
154 //
155 // Use atom operation to avoid the reentant of report.
156 // If current status is not zero, then the function is reentrancy.
157 //
158 if (InterlockedCompareExchange32 (&mStatusCodeNestStatus, 0, 1) == 1) {
159 return EFI_DEVICE_ERROR;
160 }
161
162 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link);) {
163 CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node, SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
164 //
165 // The handler may remove itself, so get the next handler in advance.
166 //
167 Link = GetNextNode (&mCallbackListHead, Link);
168 CallbackEntry->RscHandlerCallback (
169 Type,
170 Value,
171 Instance,
172 (EFI_GUID*)CallerId,
173 Data
174 );
175
176 }
177
178 //
179 // Restore the nest status of report
180 //
181 InterlockedCompareExchange32 (&mStatusCodeNestStatus, 1, 0);
182
183 return EFI_SUCCESS;
184 }
185
186 /**
187 Entry point of Generic Status Code Driver.
188
189 This function is the entry point of SMM Status Code Router .
190 It produces SMM Report Stataus Code Handler and Status Code protocol.
191
192 @param ImageHandle The firmware allocated handle for the EFI image.
193 @param SystemTable A pointer to the EFI System Table.
194
195 @retval EFI_SUCCESS The entry point is executed successfully.
196
197 **/
198 EFI_STATUS
199 EFIAPI
200 GenericStatusCodeSmmEntry (
201 IN EFI_HANDLE ImageHandle,
202 IN EFI_SYSTEM_TABLE *SystemTable
203 )
204 {
205 EFI_STATUS Status;
206 EFI_HANDLE Handle;
207
208 Handle = NULL;
209
210 //
211 // Install SmmRscHandler Protocol
212 //
213 Status = gSmst->SmmInstallProtocolInterface (
214 &Handle,
215 &gEfiSmmRscHandlerProtocolGuid,
216 EFI_NATIVE_INTERFACE,
217 &mSmmRscHandlerProtocol
218 );
219 ASSERT_EFI_ERROR (Status);
220
221 //
222 // Install SmmStatusCode Protocol
223 //
224 Status = gSmst->SmmInstallProtocolInterface (
225 &Handle,
226 &gEfiSmmStatusCodeProtocolGuid,
227 EFI_NATIVE_INTERFACE,
228 &mSmmStatusCodeProtocol
229 );
230 ASSERT_EFI_ERROR (Status);
231
232 return EFI_SUCCESS;
233 }