Skip to content

Commit b05ebbb

Browse files
Davidlohr Buesotorvalds
authored andcommitted
partitions/efi: detect hybrid MBRs
One of the biggest problems with GPT is compatibility with older, non-GPT systems. The problem is addressed by creating hybrid mbrs, an extension, or variant, of the traditional protective mbr. This contains, apart from the 0xEE partition, up three additional primary partitions that point to the same space marked by up to three GPT partitions. The result is that legacy OSs can see the three required MBR partitions and at the same time ignore the GPT-aware partitions that protect the GPT structures. While hybrid MBRs are hacks, workarounds and simply not part of the GPT standard, they do exist and we have no way around them. For instance, by default, OSX creates a hybrid scheme when using multi-OS booting. In order for Linux to properly discover protective MBRs, it must be made aware of devices that have hybrid MBRs. No functionality is changed by this patch, just a debug message informing the user of the MBR scheme that is being used. [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Davidlohr Bueso <davidlohr@hp.com> Reviewed-by: Karel Zak <kzak@redhat.com> Acked-by: Matt Fleming <matt.fleming@intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 3e69ac3 commit b05ebbb

2 files changed

Lines changed: 56 additions & 21 deletions

File tree

‎block/partitions/efi.c‎

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static inline int pmbr_part_valid(gpt_mbr_record *part)
158158
if (le32_to_cpu(part->starting_lba) != GPT_PRIMARY_PARTITION_TABLE_LBA)
159159
goto invalid;
160160

161-
return 1;
161+
return GPT_MBR_PROTECTIVE;
162162
invalid:
163163
return 0;
164164
}
@@ -167,21 +167,48 @@ static inline int pmbr_part_valid(gpt_mbr_record *part)
167167
* is_pmbr_valid(): test Protective MBR for validity
168168
* @mbr: pointer to a legacy mbr structure
169169
*
170-
* Description: Returns 1 if PMBR is valid, 0 otherwise.
171-
* Validity depends on two things:
170+
* Description: Checks for a valid protective or hybrid
171+
* master boot record (MBR). The validity of a pMBR depends
172+
* on all of the following properties:
172173
* 1) MSDOS signature is in the last two bytes of the MBR
173174
* 2) One partition of type 0xEE is found
175+
*
176+
* In addition, a hybrid MBR will have up to three additional
177+
* primary partitions, which point to the same space that's
178+
* marked out by up to three GPT partitions.
179+
*
180+
* Returns 0 upon invalid MBR, or GPT_MBR_PROTECTIVE or
181+
* GPT_MBR_HYBRID depending on the device layout.
174182
*/
175-
static int
176-
is_pmbr_valid(legacy_mbr *mbr)
183+
static int is_pmbr_valid(legacy_mbr *mbr)
177184
{
178-
int i;
185+
int i, ret = 0; /* invalid by default */
186+
179187
if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
180-
return 0;
188+
goto done;
189+
190+
for (i = 0; i < 4; i++) {
191+
ret = pmbr_part_valid(&mbr->partition_record[i]);
192+
if (ret == GPT_MBR_PROTECTIVE) {
193+
/*
194+
* Ok, we at least know that there's a protective MBR,
195+
* now check if there are other partition types for
196+
* hybrid MBR.
197+
*/
198+
goto check_hybrid;
199+
}
200+
}
201+
202+
if (ret != GPT_MBR_PROTECTIVE)
203+
goto done;
204+
check_hybrid:
181205
for (i = 0; i < 4; i++)
182-
if (pmbr_part_valid(&mbr->partition_record[i]))
183-
return 1;
184-
return 0;
206+
if ((mbr->partition_record[i].os_type !=
207+
EFI_PMBR_OSTYPE_EFI_GPT) &&
208+
(mbr->partition_record[i].os_type != 0x00))
209+
ret = GPT_MBR_HYBRID;
210+
done:
211+
return ret;
185212
}
186213

187214
/**
@@ -548,17 +575,22 @@ static int find_valid_gpt(struct parsed_partitions *state, gpt_header **gpt,
548575

549576
lastlba = last_lba(state->bdev);
550577
if (!force_gpt) {
551-
/* This will be added to the EFI Spec. per Intel after v1.02. */
552-
legacymbr = kzalloc(sizeof (*legacymbr), GFP_KERNEL);
553-
if (legacymbr) {
554-
read_lba(state, 0, (u8 *) legacymbr,
555-
sizeof (*legacymbr));
556-
good_pmbr = is_pmbr_valid(legacymbr);
557-
kfree(legacymbr);
558-
}
559-
if (!good_pmbr)
560-
goto fail;
561-
}
578+
/* This will be added to the EFI Spec. per Intel after v1.02. */
579+
legacymbr = kzalloc(sizeof(*legacymbr), GFP_KERNEL);
580+
if (!legacymbr)
581+
goto fail;
582+
583+
read_lba(state, 0, (u8 *)legacymbr, sizeof(*legacymbr));
584+
good_pmbr = is_pmbr_valid(legacymbr);
585+
kfree(legacymbr);
586+
587+
if (!good_pmbr)
588+
goto fail;
589+
590+
pr_debug("Device has a %s MBR\n",
591+
good_pmbr == GPT_MBR_PROTECTIVE ?
592+
"protective" : "hybrid");
593+
}
562594

563595
good_pgpt = is_gpt_valid(state, GPT_PRIMARY_PARTITION_TABLE_LBA,
564596
&pgpt, &pptes);

‎block/partitions/efi.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#define EFI_PMBR_OSTYPE_EFI 0xEF
3838
#define EFI_PMBR_OSTYPE_EFI_GPT 0xEE
3939

40+
#define GPT_MBR_PROTECTIVE 1
41+
#define GPT_MBR_HYBRID 2
42+
4043
#define GPT_HEADER_SIGNATURE 0x5452415020494645ULL
4144
#define GPT_HEADER_REVISION_V1 0x00010000
4245
#define GPT_PRIMARY_PARTITION_TABLE_LBA 1

0 commit comments

Comments
 (0)