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