]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Foundation/Library/Pei/PeiLib/PeiLib.c
Update the copyright notice format
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / Pei / PeiLib / PeiLib.c
CommitLineData
3eb9473e 1/*++\r
2\r
4ea9375a
HT
3Copyright (c) 2004 - 2008, Intel Corporation. All rights reserved.<BR>\r
4This program and the accompanying materials \r
3eb9473e 5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 PeiLib.c\r
15\r
16Abstract:\r
17\r
18 PEI Library Functions\r
19 \r
20--*/\r
21\r
22#include "TianoCommon.h"\r
23#include "PeiHob.h"\r
24#include "Pei.h"\r
25#include "PeiLib.h"\r
c7f33ca4 26#include "EfiCommonLib.h"\r
3eb9473e 27\r
28VOID\r
29PeiCopyMem (\r
30 IN VOID *Destination,\r
31 IN VOID *Source,\r
32 IN UINTN Length\r
33 );\r
34\r
35VOID\r
36ZeroMem (\r
37 IN VOID *Buffer,\r
38 IN UINTN Size\r
39 )\r
40/*++\r
41\r
42Routine Description:\r
43\r
44 Set Buffer to zero for Size bytes.\r
45\r
46Arguments:\r
47\r
48 Buffer - Memory to set.\r
49\r
50 Size - Number of bytes to set\r
51\r
52Returns:\r
53\r
54 None\r
55\r
56--*/\r
57{\r
c7f33ca4 58 EfiCommonLibZeroMem (Buffer, Size);\r
3eb9473e 59}\r
60\r
61VOID\r
62PeiCopyMem (\r
63 IN VOID *Destination,\r
64 IN VOID *Source,\r
65 IN UINTN Length\r
66 )\r
67/*++\r
68\r
69Routine Description:\r
70\r
71 Copy Length bytes from Source to Destination.\r
72\r
73Arguments:\r
74\r
75 Destination - Target of copy\r
76\r
77 Source - Place to copy from\r
78\r
79 Length - Number of bytes to copy\r
80\r
81Returns:\r
82\r
83 None\r
84\r
85--*/\r
86{\r
c7f33ca4 87 EfiCommonLibCopyMem (Destination, Source, Length);\r
3eb9473e 88}\r
89\r
90VOID\r
91CopyMem (\r
92 IN VOID *Destination,\r
93 IN VOID *Source,\r
94 IN UINTN Length\r
95 )\r
96/*++\r
97\r
98Routine Description:\r
99\r
100 Copy Length bytes from Source to Destination.\r
101\r
102Arguments:\r
103\r
104 Destination - Target of copy\r
105\r
106 Source - Place to copy from\r
107\r
108 Length - Number of bytes to copy\r
109\r
110Returns:\r
111\r
112 None\r
113\r
114--*/\r
115{\r
c7f33ca4 116 EfiCommonLibCopyMem (Destination, Source, Length);\r
3eb9473e 117}\r
118\r
119\r
120BOOLEAN\r
121CompareGuid (\r
122 IN EFI_GUID *Guid1,\r
123 IN EFI_GUID *Guid2\r
124 )\r
125/*++\r
126\r
127Routine Description:\r
128\r
129 Compares two GUIDs\r
130\r
131Arguments:\r
132\r
133 Guid1 - guid to compare\r
134 Guid2 - guid to compare\r
135\r
136Returns:\r
137 = TRUE if Guid1 == Guid2\r
138 = FALSE if Guid1 != Guid2 \r
139\r
140--*/\r
141{\r
142 if ((((INT32 *) Guid1)[0] - ((INT32 *) Guid2)[0]) == 0) {\r
143 if ((((INT32 *) Guid1)[1] - ((INT32 *) Guid2)[1]) == 0) {\r
144 if ((((INT32 *) Guid1)[2] - ((INT32 *) Guid2)[2]) == 0) {\r
145 if ((((INT32 *) Guid1)[3] - ((INT32 *) Guid2)[3]) == 0) {\r
146 return TRUE;\r
147 }\r
148 }\r
149 }\r
150 }\r
151\r
152 return FALSE;\r
153}\r
154\r
155\r
1e55f6a4 156EFI_STATUS\r
157EFIAPI \r
158PeiLibPciCfgModify (\r
159 IN EFI_PEI_SERVICES **PeiServices,\r
af1b1036 160 IN PEI_PCI_CFG_PPI *PciCfg,\r
1e55f6a4 161 IN PEI_PCI_CFG_PPI_WIDTH Width,\r
162 IN UINT64 Address,\r
163 IN UINTN SetBits,\r
164 IN UINTN ClearBits\r
165 )\r
166/*++\r
167\r
168Routine Description:\r
169\r
170 PCI read-modify-write operations.\r
171\r
172 PIWG's PI specification replaces Inte's EFI Specification 1.10.\r
173 EFI_PEI_PCI_CFG_PPI defined in Inte's EFI Specification 1.10 is replaced by\r
174 EFI_PEI_PCI_CFG2_PPI in PI 1.0. "Modify" function in these two PPI are not \r
175 compatibile with each other.\r
176 \r
177\r
178 For Framework code that make the following call:\r
179\r
180 PciCfg->Modify (\r
181 PeiServices,\r
182 PciCfg,\r
183 Width,\r
184 Address,\r
185 SetBits,\r
186 ClearBits\r
187 );\r
188 it will be updated to the following code which call this library API:\r
189 PeiLibPciCfgModify (\r
190 PeiServices,\r
af1b1036 191 PciCfg,\r
1e55f6a4 192 Width,\r
193 Address,\r
194 SetBits,\r
195 ClearBits\r
196 );\r
197\r
198 The \r
199\r
200Arguments:\r
201 \r
202 PeiServices An indirect pointer to the PEI Services Table\r
203 published by the PEI Foundation.\r
af1b1036 204 PciCfg A pointer to the this pointer of EFI_PEI_PCI_CFG_PPI. \r
205 This parameter is unused as a place holder to make\r
206 the parameter list identical to PEI_PCI_CFG_PPI_RW.\r
1e55f6a4 207 Width The width of the access. Enumerated in bytes. Type\r
208 EFI_PEI_PCI_CFG_PPI_WIDTH is defined in Read().\r
209\r
210 Address The physical address of the access.\r
211\r
212 SetBits Points to value to bitwise-OR with the read configuration value.\r
213\r
214 The size of the value is determined by Width.\r
215\r
216 ClearBits Points to the value to negate and bitwise-AND with the read configuration value.\r
217 The size of the value is determined by Width.\r
218\r
219\r
220Returns:\r
221\r
222 EFI_SUCCESS The function completed successfully.\r
223\r
224 EFI_DEVICE_ERROR There was a problem with the transaction.\r
225\r
226--*/\r
227{\r
228 EFI_STATUS Status;\r
229 EFI_PEI_PCI_CFG2_PPI *PciCfg2;\r
230\r
231 Status = (*PeiServices)->LocatePpi (\r
232 PeiServices,\r
233 &gPeiPciCfg2PpiGuid,\r
234 0,\r
235 NULL,\r
236 (VOID **) &PciCfg2\r
237 );\r
78d91947 238 ASSERT_PEI_ERROR ((CONST EFI_PEI_SERVICES **) PeiServices, Status);\r
1e55f6a4 239\r
240 Status = PciCfg2->Modify (\r
4b79797e 241 (CONST EFI_PEI_SERVICES **) PeiServices,\r
1e55f6a4 242 PciCfg2,\r
4b79797e 243 (EFI_PEI_PCI_CFG_PPI_WIDTH) Width,\r
1e55f6a4 244 Address,\r
245 &SetBits,\r
246 &ClearBits\r
247 );\r
248\r
249 return Status;\r
250}\r
251\r
252\r
3eb9473e 253#if (PI_SPECIFICATION_VERSION >= 0x00010000)\r
254\r
255VOID *\r
256EFIAPI\r
257ScanGuid (\r
258 IN VOID *Buffer,\r
259 IN UINTN Length,\r
260 IN EFI_GUID *Guid\r
261 )\r
262/*++\r
263\r
264Routine Description:\r
265\r
266 Scans a target buffer for a GUID, and returns a pointer to the matching GUID\r
267 in the target buffer.\r
268\r
269 This function searches target the buffer specified by Buffer and Length from\r
270 the lowest address to the highest address at 128-bit increments for the 128-bit\r
271 GUID value that matches Guid. If a match is found, then a pointer to the matching\r
272 GUID in the target buffer is returned. If no match is found, then NULL is returned.\r
273 If Length is 0, then NULL is returned.\r
274 If Length > 0 and Buffer is NULL, then ASSERT().\r
275 If Buffer is not aligned on a 32-bit boundary, then ASSERT().\r
276 If Length is not aligned on a 128-bit boundary, then ASSERT().\r
277 If Length is greater than (EFI_MAX_ADDRESS ?Buffer + 1), then ASSERT(). \r
278\r
279Arguments:\r
280\r
281 Buffer - Pointer to the target buffer to scan.\r
282 Length - Number of bytes in Buffer to scan.\r
283 Guid - Value to search for in the target buffer.\r
284 \r
285Returns:\r
286 A pointer to the matching Guid in the target buffer or NULL otherwise.\r
287\r
288--*/\r
289{\r
290 EFI_GUID *GuidPtr;\r
291 EFI_PEI_SERVICES **PeiServices;\r
292 \r
293 PeiServices = GetPeiServicesTablePointer();\r
294 PEI_ASSERT(PeiServices, (((UINTN)Buffer & (sizeof (Guid->Data1) - 1)) == 0));\r
295 PEI_ASSERT(PeiServices, (Length <= (EFI_MAX_ADDRESS - (UINTN)Buffer + 1)));\r
296 PEI_ASSERT(PeiServices, ((Length & (sizeof (*GuidPtr) - 1)) == 0));\r
297\r
298 GuidPtr = (EFI_GUID*)Buffer;\r
299 Buffer = GuidPtr + Length / sizeof (*GuidPtr);\r
300 while (GuidPtr < (EFI_GUID*)Buffer) {\r
301 if (CompareGuid (GuidPtr, Guid)) {\r
302 return (VOID*)GuidPtr;\r
303 }\r
304 GuidPtr++;\r
305 }\r
306 return NULL;\r
307}\r
308\r
309\r
310VOID *\r
311EFIAPI\r
312InvalidateInstructionCacheRange (\r
313 IN VOID *Address,\r
314 IN UINTN Length\r
315 )\r
316/*++\r
317\r
318Routine Description:\r
319\r
320 Invalidates a range of instruction cache lines in the cache coherency domain\r
321 of the calling CPU.\r
322\r
323 Invalidates the instruction cache lines specified by Address and Length. If\r
324 Address is not aligned on a cache line boundary, then entire instruction\r
325 cache line containing Address is invalidated. If Address + Length is not\r
326 aligned on a cache line boundary, then the entire instruction cache line\r
327 containing Address + Length -1 is invalidated. This function may choose to\r
328 invalidate the entire instruction cache if that is more efficient than\r
329 invalidating the specified range. If Length is 0, the no instruction cache\r
330 lines are invalidated. Address is returned.\r
331\r
332 If Length is greater than (EFI_MAX_ADDRESS - Address + 1), then ASSERT().\r
333\r
334Arguments:\r
335\r
336 Address - The base address of the instruction cache lines to\r
337 invalidate. If the CPU is in a physical addressing mode, then\r
338 Address is a physical address. If the CPU is in a virtual\r
339 addressing mode, then Address is a virtual address.\r
340\r
341 Length - The number of bytes to invalidate from the instruction cache.\r
342\r
343 Returns:\r
344 Address\r
345\r
346**/\r
347{\r
348 PEI_ASSERT(GetPeiServicesTablePointer() , (Length <= EFI_MAX_ADDRESS - (UINTN)Address + 1));\r
349 return Address;\r
350}\r
351\r
352\r
353EFI_STATUS\r
354EFIAPI\r
355PeiLibFfsFindNextVolume (\r
356 IN UINTN Instance,\r
357 IN OUT EFI_PEI_FV_HANDLE *VolumeHandle\r
358 )\r
359/*++\r
360\r
361Routine Description:\r
362\r
363 The wrapper of Pei Core Service function FfsFindNextVolume.\r
364\r
365Arguments:\r
366\r
367 Instance - The Fv Volume Instance.\r
368 VolumeHandle - Pointer to the current Fv Volume to search.\r
369\r
370Returns:\r
371 EFI_STATUS\r
372 \r
373--*/\r
374 \r
375{\r
376 EFI_PEI_SERVICES **PeiServices;\r
377 \r
378 PeiServices = GetPeiServicesTablePointer();\r
379 return (*PeiServices)->FfsFindNextVolume (PeiServices, Instance, VolumeHandle);\r
380}\r
381\r
382EFI_STATUS\r
383EFIAPI\r
384PeiLibFfsFindNextFile (\r
385 IN EFI_FV_FILETYPE SearchType,\r
386 IN EFI_PEI_FV_HANDLE FwVolHeader,\r
387 IN OUT EFI_PEI_FILE_HANDLE *FileHeader\r
388 )\r
389/*++\r
390\r
391Routine Description:\r
392\r
393 The wrapper of Pei Core Service function FfsFindNextFile.\r
394\r
395Arguments:\r
396\r
397 SearchType - Filter to find only file of this type.\r
398 FwVolHeader - Pointer to the current FV to search.\r
399 FileHandle - Pointer to the file matching SearchType in FwVolHeader.\r
400 - NULL if file not found\r
401\r
402Returns:\r
403 EFI_STATUS\r
404 \r
405--*/ \r
406{\r
407 EFI_PEI_SERVICES **PeiServices;\r
408 \r
409 PeiServices = GetPeiServicesTablePointer();\r
410 return (*PeiServices)->FfsFindNextFile (PeiServices, SearchType, &FwVolHeader, &FileHeader);\r
411}\r
412\r
413\r
414EFI_STATUS\r
415EFIAPI\r
416PeiLibFfsFindFileByName (\r
417 IN EFI_GUID *FileName,\r
418 IN EFI_PEI_FV_HANDLE VolumeHandle,\r
419 OUT EFI_PEI_FILE_HANDLE *FileHandle\r
420 )\r
421/*++\r
422\r
423Routine Description:\r
424\r
425 The wrapper of Pei Core Service function FfsFindFileByName.\r
426\r
427Arguments:\r
428\r
429 FileName - File name to search.\r
430 VolumeHandle - The current FV to search.\r
431 FileHandle - Pointer to the file matching name in VolumeHandle.\r
432 - NULL if file not found\r
433\r
434Returns:\r
435 EFI_STATUS\r
436 \r
437--*/ \r
438{\r
439 EFI_PEI_SERVICES **PeiServices;\r
440 \r
441 PeiServices = GetPeiServicesTablePointer();\r
442 return (*PeiServices)->FfsFindFileByName (FileName, VolumeHandle, FileHandle);\r
443}\r
444\r
445\r
446\r
447EFI_STATUS\r
448EFIAPI\r
449PeiLibFfsFindSectionData (\r
450 IN EFI_SECTION_TYPE SectionType,\r
451 IN EFI_FFS_FILE_HEADER *FfsFileHeader,\r
452 IN OUT VOID **SectionData\r
453 )\r
454/*++\r
455\r
456Routine Description:\r
457\r
458 The wrapper of Pei Core Service function FfsFindSectionData.\r
459\r
460Arguments:\r
461\r
462 SearchType - Filter to find only sections of this type.\r
463 FileHandle - Pointer to the current file to search.\r
464 SectionData - Pointer to the Section matching SectionType in FfsFileHeader.\r
465 - NULL if section not found\r
466\r
467Returns:\r
468 EFI_STATUS\r
469--*/\r
470{\r
471 EFI_PEI_SERVICES **PeiServices;\r
472 \r
473 PeiServices = GetPeiServicesTablePointer();\r
474 return (*PeiServices)->FfsFindSectionData (PeiServices, SectionType, &FfsFileHeader, SectionData);\r
475}\r
476\r
477EFI_STATUS\r
478EFIAPI\r
479PeiLibFfsGetVolumeInfo (\r
480 IN EFI_PEI_FV_HANDLE *VolumeHandle,\r
481 OUT EFI_FV_INFO *VolumeInfo\r
482 )\r
483/*++\r
484\r
485Routine Description:\r
486\r
487 The wrapper of Pei Core Service function FfsGetVolumeInfo.\r
488\r
489Arguments:\r
490\r
491 VolumeHandle - The handle to Fv Volume.\r
492 VolumeInfo - The pointer to volume information.\r
493 \r
494Returns:\r
495 EFI_STATUS\r
496--*/ \r
497{\r
498 EFI_PEI_SERVICES **PeiServices;\r
499 \r
500 PeiServices = GetPeiServicesTablePointer();\r
501 return (*PeiServices)->FfsGetVolumeInfo (VolumeHandle, VolumeInfo);\r
502}\r
503\r
504\r
505\r
506VOID\r
507EFIAPI\r
508BuildFvHob (\r
509 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
510 IN UINT64 Length\r
511 )\r
512/*++\r
513\r
514Routine Description:\r
515\r
516 Build FvHob.\r
517\r
518Arguments:\r
519\r
520 BaseAddress - Fv base address.\r
521 Length - Fv Length.\r
522\r
523Returns:\r
524 NONE.\r
525--*/ \r
526{\r
527\r
528 EFI_STATUS Status; \r
529 EFI_HOB_FIRMWARE_VOLUME *Hob;\r
530 EFI_PEI_SERVICES **PeiServices;\r
531\r
532 PeiServices = GetPeiServicesTablePointer();\r
533\r
534 //\r
535 // Check FV Signature\r
536 //\r
537 PEI_ASSERT (PeiServices, ((EFI_FIRMWARE_VOLUME_HEADER*)((UINTN)BaseAddress))->Signature == EFI_FVH_SIGNATURE);\r
538\r
539\r
540 Status = (*PeiServices)->CreateHob (\r
541 PeiServices,\r
542 EFI_HOB_TYPE_FV,\r
543 sizeof (EFI_HOB_FIRMWARE_VOLUME),\r
544 &Hob\r
545 );\r
546 Hob->BaseAddress = BaseAddress;\r
547 Hob->Length = Length;\r
548}\r
549\r
550VOID\r
551EFIAPI\r
552BuildFvHob2 (\r
553 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
554 IN UINT64 Length,\r
555 IN EFI_GUID *FvNameGuid,\r
556 IN EFI_GUID *FileNameGuid\r
557 )\r
558/*++\r
559\r
560Routine Description:\r
561\r
562 Build FvHob2.\r
563\r
564Arguments:\r
565\r
566 BaseAddress - Fv base address.\r
567 Length - Fv length.\r
568 FvNameGuid - Fv name.\r
569 FileNameGuid - File name which contians encapsulated Fv.\r
570\r
571Returns:\r
572 NONE.\r
573--*/ \r
574{\r
575\r
576 EFI_STATUS Status; \r
577 EFI_HOB_FIRMWARE_VOLUME2 *Hob;\r
578 EFI_PEI_SERVICES **PeiServices;\r
579\r
580 PeiServices = GetPeiServicesTablePointer();\r
581\r
582 //\r
583 // Check FV Signature\r
584 //\r
585 PEI_ASSERT (PeiServices, ((EFI_FIRMWARE_VOLUME_HEADER*)((UINTN)BaseAddress))->Signature == EFI_FVH_SIGNATURE);\r
586\r
587 Status = (*PeiServices)->CreateHob (\r
588 PeiServices,\r
589 EFI_HOB_TYPE_FV2,\r
590 sizeof (EFI_HOB_FIRMWARE_VOLUME2),\r
591 &Hob\r
592 );\r
593 Hob->BaseAddress = BaseAddress;\r
594 Hob->Length = Length;\r
595 CopyMem ((VOID*)&Hob->FvName, FvNameGuid, sizeof(EFI_GUID));\r
596 CopyMem ((VOID*)&Hob->FileName, FileNameGuid, sizeof(EFI_GUID));\r
597}\r
598\r
599EFI_STATUS\r
600EFIAPI\r
601PeiServicesLocatePpi (\r
602 IN EFI_GUID *Guid,\r
603 IN UINTN Instance,\r
604 IN OUT EFI_PEI_PPI_DESCRIPTOR **PpiDescriptor,\r
605 IN OUT VOID **Ppi\r
606 )\r
607/*++\r
608\r
609Routine Description:\r
610\r
611 The wrapper of Pei Core Service function LocatePpi.\r
612\r
613Arguments:\r
614\r
615 Guid - Pointer to GUID of the PPI.\r
616 Instance - Instance Number to discover.\r
617 PpiDescriptor - Pointer to reference the found descriptor. If not NULL,\r
618 returns a pointer to the descriptor (includes flags, etc)\r
619 Ppi - Pointer to reference the found PPI\r
620\r
621Returns:\r
622\r
623 Status - EFI_SUCCESS if the PPI is in the database \r
624 EFI_NOT_FOUND if the PPI is not in the database\r
625--*/ \r
626{\r
627 EFI_PEI_SERVICES **PeiServices;\r
628 \r
629 PeiServices = GetPeiServicesTablePointer();\r
630 return (*PeiServices)->LocatePpi (PeiServices, Guid, Instance, PpiDescriptor, Ppi);\r
631}\r
632\r
633\r
634VOID \r
635EFIAPI\r
636BuildGuidDataHob (\r
637 IN EFI_GUID *Guid,\r
638 IN VOID *Data,\r
639 IN UINTN DataLength\r
640 )\r
641/*++\r
642\r
643Routine Description:\r
644\r
645 Build Guid data Hob.\r
646\r
647Arguments:\r
648\r
649 Guid - guid to build data hob.\r
650 Data - data to build data hob.\r
651 DataLength - the length of data.\r
652\r
653Returns:\r
654 NONE\r
655--*/ \r
656{\r
657 VOID *HobData;\r
658 EFI_HOB_GUID_TYPE *Hob;\r
659 EFI_PEI_SERVICES **PeiServices;\r
660\r
661 PeiServices = GetPeiServicesTablePointer();\r
662 (*PeiServices)->CreateHob (\r
663 PeiServices,\r
664 EFI_HOB_TYPE_GUID_EXTENSION,\r
665 (UINT16) (sizeof (EFI_HOB_GUID_TYPE) + DataLength),\r
666 &Hob\r
667 );\r
668 CopyMem ((VOID*)&Hob->Name, (VOID*)Guid, sizeof(EFI_GUID));\r
669\r
670 HobData = Hob + 1;\r
671\r
672 CopyMem (HobData, Data, DataLength);\r
673}\r
674\r
675\r
676VOID *\r
677EFIAPI\r
678AllocatePages (\r
679 IN UINTN Pages\r
680 )\r
681/*++\r
682\r
683Routine Description:\r
684\r
685 Allocate Memory.\r
686\r
687Arguments:\r
688\r
689 Pages - Pages to allocate.\r
690\r
691Returns:\r
692 = Address if successful to allocate memory. \r
693 = NULL if fail to allocate memory.\r
694\r
695--*/ \r
696{\r
697 EFI_STATUS Status;\r
698 EFI_PHYSICAL_ADDRESS Memory; \r
699 EFI_PEI_SERVICES **PeiServices;\r
700\r
701 if (Pages == 0) {\r
702 return NULL;\r
703 }\r
704\r
705 PeiServices = GetPeiServicesTablePointer();\r
706 Status = (*PeiServices)->AllocatePages (PeiServices, EfiBootServicesData, Pages, &Memory);\r
707 if (EFI_ERROR (Status)) {\r
708 Memory = 0;\r
709 }\r
710 return (VOID *) (UINTN) Memory;\r
711\r
712}\r
713\r
714#endif\r