]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/PeiServicesLib/PeiServicesLib.c
835325cc38d727bfe9cf908edc717e0b538e811e
[mirror_edk2.git] / MdePkg / Library / PeiServicesLib / PeiServicesLib.c
1 /** @file
2 Implementation for PEI Services Library.
3
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5 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
16 #include <PiPei.h>
17
18 #include <Ppi/FirmwareVolumeInfo.h>
19 #include <Guid/FirmwareFileSystem2.h>
20
21 #include <Library/PeiServicesLib.h>
22 #include <Library/PeiServicesTablePointerLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/MemoryAllocationLib.h>
25 #include <Library/BaseMemoryLib.h>
26
27
28 GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_PEI_PPI_DESCRIPTOR mPpiListTemplate[] = {
29 {
30 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
31 &gEfiPeiFirmwareVolumeInfoPpiGuid,
32 NULL
33 }
34 };
35
36 /**
37 This service enables a given PEIM to register an interface into the PEI Foundation.
38
39 @param PpiList A pointer to the list of interfaces that the caller shall install.
40
41 @retval EFI_SUCCESS The interface was successfully installed.
42 @retval EFI_INVALID_PARAMETER The PpiList pointer is NULL.
43 @retval EFI_INVALID_PARAMETER Any of the PEI PPI descriptors in the list do not have the
44 EFI_PEI_PPI_DESCRIPTOR_PPI bit set in the Flags field.
45 @retval EFI_OUT_OF_RESOURCES There is no additional space in the PPI database.
46
47 **/
48 EFI_STATUS
49 EFIAPI
50 PeiServicesInstallPpi (
51 IN CONST EFI_PEI_PPI_DESCRIPTOR *PpiList
52 )
53 {
54 CONST EFI_PEI_SERVICES **PeiServices;
55
56 PeiServices = GetPeiServicesTablePointer ();
57 return (*PeiServices)->InstallPpi (PeiServices, PpiList);
58 }
59
60 /**
61 This service enables PEIMs to replace an entry in the PPI database with an alternate entry.
62
63 @param OldPpi The pointer to the old PEI PPI Descriptors.
64 @param NewPpi The pointer to the new PEI PPI Descriptors.
65
66 @retval EFI_SUCCESS The interface was successfully installed.
67 @retval EFI_INVALID_PARAMETER The OldPpi or NewPpi is NULL.
68 @retval EFI_INVALID_PARAMETER Any of the PEI PPI descriptors in the list do not have the
69 EFI_PEI_PPI_DESCRIPTOR_PPI bit set in the Flags field.
70 @retval EFI_OUT_OF_RESOURCES There is no additional space in the PPI database.
71 @retval EFI_NOT_FOUND The PPI for which the reinstallation was requested has not been
72 installed.
73
74 **/
75 EFI_STATUS
76 EFIAPI
77 PeiServicesReInstallPpi (
78 IN CONST EFI_PEI_PPI_DESCRIPTOR *OldPpi,
79 IN CONST EFI_PEI_PPI_DESCRIPTOR *NewPpi
80 )
81 {
82 CONST EFI_PEI_SERVICES **PeiServices;
83
84 PeiServices = GetPeiServicesTablePointer ();
85 return (*PeiServices)->ReInstallPpi (PeiServices, OldPpi, NewPpi);
86 }
87
88 /**
89 This service enables PEIMs to discover a given instance of an interface.
90
91 @param Guid A pointer to the GUID whose corresponding interface needs to be
92 found.
93 @param Instance The N-th instance of the interface that is required.
94 @param PpiDescriptor A pointer to instance of the EFI_PEI_PPI_DESCRIPTOR.
95 @param Ppi A pointer to the instance of the interface.
96
97 @retval EFI_SUCCESS The interface was successfully returned.
98 @retval EFI_NOT_FOUND The PPI descriptor is not found in the database.
99
100 **/
101 EFI_STATUS
102 EFIAPI
103 PeiServicesLocatePpi (
104 IN CONST EFI_GUID *Guid,
105 IN UINTN Instance,
106 IN OUT EFI_PEI_PPI_DESCRIPTOR **PpiDescriptor,
107 IN OUT VOID **Ppi
108 )
109 {
110 CONST EFI_PEI_SERVICES **PeiServices;
111
112 PeiServices = GetPeiServicesTablePointer ();
113 return (*PeiServices)->LocatePpi (PeiServices, Guid, Instance, PpiDescriptor, Ppi);
114 }
115
116 /**
117 This service enables PEIMs to register a given service to be invoked when another service is
118 installed or reinstalled.
119
120 @param NotifyList A pointer to the list of notification interfaces
121 that the caller shall install.
122
123 @retval EFI_SUCCESS The interface was successfully installed.
124 @retval EFI_INVALID_PARAMETER The NotifyList pointer is NULL.
125 @retval EFI_INVALID_PARAMETER Any of the PEI notify descriptors in the list do
126 not have the EFI_PEI_PPI_DESCRIPTOR_NOTIFY_TYPES
127 bit set in the Flags field.
128 @retval EFI_OUT_OF_RESOURCES There is no additional space in the PPI database.
129
130 **/
131 EFI_STATUS
132 EFIAPI
133 PeiServicesNotifyPpi (
134 IN CONST EFI_PEI_NOTIFY_DESCRIPTOR *NotifyList
135 )
136 {
137 CONST EFI_PEI_SERVICES **PeiServices;
138
139 PeiServices = GetPeiServicesTablePointer ();
140 return (*PeiServices)->NotifyPpi (PeiServices, NotifyList);
141 }
142
143 /**
144 This service enables PEIMs to ascertain the present value of the boot mode.
145
146 @param BootMode A pointer to contain the value of the boot mode.
147
148 @retval EFI_SUCCESS The boot mode was returned successfully.
149 @retval EFI_INVALID_PARAMETER BootMode is NULL.
150
151 **/
152 EFI_STATUS
153 EFIAPI
154 PeiServicesGetBootMode (
155 OUT EFI_BOOT_MODE *BootMode
156 )
157 {
158 CONST EFI_PEI_SERVICES **PeiServices;
159
160 PeiServices = GetPeiServicesTablePointer ();
161 return (*PeiServices)->GetBootMode (PeiServices, BootMode);
162 }
163
164 /**
165 This service enables PEIMs to update the boot mode variable.
166
167 @param BootMode The value of the boot mode to set.
168
169 @retval EFI_SUCCESS The value was successfully updated
170
171 **/
172 EFI_STATUS
173 EFIAPI
174 PeiServicesSetBootMode (
175 IN EFI_BOOT_MODE BootMode
176 )
177 {
178 CONST EFI_PEI_SERVICES **PeiServices;
179
180 PeiServices = GetPeiServicesTablePointer ();
181 return (*PeiServices)->SetBootMode (PeiServices, BootMode);
182 }
183
184 /**
185 This service enables a PEIM to ascertain the address of the list of HOBs in memory.
186
187 @param HobList A pointer to the list of HOBs that the PEI Foundation
188 will initialize.
189
190 @retval EFI_SUCCESS The list was successfully returned.
191 @retval EFI_NOT_AVAILABLE_YET The HOB list is not yet published.
192
193 **/
194 EFI_STATUS
195 EFIAPI
196 PeiServicesGetHobList (
197 OUT VOID **HobList
198 )
199 {
200 CONST EFI_PEI_SERVICES **PeiServices;
201
202 PeiServices = GetPeiServicesTablePointer ();
203 return (*PeiServices)->GetHobList (PeiServices, HobList);
204 }
205
206 /**
207 This service enables PEIMs to create various types of HOBs.
208
209 @param Type The type of HOB to be installed.
210 @param Length The length of the HOB to be added.
211 @param Hob The address of a pointer that will contain the
212 HOB header.
213
214 @retval EFI_SUCCESS The HOB was successfully created.
215 @retval EFI_OUT_OF_RESOURCES There is no additional space for HOB creation.
216
217 **/
218 EFI_STATUS
219 EFIAPI
220 PeiServicesCreateHob (
221 IN UINT16 Type,
222 IN UINT16 Length,
223 OUT VOID **Hob
224 )
225 {
226 CONST EFI_PEI_SERVICES **PeiServices;
227
228 PeiServices = GetPeiServicesTablePointer ();
229 return (*PeiServices)->CreateHob (PeiServices, Type, Length, Hob);
230 }
231
232 /**
233 This service enables PEIMs to discover additional firmware volumes.
234
235 @param Instance This instance of the firmware volume to find. The
236 value 0 is the Boot Firmware Volume (BFV).
237 @param VolumeHandle Handle of the firmware volume header of the volume
238 to return.
239
240 @retval EFI_SUCCESS The volume was found.
241 @retval EFI_NOT_FOUND The volume was not found.
242 @retval EFI_INVALID_PARAMETER FwVolHeader is NULL.
243
244 **/
245 EFI_STATUS
246 EFIAPI
247 PeiServicesFfsFindNextVolume (
248 IN UINTN Instance,
249 IN OUT EFI_PEI_FV_HANDLE *VolumeHandle
250 )
251 {
252 CONST EFI_PEI_SERVICES **PeiServices;
253
254 PeiServices = GetPeiServicesTablePointer ();
255 return (*PeiServices)->FfsFindNextVolume (PeiServices, Instance, VolumeHandle);
256 }
257
258 /**
259 This service enables PEIMs to discover additional firmware files.
260
261 @param SearchType A filter to find files only of this type.
262 @param VolumeHandle The pointer to the firmware volume header of the
263 volume to search. This parameter must point to a
264 valid FFS volume.
265 @param FileHandle Handle of the current file from which to begin searching.
266
267 @retval EFI_SUCCESS The file was found.
268 @retval EFI_NOT_FOUND The file was not found.
269 @retval EFI_NOT_FOUND The header checksum was not zero.
270
271 **/
272 EFI_STATUS
273 EFIAPI
274 PeiServicesFfsFindNextFile (
275 IN EFI_FV_FILETYPE SearchType,
276 IN EFI_PEI_FV_HANDLE VolumeHandle,
277 IN OUT EFI_PEI_FILE_HANDLE *FileHandle
278 )
279 {
280 CONST EFI_PEI_SERVICES **PeiServices;
281
282 PeiServices = GetPeiServicesTablePointer ();
283 return (*PeiServices)->FfsFindNextFile (PeiServices, SearchType, VolumeHandle, FileHandle);
284 }
285
286 /**
287 This service enables PEIMs to discover sections of a given type within a valid FFS file.
288
289 @param SectionType The value of the section type to find.
290 @param FileHandle A pointer to the file header that contains the set
291 of sections to be searched.
292 @param SectionData A pointer to the discovered section, if successful.
293
294 @retval EFI_SUCCESS The section was found.
295 @retval EFI_NOT_FOUND The section was not found.
296
297 **/
298 EFI_STATUS
299 EFIAPI
300 PeiServicesFfsFindSectionData (
301 IN EFI_SECTION_TYPE SectionType,
302 IN EFI_PEI_FILE_HANDLE FileHandle,
303 OUT VOID **SectionData
304 )
305 {
306 CONST EFI_PEI_SERVICES **PeiServices;
307
308 PeiServices = GetPeiServicesTablePointer ();
309 return (*PeiServices)->FfsFindSectionData (PeiServices, SectionType, FileHandle, SectionData);
310 }
311
312 /**
313 This service enables PEIMs to register the permanent memory configuration
314 that has been initialized with the PEI Foundation.
315
316 @param MemoryBegin The value of a region of installed memory.
317 @param MemoryLength The corresponding length of a region of installed memory.
318
319 @retval EFI_SUCCESS The region was successfully installed in a HOB.
320 @retval EFI_INVALID_PARAMETER MemoryBegin and MemoryLength are illegal for this system.
321 @retval EFI_OUT_OF_RESOURCES There is no additional space for HOB creation.
322
323 **/
324 EFI_STATUS
325 EFIAPI
326 PeiServicesInstallPeiMemory (
327 IN EFI_PHYSICAL_ADDRESS MemoryBegin,
328 IN UINT64 MemoryLength
329 )
330 {
331 CONST EFI_PEI_SERVICES **PeiServices;
332
333 PeiServices = GetPeiServicesTablePointer ();
334 return (*PeiServices)->InstallPeiMemory (PeiServices, MemoryBegin, MemoryLength);
335 }
336
337 /**
338 This service enables PEIMs to allocate memory after the permanent memory has been
339 installed by a PEIM.
340
341 @param MemoryType Type of memory to allocate.
342 @param Pages The number of pages to allocate.
343 @param Memory Pointer of memory allocated.
344
345 @retval EFI_SUCCESS The memory range was successfully allocated.
346 @retval EFI_INVALID_PARAMETER Type is not equal to AllocateAnyPages.
347 @retval EFI_NOT_AVAILABLE_YET Called with permanent memory not available.
348 @retval EFI_OUT_OF_RESOURCES The pages could not be allocated.
349
350 **/
351 EFI_STATUS
352 EFIAPI
353 PeiServicesAllocatePages (
354 IN EFI_MEMORY_TYPE MemoryType,
355 IN UINTN Pages,
356 OUT EFI_PHYSICAL_ADDRESS *Memory
357 )
358 {
359 CONST EFI_PEI_SERVICES **PeiServices;
360
361 PeiServices = GetPeiServicesTablePointer ();
362 return (*PeiServices)->AllocatePages (PeiServices, MemoryType, Pages, Memory);
363 }
364
365 /**
366 This service allocates memory from the Hand-Off Block (HOB) heap.
367
368 @param Size The number of bytes to allocate from the pool.
369 @param Buffer If the call succeeds, a pointer to a pointer to
370 the allocate buffer; otherwise, undefined.
371
372 @retval EFI_SUCCESS The allocation was successful
373 @retval EFI_OUT_OF_RESOURCES There is not enough heap to allocate the requested size.
374
375 **/
376 EFI_STATUS
377 EFIAPI
378 PeiServicesAllocatePool (
379 IN UINTN Size,
380 OUT VOID **Buffer
381 )
382 {
383 CONST EFI_PEI_SERVICES **PeiServices;
384
385 PeiServices = GetPeiServicesTablePointer ();
386 return (*PeiServices)->AllocatePool (PeiServices, Size, Buffer);
387 }
388
389 /**
390 Resets the entire platform.
391
392 @retval EFI_SUCCESS The function completed successfully.
393 @retval EFI_NOT_AVAILABLE_YET The service has not been installed yet.
394
395 **/
396 EFI_STATUS
397 EFIAPI
398 PeiServicesResetSystem (
399 VOID
400 )
401 {
402 CONST EFI_PEI_SERVICES **PeiServices;
403
404 PeiServices = GetPeiServicesTablePointer ();
405 return (*PeiServices)->ResetSystem (PeiServices);
406 }
407
408 /**
409 This service is a wrapper for the PEI Service RegisterForShadow(), except the
410 pointer to the PEI Services Table has been removed. See the Platform
411 Initialization Pre-EFI Initialization Core Interface Specification for details.
412
413 @param FileHandle PEIM's file handle. Must be the currently
414 executing PEIM.
415
416 @retval EFI_SUCCESS The PEIM was successfully registered for
417 shadowing.
418
419 @retval EFI_ALREADY_STARTED The PEIM was previously
420 registered for shadowing.
421
422 @retval EFI_NOT_FOUND The FileHandle does not refer to a
423 valid file handle.
424 **/
425 EFI_STATUS
426 EFIAPI
427 PeiServicesRegisterForShadow (
428 IN EFI_PEI_FILE_HANDLE FileHandle
429 )
430 {
431 return (*GetPeiServicesTablePointer())->RegisterForShadow (FileHandle);
432 }
433
434 /**
435 This service is a wrapper for the PEI Service FfsGetFileInfo(), except the pointer to the PEI Services
436 Table has been removed. See the Platform Initialization Pre-EFI Initialization Core Interface
437 Specification for details.
438
439 @param FileHandle The handle of the file.
440
441 @param FileInfo Upon exit, points to the file's
442 information.
443
444 @retval EFI_SUCCESS File information returned.
445
446 @retval EFI_INVALID_PARAMETER If FileHandle does not
447 represent a valid file.
448
449 @retval EFI_INVALID_PARAMETER FileInfo is NULL.
450
451 **/
452 EFI_STATUS
453 EFIAPI
454 PeiServicesFfsGetFileInfo (
455 IN CONST EFI_PEI_FILE_HANDLE FileHandle,
456 OUT EFI_FV_FILE_INFO *FileInfo
457 )
458 {
459 return (*GetPeiServicesTablePointer())->FfsGetFileInfo (FileHandle, FileInfo);
460 }
461
462
463 /**
464 This service is a wrapper for the PEI Service FfsFindByName(), except the pointer to the PEI Services
465 Table has been removed. See the Platform Initialization Pre-EFI Initialization Core Interface
466 Specification for details.
467
468 @param FileName A pointer to the name of the file to
469 find within the firmware volume.
470
471 @param VolumeHandle The firmware volume to search FileHandle
472 Upon exit, points to the found file's
473 handle or NULL if it could not be found.
474 @param FileHandle The pointer to found file handle
475
476 @retval EFI_SUCCESS File was found.
477
478 @retval EFI_NOT_FOUND File was not found.
479
480 @retval EFI_INVALID_PARAMETER VolumeHandle or FileHandle or
481 FileName was NULL.
482
483 **/
484 EFI_STATUS
485 EFIAPI
486 PeiServicesFfsFindFileByName (
487 IN CONST EFI_GUID *FileName,
488 IN CONST EFI_PEI_FV_HANDLE VolumeHandle,
489 OUT EFI_PEI_FILE_HANDLE *FileHandle
490 )
491 {
492 return (*GetPeiServicesTablePointer())->FfsFindFileByName (FileName, VolumeHandle, FileHandle);
493 }
494
495
496 /**
497 This service is a wrapper for the PEI Service FfsGetVolumeInfo(), except the pointer to the PEI Services
498 Table has been removed. See the Platform Initialization Pre-EFI Initialization Core Interface
499 Specification for details.
500
501 @param VolumeHandle Handle of the volume.
502
503 @param VolumeInfo Upon exit, points to the volume's
504 information.
505
506 @retval EFI_SUCCESS File information returned.
507
508 @retval EFI_INVALID_PARAMETER If FileHandle does not
509 represent a valid file.
510
511 @retval EFI_INVALID_PARAMETER If FileInfo is NULL.
512
513 **/
514 EFI_STATUS
515 EFIAPI
516 PeiServicesFfsGetVolumeInfo (
517 IN EFI_PEI_FV_HANDLE VolumeHandle,
518 OUT EFI_FV_INFO *VolumeInfo
519 )
520 {
521 return (*GetPeiServicesTablePointer())->FfsGetVolumeInfo (VolumeHandle, VolumeInfo);
522 }
523
524 /**
525 Install a EFI_PEI_FIRMWARE_VOLUME_INFO_PPI instance so the PEI Core will be notified about a new firmware volume.
526
527 This function allocates, initializes, and installs a new EFI_PEI_FIRMWARE_VOLUME_INFO_PPI using
528 the parameters passed in to initialize the fields of the EFI_PEI_FIRMWARE_VOLUME_INFO_PPI instance.
529 If the resources can not be allocated for EFI_PEI_FIRMWARE_VOLUME_INFO_PPI, then ASSERT().
530 If the EFI_PEI_FIRMWARE_VOLUME_INFO_PPI can not be installed, then ASSERT().
531
532
533 @param FvFormat Unique identifier of the format of the memory-mapped
534 firmware volume. This parameter is optional and
535 may be NULL. If NULL is specified, the
536 EFI_FIRMWARE_FILE_SYSTEM2_GUID format is assumed.
537 @param FvInfo Points to a buffer which allows the
538 EFI_PEI_FIRMWARE_VOLUME_PPI to process the volume.
539 The format of this buffer is specific to the FvFormat.
540 For memory-mapped firmware volumes, this typically
541 points to the first byte of the firmware volume.
542 @param FvInfoSize The size, in bytes, of FvInfo. For memory-mapped
543 firmware volumes, this is typically the size of
544 the firmware volume.
545 @param ParentFvName If the new firmware volume originated from a file
546 in a different firmware volume, then this parameter
547 specifies the GUID name of the originating firmware
548 volume. Otherwise, this parameter must be NULL.
549 @param ParentFileName If the new firmware volume originated from a file
550 in a different firmware volume, then this parameter
551 specifies the GUID file name of the originating
552 firmware file. Otherwise, this parameter must be NULL.
553 **/
554 VOID
555 EFIAPI
556 PeiServicesInstallFvInfoPpi (
557 IN CONST EFI_GUID *FvFormat, OPTIONAL
558 IN CONST VOID *FvInfo,
559 IN UINT32 FvInfoSize,
560 IN CONST EFI_GUID *ParentFvName, OPTIONAL
561 IN CONST EFI_GUID *ParentFileName OPTIONAL
562 )
563 {
564 EFI_STATUS Status;
565 EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *FvInfoPpi;
566 EFI_PEI_PPI_DESCRIPTOR *FvInfoPpiDescriptor;
567 EFI_GUID *ParentFvNameValue;
568 EFI_GUID *ParentFileNameValue;
569
570 ParentFvNameValue = NULL;
571 ParentFileNameValue = NULL;
572 FvInfoPpi = AllocateZeroPool (sizeof (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI));
573 ASSERT(FvInfoPpi != NULL);
574
575 if (FvFormat != NULL) {
576 CopyGuid (&FvInfoPpi->FvFormat, FvFormat);
577 } else {
578 CopyGuid (&FvInfoPpi->FvFormat, &gEfiFirmwareFileSystem2Guid);
579 }
580 FvInfoPpi->FvInfo = (VOID *) FvInfo;
581 FvInfoPpi->FvInfoSize = FvInfoSize;
582 if (ParentFvName != NULL) {
583 ParentFvNameValue = AllocateCopyPool (sizeof (EFI_GUID), ParentFvName);
584 ASSERT (ParentFvNameValue != NULL);
585 FvInfoPpi->ParentFvName = ParentFvNameValue;
586 }
587 if (ParentFileName != NULL) {
588 ParentFileNameValue = AllocateCopyPool (sizeof (EFI_GUID), ParentFileName);
589 ASSERT (ParentFileNameValue != NULL);
590 FvInfoPpi->ParentFileName = ParentFileNameValue;
591 }
592
593 FvInfoPpiDescriptor = AllocateCopyPool (sizeof(EFI_PEI_PPI_DESCRIPTOR), mPpiListTemplate);
594 ASSERT (FvInfoPpiDescriptor != NULL);
595
596 FvInfoPpiDescriptor->Ppi = (VOID *) FvInfoPpi;
597 Status = PeiServicesInstallPpi (FvInfoPpiDescriptor);
598 ASSERT_EFI_ERROR (Status);
599
600 }
601