]> git.proxmox.com Git - mirror_edk2.git/blame - Nt32Pkg/WinNtSimpleFileSystemDxe/WinNtSimpleFileSystem.c
Nt32Pkg/WinNtSimpleFileSystemDxe: Fix ASSERT() parsing '\'
[mirror_edk2.git] / Nt32Pkg / WinNtSimpleFileSystemDxe / WinNtSimpleFileSystem.c
CommitLineData
6ae81428 1/**@file\r
068eac81 2\r
462a3eba 3Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
8f2a5f80 4This program and the accompanying materials\r
068eac81 5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 WinNtSimpleFileSystem.c\r
15\r
16Abstract:\r
17\r
18 Produce Simple File System abstractions for directories on your PC using Win32 APIs.\r
19 The configuration of what devices to mount or emulate comes from NT\r
20 environment variables. The variables must be visible to the Microsoft*\r
21 Developer Studio for them to work.\r
22\r
23 * Other names and brands may be claimed as the property of others.\r
24\r
6ae81428 25**/\r
068eac81 26\r
27//\r
28// The package level header files this module uses\r
29//\r
30#include <Uefi.h>\r
31#include <WinNtDxe.h>\r
32//\r
33// The protocols, PPI and GUID defintions for this module\r
34//\r
35#include <Guid/FileSystemVolumeLabelInfo.h>\r
36#include <Protocol/WinNtIo.h>\r
37#include <Protocol/ComponentName.h>\r
38#include <Guid/FileInfo.h>\r
39#include <Protocol/DriverBinding.h>\r
40#include <Guid/FileSystemInfo.h>\r
41#include <Protocol/SimpleFileSystem.h>\r
42//\r
43// The Library classes this module consumes\r
44//\r
45#include <Library/DebugLib.h>\r
46#include <Library/BaseLib.h>\r
47#include <Library/UefiDriverEntryPoint.h>\r
48#include <Library/UefiLib.h>\r
49#include <Library/BaseMemoryLib.h>\r
50#include <Library/UefiBootServicesTableLib.h>\r
51#include <Library/MemoryAllocationLib.h>\r
52\r
53#include "WinNtSimpleFileSystem.h"\r
54\r
55EFI_DRIVER_BINDING_PROTOCOL gWinNtSimpleFileSystemDriverBinding = {\r
56 WinNtSimpleFileSystemDriverBindingSupported,\r
57 WinNtSimpleFileSystemDriverBindingStart,\r
58 WinNtSimpleFileSystemDriverBindingStop,\r
59 0xa,\r
60 NULL,\r
61 NULL\r
62};\r
63\r
64/**\r
65 The user Entry Point for module WinNtSimpleFileSystem. The user code starts with this function.\r
66\r
67 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
68 @param[in] SystemTable A pointer to the EFI System Table.\r
69 \r
70 @retval EFI_SUCCESS The entry point is executed successfully.\r
71 @retval other Some error occurs when executing this entry point.\r
72\r
73**/\r
74EFI_STATUS\r
75EFIAPI\r
76InitializeWinNtSimpleFileSystem(\r
77 IN EFI_HANDLE ImageHandle,\r
78 IN EFI_SYSTEM_TABLE *SystemTable\r
79 )\r
80{\r
81 EFI_STATUS Status;\r
82\r
83 //\r
84 // Install driver model protocol(s).\r
85 //\r
6c28f118 86 Status = EfiLibInstallDriverBindingComponentName2 (\r
068eac81 87 ImageHandle,\r
88 SystemTable,\r
89 &gWinNtSimpleFileSystemDriverBinding,\r
90 ImageHandle,\r
91 &gWinNtSimpleFileSystemComponentName,\r
6c28f118 92 &gWinNtSimpleFileSystemComponentName2\r
068eac81 93 );\r
94 ASSERT_EFI_ERROR (Status);\r
95\r
96\r
97 return Status;\r
98}\r
99\r
100CHAR16 *\r
101EfiStrChr (\r
102 IN CHAR16 *Str,\r
103 IN CHAR16 Chr\r
104 )\r
105/*++\r
106\r
107Routine Description:\r
108\r
109 Locate the first occurance of a character in a string.\r
110\r
111Arguments:\r
112\r
113 Str - Pointer to NULL terminated unicode string.\r
114 Chr - Character to locate.\r
115\r
116Returns:\r
117\r
118 If Str is NULL, then NULL is returned.\r
119 If Chr is not contained in Str, then NULL is returned.\r
120 If Chr is contained in Str, then a pointer to the first occurance of Chr in Str is returned.\r
121\r
122--*/\r
123{\r
124 if (Str == NULL) {\r
125 return Str;\r
126 }\r
127\r
128 while (*Str != '\0' && *Str != Chr) {\r
129 ++Str;\r
130 }\r
131\r
132 return (*Str == Chr) ? Str : NULL;\r
133}\r
134\r
135BOOLEAN\r
136IsZero (\r
137 IN VOID *Buffer,\r
138 IN UINTN Length\r
139 )\r
140/*++\r
141\r
142Routine Description:\r
143\r
144 TODO: Add function description\r
145\r
146Arguments:\r
147\r
148 Buffer - TODO: add argument description\r
149 Length - TODO: add argument description\r
150\r
151Returns:\r
152\r
153 TODO: add return values\r
154\r
155--*/\r
156{\r
157 if (Buffer == NULL || Length == 0) {\r
158 return FALSE;\r
159 }\r
160\r
161 if (*(UINT8 *) Buffer != 0) {\r
162 return FALSE;\r
163 }\r
164\r
165 if (Length > 1) {\r
166 if (!CompareMem (Buffer, (UINT8 *) Buffer + 1, Length - 1)) {\r
167 return FALSE;\r
168 }\r
169 }\r
170\r
171 return TRUE;\r
172}\r
173\r
174VOID\r
175CutPrefix (\r
176 IN CHAR16 *Str,\r
177 IN UINTN Count\r
178 )\r
179/*++\r
180\r
181Routine Description:\r
182\r
183 TODO: Add function description\r
184\r
185Arguments:\r
186\r
187 Str - TODO: add argument description\r
188 Count - TODO: add argument description\r
189\r
190Returns:\r
191\r
192 TODO: add return values\r
193\r
194--*/\r
195{\r
196 CHAR16 *Pointer;\r
197\r
198 if (StrLen (Str) < Count) {\r
199 ASSERT (0);\r
200 }\r
201\r
202 if (Count != 0) {\r
203 for (Pointer = Str; *(Pointer + Count); Pointer++) {\r
204 *Pointer = *(Pointer + Count);\r
205 }\r
206 *Pointer = *(Pointer + Count);\r
207 }\r
208}\r
209\r
210\r
211\r
212EFI_STATUS\r
213EFIAPI\r
214WinNtSimpleFileSystemDriverBindingSupported (\r
215 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
216 IN EFI_HANDLE ControllerHandle,\r
217 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
218 )\r
219/*++\r
220\r
221Routine Description:\r
222\r
223 Check to see if the driver supports a given controller.\r
224\r
225Arguments:\r
226\r
227 This - A pointer to an instance of the EFI_DRIVER_BINDING_PROTOCOL.\r
228\r
229 ControllerHandle - EFI handle of the controller to test.\r
230\r
231 RemainingDevicePath - Pointer to remaining portion of a device path.\r
232\r
233Returns:\r
234\r
235 EFI_SUCCESS - The device specified by ControllerHandle and RemainingDevicePath is supported by the driver\r
236 specified by This.\r
237\r
238 EFI_ALREADY_STARTED - The device specified by ControllerHandle and RemainingDevicePath is already being managed by\r
239 the driver specified by This.\r
240\r
241 EFI_ACCESS_DENIED - The device specified by ControllerHandle and RemainingDevicePath is already being managed by\r
242 a different driver or an application that requires exclusive access.\r
243\r
244 EFI_UNSUPPORTED - The device specified by ControllerHandle and RemainingDevicePath is not supported by the\r
245 driver specified by This.\r
246\r
247--*/\r
248{\r
249 EFI_STATUS Status;\r
250 EFI_WIN_NT_IO_PROTOCOL *WinNtIo;\r
251\r
252 //\r
253 // Open the IO Abstraction(s) needed to perform the supported test\r
254 //\r
255 Status = gBS->OpenProtocol (\r
256 ControllerHandle,\r
257 &gEfiWinNtIoProtocolGuid,\r
63941829 258 (VOID **) &WinNtIo,\r
068eac81 259 This->DriverBindingHandle,\r
260 ControllerHandle,\r
261 EFI_OPEN_PROTOCOL_BY_DRIVER\r
262 );\r
263 if (EFI_ERROR (Status)) {\r
264 return Status;\r
265 }\r
266\r
267 //\r
268 // Make sure GUID is for a File System handle.\r
269 //\r
270 Status = EFI_UNSUPPORTED;\r
271 if (CompareGuid (WinNtIo->TypeGuid, &gEfiWinNtFileSystemGuid)) {\r
272 Status = EFI_SUCCESS;\r
273 }\r
274\r
275 //\r
276 // Close the I/O Abstraction(s) used to perform the supported test\r
277 //\r
278 gBS->CloseProtocol (\r
279 ControllerHandle,\r
280 &gEfiWinNtIoProtocolGuid,\r
281 This->DriverBindingHandle,\r
282 ControllerHandle\r
283 );\r
284\r
285 return Status;\r
286}\r
287\r
288EFI_STATUS\r
289EFIAPI\r
290WinNtSimpleFileSystemDriverBindingStart (\r
291 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
292 IN EFI_HANDLE ControllerHandle,\r
293 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
294 )\r
295/*++\r
296\r
297Routine Description:\r
298\r
299 Starts a device controller or a bus controller.\r
300\r
301Arguments:\r
302\r
303 This - A pointer to an instance of the EFI_DRIVER_BINDING_PROTOCOL.\r
304\r
305 ControllerHandle - EFI handle of the controller to start.\r
306\r
307 RemainingDevicePath - Pointer to remaining portion of a device path.\r
308\r
309Returns:\r
310\r
311 EFI_SUCCESS - The device or bus controller has been started.\r
312\r
313 EFI_DEVICE_ERROR - The device could not be started due to a device failure.\r
314\r
315 EFI_OUT_OF_RESOURCES - The request could not be completed due to lack of resources.\r
316\r
317--*/\r
318{\r
319 EFI_STATUS Status;\r
320 EFI_WIN_NT_IO_PROTOCOL *WinNtIo;\r
321 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE *Private;\r
322\r
323 Private = NULL;\r
324\r
325 //\r
326 // Open the IO Abstraction(s) needed\r
327 //\r
328 Status = gBS->OpenProtocol (\r
329 ControllerHandle,\r
330 &gEfiWinNtIoProtocolGuid,\r
63941829 331 (VOID **) &WinNtIo,\r
068eac81 332 This->DriverBindingHandle,\r
333 ControllerHandle,\r
334 EFI_OPEN_PROTOCOL_BY_DRIVER\r
335 );\r
336 if (EFI_ERROR (Status)) {\r
337 return Status;\r
338 }\r
339\r
340 //\r
341 // Validate GUID\r
342 //\r
343 if (!CompareGuid (WinNtIo->TypeGuid, &gEfiWinNtFileSystemGuid)) {\r
344 Status = EFI_UNSUPPORTED;\r
345 goto Done;\r
346 }\r
347\r
348 Private = AllocatePool (sizeof (WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE));\r
349 if (Private == NULL) {\r
350 Status = EFI_OUT_OF_RESOURCES;\r
351\r
352 goto Done;\r
353 }\r
354\r
355 Private->Signature = WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_SIGNATURE;\r
356 Private->WinNtThunk = WinNtIo->WinNtThunk;\r
357\r
358 Private->FilePath = WinNtIo->EnvString;\r
359\r
360 Private->VolumeLabel = AllocatePool (StrSize (L"EFI_EMULATED"));\r
361 if (Private->VolumeLabel == NULL) {\r
362 Status = EFI_OUT_OF_RESOURCES;\r
363 goto Done;\r
364 }\r
365\r
366 StrCpy (Private->VolumeLabel, L"EFI_EMULATED");\r
367\r
368 Private->SimpleFileSystem.Revision = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;\r
369 Private->SimpleFileSystem.OpenVolume = WinNtSimpleFileSystemOpenVolume;\r
370\r
371 Private->WinNtThunk->SetErrorMode (SEM_FAILCRITICALERRORS);\r
372\r
373 Private->ControllerNameTable = NULL;\r
374\r
6c28f118 375 AddUnicodeString2 (\r
068eac81 376 "eng",\r
377 gWinNtSimpleFileSystemComponentName.SupportedLanguages,\r
378 &Private->ControllerNameTable,\r
6c28f118 379 WinNtIo->EnvString,\r
380 TRUE\r
068eac81 381 );\r
6c28f118 382 AddUnicodeString2 (\r
383 "en",\r
384 gWinNtSimpleFileSystemComponentName2.SupportedLanguages,\r
385 &Private->ControllerNameTable,\r
386 WinNtIo->EnvString,\r
387 FALSE\r
388 );\r
389\r
068eac81 390\r
391 Status = gBS->InstallMultipleProtocolInterfaces (\r
392 &ControllerHandle,\r
393 &gEfiSimpleFileSystemProtocolGuid,\r
394 &Private->SimpleFileSystem,\r
395 NULL\r
396 );\r
397\r
398Done:\r
399 if (EFI_ERROR (Status)) {\r
400\r
401 if (Private != NULL) {\r
402\r
403 FreeUnicodeStringTable (Private->ControllerNameTable);\r
404\r
405 FreePool (Private);\r
406 }\r
407\r
408 gBS->CloseProtocol (\r
409 ControllerHandle,\r
410 &gEfiWinNtIoProtocolGuid,\r
411 This->DriverBindingHandle,\r
412 ControllerHandle\r
413 );\r
414 }\r
415\r
416 return Status;\r
417}\r
418\r
419EFI_STATUS\r
420EFIAPI\r
421WinNtSimpleFileSystemDriverBindingStop (\r
422 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
423 IN EFI_HANDLE ControllerHandle,\r
424 IN UINTN NumberOfChildren,\r
425 IN EFI_HANDLE *ChildHandleBuffer\r
426 )\r
427/*++\r
428\r
429Routine Description:\r
430\r
431 TODO: Add function description\r
432\r
433Arguments:\r
434\r
435 This - A pointer to an instance of the EFI_DRIVER_BINDING_PROTOCOL.\r
436\r
437 ControllerHandle - A handle to the device to be stopped.\r
438\r
439 NumberOfChildren - The number of child device handles in ChildHandleBuffer.\r
440\r
441 ChildHandleBuffer - An array of child device handles to be freed.\r
442\r
443Returns:\r
444\r
445 EFI_SUCCESS - The device has been stopped.\r
446\r
447 EFI_DEVICE_ERROR - The device could not be stopped due to a device failure.\r
448\r
449--*/\r
450// TODO: EFI_UNSUPPORTED - add return value to function comment\r
451{\r
452 EFI_STATUS Status;\r
453 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFileSystem;\r
454 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE *Private;\r
455\r
456 //\r
457 // Get our context back\r
458 //\r
459 Status = gBS->OpenProtocol (\r
460 ControllerHandle,\r
461 &gEfiSimpleFileSystemProtocolGuid,\r
63941829 462 (VOID **) &SimpleFileSystem,\r
068eac81 463 This->DriverBindingHandle,\r
464 ControllerHandle,\r
465 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
466 );\r
467 if (EFI_ERROR (Status)) {\r
468 return EFI_UNSUPPORTED;\r
469 }\r
470\r
471 Private = WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (SimpleFileSystem);\r
472\r
473 //\r
474 // Uninstall the Simple File System Protocol from ControllerHandle\r
475 //\r
476 Status = gBS->UninstallMultipleProtocolInterfaces (\r
477 ControllerHandle,\r
478 &gEfiSimpleFileSystemProtocolGuid,\r
479 &Private->SimpleFileSystem,\r
480 NULL\r
481 );\r
482 if (!EFI_ERROR (Status)) {\r
483 Status = gBS->CloseProtocol (\r
484 ControllerHandle,\r
485 &gEfiWinNtIoProtocolGuid,\r
486 This->DriverBindingHandle,\r
487 ControllerHandle\r
488 );\r
489 }\r
490\r
491 if (!EFI_ERROR (Status)) {\r
492 //\r
493 // Free our instance data\r
494 //\r
495 FreeUnicodeStringTable (Private->ControllerNameTable);\r
496\r
497 FreePool (Private);\r
498 }\r
499\r
500 return Status;\r
501}\r
502\r
503EFI_STATUS\r
504EFIAPI\r
505WinNtSimpleFileSystemOpenVolume (\r
506 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,\r
c0cf06d6 507 OUT EFI_FILE_PROTOCOL **Root\r
068eac81 508 )\r
509/*++\r
510\r
511Routine Description:\r
512\r
513 Open the root directory on a volume.\r
514\r
515Arguments:\r
516\r
517 This - A pointer to the volume to open.\r
518\r
519 Root - A pointer to storage for the returned opened file handle of the root directory.\r
520\r
521Returns:\r
522\r
523 EFI_SUCCESS - The volume was opened.\r
524\r
525 EFI_UNSUPPORTED - The volume does not support the requested file system type.\r
526\r
527 EFI_NO_MEDIA - The device has no media.\r
528\r
529 EFI_DEVICE_ERROR - The device reported an error.\r
530\r
531 EFI_VOLUME_CORRUPTED - The file system structures are corrupted.\r
532\r
533 EFI_ACCESS_DENIED - The service denied access to the file.\r
534\r
535 EFI_OUT_OF_RESOURCES - The file volume could not be opened due to lack of resources.\r
536\r
537 EFI_MEDIA_CHANGED - The device has new media or the media is no longer supported.\r
538\r
539--*/\r
540// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
541{\r
542 EFI_STATUS Status;\r
543 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE *Private;\r
544 WIN_NT_EFI_FILE_PRIVATE *PrivateFile;\r
545 EFI_TPL OldTpl;\r
2bda59e4 546 CHAR16 *TempFileName;\r
5b368e7d 547 UINTN Size;\r
068eac81 548\r
549 if (This == NULL || Root == NULL) {\r
550 return EFI_INVALID_PARAMETER;\r
551 }\r
552\r
553 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
554\r
555 Private = WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (This);\r
556\r
557 PrivateFile = AllocatePool (sizeof (WIN_NT_EFI_FILE_PRIVATE));\r
558 if (PrivateFile == NULL) {\r
559 Status = EFI_OUT_OF_RESOURCES;\r
560 goto Done;\r
561 }\r
562\r
563 PrivateFile->FileName = AllocatePool (StrSize (Private->FilePath));\r
564 if (PrivateFile->FileName == NULL) {\r
565 Status = EFI_OUT_OF_RESOURCES;\r
566 goto Done;\r
567 }\r
568\r
569 PrivateFile->FilePath = AllocatePool (StrSize (Private->FilePath));\r
570 if (PrivateFile->FilePath == NULL) {\r
571 Status = EFI_OUT_OF_RESOURCES;\r
572 goto Done;\r
573 }\r
574\r
575 StrCpy (PrivateFile->FilePath, Private->FilePath);\r
576 StrCpy (PrivateFile->FileName, PrivateFile->FilePath);\r
577 PrivateFile->Signature = WIN_NT_EFI_FILE_PRIVATE_SIGNATURE;\r
578 PrivateFile->WinNtThunk = Private->WinNtThunk;\r
579 PrivateFile->SimpleFileSystem = This;\r
580 PrivateFile->IsRootDirectory = TRUE;\r
581 PrivateFile->IsDirectoryPath = TRUE;\r
582 PrivateFile->IsOpenedByRead = TRUE;\r
583 PrivateFile->EfiFile.Revision = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;\r
584 PrivateFile->EfiFile.Open = WinNtSimpleFileSystemOpen;\r
585 PrivateFile->EfiFile.Close = WinNtSimpleFileSystemClose;\r
586 PrivateFile->EfiFile.Delete = WinNtSimpleFileSystemDelete;\r
587 PrivateFile->EfiFile.Read = WinNtSimpleFileSystemRead;\r
588 PrivateFile->EfiFile.Write = WinNtSimpleFileSystemWrite;\r
589 PrivateFile->EfiFile.GetPosition = WinNtSimpleFileSystemGetPosition;\r
590 PrivateFile->EfiFile.SetPosition = WinNtSimpleFileSystemSetPosition;\r
591 PrivateFile->EfiFile.GetInfo = WinNtSimpleFileSystemGetInfo;\r
592 PrivateFile->EfiFile.SetInfo = WinNtSimpleFileSystemSetInfo;\r
593 PrivateFile->EfiFile.Flush = WinNtSimpleFileSystemFlush;\r
068eac81 594 PrivateFile->IsValidFindBuf = FALSE;\r
595\r
2bda59e4 596 //\r
597 // Set DirHandle\r
598 //\r
599 PrivateFile->DirHandle = PrivateFile->WinNtThunk->CreateFile (\r
600 PrivateFile->FilePath,\r
601 GENERIC_READ,\r
602 FILE_SHARE_READ | FILE_SHARE_WRITE,\r
603 NULL,\r
604 OPEN_EXISTING,\r
605 FILE_FLAG_BACKUP_SEMANTICS,\r
606 NULL\r
607 );\r
608\r
609 if (PrivateFile->DirHandle == INVALID_HANDLE_VALUE) {\r
610 Status = EFI_NOT_FOUND;\r
611 goto Done;\r
612 }\r
613\r
614 //\r
615 // Find the first file under it\r
616 //\r
5b368e7d 617 Size = StrSize (PrivateFile->FilePath);\r
618 Size += StrSize (L"\\*");\r
2bda59e4 619 Status = gBS->AllocatePool (\r
620 EfiBootServicesData,\r
5b368e7d 621 Size,\r
622 (VOID **)&TempFileName\r
2bda59e4 623 );\r
624 if (EFI_ERROR (Status)) {\r
625 goto Done;\r
626 }\r
627 StrCpy (TempFileName, PrivateFile->FilePath);\r
628 StrCat (TempFileName, L"\\*");\r
629\r
630 PrivateFile->LHandle = PrivateFile->WinNtThunk->FindFirstFile (TempFileName, &PrivateFile->FindBuf);\r
521465d2 631 FreePool (TempFileName);\r
2bda59e4 632\r
633 if (PrivateFile->LHandle == INVALID_HANDLE_VALUE) {\r
634 PrivateFile->IsValidFindBuf = FALSE;\r
635 } else {\r
636 PrivateFile->IsValidFindBuf = TRUE;\r
637 }\r
068eac81 638 *Root = &PrivateFile->EfiFile;\r
639\r
640 Status = EFI_SUCCESS;\r
641\r
642Done:\r
643 if (EFI_ERROR (Status)) {\r
644 if (PrivateFile) {\r
645 if (PrivateFile->FileName) {\r
646 FreePool (PrivateFile->FileName);\r
647 }\r
648\r
649 if (PrivateFile->FilePath) {\r
650 FreePool (PrivateFile->FilePath);\r
651 }\r
652\r
653 FreePool (PrivateFile);\r
654 }\r
655 }\r
656\r
657 gBS->RestoreTPL (OldTpl);\r
658\r
659 return Status;\r
660}\r
661\r
4d384084 662/**\r
663 Count the number of Leading Dot in FileNameToken.\r
664\r
665 @param FileNameToken A string representing a token in the path name.\r
666\r
667 @return UINTN The number of leading dot in the name.\r
668\r
669**/\r
670UINTN\r
671CountLeadingDots (\r
672 IN CONST CHAR16 * FileNameToken\r
673 )\r
674{\r
675 UINTN Num;\r
676\r
677 Num = 0;\r
678 while (*FileNameToken == L'.') {\r
679 Num++;\r
680 FileNameToken++;\r
681 }\r
682 \r
683 return Num;\r
684}\r
685\r
686BOOLEAN \r
687IsFileNameTokenValid (\r
688 IN CONST CHAR16 * FileNameToken\r
689 )\r
690{\r
691 UINTN Num;\r
692 if (StrStr (FileNameToken, L"/") != NULL) {\r
693 //\r
694 // No L'/' in file name.\r
695 //\r
696 return FALSE;\r
697 } else {\r
698 //\r
699 // If Token has all dot, the number should not exceed 2\r
700 //\r
701 Num = CountLeadingDots (FileNameToken);\r
702\r
703 if (Num == StrLen (FileNameToken)) {\r
704 //\r
705 // If the FileNameToken only contains a number of L'.'.\r
706 //\r
707 if (Num > 2) {\r
708 return FALSE;\r
709 }\r
710 }\r
711 }\r
712\r
713 return TRUE;\r
714}\r
715\r
716/**\r
717 Return the first string token found in the indirect pointer a String named by FileName.\r
718\r
719 On input, FileName is a indirect pointer pointing to a String.\r
720 On output, FileName is a updated to point to the next character after the first\r
721 found L"\" or NULL if there is no L"\" found.\r
722\r
723 @param FileName A indirect pointer pointing to a FileName.\r
724\r
725 @return Token The first string token found before a L"\".\r
726\r
727**/\r
728CHAR16 *\r
729GetNextFileNameToken (\r
730 IN OUT CONST CHAR16 ** FileName \r
731 )\r
732{\r
733 CHAR16 *SlashPos;\r
734 CHAR16 *Token;\r
735 UINTN Offset;\r
736 ASSERT (**FileName != L'\\');\r
737 ASSERT (**FileName != L'\0');\r
738\r
739 SlashPos = StrStr (*FileName, L"\\");\r
740 if (SlashPos == NULL) {\r
741 Token = AllocateCopyPool (StrSize(*FileName), *FileName);\r
742 *FileName = NULL;\r
743 } else {\r
744 Offset = SlashPos - *FileName;\r
745 Token = AllocateZeroPool ((Offset + 1) * sizeof (CHAR16));\r
746 StrnCpy (Token, *FileName, Offset);\r
747 //\r
748 // Point *FileName to the next character after L'\'.\r
749 //\r
750 *FileName = *FileName + Offset + 1;\r
462a3eba
MK
751 //\r
752 // If *FileName is an empty string, then set *FileName to NULL\r
753 //\r
754 if (**FileName == L'\0') {\r
755 *FileName = NULL;\r
756 }\r
4d384084 757 }\r
758\r
759 return Token;\r
760}\r
761\r
762/**\r
763 Check if a FileName contains only Valid Characters.\r
764\r
765 If FileName contains only a single L'\', return TRUE.\r
766 If FileName contains two adjacent L'\', return FALSE.\r
767 If FileName conatins L'/' , return FALSE.\r
768 If FielName contains more than two dots seperated with other FileName characters\r
769 by L'\', return FALSE. For example, L'.\...\filename.txt' is invalid path name. But L'..TwoDots\filename.txt' is valid path name.\r
770\r
771 @param FileName The File Name String to check.\r
772\r
773 @return TRUE FileName only contains valid characters.\r
774 @return FALSE FileName contains at least one invalid character.\r
775\r
776**/\r
777\r
778BOOLEAN\r
779IsFileNameValid (\r
780 IN CONST CHAR16 *FileName \r
781 )\r
782{\r
783 CHAR16 *Token;\r
784 BOOLEAN Valid;\r
785\r
786 //\r
787 // If FileName is just L'\', then it is a valid pathname. \r
788 //\r
789 if (StrCmp (FileName, L"\\") == 0) {\r
790 return TRUE;\r
791 }\r
792 //\r
793 // We don't support two or more adjacent L'\'.\r
794 //\r
795 if (StrStr (FileName, L"\\\\") != NULL) {\r
796 return FALSE;\r
797 }\r
798\r
799 //\r
800 // Is FileName has a leading L"\", skip to next character.\r
801 //\r
802 if (FileName [0] == L'\\') {\r
803 FileName++;\r
804 }\r
805\r
806 do {\r
807 Token = GetNextFileNameToken (&FileName);\r
808 Valid = IsFileNameTokenValid (Token);\r
809 FreePool (Token);\r
810 \r
811 if (!Valid)\r
812 return FALSE;\r
813 } while (FileName != NULL);\r
814\r
815 return TRUE;\r
816}\r
817\r
068eac81 818EFI_STATUS\r
819EFIAPI\r
820WinNtSimpleFileSystemOpen (\r
c0cf06d6 821 IN EFI_FILE_PROTOCOL *This,\r
822 OUT EFI_FILE_PROTOCOL **NewHandle,\r
823 IN CHAR16 *FileName,\r
824 IN UINT64 OpenMode,\r
825 IN UINT64 Attributes\r
068eac81 826 )\r
827/*++\r
828\r
829Routine Description:\r
830\r
831 Open a file relative to the source file location.\r
832\r
833Arguments:\r
834\r
c0cf06d6 835 This - A pointer to the source file location.\r
068eac81 836\r
837 NewHandle - Pointer to storage for the new file handle.\r
838\r
839 FileName - Pointer to the file name to be opened.\r
840\r
841 OpenMode - File open mode information.\r
842\r
843 Attributes - File creation attributes.\r
844\r
845Returns:\r
846\r
847 EFI_SUCCESS - The file was opened.\r
848\r
849 EFI_NOT_FOUND - The file could not be found in the volume.\r
850\r
851 EFI_NO_MEDIA - The device has no media.\r
852\r
853 EFI_MEDIA_CHANGED - The device has new media or the media is no longer supported.\r
854\r
855 EFI_DEVICE_ERROR - The device reported an error.\r
856\r
857 EFI_VOLUME_CORRUPTED - The file system structures are corrupted.\r
858\r
859 EFI_WRITE_PROTECTED - The volume or file is write protected.\r
860\r
861 EFI_ACCESS_DENIED - The service denied access to the file.\r
862\r
863 EFI_OUT_OF_RESOURCES - Not enough resources were available to open the file.\r
864\r
865 EFI_VOLUME_FULL - There is not enough space left to create the new file.\r
866\r
867--*/\r
868// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
869// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
870// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
871// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
872{\r
068eac81 873 WIN_NT_EFI_FILE_PRIVATE *PrivateFile;\r
874 WIN_NT_EFI_FILE_PRIVATE *NewPrivateFile;\r
875 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE *PrivateRoot;\r
876 EFI_STATUS Status;\r
877 CHAR16 *RealFileName;\r
878 CHAR16 *TempFileName;\r
879 CHAR16 *ParseFileName;\r
880 CHAR16 *GuardPointer;\r
881 CHAR16 TempChar;\r
882 DWORD LastError;\r
883 UINTN Count;\r
884 BOOLEAN LoopFinish;\r
885 UINTN InfoSize;\r
886 EFI_FILE_INFO *Info;\r
a00ec39b 887 UINTN Size;\r
068eac81 888\r
889 //\r
890 // Check for obvious invalid parameters.\r
891 //\r
892 if (This == NULL || NewHandle == NULL || FileName == NULL) {\r
893 return EFI_INVALID_PARAMETER;\r
894 }\r
895\r
896 switch (OpenMode) {\r
897 case EFI_FILE_MODE_CREATE | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE:\r
898 if (Attributes &~EFI_FILE_VALID_ATTR) {\r
899 return EFI_INVALID_PARAMETER;\r
900 }\r
901\r
902 if (Attributes & EFI_FILE_READ_ONLY) {\r
903 return EFI_INVALID_PARAMETER;\r
904 }\r
905\r
906 //\r
907 // fall through\r
908 //\r
909 case EFI_FILE_MODE_READ:\r
910 case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE:\r
911 break;\r
912\r
913 default:\r
914 return EFI_INVALID_PARAMETER;\r
915 }\r
916\r
917 //\r
918 // Init local variables\r
919 //\r
920 PrivateFile = WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);\r
921 PrivateRoot = WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (PrivateFile->SimpleFileSystem);\r
922 NewPrivateFile = NULL;\r
923\r
924 //\r
925 // Allocate buffer for FileName as the passed in FileName may be read only\r
926 //\r
927 TempFileName = AllocatePool (StrSize (FileName));\r
928 if (TempFileName == NULL) {\r
929 return EFI_OUT_OF_RESOURCES;\r
930 }\r
931 StrCpy (TempFileName, FileName);\r
932 FileName = TempFileName;\r
933\r
068eac81 934 if (FileName[StrLen (FileName) - 1] == L'\\') {\r
935 FileName[StrLen (FileName) - 1] = 0;\r
936 }\r
937\r
938 //\r
6a729dca 939 // If file name does not equal to "." or ".." and not trailed with "\..",\r
068eac81 940 // then we trim the leading/trailing blanks and trailing dots\r
941 //\r
6a729dca 942 if (StrCmp (FileName, L".") != 0 && StrCmp (FileName, L"..") != 0 && \r
943 ((StrLen (FileName) >= 3) ? (StrCmp (&FileName[StrLen (FileName) - 3], L"\\..") != 0) : TRUE)) {\r
068eac81 944 //\r
945 // Trim leading blanks\r
946 //\r
947 Count = 0;\r
948 for (TempFileName = FileName;\r
949 *TempFileName != 0 && *TempFileName == L' ';\r
950 TempFileName++) {\r
951 Count++;\r
952 }\r
953 CutPrefix (FileName, Count);\r
954 //\r
4d384084 955 // Trim trailing blanks\r
068eac81 956 //\r
957 for (TempFileName = FileName + StrLen (FileName) - 1;\r
4d384084 958 TempFileName >= FileName && (*TempFileName == L' ');\r
068eac81 959 TempFileName--) {\r
960 ;\r
961 }\r
962 *(TempFileName + 1) = 0;\r
963 }\r
964\r
965 //\r
966 // Attempt to open the file\r
967 //\r
968 NewPrivateFile = AllocatePool (sizeof (WIN_NT_EFI_FILE_PRIVATE));\r
969 if (NewPrivateFile == NULL) {\r
970 Status = EFI_OUT_OF_RESOURCES;\r
971 goto Done;\r
972 }\r
973\r
974 CopyMem (NewPrivateFile, PrivateFile, sizeof (WIN_NT_EFI_FILE_PRIVATE));\r
975\r
976 NewPrivateFile->FilePath = AllocatePool (StrSize (PrivateFile->FileName));\r
977 if (NewPrivateFile->FilePath == NULL) {\r
978 Status = EFI_OUT_OF_RESOURCES;\r
979 goto Done;\r
980 }\r
981\r
982 if (PrivateFile->IsDirectoryPath) {\r
983 StrCpy (NewPrivateFile->FilePath, PrivateFile->FileName);\r
984 } else {\r
985 StrCpy (NewPrivateFile->FilePath, PrivateFile->FilePath);\r
986 }\r
987\r
a00ec39b 988 Size = StrSize (NewPrivateFile->FilePath);\r
989 Size += StrSize (L"\\");\r
990 Size += StrSize (FileName);\r
991 NewPrivateFile->FileName = AllocatePool (Size);\r
068eac81 992 if (NewPrivateFile->FileName == NULL) {\r
993 Status = EFI_OUT_OF_RESOURCES;\r
994 goto Done;\r
995 }\r
996\r
997 if (*FileName == L'\\') {\r
998 StrCpy (NewPrivateFile->FileName, PrivateRoot->FilePath);\r
999 StrCat (NewPrivateFile->FileName, L"\\");\r
1000 StrCat (NewPrivateFile->FileName, FileName + 1);\r
1001 } else {\r
1002 StrCpy (NewPrivateFile->FileName, NewPrivateFile->FilePath);\r
1003 if (StrCmp (FileName, L"") != 0) {\r
1004 //\r
1005 // In case the filename becomes empty, especially after trimming dots and blanks\r
1006 //\r
1007 StrCat (NewPrivateFile->FileName, L"\\");\r
1008 StrCat (NewPrivateFile->FileName, FileName);\r
1009 }\r
1010 }\r
1011\r
4d384084 1012 if (!IsFileNameValid (NewPrivateFile->FileName)) {\r
1013 Status = EFI_NOT_FOUND;\r
1014 goto Done;\r
1015 }\r
1016\r
068eac81 1017 //\r
1018 // Get rid of . and .., except leading . or ..\r
1019 //\r
1020\r
1021 //\r
1022 // GuardPointer protect simplefilesystem root path not be destroyed\r
1023 //\r
1024 GuardPointer = NewPrivateFile->FileName + StrLen (PrivateRoot->FilePath);\r
1025\r
1026 LoopFinish = FALSE;\r
1027\r
1028 while (!LoopFinish) {\r
1029\r
1030 LoopFinish = TRUE;\r
1031\r
1032 for (ParseFileName = GuardPointer; *ParseFileName; ParseFileName++) {\r
1033 if (*ParseFileName == L'.' &&\r
1034 (*(ParseFileName + 1) == 0 || *(ParseFileName + 1) == L'\\') &&\r
1035 *(ParseFileName - 1) == L'\\'\r
1036 ) {\r
1037\r
1038 //\r
1039 // cut \.\r
1040 //\r
1041 CutPrefix (ParseFileName - 1, 2);\r
1042 LoopFinish = FALSE;\r
1043 break;\r
1044 }\r
1045\r
1046 if (*ParseFileName == L'.' &&\r
1047 *(ParseFileName + 1) == L'.' &&\r
1048 (*(ParseFileName + 2) == 0 || *(ParseFileName + 2) == L'\\') &&\r
1049 *(ParseFileName - 1) == L'\\'\r
1050 ) {\r
1051\r
1052 ParseFileName--;\r
1053 Count = 3;\r
1054\r
1055 while (ParseFileName != GuardPointer) {\r
1056 ParseFileName--;\r
1057 Count++;\r
1058 if (*ParseFileName == L'\\') {\r
1059 break;\r
1060 }\r
1061 }\r
1062\r
1063 //\r
1064 // cut \.. and its left directory\r
1065 //\r
1066 CutPrefix (ParseFileName, Count);\r
1067 LoopFinish = FALSE;\r
1068 break;\r
1069 }\r
1070 }\r
1071 }\r
1072\r
068eac81 1073 RealFileName = NewPrivateFile->FileName;\r
1074 while (EfiStrChr (RealFileName, L'\\') != NULL) {\r
1075 RealFileName = EfiStrChr (RealFileName, L'\\') + 1;\r
1076 }\r
1077\r
2bda59e4 1078 TempChar = 0;\r
1079 if (RealFileName != NewPrivateFile->FileName) {\r
1080 TempChar = *(RealFileName - 1);\r
1081 *(RealFileName - 1) = 0;\r
1082 }\r
1083 \r
068eac81 1084 FreePool (NewPrivateFile->FilePath);\r
1085 NewPrivateFile->FilePath = NULL;\r
1086 NewPrivateFile->FilePath = AllocatePool (StrSize (NewPrivateFile->FileName));\r
1087 if (NewPrivateFile->FilePath == NULL) {\r
1088 Status = EFI_OUT_OF_RESOURCES;\r
1089 goto Done;\r
1090 }\r
1091\r
1092 StrCpy (NewPrivateFile->FilePath, NewPrivateFile->FileName);\r
2bda59e4 1093 if (TempChar != 0) {\r
1094 *(RealFileName - 1) = TempChar;\r
1095 }\r
068eac81 1096\r
1097 NewPrivateFile->IsRootDirectory = FALSE;\r
1098\r
1099 //\r
1100 // Test whether file or directory\r
1101 //\r
1102 if (OpenMode & EFI_FILE_MODE_CREATE) {\r
1103 if (Attributes & EFI_FILE_DIRECTORY) {\r
1104 NewPrivateFile->IsDirectoryPath = TRUE;\r
1105 } else {\r
1106 NewPrivateFile->IsDirectoryPath = FALSE;\r
1107 }\r
1108 } else {\r
1109 NewPrivateFile->LHandle = INVALID_HANDLE_VALUE;\r
1110 NewPrivateFile->LHandle = NewPrivateFile->WinNtThunk->CreateFile (\r
1111 NewPrivateFile->FileName,\r
1112 GENERIC_READ,\r
1113 FILE_SHARE_READ | FILE_SHARE_WRITE,\r
1114 NULL,\r
1115 OPEN_EXISTING,\r
1116 0,\r
1117 NULL\r
1118 );\r
1119\r
1120 if (NewPrivateFile->LHandle != INVALID_HANDLE_VALUE) {\r
1121 NewPrivateFile->IsDirectoryPath = FALSE;\r
1122 NewPrivateFile->WinNtThunk->CloseHandle (NewPrivateFile->LHandle);\r
1123 } else {\r
1124 NewPrivateFile->IsDirectoryPath = TRUE;\r
1125 }\r
1126\r
1127 NewPrivateFile->LHandle = INVALID_HANDLE_VALUE;\r
1128 }\r
1129\r
1130 if (OpenMode & EFI_FILE_MODE_WRITE) {\r
1131 NewPrivateFile->IsOpenedByRead = FALSE;\r
1132 } else {\r
1133 NewPrivateFile->IsOpenedByRead = TRUE;\r
1134 }\r
1135\r
1136 Status = EFI_SUCCESS;\r
1137\r
1138 //\r
1139 // deal with directory\r
1140 //\r
1141 if (NewPrivateFile->IsDirectoryPath) {\r
1142\r
a00ec39b 1143 Size = StrSize (NewPrivateFile->FileName);\r
1144 Size += StrSize (L"\\*");\r
1145 TempFileName = AllocatePool (Size);\r
068eac81 1146 if (TempFileName == NULL) {\r
1147 Status = EFI_OUT_OF_RESOURCES;\r
1148 goto Done;\r
1149 }\r
1150\r
1151 StrCpy (TempFileName, NewPrivateFile->FileName);\r
1152\r
1153 if ((OpenMode & EFI_FILE_MODE_CREATE)) {\r
1154 //\r
1155 // Create a directory\r
1156 //\r
1157 if (!NewPrivateFile->WinNtThunk->CreateDirectory (TempFileName, NULL)) {\r
1158\r
1159 LastError = PrivateFile->WinNtThunk->GetLastError ();\r
1160 if (LastError != ERROR_ALREADY_EXISTS) {\r
1161 FreePool (TempFileName);\r
1162 Status = EFI_ACCESS_DENIED;\r
1163 goto Done;\r
1164 }\r
1165 }\r
1166 }\r
1167\r
1168 NewPrivateFile->DirHandle = NewPrivateFile->WinNtThunk->CreateFile (\r
1169 TempFileName,\r
1170 NewPrivateFile->IsOpenedByRead ? GENERIC_READ : (GENERIC_READ | GENERIC_WRITE),\r
1171 FILE_SHARE_READ | FILE_SHARE_WRITE,\r
1172 NULL,\r
1173 OPEN_EXISTING,\r
1174 FILE_FLAG_BACKUP_SEMANTICS,\r
1175 NULL\r
1176 );\r
1177\r
1178 if (NewPrivateFile->DirHandle == INVALID_HANDLE_VALUE) {\r
1179\r
1180 NewPrivateFile->DirHandle = NewPrivateFile->WinNtThunk->CreateFile (\r
1181 TempFileName,\r
1182 GENERIC_READ,\r
1183 FILE_SHARE_READ | FILE_SHARE_WRITE,\r
1184 NULL,\r
1185 OPEN_EXISTING,\r
1186 FILE_FLAG_BACKUP_SEMANTICS,\r
1187 NULL\r
1188 );\r
1189\r
1190 if (NewPrivateFile->DirHandle != INVALID_HANDLE_VALUE) {\r
1191 NewPrivateFile->WinNtThunk->CloseHandle (NewPrivateFile->DirHandle);\r
1192 NewPrivateFile->DirHandle = INVALID_HANDLE_VALUE;\r
1193 Status = EFI_ACCESS_DENIED;\r
1194 } else {\r
1195 Status = EFI_NOT_FOUND;\r
1196 }\r
1197\r
521465d2 1198 FreePool (TempFileName);\r
068eac81 1199 goto Done;\r
1200 }\r
1201\r
1202 //\r
1203 // Find the first file under it\r
1204 //\r
1205 StrCat (TempFileName, L"\\*");\r
1206 NewPrivateFile->LHandle = NewPrivateFile->WinNtThunk->FindFirstFile (TempFileName, &NewPrivateFile->FindBuf);\r
521465d2 1207 FreePool (TempFileName);\r
068eac81 1208\r
1209 if (NewPrivateFile->LHandle == INVALID_HANDLE_VALUE) {\r
1210 NewPrivateFile->IsValidFindBuf = FALSE;\r
1211 } else {\r
1212 NewPrivateFile->IsValidFindBuf = TRUE;\r
1213 }\r
1214 } else {\r
1215 //\r
1216 // deal with file\r
1217 //\r
1218 if (!NewPrivateFile->IsOpenedByRead) {\r
1219 NewPrivateFile->LHandle = NewPrivateFile->WinNtThunk->CreateFile (\r
1220 NewPrivateFile->FileName,\r
1221 GENERIC_READ | GENERIC_WRITE,\r
1222 FILE_SHARE_READ | FILE_SHARE_WRITE,\r
1223 NULL,\r
1224 (OpenMode & EFI_FILE_MODE_CREATE) ? OPEN_ALWAYS : OPEN_EXISTING,\r
1225 0,\r
1226 NULL\r
1227 );\r
1228\r
1229 if (NewPrivateFile->LHandle == INVALID_HANDLE_VALUE) {\r
1230 NewPrivateFile->LHandle = NewPrivateFile->WinNtThunk->CreateFile (\r
1231 NewPrivateFile->FileName,\r
1232 GENERIC_READ,\r
1233 FILE_SHARE_READ | FILE_SHARE_WRITE,\r
1234 NULL,\r
1235 OPEN_EXISTING,\r
1236 0,\r
1237 NULL\r
1238 );\r
1239\r
1240 if (NewPrivateFile->LHandle == INVALID_HANDLE_VALUE) {\r
1241 Status = EFI_NOT_FOUND;\r
1242 } else {\r
1243 Status = EFI_ACCESS_DENIED;\r
1244 NewPrivateFile->WinNtThunk->CloseHandle (NewPrivateFile->LHandle);\r
1245 NewPrivateFile->LHandle = INVALID_HANDLE_VALUE;\r
1246 }\r
1247 }\r
1248 } else {\r
1249 NewPrivateFile->LHandle = NewPrivateFile->WinNtThunk->CreateFile (\r
1250 NewPrivateFile->FileName,\r
1251 GENERIC_READ,\r
1252 FILE_SHARE_READ | FILE_SHARE_WRITE,\r
1253 NULL,\r
1254 OPEN_EXISTING,\r
1255 0,\r
1256 NULL\r
1257 );\r
1258\r
1259 if (NewPrivateFile->LHandle == INVALID_HANDLE_VALUE) {\r
1260 Status = EFI_NOT_FOUND;\r
1261 }\r
1262 }\r
1263 }\r
1264\r
1265 if ((OpenMode & EFI_FILE_MODE_CREATE) && Status == EFI_SUCCESS) {\r
1266 //\r
1267 // Set the attribute\r
1268 //\r
1269 InfoSize = 0;\r
1270 Info = NULL;\r
1271\r
1272 Status = WinNtSimpleFileSystemGetInfo (&NewPrivateFile->EfiFile, &gEfiFileInfoGuid, &InfoSize, Info);\r
1273\r
1274 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1275 Status = EFI_DEVICE_ERROR;\r
1276 goto Done;\r
1277 }\r
1278\r
1279 Info = AllocatePool (InfoSize);\r
1280 if (Info == NULL) {\r
1281 Status = EFI_OUT_OF_RESOURCES;\r
1282 goto Done;\r
1283 }\r
1284\r
1285 Status = WinNtSimpleFileSystemGetInfo (&NewPrivateFile->EfiFile, &gEfiFileInfoGuid, &InfoSize, Info);\r
1286\r
1287 if (EFI_ERROR (Status)) {\r
521465d2 1288 FreePool (Info);\r
068eac81 1289 goto Done;\r
1290 }\r
1291\r
1292 Info->Attribute = Attributes;\r
1293\r
1294 WinNtSimpleFileSystemSetInfo (&NewPrivateFile->EfiFile, &gEfiFileInfoGuid, InfoSize, Info);\r
521465d2 1295 FreePool (Info);\r
068eac81 1296 }\r
1297\r
6a729dca 1298Done:\r
068eac81 1299 FreePool (FileName);\r
1300\r
1301 if (EFI_ERROR (Status)) {\r
1302 if (NewPrivateFile) {\r
1303 if (NewPrivateFile->FileName) {\r
1304 FreePool (NewPrivateFile->FileName);\r
1305 }\r
1306\r
1307 if (NewPrivateFile->FilePath) {\r
1308 FreePool (NewPrivateFile->FilePath);\r
1309 }\r
1310\r
1311 FreePool (NewPrivateFile);\r
1312 }\r
1313 } else {\r
1314 *NewHandle = &NewPrivateFile->EfiFile;\r
2bda59e4 1315 if (StrCmp (NewPrivateFile->FileName, PrivateRoot->FilePath) == 0) {\r
1316 NewPrivateFile->IsRootDirectory = TRUE;\r
1317 } \r
068eac81 1318 }\r
1319\r
1320 return Status;\r
1321}\r
1322\r
1323EFI_STATUS\r
1324EFIAPI\r
1325WinNtSimpleFileSystemClose (\r
c0cf06d6 1326 IN EFI_FILE_PROTOCOL *This\r
068eac81 1327 )\r
1328/*++\r
1329\r
1330Routine Description:\r
1331\r
1332 Close the specified file handle.\r
1333\r
1334Arguments:\r
1335\r
1336 This - Pointer to a returned opened file handle.\r
1337\r
1338Returns:\r
1339\r
1340 EFI_SUCCESS - The file handle has been closed.\r
1341\r
1342--*/\r
1343// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
1344{\r
1345 WIN_NT_EFI_FILE_PRIVATE *PrivateFile;\r
1346 EFI_TPL OldTpl;\r
1347\r
1348 if (This == NULL) {\r
1349 return EFI_INVALID_PARAMETER;\r
1350 }\r
1351\r
1352 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1353\r
1354 PrivateFile = WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);\r
1355\r
1356 if (PrivateFile->LHandle != INVALID_HANDLE_VALUE) {\r
1357 if (PrivateFile->IsDirectoryPath) {\r
1358 PrivateFile->WinNtThunk->FindClose (PrivateFile->LHandle);\r
1359 } else {\r
1360 PrivateFile->WinNtThunk->CloseHandle (PrivateFile->LHandle);\r
1361 }\r
1362\r
1363 PrivateFile->LHandle = INVALID_HANDLE_VALUE;\r
1364 }\r
1365\r
1366 if (PrivateFile->IsDirectoryPath && PrivateFile->DirHandle != INVALID_HANDLE_VALUE) {\r
1367 PrivateFile->WinNtThunk->CloseHandle (PrivateFile->DirHandle);\r
1368 PrivateFile->DirHandle = INVALID_HANDLE_VALUE;\r
1369 }\r
1370\r
1371 if (PrivateFile->FileName) {\r
1372 FreePool (PrivateFile->FileName);\r
1373 }\r
1374\r
521465d2
RN
1375 if (PrivateFile->FilePath) {\r
1376 FreePool (PrivateFile->FilePath);\r
1377 }\r
1378\r
068eac81 1379 FreePool (PrivateFile);\r
1380\r
1381 gBS->RestoreTPL (OldTpl);\r
1382\r
1383 return EFI_SUCCESS;\r
1384}\r
1385\r
1386EFI_STATUS\r
1387EFIAPI\r
1388WinNtSimpleFileSystemDelete (\r
c0cf06d6 1389 IN EFI_FILE_PROTOCOL *This\r
068eac81 1390 )\r
1391/*++\r
1392\r
1393Routine Description:\r
1394\r
1395 Close and delete a file.\r
1396\r
1397Arguments:\r
1398\r
1399 This - Pointer to a returned opened file handle.\r
1400\r
1401Returns:\r
1402\r
1403 EFI_SUCCESS - The file handle was closed and deleted.\r
1404\r
1405 EFI_WARN_DELETE_FAILURE - The handle was closed but could not be deleted.\r
1406\r
1407--*/\r
1408// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
1409{\r
1410 EFI_STATUS Status;\r
1411 WIN_NT_EFI_FILE_PRIVATE *PrivateFile;\r
1412 EFI_TPL OldTpl;\r
1413\r
1414 if (This == NULL) {\r
1415 return EFI_INVALID_PARAMETER;\r
1416 }\r
1417\r
1418 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1419\r
1420 PrivateFile = WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);\r
1421\r
1422 Status = EFI_WARN_DELETE_FAILURE;\r
1423\r
1424 if (PrivateFile->IsDirectoryPath) {\r
1425 if (PrivateFile->LHandle != INVALID_HANDLE_VALUE) {\r
1426 PrivateFile->WinNtThunk->FindClose (PrivateFile->LHandle);\r
1427 }\r
1428\r
1429 if (PrivateFile->DirHandle != INVALID_HANDLE_VALUE) {\r
1430 PrivateFile->WinNtThunk->CloseHandle (PrivateFile->DirHandle);\r
1431 PrivateFile->DirHandle = INVALID_HANDLE_VALUE;\r
1432 }\r
1433\r
1434 if (PrivateFile->WinNtThunk->RemoveDirectory (PrivateFile->FileName)) {\r
1435 Status = EFI_SUCCESS;\r
1436 }\r
1437 } else {\r
1438 PrivateFile->WinNtThunk->CloseHandle (PrivateFile->LHandle);\r
1439 PrivateFile->LHandle = INVALID_HANDLE_VALUE;\r
1440\r
1441 if (!PrivateFile->IsOpenedByRead) {\r
1442 if (PrivateFile->WinNtThunk->DeleteFile (PrivateFile->FileName)) {\r
1443 Status = EFI_SUCCESS;\r
1444 }\r
1445 }\r
1446 }\r
1447\r
1448 FreePool (PrivateFile->FileName);\r
521465d2 1449 FreePool (PrivateFile->FilePath);\r
068eac81 1450 FreePool (PrivateFile);\r
1451\r
1452 gBS->RestoreTPL (OldTpl);\r
1453\r
1454 return Status;\r
1455}\r
1456\r
068eac81 1457VOID\r
1458WinNtSystemTimeToEfiTime (\r
1459 IN SYSTEMTIME *SystemTime,\r
1460 IN TIME_ZONE_INFORMATION *TimeZone,\r
1461 OUT EFI_TIME *Time\r
1462 )\r
1463/*++\r
1464\r
1465Routine Description:\r
1466\r
1467 TODO: Add function description\r
1468\r
1469Arguments:\r
1470\r
1471 SystemTime - TODO: add argument description\r
1472 TimeZone - TODO: add argument description\r
1473 Time - TODO: add argument description\r
1474\r
1475Returns:\r
1476\r
1477 TODO: add return values\r
1478\r
1479--*/\r
1480{\r
1481 Time->Year = (UINT16) SystemTime->wYear;\r
1482 Time->Month = (UINT8) SystemTime->wMonth;\r
1483 Time->Day = (UINT8) SystemTime->wDay;\r
1484 Time->Hour = (UINT8) SystemTime->wHour;\r
1485 Time->Minute = (UINT8) SystemTime->wMinute;\r
1486 Time->Second = (UINT8) SystemTime->wSecond;\r
1487 Time->Nanosecond = (UINT32) SystemTime->wMilliseconds * 1000000;\r
1488 Time->TimeZone = (INT16) TimeZone->Bias;\r
1489\r
1490 if (TimeZone->StandardDate.wMonth) {\r
1491 Time->Daylight = EFI_TIME_ADJUST_DAYLIGHT;\r
1492 }\r
1493}\r
1494\r
1495EFI_STATUS\r
1496EFIAPI\r
1497WinNtSimpleFileSystemRead (\r
c0cf06d6 1498 IN EFI_FILE_PROTOCOL *This,\r
1499 IN OUT UINTN *BufferSize,\r
1500 OUT VOID *Buffer\r
068eac81 1501 )\r
1502/*++\r
1503\r
1504Routine Description:\r
1505\r
1506 Read data from a file.\r
1507\r
1508Arguments:\r
1509\r
1510 This - Pointer to a returned open file handle.\r
1511\r
1512 BufferSize - On input, the size of the Buffer. On output, the number of bytes stored in the Buffer.\r
1513\r
1514 Buffer - Pointer to the first byte of the read Buffer.\r
1515\r
1516Returns:\r
1517\r
1518 EFI_SUCCESS - The data was read.\r
1519\r
1520 EFI_NO_MEDIA - The device has no media.\r
1521\r
1522 EFI_DEVICE_ERROR - The device reported an error.\r
1523\r
1524 EFI_VOLUME_CORRUPTED - The file system structures are corrupted.\r
1525\r
1526 EFI_BUFFER_TOO_SMALL - The supplied buffer size was too small to store the current directory entry.\r
1527 *BufferSize has been updated with the size needed to complete the request.\r
1528\r
1529--*/\r
1530// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
1531{\r
1532 WIN_NT_EFI_FILE_PRIVATE *PrivateFile;\r
1533 EFI_STATUS Status;\r
1534 UINTN Size;\r
1535 UINTN NameSize;\r
1536 UINTN ResultSize;\r
1537 UINTN Index;\r
1538 SYSTEMTIME SystemTime;\r
1539 EFI_FILE_INFO *Info;\r
1540 WCHAR *pw;\r
1541 TIME_ZONE_INFORMATION TimeZone;\r
1542 EFI_FILE_INFO *FileInfo;\r
1543 UINT64 Pos;\r
1544 UINT64 FileSize;\r
1545 UINTN FileInfoSize;\r
1546 EFI_TPL OldTpl;\r
1547\r
1548 if (This == NULL || BufferSize == NULL) {\r
1549 return EFI_INVALID_PARAMETER;\r
1550 }\r
1551\r
1552 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1553\r
1554 PrivateFile = WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);\r
1555\r
1556 if (PrivateFile->LHandle == INVALID_HANDLE_VALUE) {\r
1557 Status = EFI_DEVICE_ERROR;\r
1558 goto Done;\r
1559 }\r
1560\r
1561 if (!PrivateFile->IsDirectoryPath) {\r
1562\r
1563 if (This->GetPosition (This, &Pos) != EFI_SUCCESS) {\r
1564 Status = EFI_DEVICE_ERROR;\r
1565 goto Done;\r
1566 }\r
1567\r
1568 FileInfoSize = SIZE_OF_EFI_FILE_SYSTEM_INFO;\r
1569 FileInfo = AllocatePool (FileInfoSize);\r
1570\r
1571 Status = This->GetInfo (\r
1572 This,\r
1573 &gEfiFileInfoGuid,\r
1574 &FileInfoSize,\r
1575 FileInfo\r
1576 );\r
1577\r
1578 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1579 FreePool (FileInfo);\r
1580 FileInfo = AllocatePool (FileInfoSize);\r
1581 Status = This->GetInfo (\r
1582 This,\r
1583 &gEfiFileInfoGuid,\r
1584 &FileInfoSize,\r
1585 FileInfo\r
1586 );\r
1587 }\r
1588\r
1589 if (EFI_ERROR (Status)) {\r
1590 Status = EFI_DEVICE_ERROR;\r
1591 goto Done;\r
1592 }\r
1593\r
1594 FileSize = FileInfo->FileSize;\r
1595\r
1596 FreePool (FileInfo);\r
1597\r
1598 if (Pos >= FileSize) {\r
1599 *BufferSize = 0;\r
1600 if (Pos == FileSize) {\r
1601 Status = EFI_SUCCESS;\r
1602 goto Done;\r
1603 } else {\r
1604 Status = EFI_DEVICE_ERROR;\r
1605 goto Done;\r
1606 }\r
1607 }\r
1608\r
1609 Status = PrivateFile->WinNtThunk->ReadFile (\r
a00ec39b 1610 PrivateFile->LHandle,\r
1611 Buffer,\r
f66a43b2 1612 (DWORD)*BufferSize,\r
a00ec39b 1613 (LPDWORD)BufferSize,\r
1614 NULL\r
1615 ) ? EFI_SUCCESS : EFI_DEVICE_ERROR;\r
068eac81 1616 goto Done;\r
1617 }\r
1618\r
1619 //\r
1620 // Read on a directory. Perform a find next\r
1621 //\r
1622 if (!PrivateFile->IsValidFindBuf) {\r
1623 *BufferSize = 0;\r
1624 Status = EFI_SUCCESS;\r
1625 goto Done;\r
1626 }\r
1627\r
1628 Size = SIZE_OF_EFI_FILE_INFO;\r
1629\r
1630 NameSize = StrSize (PrivateFile->FindBuf.cFileName);\r
1631\r
1632 ResultSize = Size + NameSize;\r
1633\r
1634 Status = EFI_BUFFER_TOO_SMALL;\r
1635\r
1636 if (*BufferSize >= ResultSize) {\r
1637 Status = EFI_SUCCESS;\r
1638\r
1639 Info = Buffer;\r
1640 ZeroMem (Info, ResultSize);\r
1641\r
1642 Info->Size = ResultSize;\r
1643\r
1644 PrivateFile->WinNtThunk->GetTimeZoneInformation (&TimeZone);\r
1645\r
1646 PrivateFile->WinNtThunk->FileTimeToLocalFileTime (\r
1647 &PrivateFile->FindBuf.ftCreationTime,\r
1648 &PrivateFile->FindBuf.ftCreationTime\r
1649 );\r
1650\r
1651 PrivateFile->WinNtThunk->FileTimeToSystemTime (&PrivateFile->FindBuf.ftCreationTime, &SystemTime);\r
1652\r
1653 WinNtSystemTimeToEfiTime (&SystemTime, &TimeZone, &Info->CreateTime);\r
1654\r
1655 PrivateFile->WinNtThunk->FileTimeToLocalFileTime (\r
1656 &PrivateFile->FindBuf.ftLastWriteTime,\r
1657 &PrivateFile->FindBuf.ftLastWriteTime\r
1658 );\r
1659\r
1660 PrivateFile->WinNtThunk->FileTimeToSystemTime (&PrivateFile->FindBuf.ftLastWriteTime, &SystemTime);\r
1661\r
1662 WinNtSystemTimeToEfiTime (&SystemTime, &TimeZone, &Info->ModificationTime);\r
1663\r
1664 Info->FileSize = PrivateFile->FindBuf.nFileSizeLow;\r
1665\r
1666 Info->PhysicalSize = PrivateFile->FindBuf.nFileSizeLow;\r
1667\r
1668 if (PrivateFile->FindBuf.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {\r
1669 Info->Attribute |= EFI_FILE_ARCHIVE;\r
1670 }\r
1671\r
1672 if (PrivateFile->FindBuf.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {\r
1673 Info->Attribute |= EFI_FILE_HIDDEN;\r
1674 }\r
1675\r
1676 if (PrivateFile->FindBuf.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {\r
1677 Info->Attribute |= EFI_FILE_SYSTEM;\r
1678 }\r
1679\r
1680 if (PrivateFile->FindBuf.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {\r
1681 Info->Attribute |= EFI_FILE_READ_ONLY;\r
1682 }\r
1683\r
1684 if (PrivateFile->FindBuf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {\r
1685 Info->Attribute |= EFI_FILE_DIRECTORY;\r
1686 }\r
1687\r
1688 NameSize = NameSize / sizeof (WCHAR);\r
1689\r
1690 pw = (WCHAR *) (((CHAR8 *) Buffer) + Size);\r
1691\r
1692 for (Index = 0; Index < NameSize; Index++) {\r
1693 pw[Index] = PrivateFile->FindBuf.cFileName[Index];\r
1694 }\r
1695\r
1696 if (PrivateFile->WinNtThunk->FindNextFile (PrivateFile->LHandle, &PrivateFile->FindBuf)) {\r
1697 PrivateFile->IsValidFindBuf = TRUE;\r
1698 } else {\r
1699 PrivateFile->IsValidFindBuf = FALSE;\r
1700 }\r
1701 }\r
1702\r
1703 *BufferSize = ResultSize;\r
1704\r
1705Done:\r
1706 gBS->RestoreTPL (OldTpl);\r
1707 return Status;\r
1708}\r
1709\r
1710EFI_STATUS\r
1711EFIAPI\r
1712WinNtSimpleFileSystemWrite (\r
c0cf06d6 1713 IN EFI_FILE_PROTOCOL *This,\r
1714 IN OUT UINTN *BufferSize,\r
1715 IN VOID *Buffer\r
068eac81 1716 )\r
1717/*++\r
1718\r
1719Routine Description:\r
1720\r
1721 Write data to a file.\r
1722\r
1723Arguments:\r
1724\r
1725 This - Pointer to an opened file handle.\r
1726\r
1727 BufferSize - On input, the number of bytes in the Buffer to write to the file. On output, the number of bytes\r
1728 of data written to the file.\r
1729\r
1730 Buffer - Pointer to the first by of data in the buffer to write to the file.\r
1731\r
1732Returns:\r
1733\r
1734 EFI_SUCCESS - The data was written to the file.\r
1735\r
1736 EFI_UNSUPPORTED - Writes to an open directory are not supported.\r
1737\r
1738 EFI_NO_MEDIA - The device has no media.\r
1739\r
1740 EFI_DEVICE_ERROR - The device reported an error.\r
1741\r
1742 EFI_VOLUME_CORRUPTED - The file system structures are corrupt.\r
1743\r
1744 EFI_WRITE_PROTECTED - The file, directory, volume, or device is write protected.\r
1745\r
1746 EFI_ACCESS_DENIED - The file was opened read-only.\r
1747\r
1748 EFI_VOLUME_FULL - The volume is full.\r
1749\r
1750--*/\r
1751// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
1752{\r
1753 WIN_NT_EFI_FILE_PRIVATE *PrivateFile;\r
1754 EFI_STATUS Status;\r
1755 EFI_TPL OldTpl;\r
1756\r
1757 if (This == NULL || BufferSize == NULL || Buffer == NULL) {\r
1758 return EFI_INVALID_PARAMETER;\r
1759 }\r
1760\r
1761 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1762\r
1763 PrivateFile = WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);\r
1764\r
1765 if (PrivateFile->LHandle == INVALID_HANDLE_VALUE) {\r
1766 Status = EFI_DEVICE_ERROR;\r
1767 goto Done;\r
1768 }\r
1769\r
1770 if (PrivateFile->IsDirectoryPath) {\r
1771 Status = EFI_UNSUPPORTED;\r
1772 goto Done;\r
1773 }\r
1774\r
1775 if (PrivateFile->IsOpenedByRead) {\r
1776 Status = EFI_ACCESS_DENIED;\r
1777 goto Done;\r
1778 }\r
1779\r
1780 Status = PrivateFile->WinNtThunk->WriteFile (\r
a00ec39b 1781 PrivateFile->LHandle,\r
1782 Buffer,\r
f66a43b2 1783 (DWORD)*BufferSize,\r
a00ec39b 1784 (LPDWORD)BufferSize,\r
1785 NULL\r
1786 ) ? EFI_SUCCESS : EFI_DEVICE_ERROR;\r
068eac81 1787\r
1788Done:\r
1789 gBS->RestoreTPL (OldTpl);\r
1790 return Status;\r
1791\r
1792 //\r
1793 // bugbug: need to access windows error reporting\r
1794 //\r
1795}\r
1796\r
1797EFI_STATUS\r
1798EFIAPI\r
1799WinNtSimpleFileSystemSetPosition (\r
c0cf06d6 1800 IN EFI_FILE_PROTOCOL *This,\r
1801 IN UINT64 Position\r
068eac81 1802 )\r
1803/*++\r
1804\r
1805Routine Description:\r
1806\r
1807 Set a file's current position.\r
1808\r
1809Arguments:\r
1810\r
1811 This - Pointer to an opened file handle.\r
1812\r
1813 Position - The byte position from the start of the file to set.\r
1814\r
1815Returns:\r
1816\r
1817 EFI_SUCCESS - The file position has been changed.\r
1818\r
1819 EFI_UNSUPPORTED - The seek request for non-zero is not supported for directories.\r
1820\r
1821--*/\r
1822// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
1823{\r
1824 EFI_STATUS Status;\r
1825 WIN_NT_EFI_FILE_PRIVATE *PrivateFile;\r
1826 UINT32 PosLow;\r
1827 UINT32 PosHigh;\r
1828 CHAR16 *FileName;\r
1829 EFI_TPL OldTpl;\r
a00ec39b 1830 UINTN Size;\r
068eac81 1831\r
1832 if (This == NULL) {\r
1833 return EFI_INVALID_PARAMETER;\r
1834 }\r
1835\r
1836 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1837\r
1838 PrivateFile = WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);\r
1839\r
1840 if (PrivateFile->IsDirectoryPath) {\r
1841 if (Position != 0) {\r
1842 Status = EFI_UNSUPPORTED;\r
1843 goto Done;\r
1844 }\r
1845\r
a00ec39b 1846 Size = StrSize (PrivateFile->FileName);\r
1847 Size += StrSize (L"\\*");\r
1848 FileName = AllocatePool (Size);\r
068eac81 1849 if (FileName == NULL) {\r
1850 Status = EFI_OUT_OF_RESOURCES;\r
1851 goto Done;\r
1852 }\r
1853\r
1854 StrCpy (FileName, PrivateFile->FileName);\r
1855 StrCat (FileName, L"\\*");\r
1856\r
1857 if (PrivateFile->LHandle != INVALID_HANDLE_VALUE) {\r
1858 PrivateFile->WinNtThunk->FindClose (PrivateFile->LHandle);\r
1859 }\r
1860\r
1861 PrivateFile->LHandle = PrivateFile->WinNtThunk->FindFirstFile (FileName, &PrivateFile->FindBuf);\r
1862\r
1863 if (PrivateFile->LHandle == INVALID_HANDLE_VALUE) {\r
1864 PrivateFile->IsValidFindBuf = FALSE;\r
1865 } else {\r
1866 PrivateFile->IsValidFindBuf = TRUE;\r
1867 }\r
1868\r
1869 FreePool (FileName);\r
1870\r
1871 Status = (PrivateFile->LHandle == INVALID_HANDLE_VALUE) ? EFI_DEVICE_ERROR : EFI_SUCCESS;\r
1872 } else {\r
1873 if (Position == (UINT64) -1) {\r
1874 PosLow = PrivateFile->WinNtThunk->SetFilePointer (PrivateFile->LHandle, (ULONG) 0, NULL, FILE_END);\r
1875 } else {\r
1876 PosHigh = (UINT32) RShiftU64 (Position, 32);\r
1877\r
a00ec39b 1878 PosLow = PrivateFile->WinNtThunk->SetFilePointer (PrivateFile->LHandle, (ULONG) Position, (PLONG)&PosHigh, FILE_BEGIN);\r
068eac81 1879 }\r
1880\r
1881 Status = (PosLow == 0xFFFFFFFF) ? EFI_DEVICE_ERROR : EFI_SUCCESS;\r
1882 }\r
1883\r
1884Done:\r
1885 gBS->RestoreTPL (OldTpl);\r
1886 return Status;\r
1887}\r
1888\r
1889EFI_STATUS\r
1890EFIAPI\r
1891WinNtSimpleFileSystemGetPosition (\r
c0cf06d6 1892 IN EFI_FILE_PROTOCOL *This,\r
1893 OUT UINT64 *Position\r
068eac81 1894 )\r
1895/*++\r
1896\r
1897Routine Description:\r
1898\r
1899 Get a file's current position.\r
1900\r
1901Arguments:\r
1902\r
1903 This - Pointer to an opened file handle.\r
1904\r
1905 Position - Pointer to storage for the current position.\r
1906\r
1907Returns:\r
1908\r
1909 EFI_SUCCESS - The file position has been reported.\r
1910\r
1911 EFI_UNSUPPORTED - Not valid for directories.\r
1912\r
1913--*/\r
1914// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
1915{\r
1916 EFI_STATUS Status;\r
1917 WIN_NT_EFI_FILE_PRIVATE *PrivateFile;\r
1918 INT32 PositionHigh;\r
1919 UINT64 PosHigh64;\r
1920 EFI_TPL OldTpl;\r
1921\r
1922 if (This == NULL || Position == NULL) {\r
1923 return EFI_INVALID_PARAMETER;\r
1924 }\r
1925\r
1926 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1927 PrivateFile = WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);\r
1928\r
1929 PositionHigh = 0;\r
1930 PosHigh64 = 0;\r
1931\r
1932 if (PrivateFile->IsDirectoryPath) {\r
1933\r
1934 Status = EFI_UNSUPPORTED;\r
1935 goto Done;\r
1936\r
1937 } else {\r
1938\r
1939 PositionHigh = 0;\r
1940 *Position = PrivateFile->WinNtThunk->SetFilePointer (\r
a00ec39b 1941 PrivateFile->LHandle,\r
1942 0,\r
1943 (PLONG)&PositionHigh,\r
1944 FILE_CURRENT\r
1945 );\r
068eac81 1946\r
1947 Status = *Position == 0xffffffff ? EFI_DEVICE_ERROR : EFI_SUCCESS;\r
1948 if (EFI_ERROR (Status)) {\r
1949 goto Done;\r
1950 }\r
1951\r
1952 PosHigh64 = PositionHigh;\r
1953 *Position += LShiftU64 (PosHigh64, 32);\r
1954 }\r
1955\r
1956Done:\r
1957 gBS->RestoreTPL (OldTpl);\r
1958 return Status;\r
1959}\r
1960\r
068eac81 1961EFI_STATUS\r
1962WinNtSimpleFileSystemFileInfo (\r
1963 IN WIN_NT_EFI_FILE_PRIVATE *PrivateFile,\r
1964 IN OUT UINTN *BufferSize,\r
1965 OUT VOID *Buffer\r
1966 )\r
1967/*++\r
1968\r
1969Routine Description:\r
1970\r
1971 TODO: Add function description\r
1972\r
1973Arguments:\r
1974\r
1975 PrivateFile - TODO: add argument description\r
1976 BufferSize - TODO: add argument description\r
1977 Buffer - TODO: add argument description\r
1978\r
1979Returns:\r
1980\r
1981 TODO: add return values\r
1982\r
1983--*/\r
1984{\r
1985 EFI_STATUS Status;\r
1986 UINTN Size;\r
1987 UINTN NameSize;\r
1988 UINTN ResultSize;\r
1989 EFI_FILE_INFO *Info;\r
1990 BY_HANDLE_FILE_INFORMATION FileInfo;\r
1991 SYSTEMTIME SystemTime;\r
1992 CHAR16 *RealFileName;\r
1993 CHAR16 *TempPointer;\r
068eac81 1994\r
1995 Size = SIZE_OF_EFI_FILE_INFO;\r
3643e8e7
ED
1996\r
1997 RealFileName = PrivateFile->FileName;\r
1998 TempPointer = RealFileName;\r
1999 while (*TempPointer) {\r
2000 if (*TempPointer == '\\') {\r
2001 RealFileName = TempPointer + 1;\r
2002 }\r
2003\r
2004 TempPointer++;\r
2005 }\r
2006 NameSize = StrSize (RealFileName);\r
2007\r
2008 ResultSize = Size + NameSize; \r
068eac81 2009\r
2010 Status = EFI_BUFFER_TOO_SMALL;\r
2011 if (*BufferSize >= ResultSize) {\r
2012 Status = EFI_SUCCESS;\r
2013\r
2014 Info = Buffer;\r
2015 ZeroMem (Info, ResultSize);\r
2016\r
2017 Info->Size = ResultSize;\r
2018 PrivateFile->WinNtThunk->GetFileInformationByHandle (\r
2019 PrivateFile->IsDirectoryPath ? PrivateFile->DirHandle : PrivateFile->LHandle,\r
2020 &FileInfo\r
2021 );\r
2022 Info->FileSize = FileInfo.nFileSizeLow;\r
2023 Info->PhysicalSize = Info->FileSize;\r
2024\r
f515069c 2025 PrivateFile->WinNtThunk->FileTimeToLocalFileTime(&FileInfo.ftCreationTime, &FileInfo.ftCreationTime);\r
068eac81 2026 PrivateFile->WinNtThunk->FileTimeToSystemTime (&FileInfo.ftCreationTime, &SystemTime);\r
2027 Info->CreateTime.Year = SystemTime.wYear;\r
2028 Info->CreateTime.Month = (UINT8) SystemTime.wMonth;\r
2029 Info->CreateTime.Day = (UINT8) SystemTime.wDay;\r
2030 Info->CreateTime.Hour = (UINT8) SystemTime.wHour;\r
2031 Info->CreateTime.Minute = (UINT8) SystemTime.wMinute;\r
2032 Info->CreateTime.Second = (UINT8) SystemTime.wSecond;\r
2033\r
f515069c 2034 PrivateFile->WinNtThunk->FileTimeToLocalFileTime(&FileInfo.ftLastAccessTime, &FileInfo.ftLastAccessTime);\r
068eac81 2035 PrivateFile->WinNtThunk->FileTimeToSystemTime (&FileInfo.ftLastAccessTime, &SystemTime);\r
2036 Info->LastAccessTime.Year = SystemTime.wYear;\r
2037 Info->LastAccessTime.Month = (UINT8) SystemTime.wMonth;\r
2038 Info->LastAccessTime.Day = (UINT8) SystemTime.wDay;\r
2039 Info->LastAccessTime.Hour = (UINT8) SystemTime.wHour;\r
2040 Info->LastAccessTime.Minute = (UINT8) SystemTime.wMinute;\r
2041 Info->LastAccessTime.Second = (UINT8) SystemTime.wSecond;\r
2042\r
f515069c 2043 PrivateFile->WinNtThunk->FileTimeToLocalFileTime(&FileInfo.ftLastWriteTime, &FileInfo.ftLastWriteTime);\r
068eac81 2044 PrivateFile->WinNtThunk->FileTimeToSystemTime (&FileInfo.ftLastWriteTime, &SystemTime);\r
2045 Info->ModificationTime.Year = SystemTime.wYear;\r
2046 Info->ModificationTime.Month = (UINT8) SystemTime.wMonth;\r
2047 Info->ModificationTime.Day = (UINT8) SystemTime.wDay;\r
2048 Info->ModificationTime.Hour = (UINT8) SystemTime.wHour;\r
2049 Info->ModificationTime.Minute = (UINT8) SystemTime.wMinute;\r
2050 Info->ModificationTime.Second = (UINT8) SystemTime.wSecond;\r
2051\r
2052 if (FileInfo.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {\r
2053 Info->Attribute |= EFI_FILE_ARCHIVE;\r
2054 }\r
2055\r
2056 if (FileInfo.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {\r
2057 Info->Attribute |= EFI_FILE_HIDDEN;\r
2058 }\r
2059\r
2060 if (FileInfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {\r
2061 Info->Attribute |= EFI_FILE_READ_ONLY;\r
2062 }\r
2063\r
2064 if (FileInfo.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {\r
2065 Info->Attribute |= EFI_FILE_SYSTEM;\r
2066 }\r
2067\r
2068 if (FileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {\r
2069 Info->Attribute |= EFI_FILE_DIRECTORY;\r
2070 }\r
2071\r
2072 if (PrivateFile->IsDirectoryPath) {\r
2073 Info->Attribute |= EFI_FILE_DIRECTORY;\r
2074 }\r
2075\r
068eac81 2076 if (PrivateFile->IsRootDirectory) {\r
2077 *((CHAR8 *) Buffer + Size) = 0;\r
2078 } else {\r
2079 CopyMem ((CHAR8 *) Buffer + Size, RealFileName, NameSize);\r
2080 }\r
068eac81 2081 }\r
2082\r
2083 *BufferSize = ResultSize;\r
2084 return Status;\r
2085}\r
2086\r
2087EFI_STATUS\r
2088EFIAPI\r
2089WinNtSimpleFileSystemGetInfo (\r
c0cf06d6 2090 IN EFI_FILE_PROTOCOL *This,\r
2091 IN EFI_GUID *InformationType,\r
2092 IN OUT UINTN *BufferSize,\r
2093 OUT VOID *Buffer\r
068eac81 2094 )\r
2095/*++\r
2096\r
2097Routine Description:\r
2098\r
2099 Return information about a file or volume.\r
2100\r
2101Arguments:\r
2102\r
2103 This - Pointer to an opened file handle.\r
2104\r
2105 InformationType - GUID describing the type of information to be returned.\r
2106\r
2107 BufferSize - On input, the size of the information buffer. On output, the number of bytes written to the\r
2108 information buffer.\r
2109\r
2110 Buffer - Pointer to the first byte of the information buffer.\r
2111\r
2112Returns:\r
2113\r
2114 EFI_SUCCESS - The requested information has been written into the buffer.\r
2115\r
2116 EFI_UNSUPPORTED - The InformationType is not known.\r
2117\r
2118 EFI_NO_MEDIA - The device has no media.\r
2119\r
2120 EFI_DEVICE_ERROR - The device reported an error.\r
2121\r
2122 EFI_VOLUME_CORRUPTED - The file system structures are corrupt.\r
2123\r
2124 EFI_BUFFER_TOO_SMALL - The buffer size was too small to contain the requested information. The buffer size has\r
2125 been updated with the size needed to complete the requested operation.\r
2126\r
2127--*/\r
2128// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
2129{\r
2130 EFI_STATUS Status;\r
2131 WIN_NT_EFI_FILE_PRIVATE *PrivateFile;\r
2132 EFI_FILE_SYSTEM_INFO *FileSystemInfoBuffer;\r
2133 UINT32 SectorsPerCluster;\r
2134 UINT32 BytesPerSector;\r
2135 UINT32 FreeClusters;\r
2136 UINT32 TotalClusters;\r
2137 UINT32 BytesPerCluster;\r
2138 CHAR16 *DriveName;\r
2139 BOOLEAN DriveNameFound;\r
2140 BOOL NtStatus;\r
2141 UINTN Index;\r
2142 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE *PrivateRoot;\r
2143 EFI_TPL OldTpl;\r
2144\r
2145 if (This == NULL || InformationType == NULL || BufferSize == NULL) {\r
2146 return EFI_INVALID_PARAMETER;\r
2147 }\r
2148\r
2149 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
2150\r
2151 PrivateFile = WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);\r
2152 PrivateRoot = WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (PrivateFile->SimpleFileSystem);\r
2153\r
2154 Status = EFI_UNSUPPORTED;\r
2155\r
2156 if (CompareGuid (InformationType, &gEfiFileInfoGuid)) {\r
2157 Status = WinNtSimpleFileSystemFileInfo (PrivateFile, BufferSize, Buffer);\r
2158 }\r
2159\r
2160 if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid)) {\r
2161 if (*BufferSize < SIZE_OF_EFI_FILE_SYSTEM_INFO + StrSize (PrivateRoot->VolumeLabel)) {\r
2162 *BufferSize = SIZE_OF_EFI_FILE_SYSTEM_INFO + StrSize (PrivateRoot->VolumeLabel);\r
2163 Status = EFI_BUFFER_TOO_SMALL;\r
2164 goto Done;\r
2165 }\r
2166\r
2167 FileSystemInfoBuffer = (EFI_FILE_SYSTEM_INFO *) Buffer;\r
2168 FileSystemInfoBuffer->Size = SIZE_OF_EFI_FILE_SYSTEM_INFO + StrSize (PrivateRoot->VolumeLabel);\r
2169 FileSystemInfoBuffer->ReadOnly = FALSE;\r
2170\r
2171 //\r
2172 // Try to get the drive name\r
2173 //\r
2174 DriveNameFound = FALSE;\r
2175 DriveName = AllocatePool (StrSize (PrivateFile->FilePath) + 1);\r
2176 if (DriveName == NULL) {\r
2177 Status = EFI_OUT_OF_RESOURCES;\r
2178 goto Done;\r
2179 }\r
2180\r
2181 StrCpy (DriveName, PrivateFile->FilePath);\r
2182 for (Index = 0; DriveName[Index] != 0 && DriveName[Index] != ':'; Index++) {\r
2183 ;\r
2184 }\r
2185\r
2186 if (DriveName[Index] == ':') {\r
2187 DriveName[Index + 1] = '\\';\r
2188 DriveName[Index + 2] = 0;\r
2189 DriveNameFound = TRUE;\r
2190 } else if (DriveName[0] == '\\' && DriveName[1] == '\\') {\r
2191 for (Index = 2; DriveName[Index] != 0 && DriveName[Index] != '\\'; Index++) {\r
2192 ;\r
2193 }\r
2194\r
2195 if (DriveName[Index] == '\\') {\r
2196 DriveNameFound = TRUE;\r
2197 for (Index++; DriveName[Index] != 0 && DriveName[Index] != '\\'; Index++) {\r
2198 ;\r
2199 }\r
2200\r
2201 DriveName[Index] = '\\';\r
2202 DriveName[Index + 1] = 0;\r
2203 }\r
2204 }\r
2205\r
2206 //\r
2207 // Try GetDiskFreeSpace first\r
2208 //\r
2209 NtStatus = PrivateFile->WinNtThunk->GetDiskFreeSpace (\r
2210 DriveNameFound ? DriveName : NULL,\r
a00ec39b 2211 (LPDWORD)&SectorsPerCluster,\r
2212 (LPDWORD)&BytesPerSector,\r
2213 (LPDWORD)&FreeClusters,\r
2214 (LPDWORD)&TotalClusters\r
068eac81 2215 );\r
2216 if (DriveName) {\r
2217 FreePool (DriveName);\r
2218 }\r
2219\r
2220 if (NtStatus) {\r
2221 //\r
2222 // Succeeded\r
2223 //\r
2224 BytesPerCluster = BytesPerSector * SectorsPerCluster;\r
2225 FileSystemInfoBuffer->VolumeSize = MultU64x32 (TotalClusters, BytesPerCluster);\r
2226 FileSystemInfoBuffer->FreeSpace = MultU64x32 (FreeClusters, BytesPerCluster);\r
2227 FileSystemInfoBuffer->BlockSize = BytesPerCluster;\r
2228\r
2229 } else {\r
2230 //\r
2231 // try GetDiskFreeSpaceEx then\r
2232 //\r
2233 FileSystemInfoBuffer->BlockSize = 0;\r
2234 NtStatus = PrivateFile->WinNtThunk->GetDiskFreeSpaceEx (\r
2235 PrivateFile->FilePath,\r
2236 (PULARGE_INTEGER) (&FileSystemInfoBuffer->FreeSpace),\r
2237 (PULARGE_INTEGER) (&FileSystemInfoBuffer->VolumeSize),\r
2238 NULL\r
2239 );\r
2240 if (!NtStatus) {\r
2241 Status = EFI_DEVICE_ERROR;\r
2242 goto Done;\r
2243 }\r
2244 }\r
2245\r
2246 StrCpy ((CHAR16 *) FileSystemInfoBuffer->VolumeLabel, PrivateRoot->VolumeLabel);\r
2247 *BufferSize = SIZE_OF_EFI_FILE_SYSTEM_INFO + StrSize (PrivateRoot->VolumeLabel);\r
2248 Status = EFI_SUCCESS;\r
2249 }\r
2250\r
2251 if (CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {\r
2252 if (*BufferSize < StrSize (PrivateRoot->VolumeLabel)) {\r
2253 *BufferSize = StrSize (PrivateRoot->VolumeLabel);\r
2254 Status = EFI_BUFFER_TOO_SMALL;\r
2255 goto Done;\r
2256 }\r
2257\r
2258 StrCpy ((CHAR16 *) Buffer, PrivateRoot->VolumeLabel);\r
2259 *BufferSize = StrSize (PrivateRoot->VolumeLabel);\r
2260 Status = EFI_SUCCESS;\r
2261 }\r
2262\r
2263Done:\r
2264 gBS->RestoreTPL (OldTpl);\r
2265 return Status;\r
2266}\r
2267\r
2268EFI_STATUS\r
2269EFIAPI\r
2270WinNtSimpleFileSystemSetInfo (\r
c0cf06d6 2271 IN EFI_FILE_PROTOCOL*This,\r
068eac81 2272 IN EFI_GUID *InformationType,\r
2273 IN UINTN BufferSize,\r
2274 IN VOID *Buffer\r
2275 )\r
2276/*++\r
2277\r
2278Routine Description:\r
2279\r
2280 Set information about a file or volume.\r
2281\r
2282Arguments:\r
2283\r
2284 This - Pointer to an opened file handle.\r
2285\r
2286 InformationType - GUID identifying the type of information to set.\r
2287\r
2288 BufferSize - Number of bytes of data in the information buffer.\r
2289\r
2290 Buffer - Pointer to the first byte of data in the information buffer.\r
2291\r
2292Returns:\r
2293\r
2294 EFI_SUCCESS - The file or volume information has been updated.\r
2295\r
2296 EFI_UNSUPPORTED - The information identifier is not recognised.\r
2297\r
2298 EFI_NO_MEDIA - The device has no media.\r
2299\r
2300 EFI_DEVICE_ERROR - The device reported an error.\r
2301\r
2302 EFI_VOLUME_CORRUPTED - The file system structures are corrupt.\r
2303\r
2304 EFI_WRITE_PROTECTED - The file, directory, volume, or device is write protected.\r
2305\r
2306 EFI_ACCESS_DENIED - The file was opened read-only.\r
2307\r
2308 EFI_VOLUME_FULL - The volume is full.\r
2309\r
2310 EFI_BAD_BUFFER_SIZE - The buffer size is smaller than the type indicated by InformationType.\r
2311\r
2312--*/\r
2313// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
2314// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
2315{\r
2316 WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE *PrivateRoot;\r
2317 WIN_NT_EFI_FILE_PRIVATE *PrivateFile;\r
2318 EFI_FILE_INFO *OldFileInfo;\r
2319 EFI_FILE_INFO *NewFileInfo;\r
2320 EFI_STATUS Status;\r
2321 UINTN OldInfoSize;\r
2322 INTN NtStatus;\r
2323 UINT32 NewAttr;\r
2324 UINT32 OldAttr;\r
2325 CHAR16 *OldFileName;\r
2326 CHAR16 *NewFileName;\r
2327 CHAR16 *TempFileName;\r
2328 CHAR16 *CharPointer;\r
2329 BOOLEAN AttrChangeFlag;\r
2330 BOOLEAN NameChangeFlag;\r
2331 BOOLEAN SizeChangeFlag;\r
2332 BOOLEAN TimeChangeFlag;\r
2333 UINT64 CurPos;\r
2334 SYSTEMTIME NewCreationSystemTime;\r
2335 SYSTEMTIME NewLastAccessSystemTime;\r
2336 SYSTEMTIME NewLastWriteSystemTime;\r
2337 FILETIME NewCreationFileTime;\r
2338 FILETIME NewLastAccessFileTime;\r
2339 FILETIME NewLastWriteFileTime;\r
2340 WIN32_FIND_DATA FindBuf;\r
2341 EFI_FILE_SYSTEM_INFO *NewFileSystemInfo;\r
2342 EFI_TPL OldTpl;\r
a00ec39b 2343 UINTN Size;\r
068eac81 2344\r
2345 //\r
2346 // Check for invalid parameters.\r
2347 //\r
2348 if (This == NULL || InformationType == NULL || BufferSize == 0 || Buffer == NULL) {\r
2349 return EFI_INVALID_PARAMETER;\r
2350 }\r
2351\r
2352 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
2353\r
2354 //\r
2355 // Initialise locals.\r
2356 //\r
2357 PrivateFile = WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);\r
2358 PrivateRoot = WIN_NT_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (PrivateFile->SimpleFileSystem);\r
2359\r
2360 Status = EFI_UNSUPPORTED;\r
2361 OldFileInfo = NewFileInfo = NULL;\r
2362 OldFileName = NewFileName = NULL;\r
2363 AttrChangeFlag = NameChangeFlag = SizeChangeFlag = TimeChangeFlag = FALSE;\r
2364\r
2365 //\r
2366 // Set file system information.\r
2367 //\r
2368 if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid)) {\r
b80022da 2369 NewFileSystemInfo = (EFI_FILE_SYSTEM_INFO *) Buffer;\r
2370 if (BufferSize < SIZE_OF_EFI_FILE_SYSTEM_INFO + StrSize (NewFileSystemInfo->VolumeLabel)) {\r
068eac81 2371 Status = EFI_BAD_BUFFER_SIZE;\r
2372 goto Done;\r
2373 }\r
2374\r
068eac81 2375\r
2376 FreePool (PrivateRoot->VolumeLabel);\r
2377 PrivateRoot->VolumeLabel = AllocatePool (StrSize (NewFileSystemInfo->VolumeLabel));\r
2378 if (PrivateRoot->VolumeLabel == NULL) {\r
2379 Status = EFI_OUT_OF_RESOURCES;\r
2380 goto Done;\r
2381 }\r
2382\r
2383 StrCpy (PrivateRoot->VolumeLabel, NewFileSystemInfo->VolumeLabel);\r
2384\r
2385 Status = EFI_SUCCESS;\r
2386 goto Done;\r
2387 }\r
2388\r
2389 //\r
2390 // Set volume label information.\r
2391 //\r
2392 if (CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {\r
2393 if (BufferSize < StrSize (PrivateRoot->VolumeLabel)) {\r
2394 Status = EFI_BAD_BUFFER_SIZE;\r
2395 goto Done;\r
2396 }\r
2397\r
2398 StrCpy (PrivateRoot->VolumeLabel, (CHAR16 *) Buffer);\r
2399\r
2400 Status = EFI_SUCCESS;\r
2401 goto Done;\r
2402 }\r
2403\r
2404 if (!CompareGuid (InformationType, &gEfiFileInfoGuid)) {\r
2405 Status = EFI_UNSUPPORTED;\r
2406 goto Done;\r
2407 }\r
2408\r
2409 if (BufferSize < SIZE_OF_EFI_FILE_INFO) {\r
2410 Status = EFI_BAD_BUFFER_SIZE;\r
2411 goto Done;\r
2412 }\r
2413\r
2414 //\r
2415 // Set file/directory information.\r
2416 //\r
2417\r
2418 //\r
2419 // Check for invalid set file information parameters.\r
2420 //\r
2421 NewFileInfo = (EFI_FILE_INFO *) Buffer;\r
2422\r
82b76043 2423 if ((NewFileInfo->Size <= SIZE_OF_EFI_FILE_INFO) ||\r
068eac81 2424 (NewFileInfo->Attribute &~(EFI_FILE_VALID_ATTR)) ||\r
2425 (sizeof (UINTN) == 4 && NewFileInfo->Size > 0xFFFFFFFF)\r
2426 ) {\r
2427 Status = EFI_INVALID_PARAMETER;\r
2428 goto Done;\r
2429 }\r
2430\r
2431 //\r
2432 // bugbug: - This is not safe. We need something like EfiStrMaxSize()\r
2433 // that would have an additional parameter that would be the size\r
2434 // of the string array just in case there are no NULL characters in\r
2435 // the string array.\r
2436 //\r
2437 //\r
2438 // Get current file information so we can determine what kind\r
2439 // of change request this is.\r
2440 //\r
2441 OldInfoSize = 0;\r
2442 Status = WinNtSimpleFileSystemFileInfo (PrivateFile, &OldInfoSize, NULL);\r
2443\r
2444 if (Status != EFI_BUFFER_TOO_SMALL) {\r
2445 Status = EFI_DEVICE_ERROR;\r
2446 goto Done;\r
2447 }\r
2448\r
2449 OldFileInfo = AllocatePool (OldInfoSize);\r
2450 if (OldFileInfo == NULL) {\r
2451 Status = EFI_OUT_OF_RESOURCES;\r
2452 goto Done;\r
2453 }\r
2454\r
2455 Status = WinNtSimpleFileSystemFileInfo (PrivateFile, &OldInfoSize, OldFileInfo);\r
2456\r
2457 if (EFI_ERROR (Status)) {\r
2458 goto Done;\r
2459 }\r
2460\r
2461 OldFileName = AllocatePool (StrSize (PrivateFile->FileName));\r
2462 if (OldFileName == NULL) {\r
2463 Status = EFI_OUT_OF_RESOURCES;\r
2464 goto Done;\r
2465 }\r
2466\r
2467 StrCpy (OldFileName, PrivateFile->FileName);\r
2468\r
2469 //\r
2470 // Make full pathname from new filename and rootpath.\r
2471 //\r
a00ec39b 2472 if (NewFileInfo->FileName[0] == '\\') {\r
9485089d 2473 Size = StrSize (PrivateRoot->FilePath);\r
2474 Size += StrSize (L"\\");\r
2475 Size += StrSize (NewFileInfo->FileName);\r
2476 NewFileName = AllocatePool (Size);\r
2477 if (NewFileName == NULL) {\r
2478 Status = EFI_OUT_OF_RESOURCES;\r
2479 goto Done;\r
2480 }\r
2481\r
2482 StrCpy (NewFileName, PrivateRoot->FilePath);\r
2483 StrCat (NewFileName, L"\\");\r
068eac81 2484 StrCat (NewFileName, NewFileInfo->FileName + 1);\r
2485 } else {\r
9485089d 2486 Size = StrSize (PrivateFile->FilePath);\r
2487 Size += StrSize (L"\\");\r
2488 Size += StrSize (NewFileInfo->FileName);\r
2489 NewFileName = AllocatePool (Size);\r
2490 if (NewFileName == NULL) {\r
2491 Status = EFI_OUT_OF_RESOURCES;\r
2492 goto Done;\r
2493 }\r
2494\r
2495 StrCpy (NewFileName, PrivateFile->FilePath);\r
2496 StrCat (NewFileName, L"\\");\r
068eac81 2497 StrCat (NewFileName, NewFileInfo->FileName);\r
2498 }\r
2499\r
2500 //\r
2501 // Is there an attribute change request?\r
2502 //\r
2503 if (NewFileInfo->Attribute != OldFileInfo->Attribute) {\r
2504 if ((NewFileInfo->Attribute & EFI_FILE_DIRECTORY) != (OldFileInfo->Attribute & EFI_FILE_DIRECTORY)) {\r
2505 Status = EFI_INVALID_PARAMETER;\r
2506 goto Done;\r
2507 }\r
2508\r
2509 AttrChangeFlag = TRUE;\r
2510 }\r
2511\r
2512 //\r
2513 // Is there a name change request?\r
2514 // bugbug: - Need EfiStrCaseCmp()\r
2515 //\r
2516 if (StrCmp (NewFileInfo->FileName, OldFileInfo->FileName)) {\r
2517 NameChangeFlag = TRUE;\r
2518 }\r
2519\r
2520 //\r
2521 // Is there a size change request?\r
2522 //\r
2523 if (NewFileInfo->FileSize != OldFileInfo->FileSize) {\r
2524 SizeChangeFlag = TRUE;\r
2525 }\r
2526\r
2527 //\r
2528 // Is there a time stamp change request?\r
2529 //\r
2530 if (!IsZero (&NewFileInfo->CreateTime, sizeof (EFI_TIME)) &&\r
2531 CompareMem (&NewFileInfo->CreateTime, &OldFileInfo->CreateTime, sizeof (EFI_TIME))\r
2532 ) {\r
2533 TimeChangeFlag = TRUE;\r
2534 } else if (!IsZero (&NewFileInfo->LastAccessTime, sizeof (EFI_TIME)) &&\r
2535 CompareMem (&NewFileInfo->LastAccessTime, &OldFileInfo->LastAccessTime, sizeof (EFI_TIME))\r
2536 ) {\r
2537 TimeChangeFlag = TRUE;\r
2538 } else if (!IsZero (&NewFileInfo->ModificationTime, sizeof (EFI_TIME)) &&\r
2539 CompareMem (&NewFileInfo->ModificationTime, &OldFileInfo->ModificationTime, sizeof (EFI_TIME))\r
2540 ) {\r
2541 TimeChangeFlag = TRUE;\r
2542 }\r
2543\r
2544 //\r
2545 // All done if there are no change requests being made.\r
2546 //\r
2547 if (!(AttrChangeFlag || NameChangeFlag || SizeChangeFlag || TimeChangeFlag)) {\r
2548 Status = EFI_SUCCESS;\r
2549 goto Done;\r
2550 }\r
2551\r
2552 //\r
2553 // Set file or directory information.\r
2554 //\r
2555 OldAttr = PrivateFile->WinNtThunk->GetFileAttributes (OldFileName);\r
2556\r
2557 //\r
2558 // Name change.\r
2559 //\r
2560 if (NameChangeFlag) {\r
2561 //\r
2562 // Close the handles first\r
2563 //\r
2564 if (PrivateFile->IsOpenedByRead) {\r
2565 Status = EFI_ACCESS_DENIED;\r
2566 goto Done;\r
2567 }\r
2568\r
2569 for (CharPointer = NewFileName; *CharPointer != 0 && *CharPointer != L'/'; CharPointer++) {\r
2570 }\r
2571\r
2572 if (*CharPointer != 0) {\r
2573 Status = EFI_ACCESS_DENIED;\r
2574 goto Done;\r
2575 }\r
2576\r
2577 if (PrivateFile->LHandle != INVALID_HANDLE_VALUE) {\r
2578 if (PrivateFile->IsDirectoryPath) {\r
2579 PrivateFile->WinNtThunk->FindClose (PrivateFile->LHandle);\r
2580 } else {\r
2581 PrivateFile->WinNtThunk->CloseHandle (PrivateFile->LHandle);\r
2582 PrivateFile->LHandle = INVALID_HANDLE_VALUE;\r
2583 }\r
2584 }\r
2585\r
2586 if (PrivateFile->IsDirectoryPath && PrivateFile->DirHandle != INVALID_HANDLE_VALUE) {\r
2587 PrivateFile->WinNtThunk->CloseHandle (PrivateFile->DirHandle);\r
2588 PrivateFile->DirHandle = INVALID_HANDLE_VALUE;\r
2589 }\r
2590\r
2591 NtStatus = PrivateFile->WinNtThunk->MoveFile (OldFileName, NewFileName);\r
2592\r
2593 if (NtStatus) {\r
2594 //\r
2595 // modify file name\r
2596 //\r
2597 FreePool (PrivateFile->FileName);\r
2598\r
2599 PrivateFile->FileName = AllocatePool (StrSize (NewFileName));\r
2600 if (PrivateFile->FileName == NULL) {\r
2601 Status = EFI_OUT_OF_RESOURCES;\r
2602 goto Done;\r
2603 }\r
2604\r
2605 StrCpy (PrivateFile->FileName, NewFileName);\r
2606\r
a00ec39b 2607 Size = StrSize (NewFileName);\r
2608 Size += StrSize (L"\\*");\r
2609 TempFileName = AllocatePool (Size);\r
068eac81 2610\r
2611 StrCpy (TempFileName, NewFileName);\r
2612\r
2613 if (!PrivateFile->IsDirectoryPath) {\r
2614 PrivateFile->LHandle = PrivateFile->WinNtThunk->CreateFile (\r
2615 TempFileName,\r
2616 PrivateFile->IsOpenedByRead ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE,\r
2617 FILE_SHARE_READ | FILE_SHARE_WRITE,\r
2618 NULL,\r
2619 OPEN_EXISTING,\r
2620 0,\r
2621 NULL\r
2622 );\r
2623\r
2624 FreePool (TempFileName);\r
2625\r
2626 //\r
2627 // Flush buffers just in case\r
2628 //\r
2629 if (PrivateFile->WinNtThunk->FlushFileBuffers (PrivateFile->LHandle) == 0) {\r
2630 Status = EFI_DEVICE_ERROR;\r
2631 goto Done;\r
2632 }\r
2633 } else {\r
2634 PrivateFile->DirHandle = PrivateFile->WinNtThunk->CreateFile (\r
2635 TempFileName,\r
2636 PrivateFile->IsOpenedByRead ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE,\r
2637 FILE_SHARE_READ | FILE_SHARE_WRITE,\r
2638 NULL,\r
2639 OPEN_EXISTING,\r
2640 FILE_FLAG_BACKUP_SEMANTICS,\r
2641 NULL\r
2642 );\r
2643\r
2644 StrCat (TempFileName, L"\\*");\r
2645 PrivateFile->LHandle = PrivateFile->WinNtThunk->FindFirstFile (TempFileName, &FindBuf);\r
2646\r
2647 FreePool (TempFileName);\r
2648 }\r
2649 } else {\r
41e9426b 2650 Status = EFI_ACCESS_DENIED;\r
068eac81 2651Reopen: ;\r
068eac81 2652\r
2653 NtStatus = PrivateFile->WinNtThunk->SetFileAttributes (OldFileName, OldAttr);\r
2654\r
2655 if (!NtStatus) {\r
2656 goto Done;\r
2657 }\r
2658\r
a00ec39b 2659 Size = StrSize (OldFileName);\r
2660 Size += StrSize (L"\\*");\r
2661 TempFileName = AllocatePool (Size);\r
068eac81 2662\r
2663 StrCpy (TempFileName, OldFileName);\r
2664\r
2665 if (!PrivateFile->IsDirectoryPath) {\r
2666 PrivateFile->LHandle = PrivateFile->WinNtThunk->CreateFile (\r
2667 TempFileName,\r
2668 PrivateFile->IsOpenedByRead ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE,\r
2669 FILE_SHARE_READ | FILE_SHARE_WRITE,\r
2670 NULL,\r
2671 OPEN_EXISTING,\r
2672 0,\r
2673 NULL\r
2674 );\r
2675 } else {\r
2676 PrivateFile->DirHandle = PrivateFile->WinNtThunk->CreateFile (\r
2677 TempFileName,\r
2678 PrivateFile->IsOpenedByRead ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE,\r
2679 FILE_SHARE_READ | FILE_SHARE_WRITE,\r
2680 NULL,\r
2681 OPEN_EXISTING,\r
2682 FILE_FLAG_BACKUP_SEMANTICS,\r
2683 NULL\r
2684 );\r
2685\r
2686 StrCat (TempFileName, L"\\*");\r
2687 PrivateFile->LHandle = PrivateFile->WinNtThunk->FindFirstFile (TempFileName, &FindBuf);\r
2688 }\r
2689\r
2690 FreePool (TempFileName);\r
2691\r
2692 goto Done;\r
2693\r
2694 }\r
2695 }\r
2696\r
2697 //\r
2698 // Size change\r
2699 //\r
2700 if (SizeChangeFlag) {\r
2701 if (PrivateFile->IsDirectoryPath) {\r
2702 Status = EFI_UNSUPPORTED;\r
2703 goto Done;\r
2704 }\r
2705\r
2706 if (PrivateFile->IsOpenedByRead || OldFileInfo->Attribute & EFI_FILE_READ_ONLY) {\r
2707 Status = EFI_ACCESS_DENIED;\r
2708 goto Done;\r
2709 }\r
2710\r
2711 Status = This->GetPosition (This, &CurPos);\r
2712 if (EFI_ERROR (Status)) {\r
2713 goto Done;\r
2714 }\r
2715\r
2716 Status = This->SetPosition (This, NewFileInfo->FileSize);\r
2717 if (EFI_ERROR (Status)) {\r
2718 goto Done;\r
2719 }\r
2720\r
2721 if (PrivateFile->WinNtThunk->SetEndOfFile (PrivateFile->LHandle) == 0) {\r
2722 Status = EFI_DEVICE_ERROR;\r
2723 goto Done;\r
2724 }\r
2725\r
2726 Status = This->SetPosition (This, CurPos);\r
2727 if (EFI_ERROR (Status)) {\r
2728 goto Done;\r
2729 }\r
2730 }\r
2731\r
2732 //\r
2733 // Time change\r
2734 //\r
2735 if (TimeChangeFlag) {\r
2736\r
2737 NewCreationSystemTime.wYear = NewFileInfo->CreateTime.Year;\r
2738 NewCreationSystemTime.wMonth = NewFileInfo->CreateTime.Month;\r
2739 NewCreationSystemTime.wDay = NewFileInfo->CreateTime.Day;\r
2740 NewCreationSystemTime.wHour = NewFileInfo->CreateTime.Hour;\r
2741 NewCreationSystemTime.wMinute = NewFileInfo->CreateTime.Minute;\r
2742 NewCreationSystemTime.wSecond = NewFileInfo->CreateTime.Second;\r
2743 NewCreationSystemTime.wMilliseconds = 0;\r
2744\r
2745 if (!PrivateFile->WinNtThunk->SystemTimeToFileTime (\r
2746 &NewCreationSystemTime,\r
2747 &NewCreationFileTime\r
2748 )) {\r
2749 goto Done;\r
2750 }\r
2751\r
f515069c
ED
2752 if (!PrivateFile->WinNtThunk->LocalFileTimeToFileTime (\r
2753 &NewCreationFileTime,\r
2754 &NewCreationFileTime\r
2755 )) {\r
2756 goto Done;\r
2757 }\r
2758\r
068eac81 2759 NewLastAccessSystemTime.wYear = NewFileInfo->LastAccessTime.Year;\r
2760 NewLastAccessSystemTime.wMonth = NewFileInfo->LastAccessTime.Month;\r
2761 NewLastAccessSystemTime.wDay = NewFileInfo->LastAccessTime.Day;\r
2762 NewLastAccessSystemTime.wHour = NewFileInfo->LastAccessTime.Hour;\r
2763 NewLastAccessSystemTime.wMinute = NewFileInfo->LastAccessTime.Minute;\r
2764 NewLastAccessSystemTime.wSecond = NewFileInfo->LastAccessTime.Second;\r
2765 NewLastAccessSystemTime.wMilliseconds = 0;\r
2766\r
2767 if (!PrivateFile->WinNtThunk->SystemTimeToFileTime (\r
2768 &NewLastAccessSystemTime,\r
2769 &NewLastAccessFileTime\r
2770 )) {\r
2771 goto Done;\r
2772 }\r
2773\r
f515069c
ED
2774 if (!PrivateFile->WinNtThunk->LocalFileTimeToFileTime (\r
2775 &NewLastAccessFileTime,\r
2776 &NewLastAccessFileTime\r
2777 )) {\r
2778 goto Done;\r
2779 }\r
2780\r
068eac81 2781 NewLastWriteSystemTime.wYear = NewFileInfo->ModificationTime.Year;\r
2782 NewLastWriteSystemTime.wMonth = NewFileInfo->ModificationTime.Month;\r
2783 NewLastWriteSystemTime.wDay = NewFileInfo->ModificationTime.Day;\r
2784 NewLastWriteSystemTime.wHour = NewFileInfo->ModificationTime.Hour;\r
2785 NewLastWriteSystemTime.wMinute = NewFileInfo->ModificationTime.Minute;\r
2786 NewLastWriteSystemTime.wSecond = NewFileInfo->ModificationTime.Second;\r
2787 NewLastWriteSystemTime.wMilliseconds = 0;\r
2788\r
2789 if (!PrivateFile->WinNtThunk->SystemTimeToFileTime (\r
2790 &NewLastWriteSystemTime,\r
2791 &NewLastWriteFileTime\r
2792 )) {\r
2793 goto Done;\r
2794 }\r
2795\r
f515069c
ED
2796 if (!PrivateFile->WinNtThunk->LocalFileTimeToFileTime (\r
2797 &NewLastWriteFileTime,\r
2798 &NewLastWriteFileTime\r
2799 )) {\r
2800 goto Done;\r
2801 }\r
2802\r
068eac81 2803 if (!PrivateFile->WinNtThunk->SetFileTime (\r
2804 PrivateFile->IsDirectoryPath ? PrivateFile->DirHandle : PrivateFile->LHandle,\r
2805 &NewCreationFileTime,\r
2806 &NewLastAccessFileTime,\r
2807 &NewLastWriteFileTime\r
2808 )) {\r
2809 Status = EFI_DEVICE_ERROR;\r
2810 goto Done;\r
2811 }\r
2812\r
2813 }\r
2814\r
2815 //\r
2816 // No matter about AttrChangeFlag, Attribute must be set.\r
2817 // Because operation before may cause attribute change.\r
2818 //\r
2819 NewAttr = OldAttr;\r
2820\r
2821 if (NewFileInfo->Attribute & EFI_FILE_ARCHIVE) {\r
2822 NewAttr |= FILE_ATTRIBUTE_ARCHIVE;\r
2823 } else {\r
2824 NewAttr &= ~FILE_ATTRIBUTE_ARCHIVE;\r
2825 }\r
2826\r
2827 if (NewFileInfo->Attribute & EFI_FILE_HIDDEN) {\r
2828 NewAttr |= FILE_ATTRIBUTE_HIDDEN;\r
2829 } else {\r
2830 NewAttr &= ~FILE_ATTRIBUTE_HIDDEN;\r
2831 }\r
2832\r
2833 if (NewFileInfo->Attribute & EFI_FILE_SYSTEM) {\r
2834 NewAttr |= FILE_ATTRIBUTE_SYSTEM;\r
2835 } else {\r
2836 NewAttr &= ~FILE_ATTRIBUTE_SYSTEM;\r
2837 }\r
2838\r
2839 if (NewFileInfo->Attribute & EFI_FILE_READ_ONLY) {\r
2840 NewAttr |= FILE_ATTRIBUTE_READONLY;\r
2841 } else {\r
2842 NewAttr &= ~FILE_ATTRIBUTE_READONLY;\r
2843 }\r
2844\r
2845 NtStatus = PrivateFile->WinNtThunk->SetFileAttributes (NewFileName, NewAttr);\r
2846\r
2847 if (!NtStatus) {\r
41e9426b 2848 Status = EFI_DEVICE_ERROR;\r
068eac81 2849 goto Reopen;\r
2850 }\r
2851\r
2852Done:\r
2853 if (OldFileInfo != NULL) {\r
2854 FreePool (OldFileInfo);\r
2855 }\r
2856\r
2857 if (OldFileName != NULL) {\r
2858 FreePool (OldFileName);\r
2859 }\r
2860\r
2861 if (NewFileName != NULL) {\r
2862 FreePool (NewFileName);\r
2863 }\r
2864\r
2865 gBS->RestoreTPL (OldTpl);\r
2866 return Status;\r
2867}\r
2868\r
2869EFI_STATUS\r
2870EFIAPI\r
2871WinNtSimpleFileSystemFlush (\r
c0cf06d6 2872 IN EFI_FILE_PROTOCOL *This\r
068eac81 2873 )\r
2874/*++\r
2875\r
2876Routine Description:\r
2877\r
2878 Flush all modified data to the media.\r
2879\r
2880Arguments:\r
2881\r
2882 This - Pointer to an opened file handle.\r
2883\r
2884Returns:\r
2885\r
2886 EFI_SUCCESS - The data has been flushed.\r
2887\r
2888 EFI_NO_MEDIA - The device has no media.\r
2889\r
2890 EFI_DEVICE_ERROR - The device reported an error.\r
2891\r
2892 EFI_VOLUME_CORRUPTED - The file system structures have been corrupted.\r
2893\r
2894 EFI_WRITE_PROTECTED - The file, directory, volume, or device is write protected.\r
2895\r
2896 EFI_ACCESS_DENIED - The file was opened read-only.\r
2897\r
2898 EFI_VOLUME_FULL - The volume is full.\r
2899\r
2900--*/\r
2901// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
2902{\r
2903 BY_HANDLE_FILE_INFORMATION FileInfo;\r
2904 WIN_NT_EFI_FILE_PRIVATE *PrivateFile;\r
2905 EFI_STATUS Status;\r
2906 EFI_TPL OldTpl;\r
2907\r
2908 if (This == NULL) {\r
2909 return EFI_INVALID_PARAMETER;\r
2910 }\r
2911\r
2912 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
2913\r
2914 PrivateFile = WIN_NT_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);\r
2915\r
2916 if (PrivateFile->LHandle == INVALID_HANDLE_VALUE) {\r
2917 Status = EFI_DEVICE_ERROR;\r
2918 goto Done;\r
2919 }\r
2920\r
2921 if (PrivateFile->IsDirectoryPath) {\r
2922 Status = EFI_SUCCESS;\r
2923 goto Done;\r
2924 }\r
2925\r
2926 if (PrivateFile->IsOpenedByRead) {\r
2927 Status = EFI_ACCESS_DENIED;\r
2928 goto Done;\r
2929 }\r
2930\r
2931 PrivateFile->WinNtThunk->GetFileInformationByHandle (PrivateFile->LHandle, &FileInfo);\r
2932\r
2933 if (FileInfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {\r
2934 Status = EFI_ACCESS_DENIED;\r
2935 goto Done;\r
2936 }\r
2937\r
2938 Status = PrivateFile->WinNtThunk->FlushFileBuffers (PrivateFile->LHandle) ? EFI_SUCCESS : EFI_DEVICE_ERROR;\r
2939\r
2940Done:\r
2941 gBS->RestoreTPL (OldTpl);\r
2942 return Status;\r
2943 //\r
2944 // bugbug: - Use Windows error reporting.\r
2945 //\r
2946}\r
2947\r
2948\r