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