]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/PeiHobLib/HobLib.c
1. Add the fix for the following Bugs:
[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
181VOID *\r
182InternalPeiCreateHob (\r
183 IN UINT16 Type,\r
184 IN UINT16 Length\r
185 )\r
186{\r
187 EFI_STATUS Status;\r
188 VOID *Hob;\r
189\r
84a99d48 190 Status = PeiServicesCreateHob (Type, Length, &Hob);\r
878ddf1f 191 //\r
192 // Assume the process of HOB building is always successful.\r
193 //\r
194 ASSERT_EFI_ERROR (Status);\r
195 return Hob;\r
196}\r
197\r
198/**\r
5f10fa01 199 Builds a HOB for a loaded PE32 module.\r
200\r
878ddf1f 201 This function builds a HOB for a loaded PE32 module.\r
5f10fa01 202 It can only be invoked during PEI phase;\r
203 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
204 If ModuleName is NULL, then ASSERT().\r
205 If there is no additional space for HOB creation, then ASSERT().\r
878ddf1f 206\r
5f10fa01 207 @param ModuleName The GUID File Name of the module.\r
208 @param MemoryAllocationModule The 64 bit physical address of the module.\r
209 @param ModuleLength The length of the module in bytes.\r
511710d6 210 @param EntryPoint The 64 bit physical address of the module's entry point.\r
878ddf1f 211\r
212**/\r
213VOID\r
214EFIAPI\r
215BuildModuleHob (\r
216 IN CONST EFI_GUID *ModuleName,\r
217 IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule,\r
218 IN UINT64 ModuleLength,\r
219 IN EFI_PHYSICAL_ADDRESS EntryPoint\r
220 )\r
221{\r
222 EFI_HOB_MEMORY_ALLOCATION_MODULE *Hob;\r
223\r
224 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE));\r
225\r
226 CopyGuid (&(Hob->MemoryAllocationHeader.Name), &gEfiHobMemoryAllocModuleGuid);\r
227 Hob->MemoryAllocationHeader.MemoryBaseAddress = MemoryAllocationModule;\r
228 Hob->MemoryAllocationHeader.MemoryLength = ModuleLength;\r
229 Hob->MemoryAllocationHeader.MemoryType = EfiBootServicesCode;\r
230\r
d4f397c0 231 //\r
232 // Zero the reserved space to match HOB spec\r
233 //\r
234 ZeroMem (Hob->MemoryAllocationHeader.Reserved, sizeof (Hob->MemoryAllocationHeader.Reserved));\r
235 \r
878ddf1f 236 CopyGuid (&Hob->ModuleName, ModuleName);\r
237 Hob->EntryPoint = EntryPoint;\r
238}\r
239\r
240/**\r
241 Builds a HOB that describes a chunk of system memory.\r
242\r
5f10fa01 243 This function builds a HOB that describes a chunk of system memory.\r
244 It can only be invoked during PEI phase;\r
245 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
246 If there is no additional space for HOB creation, then ASSERT().\r
247\r
248 @param ResourceType The type of resource described by this HOB.\r
249 @param ResourceAttribute The resource attributes of the memory described by this HOB.\r
250 @param PhysicalStart The 64 bit physical address of memory described by this HOB.\r
251 @param NumberOfBytes The length of the memory described by this HOB in bytes.\r
878ddf1f 252\r
253**/\r
254VOID\r
255EFIAPI\r
256BuildResourceDescriptorHob (\r
257 IN EFI_RESOURCE_TYPE ResourceType,\r
258 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute,\r
259 IN EFI_PHYSICAL_ADDRESS PhysicalStart,\r
260 IN UINT64 NumberOfBytes\r
261 )\r
262{\r
263 EFI_HOB_RESOURCE_DESCRIPTOR *Hob;\r
264\r
265 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR));\r
266\r
267 Hob->ResourceType = ResourceType;\r
268 Hob->ResourceAttribute = ResourceAttribute;\r
269 Hob->PhysicalStart = PhysicalStart;\r
270 Hob->ResourceLength = NumberOfBytes;\r
271}\r
272\r
273/**\r
5f10fa01 274 Builds a GUID HOB with a certain data length.\r
275\r
276 This function builds a customized HOB tagged with a GUID for identification \r
277 and returns the start address of GUID HOB data so that caller can fill the customized data. \r
278 The HOB Header and Name field is already stripped.\r
279 It can only be invoked during PEI phase;\r
280 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
281 If Guid is NULL, then ASSERT().\r
282 If there is no additional space for HOB creation, then ASSERT().\r
533f039e 283 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().\r
878ddf1f 284\r
5f10fa01 285 @param Guid The GUID to tag the customized HOB.\r
286 @param DataLength The size of the data payload for the GUID HOB.\r
878ddf1f 287\r
288 @return The start address of GUID HOB data.\r
289\r
290**/\r
291VOID *\r
292EFIAPI\r
293BuildGuidHob (\r
294 IN CONST EFI_GUID *Guid,\r
295 IN UINTN DataLength\r
296 )\r
297{\r
298 EFI_HOB_GUID_TYPE *Hob;\r
299\r
300 //\r
301 // Make sure that data length is not too long.\r
302 //\r
303 ASSERT (DataLength <= (0xffff - sizeof (EFI_HOB_GUID_TYPE)));\r
304\r
305 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16) (sizeof (EFI_HOB_GUID_TYPE) + DataLength));\r
306 CopyGuid (&Hob->Name, Guid);\r
307 return Hob + 1;\r
308}\r
309\r
310/**\r
5f10fa01 311 Copies a data buffer to a newly-built HOB.\r
878ddf1f 312\r
5f10fa01 313 This function builds a customized HOB tagged with a GUID for identification,\r
314 copies the input data to the HOB data field and returns the start address of the GUID HOB data.\r
315 The HOB Header and Name field is already stripped.\r
316 It can only be invoked during PEI phase;\r
317 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
318 If Guid is NULL, then ASSERT().\r
319 If Data is NULL and DataLength > 0, then ASSERT().\r
320 If there is no additional space for HOB creation, then ASSERT().\r
533f039e 321 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().\r
5f10fa01 322\r
323 @param Guid The GUID to tag the customized HOB.\r
324 @param Data The data to be copied into the data field of the GUID HOB.\r
325 @param DataLength The size of the data payload for the GUID HOB.\r
878ddf1f 326\r
327 @return The start address of GUID HOB data.\r
328\r
329**/\r
330VOID *\r
331EFIAPI\r
332BuildGuidDataHob (\r
333 IN CONST EFI_GUID *Guid,\r
334 IN VOID *Data,\r
335 IN UINTN DataLength\r
336 )\r
337{\r
338 VOID *HobData;\r
339\r
533f039e 340 ASSERT (Data != NULL || DataLength == 0);\r
341\r
878ddf1f 342 HobData = BuildGuidHob (Guid, DataLength);\r
343\r
344 return CopyMem (HobData, Data, DataLength);\r
345}\r
346\r
347/**\r
348 Builds a Firmware Volume HOB.\r
349\r
5f10fa01 350 This function builds a Firmware Volume HOB.\r
351 It can only be invoked during PEI phase;\r
352 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
353 If there is no additional space for HOB creation, then ASSERT().\r
354\r
355 @param BaseAddress The base address of the Firmware Volume.\r
356 @param Length The size of the Firmware Volume in bytes.\r
878ddf1f 357\r
358**/\r
359VOID\r
360EFIAPI\r
361BuildFvHob (\r
362 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
363 IN UINT64 Length\r
364 )\r
365{\r
366 EFI_HOB_FIRMWARE_VOLUME *Hob;\r
367\r
368 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_FV, sizeof (EFI_HOB_FIRMWARE_VOLUME));\r
369\r
370 Hob->BaseAddress = BaseAddress;\r
371 Hob->Length = Length;\r
372}\r
373\r
374/**\r
375 Builds a Capsule Volume HOB.\r
376\r
5f10fa01 377 This function builds a Capsule Volume HOB.\r
378 It can only be invoked during PEI phase;\r
379 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
380 If there is no additional space for HOB creation, then ASSERT().\r
381\r
382 @param BaseAddress The base address of the Capsule Volume.\r
383 @param Length The size of the Capsule Volume in bytes.\r
878ddf1f 384\r
385**/\r
386VOID\r
387EFIAPI\r
388BuildCvHob (\r
389 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
390 IN UINT64 Length\r
391 )\r
392{\r
393 EFI_HOB_CAPSULE_VOLUME *Hob;\r
394\r
395 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_CV, sizeof (EFI_HOB_CAPSULE_VOLUME));\r
396\r
397 Hob->BaseAddress = BaseAddress;\r
398 Hob->Length = Length;\r
399}\r
400\r
401/**\r
402 Builds a HOB for the CPU.\r
403\r
5f10fa01 404 This function builds a HOB for the CPU.\r
405 It can only be invoked during PEI phase;\r
406 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
407 If there is no additional space for HOB creation, then ASSERT().\r
408\r
409 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.\r
410 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.\r
878ddf1f 411\r
412**/\r
413VOID\r
414EFIAPI\r
415BuildCpuHob (\r
416 IN UINT8 SizeOfMemorySpace,\r
417 IN UINT8 SizeOfIoSpace\r
418 )\r
419{\r
420 EFI_HOB_CPU *Hob;\r
421\r
422 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));\r
423\r
424 Hob->SizeOfMemorySpace = SizeOfMemorySpace;\r
425 Hob->SizeOfIoSpace = SizeOfIoSpace;\r
d4f397c0 426\r
427 //\r
428 // Zero the reserved space to match HOB spec\r
429 //\r
430 ZeroMem (Hob->Reserved, sizeof (Hob->Reserved)); \r
878ddf1f 431}\r
432\r
433/**\r
434 Builds a HOB for the Stack.\r
435\r
5f10fa01 436 This function builds a HOB for the stack.\r
437 It can only be invoked during PEI phase;\r
438 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
439 If there is no additional space for HOB creation, then ASSERT().\r
440\r
441 @param BaseAddress The 64 bit physical address of the Stack.\r
442 @param Length The length of the stack in bytes.\r
878ddf1f 443\r
444**/\r
445VOID\r
446EFIAPI\r
447BuildStackHob (\r
448 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
449 IN UINT64 Length\r
450 )\r
451{\r
452 EFI_HOB_MEMORY_ALLOCATION_STACK *Hob;\r
453\r
454 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_STACK));\r
455\r
456 CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocStackGuid);\r
457 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;\r
458 Hob->AllocDescriptor.MemoryLength = Length;\r
459 Hob->AllocDescriptor.MemoryType = EfiConventionalMemory;\r
d4f397c0 460\r
461 //\r
462 // Zero the reserved space to match HOB spec\r
463 //\r
464 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));\r
878ddf1f 465}\r
466\r
467/**\r
468 Builds a HOB for the BSP store.\r
469\r
5f10fa01 470 This function builds a HOB for BSP store.\r
471 It can only be invoked during PEI phase;\r
472 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
473 If there is no additional space for HOB creation, then ASSERT().\r
474\r
475 @param BaseAddress The 64 bit physical address of the BSP.\r
476 @param Length The length of the BSP store in bytes.\r
477 @param MemoryType Type of memory allocated by this HOB.\r
878ddf1f 478\r
479**/\r
480VOID\r
481EFIAPI\r
482BuildBspStoreHob (\r
483 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
484 IN UINT64 Length,\r
485 IN EFI_MEMORY_TYPE MemoryType\r
486 )\r
487{\r
488 EFI_HOB_MEMORY_ALLOCATION_BSP_STORE *Hob;\r
489\r
490 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_BSP_STORE));\r
491\r
492 CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocBspStoreGuid);\r
493 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;\r
494 Hob->AllocDescriptor.MemoryLength = Length;\r
495 Hob->AllocDescriptor.MemoryType = MemoryType;\r
d4f397c0 496\r
497 //\r
498 // Zero the reserved space to match HOB spec\r
499 //\r
500 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));\r
878ddf1f 501}\r
502\r
503/**\r
504 Builds a HOB for the memory allocation.\r
505\r
5f10fa01 506 This function builds a HOB for the memory allocation.\r
507 It can only be invoked during PEI phase;\r
508 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.\r
509 If there is no additional space for HOB creation, then ASSERT().\r
510\r
511 @param BaseAddress The 64 bit physical address of the memory.\r
512 @param Length The length of the memory allocation in bytes.\r
513 @param MemoryType Type of memory allocated by this HOB.\r
878ddf1f 514\r
515**/\r
516VOID\r
517EFIAPI\r
518BuildMemoryAllocationHob (\r
519 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
520 IN UINT64 Length,\r
521 IN EFI_MEMORY_TYPE MemoryType\r
522 )\r
523{\r
524 EFI_HOB_MEMORY_ALLOCATION *Hob;\r
525\r
526 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION));\r
527\r
528 ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));\r
529 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;\r
530 Hob->AllocDescriptor.MemoryLength = Length;\r
531 Hob->AllocDescriptor.MemoryType = MemoryType;\r
d4f397c0 532 //\r
533 // Zero the reserved space to match HOB spec\r
534 //\r
535 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));\r
878ddf1f 536}\r