]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Dxe/FwVol/FwVolRead.c
Code Scrub for Dxe Core.
[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
23c98c94 4Copyright (c) 2006 - 2008, 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
28a00297 12\r
504214c4 13**/\r
28a00297 14\r
15#include <DxeMain.h>\r
16\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
33--*/\r
34\r
022c6d45 35UINT8 mFvAttributes[] = {0, 4, 7, 9, 10, 12, 15, 16};\r
28a00297 36\r
37\r
162ed594 38\r
39/**\r
40 Convert the FFS File Attributes to FV File Attributes\r
41\r
022c6d45 42 @param FfsAttributes The attributes of UINT8 type.\r
162ed594 43\r
44 @return The attributes of EFI_FV_FILE_ATTRIBUTES\r
45\r
46**/\r
28a00297 47EFI_FV_FILE_ATTRIBUTES\r
48FfsAttributes2FvFileAttributes (\r
49 IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes\r
50 )\r
28a00297 51{\r
52 FfsAttributes = (EFI_FFS_FILE_ATTRIBUTES)((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT) >> 3);\r
53 ASSERT (FfsAttributes < 8);\r
54\r
55 return (EFI_FV_FILE_ATTRIBUTES) mFvAttributes[FfsAttributes];\r
56}\r
57\r
58\r
162ed594 59\r
60/**\r
61 Given the input key, search for the next matching file in the volume.\r
62\r
022c6d45 63 @param This Indicates the calling context.\r
64 @param Key Key is a pointer to a caller allocated\r
65 buffer that contains implementation specific\r
66 data that is used to track where to begin\r
67 the search for the next file. The size of\r
68 the buffer must be at least This->KeySize\r
69 bytes long. To reinitialize the search and\r
70 begin from the beginning of the firmware\r
71 volume, the entire buffer must be cleared to\r
72 zero. Other than clearing the buffer to\r
73 initiate a new search, the caller must not\r
74 modify the data in the buffer between calls\r
75 to GetNextFile().\r
76 @param FileType FileType is a pointer to a caller allocated\r
77 EFI_FV_FILETYPE. The GetNextFile() API can\r
78 filter it's search for files based on the\r
79 value of *FileType input. A *FileType input\r
80 of 0 causes GetNextFile() to search for\r
81 files of all types. If a file is found, the\r
82 file's type is returned in *FileType.\r
83 *FileType is not modified if no file is\r
84 found.\r
85 @param NameGuid NameGuid is a pointer to a caller allocated\r
86 EFI_GUID. If a file is found, the file's\r
87 name is returned in *NameGuid. *NameGuid is\r
88 not modified if no file is found.\r
89 @param Attributes Attributes is a pointer to a caller\r
90 allocated EFI_FV_FILE_ATTRIBUTES. If a file\r
91 is found, the file's attributes are returned\r
92 in *Attributes. *Attributes is not modified\r
93 if no file is found.\r
94 @param Size Size is a pointer to a caller allocated\r
95 UINTN. If a file is found, the file's size\r
96 is returned in *Size. *Size is not modified\r
97 if no file is found.\r
98\r
99 @retval EFI_SUCCESS Successfully find the file.\r
100 @retval EFI_DEVICE_ERROR Device error.\r
101 @retval EFI_ACCESS_DENIED Fv could not read.\r
102 @retval EFI_NOT_FOUND No matching file found.\r
162ed594 103 @retval EFI_INVALID_PARAMETER Invalid parameter\r
104\r
105**/\r
28a00297 106EFI_STATUS\r
107EFIAPI\r
108FvGetNextFile (\r
0c2b5da8 109 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,\r
110 IN OUT VOID *Key,\r
111 IN OUT EFI_FV_FILETYPE *FileType,\r
28a00297 112 OUT EFI_GUID *NameGuid,\r
113 OUT EFI_FV_FILE_ATTRIBUTES *Attributes,\r
162ed594 114 OUT UINTN *Size\r
28a00297 115 )\r
28a00297 116{\r
117 EFI_STATUS Status;\r
118 FV_DEVICE *FvDevice;\r
119 EFI_FV_ATTRIBUTES FvAttributes;\r
120 EFI_FFS_FILE_HEADER *FfsFileHeader;\r
121 UINTN *KeyValue;\r
122 LIST_ENTRY *Link;\r
123 FFS_FILE_LIST_ENTRY *FfsFileEntry;\r
124 UINTN FileLength;\r
125\r
126 FvDevice = FV_DEVICE_FROM_THIS (This);\r
127\r
128 Status = FvGetVolumeAttributes (This, &FvAttributes);\r
129 if (EFI_ERROR (Status)){\r
130 return Status;\r
131 }\r
132\r
133 //\r
134 // Check if read operation is enabled\r
135 //\r
0c2b5da8 136 if ((FvAttributes & EFI_FV2_READ_STATUS) == 0) {\r
28a00297 137 return EFI_ACCESS_DENIED;\r
138 }\r
139\r
140 if (*FileType > EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {\r
141 //\r
142 // File type needs to be in 0 - 0x0B\r
143 //\r
26fa3b49 144 return EFI_NOT_FOUND;\r
28a00297 145 }\r
146\r
147 KeyValue = (UINTN *)Key;\r
148 for (;;) {\r
149 if (*KeyValue == 0) {\r
150 //\r
151 // Search for 1st matching file\r
152 //\r
153 Link = &FvDevice->FfsFileListHeader;\r
154 } else {\r
155 //\r
156 // Key is pointer to FFsFileEntry, so get next one\r
157 //\r
158 Link = (LIST_ENTRY *)(*KeyValue);\r
159 }\r
160\r
161 if (Link->ForwardLink == &FvDevice->FfsFileListHeader) {\r
162 //\r
163 // Next is end of list so we did not find data\r
164 //\r
165 return EFI_NOT_FOUND;\r
166 }\r
167\r
168 FfsFileEntry = (FFS_FILE_LIST_ENTRY *)Link->ForwardLink;\r
169 FfsFileHeader = (EFI_FFS_FILE_HEADER *)FfsFileEntry->FfsHeader;\r
170\r
171 //\r
172 // remember the key\r
173 //\r
174 *KeyValue = (UINTN)FfsFileEntry;\r
175\r
176 if (FfsFileHeader->Type == EFI_FV_FILETYPE_FFS_PAD) {\r
177 //\r
178 // we ignore pad files\r
179 //\r
180 continue;\r
181 }\r
182\r
ef816997 183 if (*FileType == EFI_FV_FILETYPE_ALL) {\r
28a00297 184 //\r
185 // Process all file types so we have a match\r
186 //\r
187 break;\r
188 }\r
189\r
190 if (*FileType == FfsFileHeader->Type) {\r
191 //\r
192 // Found a matching file type\r
193 //\r
194 break;\r
195 }\r
196\r
022c6d45 197 }\r
28a00297 198\r
199 //\r
200 // Return FileType, NameGuid, and Attributes\r
201 //\r
202 *FileType = FfsFileHeader->Type;\r
e94a9ff7 203 CopyGuid (NameGuid, &FfsFileHeader->Name);\r
28a00297 204 *Attributes = FfsAttributes2FvFileAttributes (FfsFileHeader->Attributes);\r
205\r
206 //\r
207 // Read four bytes out of the 3 byte array and throw out extra data\r
208 //\r
209 FileLength = *(UINT32 *)&FfsFileHeader->Size[0] & 0x00FFFFFF;\r
210\r
211 //\r
212 // we need to substract the header size\r
213 //\r
214 *Size = FileLength - sizeof(EFI_FFS_FILE_HEADER);\r
215\r
28a00297 216 return EFI_SUCCESS;\r
217}\r
218\r
219\r
162ed594 220\r
221/**\r
222 Locates a file in the firmware volume and\r
223 copies it to the supplied buffer.\r
224\r
022c6d45 225 @param This Indicates the calling context.\r
226 @param NameGuid Pointer to an EFI_GUID, which is the\r
227 filename.\r
228 @param Buffer Buffer is a pointer to pointer to a buffer\r
229 in which the file or section contents or are\r
230 returned.\r
231 @param BufferSize BufferSize is a pointer to caller allocated\r
232 UINTN. On input *BufferSize indicates the\r
233 size in bytes of the memory region pointed\r
234 to by Buffer. On output, *BufferSize\r
235 contains the number of bytes required to\r
236 read the file.\r
237 @param FoundType FoundType is a pointer to a caller allocated\r
238 EFI_FV_FILETYPE that on successful return\r
239 from Read() contains the type of file read.\r
240 This output reflects the file type\r
241 irrespective of the value of the SectionType\r
242 input.\r
243 @param FileAttributes FileAttributes is a pointer to a caller\r
244 allocated EFI_FV_FILE_ATTRIBUTES. On\r
245 successful return from Read(),\r
246 *FileAttributes contains the attributes of\r
247 the file read.\r
248 @param AuthenticationStatus AuthenticationStatus is a pointer to a\r
249 caller allocated UINTN in which the\r
250 authentication status is returned.\r
251\r
252 @retval EFI_SUCCESS Successfully read to memory buffer.\r
253 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.\r
254 @retval EFI_NOT_FOUND Not found.\r
255 @retval EFI_DEVICE_ERROR Device error.\r
256 @retval EFI_ACCESS_DENIED Could not read.\r
257 @retval EFI_INVALID_PARAMETER Invalid parameter.\r
162ed594 258 @retval EFI_OUT_OF_RESOURCES Not enough buffer to be allocated.\r
259\r
260**/\r
28a00297 261EFI_STATUS\r
262EFIAPI\r
263FvReadFile (\r
0c2b5da8 264 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,\r
265 IN CONST EFI_GUID *NameGuid,\r
266 IN OUT VOID **Buffer,\r
267 IN OUT UINTN *BufferSize,\r
268 OUT EFI_FV_FILETYPE *FoundType,\r
269 OUT EFI_FV_FILE_ATTRIBUTES *FileAttributes,\r
270 OUT UINT32 *AuthenticationStatus\r
28a00297 271 )\r
28a00297 272{\r
273 EFI_STATUS Status;\r
274 FV_DEVICE *FvDevice;\r
275 EFI_GUID SearchNameGuid;\r
276 EFI_FV_FILETYPE LocalFoundType;\r
277 EFI_FV_FILE_ATTRIBUTES LocalAttributes;\r
278 UINTN FileSize;\r
279 UINT8 *SrcPtr;\r
280 EFI_FFS_FILE_HEADER *FfsHeader;\r
281 UINTN InputBufferSize;\r
022c6d45 282\r
e94a9ff7 283 if (NameGuid == NULL) {\r
28a00297 284 return EFI_INVALID_PARAMETER;\r
285 }\r
286\r
287 FvDevice = FV_DEVICE_FROM_THIS (This);\r
022c6d45 288\r
28a00297 289\r
290 //\r
291 // Keep looking until we find the matching NameGuid.\r
292 // The Key is really an FfsFileEntry\r
293 //\r
294 FvDevice->LastKey = 0;\r
295 do {\r
296 LocalFoundType = 0;\r
297 Status = FvGetNextFile (\r
298 This,\r
299 &FvDevice->LastKey,\r
300 &LocalFoundType,\r
301 &SearchNameGuid,\r
302 &LocalAttributes,\r
303 &FileSize\r
304 );\r
305 if (EFI_ERROR (Status)) {\r
306 return EFI_NOT_FOUND;\r
307 }\r
308 } while (!CompareGuid (&SearchNameGuid, NameGuid));\r
309\r
310 //\r
311 // Get a pointer to the header\r
312 //\r
313 FfsHeader = FvDevice->LastKey->FfsHeader;\r
314\r
315 //\r
316 // Remember callers buffer size\r
317 //\r
318 InputBufferSize = *BufferSize;\r
319\r
320 //\r
321 // Calculate return values\r
322 //\r
323 *FoundType = FfsHeader->Type;\r
324 *FileAttributes = FfsAttributes2FvFileAttributes (FfsHeader->Attributes);\r
325 *AuthenticationStatus = 0;\r
326 *BufferSize = FileSize;\r
327\r
328 if (Buffer == NULL) {\r
329 //\r
330 // If Buffer is NULL, we only want to get the information colected so far\r
331 //\r
332 return EFI_SUCCESS;\r
333 }\r
334\r
335 //\r
336 // Skip over file header\r
337 //\r
338 SrcPtr = ((UINT8 *)FfsHeader) + sizeof (EFI_FFS_FILE_HEADER);\r
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
345 *Buffer = CoreAllocateBootServicesPool (FileSize);\r
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
452 // Use FfsEntry to cache Section Extraction Protocol Inforomation\r
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
475 AuthenticationStatus\r
476 );\r
28a00297 477\r
478 //\r
479 // Close of stream defered to close of FfsHeader list to allow SEP to cache data\r
480 //\r
481\r
482Done:\r
483 CoreFreePool (FileBuffer);\r
484\r
485 return Status;\r
486}\r
487\r
162ed594 488\r