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