3 Copyright (c) 2006 - 2010, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 WinNtSimpleFileSystem.c
18 Produce Simple File System abstractions for directories on your PC using Win32 APIs.
19 The configuration of what devices to mount or emulate comes from NT
20 environment variables. The variables must be visible to the Microsoft*
21 Developer Studio for them to work.
23 * Other names and brands may be claimed as the property of others.
28 // The package level header files this module uses
33 // The protocols, PPI and GUID defintions for this module
35 #include <Guid/FileSystemVolumeLabelInfo.h>
36 #include <Protocol/WinNtIo.h>
37 #include <Protocol/ComponentName.h>
38 #include <Guid/FileInfo.h>
39 #include <Protocol/DriverBinding.h>
40 #include <Guid/FileSystemInfo.h>
41 #include <Protocol/SimpleFileSystem.h>
43 // The Library classes this module consumes
45 #include <Library/DebugLib.h>
46 #include <Library/BaseLib.h>
47 #include <Library/UefiDriverEntryPoint.h>
48 #include <Library/UefiLib.h>
49 #include <Library/BaseMemoryLib.h>
50 #include <Library/UefiBootServicesTableLib.h>
51 #include <Library/MemoryAllocationLib.h>
53 #include "WinNtSimpleFileSystem.h"
55 EFI_DRIVER_BINDING_PROTOCOL gWinNtSimpleFileSystemDriverBinding
= {
56 WinNtSimpleFileSystemDriverBindingSupported
,
57 WinNtSimpleFileSystemDriverBindingStart
,
58 WinNtSimpleFileSystemDriverBindingStop
,
65 The user Entry Point for module WinNtSimpleFileSystem. The user code starts with this function.
67 @param[in] ImageHandle The firmware allocated handle for the EFI image.
68 @param[in] SystemTable A pointer to the EFI System Table.
70 @retval EFI_SUCCESS The entry point is executed successfully.
71 @retval other Some error occurs when executing this entry point.
76 InitializeWinNtSimpleFileSystem(
77 IN EFI_HANDLE ImageHandle
,
78 IN EFI_SYSTEM_TABLE
*SystemTable
84 // Install driver model protocol(s).
86 Status
= EfiLibInstallDriverBindingComponentName2 (
89 &gWinNtSimpleFileSystemDriverBinding
,
91 &gWinNtSimpleFileSystemComponentName
,
92 &gWinNtSimpleFileSystemComponentName2
94 ASSERT_EFI_ERROR (Status
);
109 Locate the first occurance of a character in a string.
113 Str - Pointer to NULL terminated unicode string.
114 Chr - Character to locate.
118 If Str is NULL, then NULL is returned.
119 If Chr is not contained in Str, then NULL is returned.
120 If Chr is contained in Str, then a pointer to the first occurance of Chr in Str is returned.
128 while (*Str
!= '\0' && *Str
!= Chr
) {
132 return (*Str
== Chr
) ? Str
: NULL
;
144 TODO: Add function description
148 Buffer - TODO: add argument description
149 Length - TODO: add argument description
153 TODO: add return values
157 if (Buffer
== NULL
|| Length
== 0) {
161 if (*(UINT8
*) Buffer
!= 0) {
166 if (!CompareMem (Buffer
, (UINT8
*) Buffer
+ 1, Length
- 1)) {
183 TODO: Add function description
187 Str - TODO: add argument description
188 Count - TODO: add argument description
192 TODO: add return values
198 if (StrLen (Str
) < Count
) {
203 for (Pointer
= Str
; *(Pointer
+ Count
); Pointer
++) {
204 *Pointer
= *(Pointer
+ Count
);
206 *Pointer
= *(Pointer
+ Count
);
214 WinNtSimpleFileSystemDriverBindingSupported (
215 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
216 IN EFI_HANDLE ControllerHandle
,
217 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
223 Check to see if the driver supports a given controller.
227 This - A pointer to an instance of the EFI_DRIVER_BINDING_PROTOCOL.
229 ControllerHandle - EFI handle of the controller to test.
231 RemainingDevicePath - Pointer to remaining portion of a device path.
235 EFI_SUCCESS - The device specified by ControllerHandle and RemainingDevicePath is supported by the driver
238 EFI_ALREADY_STARTED - The device specified by ControllerHandle and RemainingDevicePath is already being managed by
239 the driver specified by This.
241 EFI_ACCESS_DENIED - The device specified by ControllerHandle and RemainingDevicePath is already being managed by
242 a different driver or an application that requires exclusive access.
244 EFI_UNSUPPORTED - The device specified by ControllerHandle and RemainingDevicePath is not supported by the
245 driver specified by This.
250 EFI_WIN_NT_IO_PROTOCOL
*WinNtIo
;
253 // Open the IO Abstraction(s) needed to perform the supported test
255 Status
= gBS
->OpenProtocol (
257 &gEfiWinNtIoProtocolGuid
,
259 This
->DriverBindingHandle
,
261 EFI_OPEN_PROTOCOL_BY_DRIVER
263 if (EFI_ERROR (Status
)) {
268 // Make sure GUID is for a File System handle.
270 Status
= EFI_UNSUPPORTED
;
271 if (CompareGuid (WinNtIo
->TypeGuid
, &gEfiWinNtFileSystemGuid
)) {
272 Status
= EFI_SUCCESS
;
276 // Close the I/O Abstraction(s) used to perform the supported test
280 &gEfiWinNtIoProtocolGuid
,
281 This
->DriverBindingHandle
,
290 WinNtSimpleFileSystemDriverBindingStart (
291 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
292 IN EFI_HANDLE ControllerHandle
,
293 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
299 Starts a device controller or a bus controller.
303 This - A pointer to an instance of the EFI_DRIVER_BINDING_PROTOCOL.
305 ControllerHandle - EFI handle of the controller to start.
307 RemainingDevicePath - Pointer to remaining portion of a device path.
311 EFI_SUCCESS - The device or bus controller has been started.
313 EFI_DEVICE_ERROR - The device could not be started due to a device failure.
315 EFI_OUT_OF_RESOURCES - The request could not be completed due to lack of resources.
320 EFI_WIN_NT_IO_PROTOCOL
*WinNtIo
;
321 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE
*Private
;
326 // Open the IO Abstraction(s) needed
328 Status
= gBS
->OpenProtocol (
330 &gEfiWinNtIoProtocolGuid
,
332 This
->DriverBindingHandle
,
334 EFI_OPEN_PROTOCOL_BY_DRIVER
336 if (EFI_ERROR (Status
)) {
343 if (!CompareGuid (WinNtIo
->TypeGuid
, &gEfiWinNtFileSystemGuid
)) {
344 Status
= EFI_UNSUPPORTED
;
348 Private
= AllocatePool (sizeof (WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE
));
349 if (Private
== NULL
) {
350 Status
= EFI_OUT_OF_RESOURCES
;
355 Private
->Signature
= WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_SIGNATURE
;
356 Private
->WinNtThunk
= WinNtIo
->WinNtThunk
;
358 Private
->FilePath
= WinNtIo
->EnvString
;
360 Private
->VolumeLabel
= AllocatePool (StrSize (L
"EFI_EMULATED"));
361 if (Private
->VolumeLabel
== NULL
) {
362 Status
= EFI_OUT_OF_RESOURCES
;
366 StrCpy (Private
->VolumeLabel
, L
"EFI_EMULATED");
368 Private
->SimpleFileSystem
.Revision
= EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION
;
369 Private
->SimpleFileSystem
.OpenVolume
= WinNtSimpleFileSystemOpenVolume
;
371 Private
->WinNtThunk
->SetErrorMode (SEM_FAILCRITICALERRORS
);
373 Private
->ControllerNameTable
= NULL
;
377 gWinNtSimpleFileSystemComponentName
.SupportedLanguages
,
378 &Private
->ControllerNameTable
,
384 gWinNtSimpleFileSystemComponentName2
.SupportedLanguages
,
385 &Private
->ControllerNameTable
,
391 Status
= gBS
->InstallMultipleProtocolInterfaces (
393 &gEfiSimpleFileSystemProtocolGuid
,
394 &Private
->SimpleFileSystem
,
399 if (EFI_ERROR (Status
)) {
401 if (Private
!= NULL
) {
403 FreeUnicodeStringTable (Private
->ControllerNameTable
);
410 &gEfiWinNtIoProtocolGuid
,
411 This
->DriverBindingHandle
,
421 WinNtSimpleFileSystemDriverBindingStop (
422 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
423 IN EFI_HANDLE ControllerHandle
,
424 IN UINTN NumberOfChildren
,
425 IN EFI_HANDLE
*ChildHandleBuffer
431 TODO: Add function description
435 This - A pointer to an instance of the EFI_DRIVER_BINDING_PROTOCOL.
437 ControllerHandle - A handle to the device to be stopped.
439 NumberOfChildren - The number of child device handles in ChildHandleBuffer.
441 ChildHandleBuffer - An array of child device handles to be freed.
445 EFI_SUCCESS - The device has been stopped.
447 EFI_DEVICE_ERROR - The device could not be stopped due to a device failure.
450 // TODO: EFI_UNSUPPORTED - add return value to function comment
453 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*SimpleFileSystem
;
454 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE
*Private
;
457 // Get our context back
459 Status
= gBS
->OpenProtocol (
461 &gEfiSimpleFileSystemProtocolGuid
,
462 (VOID
**) &SimpleFileSystem
,
463 This
->DriverBindingHandle
,
465 EFI_OPEN_PROTOCOL_GET_PROTOCOL
467 if (EFI_ERROR (Status
)) {
468 return EFI_UNSUPPORTED
;
471 Private
= WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (SimpleFileSystem
);
474 // Uninstall the Simple File System Protocol from ControllerHandle
476 Status
= gBS
->UninstallMultipleProtocolInterfaces (
478 &gEfiSimpleFileSystemProtocolGuid
,
479 &Private
->SimpleFileSystem
,
482 if (!EFI_ERROR (Status
)) {
483 Status
= gBS
->CloseProtocol (
485 &gEfiWinNtIoProtocolGuid
,
486 This
->DriverBindingHandle
,
491 if (!EFI_ERROR (Status
)) {
493 // Free our instance data
495 FreeUnicodeStringTable (Private
->ControllerNameTable
);
505 WinNtSimpleFileSystemOpenVolume (
506 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
507 OUT EFI_FILE_PROTOCOL
**Root
513 Open the root directory on a volume.
517 This - A pointer to the volume to open.
519 Root - A pointer to storage for the returned opened file handle of the root directory.
523 EFI_SUCCESS - The volume was opened.
525 EFI_UNSUPPORTED - The volume does not support the requested file system type.
527 EFI_NO_MEDIA - The device has no media.
529 EFI_DEVICE_ERROR - The device reported an error.
531 EFI_VOLUME_CORRUPTED - The file system structures are corrupted.
533 EFI_ACCESS_DENIED - The service denied access to the file.
535 EFI_OUT_OF_RESOURCES - The file volume could not be opened due to lack of resources.
537 EFI_MEDIA_CHANGED - The device has new media or the media is no longer supported.
540 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
543 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE
*Private
;
544 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
546 CHAR16
*TempFileName
;
548 if (This
== NULL
|| Root
== NULL
) {
549 return EFI_INVALID_PARAMETER
;
552 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
554 Private
= WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (This
);
556 PrivateFile
= AllocatePool (sizeof (WIN_NT_EFI_FILE_PRIVATE
));
557 if (PrivateFile
== NULL
) {
558 Status
= EFI_OUT_OF_RESOURCES
;
562 PrivateFile
->FileName
= AllocatePool (StrSize (Private
->FilePath
));
563 if (PrivateFile
->FileName
== NULL
) {
564 Status
= EFI_OUT_OF_RESOURCES
;
568 PrivateFile
->FilePath
= AllocatePool (StrSize (Private
->FilePath
));
569 if (PrivateFile
->FilePath
== NULL
) {
570 Status
= EFI_OUT_OF_RESOURCES
;
574 StrCpy (PrivateFile
->FilePath
, Private
->FilePath
);
575 StrCpy (PrivateFile
->FileName
, PrivateFile
->FilePath
);
576 PrivateFile
->Signature
= WIN_NT_EFI_FILE_PRIVATE_SIGNATURE
;
577 PrivateFile
->WinNtThunk
= Private
->WinNtThunk
;
578 PrivateFile
->SimpleFileSystem
= This
;
579 PrivateFile
->IsRootDirectory
= TRUE
;
580 PrivateFile
->IsDirectoryPath
= TRUE
;
581 PrivateFile
->IsOpenedByRead
= TRUE
;
582 PrivateFile
->EfiFile
.Revision
= EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION
;
583 PrivateFile
->EfiFile
.Open
= WinNtSimpleFileSystemOpen
;
584 PrivateFile
->EfiFile
.Close
= WinNtSimpleFileSystemClose
;
585 PrivateFile
->EfiFile
.Delete
= WinNtSimpleFileSystemDelete
;
586 PrivateFile
->EfiFile
.Read
= WinNtSimpleFileSystemRead
;
587 PrivateFile
->EfiFile
.Write
= WinNtSimpleFileSystemWrite
;
588 PrivateFile
->EfiFile
.GetPosition
= WinNtSimpleFileSystemGetPosition
;
589 PrivateFile
->EfiFile
.SetPosition
= WinNtSimpleFileSystemSetPosition
;
590 PrivateFile
->EfiFile
.GetInfo
= WinNtSimpleFileSystemGetInfo
;
591 PrivateFile
->EfiFile
.SetInfo
= WinNtSimpleFileSystemSetInfo
;
592 PrivateFile
->EfiFile
.Flush
= WinNtSimpleFileSystemFlush
;
593 PrivateFile
->IsValidFindBuf
= FALSE
;
598 PrivateFile
->DirHandle
= PrivateFile
->WinNtThunk
->CreateFile (
599 PrivateFile
->FilePath
,
601 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
604 FILE_FLAG_BACKUP_SEMANTICS
,
608 if (PrivateFile
->DirHandle
== INVALID_HANDLE_VALUE
) {
609 Status
= EFI_NOT_FOUND
;
614 // Find the first file under it
616 Status
= gBS
->AllocatePool (
618 StrSize (PrivateFile
->FilePath
) + StrSize (L
"\\*"),
621 if (EFI_ERROR (Status
)) {
624 StrCpy (TempFileName
, PrivateFile
->FilePath
);
625 StrCat (TempFileName
, L
"\\*");
627 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->FindFirstFile (TempFileName
, &PrivateFile
->FindBuf
);
629 if (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
630 PrivateFile
->IsValidFindBuf
= FALSE
;
632 PrivateFile
->IsValidFindBuf
= TRUE
;
634 *Root
= &PrivateFile
->EfiFile
;
636 Status
= EFI_SUCCESS
;
639 if (EFI_ERROR (Status
)) {
641 if (PrivateFile
->FileName
) {
642 FreePool (PrivateFile
->FileName
);
645 if (PrivateFile
->FilePath
) {
646 FreePool (PrivateFile
->FilePath
);
649 FreePool (PrivateFile
);
653 gBS
->RestoreTPL (OldTpl
);
659 Count the number of Leading Dot in FileNameToken.
661 @param FileNameToken A string representing a token in the path name.
663 @return UINTN The number of leading dot in the name.
668 IN CONST CHAR16
* FileNameToken
674 while (*FileNameToken
== L
'.') {
683 IsFileNameTokenValid (
684 IN CONST CHAR16
* FileNameToken
688 if (StrStr (FileNameToken
, L
"/") != NULL
) {
690 // No L'/' in file name.
695 // If Token has all dot, the number should not exceed 2
697 Num
= CountLeadingDots (FileNameToken
);
699 if (Num
== StrLen (FileNameToken
)) {
701 // If the FileNameToken only contains a number of L'.'.
713 Return the first string token found in the indirect pointer a String named by FileName.
715 On input, FileName is a indirect pointer pointing to a String.
716 On output, FileName is a updated to point to the next character after the first
717 found L"\" or NULL if there is no L"\" found.
719 @param FileName A indirect pointer pointing to a FileName.
721 @return Token The first string token found before a L"\".
725 GetNextFileNameToken (
726 IN OUT CONST CHAR16
** FileName
732 ASSERT (**FileName
!= L
'\\');
733 ASSERT (**FileName
!= L
'\0');
735 SlashPos
= StrStr (*FileName
, L
"\\");
736 if (SlashPos
== NULL
) {
737 Token
= AllocateCopyPool (StrSize(*FileName
), *FileName
);
740 Offset
= SlashPos
- *FileName
;
741 Token
= AllocateZeroPool ((Offset
+ 1) * sizeof (CHAR16
));
742 StrnCpy (Token
, *FileName
, Offset
);
744 // Point *FileName to the next character after L'\'.
746 *FileName
= *FileName
+ Offset
+ 1;
753 Check if a FileName contains only Valid Characters.
755 If FileName contains only a single L'\', return TRUE.
756 If FileName contains two adjacent L'\', return FALSE.
757 If FileName conatins L'/' , return FALSE.
758 If FielName contains more than two dots seperated with other FileName characters
759 by L'\', return FALSE. For example, L'.\...\filename.txt' is invalid path name. But L'..TwoDots\filename.txt' is valid path name.
761 @param FileName The File Name String to check.
763 @return TRUE FileName only contains valid characters.
764 @return FALSE FileName contains at least one invalid character.
770 IN CONST CHAR16
*FileName
777 // If FileName is just L'\', then it is a valid pathname.
779 if (StrCmp (FileName
, L
"\\") == 0) {
783 // We don't support two or more adjacent L'\'.
785 if (StrStr (FileName
, L
"\\\\") != NULL
) {
790 // Is FileName has a leading L"\", skip to next character.
792 if (FileName
[0] == L
'\\') {
797 Token
= GetNextFileNameToken (&FileName
);
798 Valid
= IsFileNameTokenValid (Token
);
803 } while (FileName
!= NULL
);
810 WinNtSimpleFileSystemOpen (
811 IN EFI_FILE_PROTOCOL
*This
,
812 OUT EFI_FILE_PROTOCOL
**NewHandle
,
821 Open a file relative to the source file location.
825 This - A pointer to the source file location.
827 NewHandle - Pointer to storage for the new file handle.
829 FileName - Pointer to the file name to be opened.
831 OpenMode - File open mode information.
833 Attributes - File creation attributes.
837 EFI_SUCCESS - The file was opened.
839 EFI_NOT_FOUND - The file could not be found in the volume.
841 EFI_NO_MEDIA - The device has no media.
843 EFI_MEDIA_CHANGED - The device has new media or the media is no longer supported.
845 EFI_DEVICE_ERROR - The device reported an error.
847 EFI_VOLUME_CORRUPTED - The file system structures are corrupted.
849 EFI_WRITE_PROTECTED - The volume or file is write protected.
851 EFI_ACCESS_DENIED - The service denied access to the file.
853 EFI_OUT_OF_RESOURCES - Not enough resources were available to open the file.
855 EFI_VOLUME_FULL - There is not enough space left to create the new file.
858 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
859 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
860 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
861 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
863 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
864 WIN_NT_EFI_FILE_PRIVATE
*NewPrivateFile
;
865 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE
*PrivateRoot
;
867 CHAR16
*RealFileName
;
868 CHAR16
*TempFileName
;
869 CHAR16
*ParseFileName
;
870 CHAR16
*GuardPointer
;
880 // Check for obvious invalid parameters.
882 if (This
== NULL
|| NewHandle
== NULL
|| FileName
== NULL
) {
883 return EFI_INVALID_PARAMETER
;
887 case EFI_FILE_MODE_CREATE
| EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
:
888 if (Attributes
&~EFI_FILE_VALID_ATTR
) {
889 return EFI_INVALID_PARAMETER
;
892 if (Attributes
& EFI_FILE_READ_ONLY
) {
893 return EFI_INVALID_PARAMETER
;
899 case EFI_FILE_MODE_READ
:
900 case EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
:
904 return EFI_INVALID_PARAMETER
;
908 // Init local variables
910 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
911 PrivateRoot
= WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (PrivateFile
->SimpleFileSystem
);
912 NewPrivateFile
= NULL
;
915 // Allocate buffer for FileName as the passed in FileName may be read only
917 TempFileName
= AllocatePool (StrSize (FileName
));
918 if (TempFileName
== NULL
) {
919 return EFI_OUT_OF_RESOURCES
;
921 StrCpy (TempFileName
, FileName
);
922 FileName
= TempFileName
;
924 if (FileName
[StrLen (FileName
) - 1] == L
'\\') {
925 FileName
[StrLen (FileName
) - 1] = 0;
929 // If file name does not equal to "." or ".." and not trailed with "\..",
930 // then we trim the leading/trailing blanks and trailing dots
932 if (StrCmp (FileName
, L
".") != 0 && StrCmp (FileName
, L
"..") != 0 &&
933 ((StrLen (FileName
) >= 3) ? (StrCmp (&FileName
[StrLen (FileName
) - 3], L
"\\..") != 0) : TRUE
)) {
935 // Trim leading blanks
938 for (TempFileName
= FileName
;
939 *TempFileName
!= 0 && *TempFileName
== L
' ';
943 CutPrefix (FileName
, Count
);
945 // Trim trailing blanks
947 for (TempFileName
= FileName
+ StrLen (FileName
) - 1;
948 TempFileName
>= FileName
&& (*TempFileName
== L
' ');
952 *(TempFileName
+ 1) = 0;
956 // Attempt to open the file
958 NewPrivateFile
= AllocatePool (sizeof (WIN_NT_EFI_FILE_PRIVATE
));
959 if (NewPrivateFile
== NULL
) {
960 Status
= EFI_OUT_OF_RESOURCES
;
964 CopyMem (NewPrivateFile
, PrivateFile
, sizeof (WIN_NT_EFI_FILE_PRIVATE
));
966 NewPrivateFile
->FilePath
= AllocatePool (StrSize (PrivateFile
->FileName
));
967 if (NewPrivateFile
->FilePath
== NULL
) {
968 Status
= EFI_OUT_OF_RESOURCES
;
972 if (PrivateFile
->IsDirectoryPath
) {
973 StrCpy (NewPrivateFile
->FilePath
, PrivateFile
->FileName
);
975 StrCpy (NewPrivateFile
->FilePath
, PrivateFile
->FilePath
);
978 Size
= StrSize (NewPrivateFile
->FilePath
);
979 Size
+= StrSize (L
"\\");
980 Size
+= StrSize (FileName
);
981 NewPrivateFile
->FileName
= AllocatePool (Size
);
982 if (NewPrivateFile
->FileName
== NULL
) {
983 Status
= EFI_OUT_OF_RESOURCES
;
987 if (*FileName
== L
'\\') {
988 StrCpy (NewPrivateFile
->FileName
, PrivateRoot
->FilePath
);
989 StrCat (NewPrivateFile
->FileName
, L
"\\");
990 StrCat (NewPrivateFile
->FileName
, FileName
+ 1);
992 StrCpy (NewPrivateFile
->FileName
, NewPrivateFile
->FilePath
);
993 if (StrCmp (FileName
, L
"") != 0) {
995 // In case the filename becomes empty, especially after trimming dots and blanks
997 StrCat (NewPrivateFile
->FileName
, L
"\\");
998 StrCat (NewPrivateFile
->FileName
, FileName
);
1002 if (!IsFileNameValid (NewPrivateFile
->FileName
)) {
1003 Status
= EFI_NOT_FOUND
;
1008 // Get rid of . and .., except leading . or ..
1012 // GuardPointer protect simplefilesystem root path not be destroyed
1014 GuardPointer
= NewPrivateFile
->FileName
+ StrLen (PrivateRoot
->FilePath
);
1018 while (!LoopFinish
) {
1022 for (ParseFileName
= GuardPointer
; *ParseFileName
; ParseFileName
++) {
1023 if (*ParseFileName
== L
'.' &&
1024 (*(ParseFileName
+ 1) == 0 || *(ParseFileName
+ 1) == L
'\\') &&
1025 *(ParseFileName
- 1) == L
'\\'
1031 CutPrefix (ParseFileName
- 1, 2);
1036 if (*ParseFileName
== L
'.' &&
1037 *(ParseFileName
+ 1) == L
'.' &&
1038 (*(ParseFileName
+ 2) == 0 || *(ParseFileName
+ 2) == L
'\\') &&
1039 *(ParseFileName
- 1) == L
'\\'
1045 while (ParseFileName
!= GuardPointer
) {
1048 if (*ParseFileName
== L
'\\') {
1054 // cut \.. and its left directory
1056 CutPrefix (ParseFileName
, Count
);
1063 RealFileName
= NewPrivateFile
->FileName
;
1064 while (EfiStrChr (RealFileName
, L
'\\') != NULL
) {
1065 RealFileName
= EfiStrChr (RealFileName
, L
'\\') + 1;
1069 if (RealFileName
!= NewPrivateFile
->FileName
) {
1070 TempChar
= *(RealFileName
- 1);
1071 *(RealFileName
- 1) = 0;
1074 FreePool (NewPrivateFile
->FilePath
);
1075 NewPrivateFile
->FilePath
= NULL
;
1076 NewPrivateFile
->FilePath
= AllocatePool (StrSize (NewPrivateFile
->FileName
));
1077 if (NewPrivateFile
->FilePath
== NULL
) {
1078 Status
= EFI_OUT_OF_RESOURCES
;
1082 StrCpy (NewPrivateFile
->FilePath
, NewPrivateFile
->FileName
);
1083 if (TempChar
!= 0) {
1084 *(RealFileName
- 1) = TempChar
;
1087 NewPrivateFile
->IsRootDirectory
= FALSE
;
1090 // Test whether file or directory
1092 if (OpenMode
& EFI_FILE_MODE_CREATE
) {
1093 if (Attributes
& EFI_FILE_DIRECTORY
) {
1094 NewPrivateFile
->IsDirectoryPath
= TRUE
;
1096 NewPrivateFile
->IsDirectoryPath
= FALSE
;
1099 NewPrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
1100 NewPrivateFile
->LHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1101 NewPrivateFile
->FileName
,
1103 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1110 if (NewPrivateFile
->LHandle
!= INVALID_HANDLE_VALUE
) {
1111 NewPrivateFile
->IsDirectoryPath
= FALSE
;
1112 NewPrivateFile
->WinNtThunk
->CloseHandle (NewPrivateFile
->LHandle
);
1114 NewPrivateFile
->IsDirectoryPath
= TRUE
;
1117 NewPrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
1120 if (OpenMode
& EFI_FILE_MODE_WRITE
) {
1121 NewPrivateFile
->IsOpenedByRead
= FALSE
;
1123 NewPrivateFile
->IsOpenedByRead
= TRUE
;
1126 Status
= EFI_SUCCESS
;
1129 // deal with directory
1131 if (NewPrivateFile
->IsDirectoryPath
) {
1133 Size
= StrSize (NewPrivateFile
->FileName
);
1134 Size
+= StrSize (L
"\\*");
1135 TempFileName
= AllocatePool (Size
);
1136 if (TempFileName
== NULL
) {
1137 Status
= EFI_OUT_OF_RESOURCES
;
1141 StrCpy (TempFileName
, NewPrivateFile
->FileName
);
1143 if ((OpenMode
& EFI_FILE_MODE_CREATE
)) {
1145 // Create a directory
1147 if (!NewPrivateFile
->WinNtThunk
->CreateDirectory (TempFileName
, NULL
)) {
1149 LastError
= PrivateFile
->WinNtThunk
->GetLastError ();
1150 if (LastError
!= ERROR_ALREADY_EXISTS
) {
1151 FreePool (TempFileName
);
1152 Status
= EFI_ACCESS_DENIED
;
1158 NewPrivateFile
->DirHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1160 NewPrivateFile
->IsOpenedByRead
? GENERIC_READ
: (GENERIC_READ
| GENERIC_WRITE
),
1161 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1164 FILE_FLAG_BACKUP_SEMANTICS
,
1168 if (NewPrivateFile
->DirHandle
== INVALID_HANDLE_VALUE
) {
1170 NewPrivateFile
->DirHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1173 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1176 FILE_FLAG_BACKUP_SEMANTICS
,
1180 if (NewPrivateFile
->DirHandle
!= INVALID_HANDLE_VALUE
) {
1181 NewPrivateFile
->WinNtThunk
->CloseHandle (NewPrivateFile
->DirHandle
);
1182 NewPrivateFile
->DirHandle
= INVALID_HANDLE_VALUE
;
1183 Status
= EFI_ACCESS_DENIED
;
1185 Status
= EFI_NOT_FOUND
;
1192 // Find the first file under it
1194 StrCat (TempFileName
, L
"\\*");
1195 NewPrivateFile
->LHandle
= NewPrivateFile
->WinNtThunk
->FindFirstFile (TempFileName
, &NewPrivateFile
->FindBuf
);
1197 if (NewPrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1198 NewPrivateFile
->IsValidFindBuf
= FALSE
;
1200 NewPrivateFile
->IsValidFindBuf
= TRUE
;
1206 if (!NewPrivateFile
->IsOpenedByRead
) {
1207 NewPrivateFile
->LHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1208 NewPrivateFile
->FileName
,
1209 GENERIC_READ
| GENERIC_WRITE
,
1210 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1212 (OpenMode
& EFI_FILE_MODE_CREATE
) ? OPEN_ALWAYS
: OPEN_EXISTING
,
1217 if (NewPrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1218 NewPrivateFile
->LHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1219 NewPrivateFile
->FileName
,
1221 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1228 if (NewPrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1229 Status
= EFI_NOT_FOUND
;
1231 Status
= EFI_ACCESS_DENIED
;
1232 NewPrivateFile
->WinNtThunk
->CloseHandle (NewPrivateFile
->LHandle
);
1233 NewPrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
1237 NewPrivateFile
->LHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1238 NewPrivateFile
->FileName
,
1240 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1247 if (NewPrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1248 Status
= EFI_NOT_FOUND
;
1253 if ((OpenMode
& EFI_FILE_MODE_CREATE
) && Status
== EFI_SUCCESS
) {
1255 // Set the attribute
1260 Status
= WinNtSimpleFileSystemGetInfo (&NewPrivateFile
->EfiFile
, &gEfiFileInfoGuid
, &InfoSize
, Info
);
1262 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1263 Status
= EFI_DEVICE_ERROR
;
1267 Info
= AllocatePool (InfoSize
);
1269 Status
= EFI_OUT_OF_RESOURCES
;
1273 Status
= WinNtSimpleFileSystemGetInfo (&NewPrivateFile
->EfiFile
, &gEfiFileInfoGuid
, &InfoSize
, Info
);
1275 if (EFI_ERROR (Status
)) {
1279 Info
->Attribute
= Attributes
;
1281 WinNtSimpleFileSystemSetInfo (&NewPrivateFile
->EfiFile
, &gEfiFileInfoGuid
, InfoSize
, Info
);
1285 FreePool (FileName
);
1287 if (EFI_ERROR (Status
)) {
1288 if (NewPrivateFile
) {
1289 if (NewPrivateFile
->FileName
) {
1290 FreePool (NewPrivateFile
->FileName
);
1293 if (NewPrivateFile
->FilePath
) {
1294 FreePool (NewPrivateFile
->FilePath
);
1297 FreePool (NewPrivateFile
);
1300 *NewHandle
= &NewPrivateFile
->EfiFile
;
1301 if (StrCmp (NewPrivateFile
->FileName
, PrivateRoot
->FilePath
) == 0) {
1302 NewPrivateFile
->IsRootDirectory
= TRUE
;
1311 WinNtSimpleFileSystemClose (
1312 IN EFI_FILE_PROTOCOL
*This
1316 Routine Description:
1318 Close the specified file handle.
1322 This - Pointer to a returned opened file handle.
1326 EFI_SUCCESS - The file handle has been closed.
1329 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1331 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1335 return EFI_INVALID_PARAMETER
;
1338 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1340 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1342 if (PrivateFile
->LHandle
!= INVALID_HANDLE_VALUE
) {
1343 if (PrivateFile
->IsDirectoryPath
) {
1344 PrivateFile
->WinNtThunk
->FindClose (PrivateFile
->LHandle
);
1346 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->LHandle
);
1349 PrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
1352 if (PrivateFile
->IsDirectoryPath
&& PrivateFile
->DirHandle
!= INVALID_HANDLE_VALUE
) {
1353 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->DirHandle
);
1354 PrivateFile
->DirHandle
= INVALID_HANDLE_VALUE
;
1357 if (PrivateFile
->FileName
) {
1358 FreePool (PrivateFile
->FileName
);
1361 FreePool (PrivateFile
);
1363 gBS
->RestoreTPL (OldTpl
);
1370 WinNtSimpleFileSystemDelete (
1371 IN EFI_FILE_PROTOCOL
*This
1375 Routine Description:
1377 Close and delete a file.
1381 This - Pointer to a returned opened file handle.
1385 EFI_SUCCESS - The file handle was closed and deleted.
1387 EFI_WARN_DELETE_FAILURE - The handle was closed but could not be deleted.
1390 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1393 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1397 return EFI_INVALID_PARAMETER
;
1400 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1402 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1404 Status
= EFI_WARN_DELETE_FAILURE
;
1406 if (PrivateFile
->IsDirectoryPath
) {
1407 if (PrivateFile
->LHandle
!= INVALID_HANDLE_VALUE
) {
1408 PrivateFile
->WinNtThunk
->FindClose (PrivateFile
->LHandle
);
1411 if (PrivateFile
->DirHandle
!= INVALID_HANDLE_VALUE
) {
1412 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->DirHandle
);
1413 PrivateFile
->DirHandle
= INVALID_HANDLE_VALUE
;
1416 if (PrivateFile
->WinNtThunk
->RemoveDirectory (PrivateFile
->FileName
)) {
1417 Status
= EFI_SUCCESS
;
1420 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->LHandle
);
1421 PrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
1423 if (!PrivateFile
->IsOpenedByRead
) {
1424 if (PrivateFile
->WinNtThunk
->DeleteFile (PrivateFile
->FileName
)) {
1425 Status
= EFI_SUCCESS
;
1430 FreePool (PrivateFile
->FileName
);
1431 FreePool (PrivateFile
);
1433 gBS
->RestoreTPL (OldTpl
);
1439 WinNtSystemTimeToEfiTime (
1440 IN SYSTEMTIME
*SystemTime
,
1441 IN TIME_ZONE_INFORMATION
*TimeZone
,
1446 Routine Description:
1448 TODO: Add function description
1452 SystemTime - TODO: add argument description
1453 TimeZone - TODO: add argument description
1454 Time - TODO: add argument description
1458 TODO: add return values
1462 Time
->Year
= (UINT16
) SystemTime
->wYear
;
1463 Time
->Month
= (UINT8
) SystemTime
->wMonth
;
1464 Time
->Day
= (UINT8
) SystemTime
->wDay
;
1465 Time
->Hour
= (UINT8
) SystemTime
->wHour
;
1466 Time
->Minute
= (UINT8
) SystemTime
->wMinute
;
1467 Time
->Second
= (UINT8
) SystemTime
->wSecond
;
1468 Time
->Nanosecond
= (UINT32
) SystemTime
->wMilliseconds
* 1000000;
1469 Time
->TimeZone
= (INT16
) TimeZone
->Bias
;
1471 if (TimeZone
->StandardDate
.wMonth
) {
1472 Time
->Daylight
= EFI_TIME_ADJUST_DAYLIGHT
;
1478 WinNtSimpleFileSystemRead (
1479 IN EFI_FILE_PROTOCOL
*This
,
1480 IN OUT UINTN
*BufferSize
,
1485 Routine Description:
1487 Read data from a file.
1491 This - Pointer to a returned open file handle.
1493 BufferSize - On input, the size of the Buffer. On output, the number of bytes stored in the Buffer.
1495 Buffer - Pointer to the first byte of the read Buffer.
1499 EFI_SUCCESS - The data was read.
1501 EFI_NO_MEDIA - The device has no media.
1503 EFI_DEVICE_ERROR - The device reported an error.
1505 EFI_VOLUME_CORRUPTED - The file system structures are corrupted.
1507 EFI_BUFFER_TOO_SMALL - The supplied buffer size was too small to store the current directory entry.
1508 *BufferSize has been updated with the size needed to complete the request.
1511 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1513 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1519 SYSTEMTIME SystemTime
;
1520 EFI_FILE_INFO
*Info
;
1522 TIME_ZONE_INFORMATION TimeZone
;
1523 EFI_FILE_INFO
*FileInfo
;
1529 if (This
== NULL
|| BufferSize
== NULL
) {
1530 return EFI_INVALID_PARAMETER
;
1533 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1535 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1537 if (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1538 Status
= EFI_DEVICE_ERROR
;
1542 if (!PrivateFile
->IsDirectoryPath
) {
1544 if (This
->GetPosition (This
, &Pos
) != EFI_SUCCESS
) {
1545 Status
= EFI_DEVICE_ERROR
;
1549 FileInfoSize
= SIZE_OF_EFI_FILE_SYSTEM_INFO
;
1550 FileInfo
= AllocatePool (FileInfoSize
);
1552 Status
= This
->GetInfo (
1559 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1560 FreePool (FileInfo
);
1561 FileInfo
= AllocatePool (FileInfoSize
);
1562 Status
= This
->GetInfo (
1570 if (EFI_ERROR (Status
)) {
1571 Status
= EFI_DEVICE_ERROR
;
1575 FileSize
= FileInfo
->FileSize
;
1577 FreePool (FileInfo
);
1579 if (Pos
>= FileSize
) {
1581 if (Pos
== FileSize
) {
1582 Status
= EFI_SUCCESS
;
1585 Status
= EFI_DEVICE_ERROR
;
1590 Status
= PrivateFile
->WinNtThunk
->ReadFile (
1591 PrivateFile
->LHandle
,
1594 (LPDWORD
)BufferSize
,
1596 ) ? EFI_SUCCESS
: EFI_DEVICE_ERROR
;
1601 // Read on a directory. Perform a find next
1603 if (!PrivateFile
->IsValidFindBuf
) {
1605 Status
= EFI_SUCCESS
;
1609 Size
= SIZE_OF_EFI_FILE_INFO
;
1611 NameSize
= StrSize (PrivateFile
->FindBuf
.cFileName
);
1613 ResultSize
= Size
+ NameSize
;
1615 Status
= EFI_BUFFER_TOO_SMALL
;
1617 if (*BufferSize
>= ResultSize
) {
1618 Status
= EFI_SUCCESS
;
1621 ZeroMem (Info
, ResultSize
);
1623 Info
->Size
= ResultSize
;
1625 PrivateFile
->WinNtThunk
->GetTimeZoneInformation (&TimeZone
);
1627 PrivateFile
->WinNtThunk
->FileTimeToLocalFileTime (
1628 &PrivateFile
->FindBuf
.ftCreationTime
,
1629 &PrivateFile
->FindBuf
.ftCreationTime
1632 PrivateFile
->WinNtThunk
->FileTimeToSystemTime (&PrivateFile
->FindBuf
.ftCreationTime
, &SystemTime
);
1634 WinNtSystemTimeToEfiTime (&SystemTime
, &TimeZone
, &Info
->CreateTime
);
1636 PrivateFile
->WinNtThunk
->FileTimeToLocalFileTime (
1637 &PrivateFile
->FindBuf
.ftLastWriteTime
,
1638 &PrivateFile
->FindBuf
.ftLastWriteTime
1641 PrivateFile
->WinNtThunk
->FileTimeToSystemTime (&PrivateFile
->FindBuf
.ftLastWriteTime
, &SystemTime
);
1643 WinNtSystemTimeToEfiTime (&SystemTime
, &TimeZone
, &Info
->ModificationTime
);
1645 Info
->FileSize
= PrivateFile
->FindBuf
.nFileSizeLow
;
1647 Info
->PhysicalSize
= PrivateFile
->FindBuf
.nFileSizeLow
;
1649 if (PrivateFile
->FindBuf
.dwFileAttributes
& FILE_ATTRIBUTE_ARCHIVE
) {
1650 Info
->Attribute
|= EFI_FILE_ARCHIVE
;
1653 if (PrivateFile
->FindBuf
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
) {
1654 Info
->Attribute
|= EFI_FILE_HIDDEN
;
1657 if (PrivateFile
->FindBuf
.dwFileAttributes
& FILE_ATTRIBUTE_SYSTEM
) {
1658 Info
->Attribute
|= EFI_FILE_SYSTEM
;
1661 if (PrivateFile
->FindBuf
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
1662 Info
->Attribute
|= EFI_FILE_READ_ONLY
;
1665 if (PrivateFile
->FindBuf
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
1666 Info
->Attribute
|= EFI_FILE_DIRECTORY
;
1669 NameSize
= NameSize
/ sizeof (WCHAR
);
1671 pw
= (WCHAR
*) (((CHAR8
*) Buffer
) + Size
);
1673 for (Index
= 0; Index
< NameSize
; Index
++) {
1674 pw
[Index
] = PrivateFile
->FindBuf
.cFileName
[Index
];
1677 if (PrivateFile
->WinNtThunk
->FindNextFile (PrivateFile
->LHandle
, &PrivateFile
->FindBuf
)) {
1678 PrivateFile
->IsValidFindBuf
= TRUE
;
1680 PrivateFile
->IsValidFindBuf
= FALSE
;
1684 *BufferSize
= ResultSize
;
1687 gBS
->RestoreTPL (OldTpl
);
1693 WinNtSimpleFileSystemWrite (
1694 IN EFI_FILE_PROTOCOL
*This
,
1695 IN OUT UINTN
*BufferSize
,
1700 Routine Description:
1702 Write data to a file.
1706 This - Pointer to an opened file handle.
1708 BufferSize - On input, the number of bytes in the Buffer to write to the file. On output, the number of bytes
1709 of data written to the file.
1711 Buffer - Pointer to the first by of data in the buffer to write to the file.
1715 EFI_SUCCESS - The data was written to the file.
1717 EFI_UNSUPPORTED - Writes to an open directory are not supported.
1719 EFI_NO_MEDIA - The device has no media.
1721 EFI_DEVICE_ERROR - The device reported an error.
1723 EFI_VOLUME_CORRUPTED - The file system structures are corrupt.
1725 EFI_WRITE_PROTECTED - The file, directory, volume, or device is write protected.
1727 EFI_ACCESS_DENIED - The file was opened read-only.
1729 EFI_VOLUME_FULL - The volume is full.
1732 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1734 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1738 if (This
== NULL
|| BufferSize
== NULL
|| Buffer
== NULL
) {
1739 return EFI_INVALID_PARAMETER
;
1742 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1744 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1746 if (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1747 Status
= EFI_DEVICE_ERROR
;
1751 if (PrivateFile
->IsDirectoryPath
) {
1752 Status
= EFI_UNSUPPORTED
;
1756 if (PrivateFile
->IsOpenedByRead
) {
1757 Status
= EFI_ACCESS_DENIED
;
1761 Status
= PrivateFile
->WinNtThunk
->WriteFile (
1762 PrivateFile
->LHandle
,
1765 (LPDWORD
)BufferSize
,
1767 ) ? EFI_SUCCESS
: EFI_DEVICE_ERROR
;
1770 gBS
->RestoreTPL (OldTpl
);
1774 // bugbug: need to access windows error reporting
1780 WinNtSimpleFileSystemSetPosition (
1781 IN EFI_FILE_PROTOCOL
*This
,
1786 Routine Description:
1788 Set a file's current position.
1792 This - Pointer to an opened file handle.
1794 Position - The byte position from the start of the file to set.
1798 EFI_SUCCESS - The file position has been changed.
1800 EFI_UNSUPPORTED - The seek request for non-zero is not supported for directories.
1803 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1806 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1814 return EFI_INVALID_PARAMETER
;
1817 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1819 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1821 if (PrivateFile
->IsDirectoryPath
) {
1822 if (Position
!= 0) {
1823 Status
= EFI_UNSUPPORTED
;
1827 Size
= StrSize (PrivateFile
->FileName
);
1828 Size
+= StrSize (L
"\\*");
1829 FileName
= AllocatePool (Size
);
1830 if (FileName
== NULL
) {
1831 Status
= EFI_OUT_OF_RESOURCES
;
1835 StrCpy (FileName
, PrivateFile
->FileName
);
1836 StrCat (FileName
, L
"\\*");
1838 if (PrivateFile
->LHandle
!= INVALID_HANDLE_VALUE
) {
1839 PrivateFile
->WinNtThunk
->FindClose (PrivateFile
->LHandle
);
1842 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->FindFirstFile (FileName
, &PrivateFile
->FindBuf
);
1844 if (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1845 PrivateFile
->IsValidFindBuf
= FALSE
;
1847 PrivateFile
->IsValidFindBuf
= TRUE
;
1850 FreePool (FileName
);
1852 Status
= (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) ? EFI_DEVICE_ERROR
: EFI_SUCCESS
;
1854 if (Position
== (UINT64
) -1) {
1855 PosLow
= PrivateFile
->WinNtThunk
->SetFilePointer (PrivateFile
->LHandle
, (ULONG
) 0, NULL
, FILE_END
);
1857 PosHigh
= (UINT32
) RShiftU64 (Position
, 32);
1859 PosLow
= PrivateFile
->WinNtThunk
->SetFilePointer (PrivateFile
->LHandle
, (ULONG
) Position
, (PLONG
)&PosHigh
, FILE_BEGIN
);
1862 Status
= (PosLow
== 0xFFFFFFFF) ? EFI_DEVICE_ERROR
: EFI_SUCCESS
;
1866 gBS
->RestoreTPL (OldTpl
);
1872 WinNtSimpleFileSystemGetPosition (
1873 IN EFI_FILE_PROTOCOL
*This
,
1874 OUT UINT64
*Position
1878 Routine Description:
1880 Get a file's current position.
1884 This - Pointer to an opened file handle.
1886 Position - Pointer to storage for the current position.
1890 EFI_SUCCESS - The file position has been reported.
1892 EFI_UNSUPPORTED - Not valid for directories.
1895 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1898 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1903 if (This
== NULL
|| Position
== NULL
) {
1904 return EFI_INVALID_PARAMETER
;
1907 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1908 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1913 if (PrivateFile
->IsDirectoryPath
) {
1915 Status
= EFI_UNSUPPORTED
;
1921 *Position
= PrivateFile
->WinNtThunk
->SetFilePointer (
1922 PrivateFile
->LHandle
,
1924 (PLONG
)&PositionHigh
,
1928 Status
= *Position
== 0xffffffff ? EFI_DEVICE_ERROR
: EFI_SUCCESS
;
1929 if (EFI_ERROR (Status
)) {
1933 PosHigh64
= PositionHigh
;
1934 *Position
+= LShiftU64 (PosHigh64
, 32);
1938 gBS
->RestoreTPL (OldTpl
);
1943 WinNtSimpleFileSystemFileInfo (
1944 IN WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
,
1945 IN OUT UINTN
*BufferSize
,
1950 Routine Description:
1952 TODO: Add function description
1956 PrivateFile - TODO: add argument description
1957 BufferSize - TODO: add argument description
1958 Buffer - TODO: add argument description
1962 TODO: add return values
1970 EFI_FILE_INFO
*Info
;
1971 BY_HANDLE_FILE_INFORMATION FileInfo
;
1972 SYSTEMTIME SystemTime
;
1973 CHAR16
*RealFileName
;
1974 CHAR16
*TempPointer
;
1976 Size
= SIZE_OF_EFI_FILE_INFO
;
1977 NameSize
= StrSize (PrivateFile
->FileName
);
1978 ResultSize
= Size
+ NameSize
;
1980 Status
= EFI_BUFFER_TOO_SMALL
;
1981 if (*BufferSize
>= ResultSize
) {
1982 Status
= EFI_SUCCESS
;
1985 ZeroMem (Info
, ResultSize
);
1987 Info
->Size
= ResultSize
;
1988 PrivateFile
->WinNtThunk
->GetFileInformationByHandle (
1989 PrivateFile
->IsDirectoryPath
? PrivateFile
->DirHandle
: PrivateFile
->LHandle
,
1992 Info
->FileSize
= FileInfo
.nFileSizeLow
;
1993 Info
->PhysicalSize
= Info
->FileSize
;
1995 PrivateFile
->WinNtThunk
->FileTimeToSystemTime (&FileInfo
.ftCreationTime
, &SystemTime
);
1996 Info
->CreateTime
.Year
= SystemTime
.wYear
;
1997 Info
->CreateTime
.Month
= (UINT8
) SystemTime
.wMonth
;
1998 Info
->CreateTime
.Day
= (UINT8
) SystemTime
.wDay
;
1999 Info
->CreateTime
.Hour
= (UINT8
) SystemTime
.wHour
;
2000 Info
->CreateTime
.Minute
= (UINT8
) SystemTime
.wMinute
;
2001 Info
->CreateTime
.Second
= (UINT8
) SystemTime
.wSecond
;
2003 PrivateFile
->WinNtThunk
->FileTimeToSystemTime (&FileInfo
.ftLastAccessTime
, &SystemTime
);
2004 Info
->LastAccessTime
.Year
= SystemTime
.wYear
;
2005 Info
->LastAccessTime
.Month
= (UINT8
) SystemTime
.wMonth
;
2006 Info
->LastAccessTime
.Day
= (UINT8
) SystemTime
.wDay
;
2007 Info
->LastAccessTime
.Hour
= (UINT8
) SystemTime
.wHour
;
2008 Info
->LastAccessTime
.Minute
= (UINT8
) SystemTime
.wMinute
;
2009 Info
->LastAccessTime
.Second
= (UINT8
) SystemTime
.wSecond
;
2011 PrivateFile
->WinNtThunk
->FileTimeToSystemTime (&FileInfo
.ftLastWriteTime
, &SystemTime
);
2012 Info
->ModificationTime
.Year
= SystemTime
.wYear
;
2013 Info
->ModificationTime
.Month
= (UINT8
) SystemTime
.wMonth
;
2014 Info
->ModificationTime
.Day
= (UINT8
) SystemTime
.wDay
;
2015 Info
->ModificationTime
.Hour
= (UINT8
) SystemTime
.wHour
;
2016 Info
->ModificationTime
.Minute
= (UINT8
) SystemTime
.wMinute
;
2017 Info
->ModificationTime
.Second
= (UINT8
) SystemTime
.wSecond
;
2019 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_ARCHIVE
) {
2020 Info
->Attribute
|= EFI_FILE_ARCHIVE
;
2023 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
) {
2024 Info
->Attribute
|= EFI_FILE_HIDDEN
;
2027 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
2028 Info
->Attribute
|= EFI_FILE_READ_ONLY
;
2031 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_SYSTEM
) {
2032 Info
->Attribute
|= EFI_FILE_SYSTEM
;
2035 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
2036 Info
->Attribute
|= EFI_FILE_DIRECTORY
;
2039 if (PrivateFile
->IsDirectoryPath
) {
2040 Info
->Attribute
|= EFI_FILE_DIRECTORY
;
2043 RealFileName
= PrivateFile
->FileName
;
2044 TempPointer
= RealFileName
;
2046 while (*TempPointer
) {
2047 if (*TempPointer
== '\\') {
2048 RealFileName
= TempPointer
+ 1;
2054 if (PrivateFile
->IsRootDirectory
) {
2055 *((CHAR8
*) Buffer
+ Size
) = 0;
2057 CopyMem ((CHAR8
*) Buffer
+ Size
, RealFileName
, NameSize
);
2061 *BufferSize
= ResultSize
;
2067 WinNtSimpleFileSystemGetInfo (
2068 IN EFI_FILE_PROTOCOL
*This
,
2069 IN EFI_GUID
*InformationType
,
2070 IN OUT UINTN
*BufferSize
,
2075 Routine Description:
2077 Return information about a file or volume.
2081 This - Pointer to an opened file handle.
2083 InformationType - GUID describing the type of information to be returned.
2085 BufferSize - On input, the size of the information buffer. On output, the number of bytes written to the
2088 Buffer - Pointer to the first byte of the information buffer.
2092 EFI_SUCCESS - The requested information has been written into the buffer.
2094 EFI_UNSUPPORTED - The InformationType is not known.
2096 EFI_NO_MEDIA - The device has no media.
2098 EFI_DEVICE_ERROR - The device reported an error.
2100 EFI_VOLUME_CORRUPTED - The file system structures are corrupt.
2102 EFI_BUFFER_TOO_SMALL - The buffer size was too small to contain the requested information. The buffer size has
2103 been updated with the size needed to complete the requested operation.
2106 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
2109 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
2110 EFI_FILE_SYSTEM_INFO
*FileSystemInfoBuffer
;
2111 UINT32 SectorsPerCluster
;
2112 UINT32 BytesPerSector
;
2113 UINT32 FreeClusters
;
2114 UINT32 TotalClusters
;
2115 UINT32 BytesPerCluster
;
2117 BOOLEAN DriveNameFound
;
2120 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE
*PrivateRoot
;
2123 if (This
== NULL
|| InformationType
== NULL
|| BufferSize
== NULL
) {
2124 return EFI_INVALID_PARAMETER
;
2127 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
2129 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
2130 PrivateRoot
= WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (PrivateFile
->SimpleFileSystem
);
2132 Status
= EFI_UNSUPPORTED
;
2134 if (CompareGuid (InformationType
, &gEfiFileInfoGuid
)) {
2135 Status
= WinNtSimpleFileSystemFileInfo (PrivateFile
, BufferSize
, Buffer
);
2138 if (CompareGuid (InformationType
, &gEfiFileSystemInfoGuid
)) {
2139 if (*BufferSize
< SIZE_OF_EFI_FILE_SYSTEM_INFO
+ StrSize (PrivateRoot
->VolumeLabel
)) {
2140 *BufferSize
= SIZE_OF_EFI_FILE_SYSTEM_INFO
+ StrSize (PrivateRoot
->VolumeLabel
);
2141 Status
= EFI_BUFFER_TOO_SMALL
;
2145 FileSystemInfoBuffer
= (EFI_FILE_SYSTEM_INFO
*) Buffer
;
2146 FileSystemInfoBuffer
->Size
= SIZE_OF_EFI_FILE_SYSTEM_INFO
+ StrSize (PrivateRoot
->VolumeLabel
);
2147 FileSystemInfoBuffer
->ReadOnly
= FALSE
;
2150 // Try to get the drive name
2152 DriveNameFound
= FALSE
;
2153 DriveName
= AllocatePool (StrSize (PrivateFile
->FilePath
) + 1);
2154 if (DriveName
== NULL
) {
2155 Status
= EFI_OUT_OF_RESOURCES
;
2159 StrCpy (DriveName
, PrivateFile
->FilePath
);
2160 for (Index
= 0; DriveName
[Index
] != 0 && DriveName
[Index
] != ':'; Index
++) {
2164 if (DriveName
[Index
] == ':') {
2165 DriveName
[Index
+ 1] = '\\';
2166 DriveName
[Index
+ 2] = 0;
2167 DriveNameFound
= TRUE
;
2168 } else if (DriveName
[0] == '\\' && DriveName
[1] == '\\') {
2169 for (Index
= 2; DriveName
[Index
] != 0 && DriveName
[Index
] != '\\'; Index
++) {
2173 if (DriveName
[Index
] == '\\') {
2174 DriveNameFound
= TRUE
;
2175 for (Index
++; DriveName
[Index
] != 0 && DriveName
[Index
] != '\\'; Index
++) {
2179 DriveName
[Index
] = '\\';
2180 DriveName
[Index
+ 1] = 0;
2185 // Try GetDiskFreeSpace first
2187 NtStatus
= PrivateFile
->WinNtThunk
->GetDiskFreeSpace (
2188 DriveNameFound
? DriveName
: NULL
,
2189 (LPDWORD
)&SectorsPerCluster
,
2190 (LPDWORD
)&BytesPerSector
,
2191 (LPDWORD
)&FreeClusters
,
2192 (LPDWORD
)&TotalClusters
2195 FreePool (DriveName
);
2202 BytesPerCluster
= BytesPerSector
* SectorsPerCluster
;
2203 FileSystemInfoBuffer
->VolumeSize
= MultU64x32 (TotalClusters
, BytesPerCluster
);
2204 FileSystemInfoBuffer
->FreeSpace
= MultU64x32 (FreeClusters
, BytesPerCluster
);
2205 FileSystemInfoBuffer
->BlockSize
= BytesPerCluster
;
2209 // try GetDiskFreeSpaceEx then
2211 FileSystemInfoBuffer
->BlockSize
= 0;
2212 NtStatus
= PrivateFile
->WinNtThunk
->GetDiskFreeSpaceEx (
2213 PrivateFile
->FilePath
,
2214 (PULARGE_INTEGER
) (&FileSystemInfoBuffer
->FreeSpace
),
2215 (PULARGE_INTEGER
) (&FileSystemInfoBuffer
->VolumeSize
),
2219 Status
= EFI_DEVICE_ERROR
;
2224 StrCpy ((CHAR16
*) FileSystemInfoBuffer
->VolumeLabel
, PrivateRoot
->VolumeLabel
);
2225 *BufferSize
= SIZE_OF_EFI_FILE_SYSTEM_INFO
+ StrSize (PrivateRoot
->VolumeLabel
);
2226 Status
= EFI_SUCCESS
;
2229 if (CompareGuid (InformationType
, &gEfiFileSystemVolumeLabelInfoIdGuid
)) {
2230 if (*BufferSize
< StrSize (PrivateRoot
->VolumeLabel
)) {
2231 *BufferSize
= StrSize (PrivateRoot
->VolumeLabel
);
2232 Status
= EFI_BUFFER_TOO_SMALL
;
2236 StrCpy ((CHAR16
*) Buffer
, PrivateRoot
->VolumeLabel
);
2237 *BufferSize
= StrSize (PrivateRoot
->VolumeLabel
);
2238 Status
= EFI_SUCCESS
;
2242 gBS
->RestoreTPL (OldTpl
);
2248 WinNtSimpleFileSystemSetInfo (
2249 IN EFI_FILE_PROTOCOL
*This
,
2250 IN EFI_GUID
*InformationType
,
2251 IN UINTN BufferSize
,
2256 Routine Description:
2258 Set information about a file or volume.
2262 This - Pointer to an opened file handle.
2264 InformationType - GUID identifying the type of information to set.
2266 BufferSize - Number of bytes of data in the information buffer.
2268 Buffer - Pointer to the first byte of data in the information buffer.
2272 EFI_SUCCESS - The file or volume information has been updated.
2274 EFI_UNSUPPORTED - The information identifier is not recognised.
2276 EFI_NO_MEDIA - The device has no media.
2278 EFI_DEVICE_ERROR - The device reported an error.
2280 EFI_VOLUME_CORRUPTED - The file system structures are corrupt.
2282 EFI_WRITE_PROTECTED - The file, directory, volume, or device is write protected.
2284 EFI_ACCESS_DENIED - The file was opened read-only.
2286 EFI_VOLUME_FULL - The volume is full.
2288 EFI_BAD_BUFFER_SIZE - The buffer size is smaller than the type indicated by InformationType.
2291 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
2292 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
2294 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE
*PrivateRoot
;
2295 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
2296 EFI_FILE_INFO
*OldFileInfo
;
2297 EFI_FILE_INFO
*NewFileInfo
;
2303 CHAR16
*OldFileName
;
2304 CHAR16
*NewFileName
;
2305 CHAR16
*TempFileName
;
2306 CHAR16
*CharPointer
;
2307 BOOLEAN AttrChangeFlag
;
2308 BOOLEAN NameChangeFlag
;
2309 BOOLEAN SizeChangeFlag
;
2310 BOOLEAN TimeChangeFlag
;
2312 SYSTEMTIME NewCreationSystemTime
;
2313 SYSTEMTIME NewLastAccessSystemTime
;
2314 SYSTEMTIME NewLastWriteSystemTime
;
2315 FILETIME NewCreationFileTime
;
2316 FILETIME NewLastAccessFileTime
;
2317 FILETIME NewLastWriteFileTime
;
2318 WIN32_FIND_DATA FindBuf
;
2319 EFI_FILE_SYSTEM_INFO
*NewFileSystemInfo
;
2324 // Check for invalid parameters.
2326 if (This
== NULL
|| InformationType
== NULL
|| BufferSize
== 0 || Buffer
== NULL
) {
2327 return EFI_INVALID_PARAMETER
;
2330 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
2333 // Initialise locals.
2335 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
2336 PrivateRoot
= WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (PrivateFile
->SimpleFileSystem
);
2338 Status
= EFI_UNSUPPORTED
;
2339 OldFileInfo
= NewFileInfo
= NULL
;
2340 OldFileName
= NewFileName
= NULL
;
2341 AttrChangeFlag
= NameChangeFlag
= SizeChangeFlag
= TimeChangeFlag
= FALSE
;
2344 // Set file system information.
2346 if (CompareGuid (InformationType
, &gEfiFileSystemInfoGuid
)) {
2347 if (BufferSize
< SIZE_OF_EFI_FILE_SYSTEM_INFO
+ StrSize (PrivateRoot
->VolumeLabel
)) {
2348 Status
= EFI_BAD_BUFFER_SIZE
;
2352 NewFileSystemInfo
= (EFI_FILE_SYSTEM_INFO
*) Buffer
;
2354 FreePool (PrivateRoot
->VolumeLabel
);
2355 PrivateRoot
->VolumeLabel
= AllocatePool (StrSize (NewFileSystemInfo
->VolumeLabel
));
2356 if (PrivateRoot
->VolumeLabel
== NULL
) {
2357 Status
= EFI_OUT_OF_RESOURCES
;
2361 StrCpy (PrivateRoot
->VolumeLabel
, NewFileSystemInfo
->VolumeLabel
);
2363 Status
= EFI_SUCCESS
;
2368 // Set volume label information.
2370 if (CompareGuid (InformationType
, &gEfiFileSystemVolumeLabelInfoIdGuid
)) {
2371 if (BufferSize
< StrSize (PrivateRoot
->VolumeLabel
)) {
2372 Status
= EFI_BAD_BUFFER_SIZE
;
2376 StrCpy (PrivateRoot
->VolumeLabel
, (CHAR16
*) Buffer
);
2378 Status
= EFI_SUCCESS
;
2382 if (!CompareGuid (InformationType
, &gEfiFileInfoGuid
)) {
2383 Status
= EFI_UNSUPPORTED
;
2387 if (BufferSize
< SIZE_OF_EFI_FILE_INFO
) {
2388 Status
= EFI_BAD_BUFFER_SIZE
;
2393 // Set file/directory information.
2397 // Check for invalid set file information parameters.
2399 NewFileInfo
= (EFI_FILE_INFO
*) Buffer
;
2401 if (NewFileInfo
->Size
<= sizeof (EFI_FILE_INFO
) ||
2402 (NewFileInfo
->Attribute
&~(EFI_FILE_VALID_ATTR
)) ||
2403 (sizeof (UINTN
) == 4 && NewFileInfo
->Size
> 0xFFFFFFFF)
2405 Status
= EFI_INVALID_PARAMETER
;
2410 // bugbug: - This is not safe. We need something like EfiStrMaxSize()
2411 // that would have an additional parameter that would be the size
2412 // of the string array just in case there are no NULL characters in
2413 // the string array.
2416 // Get current file information so we can determine what kind
2417 // of change request this is.
2420 Status
= WinNtSimpleFileSystemFileInfo (PrivateFile
, &OldInfoSize
, NULL
);
2422 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
2423 Status
= EFI_DEVICE_ERROR
;
2427 OldFileInfo
= AllocatePool (OldInfoSize
);
2428 if (OldFileInfo
== NULL
) {
2429 Status
= EFI_OUT_OF_RESOURCES
;
2433 Status
= WinNtSimpleFileSystemFileInfo (PrivateFile
, &OldInfoSize
, OldFileInfo
);
2435 if (EFI_ERROR (Status
)) {
2439 OldFileName
= AllocatePool (StrSize (PrivateFile
->FileName
));
2440 if (OldFileName
== NULL
) {
2441 Status
= EFI_OUT_OF_RESOURCES
;
2445 StrCpy (OldFileName
, PrivateFile
->FileName
);
2448 // Make full pathname from new filename and rootpath.
2450 if (NewFileInfo
->FileName
[0] == '\\') {
2451 Size
= StrSize (PrivateRoot
->FilePath
);
2452 Size
+= StrSize (L
"\\");
2453 Size
+= StrSize (NewFileInfo
->FileName
);
2454 NewFileName
= AllocatePool (Size
);
2455 if (NewFileName
== NULL
) {
2456 Status
= EFI_OUT_OF_RESOURCES
;
2460 StrCpy (NewFileName
, PrivateRoot
->FilePath
);
2461 StrCat (NewFileName
, L
"\\");
2462 StrCat (NewFileName
, NewFileInfo
->FileName
+ 1);
2464 Size
= StrSize (PrivateFile
->FilePath
);
2465 Size
+= StrSize (L
"\\");
2466 Size
+= StrSize (NewFileInfo
->FileName
);
2467 NewFileName
= AllocatePool (Size
);
2468 if (NewFileName
== NULL
) {
2469 Status
= EFI_OUT_OF_RESOURCES
;
2473 StrCpy (NewFileName
, PrivateFile
->FilePath
);
2474 StrCat (NewFileName
, L
"\\");
2475 StrCat (NewFileName
, NewFileInfo
->FileName
);
2479 // Is there an attribute change request?
2481 if (NewFileInfo
->Attribute
!= OldFileInfo
->Attribute
) {
2482 if ((NewFileInfo
->Attribute
& EFI_FILE_DIRECTORY
) != (OldFileInfo
->Attribute
& EFI_FILE_DIRECTORY
)) {
2483 Status
= EFI_INVALID_PARAMETER
;
2487 AttrChangeFlag
= TRUE
;
2491 // Is there a name change request?
2492 // bugbug: - Need EfiStrCaseCmp()
2494 if (StrCmp (NewFileInfo
->FileName
, OldFileInfo
->FileName
)) {
2495 NameChangeFlag
= TRUE
;
2499 // Is there a size change request?
2501 if (NewFileInfo
->FileSize
!= OldFileInfo
->FileSize
) {
2502 SizeChangeFlag
= TRUE
;
2506 // Is there a time stamp change request?
2508 if (!IsZero (&NewFileInfo
->CreateTime
, sizeof (EFI_TIME
)) &&
2509 CompareMem (&NewFileInfo
->CreateTime
, &OldFileInfo
->CreateTime
, sizeof (EFI_TIME
))
2511 TimeChangeFlag
= TRUE
;
2512 } else if (!IsZero (&NewFileInfo
->LastAccessTime
, sizeof (EFI_TIME
)) &&
2513 CompareMem (&NewFileInfo
->LastAccessTime
, &OldFileInfo
->LastAccessTime
, sizeof (EFI_TIME
))
2515 TimeChangeFlag
= TRUE
;
2516 } else if (!IsZero (&NewFileInfo
->ModificationTime
, sizeof (EFI_TIME
)) &&
2517 CompareMem (&NewFileInfo
->ModificationTime
, &OldFileInfo
->ModificationTime
, sizeof (EFI_TIME
))
2519 TimeChangeFlag
= TRUE
;
2523 // All done if there are no change requests being made.
2525 if (!(AttrChangeFlag
|| NameChangeFlag
|| SizeChangeFlag
|| TimeChangeFlag
)) {
2526 Status
= EFI_SUCCESS
;
2531 // Set file or directory information.
2533 OldAttr
= PrivateFile
->WinNtThunk
->GetFileAttributes (OldFileName
);
2538 if (NameChangeFlag
) {
2540 // Close the handles first
2542 if (PrivateFile
->IsOpenedByRead
) {
2543 Status
= EFI_ACCESS_DENIED
;
2547 for (CharPointer
= NewFileName
; *CharPointer
!= 0 && *CharPointer
!= L
'/'; CharPointer
++) {
2550 if (*CharPointer
!= 0) {
2551 Status
= EFI_ACCESS_DENIED
;
2555 if (PrivateFile
->LHandle
!= INVALID_HANDLE_VALUE
) {
2556 if (PrivateFile
->IsDirectoryPath
) {
2557 PrivateFile
->WinNtThunk
->FindClose (PrivateFile
->LHandle
);
2559 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->LHandle
);
2560 PrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
2564 if (PrivateFile
->IsDirectoryPath
&& PrivateFile
->DirHandle
!= INVALID_HANDLE_VALUE
) {
2565 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->DirHandle
);
2566 PrivateFile
->DirHandle
= INVALID_HANDLE_VALUE
;
2569 NtStatus
= PrivateFile
->WinNtThunk
->MoveFile (OldFileName
, NewFileName
);
2575 FreePool (PrivateFile
->FileName
);
2577 PrivateFile
->FileName
= AllocatePool (StrSize (NewFileName
));
2578 if (PrivateFile
->FileName
== NULL
) {
2579 Status
= EFI_OUT_OF_RESOURCES
;
2583 StrCpy (PrivateFile
->FileName
, NewFileName
);
2585 Size
= StrSize (NewFileName
);
2586 Size
+= StrSize (L
"\\*");
2587 TempFileName
= AllocatePool (Size
);
2589 StrCpy (TempFileName
, NewFileName
);
2591 if (!PrivateFile
->IsDirectoryPath
) {
2592 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->CreateFile (
2594 PrivateFile
->IsOpenedByRead
? GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
,
2595 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2602 FreePool (TempFileName
);
2605 // Flush buffers just in case
2607 if (PrivateFile
->WinNtThunk
->FlushFileBuffers (PrivateFile
->LHandle
) == 0) {
2608 Status
= EFI_DEVICE_ERROR
;
2612 PrivateFile
->DirHandle
= PrivateFile
->WinNtThunk
->CreateFile (
2614 PrivateFile
->IsOpenedByRead
? GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
,
2615 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2618 FILE_FLAG_BACKUP_SEMANTICS
,
2622 StrCat (TempFileName
, L
"\\*");
2623 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->FindFirstFile (TempFileName
, &FindBuf
);
2625 FreePool (TempFileName
);
2629 Status
= EFI_DEVICE_ERROR
;
2631 NtStatus
= PrivateFile
->WinNtThunk
->SetFileAttributes (OldFileName
, OldAttr
);
2637 Size
= StrSize (OldFileName
);
2638 Size
+= StrSize (L
"\\*");
2639 TempFileName
= AllocatePool (Size
);
2641 StrCpy (TempFileName
, OldFileName
);
2643 if (!PrivateFile
->IsDirectoryPath
) {
2644 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->CreateFile (
2646 PrivateFile
->IsOpenedByRead
? GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
,
2647 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2654 PrivateFile
->DirHandle
= PrivateFile
->WinNtThunk
->CreateFile (
2656 PrivateFile
->IsOpenedByRead
? GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
,
2657 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2660 FILE_FLAG_BACKUP_SEMANTICS
,
2664 StrCat (TempFileName
, L
"\\*");
2665 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->FindFirstFile (TempFileName
, &FindBuf
);
2668 FreePool (TempFileName
);
2678 if (SizeChangeFlag
) {
2679 if (PrivateFile
->IsDirectoryPath
) {
2680 Status
= EFI_UNSUPPORTED
;
2684 if (PrivateFile
->IsOpenedByRead
|| OldFileInfo
->Attribute
& EFI_FILE_READ_ONLY
) {
2685 Status
= EFI_ACCESS_DENIED
;
2689 Status
= This
->GetPosition (This
, &CurPos
);
2690 if (EFI_ERROR (Status
)) {
2694 Status
= This
->SetPosition (This
, NewFileInfo
->FileSize
);
2695 if (EFI_ERROR (Status
)) {
2699 if (PrivateFile
->WinNtThunk
->SetEndOfFile (PrivateFile
->LHandle
) == 0) {
2700 Status
= EFI_DEVICE_ERROR
;
2704 Status
= This
->SetPosition (This
, CurPos
);
2705 if (EFI_ERROR (Status
)) {
2713 if (TimeChangeFlag
) {
2715 NewCreationSystemTime
.wYear
= NewFileInfo
->CreateTime
.Year
;
2716 NewCreationSystemTime
.wMonth
= NewFileInfo
->CreateTime
.Month
;
2717 NewCreationSystemTime
.wDay
= NewFileInfo
->CreateTime
.Day
;
2718 NewCreationSystemTime
.wHour
= NewFileInfo
->CreateTime
.Hour
;
2719 NewCreationSystemTime
.wMinute
= NewFileInfo
->CreateTime
.Minute
;
2720 NewCreationSystemTime
.wSecond
= NewFileInfo
->CreateTime
.Second
;
2721 NewCreationSystemTime
.wMilliseconds
= 0;
2723 if (!PrivateFile
->WinNtThunk
->SystemTimeToFileTime (
2724 &NewCreationSystemTime
,
2725 &NewCreationFileTime
2730 NewLastAccessSystemTime
.wYear
= NewFileInfo
->LastAccessTime
.Year
;
2731 NewLastAccessSystemTime
.wMonth
= NewFileInfo
->LastAccessTime
.Month
;
2732 NewLastAccessSystemTime
.wDay
= NewFileInfo
->LastAccessTime
.Day
;
2733 NewLastAccessSystemTime
.wHour
= NewFileInfo
->LastAccessTime
.Hour
;
2734 NewLastAccessSystemTime
.wMinute
= NewFileInfo
->LastAccessTime
.Minute
;
2735 NewLastAccessSystemTime
.wSecond
= NewFileInfo
->LastAccessTime
.Second
;
2736 NewLastAccessSystemTime
.wMilliseconds
= 0;
2738 if (!PrivateFile
->WinNtThunk
->SystemTimeToFileTime (
2739 &NewLastAccessSystemTime
,
2740 &NewLastAccessFileTime
2745 NewLastWriteSystemTime
.wYear
= NewFileInfo
->ModificationTime
.Year
;
2746 NewLastWriteSystemTime
.wMonth
= NewFileInfo
->ModificationTime
.Month
;
2747 NewLastWriteSystemTime
.wDay
= NewFileInfo
->ModificationTime
.Day
;
2748 NewLastWriteSystemTime
.wHour
= NewFileInfo
->ModificationTime
.Hour
;
2749 NewLastWriteSystemTime
.wMinute
= NewFileInfo
->ModificationTime
.Minute
;
2750 NewLastWriteSystemTime
.wSecond
= NewFileInfo
->ModificationTime
.Second
;
2751 NewLastWriteSystemTime
.wMilliseconds
= 0;
2753 if (!PrivateFile
->WinNtThunk
->SystemTimeToFileTime (
2754 &NewLastWriteSystemTime
,
2755 &NewLastWriteFileTime
2760 if (!PrivateFile
->WinNtThunk
->SetFileTime (
2761 PrivateFile
->IsDirectoryPath
? PrivateFile
->DirHandle
: PrivateFile
->LHandle
,
2762 &NewCreationFileTime
,
2763 &NewLastAccessFileTime
,
2764 &NewLastWriteFileTime
2766 Status
= EFI_DEVICE_ERROR
;
2773 // No matter about AttrChangeFlag, Attribute must be set.
2774 // Because operation before may cause attribute change.
2778 if (NewFileInfo
->Attribute
& EFI_FILE_ARCHIVE
) {
2779 NewAttr
|= FILE_ATTRIBUTE_ARCHIVE
;
2781 NewAttr
&= ~FILE_ATTRIBUTE_ARCHIVE
;
2784 if (NewFileInfo
->Attribute
& EFI_FILE_HIDDEN
) {
2785 NewAttr
|= FILE_ATTRIBUTE_HIDDEN
;
2787 NewAttr
&= ~FILE_ATTRIBUTE_HIDDEN
;
2790 if (NewFileInfo
->Attribute
& EFI_FILE_SYSTEM
) {
2791 NewAttr
|= FILE_ATTRIBUTE_SYSTEM
;
2793 NewAttr
&= ~FILE_ATTRIBUTE_SYSTEM
;
2796 if (NewFileInfo
->Attribute
& EFI_FILE_READ_ONLY
) {
2797 NewAttr
|= FILE_ATTRIBUTE_READONLY
;
2799 NewAttr
&= ~FILE_ATTRIBUTE_READONLY
;
2802 NtStatus
= PrivateFile
->WinNtThunk
->SetFileAttributes (NewFileName
, NewAttr
);
2809 if (OldFileInfo
!= NULL
) {
2810 FreePool (OldFileInfo
);
2813 if (OldFileName
!= NULL
) {
2814 FreePool (OldFileName
);
2817 if (NewFileName
!= NULL
) {
2818 FreePool (NewFileName
);
2821 gBS
->RestoreTPL (OldTpl
);
2827 WinNtSimpleFileSystemFlush (
2828 IN EFI_FILE_PROTOCOL
*This
2832 Routine Description:
2834 Flush all modified data to the media.
2838 This - Pointer to an opened file handle.
2842 EFI_SUCCESS - The data has been flushed.
2844 EFI_NO_MEDIA - The device has no media.
2846 EFI_DEVICE_ERROR - The device reported an error.
2848 EFI_VOLUME_CORRUPTED - The file system structures have been corrupted.
2850 EFI_WRITE_PROTECTED - The file, directory, volume, or device is write protected.
2852 EFI_ACCESS_DENIED - The file was opened read-only.
2854 EFI_VOLUME_FULL - The volume is full.
2857 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
2859 BY_HANDLE_FILE_INFORMATION FileInfo
;
2860 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
2865 return EFI_INVALID_PARAMETER
;
2868 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
2870 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
2872 if (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
2873 Status
= EFI_DEVICE_ERROR
;
2877 if (PrivateFile
->IsDirectoryPath
) {
2878 Status
= EFI_SUCCESS
;
2882 if (PrivateFile
->IsOpenedByRead
) {
2883 Status
= EFI_ACCESS_DENIED
;
2887 PrivateFile
->WinNtThunk
->GetFileInformationByHandle (PrivateFile
->LHandle
, &FileInfo
);
2889 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
2890 Status
= EFI_ACCESS_DENIED
;
2894 Status
= PrivateFile
->WinNtThunk
->FlushFileBuffers (PrivateFile
->LHandle
) ? EFI_SUCCESS
: EFI_DEVICE_ERROR
;
2897 gBS
->RestoreTPL (OldTpl
);
2900 // bugbug: - Use Windows error reporting.