Coverage Report

Created: 2024-10-17 06:29

/src/hbfa-fl/HBFA/UefiHostTestPkg/Library/UefiBootServicesTableLibHost/Notify.c
Line
Count
Source (jump to first uncovered line)
1
/** @file
2
  Support functions for UEFI protocol notification infrastructure.
3
4
Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
5
(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
6
SPDX-License-Identifier: BSD-2-Clause-Patent
7
8
**/
9
10
#include "DxeMain.h"
11
#include "Handle.h"
12
#include "Event.h"
13
14
/**
15
  Signal event for every protocol in protocol entry.
16
17
  @param  ProtEntry              Protocol entry
18
19
**/
20
VOID
21
CoreNotifyProtocolEntry (
22
  IN PROTOCOL_ENTRY   *ProtEntry
23
  )
24
0
{
25
0
  PROTOCOL_NOTIFY     *ProtNotify;
26
0
  LIST_ENTRY          *Link;
27
28
0
  ASSERT_LOCKED (&gProtocolDatabaseLock);
29
30
0
  for (Link=ProtEntry->Notify.ForwardLink; Link != &ProtEntry->Notify; Link=Link->ForwardLink) {
31
0
    ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
32
    // CoreSignalEvent (ProtNotify->Event);
33
0
  }
34
0
}
35
36
37
38
/**
39
  Removes Protocol from the protocol list (but not the handle list).
40
41
  @param  Handle                 The handle to remove protocol on.
42
  @param  Protocol               GUID of the protocol to be moved
43
  @param  Interface              The interface of the protocol
44
45
  @return Protocol Entry
46
47
**/
48
PROTOCOL_INTERFACE *
49
CoreRemoveInterfaceFromProtocol (
50
  IN IHANDLE        *Handle,
51
  IN EFI_GUID       *Protocol,
52
  IN VOID           *Interface
53
  )
54
0
{
55
0
  PROTOCOL_INTERFACE  *Prot;
56
0
  PROTOCOL_NOTIFY     *ProtNotify;
57
0
  PROTOCOL_ENTRY      *ProtEntry;
58
0
  LIST_ENTRY          *Link;
59
60
0
  ASSERT_LOCKED (&gProtocolDatabaseLock);
61
62
0
  Prot = CoreFindProtocolInterface (Handle, Protocol, Interface);
63
0
  if (Prot != NULL) {
64
65
0
    ProtEntry = Prot->Protocol;
66
67
    //
68
    // If there's a protocol notify location pointing to this entry, back it up one
69
    //
70
0
    for(Link = ProtEntry->Notify.ForwardLink; Link != &ProtEntry->Notify; Link=Link->ForwardLink) {
71
0
      ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
72
73
0
      if (ProtNotify->Position == &Prot->ByProtocol) {
74
0
        ProtNotify->Position = Prot->ByProtocol.BackLink;
75
0
      }
76
0
    }
77
78
    //
79
    // Remove the protocol interface entry
80
    //
81
0
    RemoveEntryList (&Prot->ByProtocol);
82
0
  }
83
84
0
  return Prot;
85
0
}
86
87
88
/**
89
  Add a new protocol notification record for the request protocol.
90
91
  @param  Protocol               The requested protocol to add the notify
92
                                 registration
93
  @param  Event                  The event to signal
94
  @param  Registration           Returns the registration record
95
96
  @retval EFI_INVALID_PARAMETER  Invalid parameter
97
  @retval EFI_SUCCESS            Successfully returned the registration record
98
                                 that has been added
99
100
**/
101
EFI_STATUS
102
EFIAPI
103
CoreRegisterProtocolNotify (
104
  IN EFI_GUID       *Protocol,
105
  IN EFI_EVENT      Event,
106
  OUT  VOID         **Registration
107
  )
