]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Foundation/Library/Pei/PeiLib/PeiLib.c
1) Make EfiCpuVersion.c pass ICC and GCC tool-chain
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / Pei / PeiLib / PeiLib.c
CommitLineData
3eb9473e 1/*++\r
2\r
3Copyright (c) 2004 - 2007, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
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
160 IN PEI_PCI_CFG_PPI_WIDTH Width,\r
161 IN UINT64 Address,\r
162 IN UINTN SetBits,\r
163 IN UINTN ClearBits\r
164 )\r
165/*++\r
166\r
167Routine Description:\r
168\r
169 PCI read-modify-write operations.\r
170\r
171 PIWG's PI specification replaces Inte's EFI Specification 1.10.\r
172 EFI_PEI_PCI_CFG_PPI defined in Inte's EFI Specification 1.10 is replaced by\r
173 EFI_PEI_PCI_CFG2_PPI in PI 1.0. "Modify" function in these two PPI are not \r
174 compatibile with each other.\r
175 \r
176\r
177 For Framework code that make the following call:\r
178\r
179 PciCfg->Modify (\r
180 PeiServices,\r
181 PciCfg,\r
182 Width,\r
183 Address,\r
184 SetBits,\r
185 ClearBits\r
186 );\r
187 it will be updated to the following code which call this library API:\r
188 PeiLibPciCfgModify (\r
189 PeiServices,\r
190 Width,\r
191 Address,\r
192 SetBits,\r
193 ClearBits\r
194 );\r
195\r
196 The \r
197\r
198Arguments:\r
199 \r
200 PeiServices An indirect pointer to the PEI Services Table\r
201 published by the PEI Foundation.\r
202\r
203 Width The width of the access. Enumerated in bytes. Type\r
204 EFI_PEI_PCI_CFG_PPI_WIDTH is defined in Read().\r
205\r
206 Address The physical address of the access.\r
207\r
208 SetBits Points to value to bitwise-OR with the read configuration value.\r
209\r
210 The size of the value is determined by Width.\r
211\r
212 ClearBits Points to the value to negate and bitwise-AND with the read configuration value.\r
213 The size of the value is determined by Width.\r
214\r
215\r
216Returns:\r
217\r
218 EFI_SUCCESS The function completed successfully.\r
219\r
220 EFI_DEVICE_ERROR There was a problem with the transaction.\r
221\r
222--*/\r
223{\r
224 EFI_STATUS Status;\r
225 EFI_PEI_PCI_CFG2_PPI *PciCfg2;\r
226\r
227 Status = (*PeiServices)->LocatePpi (\r
228 PeiServices,\r
229 &gPeiPciCfg2PpiGuid,\r
230 0,\r
231 NULL,\r
232 (VOID **) &PciCfg2\r
233 );\r
3149b377 234 ASSERT_PEI_ERROR (PeiServices, Status);\r
1e55f6a4 235\r
236 Status = PciCfg2->Modify (\r
237 PeiServices,\r
238 PciCfg2,\r
239 Width,\r
240 Address,\r
241 &SetBits,\r
242 &ClearBits\r
243 );\r
244\r
245 return Status;\r
246}\r
247\r
248\r
3eb9473e 249#if (PI_SPECIFICATION_VERSION >= 0x00010000)\r
250\r
251VOID *\r
252EFIAPI\r
253ScanGuid (\r
254 IN VOID *Buffer,\r
255 IN UINTN Length,\r
256 IN EFI_GUID *Guid\r
257 )\r
258/*++\r
259\r
260Routine Description:\r
261\r
262 Scans a target buffer for a GUID, and returns a pointer to the matching GUID\r
263 in the target buffer.\r
264\r
265 This function searches target the buffer specified by Buffer and Length from\r
266 the lowest address to the highest address at 128-bit increments for the 128-bit\r
267 GUID value that matches Guid. If a match is found, then a pointer to the matching\r
268 GUID in the target buffer is returned. If no match is found, then NULL is returned.\r
269 If Length is 0, then NULL is returned.\r
270 If Length > 0 and Buffer is NULL, then ASSERT().\r
271 If Buffer is not aligned on a 32-bit boundary, then ASSERT().\r
272 If Length is not aligned on a 128-bit boundary, then ASSERT().\r
273 If Length is greater than (EFI_MAX_ADDRESS ?Buffer + 1), then ASSERT(). \r
274\r
275Arguments:\r
276\r
277 Buffer - Pointer to the target buffer to scan.\r
278 Length - Number of bytes in Buffer to scan.\r
279 Guid - Value to search for in the target buffer.\r
280 \r
281Returns:\r
282 A pointer to the matching Guid in the target buffer or NULL otherwise.\r
283\r
284--*/\r
285{\r
286 EFI_GUID *GuidPtr;\r
287 EFI_PEI_SERVICES **PeiServices;\r
288 \r
289 PeiServices = GetPeiServicesTablePointer();\r
290 PEI_ASSERT(PeiServices, (((UINTN)Buffer & (sizeof (Guid->Data1) - 1)) == 0));\r
291 PEI_ASSERT(PeiServices, (Length <= (EFI_MAX_ADDRESS - (UINTN)Buffer + 1)));\r
292 PEI_ASSERT(PeiServices, ((Length & (sizeof (*GuidPtr) - 1)) == 0));\r
293\r
294 GuidPtr = (EFI_GUID*)Buffer;\r
295 Buffer = GuidPtr + Length / sizeof (*GuidPtr);\r
296 while (GuidPtr < (EFI_GUID*)Buffer) {\r
297 if (CompareGuid (GuidPtr, Guid)) {\r
298 return (VOID*)GuidPtr;\r
299 }\r
300 GuidPtr++;\r
301 }\r
302 return NULL;\r
303}\r
304\r
305\r
306VOID *\r
307EFIAPI\r
308InvalidateInstructionCacheRange (\r
309 IN VOID *Address,\r
310 IN UINTN Length\r
311 )\r
312/*++\r
313\r
314Routine Description:\r
315\r
316 Invalidates a range of instruction cache lines in the cache coherency domain\r
317 of the calling CPU.\r
318\r
319 Invalidates the instruction cache lines specified by Address and Length. If\r
320 Address is not aligned on a cache line boundary, then entire instruction\r
321 cache line containing Address is invalidated. If Address + Length is not\r
322 aligned on a cache line boundary, then the entire instruction cache line\r
323 containing Address + Length -1 is invalidated. This function may choose to\r
324 invalidate the entire instruction cache if that is more efficient than\r
325 invalidating the specified range. If Length is 0, the no instruction cache\r
326 lines are invalidated. Address is returned.\r
327\r
328 If Length is greater than (EFI_MAX_ADDRESS - Address + 1), then ASSERT().\r
329\r
330Arguments:\r
331\r
332 Address - The base address of the instruction cache lines to\r
333 invalidate. If the CPU is in a physical addressing mode, then\r
334 Address is a physical address. If the CPU is in a virtual\r
335 addressing mode, then Address is a virtual address.\r
336\r
337 Length - The number of bytes to invalidate from the instruction cache.\r
338\r
339 Returns:\r
340 Address\r
341\r
342**/\r
343{\r
344 PEI_ASSERT(GetPeiServicesTablePointer() , (Length <= EFI_MAX_ADDRESS - (UINTN)Address + 1));\r
345 return Address;\r
346}\r
347\r
348\r
349EFI_STATUS\r
350EFIAPI\r
351PeiLibFfsFindNextVolume (\r
352 IN UINTN Instance,\r
353 IN OUT EFI_PEI_FV_HANDLE *VolumeHandle\r
354 )\r
355/*++\r
356\r
357Routine Description:\r
358\r
359 The wrapper of Pei Core Service function FfsFindNextVolume.\r
360\r
361Arguments:\r
362\r
363 Instance - The Fv Volume Instance.\r
364 VolumeHandle - Pointer to the current Fv Volume to search.\r
365\r
366Returns:\r
367 EFI_STATUS\r
368 \r
369--*/\r
370 \r
371{\r
372 EFI_PEI_SERVICES **PeiServices;\r
373 \r
374 PeiServices = GetPeiServicesTablePointer();\r
375 return (*PeiServices)->FfsFindNextVolume (PeiServices, Instance, VolumeHandle);\r
376}\r
377\r
378EFI_STATUS\r
379EFIAPI\r
380PeiLibFfsFindNextFile (\r
381 IN EFI_FV_FILETYPE SearchType,\r
382 IN EFI_PEI_FV_HANDLE FwVolHeader,\r
383 IN OUT EFI_PEI_FILE_HANDLE *FileHeader\r
384 )\r
385/*++\r
386\r
387Routine Description:\r
388\r
389 The wrapper of Pei Core Service function FfsFindNextFile.\r
390\r
391Arguments:\r
392\r
393 SearchType - Filter to find only file of this type.\r
394 FwVolHeader - Pointer to the current FV to search.\r
395 FileHandle - Pointer to the file matching SearchType in FwVolHeader.\r
396 - NULL if file not found\r
397\r
398Returns:\r
399 EFI_STATUS\r
400 \r
401--*/ \r
402{\r
403 EFI_PEI_SERVICES **PeiServices;\r
404 \r
405 PeiServices = GetPeiServicesTablePointer();\r
406 return (*PeiServices)->FfsFindNextFile (PeiServices, SearchType, &FwVolHeader, &FileHeader);\r
407}\r
408\r
409\r
410EFI_STATUS\r
411EFIAPI\r
412PeiLibFfsFindFileByName (\r
413 IN EFI_GUID *FileName,\r
414 IN EFI_PEI_FV_HANDLE VolumeHandle,\r
415 OUT EFI_PEI_FILE_HANDLE *FileHandle\r
416 )\r
417/*++\r
418\r
419Routine Description:\r
420\r
421 The wrapper of Pei Core Service function FfsFindFileByName.\r
422\r
423Arguments:\r
424\r
425 FileName - File name to search.\r
426 VolumeHandle - The current FV to search.\r
427 FileHandle - Pointer to the file matching name in VolumeHandle.\r
428 - NULL if file not found\r
429\r
430Returns:\r
431 EFI_STATUS\r
432 \r
433--*/ \r
434{\r
435 EFI_PEI_SERVICES **PeiServices;\r
436 \r
437 PeiServices = GetPeiServicesTablePointer();\r
438 return (*PeiServices)->FfsFindFileByName (FileName, VolumeHandle, FileHandle);\r
439}\r
440\r
441\r
442\r
443EFI_STATUS\r
444EFIAPI\r
445PeiLibFfsFindSectionData (\r
446 IN EFI_SECTION_TYPE SectionType,\r
447 IN EFI_FFS_FILE_HEADER *FfsFileHeader,\r
448 IN OUT VOID **SectionData\r
449 )\r
450/*++\r
451\r
452Routine Description:\r
453\r
454 The wrapper of Pei Core Service function FfsFindSectionData.\r
455\r
456Arguments:\r
457\r
458 SearchType - Filter to find only sections of this type.\r
459 FileHandle - Pointer to the current file to search.\r
460 SectionData - Pointer to the Section matching SectionType in FfsFileHeader.\r
461 - NULL if section not found\r
462\r
463Returns:\r
464 EFI_STATUS\r
465--*/\r
466{\r
467 EFI_PEI_SERVICES **PeiServices;\r
468 \r
469 PeiServices = GetPeiServicesTablePointer();\r
470 return (*PeiServices)->FfsFindSectionData (PeiServices, SectionType, &FfsFileHeader, SectionData);\r
471}\r
472\r
473EFI_STATUS\r
474EFIAPI\r
475PeiLibFfsGetVolumeInfo (\r
476 IN EFI_PEI_FV_HANDLE *VolumeHandle,\r
477 OUT EFI_FV_INFO *VolumeInfo\r
478 )\r
479/*++\r
480\r
481Routine Description:\r
482\r
483 The wrapper of Pei Core Service function FfsGetVolumeInfo.\r
484\r
485Arguments:\r
486\r
487 VolumeHandle - The handle to Fv Volume.\r
488 VolumeInfo - The pointer to volume information.\r
489 \r
490Returns:\r
491 EFI_STATUS\r
492--*/ \r
493{\r
494 EFI_PEI_SERVICES **PeiServices;\r
495 \r
496 PeiServices = GetPeiServicesTablePointer();\r
497 return (*PeiServices)->FfsGetVolumeInfo (VolumeHandle, VolumeInfo);\r
498}\r
499\r
500\r
501\r
502VOID\r
503EFIAPI\r
504BuildFvHob (\r
505 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
506 IN UINT64 Length\r
507 )\r
508/*++\r
509\r
510Routine Description:\r
511\r
512 Build FvHob.\r
513\r
514Arguments:\r
515\r
516 BaseAddress - Fv base address.\r
517 Length - Fv Length.\r
518\r
519Returns:\r
520 NONE.\r
521--*/ \r
522{\r
523\r
524 EFI_STATUS Status; \r
525 EFI_HOB_FIRMWARE_VOLUME *Hob;\r
526 EFI_PEI_SERVICES **PeiServices;\r
527\r
528 PeiServices = GetPeiServicesTablePointer();\r
529\r
530 //\r
531 // Check FV Signature\r
532 //\r
533 PEI_ASSERT (PeiServices, ((EFI_FIRMWARE_VOLUME_HEADER*)((UINTN)BaseAddress))->Signature == EFI_FVH_SIGNATURE);\r
534\r
535\r
536 Status = (*PeiServices)->CreateHob (\r
537 PeiServices,\r
538 EFI_HOB_TYPE_FV,\r
539 sizeof (EFI_HOB_FIRMWARE_VOLUME),\r
540 &Hob\r
541 );\r
542 Hob->BaseAddress = BaseAddress;\r
543 Hob->Length = Length;\r
544}\r
545\r
546VOID\r
547EFIAPI\r
548BuildFvHob2 (\r
549 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
550 IN UINT64 Length,\r
551 IN EFI_GUID *FvNameGuid,\r
552 IN EFI_GUID *FileNameGuid\r
553 )\r
554/*++\r
555\r
556Routine Description:\r
557\r
558 Build FvHob2.\r
559\r
560Arguments:\r
561\r
562 BaseAddress - Fv base address.\r
563 Length - Fv length.\r
564 FvNameGuid - Fv name.\r
565 FileNameGuid - File name which contians encapsulated Fv.\r
566\r
567Returns:\r
568 NONE.\r
569--*/ \r
570{\r
571\r
572 EFI_STATUS Status; \r
573 EFI_HOB_FIRMWARE_VOLUME2 *Hob;\r
574 EFI_PEI_SERVICES **PeiServices;\r
575\r
576 PeiServices = GetPeiServicesTablePointer();\r
577\r
578 //\r
579 // Check FV Signature\r
580 //\r
581 PEI_ASSERT (PeiServices, ((EFI_FIRMWARE_VOLUME_HEADER*)((UINTN)BaseAddress))->Signature == EFI_FVH_SIGNATURE);\r
582\r
583 Status = (*PeiServices)->CreateHob (\r
584 PeiServices,\r
585 EFI_HOB_TYPE_FV2,\r
586 sizeof (EFI_HOB_FIRMWARE_VOLUME2),\r
587 &Hob\r
588 );\r
589 Hob->BaseAddress = BaseAddress;\r
590 Hob->Length = Length;\r
591 CopyMem ((VOID*)&Hob->FvName, FvNameGuid, sizeof(EFI_GUID));\r
592 CopyMem ((VOID*)&Hob->FileName, FileNameGuid, sizeof(EFI_GUID));\r
593}\r
594\r
595EFI_STATUS\r
596EFIAPI\r
597PeiServicesLocatePpi (\r
598 IN EFI_GUID *Guid,\r
599 IN UINTN Instance,\r
600 IN OUT EFI_PEI_PPI_DESCRIPTOR **PpiDescriptor,\r
601 IN OUT VOID **Ppi\r
602 )\r
603/*++\r
604\r
605Routine Description:\r
606\r
607 The wrapper of Pei Core Service function LocatePpi.\r
608\r
609Arguments:\r
610\r
611 Guid - Pointer to GUID of the PPI.\r
612 Instance - Instance Number to discover.\r
613 PpiDescriptor - Pointer to reference the found descriptor. If not NULL,\r
614 returns a pointer to the descriptor (includes flags, etc)\r
615 Ppi - Pointer to reference the found PPI\r
616\r
617Returns:\r
618\r
619 Status - EFI_SUCCESS if the PPI is in the database \r
620 EFI_NOT_FOUND if the PPI is not in the database\r
621--*/ \r
622{\r
623 EFI_PEI_SERVICES **PeiServices;\r
624 \r
625 PeiServices = GetPeiServicesTablePointer();\r
626 return (*PeiServices)->LocatePpi (PeiServices, Guid, Instance, PpiDescriptor, Ppi);\r
627}\r
628\r
629\r
630VOID \r
631EFIAPI\r
632BuildGuidDataHob (\r
633 IN EFI_GUID *Guid,\r
634 IN VOID *Data,\r
635 IN UINTN DataLength\r
636 )\r
637/*++\r
638\r
639Routine Description:\r
640\r
641 Build Guid data Hob.\r
642\r
643Arguments:\r
644\r
645 Guid - guid to build data hob.\r
646 Data - data to build data hob.\r
647 DataLength - the length of data.\r
648\r
649Returns:\r
650 NONE\r
651--*/ \r
652{\r
653 VOID *HobData;\r
654 EFI_HOB_GUID_TYPE *Hob;\r
655 EFI_PEI_SERVICES **PeiServices;\r
656\r
657 PeiServices = GetPeiServicesTablePointer();\r
658 (*PeiServices)->CreateHob (\r
659 PeiServices,\r
660 EFI_HOB_TYPE_GUID_EXTENSION,\r
661 (UINT16) (sizeof (EFI_HOB_GUID_TYPE) + DataLength),\r
662 &Hob\r
663 );\r
664 CopyMem ((VOID*)&Hob->Name, (VOID*)Guid, sizeof(EFI_GUID));\r
665\r
666 HobData = Hob + 1;\r
667\r
668 CopyMem (HobData, Data, DataLength);\r
669}\r
670\r
671\r
672VOID *\r
673EFIAPI\r
674AllocatePages (\r
675 IN UINTN Pages\r
676 )\r
677/*++\r
678\r
679Routine Description:\r
680\r
681 Allocate Memory.\r
682\r
683Arguments:\r
684\r
685 Pages - Pages to allocate.\r
686\r
687Returns:\r
688 = Address if successful to allocate memory. \r
689 = NULL if fail to allocate memory.\r
690\r
691--*/ \r
692{\r
693 EFI_STATUS Status;\r
694 EFI_PHYSICAL_ADDRESS Memory; \r
695 EFI_PEI_SERVICES **PeiServices;\r
696\r
697 if (Pages == 0) {\r
698 return NULL;\r
699 }\r
700\r
701 PeiServices = GetPeiServicesTablePointer();\r
702 Status = (*PeiServices)->AllocatePages (PeiServices, EfiBootServicesData, Pages, &Memory);\r
703 if (EFI_ERROR (Status)) {\r
704 Memory = 0;\r
705 }\r
706 return (VOID *) (UINTN) Memory;\r
707\r
708}\r
709\r
710#endif\r