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