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