]>
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 | |
19 | #include <Protocol/FirmwareVolume2.h>\r | |
20 | #include <Protocol/LoadedImage.h>\r | |
21 | \r | |
22 | \r | |
23 | /**\r | |
24 | Internal function which read the image specified by Firmware File GUID name and \r | |
25 | the Firmware Section tyep from a specified Firmware Volume \r | |
26 | \r | |
27 | \r | |
28 | @param Fv The Firmware Volume Protocol instance.\r | |
29 | @param NameGuid The GUID name of a Firmware File.\r | |
30 | @param SectionType The Firmware Section type.\r | |
31 | @param Buffer On output, Buffer contains the the data read from the section in the Firmware File found.\r | |
32 | @param Size On output, the size of Buffer.\r | |
33 | \r | |
34 | @retval EFI_SUCCESS The image is found and data and size is returned.\r | |
35 | @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r | |
36 | @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r | |
37 | @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r | |
38 | @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r | |
39 | \r | |
40 | **/\r | |
41 | STATIC\r | |
42 | EFI_STATUS\r | |
43 | GetImageFromFv (\r | |
44 | IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r | |
45 | IN CONST EFI_GUID *NameGuid,\r | |
46 | IN EFI_SECTION_TYPE SectionType,\r | |
47 | OUT VOID **Buffer,\r | |
48 | OUT UINTN *Size\r | |
49 | )\r | |
50 | {\r | |
51 | EFI_STATUS Status;\r | |
52 | EFI_FV_FILETYPE FileType;\r | |
53 | EFI_FV_FILE_ATTRIBUTES Attributes;\r | |
54 | UINT32 AuthenticationStatus;\r | |
55 | \r | |
56 | //\r | |
57 | // Read desired section content in NameGuid file\r | |
58 | //\r | |
59 | *Buffer = NULL;\r | |
60 | *Size = 0;\r | |
61 | Status = Fv->ReadSection (\r | |
62 | Fv,\r | |
63 | NameGuid,\r | |
64 | SectionType,\r | |
65 | 0,\r | |
66 | Buffer,\r | |
67 | Size,\r | |
68 | &AuthenticationStatus\r | |
69 | );\r | |
70 | \r | |
71 | if (EFI_ERROR (Status) && (SectionType == EFI_SECTION_TE)) {\r | |
72 | //\r | |
73 | // Try reading PE32 section, since the TE section does not exist\r | |
74 | //\r | |
75 | *Buffer = NULL;\r | |
76 | *Size = 0;\r | |
77 | Status = Fv->ReadSection (\r | |
78 | Fv,\r | |
79 | NameGuid,\r | |
80 | EFI_SECTION_PE32,\r | |
81 | 0,\r | |
82 | Buffer,\r | |
83 | Size,\r | |
84 | &AuthenticationStatus\r | |
85 | );\r | |
86 | }\r | |
87 | \r | |
88 | if (EFI_ERROR (Status) && \r | |
89 | ((SectionType == EFI_SECTION_TE) || (SectionType == EFI_SECTION_PE32))) {\r | |
90 | //\r | |
91 | // Try reading raw file, since the desired section does not exist\r | |
92 | //\r | |
93 | *Buffer = NULL;\r | |
94 | *Size = 0;\r | |
95 | Status = Fv->ReadFile (\r | |
96 | Fv,\r | |
97 | NameGuid,\r | |
98 | Buffer,\r | |
99 | Size,\r | |
100 | &FileType,\r | |
101 | &Attributes,\r | |
102 | &AuthenticationStatus\r | |
103 | );\r | |
104 | }\r | |
105 | \r | |
106 | return Status;\r | |
107 | }\r | |
108 | \r | |
109 | /**\r | |
110 | Allocate and fill a buffer with an image identified by a Firmware File GUID name and a Firmware Section type. \r | |
111 | The Firmware Volumes to search for the Firmware File can be specified to be either all Firmware Volumes \r | |
112 | in the system, or the Firmware Volume which contains the Firmware File specified by an image handle.\r | |
113 | \r | |
114 | If ImageHandle is NULL, all Firmware Volumes in the system will be searched. If ImageHandle is not NULL, \r | |
115 | ImageHandle is interpreted as EFI_PEI_FILE_HANDLE for the implementation of this function for PEI phase. \r | |
116 | The input parameter ImageHandle is interpreted as EFI_HANDLE, on which an EFI_LOADED_IMAGE_PROTOCOL \r | |
117 | is installed, for the implementation of this function for DXE phase. The search always starts from the FV \r | |
118 | identified by ImageHandle. If WithinImageFv is TRUE, search will only be performed on the first FV. If WithinImageFv \r | |
119 | is FALSE, search will continue on other FVs if it fails on the first FV. The search order of Firmware Volumes is \r | |
120 | deterministic but arbitrary if no new firmware volume is added into the system between each search. \r | |
121 | \r | |
122 | The search order for the section type specified by SectionType in the Firmware File is using a depth-first \r | |
123 | and left-to-right algorithm through all sections. The first section found to match SectionType will be returned. \r | |
124 | \r | |
125 | If SectionType is EFI_SECTION_PE32, EFI_SECTION_PE32 will be used as Firmware Section type \r | |
126 | to read Firmware Section data from the Firmware File. If no such section exists, the function will try \r | |
127 | to read a Firmware File named with NameGuid. If no such file exists, EFI_NOT_FOUND is returned.\r | |
128 | \r | |
129 | If SectionType is EFI_SECTION_TE, EFI_SECTION_TE will be used as Firmware Section type to read Firmware Section \r | |
130 | data from the Firmware File. If no such section exists, EFI_SECTION_PE32 will be used as Firmware Section type to \r | |
131 | read Firmware Section data from the Firmware File. If no such section exists, the function will try to read a Firmware \r | |
132 | File named with NameGuid. If no such file exists, EFI_NOT_FOUND is returned.\r | |
133 | \r | |
134 | The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r | |
135 | by this function. This function can only be called at TPL_NOTIFY and below.\r | |
136 | \r | |
137 | If ImageHandle is NULL and WithinImage is TRUE, then ASSERT ();\r | |
138 | If NameGuid is NULL, then ASSERT();\r | |
139 | If Buffer is NULL, then ASSERT();\r | |
140 | If Size is NULL, then ASSERT().\r | |
141 | \r | |
142 | @param NameGuid The GUID name of a Firmware File.\r | |
143 | @param SectionType The Firmware Section type.\r | |
144 | @param Buffer On output, Buffer contains the the data read from the section in the Firmware File found.\r | |
145 | @param Size On output, the size of Buffer.\r | |
146 | \r | |
147 | @retval EFI_SUCCESS The image is found and data and size is returned.\r | |
148 | @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r | |
149 | @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r | |
150 | @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r | |
151 | @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r | |
152 | \r | |
153 | **/\r | |
154 | \r | |
155 | EFI_STATUS\r | |
156 | EFIAPI\r | |
157 | GetSectionFromFvFile (\r | |
158 | IN CONST VOID *ImageHandle,\r | |
159 | IN CONST EFI_GUID *NameGuid,\r | |
160 | IN EFI_SECTION_TYPE SectionType,\r | |
161 | OUT VOID **Buffer,\r | |
162 | OUT UINTN *Size,\r | |
163 | IN BOOLEAN WithinImageFv\r | |
164 | )\r | |
165 | {\r | |
166 | EFI_STATUS Status;\r | |
167 | EFI_HANDLE *HandleBuffer;\r | |
168 | UINTN HandleCount;\r | |
169 | UINTN Index;\r | |
170 | EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r | |
171 | EFI_FIRMWARE_VOLUME2_PROTOCOL *ImageFv;\r | |
172 | EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r | |
173 | \r | |
174 | ASSERT (NameGuid != NULL);\r | |
175 | ASSERT (Buffer != NULL);\r | |
176 | ASSERT (Size != NULL);\r | |
177 | ASSERT (!(ImageHandle == NULL && WithinImageFv));\r | |
178 | \r | |
179 | Status = EFI_NOT_FOUND;\r | |
180 | ImageFv = NULL;\r | |
181 | if (ImageHandle != NULL) {\r | |
182 | Status = gBS->HandleProtocol (\r | |
183 | (EFI_HANDLE *) ImageHandle,\r | |
184 | &gEfiLoadedImageProtocolGuid,\r | |
185 | (VOID **) &LoadedImage\r | |
186 | );\r | |
187 | if (EFI_ERROR (Status)) {\r | |
188 | return Status;\r | |
189 | }\r | |
190 | Status = gBS->HandleProtocol (\r | |
191 | LoadedImage->DeviceHandle,\r | |
192 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
193 | (VOID **) &ImageFv\r | |
194 | );\r | |
195 | if (!EFI_ERROR (Status)) {\r | |
196 | Status = GetImageFromFv (ImageFv, NameGuid, SectionType, Buffer, Size);\r | |
197 | }\r | |
198 | }\r | |
199 | \r | |
200 | if (Status == EFI_SUCCESS || WithinImageFv) {\r | |
201 | return Status;\r | |
202 | }\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 | //\r | |
217 | // Find desired image in all Fvs\r | |
218 | //\r | |
219 | for (Index = 0; Index < HandleCount; ++Index) {\r | |
220 | Status = gBS->HandleProtocol (\r | |
221 | HandleBuffer[Index],\r | |
222 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
223 | (VOID**)&Fv\r | |
224 | );\r | |
225 | \r | |
226 | if (EFI_ERROR (Status)) {\r | |
227 | goto Done;\r | |
228 | }\r | |
229 | \r | |
230 | if (ImageFv != NULL && Fv == ImageFv) {\r | |
231 | continue;\r | |
232 | }\r | |
233 | \r | |
234 | Status = GetImageFromFv (Fv, NameGuid, SectionType, Buffer, Size);\r | |
235 | \r | |
236 | if (!EFI_ERROR (Status)) {\r | |
237 | goto Done;\r | |
238 | }\r | |
239 | }\r | |
240 | \r | |
241 | //\r | |
242 | // Not found image\r | |
243 | //\r | |
244 | if (Index == HandleCount) {\r | |
245 | Status = EFI_NOT_FOUND;\r | |
246 | }\r | |
247 | \r | |
248 | Done:\r | |
249 | \r | |
250 | if (HandleBuffer != NULL) { \r | |
251 | FreePool(HandleBuffer);\r | |
252 | }\r | |
253 | \r | |
254 | return Status;\r | |
255 | }\r | |
256 | \r |