3 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
4 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
;
549 if (This
== NULL
|| Root
== NULL
) {
550 return EFI_INVALID_PARAMETER
;
553 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
555 Private
= WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (This
);
557 PrivateFile
= AllocatePool (sizeof (WIN_NT_EFI_FILE_PRIVATE
));
558 if (PrivateFile
== NULL
) {
559 Status
= EFI_OUT_OF_RESOURCES
;
563 PrivateFile
->FileName
= AllocatePool (StrSize (Private
->FilePath
));
564 if (PrivateFile
->FileName
== NULL
) {
565 Status
= EFI_OUT_OF_RESOURCES
;
569 PrivateFile
->FilePath
= AllocatePool (StrSize (Private
->FilePath
));
570 if (PrivateFile
->FilePath
== NULL
) {
571 Status
= EFI_OUT_OF_RESOURCES
;
575 StrCpy (PrivateFile
->FilePath
, Private
->FilePath
);
576 StrCpy (PrivateFile
->FileName
, PrivateFile
->FilePath
);
577 PrivateFile
->Signature
= WIN_NT_EFI_FILE_PRIVATE_SIGNATURE
;
578 PrivateFile
->WinNtThunk
= Private
->WinNtThunk
;
579 PrivateFile
->SimpleFileSystem
= This
;
580 PrivateFile
->IsRootDirectory
= TRUE
;
581 PrivateFile
->IsDirectoryPath
= TRUE
;
582 PrivateFile
->IsOpenedByRead
= TRUE
;
583 PrivateFile
->EfiFile
.Revision
= EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION
;
584 PrivateFile
->EfiFile
.Open
= WinNtSimpleFileSystemOpen
;
585 PrivateFile
->EfiFile
.Close
= WinNtSimpleFileSystemClose
;
586 PrivateFile
->EfiFile
.Delete
= WinNtSimpleFileSystemDelete
;
587 PrivateFile
->EfiFile
.Read
= WinNtSimpleFileSystemRead
;
588 PrivateFile
->EfiFile
.Write
= WinNtSimpleFileSystemWrite
;
589 PrivateFile
->EfiFile
.GetPosition
= WinNtSimpleFileSystemGetPosition
;
590 PrivateFile
->EfiFile
.SetPosition
= WinNtSimpleFileSystemSetPosition
;
591 PrivateFile
->EfiFile
.GetInfo
= WinNtSimpleFileSystemGetInfo
;
592 PrivateFile
->EfiFile
.SetInfo
= WinNtSimpleFileSystemSetInfo
;
593 PrivateFile
->EfiFile
.Flush
= WinNtSimpleFileSystemFlush
;
594 PrivateFile
->IsValidFindBuf
= FALSE
;
599 PrivateFile
->DirHandle
= PrivateFile
->WinNtThunk
->CreateFile (
600 PrivateFile
->FilePath
,
602 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
605 FILE_FLAG_BACKUP_SEMANTICS
,
609 if (PrivateFile
->DirHandle
== INVALID_HANDLE_VALUE
) {
610 Status
= EFI_NOT_FOUND
;
615 // Find the first file under it
617 Size
= StrSize (PrivateFile
->FilePath
);
618 Size
+= StrSize (L
"\\*");
619 Status
= gBS
->AllocatePool (
622 (VOID
**)&TempFileName
624 if (EFI_ERROR (Status
)) {
627 StrCpy (TempFileName
, PrivateFile
->FilePath
);
628 StrCat (TempFileName
, L
"\\*");
630 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->FindFirstFile (TempFileName
, &PrivateFile
->FindBuf
);
631 FreePool (TempFileName
);
633 if (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
634 PrivateFile
->IsValidFindBuf
= FALSE
;
636 PrivateFile
->IsValidFindBuf
= TRUE
;
638 *Root
= &PrivateFile
->EfiFile
;
640 Status
= EFI_SUCCESS
;
643 if (EFI_ERROR (Status
)) {
645 if (PrivateFile
->FileName
) {
646 FreePool (PrivateFile
->FileName
);
649 if (PrivateFile
->FilePath
) {
650 FreePool (PrivateFile
->FilePath
);
653 FreePool (PrivateFile
);
657 gBS
->RestoreTPL (OldTpl
);
663 Count the number of Leading Dot in FileNameToken.
665 @param FileNameToken A string representing a token in the path name.
667 @return UINTN The number of leading dot in the name.
672 IN CONST CHAR16
* FileNameToken
678 while (*FileNameToken
== L
'.') {
687 IsFileNameTokenValid (
688 IN CONST CHAR16
* FileNameToken
692 if (StrStr (FileNameToken
, L
"/") != NULL
) {
694 // No L'/' in file name.
699 // If Token has all dot, the number should not exceed 2
701 Num
= CountLeadingDots (FileNameToken
);
703 if (Num
== StrLen (FileNameToken
)) {
705 // If the FileNameToken only contains a number of L'.'.
717 Return the first string token found in the indirect pointer a String named by FileName.
719 On input, FileName is a indirect pointer pointing to a String.
720 On output, FileName is a updated to point to the next character after the first
721 found L"\" or NULL if there is no L"\" found.
723 @param FileName A indirect pointer pointing to a FileName.
725 @return Token The first string token found before a L"\".
729 GetNextFileNameToken (
730 IN OUT CONST CHAR16
** FileName
736 ASSERT (**FileName
!= L
'\\');
737 ASSERT (**FileName
!= L
'\0');
739 SlashPos
= StrStr (*FileName
, L
"\\");
740 if (SlashPos
== NULL
) {
741 Token
= AllocateCopyPool (StrSize(*FileName
), *FileName
);
744 Offset
= SlashPos
- *FileName
;
745 Token
= AllocateZeroPool ((Offset
+ 1) * sizeof (CHAR16
));
746 StrnCpy (Token
, *FileName
, Offset
);
748 // Point *FileName to the next character after L'\'.
750 *FileName
= *FileName
+ Offset
+ 1;
752 // If *FileName is an empty string, then set *FileName to NULL
754 if (**FileName
== L
'\0') {
763 Check if a FileName contains only Valid Characters.
765 If FileName contains only a single L'\', return TRUE.
766 If FileName contains two adjacent L'\', return FALSE.
767 If FileName conatins L'/' , return FALSE.
768 If FielName contains more than two dots seperated with other FileName characters
769 by L'\', return FALSE. For example, L'.\...\filename.txt' is invalid path name. But L'..TwoDots\filename.txt' is valid path name.
771 @param FileName The File Name String to check.
773 @return TRUE FileName only contains valid characters.
774 @return FALSE FileName contains at least one invalid character.
780 IN CONST CHAR16
*FileName
787 // If FileName is just L'\', then it is a valid pathname.
789 if (StrCmp (FileName
, L
"\\") == 0) {
793 // We don't support two or more adjacent L'\'.
795 if (StrStr (FileName
, L
"\\\\") != NULL
) {
800 // Is FileName has a leading L"\", skip to next character.
802 if (FileName
[0] == L
'\\') {
807 Token
= GetNextFileNameToken (&FileName
);
808 Valid
= IsFileNameTokenValid (Token
);
813 } while (FileName
!= NULL
);
820 WinNtSimpleFileSystemOpen (
821 IN EFI_FILE_PROTOCOL
*This
,
822 OUT EFI_FILE_PROTOCOL
**NewHandle
,
831 Open a file relative to the source file location.
835 This - A pointer to the source file location.
837 NewHandle - Pointer to storage for the new file handle.
839 FileName - Pointer to the file name to be opened.
841 OpenMode - File open mode information.
843 Attributes - File creation attributes.
847 EFI_SUCCESS - The file was opened.
849 EFI_NOT_FOUND - The file could not be found in the volume.
851 EFI_NO_MEDIA - The device has no media.
853 EFI_MEDIA_CHANGED - The device has new media or the media is no longer supported.
855 EFI_DEVICE_ERROR - The device reported an error.
857 EFI_VOLUME_CORRUPTED - The file system structures are corrupted.
859 EFI_WRITE_PROTECTED - The volume or file is write protected.
861 EFI_ACCESS_DENIED - The service denied access to the file.
863 EFI_OUT_OF_RESOURCES - Not enough resources were available to open the file.
865 EFI_VOLUME_FULL - There is not enough space left to create the new file.
868 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
869 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
870 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
871 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
873 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
874 WIN_NT_EFI_FILE_PRIVATE
*NewPrivateFile
;
875 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE
*PrivateRoot
;
877 CHAR16
*RealFileName
;
878 CHAR16
*TempFileName
;
879 CHAR16
*ParseFileName
;
880 CHAR16
*GuardPointer
;
890 // Check for obvious invalid parameters.
892 if (This
== NULL
|| NewHandle
== NULL
|| FileName
== NULL
) {
893 return EFI_INVALID_PARAMETER
;
897 case EFI_FILE_MODE_CREATE
| EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
:
898 if (Attributes
&~EFI_FILE_VALID_ATTR
) {
899 return EFI_INVALID_PARAMETER
;
902 if (Attributes
& EFI_FILE_READ_ONLY
) {
903 return EFI_INVALID_PARAMETER
;
909 case EFI_FILE_MODE_READ
:
910 case EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
:
914 return EFI_INVALID_PARAMETER
;
918 // Init local variables
920 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
921 PrivateRoot
= WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (PrivateFile
->SimpleFileSystem
);
922 NewPrivateFile
= NULL
;
925 // Allocate buffer for FileName as the passed in FileName may be read only
927 TempFileName
= AllocatePool (StrSize (FileName
));
928 if (TempFileName
== NULL
) {
929 return EFI_OUT_OF_RESOURCES
;
931 StrCpy (TempFileName
, FileName
);
932 FileName
= TempFileName
;
934 if (FileName
[StrLen (FileName
) - 1] == L
'\\') {
935 FileName
[StrLen (FileName
) - 1] = 0;
939 // If file name does not equal to "." or ".." and not trailed with "\..",
940 // then we trim the leading/trailing blanks and trailing dots
942 if (StrCmp (FileName
, L
".") != 0 && StrCmp (FileName
, L
"..") != 0 &&
943 ((StrLen (FileName
) >= 3) ? (StrCmp (&FileName
[StrLen (FileName
) - 3], L
"\\..") != 0) : TRUE
)) {
945 // Trim leading blanks
948 for (TempFileName
= FileName
;
949 *TempFileName
!= 0 && *TempFileName
== L
' ';
953 CutPrefix (FileName
, Count
);
955 // Trim trailing blanks
957 for (TempFileName
= FileName
+ StrLen (FileName
) - 1;
958 TempFileName
>= FileName
&& (*TempFileName
== L
' ');
962 *(TempFileName
+ 1) = 0;
966 // Attempt to open the file
968 NewPrivateFile
= AllocatePool (sizeof (WIN_NT_EFI_FILE_PRIVATE
));
969 if (NewPrivateFile
== NULL
) {
970 Status
= EFI_OUT_OF_RESOURCES
;
974 CopyMem (NewPrivateFile
, PrivateFile
, sizeof (WIN_NT_EFI_FILE_PRIVATE
));
976 NewPrivateFile
->FilePath
= AllocatePool (StrSize (PrivateFile
->FileName
));
977 if (NewPrivateFile
->FilePath
== NULL
) {
978 Status
= EFI_OUT_OF_RESOURCES
;
982 if (PrivateFile
->IsDirectoryPath
) {
983 StrCpy (NewPrivateFile
->FilePath
, PrivateFile
->FileName
);
985 StrCpy (NewPrivateFile
->FilePath
, PrivateFile
->FilePath
);
988 Size
= StrSize (NewPrivateFile
->FilePath
);
989 Size
+= StrSize (L
"\\");
990 Size
+= StrSize (FileName
);
991 NewPrivateFile
->FileName
= AllocatePool (Size
);
992 if (NewPrivateFile
->FileName
== NULL
) {
993 Status
= EFI_OUT_OF_RESOURCES
;
997 if (*FileName
== L
'\\') {
998 StrCpy (NewPrivateFile
->FileName
, PrivateRoot
->FilePath
);
999 StrCat (NewPrivateFile
->FileName
, L
"\\");
1000 StrCat (NewPrivateFile
->FileName
, FileName
+ 1);
1002 StrCpy (NewPrivateFile
->FileName
, NewPrivateFile
->FilePath
);
1003 if (StrCmp (FileName
, L
"") != 0) {
1005 // In case the filename becomes empty, especially after trimming dots and blanks
1007 StrCat (NewPrivateFile
->FileName
, L
"\\");
1008 StrCat (NewPrivateFile
->FileName
, FileName
);
1012 if (!IsFileNameValid (NewPrivateFile
->FileName
)) {
1013 Status
= EFI_NOT_FOUND
;
1018 // Get rid of . and .., except leading . or ..
1022 // GuardPointer protect simplefilesystem root path not be destroyed
1024 GuardPointer
= NewPrivateFile
->FileName
+ StrLen (PrivateRoot
->FilePath
);
1028 while (!LoopFinish
) {
1032 for (ParseFileName
= GuardPointer
; *ParseFileName
; ParseFileName
++) {
1033 if (*ParseFileName
== L
'.' &&
1034 (*(ParseFileName
+ 1) == 0 || *(ParseFileName
+ 1) == L
'\\') &&
1035 *(ParseFileName
- 1) == L
'\\'
1041 CutPrefix (ParseFileName
- 1, 2);
1046 if (*ParseFileName
== L
'.' &&
1047 *(ParseFileName
+ 1) == L
'.' &&
1048 (*(ParseFileName
+ 2) == 0 || *(ParseFileName
+ 2) == L
'\\') &&
1049 *(ParseFileName
- 1) == L
'\\'
1055 while (ParseFileName
!= GuardPointer
) {
1058 if (*ParseFileName
== L
'\\') {
1064 // cut \.. and its left directory
1066 CutPrefix (ParseFileName
, Count
);
1073 RealFileName
= NewPrivateFile
->FileName
;
1074 while (EfiStrChr (RealFileName
, L
'\\') != NULL
) {
1075 RealFileName
= EfiStrChr (RealFileName
, L
'\\') + 1;
1079 if (RealFileName
!= NewPrivateFile
->FileName
) {
1080 TempChar
= *(RealFileName
- 1);
1081 *(RealFileName
- 1) = 0;
1084 FreePool (NewPrivateFile
->FilePath
);
1085 NewPrivateFile
->FilePath
= NULL
;
1086 NewPrivateFile
->FilePath
= AllocatePool (StrSize (NewPrivateFile
->FileName
));
1087 if (NewPrivateFile
->FilePath
== NULL
) {
1088 Status
= EFI_OUT_OF_RESOURCES
;
1092 StrCpy (NewPrivateFile
->FilePath
, NewPrivateFile
->FileName
);
1093 if (TempChar
!= 0) {
1094 *(RealFileName
- 1) = TempChar
;
1097 NewPrivateFile
->IsRootDirectory
= FALSE
;
1100 // Test whether file or directory
1102 if (OpenMode
& EFI_FILE_MODE_CREATE
) {
1103 if (Attributes
& EFI_FILE_DIRECTORY
) {
1104 NewPrivateFile
->IsDirectoryPath
= TRUE
;
1106 NewPrivateFile
->IsDirectoryPath
= FALSE
;
1109 NewPrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
1110 NewPrivateFile
->LHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1111 NewPrivateFile
->FileName
,
1113 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1120 if (NewPrivateFile
->LHandle
!= INVALID_HANDLE_VALUE
) {
1121 NewPrivateFile
->IsDirectoryPath
= FALSE
;
1122 NewPrivateFile
->WinNtThunk
->CloseHandle (NewPrivateFile
->LHandle
);
1124 NewPrivateFile
->IsDirectoryPath
= TRUE
;
1127 NewPrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
1130 if (OpenMode
& EFI_FILE_MODE_WRITE
) {
1131 NewPrivateFile
->IsOpenedByRead
= FALSE
;
1133 NewPrivateFile
->IsOpenedByRead
= TRUE
;
1136 Status
= EFI_SUCCESS
;
1139 // deal with directory
1141 if (NewPrivateFile
->IsDirectoryPath
) {
1143 Size
= StrSize (NewPrivateFile
->FileName
);
1144 Size
+= StrSize (L
"\\*");
1145 TempFileName
= AllocatePool (Size
);
1146 if (TempFileName
== NULL
) {
1147 Status
= EFI_OUT_OF_RESOURCES
;
1151 StrCpy (TempFileName
, NewPrivateFile
->FileName
);
1153 if ((OpenMode
& EFI_FILE_MODE_CREATE
)) {
1155 // Create a directory
1157 if (!NewPrivateFile
->WinNtThunk
->CreateDirectory (TempFileName
, NULL
)) {
1159 LastError
= PrivateFile
->WinNtThunk
->GetLastError ();
1160 if (LastError
!= ERROR_ALREADY_EXISTS
) {
1161 FreePool (TempFileName
);
1162 Status
= EFI_ACCESS_DENIED
;
1168 NewPrivateFile
->DirHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1170 NewPrivateFile
->IsOpenedByRead
? GENERIC_READ
: (GENERIC_READ
| GENERIC_WRITE
),
1171 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1174 FILE_FLAG_BACKUP_SEMANTICS
,
1178 if (NewPrivateFile
->DirHandle
== INVALID_HANDLE_VALUE
) {
1180 NewPrivateFile
->DirHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1183 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1186 FILE_FLAG_BACKUP_SEMANTICS
,
1190 if (NewPrivateFile
->DirHandle
!= INVALID_HANDLE_VALUE
) {
1191 NewPrivateFile
->WinNtThunk
->CloseHandle (NewPrivateFile
->DirHandle
);
1192 NewPrivateFile
->DirHandle
= INVALID_HANDLE_VALUE
;
1193 Status
= EFI_ACCESS_DENIED
;
1195 Status
= EFI_NOT_FOUND
;
1198 FreePool (TempFileName
);
1203 // Find the first file under it
1205 StrCat (TempFileName
, L
"\\*");
1206 NewPrivateFile
->LHandle
= NewPrivateFile
->WinNtThunk
->FindFirstFile (TempFileName
, &NewPrivateFile
->FindBuf
);
1207 FreePool (TempFileName
);
1209 if (NewPrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1210 NewPrivateFile
->IsValidFindBuf
= FALSE
;
1212 NewPrivateFile
->IsValidFindBuf
= TRUE
;
1218 if (!NewPrivateFile
->IsOpenedByRead
) {
1219 NewPrivateFile
->LHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1220 NewPrivateFile
->FileName
,
1221 GENERIC_READ
| GENERIC_WRITE
,
1222 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1224 (OpenMode
& EFI_FILE_MODE_CREATE
) ? OPEN_ALWAYS
: OPEN_EXISTING
,
1229 if (NewPrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1230 NewPrivateFile
->LHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1231 NewPrivateFile
->FileName
,
1233 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1240 if (NewPrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1241 Status
= EFI_NOT_FOUND
;
1243 Status
= EFI_ACCESS_DENIED
;
1244 NewPrivateFile
->WinNtThunk
->CloseHandle (NewPrivateFile
->LHandle
);
1245 NewPrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
1249 NewPrivateFile
->LHandle
= NewPrivateFile
->WinNtThunk
->CreateFile (
1250 NewPrivateFile
->FileName
,
1252 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1259 if (NewPrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1260 Status
= EFI_NOT_FOUND
;
1265 if ((OpenMode
& EFI_FILE_MODE_CREATE
) && Status
== EFI_SUCCESS
) {
1267 // Set the attribute
1272 Status
= WinNtSimpleFileSystemGetInfo (&NewPrivateFile
->EfiFile
, &gEfiFileInfoGuid
, &InfoSize
, Info
);
1274 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1275 Status
= EFI_DEVICE_ERROR
;
1279 Info
= AllocatePool (InfoSize
);
1281 Status
= EFI_OUT_OF_RESOURCES
;
1285 Status
= WinNtSimpleFileSystemGetInfo (&NewPrivateFile
->EfiFile
, &gEfiFileInfoGuid
, &InfoSize
, Info
);
1287 if (EFI_ERROR (Status
)) {
1292 Info
->Attribute
= Attributes
;
1294 WinNtSimpleFileSystemSetInfo (&NewPrivateFile
->EfiFile
, &gEfiFileInfoGuid
, InfoSize
, Info
);
1299 FreePool (FileName
);
1301 if (EFI_ERROR (Status
)) {
1302 if (NewPrivateFile
) {
1303 if (NewPrivateFile
->FileName
) {
1304 FreePool (NewPrivateFile
->FileName
);
1307 if (NewPrivateFile
->FilePath
) {
1308 FreePool (NewPrivateFile
->FilePath
);
1311 FreePool (NewPrivateFile
);
1314 *NewHandle
= &NewPrivateFile
->EfiFile
;
1315 if (StrCmp (NewPrivateFile
->FileName
, PrivateRoot
->FilePath
) == 0) {
1316 NewPrivateFile
->IsRootDirectory
= TRUE
;
1325 WinNtSimpleFileSystemClose (
1326 IN EFI_FILE_PROTOCOL
*This
1330 Routine Description:
1332 Close the specified file handle.
1336 This - Pointer to a returned opened file handle.
1340 EFI_SUCCESS - The file handle has been closed.
1343 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1345 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1349 return EFI_INVALID_PARAMETER
;
1352 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1354 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1356 if (PrivateFile
->LHandle
!= INVALID_HANDLE_VALUE
) {
1357 if (PrivateFile
->IsDirectoryPath
) {
1358 PrivateFile
->WinNtThunk
->FindClose (PrivateFile
->LHandle
);
1360 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->LHandle
);
1363 PrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
1366 if (PrivateFile
->IsDirectoryPath
&& PrivateFile
->DirHandle
!= INVALID_HANDLE_VALUE
) {
1367 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->DirHandle
);
1368 PrivateFile
->DirHandle
= INVALID_HANDLE_VALUE
;
1371 if (PrivateFile
->FileName
) {
1372 FreePool (PrivateFile
->FileName
);
1375 if (PrivateFile
->FilePath
) {
1376 FreePool (PrivateFile
->FilePath
);
1379 FreePool (PrivateFile
);
1381 gBS
->RestoreTPL (OldTpl
);
1388 WinNtSimpleFileSystemDelete (
1389 IN EFI_FILE_PROTOCOL
*This
1393 Routine Description:
1395 Close and delete a file.
1399 This - Pointer to a returned opened file handle.
1403 EFI_SUCCESS - The file handle was closed and deleted.
1405 EFI_WARN_DELETE_FAILURE - The handle was closed but could not be deleted.
1408 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1411 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1415 return EFI_INVALID_PARAMETER
;
1418 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1420 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1422 Status
= EFI_WARN_DELETE_FAILURE
;
1424 if (PrivateFile
->IsDirectoryPath
) {
1425 if (PrivateFile
->LHandle
!= INVALID_HANDLE_VALUE
) {
1426 PrivateFile
->WinNtThunk
->FindClose (PrivateFile
->LHandle
);
1429 if (PrivateFile
->DirHandle
!= INVALID_HANDLE_VALUE
) {
1430 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->DirHandle
);
1431 PrivateFile
->DirHandle
= INVALID_HANDLE_VALUE
;
1434 if (PrivateFile
->WinNtThunk
->RemoveDirectory (PrivateFile
->FileName
)) {
1435 Status
= EFI_SUCCESS
;
1438 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->LHandle
);
1439 PrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
1441 if (!PrivateFile
->IsOpenedByRead
) {
1442 if (PrivateFile
->WinNtThunk
->DeleteFile (PrivateFile
->FileName
)) {
1443 Status
= EFI_SUCCESS
;
1448 FreePool (PrivateFile
->FileName
);
1449 FreePool (PrivateFile
->FilePath
);
1450 FreePool (PrivateFile
);
1452 gBS
->RestoreTPL (OldTpl
);
1458 WinNtSystemTimeToEfiTime (
1459 IN SYSTEMTIME
*SystemTime
,
1460 IN TIME_ZONE_INFORMATION
*TimeZone
,
1465 Routine Description:
1467 TODO: Add function description
1471 SystemTime - TODO: add argument description
1472 TimeZone - TODO: add argument description
1473 Time - TODO: add argument description
1477 TODO: add return values
1481 Time
->Year
= (UINT16
) SystemTime
->wYear
;
1482 Time
->Month
= (UINT8
) SystemTime
->wMonth
;
1483 Time
->Day
= (UINT8
) SystemTime
->wDay
;
1484 Time
->Hour
= (UINT8
) SystemTime
->wHour
;
1485 Time
->Minute
= (UINT8
) SystemTime
->wMinute
;
1486 Time
->Second
= (UINT8
) SystemTime
->wSecond
;
1487 Time
->Nanosecond
= (UINT32
) SystemTime
->wMilliseconds
* 1000000;
1488 Time
->TimeZone
= (INT16
) TimeZone
->Bias
;
1490 if (TimeZone
->StandardDate
.wMonth
) {
1491 Time
->Daylight
= EFI_TIME_ADJUST_DAYLIGHT
;
1496 Convert the FileTime to EfiTime.
1498 @param PrivateFile Pointer to WIN_NT_EFI_FILE_PRIVATE.
1499 @param TimeZone Pointer to the current time zone.
1500 @param FileTime Pointer to file time.
1501 @param EfiTime Pointer to EFI time.
1504 WinNtFileTimeToEfiTime (
1505 IN CONST WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
,
1506 IN TIME_ZONE_INFORMATION
*TimeZone
,
1507 IN CONST FILETIME
*FileTime
,
1508 OUT EFI_TIME
*EfiTime
1511 FILETIME TempFileTime
;
1512 SYSTEMTIME SystemTime
;
1514 PrivateFile
->WinNtThunk
->FileTimeToLocalFileTime (FileTime
, &TempFileTime
);
1515 PrivateFile
->WinNtThunk
->FileTimeToSystemTime (&TempFileTime
, &SystemTime
);
1516 WinNtSystemTimeToEfiTime (&SystemTime
, TimeZone
, EfiTime
);
1521 WinNtSimpleFileSystemRead (
1522 IN EFI_FILE_PROTOCOL
*This
,
1523 IN OUT UINTN
*BufferSize
,
1528 Routine Description:
1530 Read data from a file.
1534 This - Pointer to a returned open file handle.
1536 BufferSize - On input, the size of the Buffer. On output, the number of bytes stored in the Buffer.
1538 Buffer - Pointer to the first byte of the read Buffer.
1542 EFI_SUCCESS - The data was read.
1544 EFI_NO_MEDIA - The device has no media.
1546 EFI_DEVICE_ERROR - The device reported an error.
1548 EFI_VOLUME_CORRUPTED - The file system structures are corrupted.
1550 EFI_BUFFER_TOO_SMALL - The supplied buffer size was too small to store the current directory entry.
1551 *BufferSize has been updated with the size needed to complete the request.
1554 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1556 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1562 EFI_FILE_INFO
*Info
;
1564 TIME_ZONE_INFORMATION TimeZone
;
1565 EFI_FILE_INFO
*FileInfo
;
1571 if (This
== NULL
|| BufferSize
== NULL
) {
1572 return EFI_INVALID_PARAMETER
;
1575 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1577 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1579 if (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1580 Status
= EFI_DEVICE_ERROR
;
1584 if (!PrivateFile
->IsDirectoryPath
) {
1586 if (This
->GetPosition (This
, &Pos
) != EFI_SUCCESS
) {
1587 Status
= EFI_DEVICE_ERROR
;
1591 FileInfoSize
= SIZE_OF_EFI_FILE_SYSTEM_INFO
;
1592 FileInfo
= AllocatePool (FileInfoSize
);
1594 Status
= This
->GetInfo (
1601 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1602 FreePool (FileInfo
);
1603 FileInfo
= AllocatePool (FileInfoSize
);
1604 Status
= This
->GetInfo (
1612 if (EFI_ERROR (Status
)) {
1613 Status
= EFI_DEVICE_ERROR
;
1617 FileSize
= FileInfo
->FileSize
;
1619 FreePool (FileInfo
);
1621 if (Pos
>= FileSize
) {
1623 if (Pos
== FileSize
) {
1624 Status
= EFI_SUCCESS
;
1627 Status
= EFI_DEVICE_ERROR
;
1632 Status
= PrivateFile
->WinNtThunk
->ReadFile (
1633 PrivateFile
->LHandle
,
1636 (LPDWORD
)BufferSize
,
1638 ) ? EFI_SUCCESS
: EFI_DEVICE_ERROR
;
1643 // Read on a directory. Perform a find next
1645 if (!PrivateFile
->IsValidFindBuf
) {
1647 Status
= EFI_SUCCESS
;
1651 Size
= SIZE_OF_EFI_FILE_INFO
;
1653 NameSize
= StrSize (PrivateFile
->FindBuf
.cFileName
);
1655 ResultSize
= Size
+ NameSize
;
1657 Status
= EFI_BUFFER_TOO_SMALL
;
1659 if (*BufferSize
>= ResultSize
) {
1660 Status
= EFI_SUCCESS
;
1663 ZeroMem (Info
, ResultSize
);
1665 Info
->Size
= ResultSize
;
1667 PrivateFile
->WinNtThunk
->GetTimeZoneInformation (&TimeZone
);
1668 WinNtFileTimeToEfiTime (PrivateFile
, &TimeZone
, &PrivateFile
->FindBuf
.ftCreationTime
, &Info
->CreateTime
);
1669 WinNtFileTimeToEfiTime (PrivateFile
, &TimeZone
, &PrivateFile
->FindBuf
.ftLastAccessTime
, &Info
->LastAccessTime
);
1670 WinNtFileTimeToEfiTime (PrivateFile
, &TimeZone
, &PrivateFile
->FindBuf
.ftLastWriteTime
, &Info
->ModificationTime
);
1672 Info
->FileSize
= PrivateFile
->FindBuf
.nFileSizeLow
;
1674 Info
->PhysicalSize
= PrivateFile
->FindBuf
.nFileSizeLow
;
1676 if (PrivateFile
->FindBuf
.dwFileAttributes
& FILE_ATTRIBUTE_ARCHIVE
) {
1677 Info
->Attribute
|= EFI_FILE_ARCHIVE
;
1680 if (PrivateFile
->FindBuf
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
) {
1681 Info
->Attribute
|= EFI_FILE_HIDDEN
;
1684 if (PrivateFile
->FindBuf
.dwFileAttributes
& FILE_ATTRIBUTE_SYSTEM
) {
1685 Info
->Attribute
|= EFI_FILE_SYSTEM
;
1688 if (PrivateFile
->FindBuf
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
1689 Info
->Attribute
|= EFI_FILE_READ_ONLY
;
1692 if (PrivateFile
->FindBuf
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
1693 Info
->Attribute
|= EFI_FILE_DIRECTORY
;
1696 NameSize
= NameSize
/ sizeof (WCHAR
);
1698 pw
= (WCHAR
*) (((CHAR8
*) Buffer
) + Size
);
1700 for (Index
= 0; Index
< NameSize
; Index
++) {
1701 pw
[Index
] = PrivateFile
->FindBuf
.cFileName
[Index
];
1704 if (PrivateFile
->WinNtThunk
->FindNextFile (PrivateFile
->LHandle
, &PrivateFile
->FindBuf
)) {
1705 PrivateFile
->IsValidFindBuf
= TRUE
;
1707 PrivateFile
->IsValidFindBuf
= FALSE
;
1711 *BufferSize
= ResultSize
;
1714 gBS
->RestoreTPL (OldTpl
);
1720 WinNtSimpleFileSystemWrite (
1721 IN EFI_FILE_PROTOCOL
*This
,
1722 IN OUT UINTN
*BufferSize
,
1727 Routine Description:
1729 Write data to a file.
1733 This - Pointer to an opened file handle.
1735 BufferSize - On input, the number of bytes in the Buffer to write to the file. On output, the number of bytes
1736 of data written to the file.
1738 Buffer - Pointer to the first by of data in the buffer to write to the file.
1742 EFI_SUCCESS - The data was written to the file.
1744 EFI_UNSUPPORTED - Writes to an open directory are not supported.
1746 EFI_NO_MEDIA - The device has no media.
1748 EFI_DEVICE_ERROR - The device reported an error.
1750 EFI_VOLUME_CORRUPTED - The file system structures are corrupt.
1752 EFI_WRITE_PROTECTED - The file, directory, volume, or device is write protected.
1754 EFI_ACCESS_DENIED - The file was opened read-only.
1756 EFI_VOLUME_FULL - The volume is full.
1759 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1761 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1765 if (This
== NULL
|| BufferSize
== NULL
|| Buffer
== NULL
) {
1766 return EFI_INVALID_PARAMETER
;
1769 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1771 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1773 if (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1774 Status
= EFI_DEVICE_ERROR
;
1778 if (PrivateFile
->IsDirectoryPath
) {
1779 Status
= EFI_UNSUPPORTED
;
1783 if (PrivateFile
->IsOpenedByRead
) {
1784 Status
= EFI_ACCESS_DENIED
;
1788 Status
= PrivateFile
->WinNtThunk
->WriteFile (
1789 PrivateFile
->LHandle
,
1792 (LPDWORD
)BufferSize
,
1794 ) ? EFI_SUCCESS
: EFI_DEVICE_ERROR
;
1797 gBS
->RestoreTPL (OldTpl
);
1801 // bugbug: need to access windows error reporting
1807 WinNtSimpleFileSystemSetPosition (
1808 IN EFI_FILE_PROTOCOL
*This
,
1813 Routine Description:
1815 Set a file's current position.
1819 This - Pointer to an opened file handle.
1821 Position - The byte position from the start of the file to set.
1825 EFI_SUCCESS - The file position has been changed.
1827 EFI_UNSUPPORTED - The seek request for non-zero is not supported for directories.
1830 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1833 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1841 return EFI_INVALID_PARAMETER
;
1844 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1846 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1848 if (PrivateFile
->IsDirectoryPath
) {
1849 if (Position
!= 0) {
1850 Status
= EFI_UNSUPPORTED
;
1854 Size
= StrSize (PrivateFile
->FileName
);
1855 Size
+= StrSize (L
"\\*");
1856 FileName
= AllocatePool (Size
);
1857 if (FileName
== NULL
) {
1858 Status
= EFI_OUT_OF_RESOURCES
;
1862 StrCpy (FileName
, PrivateFile
->FileName
);
1863 StrCat (FileName
, L
"\\*");
1865 if (PrivateFile
->LHandle
!= INVALID_HANDLE_VALUE
) {
1866 PrivateFile
->WinNtThunk
->FindClose (PrivateFile
->LHandle
);
1869 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->FindFirstFile (FileName
, &PrivateFile
->FindBuf
);
1871 if (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
1872 PrivateFile
->IsValidFindBuf
= FALSE
;
1874 PrivateFile
->IsValidFindBuf
= TRUE
;
1877 FreePool (FileName
);
1879 Status
= (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) ? EFI_DEVICE_ERROR
: EFI_SUCCESS
;
1881 if (Position
== (UINT64
) -1) {
1882 PosLow
= PrivateFile
->WinNtThunk
->SetFilePointer (PrivateFile
->LHandle
, (ULONG
) 0, NULL
, FILE_END
);
1884 PosHigh
= (UINT32
) RShiftU64 (Position
, 32);
1886 PosLow
= PrivateFile
->WinNtThunk
->SetFilePointer (PrivateFile
->LHandle
, (ULONG
) Position
, (PLONG
)&PosHigh
, FILE_BEGIN
);
1889 Status
= (PosLow
== 0xFFFFFFFF) ? EFI_DEVICE_ERROR
: EFI_SUCCESS
;
1893 gBS
->RestoreTPL (OldTpl
);
1899 WinNtSimpleFileSystemGetPosition (
1900 IN EFI_FILE_PROTOCOL
*This
,
1901 OUT UINT64
*Position
1905 Routine Description:
1907 Get a file's current position.
1911 This - Pointer to an opened file handle.
1913 Position - Pointer to storage for the current position.
1917 EFI_SUCCESS - The file position has been reported.
1919 EFI_UNSUPPORTED - Not valid for directories.
1922 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
1925 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
1930 if (This
== NULL
|| Position
== NULL
) {
1931 return EFI_INVALID_PARAMETER
;
1934 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1935 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
1940 if (PrivateFile
->IsDirectoryPath
) {
1942 Status
= EFI_UNSUPPORTED
;
1948 *Position
= PrivateFile
->WinNtThunk
->SetFilePointer (
1949 PrivateFile
->LHandle
,
1951 (PLONG
)&PositionHigh
,
1955 Status
= *Position
== 0xffffffff ? EFI_DEVICE_ERROR
: EFI_SUCCESS
;
1956 if (EFI_ERROR (Status
)) {
1960 PosHigh64
= PositionHigh
;
1961 *Position
+= LShiftU64 (PosHigh64
, 32);
1965 gBS
->RestoreTPL (OldTpl
);
1970 WinNtSimpleFileSystemFileInfo (
1971 IN WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
,
1972 IN OUT UINTN
*BufferSize
,
1977 Routine Description:
1979 TODO: Add function description
1983 PrivateFile - TODO: add argument description
1984 BufferSize - TODO: add argument description
1985 Buffer - TODO: add argument description
1989 TODO: add return values
1997 EFI_FILE_INFO
*Info
;
1998 BY_HANDLE_FILE_INFORMATION FileInfo
;
1999 CHAR16
*RealFileName
;
2000 CHAR16
*TempPointer
;
2001 TIME_ZONE_INFORMATION TimeZone
;
2003 Size
= SIZE_OF_EFI_FILE_INFO
;
2005 RealFileName
= PrivateFile
->FileName
;
2006 TempPointer
= RealFileName
;
2007 while (*TempPointer
) {
2008 if (*TempPointer
== '\\') {
2009 RealFileName
= TempPointer
+ 1;
2014 NameSize
= StrSize (RealFileName
);
2016 ResultSize
= Size
+ NameSize
;
2018 Status
= EFI_BUFFER_TOO_SMALL
;
2019 if (*BufferSize
>= ResultSize
) {
2020 Status
= EFI_SUCCESS
;
2023 ZeroMem (Info
, ResultSize
);
2025 Info
->Size
= ResultSize
;
2026 PrivateFile
->WinNtThunk
->GetFileInformationByHandle (
2027 PrivateFile
->IsDirectoryPath
? PrivateFile
->DirHandle
: PrivateFile
->LHandle
,
2030 Info
->FileSize
= FileInfo
.nFileSizeLow
;
2031 Info
->PhysicalSize
= Info
->FileSize
;
2033 PrivateFile
->WinNtThunk
->GetTimeZoneInformation (&TimeZone
);
2034 WinNtFileTimeToEfiTime (PrivateFile
, &TimeZone
, &FileInfo
.ftCreationTime
, &Info
->CreateTime
);
2035 WinNtFileTimeToEfiTime (PrivateFile
, &TimeZone
, &FileInfo
.ftLastAccessTime
, &Info
->LastAccessTime
);
2036 WinNtFileTimeToEfiTime (PrivateFile
, &TimeZone
, &FileInfo
.ftLastWriteTime
, &Info
->ModificationTime
);
2038 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_ARCHIVE
) {
2039 Info
->Attribute
|= EFI_FILE_ARCHIVE
;
2042 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
) {
2043 Info
->Attribute
|= EFI_FILE_HIDDEN
;
2046 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
2047 Info
->Attribute
|= EFI_FILE_READ_ONLY
;
2050 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_SYSTEM
) {
2051 Info
->Attribute
|= EFI_FILE_SYSTEM
;
2054 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
2055 Info
->Attribute
|= EFI_FILE_DIRECTORY
;
2058 if (PrivateFile
->IsDirectoryPath
) {
2059 Info
->Attribute
|= EFI_FILE_DIRECTORY
;
2062 if (PrivateFile
->IsRootDirectory
) {
2063 *((CHAR8
*) Buffer
+ Size
) = 0;
2065 CopyMem ((CHAR8
*) Buffer
+ Size
, RealFileName
, NameSize
);
2069 *BufferSize
= ResultSize
;
2075 WinNtSimpleFileSystemGetInfo (
2076 IN EFI_FILE_PROTOCOL
*This
,
2077 IN EFI_GUID
*InformationType
,
2078 IN OUT UINTN
*BufferSize
,
2083 Routine Description:
2085 Return information about a file or volume.
2089 This - Pointer to an opened file handle.
2091 InformationType - GUID describing the type of information to be returned.
2093 BufferSize - On input, the size of the information buffer. On output, the number of bytes written to the
2096 Buffer - Pointer to the first byte of the information buffer.
2100 EFI_SUCCESS - The requested information has been written into the buffer.
2102 EFI_UNSUPPORTED - The InformationType is not known.
2104 EFI_NO_MEDIA - The device has no media.
2106 EFI_DEVICE_ERROR - The device reported an error.
2108 EFI_VOLUME_CORRUPTED - The file system structures are corrupt.
2110 EFI_BUFFER_TOO_SMALL - The buffer size was too small to contain the requested information. The buffer size has
2111 been updated with the size needed to complete the requested operation.
2114 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
2117 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
2118 EFI_FILE_SYSTEM_INFO
*FileSystemInfoBuffer
;
2119 UINT32 SectorsPerCluster
;
2120 UINT32 BytesPerSector
;
2121 UINT32 FreeClusters
;
2122 UINT32 TotalClusters
;
2123 UINT32 BytesPerCluster
;
2125 BOOLEAN DriveNameFound
;
2128 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE
*PrivateRoot
;
2131 if (This
== NULL
|| InformationType
== NULL
|| BufferSize
== NULL
) {
2132 return EFI_INVALID_PARAMETER
;
2135 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
2137 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
2138 PrivateRoot
= WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (PrivateFile
->SimpleFileSystem
);
2140 Status
= EFI_UNSUPPORTED
;
2142 if (CompareGuid (InformationType
, &gEfiFileInfoGuid
)) {
2143 Status
= WinNtSimpleFileSystemFileInfo (PrivateFile
, BufferSize
, Buffer
);
2146 if (CompareGuid (InformationType
, &gEfiFileSystemInfoGuid
)) {
2147 if (*BufferSize
< SIZE_OF_EFI_FILE_SYSTEM_INFO
+ StrSize (PrivateRoot
->VolumeLabel
)) {
2148 *BufferSize
= SIZE_OF_EFI_FILE_SYSTEM_INFO
+ StrSize (PrivateRoot
->VolumeLabel
);
2149 Status
= EFI_BUFFER_TOO_SMALL
;
2153 FileSystemInfoBuffer
= (EFI_FILE_SYSTEM_INFO
*) Buffer
;
2154 FileSystemInfoBuffer
->Size
= SIZE_OF_EFI_FILE_SYSTEM_INFO
+ StrSize (PrivateRoot
->VolumeLabel
);
2155 FileSystemInfoBuffer
->ReadOnly
= FALSE
;
2158 // Try to get the drive name
2160 DriveNameFound
= FALSE
;
2161 DriveName
= AllocatePool (StrSize (PrivateFile
->FilePath
) + 1);
2162 if (DriveName
== NULL
) {
2163 Status
= EFI_OUT_OF_RESOURCES
;
2167 StrCpy (DriveName
, PrivateFile
->FilePath
);
2168 for (Index
= 0; DriveName
[Index
] != 0 && DriveName
[Index
] != ':'; Index
++) {
2172 if (DriveName
[Index
] == ':') {
2173 DriveName
[Index
+ 1] = '\\';
2174 DriveName
[Index
+ 2] = 0;
2175 DriveNameFound
= TRUE
;
2176 } else if (DriveName
[0] == '\\' && DriveName
[1] == '\\') {
2177 for (Index
= 2; DriveName
[Index
] != 0 && DriveName
[Index
] != '\\'; Index
++) {
2181 if (DriveName
[Index
] == '\\') {
2182 DriveNameFound
= TRUE
;
2183 for (Index
++; DriveName
[Index
] != 0 && DriveName
[Index
] != '\\'; Index
++) {
2187 DriveName
[Index
] = '\\';
2188 DriveName
[Index
+ 1] = 0;
2193 // Try GetDiskFreeSpace first
2195 NtStatus
= PrivateFile
->WinNtThunk
->GetDiskFreeSpace (
2196 DriveNameFound
? DriveName
: NULL
,
2197 (LPDWORD
)&SectorsPerCluster
,
2198 (LPDWORD
)&BytesPerSector
,
2199 (LPDWORD
)&FreeClusters
,
2200 (LPDWORD
)&TotalClusters
2203 FreePool (DriveName
);
2210 BytesPerCluster
= BytesPerSector
* SectorsPerCluster
;
2211 FileSystemInfoBuffer
->VolumeSize
= MultU64x32 (TotalClusters
, BytesPerCluster
);
2212 FileSystemInfoBuffer
->FreeSpace
= MultU64x32 (FreeClusters
, BytesPerCluster
);
2213 FileSystemInfoBuffer
->BlockSize
= BytesPerCluster
;
2217 // try GetDiskFreeSpaceEx then
2219 FileSystemInfoBuffer
->BlockSize
= 0;
2220 NtStatus
= PrivateFile
->WinNtThunk
->GetDiskFreeSpaceEx (
2221 PrivateFile
->FilePath
,
2222 (PULARGE_INTEGER
) (&FileSystemInfoBuffer
->FreeSpace
),
2223 (PULARGE_INTEGER
) (&FileSystemInfoBuffer
->VolumeSize
),
2227 Status
= EFI_DEVICE_ERROR
;
2232 StrCpy ((CHAR16
*) FileSystemInfoBuffer
->VolumeLabel
, PrivateRoot
->VolumeLabel
);
2233 *BufferSize
= SIZE_OF_EFI_FILE_SYSTEM_INFO
+ StrSize (PrivateRoot
->VolumeLabel
);
2234 Status
= EFI_SUCCESS
;
2237 if (CompareGuid (InformationType
, &gEfiFileSystemVolumeLabelInfoIdGuid
)) {
2238 if (*BufferSize
< StrSize (PrivateRoot
->VolumeLabel
)) {
2239 *BufferSize
= StrSize (PrivateRoot
->VolumeLabel
);
2240 Status
= EFI_BUFFER_TOO_SMALL
;
2244 StrCpy ((CHAR16
*) Buffer
, PrivateRoot
->VolumeLabel
);
2245 *BufferSize
= StrSize (PrivateRoot
->VolumeLabel
);
2246 Status
= EFI_SUCCESS
;
2250 gBS
->RestoreTPL (OldTpl
);
2256 WinNtSimpleFileSystemSetInfo (
2257 IN EFI_FILE_PROTOCOL
*This
,
2258 IN EFI_GUID
*InformationType
,
2259 IN UINTN BufferSize
,
2264 Routine Description:
2266 Set information about a file or volume.
2270 This - Pointer to an opened file handle.
2272 InformationType - GUID identifying the type of information to set.
2274 BufferSize - Number of bytes of data in the information buffer.
2276 Buffer - Pointer to the first byte of data in the information buffer.
2280 EFI_SUCCESS - The file or volume information has been updated.
2282 EFI_UNSUPPORTED - The information identifier is not recognised.
2284 EFI_NO_MEDIA - The device has no media.
2286 EFI_DEVICE_ERROR - The device reported an error.
2288 EFI_VOLUME_CORRUPTED - The file system structures are corrupt.
2290 EFI_WRITE_PROTECTED - The file, directory, volume, or device is write protected.
2292 EFI_ACCESS_DENIED - The file was opened read-only.
2294 EFI_VOLUME_FULL - The volume is full.
2296 EFI_BAD_BUFFER_SIZE - The buffer size is smaller than the type indicated by InformationType.
2299 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
2300 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
2302 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE
*PrivateRoot
;
2303 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
2304 EFI_FILE_INFO
*OldFileInfo
;
2305 EFI_FILE_INFO
*NewFileInfo
;
2311 CHAR16
*OldFileName
;
2312 CHAR16
*NewFileName
;
2313 CHAR16
*TempFileName
;
2314 CHAR16
*CharPointer
;
2315 BOOLEAN AttrChangeFlag
;
2316 BOOLEAN NameChangeFlag
;
2317 BOOLEAN SizeChangeFlag
;
2318 BOOLEAN TimeChangeFlag
;
2320 SYSTEMTIME NewCreationSystemTime
;
2321 SYSTEMTIME NewLastAccessSystemTime
;
2322 SYSTEMTIME NewLastWriteSystemTime
;
2323 FILETIME NewCreationFileTime
;
2324 FILETIME NewLastAccessFileTime
;
2325 FILETIME NewLastWriteFileTime
;
2326 WIN32_FIND_DATA FindBuf
;
2327 EFI_FILE_SYSTEM_INFO
*NewFileSystemInfo
;
2332 // Check for invalid parameters.
2334 if (This
== NULL
|| InformationType
== NULL
|| BufferSize
== 0 || Buffer
== NULL
) {
2335 return EFI_INVALID_PARAMETER
;
2338 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
2341 // Initialise locals.
2343 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
2344 PrivateRoot
= WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (PrivateFile
->SimpleFileSystem
);
2346 Status
= EFI_UNSUPPORTED
;
2347 OldFileInfo
= NewFileInfo
= NULL
;
2348 OldFileName
= NewFileName
= NULL
;
2349 AttrChangeFlag
= NameChangeFlag
= SizeChangeFlag
= TimeChangeFlag
= FALSE
;
2352 // Set file system information.
2354 if (CompareGuid (InformationType
, &gEfiFileSystemInfoGuid
)) {
2355 NewFileSystemInfo
= (EFI_FILE_SYSTEM_INFO
*) Buffer
;
2356 if (BufferSize
< SIZE_OF_EFI_FILE_SYSTEM_INFO
+ StrSize (NewFileSystemInfo
->VolumeLabel
)) {
2357 Status
= EFI_BAD_BUFFER_SIZE
;
2362 FreePool (PrivateRoot
->VolumeLabel
);
2363 PrivateRoot
->VolumeLabel
= AllocatePool (StrSize (NewFileSystemInfo
->VolumeLabel
));
2364 if (PrivateRoot
->VolumeLabel
== NULL
) {
2365 Status
= EFI_OUT_OF_RESOURCES
;
2369 StrCpy (PrivateRoot
->VolumeLabel
, NewFileSystemInfo
->VolumeLabel
);
2371 Status
= EFI_SUCCESS
;
2376 // Set volume label information.
2378 if (CompareGuid (InformationType
, &gEfiFileSystemVolumeLabelInfoIdGuid
)) {
2379 if (BufferSize
< StrSize (PrivateRoot
->VolumeLabel
)) {
2380 Status
= EFI_BAD_BUFFER_SIZE
;
2384 StrCpy (PrivateRoot
->VolumeLabel
, (CHAR16
*) Buffer
);
2386 Status
= EFI_SUCCESS
;
2390 if (!CompareGuid (InformationType
, &gEfiFileInfoGuid
)) {
2391 Status
= EFI_UNSUPPORTED
;
2395 if (BufferSize
< SIZE_OF_EFI_FILE_INFO
) {
2396 Status
= EFI_BAD_BUFFER_SIZE
;
2401 // Set file/directory information.
2405 // Check for invalid set file information parameters.
2407 NewFileInfo
= (EFI_FILE_INFO
*) Buffer
;
2409 if ((NewFileInfo
->Size
<= SIZE_OF_EFI_FILE_INFO
) ||
2410 (NewFileInfo
->Attribute
&~(EFI_FILE_VALID_ATTR
)) ||
2411 (sizeof (UINTN
) == 4 && NewFileInfo
->Size
> 0xFFFFFFFF)
2413 Status
= EFI_INVALID_PARAMETER
;
2418 // bugbug: - This is not safe. We need something like EfiStrMaxSize()
2419 // that would have an additional parameter that would be the size
2420 // of the string array just in case there are no NULL characters in
2421 // the string array.
2424 // Get current file information so we can determine what kind
2425 // of change request this is.
2428 Status
= WinNtSimpleFileSystemFileInfo (PrivateFile
, &OldInfoSize
, NULL
);
2430 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
2431 Status
= EFI_DEVICE_ERROR
;
2435 OldFileInfo
= AllocatePool (OldInfoSize
);
2436 if (OldFileInfo
== NULL
) {
2437 Status
= EFI_OUT_OF_RESOURCES
;
2441 Status
= WinNtSimpleFileSystemFileInfo (PrivateFile
, &OldInfoSize
, OldFileInfo
);
2443 if (EFI_ERROR (Status
)) {
2447 OldFileName
= AllocatePool (StrSize (PrivateFile
->FileName
));
2448 if (OldFileName
== NULL
) {
2449 Status
= EFI_OUT_OF_RESOURCES
;
2453 StrCpy (OldFileName
, PrivateFile
->FileName
);
2456 // Make full pathname from new filename and rootpath.
2458 if (NewFileInfo
->FileName
[0] == '\\') {
2459 Size
= StrSize (PrivateRoot
->FilePath
);
2460 Size
+= StrSize (L
"\\");
2461 Size
+= StrSize (NewFileInfo
->FileName
);
2462 NewFileName
= AllocatePool (Size
);
2463 if (NewFileName
== NULL
) {
2464 Status
= EFI_OUT_OF_RESOURCES
;
2468 StrCpy (NewFileName
, PrivateRoot
->FilePath
);
2469 StrCat (NewFileName
, L
"\\");
2470 StrCat (NewFileName
, NewFileInfo
->FileName
+ 1);
2472 Size
= StrSize (PrivateFile
->FilePath
);
2473 Size
+= StrSize (L
"\\");
2474 Size
+= StrSize (NewFileInfo
->FileName
);
2475 NewFileName
= AllocatePool (Size
);
2476 if (NewFileName
== NULL
) {
2477 Status
= EFI_OUT_OF_RESOURCES
;
2481 StrCpy (NewFileName
, PrivateFile
->FilePath
);
2482 StrCat (NewFileName
, L
"\\");
2483 StrCat (NewFileName
, NewFileInfo
->FileName
);
2487 // Is there an attribute change request?
2489 if (NewFileInfo
->Attribute
!= OldFileInfo
->Attribute
) {
2490 if ((NewFileInfo
->Attribute
& EFI_FILE_DIRECTORY
) != (OldFileInfo
->Attribute
& EFI_FILE_DIRECTORY
)) {
2491 Status
= EFI_INVALID_PARAMETER
;
2495 AttrChangeFlag
= TRUE
;
2499 // Is there a name change request?
2500 // bugbug: - Need EfiStrCaseCmp()
2502 if (StrCmp (NewFileInfo
->FileName
, OldFileInfo
->FileName
)) {
2503 NameChangeFlag
= TRUE
;
2507 // Is there a size change request?
2509 if (NewFileInfo
->FileSize
!= OldFileInfo
->FileSize
) {
2510 SizeChangeFlag
= TRUE
;
2514 // Is there a time stamp change request?
2516 if (!IsZero (&NewFileInfo
->CreateTime
, sizeof (EFI_TIME
)) &&
2517 CompareMem (&NewFileInfo
->CreateTime
, &OldFileInfo
->CreateTime
, sizeof (EFI_TIME
))
2519 TimeChangeFlag
= TRUE
;
2520 } else if (!IsZero (&NewFileInfo
->LastAccessTime
, sizeof (EFI_TIME
)) &&
2521 CompareMem (&NewFileInfo
->LastAccessTime
, &OldFileInfo
->LastAccessTime
, sizeof (EFI_TIME
))
2523 TimeChangeFlag
= TRUE
;
2524 } else if (!IsZero (&NewFileInfo
->ModificationTime
, sizeof (EFI_TIME
)) &&
2525 CompareMem (&NewFileInfo
->ModificationTime
, &OldFileInfo
->ModificationTime
, sizeof (EFI_TIME
))
2527 TimeChangeFlag
= TRUE
;
2531 // All done if there are no change requests being made.
2533 if (!(AttrChangeFlag
|| NameChangeFlag
|| SizeChangeFlag
|| TimeChangeFlag
)) {
2534 Status
= EFI_SUCCESS
;
2539 // Set file or directory information.
2541 OldAttr
= PrivateFile
->WinNtThunk
->GetFileAttributes (OldFileName
);
2546 if (NameChangeFlag
) {
2548 // Close the handles first
2550 if (PrivateFile
->IsOpenedByRead
) {
2551 Status
= EFI_ACCESS_DENIED
;
2555 for (CharPointer
= NewFileName
; *CharPointer
!= 0 && *CharPointer
!= L
'/'; CharPointer
++) {
2558 if (*CharPointer
!= 0) {
2559 Status
= EFI_ACCESS_DENIED
;
2563 if (PrivateFile
->LHandle
!= INVALID_HANDLE_VALUE
) {
2564 if (PrivateFile
->IsDirectoryPath
) {
2565 PrivateFile
->WinNtThunk
->FindClose (PrivateFile
->LHandle
);
2567 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->LHandle
);
2568 PrivateFile
->LHandle
= INVALID_HANDLE_VALUE
;
2572 if (PrivateFile
->IsDirectoryPath
&& PrivateFile
->DirHandle
!= INVALID_HANDLE_VALUE
) {
2573 PrivateFile
->WinNtThunk
->CloseHandle (PrivateFile
->DirHandle
);
2574 PrivateFile
->DirHandle
= INVALID_HANDLE_VALUE
;
2577 NtStatus
= PrivateFile
->WinNtThunk
->MoveFile (OldFileName
, NewFileName
);
2583 FreePool (PrivateFile
->FileName
);
2585 PrivateFile
->FileName
= AllocatePool (StrSize (NewFileName
));
2586 if (PrivateFile
->FileName
== NULL
) {
2587 Status
= EFI_OUT_OF_RESOURCES
;
2591 StrCpy (PrivateFile
->FileName
, NewFileName
);
2593 Size
= StrSize (NewFileName
);
2594 Size
+= StrSize (L
"\\*");
2595 TempFileName
= AllocatePool (Size
);
2597 StrCpy (TempFileName
, NewFileName
);
2599 if (!PrivateFile
->IsDirectoryPath
) {
2600 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->CreateFile (
2602 PrivateFile
->IsOpenedByRead
? GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
,
2603 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2610 FreePool (TempFileName
);
2613 // Flush buffers just in case
2615 if (PrivateFile
->WinNtThunk
->FlushFileBuffers (PrivateFile
->LHandle
) == 0) {
2616 Status
= EFI_DEVICE_ERROR
;
2620 PrivateFile
->DirHandle
= PrivateFile
->WinNtThunk
->CreateFile (
2622 PrivateFile
->IsOpenedByRead
? GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
,
2623 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2626 FILE_FLAG_BACKUP_SEMANTICS
,
2630 StrCat (TempFileName
, L
"\\*");
2631 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->FindFirstFile (TempFileName
, &FindBuf
);
2633 FreePool (TempFileName
);
2636 Status
= EFI_ACCESS_DENIED
;
2639 NtStatus
= PrivateFile
->WinNtThunk
->SetFileAttributes (OldFileName
, OldAttr
);
2645 Size
= StrSize (OldFileName
);
2646 Size
+= StrSize (L
"\\*");
2647 TempFileName
= AllocatePool (Size
);
2649 StrCpy (TempFileName
, OldFileName
);
2651 if (!PrivateFile
->IsDirectoryPath
) {
2652 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->CreateFile (
2654 PrivateFile
->IsOpenedByRead
? GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
,
2655 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2662 PrivateFile
->DirHandle
= PrivateFile
->WinNtThunk
->CreateFile (
2664 PrivateFile
->IsOpenedByRead
? GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
,
2665 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2668 FILE_FLAG_BACKUP_SEMANTICS
,
2672 StrCat (TempFileName
, L
"\\*");
2673 PrivateFile
->LHandle
= PrivateFile
->WinNtThunk
->FindFirstFile (TempFileName
, &FindBuf
);
2676 FreePool (TempFileName
);
2686 if (SizeChangeFlag
) {
2687 if (PrivateFile
->IsDirectoryPath
) {
2688 Status
= EFI_UNSUPPORTED
;
2692 if (PrivateFile
->IsOpenedByRead
|| OldFileInfo
->Attribute
& EFI_FILE_READ_ONLY
) {
2693 Status
= EFI_ACCESS_DENIED
;
2697 Status
= This
->GetPosition (This
, &CurPos
);
2698 if (EFI_ERROR (Status
)) {
2702 Status
= This
->SetPosition (This
, NewFileInfo
->FileSize
);
2703 if (EFI_ERROR (Status
)) {
2707 if (PrivateFile
->WinNtThunk
->SetEndOfFile (PrivateFile
->LHandle
) == 0) {
2708 Status
= EFI_DEVICE_ERROR
;
2712 Status
= This
->SetPosition (This
, CurPos
);
2713 if (EFI_ERROR (Status
)) {
2721 if (TimeChangeFlag
) {
2723 NewCreationSystemTime
.wYear
= NewFileInfo
->CreateTime
.Year
;
2724 NewCreationSystemTime
.wMonth
= NewFileInfo
->CreateTime
.Month
;
2725 NewCreationSystemTime
.wDay
= NewFileInfo
->CreateTime
.Day
;
2726 NewCreationSystemTime
.wHour
= NewFileInfo
->CreateTime
.Hour
;
2727 NewCreationSystemTime
.wMinute
= NewFileInfo
->CreateTime
.Minute
;
2728 NewCreationSystemTime
.wSecond
= NewFileInfo
->CreateTime
.Second
;
2729 NewCreationSystemTime
.wMilliseconds
= 0;
2731 if (!PrivateFile
->WinNtThunk
->SystemTimeToFileTime (
2732 &NewCreationSystemTime
,
2733 &NewCreationFileTime
2738 if (!PrivateFile
->WinNtThunk
->LocalFileTimeToFileTime (
2739 &NewCreationFileTime
,
2740 &NewCreationFileTime
2745 NewLastAccessSystemTime
.wYear
= NewFileInfo
->LastAccessTime
.Year
;
2746 NewLastAccessSystemTime
.wMonth
= NewFileInfo
->LastAccessTime
.Month
;
2747 NewLastAccessSystemTime
.wDay
= NewFileInfo
->LastAccessTime
.Day
;
2748 NewLastAccessSystemTime
.wHour
= NewFileInfo
->LastAccessTime
.Hour
;
2749 NewLastAccessSystemTime
.wMinute
= NewFileInfo
->LastAccessTime
.Minute
;
2750 NewLastAccessSystemTime
.wSecond
= NewFileInfo
->LastAccessTime
.Second
;
2751 NewLastAccessSystemTime
.wMilliseconds
= 0;
2753 if (!PrivateFile
->WinNtThunk
->SystemTimeToFileTime (
2754 &NewLastAccessSystemTime
,
2755 &NewLastAccessFileTime
2760 if (!PrivateFile
->WinNtThunk
->LocalFileTimeToFileTime (
2761 &NewLastAccessFileTime
,
2762 &NewLastAccessFileTime
2767 NewLastWriteSystemTime
.wYear
= NewFileInfo
->ModificationTime
.Year
;
2768 NewLastWriteSystemTime
.wMonth
= NewFileInfo
->ModificationTime
.Month
;
2769 NewLastWriteSystemTime
.wDay
= NewFileInfo
->ModificationTime
.Day
;
2770 NewLastWriteSystemTime
.wHour
= NewFileInfo
->ModificationTime
.Hour
;
2771 NewLastWriteSystemTime
.wMinute
= NewFileInfo
->ModificationTime
.Minute
;
2772 NewLastWriteSystemTime
.wSecond
= NewFileInfo
->ModificationTime
.Second
;
2773 NewLastWriteSystemTime
.wMilliseconds
= 0;
2775 if (!PrivateFile
->WinNtThunk
->SystemTimeToFileTime (
2776 &NewLastWriteSystemTime
,
2777 &NewLastWriteFileTime
2782 if (!PrivateFile
->WinNtThunk
->LocalFileTimeToFileTime (
2783 &NewLastWriteFileTime
,
2784 &NewLastWriteFileTime
2789 if (!PrivateFile
->WinNtThunk
->SetFileTime (
2790 PrivateFile
->IsDirectoryPath
? PrivateFile
->DirHandle
: PrivateFile
->LHandle
,
2791 &NewCreationFileTime
,
2792 &NewLastAccessFileTime
,
2793 &NewLastWriteFileTime
2795 Status
= EFI_DEVICE_ERROR
;
2802 // No matter about AttrChangeFlag, Attribute must be set.
2803 // Because operation before may cause attribute change.
2807 if (NewFileInfo
->Attribute
& EFI_FILE_ARCHIVE
) {
2808 NewAttr
|= FILE_ATTRIBUTE_ARCHIVE
;
2810 NewAttr
&= ~FILE_ATTRIBUTE_ARCHIVE
;
2813 if (NewFileInfo
->Attribute
& EFI_FILE_HIDDEN
) {
2814 NewAttr
|= FILE_ATTRIBUTE_HIDDEN
;
2816 NewAttr
&= ~FILE_ATTRIBUTE_HIDDEN
;
2819 if (NewFileInfo
->Attribute
& EFI_FILE_SYSTEM
) {
2820 NewAttr
|= FILE_ATTRIBUTE_SYSTEM
;
2822 NewAttr
&= ~FILE_ATTRIBUTE_SYSTEM
;
2825 if (NewFileInfo
->Attribute
& EFI_FILE_READ_ONLY
) {
2826 NewAttr
|= FILE_ATTRIBUTE_READONLY
;
2828 NewAttr
&= ~FILE_ATTRIBUTE_READONLY
;
2831 NtStatus
= PrivateFile
->WinNtThunk
->SetFileAttributes (NewFileName
, NewAttr
);
2834 Status
= EFI_DEVICE_ERROR
;
2839 if (OldFileInfo
!= NULL
) {
2840 FreePool (OldFileInfo
);
2843 if (OldFileName
!= NULL
) {
2844 FreePool (OldFileName
);
2847 if (NewFileName
!= NULL
) {
2848 FreePool (NewFileName
);
2851 gBS
->RestoreTPL (OldTpl
);
2857 WinNtSimpleFileSystemFlush (
2858 IN EFI_FILE_PROTOCOL
*This
2862 Routine Description:
2864 Flush all modified data to the media.
2868 This - Pointer to an opened file handle.
2872 EFI_SUCCESS - The data has been flushed.
2874 EFI_NO_MEDIA - The device has no media.
2876 EFI_DEVICE_ERROR - The device reported an error.
2878 EFI_VOLUME_CORRUPTED - The file system structures have been corrupted.
2880 EFI_WRITE_PROTECTED - The file, directory, volume, or device is write protected.
2882 EFI_ACCESS_DENIED - The file was opened read-only.
2884 EFI_VOLUME_FULL - The volume is full.
2887 // TODO: EFI_INVALID_PARAMETER - add return value to function comment
2889 BY_HANDLE_FILE_INFORMATION FileInfo
;
2890 WIN_NT_EFI_FILE_PRIVATE
*PrivateFile
;
2895 return EFI_INVALID_PARAMETER
;
2898 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
2900 PrivateFile
= WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This
);
2902 if (PrivateFile
->LHandle
== INVALID_HANDLE_VALUE
) {
2903 Status
= EFI_DEVICE_ERROR
;
2907 if (PrivateFile
->IsDirectoryPath
) {
2908 Status
= EFI_SUCCESS
;
2912 if (PrivateFile
->IsOpenedByRead
) {
2913 Status
= EFI_ACCESS_DENIED
;
2917 PrivateFile
->WinNtThunk
->GetFileInformationByHandle (PrivateFile
->LHandle
, &FileInfo
);
2919 if (FileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
2920 Status
= EFI_ACCESS_DENIED
;
2924 Status
= PrivateFile
->WinNtThunk
->FlushFileBuffers (PrivateFile
->LHandle
) ? EFI_SUCCESS
: EFI_DEVICE_ERROR
;
2927 gBS
->RestoreTPL (OldTpl
);
2930 // bugbug: - Use Windows error reporting.