]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Foundation/Library/EdkIIGlueLib/Library/PeiHobLib/HobLib.c
Update the copyright notice format
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / EdkIIGlueLib / Library / PeiHobLib / HobLib.c
CommitLineData
3eb9473e 1/*++\r
2\r
2c7e5c2f
HT
3Copyright (c) 2004 - 2007, 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
12\r
13Module Name:\r
14\r
15 HobLib.c\r
16 \r
17Abstract: \r
18\r
19 HOB Library.\r
20\r
21--*/\r
22\r
23#include "EdkIIGluePeim.h"\r
24\r
25/**\r
26 Returns the pointer to the HOB list.\r
27\r
28 This function returns the pointer to first HOB in the list.\r
29\r
30 @return The pointer to the HOB list.\r
31\r
32**/\r
33VOID *\r
34EFIAPI\r
35GetHobList (\r
36 VOID\r
37 )\r
38{\r
39 EFI_STATUS Status;\r
40 VOID *HobList;\r
41\r
42 Status = PeiServicesGetHobList (&HobList);\r
43 ASSERT_EFI_ERROR (Status);\r
44 ASSERT (HobList != NULL);\r
45\r
46 return HobList;\r
47}\r
48\r
49/**\r
50 Returns the next instance of a HOB type from the starting HOB.\r
51\r
52 This function searches the first instance of a HOB type from the starting HOB pointer. \r
53 If there does not exist such HOB type from the starting HOB pointer, it will return NULL.\r
54 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer\r
55 unconditionally: it returns HobStart back if HobStart itself meets the requirement;\r
56 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.\r
57 If HobStart is NULL, then ASSERT().\r
58\r
59 @param Type The HOB type to return.\r
60 @param HobStart The starting HOB pointer to search from.\r
61\r
62 @return The next instance of a HOB type from the starting HOB.\r
63\r
64**/\r
65VOID *\r
66EFIAPI\r
67GetNextHob (\r
68 IN UINT16 Type,\r
69 IN CONST VOID *HobStart\r
70 )\r
71{\r
72 EFI_PEI_HOB_POINTERS Hob;\r
73\r
74 ASSERT (HobStart != NULL);\r
75 \r
76 Hob.Raw = (UINT8 *) HobStart;\r
77 //\r
78 // Parse the HOB list until end of list or matching type is found.\r
79 //\r
80 while (!END_OF_HOB_LIST (Hob)) {\r
81 if (Hob.Header->HobType == Type) {\r
82 return Hob.Raw;\r
83 }\r
84 Hob.Raw = GET_NEXT_HOB (Hob);\r
85 }\r
86 return NULL;\r
87}\r
88\r
89/**\r
90 Returns the first instance of a HOB type among the whole HOB list.\r
91\r
92 This function searches the first instance of a HOB type among the whole HOB list. \r
93 If there does not exist such HOB type in the HOB list, it will return NULL. \r
94\r
95 @param Type The HOB type to return.\r
96\r
97 @return The next instance of a HOB type from the starting HOB.\r
98\r
99**/\r
100VOID *\r
101EFIAPI\r
102GetFirstHob (\r
103 IN UINT16 Type\r
104 )\r
105{\r
106 VOID *HobList;\r
107\r
108 HobList = GetHobList ();\r
109 return GetNextHob (Type, HobList);\r
110}\r
111\r
112/**\r
113 This function searches the first instance of a HOB from the starting HOB pointer. \r
114 Such HOB should satisfy two conditions: \r
115 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid. \r
116 If there does not exist such HOB from the starting HOB pointer, it will return NULL. \r
117 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()\r
118 to extract the data section and its size info respectively.\r
119 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer\r
120 unconditionally: it returns HobStart back if HobStart itself meets the requirement;\r
121 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.\r
122 If Guid is NULL, then ASSERT().\r
123 If HobStart is NULL, then ASSERT().\r
124\r
125 @param Guid The GUID to match with in the HOB list.\r
126 @param HobStart A pointer to a Guid.\r
127\r
128 @return The next instance of the matched GUID HOB from the starting HOB.\r
129\r
130**/\r
131VOID *\r
132EFIAPI\r
133GlueGetNextGuidHob (\r
134 IN CONST EFI_GUID *Guid,\r
135 IN CONST VOID *HobStart\r
136 )\r
137{\r
138 EFI_PEI_HOB_POINTERS GuidHob;\r
139\r
140 GuidHob.Raw = (UINT8 *) HobStart;\r
141 while ((GuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, GuidHob.Raw)) != NULL) {\r
142 if (CompareGuid (Guid, &GuidHob.Guid->Name)) {\r
143 break;\r
144 }\r
145 GuidHob.Raw = GET_NEXT_HOB (GuidHob);\r
146 }\r
147 return GuidHob.Raw;\r
148}\r
149\r
150/**\r
151 This function searches the first instance of a HOB among the whole HOB list. \r
152 Such HOB should satisfy two conditions:\r
153 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.\r
154 If there does not exist such HOB from the starting HOB pointer, it will return NULL.\r
155 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()\r
156 to extract the data section and its size info respectively.\r
157 If Guid is NULL, then ASSERT().\r
158\r
159 @param Guid The GUID to match with in the HOB list.\r
160\r
161 @return The first instance of the matched GUID HOB among the whole HOB list.\r
162\r
163**/\r
164VOID *\r
165EFIAPI\r
166GlueGetFirstGuidHob (\r
167 IN CONST EFI_GUID *Guid\r
168 )\r
169{\r
170 VOID *HobList;\r
171\r
172 HobList = GetHobList ();\r
173 return GetNextGuidHob (Guid, HobList);\r
174}\r
175\r
176/**\r
177 Get the Boot Mode from the HOB list.\r
178\r
179 This function returns the system boot mode information from the \r
180 PHIT HOB in HOB list.\r
181\r
182 @param VOID\r
183\r
184 @return The Boot Mode.\r
185\r
186**/\r
187EFI_BOOT_MODE\r
188EFIAPI\r
189GetBootModeHob (\r
190 VOID\r
191 )\r
192{\r
193 EFI_STATUS Status;\r
194 EFI_BOOT_MODE BootMode;\r
195\r
196 Status = PeiServicesGetBootMode (&BootMode);\r
197 ASSERT_EFI_ERROR (Status);\r
198\r
199 return BootMode;\r
200}\r
201\r
202/**\r
203 Adds a new HOB to the HOB List.\r
204\r
205 This internal function enables PEIMs to create various types of HOBs.\r
206\r
207 @param Type Type of the new HOB.\r
208 @param Length Length of the new HOB to allocate.\r
209\r
210 @return The address of new HOB.\r
211\r
212**/\r
213STATIC\r
214VOID *\r
215InternalPeiCreateHob (\r
216 IN UINT16 Type,\r
217 IN UINT16 Length\r
218 )\r
219{\r
220 EFI_STATUS Status;\r
221 VOID *Hob;\r
222\r
223 Status = PeiServicesCreateHob (Type, Length, &Hob);\r
224 //\r
225 // Assume the process of HOB building is always successful.\r
226 //\r
227 ASSERT_EFI_ERROR (Status);\r
228 return Hob;\r
229}\r
230\r
231/**\r
232 Builds a HOB for a loaded PE32 module.\r
233\r
234 This function builds a HOB for a loaded PE32 module.\r
235 It can only be invoked during PEI phase;\r
236 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
237 If ModuleName is NULL, then ASSERT().\r
238 If there is no additional space for HOB creation, then ASSERT().\r
239\r
240 @param ModuleName The GUID File Name of the module.\r
241 @param MemoryAllocationModule The 64 bit physical address of the module.\r
242 @param ModuleLength The length of the module in bytes.\r
243 @param EntryPoint The 64 bit physical address of the module's entry point.\r
244\r
245**/\r
246VOID\r
247EFIAPI\r
248GlueBuildModuleHob (\r
249 IN CONST EFI_GUID *ModuleName,\r
250 IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule,\r
251 IN UINT64 ModuleLength,\r
252 IN EFI_PHYSICAL_ADDRESS EntryPoint\r
253 )\r
254{\r
255 EFI_HOB_MEMORY_ALLOCATION_MODULE *Hob;\r
256\r
257 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE));\r
258\r
259 CopyGuid (&(Hob->MemoryAllocationHeader.Name), &gEfiHobMemoryAllocModuleGuid);\r
260 Hob->MemoryAllocationHeader.MemoryBaseAddress = MemoryAllocationModule;\r
261 Hob->MemoryAllocationHeader.MemoryLength = ModuleLength;\r
262 Hob->MemoryAllocationHeader.MemoryType = EfiBootServicesCode;\r
263\r
264 //\r
265 // Zero the reserved space to match HOB spec\r
266 //\r
267 ZeroMem (Hob->MemoryAllocationHeader.Reserved, sizeof (Hob->MemoryAllocationHeader.Reserved));\r
268 \r
269 CopyGuid (&Hob->ModuleName, ModuleName);\r
270 Hob->EntryPoint = EntryPoint;\r
271}\r
272\r
273/**\r
274 Builds a HOB that describes a chunk of system memory.\r
275\r
276 This function builds a HOB that describes a chunk of system memory.\r
277 It can only be invoked during PEI phase;\r
278 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
279 If there is no additional space for HOB creation, then ASSERT().\r
280\r
281 @param ResourceType The type of resource described by this HOB.\r
282 @param ResourceAttribute The resource attributes of the memory described by this HOB.\r
283 @param PhysicalStart The 64 bit physical address of memory described by this HOB.\r
284 @param NumberOfBytes The length of the memory described by this HOB in bytes.\r
285\r
286**/\r
287VOID\r
288EFIAPI\r
289BuildResourceDescriptorHob (\r
290 IN EFI_RESOURCE_TYPE ResourceType,\r
291 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute,\r
292 IN EFI_PHYSICAL_ADDRESS PhysicalStart,\r
293 IN UINT64 NumberOfBytes\r
294 )\r
295{\r
296 EFI_HOB_RESOURCE_DESCRIPTOR *Hob;\r
297\r
298 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR));\r
299\r
300 Hob->ResourceType = ResourceType;\r
301 Hob->ResourceAttribute = ResourceAttribute;\r
302 Hob->PhysicalStart = PhysicalStart;\r
303 Hob->ResourceLength = NumberOfBytes;\r
304}\r
305\r
306/**\r
307 Builds a GUID HOB with a certain data length.\r
308\r
309 This function builds a customized HOB tagged with a GUID for identification \r
310 and returns the start address of GUID HOB data so that caller can fill the customized data. \r
311 The HOB Header and Name field is already stripped.\r
312 It can only be invoked during PEI phase;\r
313 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
314 If Guid is NULL, then ASSERT().\r
315 If there is no additional space for HOB creation, then ASSERT().\r
316 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().\r
317\r
318 @param Guid The GUID to tag the customized HOB.\r
319 @param DataLength The size of the data payload for the GUID HOB.\r
320\r
321 @return The start address of GUID HOB data.\r
322\r
323**/\r
324VOID *\r
325EFIAPI\r
326BuildGuidHob (\r
327 IN CONST EFI_GUID *Guid,\r
328 IN UINTN DataLength\r
329 )\r
330{\r
331 EFI_HOB_GUID_TYPE *Hob;\r
332\r
333 //\r
334 // Make sure that data length is not too long.\r
335 //\r
336 ASSERT (DataLength <= (0xffff - sizeof (EFI_HOB_GUID_TYPE)));\r
337\r
338 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16) (sizeof (EFI_HOB_GUID_TYPE) + DataLength));\r
339 CopyGuid (&Hob->Name, Guid);\r
340 return Hob + 1;\r
341}\r
342\r
343/**\r
344 Copies a data buffer to a newly-built HOB.\r
345\r
346 This function builds a customized HOB tagged with a GUID for identification,\r
347 copies the input data to the HOB data field and returns the start address of the GUID HOB data.\r
348 The HOB Header and Name field is already stripped.\r
349 It can only be invoked during PEI phase;\r
350 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
351 If Guid is NULL, then ASSERT().\r
352 If Data is NULL and DataLength > 0, then ASSERT().\r
353 If there is no additional space for HOB creation, then ASSERT().\r
354 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().\r
355\r
356 @param Guid The GUID to tag the customized HOB.\r
357 @param Data The data to be copied into the data field of the GUID HOB.\r
358 @param DataLength The size of the data payload for the GUID HOB.\r
359\r
360 @return The start address of GUID HOB data.\r
361\r
362**/\r
363VOID *\r
364EFIAPI\r
365BuildGuidDataHob (\r
366 IN CONST EFI_GUID *Guid,\r
367 IN VOID *Data,\r
368 IN UINTN DataLength\r
369 )\r
370{\r
371 VOID *HobData;\r
372\r
373 ASSERT (Data != NULL || DataLength == 0);\r
374\r
375 HobData = BuildGuidHob (Guid, DataLength);\r
376\r
377 return CopyMem (HobData, Data, DataLength);\r
378}\r
379\r
380/**\r
381 Builds a Firmware Volume HOB.\r
382\r
383 This function builds a Firmware Volume HOB.\r
384 It can only be invoked during PEI phase;\r
385 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
386 If there is no additional space for HOB creation, then ASSERT().\r
387\r
388 @param BaseAddress The base address of the Firmware Volume.\r
389 @param Length The size of the Firmware Volume in bytes.\r
390\r
391**/\r
392VOID\r
393EFIAPI\r
394BuildFvHob (\r
395 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
396 IN UINT64 Length\r
397 )\r
398{\r
399 EFI_HOB_FIRMWARE_VOLUME *Hob;\r
400\r
401 //\r
402 // Check FV Signature\r
403 //\r
404 ASSERT (((EFI_FIRMWARE_VOLUME_HEADER*)((UINTN)BaseAddress))->Signature == EFI_FVH_SIGNATURE);\r
405 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_FV, sizeof (EFI_HOB_FIRMWARE_VOLUME));\r
406\r
407 Hob->BaseAddress = BaseAddress;\r
408 Hob->Length = Length;\r
409}\r
410\r
411/**\r
412 Builds a Capsule Volume HOB.\r
413\r
414 This function builds a Capsule Volume HOB.\r
415 It can only be invoked during PEI phase;\r
416 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
417 If there is no additional space for HOB creation, then ASSERT().\r
418\r
419 @param BaseAddress The base address of the Capsule Volume.\r
420 @param Length The size of the Capsule Volume in bytes.\r
421\r
422**/\r
423VOID\r
424EFIAPI\r
425BuildCvHob (\r
426 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
427 IN UINT64 Length\r
428 )\r
429{\r
430 EFI_HOB_CAPSULE_VOLUME *Hob;\r
431\r
432 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_CV, sizeof (EFI_HOB_CAPSULE_VOLUME));\r
433\r
434 Hob->BaseAddress = BaseAddress;\r
435 Hob->Length = Length;\r
436}\r
437\r
438/**\r
439 Builds a HOB for the CPU.\r
440\r
441 This function builds a HOB for the CPU.\r
442 It can only be invoked during PEI phase;\r
443 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
444 If there is no additional space for HOB creation, then ASSERT().\r
445\r
446 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.\r
447 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.\r
448\r
449**/\r
450VOID\r
451EFIAPI\r
452BuildCpuHob (\r
453 IN UINT8 SizeOfMemorySpace,\r
454 IN UINT8 SizeOfIoSpace\r
455 )\r
456{\r
457 EFI_HOB_CPU *Hob;\r
458\r
459 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));\r
460\r
461 Hob->SizeOfMemorySpace = SizeOfMemorySpace;\r
462 Hob->SizeOfIoSpace = SizeOfIoSpace;\r
463\r
464 //\r
465 // Zero the reserved space to match HOB spec\r
466 //\r
467 ZeroMem (Hob->Reserved, sizeof (Hob->Reserved)); \r
468}\r
469\r
470/**\r
471 Builds a HOB for the Stack.\r
472\r
473 This function builds a HOB for the stack.\r
474 It can only be invoked during PEI phase;\r
475 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
476 If there is no additional space for HOB creation, then ASSERT().\r
477\r
478 @param BaseAddress The 64 bit physical address of the Stack.\r
479 @param Length The length of the stack in bytes.\r
480\r
481**/\r
482VOID\r
483EFIAPI\r
484BuildStackHob (\r
485 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
486 IN UINT64 Length\r
487 )\r
488{\r
489 EFI_HOB_MEMORY_ALLOCATION_STACK *Hob;\r
490\r
491 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_STACK));\r
492\r
493 CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocStackGuid);\r
494 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;\r
495 Hob->AllocDescriptor.MemoryLength = Length;\r
c7f33ca4 496 Hob->AllocDescriptor.MemoryType = EfiBootServicesData;\r
3eb9473e 497\r
498 //\r
499 // Zero the reserved space to match HOB spec\r
500 //\r
501 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));\r
502}\r
503\r
504/**\r
505 Builds a HOB for the BSP store.\r
506\r
507 This function builds a HOB for BSP store.\r
508 It can only be invoked during PEI phase;\r
509 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
510 If there is no additional space for HOB creation, then ASSERT().\r
511\r
512 @param BaseAddress The 64 bit physical address of the BSP.\r
513 @param Length The length of the BSP store in bytes.\r
514 @param MemoryType Type of memory allocated by this HOB.\r
515\r
516**/\r
517VOID\r
518EFIAPI\r
519BuildBspStoreHob (\r
520 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
521 IN UINT64 Length,\r
522 IN EFI_MEMORY_TYPE MemoryType\r
523 )\r
524{\r
525 EFI_HOB_MEMORY_ALLOCATION_BSP_STORE *Hob;\r
526\r
527 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_BSP_STORE));\r
528\r
529 CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocBspStoreGuid);\r
530 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;\r
531 Hob->AllocDescriptor.MemoryLength = Length;\r
532 Hob->AllocDescriptor.MemoryType = MemoryType;\r
533\r
534 //\r
535 // Zero the reserved space to match HOB spec\r
536 //\r
537 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));\r
538}\r
539\r
540/**\r
541 Builds a HOB for the memory allocation.\r
542\r
543 This function builds a HOB for the memory allocation.\r
544 It can only be invoked during PEI phase;\r
545 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
546 If there is no additional space for HOB creation, then ASSERT().\r
547\r
548 @param BaseAddress The 64 bit physical address of the memory.\r
549 @param Length The length of the memory allocation in bytes.\r
550 @param MemoryType Type of memory allocated by this HOB.\r
551\r
552**/\r
553VOID\r
554EFIAPI\r
555GlueBuildMemoryAllocationHob (\r
556 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
557 IN UINT64 Length,\r
558 IN EFI_MEMORY_TYPE MemoryType\r
559 )\r
560{\r
561 EFI_HOB_MEMORY_ALLOCATION *Hob;\r
562\r
563 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION));\r
564\r
565 ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));\r
566 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;\r
567 Hob->AllocDescriptor.MemoryLength = Length;\r
568 Hob->AllocDescriptor.MemoryType = MemoryType;\r
569 //\r
570 // Zero the reserved space to match HOB spec\r
571 //\r
572 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));\r
573}\r