]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c
MdeModulePkg: Clean up source files
[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
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
ebd2be68
FT
529 NewFile->DirReadNext = NULL;\r
530 if (!IsListEmpty (&Instance->FileInfoHead)) {\r
531 NewFile->DirReadNext = FVFS_GET_FIRST_FILE_INFO (Instance);\r
532 }\r
9da91aea
BJ
533\r
534 *NewHandle = &NewFile->FileProtocol;\r
535 return EFI_SUCCESS;\r
536 }\r
537\r
538 //\r
539 // Do a linear search for a file in the FV with a matching filename\r
540 //\r
1ca40fa9
OM
541 Status = EFI_NOT_FOUND;\r
542 FvFileInfo = NULL;\r
9da91aea
BJ
543 for (FvFileInfoLink = GetFirstNode (&Instance->FileInfoHead);\r
544 !IsNull (&Instance->FileInfoHead, FvFileInfoLink);\r
545 FvFileInfoLink = GetNextNode (&Instance->FileInfoHead, FvFileInfoLink)) {\r
546 FvFileInfo = FVFS_FILE_INFO_FROM_LINK (FvFileInfoLink);\r
547 if (mUnicodeCollation->StriColl (mUnicodeCollation, &FvFileInfo->FileInfo.FileName[0], FileName) == 0) {\r
1ca40fa9
OM
548 Status = EFI_SUCCESS;\r
549 break;\r
550 }\r
551 }\r
9da91aea 552\r
1ca40fa9
OM
553 // If the file has not been found check if the filename exists with an extension\r
554 // in case there was no extension present.\r
555 // FvFileSystem adds a 'virtual' extension '.EFI' to EFI applications and drivers\r
556 // present in the Firmware Volume\r
557 if (Status == EFI_NOT_FOUND) {\r
558 FileNameLength = StrLen (FileName);\r
559\r
560 // Does the filename already contain the '.EFI' extension?\r
561 if (mUnicodeCollation->StriColl (mUnicodeCollation, FileName + FileNameLength - 4, L".efi") != 0) {\r
562 // No, there was no extension. So add one and search again for the file\r
563 // NewFileNameLength = FileNameLength + 1 + 4 = (Number of non-null character) + (file extension) + (a null character)\r
564 NewFileNameLength = FileNameLength + 1 + 4;\r
469293f8
JW
565 FileNameWithExtension = AllocatePool (NewFileNameLength * 2);\r
566 StrCpyS (FileNameWithExtension, NewFileNameLength, FileName);\r
1ca40fa9
OM
567 StrCatS (FileNameWithExtension, NewFileNameLength, L".EFI");\r
568\r
569 for (FvFileInfoLink = GetFirstNode (&Instance->FileInfoHead);\r
570 !IsNull (&Instance->FileInfoHead, FvFileInfoLink);\r
571 FvFileInfoLink = GetNextNode (&Instance->FileInfoHead, FvFileInfoLink)) {\r
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
OM
586\r
587 NewFile->Signature = FVFS_FILE_SIGNATURE;\r
588 NewFile->Instance = Instance;\r
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
616 FV_FILESYSTEM_INSTANCE *Instance;\r
617 FV_FILESYSTEM_FILE *File;\r
618\r
619 File = FVFS_FILE_FROM_FILE_THIS (This);\r
620 Instance = File->Instance;\r
621\r
622 if (File != Instance->Root) {\r
623 RemoveEntryList (&File->Link);\r
624 FreePool (File);\r
625 }\r
626 return EFI_SUCCESS;\r
627}\r
628\r
629/**\r
630 Reads data from a file.\r
631\r
632 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
633 handle to read data from.\r
634 @param BufferSize On input, the size of the Buffer. On output, the amount of data\r
635 returned in Buffer. In both cases, the size is measured in bytes.\r
636 @param Buffer The buffer into which the data is read.\r
637\r
638 @retval EFI_SUCCESS Data was read.\r
639 @retval EFI_NO_MEDIA The device has no medium.\r
640 @retval EFI_DEVICE_ERROR The device reported an error.\r
641 @retval EFI_DEVICE_ERROR An attempt was made to read from a deleted file.\r
642 @retval EFI_DEVICE_ERROR On entry, the current file position is beyond the end of the file.\r
643 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
644 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory\r
645 entry. BufferSize has been updated with the size\r
646 needed to complete the request.\r
647\r
648**/\r
649EFI_STATUS\r
650EFIAPI\r
651FvSimpleFileSystemRead (\r
652 IN EFI_FILE_PROTOCOL *This,\r
653 IN OUT UINTN *BufferSize,\r
654 OUT VOID *Buffer\r
655 )\r
656{\r
657 FV_FILESYSTEM_INSTANCE *Instance;\r
658 FV_FILESYSTEM_FILE *File;\r
659 EFI_STATUS Status;\r
660 LIST_ENTRY *FvFileInfoLink;\r
661 VOID *FileBuffer;\r
662 UINTN FileSize;\r
663\r
664 File = FVFS_FILE_FROM_FILE_THIS (This);\r
665 Instance = File->Instance;\r
666\r
667 if (File->FvFileInfo == Instance->Root->FvFileInfo) {\r
668 if (File->DirReadNext) {\r
669 //\r
670 // Directory read: populate Buffer with an EFI_FILE_INFO\r
671 //\r
672 Status = FvFsGetFileInfo (File->DirReadNext, BufferSize, Buffer);\r
673 if (!EFI_ERROR (Status)) {\r
674 //\r
675 // Successfully read a directory entry, now update the pointer to the\r
676 // next file, which will be read on the next call to this function\r
677 //\r
678 FvFileInfoLink = GetNextNode (&Instance->FileInfoHead, &File->DirReadNext->Link);\r
679 if (IsNull (&Instance->FileInfoHead, FvFileInfoLink)) {\r
680 //\r
681 // No more files left\r
682 //\r
683 File->DirReadNext = NULL;\r
684 } else {\r
685 File->DirReadNext = FVFS_FILE_INFO_FROM_LINK (FvFileInfoLink);\r
686 }\r
687 }\r
688 return Status;\r
689 } else {\r
690 //\r
691 // Directory read. All entries have been read, so return a zero-size\r
692 // buffer.\r
693 //\r
694 *BufferSize = 0;\r
695 return EFI_SUCCESS;\r
696 }\r
697 } else {\r
270fc03f 698 FileSize = (UINTN)File->FvFileInfo->FileInfo.FileSize;\r
9da91aea
BJ
699\r
700 FileBuffer = AllocateZeroPool (FileSize);\r
701 if (FileBuffer == NULL) {\r
702 return EFI_DEVICE_ERROR;\r
703 }\r
704\r
705 Status = FvFsReadFile (File->Instance->FvProtocol, File->FvFileInfo, &FileSize, &FileBuffer);\r
706 if (EFI_ERROR (Status)) {\r
707 return EFI_DEVICE_ERROR;\r
708 }\r
709\r
710 if (*BufferSize + File->Position > FileSize) {\r
270fc03f 711 *BufferSize = (UINTN)(FileSize - File->Position);\r
9da91aea
BJ
712 }\r
713\r
714 CopyMem (Buffer, (UINT8*)FileBuffer + File->Position, *BufferSize);\r
715 File->Position += *BufferSize;\r
716\r
717 return EFI_SUCCESS;\r
718 }\r
719}\r
720\r
721/**\r
722 Writes data to a file.\r
723\r
724 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
725 handle to write data to.\r
726 @param BufferSize On input, the size of the Buffer. On output, the amount of data\r
727 actually written. In both cases, the size is measured in bytes.\r
728 @param Buffer The buffer of data to write.\r
729\r
730 @retval EFI_SUCCESS Data was written.\r
731 @retval EFI_UNSUPPORTED Writes to open directory files are not supported.\r
732 @retval EFI_NO_MEDIA The device has no medium.\r
733 @retval EFI_DEVICE_ERROR The device reported an error.\r
734 @retval EFI_DEVICE_ERROR An attempt was made to write to a deleted file.\r
735 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
736 @retval EFI_WRITE_PROTECTED The file or medium is write-protected.\r
737 @retval EFI_ACCESS_DENIED The file was opened read only.\r
738 @retval EFI_VOLUME_FULL The volume is full.\r
739\r
740**/\r
741EFI_STATUS\r
742EFIAPI\r
743FvSimpleFileSystemWrite (\r
744 IN EFI_FILE_PROTOCOL *This,\r
745 IN OUT UINTN *BufferSize,\r
746 IN VOID *Buffer\r
747 )\r
748{\r
749 FV_FILESYSTEM_INSTANCE *Instance;\r
750 FV_FILESYSTEM_FILE *File;\r
751\r
752 File = FVFS_FILE_FROM_FILE_THIS (This);\r
753 Instance = File->Instance;\r
754\r
755 if (File->FvFileInfo == Instance->Root->FvFileInfo) {\r
756 return EFI_UNSUPPORTED;\r
757 } else {\r
758 return EFI_WRITE_PROTECTED;\r
759 }\r
760}\r
761\r
762/**\r
763 Returns a file's current position.\r
764\r
765 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
766 handle to get the current position on.\r
767 @param Position The address to return the file's current position value.\r
768\r
769 @retval EFI_SUCCESS The position was returned.\r
770 @retval EFI_UNSUPPORTED The request is not valid on open directories.\r
771 @retval EFI_DEVICE_ERROR An attempt was made to get the position from a deleted file.\r
772\r
773**/\r
774EFI_STATUS\r
775EFIAPI\r
776FvSimpleFileSystemGetPosition (\r
777 IN EFI_FILE_PROTOCOL *This,\r
778 OUT UINT64 *Position\r
779 )\r
780{\r
781 FV_FILESYSTEM_INSTANCE *Instance;\r
782 FV_FILESYSTEM_FILE *File;\r
783\r
784 File = FVFS_FILE_FROM_FILE_THIS (This);\r
785 Instance = File->Instance;\r
786\r
787 if (File->FvFileInfo == Instance->Root->FvFileInfo) {\r
788 return EFI_UNSUPPORTED;\r
789 } else {\r
790 *Position = File->Position;\r
791 return EFI_SUCCESS;\r
792 }\r
793}\r
794\r
795/**\r
796 Sets a file's current position.\r
797\r
798 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the\r
799 file handle to set the requested position on.\r
800 @param Position The byte position from the start of the file to set.\r
801\r
802 @retval EFI_SUCCESS The position was set.\r
803 @retval EFI_UNSUPPORTED The seek request for nonzero is not valid on open\r
804 directories.\r
805 @retval EFI_DEVICE_ERROR An attempt was made to set the position of a deleted file.\r
806\r
807**/\r
808EFI_STATUS\r
809EFIAPI\r
810FvSimpleFileSystemSetPosition (\r
811 IN EFI_FILE_PROTOCOL *This,\r
812 IN UINT64 Position\r
813 )\r
814{\r
815 FV_FILESYSTEM_INSTANCE *Instance;\r
816 FV_FILESYSTEM_FILE *File;\r
817\r
818 File = FVFS_FILE_FROM_FILE_THIS (This);\r
819 Instance = File->Instance;\r
820\r
821 if (File->FvFileInfo == Instance->Root->FvFileInfo) {\r
822 if (Position != 0) {\r
823 return EFI_UNSUPPORTED;\r
824 }\r
825 //\r
826 // Reset directory position to first entry\r
827 //\r
ebd2be68 828 if (File->DirReadNext) {\r
d1102dba 829 File->DirReadNext = FVFS_GET_FIRST_FILE_INFO (Instance);\r
ebd2be68 830 }\r
9da91aea
BJ
831 } else if (Position == 0xFFFFFFFFFFFFFFFFull) {\r
832 File->Position = File->FvFileInfo->FileInfo.FileSize;\r
833 } else {\r
834 File->Position = Position;\r
835 }\r
836\r
837 return EFI_SUCCESS;\r
838}\r
839\r
840/**\r
841 Flushes all modified data associated with a file to a device.\r
842\r
843 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
844 handle to flush.\r
845\r
846 @retval EFI_SUCCESS The data was flushed.\r
847 @retval EFI_NO_MEDIA The device has no medium.\r
848 @retval EFI_DEVICE_ERROR The device reported an error.\r
849 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
850 @retval EFI_WRITE_PROTECTED The file or medium is write-protected.\r
851 @retval EFI_ACCESS_DENIED The file was opened read-only.\r
852 @retval EFI_VOLUME_FULL The volume is full.\r
853\r
854**/\r
855EFI_STATUS\r
856EFIAPI\r
857FvSimpleFileSystemFlush (\r
858 IN EFI_FILE_PROTOCOL *This\r
859 )\r
860{\r
861 return EFI_WRITE_PROTECTED;\r
862}\r
863\r
864/**\r
865 Close and delete the file handle.\r
866\r
867 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the\r
868 handle to the file to delete.\r
869\r
870 @retval EFI_SUCCESS The file was closed and deleted, and the handle was closed.\r
871 @retval EFI_WARN_DELETE_FAILURE The handle was closed, but the file was not deleted.\r
872\r
873**/\r
874EFI_STATUS\r
875EFIAPI\r
876FvSimpleFileSystemDelete (\r
877 IN EFI_FILE_PROTOCOL *This\r
878 )\r
879{\r
880 EFI_STATUS Status;\r
881\r
882 Status = FvSimpleFileSystemClose (This);\r
883 ASSERT_EFI_ERROR (Status);\r
884\r
885 return EFI_WARN_DELETE_FAILURE;\r
886}\r
887\r
888/**\r
889 Returns information about a file.\r
890\r
891 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
892 handle the requested information is for.\r
893 @param InformationType The type identifier for the information being requested.\r
894 @param BufferSize On input, the size of Buffer. On output, the amount of data\r
895 returned in Buffer. In both cases, the size is measured in bytes.\r
896 @param Buffer A pointer to the data buffer to return. The buffer's type is\r
897 indicated by InformationType.\r
898\r
899 @retval EFI_SUCCESS The information was returned.\r
900 @retval EFI_UNSUPPORTED The InformationType is not known.\r
901 @retval EFI_NO_MEDIA The device has no medium.\r
902 @retval EFI_DEVICE_ERROR The device reported an error.\r
903 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
904 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.\r
905 BufferSize has been updated with the size needed to complete\r
906 the request.\r
907**/\r
908EFI_STATUS\r
909EFIAPI\r
910FvSimpleFileSystemGetInfo (\r
911 IN EFI_FILE_PROTOCOL *This,\r
912 IN EFI_GUID *InformationType,\r
913 IN OUT UINTN *BufferSize,\r
914 OUT VOID *Buffer\r
915 )\r
916{\r
917 FV_FILESYSTEM_FILE *File;\r
918 EFI_FILE_SYSTEM_INFO *FsInfoOut;\r
919 EFI_FILE_SYSTEM_VOLUME_LABEL *FsVolumeLabel;\r
920 FV_FILESYSTEM_INSTANCE *Instance;\r
921 UINTN Size;\r
922 EFI_STATUS Status;\r
923\r
924 File = FVFS_FILE_FROM_FILE_THIS (This);\r
925\r
926 if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid)) {\r
927 //\r
928 // Return filesystem info\r
929 //\r
930 Instance = File->Instance;\r
931\r
932 Size = sizeof (EFI_FILE_SYSTEM_INFO) + StrSize (Instance->VolumeLabel) - sizeof (CHAR16);\r
933\r
934 if (*BufferSize < Size) {\r
935 *BufferSize = Size;\r
936 return EFI_BUFFER_TOO_SMALL;\r
937 }\r
938\r
939 //\r
940 // Cast output buffer for convenience\r
941 //\r
942 FsInfoOut = (EFI_FILE_SYSTEM_INFO *) Buffer;\r
943\r
944 CopyMem (FsInfoOut, &mFsInfoTemplate, sizeof (EFI_FILE_SYSTEM_INFO));\r
d1102dba
LG
945 Status = StrnCpyS ( FsInfoOut->VolumeLabel,\r
946 (*BufferSize - OFFSET_OF (EFI_FILE_SYSTEM_INFO, VolumeLabel)) / sizeof (CHAR16),\r
947 Instance->VolumeLabel,\r
f5fc2742
QS
948 StrLen (Instance->VolumeLabel)\r
949 );\r
9da91aea
BJ
950 ASSERT_EFI_ERROR (Status);\r
951 FsInfoOut->Size = Size;\r
952 return Status;\r
953 } else if (CompareGuid (InformationType, &gEfiFileInfoGuid)) {\r
954 //\r
955 // Return file info\r
956 //\r
957 return FvFsGetFileInfo (File->FvFileInfo, BufferSize, (EFI_FILE_INFO *) Buffer);\r
958 } else if (CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {\r
959 //\r
960 // Return Volume Label\r
961 //\r
962 Instance = File->Instance;\r
963 Size = sizeof (EFI_FILE_SYSTEM_VOLUME_LABEL) + StrSize (Instance->VolumeLabel) - sizeof (CHAR16);;\r
964 if (*BufferSize < Size) {\r
965 *BufferSize = Size;\r
966 return EFI_BUFFER_TOO_SMALL;\r
967 }\r
968\r
969 FsVolumeLabel = (EFI_FILE_SYSTEM_VOLUME_LABEL*) Buffer;\r
d1102dba 970 Status = StrnCpyS (FsVolumeLabel->VolumeLabel,\r
f5fc2742 971 (*BufferSize - OFFSET_OF (EFI_FILE_SYSTEM_VOLUME_LABEL, VolumeLabel)) / sizeof (CHAR16),\r
d1102dba 972 Instance->VolumeLabel,\r
f5fc2742
QS
973 StrLen (Instance->VolumeLabel)\r
974 );\r
9da91aea
BJ
975 ASSERT_EFI_ERROR (Status);\r
976 return Status;\r
977 } else {\r
978 return EFI_UNSUPPORTED;\r
979 }\r
980}\r
981\r
982/**\r
983 Sets information about a file.\r
984\r
985 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
986 handle the information is for.\r
987 @param InformationType The type identifier for the information being set.\r
988 @param BufferSize The size, in bytes, of Buffer.\r
989 @param Buffer A pointer to the data buffer to write. The buffer's type is\r
990 indicated by InformationType.\r
991\r
992 @retval EFI_SUCCESS The information was set.\r
993 @retval EFI_UNSUPPORTED The InformationType is not known.\r
994 @retval EFI_NO_MEDIA The device has no medium.\r
995 @retval EFI_DEVICE_ERROR The device reported an error.\r
996 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
997 @retval EFI_WRITE_PROTECTED InformationType is EFI_FILE_INFO_ID and the media is\r
998 read-only.\r
999 @retval EFI_WRITE_PROTECTED InformationType is EFI_FILE_PROTOCOL_SYSTEM_INFO_ID\r
1000 and the media is read only.\r
1001 @retval EFI_WRITE_PROTECTED InformationType is EFI_FILE_SYSTEM_VOLUME_LABEL_ID\r
1002 and the media is read-only.\r
1003 @retval EFI_ACCESS_DENIED An attempt is made to change the name of a file to a\r
1004 file that is already present.\r
1005 @retval EFI_ACCESS_DENIED An attempt is being made to change the EFI_FILE_DIRECTORY\r
1006 Attribute.\r
1007 @retval EFI_ACCESS_DENIED An attempt is being made to change the size of a directory.\r
1008 @retval EFI_ACCESS_DENIED InformationType is EFI_FILE_INFO_ID and the file was opened\r
1009 read-only and an attempt is being made to modify a field\r
1010 other than Attribute.\r
1011 @retval EFI_VOLUME_FULL The volume is full.\r
1012 @retval EFI_BAD_BUFFER_SIZE BufferSize is smaller than the size of the type indicated\r
1013 by InformationType.\r
1014\r
1015**/\r
1016EFI_STATUS\r
1017EFIAPI\r
1018FvSimpleFileSystemSetInfo (\r
1019 IN EFI_FILE_PROTOCOL *This,\r
1020 IN EFI_GUID *InformationType,\r
1021 IN UINTN BufferSize,\r
1022 IN VOID *Buffer\r
1023 )\r
1024{\r
d1102dba 1025 if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid) ||\r
9da91aea
BJ
1026 CompareGuid (InformationType, &gEfiFileInfoGuid) ||\r
1027 CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {\r
1028 return EFI_WRITE_PROTECTED;\r
1029 }\r
1030\r
1031 return EFI_UNSUPPORTED;\r
1032}\r
1033\r