From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.126; helo=mga18.intel.com; envelope-from=hao.a.wu@intel.com; receiver=edk2-devel@lists.01.org Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 57F98210D9791 for ; Wed, 30 Jan 2019 00:37:46 -0800 (PST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Jan 2019 00:37:45 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,540,1539673200"; d="scan'208";a="271082128" Received: from fmsmsx105.amr.corp.intel.com ([10.18.124.203]) by orsmga004.jf.intel.com with ESMTP; 30 Jan 2019 00:37:45 -0800 Received: from fmsmsx117.amr.corp.intel.com (10.18.116.17) by FMSMSX105.amr.corp.intel.com (10.18.124.203) with Microsoft SMTP Server (TLS) id 14.3.408.0; Wed, 30 Jan 2019 00:37:45 -0800 Received: from shsmsx152.ccr.corp.intel.com (10.239.6.52) by fmsmsx117.amr.corp.intel.com (10.18.116.17) with Microsoft SMTP Server (TLS) id 14.3.408.0; Wed, 30 Jan 2019 00:37:44 -0800 Received: from shsmsx104.ccr.corp.intel.com ([169.254.5.102]) by SHSMSX152.ccr.corp.intel.com ([169.254.6.109]) with mapi id 14.03.0415.000; Wed, 30 Jan 2019 16:37:42 +0800 From: "Wu, Hao A" To: "Chen, Chen A" , "edk2-devel@lists.01.org" CC: "Ni, Ray" , "Zhang, Chao B" Thread-Topic: [edk2] [PATCH v3 3/3] FatPkg: Add GPT check in FatPei to support Capsule-on-Disk feature. Thread-Index: AQHUt5OBfVulrnazdUikNNQ4Mj+/raXHft+Q Date: Wed, 30 Jan 2019 08:37:42 +0000 Message-ID: References: <20190129052737.16272-1-chen.a.chen@intel.com> <20190129052737.16272-4-chen.a.chen@intel.com> In-Reply-To: <20190129052737.16272-4-chen.a.chen@intel.com> Accept-Language: zh-CN, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [PATCH v3 3/3] FatPkg: Add GPT check in FatPei to support Capsule-on-Disk feature. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 30 Jan 2019 08:37:46 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable > -----Original Message----- > From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Ch= en > A Chen > Sent: Tuesday, January 29, 2019 1:28 PM > To: edk2-devel@lists.01.org > Cc: Ni, Ray; Zhang, Chao B > Subject: [edk2] [PATCH v3 3/3] FatPkg: Add GPT check in FatPei to support > Capsule-on-Disk feature. >=20 > BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1470 > This feature is used for finding GPT partition. > Follow the following step to check. > 1) Check Protective MBR. > 2) Check GPT primary/backup header. > 3) Check GPT primary/backup entry array. >=20 > Cc: Ruiyu Ni > Cc: Zhang Chao B > Contributed-under: TianoCore Contribution Agreement 1.1 > Signed-off-by: Chen A Chen > Reviewed-by: Hao Wu > --- > FatPkg/FatPei/FatLitePeim.h | 3 +- > FatPkg/FatPei/FatPei.inf | 3 + > FatPkg/FatPei/Gpt.c | 548 > ++++++++++++++++++++++++++++++++++++++++++++ > FatPkg/FatPei/Part.c | 36 ++- > 4 files changed, 585 insertions(+), 5 deletions(-) > create mode 100644 FatPkg/FatPei/Gpt.c >=20 > diff --git a/FatPkg/FatPei/FatLitePeim.h b/FatPkg/FatPei/FatLitePeim.h > index fbf887da5f..82ab045f2a 100644 > --- a/FatPkg/FatPei/FatLitePeim.h > +++ b/FatPkg/FatPei/FatLitePeim.h > @@ -1,7 +1,7 @@ > /** @file > Data structures for FAT recovery PEIM >=20 > -Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
> +Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
>=20 > This program and the accompanying materials are licensed and made > available > under the terms and conditions of the BSD License which accompanies this > @@ -27,6 +27,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY > KIND, EITHER EXPRESS OR IMPLIED. > #include > #include > #include > +#include > #include > #include > #include > diff --git a/FatPkg/FatPei/FatPei.inf b/FatPkg/FatPei/FatPei.inf > index 57312a9047..050bc4e157 100644 > --- a/FatPkg/FatPei/FatPei.inf > +++ b/FatPkg/FatPei/FatPei.inf > @@ -31,6 +31,7 @@ >=20 > [Sources] > Mbr.c > + Gpt.c > Eltorito.c > Part.c > FatLiteApi.c > @@ -49,6 +50,7 @@ > [LibraryClasses] > PcdLib > BaseMemoryLib > + MemoryAllocationLib > PeimEntryPoint > BaseLib > DebugLib > @@ -61,6 +63,7 @@ > gRecoveryOnFatIdeDiskGuid ## SOMETIMES_CONSUMES ## > UNDEFINED > gRecoveryOnFatFloppyDiskGuid ## SOMETIMES_CONSUMES ## > UNDEFINED > gRecoveryOnFatNvmeDiskGuid ## SOMETIMES_CONSUMES ## > UNDEFINED > + gEfiPartTypeUnusedGuid ## SOMETIMES_CONSUMES ## > UNDEFINED >=20 >=20 > [Ppis] > diff --git a/FatPkg/FatPei/Gpt.c b/FatPkg/FatPei/Gpt.c > new file mode 100644 > index 0000000000..c3afb668d7 > --- /dev/null > +++ b/FatPkg/FatPei/Gpt.c > @@ -0,0 +1,548 @@ > +/** @file > + Routines supporting partition discovery and > + logical device reading > + > +Copyright (c) 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 > +#include > +#include "FatLitePeim.h" > + > +// > +// Assumption: 'a' and 'blocksize' are all UINT32 or UINT64. > +// If 'a' and 'blocksize' are not the same type, should use DivU64xU32 t= o > calculate. > +// > +#define EFI_SIZE_TO_BLOCKS(a, blocksize) (((a) / (blocksize)) + (((a) % > (blocksize)) ? 1 : 0)) > + > +// > +// GPT Partition Entry Status > +// > +typedef struct { > + BOOLEAN OutOfRange; > + BOOLEAN Overlap; > + BOOLEAN OsSpecific; > +} EFI_PARTITION_ENTRY_STATUS; > + > +/** > + Check if the CRC field in the Partition table header is valid. > + > + @param[in] PartHeader Partition table header structure > + > + @retval TRUE the CRC is valid > + @retval FALSE the CRC is invalid > + > +**/ > +BOOLEAN > +PartitionCheckGptHeaderCRC ( > + IN EFI_PARTITION_TABLE_HEADER *PartHeader > + ) > +{ > + UINT32 GptHdrCrc; > + UINT32 Crc; > + > + GptHdrCrc =3D PartHeader->Header.CRC32; > + > + // > + // Set CRC field to zero when doing calcuation > + // > + PartHeader->Header.CRC32 =3D 0; > + > + Crc =3D CalculateCrc32 (PartHeader, PartHeader->Header.HeaderSize); > + > + // > + // Restore Header CRC > + // > + PartHeader->Header.CRC32 =3D GptHdrCrc; > + > + return (GptHdrCrc =3D=3D Crc); > +} > + > + > +/** > + Check if the CRC field in the Partition table header is valid > + for Partition entry array. > + > + @param[in] PartHeader Partition table header structure > + @param[in] PartEntry The partition entry array > + > + @retval TRUE the CRC is valid > + @retval FALSE the CRC is invalid > + > +**/ > +BOOLEAN > +PartitionCheckGptEntryArrayCRC ( > + IN EFI_PARTITION_TABLE_HEADER *PartHeader, > + IN EFI_PARTITION_ENTRY *PartEntry > + ) > +{ > + UINT32 Crc; > + UINTN Size; > + > + Size =3D (UINTN)MultU64x32(PartHeader->NumberOfPartitionEntries, > PartHeader->SizeOfPartitionEntry); > + Crc =3D CalculateCrc32 (PartEntry, Size); > + > + return (BOOLEAN) (PartHeader->PartitionEntryArrayCRC32 =3D=3D Crc); > +} > + > +/** > + The function is used for valid GPT table. Both for Primary and Backup = GPT > header. > + > + @param[in] PrivateData The global memory map > + @param[in] ParentBlockDevNo The parent block device > + @param[in] IsPrimaryHeader Indicate to which header will be checked= . > + @param[in] PartHdr Stores the partition table that is read > + > + @retval TRUE The partition table is valid > + @retval FALSE The partition table is not valid > + > +**/ > +BOOLEAN > +PartitionCheckGptHeader ( > + IN PEI_FAT_PRIVATE_DATA *PrivateData, > + IN UINTN ParentBlockDevNo, > + IN BOOLEAN IsPrimaryHeader, > + IN EFI_PARTITION_TABLE_HEADER *PartHdr > + ) > +{ > + PEI_FAT_BLOCK_DEVICE *ParentBlockDev; > + EFI_PEI_LBA Lba; > + EFI_PEI_LBA AlternateLba; > + EFI_PEI_LBA EntryArrayLastLba; > + > + UINT64 PartitionEntryArraySize; > + UINT64 PartitionEntryBlockNumb; > + UINT32 EntryArraySizeRemainder; > + > + ParentBlockDev =3D &(PrivateData->BlockDevice[ParentBlockDevNo]); > + > + if (IsPrimaryHeader) { > + Lba =3D PRIMARY_PART_HEADER_LBA; > + AlternateLba =3D ParentBlockDev->LastBlock; > + } else { > + Lba =3D ParentBlockDev->LastBlock; > + AlternateLba =3D PRIMARY_PART_HEADER_LBA; > + } > + > + if ( (PartHdr->Header.Signature !=3D EFI_PTAB_HEADER_ID) || > + (PartHdr->Header.Revision !=3D 0x00010000) || > + (PartHdr->Header.HeaderSize < 92) || > + (PartHdr->Header.HeaderSize > ParentBlockDev->BlockSize) || > + (!PartitionCheckGptHeaderCRC (PartHdr)) || > + (PartHdr->Header.Reserved !=3D 0) > + ) { > + DEBUG ((DEBUG_ERROR, "Invalid efi partition table header\n")); > + return FALSE; > + } > + > + // > + // | Block0 | Block1 |Block2 ~ FirstUsableLBA - > 1|FirstUsableLBA, ... ,LastUsableLBA|LastUsableLBA+1 ~ LastBlock-1| Last= Block > | > + // |Protective MBR|Primary Header|Entry Array(At Least 16384)| > Partition | Entry Array(At Least 16384) |BackUp Header| > + // > + // 1. Protective MBR is fixed at Block 0. > + // 2. Primary Header is fixed at Block 1. > + // 3. Backup Header is fixed at LastBlock. > + // 4. Must be remain 128*128 bytes for primary entry array. > + // 5. Must be remain 128*128 bytes for backup entry array. > + // 6. SizeOfPartitionEntry must be equals to 128 * 2^n. > + // > + if ( (PartHdr->MyLBA !=3D Lba) || > + (PartHdr->AlternateLBA !=3D AlternateLba) || > + (PartHdr->FirstUsableLBA < 2 + EFI_SIZE_TO_BLOCKS > (EFI_GPT_PART_ENTRY_MIN_SIZE, ParentBlockDev->BlockSize)) || > + (PartHdr->LastUsableLBA > ParentBlockDev->LastBlock - 1 - > EFI_SIZE_TO_BLOCKS (EFI_GPT_PART_ENTRY_MIN_SIZE, ParentBlockDev- > >BlockSize)) || > + (PartHdr->FirstUsableLBA > PartHdr->LastUsableLBA) || > + (PartHdr->PartitionEntryLBA < 2) || > + (PartHdr->PartitionEntryLBA > ParentBlockDev->LastBlock - 1) || > + (PartHdr->PartitionEntryLBA >=3D PartHdr->FirstUsableLBA && PartH= dr- > >PartitionEntryLBA <=3D PartHdr->LastUsableLBA) || > + (PartHdr->SizeOfPartitionEntry%128 !=3D 0) || > + (PartHdr->SizeOfPartitionEntry !=3D sizeof (EFI_PARTITION_ENTRY)) > + ) { > + DEBUG ((DEBUG_ERROR, "Invalid efi partition table header\n")); > + return FALSE; > + } > + > + // > + // Ensure the NumberOfPartitionEntries * SizeOfPartitionEntry doesn't > overflow. > + // > + if (PartHdr->NumberOfPartitionEntries > DivU64x32 (MAX_UINTN, PartHdr- > >SizeOfPartitionEntry)) { > + DEBUG ((DEBUG_ERROR, "Memory overflow in GPT Entry Array\n")); > + return FALSE; > + } > + > + PartitionEntryArraySize =3D MultU64x32 (PartHdr->NumberOfPartitionEntr= ies, > PartHdr->SizeOfPartitionEntry); > + EntryArraySizeRemainder =3D 0; > + PartitionEntryBlockNumb =3D DivU64x32Remainder (PartitionEntryArraySiz= e, > ParentBlockDev->BlockSize, &EntryArraySizeRemainder); > + if (EntryArraySizeRemainder !=3D 0) { > + PartitionEntryBlockNumb++; > + } > + > + if (IsPrimaryHeader) { > + EntryArrayLastLba =3D PartHdr->FirstUsableLBA; > + } else { > + EntryArrayLastLba =3D ParentBlockDev->LastBlock; > + } > + > + // > + // Make sure partition entry array not overlaps with partition area or= the > LastBlock. > + // > + if (PartHdr->PartitionEntryLBA + PartitionEntryBlockNumb > > EntryArrayLastLba) { > + DEBUG ((DEBUG_ERROR, "GPT Partition Entry Array Error!\n")); > + DEBUG ((DEBUG_ERROR, "PartitionEntryArraySize =3D %lu.\n", > PartitionEntryArraySize)); > + DEBUG ((DEBUG_ERROR, "PartitionEntryLBA =3D %lu.\n", PartHdr- > >PartitionEntryLBA)); > + DEBUG ((DEBUG_ERROR, "PartitionEntryBlockNumb =3D %lu.\n", > PartitionEntryBlockNumb)); > + DEBUG ((DEBUG_ERROR, "EntryArrayLastLba =3D %lu.\n", EntryArrayLastL= ba)); > + return FALSE; > + } > + > + return TRUE; > +} > + > +/** > + This function is used to verify each partition in block device. > + > + @param[in] PrivateData The global memory map > + @param[in] ParentBlockDevNo The parent block device > + @param[in] PartHdr Stores the partition table that is read > + > + @retval TRUE The partition is valid > + @retval FALSE The partition is not valid > + > +**/ > +BOOLEAN > +PartitionCheckGptEntryArray ( > + IN PEI_FAT_PRIVATE_DATA *PrivateData, > + IN UINTN ParentBlockDevNo, > + IN EFI_PARTITION_TABLE_HEADER *PartHdr > + ) > +{ > + EFI_STATUS Status; > + PEI_FAT_BLOCK_DEVICE *ParentBlockDev; > + PEI_FAT_BLOCK_DEVICE *BlockDevPtr; > + > + UINT64 PartitionEntryArraySize; > + UINT64 PartitionEntryBlockNumb; > + UINT32 EntryArraySizeRemainder; > + > + EFI_PARTITION_ENTRY *PartitionEntryBuffer; > + EFI_PARTITION_ENTRY_STATUS *PartitionEntryStatus; > + > + BOOLEAN Found; > + EFI_LBA StartingLBA; > + EFI_LBA EndingLBA; > + UINTN Index; > + UINTN Index1; > + UINTN Index2; > + EFI_PARTITION_ENTRY *Entry; > + > + ParentBlockDev =3D &(PrivateData->BlockDevice[ParentBlockDevNo]); > + Found =3D FALSE; > + > + PartitionEntryArraySize =3D MultU64x32 (PartHdr->NumberOfPartitionEntr= ies, > PartHdr->SizeOfPartitionEntry); > + EntryArraySizeRemainder =3D 0; > + PartitionEntryBlockNumb =3D DivU64x32Remainder (PartitionEntryArraySiz= e, > ParentBlockDev->BlockSize, &EntryArraySizeRemainder); > + if (EntryArraySizeRemainder !=3D 0) { > + PartitionEntryBlockNumb++; > + } > + PartitionEntryArraySize =3D MultU64x32 (PartitionEntryBlockNumb, > ParentBlockDev->BlockSize); > + > + PartitionEntryBuffer =3D (EFI_PARTITION_ENTRY *) AllocatePages > (EFI_SIZE_TO_PAGES ((UINTN)PartitionEntryArraySize)); > + if (PartitionEntryBuffer =3D=3D NULL) { > + DEBUG ((DEBUG_ERROR, "Allocate memory error!\n")); > + goto EXIT; > + } > + > + PartitionEntryStatus =3D (EFI_PARTITION_ENTRY_STATUS *) AllocatePages > (EFI_SIZE_TO_PAGES (PartHdr->NumberOfPartitionEntries * sizeof > (EFI_PARTITION_ENTRY_STATUS))); > + if (PartitionEntryStatus =3D=3D NULL) { > + DEBUG ((DEBUG_ERROR, "Allocate memory error!\n")); > + goto EXIT; > + } > + ZeroMem (PartitionEntryStatus, PartHdr->NumberOfPartitionEntries * siz= eof > (EFI_PARTITION_ENTRY_STATUS)); > + > + Status =3D FatReadBlock ( > + PrivateData, > + ParentBlockDevNo, > + PartHdr->PartitionEntryLBA, > + (UINTN)PartitionEntryArraySize, > + PartitionEntryBuffer > + ); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "Read partition entry array error!\n")); > + goto EXIT; > + } > + > + if (!PartitionCheckGptEntryArrayCRC (PartHdr, PartitionEntryBuffer)) { > + DEBUG ((DEBUG_ERROR, "Partition entries CRC check fail\n")); > + goto EXIT; > + } > + > + for (Index1 =3D 0; Index1 < PartHdr->NumberOfPartitionEntries; Index1+= +) { > + Entry =3D (EFI_PARTITION_ENTRY *) ((UINT8 *) PartitionEntryBuffer + = Index1 > * PartHdr->SizeOfPartitionEntry); > + if (CompareGuid (&Entry->PartitionTypeGUID, &gEfiPartTypeUnusedGuid)= ) { > + continue; > + } > + > + StartingLBA =3D Entry->StartingLBA; > + EndingLBA =3D Entry->EndingLBA; > + if (StartingLBA > EndingLBA || > + StartingLBA < PartHdr->FirstUsableLBA || > + StartingLBA > PartHdr->LastUsableLBA || > + EndingLBA < PartHdr->FirstUsableLBA || > + EndingLBA > PartHdr->LastUsableLBA > + ) { > + PartitionEntryStatus[Index1].OutOfRange =3D TRUE; > + continue; > + } > + > + if ((Entry->Attributes & BIT1) !=3D 0) { > + // > + // If Bit 1 is set, this indicate that this is an OS specific GUID= partition. > + // > + PartitionEntryStatus[Index1].OsSpecific =3D TRUE; > + } > + > + for (Index2 =3D Index1 + 1; Index2 < PartHdr->NumberOfPartitionEntri= es; > Index2++) { > + Entry =3D (EFI_PARTITION_ENTRY *) ((UINT8 *) PartitionEntryBuffer = + Index2 > * PartHdr->SizeOfPartitionEntry); > + if (CompareGuid (&Entry->PartitionTypeGUID, &gEfiPartTypeUnusedGui= d)) > { > + continue; > + } > + > + if (Entry->EndingLBA >=3D StartingLBA && Entry->StartingLBA <=3D E= ndingLBA) > { > + // > + // This region overlaps with the Index1'th region > + // > + PartitionEntryStatus[Index1].Overlap =3D TRUE; > + PartitionEntryStatus[Index2].Overlap =3D TRUE; > + continue; > + } > + } > + } > + > + for (Index =3D 0; Index < PartHdr->NumberOfPartitionEntries; Index++) = { > + if (CompareGuid (&PartitionEntryBuffer[Index].PartitionTypeGUID, > &gEfiPartTypeUnusedGuid)|| > + PartitionEntryStatus[Index].OutOfRange || > + PartitionEntryStatus[Index].Overlap || > + PartitionEntryStatus[Index].OsSpecific) { > + // > + // Don't use null EFI Partition Entries, Invalid Partition Entries= or OS > specific > + // partition Entries > + // > + continue; > + } > + > + if (PrivateData->BlockDeviceCount >=3D PEI_FAT_MAX_BLOCK_DEVICE) { > + break; > + } > + > + Found =3D TRUE; > + BlockDevPtr =3D &(PrivateData->BlockDevice[Private= Data- > >BlockDeviceCount]); > + > + BlockDevPtr->BlockSize =3D ParentBlockDev->BlockSize; > + BlockDevPtr->LastBlock =3D PartitionEntryBuffer[Index].Ending= LBA; > + BlockDevPtr->IoAlign =3D ParentBlockDev->IoAlign; > + BlockDevPtr->Logical =3D TRUE; > + BlockDevPtr->PartitionChecked =3D FALSE; > + BlockDevPtr->StartingPos =3D MultU64x32 ( > + PartitionEntryBuffer[Index].Starti= ngLBA, > + ParentBlockDev->BlockSize > + ); > + BlockDevPtr->ParentDevNo =3D ParentBlockDevNo; > + > + PrivateData->BlockDeviceCount++; > + > + DEBUG ((DEBUG_INFO, "Find GPT Partition [0x%lx", > PartitionEntryBuffer[Index].StartingLBA, BlockDevPtr->LastBlock)); > + DEBUG ((DEBUG_INFO, ", 0x%lx]\n", BlockDevPtr->LastBlock)); > + DEBUG ((DEBUG_INFO, " BlockSize %x\n", BlockDevPtr->BlockSi= ze)); > + } > + > +EXIT: > + if (PartitionEntryBuffer !=3D NULL) { > + FreePages (PartitionEntryBuffer, EFI_SIZE_TO_PAGES > ((UINTN)PartitionEntryArraySize)); > + } > + > + if (PartitionEntryStatus !=3D NULL) { > + FreePages (PartitionEntryStatus, EFI_SIZE_TO_PAGES (PartHdr- > >NumberOfPartitionEntries * sizeof (EFI_PARTITION_ENTRY_STATUS))); > + } > + > + return Found; > +} > + > +/** > + The function is used to check GPT structure, include GPT header and GP= T > entry array. > + > + 1. Check GPT header. > + 2. Check partition entry array. > + 3. Check each partitions. > + > + @param[in] PrivateData The global memory map > + @param[in] ParentBlockDevNo The parent block device > + @param[in] IsPrimary Indicate primary or backup to be check > + > + @retval TRUE Primary or backup GPT structure is valid. > + @retval FALSE Both primary and backup are invalid. > + > +**/ > +BOOLEAN > +PartitionCheckGptStructure ( > + IN PEI_FAT_PRIVATE_DATA *PrivateData, > + IN UINTN ParentBlockDevNo, > + IN BOOLEAN IsPrimary > + ) > +{ > + EFI_STATUS Status; > + PEI_FAT_BLOCK_DEVICE *ParentBlockDev; > + EFI_PARTITION_TABLE_HEADER *PartHdr; > + EFI_PEI_LBA GptHeaderLBA; > + > + ParentBlockDev =3D &(PrivateData->BlockDevice[ParentBlockDevNo]); > + PartHdr =3D (EFI_PARTITION_TABLE_HEADER *) PrivateData->BlockD= ata; > + > + if (IsPrimary) { > + GptHeaderLBA =3D PRIMARY_PART_HEADER_LBA; > + } else { > + GptHeaderLBA =3D ParentBlockDev->LastBlock; > + } > + > + Status =3D FatReadBlock ( > + PrivateData, > + ParentBlockDevNo, > + GptHeaderLBA, > + ParentBlockDev->BlockSize, > + PartHdr > + ); > + if (EFI_ERROR (Status)) { > + return FALSE; > + } > + > + if (!PartitionCheckGptHeader (PrivateData, ParentBlockDevNo, IsPrimary= , > PartHdr)) { > + return FALSE; > + } > + > + if (!PartitionCheckGptEntryArray (PrivateData, ParentBlockDevNo, PartH= dr)) > { > + return FALSE; > + } > + > + return TRUE; > +} > + > +/** > + This function is used to check protective MBR structure before checkin= g GPT. > + > + @param[in] PrivateData The global memory map > + @param[in] ParentBlockDevNo The parent block device > + > + @retval TRUE Valid protective MBR > + @retval FALSE Invalid MBR > +**/ > +BOOLEAN > +PartitionCheckProtectiveMbr ( > + IN PEI_FAT_PRIVATE_DATA *PrivateData, > + IN UINTN ParentBlockDevNo > + ) > +{ > + EFI_STATUS Status; > + MASTER_BOOT_RECORD *ProtectiveMbr; > + MBR_PARTITION_RECORD *MbrPartition; > + PEI_FAT_BLOCK_DEVICE *ParentBlockDev; > + UINTN Index; > + > + ProtectiveMbr =3D (MASTER_BOOT_RECORD *) PrivateData->BlockData; > + ParentBlockDev =3D &(PrivateData->BlockDevice[ParentBlockDevNo]); > + > + // > + // Read Protective MBR > + // > + Status =3D FatReadBlock ( > + PrivateData, > + ParentBlockDevNo, > + 0, > + ParentBlockDev->BlockSize, > + ProtectiveMbr > + ); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "GPT Error When Read Protective Mbr From > Partition!\n")); > + return FALSE; > + } > + > + if (ProtectiveMbr->Signature !=3D MBR_SIGNATURE) { > + DEBUG ((DEBUG_ERROR, "Protective Mbr Signature is invalid!\n")); > + return FALSE; > + } > + > + // > + // The partition define in UEFI Spec Table 17. > + // Boot Code, Unique MBR Disk Signature, Unknown. > + // These parts will not be used by UEFI, so we skip to check them. > + // > + for (Index =3D 0; Index < MAX_MBR_PARTITIONS; Index++) { > + MbrPartition =3D (MBR_PARTITION_RECORD *)&ProtectiveMbr- > >Partition[Index]; > + if (MbrPartition->BootIndicator =3D=3D 0x00 && > + MbrPartition->StartSector =3D=3D 0x02 && > + MbrPartition->OSIndicator =3D=3D PMBR_GPT_PARTITION && > + UNPACK_UINT32 (MbrPartition->StartingLBA) =3D=3D 1 > + ) { > + return TRUE; > + } > + } > + > + DEBUG ((DEBUG_ERROR, "Protective Mbr, All Partition Entry Are > Empty!\n")); > + return FALSE; > +} > + > +/** > + This function is used for finding GPT partition on block device. > + As follow UEFI spec we should check protective MBR first and then > + try to check both primary/backup GPT structures. > + > + @param[in] PrivateData The global memory map > + @param[in] ParentBlockDevNo The parent block device > + > + @retval TRUE New partitions are detected and logical bloc= k devices > + are added to block device array > + @retval FALSE No new partitions are added > + > +**/ > +BOOLEAN > +FatFindGptPartitions ( > + IN PEI_FAT_PRIVATE_DATA *PrivateData, > + IN UINTN ParentBlockDevNo > + ) > +{ > + BOOLEAN Found; > + PEI_FAT_BLOCK_DEVICE *ParentBlockDev; > + > + if (ParentBlockDevNo > PEI_FAT_MAX_BLOCK_DEVICE - 1) { > + return FALSE; > + } > + > + ParentBlockDev =3D &(PrivateData->BlockDevice[ParentBlockDevNo]); > + if (ParentBlockDev->BlockSize > PEI_FAT_MAX_BLOCK_SIZE) { > + DEBUG ((DEBUG_ERROR, "Device BlockSize %x exceed > FAT_MAX_BLOCK_SIZE\n", ParentBlockDev->BlockSize)); > + return FALSE; > + } > + > + if (!PartitionCheckProtectiveMbr (PrivateData, ParentBlockDevNo)) { > + return FALSE; > + } > + > + Found =3D PartitionCheckGptStructure (PrivateData, ParentBlockDevNo, T= RUE); > + if (!Found) { > + DEBUG ((DEBUG_ERROR, "Primary GPT Header Error, Try to Check Backup > GPT Header!\n")); > + Found =3D PartitionCheckGptStructure (PrivateData, ParentBlockDevNo, > FALSE); > + } > + > + if (Found) { > + ParentBlockDev->PartitionChecked =3D TRUE; > + } > + > + return Found; > +} > diff --git a/FatPkg/FatPei/Part.c b/FatPkg/FatPei/Part.c > index 8a54e56f5a..9b49eccf4e 100644 > --- a/FatPkg/FatPei/Part.c > +++ b/FatPkg/FatPei/Part.c > @@ -52,6 +52,25 @@ FatFindMbrPartitions ( > IN UINTN ParentBlockDevNo > ); >=20 > +/** > + This function is used for finding GPT partition on block device. > + As follow UEFI spec we should check protective MBR first and then > + try to check both primary/backup GPT structures. > + > + @param[in] PrivateData The global memory map > + @param[in] ParentBlockDevNo The parent block device > + > + @retval TRUE New partitions are detected and logical bloc= k devices > + are added to block device array > + @retval FALSE No new partitions are added > + > +**/ > +BOOLEAN > +FatFindGptPartitions ( > + IN PEI_FAT_PRIVATE_DATA *PrivateData, > + IN UINTN ParentBlockDevNo > + ); > + > /** > This function finds partitions (logical devices) in physical block dev= ices. >=20 > @@ -71,12 +90,21 @@ FatFindPartitions ( >=20 > for (Index =3D 0; Index < PrivateData->BlockDeviceCount; Index++) { > if (!PrivateData->BlockDevice[Index].PartitionChecked) { > - Found =3D FatFindMbrPartitions (PrivateData, Index); > - if (!Found) { > - Found =3D FatFindEltoritoPartitions (PrivateData, Index); > + if (FatFindGptPartitions (PrivateData, Index)) { > + Found =3D TRUE; > + continue; > + } > + > + if (FatFindMbrPartitions (PrivateData, Index)) { > + Found =3D TRUE; > + continue; > + } > + > + if (FatFindEltoritoPartitions (PrivateData, Index)) { > + Found =3D TRUE; > + continue; > } Reviewed-by: Hao Wu Best Regards, Hao Wu > } > } > } while (Found && PrivateData->BlockDeviceCount <=3D > PEI_FAT_MAX_BLOCK_DEVICE); > } > - > -- > 2.16.2.windows.1 >=20 > _______________________________________________ > edk2-devel mailing list > edk2-devel@lists.01.org > https://lists.01.org/mailman/listinfo/edk2-devel