]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/DxeCoreHobLib/HobLib.c
Add check before use to make code run more safer.
[mirror_edk2.git] / MdePkg / Library / DxeCoreHobLib / HobLib.c
CommitLineData
738ec565 1/** @file\r
6812ef9f 2 HOB Library implementation for DxeCore driver.\r
738ec565 3\r
ef2635c3 4Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
19388d29 5This program and the accompanying materials\r
3e5c3238 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
58380e9c 8http://opensource.org/licenses/bsd-license.php.\r
738ec565 9\r
3e5c3238 10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
738ec565 12\r
738ec565 13**/\r
14\r
c7d265a9 15#include <PiDxe.h>\r
c892d846 16\r
c7d265a9 17#include <Library/HobLib.h>\r
18#include <Library/DebugLib.h>\r
19#include <Library/BaseMemoryLib.h>\r
6812ef9f 20#include <Library/DxeCoreEntryPoint.h>\r
738ec565 21\r
22/**\r
23 Returns the pointer to the HOB list.\r
24\r
25 This function returns the pointer to first HOB in the list.\r
3e5c3238 26 For PEI phase, the PEI service GetHobList() can be used to retrieve the pointer \r
27 to the HOB list. For the DXE phase, the HOB list pointer can be retrieved through\r
28 the EFI System Table by looking up theHOB list GUID in the System Configuration Table.\r
29 Since the System Configuration Table does not exist that the time the DXE Core is \r
30 launched, the DXE Core uses a global variable from the DXE Core Entry Point Library \r
31 to manage the pointer to the HOB list.\r
32 \r
33 If the pointer to the HOB list is NULL, then ASSERT().\r
34 \r
738ec565 35 @return The pointer to the HOB list.\r
36\r
37**/\r
38VOID *\r
39EFIAPI\r
40GetHobList (\r
41 VOID\r
42 )\r
43{\r
6f890d5b 44 ASSERT (gHobList != NULL);\r
738ec565 45 return gHobList;\r
46}\r
47\r
48/**\r
49 Returns the next instance of a HOB type from the starting HOB.\r
50\r
3e5c3238 51 This function searches the first instance of a HOB type from the starting HOB pointer. \r
738ec565 52 If there does not exist such HOB type from the starting HOB pointer, it will return NULL.\r
53 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer\r
54 unconditionally: it returns HobStart back if HobStart itself meets the requirement;\r
55 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.\r
3e5c3238 56 \r
738ec565 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
8693ca5d 75\r
738ec565 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
3e5c3238 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 If the pointer to the HOB list is NULL, then ASSERT().\r
738ec565 96\r
97 @param Type The HOB type to return.\r
98\r
99 @return The next instance of a HOB type from the starting HOB.\r
100\r
101**/\r
102VOID *\r
103EFIAPI\r
104GetFirstHob (\r
105 IN UINT16 Type\r
106 )\r
107{\r
108 VOID *HobList;\r
109\r
110 HobList = GetHobList ();\r
111 return GetNextHob (Type, HobList);\r
112}\r
113\r
114/**\r
3e5c3238 115 Returns the next instance of the matched GUID HOB from the starting HOB.\r
116 \r
117 This function searches the first instance of a HOB from the starting HOB pointer. \r
118 Such HOB should satisfy two conditions: \r
58380e9c 119 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION, and its GUID Name equals to the input Guid. \r
120 If such a HOB from the starting HOB pointer does not exist, it will return NULL. \r
738ec565 121 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()\r
58380e9c 122 to extract the data section and its size information, respectively.\r
738ec565 123 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer\r
124 unconditionally: it returns HobStart back if HobStart itself meets the requirement;\r
125 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.\r
3e5c3238 126 \r
738ec565 127 If Guid is NULL, then ASSERT().\r
128 If HobStart is NULL, then ASSERT().\r
129\r
130 @param Guid The GUID to match with in the HOB list.\r
131 @param HobStart A pointer to a Guid.\r
132\r
133 @return The next instance of the matched GUID HOB from the starting HOB.\r
134\r
135**/\r
136VOID *\r
137EFIAPI\r
138GetNextGuidHob (\r
139 IN CONST EFI_GUID *Guid,\r
140 IN CONST VOID *HobStart\r
141 )\r
142{\r
143 EFI_PEI_HOB_POINTERS GuidHob;\r
144\r
145 GuidHob.Raw = (UINT8 *) HobStart;\r
146 while ((GuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, GuidHob.Raw)) != NULL) {\r
147 if (CompareGuid (Guid, &GuidHob.Guid->Name)) {\r
148 break;\r
149 }\r
150 GuidHob.Raw = GET_NEXT_HOB (GuidHob);\r
151 }\r
152 return GuidHob.Raw;\r
153}\r
154\r
155/**\r
3e5c3238 156 Returns the first instance of the matched GUID HOB among the whole HOB list.\r
157 \r
158 This function searches the first instance of a HOB among the whole HOB list. \r
738ec565 159 Such HOB should satisfy two conditions:\r
160 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.\r
58380e9c 161 If such a HOB from the starting HOB pointer does not exist, it will return NULL.\r
738ec565 162 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()\r
58380e9c 163 to extract the data section and its size information, respectively.\r
3e5c3238 164 \r
165 If the pointer to the HOB list is NULL, then ASSERT().\r
738ec565 166 If Guid is NULL, then ASSERT().\r
167\r
168 @param Guid The GUID to match with in the HOB list.\r
169\r
170 @return The first instance of the matched GUID HOB among the whole HOB list.\r
171\r
172**/\r
173VOID *\r
174EFIAPI\r
175GetFirstGuidHob (\r
176 IN CONST EFI_GUID *Guid\r
177 )\r
178{\r
179 VOID *HobList;\r
180\r
181 HobList = GetHobList ();\r
182 return GetNextGuidHob (Guid, HobList);\r
183}\r
184\r
185/**\r
3e5c3238 186 Get the system boot mode from the HOB list.\r
738ec565 187\r
3e5c3238 188 This function returns the system boot mode information from the \r
738ec565 189 PHIT HOB in HOB list.\r
190\r
3e5c3238 191 If the pointer to the HOB list is NULL, then ASSERT().\r
192 \r
738ec565 193 @param VOID\r
194\r
195 @return The Boot Mode.\r
196\r
197**/\r
198EFI_BOOT_MODE\r
199EFIAPI\r
200GetBootModeHob (\r
201 VOID\r
202 )\r
203{\r
204 EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;\r
205\r
206 HandOffHob = (EFI_HOB_HANDOFF_INFO_TABLE *) GetHobList ();\r
207\r
208 return HandOffHob->BootMode;\r
209}\r
3e5c3238 210\r
738ec565 211/**\r
212 Builds a HOB for a loaded PE32 module.\r
213\r
214 This function builds a HOB for a loaded PE32 module.\r
215 It can only be invoked during PEI phase;\r
58380e9c 216 for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.\r
3e5c3238 217 \r
738ec565 218 If ModuleName is NULL, then ASSERT().\r
219 If there is no additional space for HOB creation, then ASSERT().\r
220\r
221 @param ModuleName The GUID File Name of the module.\r
222 @param MemoryAllocationModule The 64 bit physical address of the module.\r
223 @param ModuleLength The length of the module in bytes.\r
3e5c3238 224 @param EntryPoint The 64 bit physical address of the module entry point.\r
738ec565 225\r
226**/\r
227VOID\r
228EFIAPI\r
229BuildModuleHob (\r
230 IN CONST EFI_GUID *ModuleName,\r
231 IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule,\r
232 IN UINT64 ModuleLength,\r
233 IN EFI_PHYSICAL_ADDRESS EntryPoint\r
234 )\r
235{\r
236 //\r
237 // PEI HOB is read only for DXE phase\r
238 //\r
239 ASSERT (FALSE);\r
240}\r
241\r
242/**\r
243 Builds a HOB that describes a chunk of system memory.\r
244\r
245 This function builds a HOB that describes a chunk of system memory.\r
246 It can only be invoked during PEI phase;\r
58380e9c 247 for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.\r
3e5c3238 248 \r
738ec565 249 If there is no additional space for HOB creation, then ASSERT().\r
250\r
251 @param ResourceType The type of resource described by this HOB.\r
252 @param ResourceAttribute The resource attributes of the memory described by this HOB.\r
253 @param PhysicalStart The 64 bit physical address of memory described by this HOB.\r
254 @param NumberOfBytes The length of the memory described by this HOB in bytes.\r
255\r
256**/\r
257VOID\r
258EFIAPI\r
259BuildResourceDescriptorHob (\r
260 IN EFI_RESOURCE_TYPE ResourceType,\r
261 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute,\r
262 IN EFI_PHYSICAL_ADDRESS PhysicalStart,\r
263 IN UINT64 NumberOfBytes\r
264 )\r
265{\r
266 //\r
267 // PEI HOB is read only for DXE phase\r
268 //\r
269 ASSERT (FALSE);\r
270}\r
271\r
272/**\r
3e5c3238 273 Builds a customized HOB tagged with a GUID for identification and returns \r
274 the start address of GUID HOB data.\r
738ec565 275\r
3e5c3238 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
738ec565 278 The HOB Header and Name field is already stripped.\r
58380e9c 279 It can only be invoked during PEI phase.\r
280 For DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.\r
3e5c3238 281 \r
738ec565 282 If Guid is NULL, then ASSERT().\r
283 If there is no additional space for HOB creation, then ASSERT().\r
284 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().\r
285\r
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
288\r
ef2635c3
RN
289 @retval NULL The GUID HOB could not be allocated.\r
290 @retval others The start address of GUID HOB data.\r
738ec565 291\r
292**/\r
293VOID *\r
294EFIAPI\r
295BuildGuidHob (\r
296 IN CONST EFI_GUID *Guid,\r
297 IN UINTN DataLength\r
298 )\r
299{\r
300 //\r
301 // PEI HOB is read only for DXE phase\r
302 //\r
303 ASSERT (FALSE);\r
304 return NULL;\r
305}\r
306\r
307/**\r
3e5c3238 308 Builds a customized HOB tagged with a GUID for identification, copies the input data to the HOB \r
309 data field, and returns the start address of the GUID HOB data.\r
738ec565 310\r
3e5c3238 311 This function builds a customized HOB tagged with a GUID for identification and copies the input\r
312 data to the HOB data field and returns the start address of the GUID HOB data. It can only be \r
58380e9c 313 invoked during PEI phase; for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase. \r
738ec565 314 The HOB Header and Name field is already stripped.\r
58380e9c 315 It can only be invoked during PEI phase.\r
316 For DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.\r
3e5c3238 317 \r
738ec565 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
321 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().\r
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
326\r
ef2635c3
RN
327 @retval NULL The GUID HOB could not be allocated.\r
328 @retval others The start address of GUID HOB data.\r
738ec565 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 //\r
340 // PEI HOB is read only for DXE phase\r
341 //\r
342 ASSERT (FALSE);\r
343 return NULL;\r
344}\r
345\r
346/**\r
347 Builds a Firmware Volume HOB.\r
348\r
349 This function builds a Firmware Volume HOB.\r
350 It can only be invoked during PEI phase;\r
58380e9c 351 for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.\r
3e5c3238 352 \r
738ec565 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
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 //\r
367 // PEI HOB is read only for DXE phase\r
368 //\r
369 ASSERT (FALSE);\r
370}\r
371\r
07ad9b81 372/**\r
373 Builds a EFI_HOB_TYPE_FV2 HOB.\r
374\r
375 This function builds a EFI_HOB_TYPE_FV2 HOB.\r
376 It can only be invoked during PEI phase;\r
58380e9c 377 for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.\r
3e5c3238 378 \r
07ad9b81 379 If there is no additional space for HOB creation, then ASSERT().\r
380\r
381 @param BaseAddress The base address of the Firmware Volume.\r
382 @param Length The size of the Firmware Volume in bytes.\r
3e5c3238 383 @param FvName The name of the Firmware Volume.\r
07ad9b81 384 @param FileName The name of the file.\r
385 \r
386**/\r
387VOID\r
388EFIAPI\r
389BuildFv2Hob (\r
390 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
391 IN UINT64 Length,\r
392 IN CONST EFI_GUID *FvName,\r
393 IN CONST EFI_GUID *FileName\r
394 )\r
395{\r
396 ASSERT (FALSE);\r
397}\r
398\r
738ec565 399/**\r
400 Builds a Capsule Volume HOB.\r
401\r
402 This function builds a Capsule Volume HOB.\r
403 It can only be invoked during PEI phase;\r
58380e9c 404 for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.\r
3e5c3238 405 \r
c85cea80 406 If the platform does not support Capsule Volume HOBs, then ASSERT().\r
738ec565 407 If there is no additional space for HOB creation, then ASSERT().\r
408\r
409 @param BaseAddress The base address of the Capsule Volume.\r
410 @param Length The size of the Capsule Volume in bytes.\r
411\r
412**/\r
413VOID\r
414EFIAPI\r
415BuildCvHob (\r
416 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
417 IN UINT64 Length\r
418 )\r
419{\r
420 //\r
421 // PEI HOB is read only for DXE phase\r
422 //\r
423 ASSERT (FALSE);\r
424}\r
425\r
426/**\r
427 Builds a HOB for the CPU.\r
428\r
429 This function builds a HOB for the CPU.\r
430 It can only be invoked during PEI phase;\r
58380e9c 431 for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.\r
3e5c3238 432 \r
738ec565 433 If there is no additional space for HOB creation, then ASSERT().\r
434\r
435 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.\r
436 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.\r
437\r
438**/\r
439VOID\r
440EFIAPI\r
441BuildCpuHob (\r
442 IN UINT8 SizeOfMemorySpace,\r
443 IN UINT8 SizeOfIoSpace\r
444 )\r
445{\r
446 //\r
447 // PEI HOB is read only for DXE phase\r
448 //\r
449 ASSERT (FALSE);\r
450}\r
451\r
452/**\r
453 Builds a HOB for the Stack.\r
454\r
455 This function builds a HOB for the stack.\r
456 It can only be invoked during PEI phase;\r
58380e9c 457 for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.\r
3e5c3238 458 \r
738ec565 459 If there is no additional space for HOB creation, then ASSERT().\r
460\r
461 @param BaseAddress The 64 bit physical address of the Stack.\r
462 @param Length The length of the stack in bytes.\r
463\r
464**/\r
465VOID\r
466EFIAPI\r
467BuildStackHob (\r
468 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
469 IN UINT64 Length\r
470 )\r
471{\r
472 //\r
473 // PEI HOB is read only for DXE phase\r
474 //\r
475 ASSERT (FALSE);\r
476}\r
477\r
478/**\r
479 Builds a HOB for the BSP store.\r
480\r
481 This function builds a HOB for BSP store.\r
482 It can only be invoked during PEI phase;\r
58380e9c 483 for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.\r
3e5c3238 484 \r
738ec565 485 If there is no additional space for HOB creation, then ASSERT().\r
486\r
487 @param BaseAddress The 64 bit physical address of the BSP.\r
488 @param Length The length of the BSP store in bytes.\r
489 @param MemoryType Type of memory allocated by this HOB.\r
490\r
491**/\r
492VOID\r
493EFIAPI\r
494BuildBspStoreHob (\r
495 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
496 IN UINT64 Length,\r
497 IN EFI_MEMORY_TYPE MemoryType\r
498 )\r
499{\r
500 //\r
501 // PEI HOB is read only for DXE phase\r
502 //\r
503 ASSERT (FALSE);\r
504}\r
505\r
506/**\r
507 Builds a HOB for the memory allocation.\r
508\r
509 This function builds a HOB for the memory allocation.\r
510 It can only be invoked during PEI phase;\r
58380e9c 511 for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.\r
3e5c3238 512 \r
738ec565 513 If there is no additional space for HOB creation, then ASSERT().\r
514\r
515 @param BaseAddress The 64 bit physical address of the memory.\r
516 @param Length The length of the memory allocation in bytes.\r
517 @param MemoryType Type of memory allocated by this HOB.\r
518\r
519**/\r
520VOID\r
521EFIAPI\r
522BuildMemoryAllocationHob (\r
523 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
524 IN UINT64 Length,\r
525 IN EFI_MEMORY_TYPE MemoryType\r
526 )\r
527{\r
528 //\r
529 // PEI HOB is read only for DXE phase\r
530 //\r
531 ASSERT (FALSE);\r
532}\r