]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/PartitionDxe: Fix UDF fs access on certain CD/DVD medias
authorPaulo Alcantara <pcacjr@zytor.com>
Fri, 13 Oct 2017 13:24:49 +0000 (21:24 +0800)
committerHao Wu <hao.a.wu@intel.com>
Wed, 15 Nov 2017 12:43:42 +0000 (20:43 +0800)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=725

Historically many drives or medias do not correctly return their value
for block N - which is also referred as last addressable/recorded block.
When they do so, there is still a problem when relying on last recorded
block number returned by SCSI commands READ CAPACITY and READ TRACK
INFORMATION - that is, between block 0 and block N there may be
unwritten blocks which are located outside any track.

That said, the Partition driver was unable to find AVDP at block N on
certain medias that do not either return or report their last recorded
block number correctly.

Apparently there is no official or correct way to find the correct block
number, however tools like the Philips UDF Conformance Tool (udf_test)
apply a correction by searching for an AVDP or VAT in blocks N through
N-456 -- this can be observed by looking at the log reported by udf_test
on those CD/DVD medias. So, if the AVDP or VAT is found, then it sets
the last recorded block number to where AVDP or VAT was located.

With the below setence in UDF 2.60, 6.13.2.2 Background Physical
Formatting:

"... the second AVDP must be recorded after the Background physical
Formatting has been finished..."

Implies that the last recorded block is the one where second AVDP was
recorded.

This patch implements a similar way to correct the last recorded block
number by searching for last AVDP in blocks N-1 through N-512 on those
certain medias, as well as ensure a minimum number of 2 AVDPs found as
specified by ECMA 167 and UDF 2.60 specifications.

Cc: Hao Wu <hao.a.wu@intel.com>
Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Star Zeng <star.zeng@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Reported-by: Hao Wu <hao.a.wu@intel.com>
Signed-off-by: Paulo Alcantara <pcacjr@zytor.com>
Reviewed-by: Hao Wu <hao.a.wu@intel.com>
MdeModulePkg/Universal/Disk/PartitionDxe/Udf.c

index 8aee30c759e3b4cea9cb17534b9f8a86f5fe84f5..7eee74895872134d81cc2c99c23a3bf95bf54a71 100644 (file)
@@ -14,6 +14,8 @@
 \r
 #include "Partition.h"\r
 \r
+#define MAX_CORRECTION_BLOCKS_NUM 512u\r
+\r
 //\r
 // C5BD4D42-1A76-4996-8956-73CDA326CD0A\r
 //\r
