gpt.c
来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 769 行 · 第 1/2 页
C
769 行
}
DEBUG ((EFI_D_INFO, " Valid efi partition table header\n"));
gBS->FreePool (PartHdr);
return TRUE;
}
BOOLEAN
PartitionCheckGptEntryArrayCRC (
IN EFI_BLOCK_IO_PROTOCOL *BlockIo,
IN EFI_DISK_IO_PROTOCOL *DiskIo,
IN EFI_PARTITION_TABLE_HEADER *PartHeader
)
/*++
Routine Description:
Check if the CRC field in the Partition table header is valid
for Partition entry array
Arguments:
BlockIo - parent BlockIo interface
DiskIo - Disk Io Protocol.
PartHeader - Partition table header structure
Returns:
TRUE - the CRC is valid
FALSE - the CRC is invalid
--*/
{
EFI_STATUS Status;
UINT8 *Ptr;
UINT32 Crc;
UINTN Size;
//
// Read the EFI Partition Entries
//
Ptr = EfiLibAllocatePool (PartHeader->NumberOfPartitionEntries * PartHeader->SizeOfPartitionEntry);
if (Ptr == NULL) {
DEBUG ((EFI_D_ERROR, " Allocate pool error\n"));
return FALSE;
}
Status = DiskIo->ReadDisk (
DiskIo,
BlockIo->Media->MediaId,
MultU64x32(PartHeader->PartitionEntryLBA, BlockIo->Media->BlockSize),
PartHeader->NumberOfPartitionEntries * PartHeader->SizeOfPartitionEntry,
Ptr
);
if (EFI_ERROR (Status)) {
gBS->FreePool (Ptr);
return FALSE;
}
Size = PartHeader->NumberOfPartitionEntries * PartHeader->SizeOfPartitionEntry;
Status = gBS->CalculateCrc32 (Ptr, Size, &Crc);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "CheckPEntryArrayCRC: Crc calculation failed\n"));
gBS->FreePool (Ptr);
return FALSE;
}
gBS->FreePool (Ptr);
return (BOOLEAN) (PartHeader->PartitionEntryArrayCRC32 == Crc);
}
BOOLEAN
PartitionRestoreGptTable (
IN EFI_BLOCK_IO_PROTOCOL *BlockIo,
IN EFI_DISK_IO_PROTOCOL *DiskIo,
IN EFI_PARTITION_TABLE_HEADER *PartHeader
)
/*++
Routine Description:
Restore Partition Table to its alternate place
(Primary -> Backup or Backup -> Primary)
Arguments:
BlockIo - parent BlockIo interface
DiskIo - Disk Io Protocol.
PartHeader - the source Partition table header structure
Returns:
TRUE - Restoring succeeds
FALSE - Restoring failed
--*/
{
EFI_STATUS Status;
UINTN BlockSize;
EFI_PARTITION_TABLE_HEADER *PartHdr;
EFI_LBA PEntryLBA;
UINT8 *Ptr;
PartHdr = NULL;
Ptr = NULL;
BlockSize = BlockIo->Media->BlockSize;
PartHdr = EfiLibAllocateZeroPool (BlockSize);
if (PartHdr == NULL) {
DEBUG ((EFI_D_ERROR, "Allocate pool error\n"));
return FALSE;
}
PEntryLBA = (PartHeader->MyLBA == PRIMARY_PART_HEADER_LBA) ? \
(PartHeader->LastUsableLBA + 1) : \
(PRIMARY_PART_HEADER_LBA + 1);
EfiCopyMem (PartHdr, PartHeader, sizeof (EFI_PARTITION_TABLE_HEADER));
PartHdr->MyLBA = PartHeader->AlternateLBA;
PartHdr->AlternateLBA = PartHeader->MyLBA;
PartHdr->PartitionEntryLBA = PEntryLBA;
PartitionSetCrc ((EFI_TABLE_HEADER *) PartHdr);
Status = BlockIo->WriteBlocks (BlockIo, BlockIo->Media->MediaId, PartHdr->MyLBA, BlockSize, PartHdr);
if (EFI_ERROR (Status)) {
goto Done;
}
Ptr = EfiLibAllocatePool (PartHeader->NumberOfPartitionEntries * PartHeader->SizeOfPartitionEntry);
if (Ptr == NULL) {
DEBUG ((EFI_D_ERROR, " Allocate pool effor\n"));
Status = EFI_OUT_OF_RESOURCES;
goto Done;
}
Status = DiskIo->ReadDisk (
DiskIo,
BlockIo->Media->MediaId,
MultU64x32(PartHeader->PartitionEntryLBA, BlockIo->Media->BlockSize),
PartHeader->NumberOfPartitionEntries * PartHeader->SizeOfPartitionEntry,
Ptr
);
if (EFI_ERROR (Status)) {
goto Done;
}
Status = DiskIo->WriteDisk (
DiskIo,
BlockIo->Media->MediaId,
MultU64x32(PEntryLBA, BlockIo->Media->BlockSize),
PartHeader->NumberOfPartitionEntries * PartHeader->SizeOfPartitionEntry,
Ptr
);
Done:
gBS->FreePool (PartHdr);
gBS->FreePool (Ptr);
if (EFI_ERROR (Status)) {
return FALSE;
}
return TRUE;
}
VOID
PartitionCheckGptEntry (
IN EFI_PARTITION_TABLE_HEADER *PartHeader,
IN EFI_PARTITION_ENTRY *PartEntry,
OUT EFI_PARTITION_ENTRY_STATUS *PEntryStatus
)
/*++
Routine Description:
Check each partition entry for its range
Arguments:
PartHeader - the partition table header
PartEntry - the partition entry array
PEntryStatus - the partition entry status array recording the status of
each partition
Returns:
VOID
--*/
{
EFI_LBA StartingLBA;
EFI_LBA EndingLBA;
UINTN Index1;
UINTN Index2;
DEBUG ((EFI_D_INFO, " start check partition entries\n"));
for (Index1 = 0; Index1 < PartHeader->NumberOfPartitionEntries; Index1++) {
if (EfiCompareGuid (&PartEntry[Index1].PartitionTypeGUID, &gEfiPartTypeUnusedGuid)) {
continue;
}
StartingLBA = PartEntry[Index1].StartingLBA;
EndingLBA = PartEntry[Index1].EndingLBA;
if (StartingLBA > EndingLBA ||
StartingLBA < PartHeader->FirstUsableLBA ||
StartingLBA > PartHeader->LastUsableLBA ||
EndingLBA < PartHeader->FirstUsableLBA ||
EndingLBA > PartHeader->LastUsableLBA
) {
PEntryStatus[Index1].OutOfRange = TRUE;
continue;
}
for (Index2 = Index1 + 1; Index2 < PartHeader->NumberOfPartitionEntries; Index2++) {
if (EfiCompareGuid (&PartEntry[Index2].PartitionTypeGUID, &gEfiPartTypeUnusedGuid)) {
continue;
}
if (PartEntry[Index2].EndingLBA >= StartingLBA && PartEntry[Index2].StartingLBA <= EndingLBA) {
//
// This region overlaps with the Index1'th region
//
PEntryStatus[Index1].Overlap = TRUE;
PEntryStatus[Index2].Overlap = TRUE;
continue;
}
}
}
DEBUG ((EFI_D_INFO, " End check partition entries\n"));
}
VOID
PartitionSetCrc (
IN OUT EFI_TABLE_HEADER *Hdr
)
/*++
Routine Description:
Updates the CRC32 value in the table header
Arguments:
Hdr - The table to update
Returns:
None
--*/
{
PartitionSetCrcAltSize (Hdr->HeaderSize, Hdr);
}
VOID
PartitionSetCrcAltSize (
IN UINTN Size,
IN OUT EFI_TABLE_HEADER *Hdr
)
/*++
Routine Description:
Updates the CRC32 value in the table header
Arguments:
Size - The size of the table
Hdr - The table to update
Returns:
None
--*/
{
UINT32 Crc;
Hdr->CRC32 = 0;
gBS->CalculateCrc32 ((UINT8 *) Hdr, Size, &Crc);
Hdr->CRC32 = Crc;
}
BOOLEAN
PartitionCheckCrc (
IN UINTN MaxSize,
IN OUT EFI_TABLE_HEADER *Hdr
)
/*++
Routine Description:
Checks the CRC32 value in the table header
Arguments:
MaxSize - Max Size limit
Hdr - The table to check
Returns:
TRUE if the CRC is OK in the table
--*/
{
return PartitionCheckCrcAltSize (MaxSize, Hdr->HeaderSize, Hdr);
}
BOOLEAN
PartitionCheckCrcAltSize (
IN UINTN MaxSize,
IN UINTN Size,
IN OUT EFI_TABLE_HEADER *Hdr
)
/*++
Routine Description:
Checks the CRC32 value in the table header
Arguments:
MaxSize - Max Size Limit
Size - The size of the table
Hdr - The table to check
Returns:
TRUE if the CRC is OK in the table
--*/
{
UINT32 Crc;
UINT32 OrgCrc;
EFI_STATUS Status;
Crc = 0;
if (Size == 0) {
//
// If header size is 0 CRC will pass so return FALSE here
//
return FALSE;
}
if (MaxSize && Size > MaxSize) {
DEBUG ((EFI_D_ERROR, "CheckCrc32: Size > MaxSize\n"));
return FALSE;
}
//
// clear old crc from header
//
OrgCrc = Hdr->CRC32;
Hdr->CRC32 = 0;
Status = gBS->CalculateCrc32 ((UINT8 *) Hdr, Size, &Crc);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "CheckCrc32: Crc calculation failed\n"));
return FALSE;
}
//
// set results
//
Hdr->CRC32 = Crc;
//
// return status
//
DEBUG_CODE (
if (OrgCrc != Crc) {
DEBUG ((EFI_D_ERROR, "CheckCrc32: Crc check failed\n"));
}
)
return (BOOLEAN) (OrgCrc == Crc);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?