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