]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/PeiHobLib/HobLib.c
remove some comments introduced by tools.
[mirror_edk2.git] / MdePkg / Library / PeiHobLib / HobLib.c
CommitLineData
66f0059f 1/** @file\r
2 HOB Library.\r
3\r
4 Copyright (c) 2006 - 2007, Intel Corporation<BR>\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
66f0059f 13**/\r
14\r
c892d846 15\r
66f0059f 16#include <PiPei.h>\r
c892d846 17\r
66f0059f 18#include <Guid/MemoryAllocationHob.h>\r
c892d846 19\r
66f0059f 20#include <Library/HobLib.h>\r
21#include <Library/DebugLib.h>\r
22#include <Library/PeiServicesLib.h>\r
23#include <Library/BaseMemoryLib.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
133GetNextGuidHob (\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
166GetFirstGuidHob (\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
248BuildModuleHob (\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 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_FV, sizeof (EFI_HOB_FIRMWARE_VOLUME));\r
402\r
403 Hob->BaseAddress = BaseAddress;\r
404 Hob->Length = Length;\r
405}\r
406\r
407/**\r
408 Builds a Capsule Volume HOB.\r
409\r
410 This function builds a Capsule Volume HOB.\r
411 It can only be invoked during PEI phase;\r
412 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
413 If there is no additional space for HOB creation, then ASSERT().\r
414\r
415 @param BaseAddress The base address of the Capsule Volume.\r
416 @param Length The size of the Capsule Volume in bytes.\r
417\r
418**/\r
419VOID\r
420EFIAPI\r
421BuildCvHob (\r
422 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
423 IN UINT64 Length\r
424 )\r
425{\r
426 ASSERT (FALSE);\r
427}\r
428\r
429/**\r
430 Builds a HOB for the CPU.\r
431\r
432 This function builds a HOB for the CPU.\r
433 It can only be invoked during PEI phase;\r
434 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
435 If there is no additional space for HOB creation, then ASSERT().\r
436\r
437 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.\r
438 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.\r
439\r
440**/\r
441VOID\r
442EFIAPI\r
443BuildCpuHob (\r
444 IN UINT8 SizeOfMemorySpace,\r
445 IN UINT8 SizeOfIoSpace\r
446 )\r
447{\r
448 EFI_HOB_CPU *Hob;\r
449\r
450 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));\r
451\r
452 Hob->SizeOfMemorySpace = SizeOfMemorySpace;\r
453 Hob->SizeOfIoSpace = SizeOfIoSpace;\r
454\r
455 //\r
456 // Zero the reserved space to match HOB spec\r
457 //\r
458 ZeroMem (Hob->Reserved, sizeof (Hob->Reserved)); \r
459}\r
460\r
461/**\r
462 Builds a HOB for the Stack.\r
463\r
464 This function builds a HOB for the stack.\r
465 It can only be invoked during PEI phase;\r
466 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
467 If there is no additional space for HOB creation, then ASSERT().\r
468\r
469 @param BaseAddress The 64 bit physical address of the Stack.\r
470 @param Length The length of the stack in bytes.\r
471\r
472**/\r
473VOID\r
474EFIAPI\r
475BuildStackHob (\r
476 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
477 IN UINT64 Length\r
478 )\r
479{\r
480 EFI_HOB_MEMORY_ALLOCATION_STACK *Hob;\r
481\r
482 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_STACK));\r
483\r
484 CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocStackGuid);\r
485 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;\r
486 Hob->AllocDescriptor.MemoryLength = Length;\r
487 Hob->AllocDescriptor.MemoryType = EfiConventionalMemory;\r
488\r
489 //\r
490 // Zero the reserved space to match HOB spec\r
491 //\r
492 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));\r
493}\r
494\r
495/**\r
496 Builds a HOB for the BSP store.\r
497\r
498 This function builds a HOB for BSP store.\r
499 It can only be invoked during PEI phase;\r
500 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
501 If there is no additional space for HOB creation, then ASSERT().\r
502\r
503 @param BaseAddress The 64 bit physical address of the BSP.\r
504 @param Length The length of the BSP store in bytes.\r
505 @param MemoryType Type of memory allocated by this HOB.\r
506\r
507**/\r
508VOID\r
509EFIAPI\r
510BuildBspStoreHob (\r
511 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
512 IN UINT64 Length,\r
513 IN EFI_MEMORY_TYPE MemoryType\r
514 )\r
515{\r
516 EFI_HOB_MEMORY_ALLOCATION_BSP_STORE *Hob;\r
517\r
518 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_BSP_STORE));\r
519\r
520 CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocBspStoreGuid);\r
521 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;\r
522 Hob->AllocDescriptor.MemoryLength = Length;\r
523 Hob->AllocDescriptor.MemoryType = MemoryType;\r
524\r
525 //\r
526 // Zero the reserved space to match HOB spec\r
527 //\r
528 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));\r
529}\r
530\r
531/**\r
532 Builds a HOB for the memory allocation.\r
533\r
534 This function builds a HOB for the memory allocation.\r
535 It can only be invoked during PEI phase;\r
536 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
537 If there is no additional space for HOB creation, then ASSERT().\r
538\r
539 @param BaseAddress The 64 bit physical address of the memory.\r
540 @param Length The length of the memory allocation in bytes.\r
541 @param MemoryType Type of memory allocated by this HOB.\r
542\r
543**/\r
544VOID\r
545EFIAPI\r
546BuildMemoryAllocationHob (\r
547 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
548 IN UINT64 Length,\r
549 IN EFI_MEMORY_TYPE MemoryType\r
550 )\r
551{\r
552 EFI_HOB_MEMORY_ALLOCATION *Hob;\r
553\r
554 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION));\r
555\r
556 ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));\r
557 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;\r
558 Hob->AllocDescriptor.MemoryLength = Length;\r
559 Hob->AllocDescriptor.MemoryType = MemoryType;\r
560 //\r
561 // Zero the reserved space to match HOB spec\r
562 //\r
563 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));\r
564}\r