]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Pei/Ppi/Ppi.c
a0780c7838f9cff2d2af3bfaf47a72fec5c4dd24
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Ppi / Ppi.c
1 /** @file
2 EFI PEI Core PPI services
3
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "PeiMain.h"
16
17 /**
18
19 Initialize PPI services.
20
21 @param PrivateData Pointer to the PEI Core data.
22 @param OldCoreData Pointer to old PEI Core data.
23 NULL if being run in non-permament memory mode.
24
25 **/
26 VOID
27 InitializePpiServices (
28 IN PEI_CORE_INSTANCE *PrivateData,
29 IN PEI_CORE_INSTANCE *OldCoreData
30 )
31 {
32 if (OldCoreData == NULL) {
33 PrivateData->PpiData.NotifyListEnd = FixedPcdGet32 (PcdPeiCoreMaxPpiSupported)-1;
34 PrivateData->PpiData.DispatchListEnd = FixedPcdGet32 (PcdPeiCoreMaxPpiSupported)-1;
35 PrivateData->PpiData.LastDispatchedNotify = FixedPcdGet32 (PcdPeiCoreMaxPpiSupported)-1;
36 }
37 }
38
39 /**
40
41 Migrate the Hob list from the CAR stack to PEI installed memory.
42
43 @param PrivateData Pointer to PeiCore's private data structure.
44 @param OldCheckingBottom Bottom of temporary memory range. All Ppi in this range
45 will be fixup for PpiData and PpiDescriptor pointer.
46 @param OldCheckingTop Top of temporary memory range. All Ppi in this range
47 will be fixup for PpiData and PpiDescriptor.
48 @param Fixup The address difference between
49 the new Hob list and old Hob list.
50
51 **/
52 VOID
53 ConvertPpiPointers (
54 IN PEI_CORE_INSTANCE *PrivateData,
55 IN UINTN OldCheckingBottom,
56 IN UINTN OldCheckingTop,
57 IN INTN Fixup
58 )
59 {
60 UINT8 Index;
61 PEI_PPI_LIST_POINTERS *PpiPointer;
62
63 for (Index = 0; Index < FixedPcdGet32 (PcdPeiCoreMaxPpiSupported); Index++) {
64 if (Index < PrivateData->PpiData.PpiListEnd ||
65 Index > PrivateData->PpiData.NotifyListEnd) {
66 PpiPointer = &PrivateData->PpiData.PpiListPtrs[Index];
67
68 if (((UINTN)PpiPointer->Raw < OldCheckingTop) &&
69 ((UINTN)PpiPointer->Raw >= OldCheckingBottom)) {
70 //
71 // Convert the pointer to the PEIM descriptor from the old HOB heap
72 // to the relocated HOB heap.
73 //
74 PpiPointer->Raw = (VOID *) ((UINTN)PpiPointer->Raw + Fixup);
75
76 //
77 // Only when the PEIM descriptor is in the old HOB should it be necessary
78 // to try to convert the pointers in the PEIM descriptor
79 //
80
81 if (((UINTN)PpiPointer->Ppi->Guid < OldCheckingTop) &&
82 ((UINTN)PpiPointer->Ppi->Guid >= OldCheckingBottom)) {
83 //
84 // Convert the pointer to the GUID in the PPI or NOTIFY descriptor
85 // from the old HOB heap to the relocated HOB heap.
86 //
87 PpiPointer->Ppi->Guid = (VOID *) ((UINTN)PpiPointer->Ppi->Guid + Fixup);
88 }
89
90 //
91 // Assume that no code is located in the temporary memory, so the pointer to
92 // the notification function in the NOTIFY descriptor needs not be converted.
93 //
94 if (Index < PrivateData->PpiData.PpiListEnd &&
95 (UINTN)PpiPointer->Ppi->Ppi < OldCheckingTop &&
96 (UINTN)PpiPointer->Ppi->Ppi >= OldCheckingBottom) {
97 //
98 // Convert the pointer to the PPI interface structure in the PPI descriptor
99 // from the old HOB heap to the relocated HOB heap.
100 //
101 PpiPointer->Ppi->Ppi = (VOID *) ((UINTN)PpiPointer->Ppi->Ppi+ Fixup);
102 }
103 }
104 }
105 }
106 }
107
108 /**
109
110 This function installs an interface in the PEI PPI database by GUID.
111 The purpose of the service is to publish an interface that other parties
112 can use to call additional PEIMs.
113
114 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
115 @param PpiList Pointer to a list of PEI PPI Descriptors.
116
117 @retval EFI_SUCCESS if all PPIs in PpiList are successfully installed.
118 @retval EFI_INVALID_PARAMETER if PpiList is NULL pointer
119 if any PPI in PpiList is not valid
120 @retval EFI_OUT_OF_RESOURCES if there is no more memory resource to install PPI
121
122 **/
123 EFI_STATUS
124 EFIAPI
125 PeiInstallPpi (
126 IN CONST EFI_PEI_SERVICES **PeiServices,
127 IN CONST EFI_PEI_PPI_DESCRIPTOR *PpiList
128 )
129 {
130 PEI_CORE_INSTANCE *PrivateData;
131 INTN Index;
132 INTN LastCallbackInstall;
133
134
135 if (PpiList == NULL) {
136 return EFI_INVALID_PARAMETER;
137 }
138
139 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS(PeiServices);
140
141 Index = PrivateData->PpiData.PpiListEnd;
142 LastCallbackInstall = Index;
143
144 //
145 // This is loop installs all PPI descriptors in the PpiList. It is terminated
146 // by the EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST being set in the last
147 // EFI_PEI_PPI_DESCRIPTOR in the list.
148 //
149
150 for (;;) {
151 //
152 // Since PpiData is used for NotifyList and PpiList, max resource
153 // is reached if the Install reaches the NotifyList
154 //
155 if (Index == PrivateData->PpiData.NotifyListEnd + 1) {
156 return EFI_OUT_OF_RESOURCES;
157 }
158 //
159 // Check if it is a valid PPI.
160 // If not, rollback list to exclude all in this list.
161 // Try to indicate which item failed.
162 //
163 if ((PpiList->Flags & EFI_PEI_PPI_DESCRIPTOR_PPI) == 0) {
164 PrivateData->PpiData.PpiListEnd = LastCallbackInstall;
165 DEBUG((EFI_D_ERROR, "ERROR -> InstallPpi: %g %p\n", PpiList->Guid, PpiList->Ppi));
166 return EFI_INVALID_PARAMETER;
167 }
168
169 DEBUG((EFI_D_INFO, "Install PPI: %g\n", PpiList->Guid));
170 PrivateData->PpiData.PpiListPtrs[Index].Ppi = (EFI_PEI_PPI_DESCRIPTOR*) PpiList;
171 PrivateData->PpiData.PpiListEnd++;
172
173 //
174 // Continue until the end of the PPI List.
175 //
176 if ((PpiList->Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) ==
177 EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) {
178 break;
179 }
180 PpiList++;
181 Index++;
182 }
183
184 //
185 // Dispatch any callback level notifies for newly installed PPIs.
186 //
187 DispatchNotify (
188 PrivateData,
189 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,
190 LastCallbackInstall,
191 PrivateData->PpiData.PpiListEnd,
192 PrivateData->PpiData.DispatchListEnd,
193 PrivateData->PpiData.NotifyListEnd
194 );
195
196
197 return EFI_SUCCESS;
198 }
199
200 /**
201
202 This function reinstalls an interface in the PEI PPI database by GUID.
203 The purpose of the service is to publish an interface that other parties can
204 use to replace an interface of the same name in the protocol database with a
205 different interface.
206
207 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
208 @param OldPpi Pointer to the old PEI PPI Descriptors.
209 @param NewPpi Pointer to the new PEI PPI Descriptors.
210
211 @retval EFI_SUCCESS if the operation was successful
212 @retval EFI_INVALID_PARAMETER if OldPpi or NewPpi is NULL
213 @retval EFI_INVALID_PARAMETER if NewPpi is not valid
214 @retval EFI_NOT_FOUND if the PPI was not in the database
215
216 **/
217 EFI_STATUS
218 EFIAPI
219 PeiReInstallPpi (
220 IN CONST EFI_PEI_SERVICES **PeiServices,
221 IN CONST EFI_PEI_PPI_DESCRIPTOR *OldPpi,
222 IN CONST EFI_PEI_PPI_DESCRIPTOR *NewPpi
223 )
224 {
225 PEI_CORE_INSTANCE *PrivateData;
226 INTN Index;
227
228
229 if ((OldPpi == NULL) || (NewPpi == NULL)) {
230 return EFI_INVALID_PARAMETER;
231 }
232
233 if ((NewPpi->Flags & EFI_PEI_PPI_DESCRIPTOR_PPI) == 0) {
234 return EFI_INVALID_PARAMETER;
235 }
236
237 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS(PeiServices);
238
239 //
240 // Find the old PPI instance in the database. If we can not find it,
241 // return the EFI_NOT_FOUND error.
242 //
243 for (Index = 0; Index < PrivateData->PpiData.PpiListEnd; Index++) {
244 if (OldPpi == PrivateData->PpiData.PpiListPtrs[Index].Ppi) {
245 break;
246 }
247 }
248 if (Index == PrivateData->PpiData.PpiListEnd) {
249 return EFI_NOT_FOUND;
250 }
251
252 //
253 // Remove the old PPI from the database, add the new one.
254 //
255 DEBUG((EFI_D_INFO, "Reinstall PPI: %g\n", NewPpi->Guid));
256 PrivateData->PpiData.PpiListPtrs[Index].Ppi = (EFI_PEI_PPI_DESCRIPTOR *) NewPpi;
257
258 //
259 // Dispatch any callback level notifies for the newly installed PPI.
260 //
261 DispatchNotify (
262 PrivateData,
263 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,
264 Index,
265 Index+1,
266 PrivateData->PpiData.DispatchListEnd,
267 PrivateData->PpiData.NotifyListEnd
268 );
269
270
271 return EFI_SUCCESS;
272 }
273
274 /**
275
276 Locate a given named PPI.
277
278
279 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
280 @param Guid Pointer to GUID of the PPI.
281 @param Instance Instance Number to discover.
282 @param PpiDescriptor Pointer to reference the found descriptor. If not NULL,
283 returns a pointer to the descriptor (includes flags, etc)
284 @param Ppi Pointer to reference the found PPI
285
286 @retval EFI_SUCCESS if the PPI is in the database
287 @retval EFI_NOT_FOUND if the PPI is not in the database
288
289 **/
290 EFI_STATUS
291 EFIAPI
292 PeiLocatePpi (
293 IN CONST EFI_PEI_SERVICES **PeiServices,
294 IN CONST EFI_GUID *Guid,
295 IN UINTN Instance,
296 IN OUT EFI_PEI_PPI_DESCRIPTOR **PpiDescriptor,
297 IN OUT VOID **Ppi
298 )
299 {
300 PEI_CORE_INSTANCE *PrivateData;
301 INTN Index;
302 EFI_GUID *CheckGuid;
303 EFI_PEI_PPI_DESCRIPTOR *TempPtr;
304
305
306 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS(PeiServices);
307
308 //
309 // Search the data base for the matching instance of the GUIDed PPI.
310 //
311 for (Index = 0; Index < PrivateData->PpiData.PpiListEnd; Index++) {
312 TempPtr = PrivateData->PpiData.PpiListPtrs[Index].Ppi;
313 CheckGuid = TempPtr->Guid;
314
315 //
316 // Don't use CompareGuid function here for performance reasons.
317 // Instead we compare the GUID as INT32 at a time and branch
318 // on the first failed comparison.
319 //
320 if ((((INT32 *)Guid)[0] == ((INT32 *)CheckGuid)[0]) &&
321 (((INT32 *)Guid)[1] == ((INT32 *)CheckGuid)[1]) &&
322 (((INT32 *)Guid)[2] == ((INT32 *)CheckGuid)[2]) &&
323 (((INT32 *)Guid)[3] == ((INT32 *)CheckGuid)[3])) {
324 if (Instance == 0) {
325
326 if (PpiDescriptor != NULL) {
327 *PpiDescriptor = TempPtr;
328 }
329
330 if (Ppi != NULL) {
331 *Ppi = TempPtr->Ppi;
332 }
333
334
335 return EFI_SUCCESS;
336 }
337 Instance--;
338 }
339 }
340
341 return EFI_NOT_FOUND;
342 }
343
344 /**
345
346 This function installs a notification service to be called back when a given
347 interface is installed or reinstalled. The purpose of the service is to publish
348 an interface that other parties can use to call additional PPIs that may materialize later.
349
350 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
351 @param NotifyList Pointer to list of Descriptors to notify upon.
352
353 @retval EFI_SUCCESS if successful
354 @retval EFI_OUT_OF_RESOURCES if no space in the database
355 @retval EFI_INVALID_PARAMETER if not a good decriptor
356
357 **/
358 EFI_STATUS
359 EFIAPI
360 PeiNotifyPpi (
361 IN CONST EFI_PEI_SERVICES **PeiServices,
362 IN CONST EFI_PEI_NOTIFY_DESCRIPTOR *NotifyList
363 )
364 {
365 PEI_CORE_INSTANCE *PrivateData;
366 INTN Index;
367 INTN NotifyIndex;
368 INTN LastCallbackNotify;
369 EFI_PEI_NOTIFY_DESCRIPTOR *NotifyPtr;
370 UINTN NotifyDispatchCount;
371
372
373 NotifyDispatchCount = 0;
374
375 if (NotifyList == NULL) {
376 return EFI_INVALID_PARAMETER;
377 }
378
379 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS(PeiServices);
380
381 Index = PrivateData->PpiData.NotifyListEnd;
382 LastCallbackNotify = Index;
383
384 //
385 // This is loop installs all Notify descriptors in the NotifyList. It is
386 // terminated by the EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST being set in the last
387 // EFI_PEI_NOTIFY_DESCRIPTOR in the list.
388 //
389
390 for (;;) {
391 //
392 // Since PpiData is used for NotifyList and InstallList, max resource
393 // is reached if the Install reaches the PpiList
394 //
395 if (Index == PrivateData->PpiData.PpiListEnd - 1) {
396 return EFI_OUT_OF_RESOURCES;
397 }
398
399 //
400 // If some of the PPI data is invalid restore original Notify PPI database value
401 //
402 if ((NotifyList->Flags & EFI_PEI_PPI_DESCRIPTOR_NOTIFY_TYPES) == 0) {
403 PrivateData->PpiData.NotifyListEnd = LastCallbackNotify;
404 DEBUG((EFI_D_ERROR, "ERROR -> InstallNotify: %g %p\n", NotifyList->Guid, NotifyList->Notify));
405 return EFI_INVALID_PARAMETER;
406 }
407
408 if ((NotifyList->Flags & EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH) != 0) {
409 NotifyDispatchCount ++;
410 }
411
412 PrivateData->PpiData.PpiListPtrs[Index].Notify = (EFI_PEI_NOTIFY_DESCRIPTOR *) NotifyList;
413
414 PrivateData->PpiData.NotifyListEnd--;
415 DEBUG((EFI_D_INFO, "Register PPI Notify: %g\n", NotifyList->Guid));
416 if ((NotifyList->Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) ==
417 EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) {
418 break;
419 }
420 //
421 // Go the next descriptor. Remember the NotifyList moves down.
422 //
423 NotifyList++;
424 Index--;
425 }
426
427 //
428 // If there is Dispatch Notify PPI installed put them on the bottom
429 //
430 if (NotifyDispatchCount > 0) {
431 for (NotifyIndex = LastCallbackNotify; NotifyIndex > PrivateData->PpiData.NotifyListEnd; NotifyIndex--) {
432 if ((PrivateData->PpiData.PpiListPtrs[NotifyIndex].Notify->Flags & EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH) != 0) {
433 NotifyPtr = PrivateData->PpiData.PpiListPtrs[NotifyIndex].Notify;
434
435 for (Index = NotifyIndex; Index < PrivateData->PpiData.DispatchListEnd; Index++){
436 PrivateData->PpiData.PpiListPtrs[Index].Notify = PrivateData->PpiData.PpiListPtrs[Index + 1].Notify;
437 }
438 PrivateData->PpiData.PpiListPtrs[Index].Notify = NotifyPtr;
439 PrivateData->PpiData.DispatchListEnd--;
440 }
441 }
442
443 LastCallbackNotify -= NotifyDispatchCount;
444 }
445
446 //
447 // Dispatch any callback level notifies for all previously installed PPIs.
448 //
449 DispatchNotify (
450 PrivateData,
451 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,
452 0,
453 PrivateData->PpiData.PpiListEnd,
454 LastCallbackNotify,
455 PrivateData->PpiData.NotifyListEnd
456 );
457
458 return EFI_SUCCESS;
459 }
460
461
462 /**
463
464 Process the Notify List at dispatch level.
465
466 @param PrivateData PeiCore's private data structure.
467
468 **/
469 VOID
470 ProcessNotifyList (
471 IN PEI_CORE_INSTANCE *PrivateData
472 )
473 {
474 INTN TempValue;
475
476 while (TRUE) {
477 //
478 // Check if the PEIM that was just dispatched resulted in any
479 // Notifies getting installed. If so, go process any dispatch
480 // level Notifies that match the previouly installed PPIs.
481 // Use "while" instead of "if" since DispatchNotify can modify
482 // DispatchListEnd (with NotifyPpi) so we have to iterate until the same.
483 //
484 while (PrivateData->PpiData.LastDispatchedNotify != PrivateData->PpiData.DispatchListEnd) {
485 TempValue = PrivateData->PpiData.DispatchListEnd;
486 DispatchNotify (
487 PrivateData,
488 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH,
489 0,
490 PrivateData->PpiData.LastDispatchedInstall,
491 PrivateData->PpiData.LastDispatchedNotify,
492 PrivateData->PpiData.DispatchListEnd
493 );
494 PrivateData->PpiData.LastDispatchedNotify = TempValue;
495 }
496
497
498 //
499 // Check if the PEIM that was just dispatched resulted in any
500 // PPIs getting installed. If so, go process any dispatch
501 // level Notifies that match the installed PPIs.
502 // Use "while" instead of "if" since DispatchNotify can modify
503 // PpiListEnd (with InstallPpi) so we have to iterate until the same.
504 //
505 while (PrivateData->PpiData.LastDispatchedInstall != PrivateData->PpiData.PpiListEnd) {
506 TempValue = PrivateData->PpiData.PpiListEnd;
507 DispatchNotify (
508 PrivateData,
509 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH,
510 PrivateData->PpiData.LastDispatchedInstall,
511 PrivateData->PpiData.PpiListEnd,
512 FixedPcdGet32 (PcdPeiCoreMaxPpiSupported)-1,
513 PrivateData->PpiData.DispatchListEnd
514 );
515 PrivateData->PpiData.LastDispatchedInstall = TempValue;
516 }
517
518 if (PrivateData->PpiData.LastDispatchedNotify == PrivateData->PpiData.DispatchListEnd) {
519 break;
520 }
521 }
522 return;
523 }
524
525 /**
526
527 Dispatch notifications.
528
529 @param PrivateData PeiCore's private data structure
530 @param NotifyType Type of notify to fire.
531 @param InstallStartIndex Install Beginning index.
532 @param InstallStopIndex Install Ending index.
533 @param NotifyStartIndex Notify Beginning index.
534 @param NotifyStopIndex Notify Ending index.
535
536 **/
537 VOID
538 DispatchNotify (
539 IN PEI_CORE_INSTANCE *PrivateData,
540 IN UINTN NotifyType,
541 IN INTN InstallStartIndex,
542 IN INTN InstallStopIndex,
543 IN INTN NotifyStartIndex,
544 IN INTN NotifyStopIndex
545 )
546 {
547 INTN Index1;
548 INTN Index2;
549 EFI_GUID *SearchGuid;
550 EFI_GUID *CheckGuid;
551 EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor;
552
553 //
554 // Remember that Installs moves up and Notifies moves down.
555 //
556 for (Index1 = NotifyStartIndex; Index1 > NotifyStopIndex; Index1--) {
557 NotifyDescriptor = PrivateData->PpiData.PpiListPtrs[Index1].Notify;
558
559 CheckGuid = NotifyDescriptor->Guid;
560
561 for (Index2 = InstallStartIndex; Index2 < InstallStopIndex; Index2++) {
562 SearchGuid = PrivateData->PpiData.PpiListPtrs[Index2].Ppi->Guid;
563 //
564 // Don't use CompareGuid function here for performance reasons.
565 // Instead we compare the GUID as INT32 at a time and branch
566 // on the first failed comparison.
567 //
568 if ((((INT32 *)SearchGuid)[0] == ((INT32 *)CheckGuid)[0]) &&
569 (((INT32 *)SearchGuid)[1] == ((INT32 *)CheckGuid)[1]) &&
570 (((INT32 *)SearchGuid)[2] == ((INT32 *)CheckGuid)[2]) &&
571 (((INT32 *)SearchGuid)[3] == ((INT32 *)CheckGuid)[3])) {
572 DEBUG ((EFI_D_INFO, "Notify: PPI Guid: %g, Peim notify entry point: %p\n",
573 SearchGuid,
574 NotifyDescriptor->Notify
575 ));
576 NotifyDescriptor->Notify (
577 (EFI_PEI_SERVICES **) GetPeiServicesTablePointer (),
578 NotifyDescriptor,
579 (PrivateData->PpiData.PpiListPtrs[Index2].Ppi)->Ppi
580 );
581 }
582 }
583 }
584 }
585