]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/Notify.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / Notify.c
CommitLineData
e42e9404 1/** @file\r
2 Support functions for UEFI protocol notification infrastructure.\r
3\r
d1102dba 4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
e42e9404 6\r
7**/\r
8\r
9#include "PiSmmCore.h"\r
10\r
11/**\r
12 Signal event for every protocol in protocol entry.\r
13\r
14 @param Prot Protocol interface\r
15\r
16**/\r
17VOID\r
18SmmNotifyProtocol (\r
19 IN PROTOCOL_INTERFACE *Prot\r
20 )\r
21{\r
22 PROTOCOL_ENTRY *ProtEntry;\r
23 PROTOCOL_NOTIFY *ProtNotify;\r
24 LIST_ENTRY *Link;\r
25\r
26 ProtEntry = Prot->Protocol;\r
1436aea4
MK
27 for (Link = ProtEntry->Notify.ForwardLink; Link != &ProtEntry->Notify; Link = Link->ForwardLink) {\r
28 ProtNotify = CR (Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);\r
e42e9404 29 ProtNotify->Function (&ProtEntry->ProtocolID, Prot->Interface, Prot->Handle);\r
30 }\r
31}\r
32\r
33/**\r
34 Removes Protocol from the protocol list (but not the handle list).\r
35\r
36 @param Handle The handle to remove protocol on.\r
37 @param Protocol GUID of the protocol to be moved\r
38 @param Interface The interface of the protocol\r
39\r
40 @return Protocol Entry\r
41\r
42**/\r
43PROTOCOL_INTERFACE *\r
44SmmRemoveInterfaceFromProtocol (\r
45 IN IHANDLE *Handle,\r
46 IN EFI_GUID *Protocol,\r
47 IN VOID *Interface\r
48 )\r
49{\r
50 PROTOCOL_INTERFACE *Prot;\r
51 PROTOCOL_NOTIFY *ProtNotify;\r
52 PROTOCOL_ENTRY *ProtEntry;\r
53 LIST_ENTRY *Link;\r
54\r
55 Prot = SmmFindProtocolInterface (Handle, Protocol, Interface);\r
56 if (Prot != NULL) {\r
e42e9404 57 ProtEntry = Prot->Protocol;\r
58\r
59 //\r
60 // If there's a protocol notify location pointing to this entry, back it up one\r
61 //\r
1436aea4
MK
62 for (Link = ProtEntry->Notify.ForwardLink; Link != &ProtEntry->Notify; Link = Link->ForwardLink) {\r
63 ProtNotify = CR (Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);\r
e42e9404 64\r
65 if (ProtNotify->Position == &Prot->ByProtocol) {\r
66 ProtNotify->Position = Prot->ByProtocol.BackLink;\r
67 }\r
68 }\r
69\r
70 //\r
71 // Remove the protocol interface entry\r
72 //\r
73 RemoveEntryList (&Prot->ByProtocol);\r
74 }\r
75\r
76 return Prot;\r
77}\r
78\r
79/**\r
80 Add a new protocol notification record for the request protocol.\r
81\r
82 @param Protocol The requested protocol to add the notify\r
83 registration\r
84 @param Function Points to the notification function\r
85 @param Registration Returns the registration record\r
86\r
e42e9404 87 @retval EFI_SUCCESS Successfully returned the registration record\r
aeb4c944
JF
88 that has been added or unhooked\r
89 @retval EFI_INVALID_PARAMETER Protocol is NULL or Registration is NULL\r
50903aa5
JF
90 @retval EFI_OUT_OF_RESOURCES Not enough memory resource to finish the request\r
91 @retval EFI_NOT_FOUND If the registration is not found when Function == NULL\r
e42e9404 92\r
93**/\r
94EFI_STATUS\r
95EFIAPI\r
96SmmRegisterProtocolNotify (\r
97 IN CONST EFI_GUID *Protocol,\r
98 IN EFI_SMM_NOTIFY_FN Function,\r
99 OUT VOID **Registration\r
100 )\r
101{\r
102 PROTOCOL_ENTRY *ProtEntry;\r
103 PROTOCOL_NOTIFY *ProtNotify;\r
104 LIST_ENTRY *Link;\r
105 EFI_STATUS Status;\r
106\r
1436aea4 107 if ((Protocol == NULL) || (Registration == NULL)) {\r
e42e9404 108 return EFI_INVALID_PARAMETER;\r
109 }\r
110\r
50903aa5 111 if (Function == NULL) {\r
d1102dba 112 //\r
50903aa5
JF
113 // Get the protocol entry per Protocol\r
114 //\r
1436aea4 115 ProtEntry = SmmFindProtocolEntry ((EFI_GUID *)Protocol, FALSE);\r
50903aa5 116 if (ProtEntry != NULL) {\r
1436aea4 117 ProtNotify = (PROTOCOL_NOTIFY *)*Registration;\r
50903aa5
JF
118 for (Link = ProtEntry->Notify.ForwardLink;\r
119 Link != &ProtEntry->Notify;\r
1436aea4
MK
120 Link = Link->ForwardLink)\r
121 {\r
50903aa5 122 //\r
d1102dba 123 // Compare the notification record\r
50903aa5 124 //\r
1436aea4 125 if (ProtNotify == (CR (Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE))) {\r
50903aa5
JF
126 //\r
127 // If Registration is an existing registration, then unhook it\r
128 //\r
129 ProtNotify->Signature = 0;\r
130 RemoveEntryList (&ProtNotify->Link);\r
131 FreePool (ProtNotify);\r
132 return EFI_SUCCESS;\r
133 }\r
134 }\r
135 }\r
1436aea4 136\r
50903aa5
JF
137 //\r
138 // If the registration is not found\r
139 //\r
140 return EFI_NOT_FOUND;\r
d1102dba 141 }\r
50903aa5 142\r
e42e9404 143 ProtNotify = NULL;\r
144\r
145 //\r
146 // Get the protocol entry to add the notification too\r
147 //\r
1436aea4 148 ProtEntry = SmmFindProtocolEntry ((EFI_GUID *)Protocol, TRUE);\r
e42e9404 149 if (ProtEntry != NULL) {\r
150 //\r
151 // Find whether notification already exist\r
152 //\r
153 for (Link = ProtEntry->Notify.ForwardLink;\r
154 Link != &ProtEntry->Notify;\r
1436aea4
MK
155 Link = Link->ForwardLink)\r
156 {\r
157 ProtNotify = CR (Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);\r
e42e9404 158 if (CompareGuid (&ProtNotify->Protocol->ProtocolID, Protocol) &&\r
1436aea4
MK
159 (ProtNotify->Function == Function))\r
160 {\r
e42e9404 161 //\r
162 // Notification already exist\r
163 //\r
164 *Registration = ProtNotify;\r
165\r
166 return EFI_SUCCESS;\r
167 }\r
168 }\r
169\r
170 //\r
171 // Allocate a new notification record\r
172 //\r
1436aea4 173 ProtNotify = AllocatePool (sizeof (PROTOCOL_NOTIFY));\r
e42e9404 174 if (ProtNotify != NULL) {\r
175 ProtNotify->Signature = PROTOCOL_NOTIFY_SIGNATURE;\r
1436aea4
MK
176 ProtNotify->Protocol = ProtEntry;\r
177 ProtNotify->Function = Function;\r
e42e9404 178 //\r
179 // Start at the ending\r
180 //\r
181 ProtNotify->Position = ProtEntry->Protocols.BackLink;\r
182\r
183 InsertTailList (&ProtEntry->Notify, &ProtNotify->Link);\r
184 }\r
185 }\r
186\r
187 //\r
188 // Done. If we have a protocol notify entry, then return it.\r
189 // Otherwise, we must have run out of resources trying to add one\r
190 //\r
191 Status = EFI_OUT_OF_RESOURCES;\r
192 if (ProtNotify != NULL) {\r
193 *Registration = ProtNotify;\r
1436aea4 194 Status = EFI_SUCCESS;\r
e42e9404 195 }\r
1436aea4 196\r
e42e9404 197 return Status;\r
198}\r