2 Decode a hard disk partitioned with the GPT scheme in the UEFI 2.0
5 Copyright (c) 2006 - 2009, Intel Corporation. <BR>
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "Partition.h"
21 Install child handles if the Handle supports GPT partition structure.
23 @param[in] BlockIo Parent BlockIo interface
24 @param[in] DiskIo Disk Io protocol.
25 @param[in] Lba The starting Lba of the Partition Table
26 @param[out] PartHeader Stores the partition table that is read
28 @retval TRUE The partition table is valid
29 @retval FALSE The partition table is not valid
33 PartitionValidGptTable (
34 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
35 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
37 OUT EFI_PARTITION_TABLE_HEADER
*PartHeader
42 Check if the CRC field in the Partition table header is valid
43 for Partition entry array.
45 @param[in] BlockIo Parent BlockIo interface
46 @param[in] DiskIo Disk Io Protocol.
47 @param[in] PartHeader Partition table header structure
49 @retval TRUE the CRC is valid
50 @retval FALSE the CRC is invalid
54 PartitionCheckGptEntryArrayCRC (
55 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
56 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
57 IN EFI_PARTITION_TABLE_HEADER
*PartHeader
62 Restore Partition Table to its alternate place
63 (Primary -> Backup or Backup -> Primary)
65 @param[in] BlockIo Parent BlockIo interface
66 @param[in] DiskIo Disk Io Protocol.
67 @param[in] PartHeader Partition table header structure
69 @retval TRUE Restoring succeeds
70 @retval FALSE Restoring failed
74 PartitionRestoreGptTable (
75 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
76 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
77 IN EFI_PARTITION_TABLE_HEADER
*PartHeader
82 Restore Partition Table to its alternate place.
83 (Primary -> Backup or Backup -> Primary)
85 @param[in] PartHeader Partition table header structure
86 @param[in] PartEntry The partition entry array
87 @param[out] PEntryStatus the partition entry status array
88 recording the status of each partition
92 PartitionCheckGptEntry (
93 IN EFI_PARTITION_TABLE_HEADER
*PartHeader
,
94 IN EFI_PARTITION_ENTRY
*PartEntry
,
95 OUT EFI_PARTITION_ENTRY_STATUS
*PEntryStatus
100 Checks the CRC32 value in the table header.
102 @param MaxSize Max Size limit
103 @param Size The size of the table
104 @param Hdr Table to check
106 @return TRUE CRC Valid
107 @return FALSE CRC Invalid
111 PartitionCheckCrcAltSize (
114 IN OUT EFI_TABLE_HEADER
*Hdr
119 Checks the CRC32 value in the table header.
121 @param MaxSize Max Size limit
122 @param Hdr Table to check
124 @return TRUE CRC Valid
125 @return FALSE CRC Invalid
131 IN OUT EFI_TABLE_HEADER
*Hdr
136 Updates the CRC32 value in the table header.
138 @param Size The size of the table
139 @param Hdr Table to update
143 PartitionSetCrcAltSize (
145 IN OUT EFI_TABLE_HEADER
*Hdr
150 Updates the CRC32 value in the table header.
152 @param Hdr Table to update
157 IN OUT EFI_TABLE_HEADER
*Hdr
161 Install child handles if the Handle supports GPT partition structure.
163 @param[in] This - Calling context.
164 @param[in] Handle - Parent Handle
165 @param[in] DiskIo - Parent DiskIo interface
166 @param[in] BlockIo - Parent BlockIo interface
167 @param[in] DevicePath - Parent Device Path
169 @retval EFI_SUCCESS Valid GPT disk
170 @retval EFI_MEDIA_CHANGED Media changed Detected
171 @retval other Not a valid GPT disk
175 PartitionInstallGptChildHandles (
176 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
177 IN EFI_HANDLE Handle
,
178 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
179 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
180 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
186 MASTER_BOOT_RECORD
*ProtectiveMbr
;
187 EFI_PARTITION_TABLE_HEADER
*PrimaryHeader
;
188 EFI_PARTITION_TABLE_HEADER
*BackupHeader
;
189 EFI_PARTITION_ENTRY
*PartEntry
;
190 EFI_PARTITION_ENTRY_STATUS
*PEntryStatus
;
192 EFI_STATUS GptValidStatus
;
193 HARDDRIVE_DEVICE_PATH HdDev
;
195 ProtectiveMbr
= NULL
;
196 PrimaryHeader
= NULL
;
201 BlockSize
= BlockIo
->Media
->BlockSize
;
202 LastBlock
= BlockIo
->Media
->LastBlock
;
204 DEBUG ((EFI_D_INFO
, " BlockSize : %d \n", BlockSize
));
205 DEBUG ((EFI_D_INFO
, " LastBlock : %lx \n", LastBlock
));
207 GptValidStatus
= EFI_NOT_FOUND
;
210 // Allocate a buffer for the Protective MBR
212 ProtectiveMbr
= AllocatePool (BlockSize
);
213 if (ProtectiveMbr
== NULL
) {
214 return EFI_NOT_FOUND
;
218 // Read the Protective MBR from LBA #0
220 Status
= DiskIo
->ReadDisk (
222 BlockIo
->Media
->MediaId
,
224 BlockIo
->Media
->BlockSize
,
227 if (EFI_ERROR (Status
)) {
228 GptValidStatus
= Status
;
232 // Verify that the Protective MBR is valid
234 for (Index
= 0; Index
< MAX_MBR_PARTITIONS
; Index
++) {
235 if (ProtectiveMbr
->Partition
[Index
].BootIndicator
== 0x00 &&
236 ProtectiveMbr
->Partition
[Index
].OSIndicator
== PMBR_GPT_PARTITION
&&
237 UNPACK_UINT32 (ProtectiveMbr
->Partition
[Index
].StartingLBA
) == 1
242 if (Index
== MAX_MBR_PARTITIONS
) {
247 // Allocate the GPT structures
249 PrimaryHeader
= AllocateZeroPool (sizeof (EFI_PARTITION_TABLE_HEADER
));
250 if (PrimaryHeader
== NULL
) {
254 BackupHeader
= AllocateZeroPool (sizeof (EFI_PARTITION_TABLE_HEADER
));
255 if (BackupHeader
== NULL
) {
260 // Check primary and backup partition tables
262 if (!PartitionValidGptTable (BlockIo
, DiskIo
, PRIMARY_PART_HEADER_LBA
, PrimaryHeader
)) {
263 DEBUG ((EFI_D_INFO
, " Not Valid primary partition table\n"));
265 if (!PartitionValidGptTable (BlockIo
, DiskIo
, LastBlock
, BackupHeader
)) {
266 DEBUG ((EFI_D_INFO
, " Not Valid backup partition table\n"));
269 DEBUG ((EFI_D_INFO
, " Valid backup partition table\n"));
270 DEBUG ((EFI_D_INFO
, " Restore primary partition table by the backup\n"));
271 if (!PartitionRestoreGptTable (BlockIo
, DiskIo
, BackupHeader
)) {
272 DEBUG ((EFI_D_INFO
, " Restore primary partition table error\n"));
275 if (PartitionValidGptTable (BlockIo
, DiskIo
, BackupHeader
->AlternateLBA
, PrimaryHeader
)) {
276 DEBUG ((EFI_D_INFO
, " Restore backup partition table success\n"));
279 } else if (!PartitionValidGptTable (BlockIo
, DiskIo
, PrimaryHeader
->AlternateLBA
, BackupHeader
)) {
280 DEBUG ((EFI_D_INFO
, " Valid primary and !Valid backup partition table\n"));
281 DEBUG ((EFI_D_INFO
, " Restore backup partition table by the primary\n"));
282 if (!PartitionRestoreGptTable (BlockIo
, DiskIo
, PrimaryHeader
)) {
283 DEBUG ((EFI_D_INFO
, " Restore backup partition table error\n"));
286 if (PartitionValidGptTable (BlockIo
, DiskIo
, PrimaryHeader
->AlternateLBA
, BackupHeader
)) {
287 DEBUG ((EFI_D_INFO
, " Restore backup partition table success\n"));
292 DEBUG ((EFI_D_INFO
, " Valid primary and Valid backup partition table\n"));
295 // Read the EFI Partition Entries
297 PartEntry
= AllocatePool (PrimaryHeader
->NumberOfPartitionEntries
* sizeof (EFI_PARTITION_ENTRY
));
298 if (PartEntry
== NULL
) {
299 DEBUG ((EFI_D_ERROR
, "Allocate pool error\n"));
303 Status
= DiskIo
->ReadDisk (
305 BlockIo
->Media
->MediaId
,
306 MultU64x32(PrimaryHeader
->PartitionEntryLBA
, BlockSize
),
307 PrimaryHeader
->NumberOfPartitionEntries
* (PrimaryHeader
->SizeOfPartitionEntry
),
310 if (EFI_ERROR (Status
)) {
311 GptValidStatus
= Status
;
312 DEBUG ((EFI_D_ERROR
, " Partition Entry ReadDisk error\n"));
316 DEBUG ((EFI_D_INFO
, " Partition entries read block success\n"));
318 DEBUG ((EFI_D_INFO
, " Number of partition entries: %d\n", PrimaryHeader
->NumberOfPartitionEntries
));
320 PEntryStatus
= AllocateZeroPool (PrimaryHeader
->NumberOfPartitionEntries
* sizeof (EFI_PARTITION_ENTRY_STATUS
));
321 if (PEntryStatus
== NULL
) {
322 DEBUG ((EFI_D_ERROR
, "Allocate pool error\n"));
327 // Check the integrity of partition entries
329 PartitionCheckGptEntry (PrimaryHeader
, PartEntry
, PEntryStatus
);
332 // If we got this far the GPT layout of the disk is valid and we should return true
334 GptValidStatus
= EFI_SUCCESS
;
337 // Create child device handles
339 for (Index
= 0; Index
< PrimaryHeader
->NumberOfPartitionEntries
; Index
++) {
340 if (CompareGuid (&PartEntry
[Index
].PartitionTypeGUID
, &gEfiPartTypeUnusedGuid
) ||
341 PEntryStatus
[Index
].OutOfRange
||
342 PEntryStatus
[Index
].Overlap
345 // Don't use null EFI Partition Entries or Invalid Partition Entries
350 ZeroMem (&HdDev
, sizeof (HdDev
));
351 HdDev
.Header
.Type
= MEDIA_DEVICE_PATH
;
352 HdDev
.Header
.SubType
= MEDIA_HARDDRIVE_DP
;
353 SetDevicePathNodeLength (&HdDev
.Header
, sizeof (HdDev
));
355 HdDev
.PartitionNumber
= (UINT32
) Index
+ 1;
356 HdDev
.MBRType
= MBR_TYPE_EFI_PARTITION_TABLE_HEADER
;
357 HdDev
.SignatureType
= SIGNATURE_TYPE_GUID
;
358 HdDev
.PartitionStart
= PartEntry
[Index
].StartingLBA
;
359 HdDev
.PartitionSize
= PartEntry
[Index
].EndingLBA
- PartEntry
[Index
].StartingLBA
+ 1;
360 CopyMem (HdDev
.Signature
, &PartEntry
[Index
].UniquePartitionGUID
, sizeof (EFI_GUID
));
362 DEBUG ((EFI_D_INFO
, " Index : %d\n", (UINT32
) Index
));
363 DEBUG ((EFI_D_INFO
, " Start LBA : %lx\n", (UINT64
) HdDev
.PartitionStart
));
364 DEBUG ((EFI_D_INFO
, " End LBA : %lx\n", (UINT64
) PartEntry
[Index
].EndingLBA
));
365 DEBUG ((EFI_D_INFO
, " Partition size: %lx\n", (UINT64
) HdDev
.PartitionSize
));
366 DEBUG ((EFI_D_INFO
, " Start : %lx", MultU64x32 (PartEntry
[Index
].StartingLBA
, BlockSize
)));
367 DEBUG ((EFI_D_INFO
, " End : %lx\n", MultU64x32 (PartEntry
[Index
].EndingLBA
, BlockSize
)));
369 Status
= PartitionInstallChildHandle (
375 (EFI_DEVICE_PATH_PROTOCOL
*) &HdDev
,
376 PartEntry
[Index
].StartingLBA
,
377 PartEntry
[Index
].EndingLBA
,
379 CompareGuid(&PartEntry
[Index
].PartitionTypeGUID
, &gEfiPartTypeSystemPartGuid
)
383 DEBUG ((EFI_D_INFO
, "Prepare to Free Pool\n"));
386 if (ProtectiveMbr
!= NULL
) {
387 FreePool (ProtectiveMbr
);
389 if (PrimaryHeader
!= NULL
) {
390 FreePool (PrimaryHeader
);
392 if (BackupHeader
!= NULL
) {
393 FreePool (BackupHeader
);
395 if (PartEntry
!= NULL
) {
396 FreePool (PartEntry
);
398 if (PEntryStatus
!= NULL
) {
399 FreePool (PEntryStatus
);
402 return GptValidStatus
;
407 Install child handles if the Handle supports GPT partition structure.
409 @param[in] BlockIo Parent BlockIo interface
410 @param[in] DiskIo Disk Io protocol.
411 @param[in] Lba The starting Lba of the Partition Table
412 @param[out] PartHeader Stores the partition table that is read
414 @retval TRUE The partition table is valid
415 @retval FALSE The partition table is not valid
419 PartitionValidGptTable (
420 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
421 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
423 OUT EFI_PARTITION_TABLE_HEADER
*PartHeader
428 EFI_PARTITION_TABLE_HEADER
*PartHdr
;
430 BlockSize
= BlockIo
->Media
->BlockSize
;
432 PartHdr
= AllocateZeroPool (BlockSize
);
434 if (PartHdr
== NULL
) {
435 DEBUG ((EFI_D_ERROR
, "Allocate pool error\n"));
439 // Read the EFI Partition Table Header
441 Status
= DiskIo
->ReadDisk (
443 BlockIo
->Media
->MediaId
,
444 MultU64x32 (Lba
, BlockSize
),
448 if (EFI_ERROR (Status
)) {
453 if ((PartHdr
->Header
.Signature
!= EFI_PTAB_HEADER_ID
) ||
454 !PartitionCheckCrc (BlockSize
, &PartHdr
->Header
) ||
455 PartHdr
->MyLBA
!= Lba
457 DEBUG ((EFI_D_INFO
, "Invalid efi partition table header\n"));
462 CopyMem (PartHeader
, PartHdr
, sizeof (EFI_PARTITION_TABLE_HEADER
));
463 if (!PartitionCheckGptEntryArrayCRC (BlockIo
, DiskIo
, PartHeader
)) {
468 DEBUG ((EFI_D_INFO
, " Valid efi partition table header\n"));
475 Check if the CRC field in the Partition table header is valid
476 for Partition entry array.
478 @param[in] BlockIo Parent BlockIo interface
479 @param[in] DiskIo Disk Io Protocol.
480 @param[in] PartHeader Partition table header structure
482 @retval TRUE the CRC is valid
483 @retval FALSE the CRC is invalid
487 PartitionCheckGptEntryArrayCRC (
488 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
489 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
490 IN EFI_PARTITION_TABLE_HEADER
*PartHeader
499 // Read the EFI Partition Entries
501 Ptr
= AllocatePool (PartHeader
->NumberOfPartitionEntries
* PartHeader
->SizeOfPartitionEntry
);
503 DEBUG ((EFI_D_ERROR
, " Allocate pool error\n"));
507 Status
= DiskIo
->ReadDisk (
509 BlockIo
->Media
->MediaId
,
510 MultU64x32(PartHeader
->PartitionEntryLBA
, BlockIo
->Media
->BlockSize
),
511 PartHeader
->NumberOfPartitionEntries
* PartHeader
->SizeOfPartitionEntry
,
514 if (EFI_ERROR (Status
)) {
519 Size
= PartHeader
->NumberOfPartitionEntries
* PartHeader
->SizeOfPartitionEntry
;
521 Status
= gBS
->CalculateCrc32 (Ptr
, Size
, &Crc
);
522 if (EFI_ERROR (Status
)) {
523 DEBUG ((EFI_D_ERROR
, "CheckPEntryArrayCRC: Crc calculation failed\n"));
530 return (BOOLEAN
) (PartHeader
->PartitionEntryArrayCRC32
== Crc
);
535 Restore Partition Table to its alternate place
536 (Primary -> Backup or Backup -> Primary)
538 @param[in] BlockIo Parent BlockIo interface
539 @param[in] DiskIo Disk Io Protocol.
540 @param[in] PartHeader Partition table header structure
542 @retval TRUE Restoring succeeds
543 @retval FALSE Restoring failed
547 PartitionRestoreGptTable (
548 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
549 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
550 IN EFI_PARTITION_TABLE_HEADER
*PartHeader
555 EFI_PARTITION_TABLE_HEADER
*PartHdr
;
562 BlockSize
= BlockIo
->Media
->BlockSize
;
564 PartHdr
= AllocateZeroPool (BlockSize
);
566 if (PartHdr
== NULL
) {
567 DEBUG ((EFI_D_ERROR
, "Allocate pool error\n"));
571 PEntryLBA
= (PartHeader
->MyLBA
== PRIMARY_PART_HEADER_LBA
) ? \
572 (PartHeader
->LastUsableLBA
+ 1) : \
573 (PRIMARY_PART_HEADER_LBA
+ 1);
575 CopyMem (PartHdr
, PartHeader
, sizeof (EFI_PARTITION_TABLE_HEADER
));
577 PartHdr
->MyLBA
= PartHeader
->AlternateLBA
;
578 PartHdr
->AlternateLBA
= PartHeader
->MyLBA
;
579 PartHdr
->PartitionEntryLBA
= PEntryLBA
;
580 PartitionSetCrc ((EFI_TABLE_HEADER
*) PartHdr
);
582 Status
= DiskIo
->WriteDisk (
584 BlockIo
->Media
->MediaId
,
585 MultU64x32 (PartHdr
->MyLBA
, BlockIo
->Media
->BlockSize
),
589 if (EFI_ERROR (Status
)) {
593 Ptr
= AllocatePool (PartHeader
->NumberOfPartitionEntries
* PartHeader
->SizeOfPartitionEntry
);
595 DEBUG ((EFI_D_ERROR
, " Allocate pool error\n"));
596 Status
= EFI_OUT_OF_RESOURCES
;
600 Status
= DiskIo
->ReadDisk (
602 BlockIo
->Media
->MediaId
,
603 MultU64x32(PartHeader
->PartitionEntryLBA
, BlockIo
->Media
->BlockSize
),
604 PartHeader
->NumberOfPartitionEntries
* PartHeader
->SizeOfPartitionEntry
,
607 if (EFI_ERROR (Status
)) {
611 Status
= DiskIo
->WriteDisk (
613 BlockIo
->Media
->MediaId
,
614 MultU64x32(PEntryLBA
, BlockIo
->Media
->BlockSize
),
615 PartHeader
->NumberOfPartitionEntries
* PartHeader
->SizeOfPartitionEntry
,
626 if (EFI_ERROR (Status
)) {
635 Restore Partition Table to its alternate place.
636 (Primary -> Backup or Backup -> Primary)
638 @param[in] PartHeader Partition table header structure
639 @param[in] PartEntry The partition entry array
640 @param[out] PEntryStatus the partition entry status array
641 recording the status of each partition
645 PartitionCheckGptEntry (
646 IN EFI_PARTITION_TABLE_HEADER
*PartHeader
,
647 IN EFI_PARTITION_ENTRY
*PartEntry
,
648 OUT EFI_PARTITION_ENTRY_STATUS
*PEntryStatus
656 DEBUG ((EFI_D_INFO
, " start check partition entries\n"));
657 for (Index1
= 0; Index1
< PartHeader
->NumberOfPartitionEntries
; Index1
++) {
658 if (CompareGuid (&PartEntry
[Index1
].PartitionTypeGUID
, &gEfiPartTypeUnusedGuid
)) {
662 StartingLBA
= PartEntry
[Index1
].StartingLBA
;
663 EndingLBA
= PartEntry
[Index1
].EndingLBA
;
664 if (StartingLBA
> EndingLBA
||
665 StartingLBA
< PartHeader
->FirstUsableLBA
||
666 StartingLBA
> PartHeader
->LastUsableLBA
||
667 EndingLBA
< PartHeader
->FirstUsableLBA
||
668 EndingLBA
> PartHeader
->LastUsableLBA
670 PEntryStatus
[Index1
].OutOfRange
= TRUE
;
674 for (Index2
= Index1
+ 1; Index2
< PartHeader
->NumberOfPartitionEntries
; Index2
++) {
676 if (CompareGuid (&PartEntry
[Index2
].PartitionTypeGUID
, &gEfiPartTypeUnusedGuid
)) {
680 if (PartEntry
[Index2
].EndingLBA
>= StartingLBA
&& PartEntry
[Index2
].StartingLBA
<= EndingLBA
) {
682 // This region overlaps with the Index1'th region
684 PEntryStatus
[Index1
].Overlap
= TRUE
;
685 PEntryStatus
[Index2
].Overlap
= TRUE
;
692 DEBUG ((EFI_D_INFO
, " End check partition entries\n"));
697 Updates the CRC32 value in the table header.
699 @param Hdr Table to update
704 IN OUT EFI_TABLE_HEADER
*Hdr
707 PartitionSetCrcAltSize (Hdr
->HeaderSize
, Hdr
);
712 Updates the CRC32 value in the table header.
714 @param Size The size of the table
715 @param Hdr Table to update
719 PartitionSetCrcAltSize (
721 IN OUT EFI_TABLE_HEADER
*Hdr
727 gBS
->CalculateCrc32 ((UINT8
*) Hdr
, Size
, &Crc
);
733 Checks the CRC32 value in the table header.
735 @param MaxSize Max Size limit
736 @param Hdr Table to check
738 @return TRUE CRC Valid
739 @return FALSE CRC Invalid
745 IN OUT EFI_TABLE_HEADER
*Hdr
748 return PartitionCheckCrcAltSize (MaxSize
, Hdr
->HeaderSize
, Hdr
);
753 Checks the CRC32 value in the table header.
755 @param MaxSize Max Size limit
756 @param Size The size of the table
757 @param Hdr Table to check
759 @return TRUE CRC Valid
760 @return FALSE CRC Invalid
764 PartitionCheckCrcAltSize (
767 IN OUT EFI_TABLE_HEADER
*Hdr
778 // If header size is 0 CRC will pass so return FALSE here
783 if ((MaxSize
!= 0) && (Size
> MaxSize
)) {
784 DEBUG ((EFI_D_ERROR
, "CheckCrc32: Size > MaxSize\n"));
788 // clear old crc from header
793 Status
= gBS
->CalculateCrc32 ((UINT8
*) Hdr
, Size
, &Crc
);
794 if (EFI_ERROR (Status
)) {
795 DEBUG ((EFI_D_ERROR
, "CheckCrc32: Crc calculation failed\n"));
808 DEBUG ((EFI_D_ERROR
, "CheckCrc32: Crc check failed\n"));
812 return (BOOLEAN
) (OrgCrc
== Crc
);