]>
Commit | Line | Data |
---|---|---|
c2df8e13 | 1 | /** @file\r |
2 | Implements functions to read firmware file.\r | |
3 | \r | |
67a6cf6d | 4 | Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r |
c2df8e13 | 5 | \r |
6 | This program and the accompanying materials\r | |
7 | are licensed and made available under the terms and conditions\r | |
8 | of the BSD License which accompanies this distribution. The\r | |
9 | full text of the license may be found at\r | |
10 | http://opensource.org/licenses/bsd-license.php\r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | #include "FwVolDriver.h"\r | |
18 | \r | |
5eddabfc | 19 | /**\r |
67a6cf6d SZ |
20 | Required Alignment Alignment Value in FFS FFS_ATTRIB_DATA_ALIGNMENT2 Alignment Value in\r |
21 | (bytes) Attributes Field in FFS Attributes Field Firmware Volume Interfaces\r | |
22 | 1 0 0 0\r | |
23 | 16 1 0 4\r | |
24 | 128 2 0 7\r | |
25 | 512 3 0 9\r | |
26 | 1 KB 4 0 10\r | |
27 | 4 KB 5 0 12\r | |
28 | 32 KB 6 0 15\r | |
29 | 64 KB 7 0 16\r | |
30 | 128 KB 0 1 17\r | |
31 | 256 KB 1 1 18\r | |
32 | 512 KB 2 1 19\r | |
33 | 1 MB 3 1 20\r | |
34 | 2 MB 4 1 21\r | |
35 | 4 MB 5 1 22\r | |
36 | 8 MB 6 1 23\r | |
37 | 16 MB 7 1 24\r | |
5eddabfc SZ |
38 | **/\r |
39 | UINT8 mFvAttributes[] = {0, 4, 7, 9, 10, 12, 15, 16};\r | |
67a6cf6d | 40 | UINT8 mFvAttributes2[] = {17, 18, 19, 20, 21, 22, 23, 24};\r |
c2df8e13 | 41 | \r |
42 | /**\r | |
43 | Convert the FFS File Attributes to FV File Attributes.\r | |
44 | \r | |
45 | @param FfsAttributes The attributes of UINT8 type.\r | |
46 | \r | |
47 | @return The attributes of EFI_FV_FILE_ATTRIBUTES\r | |
48 | \r | |
49 | **/\r | |
50 | EFI_FV_FILE_ATTRIBUTES\r | |
51 | FfsAttributes2FvFileAttributes (\r | |
52 | IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes\r | |
53 | )\r | |
54 | {\r | |
5eddabfc SZ |
55 | UINT8 DataAlignment;\r |
56 | EFI_FV_FILE_ATTRIBUTES FileAttribute;\r | |
57 | \r | |
58 | DataAlignment = (UINT8) ((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT) >> 3);\r | |
59 | ASSERT (DataAlignment < 8);\r | |
60 | \r | |
67a6cf6d SZ |
61 | if ((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT_2) != 0) {\r |
62 | FileAttribute = (EFI_FV_FILE_ATTRIBUTES) mFvAttributes2[DataAlignment];\r | |
63 | } else {\r | |
64 | FileAttribute = (EFI_FV_FILE_ATTRIBUTES) mFvAttributes[DataAlignment];\r | |
65 | }\r | |
5eddabfc SZ |
66 | \r |
67 | if ((FfsAttributes & FFS_ATTRIB_FIXED) == FFS_ATTRIB_FIXED) {\r | |
68 | FileAttribute |= EFI_FV_FILE_ATTRIB_FIXED;\r | |
69 | }\r | |
70 | \r | |
71 | return FileAttribute;\r | |
c2df8e13 | 72 | }\r |
73 | \r | |
74 | /**\r | |
75 | Given the input key, search for the next matching file in the volume.\r | |
76 | \r | |
77 | @param This Indicates the calling context.\r | |
78 | @param Key Key is a pointer to a caller allocated\r | |
79 | buffer that contains implementation specific\r | |
80 | data that is used to track where to begin\r | |
81 | the search for the next file. The size of\r | |
82 | the buffer must be at least This->KeySize\r | |
83 | bytes long. To reinitialize the search and\r | |
84 | begin from the beginning of the firmware\r | |
85 | volume, the entire buffer must be cleared to\r | |
86 | zero. Other than clearing the buffer to\r | |
87 | initiate a new search, the caller must not\r | |
88 | modify the data in the buffer between calls\r | |
89 | to GetNextFile().\r | |
90 | @param FileType FileType is a pointer to a caller allocated\r | |
91 | EFI_FV_FILETYPE. The GetNextFile() API can\r | |
92 | filter it's search for files based on the\r | |
93 | value of *FileType input. A *FileType input\r | |
94 | of 0 causes GetNextFile() to search for\r | |
95 | files of all types. If a file is found, the\r | |
96 | file's type is returned in *FileType.\r | |
97 | *FileType is not modified if no file is\r | |
98 | found.\r | |
99 | @param NameGuid NameGuid is a pointer to a caller allocated\r | |
100 | EFI_GUID. If a file is found, the file's\r | |
101 | name is returned in *NameGuid. *NameGuid is\r | |
102 | not modified if no file is found.\r | |
103 | @param Attributes Attributes is a pointer to a caller\r | |
104 | allocated EFI_FV_FILE_ATTRIBUTES. If a file\r | |
105 | is found, the file's attributes are returned\r | |
106 | in *Attributes. *Attributes is not modified\r | |
107 | if no file is found.\r | |
108 | @param Size Size is a pointer to a caller allocated\r | |
109 | UINTN. If a file is found, the file's size\r | |
110 | is returned in *Size. *Size is not modified\r | |
111 | if no file is found.\r | |
112 | \r | |
113 | @retval EFI_SUCCESS Successfully find the file.\r | |
114 | @retval EFI_DEVICE_ERROR Device error.\r | |
115 | @retval EFI_ACCESS_DENIED Fv could not read.\r | |
116 | @retval EFI_NOT_FOUND No matching file found.\r | |
117 | @retval EFI_INVALID_PARAMETER Invalid parameter\r | |
118 | \r | |
119 | **/\r | |
120 | EFI_STATUS\r | |
121 | EFIAPI\r | |
122 | FvGetNextFile (\r | |
123 | IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,\r | |
124 | IN OUT VOID *Key,\r | |
125 | IN OUT EFI_FV_FILETYPE *FileType,\r | |
126 | OUT EFI_GUID *NameGuid,\r | |
127 | OUT EFI_FV_FILE_ATTRIBUTES *Attributes,\r | |
128 | OUT UINTN *Size\r | |
129 | )\r | |
130 | {\r | |
131 | EFI_STATUS Status;\r | |
132 | FV_DEVICE *FvDevice;\r | |
133 | EFI_FV_ATTRIBUTES FvAttributes;\r | |
134 | EFI_FFS_FILE_HEADER *FfsFileHeader;\r | |
135 | UINTN *KeyValue;\r | |
136 | LIST_ENTRY *Link;\r | |
137 | FFS_FILE_LIST_ENTRY *FfsFileEntry;\r | |
c2df8e13 | 138 | \r |
139 | FvDevice = FV_DEVICE_FROM_THIS (This);\r | |
140 | \r | |
141 | Status = This->GetVolumeAttributes (This, &FvAttributes);\r | |
142 | if (EFI_ERROR (Status)) {\r | |
143 | return Status;\r | |
144 | }\r | |
145 | \r | |
146 | KeyValue = (UINTN *) Key;\r | |
147 | FfsFileHeader = NULL;\r | |
148 | \r | |
149 | //\r | |
150 | // Check if read operation is enabled\r | |
151 | //\r | |
152 | if ((FvAttributes & EFI_FV2_READ_STATUS) == 0) {\r | |
153 | return EFI_ACCESS_DENIED;\r | |
154 | }\r | |
155 | \r | |
156 | if (*FileType > EFI_FV_FILETYPE_SMM_CORE) {\r | |
157 | //\r | |
158 | // File type needs to be in 0 - 0x0D\r | |
159 | //\r | |
160 | return EFI_NOT_FOUND;\r | |
161 | }\r | |
162 | \r | |
163 | do {\r | |
164 | if (*KeyValue == 0) {\r | |
165 | //\r | |
166 | // Search for 1st matching file\r | |
167 | //\r | |
168 | Link = &FvDevice->FfsFileListHeader;\r | |
169 | if (Link->ForwardLink == &FvDevice->FfsFileListHeader) {\r | |
170 | return EFI_NOT_FOUND;\r | |
171 | }\r | |
172 | \r | |
173 | FfsFileEntry = (FFS_FILE_LIST_ENTRY *) Link->ForwardLink;\r | |
174 | FfsFileHeader = (EFI_FFS_FILE_HEADER *) FfsFileEntry->FfsHeader;\r | |
175 | \r | |
176 | //\r | |
177 | // remember the key\r | |
178 | //\r | |
179 | *KeyValue = (UINTN) FfsFileEntry;\r | |
180 | \r | |
181 | //\r | |
182 | // we ignore pad files\r | |
183 | //\r | |
184 | if (FfsFileHeader->Type == EFI_FV_FILETYPE_FFS_PAD) {\r | |
185 | continue;\r | |
186 | }\r | |
187 | \r | |
188 | if (*FileType == 0) {\r | |
189 | break;\r | |
190 | }\r | |
191 | \r | |
192 | if (*FileType == FfsFileHeader->Type) {\r | |
193 | break;\r | |
194 | }\r | |
195 | \r | |
196 | } else {\r | |
197 | //\r | |
198 | // Getting link from last Ffs\r | |
199 | //\r | |
200 | Link = (LIST_ENTRY *) (*KeyValue);\r | |
201 | if (Link->ForwardLink == &FvDevice->FfsFileListHeader) {\r | |
202 | return EFI_NOT_FOUND;\r | |
203 | }\r | |
204 | \r | |
205 | FfsFileEntry = (FFS_FILE_LIST_ENTRY *) Link->ForwardLink;\r | |
206 | FfsFileHeader = (EFI_FFS_FILE_HEADER *) FfsFileEntry->FfsHeader;\r | |
207 | \r | |
208 | //\r | |
209 | // remember the key\r | |
210 | //\r | |
211 | *KeyValue = (UINTN) FfsFileEntry;\r | |
212 | \r | |
213 | //\r | |
214 | // we ignore pad files\r | |
215 | //\r | |
216 | if (FfsFileHeader->Type == EFI_FV_FILETYPE_FFS_PAD) {\r | |
217 | continue;\r | |
218 | }\r | |
219 | \r | |
220 | if (*FileType == EFI_FV_FILETYPE_ALL) {\r | |
221 | break;\r | |
222 | }\r | |
223 | \r | |
224 | if (*FileType == FfsFileHeader->Type) {\r | |
225 | break;\r | |
226 | }\r | |
227 | }\r | |
228 | } while (Link->ForwardLink != &FvDevice->FfsFileListHeader);\r | |
229 | \r | |
230 | //\r | |
231 | // Cache this file entry\r | |
232 | //\r | |
233 | FvDevice->CurrentFfsFile = FfsFileEntry;\r | |
234 | \r | |
235 | *FileType = FfsFileHeader->Type;\r | |
236 | CopyGuid (NameGuid, &FfsFileHeader->Name);\r | |
237 | *Attributes = FfsAttributes2FvFileAttributes (FfsFileHeader->Attributes);\r | |
5eddabfc SZ |
238 | if ((FvDevice->FwVolHeader->Attributes & EFI_FVB2_MEMORY_MAPPED) == EFI_FVB2_MEMORY_MAPPED) {\r |
239 | *Attributes |= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED;\r | |
240 | }\r | |
c2df8e13 | 241 | \r |
c2df8e13 | 242 | //\r |
243 | // we need to substract the header size\r | |
244 | //\r | |
23491d5c SZ |
245 | if (IS_FFS_FILE2 (FfsFileHeader)) {\r |
246 | *Size = FFS_FILE2_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER2);\r | |
247 | } else {\r | |
248 | *Size = FFS_FILE_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER);\r | |
249 | }\r | |
c2df8e13 | 250 | \r |
251 | if (CompareGuid (&gEfiFirmwareVolumeTopFileGuid, NameGuid)) {\r | |
252 | //\r | |
253 | // specially deal with VTF file\r | |
254 | //\r | |
255 | UINT8 *SrcPtr;\r | |
256 | UINT32 Tmp;\r | |
257 | \r | |
23491d5c SZ |
258 | if (IS_FFS_FILE2 (FfsFileHeader)) {\r |
259 | SrcPtr = ((UINT8 *) FfsFileHeader) + sizeof (EFI_FFS_FILE_HEADER2);\r | |
260 | } else {\r | |
261 | SrcPtr = ((UINT8 *) FfsFileHeader) + sizeof (EFI_FFS_FILE_HEADER);\r | |
262 | }\r | |
c2df8e13 | 263 | \r |
264 | while (*Size >= 4) {\r | |
265 | Tmp = *(UINT32 *) SrcPtr;\r | |
266 | if (Tmp == 0) {\r | |
267 | SrcPtr += 4;\r | |
268 | (*Size) -= 4;\r | |
269 | } else {\r | |
270 | break;\r | |
271 | }\r | |
272 | }\r | |
273 | }\r | |
274 | \r | |
275 | return EFI_SUCCESS;\r | |
276 | }\r | |
277 | \r | |
278 | /**\r | |
279 | Locates a file in the firmware volume and\r | |
280 | copies it to the supplied buffer.\r | |
281 | \r | |
282 | @param This Indicates the calling context.\r | |
283 | @param NameGuid Pointer to an EFI_GUID, which is the\r | |
284 | filename.\r | |
285 | @param Buffer Buffer is a pointer to pointer to a buffer\r | |
286 | in which the file or section contents or are\r | |
287 | returned.\r | |
288 | @param BufferSize BufferSize is a pointer to caller allocated\r | |
289 | UINTN. On input *BufferSize indicates the\r | |
290 | size in bytes of the memory region pointed\r | |
291 | to by Buffer. On output, *BufferSize\r | |
292 | contains the number of bytes required to\r | |
293 | read the file.\r | |
294 | @param FoundType FoundType is a pointer to a caller allocated\r | |
295 | EFI_FV_FILETYPE that on successful return\r | |
296 | from Read() contains the type of file read.\r | |
297 | This output reflects the file type\r | |
298 | irrespective of the value of the SectionType\r | |
299 | input.\r | |
300 | @param FileAttributes FileAttributes is a pointer to a caller\r | |
301 | allocated EFI_FV_FILE_ATTRIBUTES. On\r | |
302 | successful return from Read(),\r | |
303 | *FileAttributes contains the attributes of\r | |
304 | the file read.\r | |
305 | @param AuthenticationStatus AuthenticationStatus is a pointer to a\r | |
306 | caller allocated UINTN in which the\r | |
307 | authentication status is returned.\r | |
308 | \r | |
309 | @retval EFI_SUCCESS Successfully read to memory buffer.\r | |
310 | @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.\r | |
311 | @retval EFI_NOT_FOUND Not found.\r | |
312 | @retval EFI_DEVICE_ERROR Device error.\r | |
313 | @retval EFI_ACCESS_DENIED Could not read.\r | |
314 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
315 | @retval EFI_OUT_OF_RESOURCES Not enough buffer to be allocated.\r | |
316 | \r | |
317 | **/\r | |
318 | EFI_STATUS\r | |
319 | EFIAPI\r | |
320 | FvReadFile (\r | |
321 | IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,\r | |
322 | IN CONST EFI_GUID *NameGuid,\r | |
323 | IN OUT VOID **Buffer,\r | |
324 | IN OUT UINTN *BufferSize,\r | |
325 | OUT EFI_FV_FILETYPE *FoundType,\r | |
326 | OUT EFI_FV_FILE_ATTRIBUTES *FileAttributes,\r | |
327 | OUT UINT32 *AuthenticationStatus\r | |
328 | )\r | |
329 | {\r | |
330 | EFI_STATUS Status;\r | |
331 | FV_DEVICE *FvDevice;\r | |
332 | UINTN Key;\r | |
333 | EFI_GUID SearchNameGuid;\r | |
334 | EFI_FV_ATTRIBUTES FvAttributes;\r | |
335 | EFI_FV_FILETYPE LocalFoundType;\r | |
336 | EFI_FV_FILE_ATTRIBUTES LocalAttributes;\r | |
337 | UINTN FileSize;\r | |
338 | UINT8 *SrcPtr;\r | |
339 | FFS_FILE_LIST_ENTRY *FfsFileEntry;\r | |
340 | EFI_FFS_FILE_HEADER *FfsHeader;\r | |
341 | UINT8 *FileBuffer;\r | |
342 | \r | |
343 | if (NULL == This || NULL == NameGuid) {\r | |
344 | return EFI_INVALID_PARAMETER;\r | |
345 | }\r | |
346 | \r | |
347 | FvDevice = FV_DEVICE_FROM_THIS (This);\r | |
348 | \r | |
349 | Status = This->GetVolumeAttributes (This, &FvAttributes);\r | |
350 | if (EFI_ERROR (Status)) {\r | |
351 | return Status;\r | |
352 | }\r | |
353 | //\r | |
354 | // First check to see that FV is enabled for reads...\r | |
355 | //\r | |
356 | if (0 == (FvAttributes & EFI_FV2_READ_STATUS)) {\r | |
357 | return EFI_ACCESS_DENIED;\r | |
358 | }\r | |
359 | \r | |
360 | FfsHeader = NULL;\r | |
361 | \r | |
362 | //\r | |
363 | // Check if the file was read last time.\r | |
364 | //\r | |
365 | FfsFileEntry = FvDevice->CurrentFfsFile;\r | |
366 | \r | |
367 | if (FfsFileEntry != NULL) {\r | |
368 | FfsHeader = (EFI_FFS_FILE_HEADER *) FfsFileEntry->FfsHeader;\r | |
369 | }\r | |
370 | \r | |
371 | if ((FfsFileEntry == NULL) || (!CompareGuid (&FfsHeader->Name, NameGuid))) {\r | |
372 | //\r | |
373 | // If not match or no file cached, search this file\r | |
374 | //\r | |
375 | Key = 0;\r | |
376 | do {\r | |
377 | LocalFoundType = 0;\r | |
378 | Status = This->GetNextFile (\r | |
379 | This,\r | |
380 | &Key,\r | |
381 | &LocalFoundType,\r | |
382 | &SearchNameGuid,\r | |
383 | &LocalAttributes,\r | |
384 | &FileSize\r | |
385 | );\r | |
386 | if (EFI_ERROR (Status)) {\r | |
387 | return EFI_NOT_FOUND;\r | |
388 | }\r | |
389 | } while (!CompareGuid (&SearchNameGuid, NameGuid));\r | |
390 | \r | |
391 | //\r | |
392 | // Get file entry\r | |
393 | //\r | |
394 | FfsFileEntry = (FFS_FILE_LIST_ENTRY *) Key;\r | |
395 | \r | |
396 | //\r | |
397 | // Update the cache\r | |
398 | //\r | |
399 | FvDevice->CurrentFfsFile = FfsFileEntry;\r | |
400 | \r | |
401 | FfsHeader = (EFI_FFS_FILE_HEADER *) FfsFileEntry->FfsHeader;\r | |
402 | \r | |
403 | } else {\r | |
404 | //\r | |
405 | // Get File Size of the cached file\r | |
406 | //\r | |
23491d5c SZ |
407 | if (IS_FFS_FILE2 (FfsHeader)) {\r |
408 | FileSize = FFS_FILE2_SIZE (FfsHeader) - sizeof (EFI_FFS_FILE_HEADER2);\r | |
409 | } else {\r | |
410 | FileSize = FFS_FILE_SIZE (FfsHeader) - sizeof (EFI_FFS_FILE_HEADER);\r | |
411 | }\r | |
c2df8e13 | 412 | }\r |
413 | //\r | |
414 | // Get file info\r | |
415 | //\r | |
416 | *FoundType = FfsHeader->Type;\r | |
417 | *FileAttributes = FfsAttributes2FvFileAttributes (FfsHeader->Attributes);\r | |
5eddabfc SZ |
418 | if ((FvDevice->FwVolHeader->Attributes & EFI_FVB2_MEMORY_MAPPED) == EFI_FVB2_MEMORY_MAPPED) {\r |
419 | *FileAttributes |= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED;\r | |
420 | }\r | |
37136e06 SZ |
421 | //\r |
422 | // Inherit the authentication status.\r | |
423 | //\r | |
424 | *AuthenticationStatus = FvDevice->AuthenticationStatus;\r | |
c2df8e13 | 425 | \r |
426 | //\r | |
427 | // If Buffer is NULL, we only want to get some information\r | |
428 | //\r | |
429 | if (Buffer == NULL) {\r | |
430 | *BufferSize = FileSize;\r | |
431 | return EFI_SUCCESS;\r | |
432 | }\r | |
433 | \r | |
23491d5c SZ |
434 | if (IS_FFS_FILE2 (FfsHeader)) {\r |
435 | SrcPtr = ((UINT8 *) FfsHeader) + sizeof (EFI_FFS_FILE_HEADER2);\r | |
436 | } else {\r | |
437 | SrcPtr = ((UINT8 *) FfsHeader) + sizeof (EFI_FFS_FILE_HEADER);\r | |
438 | }\r | |
c2df8e13 | 439 | \r |
440 | if (CompareGuid (&gEfiFirmwareVolumeTopFileGuid, NameGuid)) {\r | |
441 | //\r | |
442 | // specially deal with VTF file\r | |
443 | //\r | |
444 | UINT32 Tmp;\r | |
445 | \r | |
446 | while (FileSize >= 4) {\r | |
447 | Tmp = *(UINT32 *) SrcPtr;\r | |
448 | if (Tmp == 0) {\r | |
449 | SrcPtr += 4;\r | |
450 | FileSize -= 4;\r | |
451 | } else {\r | |
452 | break;\r | |
453 | }\r | |
454 | }\r | |
455 | }\r | |
456 | //\r | |
457 | // If we drop out of the above loop, we've found the correct file header...\r | |
458 | //\r | |
459 | if (*Buffer == NULL) {\r | |
460 | FileBuffer = AllocateCopyPool (FileSize, SrcPtr);\r | |
461 | if (FileBuffer == NULL) {\r | |
462 | return EFI_OUT_OF_RESOURCES;\r | |
463 | }\r | |
464 | \r | |
465 | *BufferSize = FileSize;\r | |
466 | *Buffer = FileBuffer;\r | |
467 | \r | |
468 | return EFI_SUCCESS;\r | |
469 | }\r | |
470 | //\r | |
471 | // If the user's buffer is smaller than the file size, then copy as much\r | |
472 | // as we can and return an appropriate status.\r | |
473 | //\r | |
474 | if (FileSize > *BufferSize) {\r | |
475 | CopyMem (*Buffer, SrcPtr, *BufferSize);\r | |
476 | *BufferSize = FileSize;\r | |
477 | return EFI_WARN_BUFFER_TOO_SMALL;\r | |
478 | }\r | |
479 | //\r | |
480 | // User's buffer size is ok, so copy the entire file to their buffer.\r | |
481 | //\r | |
482 | *BufferSize = FileSize;\r | |
483 | CopyMem (*Buffer, SrcPtr, *BufferSize);\r | |
484 | \r | |
485 | return EFI_SUCCESS;\r | |
486 | }\r | |
487 | \r | |
488 | /**\r | |
489 | Locates a section in a given FFS File and\r | |
490 | copies it to the supplied buffer (not including section header).\r | |
491 | \r | |
492 | @param This Indicates the calling context.\r | |
493 | @param NameGuid Pointer to an EFI_GUID, which is the\r | |
494 | filename.\r | |
495 | @param SectionType Indicates the section type to return.\r | |
496 | @param SectionInstance Indicates which instance of sections with a\r | |
497 | type of SectionType to return.\r | |
498 | @param Buffer Buffer is a pointer to pointer to a buffer\r | |
499 | in which the file or section contents or are\r | |
500 | returned.\r | |
501 | @param BufferSize BufferSize is a pointer to caller allocated\r | |
502 | UINTN.\r | |
503 | @param AuthenticationStatus AuthenticationStatus is a pointer to a\r | |
504 | caller allocated UINT32 in which the\r | |
505 | authentication status is returned.\r | |
506 | \r | |
507 | @retval EFI_SUCCESS Successfully read the file section into\r | |
508 | buffer.\r | |
509 | @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.\r | |
510 | @retval EFI_NOT_FOUND Section not found.\r | |
511 | @retval EFI_DEVICE_ERROR Device error.\r | |
512 | @retval EFI_ACCESS_DENIED Could not read.\r | |
513 | @retval EFI_INVALID_PARAMETER Invalid parameter.\r | |
514 | \r | |
515 | **/\r | |
516 | EFI_STATUS\r | |
517 | EFIAPI\r | |
518 | FvReadFileSection (\r | |
519 | IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL *This,\r | |
520 | IN CONST EFI_GUID *NameGuid,\r | |
521 | IN EFI_SECTION_TYPE SectionType,\r | |
522 | IN UINTN SectionInstance,\r | |
523 | IN OUT VOID **Buffer,\r | |
524 | IN OUT UINTN *BufferSize,\r | |
525 | OUT UINT32 *AuthenticationStatus\r | |
526 | )\r | |
527 | {\r | |
528 | EFI_STATUS Status;\r | |
0c3a1db4 | 529 | FV_DEVICE *FvDevice;\r |
c2df8e13 | 530 | EFI_FV_ATTRIBUTES FvAttributes;\r |
531 | EFI_FV_FILETYPE FileType;\r | |
532 | EFI_FV_FILE_ATTRIBUTES FileAttributes;\r | |
533 | UINTN FileSize;\r | |
534 | UINT8 *FileBuffer;\r | |
535 | EFI_SECTION_EXTRACTION_PROTOCOL *Sep;\r | |
536 | UINTN StreamHandle;\r | |
537 | \r | |
538 | if (NULL == This || NULL == NameGuid || Buffer == NULL) {\r | |
539 | return EFI_INVALID_PARAMETER;\r | |
540 | }\r | |
541 | \r | |
0c3a1db4 SZ |
542 | FvDevice = FV_DEVICE_FROM_THIS (This);\r |
543 | \r | |
c2df8e13 | 544 | Status = This->GetVolumeAttributes (This, &FvAttributes);\r |
545 | if (EFI_ERROR (Status)) {\r | |
546 | return Status;\r | |
547 | }\r | |
548 | //\r | |
549 | // First check to see that FV is enabled for reads...\r | |
550 | //\r | |
551 | if (0 == (FvAttributes & EFI_FV2_READ_STATUS)) {\r | |
552 | return EFI_ACCESS_DENIED;\r | |
553 | }\r | |
554 | //\r | |
555 | // Read the whole file into buffer\r | |
556 | //\r | |
557 | FileBuffer = NULL;\r | |
558 | Status = This->ReadFile (\r | |
559 | This,\r | |
560 | NameGuid,\r | |
561 | (VOID **) &FileBuffer,\r | |
562 | &FileSize,\r | |
563 | &FileType,\r | |
564 | &FileAttributes,\r | |
565 | AuthenticationStatus\r | |
566 | );\r | |
567 | \r | |
568 | if (EFI_ERROR (Status)) {\r | |
569 | return Status;\r | |
570 | }\r | |
571 | //\r | |
572 | // Check to see that the file actually HAS sections before we go any further.\r | |
573 | //\r | |
574 | if (FileType == EFI_FV_FILETYPE_RAW) {\r | |
575 | FreePool (FileBuffer);\r | |
576 | return EFI_NOT_FOUND;\r | |
577 | }\r | |
578 | //\r | |
579 | // Located the protocol\r | |
580 | //\r | |
581 | Status = gBS->LocateProtocol (\r | |
582 | &gEfiSectionExtractionProtocolGuid,\r | |
583 | NULL,\r | |
584 | (VOID **) &Sep\r | |
585 | );\r | |
586 | if (EFI_ERROR (Status)) {\r | |
587 | FreePool (FileBuffer);\r | |
588 | return Status;\r | |
589 | }\r | |
590 | \r | |
591 | Status = Sep->OpenSectionStream (\r | |
592 | Sep,\r | |
593 | FileSize,\r | |
594 | FileBuffer,\r | |
595 | &StreamHandle\r | |
596 | );\r | |
597 | \r | |
598 | if (EFI_ERROR (Status)) {\r | |
599 | FreePool (FileBuffer);\r | |
600 | return Status;\r | |
601 | }\r | |
602 | \r | |
603 | if (SectionType == 0) {\r | |
604 | //\r | |
605 | // We need the whole section stream\r | |
606 | //\r | |
607 | Status = Sep->GetSection (\r | |
608 | Sep,\r | |
609 | StreamHandle,\r | |
610 | NULL,\r | |
611 | NULL,\r | |
612 | 0,\r | |
613 | Buffer,\r | |
614 | BufferSize,\r | |
615 | AuthenticationStatus\r | |
616 | );\r | |
617 | } else {\r | |
618 | Status = Sep->GetSection (\r | |
619 | Sep,\r | |
620 | StreamHandle,\r | |
621 | &SectionType,\r | |
622 | NULL,\r | |
623 | SectionInstance,\r | |
624 | Buffer,\r | |
625 | BufferSize,\r | |
626 | AuthenticationStatus\r | |
627 | );\r | |
628 | }\r | |
0c3a1db4 SZ |
629 | \r |
630 | if (!EFI_ERROR (Status)) {\r | |
631 | //\r | |
632 | // Inherit the authentication status.\r | |
633 | //\r | |
634 | *AuthenticationStatus |= FvDevice->AuthenticationStatus;\r | |
635 | }\r | |
636 | \r | |
c2df8e13 | 637 | //\r |
638 | // Handle AuthenticationStatus if necessary\r | |
639 | //\r | |
640 | Sep->CloseSectionStream (Sep, StreamHandle);\r | |
641 | \r | |
642 | FreePool (FileBuffer);\r | |
643 | \r | |
644 | return Status;\r | |
645 | }\r |