2 Routines supporting partition discovery and
5 Copyright (c) 2019 Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License which accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <IndustryStandard/Mbr.h>
18 #include <Uefi/UefiGpt.h>
19 #include <Library/BaseLib.h>
20 #include "FatLitePeim.h"
23 // Assumption: 'a' and 'blocksize' are all UINT32 or UINT64.
24 // If 'a' and 'blocksize' are not the same type, should use DivU64xU32 to calculate.
26 #define EFI_SIZE_TO_BLOCKS(a, blocksize) (((a) / (blocksize)) + (((a) % (blocksize)) ? 1 : 0))
29 // GPT Partition Entry Status
35 } EFI_PARTITION_ENTRY_STATUS
;
38 Check if the CRC field in the Partition table header is valid.
40 @param[in] PartHeader Partition table header structure
42 @retval TRUE the CRC is valid
43 @retval FALSE the CRC is invalid
47 PartitionCheckGptHeaderCRC (
48 IN EFI_PARTITION_TABLE_HEADER
*PartHeader
54 GptHdrCrc
= PartHeader
->Header
.CRC32
;
57 // Set CRC field to zero when doing calcuation
59 PartHeader
->Header
.CRC32
= 0;
61 Crc
= CalculateCrc32 (PartHeader
, PartHeader
->Header
.HeaderSize
);
66 PartHeader
->Header
.CRC32
= GptHdrCrc
;
68 return (GptHdrCrc
== Crc
);
73 Check if the CRC field in the Partition table header is valid
74 for Partition entry array.
76 @param[in] PartHeader Partition table header structure
77 @param[in] PartEntry The partition entry array
79 @retval TRUE the CRC is valid
80 @retval FALSE the CRC is invalid
84 PartitionCheckGptEntryArrayCRC (
85 IN EFI_PARTITION_TABLE_HEADER
*PartHeader
,
86 IN EFI_PARTITION_ENTRY
*PartEntry
92 Size
= (UINTN
)MultU64x32(PartHeader
->NumberOfPartitionEntries
, PartHeader
->SizeOfPartitionEntry
);
93 Crc
= CalculateCrc32 (PartEntry
, Size
);
95 return (BOOLEAN
) (PartHeader
->PartitionEntryArrayCRC32
== Crc
);
99 The function is used for valid GPT table. Both for Primary and Backup GPT header.
101 @param[in] PrivateData The global memory map
102 @param[in] ParentBlockDevNo The parent block device
103 @param[in] IsPrimaryHeader Indicate to which header will be checked.
104 @param[in] PartHdr Stores the partition table that is read
106 @retval TRUE The partition table is valid
107 @retval FALSE The partition table is not valid
111 PartitionCheckGptHeader (
112 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
113 IN UINTN ParentBlockDevNo
,
114 IN BOOLEAN IsPrimaryHeader
,
115 IN EFI_PARTITION_TABLE_HEADER
*PartHdr
118 PEI_FAT_BLOCK_DEVICE
*ParentBlockDev
;
120 EFI_PEI_LBA AlternateLba
;
121 EFI_PEI_LBA EntryArrayLastLba
;
123 UINT64 PartitionEntryArraySize
;
124 UINT64 PartitionEntryBlockNumb
;
125 UINT32 EntryArraySizeRemainder
;
127 ParentBlockDev
= &(PrivateData
->BlockDevice
[ParentBlockDevNo
]);
129 if (IsPrimaryHeader
) {
130 Lba
= PRIMARY_PART_HEADER_LBA
;
131 AlternateLba
= ParentBlockDev
->LastBlock
;
133 Lba
= ParentBlockDev
->LastBlock
;
134 AlternateLba
= PRIMARY_PART_HEADER_LBA
;
137 if ( (PartHdr
->Header
.Signature
!= EFI_PTAB_HEADER_ID
) ||
138 (PartHdr
->Header
.Revision
!= 0x00010000) ||
139 (PartHdr
->Header
.HeaderSize
< 92) ||
140 (PartHdr
->Header
.HeaderSize
> ParentBlockDev
->BlockSize
) ||
141 (!PartitionCheckGptHeaderCRC (PartHdr
)) ||
142 (PartHdr
->Header
.Reserved
!= 0)
144 DEBUG ((DEBUG_ERROR
, "Invalid efi partition table header\n"));
149 // | Block0 | Block1 |Block2 ~ FirstUsableLBA - 1|FirstUsableLBA, ... ,LastUsableLBA|LastUsableLBA+1 ~ LastBlock-1| LastBlock |
150 // |Protective MBR|Primary Header|Entry Array(At Least 16384)| Partition | Entry Array(At Least 16384) |BackUp Header|
152 // 1. Protective MBR is fixed at Block 0.
153 // 2. Primary Header is fixed at Block 1.
154 // 3. Backup Header is fixed at LastBlock.
155 // 4. Must be remain 128*128 bytes for primary entry array.
156 // 5. Must be remain 128*128 bytes for backup entry array.
157 // 6. SizeOfPartitionEntry must be equals to 128 * 2^n.
159 if ( (PartHdr
->MyLBA
!= Lba
) ||
160 (PartHdr
->AlternateLBA
!= AlternateLba
) ||
161 (PartHdr
->FirstUsableLBA
< 2 + EFI_SIZE_TO_BLOCKS (EFI_GPT_PART_ENTRY_MIN_SIZE
, ParentBlockDev
->BlockSize
)) ||
162 (PartHdr
->LastUsableLBA
> ParentBlockDev
->LastBlock
- 1 - EFI_SIZE_TO_BLOCKS (EFI_GPT_PART_ENTRY_MIN_SIZE
, ParentBlockDev
->BlockSize
)) ||
163 (PartHdr
->FirstUsableLBA
> PartHdr
->LastUsableLBA
) ||
164 (PartHdr
->PartitionEntryLBA
< 2) ||
165 (PartHdr
->PartitionEntryLBA
> ParentBlockDev
->LastBlock
- 1) ||
166 (PartHdr
->PartitionEntryLBA
>= PartHdr
->FirstUsableLBA
&& PartHdr
->PartitionEntryLBA
<= PartHdr
->LastUsableLBA
) ||
167 (PartHdr
->SizeOfPartitionEntry
%128 != 0) ||
168 (PartHdr
->SizeOfPartitionEntry
!= sizeof (EFI_PARTITION_ENTRY
))
170 DEBUG ((DEBUG_ERROR
, "Invalid efi partition table header\n"));
175 // Ensure the NumberOfPartitionEntries * SizeOfPartitionEntry doesn't overflow.
177 if (PartHdr
->NumberOfPartitionEntries
> DivU64x32 (MAX_UINTN
, PartHdr
->SizeOfPartitionEntry
)) {
178 DEBUG ((DEBUG_ERROR
, "Memory overflow in GPT Entry Array\n"));
182 PartitionEntryArraySize
= MultU64x32 (PartHdr
->NumberOfPartitionEntries
, PartHdr
->SizeOfPartitionEntry
);
183 EntryArraySizeRemainder
= 0;
184 PartitionEntryBlockNumb
= DivU64x32Remainder (PartitionEntryArraySize
, ParentBlockDev
->BlockSize
, &EntryArraySizeRemainder
);
185 if (EntryArraySizeRemainder
!= 0) {
186 PartitionEntryBlockNumb
++;
189 if (IsPrimaryHeader
) {
190 EntryArrayLastLba
= PartHdr
->FirstUsableLBA
;
192 EntryArrayLastLba
= ParentBlockDev
->LastBlock
;
196 // Make sure partition entry array not overlaps with partition area or the LastBlock.
198 if (PartHdr
->PartitionEntryLBA
+ PartitionEntryBlockNumb
> EntryArrayLastLba
) {
199 DEBUG ((DEBUG_ERROR
, "GPT Partition Entry Array Error!\n"));
200 DEBUG ((DEBUG_ERROR
, "PartitionEntryArraySize = %lu.\n", PartitionEntryArraySize
));
201 DEBUG ((DEBUG_ERROR
, "PartitionEntryLBA = %lu.\n", PartHdr
->PartitionEntryLBA
));
202 DEBUG ((DEBUG_ERROR
, "PartitionEntryBlockNumb = %lu.\n", PartitionEntryBlockNumb
));
203 DEBUG ((DEBUG_ERROR
, "EntryArrayLastLba = %lu.\n", EntryArrayLastLba
));
211 This function is used to verify each partition in block device.
213 @param[in] PrivateData The global memory map
214 @param[in] ParentBlockDevNo The parent block device
215 @param[in] PartHdr Stores the partition table that is read
217 @retval TRUE The partition is valid
218 @retval FALSE The partition is not valid
222 PartitionCheckGptEntryArray (
223 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
224 IN UINTN ParentBlockDevNo
,
225 IN EFI_PARTITION_TABLE_HEADER
*PartHdr
229 PEI_FAT_BLOCK_DEVICE
*ParentBlockDev
;
230 PEI_FAT_BLOCK_DEVICE
*BlockDevPtr
;
232 UINT64 PartitionEntryArraySize
;
233 UINT64 PartitionEntryBlockNumb
;
234 UINT32 EntryArraySizeRemainder
;
236 EFI_PARTITION_ENTRY
*PartitionEntryBuffer
;
237 EFI_PARTITION_ENTRY_STATUS
*PartitionEntryStatus
;
245 EFI_PARTITION_ENTRY
*Entry
;
247 PartitionEntryBuffer
= NULL
;
248 PartitionEntryStatus
= NULL
;
250 ParentBlockDev
= &(PrivateData
->BlockDevice
[ParentBlockDevNo
]);
253 PartitionEntryArraySize
= MultU64x32 (PartHdr
->NumberOfPartitionEntries
, PartHdr
->SizeOfPartitionEntry
);
254 EntryArraySizeRemainder
= 0;
255 PartitionEntryBlockNumb
= DivU64x32Remainder (PartitionEntryArraySize
, ParentBlockDev
->BlockSize
, &EntryArraySizeRemainder
);
256 if (EntryArraySizeRemainder
!= 0) {
257 PartitionEntryBlockNumb
++;
259 PartitionEntryArraySize
= MultU64x32 (PartitionEntryBlockNumb
, ParentBlockDev
->BlockSize
);
261 PartitionEntryBuffer
= (EFI_PARTITION_ENTRY
*) AllocatePages (EFI_SIZE_TO_PAGES ((UINTN
)PartitionEntryArraySize
));
262 if (PartitionEntryBuffer
== NULL
) {
263 DEBUG ((DEBUG_ERROR
, "Allocate memory error!\n"));
267 PartitionEntryStatus
= (EFI_PARTITION_ENTRY_STATUS
*) AllocatePages (EFI_SIZE_TO_PAGES (PartHdr
->NumberOfPartitionEntries
* sizeof (EFI_PARTITION_ENTRY_STATUS
)));
268 if (PartitionEntryStatus
== NULL
) {
269 DEBUG ((DEBUG_ERROR
, "Allocate memory error!\n"));
272 ZeroMem (PartitionEntryStatus
, PartHdr
->NumberOfPartitionEntries
* sizeof (EFI_PARTITION_ENTRY_STATUS
));
274 Status
= FatReadBlock (
277 PartHdr
->PartitionEntryLBA
,
278 (UINTN
)PartitionEntryArraySize
,
281 if (EFI_ERROR (Status
)) {
282 DEBUG ((DEBUG_ERROR
, "Read partition entry array error!\n"));
286 if (!PartitionCheckGptEntryArrayCRC (PartHdr
, PartitionEntryBuffer
)) {
287 DEBUG ((DEBUG_ERROR
, "Partition entries CRC check fail\n"));
291 for (Index1
= 0; Index1
< PartHdr
->NumberOfPartitionEntries
; Index1
++) {
292 Entry
= (EFI_PARTITION_ENTRY
*) ((UINT8
*) PartitionEntryBuffer
+ Index1
* PartHdr
->SizeOfPartitionEntry
);
293 if (CompareGuid (&Entry
->PartitionTypeGUID
, &gEfiPartTypeUnusedGuid
)) {
297 StartingLBA
= Entry
->StartingLBA
;
298 EndingLBA
= Entry
->EndingLBA
;
299 if (StartingLBA
> EndingLBA
||
300 StartingLBA
< PartHdr
->FirstUsableLBA
||
301 StartingLBA
> PartHdr
->LastUsableLBA
||
302 EndingLBA
< PartHdr
->FirstUsableLBA
||
303 EndingLBA
> PartHdr
->LastUsableLBA
305 PartitionEntryStatus
[Index1
].OutOfRange
= TRUE
;
309 if ((Entry
->Attributes
& BIT1
) != 0) {
311 // If Bit 1 is set, this indicate that this is an OS specific GUID partition.
313 PartitionEntryStatus
[Index1
].OsSpecific
= TRUE
;
316 for (Index2
= Index1
+ 1; Index2
< PartHdr
->NumberOfPartitionEntries
; Index2
++) {
317 Entry
= (EFI_PARTITION_ENTRY
*) ((UINT8
*) PartitionEntryBuffer
+ Index2
* PartHdr
->SizeOfPartitionEntry
);
318 if (CompareGuid (&Entry
->PartitionTypeGUID
, &gEfiPartTypeUnusedGuid
)) {
322 if (Entry
->EndingLBA
>= StartingLBA
&& Entry
->StartingLBA
<= EndingLBA
) {
324 // This region overlaps with the Index1'th region
326 PartitionEntryStatus
[Index1
].Overlap
= TRUE
;
327 PartitionEntryStatus
[Index2
].Overlap
= TRUE
;
333 for (Index
= 0; Index
< PartHdr
->NumberOfPartitionEntries
; Index
++) {
334 if (CompareGuid (&PartitionEntryBuffer
[Index
].PartitionTypeGUID
, &gEfiPartTypeUnusedGuid
)||
335 PartitionEntryStatus
[Index
].OutOfRange
||
336 PartitionEntryStatus
[Index
].Overlap
||
337 PartitionEntryStatus
[Index
].OsSpecific
) {
339 // Don't use null EFI Partition Entries, Invalid Partition Entries or OS specific
345 if (PrivateData
->BlockDeviceCount
>= PEI_FAT_MAX_BLOCK_DEVICE
) {
350 BlockDevPtr
= &(PrivateData
->BlockDevice
[PrivateData
->BlockDeviceCount
]);
352 BlockDevPtr
->BlockSize
= ParentBlockDev
->BlockSize
;
353 BlockDevPtr
->LastBlock
= PartitionEntryBuffer
[Index
].EndingLBA
;
354 BlockDevPtr
->IoAlign
= ParentBlockDev
->IoAlign
;
355 BlockDevPtr
->Logical
= TRUE
;
356 BlockDevPtr
->PartitionChecked
= FALSE
;
357 BlockDevPtr
->StartingPos
= MultU64x32 (
358 PartitionEntryBuffer
[Index
].StartingLBA
,
359 ParentBlockDev
->BlockSize
361 BlockDevPtr
->ParentDevNo
= ParentBlockDevNo
;
363 PrivateData
->BlockDeviceCount
++;
365 DEBUG ((DEBUG_INFO
, "Find GPT Partition [0x%lx", PartitionEntryBuffer
[Index
].StartingLBA
, BlockDevPtr
->LastBlock
));
366 DEBUG ((DEBUG_INFO
, ", 0x%lx]\n", BlockDevPtr
->LastBlock
));
367 DEBUG ((DEBUG_INFO
, " BlockSize %x\n", BlockDevPtr
->BlockSize
));
371 if (PartitionEntryBuffer
!= NULL
) {
372 FreePages (PartitionEntryBuffer
, EFI_SIZE_TO_PAGES ((UINTN
)PartitionEntryArraySize
));
375 if (PartitionEntryStatus
!= NULL
) {
376 FreePages (PartitionEntryStatus
, EFI_SIZE_TO_PAGES (PartHdr
->NumberOfPartitionEntries
* sizeof (EFI_PARTITION_ENTRY_STATUS
)));
383 The function is used to check GPT structure, include GPT header and GPT entry array.
386 2. Check partition entry array.
387 3. Check each partitions.
389 @param[in] PrivateData The global memory map
390 @param[in] ParentBlockDevNo The parent block device
391 @param[in] IsPrimary Indicate primary or backup to be check
393 @retval TRUE Primary or backup GPT structure is valid.
394 @retval FALSE Both primary and backup are invalid.
398 PartitionCheckGptStructure (
399 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
400 IN UINTN ParentBlockDevNo
,
405 PEI_FAT_BLOCK_DEVICE
*ParentBlockDev
;
406 EFI_PARTITION_TABLE_HEADER
*PartHdr
;
407 EFI_PEI_LBA GptHeaderLBA
;
409 ParentBlockDev
= &(PrivateData
->BlockDevice
[ParentBlockDevNo
]);
410 PartHdr
= (EFI_PARTITION_TABLE_HEADER
*) PrivateData
->BlockData
;
413 GptHeaderLBA
= PRIMARY_PART_HEADER_LBA
;
415 GptHeaderLBA
= ParentBlockDev
->LastBlock
;
418 Status
= FatReadBlock (
422 ParentBlockDev
->BlockSize
,
425 if (EFI_ERROR (Status
)) {
429 if (!PartitionCheckGptHeader (PrivateData
, ParentBlockDevNo
, IsPrimary
, PartHdr
)) {
433 if (!PartitionCheckGptEntryArray (PrivateData
, ParentBlockDevNo
, PartHdr
)) {
441 This function is used to check protective MBR structure before checking GPT.
443 @param[in] PrivateData The global memory map
444 @param[in] ParentBlockDevNo The parent block device
446 @retval TRUE Valid protective MBR
447 @retval FALSE Invalid MBR
450 PartitionCheckProtectiveMbr (
451 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
452 IN UINTN ParentBlockDevNo
456 MASTER_BOOT_RECORD
*ProtectiveMbr
;
457 MBR_PARTITION_RECORD
*MbrPartition
;
458 PEI_FAT_BLOCK_DEVICE
*ParentBlockDev
;
461 ProtectiveMbr
= (MASTER_BOOT_RECORD
*) PrivateData
->BlockData
;
462 ParentBlockDev
= &(PrivateData
->BlockDevice
[ParentBlockDevNo
]);
465 // Read Protective MBR
467 Status
= FatReadBlock (
471 ParentBlockDev
->BlockSize
,
474 if (EFI_ERROR (Status
)) {
475 DEBUG ((DEBUG_ERROR
, "GPT Error When Read Protective Mbr From Partition!\n"));
479 if (ProtectiveMbr
->Signature
!= MBR_SIGNATURE
) {
480 DEBUG ((DEBUG_ERROR
, "Protective Mbr Signature is invalid!\n"));
485 // The partition define in UEFI Spec Table 17.
486 // Boot Code, Unique MBR Disk Signature, Unknown.
487 // These parts will not be used by UEFI, so we skip to check them.
489 for (Index
= 0; Index
< MAX_MBR_PARTITIONS
; Index
++) {
490 MbrPartition
= (MBR_PARTITION_RECORD
*)&ProtectiveMbr
->Partition
[Index
];
491 if (MbrPartition
->BootIndicator
== 0x00 &&
492 MbrPartition
->StartSector
== 0x02 &&
493 MbrPartition
->OSIndicator
== PMBR_GPT_PARTITION
&&
494 UNPACK_UINT32 (MbrPartition
->StartingLBA
) == 1
500 DEBUG ((DEBUG_ERROR
, "Protective Mbr, All Partition Entry Are Empty!\n"));
505 This function is used for finding GPT partition on block device.
506 As follow UEFI spec we should check protective MBR first and then
507 try to check both primary/backup GPT structures.
509 @param[in] PrivateData The global memory map
510 @param[in] ParentBlockDevNo The parent block device
512 @retval TRUE New partitions are detected and logical block devices
513 are added to block device array
514 @retval FALSE No new partitions are added
518 FatFindGptPartitions (
519 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
520 IN UINTN ParentBlockDevNo
524 PEI_FAT_BLOCK_DEVICE
*ParentBlockDev
;
526 if (ParentBlockDevNo
> PEI_FAT_MAX_BLOCK_DEVICE
- 1) {
530 ParentBlockDev
= &(PrivateData
->BlockDevice
[ParentBlockDevNo
]);
531 if (ParentBlockDev
->BlockSize
> PEI_FAT_MAX_BLOCK_SIZE
) {
532 DEBUG ((DEBUG_ERROR
, "Device BlockSize %x exceed FAT_MAX_BLOCK_SIZE\n", ParentBlockDev
->BlockSize
));
536 if (!PartitionCheckProtectiveMbr (PrivateData
, ParentBlockDevNo
)) {
540 Found
= PartitionCheckGptStructure (PrivateData
, ParentBlockDevNo
, TRUE
);
542 DEBUG ((DEBUG_ERROR
, "Primary GPT Header Error, Try to Check Backup GPT Header!\n"));
543 Found
= PartitionCheckGptStructure (PrivateData
, ParentBlockDevNo
, FALSE
);
547 ParentBlockDev
->PartitionChecked
= TRUE
;