]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Dxe/FwVol/FwVolRead.c
PI 1.1 SMM Feature Check-in
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / FwVol / FwVolRead.c
... / ...
CommitLineData
1/** @file\r
2 Implements functions to read firmware file\r
3\r
4Copyright (c) 2006 - 2009, Intel Corporation. <BR>\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "DxeMain.h"\r
16#include "FwVolDriver.h"\r
17\r
18/**\r
19Required Alignment Alignment Value in FFS Alignment Value in\r
20(bytes) Attributes Field Firmware Volume Interfaces\r
211 0 0\r
222 0 1\r
234 0 2\r
248 0 3\r
2516 1 4\r
26128 2 7\r
27512 3 9\r
281 KB 4 10\r
294 KB 5 12\r
3032 KB 6 15\r
3164 KB 7 16\r
32**/\r
33UINT8 mFvAttributes[] = {0, 4, 7, 9, 10, 12, 15, 16};\r
34\r
35\r
36\r
37/**\r
38 Convert the FFS File Attributes to FV File Attributes\r
39\r
40 @param FfsAttributes The attributes of UINT8 type.\r
41\r
42 @return The attributes of EFI_FV_FILE_ATTRIBUTES\r
43\r
44**/\r
45EFI_FV_FILE_ATTRIBUTES\r
46FfsAttributes2FvFileAttributes (\r
47 IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes\r
48 )\r
49{\r
50 FfsAttributes = (EFI_FFS_FILE_ATTRIBUTES)((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT) >> 3);\r
51 ASSERT (FfsAttributes < 8);\r
52\r
53 return (EFI_FV_FILE_ATTRIBUTES) mFvAttributes[FfsAttributes];\r
54}\r
55\r
56\r
57\r
58/**\r
59 Given the input key, search for the next matching file in the volume.\r
60\r
61 @param This Indicates the calling context.\r
62 @param Key Key is a pointer to a caller allocated\r
63 buffer that contains implementation specific\r
64 data that is used to track where to begin\r
65 the search for the next file. The size of\r
66 the buffer must be at least This->KeySize\r
67 bytes long. To reinitialize the search and\r
68 begin from the beginning of the firmware\r
69 volume, the entire buffer must be cleared to\r
70 zero. Other than clearing the buffer to\r
71 initiate a new search, the caller must not\r
72 modify the data in the buffer between calls\r
73 to GetNextFile().\r
74 @param FileType FileType is a pointer to a caller allocated\r
75 EFI_FV_FILETYPE. The GetNextFile() API can\r
76 filter it's search for files based on the\r
77 value of *FileType input. A *FileType input\r
78 of 0 causes GetNextFile() to search for\r
79 files of all types. If a file is found, the\r
80 file's type is returned in *FileType.\r
81 *FileType is not modified if no file is\r
82 found.\r
83 @param NameGuid NameGuid is a pointer to a caller allocated\r
84 EFI_GUID. If a file is found, the file's\r
85 name is returned in *NameGuid. *NameGuid is\r
86 not modified if no file is found.\r
87 @param Attributes Attributes is a pointer to a caller\r
88 allocated EFI_FV_FILE_ATTRIBUTES. If a file\r
89 is found, the file's attributes are returned\r
90 in *Attributes. *Attributes is not modified\r
91 if no file is found.\r
92 @param Size Size is a pointer to a caller allocated\r
93 UINTN. If a file is found, the file's size\r
94 is returned in *Size. *Size is not modified\r
95 if no file is found.\r
96\r
97 @retval EFI_SUCCESS Successfully find the file.\r
98 @retval EFI_DEVICE_ERROR Device error.\r
99 @retval EFI_ACCESS_DENIED Fv could not read.\r
100 @retval EFI_NOT_FOUND No matching file found.\r
101 @retval EFI_INVALID_PARAMETER Invalid parameter\r
102\r
103**/\r
104EFI_STATUS\r
105EFIAPI\r
106FvGetNextFile (\r
107 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,\r
108 IN OUT VOID *Key,\r
109 IN OUT EFI_FV_FILETYPE *FileType,\r
110 OUT EFI_GUID *NameGuid,\r
111 OUT EFI_FV_FILE_ATTRIBUTES *Attributes,\r
112 OUT UINTN *Size\r
113 )\r
114{\r
115 EFI_STATUS Status;\r
116 FV_DEVICE *FvDevice;\r
117 EFI_FV_ATTRIBUTES FvAttributes;\r
118 EFI_FFS_FILE_HEADER *FfsFileHeader;\r
119 UINTN *KeyValue;\r
120 LIST_ENTRY *Link;\r
121 FFS_FILE_LIST_ENTRY *FfsFileEntry;\r
122 UINTN FileLength;\r
123\r
124 FvDevice = FV_DEVICE_FROM_THIS (This);\r
125\r
126 Status = FvGetVolumeAttributes (This, &FvAttributes);\r
127 if (EFI_ERROR (Status)){\r
128 return Status;\r
129 }\r
130\r
131 //\r
132 // Check if read operation is enabled\r
133 //\r
134 if ((FvAttributes & EFI_FV2_READ_STATUS) == 0) {\r
135 return EFI_ACCESS_DENIED;\r
136 }\r
137\r
138 if (*FileType > EFI_FV_FILETYPE_SMM_CORE) {\r
139 //\r
140 // File type needs to be in 0 - 0x0D\r
141 //\r
142 return EFI_NOT_FOUND;\r
143 }\r
144\r
145 KeyValue = (UINTN *)Key;\r
146 for (;;) {\r
147 if (*KeyValue == 0) {\r
148 //\r
149 // Search for 1st matching file\r
150 //\r
151 Link = &FvDevice->FfsFileListHeader;\r
152 } else {\r
153 //\r
154 // Key is pointer to FFsFileEntry, so get next one\r
155 //\r
156 Link = (LIST_ENTRY *)(*KeyValue);\r
157 }\r
158\r
159 if (Link->ForwardLink == &FvDevice->FfsFileListHeader) {\r
160 //\r
161 // Next is end of list so we did not find data\r
162 //\r
163 return EFI_NOT_FOUND;\r
164 }\r
165\r
166 FfsFileEntry = (FFS_FILE_LIST_ENTRY *)Link->ForwardLink;\r
167 FfsFileHeader = (EFI_FFS_FILE_HEADER *)FfsFileEntry->FfsHeader;\r
168\r
169 //\r
170 // remember the key\r
171 //\r
172 *KeyValue = (UINTN)FfsFileEntry;\r
173\r
174 if (FfsFileHeader->Type == EFI_FV_FILETYPE_FFS_PAD) {\r
175 //\r
176 // we ignore pad files\r
177 //\r
178 continue;\r
179 }\r
180\r
181 if (*FileType == EFI_FV_FILETYPE_ALL) {\r
182 //\r
183 // Process all file types so we have a match\r
184 //\r
185 break;\r
186 }\r
187\r
188 if (*FileType == FfsFileHeader->Type) {\r
189 //\r
190 // Found a matching file type\r
191 //\r
192 break;\r
193 }\r
194\r
195 }\r
196\r
197 //\r
198 // Return FileType, NameGuid, and Attributes\r
199 //\r
200 *FileType = FfsFileHeader->Type;\r
201 CopyGuid (NameGuid, &FfsFileHeader->Name);\r
202 *Attributes = FfsAttributes2FvFileAttributes (FfsFileHeader->Attributes);\r
203\r
204 //\r
205 // Read four bytes out of the 3 byte array and throw out extra data\r
206 //\r
207 FileLength = *(UINT32 *)&FfsFileHeader->Size[0] & 0x00FFFFFF;\r
208\r
209 //\r
210 // we need to substract the header size\r
211 //\r
212 *Size = FileLength - sizeof(EFI_FFS_FILE_HEADER);\r
213\r
214 return EFI_SUCCESS;\r
215}\r
216\r
217\r
218\r
219/**\r
220 Locates a file in the firmware volume and\r
221 copies it to the supplied buffer.\r
222\r
223 @param This Indicates the calling context.\r
224 @param NameGuid Pointer to an EFI_GUID, which is the\r
225 filename.\r
226 @param Buffer Buffer is a pointer to pointer to a buffer\r
227 in which the file or section contents or are\r
228 returned.\r
229 @param BufferSize BufferSize is a pointer to caller allocated\r
230 UINTN. On input *BufferSize indicates the\r
231 size in bytes of the memory region pointed\r
232 to by Buffer. On output, *BufferSize\r
233 contains the number of bytes required to\r
234 read the file.\r
235 @param FoundType FoundType is a pointer to a caller allocated\r
236 EFI_FV_FILETYPE that on successful return\r
237 from Read() contains the type of file read.\r
238 This output reflects the file type\r
239 irrespective of the value of the SectionType\r
240 input.\r
241 @param FileAttributes FileAttributes is a pointer to a caller\r
242 allocated EFI_FV_FILE_ATTRIBUTES. On\r
243 successful return from Read(),\r
244 *FileAttributes contains the attributes of\r
245 the file read.\r
246 @param AuthenticationStatus AuthenticationStatus is a pointer to a\r
247 caller allocated UINTN in which the\r
248 authentication status is returned.\r
249\r
250 @retval EFI_SUCCESS Successfully read to memory buffer.\r
251 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.\r
252 @retval EFI_NOT_FOUND Not found.\r
253 @retval EFI_DEVICE_ERROR Device error.\r
254 @retval EFI_ACCESS_DENIED Could not read.\r
255 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
256 @retval EFI_OUT_OF_RESOURCES Not enough buffer to be allocated.\r
257\r
258**/\r
259EFI_STATUS\r
260EFIAPI\r
261FvReadFile (\r
262 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,\r
263 IN CONST EFI_GUID *NameGuid,\r
264 IN OUT VOID **Buffer,\r
265 IN OUT UINTN *BufferSize,\r
266 OUT EFI_FV_FILETYPE *FoundType,\r
267 OUT EFI_FV_FILE_ATTRIBUTES *FileAttributes,\r
268 OUT UINT32 *AuthenticationStatus\r
269 )\r
270{\r
271 EFI_STATUS Status;\r
272 FV_DEVICE *FvDevice;\r
273 EFI_GUID SearchNameGuid;\r
274 EFI_FV_FILETYPE LocalFoundType;\r
275 EFI_FV_FILE_ATTRIBUTES LocalAttributes;\r
276 UINTN FileSize;\r
277 UINT8 *SrcPtr;\r
278 EFI_FFS_FILE_HEADER *FfsHeader;\r
279 UINTN InputBufferSize;\r
280\r
281 if (NameGuid == NULL) {\r
282 return EFI_INVALID_PARAMETER;\r
283 }\r
284\r
285 FvDevice = FV_DEVICE_FROM_THIS (This);\r
286\r
287\r
288 //\r
289 // Keep looking until we find the matching NameGuid.\r
290 // The Key is really an FfsFileEntry\r
291 //\r
292 FvDevice->LastKey = 0;\r
293 do {\r
294 LocalFoundType = 0;\r
295 Status = FvGetNextFile (\r
296 This,\r
297 &FvDevice->LastKey,\r
298 &LocalFoundType,\r
299 &SearchNameGuid,\r
300 &LocalAttributes,\r
301 &FileSize\r
302 );\r
303 if (EFI_ERROR (Status)) {\r
304 return EFI_NOT_FOUND;\r
305 }\r
306 } while (!CompareGuid (&SearchNameGuid, NameGuid));\r
307\r
308 //\r
309 // Get a pointer to the header\r
310 //\r
311 FfsHeader = FvDevice->LastKey->FfsHeader;\r
312\r
313 //\r
314 // Remember callers buffer size\r
315 //\r
316 InputBufferSize = *BufferSize;\r
317\r
318 //\r
319 // Calculate return values\r
320 //\r
321 *FoundType = FfsHeader->Type;\r
322 *FileAttributes = FfsAttributes2FvFileAttributes (FfsHeader->Attributes);\r
323 *AuthenticationStatus = 0;\r
324 *BufferSize = FileSize;\r
325\r
326 if (Buffer == NULL) {\r
327 //\r
328 // If Buffer is NULL, we only want to get the information colected so far\r
329 //\r
330 return EFI_SUCCESS;\r
331 }\r
332\r
333 //\r
334 // Skip over file header\r
335 //\r
336 SrcPtr = ((UINT8 *)FfsHeader) + sizeof (EFI_FFS_FILE_HEADER);\r
337\r
338 Status = EFI_SUCCESS;\r
339 if (*Buffer == NULL) {\r
340 //\r
341 // Caller passed in a pointer so allocate buffer for them\r
342 //\r
343 *Buffer = AllocatePool (FileSize);\r
344 if (*Buffer == NULL) {\r
345 return EFI_OUT_OF_RESOURCES;\r
346 }\r
347 } else if (FileSize > InputBufferSize) {\r
348 //\r
349 // Callers buffer was not big enough\r
350 //\r
351 Status = EFI_WARN_BUFFER_TOO_SMALL;\r
352 FileSize = InputBufferSize;\r
353 }\r
354\r
355 //\r
356 // Copy data into callers buffer\r
357 //\r
358 CopyMem (*Buffer, SrcPtr, FileSize);\r
359\r
360 return Status;\r
361}\r
362\r
363\r
364\r
365/**\r
366 Locates a section in a given FFS File and\r
367 copies it to the supplied buffer (not including section header).\r
368\r
369 @param This Indicates the calling context.\r
370 @param NameGuid Pointer to an EFI_GUID, which is the\r
371 filename.\r
372 @param SectionType Indicates the section type to return.\r
373 @param SectionInstance Indicates which instance of sections with a\r
374 type of SectionType to return.\r
375 @param Buffer Buffer is a pointer to pointer to a buffer\r
376 in which the file or section contents or are\r
377 returned.\r
378 @param BufferSize BufferSize is a pointer to caller allocated\r
379 UINTN.\r
380 @param AuthenticationStatus AuthenticationStatus is a pointer to a\r
381 caller allocated UINT32 in which the\r
382 authentication status is returned.\r
383\r
384 @retval EFI_SUCCESS Successfully read the file section into\r
385 buffer.\r
386 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.\r
387 @retval EFI_NOT_FOUND Section not found.\r
388 @retval EFI_DEVICE_ERROR Device error.\r
389 @retval EFI_ACCESS_DENIED Could not read.\r
390 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
391\r
392**/\r
393EFI_STATUS\r
394EFIAPI\r
395FvReadFileSection (\r
396 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,\r
397 IN CONST EFI_GUID *NameGuid,\r
398 IN EFI_SECTION_TYPE SectionType,\r
399 IN UINTN SectionInstance,\r
400 IN OUT VOID **Buffer,\r
401 IN OUT UINTN *BufferSize,\r
402 OUT UINT32 *AuthenticationStatus\r
403 )\r
404{\r
405 EFI_STATUS Status;\r
406 FV_DEVICE *FvDevice;\r
407 EFI_FV_FILETYPE FileType;\r
408 EFI_FV_FILE_ATTRIBUTES FileAttributes;\r
409 UINTN FileSize;\r
410 UINT8 *FileBuffer;\r
411 FFS_FILE_LIST_ENTRY *FfsEntry;\r
412\r
413 if (NameGuid == NULL || Buffer == NULL) {\r
414 return EFI_INVALID_PARAMETER;\r
415 }\r
416\r
417 FvDevice = FV_DEVICE_FROM_THIS (This);\r
418\r
419 //\r
420 // Read the whole file into buffer\r
421 //\r
422 FileBuffer = NULL;\r
423 Status = FvReadFile (\r
424 This,\r
425 NameGuid,\r
426 (VOID **)&FileBuffer,\r
427 &FileSize,\r
428 &FileType,\r
429 &FileAttributes,\r
430 AuthenticationStatus\r
431 );\r
432 //\r
433 // Get the last key used by our call to FvReadFile as it is the FfsEntry for this file.\r
434 //\r
435 FfsEntry = (FFS_FILE_LIST_ENTRY *) FvDevice->LastKey;\r
436\r
437 if (EFI_ERROR (Status)) {\r
438 return Status;\r
439 }\r
440\r
441 //\r
442 // Check to see that the file actually HAS sections before we go any further.\r
443 //\r
444 if (FileType == EFI_FV_FILETYPE_RAW) {\r
445 Status = EFI_NOT_FOUND;\r
446 goto Done;\r
447 }\r
448\r
449 //\r
450 // Use FfsEntry to cache Section Extraction Protocol Inforomation\r
451 //\r
452 if (FfsEntry->StreamHandle == 0) {\r
453 Status = OpenSectionStream (\r
454 FileSize,\r
455 FileBuffer,\r
456 &FfsEntry->StreamHandle\r
457 );\r
458 if (EFI_ERROR (Status)) {\r
459 goto Done;\r
460 }\r
461 }\r
462\r
463 //\r
464 // If SectionType == 0 We need the whole section stream\r
465 //\r
466 Status = GetSection (\r
467 FfsEntry->StreamHandle,\r
468 (SectionType == 0) ? NULL : &SectionType,\r
469 NULL,\r
470 (SectionType == 0) ? 0 : SectionInstance,\r
471 Buffer,\r
472 BufferSize,\r
473 AuthenticationStatus\r
474 );\r
475\r
476 //\r
477 // Close of stream defered to close of FfsHeader list to allow SEP to cache data\r
478 //\r
479\r
480Done:\r
481 CoreFreePool (FileBuffer);\r
482\r
483 return Status;\r
484}\r
485\r
486\r