gpt.c
来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 769 行 · 第 1/2 页
C
769 行
/*++
Copyright (c) 2004 - 2006, 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.
Module Name:
Gpt.c
Abstract:
Decode a hard disk partitioned with the GPT scheme in the EFI 1.0
specification.
--*/
#include "Partition.h"
#include "Gpt.h"
#include "Mbr.h"
BOOLEAN
PartitionValidGptTable (
IN EFI_BLOCK_IO_PROTOCOL *BlockIo,
IN EFI_DISK_IO_PROTOCOL *DiskIo,
IN EFI_LBA Lba,
OUT EFI_PARTITION_TABLE_HEADER *PartHeader
);
BOOLEAN
PartitionCheckGptEntryArrayCRC (
IN EFI_BLOCK_IO_PROTOCOL *BlockIo,
IN EFI_DISK_IO_PROTOCOL *DiskIo,
IN EFI_PARTITION_TABLE_HEADER *PartHeader
);
BOOLEAN
PartitionRestoreGptTable (
IN EFI_BLOCK_IO_PROTOCOL *BlockIo,
IN EFI_DISK_IO_PROTOCOL *DiskIo,
IN EFI_PARTITION_TABLE_HEADER *PartHeader
);
VOID
PartitionCheckGptEntry (
IN EFI_PARTITION_TABLE_HEADER *PartHeader,
IN EFI_PARTITION_ENTRY *PartEntry,
OUT EFI_PARTITION_ENTRY_STATUS *PEntryStatus
);
BOOLEAN
PartitionCheckCrcAltSize (
IN UINTN MaxSize,
IN UINTN Size,
IN OUT EFI_TABLE_HEADER *Hdr
);
BOOLEAN
PartitionCheckCrc (
IN UINTN MaxSize,
IN OUT EFI_TABLE_HEADER *Hdr
);
VOID
PartitionSetCrcAltSize (
IN UINTN Size,
IN OUT EFI_TABLE_HEADER *Hdr
);
VOID
PartitionSetCrc (
IN OUT EFI_TABLE_HEADER *Hdr
);
BOOLEAN
PartitionInstallGptChildHandles (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Handle,
IN EFI_DISK_IO_PROTOCOL *DiskIo,
IN EFI_BLOCK_IO_PROTOCOL *BlockIo,
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
)
/*++
Routine Description:
Install child handles if the Handle supports GPT partition structure.
Arguments:
This - Calling context.
Handle - Parent Handle
DiskIo - Parent DiskIo interface
BlockIo - Parent BlockIo interface
DevicePath - Parent Device Path
Returns:
TRUE - Valid GPT disk
FALSE - Not a valid GPT disk
--*/
{
EFI_STATUS Status;
UINT32 BlockSize;
EFI_LBA LastBlock;
MASTER_BOOT_RECORD *ProtectiveMbr;
EFI_PARTITION_TABLE_HEADER *PrimaryHeader;
EFI_PARTITION_TABLE_HEADER *BackupHeader;
EFI_PARTITION_ENTRY *PartEntry;
EFI_PARTITION_ENTRY_STATUS *PEntryStatus;
UINTN Index;
BOOLEAN GptValid;
HARDDRIVE_DEVICE_PATH HdDev;
ProtectiveMbr = NULL;
PrimaryHeader = NULL;
BackupHeader = NULL;
PartEntry = NULL;
PEntryStatus = NULL;
BlockSize = BlockIo->Media->BlockSize;
LastBlock = BlockIo->Media->LastBlock;
DEBUG ((EFI_D_INFO, " BlockSize : %d \n", BlockSize));
DEBUG ((EFI_D_INFO, " LastBlock : %x \n", LastBlock));
GptValid = FALSE;
//
// Allocate a buffer for the Protective MBR
//
ProtectiveMbr = EfiLibAllocatePool (BlockSize);
if (ProtectiveMbr == NULL) {
return FALSE;
}
//
// Read the Protective MBR from LBA #0
//
Status = BlockIo->ReadBlocks (
BlockIo,
BlockIo->Media->MediaId,
0,
BlockIo->Media->BlockSize,
ProtectiveMbr
);
if (EFI_ERROR (Status)) {
goto Done;
}
//
// Verify that the Protective MBR is valid
//
if (ProtectiveMbr->Partition[0].BootIndicator != 0x00 ||
ProtectiveMbr->Partition[0].OSIndicator != PMBR_GPT_PARTITION ||
UNPACK_UINT32 (ProtectiveMbr->Partition[0].StartingLBA) != 1
) {
goto Done;
}
//
// Allocate the GPT structures
//
PrimaryHeader = EfiLibAllocateZeroPool (sizeof (EFI_PARTITION_TABLE_HEADER));
if (PrimaryHeader == NULL) {
goto Done;
}
BackupHeader = EfiLibAllocateZeroPool (sizeof (EFI_PARTITION_TABLE_HEADER));
if (BackupHeader == NULL) {
goto Done;
}
//
// Check primary and backup partition tables
//
if (!PartitionValidGptTable (BlockIo, DiskIo, PRIMARY_PART_HEADER_LBA, PrimaryHeader)) {
DEBUG ((EFI_D_INFO, " Not Valid primary partition table\n"));
if (!PartitionValidGptTable (BlockIo, DiskIo, LastBlock, BackupHeader)) {
DEBUG ((EFI_D_INFO, " Not Valid backup partition table\n"));
goto Done;
} else {
DEBUG ((EFI_D_INFO, " Valid backup partition table\n"));
DEBUG ((EFI_D_INFO, " Restore primary partition table by the backup\n"));
if (!PartitionRestoreGptTable (BlockIo, DiskIo, BackupHeader)) {
DEBUG ((EFI_D_INFO, " Restore primary partition table error\n"));
}
if (PartitionValidGptTable (BlockIo, DiskIo, BackupHeader->AlternateLBA, PrimaryHeader)) {
DEBUG ((EFI_D_INFO, " Restore backup partition table success\n"));
}
}
} else if (!PartitionValidGptTable (BlockIo, DiskIo, PrimaryHeader->AlternateLBA, BackupHeader)) {
DEBUG ((EFI_D_INFO, " Valid primary and !Valid backup partition table\n"));
DEBUG ((EFI_D_INFO, " Restore backup partition table by the primary\n"));
if (!PartitionRestoreGptTable (BlockIo, DiskIo, PrimaryHeader)) {
DEBUG ((EFI_D_INFO, " Restore backup partition table error\n"));
}
if (PartitionValidGptTable (BlockIo, DiskIo, PrimaryHeader->AlternateLBA, BackupHeader)) {
DEBUG ((EFI_D_INFO, " Restore backup partition table success\n"));
}
}
DEBUG ((EFI_D_INFO, " Valid primary and Valid backup partition table\n"));
//
// Read the EFI Partition Entries
//
PartEntry = EfiLibAllocatePool (PrimaryHeader->NumberOfPartitionEntries * sizeof (EFI_PARTITION_ENTRY));
if (PartEntry == NULL) {
DEBUG ((EFI_D_ERROR, "Allocate pool error\n"));
goto Done;
}
Status = DiskIo->ReadDisk (
DiskIo,
BlockIo->Media->MediaId,
MultU64x32(PrimaryHeader->PartitionEntryLBA, BlockSize),
PrimaryHeader->NumberOfPartitionEntries * (PrimaryHeader->SizeOfPartitionEntry),
PartEntry
);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_INFO, " Partition Entry ReadBlocks error\n"));
goto Done;
}
DEBUG ((EFI_D_INFO, " Partition entries read block success\n"));
DEBUG ((EFI_D_INFO, " Number of partition entries: %d\n", PrimaryHeader->NumberOfPartitionEntries));
PEntryStatus = EfiLibAllocateZeroPool (PrimaryHeader->NumberOfPartitionEntries * sizeof (EFI_PARTITION_ENTRY_STATUS));
if (PEntryStatus == NULL) {
DEBUG ((EFI_D_ERROR, "Allocate pool error\n"));
goto Done;
}
//
// Check the integrity of partition entries
//
PartitionCheckGptEntry (PrimaryHeader, PartEntry, PEntryStatus);
//
// If we got this far the GPT layout of the disk is valid and we should return true
//
GptValid = TRUE;
//
// Create child device handles
//
for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) {
if (EfiCompareGuid (&PartEntry[Index].PartitionTypeGUID, &gEfiPartTypeUnusedGuid) ||
PEntryStatus[Index].OutOfRange ||
PEntryStatus[Index].Overlap
) {
//
// Don't use null EFI Partition Entries or Invalid Partition Entries
//
continue;
}
EfiZeroMem (&HdDev, sizeof (HdDev));
HdDev.Header.Type = MEDIA_DEVICE_PATH;
HdDev.Header.SubType = MEDIA_HARDDRIVE_DP;
SetDevicePathNodeLength (&HdDev.Header, sizeof (HdDev));
HdDev.PartitionNumber = (UINT32) Index + 1;
HdDev.MBRType = MBR_TYPE_EFI_PARTITION_TABLE_HEADER;
HdDev.SignatureType = SIGNATURE_TYPE_GUID;
HdDev.PartitionStart = PartEntry[Index].StartingLBA;
HdDev.PartitionSize = PartEntry[Index].EndingLBA - PartEntry[Index].StartingLBA + 1;
EfiCopyMem (HdDev.Signature, &PartEntry[Index].UniquePartitionGUID, sizeof (EFI_GUID));
DEBUG ((EFI_D_INFO, " Index : %d\n", Index));
DEBUG ((EFI_D_INFO, " Start LBA : %x\n", HdDev.PartitionStart));
DEBUG ((EFI_D_INFO, " End LBA : %x\n", PartEntry[Index].EndingLBA));
DEBUG ((EFI_D_INFO, " Partition size: %x\n", HdDev.PartitionSize));
DEBUG ((EFI_D_INFO, " Start : %x", MultU64x32 (PartEntry[Index].StartingLBA, BlockSize)));
DEBUG ((EFI_D_INFO, " End : %x\n", MultU64x32 (PartEntry[Index].EndingLBA, BlockSize)));
Status = PartitionInstallChildHandle (
This,
Handle,
DiskIo,
BlockIo,
DevicePath,
(EFI_DEVICE_PATH_PROTOCOL *) &HdDev,
PartEntry[Index].StartingLBA,
PartEntry[Index].EndingLBA,
BlockSize,
EfiCompareGuid(&PartEntry[Index].PartitionTypeGUID, &gEfiPartTypeSystemPartGuid)
);
}
DEBUG ((EFI_D_INFO, "Prepare to Free Pool\n"));
Done:
if (ProtectiveMbr != NULL) {
gBS->FreePool (ProtectiveMbr);
}
if (PrimaryHeader != NULL) {
gBS->FreePool (PrimaryHeader);
}
if (BackupHeader != NULL) {
gBS->FreePool (BackupHeader);
}
if (PartEntry != NULL) {
gBS->FreePool (PartEntry);
}
if (PEntryStatus != NULL) {
gBS->FreePool (PEntryStatus);
}
return GptValid;
}
BOOLEAN
PartitionValidGptTable (
IN EFI_BLOCK_IO_PROTOCOL *BlockIo,
IN EFI_DISK_IO_PROTOCOL *DiskIo,
IN EFI_LBA Lba,
OUT EFI_PARTITION_TABLE_HEADER *PartHeader
)
/*++
Routine Description:
Check if the GPT partition table is valid
Arguments:
BlockIo - Parent BlockIo interface
DiskIo - Disk Io protocol.
Lba - The starting Lba of the Partition Table
PartHeader - Stores the partition table that is read
Returns:
TRUE - The partition table is valid
FALSE - The partition table is not valid
--*/
{
EFI_STATUS Status;
UINT32 BlockSize;
EFI_PARTITION_TABLE_HEADER *PartHdr;
BlockSize = BlockIo->Media->BlockSize;
PartHdr = EfiLibAllocateZeroPool (BlockSize);
if (PartHdr == NULL) {
DEBUG ((EFI_D_ERROR, "Allocate pool error\n"));
return FALSE;
}
//
// Read the EFI Partition Table Header
//
Status = BlockIo->ReadBlocks (
BlockIo,
BlockIo->Media->MediaId,
Lba,
BlockSize,
PartHdr
);
if (EFI_ERROR (Status)) {
gBS->FreePool (PartHdr);
return FALSE;
}
if (EfiCompareMem (&PartHdr->Header.Signature, EFI_PTAB_HEADER_ID, sizeof (UINT64)) != 0 ||
!PartitionCheckCrc (BlockSize, &PartHdr->Header) ||
PartHdr->MyLBA != Lba
) {
DEBUG ((EFI_D_INFO, " !Valid efi partition table header\n"));
gBS->FreePool (PartHdr);
return FALSE;
}
EfiCopyMem (PartHeader, PartHdr, sizeof (EFI_PARTITION_TABLE_HEADER));
if (!PartitionCheckGptEntryArrayCRC (BlockIo, DiskIo, PartHeader)) {
gBS->FreePool (PartHdr);
return FALSE;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?