@@ -48,61 +50,199 @@ UDF_DEVICE_PATH gUdfDevicePath = {
 /**\r
   Find the anchor volume descriptor pointer.\r
 \r
-  @param[in]  BlockIo             BlockIo interface.\r
-  @param[in]  DiskIo              DiskIo interface.\r
-  @param[out] AnchorPoint         Anchor volume descriptor pointer.\r
+  @param[in]  BlockIo               BlockIo interface.\r
+  @param[in]  DiskIo                DiskIo interface.\r
+  @param[out] AnchorPoint           Anchor volume descriptor pointer.\r
+  @param[out] LastRecordedBlock     Last recorded block.\r
 \r
-  @retval EFI_SUCCESS             Anchor volume descriptor pointer found.\r
-  @retval EFI_VOLUME_CORRUPTED    The file system structures are corrupted.\r
-  @retval other                   Anchor volume descriptor pointer not found.\r
+  @retval EFI_SUCCESS               Anchor volume descriptor pointer found.\r
+  @retval EFI_VOLUME_CORRUPTED      The file system structures are corrupted.\r
+  @retval other                     Anchor volume descriptor pointer not found.\r
 \r
 **/\r
 EFI_STATUS\r
 FindAnchorVolumeDescriptorPointer (\r
   IN   EFI_BLOCK_IO_PROTOCOL                 *BlockIo,\r
   IN   EFI_DISK_IO_PROTOCOL                  *DiskIo,\r
-  OUT  UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER  *AnchorPoint\r
+  OUT  UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER  *AnchorPoint,\r
+  OUT  EFI_LBA                               *LastRecordedBlock\r
   )\r
 {\r
-  EFI_STATUS          Status;\r
-  UINT32              BlockSize;\r
-  EFI_LBA             EndLBA;\r
-  EFI_LBA             DescriptorLBAs[4];\r
-  UINTN               Index;\r
-  UDF_DESCRIPTOR_TAG  *DescriptorTag;\r
+  EFI_STATUS                            Status;\r
+  UINT32                                BlockSize;\r
+  EFI_LBA                               EndLBA;\r
+  UDF_DESCRIPTOR_TAG                    *DescriptorTag;\r
+  UINTN                                 AvdpsCount;\r
+  UINTN                                 Size;\r
+  UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER  *AnchorPoints;\r
+  INTN                                  Index;\r
+  UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER  *AnchorPointPtr;\r
+  EFI_LBA                               LastAvdpBlockNum;\r
 \r
+  //\r
+  // UDF 2.60, 2.2.3 Anchor Volume Descriptor Pointer\r
+  //\r
+  // An Anchor Volume Descriptor Pointer structure shall be recorded in at\r
+  // least 2 of the following 3 locations on the media: Logical Sector 256,\r
+  // N - 256 or N, where N is the last *addressable* sector of a volume.\r
+  //\r
+  // To figure out what logical sector N is, the SCSI commands READ CAPACITY and\r
+  // READ TRACK INFORMATION are used, however many drives or medias report their\r
+  // "last recorded block" wrongly. Although, READ CAPACITY returns the last\r
+  // readable data block but there might be unwritten blocks, which are located\r
+  // outside any track and therefore AVDP will not be found at block N.\r
+  //\r
+  // That said, we define a magic number of 512 blocks to be used as correction\r
+  // when attempting to find AVDP and define last block number.\r
+  //\r
   BlockSize = BlockIo->Media->BlockSize;\r
   EndLBA = BlockIo->Media->LastBlock;\r
-  DescriptorLBAs[0] = 256;\r
-  DescriptorLBAs[1] = EndLBA - 256;\r
-  DescriptorLBAs[2] = EndLBA;\r
-  DescriptorLBAs[3] = 512;\r
+  *LastRecordedBlock = EndLBA;\r
+  AvdpsCount = 0;\r
 \r
-  for (Index = 0; Index < ARRAY_SIZE (DescriptorLBAs); Index++) {\r
-    Status = DiskIo->ReadDisk (\r
-      DiskIo,\r
-      BlockIo->Media->MediaId,\r
-      MultU64x32 (DescriptorLBAs[Index], BlockSize),\r
-      sizeof (UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER),\r
-      (VOID *)AnchorPoint\r
-      );\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
+  //\r
+  // Find AVDP at block 256\r
+  //\r
+  Status = DiskIo->ReadDisk (\r
+    DiskIo,\r
+    BlockIo->Media->MediaId,\r
+    MultU64x32 (256, BlockSize),\r
+    sizeof (*AnchorPoint),\r
+    AnchorPoint\r
+    );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  DescriptorTag = &AnchorPoint->DescriptorTag;\r
+\r
+  //\r
+  // Check if read block is a valid AVDP descriptor\r
+  //\r
+  if (DescriptorTag->TagIdentifier == UdfAnchorVolumeDescriptorPointer) {\r
+    DEBUG ((DEBUG_INFO, "%a: found AVDP at block %d\n", __FUNCTION__, 256));\r
+    AvdpsCount++;\r
+  }\r
+\r
+  //\r
+  // Find AVDP at block N - 256\r
+  //\r
+  Status = DiskIo->ReadDisk (\r
+    DiskIo,\r
+    BlockIo->Media->MediaId,\r
+    MultU64x32 ((UINT64)EndLBA - 256, BlockSize),\r
+    sizeof (*AnchorPoint),\r
+    AnchorPoint\r
+    );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Check if read block is a valid AVDP descriptor\r
+  //\r
+  if (DescriptorTag->TagIdentifier == UdfAnchorVolumeDescriptorPointer &&\r
+      ++AvdpsCount == 2) {\r
+    DEBUG ((DEBUG_INFO, "%a: found AVDP at block %Ld\n", __FUNCTION__,\r
+            EndLBA - 256));\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Check if at least one AVDP was found in previous locations\r
+  //\r
+  if (AvdpsCount == 0) {\r
+    return EFI_VOLUME_CORRUPTED;\r
+  }\r
+\r
+  //\r
+  // Find AVDP at block N\r
+  //\r
+  Status = DiskIo->ReadDisk (\r
+    DiskIo,\r
+    BlockIo->Media->MediaId,\r
+    MultU64x32 ((UINT64)EndLBA, BlockSize),\r
+    sizeof (*AnchorPoint),\r
+    AnchorPoint\r
+    );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Check if read block is a valid AVDP descriptor\r
+  //\r
+  if (DescriptorTag->TagIdentifier == UdfAnchorVolumeDescriptorPointer) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // No AVDP found at block N. Possibly drive/media returned bad last recorded\r
+  // block, or it is part of unwritten data blocks and outside any track.\r
+  //\r
+  // Search backwards for an AVDP from block N-1 through\r
+  // N-MAX_CORRECTION_BLOCKS_NUM. If any AVDP is found, then correct last block\r
+  // number for the new UDF partition child handle.\r
+  //\r
+  Size = MAX_CORRECTION_BLOCKS_NUM * BlockSize;\r
+\r
+  AnchorPoints = AllocateZeroPool (Size);\r
+  if (AnchorPoints == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  //\r
+  // Read consecutive MAX_CORRECTION_BLOCKS_NUM disk blocks\r
+  //\r
+  Status = DiskIo->ReadDisk (\r
+    DiskIo,\r
+    BlockIo->Media->MediaId,\r
+    MultU64x32 ((UINT64)EndLBA - MAX_CORRECTION_BLOCKS_NUM, BlockSize),\r
+    Size,\r
+    AnchorPoints\r
+    );\r
+  if (EFI_ERROR (Status)) {\r
+    goto Out_Free;\r
+  }\r
 \r
-    DescriptorTag = &AnchorPoint->DescriptorTag;\r
+  Status = EFI_VOLUME_CORRUPTED;\r
+\r
+  //\r
+  // Search for AVDP from blocks N-1 through N-MAX_CORRECTION_BLOCKS_NUM\r
+  //\r
+  for (Index = MAX_CORRECTION_BLOCKS_NUM - 2; Index >= 0; Index--) {\r
+    AnchorPointPtr = (VOID *)((UINTN)AnchorPoints + Index * BlockSize);\r
+\r
+    DescriptorTag = &AnchorPointPtr->DescriptorTag;\r
 \r
     //\r
-    // Check if read LBA has a valid AVDP descriptor.\r
+    // Check if read block is a valid AVDP descriptor\r
     //\r
     if (DescriptorTag->TagIdentifier == UdfAnchorVolumeDescriptorPointer) {\r
-      return EFI_SUCCESS;\r
+      //\r
+      // Calculate last recorded block number\r
+      //\r
+      LastAvdpBlockNum = EndLBA - (MAX_CORRECTION_BLOCKS_NUM - Index);\r
+      DEBUG ((DEBUG_WARN, "%a: found AVDP at block %Ld\n", __FUNCTION__,\r
+              LastAvdpBlockNum));\r
+      DEBUG ((DEBUG_WARN, "%a: correcting last block from %Ld to %Ld\n",\r
+              __FUNCTION__, EndLBA, LastAvdpBlockNum));\r
+      //\r
+      // Save read AVDP from last block\r
+      //\r
+      CopyMem (AnchorPoint, AnchorPointPtr, sizeof (*AnchorPointPtr));\r
+      //\r
+      // Set last recorded block number\r
+      //\r
+      *LastRecordedBlock = LastAvdpBlockNum;\r
+      Status = EFI_SUCCESS;\r
+      break;\r
     }\r
   }\r
-  //\r
-  // No AVDP found.\r
-  //\r
-  return EFI_VOLUME_CORRUPTED;\r
+\r
+Out_Free:\r
+  FreePool (AnchorPoints);\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -280,16 +420,17 @@ IsLogicalVolumeDescriptorSupported (
   Find UDF logical volume location and whether it is supported by current EDK2\r
   UDF file system implementation.\r
 \r
-  @param[in]  BlockIo             BlockIo interface.\r
-  @param[in]  DiskIo              DiskIo interface.\r
-  @param[in]  AnchorPoint         Anchor volume descriptor pointer.\r
-  @param[out] MainVdsStartBlock   Main VDS starting block number.\r
-  @param[out] MainVdsEndBlock     Main VDS ending block number.\r
+  @param[in]  BlockIo               BlockIo interface.\r
+  @param[in]  DiskIo                DiskIo interface.\r
+  @param[in]  AnchorPoint           Anchor volume descriptor pointer.\r
+  @param[in]  LastRecordedBlock     Last recorded block in media.\r
+  @param[out] MainVdsStartBlock     Main VDS starting block number.\r
+  @param[out] MainVdsEndBlock       Main VDS ending block number.\r
 \r
-  @retval EFI_SUCCESS             UDF logical volume was found.\r
-  @retval EFI_VOLUME_CORRUPTED    UDF file system structures are corrupted.\r
-  @retval EFI_UNSUPPORTED         UDF logical volume is not supported.\r
-  @retval other                   Failed to perform disk I/O.\r
+  @retval EFI_SUCCESS               UDF logical volume was found.\r
+  @retval EFI_VOLUME_CORRUPTED      UDF file system structures are corrupted.\r
+  @retval EFI_UNSUPPORTED           UDF logical volume is not supported.\r
+  @retval other                     Failed to perform disk I/O.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -297,13 +438,13 @@ FindLogicalVolumeLocation (
   IN   EFI_BLOCK_IO_PROTOCOL                 *BlockIo,\r
   IN   EFI_DISK_IO_PROTOCOL                  *DiskIo,\r
   IN   UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER  *AnchorPoint,\r
+  IN   EFI_LBA                               LastRecordedBlock,\r
   OUT  UINT64                                *MainVdsStartBlock,\r
   OUT  UINT64                                *MainVdsEndBlock\r
   )\r
 {\r
   EFI_STATUS                     Status;\r
   UINT32                         BlockSize;\r
-  EFI_LBA                        LastBlock;\r
   UDF_EXTENT_AD                  *ExtentAd;\r
   UINT64                         SeqBlocksNum;\r
   UINT64                         SeqStartBlock;\r
@@ -316,7 +457,6 @@ FindLogicalVolumeLocation (
   UDF_DESCRIPTOR_TAG             *DescriptorTag;\r
 \r
   BlockSize = BlockIo->Media->BlockSize;\r
-  LastBlock = BlockIo->Media->LastBlock;\r
   ExtentAd = &AnchorPoint->MainVolumeDescriptorSequenceExtent;\r
 \r
   //\r
@@ -328,7 +468,7 @@ FindLogicalVolumeLocation (
   // Also make sure it does not exceed maximum number of blocks in the disk.\r
   //\r
   SeqBlocksNum = DivU64x32 ((UINT64)ExtentAd->ExtentLength, BlockSize);\r
-  if (SeqBlocksNum < 16 || (EFI_LBA)SeqBlocksNum > LastBlock + 1) {\r
+  if (SeqBlocksNum < 16 || (EFI_LBA)SeqBlocksNum > LastRecordedBlock + 1) {\r
     return EFI_VOLUME_CORRUPTED;\r
   }\r
 \r
@@ -336,8 +476,8 @@ FindLogicalVolumeLocation (
   // Check for valid Volume Descriptor Sequence starting block number\r
   //\r
   SeqStartBlock = (UINT64)ExtentAd->ExtentLocation;\r
-  if (SeqStartBlock > LastBlock ||\r
-      SeqStartBlock + SeqBlocksNum - 1 > LastBlock) {\r
+  if (SeqStartBlock > LastRecordedBlock ||\r
+      SeqStartBlock + SeqBlocksNum - 1 > LastRecordedBlock) {\r
     return EFI_VOLUME_CORRUPTED;\r
   }\r
 \r
@@ -437,9 +577,10 @@ FindLogicalVolumeLocation (
     *MainVdsStartBlock = GuardMainVdsStartBlock;\r
     //\r
     // We do not need to read either LVD or PD descriptors to know the last\r
-    // valid block in the found UDF file system. It's already LastBlock.\r
+    // valid block in the found UDF file system. It's already\r
+    // LastRecordedBlock.\r
     //\r
-    *MainVdsEndBlock = LastBlock;\r
+    *MainVdsEndBlock = LastRecordedBlock;\r
 \r
     Status = EFI_SUCCESS;\r
   }\r
@@ -473,8 +614,9 @@ FindUdfFileSystem (
   OUT EFI_LBA               *EndingLBA\r
   )\r
 {\r
-  EFI_STATUS Status;\r
+  EFI_STATUS                            Status;\r
   UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER  AnchorPoint;\r
+  EFI_LBA                               LastRecordedBlock;\r
 \r
   //\r
   // Find UDF volume identifiers\r
@@ -487,7 +629,12 @@ FindUdfFileSystem (
   //\r
   // Find Anchor Volume Descriptor Pointer\r
   //\r
-  Status = FindAnchorVolumeDescriptorPointer (BlockIo, DiskIo, &AnchorPoint);\r
+  Status = FindAnchorVolumeDescriptorPointer (\r
+    BlockIo,\r
+    DiskIo,\r
+    &AnchorPoint,\r
+    &LastRecordedBlock\r
+    );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
@@ -499,6 +646,7 @@ FindUdfFileSystem (
     BlockIo,\r
     DiskIo,\r
     &AnchorPoint,\r
+    LastRecordedBlock,\r
     (UINT64 *)StartingLBA,\r
     (UINT64 *)EndingLBA\r
     );\r