625f2c5637d4b2c974f947fb42b1e4b07feb2dde
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / UdfDxe / File.c
1 /** @file
2 Handle operations in files and directories from UDF/ECMA-167 file systems.
3
4 Copyright (C) 2014-2017 Paulo Alcantara <pcacjr@zytor.com>
5
6 This program and the accompanying materials are licensed and made available
7 under the terms and conditions of the BSD License which accompanies this
8 distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
12 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 **/
14
15 #include "Udf.h"
16
17 EFI_FILE_PROTOCOL gUdfFileIoOps = {
18 EFI_FILE_PROTOCOL_REVISION,
19 UdfOpen,
20 UdfClose,
21 UdfDelete,
22 UdfRead,
23 UdfWrite,
24 UdfGetPosition,
25 UdfSetPosition,
26 UdfGetInfo,
27 UdfSetInfo,
28 UdfFlush,
29 NULL,
30 NULL,
31 NULL,
32 NULL
33 };
34
35 #define _ROOT_FILE(_PrivData) (_PrivData)->Root
36 #define _PARENT_FILE(_PrivData) \
37 ((_PrivData)->IsRootDirectory ? (_PrivData)->Root : &(_PrivData)->File)
38 #define _FILE(_PrivData) _PARENT_FILE(_PrivData)
39
40 /**
41 Open the root directory on a volume.
42
43 @param This Protocol instance pointer.
44 @param Root Returns an Open file handle for the root directory
45
46 @retval EFI_SUCCESS The device was opened.
47 @retval EFI_UNSUPPORTED This volume does not support the file system.
48 @retval EFI_NO_MEDIA The device has no media.
49 @retval EFI_DEVICE_ERROR The device reported an error.
50 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
51 @retval EFI_ACCESS_DENIED The service denied access to the file.
52 @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of
53 resources.
54
55 **/
56 EFI_STATUS
57 EFIAPI
58 UdfOpenVolume (
59 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,
60 OUT EFI_FILE_PROTOCOL **Root
61 )
62 {
63 EFI_TPL OldTpl;
64 EFI_STATUS Status;
65 PRIVATE_UDF_SIMPLE_FS_DATA *PrivFsData;
66 PRIVATE_UDF_FILE_DATA *PrivFileData;
67
68 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
69
70 if (This == NULL || Root == NULL) {
71 Status = EFI_INVALID_PARAMETER;
72 goto Error_Invalid_Params;
73 }
74
75 PrivFsData = PRIVATE_UDF_SIMPLE_FS_DATA_FROM_THIS (This);
76
77 if (PrivFsData->OpenFiles == 0) {
78 //
79 // There is no more open files. Read volume information again since it was
80 // cleaned up on the last UdfClose() call.
81 //
82 Status = ReadUdfVolumeInformation (
83 PrivFsData->BlockIo,
84 PrivFsData->DiskIo,
85 &PrivFsData->Volume
86 );
87 if (EFI_ERROR (Status)) {
88 goto Error_Read_Udf_Volume;
89 }
90 }
91
92 CleanupFileInformation (&PrivFsData->Root);
93
94 //
95 // Find root directory file.
96 //
97 Status = FindRootDirectory (
98 PrivFsData->BlockIo,
99 PrivFsData->DiskIo,
100 &PrivFsData->Volume,
101 &PrivFsData->Root
102 );
103 if (EFI_ERROR (Status)) {
104 goto Error_Find_Root_Dir;
105 }
106
107 PrivFileData =
108 (PRIVATE_UDF_FILE_DATA *) AllocateZeroPool (sizeof (PRIVATE_UDF_FILE_DATA));
109 if (PrivFileData == NULL) {
110 Status = EFI_OUT_OF_RESOURCES;
111 goto Error_Alloc_Priv_File_Data;
112 }
113
114 PrivFileData->Signature = PRIVATE_UDF_FILE_DATA_SIGNATURE;
115 PrivFileData->SimpleFs = This;
116 PrivFileData->Root = &PrivFsData->Root;
117 PrivFileData->IsRootDirectory = TRUE;
118
119 CopyMem ((VOID *)&PrivFileData->FileIo, (VOID *)&gUdfFileIoOps,
120 sizeof (EFI_FILE_PROTOCOL));
121
122 *Root = &PrivFileData->FileIo;
123
124 PrivFsData->OpenFiles++;
125
126 gBS->RestoreTPL (OldTpl);
127
128 return EFI_SUCCESS;
129
130 Error_Alloc_Priv_File_Data:
131 CleanupFileInformation (&PrivFsData->Root);
132
133 Error_Find_Root_Dir:
134 CleanupVolumeInformation (&PrivFsData->Volume);
135
136 Error_Read_Udf_Volume:
137 Error_Invalid_Params:
138 gBS->RestoreTPL (OldTpl);
139
140 return Status;
141 }
142
143 /**
144 Opens a new file relative to the source file's location.
145
146 @param This The protocol instance pointer.
147 @param NewHandle Returns File Handle for FileName.
148 @param FileName Null terminated string. "\", ".", and ".." are supported.
149 @param OpenMode Open mode for file.
150 @param Attributes Only used for EFI_FILE_MODE_CREATE.
151
152 @retval EFI_SUCCESS The device was opened.
153 @retval EFI_NOT_FOUND The specified file could not be found on the
154 device.
155 @retval EFI_NO_MEDIA The device has no media.
156 @retval EFI_MEDIA_CHANGED The media has changed.
157 @retval EFI_DEVICE_ERROR The device reported an error.
158 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
159 @retval EFI_ACCESS_DENIED The service denied access to the file.
160 @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of
161 resources.
162 @retval EFI_VOLUME_FULL The volume is full.
163
164 **/
165 EFI_STATUS
166 EFIAPI
167 UdfOpen (
168 IN EFI_FILE_PROTOCOL *This,
169 OUT EFI_FILE_PROTOCOL **NewHandle,
170 IN CHAR16 *FileName,
171 IN UINT64 OpenMode,
172 IN UINT64 Attributes
173 )
174 {
175 EFI_TPL OldTpl;
176 EFI_STATUS Status;
177 PRIVATE_UDF_FILE_DATA *PrivFileData;
178 PRIVATE_UDF_SIMPLE_FS_DATA *PrivFsData;
179 CHAR16 FilePath[UDF_PATH_LENGTH];
180 UDF_FILE_INFO File;
181 PRIVATE_UDF_FILE_DATA *NewPrivFileData;
182 CHAR16 *TempFileName;
183
184 ZeroMem (FilePath, sizeof FilePath);
185 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
186
187 if (This == NULL || NewHandle == NULL || FileName == NULL) {
188 Status = EFI_INVALID_PARAMETER;
189 goto Error_Invalid_Params;
190 }
191
192 if (OpenMode != EFI_FILE_MODE_READ) {
193 Status = EFI_WRITE_PROTECTED;
194 goto Error_Invalid_Params;
195 }
196
197 PrivFileData = PRIVATE_UDF_FILE_DATA_FROM_THIS (This);
198
199 PrivFsData = PRIVATE_UDF_SIMPLE_FS_DATA_FROM_THIS (PrivFileData->SimpleFs);
200
201 //
202 // Build full path
203 //
204 if (*FileName == L'\\') {
205 StrCpyS (FilePath, UDF_PATH_LENGTH, FileName);
206 } else {
207 StrCpyS (FilePath, UDF_PATH_LENGTH, PrivFileData->AbsoluteFileName);
208 StrCatS (FilePath, UDF_PATH_LENGTH, L"\\");
209 StrCatS (FilePath, UDF_PATH_LENGTH, FileName);
210 }
211
212 MangleFileName (FilePath);
213 if (FilePath[0] == L'\0') {
214 Status = EFI_NOT_FOUND;
215 goto Error_Bad_FileName;
216 }
217
218 Status = FindFile (
219 PrivFsData->BlockIo,
220 PrivFsData->DiskIo,
221 &PrivFsData->Volume,
222 FilePath,
223 _ROOT_FILE (PrivFileData),
224 _PARENT_FILE (PrivFileData),
225 &_PARENT_FILE(PrivFileData)->FileIdentifierDesc->Icb,
226 &File
227 );
228 if (EFI_ERROR (Status)) {
229 goto Error_Find_File;
230 }
231
232 NewPrivFileData =
233 (PRIVATE_UDF_FILE_DATA *)AllocateZeroPool (sizeof (PRIVATE_UDF_FILE_DATA));
234 if (NewPrivFileData == NULL) {
235 Status = EFI_OUT_OF_RESOURCES;
236 goto Error_Alloc_New_Priv_File_Data;
237 }
238
239 CopyMem ((VOID *)NewPrivFileData, (VOID *)PrivFileData,
240 sizeof (PRIVATE_UDF_FILE_DATA));
241 CopyMem ((VOID *)&NewPrivFileData->File, &File, sizeof (UDF_FILE_INFO));
242
243 NewPrivFileData->IsRootDirectory = FALSE;
244
245 StrCpyS (NewPrivFileData->AbsoluteFileName, UDF_PATH_LENGTH, FilePath);
246 FileName = NewPrivFileData->AbsoluteFileName;
247
248 while ((TempFileName = StrStr (FileName, L"\\")) != NULL) {
249 FileName = TempFileName + 1;
250 }
251
252 StrCpyS (NewPrivFileData->FileName, UDF_PATH_LENGTH, FileName);
253
254 Status = GetFileSize (
255 PrivFsData->BlockIo,
256 PrivFsData->DiskIo,
257 &PrivFsData->Volume,
258 &NewPrivFileData->File,
259 &NewPrivFileData->FileSize
260 );
261 ASSERT_EFI_ERROR (Status);
262 if (EFI_ERROR (Status)) {
263 goto Error_Get_File_Size;
264 }
265
266 NewPrivFileData->FilePosition = 0;
267 ZeroMem ((VOID *)&NewPrivFileData->ReadDirInfo,
268 sizeof (UDF_READ_DIRECTORY_INFO));
269
270 *NewHandle = &NewPrivFileData->FileIo;
271
272 PrivFsData->OpenFiles++;
273
274 gBS->RestoreTPL (OldTpl);
275
276 return Status;
277
278 Error_Get_File_Size:
279 FreePool ((VOID *)NewPrivFileData);
280
281 Error_Alloc_New_Priv_File_Data:
282 CleanupFileInformation (&File);
283
284 Error_Find_File:
285 Error_Bad_FileName:
286 Error_Invalid_Params:
287 gBS->RestoreTPL (OldTpl);
288
289 return Status;
290 }
291
292 /**
293 Read data from the file.
294
295 @param This Protocol instance pointer.
296 @param BufferSize On input size of buffer, on output amount of data in
297 buffer.
298 @param Buffer The buffer in which data is read.
299
300 @retval EFI_SUCCESS Data was read.
301 @retval EFI_NO_MEDIA The device has no media.
302 @retval EFI_DEVICE_ERROR The device reported an error.
303 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
304 @retval EFI_BUFFER_TO_SMALL BufferSize is too small. BufferSize contains
305 required size.
306
307 **/
308 EFI_STATUS
309 EFIAPI
310 UdfRead (
311 IN EFI_FILE_PROTOCOL *This,
312 IN OUT UINTN *BufferSize,
313 OUT VOID *Buffer
314 )
315 {
316 EFI_TPL OldTpl;
317 EFI_STATUS Status;
318 PRIVATE_UDF_FILE_DATA *PrivFileData;
319 PRIVATE_UDF_SIMPLE_FS_DATA *PrivFsData;
320 UDF_VOLUME_INFO *Volume;
321 UDF_FILE_INFO *Parent;
322 UDF_READ_DIRECTORY_INFO *ReadDirInfo;
323 EFI_BLOCK_IO_PROTOCOL *BlockIo;
324 EFI_DISK_IO_PROTOCOL *DiskIo;
325 UDF_FILE_INFO FoundFile;
326 UDF_FILE_IDENTIFIER_DESCRIPTOR *NewFileIdentifierDesc;
327 VOID *NewFileEntryData;
328 CHAR16 FileName[UDF_FILENAME_LENGTH];
329 UINT64 FileSize;
330 UINT64 BufferSizeUint64;
331
332 ZeroMem (FileName, sizeof FileName);
333 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
334
335 if (This == NULL || BufferSize == NULL || (*BufferSize != 0 &&
336 Buffer == NULL)) {
337 Status = EFI_INVALID_PARAMETER;
338 goto Error_Invalid_Params;
339 }
340
341 PrivFileData = PRIVATE_UDF_FILE_DATA_FROM_THIS (This);
342 PrivFsData = PRIVATE_UDF_SIMPLE_FS_DATA_FROM_THIS (PrivFileData->SimpleFs);
343
344 BlockIo = PrivFsData->BlockIo;
345 DiskIo = PrivFsData->DiskIo;
346 Volume = &PrivFsData->Volume;
347 ReadDirInfo = &PrivFileData->ReadDirInfo;
348 NewFileIdentifierDesc = NULL;
349 NewFileEntryData = NULL;
350
351 Parent = _PARENT_FILE (PrivFileData);
352
353 Status = EFI_VOLUME_CORRUPTED;
354
355 if (IS_FID_NORMAL_FILE (Parent->FileIdentifierDesc)) {
356 if (PrivFileData->FilePosition > PrivFileData->FileSize) {
357 //
358 // File's position is beyond the EOF
359 //
360 Status = EFI_DEVICE_ERROR;
361 goto Error_File_Beyond_The_Eof;
362 }
363
364 if (PrivFileData->FilePosition == PrivFileData->FileSize) {
365 *BufferSize = 0;
366 Status = EFI_SUCCESS;
367 goto Done;
368 }
369
370 BufferSizeUint64 = *BufferSize;
371
372 Status = ReadFileData (
373 BlockIo,
374 DiskIo,
375 Volume,
376 Parent,
377 PrivFileData->FileSize,
378 &PrivFileData->FilePosition,
379 Buffer,
380 &BufferSizeUint64
381 );
382 ASSERT (BufferSizeUint64 <= MAX_UINTN);
383 *BufferSize = (UINTN)BufferSizeUint64;
384 } else if (IS_FID_DIRECTORY_FILE (Parent->FileIdentifierDesc)) {
385 if (ReadDirInfo->FidOffset == 0 && PrivFileData->FilePosition > 0) {
386 Status = EFI_DEVICE_ERROR;
387 *BufferSize = 0;
388 goto Done;
389 }
390
391 for (;;) {
392 Status = ReadDirectoryEntry (
393 BlockIo,
394 DiskIo,
395 Volume,
396 &Parent->FileIdentifierDesc->Icb,
397 Parent->FileEntry,
398 ReadDirInfo,
399 &NewFileIdentifierDesc
400 );
401 if (EFI_ERROR (Status)) {
402 if (Status == EFI_DEVICE_ERROR) {
403 FreePool (ReadDirInfo->DirectoryData);
404 ZeroMem ((VOID *)ReadDirInfo, sizeof (UDF_READ_DIRECTORY_INFO));
405
406 *BufferSize = 0;
407 Status = EFI_SUCCESS;
408 }
409
410 goto Done;
411 }
412
413 if (!IS_FID_PARENT_FILE (NewFileIdentifierDesc)) {
414 break;
415 }
416
417 FreePool ((VOID *)NewFileIdentifierDesc);
418 }
419
420 Status = FindFileEntry (
421 BlockIo,
422 DiskIo,
423 Volume,
424 &NewFileIdentifierDesc->Icb,
425 &NewFileEntryData
426 );
427 if (EFI_ERROR (Status)) {
428 goto Error_Find_Fe;
429 }
430 ASSERT (NewFileEntryData != NULL);
431
432 if (IS_FE_SYMLINK (NewFileEntryData)) {
433 Status = ResolveSymlink (
434 BlockIo,
435 DiskIo,
436 Volume,
437 Parent,
438 NewFileEntryData,
439 &FoundFile
440 );
441 if (EFI_ERROR (Status)) {
442 goto Error_Resolve_Symlink;
443 }
444
445 FreePool ((VOID *)NewFileEntryData);
446 NewFileEntryData = FoundFile.FileEntry;
447
448 Status = GetFileNameFromFid (NewFileIdentifierDesc, FileName);
449 if (EFI_ERROR (Status)) {
450 FreePool ((VOID *)FoundFile.FileIdentifierDesc);
451 goto Error_Get_FileName;
452 }
453
454 FreePool ((VOID *)NewFileIdentifierDesc);
455 NewFileIdentifierDesc = FoundFile.FileIdentifierDesc;
456 } else {
457 FoundFile.FileIdentifierDesc = NewFileIdentifierDesc;
458 FoundFile.FileEntry = NewFileEntryData;
459
460 Status = GetFileNameFromFid (FoundFile.FileIdentifierDesc, FileName);
461 if (EFI_ERROR (Status)) {
462 goto Error_Get_FileName;
463 }
464 }
465
466 Status = GetFileSize (
467 BlockIo,
468 DiskIo,
469 Volume,
470 &FoundFile,
471 &FileSize
472 );
473 if (EFI_ERROR (Status)) {
474 goto Error_Get_File_Size;
475 }
476
477 Status = SetFileInfo (
478 &FoundFile,
479 FileSize,
480 FileName,
481 BufferSize,
482 Buffer
483 );
484 if (EFI_ERROR (Status)) {
485 goto Error_Set_File_Info;
486 }
487
488 PrivFileData->FilePosition++;
489 Status = EFI_SUCCESS;
490 } else if (IS_FID_DELETED_FILE (Parent->FileIdentifierDesc)) {
491 Status = EFI_DEVICE_ERROR;
492 }
493
494 Error_Set_File_Info:
495 Error_Get_File_Size:
496 Error_Get_FileName:
497 Error_Resolve_Symlink:
498 if (NewFileEntryData != NULL) {
499 FreePool (NewFileEntryData);
500 }
501
502 Error_Find_Fe:
503 if (NewFileIdentifierDesc != NULL) {
504 FreePool ((VOID *)NewFileIdentifierDesc);
505 }
506
507 Done:
508 Error_File_Beyond_The_Eof:
509 Error_Invalid_Params:
510 gBS->RestoreTPL (OldTpl);
511
512 return Status;
513 }
514
515 /**
516 Close the file handle.
517
518 @param This Protocol instance pointer.
519
520 @retval EFI_SUCCESS The file was closed.
521
522 **/
523 EFI_STATUS
524 EFIAPI
525 UdfClose (
526 IN EFI_FILE_PROTOCOL *This
527 )
528 {
529 EFI_TPL OldTpl;
530 EFI_STATUS Status;
531 PRIVATE_UDF_FILE_DATA *PrivFileData;
532 PRIVATE_UDF_SIMPLE_FS_DATA *PrivFsData;
533
534 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
535
536 Status = EFI_SUCCESS;
537
538 if (This == NULL) {
539 Status = EFI_INVALID_PARAMETER;
540 goto Exit;
541 }
542
543 PrivFileData = PRIVATE_UDF_FILE_DATA_FROM_THIS (This);
544
545 PrivFsData = PRIVATE_UDF_SIMPLE_FS_DATA_FROM_THIS (PrivFileData->SimpleFs);
546
547 if (!PrivFileData->IsRootDirectory) {
548 CleanupFileInformation (&PrivFileData->File);
549
550 if (PrivFileData->ReadDirInfo.DirectoryData != NULL) {
551 FreePool (PrivFileData->ReadDirInfo.DirectoryData);
552 }
553 }
554
555 if (--PrivFsData->OpenFiles == 0) {
556 CleanupVolumeInformation (&PrivFsData->Volume);
557 }
558
559 FreePool ((VOID *)PrivFileData);
560
561 Exit:
562 gBS->RestoreTPL (OldTpl);
563
564 return Status;
565 }
566
567 /**
568 Close and delete the file handle.
569
570 @param This Protocol instance pointer.
571
572 @retval EFI_SUCCESS The file was closed and deleted.
573 @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not
574 deleted.
575
576 **/
577 EFI_STATUS
578 EFIAPI
579 UdfDelete (
580 IN EFI_FILE_PROTOCOL *This
581 )
582 {
583 PRIVATE_UDF_FILE_DATA *PrivFileData;
584
585 if (This == NULL) {
586 return EFI_INVALID_PARAMETER;
587 }
588
589 PrivFileData = PRIVATE_UDF_FILE_DATA_FROM_THIS (This);
590
591 (VOID)PrivFileData->FileIo.Close(This);
592
593 return EFI_WARN_DELETE_FAILURE;
594 }
595
596 /**
597 Write data to a file.
598
599 @param This Protocol instance pointer.
600 @param BufferSize On input size of buffer, on output amount of data in
601 buffer.
602 @param Buffer The buffer in which data to write.
603
604 @retval EFI_SUCCESS Data was written.
605 @retval EFI_UNSUPPORTED Writes to Open directory are not supported.
606 @retval EFI_NO_MEDIA The device has no media.
607 @retval EFI_DEVICE_ERROR The device reported an error.
608 @retval EFI_DEVICE_ERROR An attempt was made to write to a deleted file.
609 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
610 @retval EFI_WRITE_PROTECTED The device is write protected.
611 @retval EFI_ACCESS_DENIED The file was open for read only.
612 @retval EFI_VOLUME_FULL The volume is full.
613
614 **/
615 EFI_STATUS
616 EFIAPI
617 UdfWrite (
618 IN EFI_FILE_PROTOCOL *This,
619 IN OUT UINTN *BufferSize,
620 IN VOID *Buffer
621 )
622 {
623 return EFI_UNSUPPORTED;
624 }
625
626 /**
627 Get file's current position.
628
629 @param This Protocol instance pointer.
630 @param Position Byte position from the start of the file.
631
632 @retval EFI_SUCCESS Position was updated.
633 @retval EFI_UNSUPPORTED Seek request for directories is not valid.
634
635 **/
636 EFI_STATUS
637 EFIAPI
638 UdfGetPosition (
639 IN EFI_FILE_PROTOCOL *This,
640 OUT UINT64 *Position
641 )
642 {
643 PRIVATE_UDF_FILE_DATA *PrivFileData;
644
645 if (This == NULL || Position == NULL) {
646 return EFI_INVALID_PARAMETER;
647 }
648
649 PrivFileData = PRIVATE_UDF_FILE_DATA_FROM_THIS (This);
650
651 //
652 // As per UEFI spec, if the file handle is a directory, then the current file
653 // position has no meaning and the operation is not supported.
654 //
655 if (IS_FID_DIRECTORY_FILE (&PrivFileData->File.FileIdentifierDesc)) {
656 return EFI_UNSUPPORTED;
657 }
658
659 //
660 // The file is not a directory. So, return its position.
661 //
662 *Position = PrivFileData->FilePosition;
663
664 return EFI_SUCCESS;
665 }
666
667 /**
668 Set file's current position.
669
670 @param This Protocol instance pointer.
671 @param Position Byte position from the start of the file.
672
673 @retval EFI_SUCCESS Position was updated.
674 @retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open.
675
676 **/
677 EFI_STATUS
678 EFIAPI
679 UdfSetPosition (
680 IN EFI_FILE_PROTOCOL *This,
681 IN UINT64 Position
682 )
683 {
684 EFI_STATUS Status;
685 PRIVATE_UDF_FILE_DATA *PrivFileData;
686 UDF_FILE_IDENTIFIER_DESCRIPTOR *FileIdentifierDesc;
687
688 if (This == NULL) {
689 return EFI_INVALID_PARAMETER;
690 }
691
692 Status = EFI_UNSUPPORTED;
693
694 PrivFileData = PRIVATE_UDF_FILE_DATA_FROM_THIS (This);
695
696 FileIdentifierDesc = _FILE (PrivFileData)->FileIdentifierDesc;
697 ASSERT (FileIdentifierDesc != NULL);
698 if (IS_FID_DIRECTORY_FILE (FileIdentifierDesc)) {
699 //
700 // If the file handle is a directory, the _only_ position that may be set is
701 // zero. This has no effect of starting the read proccess of the directory
702 // entries over.
703 //
704 if (Position == 0) {
705 PrivFileData->FilePosition = Position;
706 PrivFileData->ReadDirInfo.FidOffset = 0;
707 Status = EFI_SUCCESS;
708 }
709 } else if (IS_FID_NORMAL_FILE (FileIdentifierDesc)) {
710 //
711 // Seeking to position 0xFFFFFFFFFFFFFFFF causes the current position to be
712 // set to the EOF.
713 //
714 if (Position == 0xFFFFFFFFFFFFFFFF) {
715 PrivFileData->FilePosition = PrivFileData->FileSize - 1;
716 } else {
717 PrivFileData->FilePosition = Position;
718 }
719
720 Status = EFI_SUCCESS;
721 }
722
723 return Status;
724 }
725
726 /**
727 Get information about a file.
728
729 @param This Protocol instance pointer.
730 @param InformationType Type of information to return in Buffer.
731 @param BufferSize On input size of buffer, on output amount of data in
732 buffer.
733 @param Buffer The buffer to return data.
734
735 @retval EFI_SUCCESS Data was returned.
736 @retval EFI_UNSUPPORTED InformationType is not supported.
737 @retval EFI_NO_MEDIA The device has no media.
738 @retval EFI_DEVICE_ERROR The device reported an error.
739 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
740 @retval EFI_WRITE_PROTECTED The device is write protected.
741 @retval EFI_ACCESS_DENIED The file was open for read only.
742 @retval EFI_BUFFER_TOO_SMALL Buffer was too small; required size returned in
743 BufferSize.
744
745 **/
746 EFI_STATUS
747 EFIAPI
748 UdfGetInfo (
749 IN EFI_FILE_PROTOCOL *This,
750 IN EFI_GUID *InformationType,
751 IN OUT UINTN *BufferSize,
752 OUT VOID *Buffer
753 )
754 {
755 EFI_STATUS Status;
756 PRIVATE_UDF_FILE_DATA *PrivFileData;
757 PRIVATE_UDF_SIMPLE_FS_DATA *PrivFsData;
758 EFI_FILE_SYSTEM_INFO *FileSystemInfo;
759 UINTN FileSystemInfoLength;
760 CHAR16 *String;
761 UDF_FILE_SET_DESCRIPTOR *FileSetDesc;
762 UINTN Index;
763 UINT8 *OstaCompressed;
764 UINT8 CompressionId;
765 UINT64 VolumeSize;
766 UINT64 FreeSpaceSize;
767 CHAR16 VolumeLabel[64];
768
769 if (This == NULL || InformationType == NULL || BufferSize == NULL ||
770 (*BufferSize != 0 && Buffer == NULL)) {
771 return EFI_INVALID_PARAMETER;
772 }
773
774 PrivFileData = PRIVATE_UDF_FILE_DATA_FROM_THIS (This);
775
776 PrivFsData = PRIVATE_UDF_SIMPLE_FS_DATA_FROM_THIS (PrivFileData->SimpleFs);
777
778 Status = EFI_UNSUPPORTED;
779
780 if (CompareGuid (InformationType, &gEfiFileInfoGuid)) {
781 Status = SetFileInfo (
782 _FILE (PrivFileData),
783 PrivFileData->FileSize,
784 PrivFileData->FileName,
785 BufferSize,
786 Buffer
787 );
788 } else if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid)) {
789 String = VolumeLabel;
790
791 FileSetDesc = PrivFsData->Volume.FileSetDescs[0];
792
793 OstaCompressed = &FileSetDesc->LogicalVolumeIdentifier[0];
794
795 CompressionId = OstaCompressed[0];
796 if (!IS_VALID_COMPRESSION_ID (CompressionId)) {
797 return EFI_VOLUME_CORRUPTED;
798 }
799
800 for (Index = 1; Index < 128; Index++) {
801 if (CompressionId == 16) {
802 *String = *(UINT8 *)(OstaCompressed + Index) << 8;
803 Index++;
804 } else {
805 *String = 0;
806 }
807
808 if (Index < 128) {
809 *String |= (CHAR16)(*(UINT8 *)(OstaCompressed + Index));
810 }
811
812 //
813 // Unlike FID Identifiers, Logical Volume Identifier is stored in a
814 // NULL-terminated OSTA compressed format, so we must check for the NULL
815 // character.
816 //
817 if (*String == L'\0') {
818 break;
819 }
820
821 String++;
822 }
823
824 *String = L'\0';
825
826 FileSystemInfoLength = StrSize (VolumeLabel) +
827 sizeof (EFI_FILE_SYSTEM_INFO);
828 if (*BufferSize < FileSystemInfoLength) {
829 *BufferSize = FileSystemInfoLength;
830 return EFI_BUFFER_TOO_SMALL;
831 }
832
833 FileSystemInfo = (EFI_FILE_SYSTEM_INFO *)Buffer;
834 StrCpyS (FileSystemInfo->VolumeLabel, ARRAY_SIZE (VolumeLabel),
835 VolumeLabel);
836 Status = GetVolumeSize (
837 PrivFsData->BlockIo,
838 PrivFsData->DiskIo,
839 &PrivFsData->Volume,
840 &VolumeSize,
841 &FreeSpaceSize
842 );
843 if (EFI_ERROR (Status)) {
844 return Status;
845 }
846
847 FileSystemInfo->Size = FileSystemInfoLength;
848 FileSystemInfo->ReadOnly = TRUE;
849 FileSystemInfo->BlockSize =
850 LV_BLOCK_SIZE (&PrivFsData->Volume, UDF_DEFAULT_LV_NUM);
851 FileSystemInfo->VolumeSize = VolumeSize;
852 FileSystemInfo->FreeSpace = FreeSpaceSize;
853
854 *BufferSize = FileSystemInfoLength;
855 Status = EFI_SUCCESS;
856 }
857
858 return Status;
859 }
860
861 /**
862 Set information about a file.
863
864 @param This Protocol instance pointer.
865 @param InformationType Type of information in Buffer.
866 @param BufferSize Size of buffer.
867 @param Buffer The data to write.
868
869 @retval EFI_SUCCESS Data was set.
870 @retval EFI_UNSUPPORTED InformationType is not supported.
871 @retval EFI_NO_MEDIA The device has no media.
872 @retval EFI_DEVICE_ERROR The device reported an error.
873 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
874 @retval EFI_WRITE_PROTECTED The device is write protected.
875 @retval EFI_ACCESS_DENIED The file was open for read only.
876
877 **/
878 EFI_STATUS
879 EFIAPI
880 UdfSetInfo (
881 IN EFI_FILE_PROTOCOL *This,
882 IN EFI_GUID *InformationType,
883 IN UINTN BufferSize,
884 IN VOID *Buffer
885 )
886 {
887 return EFI_WRITE_PROTECTED;
888 }
889
890 /**
891 Flush data back for the file handle.
892
893 @param This Protocol instance pointer.
894
895 @retval EFI_SUCCESS Data was flushed.
896 @retval EFI_UNSUPPORTED Writes to Open directory are not supported.
897 @retval EFI_NO_MEDIA The device has no media.
898 @retval EFI_DEVICE_ERROR The device reported an error.
899 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
900 @retval EFI_WRITE_PROTECTED The device is write protected.
901 @retval EFI_ACCESS_DENIED The file was open for read only.
902 @retval EFI_VOLUME_FULL The volume is full.
903
904 **/
905 EFI_STATUS
906 EFIAPI
907 UdfFlush (
908 IN EFI_FILE_PROTOCOL *This
909 )
910 {
911 return EFI_WRITE_PROTECTED;
912 }