]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/PeiHobLib/HobLib.c
Initial import.
[mirror_edk2.git] / MdePkg / Library / PeiHobLib / HobLib.c
1 /** @file
2 HOB Library.
3
4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 Module Name: HobLib.c
14
15 **/
16
17
18
19 /**
20 Returns the pointer to the HOB list.
21
22 None.
23
24 The pointer to the HOB list.
25
26 **/
27 VOID *
28 EFIAPI
29 GetHobList (
30 VOID
31 )
32 {
33 EFI_STATUS Status;
34 VOID *HobList;
35
36 Status = PeiCoreGetHobList (&HobList);
37 ASSERT_EFI_ERROR (Status);
38 ASSERT (HobList != NULL);
39
40 return HobList;
41 }
42
43 /**
44 This function searches the first instance of a HOB type from the starting HOB pointer.
45 If there does not exist such HOB type from the starting HOB pointer, it will return NULL.
46
47 @param Type The HOB type to return.
48 @param HobStart The starting HOB pointer to search from.
49
50 @return The next instance of a HOB type from the starting HOB.
51
52 **/
53 VOID *
54 EFIAPI
55 GetNextHob (
56 IN UINT16 Type,
57 IN CONST VOID *HobStart
58 )
59 {
60 EFI_PEI_HOB_POINTERS Hob;
61
62 ASSERT (HobStart != NULL);
63
64 Hob.Raw = (UINT8 *) HobStart;
65 //
66 // Parse the HOB list, stop if end of list or matching type found.
67 //
68 while (!END_OF_HOB_LIST (Hob)) {
69 if (Hob.Header->HobType == Type) {
70 return Hob.Raw;
71 }
72 Hob.Raw = GET_NEXT_HOB (Hob);
73 }
74 return NULL;
75 }
76
77 /**
78 This function searches the first instance of a HOB type among the whole HOB list.
79 If there does not exist such HOB type in the HOB list, it will return NULL.
80
81 @param Type The HOB type to return.
82
83 @return The next instance of a HOB type from the starting HOB.
84
85 **/
86 VOID *
87 EFIAPI
88 GetFirstHob (
89 IN UINT16 Type
90 )
91 {
92 VOID *HobList;
93
94 HobList = GetHobList ();
95 return GetNextHob (Type, HobList);
96 }
97
98 /**
99 This function searches the first instance of a HOB from the starting HOB pointer.
100 Such HOB should satisfy two conditions:
101 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
102 If there does not exist such HOB from the starting HOB pointer, it will return NULL.
103
104 @param Guid The GUID to match with in the HOB list.
105 @param HobStart A pointer to a Guid.
106
107 @return The next instance of the matched GUID HOB from the starting HOB.
108
109 **/
110 VOID *
111 EFIAPI
112 GetNextGuidHob (
113 IN CONST EFI_GUID *Guid,
114 IN CONST VOID *HobStart
115 )
116 {
117 EFI_PEI_HOB_POINTERS GuidHob;
118
119 GuidHob.Raw = (UINT8 *) HobStart;
120 while ((GuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, GuidHob.Raw)) != NULL) {
121 if (CompareGuid (Guid, &GuidHob.Guid->Name)) {
122 break;
123 }
124 GuidHob.Raw = GET_NEXT_HOB (GuidHob);
125 }
126 return GuidHob.Raw;
127 }
128
129 /**
130 This function searches the first instance of a HOB among the whole HOB list.
131 Such HOB should satisfy two conditions:
132 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
133 If there does not exist such HOB from the starting HOB pointer, it will return NULL.
134
135 @param Guid The GUID to match with in the HOB list.
136
137 @return The first instance of the matched GUID HOB among the whole HOB list.
138
139 **/
140 VOID *
141 EFIAPI
142 GetFirstGuidHob (
143 IN CONST EFI_GUID *Guid
144 )
145 {
146 VOID *HobList;
147
148 HobList = GetHobList ();
149 return GetNextGuidHob (Guid, HobList);
150 }
151
152 /**
153 Add a new HOB to the HOB List.
154
155 @param Type Type of the new HOB.
156 @param Length Length of the new HOB to allocate.
157
158 @return The address of new HOB.
159
160 **/
161 VOID *
162 InternalPeiCreateHob (
163 IN UINT16 Type,
164 IN UINT16 Length
165 )
166 {
167 EFI_STATUS Status;
168 VOID *Hob;
169
170 Status = PeiCoreCreateHob (Type, Length, &Hob);
171 //
172 // Assume the process of HOB building is always successful.
173 //
174 ASSERT_EFI_ERROR (Status);
175 return Hob;
176 }
177
178 /**
179 This function builds a HOB for a loaded PE32 module.
180
181 @param ModuleName The GUID File Name of the module.
182 @param MemoryAllocationModule The 64 bit physical address of the module.
183 @param ModuleLength The length of the module in bytes.
184 @param EntryPoint The 64 bit physical address of the module\92s entry point.
185
186 **/
187 VOID
188 EFIAPI
189 BuildModuleHob (
190 IN CONST EFI_GUID *ModuleName,
191 IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule,
192 IN UINT64 ModuleLength,
193 IN EFI_PHYSICAL_ADDRESS EntryPoint
194 )
195 {
196 EFI_HOB_MEMORY_ALLOCATION_MODULE *Hob;
197
198 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE));
199
200 CopyGuid (&(Hob->MemoryAllocationHeader.Name), &gEfiHobMemoryAllocModuleGuid);
201 Hob->MemoryAllocationHeader.MemoryBaseAddress = MemoryAllocationModule;
202 Hob->MemoryAllocationHeader.MemoryLength = ModuleLength;
203 Hob->MemoryAllocationHeader.MemoryType = EfiBootServicesCode;
204
205 CopyGuid (&Hob->ModuleName, ModuleName);
206 Hob->EntryPoint = EntryPoint;
207 }
208
209 /**
210 Builds a HOB that describes a chunk of system memory.
211
212 @param ResourceType The type of resource described by this HOB.
213 @param ResourceAttribute The resource attributes of the memory described by this HOB.
214 @param PhysicalStart The 64 bit physical address of memory described by this HOB.
215 @param NumberOfBytes The length of the memory described by this HOB in bytes.
216
217 **/
218 VOID
219 EFIAPI
220 BuildResourceDescriptorHob (
221 IN EFI_RESOURCE_TYPE ResourceType,
222 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute,
223 IN EFI_PHYSICAL_ADDRESS PhysicalStart,
224 IN UINT64 NumberOfBytes
225 )
226 {
227 EFI_HOB_RESOURCE_DESCRIPTOR *Hob;
228
229 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR));
230
231 Hob->ResourceType = ResourceType;
232 Hob->ResourceAttribute = ResourceAttribute;
233 Hob->PhysicalStart = PhysicalStart;
234 Hob->ResourceLength = NumberOfBytes;
235 }
236
237 /**
238 This function builds a customized HOB tagged with a GUID for identification
239 and returns the start address of GUID HOB data so that caller can fill the customized data.
240
241 @param Guid The GUID to tag the customized HOB.
242 @param DataLength The size of the data payload for the GUID HOB.
243
244 @return The start address of GUID HOB data.
245
246 **/
247 VOID *
248 EFIAPI
249 BuildGuidHob (
250 IN CONST EFI_GUID *Guid,
251 IN UINTN DataLength
252 )
253 {
254 EFI_HOB_GUID_TYPE *Hob;
255
256 //
257 // Make sure that data length is not too long.
258 //
259 ASSERT (DataLength <= (0xffff - sizeof (EFI_HOB_GUID_TYPE)));
260
261 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16) (sizeof (EFI_HOB_GUID_TYPE) + DataLength));
262 CopyGuid (&Hob->Name, Guid);
263 return Hob + 1;
264 }
265
266 /**
267 This function builds a customized HOB tagged with a GUID for identification,
268 copies the input data to the HOB data field, and returns the start address of GUID HOB data.
269
270 @param Guid The GUID to tag the customized HOB.
271 @param Data The data to be copied into the data field of the GUID HOB.
272 @param DataLength The size of the data payload for the GUID HOB.
273
274 @return The start address of GUID HOB data.
275
276 **/
277 VOID *
278 EFIAPI
279 BuildGuidDataHob (
280 IN CONST EFI_GUID *Guid,
281 IN VOID *Data,
282 IN UINTN DataLength
283 )
284 {
285 VOID *HobData;
286
287 HobData = BuildGuidHob (Guid, DataLength);
288
289 return CopyMem (HobData, Data, DataLength);
290 }
291
292 /**
293 Builds a Firmware Volume HOB.
294
295 @param BaseAddress The base address of the Firmware Volume.
296 @param Length The size of the Firmware Volume in bytes.
297
298 **/
299 VOID
300 EFIAPI
301 BuildFvHob (
302 IN EFI_PHYSICAL_ADDRESS BaseAddress,
303 IN UINT64 Length
304 )
305 {
306 EFI_HOB_FIRMWARE_VOLUME *Hob;
307
308 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_FV, sizeof (EFI_HOB_FIRMWARE_VOLUME));
309
310 Hob->BaseAddress = BaseAddress;
311 Hob->Length = Length;
312 }
313
314 /**
315 Builds a Capsule Volume HOB.
316
317 @param BaseAddress The base address of the Capsule Volume.
318 @param Length The size of the Capsule Volume in bytes.
319
320 **/
321 VOID
322 EFIAPI
323 BuildCvHob (
324 IN EFI_PHYSICAL_ADDRESS BaseAddress,
325 IN UINT64 Length
326 )
327 {
328 EFI_HOB_CAPSULE_VOLUME *Hob;
329
330 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_CV, sizeof (EFI_HOB_CAPSULE_VOLUME));
331
332 Hob->BaseAddress = BaseAddress;
333 Hob->Length = Length;
334 }
335
336 /**
337 Builds a HOB for the CPU.
338
339 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
340 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
341
342 **/
343 VOID
344 EFIAPI
345 BuildCpuHob (
346 IN UINT8 SizeOfMemorySpace,
347 IN UINT8 SizeOfIoSpace
348 )
349 {
350 EFI_HOB_CPU *Hob;
351
352 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));
353
354 Hob->SizeOfMemorySpace = SizeOfMemorySpace;
355 Hob->SizeOfIoSpace = SizeOfIoSpace;
356 }
357
358 /**
359 Builds a HOB for the Stack.
360
361 @param BaseAddress The 64 bit physical address of the Stack.
362 @param Length The length of the stack in bytes.
363
364 **/
365 VOID
366 EFIAPI
367 BuildStackHob (
368 IN EFI_PHYSICAL_ADDRESS BaseAddress,
369 IN UINT64 Length
370 )
371 {
372 EFI_HOB_MEMORY_ALLOCATION_STACK *Hob;
373
374 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_STACK));
375
376 CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocStackGuid);
377 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
378 Hob->AllocDescriptor.MemoryLength = Length;
379 Hob->AllocDescriptor.MemoryType = EfiConventionalMemory;
380 }
381
382 /**
383 Builds a HOB for the BSP store.
384
385 @param BaseAddress The 64 bit physical address of the BSP.
386 @param Length The length of the BSP store in bytes.
387 @param MemoryType Type of memory allocated by this HOB.
388
389 **/
390 VOID
391 EFIAPI
392 BuildBspStoreHob (
393 IN EFI_PHYSICAL_ADDRESS BaseAddress,
394 IN UINT64 Length,
395 IN EFI_MEMORY_TYPE MemoryType
396 )
397 {
398 EFI_HOB_MEMORY_ALLOCATION_BSP_STORE *Hob;
399
400 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_BSP_STORE));
401
402 CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocBspStoreGuid);
403 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
404 Hob->AllocDescriptor.MemoryLength = Length;
405 Hob->AllocDescriptor.MemoryType = MemoryType;
406 }
407
408 /**
409 Builds a HOB for the memory allocation.
410
411 @param BaseAddress The 64 bit physical address of the memory.
412 @param Length The length of the memory allocation in bytes.
413 @param MemoryType Type of memory allocated by this HOB.
414
415 **/
416 VOID
417 EFIAPI
418 BuildMemoryAllocationHob (
419 IN EFI_PHYSICAL_ADDRESS BaseAddress,
420 IN UINT64 Length,
421 IN EFI_MEMORY_TYPE MemoryType
422 )
423 {
424 EFI_HOB_MEMORY_ALLOCATION *Hob;
425
426 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION));
427
428 ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));
429 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
430 Hob->AllocDescriptor.MemoryLength = Length;
431 Hob->AllocDescriptor.MemoryType = MemoryType;
432 }