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