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