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