2 Handle on-disk format and volume structures in UDF/ECMA-167 file systems.
4 Copyright (C) 2014-2017 Paulo Alcantara <pcacjr@zytor.com>
5 Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License which accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
13 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 // Vendor-Defined Device Path GUID for UDF file system
21 EFI_GUID gUdfDevPathGuid
= EFI_UDF_DEVICE_PATH_GUID
;
24 Find the anchor volume descriptor pointer.
26 @param[in] BlockIo BlockIo interface.
27 @param[in] DiskIo DiskIo interface.
28 @param[out] AnchorPoint Anchor volume descriptor pointer.
30 @retval EFI_SUCCESS Anchor volume descriptor pointer found.
31 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
32 @retval other Anchor volume descriptor pointer not found.
36 FindAnchorVolumeDescriptorPointer (
37 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
38 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
39 OUT UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER
*AnchorPoint
45 EFI_LBA DescriptorLBAs
[4];
47 UDF_DESCRIPTOR_TAG
*DescriptorTag
;
49 BlockSize
= BlockIo
->Media
->BlockSize
;
50 EndLBA
= BlockIo
->Media
->LastBlock
;
51 DescriptorLBAs
[0] = 256;
52 DescriptorLBAs
[1] = EndLBA
- 256;
53 DescriptorLBAs
[2] = EndLBA
;
54 DescriptorLBAs
[3] = 512;
56 for (Index
= 0; Index
< ARRAY_SIZE (DescriptorLBAs
); Index
++) {
57 Status
= DiskIo
->ReadDisk (
59 BlockIo
->Media
->MediaId
,
60 MultU64x32 (DescriptorLBAs
[Index
], BlockSize
),
61 sizeof (UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER
),
64 if (EFI_ERROR (Status
)) {
68 DescriptorTag
= &AnchorPoint
->DescriptorTag
;
71 // Check if read LBA has a valid AVDP descriptor.
73 if (DescriptorTag
->TagIdentifier
== UdfAnchorVolumeDescriptorPointer
) {
80 return EFI_VOLUME_CORRUPTED
;
84 Save the content of Logical Volume Descriptors and Partitions Descriptors in
87 @param[in] BlockIo BlockIo interface.
88 @param[in] DiskIo DiskIo interface.
89 @param[in] AnchorPoint Anchor volume descriptor pointer.
90 @param[out] Volume UDF volume information structure.
92 @retval EFI_SUCCESS The descriptors were saved.
93 @retval EFI_OUT_OF_RESOURCES The descriptors were not saved due to lack of
95 @retval other The descriptors were not saved due to
100 StartMainVolumeDescriptorSequence (
101 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
102 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
103 IN UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER
*AnchorPoint
,
104 OUT UDF_VOLUME_INFO
*Volume
109 UDF_EXTENT_AD
*ExtentAd
;
110 EFI_LBA SeqStartBlock
;
112 BOOLEAN StopSequence
;
114 UDF_DESCRIPTOR_TAG
*DescriptorTag
;
115 UINT32 LogicalBlockSize
;
117 BlockSize
= BlockIo
->Media
->BlockSize
;
118 ExtentAd
= &AnchorPoint
->MainVolumeDescriptorSequenceExtent
;
121 // Allocate buffer for reading disk blocks
123 Buffer
= AllocateZeroPool ((UINTN
)BlockSize
);
124 if (Buffer
== NULL
) {
125 return EFI_OUT_OF_RESOURCES
;
129 // The logical partition created by Partition driver is relative to the main
130 // VDS extent location, so we start the Main Volume Descriptor Sequence at
133 // We don't need to check again if we have valid Volume Descriptors here since
134 // Partition driver already did.
137 SeqEndBlock
= SeqStartBlock
+ DivU64x32 ((UINT64
)ExtentAd
->ExtentLength
,
139 StopSequence
= FALSE
;
140 for (; SeqStartBlock
< SeqEndBlock
&& !StopSequence
; SeqStartBlock
++) {
144 Status
= BlockIo
->ReadBlocks (
146 BlockIo
->Media
->MediaId
,
151 if (EFI_ERROR (Status
)) {
155 DescriptorTag
= Buffer
;
157 switch (DescriptorTag
->TagIdentifier
) {
158 case UdfPartitionDescriptor
:
160 // Save Partition Descriptor
162 CopyMem (&Volume
->PartitionDesc
, Buffer
, sizeof (Volume
->PartitionDesc
));
165 case UdfLogicalVolumeDescriptor
:
167 // Save Logical Volume Descriptor
169 CopyMem (&Volume
->LogicalVolDesc
, Buffer
, sizeof (Volume
->LogicalVolDesc
));
172 case UdfTerminatingDescriptor
:
182 // Determine FE (File Entry) size
184 LogicalBlockSize
= Volume
->LogicalVolDesc
.LogicalBlockSize
;
185 if (LogicalBlockSize
>= UDF_LOGICAL_SECTOR_SIZE
) {
186 Volume
->FileEntrySize
= (UINTN
)LogicalBlockSize
;
188 Volume
->FileEntrySize
= UDF_LOGICAL_SECTOR_SIZE
;
191 Status
= EFI_SUCCESS
;
195 // Free block read buffer
203 Return a Partition Descriptor given a Long Allocation Descriptor. This is
204 necessary to calculate the right extent (LongAd) offset which is added up
205 with partition's starting location.
207 @param[in] Volume Volume information pointer.
208 @param[in] LongAd Long Allocation Descriptor pointer.
210 @return A pointer to a Partition Descriptor.
213 UDF_PARTITION_DESCRIPTOR
*
215 IN UDF_VOLUME_INFO
*Volume
,
216 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*LongAd
219 UDF_LOGICAL_VOLUME_DESCRIPTOR
*LogicalVolDesc
;
222 LogicalVolDesc
= &Volume
->LogicalVolDesc
;
224 switch (LogicalVolDesc
->DomainIdentifier
.Suffix
.Domain
.UdfRevision
) {
232 // UDF 1.02 specification:
234 // There shall be exactly one prevailing Logical Volume Descriptor recorded
235 // per Volume Set. The Partition Maps field shall contain only Type 1
238 // UDF 1.50 through 2.60 specs say:
240 // For the purpose of interchange partition maps shall be limited to
241 // Partition Map type 1, except type 2 maps as described in the document.
243 // NOTE: Only one Type 1 (Physical) Partition is supported. It has been
244 // checked already in Partition driver for existence of a single Type 1
245 // Partition map, so we don't have to double check here.
247 // Partition reference number can also be retrieved from
248 // LongAd->ExtentLocation.PartitionReferenceNumber, however the spec says
249 // it may be 0, so let's not rely on it.
251 PartitionNum
= *(UINT16
*)((UINTN
)&LogicalVolDesc
->PartitionMaps
[4]);
256 // Unsupported UDF revision
262 // Check if partition number matches Partition Descriptor found in Main Volume
263 // Descriptor Sequence.
265 if (Volume
->PartitionDesc
.PartitionNumber
== PartitionNum
) {
266 return &Volume
->PartitionDesc
;
273 Return logical sector number of a given Long Allocation Descriptor.
275 @param[in] Volume Volume information pointer.
276 @param[in] LongAd Long Allocation Descriptor pointer.
277 @param[out] Lsn Logical sector number pointer.
279 @retval EFI_SUCCESS Logical sector number successfully returned.
280 @retval EFI_UNSUPPORTED Logical sector number is not returned due to
286 IN UDF_VOLUME_INFO
*Volume
,
287 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*LongAd
,
291 UDF_PARTITION_DESCRIPTOR
*PartitionDesc
;
293 PartitionDesc
= GetPdFromLongAd (Volume
, LongAd
);
294 if (PartitionDesc
== NULL
) {
297 "%a: Fail to get the Partition Descriptor from the given Long Allocation Descriptor.\n",
300 return EFI_UNSUPPORTED
;
303 *Lsn
= (UINT64
)PartitionDesc
->PartitionStartingLocation
-
304 Volume
->MainVdsStartLocation
+
305 LongAd
->ExtentLocation
.LogicalBlockNumber
;
311 Return logical sector number of a given Short Allocation Descriptor.
313 @param[in] Volume Volume pointer.
314 @param[in] PartitionDesc Partition Descriptor pointer.
315 @param[in] ShortAd Short Allocation Descriptor pointer.
317 @return The logical sector number of a given Short Allocation Descriptor.
322 IN UDF_VOLUME_INFO
*Volume
,
323 IN UDF_PARTITION_DESCRIPTOR
*PartitionDesc
,
324 IN UDF_SHORT_ALLOCATION_DESCRIPTOR
*ShortAd
327 return (UINT64
)PartitionDesc
->PartitionStartingLocation
-
328 Volume
->MainVdsStartLocation
+ ShortAd
->ExtentPosition
;
332 Find File Set Descriptor of a given Logical Volume Descriptor.
334 The found FSD will contain the extent (LogicalVolumeContentsUse) where our
337 @param[in] BlockIo BlockIo interface.
338 @param[in] DiskIo DiskIo interface.
339 @param[in] Volume Volume information pointer.
341 @retval EFI_SUCCESS File Set Descriptor pointer found.
342 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
343 @retval other File Set Descriptor pointer not found.
347 FindFileSetDescriptor (
348 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
349 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
350 IN UDF_VOLUME_INFO
*Volume
355 UDF_LOGICAL_VOLUME_DESCRIPTOR
*LogicalVolDesc
;
356 UDF_DESCRIPTOR_TAG
*DescriptorTag
;
358 LogicalVolDesc
= &Volume
->LogicalVolDesc
;
359 Status
= GetLongAdLsn (Volume
, &LogicalVolDesc
->LogicalVolumeContentsUse
, &Lsn
);
360 if (EFI_ERROR (Status
)) {
365 // As per UDF 2.60 specification:
367 // There shall be exactly one File Set Descriptor recorded per Logical
372 Status
= DiskIo
->ReadDisk (
374 BlockIo
->Media
->MediaId
,
375 MultU64x32 (Lsn
, LogicalVolDesc
->LogicalBlockSize
),
376 sizeof (Volume
->FileSetDesc
),
379 if (EFI_ERROR (Status
)) {
383 DescriptorTag
= &Volume
->FileSetDesc
.DescriptorTag
;
386 // Check if read block is a File Set Descriptor
388 if (DescriptorTag
->TagIdentifier
!= UdfFileSetDescriptor
) {
389 return EFI_VOLUME_CORRUPTED
;
396 Read Volume and File Structure on an UDF file system.
398 @param[in] BlockIo BlockIo interface.
399 @param[in] DiskIo DiskIo interface.
400 @param[out] Volume Volume information pointer.
402 @retval EFI_SUCCESS Volume and File Structure were read.
403 @retval other Volume and File Structure were not read.
407 ReadVolumeFileStructure (
408 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
409 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
410 OUT UDF_VOLUME_INFO
*Volume
414 UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER AnchorPoint
;
415 UDF_EXTENT_AD
*ExtentAd
;
418 // Find Anchor Volume Descriptor Pointer
420 Status
= FindAnchorVolumeDescriptorPointer (
425 if (EFI_ERROR (Status
)) {
430 // Save Main VDS start block number
432 ExtentAd
= &AnchorPoint
.MainVolumeDescriptorSequenceExtent
;
434 Volume
->MainVdsStartLocation
= (UINT64
)ExtentAd
->ExtentLocation
;
437 // Start Main Volume Descriptor Sequence.
439 Status
= StartMainVolumeDescriptorSequence (
445 if (EFI_ERROR (Status
)) {
453 Calculate length of a given File Identifier Descriptor.
455 @param[in] FileIdentifierDesc File Identifier Descriptor pointer.
457 @return The length of a given File Identifier Descriptor.
461 GetFidDescriptorLength (
462 IN UDF_FILE_IDENTIFIER_DESCRIPTOR
*FileIdentifierDesc
466 (INTN
)((OFFSET_OF (UDF_FILE_IDENTIFIER_DESCRIPTOR
, Data
[0]) + 3 +
467 FileIdentifierDesc
->LengthOfFileIdentifier
+
468 FileIdentifierDesc
->LengthOfImplementationUse
) >> 2) << 2
473 Duplicate a given File Identifier Descriptor.
475 @param[in] FileIdentifierDesc File Identifier Descriptor pointer.
476 @param[out] NewFileIdentifierDesc The duplicated File Identifier Descriptor.
481 IN UDF_FILE_IDENTIFIER_DESCRIPTOR
*FileIdentifierDesc
,
482 OUT UDF_FILE_IDENTIFIER_DESCRIPTOR
**NewFileIdentifierDesc
485 *NewFileIdentifierDesc
=
486 (UDF_FILE_IDENTIFIER_DESCRIPTOR
*)AllocateCopyPool (
487 (UINTN
) GetFidDescriptorLength (FileIdentifierDesc
), FileIdentifierDesc
);
491 Duplicate either a given File Entry or a given Extended File Entry.
493 @param[in] BlockIo BlockIo interface.
494 @param[in] Volume Volume information pointer.
495 @param[in] FileEntry (Extended) File Entry pointer.
496 @param[out] NewFileEntry The duplicated (Extended) File Entry.
501 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
502 IN UDF_VOLUME_INFO
*Volume
,
504 OUT VOID
**NewFileEntry
507 *NewFileEntry
= AllocateCopyPool (Volume
->FileEntrySize
, FileEntry
);
511 Get raw data + length of a given File Entry or Extended File Entry.
513 The file's recorded data can contain either real file content (inline) or
514 a sequence of extents (or Allocation Descriptors) which tells where file's
515 content is stored in.
517 NOTE: The FE/EFE can be thought it was an inode.
519 @param[in] FileEntryData (Extended) File Entry pointer.
520 @param[out] Data Buffer contains the raw data of a given
521 (Extended) File Entry.
522 @param[out] Length Length of the data in Buffer.
527 IN VOID
*FileEntryData
,
532 UDF_DESCRIPTOR_TAG
*DescriptorTag
;
533 UDF_EXTENDED_FILE_ENTRY
*ExtendedFileEntry
;
534 UDF_FILE_ENTRY
*FileEntry
;
536 DescriptorTag
= FileEntryData
;
538 if (DescriptorTag
->TagIdentifier
== UdfExtendedFileEntry
) {
539 ExtendedFileEntry
= (UDF_EXTENDED_FILE_ENTRY
*)FileEntryData
;
541 *Length
= ExtendedFileEntry
->InformationLength
;
542 *Data
= (VOID
*)((UINT8
*)ExtendedFileEntry
->Data
+
543 ExtendedFileEntry
->LengthOfExtendedAttributes
);
544 } else if (DescriptorTag
->TagIdentifier
== UdfFileEntry
) {
545 FileEntry
= (UDF_FILE_ENTRY
*)FileEntryData
;
547 *Length
= FileEntry
->InformationLength
;
548 *Data
= (VOID
*)((UINT8
*)FileEntry
->Data
+
549 FileEntry
->LengthOfExtendedAttributes
);
554 Get Allocation Descriptors' data information from a given FE/EFE.
556 @param[in] FileEntryData (Extended) File Entry pointer.
557 @param[out] AdsData Buffer contains the Allocation Descriptors'
558 data from a given FE/EFE.
559 @param[out] Length Length of the data in AdsData.
564 IN VOID
*FileEntryData
,
569 UDF_DESCRIPTOR_TAG
*DescriptorTag
;
570 UDF_EXTENDED_FILE_ENTRY
*ExtendedFileEntry
;
571 UDF_FILE_ENTRY
*FileEntry
;
573 DescriptorTag
= FileEntryData
;
575 if (DescriptorTag
->TagIdentifier
== UdfExtendedFileEntry
) {
576 ExtendedFileEntry
= (UDF_EXTENDED_FILE_ENTRY
*)FileEntryData
;
578 *Length
= ExtendedFileEntry
->LengthOfAllocationDescriptors
;
579 *AdsData
= (VOID
*)((UINT8
*)ExtendedFileEntry
->Data
+
580 ExtendedFileEntry
->LengthOfExtendedAttributes
);
581 } else if (DescriptorTag
->TagIdentifier
== UdfFileEntry
) {
582 FileEntry
= (UDF_FILE_ENTRY
*)FileEntryData
;
584 *Length
= FileEntry
->LengthOfAllocationDescriptors
;
585 *AdsData
= (VOID
*)((UINT8
*)FileEntry
->Data
+
586 FileEntry
->LengthOfExtendedAttributes
);
591 Read next Long Allocation Descriptor from a given file's data.
593 @param[in] Data File's data pointer.
594 @param[in,out] Offset Starting offset of the File's data to read.
595 @param[in] Length Length of the data to read.
596 @param[out] FoundLongAd Long Allocation Descriptor pointer.
598 @retval EFI_SUCCESS A Long Allocation Descriptor was found.
599 @retval EFI_DEVICE_ERROR No more Long Allocation Descriptors.
605 IN OUT UINT64
*Offset
,
607 OUT UDF_LONG_ALLOCATION_DESCRIPTOR
**FoundLongAd
610 UDF_LONG_ALLOCATION_DESCRIPTOR
*LongAd
;
611 UDF_EXTENT_FLAGS ExtentFlags
;
614 if (*Offset
>= Length
) {
616 // No more Long Allocation Descriptors.
618 return EFI_DEVICE_ERROR
;
622 (UDF_LONG_ALLOCATION_DESCRIPTOR
*)((UINT8
*)Data
+ *Offset
);
625 // If it's either an indirect AD (Extended Alllocation Descriptor) or an
626 // allocated AD, then return it.
628 ExtentFlags
= GET_EXTENT_FLAGS (LongAdsSequence
, LongAd
);
629 if (ExtentFlags
== ExtentIsNextExtent
||
630 ExtentFlags
== ExtentRecordedAndAllocated
) {
635 // This AD is either not recorded but allocated, or not recorded and not
636 // allocated. Skip it.
638 *Offset
+= AD_LENGTH (LongAdsSequence
);
641 *FoundLongAd
= LongAd
;
647 Read next Short Allocation Descriptor from a given file's data.
649 @param[in] Data File's data pointer.
650 @param[in,out] Offset Starting offset of the File's data to read.
651 @param[in] Length Length of the data to read.
652 @param[out] FoundShortAd Short Allocation Descriptor pointer.
654 @retval EFI_SUCCESS A Short Allocation Descriptor was found.
655 @retval EFI_DEVICE_ERROR No more Short Allocation Descriptors.
661 IN OUT UINT64
*Offset
,
663 OUT UDF_SHORT_ALLOCATION_DESCRIPTOR
**FoundShortAd
666 UDF_SHORT_ALLOCATION_DESCRIPTOR
*ShortAd
;
667 UDF_EXTENT_FLAGS ExtentFlags
;
670 if (*Offset
>= Length
) {
672 // No more Short Allocation Descriptors.
674 return EFI_DEVICE_ERROR
;
678 (UDF_SHORT_ALLOCATION_DESCRIPTOR
*)((UINT8
*)Data
+ *Offset
);
681 // If it's either an indirect AD (Extended Alllocation Descriptor) or an
682 // allocated AD, then return it.
684 ExtentFlags
= GET_EXTENT_FLAGS (ShortAdsSequence
, ShortAd
);
685 if (ExtentFlags
== ExtentIsNextExtent
||
686 ExtentFlags
== ExtentRecordedAndAllocated
) {
691 // This AD is either not recorded but allocated, or not recorded and not
692 // allocated. Skip it.
694 *Offset
+= AD_LENGTH (ShortAdsSequence
);
697 *FoundShortAd
= ShortAd
;
703 Get either a Short Allocation Descriptor or a Long Allocation Descriptor from
706 @param[in] RecordingFlags Flag to indicate the type of descriptor.
707 @param[in] Data File's data pointer.
708 @param[in,out] Offset Starting offset of the File's data to read.
709 @param[in] Length Length of the data to read.
710 @param[out] FoundAd Allocation Descriptor pointer.
712 @retval EFI_SUCCESS A Short Allocation Descriptor was found.
713 @retval EFI_DEVICE_ERROR No more Allocation Descriptors.
714 Invalid type of descriptor was given.
718 GetAllocationDescriptor (
719 IN UDF_FE_RECORDING_FLAGS RecordingFlags
,
721 IN OUT UINT64
*Offset
,
726 if (RecordingFlags
== LongAdsSequence
) {
727 return GetLongAdFromAds (
731 (UDF_LONG_ALLOCATION_DESCRIPTOR
**)FoundAd
733 } else if (RecordingFlags
== ShortAdsSequence
) {
734 return GetShortAdFromAds (
738 (UDF_SHORT_ALLOCATION_DESCRIPTOR
**)FoundAd
743 // Code should never reach here.
746 return EFI_DEVICE_ERROR
;
750 Return logical sector number of either Short or Long Allocation Descriptor.
752 @param[in] RecordingFlags Flag to indicate the type of descriptor.
753 @param[in] Volume Volume information pointer.
754 @param[in] ParentIcb Long Allocation Descriptor pointer.
755 @param[in] Ad Allocation Descriptor pointer.
756 @param[out] Lsn Logical sector number pointer.
758 @retval EFI_SUCCESS Logical sector number of the given Allocation
759 Descriptor successfully returned.
760 @retval EFI_UNSUPPORTED Logical sector number of the given Allocation
761 Descriptor is not returned due to unrecognized
766 GetAllocationDescriptorLsn (
767 IN UDF_FE_RECORDING_FLAGS RecordingFlags
,
768 IN UDF_VOLUME_INFO
*Volume
,
769 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*ParentIcb
,
774 UDF_PARTITION_DESCRIPTOR
*PartitionDesc
;
776 if (RecordingFlags
== LongAdsSequence
) {
777 return GetLongAdLsn (Volume
, (UDF_LONG_ALLOCATION_DESCRIPTOR
*)Ad
, Lsn
);
778 } else if (RecordingFlags
== ShortAdsSequence
) {
779 PartitionDesc
= GetPdFromLongAd (Volume
, ParentIcb
);
780 if (PartitionDesc
== NULL
) {
781 return EFI_UNSUPPORTED
;
784 *Lsn
= GetShortAdLsn (
787 (UDF_SHORT_ALLOCATION_DESCRIPTOR
*)Ad
793 // Code should never reach here.
796 return EFI_UNSUPPORTED
;
800 Return offset + length of a given indirect Allocation Descriptor (AED).
802 @param[in] BlockIo BlockIo interface.
803 @param[in] DiskIo DiskIo interface.
804 @param[in] Volume Volume information pointer.
805 @param[in] ParentIcb Long Allocation Descriptor pointer.
806 @param[in] RecordingFlags Flag to indicate the type of descriptor.
807 @param[in] Ad Allocation Descriptor pointer.
808 @param[out] Offset Offset of a given indirect Allocation
810 @param[out] Length Length of a given indirect Allocation
813 @retval EFI_SUCCESS The offset and length were returned.
814 @retval EFI_OUT_OF_RESOURCES The offset and length were not returned due
815 to lack of resources.
816 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
817 @retval other The offset and length were not returned.
822 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
823 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
824 IN UDF_VOLUME_INFO
*Volume
,
825 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*ParentIcb
,
826 IN UDF_FE_RECORDING_FLAGS RecordingFlags
,
836 UINT32 LogicalBlockSize
;
837 UDF_ALLOCATION_EXTENT_DESCRIPTOR
*AllocExtDesc
;
838 UDF_DESCRIPTOR_TAG
*DescriptorTag
;
840 ExtentLength
= GET_EXTENT_LENGTH (RecordingFlags
, Ad
);
841 Status
= GetAllocationDescriptorLsn (RecordingFlags
,
846 if (EFI_ERROR (Status
)) {
850 Data
= AllocatePool (ExtentLength
);
852 return EFI_OUT_OF_RESOURCES
;
855 LogicalBlockSize
= Volume
->LogicalVolDesc
.LogicalBlockSize
;
860 Status
= DiskIo
->ReadDisk (
862 BlockIo
->Media
->MediaId
,
863 MultU64x32 (Lsn
, LogicalBlockSize
),
867 if (EFI_ERROR (Status
)) {
871 AllocExtDesc
= (UDF_ALLOCATION_EXTENT_DESCRIPTOR
*)Data
;
873 DescriptorTag
= &AllocExtDesc
->DescriptorTag
;
876 // Check if read extent contains a valid tag identifier for AED.
878 if (DescriptorTag
->TagIdentifier
!= UdfAllocationExtentDescriptor
) {
879 Status
= EFI_VOLUME_CORRUPTED
;
884 // Get AED's block offset and its length.
886 *Offset
= MultU64x32 (Lsn
, LogicalBlockSize
) +
887 sizeof (UDF_ALLOCATION_EXTENT_DESCRIPTOR
);
888 *Length
= AllocExtDesc
->LengthOfAllocationDescriptors
;
897 Read Allocation Extent Descriptor into memory.
899 @param[in] BlockIo BlockIo interface.
900 @param[in] DiskIo DiskIo interface.
901 @param[in] Volume Volume information pointer.
902 @param[in] ParentIcb Long Allocation Descriptor pointer.
903 @param[in] RecordingFlags Flag to indicate the type of descriptor.
904 @param[in] Ad Allocation Descriptor pointer.
905 @param[out] Data Buffer that contains the Allocation Extent
907 @param[out] Length Length of Data.
909 @retval EFI_SUCCESS The Allocation Extent Descriptor was read.
910 @retval EFI_OUT_OF_RESOURCES The Allocation Extent Descriptor was not read
911 due to lack of resources.
912 @retval other Fail to read the disk.
917 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
918 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
919 IN UDF_VOLUME_INFO
*Volume
,
920 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*ParentIcb
,
921 IN UDF_FE_RECORDING_FLAGS RecordingFlags
,
931 // Get AED's offset + length.
933 Status
= GetAedAdsOffset (
943 if (EFI_ERROR (Status
)) {
948 // Allocate buffer to read in AED's data.
950 *Data
= AllocatePool ((UINTN
) (*Length
));
952 return EFI_OUT_OF_RESOURCES
;
955 return DiskIo
->ReadDisk (
957 BlockIo
->Media
->MediaId
,
965 Function used to serialise reads of Allocation Descriptors.
967 @param[in] RecordingFlags Flag to indicate the type of descriptor.
968 @param[in] Ad Allocation Descriptor pointer.
969 @param[in, out] Buffer Buffer to hold the next Allocation Descriptor.
970 @param[in] Length Length of Buffer.
972 @retval EFI_SUCCESS Buffer was grown to hold the next Allocation
974 @retval EFI_OUT_OF_RESOURCES Buffer was not grown due to lack of resources.
978 GrowUpBufferToNextAd (
979 IN UDF_FE_RECORDING_FLAGS RecordingFlags
,
981 IN OUT VOID
**Buffer
,
987 ExtentLength
= GET_EXTENT_LENGTH (RecordingFlags
, Ad
);
989 if (*Buffer
== NULL
) {
990 *Buffer
= AllocatePool (ExtentLength
);
991 if (*Buffer
== NULL
) {
992 return EFI_OUT_OF_RESOURCES
;
995 *Buffer
= ReallocatePool ((UINTN
) Length
, (UINTN
) (Length
+ ExtentLength
), *Buffer
);
996 if (*Buffer
== NULL
) {
997 return EFI_OUT_OF_RESOURCES
;
1005 Read data or size of either a File Entry or an Extended File Entry.
1007 @param[in] BlockIo BlockIo interface.
1008 @param[in] DiskIo DiskIo interface.
1009 @param[in] Volume Volume information pointer.
1010 @param[in] ParentIcb Long Allocation Descriptor pointer.
1011 @param[in] FileEntryData FE/EFE structure pointer.
1012 @param[in, out] ReadFileInfo Read file information pointer.
1014 @retval EFI_SUCCESS Data or size of a FE/EFE was read.
1015 @retval EFI_OUT_OF_RESOURCES Data or size of a FE/EFE was not read due to
1017 @retval EFI_INVALID_PARAMETER The read file flag given in ReadFileInfo is
1019 @retval EFI_UNSUPPORTED The FE recording flag given in FileEntryData
1021 @retval other Data or size of a FE/EFE was not read.
1026 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
1027 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
1028 IN UDF_VOLUME_INFO
*Volume
,
1029 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*ParentIcb
,
1030 IN VOID
*FileEntryData
,
1031 IN OUT UDF_READ_FILE_INFO
*ReadFileInfo
1035 UINT32 LogicalBlockSize
;
1042 UINT64 FilePosition
;
1047 BOOLEAN FinishedSeeking
;
1048 UINT32 ExtentLength
;
1049 UDF_FE_RECORDING_FLAGS RecordingFlags
;
1051 LogicalBlockSize
= Volume
->LogicalVolDesc
.LogicalBlockSize
;
1055 // set BytesLeft to suppress incorrect compiler/analyzer warnings
1060 FinishedSeeking
= FALSE
;
1063 switch (ReadFileInfo
->Flags
) {
1064 case ReadFileGetFileSize
:
1065 case ReadFileAllocateAndRead
:
1067 // Initialise ReadFileInfo structure for either getting file size, or
1068 // reading file's recorded data.
1070 ReadFileInfo
->ReadLength
= 0;
1071 ReadFileInfo
->FileData
= NULL
;
1073 case ReadFileSeekAndRead
:
1075 // About to seek a file and/or read its data.
1077 Length
= ReadFileInfo
->FileSize
- ReadFileInfo
->FilePosition
;
1078 if (ReadFileInfo
->FileDataSize
> Length
) {
1080 // About to read beyond the EOF -- truncate it.
1082 ReadFileInfo
->FileDataSize
= Length
;
1086 // Initialise data to start seeking and/or reading a file.
1088 BytesLeft
= ReadFileInfo
->FileDataSize
;
1091 FinishedSeeking
= FALSE
;
1096 RecordingFlags
= GET_FE_RECORDING_FLAGS (FileEntryData
);
1097 switch (RecordingFlags
) {
1100 // There are no extents for this FE/EFE. All data is inline.
1102 GetFileEntryData (FileEntryData
, &Data
, &Length
);
1104 if (ReadFileInfo
->Flags
== ReadFileGetFileSize
) {
1105 ReadFileInfo
->ReadLength
= Length
;
1106 } else if (ReadFileInfo
->Flags
== ReadFileAllocateAndRead
) {
1108 // Allocate buffer for starting read data.
1110 ReadFileInfo
->FileData
= AllocatePool ((UINTN
) Length
);
1111 if (ReadFileInfo
->FileData
== NULL
) {
1112 return EFI_OUT_OF_RESOURCES
;
1116 // Read all inline data into ReadFileInfo->FileData
1118 CopyMem (ReadFileInfo
->FileData
, Data
, (UINTN
) Length
);
1119 ReadFileInfo
->ReadLength
= Length
;
1120 } else if (ReadFileInfo
->Flags
== ReadFileSeekAndRead
) {
1122 // If FilePosition is non-zero, seek file to FilePosition, read
1123 // FileDataSize bytes and then updates FilePosition.
1126 ReadFileInfo
->FileData
,
1127 (VOID
*)((UINT8
*)Data
+ ReadFileInfo
->FilePosition
),
1128 (UINTN
) ReadFileInfo
->FileDataSize
1131 ReadFileInfo
->FilePosition
+= ReadFileInfo
->FileDataSize
;
1134 return EFI_INVALID_PARAMETER
;
1137 Status
= EFI_SUCCESS
;
1140 case LongAdsSequence
:
1141 case ShortAdsSequence
:
1143 // This FE/EFE contains a run of Allocation Descriptors. Get data + size
1144 // for start reading them out.
1146 GetAdsInformation (FileEntryData
, &Data
, &Length
);
1153 Status
= GetAllocationDescriptor (
1160 if (Status
== EFI_DEVICE_ERROR
) {
1161 Status
= EFI_SUCCESS
;
1166 // Check if AD is an indirect AD. If so, read Allocation Extent
1167 // Descriptor and its extents (ADs).
1169 if (GET_EXTENT_FLAGS (RecordingFlags
, Ad
) == ExtentIsNextExtent
) {
1176 Status
= GetAedAdsData (
1186 if (EFI_ERROR (Status
)) {
1189 ASSERT (Data
!= NULL
);
1195 ExtentLength
= GET_EXTENT_LENGTH (RecordingFlags
, Ad
);
1197 Status
= GetAllocationDescriptorLsn (RecordingFlags
,
1202 if (EFI_ERROR (Status
)) {
1206 switch (ReadFileInfo
->Flags
) {
1207 case ReadFileGetFileSize
:
1208 ReadFileInfo
->ReadLength
+= ExtentLength
;
1210 case ReadFileAllocateAndRead
:
1212 // Increase FileData (if necessary) to read next extent.
1214 Status
= GrowUpBufferToNextAd (
1217 &ReadFileInfo
->FileData
,
1218 ReadFileInfo
->ReadLength
1220 if (EFI_ERROR (Status
)) {
1221 goto Error_Alloc_Buffer_To_Next_Ad
;
1225 // Read extent's data into FileData.
1227 Status
= DiskIo
->ReadDisk (
1229 BlockIo
->Media
->MediaId
,
1230 MultU64x32 (Lsn
, LogicalBlockSize
),
1232 (VOID
*)((UINT8
*)ReadFileInfo
->FileData
+
1233 ReadFileInfo
->ReadLength
)
1235 if (EFI_ERROR (Status
)) {
1236 goto Error_Read_Disk_Blk
;
1239 ReadFileInfo
->ReadLength
+= ExtentLength
;
1241 case ReadFileSeekAndRead
:
1243 // Seek file first before reading in its data.
1245 if (FinishedSeeking
) {
1247 goto Skip_File_Seek
;
1250 if (FilePosition
+ ExtentLength
< ReadFileInfo
->FilePosition
) {
1251 FilePosition
+= ExtentLength
;
1255 if (FilePosition
+ ExtentLength
> ReadFileInfo
->FilePosition
) {
1256 Offset
= ReadFileInfo
->FilePosition
- FilePosition
;
1262 // Done with seeking file. Start reading its data.
1264 FinishedSeeking
= TRUE
;
1268 // Make sure we don't read more data than really wanted.
1270 if (ExtentLength
- Offset
> BytesLeft
) {
1271 DataLength
= BytesLeft
;
1273 DataLength
= ExtentLength
- Offset
;
1277 // Read extent's data into FileData.
1279 Status
= DiskIo
->ReadDisk (
1281 BlockIo
->Media
->MediaId
,
1282 Offset
+ MultU64x32 (Lsn
, LogicalBlockSize
),
1284 (VOID
*)((UINT8
*)ReadFileInfo
->FileData
+
1287 if (EFI_ERROR (Status
)) {
1288 goto Error_Read_Disk_Blk
;
1292 // Update current file's position.
1294 DataOffset
+= DataLength
;
1295 ReadFileInfo
->FilePosition
+= DataLength
;
1297 BytesLeft
-= DataLength
;
1298 if (BytesLeft
== 0) {
1300 // There is no more file data to read.
1302 Status
= EFI_SUCCESS
;
1311 // Point to the next AD (extent).
1313 AdOffset
+= AD_LENGTH (RecordingFlags
);
1317 case ExtendedAdsSequence
:
1318 // FIXME: Not supported. Got no volume with it, yet.
1320 Status
= EFI_UNSUPPORTED
;
1325 // A flag value reserved by the ECMA-167 standard (3rd Edition - June
1326 // 1997); 14.6 ICB Tag; 14.6.8 Flags (RBP 18); was found.
1328 Status
= EFI_UNSUPPORTED
;
1339 Error_Read_Disk_Blk
:
1340 Error_Alloc_Buffer_To_Next_Ad
:
1341 if (ReadFileInfo
->Flags
!= ReadFileSeekAndRead
) {
1342 FreePool (ReadFileInfo
->FileData
);
1354 Find a file by its filename from a given Parent file.
1356 @param[in] BlockIo BlockIo interface.
1357 @param[in] DiskIo DiskIo interface.
1358 @param[in] Volume Volume information pointer.
1359 @param[in] FileName File name string.
1360 @param[in] Parent Parent directory file.
1361 @param[in] Icb Long Allocation Descriptor pointer.
1362 @param[out] File Found file.
1364 @retval EFI_SUCCESS The file was found.
1365 @retval EFI_INVALID_PARAMETER One or more input parameters are invalid.
1366 @retval EFI_NOT_FOUND The file was not found.
1371 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
1372 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
1373 IN UDF_VOLUME_INFO
*Volume
,
1374 IN CHAR16
*FileName
,
1375 IN UDF_FILE_INFO
*Parent
,
1376 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*Icb
,
1377 OUT UDF_FILE_INFO
*File
1381 UDF_FILE_IDENTIFIER_DESCRIPTOR
*FileIdentifierDesc
;
1382 UDF_READ_DIRECTORY_INFO ReadDirInfo
;
1384 CHAR16 FoundFileName
[UDF_FILENAME_LENGTH
];
1385 VOID
*CompareFileEntry
;
1388 // Check if both Parent->FileIdentifierDesc and Icb are NULL.
1390 if ((Parent
->FileIdentifierDesc
== NULL
) && (Icb
== NULL
)) {
1391 return EFI_INVALID_PARAMETER
;
1395 // Check if parent file is really directory.
1397 if (FE_ICB_FILE_TYPE (Parent
->FileEntry
) != UdfFileEntryDirectory
) {
1398 return EFI_NOT_FOUND
;
1402 // If FileName is current file or working directory, just duplicate Parent's
1403 // FE/EFE and FID descriptors.
1405 if (StrCmp (FileName
, L
".") == 0) {
1406 if (Parent
->FileIdentifierDesc
== NULL
) {
1407 return EFI_INVALID_PARAMETER
;
1410 DuplicateFe (BlockIo
, Volume
, Parent
->FileEntry
, &File
->FileEntry
);
1411 if (File
->FileEntry
== NULL
) {
1412 return EFI_OUT_OF_RESOURCES
;
1415 DuplicateFid (Parent
->FileIdentifierDesc
, &File
->FileIdentifierDesc
);
1416 if (File
->FileIdentifierDesc
== NULL
) {
1417 FreePool (File
->FileEntry
);
1418 return EFI_OUT_OF_RESOURCES
;
1425 // Start directory listing.
1427 ZeroMem ((VOID
*)&ReadDirInfo
, sizeof (UDF_READ_DIRECTORY_INFO
));
1431 Status
= ReadDirectoryEntry (
1435 (Parent
->FileIdentifierDesc
!= NULL
) ?
1436 &Parent
->FileIdentifierDesc
->Icb
:
1442 if (EFI_ERROR (Status
)) {
1443 if (Status
== EFI_DEVICE_ERROR
) {
1444 Status
= EFI_NOT_FOUND
;
1450 // After calling function ReadDirectoryEntry(), if 'FileIdentifierDesc' is
1451 // NULL, then the 'Status' must be EFI_OUT_OF_RESOURCES. Hence, if the code
1452 // reaches here, 'FileIdentifierDesc' must be not NULL.
1454 // The ASSERT here is for addressing a false positive NULL pointer
1455 // dereference issue raised from static analysis.
1457 ASSERT (FileIdentifierDesc
!= NULL
);
1459 if (FileIdentifierDesc
->FileCharacteristics
& PARENT_FILE
) {
1461 // This FID contains the location (FE/EFE) of the parent directory of this
1462 // directory (Parent), and if FileName is either ".." or "\\", then it's
1463 // the expected FID.
1465 if (StrCmp (FileName
, L
"..") == 0 || StrCmp (FileName
, L
"\\") == 0) {
1470 Status
= GetFileNameFromFid (FileIdentifierDesc
, ARRAY_SIZE (FoundFileName
), FoundFileName
);
1471 if (EFI_ERROR (Status
)) {
1475 if (StrCmp (FileName
, FoundFileName
) == 0) {
1477 // FID has been found. Prepare to find its respective FE/EFE.
1484 FreePool ((VOID
*)FileIdentifierDesc
);
1487 if (ReadDirInfo
.DirectoryData
!= NULL
) {
1489 // Free all allocated resources for the directory listing.
1491 FreePool (ReadDirInfo
.DirectoryData
);
1495 Status
= EFI_SUCCESS
;
1497 File
->FileIdentifierDesc
= FileIdentifierDesc
;
1500 // If the requested file is root directory, then the FE/EFE was already
1501 // retrieved in UdfOpenVolume() function, thus no need to find it again.
1503 // Otherwise, find FE/EFE from the respective FID.
1505 if (StrCmp (FileName
, L
"\\") != 0) {
1506 Status
= FindFileEntry (
1510 &FileIdentifierDesc
->Icb
,
1513 if (EFI_ERROR (Status
)) {
1518 // Make sure that both Parent's FE/EFE and found FE/EFE are not equal.
1520 if (CompareMem ((VOID
*)Parent
->FileEntry
, (VOID
*)CompareFileEntry
,
1521 Volume
->FileEntrySize
) != 0) {
1522 File
->FileEntry
= CompareFileEntry
;
1524 FreePool ((VOID
*)FileIdentifierDesc
);
1525 FreePool ((VOID
*)CompareFileEntry
);
1526 Status
= EFI_NOT_FOUND
;
1534 FreePool ((VOID
*)FileIdentifierDesc
);
1540 Read volume information on a medium which contains a valid UDF file system.
1542 @param[in] BlockIo BlockIo interface.
1543 @param[in] DiskIo DiskIo interface.
1544 @param[out] Volume UDF volume information structure.
1546 @retval EFI_SUCCESS Volume information read.
1547 @retval EFI_NO_MEDIA The device has no media.
1548 @retval EFI_DEVICE_ERROR The device reported an error.
1549 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1550 @retval EFI_OUT_OF_RESOURCES The volume was not read due to lack of resources.
1554 ReadUdfVolumeInformation (
1555 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
1556 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
1557 OUT UDF_VOLUME_INFO
*Volume
1563 // Read all necessary UDF volume information and keep it private to the driver
1565 Status
= ReadVolumeFileStructure (
1570 if (EFI_ERROR (Status
)) {
1575 // Find File Set Descriptor
1577 Status
= FindFileSetDescriptor (BlockIo
, DiskIo
, Volume
);
1578 if (EFI_ERROR (Status
)) {
1586 Find the root directory on an UDF volume.
1588 @param[in] BlockIo BlockIo interface.
1589 @param[in] DiskIo DiskIo interface.
1590 @param[in] Volume UDF volume information structure.
1591 @param[out] File Root directory file.
1593 @retval EFI_SUCCESS Root directory found.
1594 @retval EFI_NO_MEDIA The device has no media.
1595 @retval EFI_DEVICE_ERROR The device reported an error.
1596 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1597 @retval EFI_OUT_OF_RESOURCES The root directory was not found due to lack of
1603 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
1604 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
1605 IN UDF_VOLUME_INFO
*Volume
,
1606 OUT UDF_FILE_INFO
*File
1610 UDF_FILE_INFO Parent
;
1612 Status
= FindFileEntry (
1616 &Volume
->FileSetDesc
.RootDirectoryIcb
,
1619 if (EFI_ERROR (Status
)) {
1623 Parent
.FileEntry
= File
->FileEntry
;
1624 Parent
.FileIdentifierDesc
= NULL
;
1633 &Volume
->FileSetDesc
.RootDirectoryIcb
,
1636 if (EFI_ERROR (Status
)) {
1637 FreePool (File
->FileEntry
);
1644 Find either a File Entry or a Extended File Entry from a given ICB.
1646 @param[in] BlockIo BlockIo interface.
1647 @param[in] DiskIo DiskIo interface.
1648 @param[in] Volume UDF volume information structure.
1649 @param[in] Icb ICB of the FID.
1650 @param[out] FileEntry File Entry or Extended File Entry.
1652 @retval EFI_SUCCESS File Entry or Extended File Entry found.
1653 @retval EFI_NO_MEDIA The device has no media.
1654 @retval EFI_DEVICE_ERROR The device reported an error.
1655 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1656 @retval EFI_OUT_OF_RESOURCES The FE/EFE entry was not found due to lack of
1662 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
1663 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
1664 IN UDF_VOLUME_INFO
*Volume
,
1665 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*Icb
,
1666 OUT VOID
**FileEntry
1671 UINT32 LogicalBlockSize
;
1672 UDF_DESCRIPTOR_TAG
*DescriptorTag
;
1675 Status
= GetLongAdLsn (Volume
, Icb
, &Lsn
);
1676 if (EFI_ERROR (Status
)) {
1680 LogicalBlockSize
= Volume
->LogicalVolDesc
.LogicalBlockSize
;
1682 ReadBuffer
= AllocateZeroPool (Volume
->FileEntrySize
);
1683 if (ReadBuffer
== NULL
) {
1684 return EFI_OUT_OF_RESOURCES
;
1690 Status
= DiskIo
->ReadDisk (
1692 BlockIo
->Media
->MediaId
,
1693 MultU64x32 (Lsn
, LogicalBlockSize
),
1694 Volume
->FileEntrySize
,
1697 if (EFI_ERROR (Status
)) {
1698 goto Error_Read_Disk_Blk
;
1701 DescriptorTag
= ReadBuffer
;
1704 // Check if the read extent contains a valid Tag Identifier for the expected
1707 if (DescriptorTag
->TagIdentifier
!= UdfFileEntry
&&
1708 DescriptorTag
->TagIdentifier
!= UdfExtendedFileEntry
) {
1709 Status
= EFI_VOLUME_CORRUPTED
;
1710 goto Error_Invalid_Fe
;
1713 *FileEntry
= ReadBuffer
;
1717 Error_Read_Disk_Blk
:
1718 FreePool (ReadBuffer
);
1724 Find a file given its absolute path on an UDF volume.
1726 @param[in] BlockIo BlockIo interface.
1727 @param[in] DiskIo DiskIo interface.
1728 @param[in] Volume UDF volume information structure.
1729 @param[in] FilePath File's absolute path.
1730 @param[in] Root Root directory file.
1731 @param[in] Parent Parent directory file.
1732 @param[in] Icb ICB of Parent.
1733 @param[out] File Found file.
1735 @retval EFI_SUCCESS FilePath was found.
1736 @retval EFI_NO_MEDIA The device has no media.
1737 @retval EFI_DEVICE_ERROR The device reported an error.
1738 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1739 @retval EFI_OUT_OF_RESOURCES The FilePath file was not found due to lack of
1745 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
1746 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
1747 IN UDF_VOLUME_INFO
*Volume
,
1748 IN CHAR16
*FilePath
,
1749 IN UDF_FILE_INFO
*Root
,
1750 IN UDF_FILE_INFO
*Parent
,
1751 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*Icb
,
1752 OUT UDF_FILE_INFO
*File
1756 CHAR16 FileName
[UDF_FILENAME_LENGTH
];
1757 CHAR16
*FileNamePointer
;
1758 UDF_FILE_INFO PreviousFile
;
1761 Status
= EFI_NOT_FOUND
;
1763 CopyMem ((VOID
*)&PreviousFile
, (VOID
*)Parent
, sizeof (UDF_FILE_INFO
));
1764 while (*FilePath
!= L
'\0') {
1765 FileNamePointer
= FileName
;
1766 while (*FilePath
!= L
'\0' && *FilePath
!= L
'\\') {
1767 if ((((UINTN
)FileNamePointer
- (UINTN
)FileName
) / sizeof (CHAR16
)) >=
1768 (ARRAY_SIZE (FileName
) - 1)) {
1769 return EFI_NOT_FOUND
;
1772 *FileNamePointer
++ = *FilePath
++;
1775 *FileNamePointer
= L
'\0';
1776 if (FileName
[0] == L
'\0') {
1778 // Open root directory.
1782 // There is no file found for the root directory yet. So, find only its
1785 // See UdfOpenVolume() function.
1787 Status
= InternalFindFile (BlockIo
,
1796 // We've already a file pointer (Root) for the root directory. Duplicate
1797 // its FE/EFE and FID descriptors.
1799 Status
= EFI_SUCCESS
;
1800 DuplicateFe (BlockIo
, Volume
, Root
->FileEntry
, &File
->FileEntry
);
1801 if (File
->FileEntry
== NULL
) {
1802 Status
= EFI_OUT_OF_RESOURCES
;
1805 // File->FileEntry is not NULL.
1807 DuplicateFid (Root
->FileIdentifierDesc
, &File
->FileIdentifierDesc
);
1808 if (File
->FileIdentifierDesc
== NULL
) {
1809 FreePool (File
->FileEntry
);
1810 Status
= EFI_OUT_OF_RESOURCES
;
1816 // No root directory. Find filename from the current directory.
1818 Status
= InternalFindFile (BlockIo
,
1827 if (EFI_ERROR (Status
)) {
1832 // If the found file is a symlink, then find its respective FE/EFE and
1835 if (FE_ICB_FILE_TYPE (File
->FileEntry
) == UdfFileEntrySymlink
) {
1836 FreePool ((VOID
*)File
->FileIdentifierDesc
);
1838 FileEntry
= File
->FileEntry
;
1840 Status
= ResolveSymlink (BlockIo
,
1847 FreePool (FileEntry
);
1849 if (EFI_ERROR (Status
)) {
1854 if (CompareMem ((VOID
*)&PreviousFile
, (VOID
*)Parent
,
1855 sizeof (UDF_FILE_INFO
)) != 0) {
1856 CleanupFileInformation (&PreviousFile
);
1859 CopyMem ((VOID
*)&PreviousFile
, (VOID
*)File
, sizeof (UDF_FILE_INFO
));
1860 if (*FilePath
!= L
'\0' && *FilePath
== L
'\\') {
1869 Read a directory entry at a time on an UDF volume.
1871 @param[in] BlockIo BlockIo interface.
1872 @param[in] DiskIo DiskIo interface.
1873 @param[in] Volume UDF volume information structure.
1874 @param[in] ParentIcb ICB of the parent file.
1875 @param[in] FileEntryData FE/EFE of the parent file.
1876 @param[in, out] ReadDirInfo Next read directory listing structure
1878 @param[out] FoundFid File Identifier Descriptor pointer.
1880 @retval EFI_SUCCESS Directory entry read.
1881 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.
1882 @retval EFI_NO_MEDIA The device has no media.
1883 @retval EFI_DEVICE_ERROR The device reported an error.
1884 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1885 @retval EFI_OUT_OF_RESOURCES The directory entry was not read due to lack of
1890 ReadDirectoryEntry (
1891 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
1892 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
1893 IN UDF_VOLUME_INFO
*Volume
,
1894 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*ParentIcb
,
1895 IN VOID
*FileEntryData
,
1896 IN OUT UDF_READ_DIRECTORY_INFO
*ReadDirInfo
,
1897 OUT UDF_FILE_IDENTIFIER_DESCRIPTOR
**FoundFid
1901 UDF_READ_FILE_INFO ReadFileInfo
;
1902 UDF_FILE_IDENTIFIER_DESCRIPTOR
*FileIdentifierDesc
;
1904 if (ReadDirInfo
->DirectoryData
== NULL
) {
1906 // The directory's recorded data has not been read yet. So let's cache it
1907 // into memory and the next calls won't need to read it again.
1909 ReadFileInfo
.Flags
= ReadFileAllocateAndRead
;
1919 if (EFI_ERROR (Status
)) {
1924 // Fill in ReadDirInfo structure with the read directory's data information.
1926 ReadDirInfo
->DirectoryData
= ReadFileInfo
.FileData
;
1927 ReadDirInfo
->DirectoryLength
= ReadFileInfo
.ReadLength
;
1931 if (ReadDirInfo
->FidOffset
>= ReadDirInfo
->DirectoryLength
) {
1933 // There are no longer FIDs for this directory. By returning
1934 // EFI_DEVICE_ERROR to the callee will indicate end of directory
1937 return EFI_DEVICE_ERROR
;
1941 // Get FID for this entry.
1943 FileIdentifierDesc
= GET_FID_FROM_ADS (ReadDirInfo
->DirectoryData
,
1944 ReadDirInfo
->FidOffset
);
1946 // Update FidOffset to point to next FID.
1948 ReadDirInfo
->FidOffset
+= GetFidDescriptorLength (FileIdentifierDesc
);
1949 } while (FileIdentifierDesc
->FileCharacteristics
& DELETED_FILE
);
1951 DuplicateFid (FileIdentifierDesc
, FoundFid
);
1952 if (*FoundFid
== NULL
) {
1953 return EFI_OUT_OF_RESOURCES
;
1960 Get a filename (encoded in OSTA-compressed format) from a File Identifier
1961 Descriptor on an UDF volume.
1963 @attention This is boundary function that may receive untrusted input.
1964 @attention The input is from FileSystem.
1966 The File Identifier Descriptor is external input, so this routine will do
1967 basic validation for File Identifier Descriptor and report status.
1969 @param[in] FileIdentifierDesc File Identifier Descriptor pointer.
1970 @param[in] CharMax The maximum number of FileName Unicode char,
1971 including terminating null char.
1972 @param[out] FileName Decoded filename.
1974 @retval EFI_SUCCESS Filename decoded and read.
1975 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1976 @retval EFI_BUFFER_TOO_SMALL The string buffer FileName cannot hold the
1980 GetFileNameFromFid (
1981 IN UDF_FILE_IDENTIFIER_DESCRIPTOR
*FileIdentifierDesc
,
1983 OUT CHAR16
*FileName
1986 UINT8
*OstaCompressed
;
1987 UINT8 CompressionId
;
1990 CHAR16
*FileNameBak
;
1993 return EFI_BUFFER_TOO_SMALL
;
1998 (UINT8
*)FileIdentifierDesc
->Data
+
1999 FileIdentifierDesc
->LengthOfImplementationUse
2002 CompressionId
= OstaCompressed
[0];
2003 if (!IS_VALID_COMPRESSION_ID (CompressionId
)) {
2004 return EFI_VOLUME_CORRUPTED
;
2007 FileNameBak
= FileName
;
2012 Length
= FileIdentifierDesc
->LengthOfFileIdentifier
;
2013 if (CompressionId
== 16) {
2014 if (((UINTN
)Length
>> 1) > CharMax
) {
2015 return EFI_BUFFER_TOO_SMALL
;
2018 if ((Length
!= 0) && ((UINTN
)Length
- 1 > CharMax
)) {
2019 return EFI_BUFFER_TOO_SMALL
;
2023 for (Index
= 1; Index
< Length
; Index
++) {
2024 if (CompressionId
== 16) {
2025 *FileName
= OstaCompressed
[Index
++] << 8;
2030 if (Index
< Length
) {
2031 *FileName
|= (CHAR16
)(OstaCompressed
[Index
]);
2037 Index
= ((UINTN
)FileName
- (UINTN
)FileNameBak
) / sizeof (CHAR16
);
2038 if (Index
> CharMax
- 1) {
2039 Index
= CharMax
- 1;
2041 FileNameBak
[Index
] = L
'\0';
2047 Resolve a symlink file on an UDF volume.
2049 @attention This is boundary function that may receive untrusted input.
2050 @attention The input is from FileSystem.
2052 The Path Component is external input, so this routine will do basic
2053 validation for Path Component and report status.
2055 @param[in] BlockIo BlockIo interface.
2056 @param[in] DiskIo DiskIo interface.
2057 @param[in] Volume UDF volume information structure.
2058 @param[in] Parent Parent file.
2059 @param[in] FileEntryData FE/EFE structure pointer.
2060 @param[out] File Resolved file.
2062 @retval EFI_SUCCESS Symlink file resolved.
2063 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.
2064 @retval EFI_NO_MEDIA The device has no media.
2065 @retval EFI_DEVICE_ERROR The device reported an error.
2066 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
2067 @retval EFI_OUT_OF_RESOURCES The symlink file was not resolved due to lack of
2073 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
2074 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
2075 IN UDF_VOLUME_INFO
*Volume
,
2076 IN UDF_FILE_INFO
*Parent
,
2077 IN VOID
*FileEntryData
,
2078 OUT UDF_FILE_INFO
*File
2082 UDF_READ_FILE_INFO ReadFileInfo
;
2086 UDF_PATH_COMPONENT
*PathComp
;
2087 UINT8 PathCompLength
;
2088 CHAR16 FileName
[UDF_FILENAME_LENGTH
];
2091 UINT8 CompressionId
;
2092 UDF_FILE_INFO PreviousFile
;
2095 // Symlink files on UDF volumes do not contain so much data other than
2096 // Path Components which resolves to real filenames, so it's OK to read in
2097 // all its data here -- usually the data will be inline with the FE/EFE for
2100 ReadFileInfo
.Flags
= ReadFileAllocateAndRead
;
2106 &Parent
->FileIdentifierDesc
->Icb
,
2110 if (EFI_ERROR (Status
)) {
2114 Length
= ReadFileInfo
.ReadLength
;
2116 Data
= (UINT8
*)ReadFileInfo
.FileData
;
2117 EndData
= Data
+ Length
;
2119 CopyMem ((VOID
*)&PreviousFile
, (VOID
*)Parent
, sizeof (UDF_FILE_INFO
));
2122 PathComp
= (UDF_PATH_COMPONENT
*)Data
;
2124 PathCompLength
= PathComp
->LengthOfComponentIdentifier
;
2126 switch (PathComp
->ComponentType
) {
2129 // This Path Component specifies the root directory hierarchy subject to
2130 // agreement between the originator and recipient of the medium. Skip it.
2136 // "\\." of the current directory. Read next Path Component.
2138 goto Next_Path_Component
;
2141 // ".." (parent directory). Go to it.
2143 CopyMem ((VOID
*)FileName
, L
"..", 6);
2147 // "." (current file). Duplicate both FE/EFE and FID of this file.
2149 DuplicateFe (BlockIo
, Volume
, PreviousFile
.FileEntry
, &File
->FileEntry
);
2150 if (File
->FileEntry
== NULL
) {
2151 Status
= EFI_OUT_OF_RESOURCES
;
2152 goto Error_Find_File
;
2155 DuplicateFid (PreviousFile
.FileIdentifierDesc
,
2156 &File
->FileIdentifierDesc
);
2157 if (File
->FileIdentifierDesc
== NULL
) {
2158 FreePool (File
->FileEntry
);
2159 Status
= EFI_OUT_OF_RESOURCES
;
2160 goto Error_Find_File
;
2162 goto Next_Path_Component
;
2165 // This Path Component identifies an object, either a file or a
2166 // directory or an alias.
2168 // Decode it from the compressed data in ComponentIdentifier and find
2171 CompressionId
= PathComp
->ComponentIdentifier
[0];
2172 if (!IS_VALID_COMPRESSION_ID (CompressionId
)) {
2173 return EFI_VOLUME_CORRUPTED
;
2177 for (Index
= 1; Index
< PathCompLength
; Index
++) {
2178 if (CompressionId
== 16) {
2179 *Char
= *(UINT8
*)((UINT8
*)PathComp
->ComponentIdentifier
+
2183 if (Index
> ARRAY_SIZE (FileName
)) {
2184 return EFI_UNSUPPORTED
;
2189 if (Index
< Length
) {
2190 *Char
|= (CHAR16
)(*(UINT8
*)((UINT8
*)PathComp
->ComponentIdentifier
+ Index
));
2196 Index
= ((UINTN
)Char
- (UINTN
)FileName
) / sizeof (CHAR16
);
2197 if (Index
> ARRAY_SIZE (FileName
) - 1) {
2198 Index
= ARRAY_SIZE (FileName
) - 1;
2200 FileName
[Index
] = L
'\0';
2205 // Find file from the read filename in symlink's file data.
2207 Status
= InternalFindFile (
2216 if (EFI_ERROR (Status
)) {
2217 goto Error_Find_File
;
2220 Next_Path_Component
:
2221 Data
+= sizeof (UDF_PATH_COMPONENT
) + PathCompLength
;
2222 if (Data
>= EndData
) {
2226 if (CompareMem ((VOID
*)&PreviousFile
, (VOID
*)Parent
,
2227 sizeof (UDF_FILE_INFO
)) != 0) {
2228 CleanupFileInformation (&PreviousFile
);
2231 CopyMem ((VOID
*)&PreviousFile
, (VOID
*)File
, sizeof (UDF_FILE_INFO
));
2235 // Unmap the symlink file.
2237 FreePool (ReadFileInfo
.FileData
);
2242 if (CompareMem ((VOID
*)&PreviousFile
, (VOID
*)Parent
,
2243 sizeof (UDF_FILE_INFO
)) != 0) {
2244 CleanupFileInformation (&PreviousFile
);
2247 FreePool (ReadFileInfo
.FileData
);
2253 Clean up in-memory UDF file information.
2255 @param[in] File File information pointer.
2259 CleanupFileInformation (
2260 IN UDF_FILE_INFO
*File
2263 if (File
->FileEntry
!= NULL
) {
2264 FreePool (File
->FileEntry
);
2266 if (File
->FileIdentifierDesc
!= NULL
) {
2267 FreePool ((VOID
*)File
->FileIdentifierDesc
);
2270 ZeroMem ((VOID
*)File
, sizeof (UDF_FILE_INFO
));
2274 Find a file from its absolute path on an UDF volume.
2276 @param[in] BlockIo BlockIo interface.
2277 @param[in] DiskIo DiskIo interface.
2278 @param[in] Volume UDF volume information structure.
2279 @param[in] File File information structure.
2280 @param[out] Size Size of the file.
2282 @retval EFI_SUCCESS File size calculated and set in Size.
2283 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.
2284 @retval EFI_NO_MEDIA The device has no media.
2285 @retval EFI_DEVICE_ERROR The device reported an error.
2286 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
2287 @retval EFI_OUT_OF_RESOURCES The file size was not calculated due to lack of
2293 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
2294 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
2295 IN UDF_VOLUME_INFO
*Volume
,
2296 IN UDF_FILE_INFO
*File
,
2301 UDF_READ_FILE_INFO ReadFileInfo
;
2303 ReadFileInfo
.Flags
= ReadFileGetFileSize
;
2309 &File
->FileIdentifierDesc
->Icb
,
2313 if (EFI_ERROR (Status
)) {
2317 *Size
= ReadFileInfo
.ReadLength
;
2323 Set information about a file on an UDF volume.
2325 @param[in] File File pointer.
2326 @param[in] FileSize Size of the file.
2327 @param[in] FileName Filename of the file.
2328 @param[in, out] BufferSize Size of the returned file infomation.
2329 @param[out] Buffer Data of the returned file information.
2331 @retval EFI_SUCCESS File information set.
2332 @retval EFI_NO_MEDIA The device has no media.
2333 @retval EFI_DEVICE_ERROR The device reported an error.
2334 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
2335 @retval EFI_OUT_OF_RESOURCES The file information was not set due to lack of
2341 IN UDF_FILE_INFO
*File
,
2343 IN CHAR16
*FileName
,
2344 IN OUT UINTN
*BufferSize
,
2348 UINTN FileInfoLength
;
2349 EFI_FILE_INFO
*FileInfo
;
2350 UDF_FILE_ENTRY
*FileEntry
;
2351 UDF_EXTENDED_FILE_ENTRY
*ExtendedFileEntry
;
2352 UDF_DESCRIPTOR_TAG
*DescriptorTag
;
2355 // Calculate the needed size for the EFI_FILE_INFO structure.
2357 FileInfoLength
= sizeof (EFI_FILE_INFO
) + ((FileName
!= NULL
) ?
2358 StrSize (FileName
) :
2360 if (*BufferSize
< FileInfoLength
) {
2362 // The given Buffer has no size enough for EFI_FILE_INFO structure.
2364 *BufferSize
= FileInfoLength
;
2365 return EFI_BUFFER_TOO_SMALL
;
2369 // Buffer now contains room enough to store EFI_FILE_INFO structure.
2370 // Now, fill it in with all necessary information about the file.
2372 FileInfo
= (EFI_FILE_INFO
*)Buffer
;
2373 FileInfo
->Size
= FileInfoLength
;
2374 FileInfo
->Attribute
&= ~EFI_FILE_VALID_ATTR
;
2375 FileInfo
->Attribute
|= EFI_FILE_READ_ONLY
;
2377 if (IS_FID_DIRECTORY_FILE (File
->FileIdentifierDesc
)) {
2378 FileInfo
->Attribute
|= EFI_FILE_DIRECTORY
;
2379 } else if (IS_FID_NORMAL_FILE (File
->FileIdentifierDesc
)) {
2380 FileInfo
->Attribute
|= EFI_FILE_ARCHIVE
;
2383 if (IS_FID_HIDDEN_FILE (File
->FileIdentifierDesc
)) {
2384 FileInfo
->Attribute
|= EFI_FILE_HIDDEN
;
2387 DescriptorTag
= File
->FileEntry
;
2389 if (DescriptorTag
->TagIdentifier
== UdfFileEntry
) {
2390 FileEntry
= (UDF_FILE_ENTRY
*)File
->FileEntry
;
2393 // Check if FE has the system attribute set.
2395 if (FileEntry
->IcbTag
.Flags
& (1 << 10)) {
2396 FileInfo
->Attribute
|= EFI_FILE_SYSTEM
;
2399 FileInfo
->FileSize
= FileSize
;
2400 FileInfo
->PhysicalSize
= FileSize
;
2402 FileInfo
->CreateTime
.Year
= FileEntry
->AccessTime
.Year
;
2403 FileInfo
->CreateTime
.Month
= FileEntry
->AccessTime
.Month
;
2404 FileInfo
->CreateTime
.Day
= FileEntry
->AccessTime
.Day
;
2405 FileInfo
->CreateTime
.Hour
= FileEntry
->AccessTime
.Hour
;
2406 FileInfo
->CreateTime
.Minute
= FileEntry
->AccessTime
.Second
;
2407 FileInfo
->CreateTime
.Second
= FileEntry
->AccessTime
.Second
;
2408 FileInfo
->CreateTime
.Nanosecond
=
2409 FileEntry
->AccessTime
.HundredsOfMicroseconds
;
2411 FileInfo
->LastAccessTime
.Year
=
2412 FileEntry
->AccessTime
.Year
;
2413 FileInfo
->LastAccessTime
.Month
=
2414 FileEntry
->AccessTime
.Month
;
2415 FileInfo
->LastAccessTime
.Day
=
2416 FileEntry
->AccessTime
.Day
;
2417 FileInfo
->LastAccessTime
.Hour
=
2418 FileEntry
->AccessTime
.Hour
;
2419 FileInfo
->LastAccessTime
.Minute
=
2420 FileEntry
->AccessTime
.Minute
;
2421 FileInfo
->LastAccessTime
.Second
=
2422 FileEntry
->AccessTime
.Second
;
2423 FileInfo
->LastAccessTime
.Nanosecond
=
2424 FileEntry
->AccessTime
.HundredsOfMicroseconds
;
2425 } else if (DescriptorTag
->TagIdentifier
== UdfExtendedFileEntry
) {
2426 ExtendedFileEntry
= (UDF_EXTENDED_FILE_ENTRY
*)File
->FileEntry
;
2429 // Check if EFE has the system attribute set.
2431 if (ExtendedFileEntry
->IcbTag
.Flags
& (1 << 10)) {
2432 FileInfo
->Attribute
|= EFI_FILE_SYSTEM
;
2435 FileInfo
->FileSize
= FileSize
;
2436 FileInfo
->PhysicalSize
= FileSize
;
2438 FileInfo
->CreateTime
.Year
= ExtendedFileEntry
->CreationTime
.Year
;
2439 FileInfo
->CreateTime
.Month
= ExtendedFileEntry
->CreationTime
.Month
;
2440 FileInfo
->CreateTime
.Day
= ExtendedFileEntry
->CreationTime
.Day
;
2441 FileInfo
->CreateTime
.Hour
= ExtendedFileEntry
->CreationTime
.Hour
;
2442 FileInfo
->CreateTime
.Minute
= ExtendedFileEntry
->CreationTime
.Second
;
2443 FileInfo
->CreateTime
.Second
= ExtendedFileEntry
->CreationTime
.Second
;
2444 FileInfo
->CreateTime
.Nanosecond
=
2445 ExtendedFileEntry
->AccessTime
.HundredsOfMicroseconds
;
2447 FileInfo
->LastAccessTime
.Year
=
2448 ExtendedFileEntry
->AccessTime
.Year
;
2449 FileInfo
->LastAccessTime
.Month
=
2450 ExtendedFileEntry
->AccessTime
.Month
;
2451 FileInfo
->LastAccessTime
.Day
=
2452 ExtendedFileEntry
->AccessTime
.Day
;
2453 FileInfo
->LastAccessTime
.Hour
=
2454 ExtendedFileEntry
->AccessTime
.Hour
;
2455 FileInfo
->LastAccessTime
.Minute
=
2456 ExtendedFileEntry
->AccessTime
.Minute
;
2457 FileInfo
->LastAccessTime
.Second
=
2458 ExtendedFileEntry
->AccessTime
.Second
;
2459 FileInfo
->LastAccessTime
.Nanosecond
=
2460 ExtendedFileEntry
->AccessTime
.HundredsOfMicroseconds
;
2463 FileInfo
->CreateTime
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
2464 FileInfo
->CreateTime
.Daylight
= EFI_TIME_ADJUST_DAYLIGHT
;
2465 FileInfo
->LastAccessTime
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
2466 FileInfo
->LastAccessTime
.Daylight
= EFI_TIME_ADJUST_DAYLIGHT
;
2468 CopyMem ((VOID
*)&FileInfo
->ModificationTime
,
2469 (VOID
*)&FileInfo
->LastAccessTime
,
2472 if (FileName
!= NULL
) {
2473 StrCpyS (FileInfo
->FileName
, StrLen (FileName
) + 1, FileName
);
2475 FileInfo
->FileName
[0] = '\0';
2478 *BufferSize
= FileInfoLength
;
2484 Get volume and free space size information of an UDF volume.
2486 @param[in] BlockIo BlockIo interface.
2487 @param[in] DiskIo DiskIo interface.
2488 @param[in] Volume UDF volume information structure.
2489 @param[out] VolumeSize Volume size.
2490 @param[out] FreeSpaceSize Free space size.
2492 @retval EFI_SUCCESS Volume and free space size calculated.
2493 @retval EFI_NO_MEDIA The device has no media.
2494 @retval EFI_DEVICE_ERROR The device reported an error.
2495 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
2496 @retval EFI_OUT_OF_RESOURCES The volume and free space size were not
2497 calculated due to lack of resources.
2502 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
2503 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
2504 IN UDF_VOLUME_INFO
*Volume
,
2505 OUT UINT64
*VolumeSize
,
2506 OUT UINT64
*FreeSpaceSize
2510 UDF_LOGICAL_VOLUME_DESCRIPTOR
*LogicalVolDesc
;
2511 UDF_EXTENT_AD
*ExtentAd
;
2513 UINT32 LogicalBlockSize
;
2514 UDF_LOGICAL_VOLUME_INTEGRITY
*LogicalVolInt
;
2515 UDF_DESCRIPTOR_TAG
*DescriptorTag
;
2520 LogicalVolDesc
= &Volume
->LogicalVolDesc
;
2522 ExtentAd
= &LogicalVolDesc
->IntegritySequenceExtent
;
2524 if (ExtentAd
->ExtentLength
== 0) {
2525 return EFI_VOLUME_CORRUPTED
;
2528 LogicalVolInt
= AllocatePool (ExtentAd
->ExtentLength
);
2529 if (LogicalVolInt
== NULL
) {
2530 return EFI_OUT_OF_RESOURCES
;
2534 // Get location of Logical Volume Integrity Descriptor
2536 Lsn
= (UINT64
)ExtentAd
->ExtentLocation
- Volume
->MainVdsStartLocation
;
2538 LogicalBlockSize
= LogicalVolDesc
->LogicalBlockSize
;
2543 Status
= DiskIo
->ReadDisk (
2545 BlockIo
->Media
->MediaId
,
2546 MultU64x32 (Lsn
, LogicalBlockSize
),
2547 ExtentAd
->ExtentLength
,
2550 if (EFI_ERROR (Status
)) {
2554 DescriptorTag
= &LogicalVolInt
->DescriptorTag
;
2557 // Check if read block is a Logical Volume Integrity Descriptor
2559 if (DescriptorTag
->TagIdentifier
!= UdfLogicalVolumeIntegrityDescriptor
) {
2560 Status
= EFI_VOLUME_CORRUPTED
;
2567 Length
= LogicalVolInt
->NumberOfPartitions
;
2568 for (Index
= 0; Index
< Length
; Index
+= sizeof (UINT32
)) {
2569 LsnsNo
= *(UINT32
*)((UINT8
*)LogicalVolInt
->Data
+ Index
);
2571 // Check if size is not specified
2573 if (LsnsNo
== 0xFFFFFFFFUL
) {
2577 // Accumulate free space size
2579 *FreeSpaceSize
+= MultU64x32 ((UINT64
)LsnsNo
, LogicalBlockSize
);
2582 Length
= LogicalVolInt
->NumberOfPartitions
* sizeof (UINT32
) * 2;
2583 for (; Index
< Length
; Index
+= sizeof (UINT32
)) {
2584 LsnsNo
= *(UINT32
*)((UINT8
*)LogicalVolInt
->Data
+ Index
);
2586 // Check if size is not specified
2588 if (LsnsNo
== 0xFFFFFFFFUL
) {
2592 // Accumulate used volume space
2594 *VolumeSize
+= MultU64x32 ((UINT64
)LsnsNo
, LogicalBlockSize
);
2597 Status
= EFI_SUCCESS
;
2601 // Free Logical Volume Integrity Descriptor
2603 FreePool (LogicalVolInt
);
2609 Seek a file and read its data into memory on an UDF volume.
2611 @param[in] BlockIo BlockIo interface.
2612 @param[in] DiskIo DiskIo interface.
2613 @param[in] Volume UDF volume information structure.
2614 @param[in] File File information structure.
2615 @param[in] FileSize Size of the file.
2616 @param[in, out] FilePosition File position.
2617 @param[in, out] Buffer File data.
2618 @param[in, out] BufferSize Read size.
2620 @retval EFI_SUCCESS File seeked and read.
2621 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.
2622 @retval EFI_NO_MEDIA The device has no media.
2623 @retval EFI_DEVICE_ERROR The device reported an error.
2624 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
2625 @retval EFI_OUT_OF_RESOURCES The file's recorded data was not read due to lack
2631 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
2632 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
2633 IN UDF_VOLUME_INFO
*Volume
,
2634 IN UDF_FILE_INFO
*File
,
2636 IN OUT UINT64
*FilePosition
,
2637 IN OUT VOID
*Buffer
,
2638 IN OUT UINT64
*BufferSize
2642 UDF_READ_FILE_INFO ReadFileInfo
;
2644 ReadFileInfo
.Flags
= ReadFileSeekAndRead
;
2645 ReadFileInfo
.FilePosition
= *FilePosition
;
2646 ReadFileInfo
.FileData
= Buffer
;
2647 ReadFileInfo
.FileDataSize
= *BufferSize
;
2648 ReadFileInfo
.FileSize
= FileSize
;
2654 &File
->FileIdentifierDesc
->Icb
,
2658 if (EFI_ERROR (Status
)) {
2662 *BufferSize
= ReadFileInfo
.FileDataSize
;
2663 *FilePosition
= ReadFileInfo
.FilePosition
;
2669 Check if ControllerHandle supports an UDF file system.
2671 @param[in] This Protocol instance pointer.
2672 @param[in] ControllerHandle Handle of device to test.
2674 @retval EFI_SUCCESS UDF file system found.
2675 @retval EFI_UNSUPPORTED UDF file system not found.
2679 SupportUdfFileSystem (
2680 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
2681 IN EFI_HANDLE ControllerHandle
2685 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2686 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
2687 EFI_DEVICE_PATH_PROTOCOL
*LastDevicePathNode
;
2688 EFI_GUID
*VendorDefinedGuid
;
2691 // Open Device Path protocol on ControllerHandle
2693 Status
= gBS
->OpenProtocol (
2695 &gEfiDevicePathProtocolGuid
,
2696 (VOID
**)&DevicePath
,
2697 This
->DriverBindingHandle
,
2699 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2701 if (EFI_ERROR (Status
)) {
2702 return EFI_UNSUPPORTED
;
2705 Status
= EFI_UNSUPPORTED
;
2708 // Get last Device Path node
2710 LastDevicePathNode
= NULL
;
2711 DevicePathNode
= DevicePath
;
2712 while (!IsDevicePathEnd (DevicePathNode
)) {
2713 LastDevicePathNode
= DevicePathNode
;
2714 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
2717 // Check if last Device Path node contains a Vendor-Defined Media Device Path
2718 // of an UDF file system.
2720 if (LastDevicePathNode
!= NULL
&&
2721 DevicePathType (LastDevicePathNode
) == MEDIA_DEVICE_PATH
&&
2722 DevicePathSubType (LastDevicePathNode
) == MEDIA_VENDOR_DP
) {
2723 VendorDefinedGuid
= (EFI_GUID
*)((UINTN
)LastDevicePathNode
+
2724 OFFSET_OF (VENDOR_DEVICE_PATH
, Guid
));
2725 if (CompareGuid (VendorDefinedGuid
, &gUdfDevPathGuid
)) {
2726 Status
= EFI_SUCCESS
;
2731 // Close Device Path protocol on ControllerHandle
2733 gBS
->CloseProtocol (
2735 &gEfiDevicePathProtocolGuid
,
2736 This
->DriverBindingHandle
,