]>
Commit | Line | Data |
---|---|---|
1 | /*++\r | |
2 | \r | |
3 | Copyright (c) 2006 - 2007, Intel Corporation. All rights reserved.<BR>\r | |
4 | This program and the accompanying materials\r | |
5 | are licensed and made available under the terms and conditions of the BSD License\r | |
6 | which accompanies this distribution. The full text of the license may be found at\r | |
7 | http://opensource.org/licenses/bsd-license.php\r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
11 | \r | |
12 | Module Name:\r | |
13 | \r | |
14 | GetImage.c\r | |
15 | \r | |
16 | Abstract:\r | |
17 | \r | |
18 | Image data extraction support for common use.\r | |
19 | \r | |
20 | --*/\r | |
21 | \r | |
22 | #include "Tiano.h"\r | |
23 | #include "EfiDriverLib.h"\r | |
24 | #include "EfiImageFormat.h"\r | |
25 | \r | |
26 | #include EFI_PROTOCOL_CONSUMER (LoadedImage)\r | |
27 | \r | |
28 | EFI_STATUS\r | |
29 | GetImageFromFv (\r | |
30 | #if (PI_SPECIFICATION_VERSION < 0x00010000)\r | |
31 | IN EFI_FIRMWARE_VOLUME_PROTOCOL *Fv,\r | |
32 | #else\r | |
33 | IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r | |
34 | #endif\r | |
35 | IN EFI_GUID *NameGuid,\r | |
36 | IN EFI_SECTION_TYPE SectionType,\r | |
37 | OUT VOID **Buffer,\r | |
38 | OUT UINTN *Size\r | |
39 | )\r | |
40 | {\r | |
41 | EFI_STATUS Status;\r | |
42 | EFI_FV_FILETYPE FileType;\r | |
43 | EFI_FV_FILE_ATTRIBUTES Attributes;\r | |
44 | UINT32 AuthenticationStatus;\r | |
45 | \r | |
46 | //\r | |
47 | // Read desired section content in NameGuid file\r | |
48 | //\r | |
49 | *Buffer = NULL;\r | |
50 | *Size = 0;\r | |
51 | Status = Fv->ReadSection (\r | |
52 | Fv,\r | |
53 | NameGuid,\r | |
54 | SectionType,\r | |
55 | 0,\r | |
56 | Buffer,\r | |
57 | Size,\r | |
58 | &AuthenticationStatus\r | |
59 | );\r | |
60 | \r | |
61 | if (EFI_ERROR (Status) && (SectionType == EFI_SECTION_TE)) {\r | |
62 | //\r | |
63 | // Try reading PE32 section, since the TE section does not exist\r | |
64 | //\r | |
65 | *Buffer = NULL;\r | |
66 | *Size = 0;\r | |
67 | Status = Fv->ReadSection (\r | |
68 | Fv,\r | |
69 | NameGuid,\r | |
70 | EFI_SECTION_PE32,\r | |
71 | 0,\r | |
72 | Buffer,\r | |
73 | Size,\r | |
74 | &AuthenticationStatus\r | |
75 | );\r | |
76 | }\r | |
77 | \r | |
78 | if (EFI_ERROR (Status) && \r | |
79 | ((SectionType == EFI_SECTION_TE) || (SectionType == EFI_SECTION_PE32))) {\r | |
80 | //\r | |
81 | // Try reading raw file, since the desired section does not exist\r | |
82 | //\r | |
83 | *Buffer = NULL;\r | |
84 | *Size = 0;\r | |
85 | Status = Fv->ReadFile (\r | |
86 | Fv,\r | |
87 | NameGuid,\r | |
88 | Buffer,\r | |
89 | Size,\r | |
90 | &FileType,\r | |
91 | &Attributes,\r | |
92 | &AuthenticationStatus\r | |
93 | );\r | |
94 | }\r | |
95 | \r | |
96 | return Status;\r | |
97 | }\r | |
98 | \r | |
99 | EFI_STATUS\r | |
100 | GetImage (\r | |
101 | IN EFI_GUID *NameGuid,\r | |
102 | IN EFI_SECTION_TYPE SectionType,\r | |
103 | OUT VOID **Buffer,\r | |
104 | OUT UINTN *Size\r | |
105 | )\r | |
106 | {\r | |
107 | return GetImageEx (NULL, NameGuid, SectionType, Buffer, Size, FALSE);\r | |
108 | }\r | |
109 | \r | |
110 | EFI_STATUS\r | |
111 | GetImageEx (\r | |
112 | IN EFI_HANDLE ImageHandle,\r | |
113 | IN EFI_GUID *NameGuid,\r | |
114 | IN EFI_SECTION_TYPE SectionType,\r | |
115 | OUT VOID **Buffer,\r | |
116 | OUT UINTN *Size,\r | |
117 | BOOLEAN WithinImageFv\r | |
118 | )\r | |
119 | {\r | |
120 | EFI_STATUS Status;\r | |
121 | EFI_HANDLE *HandleBuffer;\r | |
122 | UINTN HandleCount;\r | |
123 | UINTN Index;\r | |
124 | EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r | |
125 | #if (PI_SPECIFICATION_VERSION < 0x00010000)\r | |
126 | EFI_FIRMWARE_VOLUME_PROTOCOL *ImageFv;\r | |
127 | EFI_FIRMWARE_VOLUME_PROTOCOL *Fv;\r | |
128 | #else\r | |
129 | EFI_FIRMWARE_VOLUME2_PROTOCOL *ImageFv;\r | |
130 | EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r | |
131 | #endif\r | |
132 | \r | |
133 | if (ImageHandle == NULL && WithinImageFv) {\r | |
134 | return EFI_INVALID_PARAMETER;\r | |
135 | }\r | |
136 | \r | |
137 | Status = EFI_NOT_FOUND;\r | |
138 | ImageFv = NULL;\r | |
139 | if (ImageHandle != NULL) {\r | |
140 | Status = gBS->HandleProtocol (\r | |
141 | ImageHandle,\r | |
142 | &gEfiLoadedImageProtocolGuid,\r | |
143 | (VOID **) &LoadedImage\r | |
144 | );\r | |
145 | if (EFI_ERROR (Status)) {\r | |
146 | return Status;\r | |
147 | }\r | |
148 | Status = gBS->HandleProtocol (\r | |
149 | LoadedImage->DeviceHandle,\r | |
150 | #if (PI_SPECIFICATION_VERSION < 0x00010000) \r | |
151 | &gEfiFirmwareVolumeProtocolGuid,\r | |
152 | #else\r | |
153 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
154 | #endif\r | |
155 | (VOID **) &ImageFv\r | |
156 | );\r | |
157 | if (!EFI_ERROR (Status)) {\r | |
158 | Status = GetImageFromFv (ImageFv, NameGuid, SectionType, Buffer, Size);\r | |
159 | }\r | |
160 | }\r | |
161 | \r | |
162 | if (Status == EFI_SUCCESS || WithinImageFv) {\r | |
163 | return Status;\r | |
164 | }\r | |
165 | \r | |
166 | Status = gBS->LocateHandleBuffer (\r | |
167 | ByProtocol,\r | |
168 | #if (PI_SPECIFICATION_VERSION < 0x00010000)\r | |
169 | &gEfiFirmwareVolumeProtocolGuid,\r | |
170 | #else\r | |
171 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
172 | #endif\r | |
173 | NULL,\r | |
174 | &HandleCount,\r | |
175 | &HandleBuffer\r | |
176 | );\r | |
177 | if (EFI_ERROR (Status)) {\r | |
178 | return Status;\r | |
179 | }\r | |
180 | \r | |
181 | //\r | |
182 | // Find desired image in all Fvs\r | |
183 | //\r | |
184 | for (Index = 0; Index < HandleCount; ++Index) {\r | |
185 | Status = gBS->HandleProtocol (\r | |
186 | HandleBuffer[Index],\r | |
187 | #if (PI_SPECIFICATION_VERSION < 0x00010000)\r | |
188 | &gEfiFirmwareVolumeProtocolGuid,\r | |
189 | #else\r | |
190 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
191 | #endif\r | |
192 | (VOID**)&Fv\r | |
193 | );\r | |
194 | \r | |
195 | if (EFI_ERROR (Status)) {\r | |
196 | gBS->FreePool(HandleBuffer);\r | |
197 | return Status;\r | |
198 | }\r | |
199 | \r | |
200 | if (ImageFv != NULL && Fv == ImageFv) {\r | |
201 | continue;\r | |
202 | }\r | |
203 | \r | |
204 | Status = GetImageFromFv (Fv, NameGuid, SectionType, Buffer, Size);\r | |
205 | \r | |
206 | if (!EFI_ERROR (Status)) {\r | |
207 | break;\r | |
208 | }\r | |
209 | }\r | |
210 | gBS->FreePool(HandleBuffer);\r | |
211 | \r | |
212 | //\r | |
213 | // Not found image\r | |
214 | //\r | |
215 | if (Index == HandleCount) {\r | |
216 | return EFI_NOT_FOUND;\r | |
217 | }\r | |
218 | \r | |
219 | return EFI_SUCCESS;\r | |
220 | }\r |