]>
Commit | Line | Data |
---|---|---|
1c280088 | 1 | /** @file\r |
2 | Mde PI library functions.\r | |
3 | \r | |
4 | Copyright (c) 2007, 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 | **/\r | |
14 | \r | |
15 | #include <PiDxe.h>\r | |
16 | #include <Library/DebugLib.h>\r | |
17 | #include <Library/MemoryAllocationLib.h>\r | |
18 | #include <Library/UefiBootServicesTableLib.h>\r | |
166152e8 | 19 | #include <Library/DxePiLib.h>\r |
1c280088 | 20 | #include <Protocol/FirmwareVolume2.h>\r |
21 | #include <Protocol/LoadedImage.h>\r | |
22 | \r | |
23 | \r | |
24 | /**\r | |
166152e8 | 25 | Identify the device handle from which the Image is loaded from. As this device handle is passed to\r |
26 | GetSectionFromFv as the identifier for a Firmware Volume, an EFI_FIRMWARE_VOLUME2_PROTOCOL \r | |
27 | protocol instance should be located succesfully by calling gBS->HandleProtocol ().\r | |
1c280088 | 28 | \r |
166152e8 | 29 | This function locates the EFI_LOADED_IMAGE_PROTOCOL instance installed\r |
30 | on ImageHandle. It then returns EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle.\r | |
31 | \r | |
32 | If ImageHandle is NULL, then ASSERT ();\r | |
33 | If failed to locate a EFI_LOADED_IMAGE_PROTOCOL on ImageHandle, then ASSERT ();\r | |
34 | \r | |
35 | @param ImageHandle The firmware allocated handle for UEFI image.\r | |
1c280088 | 36 | \r |
166152e8 | 37 | @retval EFI_HANDLE The device handle from which the Image is loaded from.\r |
1c280088 | 38 | \r |
39 | **/\r | |
166152e8 | 40 | EFI_HANDLE\r |
41 | InternalImageHandleToFvHandle (\r | |
42 | EFI_HANDLE ImageHandle\r | |
43 | )\r | |
44 | {\r | |
45 | EFI_STATUS Status;\r | |
46 | EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r | |
47 | \r | |
48 | ASSERT (ImageHandle != NULL);\r | |
49 | \r | |
50 | Status = gBS->HandleProtocol (\r | |
51 | (EFI_HANDLE *) ImageHandle,\r | |
52 | &gEfiLoadedImageProtocolGuid,\r | |
53 | (VOID **) &LoadedImage\r | |
54 | );\r | |
55 | \r | |
56 | ASSERT_EFI_ERROR (Status);\r | |
57 | \r | |
58 | return LoadedImage->DeviceHandle;\r | |
59 | \r | |
60 | }\r | |
61 | \r | |
62 | /**\r | |
63 | Allocate and fill a buffer from a Firmware Section identified by a Firmware File GUID name, a Firmware \r | |
64 | Section type and instance number from the specified Firmware Volume.\r | |
65 | \r | |
66 | This functions first locate the EFI_FIRMWARE_VOLUME2_PROTOCOL protocol instance on FvHandle in order to \r | |
67 | carry out the Firmware Volume read operation. The function then reads the Firmware Section found sepcifed \r | |
68 | by NameGuid, SectionType and Instance. \r | |
69 | \r | |
70 | The search order for the section specified by SectionType within a Firmware File is defined by\r | |
71 | EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection (). Please check Section 2.4 of Volume 3: Platform Initialization \r | |
72 | Shared Architectural Elements for detailes.\r | |
73 | \r | |
74 | If SectionType is EFI_SECTION_TE, EFI_SECTION_TE will be used as Firmware Section type to read Firmware Section \r | |
75 | data from the Firmware File. If no such section exists, EFI_SECTION_PE32 will be used as Firmware Section type to \r | |
76 | read Firmware Section data from the Firmware File. If no such section specified is found to match , \r | |
77 | EFI_NOT_FOUND is returned.\r | |
78 | \r | |
79 | The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r | |
80 | by this function. This function can be only called at TPL_NOTIFY and below.\r | |
81 | \r | |
82 | If FvHandle is NULL, then ASSERT ();\r | |
83 | If NameGuid is NULL, then ASSERT();\r | |
84 | If Buffer is NULL, then ASSERT();\r | |
85 | If Size is NULL, then ASSERT().\r | |
86 | \r | |
87 | @param FvHandle The device handle that contains a instance of EFI_FIRMWARE_VOLUME2_PROTOCOL instance.\r | |
88 | @param NameGuid The GUID name of a Firmware File.\r | |
89 | @param SectionType The Firmware Section type.\r | |
90 | @param Instance The instance number of Firmware Section to read from starting from 0.\r | |
91 | @param Buffer On output, Buffer contains the the data read from the section in the Firmware File found.\r | |
92 | @param Size On output, the size of Buffer.\r | |
93 | \r | |
94 | @retval EFI_SUCCESS The image is found and data and size is returned.\r | |
95 | @retval EFI_UNSUPPORTED FvHandle does not support EFI_FIRMWARE_VOLUME2_PROTOCOL.\r | |
96 | @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r | |
97 | @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r | |
98 | @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r | |
99 | @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r | |
100 | \r | |
101 | **/\r | |
102 | \r | |
103 | \r | |
1c280088 | 104 | EFI_STATUS\r |
166152e8 | 105 | GetSectionFromFv (\r |
106 | IN EFI_HANDLE FvHandle,\r | |
107 | IN CONST EFI_GUID *NameGuid,\r | |
108 | IN EFI_SECTION_TYPE SectionType,\r | |
109 | IN UINTN Instance,\r | |
110 | OUT VOID **Buffer,\r | |
111 | OUT UINTN *Size\r | |
1c280088 | 112 | )\r |
113 | {\r | |
166152e8 | 114 | EFI_STATUS Status;\r |
115 | EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r | |
116 | UINT32 AuthenticationStatus;\r | |
117 | \r | |
118 | ASSERT (FvHandle != NULL);\r | |
119 | \r | |
120 | Status = gBS->HandleProtocol (\r | |
121 | FvHandle,\r | |
122 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
123 | (VOID **) &Fv\r | |
124 | );\r | |
125 | if (EFI_ERROR (Status)) {\r | |
126 | return Status;\r | |
127 | }\r | |
1c280088 | 128 | \r |
129 | //\r | |
130 | // Read desired section content in NameGuid file\r | |
131 | //\r | |
132 | *Buffer = NULL;\r | |
133 | *Size = 0;\r | |
134 | Status = Fv->ReadSection (\r | |
135 | Fv,\r | |
136 | NameGuid,\r | |
137 | SectionType,\r | |
138 | 0,\r | |
139 | Buffer,\r | |
140 | Size,\r | |
141 | &AuthenticationStatus\r | |
142 | );\r | |
143 | \r | |
144 | if (EFI_ERROR (Status) && (SectionType == EFI_SECTION_TE)) {\r | |
145 | //\r | |
166152e8 | 146 | // Try reading PE32 section, if the required section is TE type \r |
1c280088 | 147 | //\r |
148 | *Buffer = NULL;\r | |
149 | *Size = 0;\r | |
150 | Status = Fv->ReadSection (\r | |
151 | Fv,\r | |
152 | NameGuid,\r | |
153 | EFI_SECTION_PE32,\r | |
154 | 0,\r | |
155 | Buffer,\r | |
156 | Size,\r | |
157 | &AuthenticationStatus\r | |
158 | );\r | |
159 | }\r | |
160 | \r | |
1c280088 | 161 | return Status;\r |
162 | }\r | |
163 | \r | |
b0d803fe | 164 | \r |
b0d803fe | 165 | \r |
166 | EFI_STATUS\r | |
167 | EFIAPI\r | |
166152e8 | 168 | PiLibGetSectionFromAnyFv (\r |
b0d803fe | 169 | IN CONST EFI_GUID *NameGuid,\r |
170 | IN EFI_SECTION_TYPE SectionType,\r | |
171 | IN UINTN Instance,\r | |
172 | OUT VOID **Buffer,\r | |
173 | OUT UINTN *Size\r | |
174 | )\r | |
175 | {\r | |
176 | EFI_STATUS Status;\r | |
177 | EFI_HANDLE *HandleBuffer;\r | |
178 | UINTN HandleCount;\r | |
179 | UINTN Index;\r | |
180 | EFI_HANDLE FvHandle;\r | |
181 | EFI_TPL OldTpl;\r | |
182 | \r | |
183 | //\r | |
184 | // Search the FV that contain the caller's FFS first.\r | |
185 | // FV builder can choose to build FFS into the this FV\r | |
186 | // so that this implementation of GetSectionFromAnyFv\r | |
187 | // will locate the FFS faster.\r | |
188 | //\r | |
166152e8 | 189 | FvHandle = InternalImageHandleToFvHandle (gImageHandle);\r |
b0d803fe | 190 | Status = GetSectionFromFv (\r |
191 | FvHandle,\r | |
192 | NameGuid,\r | |
193 | SectionType,\r | |
194 | Instance,\r | |
195 | Buffer,\r | |
196 | Size\r | |
197 | );\r | |
198 | if (!EFI_ERROR (Status)) {\r | |
199 | return EFI_SUCCESS;\r | |
200 | }\r | |
201 | \r | |
202 | OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r | |
203 | \r | |
204 | HandleBuffer = NULL;\r | |
205 | Status = gBS->LocateHandleBuffer (\r | |
206 | ByProtocol,\r | |
207 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
208 | NULL,\r | |
209 | &HandleCount,\r | |
210 | &HandleBuffer\r | |
211 | );\r | |
212 | if (EFI_ERROR (Status)) {\r | |
213 | goto Done;\r | |
214 | }\r | |
215 | \r | |
216 | for (Index = 0; Index < HandleCount; ++Index) {\r | |
217 | //\r | |
218 | // Skip the FV that contain the caller's FFS\r | |
219 | //\r | |
220 | if (HandleBuffer[Index] == FvHandle) {\r | |
221 | continue;\r | |
222 | }\r | |
223 | \r | |
224 | Status = GetSectionFromFv (\r | |
225 | HandleBuffer[Index], \r | |
226 | NameGuid, \r | |
227 | SectionType, \r | |
228 | Instance,\r | |
229 | Buffer, \r | |
230 | Size\r | |
231 | );\r | |
232 | \r | |
233 | if (!EFI_ERROR (Status)) {\r | |
234 | goto Done;\r | |
235 | }\r | |
236 | }\r | |
237 | \r | |
238 | if (Index == HandleCount) {\r | |
239 | Status = EFI_NOT_FOUND;\r | |
240 | }\r | |
241 | \r | |
242 | Done:\r | |
243 | \r | |
244 | gBS->RestoreTPL (OldTpl);\r | |
245 | \r | |
246 | if (HandleBuffer != NULL) { \r | |
247 | FreePool(HandleBuffer);\r | |
248 | }\r | |
249 | return Status;\r | |
250 | \r | |
251 | }\r | |
252 | \r | |
b0d803fe | 253 | \r |
254 | EFI_STATUS\r | |
255 | EFIAPI\r | |
166152e8 | 256 | PiLibGetSectionFromCurrentFv (\r |
b0d803fe | 257 | IN CONST EFI_GUID *NameGuid,\r |
258 | IN EFI_SECTION_TYPE SectionType,\r | |
259 | IN UINTN Instance,\r | |
260 | OUT VOID **Buffer,\r | |
261 | OUT UINTN *Size\r | |
262 | )\r | |
263 | {\r | |
264 | return GetSectionFromFv(\r | |
166152e8 | 265 | InternalImageHandleToFvHandle(gImageHandle),\r |
b0d803fe | 266 | NameGuid,\r |
267 | SectionType,\r | |
268 | Instance,\r | |
269 | Buffer,\r | |
270 | Size\r | |
271 | );\r | |
272 | }\r | |
273 | \r | |
274 | \r | |
275 | \r | |
276 | EFI_STATUS\r | |
277 | EFIAPI\r | |
166152e8 | 278 | PiLibGetSectionFromCurrentFfs (\r |
b0d803fe | 279 | IN EFI_SECTION_TYPE SectionType,\r |
280 | IN UINTN Instance,\r | |
281 | OUT VOID **Buffer,\r | |
282 | OUT UINTN *Size\r | |
283 | )\r | |
284 | {\r | |
285 | return GetSectionFromFv(\r | |
166152e8 | 286 | InternalImageHandleToFvHandle(gImageHandle),\r |
b0d803fe | 287 | &gEfiCallerIdGuid,\r |
288 | SectionType,\r | |
289 | Instance,\r | |
290 | Buffer,\r | |
291 | Size\r | |
292 | );\r | |
293 | }\r | |
294 | \r |