]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c
FatBinPkg: Update EBC/IA32/X64/IPF binaries
[mirror_edk2.git] / MdeModulePkg / Universal / FvSimpleFileSystemDxe / FvSimpleFileSystem.c
CommitLineData
9da91aea
BJ
1/** @file\r
2 This driver uses the EFI_FIRMWARE_VOLUME2_PROTOCOL to expose files in firmware\r
3 volumes via the the EFI_SIMPLE_FILESYSTEM_PROTOCOL and EFI_FILE_PROTOCOL.\r
4\r
5 It will expose a single directory, containing one file for each file in the firmware\r
6 volume. If a file has a UI section, its contents will be used as a filename.\r
7 Otherwise, a string representation of the GUID will be used.\r
8 Files of an executable type (That is PEIM, DRIVER, COMBINED_PEIM_DRIVER and APPLICATION)\r
9 will have ".efi" added to their filename.\r
10\r
11 Its primary intended use is to be able to start EFI applications embedded in FVs\r
12 from the UEFI shell. It is entirely read-only.\r
13\r
14Copyright (c) 2014, ARM Limited. All rights reserved.\r
f5fc2742 15Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>\r
9da91aea
BJ
16\r
17This program and the accompanying materials\r
18are licensed and made available under the terms and conditions of the BSD License\r
19which accompanies this distribution. The full text of the license may be found at\r
20http://opensource.org/licenses/bsd-license.php\r
21\r
22THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
23WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
24\r
25**/\r
26\r
27#include "FvSimpleFileSystemInternal.h"\r
28\r
29//\r
30// Template for EFI_FILE_SYSTEM_INFO data structure.\r
31//\r
32EFI_FILE_SYSTEM_INFO mFsInfoTemplate = {\r
33 0, // Populate at runtime\r
34 TRUE, // Read-only\r
35 0, // Don't know volume size\r
36 0, // No free space\r
37 0, // Don't know block size\r
38 L"" // Populate at runtime\r
39};\r
40\r
41//\r
42// Template for EFI_FILE_PROTOCOL data structure.\r
43//\r
44EFI_FILE_PROTOCOL mFileSystemTemplate = {\r
45 EFI_FILE_PROTOCOL_REVISION,\r
46 FvSimpleFileSystemOpen,\r
47 FvSimpleFileSystemClose,\r
48 FvSimpleFileSystemDelete,\r
49 FvSimpleFileSystemRead,\r
50 FvSimpleFileSystemWrite,\r
51 FvSimpleFileSystemGetPosition,\r
52 FvSimpleFileSystemSetPosition,\r
53 FvSimpleFileSystemGetInfo,\r
54 FvSimpleFileSystemSetInfo,\r
55 FvSimpleFileSystemFlush\r
56};\r
57\r
58/**\r
59 Find and call ReadSection on the first section found of an executable type.\r
60\r
61 @param FvProtocol A pointer to the EFI_FIRMWARE_VOLUME2_PROTOCOL instance.\r
62 @param FvFileInfo A pointer to the FV_FILESYSTEM_FILE_INFO instance that is a struct\r
63 representing a file's info.\r
64 @param BufferSize Pointer to a caller-allocated UINTN. It indicates the size of\r
65 the memory represented by *Buffer.\r
66 @param Buffer Pointer to a pointer to a data buffer to contain file content.\r
67\r
68 @retval EFI_SUCCESS The call completed successfully.\r
69 @retval EFI_WARN_BUFFER_TOO_SMALL The buffer is too small to contain the requested output.\r
70 @retval EFI_ACCESS_DENIED The firmware volume is configured to disallow reads.\r
71 @retval EFI_NOT_FOUND The requested file was not found in the firmware volume.\r
72 @retval EFI_DEVICE_ERROR A hardware error occurred when attempting toaccess the firmware volume.\r
73\r
74**/\r
75EFI_STATUS\r
76FvFsFindExecutableSection (\r
77 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *FvProtocol,\r
78 IN FV_FILESYSTEM_FILE_INFO *FvFileInfo,\r
79 IN OUT UINTN *BufferSize,\r
80 IN OUT VOID **Buffer\r
81 )\r
82{\r
83 EFI_SECTION_TYPE SectionType;\r
84 UINT32 AuthenticationStatus;\r
85 EFI_STATUS Status;\r
86\r
87 for (SectionType = EFI_SECTION_PE32; SectionType <= EFI_SECTION_TE; SectionType++) {\r
88 Status = FvProtocol->ReadSection (\r
89 FvProtocol,\r
90 &FvFileInfo->NameGuid,\r
91 SectionType,\r
92 0,\r
93 Buffer,\r
94 BufferSize,\r
95 &AuthenticationStatus\r
96 );\r
97 if (Status != EFI_NOT_FOUND) {\r
98 return Status;\r
99 }\r
100 }\r
101\r
102 return EFI_NOT_FOUND;\r
103}\r
104\r
105/**\r
106 Get the size of the buffer that will be returned by FvFsReadFile.\r
107\r
108 @param FvProtocol A pointer to the EFI_FIRMWARE_VOLUME2_PROTOCOL instance.\r
109 @param FvFileInfo A pointer to the FV_FILESYSTEM_FILE_INFO instance that is a struct\r
110 representing a file's info.\r
111\r
112 @retval EFI_SUCCESS The file size was gotten correctly.\r
113 @retval Others The file size wasn't gotten correctly.\r
114\r
115**/\r
116EFI_STATUS\r
117FvFsGetFileSize (\r
118 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *FvProtocol,\r
119 IN OUT FV_FILESYSTEM_FILE_INFO *FvFileInfo\r
120 )\r
121{\r
122 UINT32 AuthenticationStatus;\r
123 EFI_FV_FILETYPE FoundType;\r
124 EFI_FV_FILE_ATTRIBUTES Attributes;\r
125 EFI_STATUS Status;\r
126 UINT8 IgnoredByte;\r
127 VOID *IgnoredPtr;\r
128\r
129 //\r
130 // To get the size of a section, we pass 0 for BufferSize. But we can't pass\r
131 // NULL for Buffer, as that will cause a return of INVALID_PARAMETER, and we\r
132 // can't pass NULL for *Buffer, as that will cause the callee to allocate\r
133 // a buffer of the sections size.\r
134 //\r
135 IgnoredPtr = &IgnoredByte;\r
136 FvFileInfo->FileInfo.FileSize = 0;\r
137\r
138 if (FV_FILETYPE_IS_EXECUTABLE (FvFileInfo->Type)) {\r
139 //\r
140 // Get the size of the first executable section out of the file.\r
141 //\r
270fc03f 142 Status = FvFsFindExecutableSection (FvProtocol, FvFileInfo, (UINTN*)&FvFileInfo->FileInfo.FileSize, &IgnoredPtr);\r
9da91aea
BJ
143 if (Status == EFI_WARN_BUFFER_TOO_SMALL) {\r
144 return EFI_SUCCESS;\r
145 }\r
146 } else if (FvFileInfo->Type == EFI_FV_FILETYPE_FREEFORM) {\r
147 //\r
148 // Try to get the size of a raw section out of the file\r
149 //\r
150 Status = FvProtocol->ReadSection (\r
151 FvProtocol,\r
152 &FvFileInfo->NameGuid,\r
153 EFI_SECTION_RAW,\r
154 0,\r
155 &IgnoredPtr,\r
270fc03f 156 (UINTN*)&FvFileInfo->FileInfo.FileSize,\r
9da91aea
BJ
157 &AuthenticationStatus\r
158 );\r
159 if (Status == EFI_WARN_BUFFER_TOO_SMALL) {\r
160 return EFI_SUCCESS;\r
161 }\r
162 if (EFI_ERROR (Status)) {\r
163 //\r
164 // Didn't find a raw section, just return the whole file's size.\r
165 //\r
166 return FvProtocol->ReadFile (\r
167 FvProtocol,\r
168 &FvFileInfo->NameGuid,\r
169 NULL,\r
270fc03f 170 (UINTN*)&FvFileInfo->FileInfo.FileSize,\r
9da91aea
BJ
171 &FoundType,\r
172 &Attributes,\r
173 &AuthenticationStatus\r
174 );\r
175 }\r
176 } else {\r
177 //\r
178 // Get the size of the entire file\r
179 //\r
180 return FvProtocol->ReadFile (\r
181 FvProtocol,\r
182 &FvFileInfo->NameGuid,\r
183 NULL,\r
270fc03f 184 (UINTN*)&FvFileInfo->FileInfo.FileSize,\r
9da91aea
BJ
185 &FoundType,\r
186 &Attributes,\r
187 &AuthenticationStatus\r
188 );\r
189 }\r
190\r
191 return Status;\r
192}\r
193\r
194/**\r
195 Helper function to read a file.\r
196\r
197 The data returned depends on the type of the underlying FV file:\r
198 - For executable types, the first section found that contains executable code is returned.\r
199 - For files of type FREEFORM, the driver attempts to return the first section of type RAW.\r
200 If none is found, the entire contents of the FV file are returned.\r
201 - On all other files the entire contents of the FV file is returned, as by\r
202 EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadFile.\r
203\r
204 @param FvProtocol A pointer to the EFI_FIRMWARE_VOLUME2_PROTOCOL instance.\r
205 @param FvFileInfo A pointer to the FV_FILESYSTEM_FILE_INFO instance that is a struct\r
206 representing a file's info.\r
207 @param BufferSize Pointer to a caller-allocated UINTN. It indicates the size of\r
208 the memory represented by *Buffer.\r
209 @param Buffer Pointer to a pointer to a data buffer to contain file content.\r
210\r
211 @retval EFI_SUCCESS The call completed successfully.\r
212 @retval EFI_WARN_BUFFER_TOO_SMALL The buffer is too small to contain the requested output.\r
213 @retval EFI_ACCESS_DENIED The firmware volume is configured to disallow reads.\r
214 @retval EFI_NOT_FOUND The requested file was not found in the firmware volume.\r
215 @retval EFI_DEVICE_ERROR A hardware error occurred when attempting toaccess the firmware volume.\r
216\r
217**/\r
218EFI_STATUS\r
219FvFsReadFile (\r
220 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *FvProtocol,\r
221 IN FV_FILESYSTEM_FILE_INFO *FvFileInfo,\r
222 IN OUT UINTN *BufferSize,\r
223 IN OUT VOID **Buffer\r
224 )\r
225{\r
226 UINT32 AuthenticationStatus;\r
227 EFI_FV_FILETYPE FoundType;\r
228 EFI_FV_FILE_ATTRIBUTES Attributes;\r
229 EFI_STATUS Status;\r
230\r
231 if (FV_FILETYPE_IS_EXECUTABLE (FvFileInfo->Type)) {\r
232 //\r
233 // Read the first executable section out of the file.\r
234 //\r
235 Status = FvFsFindExecutableSection (FvProtocol, FvFileInfo, BufferSize, Buffer);\r
236 } else if (FvFileInfo->Type == EFI_FV_FILETYPE_FREEFORM) {\r
237 //\r
238 // Try to read a raw section out of the file\r
239 //\r
240 Status = FvProtocol->ReadSection (\r
241 FvProtocol,\r
242 &FvFileInfo->NameGuid,\r
243 EFI_SECTION_RAW,\r
244 0,\r
245 Buffer,\r
246 BufferSize,\r
247 &AuthenticationStatus\r
248 );\r
249 if (EFI_ERROR (Status)) {\r
250 //\r
251 // Didn't find a raw section, just return the whole file.\r
252 //\r
253 Status = FvProtocol->ReadFile (\r
254 FvProtocol,\r
255 &FvFileInfo->NameGuid,\r
256 Buffer,\r
257 BufferSize,\r
258 &FoundType,\r
259 &Attributes,\r
260 &AuthenticationStatus\r
261 );\r
262 }\r
263 } else {\r
264 //\r
265 // Read the entire file\r
266 //\r
267 Status = FvProtocol->ReadFile (\r
268 FvProtocol,\r
269 &FvFileInfo->NameGuid,\r
270 Buffer,\r
271 BufferSize,\r
272 &FoundType,\r
273 &Attributes,\r
274 &AuthenticationStatus\r
275 );\r
276 }\r
277\r
278 return Status;\r
279}\r
280\r
281/**\r
282 Helper function for populating an EFI_FILE_INFO for a file.\r
283\r
284 Note the CreateTime, LastAccessTime and ModificationTime fields in EFI_FILE_INFO\r
285 are full zero as FV2 protocol has no corresponding info to fill.\r
286\r
287 @param FvFileInfo A pointer to the FV_FILESYSTEM_FILE_INFO instance that is a struct\r
288 representing a file's info.\r
289 @param BufferSize Pointer to a caller-allocated UINTN. It indicates the size of\r
290 the memory represented by FileInfo.\r
291 @param FileInfo A pointer to EFI_FILE_INFO to contain the returned file info.\r
292\r
293 @retval EFI_SUCCESS The call completed successfully.\r
294 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to contain the requested output.\r
295\r
296**/\r
297EFI_STATUS\r
298FvFsGetFileInfo (\r
299 IN FV_FILESYSTEM_FILE_INFO *FvFileInfo,\r
300 IN OUT UINTN *BufferSize,\r
301 OUT EFI_FILE_INFO *FileInfo\r
302 )\r
303{\r
304 UINTN InfoSize;\r
305\r
270fc03f 306 InfoSize = (UINTN)FvFileInfo->FileInfo.Size;\r
9da91aea
BJ
307 if (*BufferSize < InfoSize) {\r
308 *BufferSize = InfoSize;\r
309 return EFI_BUFFER_TOO_SMALL;\r
310 }\r
311\r
312 //\r
313 // Initialize FileInfo\r
314 //\r
315 CopyMem (FileInfo, &FvFileInfo->FileInfo, InfoSize);\r
316\r
317 *BufferSize = InfoSize;\r
318 return EFI_SUCCESS;\r
319}\r
320\r
321/**\r
322 Removes the last directory or file entry in a path by changing the last\r
323 L'\' to a CHAR_NULL.\r
324\r
325 @param Path The pointer to the path to modify.\r
326\r
327 @retval FALSE Nothing was found to remove.\r
328 @retval TRUE A directory or file was removed.\r
329\r
330**/\r
331BOOLEAN\r
332EFIAPI\r
333RemoveLastItemFromPath (\r
334 IN OUT CHAR16 *Path\r
335 )\r
336{\r
337 CHAR16 *Walker;\r
338 CHAR16 *LastSlash;\r
339 //\r
340 // get directory name from path... ('chop' off extra)\r
341 //\r
342 for ( Walker = Path, LastSlash = NULL\r
343 ; Walker != NULL && *Walker != CHAR_NULL\r
344 ; Walker++\r
345 ){\r
346 if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {\r
347 LastSlash = Walker + 1;\r
348 }\r
349 }\r
350\r
351 if (LastSlash != NULL) {\r
352 *LastSlash = CHAR_NULL;\r
353 return (TRUE);\r
354 }\r
355\r
356 return (FALSE);\r
357}\r
358\r
359/**\r
360 Function to clean up paths.\r
361\r
362 - Single periods in the path are removed.\r
363 - Double periods in the path are removed along with a single parent directory.\r
364 - Forward slashes L'/' are converted to backward slashes L'\'.\r
365\r
366 This will be done inline and the existing buffer may be larger than required\r
367 upon completion.\r
368\r
369 @param Path The pointer to the string containing the path.\r
370\r
371 @retval NULL An error occured.\r
372 @return Path in all other instances.\r
373\r
374**/\r
375CHAR16*\r
376EFIAPI\r
377TrimFilePathToAbsolutePath (\r
378 IN CHAR16 *Path\r
379 )\r
380{\r
381 CHAR16 *TempString;\r
382 UINTN TempSize;\r
383\r
384 if (Path == NULL) {\r
385 return NULL;\r
386 }\r
387\r
388 //\r
389 // Fix up the '/' vs '\'\r
390 //\r
391 for (TempString = Path ; (TempString != NULL) && (*TempString != CHAR_NULL); TempString++) {\r
392 if (*TempString == L'/') {\r
393 *TempString = L'\\';\r
394 }\r
395 }\r
396\r
397 //\r
398 // Fix up the ..\r
399 //\r
400 while ((TempString = StrStr (Path, L"\\..\\")) != NULL) {\r
401 *TempString = CHAR_NULL;\r
402 TempString += 4;\r
403 RemoveLastItemFromPath (Path);\r
404 TempSize = StrSize (TempString);\r
405 CopyMem (Path + StrLen (Path), TempString, TempSize);\r
406 }\r
407\r
408 if (((TempString = StrStr (Path, L"\\..")) != NULL) && (*(TempString + 3) == CHAR_NULL)) {\r
409 *TempString = CHAR_NULL;\r
410 RemoveLastItemFromPath (Path);\r
411 }\r
412\r
413 //\r
414 // Fix up the .\r
415 //\r
416 while ((TempString = StrStr (Path, L"\\.\\")) != NULL) {\r
417 *TempString = CHAR_NULL;\r
418 TempString += 2;\r
419 TempSize = StrSize (TempString);\r
420 CopyMem(Path + StrLen (Path), TempString, TempSize);\r
421 }\r
422\r
423 if (((TempString = StrStr (Path, L"\\.")) != NULL) && (*(TempString + 2) == CHAR_NULL)) {\r
424 *(TempString + 1) = CHAR_NULL;\r
425 }\r
426\r
427 while ((TempString = StrStr (Path, L"\\\\")) != NULL) {\r
428 *TempString = CHAR_NULL;\r
429 TempString += 1;\r
430 TempSize = StrSize(TempString);\r
431 CopyMem(Path + StrLen(Path), TempString, TempSize);\r
432 }\r
433\r
434 if (((TempString = StrStr(Path, L"\\\\")) != NULL) && (*(TempString + 1) == CHAR_NULL)) {\r
435 *(TempString) = CHAR_NULL;\r
436 }\r
437\r
438 return Path;\r
439}\r
440\r
441/**\r
442 Opens a new file relative to the source file's location.\r
443\r
444 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
445 handle to the source location. This would typically be an open\r
446 handle to a directory.\r
447 @param NewHandle A pointer to the location to return the opened handle for the new\r
448 file.\r
449 @param FileName The Null-terminated string of the name of the file to be opened.\r
450 The file name may contain the following path modifiers: "\", ".",\r
451 and "..".\r
452 @param OpenMode The mode to open the file. The only valid combinations that the\r
453 file may be opened with are: Read, Read/Write, or Create/Read/Write.\r
454 @param Attributes Only valid for EFI_FILE_MODE_CREATE, in which case these are the\r
455 attribute bits for the newly created file.\r
456\r
457 @retval EFI_SUCCESS The file was opened.\r
458 @retval EFI_NOT_FOUND The specified file could not be found on the device.\r
459 @retval EFI_NO_MEDIA The device has no medium.\r
460 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no\r
461 longer supported.\r
462 @retval EFI_DEVICE_ERROR The device reported an error.\r
463 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
464 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write\r
465 when the media is write-protected.\r
466 @retval EFI_ACCESS_DENIED The service denied access to the file.\r
467 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file.\r
468 @retval EFI_VOLUME_FULL The volume is full.\r
469\r
470**/\r
471EFI_STATUS\r
472EFIAPI\r
473FvSimpleFileSystemOpen (\r
474 IN EFI_FILE_PROTOCOL *This,\r
475 OUT EFI_FILE_PROTOCOL **NewHandle,\r
476 IN CHAR16 *FileName,\r
477 IN UINT64 OpenMode,\r
478 IN UINT64 Attributes\r
479 )\r
480{\r
481 FV_FILESYSTEM_INSTANCE *Instance;\r
482 FV_FILESYSTEM_FILE *File;\r
483 FV_FILESYSTEM_FILE *NewFile;\r
484 FV_FILESYSTEM_FILE_INFO *FvFileInfo;\r
485 LIST_ENTRY *FvFileInfoLink;\r
1ca40fa9
OM
486 EFI_STATUS Status;\r
487 UINTN FileNameLength;\r
488 UINTN NewFileNameLength;\r
489 CHAR16 *FileNameWithExtension;\r
9da91aea
BJ
490\r
491 //\r
492 // Check for a valid mode\r
493 //\r
494 switch (OpenMode) {\r
495 case EFI_FILE_MODE_READ:\r
496 break;\r
497\r
498 default:\r
499 return EFI_WRITE_PROTECTED;\r
500 }\r
501\r
502 File = FVFS_FILE_FROM_FILE_THIS (This);\r
503 Instance = File->Instance;\r
504\r
505 FileName = TrimFilePathToAbsolutePath (FileName);\r
5a2dcd13
FT
506 if (FileName == NULL) {\r
507 return EFI_INVALID_PARAMETER;\r
508 }\r
9da91aea
BJ
509\r
510 if (FileName[0] == L'\\') {\r
511 FileName++;\r
512 }\r
513\r
514 //\r
515 // Check for opening root\r
516 //\r
517 if (StrCmp (FileName, L".") == 0 || StrCmp (FileName, L"") == 0) {\r
518 NewFile = AllocateZeroPool (sizeof (FV_FILESYSTEM_FILE));\r
519 if (NewFile == NULL) {\r
520 return EFI_OUT_OF_RESOURCES;\r
521 }\r
522 NewFile->Signature = FVFS_FILE_SIGNATURE;\r
523 NewFile->Instance = Instance;\r
524 NewFile->FvFileInfo = File->FvFileInfo;\r
525 CopyMem (&NewFile->FileProtocol, &mFileSystemTemplate, sizeof (mFileSystemTemplate));\r
526 InitializeListHead (&NewFile->Link);\r
527 InsertHeadList (&Instance->FileHead, &NewFile->Link);\r
528\r
529 NewFile->DirReadNext = FVFS_GET_FIRST_FILE_INFO (Instance);\r
530\r
531 *NewHandle = &NewFile->FileProtocol;\r
532 return EFI_SUCCESS;\r
533 }\r
534\r
535 //\r
536 // Do a linear search for a file in the FV with a matching filename\r
537 //\r
1ca40fa9
OM
538 Status = EFI_NOT_FOUND;\r
539 FvFileInfo = NULL;\r
9da91aea
BJ
540 for (FvFileInfoLink = GetFirstNode (&Instance->FileInfoHead);\r
541 !IsNull (&Instance->FileInfoHead, FvFileInfoLink);\r
542 FvFileInfoLink = GetNextNode (&Instance->FileInfoHead, FvFileInfoLink)) {\r
543 FvFileInfo = FVFS_FILE_INFO_FROM_LINK (FvFileInfoLink);\r
544 if (mUnicodeCollation->StriColl (mUnicodeCollation, &FvFileInfo->FileInfo.FileName[0], FileName) == 0) {\r
1ca40fa9
OM
545 Status = EFI_SUCCESS;\r
546 break;\r
547 }\r
548 }\r
9da91aea 549\r
1ca40fa9
OM
550 // If the file has not been found check if the filename exists with an extension\r
551 // in case there was no extension present.\r
552 // FvFileSystem adds a 'virtual' extension '.EFI' to EFI applications and drivers\r
553 // present in the Firmware Volume\r
554 if (Status == EFI_NOT_FOUND) {\r
555 FileNameLength = StrLen (FileName);\r
556\r
557 // Does the filename already contain the '.EFI' extension?\r
558 if (mUnicodeCollation->StriColl (mUnicodeCollation, FileName + FileNameLength - 4, L".efi") != 0) {\r
559 // No, there was no extension. So add one and search again for the file\r
560 // NewFileNameLength = FileNameLength + 1 + 4 = (Number of non-null character) + (file extension) + (a null character)\r
561 NewFileNameLength = FileNameLength + 1 + 4;\r
562 FileNameWithExtension = AllocateCopyPool (NewFileNameLength * 2, FileName);\r
563 StrCatS (FileNameWithExtension, NewFileNameLength, L".EFI");\r
564\r
565 for (FvFileInfoLink = GetFirstNode (&Instance->FileInfoHead);\r
566 !IsNull (&Instance->FileInfoHead, FvFileInfoLink);\r
567 FvFileInfoLink = GetNextNode (&Instance->FileInfoHead, FvFileInfoLink)) {\r
568 FvFileInfo = FVFS_FILE_INFO_FROM_LINK (FvFileInfoLink);\r
569 if (mUnicodeCollation->StriColl (mUnicodeCollation, &FvFileInfo->FileInfo.FileName[0], FileNameWithExtension) == 0) {\r
570 Status = EFI_SUCCESS;\r
571 break;\r
572 }\r
573 }\r
574 }\r
575 }\r
9da91aea 576\r
1ca40fa9
OM
577 if (!EFI_ERROR (Status)) {\r
578 NewFile = AllocateZeroPool (sizeof (FV_FILESYSTEM_FILE));\r
579 if (NewFile == NULL) {\r
580 return EFI_OUT_OF_RESOURCES;\r
9da91aea 581 }\r
1ca40fa9
OM
582\r
583 NewFile->Signature = FVFS_FILE_SIGNATURE;\r
584 NewFile->Instance = Instance;\r
585 NewFile->FvFileInfo = FvFileInfo;\r
586 CopyMem (&NewFile->FileProtocol, &mFileSystemTemplate, sizeof (mFileSystemTemplate));\r
587 InitializeListHead (&NewFile->Link);\r
588 InsertHeadList (&Instance->FileHead, &NewFile->Link);\r
589\r
590 *NewHandle = &NewFile->FileProtocol;\r
591 return EFI_SUCCESS;\r
9da91aea
BJ
592 }\r
593\r
594 return EFI_NOT_FOUND;\r
595}\r
596\r
597/**\r
598 Closes a specified file handle.\r
599\r
600 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
601 handle to close.\r
602\r
603 @retval EFI_SUCCESS The file was closed.\r
604\r
605**/\r
606EFI_STATUS\r
607EFIAPI\r
608FvSimpleFileSystemClose (\r
609 IN EFI_FILE_PROTOCOL *This\r
610 )\r
611{\r
612 FV_FILESYSTEM_INSTANCE *Instance;\r
613 FV_FILESYSTEM_FILE *File;\r
614\r
615 File = FVFS_FILE_FROM_FILE_THIS (This);\r
616 Instance = File->Instance;\r
617\r
618 if (File != Instance->Root) {\r
619 RemoveEntryList (&File->Link);\r
620 FreePool (File);\r
621 }\r
622 return EFI_SUCCESS;\r
623}\r
624\r
625/**\r
626 Reads data from a file.\r
627\r
628 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
629 handle to read data from.\r
630 @param BufferSize On input, the size of the Buffer. On output, the amount of data\r
631 returned in Buffer. In both cases, the size is measured in bytes.\r
632 @param Buffer The buffer into which the data is read.\r
633\r
634 @retval EFI_SUCCESS Data was read.\r
635 @retval EFI_NO_MEDIA The device has no medium.\r
636 @retval EFI_DEVICE_ERROR The device reported an error.\r
637 @retval EFI_DEVICE_ERROR An attempt was made to read from a deleted file.\r
638 @retval EFI_DEVICE_ERROR On entry, the current file position is beyond the end of the file.\r
639 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
640 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory\r
641 entry. BufferSize has been updated with the size\r
642 needed to complete the request.\r
643\r
644**/\r
645EFI_STATUS\r
646EFIAPI\r
647FvSimpleFileSystemRead (\r
648 IN EFI_FILE_PROTOCOL *This,\r
649 IN OUT UINTN *BufferSize,\r
650 OUT VOID *Buffer\r
651 )\r
652{\r
653 FV_FILESYSTEM_INSTANCE *Instance;\r
654 FV_FILESYSTEM_FILE *File;\r
655 EFI_STATUS Status;\r
656 LIST_ENTRY *FvFileInfoLink;\r
657 VOID *FileBuffer;\r
658 UINTN FileSize;\r
659\r
660 File = FVFS_FILE_FROM_FILE_THIS (This);\r
661 Instance = File->Instance;\r
662\r
663 if (File->FvFileInfo == Instance->Root->FvFileInfo) {\r
664 if (File->DirReadNext) {\r
665 //\r
666 // Directory read: populate Buffer with an EFI_FILE_INFO\r
667 //\r
668 Status = FvFsGetFileInfo (File->DirReadNext, BufferSize, Buffer);\r
669 if (!EFI_ERROR (Status)) {\r
670 //\r
671 // Successfully read a directory entry, now update the pointer to the\r
672 // next file, which will be read on the next call to this function\r
673 //\r
674 FvFileInfoLink = GetNextNode (&Instance->FileInfoHead, &File->DirReadNext->Link);\r
675 if (IsNull (&Instance->FileInfoHead, FvFileInfoLink)) {\r
676 //\r
677 // No more files left\r
678 //\r
679 File->DirReadNext = NULL;\r
680 } else {\r
681 File->DirReadNext = FVFS_FILE_INFO_FROM_LINK (FvFileInfoLink);\r
682 }\r
683 }\r
684 return Status;\r
685 } else {\r
686 //\r
687 // Directory read. All entries have been read, so return a zero-size\r
688 // buffer.\r
689 //\r
690 *BufferSize = 0;\r
691 return EFI_SUCCESS;\r
692 }\r
693 } else {\r
270fc03f 694 FileSize = (UINTN)File->FvFileInfo->FileInfo.FileSize;\r
9da91aea
BJ
695\r
696 FileBuffer = AllocateZeroPool (FileSize);\r
697 if (FileBuffer == NULL) {\r
698 return EFI_DEVICE_ERROR;\r
699 }\r
700\r
701 Status = FvFsReadFile (File->Instance->FvProtocol, File->FvFileInfo, &FileSize, &FileBuffer);\r
702 if (EFI_ERROR (Status)) {\r
703 return EFI_DEVICE_ERROR;\r
704 }\r
705\r
706 if (*BufferSize + File->Position > FileSize) {\r
270fc03f 707 *BufferSize = (UINTN)(FileSize - File->Position);\r
9da91aea
BJ
708 }\r
709\r
710 CopyMem (Buffer, (UINT8*)FileBuffer + File->Position, *BufferSize);\r
711 File->Position += *BufferSize;\r
712\r
713 return EFI_SUCCESS;\r
714 }\r
715}\r
716\r
717/**\r
718 Writes data to a file.\r
719\r
720 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
721 handle to write data to.\r
722 @param BufferSize On input, the size of the Buffer. On output, the amount of data\r
723 actually written. In both cases, the size is measured in bytes.\r
724 @param Buffer The buffer of data to write.\r
725\r
726 @retval EFI_SUCCESS Data was written.\r
727 @retval EFI_UNSUPPORTED Writes to open directory files are not supported.\r
728 @retval EFI_NO_MEDIA The device has no medium.\r
729 @retval EFI_DEVICE_ERROR The device reported an error.\r
730 @retval EFI_DEVICE_ERROR An attempt was made to write to a deleted file.\r
731 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
732 @retval EFI_WRITE_PROTECTED The file or medium is write-protected.\r
733 @retval EFI_ACCESS_DENIED The file was opened read only.\r
734 @retval EFI_VOLUME_FULL The volume is full.\r
735\r
736**/\r
737EFI_STATUS\r
738EFIAPI\r
739FvSimpleFileSystemWrite (\r
740 IN EFI_FILE_PROTOCOL *This,\r
741 IN OUT UINTN *BufferSize,\r
742 IN VOID *Buffer\r
743 )\r
744{\r
745 FV_FILESYSTEM_INSTANCE *Instance;\r
746 FV_FILESYSTEM_FILE *File;\r
747\r
748 File = FVFS_FILE_FROM_FILE_THIS (This);\r
749 Instance = File->Instance;\r
750\r
751 if (File->FvFileInfo == Instance->Root->FvFileInfo) {\r
752 return EFI_UNSUPPORTED;\r
753 } else {\r
754 return EFI_WRITE_PROTECTED;\r
755 }\r
756}\r
757\r
758/**\r
759 Returns a file's current position.\r
760\r
761 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
762 handle to get the current position on.\r
763 @param Position The address to return the file's current position value.\r
764\r
765 @retval EFI_SUCCESS The position was returned.\r
766 @retval EFI_UNSUPPORTED The request is not valid on open directories.\r
767 @retval EFI_DEVICE_ERROR An attempt was made to get the position from a deleted file.\r
768\r
769**/\r
770EFI_STATUS\r
771EFIAPI\r
772FvSimpleFileSystemGetPosition (\r
773 IN EFI_FILE_PROTOCOL *This,\r
774 OUT UINT64 *Position\r
775 )\r
776{\r
777 FV_FILESYSTEM_INSTANCE *Instance;\r
778 FV_FILESYSTEM_FILE *File;\r
779\r
780 File = FVFS_FILE_FROM_FILE_THIS (This);\r
781 Instance = File->Instance;\r
782\r
783 if (File->FvFileInfo == Instance->Root->FvFileInfo) {\r
784 return EFI_UNSUPPORTED;\r
785 } else {\r
786 *Position = File->Position;\r
787 return EFI_SUCCESS;\r
788 }\r
789}\r
790\r
791/**\r
792 Sets a file's current position.\r
793\r
794 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the\r
795 file handle to set the requested position on.\r
796 @param Position The byte position from the start of the file to set.\r
797\r
798 @retval EFI_SUCCESS The position was set.\r
799 @retval EFI_UNSUPPORTED The seek request for nonzero is not valid on open\r
800 directories.\r
801 @retval EFI_DEVICE_ERROR An attempt was made to set the position of a deleted file.\r
802\r
803**/\r
804EFI_STATUS\r
805EFIAPI\r
806FvSimpleFileSystemSetPosition (\r
807 IN EFI_FILE_PROTOCOL *This,\r
808 IN UINT64 Position\r
809 )\r
810{\r
811 FV_FILESYSTEM_INSTANCE *Instance;\r
812 FV_FILESYSTEM_FILE *File;\r
813\r
814 File = FVFS_FILE_FROM_FILE_THIS (This);\r
815 Instance = File->Instance;\r
816\r
817 if (File->FvFileInfo == Instance->Root->FvFileInfo) {\r
818 if (Position != 0) {\r
819 return EFI_UNSUPPORTED;\r
820 }\r
821 //\r
822 // Reset directory position to first entry\r
823 //\r
824 File->DirReadNext = FVFS_GET_FIRST_FILE_INFO (Instance);\r
825 } else if (Position == 0xFFFFFFFFFFFFFFFFull) {\r
826 File->Position = File->FvFileInfo->FileInfo.FileSize;\r
827 } else {\r
828 File->Position = Position;\r
829 }\r
830\r
831 return EFI_SUCCESS;\r
832}\r
833\r
834/**\r
835 Flushes all modified data associated with a file to a device.\r
836\r
837 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
838 handle to flush.\r
839\r
840 @retval EFI_SUCCESS The data was flushed.\r
841 @retval EFI_NO_MEDIA The device has no medium.\r
842 @retval EFI_DEVICE_ERROR The device reported an error.\r
843 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
844 @retval EFI_WRITE_PROTECTED The file or medium is write-protected.\r
845 @retval EFI_ACCESS_DENIED The file was opened read-only.\r
846 @retval EFI_VOLUME_FULL The volume is full.\r
847\r
848**/\r
849EFI_STATUS\r
850EFIAPI\r
851FvSimpleFileSystemFlush (\r
852 IN EFI_FILE_PROTOCOL *This\r
853 )\r
854{\r
855 return EFI_WRITE_PROTECTED;\r
856}\r
857\r
858/**\r
859 Close and delete the file handle.\r
860\r
861 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the\r
862 handle to the file to delete.\r
863\r
864 @retval EFI_SUCCESS The file was closed and deleted, and the handle was closed.\r
865 @retval EFI_WARN_DELETE_FAILURE The handle was closed, but the file was not deleted.\r
866\r
867**/\r
868EFI_STATUS\r
869EFIAPI\r
870FvSimpleFileSystemDelete (\r
871 IN EFI_FILE_PROTOCOL *This\r
872 )\r
873{\r
874 EFI_STATUS Status;\r
875\r
876 Status = FvSimpleFileSystemClose (This);\r
877 ASSERT_EFI_ERROR (Status);\r
878\r
879 return EFI_WARN_DELETE_FAILURE;\r
880}\r
881\r
882/**\r
883 Returns information about a file.\r
884\r
885 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
886 handle the requested information is for.\r
887 @param InformationType The type identifier for the information being requested.\r
888 @param BufferSize On input, the size of Buffer. On output, the amount of data\r
889 returned in Buffer. In both cases, the size is measured in bytes.\r
890 @param Buffer A pointer to the data buffer to return. The buffer's type is\r
891 indicated by InformationType.\r
892\r
893 @retval EFI_SUCCESS The information was returned.\r
894 @retval EFI_UNSUPPORTED The InformationType is not known.\r
895 @retval EFI_NO_MEDIA The device has no medium.\r
896 @retval EFI_DEVICE_ERROR The device reported an error.\r
897 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
898 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.\r
899 BufferSize has been updated with the size needed to complete\r
900 the request.\r
901**/\r
902EFI_STATUS\r
903EFIAPI\r
904FvSimpleFileSystemGetInfo (\r
905 IN EFI_FILE_PROTOCOL *This,\r
906 IN EFI_GUID *InformationType,\r
907 IN OUT UINTN *BufferSize,\r
908 OUT VOID *Buffer\r
909 )\r
910{\r
911 FV_FILESYSTEM_FILE *File;\r
912 EFI_FILE_SYSTEM_INFO *FsInfoOut;\r
913 EFI_FILE_SYSTEM_VOLUME_LABEL *FsVolumeLabel;\r
914 FV_FILESYSTEM_INSTANCE *Instance;\r
915 UINTN Size;\r
916 EFI_STATUS Status;\r
917\r
918 File = FVFS_FILE_FROM_FILE_THIS (This);\r
919\r
920 if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid)) {\r
921 //\r
922 // Return filesystem info\r
923 //\r
924 Instance = File->Instance;\r
925\r
926 Size = sizeof (EFI_FILE_SYSTEM_INFO) + StrSize (Instance->VolumeLabel) - sizeof (CHAR16);\r
927\r
928 if (*BufferSize < Size) {\r
929 *BufferSize = Size;\r
930 return EFI_BUFFER_TOO_SMALL;\r
931 }\r
932\r
933 //\r
934 // Cast output buffer for convenience\r
935 //\r
936 FsInfoOut = (EFI_FILE_SYSTEM_INFO *) Buffer;\r
937\r
938 CopyMem (FsInfoOut, &mFsInfoTemplate, sizeof (EFI_FILE_SYSTEM_INFO));\r
f5fc2742
QS
939 Status = StrnCpyS ( FsInfoOut->VolumeLabel, \r
940 (*BufferSize - OFFSET_OF (EFI_FILE_SYSTEM_INFO, VolumeLabel)) / sizeof (CHAR16), \r
941 Instance->VolumeLabel, \r
942 StrLen (Instance->VolumeLabel)\r
943 );\r
9da91aea
BJ
944 ASSERT_EFI_ERROR (Status);\r
945 FsInfoOut->Size = Size;\r
946 return Status;\r
947 } else if (CompareGuid (InformationType, &gEfiFileInfoGuid)) {\r
948 //\r
949 // Return file info\r
950 //\r
951 return FvFsGetFileInfo (File->FvFileInfo, BufferSize, (EFI_FILE_INFO *) Buffer);\r
952 } else if (CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {\r
953 //\r
954 // Return Volume Label\r
955 //\r
956 Instance = File->Instance;\r
957 Size = sizeof (EFI_FILE_SYSTEM_VOLUME_LABEL) + StrSize (Instance->VolumeLabel) - sizeof (CHAR16);;\r
958 if (*BufferSize < Size) {\r
959 *BufferSize = Size;\r
960 return EFI_BUFFER_TOO_SMALL;\r
961 }\r
962\r
963 FsVolumeLabel = (EFI_FILE_SYSTEM_VOLUME_LABEL*) Buffer;\r
f5fc2742
QS
964 Status = StrnCpyS (FsVolumeLabel->VolumeLabel, \r
965 (*BufferSize - OFFSET_OF (EFI_FILE_SYSTEM_VOLUME_LABEL, VolumeLabel)) / sizeof (CHAR16),\r
966 Instance->VolumeLabel, \r
967 StrLen (Instance->VolumeLabel)\r
968 );\r
9da91aea
BJ
969 ASSERT_EFI_ERROR (Status);\r
970 return Status;\r
971 } else {\r
972 return EFI_UNSUPPORTED;\r
973 }\r
974}\r
975\r
976/**\r
977 Sets information about a file.\r
978\r
979 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
980 handle the information is for.\r
981 @param InformationType The type identifier for the information being set.\r
982 @param BufferSize The size, in bytes, of Buffer.\r
983 @param Buffer A pointer to the data buffer to write. The buffer's type is\r
984 indicated by InformationType.\r
985\r
986 @retval EFI_SUCCESS The information was set.\r
987 @retval EFI_UNSUPPORTED The InformationType is not known.\r
988 @retval EFI_NO_MEDIA The device has no medium.\r
989 @retval EFI_DEVICE_ERROR The device reported an error.\r
990 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
991 @retval EFI_WRITE_PROTECTED InformationType is EFI_FILE_INFO_ID and the media is\r
992 read-only.\r
993 @retval EFI_WRITE_PROTECTED InformationType is EFI_FILE_PROTOCOL_SYSTEM_INFO_ID\r
994 and the media is read only.\r
995 @retval EFI_WRITE_PROTECTED InformationType is EFI_FILE_SYSTEM_VOLUME_LABEL_ID\r
996 and the media is read-only.\r
997 @retval EFI_ACCESS_DENIED An attempt is made to change the name of a file to a\r
998 file that is already present.\r
999 @retval EFI_ACCESS_DENIED An attempt is being made to change the EFI_FILE_DIRECTORY\r
1000 Attribute.\r
1001 @retval EFI_ACCESS_DENIED An attempt is being made to change the size of a directory.\r
1002 @retval EFI_ACCESS_DENIED InformationType is EFI_FILE_INFO_ID and the file was opened\r
1003 read-only and an attempt is being made to modify a field\r
1004 other than Attribute.\r
1005 @retval EFI_VOLUME_FULL The volume is full.\r
1006 @retval EFI_BAD_BUFFER_SIZE BufferSize is smaller than the size of the type indicated\r
1007 by InformationType.\r
1008\r
1009**/\r
1010EFI_STATUS\r
1011EFIAPI\r
1012FvSimpleFileSystemSetInfo (\r
1013 IN EFI_FILE_PROTOCOL *This,\r
1014 IN EFI_GUID *InformationType,\r
1015 IN UINTN BufferSize,\r
1016 IN VOID *Buffer\r
1017 )\r
1018{\r
1019 if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid) || \r
1020 CompareGuid (InformationType, &gEfiFileInfoGuid) ||\r
1021 CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {\r
1022 return EFI_WRITE_PROTECTED;\r
1023 }\r
1024\r
1025 return EFI_UNSUPPORTED;\r
1026}\r
1027\r