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