3 Copyright (c) 2006 - 2008, 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
,
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
,
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
;
547 if (This
== NULL
|| Root
== NULL
) {
548 return EFI_INVALID_PARAMETER
;
551 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
553 Private
= WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (This
);
555 PrivateFile
= AllocatePool (sizeof (WIN_NT_EFI_FILE_PRIVATE
));
556 if (PrivateFile
== NULL
) {
557 Status
= EFI_OUT_OF_RESOURCES
;
561 PrivateFile
->FileName
= AllocatePool (StrSize (Private
->FilePath
));
562 if (PrivateFile
->FileName
== NULL
) {
563 Status
= EFI_OUT_OF_RESOURCES
;
567 PrivateFile
->FilePath
= AllocatePool (StrSize (Private
->FilePath
));
568 if (PrivateFile
->FilePath
== NULL
) {
569 Status
= EFI_OUT_OF_RESOURCES
;
573 StrCpy (PrivateFile
->FilePath
, Private
->FilePath
);
574 StrCpy (PrivateFile
->FileName
, PrivateFile
->FilePath
);
575 PrivateFile
->Signature
= WIN_NT_EFI_FILE_PRIVATE_SIGNATURE
;
576 PrivateFile
->WinNtThunk
= Private
->WinNtThunk
;
577 PrivateFile
->SimpleFileSystem
= This
;
578 PrivateFile
->IsRootDirectory
= TRUE
;
579 PrivateFile
->IsDirectoryPath
= TRUE
;
580 PrivateFile
->IsOpenedByRead
= TRUE
;
581 PrivateFile
->EfiFile
.Revision
= EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION
;
582 PrivateFile
->EfiFile
.Open
= WinNtSimpleFileSystemOpen
;
583 PrivateFile
->EfiFile
.Close
= WinNtSimpleFileSystemClose
;
584 PrivateFile
->EfiFile
.Delete
= WinNtSimpleFileSystemDelete
;
585 PrivateFile
->EfiFile
.Read
= WinNtSimpleFileSystemRead
;
586 PrivateFile
->EfiFile
.Write
= WinNtSimpleFileSystemWrite
;
587 PrivateFile
->EfiFile
.GetPosition
= WinNtSimpleFileSystemGetPosition
;
588 PrivateFile
->EfiFile
.SetPosition
= WinNtSimpleFileSystemSetPosition
;
589 PrivateFile
->EfiFile
.GetInfo
= WinNtSimpleFileSystemGetInfo
;
590 PrivateFile
->EfiFile
.SetInfo
= WinNtSimpleFileSystemSetInfo
;
591 PrivateFile
->EfiFile
.Flush
= WinNtSimpleFileSystemFlush
;
592 PrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
593 PrivateFile
->DirHandle
= INVALID_HANDLE_VALUE
;
594 PrivateFile
->IsValidFindBuf
= FALSE
;
596 *Root
= &PrivateFile
->EfiFile
;
598 Status
= EFI_SUCCESS
;
601 if (EFI_ERROR (Status
)) {
603 if (PrivateFile
->FileName
) {
604 FreePool (PrivateFile
->FileName
);
607 if (PrivateFile
->FilePath
) {
608 FreePool (PrivateFile
->FilePath
);
611 FreePool (PrivateFile
);
615 gBS
->RestoreTPL (OldTpl
);
621 Count the number of Leading Dot in FileNameToken.
623 @param FileNameToken A string representing a token in the path name.
625 @return UINTN The number of leading dot in the name.
630 IN CONST CHAR16
* FileNameToken
636 while (*FileNameToken
== L
'.') {
645 IsFileNameTokenValid (
646 IN CONST CHAR16
* FileNameToken
650 if (StrStr (FileNameToken
, L
"/") != NULL
) {
652 // No L'/' in file name.
657 // If Token has all dot, the number should not exceed 2
659 Num
= CountLeadingDots (FileNameToken
);
661 if (Num
== StrLen (FileNameToken
)) {
663 // If the FileNameToken only contains a number of L'.'.
675 Return the first string token found in the indirect pointer a String named by FileName.
677 On input, FileName is a indirect pointer pointing to a String.
678 On output, FileName is a updated to point to the next character after the first
679 found L"\" or NULL if there is no L"\" found.
681 @param FileName A indirect pointer pointing to a FileName.
683 @return Token The first string token found before a L"\".
687 GetNextFileNameToken (
688 IN OUT CONST CHAR16
** FileName
694 ASSERT (**FileName
!= L
'\\');
695 ASSERT (**FileName
!= L
'\0');
697 SlashPos
= StrStr (*FileName
, L
"\\");
698 if (SlashPos
== NULL
) {
699 Token
= AllocateCopyPool (StrSize(*FileName
), *FileName
);
702 Offset
= SlashPos
- *FileName
;
703 Token
= AllocateZeroPool ((Offset
+ 1) * sizeof (CHAR16
));
704 StrnCpy (Token
, *FileName
, Offset
);
706 // Point *FileName to the next character after L'\'.
708 *FileName
= *FileName
+ Offset
+ 1;
715 Check if a FileName contains only Valid Characters.
717 If FileName contains only a single L'\', return TRUE.
718 If FileName contains two adjacent L'\', return FALSE.
719 If FileName conatins L'/' , return FALSE.
720 If FielName contains more than two dots seperated with other FileName characters
721 by L'\', return FALSE. For example, L'.\...\filename.txt' is invalid path name. But L'..TwoDots\filename.txt' is valid path name.
723 @param FileName The File Name String to check.
725 @return TRUE FileName only contains valid characters.
726 @return FALSE FileName contains at least one invalid character.
732 IN CONST CHAR16
*FileName
739 // If FileName is just L'\', then it is a valid pathname.
741 if (StrCmp (FileName
, L
"\\") == 0) {
745 // We don't support two or more adjacent L'\'.
747 if (StrStr (FileName
, L
"\\\\") != NULL
) {
752 // Is FileName has a leading L"\", skip to next character.
754 if (FileName
[0] == L
'\\') {
759 Token
= GetNextFileNameToken (&FileName
);
760 Valid
= IsFileNameTokenValid (Token
);
765 } while (FileName
!= NULL
);
772 WinNtSimpleFileSystemOpen (
774 OUT EFI_FILE
**NewHandle
,
783 Open a file relative to the source file location.
787 This - A pointer to the seource file location.
789 NewHandle - Pointer to storage for the new file handle.
791 FileName - Pointer to the file name to be opened.
793 OpenMode - File open mode information.
795 Attributes - File creation attributes.
799 EFI_SUCCESS - The file was opened.
801 EFI_NOT_FOUND - The file could not be found in the volume.
803 EFI_NO_MEDIA - The device has no media.
805 EFI_MEDIA_CHANGED - The device has new media or the media is no longer supported.
807 EFI_DEVICE_ERROR - The device reported an error.
809 EFI_VOLUME_CORRUPTED - The file system structures are corrupted.
811 EFI_WRITE_PROTECTED - The volume or file is write protected.
813 EFI_ACCESS_DENIED - The service denied access to the file.
815 EFI_OUT_OF_RESOURCES - Not enough resources were available to open the file.
817 EFI_VOLUME_FULL - There is not enough space left to create the new file.
820 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
821 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
822 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
823 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
826 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
827 WIN_NT_EFI_FILE_PRIVATE
*NewPrivateFile
;
828 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE
*PrivateRoot
;
830 CHAR16
*RealFileName
;
831 CHAR16
*TempFileName
;
832 CHAR16
*ParseFileName
;
833 CHAR16
*GuardPointer
;
842 // Check for obvious invalid parameters.
844 if (This
== NULL
|| NewHandle
== NULL
|| FileName
== NULL
) {
845 return EFI_INVALID_PARAMETER
;
849 case EFI_FILE_MODE_CREATE
| EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
:
850 if (Attributes
&~EFI_FILE_VALID_ATTR
) {
851 return EFI_INVALID_PARAMETER
;
854 if (Attributes
& EFI_FILE_READ_ONLY
) {
855 return EFI_INVALID_PARAMETER
;
861 case EFI_FILE_MODE_READ
:
862 case EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
:
866 return EFI_INVALID_PARAMETER
;
870 // Init local variables
872 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
873 PrivateRoot
= WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (PrivateFile
->SimpleFileSystem
);
874 NewPrivateFile
= NULL
;
877 // Allocate buffer for FileName as the passed in FileName may be read only
879 TempFileName
= AllocatePool (StrSize (FileName
));
880 if (TempFileName
== NULL
) {
881 return EFI_OUT_OF_RESOURCES
;
883 StrCpy (TempFileName
, FileName
);
884 FileName
= TempFileName
;
887 // BUGBUG: assume an open of root
888 // if current location, return current data
890 if (StrCmp (FileName
, L
"\\") == 0 || (StrCmp (FileName
, L
".") == 0 && PrivateFile
->IsRootDirectory
)) {
892 // BUGBUG: assume an open root
895 Status
= WinNtSimpleFileSystemOpenVolume (PrivateFile
->SimpleFileSystem
, &Root
);
896 NewPrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (Root
);
900 if (FileName
[StrLen (FileName
) - 1] == L
'\\') {
901 FileName
[StrLen (FileName
) - 1] = 0;
905 // If file name does not equal to "." or "..",
906 // then we trim the leading/trailing blanks and trailing dots
908 if (StrCmp (FileName
, L
".") != 0 && StrCmp (FileName
, L
"..") != 0) {
910 // Trim leading blanks
913 for (TempFileName
= FileName
;
914 *TempFileName
!= 0 && *TempFileName
== L
' ';
918 CutPrefix (FileName
, Count
);
920 // Trim trailing blanks
922 for (TempFileName
= FileName
+ StrLen (FileName
) - 1;
923 TempFileName
>= FileName
&& (*TempFileName
== L
' ');
927 *(TempFileName
+ 1) = 0;
931 // Attempt to open the file
933 NewPrivateFile
= AllocatePool (sizeof (WIN_NT_EFI_FILE_PRIVATE
));
934 if (NewPrivateFile
== NULL
) {
935 Status
= EFI_OUT_OF_RESOURCES
;
939 CopyMem (NewPrivateFile
, PrivateFile
, sizeof (WIN_NT_EFI_FILE_PRIVATE
));
941 NewPrivateFile
->FilePath
= AllocatePool (StrSize (PrivateFile
->FileName
));
942 if (NewPrivateFile
->FilePath
== NULL
) {
943 Status
= EFI_OUT_OF_RESOURCES
;
947 if (PrivateFile
->IsDirectoryPath
) {
948 StrCpy (NewPrivateFile
->FilePath
, PrivateFile
->FileName
);
950 StrCpy (NewPrivateFile
->FilePath
, PrivateFile
->FilePath
);
953 NewPrivateFile
->FileName
= AllocatePool (StrSize (NewPrivateFile
->FilePath
) + StrSize (L
"\\") + StrSize (FileName
));
954 if (NewPrivateFile
->FileName
== NULL
) {
955 Status
= EFI_OUT_OF_RESOURCES
;
959 if (*FileName
== L
'\\') {
960 StrCpy (NewPrivateFile
->FileName
, PrivateRoot
->FilePath
);
961 StrCat (NewPrivateFile
->FileName
, L
"\\");
962 StrCat (NewPrivateFile
->FileName
, FileName
+ 1);
964 StrCpy (NewPrivateFile
->FileName
, NewPrivateFile
->FilePath
);
965 if (StrCmp (FileName
, L
"") != 0) {
967 // In case the filename becomes empty, especially after trimming dots and blanks
969 StrCat (NewPrivateFile
->FileName
, L
"\\");
970 StrCat (NewPrivateFile
->FileName
, FileName
);
974 if (!IsFileNameValid (NewPrivateFile
->FileName
)) {
975 Status
= EFI_NOT_FOUND
;
980 // Get rid of . and .., except leading . or ..
984 // GuardPointer protect simplefilesystem root path not be destroyed
986 GuardPointer
= NewPrivateFile
->FileName
+ StrLen (PrivateRoot
->FilePath
);
990 while (!LoopFinish
) {
994 for (ParseFileName
= GuardPointer
; *ParseFileName
; ParseFileName
++) {
995 if (*ParseFileName
== L
'.' &&
996 (*(ParseFileName
+ 1) == 0 || *(ParseFileName
+ 1) == L
'\\') &&
997 *(ParseFileName
- 1) == L
'\\'
1003 CutPrefix (ParseFileName
- 1, 2);
1008 if (*ParseFileName
== L
'.' &&
1009 *(ParseFileName
+ 1) == L
'.' &&
1010 (*(ParseFileName
+ 2) == 0 || *(ParseFileName
+ 2) == L
'\\') &&
1011 *(ParseFileName
- 1) == L
'\\'
1017 while (ParseFileName
!= GuardPointer
) {
1020 if (*ParseFileName
== L
'\\') {
1026 // cut \.. and its left directory
1028 CutPrefix (ParseFileName
, Count
);
1035 if (StrCmp (NewPrivateFile
->FileName
, PrivateRoot
->FilePath
) == 0) {
1036 NewPrivateFile
->IsRootDirectory
= TRUE
;
1037 FreePool (NewPrivateFile
->FilePath
);
1038 FreePool (NewPrivateFile
->FileName
);
1039 FreePool (NewPrivateFile
);
1043 RealFileName
= NewPrivateFile
->FileName
;
1044 while (EfiStrChr (RealFileName
, L
'\\') != NULL
) {
1045 RealFileName
= EfiStrChr (RealFileName
, L
'\\') + 1;
1048 TempChar
= *(RealFileName
- 1);
1049 *(RealFileName
- 1) = 0;
1051 FreePool (NewPrivateFile
->FilePath
);
1052 NewPrivateFile
->FilePath
= NULL
;
1053 NewPrivateFile
->FilePath
= AllocatePool (StrSize (NewPrivateFile
->FileName
));
1054 if (NewPrivateFile
->FilePath
== NULL
) {
1055 Status
= EFI_OUT_OF_RESOURCES
;
1059 StrCpy (NewPrivateFile
->FilePath
, NewPrivateFile
->FileName
);
1061 *(RealFileName
- 1) = TempChar
;
1063 NewPrivateFile
->IsRootDirectory
= FALSE
;
1066 // Test whether file or directory
1068 if (OpenMode
& EFI_FILE_MODE_CREATE
) {
1069 if (Attributes
& EFI_FILE_DIRECTORY
) {
1070 NewPrivateFile
->IsDirectoryPath
= TRUE
;
1072 NewPrivateFile
->IsDirectoryPath
= FALSE
;
1075 NewPrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
1076 NewPrivateFile
->LHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1077 NewPrivateFile
->FileName
,
1079 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1086 if (NewPrivateFile
->LHandle
!= INVALID_HANDLE_VALUE
) {
1087 NewPrivateFile
->IsDirectoryPath
= FALSE
;
1088 NewPrivateFile
->WinNtThunk
->CloseHandle (NewPrivateFile
->LHandle
);
1090 NewPrivateFile
->IsDirectoryPath
= TRUE
;
1093 NewPrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
1096 if (OpenMode
& EFI_FILE_MODE_WRITE
) {
1097 NewPrivateFile
->IsOpenedByRead
= FALSE
;
1099 NewPrivateFile
->IsOpenedByRead
= TRUE
;
1102 Status
= EFI_SUCCESS
;
1105 // deal with directory
1107 if (NewPrivateFile
->IsDirectoryPath
) {
1109 TempFileName
= AllocatePool (StrSize (NewPrivateFile
->FileName
) + StrSize (L
"\\*"));
1110 if (TempFileName
== NULL
) {
1111 Status
= EFI_OUT_OF_RESOURCES
;
1115 StrCpy (TempFileName
, NewPrivateFile
->FileName
);
1117 if ((OpenMode
& EFI_FILE_MODE_CREATE
)) {
1119 // Create a directory
1121 if (!NewPrivateFile
->WinNtThunk
->CreateDirectory (TempFileName
, NULL
)) {
1123 LastError
= PrivateFile
->WinNtThunk
->GetLastError ();
1124 if (LastError
!= ERROR_ALREADY_EXISTS
) {
1125 FreePool (TempFileName
);
1126 Status
= EFI_ACCESS_DENIED
;
1132 NewPrivateFile
->DirHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1134 NewPrivateFile
->IsOpenedByRead
? GENERIC_READ
: (GENERIC_READ
| GENERIC_WRITE
),
1135 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1138 FILE_FLAG_BACKUP_SEMANTICS
,
1142 if (NewPrivateFile
->DirHandle
== INVALID_HANDLE_VALUE
) {
1144 NewPrivateFile
->DirHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1147 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1150 FILE_FLAG_BACKUP_SEMANTICS
,
1154 if (NewPrivateFile
->DirHandle
!= INVALID_HANDLE_VALUE
) {
1155 NewPrivateFile
->WinNtThunk
->CloseHandle (NewPrivateFile
->DirHandle
);
1156 NewPrivateFile
->DirHandle
= INVALID_HANDLE_VALUE
;
1157 Status
= EFI_ACCESS_DENIED
;
1159 Status
= EFI_NOT_FOUND
;
1166 // Find the first file under it
1168 StrCat (TempFileName
, L
"\\*");
1169 NewPrivateFile
->LHandle
= NewPrivateFile
->WinNtThunk
->FindFirstFile (TempFileName
, &NewPrivateFile
->FindBuf
);
1171 if (NewPrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1172 NewPrivateFile
->IsValidFindBuf
= FALSE
;
1174 NewPrivateFile
->IsValidFindBuf
= TRUE
;
1180 if (!NewPrivateFile
->IsOpenedByRead
) {
1181 NewPrivateFile
->LHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1182 NewPrivateFile
->FileName
,
1183 GENERIC_READ
| GENERIC_WRITE
,
1184 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1186 (OpenMode
& EFI_FILE_MODE_CREATE
) ? OPEN_ALWAYS
: OPEN_EXISTING
,
1191 if (NewPrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1192 NewPrivateFile
->LHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1193 NewPrivateFile
->FileName
,
1195 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1202 if (NewPrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1203 Status
= EFI_NOT_FOUND
;
1205 Status
= EFI_ACCESS_DENIED
;
1206 NewPrivateFile
->WinNtThunk
->CloseHandle (NewPrivateFile
->LHandle
);
1207 NewPrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
1211 NewPrivateFile
->LHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1212 NewPrivateFile
->FileName
,
1214 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1221 if (NewPrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1222 Status
= EFI_NOT_FOUND
;
1227 if ((OpenMode
& EFI_FILE_MODE_CREATE
) && Status
== EFI_SUCCESS
) {
1229 // Set the attribute
1234 Status
= WinNtSimpleFileSystemGetInfo (&NewPrivateFile
->EfiFile
, &gEfiFileInfoGuid
, &InfoSize
, Info
);
1236 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1237 Status
= EFI_DEVICE_ERROR
;
1241 Info
= AllocatePool (InfoSize
);
1243 Status
= EFI_OUT_OF_RESOURCES
;
1247 Status
= WinNtSimpleFileSystemGetInfo (&NewPrivateFile
->EfiFile
, &gEfiFileInfoGuid
, &InfoSize
, Info
);
1249 if (EFI_ERROR (Status
)) {
1253 Info
->Attribute
= Attributes
;
1255 WinNtSimpleFileSystemSetInfo (&NewPrivateFile
->EfiFile
, &gEfiFileInfoGuid
, InfoSize
, Info
);
1259 FreePool (FileName
);
1261 if (EFI_ERROR (Status
)) {
1262 if (NewPrivateFile
) {
1263 if (NewPrivateFile
->FileName
) {
1264 FreePool (NewPrivateFile
->FileName
);
1267 if (NewPrivateFile
->FilePath
) {
1268 FreePool (NewPrivateFile
->FilePath
);
1271 FreePool (NewPrivateFile
);
1274 *NewHandle
= &NewPrivateFile
->EfiFile
;
1282 WinNtSimpleFileSystemClose (
1287 Routine Description:
1289 Close the specified file handle.
1293 This - Pointer to a returned opened file handle.
1297 EFI_SUCCESS - The file handle has been closed.
1300 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1302 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1306 return EFI_INVALID_PARAMETER
;
1309 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1311 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1313 if (PrivateFile
->LHandle
!= INVALID_HANDLE_VALUE
) {
1314 if (PrivateFile
->IsDirectoryPath
) {
1315 PrivateFile
->WinNtThunk
->FindClose (PrivateFile
->LHandle
);
1317 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->LHandle
);
1320 PrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
1323 if (PrivateFile
->IsDirectoryPath
&& PrivateFile
->DirHandle
!= INVALID_HANDLE_VALUE
) {
1324 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->DirHandle
);
1325 PrivateFile
->DirHandle
= INVALID_HANDLE_VALUE
;
1328 if (PrivateFile
->FileName
) {
1329 FreePool (PrivateFile
->FileName
);
1332 FreePool (PrivateFile
);
1334 gBS
->RestoreTPL (OldTpl
);
1341 WinNtSimpleFileSystemDelete (
1346 Routine Description:
1348 Close and delete a file.
1352 This - Pointer to a returned opened file handle.
1356 EFI_SUCCESS - The file handle was closed and deleted.
1358 EFI_WARN_DELETE_FAILURE - The handle was closed but could not be deleted.
1361 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1364 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1368 return EFI_INVALID_PARAMETER
;
1371 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1373 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1375 Status
= EFI_WARN_DELETE_FAILURE
;
1377 if (PrivateFile
->IsDirectoryPath
) {
1378 if (PrivateFile
->LHandle
!= INVALID_HANDLE_VALUE
) {
1379 PrivateFile
->WinNtThunk
->FindClose (PrivateFile
->LHandle
);
1382 if (PrivateFile
->DirHandle
!= INVALID_HANDLE_VALUE
) {
1383 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->DirHandle
);
1384 PrivateFile
->DirHandle
= INVALID_HANDLE_VALUE
;
1387 if (PrivateFile
->WinNtThunk
->RemoveDirectory (PrivateFile
->FileName
)) {
1388 Status
= EFI_SUCCESS
;
1391 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->LHandle
);
1392 PrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
1394 if (!PrivateFile
->IsOpenedByRead
) {
1395 if (PrivateFile
->WinNtThunk
->DeleteFile (PrivateFile
->FileName
)) {
1396 Status
= EFI_SUCCESS
;
1401 FreePool (PrivateFile
->FileName
);
1402 FreePool (PrivateFile
);
1404 gBS
->RestoreTPL (OldTpl
);
1411 WinNtSystemTimeToEfiTime (
1412 IN SYSTEMTIME
*SystemTime
,
1413 IN TIME_ZONE_INFORMATION
*TimeZone
,
1418 Routine Description:
1420 TODO: Add function description
1424 SystemTime - TODO: add argument description
1425 TimeZone - TODO: add argument description
1426 Time - TODO: add argument description
1430 TODO: add return values
1434 Time
->Year
= (UINT16
) SystemTime
->wYear
;
1435 Time
->Month
= (UINT8
) SystemTime
->wMonth
;
1436 Time
->Day
= (UINT8
) SystemTime
->wDay
;
1437 Time
->Hour
= (UINT8
) SystemTime
->wHour
;
1438 Time
->Minute
= (UINT8
) SystemTime
->wMinute
;
1439 Time
->Second
= (UINT8
) SystemTime
->wSecond
;
1440 Time
->Nanosecond
= (UINT32
) SystemTime
->wMilliseconds
* 1000000;
1441 Time
->TimeZone
= (INT16
) TimeZone
->Bias
;
1443 if (TimeZone
->StandardDate
.wMonth
) {
1444 Time
->Daylight
= EFI_TIME_ADJUST_DAYLIGHT
;
1450 WinNtSimpleFileSystemRead (
1452 IN OUT UINTN
*BufferSize
,
1457 Routine Description:
1459 Read data from a file.
1463 This - Pointer to a returned open file handle.
1465 BufferSize - On input, the size of the Buffer. On output, the number of bytes stored in the Buffer.
1467 Buffer - Pointer to the first byte of the read Buffer.
1471 EFI_SUCCESS - The data was read.
1473 EFI_NO_MEDIA - The device has no media.
1475 EFI_DEVICE_ERROR - The device reported an error.
1477 EFI_VOLUME_CORRUPTED - The file system structures are corrupted.
1479 EFI_BUFFER_TOO_SMALL - The supplied buffer size was too small to store the current directory entry.
1480 *BufferSize has been updated with the size needed to complete the request.
1483 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1485 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1491 SYSTEMTIME SystemTime
;
1492 EFI_FILE_INFO
*Info
;
1494 TIME_ZONE_INFORMATION TimeZone
;
1495 EFI_FILE_INFO
*FileInfo
;
1501 if (This
== NULL
|| BufferSize
== NULL
) {
1502 return EFI_INVALID_PARAMETER
;
1505 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1507 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1509 if (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1510 Status
= EFI_DEVICE_ERROR
;
1514 if (!PrivateFile
->IsDirectoryPath
) {
1516 if (This
->GetPosition (This
, &Pos
) != EFI_SUCCESS
) {
1517 Status
= EFI_DEVICE_ERROR
;
1521 FileInfoSize
= SIZE_OF_EFI_FILE_SYSTEM_INFO
;
1522 FileInfo
= AllocatePool (FileInfoSize
);
1524 Status
= This
->GetInfo (
1531 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1532 FreePool (FileInfo
);
1533 FileInfo
= AllocatePool (FileInfoSize
);
1534 Status
= This
->GetInfo (
1542 if (EFI_ERROR (Status
)) {
1543 Status
= EFI_DEVICE_ERROR
;
1547 FileSize
= FileInfo
->FileSize
;
1549 FreePool (FileInfo
);
1551 if (Pos
>= FileSize
) {
1553 if (Pos
== FileSize
) {
1554 Status
= EFI_SUCCESS
;
1557 Status
= EFI_DEVICE_ERROR
;
1562 Status
= PrivateFile
->WinNtThunk
->ReadFile (
1563 PrivateFile
->LHandle
,
1568 ) ? EFI_SUCCESS
: EFI_DEVICE_ERROR
;
1573 // Read on a directory. Perform a find next
1575 if (!PrivateFile
->IsValidFindBuf
) {
1577 Status
= EFI_SUCCESS
;
1581 Size
= SIZE_OF_EFI_FILE_INFO
;
1583 NameSize
= StrSize (PrivateFile
->FindBuf
.cFileName
);
1585 ResultSize
= Size
+ NameSize
;
1587 Status
= EFI_BUFFER_TOO_SMALL
;
1589 if (*BufferSize
>= ResultSize
) {
1590 Status
= EFI_SUCCESS
;
1593 ZeroMem (Info
, ResultSize
);
1595 Info
->Size
= ResultSize
;
1597 PrivateFile
->WinNtThunk
->GetTimeZoneInformation (&TimeZone
);
1599 PrivateFile
->WinNtThunk
->FileTimeToLocalFileTime (
1600 &PrivateFile
->FindBuf
.ftCreationTime
,
1601 &PrivateFile
->FindBuf
.ftCreationTime
1604 PrivateFile
->WinNtThunk
->FileTimeToSystemTime (&PrivateFile
->FindBuf
.ftCreationTime
, &SystemTime
);
1606 WinNtSystemTimeToEfiTime (&SystemTime
, &TimeZone
, &Info
->CreateTime
);
1608 PrivateFile
->WinNtThunk
->FileTimeToLocalFileTime (
1609 &PrivateFile
->FindBuf
.ftLastWriteTime
,
1610 &PrivateFile
->FindBuf
.ftLastWriteTime
1613 PrivateFile
->WinNtThunk
->FileTimeToSystemTime (&PrivateFile
->FindBuf
.ftLastWriteTime
, &SystemTime
);
1615 WinNtSystemTimeToEfiTime (&SystemTime
, &TimeZone
, &Info
->ModificationTime
);
1617 Info
->FileSize
= PrivateFile
->FindBuf
.nFileSizeLow
;
1619 Info
->PhysicalSize
= PrivateFile
->FindBuf
.nFileSizeLow
;
1621 if (PrivateFile
->FindBuf
.dwFileAttributes
& FILE_ATTRIBUTE_ARCHIVE
) {
1622 Info
->Attribute
|= EFI_FILE_ARCHIVE
;
1625 if (PrivateFile
->FindBuf
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
) {
1626 Info
->Attribute
|= EFI_FILE_HIDDEN
;
1629 if (PrivateFile
->FindBuf
.dwFileAttributes
& FILE_ATTRIBUTE_SYSTEM
) {
1630 Info
->Attribute
|= EFI_FILE_SYSTEM
;
1633 if (PrivateFile
->FindBuf
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
1634 Info
->Attribute
|= EFI_FILE_READ_ONLY
;
1637 if (PrivateFile
->FindBuf
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
1638 Info
->Attribute
|= EFI_FILE_DIRECTORY
;
1641 NameSize
= NameSize
/ sizeof (WCHAR
);
1643 pw
= (WCHAR
*) (((CHAR8
*) Buffer
) + Size
);
1645 for (Index
= 0; Index
< NameSize
; Index
++) {
1646 pw
[Index
] = PrivateFile
->FindBuf
.cFileName
[Index
];
1649 if (PrivateFile
->WinNtThunk
->FindNextFile (PrivateFile
->LHandle
, &PrivateFile
->FindBuf
)) {
1650 PrivateFile
->IsValidFindBuf
= TRUE
;
1652 PrivateFile
->IsValidFindBuf
= FALSE
;
1656 *BufferSize
= ResultSize
;
1659 gBS
->RestoreTPL (OldTpl
);
1665 WinNtSimpleFileSystemWrite (
1667 IN OUT UINTN
*BufferSize
,
1672 Routine Description:
1674 Write data to a file.
1678 This - Pointer to an opened file handle.
1680 BufferSize - On input, the number of bytes in the Buffer to write to the file. On output, the number of bytes
1681 of data written to the file.
1683 Buffer - Pointer to the first by of data in the buffer to write to the file.
1687 EFI_SUCCESS - The data was written to the file.
1689 EFI_UNSUPPORTED - Writes to an open directory are not supported.
1691 EFI_NO_MEDIA - The device has no media.
1693 EFI_DEVICE_ERROR - The device reported an error.
1695 EFI_VOLUME_CORRUPTED - The file system structures are corrupt.
1697 EFI_WRITE_PROTECTED - The file, directory, volume, or device is write protected.
1699 EFI_ACCESS_DENIED - The file was opened read-only.
1701 EFI_VOLUME_FULL - The volume is full.
1704 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1706 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1710 if (This
== NULL
|| BufferSize
== NULL
|| Buffer
== NULL
) {
1711 return EFI_INVALID_PARAMETER
;
1714 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1716 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1718 if (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1719 Status
= EFI_DEVICE_ERROR
;
1723 if (PrivateFile
->IsDirectoryPath
) {
1724 Status
= EFI_UNSUPPORTED
;
1728 if (PrivateFile
->IsOpenedByRead
) {
1729 Status
= EFI_ACCESS_DENIED
;
1733 Status
= PrivateFile
->WinNtThunk
->WriteFile (
1734 PrivateFile
->LHandle
,
1739 ) ? EFI_SUCCESS
: EFI_DEVICE_ERROR
;
1742 gBS
->RestoreTPL (OldTpl
);
1746 // bugbug: need to access windows error reporting
1752 WinNtSimpleFileSystemSetPosition (
1758 Routine Description:
1760 Set a file's current position.
1764 This - Pointer to an opened file handle.
1766 Position - The byte position from the start of the file to set.
1770 EFI_SUCCESS - The file position has been changed.
1772 EFI_UNSUPPORTED - The seek request for non-zero is not supported for directories.
1775 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1778 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1785 return EFI_INVALID_PARAMETER
;
1788 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1790 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1792 if (PrivateFile
->IsDirectoryPath
) {
1793 if (Position
!= 0) {
1794 Status
= EFI_UNSUPPORTED
;
1798 FileName
= AllocatePool (StrSize (PrivateFile
->FileName
) + StrSize (L
"\\*"));
1799 if (FileName
== NULL
) {
1800 Status
= EFI_OUT_OF_RESOURCES
;
1804 StrCpy (FileName
, PrivateFile
->FileName
);
1805 StrCat (FileName
, L
"\\*");
1807 if (PrivateFile
->LHandle
!= INVALID_HANDLE_VALUE
) {
1808 PrivateFile
->WinNtThunk
->FindClose (PrivateFile
->LHandle
);
1811 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->FindFirstFile (FileName
, &PrivateFile
->FindBuf
);
1813 if (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1814 PrivateFile
->IsValidFindBuf
= FALSE
;
1816 PrivateFile
->IsValidFindBuf
= TRUE
;
1819 FreePool (FileName
);
1821 Status
= (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) ? EFI_DEVICE_ERROR
: EFI_SUCCESS
;
1823 if (Position
== (UINT64
) -1) {
1824 PosLow
= PrivateFile
->WinNtThunk
->SetFilePointer (PrivateFile
->LHandle
, (ULONG
) 0, NULL
, FILE_END
);
1826 PosHigh
= (UINT32
) RShiftU64 (Position
, 32);
1828 PosLow
= PrivateFile
->WinNtThunk
->SetFilePointer (PrivateFile
->LHandle
, (ULONG
) Position
, &PosHigh
, FILE_BEGIN
);
1831 Status
= (PosLow
== 0xFFFFFFFF) ? EFI_DEVICE_ERROR
: EFI_SUCCESS
;
1835 gBS
->RestoreTPL (OldTpl
);
1841 WinNtSimpleFileSystemGetPosition (
1843 OUT UINT64
*Position
1847 Routine Description:
1849 Get a file's current position.
1853 This - Pointer to an opened file handle.
1855 Position - Pointer to storage for the current position.
1859 EFI_SUCCESS - The file position has been reported.
1861 EFI_UNSUPPORTED - Not valid for directories.
1864 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1867 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1872 if (This
== NULL
|| Position
== NULL
) {
1873 return EFI_INVALID_PARAMETER
;
1876 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1877 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1882 if (PrivateFile
->IsDirectoryPath
) {
1884 Status
= EFI_UNSUPPORTED
;
1890 *Position
= PrivateFile
->WinNtThunk
->SetFilePointer (
1891 PrivateFile
->LHandle
,
1897 Status
= *Position
== 0xffffffff ? EFI_DEVICE_ERROR
: EFI_SUCCESS
;
1898 if (EFI_ERROR (Status
)) {
1902 PosHigh64
= PositionHigh
;
1903 *Position
+= LShiftU64 (PosHigh64
, 32);
1907 gBS
->RestoreTPL (OldTpl
);
1913 WinNtSimpleFileSystemFileInfo (
1914 IN WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
,
1915 IN OUT UINTN
*BufferSize
,
1920 Routine Description:
1922 TODO: Add function description
1926 PrivateFile - TODO: add argument description
1927 BufferSize - TODO: add argument description
1928 Buffer - TODO: add argument description
1932 TODO: add return values
1940 EFI_FILE_INFO
*Info
;
1941 BY_HANDLE_FILE_INFORMATION FileInfo
;
1942 SYSTEMTIME SystemTime
;
1943 CHAR16
*RealFileName
;
1944 CHAR16
*TempPointer
;
1945 EFI_FILE_INFO
*DirInfo
;
1948 EFI_STATUS DirStatus
;
1951 Size
= SIZE_OF_EFI_FILE_INFO
;
1952 NameSize
= StrSize (PrivateFile
->FileName
);
1953 ResultSize
= Size
+ NameSize
;
1955 Status
= EFI_BUFFER_TOO_SMALL
;
1956 if (*BufferSize
>= ResultSize
) {
1957 Status
= EFI_SUCCESS
;
1960 ZeroMem (Info
, ResultSize
);
1962 Info
->Size
= ResultSize
;
1963 PrivateFile
->WinNtThunk
->GetFileInformationByHandle (
1964 PrivateFile
->IsDirectoryPath
? PrivateFile
->DirHandle
: PrivateFile
->LHandle
,
1967 Info
->FileSize
= FileInfo
.nFileSizeLow
;
1968 Info
->PhysicalSize
= Info
->FileSize
;
1970 PrivateFile
->WinNtThunk
->FileTimeToSystemTime (&FileInfo
.ftCreationTime
, &SystemTime
);
1971 Info
->CreateTime
.Year
= SystemTime
.wYear
;
1972 Info
->CreateTime
.Month
= (UINT8
) SystemTime
.wMonth
;
1973 Info
->CreateTime
.Day
= (UINT8
) SystemTime
.wDay
;
1974 Info
->CreateTime
.Hour
= (UINT8
) SystemTime
.wHour
;
1975 Info
->CreateTime
.Minute
= (UINT8
) SystemTime
.wMinute
;
1976 Info
->CreateTime
.Second
= (UINT8
) SystemTime
.wSecond
;
1978 PrivateFile
->WinNtThunk
->FileTimeToSystemTime (&FileInfo
.ftLastAccessTime
, &SystemTime
);
1979 Info
->LastAccessTime
.Year
= SystemTime
.wYear
;
1980 Info
->LastAccessTime
.Month
= (UINT8
) SystemTime
.wMonth
;
1981 Info
->LastAccessTime
.Day
= (UINT8
) SystemTime
.wDay
;
1982 Info
->LastAccessTime
.Hour
= (UINT8
) SystemTime
.wHour
;
1983 Info
->LastAccessTime
.Minute
= (UINT8
) SystemTime
.wMinute
;
1984 Info
->LastAccessTime
.Second
= (UINT8
) SystemTime
.wSecond
;
1986 PrivateFile
->WinNtThunk
->FileTimeToSystemTime (&FileInfo
.ftLastWriteTime
, &SystemTime
);
1987 Info
->ModificationTime
.Year
= SystemTime
.wYear
;
1988 Info
->ModificationTime
.Month
= (UINT8
) SystemTime
.wMonth
;
1989 Info
->ModificationTime
.Day
= (UINT8
) SystemTime
.wDay
;
1990 Info
->ModificationTime
.Hour
= (UINT8
) SystemTime
.wHour
;
1991 Info
->ModificationTime
.Minute
= (UINT8
) SystemTime
.wMinute
;
1992 Info
->ModificationTime
.Second
= (UINT8
) SystemTime
.wSecond
;
1994 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_ARCHIVE
) {
1995 Info
->Attribute
|= EFI_FILE_ARCHIVE
;
1998 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
) {
1999 Info
->Attribute
|= EFI_FILE_HIDDEN
;
2002 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
2003 Info
->Attribute
|= EFI_FILE_READ_ONLY
;
2006 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_SYSTEM
) {
2007 Info
->Attribute
|= EFI_FILE_SYSTEM
;
2010 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
2011 Info
->Attribute
|= EFI_FILE_DIRECTORY
;
2014 if (PrivateFile
->IsDirectoryPath
) {
2015 Info
->Attribute
|= EFI_FILE_DIRECTORY
;
2018 RealFileName
= PrivateFile
->FileName
;
2019 TempPointer
= RealFileName
;
2021 while (*TempPointer
) {
2022 if (*TempPointer
== '\\') {
2023 RealFileName
= TempPointer
+ 1;
2029 if (PrivateFile
->IsRootDirectory
) {
2030 *((CHAR8
*) Buffer
+ Size
) = 0;
2032 CopyMem ((CHAR8
*) Buffer
+ Size
, RealFileName
, NameSize
);
2035 if (Info
->Attribute
& EFI_FILE_DIRECTORY
) {
2037 // The GetFileInformationByHandle.nFileSizeLow is bogus for dir so we
2038 // need to do the same thing the caller would do to get the right value
2040 ASSERT (PrivateFile
->EfiFile
.Read
!= NULL
);
2041 DirStatus
= PrivateFile
->EfiFile
.GetPosition (&PrivateFile
->EfiFile
, &Location
);
2042 if (EFI_ERROR (DirStatus
)) {
2046 PrivateFile
->EfiFile
.SetPosition (&PrivateFile
->EfiFile
, 0);
2051 DirStatus
= PrivateFile
->EfiFile
.Read (&PrivateFile
->EfiFile
, &ReadSize
, DirInfo
);
2052 if (DirStatus
== EFI_BUFFER_TOO_SMALL
) {
2053 DirInfo
= AllocatePool (ReadSize
);
2054 if (DirInfo
!= NULL
) {
2056 // Read each dir entry to figure out how big the directory is
2058 DirStatus
= PrivateFile
->EfiFile
.Read (&PrivateFile
->EfiFile
, &ReadSize
, DirInfo
);
2059 if (!EFI_ERROR (DirStatus
) && (ReadSize
!= 0)) {
2060 Info
->FileSize
+= ReadSize
;
2066 } while (!EFI_ERROR (DirStatus
) && (ReadSize
!= 0));
2069 // reset the file possition back to the previous location
2071 PrivateFile
->EfiFile
.SetPosition (&PrivateFile
->EfiFile
, Location
);
2075 *BufferSize
= ResultSize
;
2081 WinNtSimpleFileSystemGetInfo (
2083 IN EFI_GUID
*InformationType
,
2084 IN OUT UINTN
*BufferSize
,
2089 Routine Description:
2091 Return information about a file or volume.
2095 This - Pointer to an opened file handle.
2097 InformationType - GUID describing the type of information to be returned.
2099 BufferSize - On input, the size of the information buffer. On output, the number of bytes written to the
2102 Buffer - Pointer to the first byte of the information buffer.
2106 EFI_SUCCESS - The requested information has been written into the buffer.
2108 EFI_UNSUPPORTED - The InformationType is not known.
2110 EFI_NO_MEDIA - The device has no media.
2112 EFI_DEVICE_ERROR - The device reported an error.
2114 EFI_VOLUME_CORRUPTED - The file system structures are corrupt.
2116 EFI_BUFFER_TOO_SMALL - The buffer size was too small to contain the requested information. The buffer size has
2117 been updated with the size needed to complete the requested operation.
2120 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
2123 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
2124 EFI_FILE_SYSTEM_INFO
*FileSystemInfoBuffer
;
2125 UINT32 SectorsPerCluster
;
2126 UINT32 BytesPerSector
;
2127 UINT32 FreeClusters
;
2128 UINT32 TotalClusters
;
2129 UINT32 BytesPerCluster
;
2131 BOOLEAN DriveNameFound
;
2134 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE
*PrivateRoot
;
2137 if (This
== NULL
|| InformationType
== NULL
|| BufferSize
== NULL
) {
2138 return EFI_INVALID_PARAMETER
;
2141 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
2143 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
2144 PrivateRoot
= WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (PrivateFile
->SimpleFileSystem
);
2146 Status
= EFI_UNSUPPORTED
;
2148 if (CompareGuid (InformationType
, &gEfiFileInfoGuid
)) {
2149 Status
= WinNtSimpleFileSystemFileInfo (PrivateFile
, BufferSize
, Buffer
);
2152 if (CompareGuid (InformationType
, &gEfiFileSystemInfoGuid
)) {
2153 if (*BufferSize
< SIZE_OF_EFI_FILE_SYSTEM_INFO
+ StrSize (PrivateRoot
->VolumeLabel
)) {
2154 *BufferSize
= SIZE_OF_EFI_FILE_SYSTEM_INFO
+ StrSize (PrivateRoot
->VolumeLabel
);
2155 Status
= EFI_BUFFER_TOO_SMALL
;
2159 FileSystemInfoBuffer
= (EFI_FILE_SYSTEM_INFO
*) Buffer
;
2160 FileSystemInfoBuffer
->Size
= SIZE_OF_EFI_FILE_SYSTEM_INFO
+ StrSize (PrivateRoot
->VolumeLabel
);
2161 FileSystemInfoBuffer
->ReadOnly
= FALSE
;
2164 // Try to get the drive name
2166 DriveNameFound
= FALSE
;
2167 DriveName
= AllocatePool (StrSize (PrivateFile
->FilePath
) + 1);
2168 if (DriveName
== NULL
) {
2169 Status
= EFI_OUT_OF_RESOURCES
;
2173 StrCpy (DriveName
, PrivateFile
->FilePath
);
2174 for (Index
= 0; DriveName
[Index
] != 0 && DriveName
[Index
] != ':'; Index
++) {
2178 if (DriveName
[Index
] == ':') {
2179 DriveName
[Index
+ 1] = '\\';
2180 DriveName
[Index
+ 2] = 0;
2181 DriveNameFound
= TRUE
;
2182 } else if (DriveName
[0] == '\\' && DriveName
[1] == '\\') {
2183 for (Index
= 2; DriveName
[Index
] != 0 && DriveName
[Index
] != '\\'; Index
++) {
2187 if (DriveName
[Index
] == '\\') {
2188 DriveNameFound
= TRUE
;
2189 for (Index
++; DriveName
[Index
] != 0 && DriveName
[Index
] != '\\'; Index
++) {
2193 DriveName
[Index
] = '\\';
2194 DriveName
[Index
+ 1] = 0;
2199 // Try GetDiskFreeSpace first
2201 NtStatus
= PrivateFile
->WinNtThunk
->GetDiskFreeSpace (
2202 DriveNameFound
? DriveName
: NULL
,
2209 FreePool (DriveName
);
2216 BytesPerCluster
= BytesPerSector
* SectorsPerCluster
;
2217 FileSystemInfoBuffer
->VolumeSize
= MultU64x32 (TotalClusters
, BytesPerCluster
);
2218 FileSystemInfoBuffer
->FreeSpace
= MultU64x32 (FreeClusters
, BytesPerCluster
);
2219 FileSystemInfoBuffer
->BlockSize
= BytesPerCluster
;
2223 // try GetDiskFreeSpaceEx then
2225 FileSystemInfoBuffer
->BlockSize
= 0;
2226 NtStatus
= PrivateFile
->WinNtThunk
->GetDiskFreeSpaceEx (
2227 PrivateFile
->FilePath
,
2228 (PULARGE_INTEGER
) (&FileSystemInfoBuffer
->FreeSpace
),
2229 (PULARGE_INTEGER
) (&FileSystemInfoBuffer
->VolumeSize
),
2233 Status
= EFI_DEVICE_ERROR
;
2238 StrCpy ((CHAR16
*) FileSystemInfoBuffer
->VolumeLabel
, PrivateRoot
->VolumeLabel
);
2239 *BufferSize
= SIZE_OF_EFI_FILE_SYSTEM_INFO
+ StrSize (PrivateRoot
->VolumeLabel
);
2240 Status
= EFI_SUCCESS
;
2243 if (CompareGuid (InformationType
, &gEfiFileSystemVolumeLabelInfoIdGuid
)) {
2244 if (*BufferSize
< StrSize (PrivateRoot
->VolumeLabel
)) {
2245 *BufferSize
= StrSize (PrivateRoot
->VolumeLabel
);
2246 Status
= EFI_BUFFER_TOO_SMALL
;
2250 StrCpy ((CHAR16
*) Buffer
, PrivateRoot
->VolumeLabel
);
2251 *BufferSize
= StrSize (PrivateRoot
->VolumeLabel
);
2252 Status
= EFI_SUCCESS
;
2256 gBS
->RestoreTPL (OldTpl
);
2262 WinNtSimpleFileSystemSetInfo (
2264 IN EFI_GUID
*InformationType
,
2265 IN UINTN BufferSize
,
2270 Routine Description:
2272 Set information about a file or volume.
2276 This - Pointer to an opened file handle.
2278 InformationType - GUID identifying the type of information to set.
2280 BufferSize - Number of bytes of data in the information buffer.
2282 Buffer - Pointer to the first byte of data in the information buffer.
2286 EFI_SUCCESS - The file or volume information has been updated.
2288 EFI_UNSUPPORTED - The information identifier is not recognised.
2290 EFI_NO_MEDIA - The device has no media.
2292 EFI_DEVICE_ERROR - The device reported an error.
2294 EFI_VOLUME_CORRUPTED - The file system structures are corrupt.
2296 EFI_WRITE_PROTECTED - The file, directory, volume, or device is write protected.
2298 EFI_ACCESS_DENIED - The file was opened read-only.
2300 EFI_VOLUME_FULL - The volume is full.
2302 EFI_BAD_BUFFER_SIZE - The buffer size is smaller than the type indicated by InformationType.
2305 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
2306 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
2308 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE
*PrivateRoot
;
2309 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
2310 EFI_FILE_INFO
*OldFileInfo
;
2311 EFI_FILE_INFO
*NewFileInfo
;
2317 CHAR16
*OldFileName
;
2318 CHAR16
*NewFileName
;
2319 CHAR16
*TempFileName
;
2320 CHAR16
*CharPointer
;
2321 BOOLEAN AttrChangeFlag
;
2322 BOOLEAN NameChangeFlag
;
2323 BOOLEAN SizeChangeFlag
;
2324 BOOLEAN TimeChangeFlag
;
2326 SYSTEMTIME NewCreationSystemTime
;
2327 SYSTEMTIME NewLastAccessSystemTime
;
2328 SYSTEMTIME NewLastWriteSystemTime
;
2329 FILETIME NewCreationFileTime
;
2330 FILETIME NewLastAccessFileTime
;
2331 FILETIME NewLastWriteFileTime
;
2332 WIN32_FIND_DATA FindBuf
;
2333 EFI_FILE_SYSTEM_INFO
*NewFileSystemInfo
;
2337 // Check for invalid parameters.
2339 if (This
== NULL
|| InformationType
== NULL
|| BufferSize
== 0 || Buffer
== NULL
) {
2340 return EFI_INVALID_PARAMETER
;
2343 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
2346 // Initialise locals.
2348 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
2349 PrivateRoot
= WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (PrivateFile
->SimpleFileSystem
);
2351 Status
= EFI_UNSUPPORTED
;
2352 OldFileInfo
= NewFileInfo
= NULL
;
2353 OldFileName
= NewFileName
= NULL
;
2354 AttrChangeFlag
= NameChangeFlag
= SizeChangeFlag
= TimeChangeFlag
= FALSE
;
2357 // Set file system information.
2359 if (CompareGuid (InformationType
, &gEfiFileSystemInfoGuid
)) {
2360 if (BufferSize
< SIZE_OF_EFI_FILE_SYSTEM_INFO
+ StrSize (PrivateRoot
->VolumeLabel
)) {
2361 Status
= EFI_BAD_BUFFER_SIZE
;
2365 NewFileSystemInfo
= (EFI_FILE_SYSTEM_INFO
*) Buffer
;
2367 FreePool (PrivateRoot
->VolumeLabel
);
2368 PrivateRoot
->VolumeLabel
= AllocatePool (StrSize (NewFileSystemInfo
->VolumeLabel
));
2369 if (PrivateRoot
->VolumeLabel
== NULL
) {
2370 Status
= EFI_OUT_OF_RESOURCES
;
2374 StrCpy (PrivateRoot
->VolumeLabel
, NewFileSystemInfo
->VolumeLabel
);
2376 Status
= EFI_SUCCESS
;
2381 // Set volume label information.
2383 if (CompareGuid (InformationType
, &gEfiFileSystemVolumeLabelInfoIdGuid
)) {
2384 if (BufferSize
< StrSize (PrivateRoot
->VolumeLabel
)) {
2385 Status
= EFI_BAD_BUFFER_SIZE
;
2389 StrCpy (PrivateRoot
->VolumeLabel
, (CHAR16
*) Buffer
);
2391 Status
= EFI_SUCCESS
;
2395 if (!CompareGuid (InformationType
, &gEfiFileInfoGuid
)) {
2396 Status
= EFI_UNSUPPORTED
;
2400 if (BufferSize
< SIZE_OF_EFI_FILE_INFO
) {
2401 Status
= EFI_BAD_BUFFER_SIZE
;
2406 // Set file/directory information.
2410 // Check for invalid set file information parameters.
2412 NewFileInfo
= (EFI_FILE_INFO
*) Buffer
;
2414 if (NewFileInfo
->Size
<= sizeof (EFI_FILE_INFO
) ||
2415 (NewFileInfo
->Attribute
&~(EFI_FILE_VALID_ATTR
)) ||
2416 (sizeof (UINTN
) == 4 && NewFileInfo
->Size
> 0xFFFFFFFF)
2418 Status
= EFI_INVALID_PARAMETER
;
2423 // bugbug: - This is not safe. We need something like EfiStrMaxSize()
2424 // that would have an additional parameter that would be the size
2425 // of the string array just in case there are no NULL characters in
2426 // the string array.
2429 // Get current file information so we can determine what kind
2430 // of change request this is.
2433 Status
= WinNtSimpleFileSystemFileInfo (PrivateFile
, &OldInfoSize
, NULL
);
2435 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
2436 Status
= EFI_DEVICE_ERROR
;
2440 OldFileInfo
= AllocatePool (OldInfoSize
);
2441 if (OldFileInfo
== NULL
) {
2442 Status
= EFI_OUT_OF_RESOURCES
;
2446 Status
= WinNtSimpleFileSystemFileInfo (PrivateFile
, &OldInfoSize
, OldFileInfo
);
2448 if (EFI_ERROR (Status
)) {
2452 OldFileName
= AllocatePool (StrSize (PrivateFile
->FileName
));
2453 if (OldFileName
== NULL
) {
2454 Status
= EFI_OUT_OF_RESOURCES
;
2458 StrCpy (OldFileName
, PrivateFile
->FileName
);
2461 // Make full pathname from new filename and rootpath.
2463 if (NewFileInfo
->FileName
[0] == '\\') {
2464 NewFileName
= AllocatePool (StrSize (PrivateRoot
->FilePath
) + StrSize (L
"\\") + StrSize (NewFileInfo
->FileName
));
2465 if (NewFileName
== NULL
) {
2466 Status
= EFI_OUT_OF_RESOURCES
;
2470 StrCpy (NewFileName
, PrivateRoot
->FilePath
);
2471 StrCat (NewFileName
, L
"\\");
2472 StrCat (NewFileName
, NewFileInfo
->FileName
+ 1);
2474 NewFileName
= AllocatePool (StrSize (PrivateFile
->FilePath
) + StrSize (L
"\\") + StrSize (NewFileInfo
->FileName
));
2475 if (NewFileName
== NULL
) {
2476 Status
= EFI_OUT_OF_RESOURCES
;
2480 StrCpy (NewFileName
, PrivateFile
->FilePath
);
2481 StrCat (NewFileName
, L
"\\");
2482 StrCat (NewFileName
, NewFileInfo
->FileName
);
2486 // Is there an attribute change request?
2488 if (NewFileInfo
->Attribute
!= OldFileInfo
->Attribute
) {
2489 if ((NewFileInfo
->Attribute
& EFI_FILE_DIRECTORY
) != (OldFileInfo
->Attribute
& EFI_FILE_DIRECTORY
)) {
2490 Status
= EFI_INVALID_PARAMETER
;
2494 AttrChangeFlag
= TRUE
;
2498 // Is there a name change request?
2499 // bugbug: - Need EfiStrCaseCmp()
2501 if (StrCmp (NewFileInfo
->FileName
, OldFileInfo
->FileName
)) {
2502 NameChangeFlag
= TRUE
;
2506 // Is there a size change request?
2508 if (NewFileInfo
->FileSize
!= OldFileInfo
->FileSize
) {
2509 SizeChangeFlag
= TRUE
;
2513 // Is there a time stamp change request?
2515 if (!IsZero (&NewFileInfo
->CreateTime
, sizeof (EFI_TIME
)) &&
2516 CompareMem (&NewFileInfo
->CreateTime
, &OldFileInfo
->CreateTime
, sizeof (EFI_TIME
))
2518 TimeChangeFlag
= TRUE
;
2519 } else if (!IsZero (&NewFileInfo
->LastAccessTime
, sizeof (EFI_TIME
)) &&
2520 CompareMem (&NewFileInfo
->LastAccessTime
, &OldFileInfo
->LastAccessTime
, sizeof (EFI_TIME
))
2522 TimeChangeFlag
= TRUE
;
2523 } else if (!IsZero (&NewFileInfo
->ModificationTime
, sizeof (EFI_TIME
)) &&
2524 CompareMem (&NewFileInfo
->ModificationTime
, &OldFileInfo
->ModificationTime
, sizeof (EFI_TIME
))
2526 TimeChangeFlag
= TRUE
;
2530 // All done if there are no change requests being made.
2532 if (!(AttrChangeFlag
|| NameChangeFlag
|| SizeChangeFlag
|| TimeChangeFlag
)) {
2533 Status
= EFI_SUCCESS
;
2538 // Set file or directory information.
2540 OldAttr
= PrivateFile
->WinNtThunk
->GetFileAttributes (OldFileName
);
2545 if (NameChangeFlag
) {
2547 // Close the handles first
2549 if (PrivateFile
->IsOpenedByRead
) {
2550 Status
= EFI_ACCESS_DENIED
;
2554 for (CharPointer
= NewFileName
; *CharPointer
!= 0 && *CharPointer
!= L
'/'; CharPointer
++) {
2557 if (*CharPointer
!= 0) {
2558 Status
= EFI_ACCESS_DENIED
;
2562 if (PrivateFile
->LHandle
!= INVALID_HANDLE_VALUE
) {
2563 if (PrivateFile
->IsDirectoryPath
) {
2564 PrivateFile
->WinNtThunk
->FindClose (PrivateFile
->LHandle
);
2566 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->LHandle
);
2567 PrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
2571 if (PrivateFile
->IsDirectoryPath
&& PrivateFile
->DirHandle
!= INVALID_HANDLE_VALUE
) {
2572 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->DirHandle
);
2573 PrivateFile
->DirHandle
= INVALID_HANDLE_VALUE
;
2576 NtStatus
= PrivateFile
->WinNtThunk
->MoveFile (OldFileName
, NewFileName
);
2582 FreePool (PrivateFile
->FileName
);
2584 PrivateFile
->FileName
= AllocatePool (StrSize (NewFileName
));
2585 if (PrivateFile
->FileName
== NULL
) {
2586 Status
= EFI_OUT_OF_RESOURCES
;
2590 StrCpy (PrivateFile
->FileName
, NewFileName
);
2592 TempFileName
= AllocatePool (StrSize (NewFileName
) + StrSize (L
"\\*"));
2594 StrCpy (TempFileName
, NewFileName
);
2596 if (!PrivateFile
->IsDirectoryPath
) {
2597 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->CreateFile (
2599 PrivateFile
->IsOpenedByRead
? GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
,
2600 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2607 FreePool (TempFileName
);
2610 // Flush buffers just in case
2612 if (PrivateFile
->WinNtThunk
->FlushFileBuffers (PrivateFile
->LHandle
) == 0) {
2613 Status
= EFI_DEVICE_ERROR
;
2617 PrivateFile
->DirHandle
= PrivateFile
->WinNtThunk
->CreateFile (
2619 PrivateFile
->IsOpenedByRead
? GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
,
2620 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2623 FILE_FLAG_BACKUP_SEMANTICS
,
2627 StrCat (TempFileName
, L
"\\*");
2628 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->FindFirstFile (TempFileName
, &FindBuf
);
2630 FreePool (TempFileName
);
2634 Status
= EFI_DEVICE_ERROR
;
2636 NtStatus
= PrivateFile
->WinNtThunk
->SetFileAttributes (OldFileName
, OldAttr
);
2642 TempFileName
= AllocatePool (StrSize (OldFileName
) + StrSize (L
"\\*"));
2644 StrCpy (TempFileName
, OldFileName
);
2646 if (!PrivateFile
->IsDirectoryPath
) {
2647 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->CreateFile (
2649 PrivateFile
->IsOpenedByRead
? GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
,
2650 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2657 PrivateFile
->DirHandle
= PrivateFile
->WinNtThunk
->CreateFile (
2659 PrivateFile
->IsOpenedByRead
? GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
,
2660 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2663 FILE_FLAG_BACKUP_SEMANTICS
,
2667 StrCat (TempFileName
, L
"\\*");
2668 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->FindFirstFile (TempFileName
, &FindBuf
);
2671 FreePool (TempFileName
);
2681 if (SizeChangeFlag
) {
2682 if (PrivateFile
->IsDirectoryPath
) {
2683 Status
= EFI_UNSUPPORTED
;
2687 if (PrivateFile
->IsOpenedByRead
|| OldFileInfo
->Attribute
& EFI_FILE_READ_ONLY
) {
2688 Status
= EFI_ACCESS_DENIED
;
2692 Status
= This
->GetPosition (This
, &CurPos
);
2693 if (EFI_ERROR (Status
)) {
2697 Status
= This
->SetPosition (This
, NewFileInfo
->FileSize
);
2698 if (EFI_ERROR (Status
)) {
2702 if (PrivateFile
->WinNtThunk
->SetEndOfFile (PrivateFile
->LHandle
) == 0) {
2703 Status
= EFI_DEVICE_ERROR
;
2707 Status
= This
->SetPosition (This
, CurPos
);
2708 if (EFI_ERROR (Status
)) {
2716 if (TimeChangeFlag
) {
2718 NewCreationSystemTime
.wYear
= NewFileInfo
->CreateTime
.Year
;
2719 NewCreationSystemTime
.wMonth
= NewFileInfo
->CreateTime
.Month
;
2720 NewCreationSystemTime
.wDay
= NewFileInfo
->CreateTime
.Day
;
2721 NewCreationSystemTime
.wHour
= NewFileInfo
->CreateTime
.Hour
;
2722 NewCreationSystemTime
.wMinute
= NewFileInfo
->CreateTime
.Minute
;
2723 NewCreationSystemTime
.wSecond
= NewFileInfo
->CreateTime
.Second
;
2724 NewCreationSystemTime
.wMilliseconds
= 0;
2726 if (!PrivateFile
->WinNtThunk
->SystemTimeToFileTime (
2727 &NewCreationSystemTime
,
2728 &NewCreationFileTime
2733 NewLastAccessSystemTime
.wYear
= NewFileInfo
->LastAccessTime
.Year
;
2734 NewLastAccessSystemTime
.wMonth
= NewFileInfo
->LastAccessTime
.Month
;
2735 NewLastAccessSystemTime
.wDay
= NewFileInfo
->LastAccessTime
.Day
;
2736 NewLastAccessSystemTime
.wHour
= NewFileInfo
->LastAccessTime
.Hour
;
2737 NewLastAccessSystemTime
.wMinute
= NewFileInfo
->LastAccessTime
.Minute
;
2738 NewLastAccessSystemTime
.wSecond
= NewFileInfo
->LastAccessTime
.Second
;
2739 NewLastAccessSystemTime
.wMilliseconds
= 0;
2741 if (!PrivateFile
->WinNtThunk
->SystemTimeToFileTime (
2742 &NewLastAccessSystemTime
,
2743 &NewLastAccessFileTime
2748 NewLastWriteSystemTime
.wYear
= NewFileInfo
->ModificationTime
.Year
;
2749 NewLastWriteSystemTime
.wMonth
= NewFileInfo
->ModificationTime
.Month
;
2750 NewLastWriteSystemTime
.wDay
= NewFileInfo
->ModificationTime
.Day
;
2751 NewLastWriteSystemTime
.wHour
= NewFileInfo
->ModificationTime
.Hour
;
2752 NewLastWriteSystemTime
.wMinute
= NewFileInfo
->ModificationTime
.Minute
;
2753 NewLastWriteSystemTime
.wSecond
= NewFileInfo
->ModificationTime
.Second
;
2754 NewLastWriteSystemTime
.wMilliseconds
= 0;
2756 if (!PrivateFile
->WinNtThunk
->SystemTimeToFileTime (
2757 &NewLastWriteSystemTime
,
2758 &NewLastWriteFileTime
2763 if (!PrivateFile
->WinNtThunk
->SetFileTime (
2764 PrivateFile
->IsDirectoryPath
? PrivateFile
->DirHandle
: PrivateFile
->LHandle
,
2765 &NewCreationFileTime
,
2766 &NewLastAccessFileTime
,
2767 &NewLastWriteFileTime
2769 Status
= EFI_DEVICE_ERROR
;
2776 // No matter about AttrChangeFlag, Attribute must be set.
2777 // Because operation before may cause attribute change.
2781 if (NewFileInfo
->Attribute
& EFI_FILE_ARCHIVE
) {
2782 NewAttr
|= FILE_ATTRIBUTE_ARCHIVE
;
2784 NewAttr
&= ~FILE_ATTRIBUTE_ARCHIVE
;
2787 if (NewFileInfo
->Attribute
& EFI_FILE_HIDDEN
) {
2788 NewAttr
|= FILE_ATTRIBUTE_HIDDEN
;
2790 NewAttr
&= ~FILE_ATTRIBUTE_HIDDEN
;
2793 if (NewFileInfo
->Attribute
& EFI_FILE_SYSTEM
) {
2794 NewAttr
|= FILE_ATTRIBUTE_SYSTEM
;
2796 NewAttr
&= ~FILE_ATTRIBUTE_SYSTEM
;
2799 if (NewFileInfo
->Attribute
& EFI_FILE_READ_ONLY
) {
2800 NewAttr
|= FILE_ATTRIBUTE_READONLY
;
2802 NewAttr
&= ~FILE_ATTRIBUTE_READONLY
;
2805 NtStatus
= PrivateFile
->WinNtThunk
->SetFileAttributes (NewFileName
, NewAttr
);
2812 if (OldFileInfo
!= NULL
) {
2813 FreePool (OldFileInfo
);
2816 if (OldFileName
!= NULL
) {
2817 FreePool (OldFileName
);
2820 if (NewFileName
!= NULL
) {
2821 FreePool (NewFileName
);
2824 gBS
->RestoreTPL (OldTpl
);
2830 WinNtSimpleFileSystemFlush (
2835 Routine Description:
2837 Flush all modified data to the media.
2841 This - Pointer to an opened file handle.
2845 EFI_SUCCESS - The data has been flushed.
2847 EFI_NO_MEDIA - The device has no media.
2849 EFI_DEVICE_ERROR - The device reported an error.
2851 EFI_VOLUME_CORRUPTED - The file system structures have been corrupted.
2853 EFI_WRITE_PROTECTED - The file, directory, volume, or device is write protected.
2855 EFI_ACCESS_DENIED - The file was opened read-only.
2857 EFI_VOLUME_FULL - The volume is full.
2860 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
2862 BY_HANDLE_FILE_INFORMATION FileInfo
;
2863 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
2868 return EFI_INVALID_PARAMETER
;
2871 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
2873 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
2875 if (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
2876 Status
= EFI_DEVICE_ERROR
;
2880 if (PrivateFile
->IsDirectoryPath
) {
2881 Status
= EFI_SUCCESS
;
2885 if (PrivateFile
->IsOpenedByRead
) {
2886 Status
= EFI_ACCESS_DENIED
;
2890 PrivateFile
->WinNtThunk
->GetFileInformationByHandle (PrivateFile
->LHandle
, &FileInfo
);
2892 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
2893 Status
= EFI_ACCESS_DENIED
;
2897 Status
= PrivateFile
->WinNtThunk
->FlushFileBuffers (PrivateFile
->LHandle
) ? EFI_SUCCESS
: EFI_DEVICE_ERROR
;
2900 gBS
->RestoreTPL (OldTpl
);
2903 // bugbug: - Use Windows error reporting.