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