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