2 Handle on-disk format and volume structures in UDF/ECMA-167 file systems.
4 Copyright (C) 2014-2017 Paulo Alcantara <pcacjr@zytor.com>
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
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.
18 FindAnchorVolumeDescriptorPointer (
19 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
20 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
21 OUT UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER
*AnchorPoint
27 EFI_LBA DescriptorLBAs
[4];
30 BlockSize
= BlockIo
->Media
->BlockSize
;
31 EndLBA
= BlockIo
->Media
->LastBlock
;
32 DescriptorLBAs
[0] = 256;
33 DescriptorLBAs
[1] = EndLBA
- 256;
34 DescriptorLBAs
[2] = EndLBA
;
35 DescriptorLBAs
[3] = 512;
37 for (Index
= 0; Index
< ARRAY_SIZE (DescriptorLBAs
); Index
++) {
38 Status
= DiskIo
->ReadDisk (
40 BlockIo
->Media
->MediaId
,
41 MultU64x32 (DescriptorLBAs
[Index
], BlockSize
),
42 sizeof (UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER
),
45 if (EFI_ERROR (Status
)) {
49 // Check if read LBA has a valid AVDP descriptor.
51 if (IS_AVDP (AnchorPoint
)) {
58 return EFI_VOLUME_CORRUPTED
;
62 StartMainVolumeDescriptorSequence (
63 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
64 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
65 IN UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER
*AnchorPoint
,
66 OUT UDF_VOLUME_INFO
*Volume
71 UDF_EXTENT_AD
*ExtentAd
;
75 UDF_LOGICAL_VOLUME_DESCRIPTOR
*LogicalVolDesc
;
76 UDF_PARTITION_DESCRIPTOR
*PartitionDesc
;
78 UINT32 LogicalBlockSize
;
81 // We've already found an ADVP on the volume. It contains the extent
82 // (MainVolumeDescriptorSequenceExtent) where the Main Volume Descriptor
83 // Sequence starts. Therefore, we'll look for Logical Volume Descriptors and
84 // Partitions Descriptors and save them in memory, accordingly.
86 // Note also that each descriptor will be aligned on a block size (BlockSize)
87 // boundary, so we need to read one block at a time.
89 BlockSize
= BlockIo
->Media
->BlockSize
;
90 ExtentAd
= &AnchorPoint
->MainVolumeDescriptorSequenceExtent
;
91 StartingLsn
= (UINT64
)ExtentAd
->ExtentLocation
;
92 EndingLsn
= StartingLsn
+ DivU64x32 (
93 (UINT64
)ExtentAd
->ExtentLength
,
97 Volume
->LogicalVolDescs
=
98 (UDF_LOGICAL_VOLUME_DESCRIPTOR
**)AllocateZeroPool (ExtentAd
->ExtentLength
);
99 if (Volume
->LogicalVolDescs
== NULL
) {
100 return EFI_OUT_OF_RESOURCES
;
103 Volume
->PartitionDescs
=
104 (UDF_PARTITION_DESCRIPTOR
**)AllocateZeroPool (ExtentAd
->ExtentLength
);
105 if (Volume
->PartitionDescs
== NULL
) {
106 Status
= EFI_OUT_OF_RESOURCES
;
107 goto Error_Alloc_Pds
;
110 Buffer
= AllocateZeroPool (BlockSize
);
111 if (Buffer
== NULL
) {
112 Status
= EFI_OUT_OF_RESOURCES
;
113 goto Error_Alloc_Buf
;
116 Volume
->LogicalVolDescsNo
= 0;
117 Volume
->PartitionDescsNo
= 0;
119 while (StartingLsn
<= EndingLsn
) {
120 Status
= DiskIo
->ReadDisk (
122 BlockIo
->Media
->MediaId
,
123 MultU64x32 (StartingLsn
, BlockSize
),
127 if (EFI_ERROR (Status
)) {
128 goto Error_Read_Disk_Blk
;
131 if (IS_TD (Buffer
)) {
133 // Found a Terminating Descriptor. Stop the sequence then.
138 if (IS_LVD (Buffer
)) {
140 // Found a Logical Volume Descriptor.
143 (UDF_LOGICAL_VOLUME_DESCRIPTOR
*)
144 AllocateZeroPool (sizeof (UDF_LOGICAL_VOLUME_DESCRIPTOR
));
145 if (LogicalVolDesc
== NULL
) {
146 Status
= EFI_OUT_OF_RESOURCES
;
147 goto Error_Alloc_Lvd
;
150 CopyMem ((VOID
*)LogicalVolDesc
, Buffer
,
151 sizeof (UDF_LOGICAL_VOLUME_DESCRIPTOR
));
152 Volume
->LogicalVolDescs
[Volume
->LogicalVolDescsNo
++] = LogicalVolDesc
;
153 } else if (IS_PD (Buffer
)) {
155 // Found a Partition Descriptor.
158 (UDF_PARTITION_DESCRIPTOR
*)
159 AllocateZeroPool (sizeof (UDF_PARTITION_DESCRIPTOR
));
160 if (PartitionDesc
== NULL
) {
161 Status
= EFI_OUT_OF_RESOURCES
;
165 CopyMem ((VOID
*)PartitionDesc
, Buffer
,
166 sizeof (UDF_PARTITION_DESCRIPTOR
));
167 Volume
->PartitionDescs
[Volume
->PartitionDescsNo
++] = PartitionDesc
;
174 // When an UDF volume (revision 2.00 or higher) contains a File Entry rather
175 // than an Extended File Entry (which is not recommended as per spec), we need
176 // to make sure the size of a FE will be _at least_ 2048
177 // (UDF_LOGICAL_SECTOR_SIZE) bytes long to keep backward compatibility.
179 LogicalBlockSize
= LV_BLOCK_SIZE (Volume
, UDF_DEFAULT_LV_NUM
);
180 if (LogicalBlockSize
>= UDF_LOGICAL_SECTOR_SIZE
) {
181 Volume
->FileEntrySize
= LogicalBlockSize
;
183 Volume
->FileEntrySize
= UDF_LOGICAL_SECTOR_SIZE
;
192 for (Index
= 0; Index
< Volume
->PartitionDescsNo
; Index
++) {
193 FreePool ((VOID
*)Volume
->PartitionDescs
[Index
]);
196 for (Index
= 0; Index
< Volume
->LogicalVolDescsNo
; Index
++) {
197 FreePool ((VOID
*)Volume
->LogicalVolDescs
[Index
]);
204 FreePool ((VOID
*)Volume
->PartitionDescs
);
205 Volume
->PartitionDescs
= NULL
;
208 FreePool ((VOID
*)Volume
->LogicalVolDescs
);
209 Volume
->LogicalVolDescs
= NULL
;
215 // Return a Partition Descriptor given a Long Allocation Descriptor. This is
216 // necessary to calculate the right extent (LongAd) offset which is added up
217 // with partition's starting location.
219 UDF_PARTITION_DESCRIPTOR
*
221 IN UDF_VOLUME_INFO
*Volume
,
222 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*LongAd
225 UDF_LOGICAL_VOLUME_DESCRIPTOR
*LogicalVolDesc
;
227 UDF_PARTITION_DESCRIPTOR
*PartitionDesc
;
230 LogicalVolDesc
= Volume
->LogicalVolDescs
[UDF_DEFAULT_LV_NUM
];
232 switch (LV_UDF_REVISION (LogicalVolDesc
)) {
235 // As per UDF 1.02 specification:
237 // There shall be exactly one prevailing Logical Volume Descriptor recorded
238 // per Volume Set. The Partition Maps field shall contain only Type 1
241 PartitionNum
= *(UINT16
*)((UINTN
)&LogicalVolDesc
->PartitionMaps
[4]);
245 // Ensure Type 1 Partition map. Other types aren't supported in this
248 if (LogicalVolDesc
->PartitionMaps
[0] != 1 ||
249 LogicalVolDesc
->PartitionMaps
[1] != 6) {
252 PartitionNum
= *(UINT16
*)((UINTN
)&LogicalVolDesc
->PartitionMaps
[4]);
259 PartitionNum
= LongAd
->ExtentLocation
.PartitionReferenceNumber
;
263 for (Index
= 0; Index
< Volume
->PartitionDescsNo
; Index
++) {
264 PartitionDesc
= Volume
->PartitionDescs
[Index
];
265 if (PartitionDesc
->PartitionNumber
== PartitionNum
) {
266 return PartitionDesc
;
274 // Return logical sector number of a given Long Allocation Descriptor.
278 IN UDF_VOLUME_INFO
*Volume
,
279 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*LongAd
282 UDF_PARTITION_DESCRIPTOR
*PartitionDesc
;
284 PartitionDesc
= GetPdFromLongAd (Volume
, LongAd
);
285 ASSERT (PartitionDesc
!= NULL
);
287 return (UINT64
)PartitionDesc
->PartitionStartingLocation
+
288 LongAd
->ExtentLocation
.LogicalBlockNumber
;
292 // Return logical sector number of a given Short Allocation Descriptor.
296 IN UDF_PARTITION_DESCRIPTOR
*PartitionDesc
,
297 IN UDF_SHORT_ALLOCATION_DESCRIPTOR
*ShortAd
300 return (UINT64
)PartitionDesc
->PartitionStartingLocation
+
301 ShortAd
->ExtentPosition
;
305 // Find File Set Descriptor of a given Logical Volume Descriptor.
307 // The found FSD will contain the extent (LogicalVolumeContentsUse) where our
308 // root directory is.
311 FindFileSetDescriptor (
312 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
313 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
314 IN UDF_VOLUME_INFO
*Volume
,
315 IN UINTN LogicalVolDescNum
,
316 OUT UDF_FILE_SET_DESCRIPTOR
*FileSetDesc
321 UDF_LOGICAL_VOLUME_DESCRIPTOR
*LogicalVolDesc
;
323 LogicalVolDesc
= Volume
->LogicalVolDescs
[LogicalVolDescNum
];
324 Lsn
= GetLongAdLsn (Volume
, &LogicalVolDesc
->LogicalVolumeContentsUse
);
327 // Read extent (Long Ad).
329 Status
= DiskIo
->ReadDisk (
331 BlockIo
->Media
->MediaId
,
332 MultU64x32 (Lsn
, LogicalVolDesc
->LogicalBlockSize
),
333 sizeof (UDF_FILE_SET_DESCRIPTOR
),
336 if (EFI_ERROR (Status
)) {
341 // Check if the read extent contains a valid FSD's tag identifier.
343 if (!IS_FSD (FileSetDesc
)) {
344 return EFI_VOLUME_CORRUPTED
;
351 // Get all File Set Descriptors for each Logical Volume Descriptor.
354 GetFileSetDescriptors (
355 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
356 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
357 IN OUT UDF_VOLUME_INFO
*Volume
362 UDF_FILE_SET_DESCRIPTOR
*FileSetDesc
;
365 Volume
->FileSetDescs
=
366 (UDF_FILE_SET_DESCRIPTOR
**)AllocateZeroPool (
367 Volume
->LogicalVolDescsNo
* sizeof (UDF_FILE_SET_DESCRIPTOR
));
368 if (Volume
->FileSetDescs
== NULL
) {
369 return EFI_OUT_OF_RESOURCES
;
372 for (Index
= 0; Index
< Volume
->LogicalVolDescsNo
; Index
++) {
373 FileSetDesc
= AllocateZeroPool (sizeof (UDF_FILE_SET_DESCRIPTOR
));
374 if (FileSetDesc
== NULL
) {
375 Status
= EFI_OUT_OF_RESOURCES
;
376 goto Error_Alloc_Fsd
;
380 // Find a FSD for this LVD.
382 Status
= FindFileSetDescriptor (
389 if (EFI_ERROR (Status
)) {
396 Volume
->FileSetDescs
[Index
] = FileSetDesc
;
399 Volume
->FileSetDescsNo
= Volume
->LogicalVolDescsNo
;
404 for (Index
= 0; Index
< Count
; Index
++) {
405 FreePool ((VOID
*)Volume
->FileSetDescs
[Index
]);
408 FreePool ((VOID
*)Volume
->FileSetDescs
);
409 Volume
->FileSetDescs
= NULL
;
416 // Read Volume and File Structure on an UDF file system.
419 ReadVolumeFileStructure (
420 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
421 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
422 OUT UDF_VOLUME_INFO
*Volume
426 UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER AnchorPoint
;
431 Status
= FindAnchorVolumeDescriptorPointer (
436 if (EFI_ERROR (Status
)) {
441 // AVDP has been found. Start MVDS.
443 Status
= StartMainVolumeDescriptorSequence (
449 if (EFI_ERROR (Status
)) {
457 // Calculate length of a given File Identifier Descriptor.
460 GetFidDescriptorLength (
461 IN UDF_FILE_IDENTIFIER_DESCRIPTOR
*FileIdentifierDesc
465 (INTN
)((OFFSET_OF (UDF_FILE_IDENTIFIER_DESCRIPTOR
, Data
[0]) + 3 +
466 FileIdentifierDesc
->LengthOfFileIdentifier
+
467 FileIdentifierDesc
->LengthOfImplementationUse
) >> 2) << 2
472 // Duplicate a given File Identifier Descriptor.
476 IN UDF_FILE_IDENTIFIER_DESCRIPTOR
*FileIdentifierDesc
,
477 OUT UDF_FILE_IDENTIFIER_DESCRIPTOR
**NewFileIdentifierDesc
480 *NewFileIdentifierDesc
=
481 (UDF_FILE_IDENTIFIER_DESCRIPTOR
*)AllocateCopyPool (
482 (UINTN
) GetFidDescriptorLength (FileIdentifierDesc
), FileIdentifierDesc
);
486 // Duplicate either a given File Entry or a given Extended File Entry.
490 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
491 IN UDF_VOLUME_INFO
*Volume
,
493 OUT VOID
**NewFileEntry
496 *NewFileEntry
= AllocateCopyPool (Volume
->FileEntrySize
, FileEntry
);
500 // Get raw data + length of a given File Entry or Extended File Entry.
502 // The file's recorded data can contain either real file content (inline) or
503 // a sequence of extents (or Allocation Descriptors) which tells where file's
504 // content is stored in.
506 // NOTE: The FE/EFE can be thought it was an inode.
510 IN VOID
*FileEntryData
,
515 UDF_EXTENDED_FILE_ENTRY
*ExtendedFileEntry
;
516 UDF_FILE_ENTRY
*FileEntry
;
518 if (IS_EFE (FileEntryData
)) {
519 ExtendedFileEntry
= (UDF_EXTENDED_FILE_ENTRY
*)FileEntryData
;
521 *Length
= ExtendedFileEntry
->InformationLength
;
522 *Data
= (VOID
*)((UINT8
*)ExtendedFileEntry
->Data
+
523 ExtendedFileEntry
->LengthOfExtendedAttributes
);
524 } else if (IS_FE (FileEntryData
)) {
525 FileEntry
= (UDF_FILE_ENTRY
*)FileEntryData
;
527 *Length
= FileEntry
->InformationLength
;
528 *Data
= (VOID
*)((UINT8
*)FileEntry
->Data
+
529 FileEntry
->LengthOfExtendedAttributes
);
534 // Get Allocation Descriptors' data information from a given FE/EFE.
538 IN VOID
*FileEntryData
,
543 UDF_EXTENDED_FILE_ENTRY
*ExtendedFileEntry
;
544 UDF_FILE_ENTRY
*FileEntry
;
546 if (IS_EFE (FileEntryData
)) {
547 ExtendedFileEntry
= (UDF_EXTENDED_FILE_ENTRY
*)FileEntryData
;
549 *Length
= ExtendedFileEntry
->LengthOfAllocationDescriptors
;
550 *AdsData
= (VOID
*)((UINT8
*)ExtendedFileEntry
->Data
+
551 ExtendedFileEntry
->LengthOfExtendedAttributes
);
552 } else if (IS_FE (FileEntryData
)) {
553 FileEntry
= (UDF_FILE_ENTRY
*)FileEntryData
;
555 *Length
= FileEntry
->LengthOfAllocationDescriptors
;
556 *AdsData
= (VOID
*)((UINT8
*)FileEntry
->Data
+
557 FileEntry
->LengthOfExtendedAttributes
);
562 // Read next Long Allocation Descriptor from a given file's data.
567 IN OUT UINT64
*Offset
,
569 OUT UDF_LONG_ALLOCATION_DESCRIPTOR
**FoundLongAd
572 UDF_LONG_ALLOCATION_DESCRIPTOR
*LongAd
;
573 UDF_EXTENT_FLAGS ExtentFlags
;
576 if (*Offset
>= Length
) {
578 // No more Long Allocation Descriptors.
580 return EFI_DEVICE_ERROR
;
584 (UDF_LONG_ALLOCATION_DESCRIPTOR
*)((UINT8
*)Data
+ *Offset
);
587 // If it's either an indirect AD (Extended Alllocation Descriptor) or an
588 // allocated AD, then return it.
590 ExtentFlags
= GET_EXTENT_FLAGS (LONG_ADS_SEQUENCE
, LongAd
);
591 if (ExtentFlags
== EXTENT_IS_NEXT_EXTENT
||
592 ExtentFlags
== EXTENT_RECORDED_AND_ALLOCATED
) {
597 // This AD is either not recorded but allocated, or not recorded and not
598 // allocated. Skip it.
600 *Offset
+= AD_LENGTH (LONG_ADS_SEQUENCE
);
603 *FoundLongAd
= LongAd
;
609 // Read next Short Allocation Descriptor from a given file's data.
614 IN OUT UINT64
*Offset
,
616 OUT UDF_SHORT_ALLOCATION_DESCRIPTOR
**FoundShortAd
619 UDF_SHORT_ALLOCATION_DESCRIPTOR
*ShortAd
;
620 UDF_EXTENT_FLAGS ExtentFlags
;
623 if (*Offset
>= Length
) {
625 // No more Short Allocation Descriptors.
627 return EFI_DEVICE_ERROR
;
631 (UDF_SHORT_ALLOCATION_DESCRIPTOR
*)((UINT8
*)Data
+ *Offset
);
634 // If it's either an indirect AD (Extended Alllocation Descriptor) or an
635 // allocated AD, then return it.
637 ExtentFlags
= GET_EXTENT_FLAGS (SHORT_ADS_SEQUENCE
, ShortAd
);
638 if (ExtentFlags
== EXTENT_IS_NEXT_EXTENT
||
639 ExtentFlags
== EXTENT_RECORDED_AND_ALLOCATED
) {
644 // This AD is either not recorded but allocated, or not recorded and not
645 // allocated. Skip it.
647 *Offset
+= AD_LENGTH (SHORT_ADS_SEQUENCE
);
650 *FoundShortAd
= ShortAd
;
656 // Get either a Short Allocation Descriptor or a Long Allocation Descriptor from
660 GetAllocationDescriptor (
661 IN UDF_FE_RECORDING_FLAGS RecordingFlags
,
663 IN OUT UINT64
*Offset
,
668 if (RecordingFlags
== LONG_ADS_SEQUENCE
) {
669 return GetLongAdFromAds (
673 (UDF_LONG_ALLOCATION_DESCRIPTOR
**)FoundAd
675 } else if (RecordingFlags
== SHORT_ADS_SEQUENCE
) {
676 return GetShortAdFromAds (
680 (UDF_SHORT_ALLOCATION_DESCRIPTOR
**)FoundAd
684 return EFI_DEVICE_ERROR
;
688 // Return logical sector number of either Short or Long Allocation Descriptor.
691 GetAllocationDescriptorLsn (
692 IN UDF_FE_RECORDING_FLAGS RecordingFlags
,
693 IN UDF_VOLUME_INFO
*Volume
,
694 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*ParentIcb
,
698 if (RecordingFlags
== LONG_ADS_SEQUENCE
) {
699 return GetLongAdLsn (Volume
, (UDF_LONG_ALLOCATION_DESCRIPTOR
*)Ad
);
700 } else if (RecordingFlags
== SHORT_ADS_SEQUENCE
) {
701 return GetShortAdLsn (
702 GetPdFromLongAd (Volume
, ParentIcb
),
703 (UDF_SHORT_ALLOCATION_DESCRIPTOR
*)Ad
711 // Return offset + length of a given indirect Allocation Descriptor (AED).
715 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
716 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
717 IN UDF_VOLUME_INFO
*Volume
,
718 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*ParentIcb
,
719 IN UDF_FE_RECORDING_FLAGS RecordingFlags
,
729 UINT32 LogicalBlockSize
;
730 UDF_ALLOCATION_EXTENT_DESCRIPTOR
*AllocExtDesc
;
732 ExtentLength
= GET_EXTENT_LENGTH (RecordingFlags
, Ad
);
733 Lsn
= GetAllocationDescriptorLsn (RecordingFlags
,
738 Data
= AllocatePool (ExtentLength
);
740 return EFI_OUT_OF_RESOURCES
;
743 LogicalBlockSize
= LV_BLOCK_SIZE (Volume
, UDF_DEFAULT_LV_NUM
);
748 Status
= DiskIo
->ReadDisk (
750 BlockIo
->Media
->MediaId
,
751 MultU64x32 (Lsn
, LogicalBlockSize
),
755 if (EFI_ERROR (Status
)) {
760 // Check if read extent contains a valid tag identifier for AED.
762 AllocExtDesc
= (UDF_ALLOCATION_EXTENT_DESCRIPTOR
*)Data
;
763 if (!IS_AED (AllocExtDesc
)) {
764 Status
= EFI_VOLUME_CORRUPTED
;
769 // Get AED's block offset and its length.
771 *Offset
= MultU64x32 (Lsn
, LogicalBlockSize
) +
772 sizeof (UDF_ALLOCATION_EXTENT_DESCRIPTOR
);
773 *Length
= AllocExtDesc
->LengthOfAllocationDescriptors
;
782 // Read Allocation Extent Descriptor into memory.
786 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
787 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
788 IN UDF_VOLUME_INFO
*Volume
,
789 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*ParentIcb
,
790 IN UDF_FE_RECORDING_FLAGS RecordingFlags
,
800 // Get AED's offset + length.
802 Status
= GetAedAdsOffset (
812 if (EFI_ERROR (Status
)) {
817 // Allocate buffer to read in AED's data.
819 *Data
= AllocatePool ((UINTN
) (*Length
));
821 return EFI_OUT_OF_RESOURCES
;
824 return DiskIo
->ReadDisk (
826 BlockIo
->Media
->MediaId
,
834 // Function used to serialise reads of Allocation Descriptors.
837 GrowUpBufferToNextAd (
838 IN UDF_FE_RECORDING_FLAGS RecordingFlags
,
840 IN OUT VOID
**Buffer
,
846 ExtentLength
= GET_EXTENT_LENGTH (RecordingFlags
, Ad
);
848 if (*Buffer
== NULL
) {
849 *Buffer
= AllocatePool (ExtentLength
);
850 if (*Buffer
== NULL
) {
851 return EFI_OUT_OF_RESOURCES
;
854 *Buffer
= ReallocatePool ((UINTN
) Length
, (UINTN
) (Length
+ ExtentLength
), *Buffer
);
855 if (*Buffer
== NULL
) {
856 return EFI_OUT_OF_RESOURCES
;
864 // Read data or size of either a File Entry or an Extended File Entry.
868 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
869 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
870 IN UDF_VOLUME_INFO
*Volume
,
871 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*ParentIcb
,
872 IN VOID
*FileEntryData
,
873 IN OUT UDF_READ_FILE_INFO
*ReadFileInfo
877 UINT32 LogicalBlockSize
;
889 BOOLEAN FinishedSeeking
;
891 UDF_FE_RECORDING_FLAGS RecordingFlags
;
893 LogicalBlockSize
= LV_BLOCK_SIZE (Volume
, UDF_DEFAULT_LV_NUM
);
897 // set BytesLeft to suppress incorrect compiler/analyzer warnings
901 switch (ReadFileInfo
->Flags
) {
902 case READ_FILE_GET_FILESIZE
:
903 case READ_FILE_ALLOCATE_AND_READ
:
905 // Initialise ReadFileInfo structure for either getting file size, or
906 // reading file's recorded data.
908 ReadFileInfo
->ReadLength
= 0;
909 ReadFileInfo
->FileData
= NULL
;
911 case READ_FILE_SEEK_AND_READ
:
913 // About to seek a file and/or read its data.
915 Length
= ReadFileInfo
->FileSize
- ReadFileInfo
->FilePosition
;
916 if (ReadFileInfo
->FileDataSize
> Length
) {
918 // About to read beyond the EOF -- truncate it.
920 ReadFileInfo
->FileDataSize
= Length
;
924 // Initialise data to start seeking and/or reading a file.
926 BytesLeft
= ReadFileInfo
->FileDataSize
;
929 FinishedSeeking
= FALSE
;
934 RecordingFlags
= GET_FE_RECORDING_FLAGS (FileEntryData
);
935 switch (RecordingFlags
) {
938 // There are no extents for this FE/EFE. All data is inline.
940 GetFileEntryData (FileEntryData
, &Data
, &Length
);
942 if (ReadFileInfo
->Flags
== READ_FILE_GET_FILESIZE
) {
943 ReadFileInfo
->ReadLength
= Length
;
944 } else if (ReadFileInfo
->Flags
== READ_FILE_ALLOCATE_AND_READ
) {
946 // Allocate buffer for starting read data.
948 ReadFileInfo
->FileData
= AllocatePool ((UINTN
) Length
);
949 if (ReadFileInfo
->FileData
== NULL
) {
950 return EFI_OUT_OF_RESOURCES
;
954 // Read all inline data into ReadFileInfo->FileData
956 CopyMem (ReadFileInfo
->FileData
, Data
, (UINTN
) Length
);
957 ReadFileInfo
->ReadLength
= Length
;
958 } else if (ReadFileInfo
->Flags
== READ_FILE_SEEK_AND_READ
) {
960 // If FilePosition is non-zero, seek file to FilePosition, read
961 // FileDataSize bytes and then updates FilePosition.
964 ReadFileInfo
->FileData
,
965 (VOID
*)((UINT8
*)Data
+ ReadFileInfo
->FilePosition
),
966 (UINTN
) ReadFileInfo
->FileDataSize
969 ReadFileInfo
->FilePosition
+= ReadFileInfo
->FileDataSize
;
972 return EFI_INVALID_PARAMETER
;
975 Status
= EFI_SUCCESS
;
978 case LONG_ADS_SEQUENCE
:
979 case SHORT_ADS_SEQUENCE
:
981 // This FE/EFE contains a run of Allocation Descriptors. Get data + size
982 // for start reading them out.
984 GetAdsInformation (FileEntryData
, &Data
, &Length
);
991 Status
= GetAllocationDescriptor (
998 if (Status
== EFI_DEVICE_ERROR
) {
999 Status
= EFI_SUCCESS
;
1004 // Check if AD is an indirect AD. If so, read Allocation Extent
1005 // Descriptor and its extents (ADs).
1007 if (GET_EXTENT_FLAGS (RecordingFlags
, Ad
) == EXTENT_IS_NEXT_EXTENT
) {
1014 Status
= GetAedAdsData (
1024 if (EFI_ERROR (Status
)) {
1032 ExtentLength
= GET_EXTENT_LENGTH (RecordingFlags
, Ad
);
1034 Lsn
= GetAllocationDescriptorLsn (RecordingFlags
,
1039 switch (ReadFileInfo
->Flags
) {
1040 case READ_FILE_GET_FILESIZE
:
1041 ReadFileInfo
->ReadLength
+= ExtentLength
;
1043 case READ_FILE_ALLOCATE_AND_READ
:
1045 // Increase FileData (if necessary) to read next extent.
1047 Status
= GrowUpBufferToNextAd (
1050 &ReadFileInfo
->FileData
,
1051 ReadFileInfo
->ReadLength
1053 if (EFI_ERROR (Status
)) {
1054 goto Error_Alloc_Buffer_To_Next_Ad
;
1058 // Read extent's data into FileData.
1060 Status
= DiskIo
->ReadDisk (
1062 BlockIo
->Media
->MediaId
,
1063 MultU64x32 (Lsn
, LogicalBlockSize
),
1065 (VOID
*)((UINT8
*)ReadFileInfo
->FileData
+
1066 ReadFileInfo
->ReadLength
)
1068 if (EFI_ERROR (Status
)) {
1069 goto Error_Read_Disk_Blk
;
1072 ReadFileInfo
->ReadLength
+= ExtentLength
;
1074 case READ_FILE_SEEK_AND_READ
:
1076 // Seek file first before reading in its data.
1078 if (FinishedSeeking
) {
1080 goto Skip_File_Seek
;
1083 if (FilePosition
+ ExtentLength
< ReadFileInfo
->FilePosition
) {
1084 FilePosition
+= ExtentLength
;
1088 if (FilePosition
+ ExtentLength
> ReadFileInfo
->FilePosition
) {
1089 Offset
= ReadFileInfo
->FilePosition
- FilePosition
;
1095 // Done with seeking file. Start reading its data.
1097 FinishedSeeking
= TRUE
;
1101 // Make sure we don't read more data than really wanted.
1103 if (ExtentLength
- Offset
> BytesLeft
) {
1104 DataLength
= BytesLeft
;
1106 DataLength
= ExtentLength
- Offset
;
1110 // Read extent's data into FileData.
1112 Status
= DiskIo
->ReadDisk (
1114 BlockIo
->Media
->MediaId
,
1115 Offset
+ MultU64x32 (Lsn
, LogicalBlockSize
),
1117 (VOID
*)((UINT8
*)ReadFileInfo
->FileData
+
1120 if (EFI_ERROR (Status
)) {
1121 goto Error_Read_Disk_Blk
;
1125 // Update current file's position.
1127 DataOffset
+= DataLength
;
1128 ReadFileInfo
->FilePosition
+= DataLength
;
1130 BytesLeft
-= DataLength
;
1131 if (BytesLeft
== 0) {
1133 // There is no more file data to read.
1135 Status
= EFI_SUCCESS
;
1144 // Point to the next AD (extent).
1146 AdOffset
+= AD_LENGTH (RecordingFlags
);
1150 case EXTENDED_ADS_SEQUENCE
:
1151 // FIXME: Not supported. Got no volume with it, yet.
1153 Status
= EFI_UNSUPPORTED
;
1158 // A flag value reserved by the ECMA-167 standard (3rd Edition - June
1159 // 1997); 14.6 ICB Tag; 14.6.8 Flags (RBP 18); was found.
1161 Status
= EFI_UNSUPPORTED
;
1172 Error_Read_Disk_Blk
:
1173 Error_Alloc_Buffer_To_Next_Ad
:
1174 if (ReadFileInfo
->Flags
!= READ_FILE_SEEK_AND_READ
) {
1175 FreePool (ReadFileInfo
->FileData
);
1187 // Find a file by its filename from a given Parent file.
1191 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
1192 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
1193 IN UDF_VOLUME_INFO
*Volume
,
1194 IN CHAR16
*FileName
,
1195 IN UDF_FILE_INFO
*Parent
,
1196 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*Icb
,
1197 OUT UDF_FILE_INFO
*File
1201 UDF_FILE_IDENTIFIER_DESCRIPTOR
*FileIdentifierDesc
;
1202 UDF_READ_DIRECTORY_INFO ReadDirInfo
;
1204 CHAR16 FoundFileName
[UDF_FILENAME_LENGTH
];
1205 VOID
*CompareFileEntry
;
1208 // Check if parent file is really directory.
1210 if (!IS_FE_DIRECTORY (Parent
->FileEntry
)) {
1211 return EFI_NOT_FOUND
;
1215 // If FileName is current file or working directory, just duplicate Parent's
1216 // FE/EFE and FID descriptors.
1218 if (StrCmp (FileName
, L
".") == 0) {
1219 DuplicateFe (BlockIo
, Volume
, Parent
->FileEntry
, &File
->FileEntry
);
1220 DuplicateFid (Parent
->FileIdentifierDesc
, &File
->FileIdentifierDesc
);
1226 // Start directory listing.
1228 ZeroMem ((VOID
*)&ReadDirInfo
, sizeof (UDF_READ_DIRECTORY_INFO
));
1232 Status
= ReadDirectoryEntry (
1236 Parent
->FileIdentifierDesc
?
1237 &Parent
->FileIdentifierDesc
->Icb
:
1243 if (EFI_ERROR (Status
)) {
1244 if (Status
== EFI_DEVICE_ERROR
) {
1245 Status
= EFI_NOT_FOUND
;
1251 if (IS_FID_PARENT_FILE (FileIdentifierDesc
)) {
1253 // This FID contains the location (FE/EFE) of the parent directory of this
1254 // directory (Parent), and if FileName is either ".." or "\\", then it's
1255 // the expected FID.
1257 if (StrCmp (FileName
, L
"..") == 0 || StrCmp (FileName
, L
"\\") == 0) {
1262 Status
= GetFileNameFromFid (FileIdentifierDesc
, FoundFileName
);
1263 if (EFI_ERROR (Status
)) {
1267 if (StrCmp (FileName
, FoundFileName
) == 0) {
1269 // FID has been found. Prepare to find its respective FE/EFE.
1276 FreePool ((VOID
*)FileIdentifierDesc
);
1279 if (ReadDirInfo
.DirectoryData
!= NULL
) {
1281 // Free all allocated resources for the directory listing.
1283 FreePool (ReadDirInfo
.DirectoryData
);
1287 Status
= EFI_SUCCESS
;
1289 File
->FileIdentifierDesc
= FileIdentifierDesc
;
1292 // If the requested file is root directory, then the FE/EFE was already
1293 // retrieved in UdfOpenVolume() function, thus no need to find it again.
1295 // Otherwise, find FE/EFE from the respective FID.
1297 if (StrCmp (FileName
, L
"\\") != 0) {
1298 Status
= FindFileEntry (
1302 &FileIdentifierDesc
->Icb
,
1305 if (EFI_ERROR (Status
)) {
1310 // Make sure that both Parent's FE/EFE and found FE/EFE are not equal.
1312 if (CompareMem ((VOID
*)Parent
->FileEntry
, (VOID
*)CompareFileEntry
,
1313 Volume
->FileEntrySize
) != 0) {
1314 File
->FileEntry
= CompareFileEntry
;
1316 FreePool ((VOID
*)FileIdentifierDesc
);
1317 FreePool ((VOID
*)CompareFileEntry
);
1318 Status
= EFI_NOT_FOUND
;
1326 FreePool ((VOID
*)FileIdentifierDesc
);
1332 Read volume information on a medium which contains a valid UDF file system.
1334 @param[in] BlockIo BlockIo interface.
1335 @param[in] DiskIo DiskIo interface.
1336 @param[out] Volume UDF volume information structure.
1338 @retval EFI_SUCCESS Volume information read.
1339 @retval EFI_NO_MEDIA The device has no media.
1340 @retval EFI_DEVICE_ERROR The device reported an error.
1341 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1342 @retval EFI_OUT_OF_RESOURCES The volume was not read due to lack of resources.
1346 ReadUdfVolumeInformation (
1347 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
1348 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
1349 OUT UDF_VOLUME_INFO
*Volume
1354 Status
= ReadVolumeFileStructure (
1359 if (EFI_ERROR (Status
)) {
1363 Status
= GetFileSetDescriptors (
1368 if (EFI_ERROR (Status
)) {
1369 CleanupVolumeInformation (Volume
);
1376 Find the root directory on an UDF volume.
1378 @param[in] BlockIo BlockIo interface.
1379 @param[in] DiskIo DiskIo interface.
1380 @param[in] Volume UDF volume information structure.
1381 @param[out] File Root directory file.
1383 @retval EFI_SUCCESS Root directory found.
1384 @retval EFI_NO_MEDIA The device has no media.
1385 @retval EFI_DEVICE_ERROR The device reported an error.
1386 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1387 @retval EFI_OUT_OF_RESOURCES The root directory was not found due to lack of
1393 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
1394 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
1395 IN UDF_VOLUME_INFO
*Volume
,
1396 OUT UDF_FILE_INFO
*File
1400 UDF_FILE_INFO Parent
;
1402 Status
= FindFileEntry (
1406 &Volume
->FileSetDescs
[0]->RootDirectoryIcb
,
1409 if (EFI_ERROR (Status
)) {
1413 Parent
.FileEntry
= File
->FileEntry
;
1414 Parent
.FileIdentifierDesc
= NULL
;
1423 &Volume
->FileSetDescs
[0]->RootDirectoryIcb
,
1426 if (EFI_ERROR (Status
)) {
1427 FreePool (File
->FileEntry
);
1434 Find either a File Entry or a Extended File Entry from a given ICB.
1436 @param[in] BlockIo BlockIo interface.
1437 @param[in] DiskIo DiskIo interface.
1438 @param[in] Volume UDF volume information structure.
1439 @param[in] Icb ICB of the FID.
1440 @param[out] FileEntry File Entry or Extended File Entry.
1442 @retval EFI_SUCCESS File Entry or Extended File Entry found.
1443 @retval EFI_NO_MEDIA The device has no media.
1444 @retval EFI_DEVICE_ERROR The device reported an error.
1445 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1446 @retval EFI_OUT_OF_RESOURCES The FE/EFE entry was not found due to lack of
1452 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
1453 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
1454 IN UDF_VOLUME_INFO
*Volume
,
1455 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*Icb
,
1456 OUT VOID
**FileEntry
1461 UINT32 LogicalBlockSize
;
1463 Lsn
= GetLongAdLsn (Volume
, Icb
);
1464 LogicalBlockSize
= LV_BLOCK_SIZE (Volume
, UDF_DEFAULT_LV_NUM
);
1466 *FileEntry
= AllocateZeroPool (Volume
->FileEntrySize
);
1467 if (*FileEntry
== NULL
) {
1468 return EFI_OUT_OF_RESOURCES
;
1474 Status
= DiskIo
->ReadDisk (
1476 BlockIo
->Media
->MediaId
,
1477 MultU64x32 (Lsn
, LogicalBlockSize
),
1478 Volume
->FileEntrySize
,
1481 if (EFI_ERROR (Status
)) {
1482 goto Error_Read_Disk_Blk
;
1486 // Check if the read extent contains a valid Tag Identifier for the expected
1489 if (!IS_FE (*FileEntry
) && !IS_EFE (*FileEntry
)) {
1490 Status
= EFI_VOLUME_CORRUPTED
;
1491 goto Error_Invalid_Fe
;
1497 Error_Read_Disk_Blk
:
1498 FreePool (*FileEntry
);
1504 Find a file given its absolute path on an UDF volume.
1506 @param[in] BlockIo BlockIo interface.
1507 @param[in] DiskIo DiskIo interface.
1508 @param[in] Volume UDF volume information structure.
1509 @param[in] FilePath File's absolute path.
1510 @param[in] Root Root directory file.
1511 @param[in] Parent Parent directory file.
1512 @param[out] File Found file.
1514 @retval EFI_SUCCESS @p FilePath was found.
1515 @retval EFI_NO_MEDIA The device has no media.
1516 @retval EFI_DEVICE_ERROR The device reported an error.
1517 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1518 @retval EFI_OUT_OF_RESOURCES The @p FilePath file was not found due to lack of
1524 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
1525 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
1526 IN UDF_VOLUME_INFO
*Volume
,
1527 IN CHAR16
*FilePath
,
1528 IN UDF_FILE_INFO
*Root
,
1529 IN UDF_FILE_INFO
*Parent
,
1530 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*Icb
,
1531 OUT UDF_FILE_INFO
*File
1535 CHAR16 FileName
[UDF_FILENAME_LENGTH
];
1536 CHAR16
*FileNamePointer
;
1537 UDF_FILE_INFO PreviousFile
;
1540 Status
= EFI_NOT_FOUND
;
1542 CopyMem ((VOID
*)&PreviousFile
, (VOID
*)Parent
, sizeof (UDF_FILE_INFO
));
1543 while (*FilePath
!= L
'\0') {
1544 FileNamePointer
= FileName
;
1545 while (*FilePath
!= L
'\0' && *FilePath
!= L
'\\') {
1546 *FileNamePointer
++ = *FilePath
++;
1549 *FileNamePointer
= L
'\0';
1550 if (FileName
[0] == L
'\0') {
1552 // Open root directory.
1556 // There is no file found for the root directory yet. So, find only its
1559 // See UdfOpenVolume() function.
1561 Status
= InternalFindFile (BlockIo
,
1570 // We've already a file pointer (Root) for the root directory. Duplicate
1571 // its FE/EFE and FID descriptors.
1573 DuplicateFe (BlockIo
, Volume
, Root
->FileEntry
, &File
->FileEntry
);
1574 DuplicateFid (Root
->FileIdentifierDesc
, &File
->FileIdentifierDesc
);
1575 Status
= EFI_SUCCESS
;
1579 // No root directory. Find filename from the current directory.
1581 Status
= InternalFindFile (BlockIo
,
1590 if (EFI_ERROR (Status
)) {
1595 // If the found file is a symlink, then find its respective FE/EFE and
1598 if (IS_FE_SYMLINK (File
->FileEntry
)) {
1599 FreePool ((VOID
*)File
->FileIdentifierDesc
);
1601 FileEntry
= File
->FileEntry
;
1603 Status
= ResolveSymlink (BlockIo
,
1610 FreePool (FileEntry
);
1612 if (EFI_ERROR (Status
)) {
1617 if (CompareMem ((VOID
*)&PreviousFile
, (VOID
*)Parent
,
1618 sizeof (UDF_FILE_INFO
)) != 0) {
1619 CleanupFileInformation (&PreviousFile
);
1622 CopyMem ((VOID
*)&PreviousFile
, (VOID
*)File
, sizeof (UDF_FILE_INFO
));
1623 if (*FilePath
!= L
'\0' && *FilePath
== L
'\\') {
1632 Read a directory entry at a time on an UDF volume.
1634 @param[in] BlockIo BlockIo interface.
1635 @param[in] DiskIo DiskIo interface.
1636 @param[in] Volume UDF volume information structure.
1637 @param[in] ParentIcb ICB of the parent file.
1638 @param[in] FileEntryData FE/EFE of the parent file.
1639 @param[in out] ReadDirInfo Next read directory listing structure
1641 @param[out] FoundFid File Identifier Descriptor pointer.
1643 @retval EFI_SUCCESS Directory entry read.
1644 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.
1645 @retval EFI_NO_MEDIA The device has no media.
1646 @retval EFI_DEVICE_ERROR The device reported an error.
1647 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1648 @retval EFI_OUT_OF_RESOURCES The directory entry was not read due to lack of
1653 ReadDirectoryEntry (
1654 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
1655 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
1656 IN UDF_VOLUME_INFO
*Volume
,
1657 IN UDF_LONG_ALLOCATION_DESCRIPTOR
*ParentIcb
,
1658 IN VOID
*FileEntryData
,
1659 IN OUT UDF_READ_DIRECTORY_INFO
*ReadDirInfo
,
1660 OUT UDF_FILE_IDENTIFIER_DESCRIPTOR
**FoundFid
1664 UDF_READ_FILE_INFO ReadFileInfo
;
1665 UDF_FILE_IDENTIFIER_DESCRIPTOR
*FileIdentifierDesc
;
1667 if (ReadDirInfo
->DirectoryData
== NULL
) {
1669 // The directory's recorded data has not been read yet. So let's cache it
1670 // into memory and the next calls won't need to read it again.
1672 ReadFileInfo
.Flags
= READ_FILE_ALLOCATE_AND_READ
;
1682 if (EFI_ERROR (Status
)) {
1687 // Fill in ReadDirInfo structure with the read directory's data information.
1689 ReadDirInfo
->DirectoryData
= ReadFileInfo
.FileData
;
1690 ReadDirInfo
->DirectoryLength
= ReadFileInfo
.ReadLength
;
1694 if (ReadDirInfo
->FidOffset
>= ReadDirInfo
->DirectoryLength
) {
1696 // There are no longer FIDs for this directory. By returning
1697 // EFI_DEVICE_ERROR to the callee will indicate end of directory
1700 return EFI_DEVICE_ERROR
;
1704 // Get FID for this entry.
1706 FileIdentifierDesc
= GET_FID_FROM_ADS (ReadDirInfo
->DirectoryData
,
1707 ReadDirInfo
->FidOffset
);
1709 // Update FidOffset to point to next FID.
1711 ReadDirInfo
->FidOffset
+= GetFidDescriptorLength (FileIdentifierDesc
);
1712 } while (IS_FID_DELETED_FILE (FileIdentifierDesc
));
1714 DuplicateFid (FileIdentifierDesc
, FoundFid
);
1720 Get a filename (encoded in OSTA-compressed format) from a File Identifier
1721 Descriptor on an UDF volume.
1723 @param[in] FileIdentifierDesc File Identifier Descriptor pointer.
1724 @param[out] FileName Decoded filename.
1726 @retval EFI_SUCCESS Filename decoded and read.
1727 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1730 GetFileNameFromFid (
1731 IN UDF_FILE_IDENTIFIER_DESCRIPTOR
*FileIdentifierDesc
,
1732 OUT CHAR16
*FileName
1735 UINT8
*OstaCompressed
;
1736 UINT8 CompressionId
;
1742 (UINT8
*)FileIdentifierDesc
->Data
+
1743 FileIdentifierDesc
->LengthOfImplementationUse
1746 CompressionId
= OstaCompressed
[0];
1747 if (!IS_VALID_COMPRESSION_ID (CompressionId
)) {
1748 return EFI_VOLUME_CORRUPTED
;
1754 Length
= FileIdentifierDesc
->LengthOfFileIdentifier
;
1755 for (Index
= 1; Index
< Length
; Index
++) {
1756 if (CompressionId
== 16) {
1757 *FileName
= OstaCompressed
[Index
++] << 8;
1762 if (Index
< Length
) {
1763 *FileName
|= OstaCompressed
[Index
];
1775 Resolve a symlink file on an UDF volume.
1777 @param[in] BlockIo BlockIo interface.
1778 @param[in] DiskIo DiskIo interface.
1779 @param[in] Volume UDF volume information structure.
1780 @param[in] Parent Parent file.
1781 @param[in] FileEntryData FE/EFE structure pointer.
1782 @param[out] File Resolved file.
1784 @retval EFI_SUCCESS Symlink file resolved.
1785 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.
1786 @retval EFI_NO_MEDIA The device has no media.
1787 @retval EFI_DEVICE_ERROR The device reported an error.
1788 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1789 @retval EFI_OUT_OF_RESOURCES The symlink file was not resolved due to lack of
1795 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
1796 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
1797 IN UDF_VOLUME_INFO
*Volume
,
1798 IN UDF_FILE_INFO
*Parent
,
1799 IN VOID
*FileEntryData
,
1800 OUT UDF_FILE_INFO
*File
1804 UDF_READ_FILE_INFO ReadFileInfo
;
1808 UDF_PATH_COMPONENT
*PathComp
;
1809 UINT8 PathCompLength
;
1810 CHAR16 FileName
[UDF_FILENAME_LENGTH
];
1813 UINT8 CompressionId
;
1814 UDF_FILE_INFO PreviousFile
;
1817 // Symlink files on UDF volumes do not contain so much data other than
1818 // Path Components which resolves to real filenames, so it's OK to read in
1819 // all its data here -- usually the data will be inline with the FE/EFE for
1822 ReadFileInfo
.Flags
= READ_FILE_ALLOCATE_AND_READ
;
1828 &Parent
->FileIdentifierDesc
->Icb
,
1832 if (EFI_ERROR (Status
)) {
1836 Length
= ReadFileInfo
.ReadLength
;
1838 Data
= (UINT8
*)ReadFileInfo
.FileData
;
1839 EndData
= Data
+ Length
;
1841 CopyMem ((VOID
*)&PreviousFile
, (VOID
*)Parent
, sizeof (UDF_FILE_INFO
));
1844 PathComp
= (UDF_PATH_COMPONENT
*)Data
;
1846 PathCompLength
= PathComp
->LengthOfComponentIdentifier
;
1848 switch (PathComp
->ComponentType
) {
1851 // This Path Component specifies the root directory hierarchy subject to
1852 // agreement between the originator and recipient of the medium. Skip it.
1858 // "\\." of the current directory. Read next Path Component.
1860 goto Next_Path_Component
;
1863 // ".." (parent directory). Go to it.
1865 CopyMem ((VOID
*)FileName
, L
"..", 6);
1869 // "." (current file). Duplicate both FE/EFE and FID of this file.
1871 DuplicateFe (BlockIo
, Volume
, PreviousFile
.FileEntry
, &File
->FileEntry
);
1872 DuplicateFid (PreviousFile
.FileIdentifierDesc
,
1873 &File
->FileIdentifierDesc
);
1874 goto Next_Path_Component
;
1877 // This Path Component identifies an object, either a file or a
1878 // directory or an alias.
1880 // Decode it from the compressed data in ComponentIdentifier and find
1883 CompressionId
= PathComp
->ComponentIdentifier
[0];
1884 if (!IS_VALID_COMPRESSION_ID (CompressionId
)) {
1885 return EFI_VOLUME_CORRUPTED
;
1889 for (Index
= 1; Index
< PathCompLength
; Index
++) {
1890 if (CompressionId
== 16) {
1891 *C
= *(UINT8
*)((UINT8
*)PathComp
->ComponentIdentifier
+
1898 if (Index
< Length
) {
1899 *C
|= *(UINT8
*)((UINT8
*)PathComp
->ComponentIdentifier
+ Index
);
1910 // Find file from the read filename in symlink's file data.
1912 Status
= InternalFindFile (
1921 if (EFI_ERROR (Status
)) {
1922 goto Error_Find_File
;
1925 Next_Path_Component
:
1926 Data
+= sizeof (UDF_PATH_COMPONENT
) + PathCompLength
;
1927 if (Data
>= EndData
) {
1931 if (CompareMem ((VOID
*)&PreviousFile
, (VOID
*)Parent
,
1932 sizeof (UDF_FILE_INFO
)) != 0) {
1933 CleanupFileInformation (&PreviousFile
);
1936 CopyMem ((VOID
*)&PreviousFile
, (VOID
*)File
, sizeof (UDF_FILE_INFO
));
1940 // Unmap the symlink file.
1942 FreePool (ReadFileInfo
.FileData
);
1947 if (CompareMem ((VOID
*)&PreviousFile
, (VOID
*)Parent
,
1948 sizeof (UDF_FILE_INFO
)) != 0) {
1949 CleanupFileInformation (&PreviousFile
);
1952 FreePool (ReadFileInfo
.FileData
);
1958 Clean up in-memory UDF volume information.
1960 @param[in] Volume Volume information pointer.
1964 CleanupVolumeInformation (
1965 IN UDF_VOLUME_INFO
*Volume
1970 if (Volume
->LogicalVolDescs
!= NULL
) {
1971 for (Index
= 0; Index
< Volume
->LogicalVolDescsNo
; Index
++) {
1972 FreePool ((VOID
*)Volume
->LogicalVolDescs
[Index
]);
1974 FreePool ((VOID
*)Volume
->LogicalVolDescs
);
1977 if (Volume
->PartitionDescs
!= NULL
) {
1978 for (Index
= 0; Index
< Volume
->PartitionDescsNo
; Index
++) {
1979 FreePool ((VOID
*)Volume
->PartitionDescs
[Index
]);
1981 FreePool ((VOID
*)Volume
->PartitionDescs
);
1984 if (Volume
->FileSetDescs
!= NULL
) {
1985 for (Index
= 0; Index
< Volume
->FileSetDescsNo
; Index
++) {
1986 FreePool ((VOID
*)Volume
->FileSetDescs
[Index
]);
1988 FreePool ((VOID
*)Volume
->FileSetDescs
);
1991 ZeroMem ((VOID
*)Volume
, sizeof (UDF_VOLUME_INFO
));
1995 Clean up in-memory UDF file information.
1997 @param[in] File File information pointer.
2001 CleanupFileInformation (
2002 IN UDF_FILE_INFO
*File
2005 if (File
->FileEntry
!= NULL
) {
2006 FreePool (File
->FileEntry
);
2008 if (File
->FileIdentifierDesc
!= NULL
) {
2009 FreePool ((VOID
*)File
->FileIdentifierDesc
);
2012 ZeroMem ((VOID
*)File
, sizeof (UDF_FILE_INFO
));
2016 Find a file from its absolute path on an UDF volume.
2018 @param[in] BlockIo BlockIo interface.
2019 @param[in] DiskIo DiskIo interface.
2020 @param[in] Volume UDF volume information structure.
2021 @param[in] File File information structure.
2022 @param[out] Size Size of the file.
2024 @retval EFI_SUCCESS File size calculated and set in @p Size.
2025 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.
2026 @retval EFI_NO_MEDIA The device has no media.
2027 @retval EFI_DEVICE_ERROR The device reported an error.
2028 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
2029 @retval EFI_OUT_OF_RESOURCES The file size was not calculated due to lack of
2035 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
2036 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
2037 IN UDF_VOLUME_INFO
*Volume
,
2038 IN UDF_FILE_INFO
*File
,
2043 UDF_READ_FILE_INFO ReadFileInfo
;
2045 ReadFileInfo
.Flags
= READ_FILE_GET_FILESIZE
;
2051 &File
->FileIdentifierDesc
->Icb
,
2055 if (EFI_ERROR (Status
)) {
2059 *Size
= ReadFileInfo
.ReadLength
;
2065 Set information about a file on an UDF volume.
2067 @param[in] File File pointer.
2068 @param[in] FileSize Size of the file.
2069 @param[in] FileName Filename of the file.
2070 @param[in out] BufferSize Size of the returned file infomation.
2071 @param[out] Buffer Data of the returned file information.
2073 @retval EFI_SUCCESS File information set.
2074 @retval EFI_NO_MEDIA The device has no media.
2075 @retval EFI_DEVICE_ERROR The device reported an error.
2076 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
2077 @retval EFI_OUT_OF_RESOURCES The file information was not set due to lack of
2083 IN UDF_FILE_INFO
*File
,
2085 IN CHAR16
*FileName
,
2086 IN OUT UINTN
*BufferSize
,
2090 UINTN FileInfoLength
;
2091 EFI_FILE_INFO
*FileInfo
;
2092 UDF_FILE_ENTRY
*FileEntry
;
2093 UDF_EXTENDED_FILE_ENTRY
*ExtendedFileEntry
;
2096 // Calculate the needed size for the EFI_FILE_INFO structure.
2098 FileInfoLength
= sizeof (EFI_FILE_INFO
) + (FileName
?
2099 StrSize (FileName
) :
2101 if (*BufferSize
< FileInfoLength
) {
2103 // The given Buffer has no size enough for EFI_FILE_INFO structure.
2105 *BufferSize
= FileInfoLength
;
2106 return EFI_BUFFER_TOO_SMALL
;
2110 // Buffer now contains room enough to store EFI_FILE_INFO structure.
2111 // Now, fill it in with all necessary information about the file.
2113 FileInfo
= (EFI_FILE_INFO
*)Buffer
;
2114 FileInfo
->Size
= FileInfoLength
;
2115 FileInfo
->Attribute
&= ~EFI_FILE_VALID_ATTR
;
2116 FileInfo
->Attribute
|= EFI_FILE_READ_ONLY
;
2118 if (IS_FID_DIRECTORY_FILE (File
->FileIdentifierDesc
)) {
2119 FileInfo
->Attribute
|= EFI_FILE_DIRECTORY
;
2120 } else if (IS_FID_NORMAL_FILE (File
->FileIdentifierDesc
)) {
2121 FileInfo
->Attribute
|= EFI_FILE_ARCHIVE
;
2124 if (IS_FID_HIDDEN_FILE (File
->FileIdentifierDesc
)) {
2125 FileInfo
->Attribute
|= EFI_FILE_HIDDEN
;
2128 if (IS_FE (File
->FileEntry
)) {
2129 FileEntry
= (UDF_FILE_ENTRY
*)File
->FileEntry
;
2132 // Check if FE has the system attribute set.
2134 if (FileEntry
->IcbTag
.Flags
& (1 << 10)) {
2135 FileInfo
->Attribute
|= EFI_FILE_SYSTEM
;
2138 FileInfo
->FileSize
= FileSize
;
2139 FileInfo
->PhysicalSize
= FileSize
;
2141 FileInfo
->CreateTime
.Year
= FileEntry
->AccessTime
.Year
;
2142 FileInfo
->CreateTime
.Month
= FileEntry
->AccessTime
.Month
;
2143 FileInfo
->CreateTime
.Day
= FileEntry
->AccessTime
.Day
;
2144 FileInfo
->CreateTime
.Hour
= FileEntry
->AccessTime
.Hour
;
2145 FileInfo
->CreateTime
.Minute
= FileEntry
->AccessTime
.Second
;
2146 FileInfo
->CreateTime
.Second
= FileEntry
->AccessTime
.Second
;
2147 FileInfo
->CreateTime
.Nanosecond
=
2148 FileEntry
->AccessTime
.HundredsOfMicroseconds
;
2150 FileInfo
->LastAccessTime
.Year
=
2151 FileEntry
->AccessTime
.Year
;
2152 FileInfo
->LastAccessTime
.Month
=
2153 FileEntry
->AccessTime
.Month
;
2154 FileInfo
->LastAccessTime
.Day
=
2155 FileEntry
->AccessTime
.Day
;
2156 FileInfo
->LastAccessTime
.Hour
=
2157 FileEntry
->AccessTime
.Hour
;
2158 FileInfo
->LastAccessTime
.Minute
=
2159 FileEntry
->AccessTime
.Minute
;
2160 FileInfo
->LastAccessTime
.Second
=
2161 FileEntry
->AccessTime
.Second
;
2162 FileInfo
->LastAccessTime
.Nanosecond
=
2163 FileEntry
->AccessTime
.HundredsOfMicroseconds
;
2164 } else if (IS_EFE (File
->FileEntry
)) {
2165 ExtendedFileEntry
= (UDF_EXTENDED_FILE_ENTRY
*)File
->FileEntry
;
2168 // Check if EFE has the system attribute set.
2170 if (ExtendedFileEntry
->IcbTag
.Flags
& (1 << 10)) {
2171 FileInfo
->Attribute
|= EFI_FILE_SYSTEM
;
2174 FileInfo
->FileSize
= FileSize
;
2175 FileInfo
->PhysicalSize
= FileSize
;
2177 FileInfo
->CreateTime
.Year
= ExtendedFileEntry
->CreationTime
.Year
;
2178 FileInfo
->CreateTime
.Month
= ExtendedFileEntry
->CreationTime
.Month
;
2179 FileInfo
->CreateTime
.Day
= ExtendedFileEntry
->CreationTime
.Day
;
2180 FileInfo
->CreateTime
.Hour
= ExtendedFileEntry
->CreationTime
.Hour
;
2181 FileInfo
->CreateTime
.Minute
= ExtendedFileEntry
->CreationTime
.Second
;
2182 FileInfo
->CreateTime
.Second
= ExtendedFileEntry
->CreationTime
.Second
;
2183 FileInfo
->CreateTime
.Nanosecond
=
2184 ExtendedFileEntry
->AccessTime
.HundredsOfMicroseconds
;
2186 FileInfo
->LastAccessTime
.Year
=
2187 ExtendedFileEntry
->AccessTime
.Year
;
2188 FileInfo
->LastAccessTime
.Month
=
2189 ExtendedFileEntry
->AccessTime
.Month
;
2190 FileInfo
->LastAccessTime
.Day
=
2191 ExtendedFileEntry
->AccessTime
.Day
;
2192 FileInfo
->LastAccessTime
.Hour
=
2193 ExtendedFileEntry
->AccessTime
.Hour
;
2194 FileInfo
->LastAccessTime
.Minute
=
2195 ExtendedFileEntry
->AccessTime
.Minute
;
2196 FileInfo
->LastAccessTime
.Second
=
2197 ExtendedFileEntry
->AccessTime
.Second
;
2198 FileInfo
->LastAccessTime
.Nanosecond
=
2199 ExtendedFileEntry
->AccessTime
.HundredsOfMicroseconds
;
2202 FileInfo
->CreateTime
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
2203 FileInfo
->CreateTime
.Daylight
= EFI_TIME_ADJUST_DAYLIGHT
;
2204 FileInfo
->LastAccessTime
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
2205 FileInfo
->LastAccessTime
.Daylight
= EFI_TIME_ADJUST_DAYLIGHT
;
2207 CopyMem ((VOID
*)&FileInfo
->ModificationTime
,
2208 (VOID
*)&FileInfo
->LastAccessTime
,
2211 if (FileName
!= NULL
) {
2212 StrCpyS (FileInfo
->FileName
, StrLen (FileName
) + 1, FileName
);
2214 FileInfo
->FileName
[0] = '\0';
2217 *BufferSize
= FileInfoLength
;
2223 Get volume and free space size information of an UDF volume.
2225 @param[in] BlockIo BlockIo interface.
2226 @param[in] DiskIo DiskIo interface.
2227 @param[in] Volume UDF volume information structure.
2228 @param[out] VolumeSize Volume size.
2229 @param[out] FreeSpaceSize Free space size.
2231 @retval EFI_SUCCESS Volume and free space size calculated.
2232 @retval EFI_NO_MEDIA The device has no media.
2233 @retval EFI_DEVICE_ERROR The device reported an error.
2234 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
2235 @retval EFI_OUT_OF_RESOURCES The volume and free space size were not
2236 calculated due to lack of resources.
2241 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
2242 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
2243 IN UDF_VOLUME_INFO
*Volume
,
2244 OUT UINT64
*VolumeSize
,
2245 OUT UINT64
*FreeSpaceSize
2248 UDF_EXTENT_AD ExtentAd
;
2249 UINT32 LogicalBlockSize
;
2252 UDF_LOGICAL_VOLUME_INTEGRITY
*LogicalVolInt
;
2260 for (Index
= 0; Index
< Volume
->LogicalVolDescsNo
; Index
++) {
2261 CopyMem ((VOID
*)&ExtentAd
,
2262 (VOID
*)&Volume
->LogicalVolDescs
[Index
]->IntegritySequenceExtent
,
2263 sizeof (UDF_EXTENT_AD
));
2264 if (ExtentAd
.ExtentLength
== 0) {
2268 LogicalBlockSize
= LV_BLOCK_SIZE (Volume
, Index
);
2271 LogicalVolInt
= (UDF_LOGICAL_VOLUME_INTEGRITY
*)
2272 AllocatePool (ExtentAd
.ExtentLength
);
2273 if (LogicalVolInt
== NULL
) {
2274 return EFI_OUT_OF_RESOURCES
;
2277 Lsn
= (UINT64
)ExtentAd
.ExtentLocation
;
2279 Status
= DiskIo
->ReadDisk (
2281 BlockIo
->Media
->MediaId
,
2282 MultU64x32 (Lsn
, LogicalBlockSize
),
2283 ExtentAd
.ExtentLength
,
2284 (VOID
*)LogicalVolInt
2286 if (EFI_ERROR (Status
)) {
2287 FreePool ((VOID
*)LogicalVolInt
);
2291 if (!IS_LVID (LogicalVolInt
)) {
2292 FreePool ((VOID
*)LogicalVolInt
);
2293 return EFI_VOLUME_CORRUPTED
;
2296 Length
= LogicalVolInt
->NumberOfPartitions
;
2297 for (Index
= 0; Index
< Length
; Index
+= sizeof (UINT32
)) {
2298 LsnsNo
= *(UINT32
*)((UINT8
*)LogicalVolInt
->Data
+ Index
);
2299 if (LsnsNo
== 0xFFFFFFFFUL
) {
2301 // Size not specified.
2306 *FreeSpaceSize
+= MultU64x32 ((UINT64
)LsnsNo
, LogicalBlockSize
);
2309 Length
= (LogicalVolInt
->NumberOfPartitions
* sizeof (UINT32
)) << 1;
2310 for (; Index
< Length
; Index
+= sizeof (UINT32
)) {
2311 LsnsNo
= *(UINT32
*)((UINT8
*)LogicalVolInt
->Data
+ Index
);
2312 if (LsnsNo
== 0xFFFFFFFFUL
) {
2314 // Size not specified.
2319 *VolumeSize
+= MultU64x32 ((UINT64
)LsnsNo
, LogicalBlockSize
);
2322 CopyMem ((VOID
*)&ExtentAd
,(VOID
*)&LogicalVolInt
->NextIntegrityExtent
,
2323 sizeof (UDF_EXTENT_AD
));
2324 if (ExtentAd
.ExtentLength
> 0) {
2325 FreePool ((VOID
*)LogicalVolInt
);
2326 goto Read_Next_Sequence
;
2329 FreePool ((VOID
*)LogicalVolInt
);
2336 Seek a file and read its data into memory on an UDF volume.
2338 @param[in] BlockIo BlockIo interface.
2339 @param[in] DiskIo DiskIo interface.
2340 @param[in] Volume UDF volume information structure.
2341 @param[in] File File information structure.
2342 @param[in] FileSize Size of the file.
2343 @param[in out] FilePosition File position.
2344 @param[in out] Buffer File data.
2345 @param[in out] BufferSize Read size.
2347 @retval EFI_SUCCESS File seeked and read.
2348 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.
2349 @retval EFI_NO_MEDIA The device has no media.
2350 @retval EFI_DEVICE_ERROR The device reported an error.
2351 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
2352 @retval EFI_OUT_OF_RESOURCES The file's recorded data was not read due to lack
2358 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
2359 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
2360 IN UDF_VOLUME_INFO
*Volume
,
2361 IN UDF_FILE_INFO
*File
,
2363 IN OUT UINT64
*FilePosition
,
2364 IN OUT VOID
*Buffer
,
2365 IN OUT UINT64
*BufferSize
2369 UDF_READ_FILE_INFO ReadFileInfo
;
2371 ReadFileInfo
.Flags
= READ_FILE_SEEK_AND_READ
;
2372 ReadFileInfo
.FilePosition
= *FilePosition
;
2373 ReadFileInfo
.FileData
= Buffer
;
2374 ReadFileInfo
.FileDataSize
= *BufferSize
;
2375 ReadFileInfo
.FileSize
= FileSize
;
2381 &File
->FileIdentifierDesc
->Icb
,
2385 if (EFI_ERROR (Status
)) {
2389 *BufferSize
= ReadFileInfo
.FileDataSize
;
2390 *FilePosition
= ReadFileInfo
.FilePosition
;
2396 Check if ControllerHandle supports an UDF file system.
2398 @param[in] This Protocol instance pointer.
2399 @param[in] ControllerHandle Handle of device to test.
2401 @retval EFI_SUCCESS UDF file system found.
2402 @retval EFI_UNSUPPORTED UDF file system not found.
2406 SupportUdfFileSystem (
2407 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
2408 IN EFI_HANDLE ControllerHandle
2412 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2413 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
2414 EFI_DEVICE_PATH_PROTOCOL
*LastDevicePathNode
;
2415 EFI_GUID
*VendorDefinedGuid
;
2416 EFI_GUID UdfDevPathGuid
= EFI_UDF_DEVICE_PATH_GUID
;
2419 // Open Device Path protocol on ControllerHandle
2421 Status
= gBS
->OpenProtocol (
2423 &gEfiDevicePathProtocolGuid
,
2424 (VOID
**)&DevicePath
,
2425 This
->DriverBindingHandle
,
2427 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2429 if (EFI_ERROR (Status
)) {
2430 return EFI_UNSUPPORTED
;
2433 Status
= EFI_UNSUPPORTED
;
2436 // Get last Device Path node
2438 LastDevicePathNode
= NULL
;
2439 DevicePathNode
= DevicePath
;
2440 while (!IsDevicePathEnd (DevicePathNode
)) {
2441 LastDevicePathNode
= DevicePathNode
;
2442 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
2445 // Check if last Device Path node contains a Vendor-Defined Media Device Path
2446 // of an UDF file system.
2448 if (LastDevicePathNode
!= NULL
&&
2449 DevicePathType (LastDevicePathNode
) == MEDIA_DEVICE_PATH
&&
2450 DevicePathSubType (LastDevicePathNode
) == MEDIA_VENDOR_DP
) {
2451 VendorDefinedGuid
= (EFI_GUID
*)((UINTN
)LastDevicePathNode
+
2452 OFFSET_OF (VENDOR_DEVICE_PATH
, Guid
));
2453 if (CompareGuid (VendorDefinedGuid
, &UdfDevPathGuid
)) {
2454 Status
= EFI_SUCCESS
;
2459 // Close Device Path protocol on ControllerHandle
2461 gBS
->CloseProtocol (
2463 &gEfiDevicePathProtocolGuid
,
2464 This
->DriverBindingHandle
,