3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Decode a hard disk partitioned with the GPT scheme in the EFI 1.0
23 #include "Partition.h"
26 PartitionValidGptTable (
27 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
28 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
30 OUT EFI_PARTITION_TABLE_HEADER
*PartHeader
34 PartitionCheckGptEntryArrayCRC (
35 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
36 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
37 IN EFI_PARTITION_TABLE_HEADER
*PartHeader
41 PartitionRestoreGptTable (
42 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
43 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
44 IN EFI_PARTITION_TABLE_HEADER
*PartHeader
48 PartitionCheckGptEntry (
49 IN EFI_PARTITION_TABLE_HEADER
*PartHeader
,
50 IN EFI_PARTITION_ENTRY
*PartEntry
,
51 OUT EFI_PARTITION_ENTRY_STATUS
*PEntryStatus
55 PartitionCheckCrcAltSize (
58 IN OUT EFI_TABLE_HEADER
*Hdr
64 IN OUT EFI_TABLE_HEADER
*Hdr
68 PartitionSetCrcAltSize (
70 IN OUT EFI_TABLE_HEADER
*Hdr
75 IN OUT EFI_TABLE_HEADER
*Hdr
79 PartitionInstallGptChildHandles (
80 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
82 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
83 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
84 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
89 Install child handles if the Handle supports GPT partition structure.
92 This - Calling context.
93 Handle - Parent Handle
94 DiskIo - Parent DiskIo interface
95 BlockIo - Parent BlockIo interface
96 DevicePath - Parent Device Path
100 FALSE - Not a valid GPT disk
107 MASTER_BOOT_RECORD
*ProtectiveMbr
;
108 EFI_PARTITION_TABLE_HEADER
*PrimaryHeader
;
109 EFI_PARTITION_TABLE_HEADER
*BackupHeader
;
110 EFI_PARTITION_ENTRY
*PartEntry
;
111 EFI_PARTITION_ENTRY_STATUS
*PEntryStatus
;
114 HARDDRIVE_DEVICE_PATH HdDev
;
116 ProtectiveMbr
= NULL
;
117 PrimaryHeader
= NULL
;
122 BlockSize
= BlockIo
->Media
->BlockSize
;
123 LastBlock
= BlockIo
->Media
->LastBlock
;
125 DEBUG ((EFI_D_INFO
, " BlockSize : %d \n", BlockSize
));
126 DEBUG ((EFI_D_INFO
, " LastBlock : %x \n", LastBlock
));
131 // Allocate a buffer for the Protective MBR
133 ProtectiveMbr
= AllocatePool (BlockSize
);
134 if (ProtectiveMbr
== NULL
) {
139 // Read the Protective MBR from LBA #0
141 Status
= BlockIo
->ReadBlocks (
143 BlockIo
->Media
->MediaId
,
145 BlockIo
->Media
->BlockSize
,
148 if (EFI_ERROR (Status
)) {
152 // Verify that the Protective MBR is valid
154 if (ProtectiveMbr
->Partition
[0].BootIndicator
!= 0x00 ||
155 ProtectiveMbr
->Partition
[0].OSIndicator
!= PMBR_GPT_PARTITION
||
156 UNPACK_UINT32 (ProtectiveMbr
->Partition
[0].StartingLBA
) != 1
162 // Allocate the GPT structures
164 PrimaryHeader
= AllocateZeroPool (sizeof (EFI_PARTITION_TABLE_HEADER
));
165 if (PrimaryHeader
== NULL
) {
169 BackupHeader
= AllocateZeroPool (sizeof (EFI_PARTITION_TABLE_HEADER
));
171 if (BackupHeader
== NULL
) {
176 // Check primary and backup partition tables
178 if (!PartitionValidGptTable (BlockIo
, DiskIo
, PRIMARY_PART_HEADER_LBA
, PrimaryHeader
)) {
179 DEBUG ((EFI_D_INFO
, " Not Valid primary partition table\n"));
181 if (!PartitionValidGptTable (BlockIo
, DiskIo
, LastBlock
, BackupHeader
)) {
182 DEBUG ((EFI_D_INFO
, " Not Valid backup partition table\n"));
185 DEBUG ((EFI_D_INFO
, " Valid backup partition table\n"));
186 DEBUG ((EFI_D_INFO
, " Restore primary partition table by the backup\n"));
187 if (!PartitionRestoreGptTable (BlockIo
, DiskIo
, BackupHeader
)) {
188 DEBUG ((EFI_D_INFO
, " Restore primary partition table error\n"));
191 if (PartitionValidGptTable (BlockIo
, DiskIo
, BackupHeader
->AlternateLBA
, PrimaryHeader
)) {
192 DEBUG ((EFI_D_INFO
, " Restore backup partition table success\n"));
195 } else if (!PartitionValidGptTable (BlockIo
, DiskIo
, PrimaryHeader
->AlternateLBA
, BackupHeader
)) {
196 DEBUG ((EFI_D_INFO
, " Valid primary and !Valid backup partition table\n"));
197 DEBUG ((EFI_D_INFO
, " Restore backup partition table by the primary\n"));
198 if (!PartitionRestoreGptTable (BlockIo
, DiskIo
, PrimaryHeader
)) {
199 DEBUG ((EFI_D_INFO
, " Restore backup partition table error\n"));
202 if (PartitionValidGptTable (BlockIo
, DiskIo
, PrimaryHeader
->AlternateLBA
, BackupHeader
)) {
203 DEBUG ((EFI_D_INFO
, " Restore backup partition table success\n"));
208 DEBUG ((EFI_D_INFO
, " Valid primary and Valid backup partition table\n"));
211 // Read the EFI Partition Entries
213 PartEntry
= AllocatePool (PrimaryHeader
->NumberOfPartitionEntries
* sizeof (EFI_PARTITION_ENTRY
));
214 if (PartEntry
== NULL
) {
215 DEBUG ((EFI_D_ERROR
, "Allocate pool error\n"));
219 Status
= DiskIo
->ReadDisk (
221 BlockIo
->Media
->MediaId
,
222 MultU64x32(PrimaryHeader
->PartitionEntryLBA
, BlockSize
),
223 PrimaryHeader
->NumberOfPartitionEntries
* (PrimaryHeader
->SizeOfPartitionEntry
),
226 if (EFI_ERROR (Status
)) {
227 DEBUG ((EFI_D_INFO
, " Partition Entry ReadBlocks error\n"));
231 DEBUG ((EFI_D_INFO
, " Partition entries read block success\n"));
233 DEBUG ((EFI_D_INFO
, " Number of partition entries: %d\n", PrimaryHeader
->NumberOfPartitionEntries
));
235 PEntryStatus
= AllocateZeroPool (PrimaryHeader
->NumberOfPartitionEntries
* sizeof (EFI_PARTITION_ENTRY_STATUS
));
236 if (PEntryStatus
== NULL
) {
237 DEBUG ((EFI_D_ERROR
, "Allocate pool error\n"));
242 // Check the integrity of partition entries
244 PartitionCheckGptEntry (PrimaryHeader
, PartEntry
, PEntryStatus
);
247 // If we got this far the GPT layout of the disk is valid and we should return true
252 // Create child device handles
254 for (Index
= 0; Index
< PrimaryHeader
->NumberOfPartitionEntries
; Index
++) {
255 if (CompareGuid (&PartEntry
[Index
].PartitionTypeGUID
, &gEfiPartTypeUnusedGuid
) ||
256 PEntryStatus
[Index
].OutOfRange
||
257 PEntryStatus
[Index
].Overlap
260 // Don't use null EFI Partition Entries or Invalid Partition Entries
265 ZeroMem (&HdDev
, sizeof (HdDev
));
266 HdDev
.Header
.Type
= MEDIA_DEVICE_PATH
;
267 HdDev
.Header
.SubType
= MEDIA_HARDDRIVE_DP
;
268 SetDevicePathNodeLength (&HdDev
.Header
, sizeof (HdDev
));
270 HdDev
.PartitionNumber
= (UINT32
) Index
+ 1;
271 HdDev
.MBRType
= MBR_TYPE_EFI_PARTITION_TABLE_HEADER
;
272 HdDev
.SignatureType
= SIGNATURE_TYPE_GUID
;
273 HdDev
.PartitionStart
= PartEntry
[Index
].StartingLBA
;
274 HdDev
.PartitionSize
= PartEntry
[Index
].EndingLBA
- PartEntry
[Index
].StartingLBA
+ 1;
275 CopyMem (HdDev
.Signature
, &PartEntry
[Index
].UniquePartitionGUID
, sizeof (EFI_GUID
));
277 DEBUG ((EFI_D_INFO
, " Index : %d\n", Index
));
278 DEBUG ((EFI_D_INFO
, " Start LBA : %x\n", HdDev
.PartitionStart
));
279 DEBUG ((EFI_D_INFO
, " End LBA : %x\n", PartEntry
[Index
].EndingLBA
));
280 DEBUG ((EFI_D_INFO
, " Partition size: %x\n", HdDev
.PartitionSize
));
281 DEBUG ((EFI_D_INFO
, " Start : %x", MultU64x32 (PartEntry
[Index
].StartingLBA
, BlockSize
)));
282 DEBUG ((EFI_D_INFO
, " End : %x\n", MultU64x32 (PartEntry
[Index
].EndingLBA
, BlockSize
)));
284 Status
= PartitionInstallChildHandle (
290 (EFI_DEVICE_PATH_PROTOCOL
*) &HdDev
,
291 PartEntry
[Index
].StartingLBA
,
292 PartEntry
[Index
].EndingLBA
,
294 CompareGuid(&PartEntry
[Index
].PartitionTypeGUID
, &gEfiPartTypeSystemPartGuid
)
298 DEBUG ((EFI_D_INFO
, "Prepare to Free Pool\n"));
301 if (ProtectiveMbr
!= NULL
) {
302 gBS
->FreePool (ProtectiveMbr
);
304 if (PrimaryHeader
!= NULL
) {
305 gBS
->FreePool (PrimaryHeader
);
307 if (BackupHeader
!= NULL
) {
308 gBS
->FreePool (BackupHeader
);
310 if (PartEntry
!= NULL
) {
311 gBS
->FreePool (PartEntry
);
313 if (PEntryStatus
!= NULL
) {
314 gBS
->FreePool (PEntryStatus
);
321 PartitionValidGptTable (
322 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
323 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
325 OUT EFI_PARTITION_TABLE_HEADER
*PartHeader
330 Check if the GPT partition table is valid
333 BlockIo - Parent BlockIo interface
334 DiskIo - Disk Io protocol.
335 Lba - The starting Lba of the Partition Table
336 PartHeader - Stores the partition table that is read
339 TRUE - The partition table is valid
340 FALSE - The partition table is not valid
346 EFI_PARTITION_TABLE_HEADER
*PartHdr
;
348 BlockSize
= BlockIo
->Media
->BlockSize
;
350 PartHdr
= AllocateZeroPool (BlockSize
);
352 if (PartHdr
== NULL
) {
353 DEBUG ((EFI_D_ERROR
, "Allocate pool error\n"));
357 // Read the EFI Partition Table Header
359 Status
= BlockIo
->ReadBlocks (
361 BlockIo
->Media
->MediaId
,
366 if (EFI_ERROR (Status
)) {
367 gBS
->FreePool (PartHdr
);
371 if (CompareMem (&PartHdr
->Header
.Signature
, EFI_PTAB_HEADER_ID
, sizeof (UINT64
)) != 0 ||
372 !PartitionCheckCrc (BlockSize
, &PartHdr
->Header
) ||
373 PartHdr
->MyLBA
!= Lba
375 DEBUG ((EFI_D_INFO
, " !Valid efi partition table header\n"));
376 gBS
->FreePool (PartHdr
);
380 CopyMem (PartHeader
, PartHdr
, sizeof (EFI_PARTITION_TABLE_HEADER
));
381 if (!PartitionCheckGptEntryArrayCRC (BlockIo
, DiskIo
, PartHeader
)) {
382 gBS
->FreePool (PartHdr
);
386 DEBUG ((EFI_D_INFO
, " Valid efi partition table header\n"));
387 gBS
->FreePool (PartHdr
);
392 PartitionCheckGptEntryArrayCRC (
393 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
394 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
395 IN EFI_PARTITION_TABLE_HEADER
*PartHeader
401 Check if the CRC field in the Partition table header is valid
402 for Partition entry array
406 BlockIo - parent BlockIo interface
407 DiskIo - Disk Io Protocol.
408 PartHeader - Partition table header structure
412 TRUE - the CRC is valid
413 FALSE - the CRC is invalid
423 // Read the EFI Partition Entries
425 Ptr
= AllocatePool (PartHeader
->NumberOfPartitionEntries
* PartHeader
->SizeOfPartitionEntry
);
427 DEBUG ((EFI_D_ERROR
, " Allocate pool error\n"));
431 Status
= DiskIo
->ReadDisk (
433 BlockIo
->Media
->MediaId
,
434 MultU64x32(PartHeader
->PartitionEntryLBA
, BlockIo
->Media
->BlockSize
),
435 PartHeader
->NumberOfPartitionEntries
* PartHeader
->SizeOfPartitionEntry
,
438 if (EFI_ERROR (Status
)) {
443 Size
= PartHeader
->NumberOfPartitionEntries
* PartHeader
->SizeOfPartitionEntry
;
445 Status
= gBS
->CalculateCrc32 (Ptr
, Size
, &Crc
);
446 if (EFI_ERROR (Status
)) {
447 DEBUG ((EFI_D_ERROR
, "CheckPEntryArrayCRC: Crc calculation failed\n"));
454 return (BOOLEAN
) (PartHeader
->PartitionEntryArrayCRC32
== Crc
);
458 PartitionRestoreGptTable (
459 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
460 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
461 IN EFI_PARTITION_TABLE_HEADER
*PartHeader
467 Restore Partition Table to its alternate place
468 (Primary -> Backup or Backup -> Primary)
472 BlockIo - parent BlockIo interface
473 DiskIo - Disk Io Protocol.
474 PartHeader - the source Partition table header structure
478 TRUE - Restoring succeeds
479 FALSE - Restoring failed
485 EFI_PARTITION_TABLE_HEADER
*PartHdr
;
492 BlockSize
= BlockIo
->Media
->BlockSize
;
494 PartHdr
= AllocateZeroPool (BlockSize
);
496 if (PartHdr
== NULL
) {
497 DEBUG ((EFI_D_ERROR
, "Allocate pool error\n"));
501 PEntryLBA
= (PartHeader
->MyLBA
== PRIMARY_PART_HEADER_LBA
) ? \
502 (PartHeader
->LastUsableLBA
+ 1) : \
503 (PRIMARY_PART_HEADER_LBA
+ 1);
505 CopyMem (PartHdr
, PartHeader
, sizeof (EFI_PARTITION_TABLE_HEADER
));
507 PartHdr
->MyLBA
= PartHeader
->AlternateLBA
;
508 PartHdr
->AlternateLBA
= PartHeader
->MyLBA
;
509 PartHdr
->PartitionEntryLBA
= PEntryLBA
;
510 PartitionSetCrc ((EFI_TABLE_HEADER
*) PartHdr
);
512 Status
= BlockIo
->WriteBlocks (BlockIo
, BlockIo
->Media
->MediaId
, PartHdr
->MyLBA
, BlockSize
, PartHdr
);
513 if (EFI_ERROR (Status
)) {
517 Ptr
= AllocatePool (PartHeader
->NumberOfPartitionEntries
* PartHeader
->SizeOfPartitionEntry
);
519 DEBUG ((EFI_D_ERROR
, " Allocate pool effor\n"));
520 Status
= EFI_OUT_OF_RESOURCES
;
524 Status
= DiskIo
->ReadDisk (
526 BlockIo
->Media
->MediaId
,
527 MultU64x32(PartHeader
->PartitionEntryLBA
, BlockIo
->Media
->BlockSize
),
528 PartHeader
->NumberOfPartitionEntries
* PartHeader
->SizeOfPartitionEntry
,
531 if (EFI_ERROR (Status
)) {
535 Status
= DiskIo
->WriteDisk (
537 BlockIo
->Media
->MediaId
,
538 MultU64x32(PEntryLBA
, BlockIo
->Media
->BlockSize
),
539 PartHeader
->NumberOfPartitionEntries
* PartHeader
->SizeOfPartitionEntry
,
544 gBS
->FreePool (PartHdr
);
547 if (EFI_ERROR (Status
)) {
555 PartitionCheckGptEntry (
556 IN EFI_PARTITION_TABLE_HEADER
*PartHeader
,
557 IN EFI_PARTITION_ENTRY
*PartEntry
,
558 OUT EFI_PARTITION_ENTRY_STATUS
*PEntryStatus
564 Check each partition entry for its range
568 PartHeader - the partition table header
569 PartEntry - the partition entry array
570 PEntryStatus - the partition entry status array recording the status of
583 DEBUG ((EFI_D_INFO
, " start check partition entries\n"));
584 for (Index1
= 0; Index1
< PartHeader
->NumberOfPartitionEntries
; Index1
++) {
585 if (CompareGuid (&PartEntry
[Index1
].PartitionTypeGUID
, &gEfiPartTypeUnusedGuid
)) {
589 StartingLBA
= PartEntry
[Index1
].StartingLBA
;
590 EndingLBA
= PartEntry
[Index1
].EndingLBA
;
591 if (StartingLBA
> EndingLBA
||
592 StartingLBA
< PartHeader
->FirstUsableLBA
||
593 StartingLBA
> PartHeader
->LastUsableLBA
||
594 EndingLBA
< PartHeader
->FirstUsableLBA
||
595 EndingLBA
> PartHeader
->LastUsableLBA
597 PEntryStatus
[Index1
].OutOfRange
= TRUE
;
601 for (Index2
= Index1
+ 1; Index2
< PartHeader
->NumberOfPartitionEntries
; Index2
++) {
603 if (CompareGuid (&PartEntry
[Index2
].PartitionTypeGUID
, &gEfiPartTypeUnusedGuid
)) {
607 if (PartEntry
[Index2
].EndingLBA
>= StartingLBA
&& PartEntry
[Index2
].StartingLBA
<= EndingLBA
) {
609 // This region overlaps with the Index1'th region
611 PEntryStatus
[Index1
].Overlap
= TRUE
;
612 PEntryStatus
[Index2
].Overlap
= TRUE
;
619 DEBUG ((EFI_D_INFO
, " End check partition entries\n"));
624 IN OUT EFI_TABLE_HEADER
*Hdr
630 Updates the CRC32 value in the table header
634 Hdr - The table to update
642 PartitionSetCrcAltSize (Hdr
->HeaderSize
, Hdr
);
646 PartitionSetCrcAltSize (
648 IN OUT EFI_TABLE_HEADER
*Hdr
654 Updates the CRC32 value in the table header
658 Size - The size of the table
659 Hdr - The table to update
670 gBS
->CalculateCrc32 ((UINT8
*) Hdr
, Size
, &Crc
);
677 IN OUT EFI_TABLE_HEADER
*Hdr
683 Checks the CRC32 value in the table header
687 MaxSize - Max Size limit
688 Hdr - The table to check
692 TRUE if the CRC is OK in the table
696 return PartitionCheckCrcAltSize (MaxSize
, Hdr
->HeaderSize
, Hdr
);
700 PartitionCheckCrcAltSize (
703 IN OUT EFI_TABLE_HEADER
*Hdr
709 Checks the CRC32 value in the table header
713 MaxSize - Max Size Limit
714 Size - The size of the table
715 Hdr - The table to check
719 TRUE if the CRC is OK in the table
731 // If header size is 0 CRC will pass so return FALSE here
736 if (MaxSize
&& Size
> MaxSize
) {
737 DEBUG ((EFI_D_ERROR
, "CheckCrc32: Size > MaxSize\n"));
741 // clear old crc from header
746 Status
= gBS
->CalculateCrc32 ((UINT8
*) Hdr
, Size
, &Crc
);
747 if (EFI_ERROR (Status
)) {
748 DEBUG ((EFI_D_ERROR
, "CheckCrc32: Crc calculation failed\n"));
761 DEBUG ((EFI_D_ERROR
, "CheckCrc32: Crc check failed\n"));
765 return (BOOLEAN
) (OrgCrc
== Crc
);