]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Pei/Hob/Hob.c
ed49e5be4ad672e47c4da143fa611f50a6d96914
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Hob / Hob.c
1 /** @file
2 This module provide Hand-Off Block manupulation.
3
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 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 **/
14
15 #include "PeiMain.h"
16
17 /**
18
19 Gets the pointer to the HOB List.
20
21 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
22 @param HobList Pointer to the HOB List.
23
24 @retval EFI_SUCCESS Get the pointer of HOB List
25 @retval EFI_NOT_AVAILABLE_YET the HOB List is not yet published
26 @retval EFI_INVALID_PARAMETER HobList is NULL (in debug mode)
27
28 **/
29 EFI_STATUS
30 EFIAPI
31 PeiGetHobList (
32 IN CONST EFI_PEI_SERVICES **PeiServices,
33 IN OUT VOID **HobList
34 )
35 {
36 PEI_CORE_INSTANCE *PrivateData;
37
38 //
39 // Only check this parameter in debug mode
40 //
41
42 DEBUG_CODE_BEGIN ();
43 if (HobList == NULL) {
44 return EFI_INVALID_PARAMETER;
45 }
46 DEBUG_CODE_END ();
47
48 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS(PeiServices);
49
50 *HobList = PrivateData->HobList.Raw;
51
52 return EFI_SUCCESS;
53 }
54
55
56 /**
57 Add a new HOB to the HOB List.
58
59 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
60 @param Type Type of the new HOB.
61 @param Length Length of the new HOB to allocate.
62 @param Hob Pointer to the new HOB.
63
64 @return EFI_SUCCESS Success to create hob.
65 @retval EFI_INVALID_PARAMETER if Hob is NULL
66 @retval EFI_NOT_AVAILABLE_YET if HobList is still not available.
67 @retval EFI_OUT_OF_RESOURCES if there is no more memory to grow the Hoblist.
68
69 **/
70 EFI_STATUS
71 EFIAPI
72 PeiCreateHob (
73 IN CONST EFI_PEI_SERVICES **PeiServices,
74 IN UINT16 Type,
75 IN UINT16 Length,
76 IN OUT VOID **Hob
77 )
78 {
79 EFI_STATUS Status;
80 EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;
81 EFI_HOB_GENERIC_HEADER *HobEnd;
82 EFI_PHYSICAL_ADDRESS FreeMemory;
83
84
85 Status = PeiGetHobList (PeiServices, Hob);
86 if (EFI_ERROR(Status)) {
87 return Status;
88 }
89
90 HandOffHob = *Hob;
91
92 //
93 // Check Length to avoid data overflow.
94 //
95 if (0x10000 - Length <= 0x7) {
96 return EFI_INVALID_PARAMETER;
97 }
98 Length = (UINT16)((Length + 0x7) & (~0x7));
99
100 FreeMemory = HandOffHob->EfiFreeMemoryTop -
101 HandOffHob->EfiFreeMemoryBottom;
102
103 if (FreeMemory < Length) {
104 DEBUG ((EFI_D_ERROR, "PeiCreateHob fail: Length - 0x%08x\n", (UINTN)Length));
105 DEBUG ((EFI_D_ERROR, " FreeMemoryTop - 0x%08x\n", (UINTN)HandOffHob->EfiFreeMemoryTop));
106 DEBUG ((EFI_D_ERROR, " FreeMemoryBottom - 0x%08x\n", (UINTN)HandOffHob->EfiFreeMemoryBottom));
107 return EFI_OUT_OF_RESOURCES;
108 }
109
110 *Hob = (VOID*) (UINTN) HandOffHob->EfiEndOfHobList;
111 ((EFI_HOB_GENERIC_HEADER*) *Hob)->HobType = Type;
112 ((EFI_HOB_GENERIC_HEADER*) *Hob)->HobLength = Length;
113 ((EFI_HOB_GENERIC_HEADER*) *Hob)->Reserved = 0;
114
115 HobEnd = (EFI_HOB_GENERIC_HEADER*) ((UINTN) *Hob + Length);
116 HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
117
118 HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST;
119 HobEnd->HobLength = (UINT16) sizeof (EFI_HOB_GENERIC_HEADER);
120 HobEnd->Reserved = 0;
121 HobEnd++;
122 HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
123
124 return EFI_SUCCESS;
125 }
126
127 /**
128 Install SEC HOB data to the HOB List.
129
130 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
131 @param SecHobList Pointer to SEC HOB List.
132
133 @return EFI_SUCCESS Success to install SEC HOB data.
134 @retval EFI_OUT_OF_RESOURCES If there is no more memory to grow the Hoblist.
135
136 **/
137 EFI_STATUS
138 PeiInstallSecHobData (
139 IN CONST EFI_PEI_SERVICES **PeiServices,
140 IN EFI_HOB_GENERIC_HEADER *SecHobList
141 )
142 {
143 EFI_STATUS Status;
144 EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;
145 EFI_PEI_HOB_POINTERS HobStart;
146 EFI_PEI_HOB_POINTERS Hob;
147 UINTN SecHobListLength;
148 EFI_PHYSICAL_ADDRESS FreeMemory;
149 EFI_HOB_GENERIC_HEADER *HobEnd;
150
151 HandOffHob = NULL;
152 Status = PeiGetHobList (PeiServices, (VOID **) &HandOffHob);
153 if (EFI_ERROR(Status)) {
154 return Status;
155 }
156 ASSERT (HandOffHob != NULL);
157
158 HobStart.Raw = (UINT8 *) SecHobList;
159 //
160 // The HobList must not contain a EFI_HOB_HANDOFF_INFO_TABLE HOB (PHIT) HOB.
161 //
162 ASSERT (HobStart.Header->HobType != EFI_HOB_TYPE_HANDOFF);
163 //
164 // Calculate the SEC HOB List length,
165 // not including the terminated HOB(EFI_HOB_TYPE_END_OF_HOB_LIST).
166 //
167 for (Hob.Raw = HobStart.Raw; !END_OF_HOB_LIST (Hob); Hob.Raw = GET_NEXT_HOB (Hob));
168 SecHobListLength = (UINTN) Hob.Raw - (UINTN) HobStart.Raw;
169 //
170 // The length must be 8-bytes aligned.
171 //
172 ASSERT ((SecHobListLength & 0x7) == 0);
173
174 FreeMemory = HandOffHob->EfiFreeMemoryTop -
175 HandOffHob->EfiFreeMemoryBottom;
176
177 if (FreeMemory < SecHobListLength) {
178 DEBUG ((DEBUG_ERROR, "PeiInstallSecHobData fail: SecHobListLength - 0x%08x\n", SecHobListLength));
179 DEBUG ((DEBUG_ERROR, " FreeMemoryTop - 0x%08x\n", (UINTN)HandOffHob->EfiFreeMemoryTop));
180 DEBUG ((DEBUG_ERROR, " FreeMemoryBottom - 0x%08x\n", (UINTN)HandOffHob->EfiFreeMemoryBottom));
181 return EFI_OUT_OF_RESOURCES;
182 }
183
184 Hob.Raw = (UINT8 *) (UINTN) HandOffHob->EfiEndOfHobList;
185 CopyMem (Hob.Raw, HobStart.Raw, SecHobListLength);
186
187 HobEnd = (EFI_HOB_GENERIC_HEADER *) ((UINTN) Hob.Raw + SecHobListLength);
188 HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
189
190 HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST;
191 HobEnd->HobLength = (UINT16) sizeof (EFI_HOB_GENERIC_HEADER);
192 HobEnd->Reserved = 0;
193 HobEnd++;
194 HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
195
196 return EFI_SUCCESS;
197 }
198
199 /**
200
201 Builds a Handoff Information Table HOB
202
203 @param BootMode - Current Bootmode
204 @param MemoryBegin - Start Memory Address.
205 @param MemoryLength - Length of Memory.
206
207 @return EFI_SUCCESS Always success to initialize HOB.
208
209 **/
210 EFI_STATUS
211 PeiCoreBuildHobHandoffInfoTable (
212 IN EFI_BOOT_MODE BootMode,
213 IN EFI_PHYSICAL_ADDRESS MemoryBegin,
214 IN UINT64 MemoryLength
215 )
216 {
217 EFI_HOB_HANDOFF_INFO_TABLE *Hob;
218 EFI_HOB_GENERIC_HEADER *HobEnd;
219
220 Hob = (VOID *)(UINTN)MemoryBegin;
221 HobEnd = (EFI_HOB_GENERIC_HEADER*) (Hob+1);
222 Hob->Header.HobType = EFI_HOB_TYPE_HANDOFF;
223 Hob->Header.HobLength = (UINT16) sizeof (EFI_HOB_HANDOFF_INFO_TABLE);
224 Hob->Header.Reserved = 0;
225
226 HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST;
227 HobEnd->HobLength = (UINT16) sizeof (EFI_HOB_GENERIC_HEADER);
228 HobEnd->Reserved = 0;
229
230 Hob->Version = EFI_HOB_HANDOFF_TABLE_VERSION;
231 Hob->BootMode = BootMode;
232
233 Hob->EfiMemoryTop = MemoryBegin + MemoryLength;
234 Hob->EfiMemoryBottom = MemoryBegin;
235 Hob->EfiFreeMemoryTop = MemoryBegin + MemoryLength;
236 Hob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS) (UINTN) (HobEnd + 1);
237 Hob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
238
239 return EFI_SUCCESS;
240 }