]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c
MdeModulePkg/FvSimpleFileSystem: Add a new module to provide access to executable...
[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
15Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
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
142 Status = FvFsFindExecutableSection (FvProtocol, FvFileInfo, &FvFileInfo->FileInfo.FileSize, &IgnoredPtr);\r
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
156 &FvFileInfo->FileInfo.FileSize,\r
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
170 &FvFileInfo->FileInfo.FileSize,\r
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
184 &FvFileInfo->FileInfo.FileSize,\r
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
306 InfoSize = FvFileInfo->FileInfo.Size;\r
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
486\r
487 //\r
488 // Check for a valid mode\r
489 //\r
490 switch (OpenMode) {\r
491 case EFI_FILE_MODE_READ:\r
492 break;\r
493\r
494 default:\r
495 return EFI_WRITE_PROTECTED;\r
496 }\r
497\r
498 File = FVFS_FILE_FROM_FILE_THIS (This);\r
499 Instance = File->Instance;\r
500\r
501 FileName = TrimFilePathToAbsolutePath (FileName);\r
502\r
503 if (FileName[0] == L'\\') {\r
504 FileName++;\r
505 }\r
506\r
507 //\r
508 // Check for opening root\r
509 //\r
510 if (StrCmp (FileName, L".") == 0 || StrCmp (FileName, L"") == 0) {\r
511 NewFile = AllocateZeroPool (sizeof (FV_FILESYSTEM_FILE));\r
512 if (NewFile == NULL) {\r
513 return EFI_OUT_OF_RESOURCES;\r
514 }\r
515 NewFile->Signature = FVFS_FILE_SIGNATURE;\r
516 NewFile->Instance = Instance;\r
517 NewFile->FvFileInfo = File->FvFileInfo;\r
518 CopyMem (&NewFile->FileProtocol, &mFileSystemTemplate, sizeof (mFileSystemTemplate));\r
519 InitializeListHead (&NewFile->Link);\r
520 InsertHeadList (&Instance->FileHead, &NewFile->Link);\r
521\r
522 NewFile->DirReadNext = FVFS_GET_FIRST_FILE_INFO (Instance);\r
523\r
524 *NewHandle = &NewFile->FileProtocol;\r
525 return EFI_SUCCESS;\r
526 }\r
527\r
528 //\r
529 // Do a linear search for a file in the FV with a matching filename\r
530 //\r
531 for (FvFileInfoLink = GetFirstNode (&Instance->FileInfoHead);\r
532 !IsNull (&Instance->FileInfoHead, FvFileInfoLink);\r
533 FvFileInfoLink = GetNextNode (&Instance->FileInfoHead, FvFileInfoLink)) {\r
534 FvFileInfo = FVFS_FILE_INFO_FROM_LINK (FvFileInfoLink);\r
535 if (mUnicodeCollation->StriColl (mUnicodeCollation, &FvFileInfo->FileInfo.FileName[0], FileName) == 0) {\r
536 NewFile = AllocateZeroPool (sizeof (FV_FILESYSTEM_FILE));\r
537 if (NewFile == NULL) {\r
538 return EFI_OUT_OF_RESOURCES;\r
539 }\r
540\r
541 NewFile->Signature = FVFS_FILE_SIGNATURE;\r
542 NewFile->Instance = Instance;\r
543 NewFile->FvFileInfo = FvFileInfo;\r
544 CopyMem (&NewFile->FileProtocol, &mFileSystemTemplate, sizeof (mFileSystemTemplate));\r
545 InitializeListHead (&NewFile->Link);\r
546 InsertHeadList (&Instance->FileHead, &NewFile->Link);\r
547\r
548 *NewHandle = &NewFile->FileProtocol;\r
549 return EFI_SUCCESS;\r
550 }\r
551 }\r
552\r
553 return EFI_NOT_FOUND;\r
554}\r
555\r
556/**\r
557 Closes a specified file handle.\r
558\r
559 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
560 handle to close.\r
561\r
562 @retval EFI_SUCCESS The file was closed.\r
563\r
564**/\r
565EFI_STATUS\r
566EFIAPI\r
567FvSimpleFileSystemClose (\r
568 IN EFI_FILE_PROTOCOL *This\r
569 )\r
570{\r
571 FV_FILESYSTEM_INSTANCE *Instance;\r
572 FV_FILESYSTEM_FILE *File;\r
573\r
574 File = FVFS_FILE_FROM_FILE_THIS (This);\r
575 Instance = File->Instance;\r
576\r
577 if (File != Instance->Root) {\r
578 RemoveEntryList (&File->Link);\r
579 FreePool (File);\r
580 }\r
581 return EFI_SUCCESS;\r
582}\r
583\r
584/**\r
585 Reads data from a file.\r
586\r
587 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
588 handle to read data from.\r
589 @param BufferSize On input, the size of the Buffer. On output, the amount of data\r
590 returned in Buffer. In both cases, the size is measured in bytes.\r
591 @param Buffer The buffer into which the data is read.\r
592\r
593 @retval EFI_SUCCESS Data was read.\r
594 @retval EFI_NO_MEDIA The device has no medium.\r
595 @retval EFI_DEVICE_ERROR The device reported an error.\r
596 @retval EFI_DEVICE_ERROR An attempt was made to read from a deleted file.\r
597 @retval EFI_DEVICE_ERROR On entry, the current file position is beyond the end of the file.\r
598 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
599 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory\r
600 entry. BufferSize has been updated with the size\r
601 needed to complete the request.\r
602\r
603**/\r
604EFI_STATUS\r
605EFIAPI\r
606FvSimpleFileSystemRead (\r
607 IN EFI_FILE_PROTOCOL *This,\r
608 IN OUT UINTN *BufferSize,\r
609 OUT VOID *Buffer\r
610 )\r
611{\r
612 FV_FILESYSTEM_INSTANCE *Instance;\r
613 FV_FILESYSTEM_FILE *File;\r
614 EFI_STATUS Status;\r
615 LIST_ENTRY *FvFileInfoLink;\r
616 VOID *FileBuffer;\r
617 UINTN FileSize;\r
618\r
619 File = FVFS_FILE_FROM_FILE_THIS (This);\r
620 Instance = File->Instance;\r
621\r
622 if (File->FvFileInfo == Instance->Root->FvFileInfo) {\r
623 if (File->DirReadNext) {\r
624 //\r
625 // Directory read: populate Buffer with an EFI_FILE_INFO\r
626 //\r
627 Status = FvFsGetFileInfo (File->DirReadNext, BufferSize, Buffer);\r
628 if (!EFI_ERROR (Status)) {\r
629 //\r
630 // Successfully read a directory entry, now update the pointer to the\r
631 // next file, which will be read on the next call to this function\r
632 //\r
633 FvFileInfoLink = GetNextNode (&Instance->FileInfoHead, &File->DirReadNext->Link);\r
634 if (IsNull (&Instance->FileInfoHead, FvFileInfoLink)) {\r
635 //\r
636 // No more files left\r
637 //\r
638 File->DirReadNext = NULL;\r
639 } else {\r
640 File->DirReadNext = FVFS_FILE_INFO_FROM_LINK (FvFileInfoLink);\r
641 }\r
642 }\r
643 return Status;\r
644 } else {\r
645 //\r
646 // Directory read. All entries have been read, so return a zero-size\r
647 // buffer.\r
648 //\r
649 *BufferSize = 0;\r
650 return EFI_SUCCESS;\r
651 }\r
652 } else {\r
653 FileSize = File->FvFileInfo->FileInfo.FileSize;\r
654\r
655 FileBuffer = AllocateZeroPool (FileSize);\r
656 if (FileBuffer == NULL) {\r
657 return EFI_DEVICE_ERROR;\r
658 }\r
659\r
660 Status = FvFsReadFile (File->Instance->FvProtocol, File->FvFileInfo, &FileSize, &FileBuffer);\r
661 if (EFI_ERROR (Status)) {\r
662 return EFI_DEVICE_ERROR;\r
663 }\r
664\r
665 if (*BufferSize + File->Position > FileSize) {\r
666 *BufferSize = FileSize - File->Position;\r
667 }\r
668\r
669 CopyMem (Buffer, (UINT8*)FileBuffer + File->Position, *BufferSize);\r
670 File->Position += *BufferSize;\r
671\r
672 return EFI_SUCCESS;\r
673 }\r
674}\r
675\r
676/**\r
677 Writes data to a file.\r
678\r
679 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
680 handle to write data to.\r
681 @param BufferSize On input, the size of the Buffer. On output, the amount of data\r
682 actually written. In both cases, the size is measured in bytes.\r
683 @param Buffer The buffer of data to write.\r
684\r
685 @retval EFI_SUCCESS Data was written.\r
686 @retval EFI_UNSUPPORTED Writes to open directory files are not supported.\r
687 @retval EFI_NO_MEDIA The device has no medium.\r
688 @retval EFI_DEVICE_ERROR The device reported an error.\r
689 @retval EFI_DEVICE_ERROR An attempt was made to write to a deleted file.\r
690 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
691 @retval EFI_WRITE_PROTECTED The file or medium is write-protected.\r
692 @retval EFI_ACCESS_DENIED The file was opened read only.\r
693 @retval EFI_VOLUME_FULL The volume is full.\r
694\r
695**/\r
696EFI_STATUS\r
697EFIAPI\r
698FvSimpleFileSystemWrite (\r
699 IN EFI_FILE_PROTOCOL *This,\r
700 IN OUT UINTN *BufferSize,\r
701 IN VOID *Buffer\r
702 )\r
703{\r
704 FV_FILESYSTEM_INSTANCE *Instance;\r
705 FV_FILESYSTEM_FILE *File;\r
706\r
707 File = FVFS_FILE_FROM_FILE_THIS (This);\r
708 Instance = File->Instance;\r
709\r
710 if (File->FvFileInfo == Instance->Root->FvFileInfo) {\r
711 return EFI_UNSUPPORTED;\r
712 } else {\r
713 return EFI_WRITE_PROTECTED;\r
714 }\r
715}\r
716\r
717/**\r
718 Returns a file's current position.\r
719\r
720 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
721 handle to get the current position on.\r
722 @param Position The address to return the file's current position value.\r
723\r
724 @retval EFI_SUCCESS The position was returned.\r
725 @retval EFI_UNSUPPORTED The request is not valid on open directories.\r
726 @retval EFI_DEVICE_ERROR An attempt was made to get the position from a deleted file.\r
727\r
728**/\r
729EFI_STATUS\r
730EFIAPI\r
731FvSimpleFileSystemGetPosition (\r
732 IN EFI_FILE_PROTOCOL *This,\r
733 OUT UINT64 *Position\r
734 )\r
735{\r
736 FV_FILESYSTEM_INSTANCE *Instance;\r
737 FV_FILESYSTEM_FILE *File;\r
738\r
739 File = FVFS_FILE_FROM_FILE_THIS (This);\r
740 Instance = File->Instance;\r
741\r
742 if (File->FvFileInfo == Instance->Root->FvFileInfo) {\r
743 return EFI_UNSUPPORTED;\r
744 } else {\r
745 *Position = File->Position;\r
746 return EFI_SUCCESS;\r
747 }\r
748}\r
749\r
750/**\r
751 Sets a file's current position.\r
752\r
753 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the\r
754 file handle to set the requested position on.\r
755 @param Position The byte position from the start of the file to set.\r
756\r
757 @retval EFI_SUCCESS The position was set.\r
758 @retval EFI_UNSUPPORTED The seek request for nonzero is not valid on open\r
759 directories.\r
760 @retval EFI_DEVICE_ERROR An attempt was made to set the position of a deleted file.\r
761\r
762**/\r
763EFI_STATUS\r
764EFIAPI\r
765FvSimpleFileSystemSetPosition (\r
766 IN EFI_FILE_PROTOCOL *This,\r
767 IN UINT64 Position\r
768 )\r
769{\r
770 FV_FILESYSTEM_INSTANCE *Instance;\r
771 FV_FILESYSTEM_FILE *File;\r
772\r
773 File = FVFS_FILE_FROM_FILE_THIS (This);\r
774 Instance = File->Instance;\r
775\r
776 if (File->FvFileInfo == Instance->Root->FvFileInfo) {\r
777 if (Position != 0) {\r
778 return EFI_UNSUPPORTED;\r
779 }\r
780 //\r
781 // Reset directory position to first entry\r
782 //\r
783 File->DirReadNext = FVFS_GET_FIRST_FILE_INFO (Instance);\r
784 } else if (Position == 0xFFFFFFFFFFFFFFFFull) {\r
785 File->Position = File->FvFileInfo->FileInfo.FileSize;\r
786 } else {\r
787 File->Position = Position;\r
788 }\r
789\r
790 return EFI_SUCCESS;\r
791}\r
792\r
793/**\r
794 Flushes all modified data associated with a file to a device.\r
795\r
796 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
797 handle to flush.\r
798\r
799 @retval EFI_SUCCESS The data was flushed.\r
800 @retval EFI_NO_MEDIA The device has no medium.\r
801 @retval EFI_DEVICE_ERROR The device reported an error.\r
802 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
803 @retval EFI_WRITE_PROTECTED The file or medium is write-protected.\r
804 @retval EFI_ACCESS_DENIED The file was opened read-only.\r
805 @retval EFI_VOLUME_FULL The volume is full.\r
806\r
807**/\r
808EFI_STATUS\r
809EFIAPI\r
810FvSimpleFileSystemFlush (\r
811 IN EFI_FILE_PROTOCOL *This\r
812 )\r
813{\r
814 return EFI_WRITE_PROTECTED;\r
815}\r
816\r
817/**\r
818 Close and delete the file handle.\r
819\r
820 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the\r
821 handle to the file to delete.\r
822\r
823 @retval EFI_SUCCESS The file was closed and deleted, and the handle was closed.\r
824 @retval EFI_WARN_DELETE_FAILURE The handle was closed, but the file was not deleted.\r
825\r
826**/\r
827EFI_STATUS\r
828EFIAPI\r
829FvSimpleFileSystemDelete (\r
830 IN EFI_FILE_PROTOCOL *This\r
831 )\r
832{\r
833 EFI_STATUS Status;\r
834\r
835 Status = FvSimpleFileSystemClose (This);\r
836 ASSERT_EFI_ERROR (Status);\r
837\r
838 return EFI_WARN_DELETE_FAILURE;\r
839}\r
840\r
841/**\r
842 Returns information about a file.\r
843\r
844 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
845 handle the requested information is for.\r
846 @param InformationType The type identifier for the information being requested.\r
847 @param BufferSize On input, the size of Buffer. On output, the amount of data\r
848 returned in Buffer. In both cases, the size is measured in bytes.\r
849 @param Buffer A pointer to the data buffer to return. The buffer's type is\r
850 indicated by InformationType.\r
851\r
852 @retval EFI_SUCCESS The information was returned.\r
853 @retval EFI_UNSUPPORTED The InformationType is not known.\r
854 @retval EFI_NO_MEDIA The device has no medium.\r
855 @retval EFI_DEVICE_ERROR The device reported an error.\r
856 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
857 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.\r
858 BufferSize has been updated with the size needed to complete\r
859 the request.\r
860**/\r
861EFI_STATUS\r
862EFIAPI\r
863FvSimpleFileSystemGetInfo (\r
864 IN EFI_FILE_PROTOCOL *This,\r
865 IN EFI_GUID *InformationType,\r
866 IN OUT UINTN *BufferSize,\r
867 OUT VOID *Buffer\r
868 )\r
869{\r
870 FV_FILESYSTEM_FILE *File;\r
871 EFI_FILE_SYSTEM_INFO *FsInfoOut;\r
872 EFI_FILE_SYSTEM_VOLUME_LABEL *FsVolumeLabel;\r
873 FV_FILESYSTEM_INSTANCE *Instance;\r
874 UINTN Size;\r
875 EFI_STATUS Status;\r
876\r
877 File = FVFS_FILE_FROM_FILE_THIS (This);\r
878\r
879 if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid)) {\r
880 //\r
881 // Return filesystem info\r
882 //\r
883 Instance = File->Instance;\r
884\r
885 Size = sizeof (EFI_FILE_SYSTEM_INFO) + StrSize (Instance->VolumeLabel) - sizeof (CHAR16);\r
886\r
887 if (*BufferSize < Size) {\r
888 *BufferSize = Size;\r
889 return EFI_BUFFER_TOO_SMALL;\r
890 }\r
891\r
892 //\r
893 // Cast output buffer for convenience\r
894 //\r
895 FsInfoOut = (EFI_FILE_SYSTEM_INFO *) Buffer;\r
896\r
897 CopyMem (FsInfoOut, &mFsInfoTemplate, sizeof (EFI_FILE_SYSTEM_INFO));\r
898 Status = StrnCpyS (FsInfoOut->VolumeLabel, (*BufferSize - OFFSET_OF (EFI_FILE_SYSTEM_INFO, VolumeLabel)) / sizeof (CHAR16), Instance->VolumeLabel, StrLen (Instance->VolumeLabel));\r
899 ASSERT_EFI_ERROR (Status);\r
900 FsInfoOut->Size = Size;\r
901 return Status;\r
902 } else if (CompareGuid (InformationType, &gEfiFileInfoGuid)) {\r
903 //\r
904 // Return file info\r
905 //\r
906 return FvFsGetFileInfo (File->FvFileInfo, BufferSize, (EFI_FILE_INFO *) Buffer);\r
907 } else if (CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {\r
908 //\r
909 // Return Volume Label\r
910 //\r
911 Instance = File->Instance;\r
912 Size = sizeof (EFI_FILE_SYSTEM_VOLUME_LABEL) + StrSize (Instance->VolumeLabel) - sizeof (CHAR16);;\r
913 if (*BufferSize < Size) {\r
914 *BufferSize = Size;\r
915 return EFI_BUFFER_TOO_SMALL;\r
916 }\r
917\r
918 FsVolumeLabel = (EFI_FILE_SYSTEM_VOLUME_LABEL*) Buffer;\r
919 Status = StrnCpyS (FsVolumeLabel->VolumeLabel, (*BufferSize - OFFSET_OF (EFI_FILE_SYSTEM_VOLUME_LABEL, VolumeLabel)) / sizeof (CHAR16), Instance->VolumeLabel, StrLen (Instance->VolumeLabel));\r
920 ASSERT_EFI_ERROR (Status);\r
921 return Status;\r
922 } else {\r
923 return EFI_UNSUPPORTED;\r
924 }\r
925}\r
926\r
927/**\r
928 Sets information about a file.\r
929\r
930 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
931 handle the information is for.\r
932 @param InformationType The type identifier for the information being set.\r
933 @param BufferSize The size, in bytes, of Buffer.\r
934 @param Buffer A pointer to the data buffer to write. The buffer's type is\r
935 indicated by InformationType.\r
936\r
937 @retval EFI_SUCCESS The information was set.\r
938 @retval EFI_UNSUPPORTED The InformationType is not known.\r
939 @retval EFI_NO_MEDIA The device has no medium.\r
940 @retval EFI_DEVICE_ERROR The device reported an error.\r
941 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
942 @retval EFI_WRITE_PROTECTED InformationType is EFI_FILE_INFO_ID and the media is\r
943 read-only.\r
944 @retval EFI_WRITE_PROTECTED InformationType is EFI_FILE_PROTOCOL_SYSTEM_INFO_ID\r
945 and the media is read only.\r
946 @retval EFI_WRITE_PROTECTED InformationType is EFI_FILE_SYSTEM_VOLUME_LABEL_ID\r
947 and the media is read-only.\r
948 @retval EFI_ACCESS_DENIED An attempt is made to change the name of a file to a\r
949 file that is already present.\r
950 @retval EFI_ACCESS_DENIED An attempt is being made to change the EFI_FILE_DIRECTORY\r
951 Attribute.\r
952 @retval EFI_ACCESS_DENIED An attempt is being made to change the size of a directory.\r
953 @retval EFI_ACCESS_DENIED InformationType is EFI_FILE_INFO_ID and the file was opened\r
954 read-only and an attempt is being made to modify a field\r
955 other than Attribute.\r
956 @retval EFI_VOLUME_FULL The volume is full.\r
957 @retval EFI_BAD_BUFFER_SIZE BufferSize is smaller than the size of the type indicated\r
958 by InformationType.\r
959\r
960**/\r
961EFI_STATUS\r
962EFIAPI\r
963FvSimpleFileSystemSetInfo (\r
964 IN EFI_FILE_PROTOCOL *This,\r
965 IN EFI_GUID *InformationType,\r
966 IN UINTN BufferSize,\r
967 IN VOID *Buffer\r
968 )\r
969{\r
970 if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid) || \r
971 CompareGuid (InformationType, &gEfiFileInfoGuid) ||\r
972 CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {\r
973 return EFI_WRITE_PROTECTED;\r
974 }\r
975\r
976 return EFI_UNSUPPORTED;\r
977}\r
978\r