108
0
{
109
0
  PROTOCOL_ENTRY    *ProtEntry;
110
0
  PROTOCOL_NOTIFY   *ProtNotify;
111
0
  EFI_STATUS        Status;
112
113
0
  if ((Protocol == NULL) || (Event == NULL) || (Registration == NULL))  {
114
0
    return EFI_INVALID_PARAMETER;
115
0
  }
116
117
0
  CoreAcquireProtocolLock ();
118
119
0
  ProtNotify = NULL;
120
121
  //
122
  // Get the protocol entry to add the notification too
123
  //
124
125
0
  ProtEntry = CoreFindProtocolEntry (Protocol, TRUE);
126
0
  if (ProtEntry != NULL) {
127
128
    //
129
    // Allocate a new notification record
130
    //
131
0
    ProtNotify = AllocatePool (sizeof(PROTOCOL_NOTIFY));
132
0
    if (ProtNotify != NULL) {
133
0
      ((IEVENT *)Event)->ExFlag |= EVT_EXFLAG_EVENT_PROTOCOL_NOTIFICATION;
134
0
      ProtNotify->Signature = PROTOCOL_NOTIFY_SIGNATURE;
135
0
      ProtNotify->Protocol = ProtEntry;
136
0
      ProtNotify->Event = Event;
137
      //
138
      // start at the begining
139
      //
140
0
      ProtNotify->Position = &ProtEntry->Protocols;
141
142
0
      InsertTailList (&ProtEntry->Notify, &ProtNotify->Link);
143
0
    }
144
0
  }
145
146
0
  CoreReleaseProtocolLock ();
147
148
  //
149
  // Done.  If we have a protocol notify entry, then return it.
150
  // Otherwise, we must have run out of resources trying to add one
151
  //
152
153
0
  Status = EFI_OUT_OF_RESOURCES;
154
0
  if (ProtNotify != NULL) {
155
0
    *Registration = ProtNotify;
156
0
    Status = EFI_SUCCESS;
157
0
  }
158
159
0
  return Status;
160
0
}
161
162
163
/**
164
  Reinstall a protocol interface on a device handle.  The OldInterface for Protocol is replaced by the NewInterface.
165
166
  @param  UserHandle             Handle on which the interface is to be
167
                                 reinstalled
168
  @param  Protocol               The numeric ID of the interface
169
  @param  OldInterface           A pointer to the old interface
170
  @param  NewInterface           A pointer to the new interface
171
172
  @retval EFI_SUCCESS            The protocol interface was installed
173
  @retval EFI_NOT_FOUND          The OldInterface on the handle was not found
174
  @retval EFI_INVALID_PARAMETER  One of the parameters has an invalid value
175
176
**/
177
EFI_STATUS
178
EFIAPI
179
CoreReinstallProtocolInterface (
180
  IN EFI_HANDLE     UserHandle,
181
  IN EFI_GUID       *Protocol,
182
  IN VOID           *OldInterface,
183
  IN VOID           *NewInterface
184
  )
185
0
{
186
0
  EFI_STATUS                Status;
187
0
  IHANDLE                   *Handle;
188
0
  PROTOCOL_INTERFACE        *Prot;
189
0
  PROTOCOL_ENTRY            *ProtEntry;
190
191
0
  Status = CoreValidateHandle (UserHandle);
192
0
  if (EFI_ERROR (Status)) {
193
0
    return Status;
194
0
  }
195
196
0
  if (Protocol == NULL) {
197
0
    return EFI_INVALID_PARAMETER;
198
0
  }
199
200
0
  Handle = (IHANDLE *) UserHandle;
201
202
  //
203
  // Lock the protocol database
204
  //
205
0
  CoreAcquireProtocolLock ();
206
207
  //
208
  // Check that Protocol exists on UserHandle, and Interface matches the interface in the database
209
  //
210
0
  Prot = CoreFindProtocolInterface (UserHandle, Protocol, OldInterface);
211
0
  if (Prot == NULL) {
212
0
    Status = EFI_NOT_FOUND;
213
0
    goto Done;
214
0
  }
215
216
  //
217
  // Attempt to disconnect all drivers that are using the protocol interface that is about to be reinstalled
218
  //
219
0
  Status = CoreDisconnectControllersUsingProtocolInterface (
220
0
             UserHandle,
221
0
             Prot
222
0
             );
223
0
  if (EFI_ERROR (Status)) {
224
    //
225
    // One or more drivers refused to release, so return the error
226
    //
227
0
    goto Done;
228
0
  }
229
230
  //
231
  // Remove the protocol interface from the protocol
232
  //
233
0
  Prot = CoreRemoveInterfaceFromProtocol (Handle, Protocol, OldInterface);
234
235
0
  if (Prot == NULL) {
236
0
    Status = EFI_NOT_FOUND;
237
0
    goto Done;
238
0
  }
239
240
0
  ProtEntry = Prot->Protocol;
241
242
  //
243
  // Update the interface on the protocol
244
  //
245
0
  Prot->Interface = NewInterface;
246
247
  //
248
  // Add this protocol interface to the tail of the
249
  // protocol entry
250
  //
251
0
  InsertTailList (&ProtEntry->Protocols, &Prot->ByProtocol);
252
253
  //
254
  // Update the Key to show that the handle has been created/modified
255
  //
256
0
  gHandleDatabaseKey++;
257
0
  Handle->Key = gHandleDatabaseKey;
258
259
  //
260
  // Release the lock and connect all drivers to UserHandle
261
  //
262
0
  CoreReleaseProtocolLock ();
263
  //
264
  // Return code is ignored on purpose.
265
  //
266
0
  CoreConnectController (
267
0
    UserHandle,
268
0
    NULL,
269
0
    NULL,
270
0
    TRUE
271
0
    );
272
0
  CoreAcquireProtocolLock ();
273
274
  //
275
  // Notify the notification list for this protocol
276
  //
277
0
  CoreNotifyProtocolEntry (ProtEntry);
278
279
0
  Status = EFI_SUCCESS;
280
281
0
Done:
282
0
  CoreReleaseProtocolLock ();
283
284
0
  return Status;
285
0
}