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