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