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