Re: [edk2] [PATCH 1/2] FatPkg: Break down Part.c file.

2019-01-24 Thread Wu, Hao A
> -Original Message-
> From: edk2-devel [mailto:edk2-devel-boun...@lists.01.org] On Behalf Of
> Chen A Chen
> Sent: Thursday, January 17, 2019 10:03 AM
> To: edk2-devel@lists.01.org
> Cc: Ni, Ray; Zhang, Chao B
> Subject: [edk2] [PATCH 1/2] FatPkg: Break down Part.c file.
> 
> BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=1470
> Break down partition parsing logic to 2 parts, Eltorito and MBR.
> 
> Cc: Ruiyu Ni 
> Cc: Zhang Chao B 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Chen A Chen 
> ---
>  FatPkg/FatPei/Eltorito.c | 239 +
>  FatPkg/FatPei/FatPei.inf |   2 +
>  FatPkg/FatPei/Mbr.c  | 182 ++
>  FatPkg/FatPei/Part.c | 385 
> +--
>  4 files changed, 424 insertions(+), 384 deletions(-)
>  create mode 100644 FatPkg/FatPei/Eltorito.c
>  create mode 100644 FatPkg/FatPei/Mbr.c
> 
> diff --git a/FatPkg/FatPei/Eltorito.c b/FatPkg/FatPei/Eltorito.c
> new file mode 100644
> index 00..ffaef51860
> --- /dev/null
> +++ b/FatPkg/FatPei/Eltorito.c
> @@ -0,0 +1,239 @@
> +/** @file
> +  Routines supporting partition discovery and
> +  logical device reading
> +
> +Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
> +
> +This program and the accompanying materials are licensed and made
> available
> +under the terms and conditions of the BSD License which accompanies this
> +distribution. The full text of the license may be found at
> +http://opensource.org/licenses/bsd-license.php
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include 
> +#include "FatLitePeim.h"
> +
> +/**
> +  This function finds Eltorito partitions. Main algorithm
> +  is ported from DXE partition driver.
> +
> +  @param  PrivateData   The global memory map
> +  @param  ParentBlockDevNo  The parent block device
> +
> +  @retval TRUE  New partitions are detected and logical block 
> devices
> +are  added to block device array
> +  @retval FALSE No New partitions are added;
> +
> +**/
> +BOOLEAN
> +FatFindEltoritoPartitions (
> +  IN  PEI_FAT_PRIVATE_DATA *PrivateData,
> +  IN  UINTNParentBlockDevNo
> +  )
> +{
> +  EFI_STATUS  Status;
> +  BOOLEAN Found;
> +  PEI_FAT_BLOCK_DEVICE*BlockDev;
> +  PEI_FAT_BLOCK_DEVICE*ParentBlockDev;
> +  UINT32  VolDescriptorLba;
> +  UINT32  Lba;
> +  CDROM_VOLUME_DESCRIPTOR *VolDescriptor;
> +  ELTORITO_CATALOG*Catalog;
> +  UINTN   Check;
> +  UINTN   Index;
> +  UINTN   MaxIndex;
> +  UINT16  *CheckBuffer;
> +  UINT32  SubBlockSize;
> +  UINT32  SectorCount;
> +  UINT32  VolSpaceSize;
> +
> +  if (ParentBlockDevNo > PEI_FAT_MAX_BLOCK_DEVICE - 1) {
> +return FALSE;
> +  }
> +
> +  Found   = FALSE;
> +  ParentBlockDev  = &(PrivateData->BlockDevice[ParentBlockDevNo]);
> +  VolSpaceSize= 0;
> +
> +  //
> +  // CD_ROM has the fixed block size as 2048 bytes
> +  //
> +  if (ParentBlockDev->BlockSize != 2048) {
> +return FALSE;
> +  }
> +
> +  VolDescriptor = (CDROM_VOLUME_DESCRIPTOR *) PrivateData-
> >BlockData;
> +  Catalog   = (ELTORITO_CATALOG *) VolDescriptor;
> +
> +  //
> +  // the ISO-9660 volume descriptor starts at 32k on the media
> +  // and CD_ROM has the fixed block size as 2048 bytes, so...
> +  //
> +  VolDescriptorLba = 15;
> +  //
> +  // ((16*2048) / Media->BlockSize) - 1;
> +  //
> +  // Loop: handle one volume descriptor per time
> +  //
> +  while (TRUE) {
> +
> +VolDescriptorLba += 1;
> +if (VolDescriptorLba > ParentBlockDev->LastBlock) {
> +  //
> +  // We are pointing past the end of the device so exit
> +  //
> +  break;
> +}
> +
> +Status = FatReadBlock (
> +  PrivateData,
> +  ParentBlockDevNo,
> +  VolDescriptorLba,
> +  ParentBlockDev->BlockSize,
> +  VolDescriptor
> +  );
> +if (EFI_ERROR (Status)) {
> +  break;
> +}
> +//
> +// Check for valid volume descriptor signature
> +//
> +if (VolDescriptor->Unknown.Type == CDVOL_TYPE_END ||
> +CompareMem (VolDescriptor->Unknown.Id, CDVOL_ID, 

[edk2] [PATCH 1/2] FatPkg: Break down Part.c file.

2019-01-16 Thread Chen A Chen
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=1470
Break down partition parsing logic to 2 parts, Eltorito and MBR.

Cc: Ruiyu Ni 
Cc: Zhang Chao B 
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Chen A Chen 
---
 FatPkg/FatPei/Eltorito.c | 239 +
 FatPkg/FatPei/FatPei.inf |   2 +
 FatPkg/FatPei/Mbr.c  | 182 ++
 FatPkg/FatPei/Part.c | 385 +--
 4 files changed, 424 insertions(+), 384 deletions(-)
 create mode 100644 FatPkg/FatPei/Eltorito.c
 create mode 100644 FatPkg/FatPei/Mbr.c

diff --git a/FatPkg/FatPei/Eltorito.c b/FatPkg/FatPei/Eltorito.c
new file mode 100644
index 00..ffaef51860
--- /dev/null
+++ b/FatPkg/FatPei/Eltorito.c
@@ -0,0 +1,239 @@
+/** @file
+  Routines supporting partition discovery and
+  logical device reading
+
+Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
+
+This program and the accompanying materials are licensed and made available
+under the terms and conditions of the BSD License which accompanies this
+distribution. The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include 
+#include "FatLitePeim.h"
+
+/**
+  This function finds Eltorito partitions. Main algorithm
+  is ported from DXE partition driver.
+
+  @param  PrivateData   The global memory map
+  @param  ParentBlockDevNo  The parent block device
+
+  @retval TRUE  New partitions are detected and logical block 
devices
+are  added to block device array
+  @retval FALSE No New partitions are added;
+
+**/
+BOOLEAN
+FatFindEltoritoPartitions (
+  IN  PEI_FAT_PRIVATE_DATA *PrivateData,
+  IN  UINTNParentBlockDevNo
+  )
+{
+  EFI_STATUS  Status;
+  BOOLEAN Found;
+  PEI_FAT_BLOCK_DEVICE*BlockDev;
+  PEI_FAT_BLOCK_DEVICE*ParentBlockDev;
+  UINT32  VolDescriptorLba;
+  UINT32  Lba;
+  CDROM_VOLUME_DESCRIPTOR *VolDescriptor;
+  ELTORITO_CATALOG*Catalog;
+  UINTN   Check;
+  UINTN   Index;
+  UINTN   MaxIndex;
+  UINT16  *CheckBuffer;
+  UINT32  SubBlockSize;
+  UINT32  SectorCount;
+  UINT32  VolSpaceSize;
+
+  if (ParentBlockDevNo > PEI_FAT_MAX_BLOCK_DEVICE - 1) {
+return FALSE;
+  }
+
+  Found   = FALSE;
+  ParentBlockDev  = &(PrivateData->BlockDevice[ParentBlockDevNo]);
+  VolSpaceSize= 0;
+
+  //
+  // CD_ROM has the fixed block size as 2048 bytes
+  //
+  if (ParentBlockDev->BlockSize != 2048) {
+return FALSE;
+  }
+
+  VolDescriptor = (CDROM_VOLUME_DESCRIPTOR *) PrivateData->BlockData;
+  Catalog   = (ELTORITO_CATALOG *) VolDescriptor;
+
+  //
+  // the ISO-9660 volume descriptor starts at 32k on the media
+  // and CD_ROM has the fixed block size as 2048 bytes, so...
+  //
+  VolDescriptorLba = 15;
+  //
+  // ((16*2048) / Media->BlockSize) - 1;
+  //
+  // Loop: handle one volume descriptor per time
+  //
+  while (TRUE) {
+
+VolDescriptorLba += 1;
+if (VolDescriptorLba > ParentBlockDev->LastBlock) {
+  //
+  // We are pointing past the end of the device so exit
+  //
+  break;
+}
+
+Status = FatReadBlock (
+  PrivateData,
+  ParentBlockDevNo,
+  VolDescriptorLba,
+  ParentBlockDev->BlockSize,
+  VolDescriptor
+  );
+if (EFI_ERROR (Status)) {
+  break;
+}
+//
+// Check for valid volume descriptor signature
+//
+if (VolDescriptor->Unknown.Type == CDVOL_TYPE_END ||
+CompareMem (VolDescriptor->Unknown.Id, CDVOL_ID, sizeof 
(VolDescriptor->Unknown.Id)) != 0
+) {
+  //
+  // end of Volume descriptor list
+  //
+  break;
+}
+//
+// Read the Volume Space Size from Primary Volume Descriptor 81-88 byte
+//
+if (VolDescriptor->Unknown.Type == CDVOL_TYPE_CODED) {
+  VolSpaceSize = VolDescriptor->PrimaryVolume.VolSpaceSize[1];
+}
+//
+// Is it an El Torito volume descriptor?
+//
+if (CompareMem (
+  VolDescriptor->BootRecordVolume.SystemId,
+  CDVOL_ELTORITO_ID,
+  sizeof (CDVOL_ELTORITO_ID) - 1
+  ) != 0) {
+  continue;
+}
+//
+// Read in the boot El Torito boot catalog
+//
+Lba = UNPACK_INT32 (VolDescriptor->BootRecordVolume.EltCatalog);
+if (Lba > ParentBlockDev->LastBlock) {
+  continue;
+}
+
+Status = FatReadBlock (
+  PrivateData,
+  ParentBlockDevNo,
+  Lba,
+  ParentBlockDev->BlockSize,
+  Catalog
+  );
+if (EFI_ERROR (Status)) {
+