]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Dxe/FwVol/FwVolRead.c
Add core FFS3 support, DxeCore.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / FwVol / FwVolRead.c
CommitLineData
23c98c94 1/** @file\r
504214c4 2 Implements functions to read firmware file\r
28a00297 3\r
6c85d162 4Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
cd5ebaa0 5This program and the accompanying materials\r
23c98c94 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
28a00297 12\r
504214c4 13**/\r
28a00297 14\r
9c4ac31c 15#include "DxeMain.h"\r
ec90508b 16#include "FwVolDriver.h"\r
28a00297 17\r
ec90508b 18/**\r
19Required Alignment Alignment Value in FFS Alignment Value in\r
28a00297 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
ec90508b 32**/\r
022c6d45 33UINT8 mFvAttributes[] = {0, 4, 7, 9, 10, 12, 15, 16};\r
28a00297 34\r
35\r
162ed594 36\r
37/**\r
38 Convert the FFS File Attributes to FV File Attributes\r
39\r
022c6d45 40 @param FfsAttributes The attributes of UINT8 type.\r
162ed594 41\r
42 @return The attributes of EFI_FV_FILE_ATTRIBUTES\r
43\r
44**/\r
28a00297 45EFI_FV_FILE_ATTRIBUTES\r
46FfsAttributes2FvFileAttributes (\r
47 IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes\r
48 )\r
28a00297 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
162ed594 57\r
58/**\r
59 Given the input key, search for the next matching file in the volume.\r
60\r
022c6d45 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
162ed594 101 @retval EFI_INVALID_PARAMETER Invalid parameter\r
102\r
103**/\r
28a00297 104EFI_STATUS\r
105EFIAPI\r
106FvGetNextFile (\r
0c2b5da8 107 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,\r
108 IN OUT VOID *Key,\r
109 IN OUT EFI_FV_FILETYPE *FileType,\r
28a00297 110 OUT EFI_GUID *NameGuid,\r
111 OUT EFI_FV_FILE_ATTRIBUTES *Attributes,\r
162ed594 112 OUT UINTN *Size\r
28a00297 113 )\r
28a00297 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
28a00297 122\r
123 FvDevice = FV_DEVICE_FROM_THIS (This);\r
124\r
125 Status = FvGetVolumeAttributes (This, &FvAttributes);\r
126 if (EFI_ERROR (Status)){\r
127 return Status;\r
128 }\r
129\r
130 //\r
131 // Check if read operation is enabled\r
132 //\r
0c2b5da8 133 if ((FvAttributes & EFI_FV2_READ_STATUS) == 0) {\r
28a00297 134 return EFI_ACCESS_DENIED;\r
135 }\r
136\r
202c3279 137 if (*FileType > EFI_FV_FILETYPE_SMM_CORE) {\r
28a00297 138 //\r
202c3279 139 // File type needs to be in 0 - 0x0D\r
28a00297 140 //\r
26fa3b49 141 return EFI_NOT_FOUND;\r
28a00297 142 }\r
143\r
144 KeyValue = (UINTN *)Key;\r
145 for (;;) {\r
146 if (*KeyValue == 0) {\r
147 //\r
148 // Search for 1st matching file\r
149 //\r
150 Link = &FvDevice->FfsFileListHeader;\r
151 } else {\r
152 //\r
153 // Key is pointer to FFsFileEntry, so get next one\r
154 //\r
155 Link = (LIST_ENTRY *)(*KeyValue);\r
156 }\r
157\r
158 if (Link->ForwardLink == &FvDevice->FfsFileListHeader) {\r
159 //\r
160 // Next is end of list so we did not find data\r
161 //\r
162 return EFI_NOT_FOUND;\r
163 }\r
164\r
165 FfsFileEntry = (FFS_FILE_LIST_ENTRY *)Link->ForwardLink;\r
166 FfsFileHeader = (EFI_FFS_FILE_HEADER *)FfsFileEntry->FfsHeader;\r
167\r
168 //\r
169 // remember the key\r
170 //\r
171 *KeyValue = (UINTN)FfsFileEntry;\r
172\r
173 if (FfsFileHeader->Type == EFI_FV_FILETYPE_FFS_PAD) {\r
174 //\r
175 // we ignore pad files\r
176 //\r
177 continue;\r
178 }\r
179\r
ef816997 180 if (*FileType == EFI_FV_FILETYPE_ALL) {\r
28a00297 181 //\r
182 // Process all file types so we have a match\r
183 //\r
184 break;\r
185 }\r
186\r
187 if (*FileType == FfsFileHeader->Type) {\r
188 //\r
189 // Found a matching file type\r
190 //\r
191 break;\r
192 }\r
193\r
022c6d45 194 }\r
28a00297 195\r
196 //\r
197 // Return FileType, NameGuid, and Attributes\r
198 //\r
199 *FileType = FfsFileHeader->Type;\r
e94a9ff7 200 CopyGuid (NameGuid, &FfsFileHeader->Name);\r
28a00297 201 *Attributes = FfsAttributes2FvFileAttributes (FfsFileHeader->Attributes);\r
202\r
28a00297 203 //\r
204 // we need to substract the header size\r
205 //\r
6c85d162
SZ
206 if (IS_FFS_FILE2 (FfsFileHeader)) {\r
207 *Size = FFS_FILE2_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER2);\r
208 } else {\r
209 *Size = FFS_FILE_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER);\r
210 }\r
28a00297 211\r
28a00297 212 return EFI_SUCCESS;\r
213}\r
214\r
215\r
162ed594 216\r
217/**\r
218 Locates a file in the firmware volume and\r
219 copies it to the supplied buffer.\r
220\r
022c6d45 221 @param This Indicates the calling context.\r
222 @param NameGuid Pointer to an EFI_GUID, which is the\r
223 filename.\r
224 @param Buffer Buffer is a pointer to pointer to a buffer\r
225 in which the file or section contents or are\r
226 returned.\r
227 @param BufferSize BufferSize is a pointer to caller allocated\r
228 UINTN. On input *BufferSize indicates the\r
229 size in bytes of the memory region pointed\r
230 to by Buffer. On output, *BufferSize\r
231 contains the number of bytes required to\r
232 read the file.\r
233 @param FoundType FoundType is a pointer to a caller allocated\r
234 EFI_FV_FILETYPE that on successful return\r
235 from Read() contains the type of file read.\r
236 This output reflects the file type\r
237 irrespective of the value of the SectionType\r
238 input.\r
239 @param FileAttributes FileAttributes is a pointer to a caller\r
240 allocated EFI_FV_FILE_ATTRIBUTES. On\r
241 successful return from Read(),\r
242 *FileAttributes contains the attributes of\r
243 the file read.\r
244 @param AuthenticationStatus AuthenticationStatus is a pointer to a\r
245 caller allocated UINTN in which the\r
246 authentication status is returned.\r
247\r
248 @retval EFI_SUCCESS Successfully read to memory buffer.\r
249 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.\r
250 @retval EFI_NOT_FOUND Not found.\r
251 @retval EFI_DEVICE_ERROR Device error.\r
252 @retval EFI_ACCESS_DENIED Could not read.\r
253 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
162ed594 254 @retval EFI_OUT_OF_RESOURCES Not enough buffer to be allocated.\r
255\r
256**/\r
28a00297 257EFI_STATUS\r
258EFIAPI\r
259FvReadFile (\r
0c2b5da8 260 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,\r
261 IN CONST EFI_GUID *NameGuid,\r
262 IN OUT VOID **Buffer,\r
263 IN OUT UINTN *BufferSize,\r
264 OUT EFI_FV_FILETYPE *FoundType,\r
265 OUT EFI_FV_FILE_ATTRIBUTES *FileAttributes,\r
266 OUT UINT32 *AuthenticationStatus\r
28a00297 267 )\r
28a00297 268{\r
269 EFI_STATUS Status;\r
270 FV_DEVICE *FvDevice;\r
271 EFI_GUID SearchNameGuid;\r
272 EFI_FV_FILETYPE LocalFoundType;\r
273 EFI_FV_FILE_ATTRIBUTES LocalAttributes;\r
274 UINTN FileSize;\r
275 UINT8 *SrcPtr;\r
276 EFI_FFS_FILE_HEADER *FfsHeader;\r
277 UINTN InputBufferSize;\r
022c6d45 278\r
e94a9ff7 279 if (NameGuid == NULL) {\r
28a00297 280 return EFI_INVALID_PARAMETER;\r
281 }\r
282\r
283 FvDevice = FV_DEVICE_FROM_THIS (This);\r
022c6d45 284\r
28a00297 285\r
286 //\r
287 // Keep looking until we find the matching NameGuid.\r
288 // The Key is really an FfsFileEntry\r
289 //\r
290 FvDevice->LastKey = 0;\r
291 do {\r
292 LocalFoundType = 0;\r
293 Status = FvGetNextFile (\r
294 This,\r
295 &FvDevice->LastKey,\r
296 &LocalFoundType,\r
297 &SearchNameGuid,\r
298 &LocalAttributes,\r
299 &FileSize\r
300 );\r
301 if (EFI_ERROR (Status)) {\r
302 return EFI_NOT_FOUND;\r
303 }\r
304 } while (!CompareGuid (&SearchNameGuid, NameGuid));\r
305\r
306 //\r
307 // Get a pointer to the header\r
308 //\r
309 FfsHeader = FvDevice->LastKey->FfsHeader;\r
310\r
311 //\r
312 // Remember callers buffer size\r
313 //\r
314 InputBufferSize = *BufferSize;\r
315\r
316 //\r
317 // Calculate return values\r
318 //\r
319 *FoundType = FfsHeader->Type;\r
320 *FileAttributes = FfsAttributes2FvFileAttributes (FfsHeader->Attributes);\r
321 *AuthenticationStatus = 0;\r
322 *BufferSize = FileSize;\r
323\r
324 if (Buffer == NULL) {\r
325 //\r
d613c2a8 326 // If Buffer is NULL, we only want to get the information collected so far\r
28a00297 327 //\r
328 return EFI_SUCCESS;\r
329 }\r
330\r
331 //\r
332 // Skip over file header\r
333 //\r
6c85d162
SZ
334 if (IS_FFS_FILE2 (FfsHeader)) {\r
335 SrcPtr = ((UINT8 *) FfsHeader) + sizeof (EFI_FFS_FILE_HEADER2);\r
336 } else {\r
337 SrcPtr = ((UINT8 *) FfsHeader) + sizeof (EFI_FFS_FILE_HEADER);\r
338 }\r
28a00297 339\r
340 Status = EFI_SUCCESS;\r
341 if (*Buffer == NULL) {\r
342 //\r
343 // Caller passed in a pointer so allocate buffer for them\r
344 //\r
9c4ac31c 345 *Buffer = AllocatePool (FileSize);\r
28a00297 346 if (*Buffer == NULL) {\r
347 return EFI_OUT_OF_RESOURCES;\r
348 }\r
349 } else if (FileSize > InputBufferSize) {\r
350 //\r
351 // Callers buffer was not big enough\r
022c6d45 352 //\r
28a00297 353 Status = EFI_WARN_BUFFER_TOO_SMALL;\r
354 FileSize = InputBufferSize;\r
355 }\r
022c6d45 356\r
28a00297 357 //\r
022c6d45 358 // Copy data into callers buffer\r
28a00297 359 //\r
360 CopyMem (*Buffer, SrcPtr, FileSize);\r
361\r
362 return Status;\r
363}\r
364\r
365\r
162ed594 366\r
367/**\r
368 Locates a section in a given FFS File and\r
369 copies it to the supplied buffer (not including section header).\r
370\r
022c6d45 371 @param This Indicates the calling context.\r
372 @param NameGuid Pointer to an EFI_GUID, which is the\r
373 filename.\r
374 @param SectionType Indicates the section type to return.\r
375 @param SectionInstance Indicates which instance of sections with a\r
376 type of SectionType to return.\r
377 @param Buffer Buffer is a pointer to pointer to a buffer\r
378 in which the file or section contents or are\r
379 returned.\r
380 @param BufferSize BufferSize is a pointer to caller allocated\r
162ed594 381 UINTN.\r
022c6d45 382 @param AuthenticationStatus AuthenticationStatus is a pointer to a\r
383 caller allocated UINT32 in which the\r
384 authentication status is returned.\r
385\r
386 @retval EFI_SUCCESS Successfully read the file section into\r
387 buffer.\r
388 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.\r
389 @retval EFI_NOT_FOUND Section not found.\r
390 @retval EFI_DEVICE_ERROR Device error.\r
391 @retval EFI_ACCESS_DENIED Could not read.\r
162ed594 392 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
393\r
394**/\r
28a00297 395EFI_STATUS\r
396EFIAPI\r
397FvReadFileSection (\r
0c2b5da8 398 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,\r
399 IN CONST EFI_GUID *NameGuid,\r
400 IN EFI_SECTION_TYPE SectionType,\r
401 IN UINTN SectionInstance,\r
402 IN OUT VOID **Buffer,\r
403 IN OUT UINTN *BufferSize,\r
404 OUT UINT32 *AuthenticationStatus\r
28a00297 405 )\r
28a00297 406{\r
407 EFI_STATUS Status;\r
408 FV_DEVICE *FvDevice;\r
409 EFI_FV_FILETYPE FileType;\r
410 EFI_FV_FILE_ATTRIBUTES FileAttributes;\r
411 UINTN FileSize;\r
412 UINT8 *FileBuffer;\r
28a00297 413 FFS_FILE_LIST_ENTRY *FfsEntry;\r
022c6d45 414\r
e94a9ff7 415 if (NameGuid == NULL || Buffer == NULL) {\r
28a00297 416 return EFI_INVALID_PARAMETER;\r
417 }\r
418\r
419 FvDevice = FV_DEVICE_FROM_THIS (This);\r
420\r
421 //\r
422 // Read the whole file into buffer\r
423 //\r
424 FileBuffer = NULL;\r
425 Status = FvReadFile (\r
426 This,\r
427 NameGuid,\r
428 (VOID **)&FileBuffer,\r
429 &FileSize,\r
430 &FileType,\r
431 &FileAttributes,\r
432 AuthenticationStatus\r
022c6d45 433 );\r
28a00297 434 //\r
435 // Get the last key used by our call to FvReadFile as it is the FfsEntry for this file.\r
022c6d45 436 //\r
e94a9ff7 437 FfsEntry = (FFS_FILE_LIST_ENTRY *) FvDevice->LastKey;\r
28a00297 438\r
439 if (EFI_ERROR (Status)) {\r
440 return Status;\r
441 }\r
022c6d45 442\r
28a00297 443 //\r
444 // Check to see that the file actually HAS sections before we go any further.\r
445 //\r
446 if (FileType == EFI_FV_FILETYPE_RAW) {\r
447 Status = EFI_NOT_FOUND;\r
448 goto Done;\r
449 }\r
450\r
451 //\r
6c85d162 452 // Use FfsEntry to cache Section Extraction Protocol Information\r
28a00297 453 //\r
454 if (FfsEntry->StreamHandle == 0) {\r
797a9d67 455 Status = OpenSectionStream (\r
e94a9ff7 456 FileSize,\r
457 FileBuffer,\r
458 &FfsEntry->StreamHandle\r
459 );\r
28a00297 460 if (EFI_ERROR (Status)) {\r
461 goto Done;\r
462 }\r
28a00297 463 }\r
464\r
465 //\r
466 // If SectionType == 0 We need the whole section stream\r
467 //\r
797a9d67 468 Status = GetSection (\r
469 FfsEntry->StreamHandle,\r
470 (SectionType == 0) ? NULL : &SectionType,\r
471 NULL,\r
472 (SectionType == 0) ? 0 : SectionInstance,\r
473 Buffer,\r
474 BufferSize,\r
6c85d162
SZ
475 AuthenticationStatus,\r
476 FvDevice->IsFfs3Fv\r
797a9d67 477 );\r
28a00297 478\r
479 //\r
480 // Close of stream defered to close of FfsHeader list to allow SEP to cache data\r
481 //\r
482\r
483Done:\r
484 CoreFreePool (FileBuffer);\r
485\r
486 return Status;\r
487}\r
488\r
162ed594 489\r