2 Scan for an UDF file system on a formatted media.
4 Caution: This file requires additional review when modified.
5 This driver will have external input - CD/DVD media.
6 This external input must be validated carefully to avoid security issue like
7 buffer overflow, integer overflow.
9 FindUdfFileSystem() routine will consume the media properties and do basic
12 Copyright (c) 2018 Qualcomm Datacenter Technologies, Inc.
13 Copyright (C) 2014-2017 Paulo Alcantara <pcacjr@zytor.com>
14 Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
16 This program and the accompanying materials are licensed and made available
17 under the terms and conditions of the BSD License which accompanies this
18 distribution. The full text of the license may be found at
19 http://opensource.org/licenses/bsd-license.php
21 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
22 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
25 #include "Partition.h"
27 #define MAX_CORRECTION_BLOCKS_NUM 512u
30 // C5BD4D42-1A76-4996-8956-73CDA326CD0A
32 #define EFI_UDF_DEVICE_PATH_GUID \
33 { 0xC5BD4D42, 0x1A76, 0x4996, \
34 { 0x89, 0x56, 0x73, 0xCD, 0xA3, 0x26, 0xCD, 0x0A } \
38 VENDOR_DEVICE_PATH DevicePath
;
39 EFI_DEVICE_PATH_PROTOCOL End
;
43 // Vendor-Defined Device Path GUID for UDF file system
45 EFI_GUID gUdfDevPathGuid
= EFI_UDF_DEVICE_PATH_GUID
;
48 // Vendor-Defined Media Device Path for UDF file system
50 UDF_DEVICE_PATH gUdfDevicePath
= {
51 { { MEDIA_DEVICE_PATH
, MEDIA_VENDOR_DP
,
52 { sizeof (VENDOR_DEVICE_PATH
), 0 } },
53 EFI_UDF_DEVICE_PATH_GUID
55 { END_DEVICE_PATH_TYPE
, END_ENTIRE_DEVICE_PATH_SUBTYPE
,
56 { sizeof (EFI_DEVICE_PATH_PROTOCOL
), 0 }
61 Find the anchor volume descriptor pointer.
63 @param[in] BlockIo BlockIo interface.
64 @param[in] DiskIo DiskIo interface.
65 @param[out] AnchorPoint Anchor volume descriptor pointer.
66 @param[out] LastRecordedBlock Last recorded block.
68 @retval EFI_SUCCESS Anchor volume descriptor pointer found.
69 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
70 @retval other Anchor volume descriptor pointer not found.
74 FindAnchorVolumeDescriptorPointer (
75 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
76 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
77 OUT UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER
*AnchorPoint
,
78 OUT EFI_LBA
*LastRecordedBlock
84 UDF_DESCRIPTOR_TAG
*DescriptorTag
;
87 UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER
*AnchorPoints
;
89 UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER
*AnchorPointPtr
;
90 EFI_LBA LastAvdpBlockNum
;
93 // UDF 2.60, 2.2.3 Anchor Volume Descriptor Pointer
95 // An Anchor Volume Descriptor Pointer structure shall be recorded in at
96 // least 2 of the following 3 locations on the media: Logical Sector 256,
97 // N - 256 or N, where N is the last *addressable* sector of a volume.
99 // To figure out what logical sector N is, the SCSI commands READ CAPACITY and
100 // READ TRACK INFORMATION are used, however many drives or medias report their
101 // "last recorded block" wrongly. Although, READ CAPACITY returns the last
102 // readable data block but there might be unwritten blocks, which are located
103 // outside any track and therefore AVDP will not be found at block N.
105 // That said, we define a magic number of 512 blocks to be used as correction
106 // when attempting to find AVDP and define last block number.
108 BlockSize
= BlockIo
->Media
->BlockSize
;
109 EndLBA
= BlockIo
->Media
->LastBlock
;
110 *LastRecordedBlock
= EndLBA
;
114 // Check if the block size of the underlying media can hold the data of an
115 // Anchor Volume Descriptor Pointer
117 if (BlockSize
< sizeof (UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER
)) {
120 "%a: Media block size 0x%x unable to hold an AVDP.\n",
124 return EFI_UNSUPPORTED
;
128 // Find AVDP at block 256
130 Status
= DiskIo
->ReadDisk (
132 BlockIo
->Media
->MediaId
,
133 MultU64x32 (256, BlockSize
),
134 sizeof (*AnchorPoint
),
137 if (EFI_ERROR (Status
)) {
141 DescriptorTag
= &AnchorPoint
->DescriptorTag
;
144 // Check if read block is a valid AVDP descriptor
146 if (DescriptorTag
->TagIdentifier
== UdfAnchorVolumeDescriptorPointer
) {
147 DEBUG ((DEBUG_INFO
, "%a: found AVDP at block %d\n", __FUNCTION__
, 256));
152 // Find AVDP at block N - 256
154 Status
= DiskIo
->ReadDisk (
156 BlockIo
->Media
->MediaId
,
157 MultU64x32 ((UINT64
)EndLBA
- 256, BlockSize
),
158 sizeof (*AnchorPoint
),
161 if (EFI_ERROR (Status
)) {
166 // Check if read block is a valid AVDP descriptor
168 if (DescriptorTag
->TagIdentifier
== UdfAnchorVolumeDescriptorPointer
&&
170 DEBUG ((DEBUG_INFO
, "%a: found AVDP at block %Ld\n", __FUNCTION__
,
176 // Check if at least one AVDP was found in previous locations
178 if (AvdpsCount
== 0) {
179 return EFI_VOLUME_CORRUPTED
;
183 // Find AVDP at block N
185 Status
= DiskIo
->ReadDisk (
187 BlockIo
->Media
->MediaId
,
188 MultU64x32 ((UINT64
)EndLBA
, BlockSize
),
189 sizeof (*AnchorPoint
),
192 if (EFI_ERROR (Status
)) {
197 // Check if read block is a valid AVDP descriptor
199 if (DescriptorTag
->TagIdentifier
== UdfAnchorVolumeDescriptorPointer
) {
204 // No AVDP found at block N. Possibly drive/media returned bad last recorded
205 // block, or it is part of unwritten data blocks and outside any track.
207 // Search backwards for an AVDP from block N-1 through
208 // N-MAX_CORRECTION_BLOCKS_NUM. If any AVDP is found, then correct last block
209 // number for the new UDF partition child handle.
211 Size
= MAX_CORRECTION_BLOCKS_NUM
* BlockSize
;
213 AnchorPoints
= AllocateZeroPool (Size
);
214 if (AnchorPoints
== NULL
) {
215 return EFI_OUT_OF_RESOURCES
;
219 // Read consecutive MAX_CORRECTION_BLOCKS_NUM disk blocks
221 Status
= DiskIo
->ReadDisk (
223 BlockIo
->Media
->MediaId
,
224 MultU64x32 ((UINT64
)EndLBA
- MAX_CORRECTION_BLOCKS_NUM
, BlockSize
),
228 if (EFI_ERROR (Status
)) {
232 Status
= EFI_VOLUME_CORRUPTED
;
235 // Search for AVDP from blocks N-1 through N-MAX_CORRECTION_BLOCKS_NUM
237 for (Index
= MAX_CORRECTION_BLOCKS_NUM
- 2; Index
>= 0; Index
--) {
238 AnchorPointPtr
= (VOID
*)((UINTN
)AnchorPoints
+ Index
* BlockSize
);
240 DescriptorTag
= &AnchorPointPtr
->DescriptorTag
;
243 // Check if read block is a valid AVDP descriptor
245 if (DescriptorTag
->TagIdentifier
== UdfAnchorVolumeDescriptorPointer
) {
247 // Calculate last recorded block number
249 LastAvdpBlockNum
= EndLBA
- (MAX_CORRECTION_BLOCKS_NUM
- Index
);
250 DEBUG ((DEBUG_WARN
, "%a: found AVDP at block %Ld\n", __FUNCTION__
,
252 DEBUG ((DEBUG_WARN
, "%a: correcting last block from %Ld to %Ld\n",
253 __FUNCTION__
, EndLBA
, LastAvdpBlockNum
));
255 // Save read AVDP from last block
257 CopyMem (AnchorPoint
, AnchorPointPtr
, sizeof (*AnchorPointPtr
));
259 // Set last recorded block number
261 *LastRecordedBlock
= LastAvdpBlockNum
;
262 Status
= EFI_SUCCESS
;
268 FreePool (AnchorPoints
);
273 Find UDF volume identifiers in a Volume Recognition Sequence.
275 @param[in] BlockIo BlockIo interface.
276 @param[in] DiskIo DiskIo interface.
278 @retval EFI_SUCCESS UDF volume identifiers were found.
279 @retval EFI_NOT_FOUND UDF volume identifiers were not found.
280 @retval other Failed to perform disk I/O.
284 FindUdfVolumeIdentifiers (
285 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
286 IN EFI_DISK_IO_PROTOCOL
*DiskIo
291 UINT64 EndDiskOffset
;
292 CDROM_VOLUME_DESCRIPTOR VolDescriptor
;
293 CDROM_VOLUME_DESCRIPTOR TerminatingVolDescriptor
;
295 ZeroMem ((VOID
*)&TerminatingVolDescriptor
, sizeof (CDROM_VOLUME_DESCRIPTOR
));
298 // Start Volume Recognition Sequence
300 EndDiskOffset
= MultU64x32 (BlockIo
->Media
->LastBlock
,
301 BlockIo
->Media
->BlockSize
);
303 for (Offset
= UDF_VRS_START_OFFSET
; Offset
< EndDiskOffset
;
304 Offset
+= UDF_LOGICAL_SECTOR_SIZE
) {
306 // Check if block device has a Volume Structure Descriptor and an Extended
309 Status
= DiskIo
->ReadDisk (
311 BlockIo
->Media
->MediaId
,
313 sizeof (CDROM_VOLUME_DESCRIPTOR
),
314 (VOID
*)&VolDescriptor
316 if (EFI_ERROR (Status
)) {
320 if (CompareMem ((VOID
*)VolDescriptor
.Unknown
.Id
,
321 (VOID
*)UDF_BEA_IDENTIFIER
,
322 sizeof (VolDescriptor
.Unknown
.Id
)) == 0) {
326 if ((CompareMem ((VOID
*)VolDescriptor
.Unknown
.Id
,
328 sizeof (VolDescriptor
.Unknown
.Id
)) != 0) ||
329 (CompareMem ((VOID
*)&VolDescriptor
,
330 (VOID
*)&TerminatingVolDescriptor
,
331 sizeof (CDROM_VOLUME_DESCRIPTOR
)) == 0)) {
332 return EFI_NOT_FOUND
;
337 // Look for "NSR0{2,3}" identifiers in the Extended Area.
339 Offset
+= UDF_LOGICAL_SECTOR_SIZE
;
340 if (Offset
>= EndDiskOffset
) {
341 return EFI_NOT_FOUND
;
344 Status
= DiskIo
->ReadDisk (
346 BlockIo
->Media
->MediaId
,
348 sizeof (CDROM_VOLUME_DESCRIPTOR
),
349 (VOID
*)&VolDescriptor
351 if (EFI_ERROR (Status
)) {
355 if ((CompareMem ((VOID
*)VolDescriptor
.Unknown
.Id
,
356 (VOID
*)UDF_NSR2_IDENTIFIER
,
357 sizeof (VolDescriptor
.Unknown
.Id
)) != 0) &&
358 (CompareMem ((VOID
*)VolDescriptor
.Unknown
.Id
,
359 (VOID
*)UDF_NSR3_IDENTIFIER
,
360 sizeof (VolDescriptor
.Unknown
.Id
)) != 0)) {
361 return EFI_NOT_FOUND
;
365 // Look for "TEA01" identifier in the Extended Area
367 Offset
+= UDF_LOGICAL_SECTOR_SIZE
;
368 if (Offset
>= EndDiskOffset
) {
369 return EFI_NOT_FOUND
;
372 Status
= DiskIo
->ReadDisk (
374 BlockIo
->Media
->MediaId
,
376 sizeof (CDROM_VOLUME_DESCRIPTOR
),
377 (VOID
*)&VolDescriptor
379 if (EFI_ERROR (Status
)) {
383 if (CompareMem ((VOID
*)VolDescriptor
.Unknown
.Id
,
384 (VOID
*)UDF_TEA_IDENTIFIER
,
385 sizeof (VolDescriptor
.Unknown
.Id
)) != 0) {
386 return EFI_NOT_FOUND
;
393 Check if Logical Volume Descriptor is supported by current EDK2 UDF file
394 system implementation.
396 @param[in] LogicalVolDesc Logical Volume Descriptor pointer.
398 @retval TRUE Logical Volume Descriptor is supported.
399 @retval FALSE Logical Volume Descriptor is not supported.
403 IsLogicalVolumeDescriptorSupported (
404 UDF_LOGICAL_VOLUME_DESCRIPTOR
*LogicalVolDesc
408 // Check for a valid UDF revision range
410 switch (LogicalVolDesc
->DomainIdentifier
.Suffix
.Domain
.UdfRevision
) {
423 // Check for a single Partition Map
425 if (LogicalVolDesc
->NumberOfPartitionMaps
> 1) {
429 // UDF 1.02 revision supports only Type 1 (Physical) partitions, but
430 // let's check it any way.
432 // PartitionMap[0] -> type
433 // PartitionMap[1] -> length (in bytes)
435 if (LogicalVolDesc
->PartitionMaps
[0] != 1 ||
436 LogicalVolDesc
->PartitionMaps
[1] != 6) {
444 Find UDF logical volume location and whether it is supported by current EDK2
445 UDF file system implementation.
447 @param[in] BlockIo BlockIo interface.
448 @param[in] DiskIo DiskIo interface.
449 @param[in] AnchorPoint Anchor volume descriptor pointer.
450 @param[in] LastRecordedBlock Last recorded block in media.
451 @param[out] MainVdsStartBlock Main VDS starting block number.
452 @param[out] MainVdsEndBlock Main VDS ending block number.
454 @retval EFI_SUCCESS UDF logical volume was found.
455 @retval EFI_VOLUME_CORRUPTED UDF file system structures are corrupted.
456 @retval EFI_UNSUPPORTED UDF logical volume is not supported.
457 @retval other Failed to perform disk I/O.
461 FindLogicalVolumeLocation (
462 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
463 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
464 IN UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER
*AnchorPoint
,
465 IN EFI_LBA LastRecordedBlock
,
466 OUT UINT64
*MainVdsStartBlock
,
467 OUT UINT64
*MainVdsEndBlock
472 UDF_EXTENT_AD
*ExtentAd
;
474 UINT64 SeqStartBlock
;
475 UINT64 GuardMainVdsStartBlock
;
478 BOOLEAN StopSequence
;
480 UDF_LOGICAL_VOLUME_DESCRIPTOR
*LogicalVolDesc
;
481 UDF_DESCRIPTOR_TAG
*DescriptorTag
;
483 BlockSize
= BlockIo
->Media
->BlockSize
;
484 ExtentAd
= &AnchorPoint
->MainVolumeDescriptorSequenceExtent
;
487 // UDF 2.60, 2.2.3.1 struct MainVolumeDescriptorSequenceExtent
489 // The Main Volume Descriptor Sequence Extent shall have a minimum length of
490 // 16 logical sectors.
492 // Also make sure it does not exceed maximum number of blocks in the disk.
494 SeqBlocksNum
= DivU64x32 ((UINT64
)ExtentAd
->ExtentLength
, BlockSize
);
495 if (SeqBlocksNum
< 16 || (EFI_LBA
)SeqBlocksNum
> LastRecordedBlock
+ 1) {
496 return EFI_VOLUME_CORRUPTED
;
500 // Check for valid Volume Descriptor Sequence starting block number
502 SeqStartBlock
= (UINT64
)ExtentAd
->ExtentLocation
;
503 if (SeqStartBlock
> LastRecordedBlock
||
504 SeqStartBlock
+ SeqBlocksNum
- 1 > LastRecordedBlock
) {
505 return EFI_VOLUME_CORRUPTED
;
508 GuardMainVdsStartBlock
= SeqStartBlock
;
511 // Allocate buffer for reading disk blocks
513 Buffer
= AllocateZeroPool ((UINTN
)BlockSize
);
514 if (Buffer
== NULL
) {
515 return EFI_OUT_OF_RESOURCES
;
518 SeqEndBlock
= SeqStartBlock
+ SeqBlocksNum
;
519 StopSequence
= FALSE
;
521 Status
= EFI_VOLUME_CORRUPTED
;
523 // Start Main Volume Descriptor Sequence
525 for (; SeqStartBlock
< SeqEndBlock
&& !StopSequence
; SeqStartBlock
++) {
529 Status
= BlockIo
->ReadBlocks (
531 BlockIo
->Media
->MediaId
,
536 if (EFI_ERROR (Status
)) {
540 DescriptorTag
= Buffer
;
543 // ECMA 167, 8.4.1 Contents of a Volume Descriptor Sequence
545 // - A Volume Descriptor Sequence shall contain one or more Primary Volume
547 // - A Volume Descriptor Sequence shall contain zero or more Implementation
548 // Use Volume Descriptors.
549 // - A Volume Descriptor Sequence shall contain zero or more Partition
551 // - A Volume Descriptor Sequence shall contain zero or more Logical Volume
553 // - A Volume Descriptor Sequence shall contain zero or more Unallocated
554 // Space Descriptors.
556 switch (DescriptorTag
->TagIdentifier
) {
557 case UdfPrimaryVolumeDescriptor
:
558 case UdfImplemenationUseVolumeDescriptor
:
559 case UdfPartitionDescriptor
:
560 case UdfUnallocatedSpaceDescriptor
:
563 case UdfLogicalVolumeDescriptor
:
564 LogicalVolDesc
= Buffer
;
567 // Check for existence of a single LVD and whether it is supported by
568 // current EDK2 UDF file system implementation.
570 if (++LvdsCount
> 1 ||
571 !IsLogicalVolumeDescriptorSupported (LogicalVolDesc
)) {
572 Status
= EFI_UNSUPPORTED
;
578 case UdfTerminatingDescriptor
:
580 // Stop the sequence when we find a Terminating Descriptor
581 // (aka Unallocated Sector), se we don't have to walk all the unallocated
582 // area unnecessarily.
589 // An invalid Volume Descriptor has been found in the sequece. Volume is
592 Status
= EFI_VOLUME_CORRUPTED
;
598 // Check if LVD was found
600 if (!EFI_ERROR (Status
) && LvdsCount
== 1) {
601 *MainVdsStartBlock
= GuardMainVdsStartBlock
;
603 // We do not need to read either LVD or PD descriptors to know the last
604 // valid block in the found UDF file system. It's already
605 // LastRecordedBlock.
607 *MainVdsEndBlock
= LastRecordedBlock
;
609 Status
= EFI_SUCCESS
;
614 // Free block read buffer
622 Find a supported UDF file system in block device.
624 @attention This is boundary function that may receive untrusted input.
625 @attention The input is from Partition.
627 The CD/DVD media is the external input, so this routine will do basic
628 validation for the media.
630 @param[in] BlockIo BlockIo interface.
631 @param[in] DiskIo DiskIo interface.
632 @param[out] StartingLBA UDF file system starting LBA.
633 @param[out] EndingLBA UDF file system starting LBA.
635 @retval EFI_SUCCESS UDF file system was found.
636 @retval other UDF file system was not found.
641 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
642 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
643 OUT EFI_LBA
*StartingLBA
,
644 OUT EFI_LBA
*EndingLBA
648 UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER AnchorPoint
;
649 EFI_LBA LastRecordedBlock
;
652 // Find UDF volume identifiers
654 Status
= FindUdfVolumeIdentifiers (BlockIo
, DiskIo
);
655 if (EFI_ERROR (Status
)) {
660 // Find Anchor Volume Descriptor Pointer
662 Status
= FindAnchorVolumeDescriptorPointer (
668 if (EFI_ERROR (Status
)) {
673 // Find Logical Volume location
675 Status
= FindLogicalVolumeLocation (
680 (UINT64
*)StartingLBA
,
688 Install child handles if the Handle supports UDF/ECMA-167 volume format.
690 @param[in] This Calling context.
691 @param[in] Handle Parent Handle.
692 @param[in] DiskIo Parent DiskIo interface.
693 @param[in] DiskIo2 Parent DiskIo2 interface.
694 @param[in] BlockIo Parent BlockIo interface.
695 @param[in] BlockIo2 Parent BlockIo2 interface.
696 @param[in] DevicePath Parent Device Path
699 @retval EFI_SUCCESS Child handle(s) was added.
700 @retval EFI_MEDIA_CHANGED Media changed Detected.
701 @retval other no child handle was added.
705 PartitionInstallUdfChildHandles (
706 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
707 IN EFI_HANDLE Handle
,
708 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
709 IN EFI_DISK_IO2_PROTOCOL
*DiskIo2
,
710 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
711 IN EFI_BLOCK_IO2_PROTOCOL
*BlockIo2
,
712 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
715 UINT32 RemainderByMediaBlockSize
;
717 EFI_BLOCK_IO_MEDIA
*Media
;
718 EFI_PARTITION_INFO_PROTOCOL PartitionInfo
;
721 BOOLEAN ChildCreated
;
723 Media
= BlockIo
->Media
;
724 ChildCreated
= FALSE
;
727 // Check if UDF logical block size is multiple of underlying device block size
730 UDF_LOGICAL_SECTOR_SIZE
, // Dividend
731 Media
->BlockSize
, // Divisor
732 &RemainderByMediaBlockSize
// Remainder
734 if (RemainderByMediaBlockSize
!= 0) {
735 return EFI_NOT_FOUND
;
739 // Detect El Torito feature first.
740 // And always continue to search for UDF.
742 Status
= PartitionInstallElToritoChildHandles (
751 if (!EFI_ERROR (Status
)) {
752 DEBUG ((DEBUG_INFO
, "PartitionDxe: El Torito standard found on handle 0x%p.\n", Handle
));
757 // Search for an UDF file system on block device
759 Status
= FindUdfFileSystem (BlockIo
, DiskIo
, &StartingLBA
, &EndingLBA
);
760 if (EFI_ERROR (Status
)) {
761 return (ChildCreated
? EFI_SUCCESS
: EFI_NOT_FOUND
);
765 // Create Partition Info protocol for UDF file system
767 ZeroMem (&PartitionInfo
, sizeof (EFI_PARTITION_INFO_PROTOCOL
));
768 PartitionInfo
.Revision
= EFI_PARTITION_INFO_PROTOCOL_REVISION
;
769 PartitionInfo
.Type
= PARTITION_TYPE_OTHER
;
772 // Install partition child handle for UDF file system
774 Status
= PartitionInstallChildHandle (
782 (EFI_DEVICE_PATH_PROTOCOL
*)&gUdfDevicePath
,
789 if (EFI_ERROR (Status
)) {
790 return (ChildCreated
? EFI_SUCCESS
: Status
);