Skip to contentSkip to conversation

libfdisk: gpt: avoid unnecessary pmbr check

PATCH Applied Davidlohr Bueso 2 participants 6 messages Sep 22, 2013 – Sep 25, 2013 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 libfdisk: gpt: avoid unnecessary pmbr check

We only jump to the 'check_hybrid' label when a valid pmbr is detected, so we need not recheck again. Move the label's logic so it doesn't include the check.

---
 libfdisk/src/gpt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
libfdisk/src/gpt.c +2 -1
index b929b84..2a6284a 100644
@@ -503,9 +503,10 @@ static int valid_pmbr(struct fdisk_context *cxt)
 			goto check_hybrid;
 		}
 	}
-check_hybrid:
+
 	if (ret != GPT_MBR_PROTECTIVE)
 		goto done;
+check_hybrid:
 	for (i = 0 ; i < 4; i++) {
 		if ((pmbr->partition_record[i].os_type != EFI_PMBR_OSTYPE) &&
 		    (pmbr->partition_record[i].os_type != 0x00))
Signature · 1 line
1.7.11.7
#1 lore raw

PATCH 2/3 libfdisk: gpt: do not assume first pt record is always pmbr

The 0xEE (GPT protective) partition does not always have to be the first element in the array of MBR partition records.

---
 libfdisk/src/gpt.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
libfdisk/src/gpt.c +3 -2
index 2a6284a..bb9e9a7 100644
@@ -475,7 +475,7 @@ static int gpt_mknew_header(struct fdisk_context *cxt,
  */
 static int valid_pmbr(struct fdisk_context *cxt)
 {
-	int i, ret = 0; /* invalid by default */
+	int i, part = 0, ret = 0; /* invalid by default */
 	struct gpt_legacy_mbr *pmbr = NULL;
 
 	if (!cxt->firstsector)
@@ -494,6 +494,7 @@ static int valid_pmbr(struct fdisk_context *cxt)
 	/* seems like a valid MBR was found, check DOS primary partitions */
 	for (i = 0; i < 4; i++) {
 		if (pmbr->partition_record[i].os_type == EFI_PMBR_OSTYPE) {
+			part = i;
 			/*
 			 * Ok, we at least know that there's a protective MBR,
 			 * now check if there are other partition types for
@@ -520,7 +521,7 @@ check_hybrid:
 	 * Hybrid MBRs do not necessarily comply with this.
 	 */
 	if (ret == GPT_MBR_PROTECTIVE) {
-		if (le32_to_cpu(pmbr->partition_record[0].size_in_lba) !=
+		if (le32_to_cpu(pmbr->partition_record[part].size_in_lba) !=
 		    min((uint32_t) cxt->total_sectors - 1, 0xFFFFFFFF))
 			ret = 0;
 	}
Signature · 1 line
1.7.11.7
maintainer
#2 lore raw

Re: [PATCH 2/3] libfdisk: gpt: do not assume first pt record is always pmbr

Quoted 2 lines · Davidlohr Bueso

The 0xEE (GPT protective) partition does not always have to be the first element in the array of MBR partition records.

Already fixed.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#3 lore raw

PATCH 3/3 libfdisk: gpt: loosen check fot pmbr size in lba

In reply to Davidlohr Bueso

While most disk partitioning tools out there create a pMBR's size in lba to be the lesser of the whole disk or 2Tib, Microsoft apparently does not[1]. It always sets the entry to the maximum 32-bit limitation - even though a drive may be smaller than that.

Loosen this check and only verify that the size is either the whole disk or 0xFFFFFFFF. No tool in its right mind would set it to any value other than these.

[1] http://thestarman.pcministry.com/asm/mbr/GPT.htm#GPTPT

---
 libfdisk/src/gpt.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
libfdisk/src/gpt.c +6 -2
index bb9e9a7..83659cb 100644
@@ -477,6 +477,7 @@ static int valid_pmbr(struct fdisk_context *cxt)
 {
 	int i, part = 0, ret = 0; /* invalid by default */
 	struct gpt_legacy_mbr *pmbr = NULL;
+	uint32_t sz_lba = 0;
 
 	if (!cxt->firstsector)
 		goto done;
@@ -517,12 +518,15 @@ check_hybrid:
 	/*
 	 * Protective MBRs take up the lesser of the whole disk
 	 * or 2 TiB (32bit LBA), ignoring the rest of the disk.
+	 * Some partitioning programs, nonetheless, choose to set
+	 * the size to the maximum 32-bit limitation, disregarding
+	 * the disk size.
 	 *
 	 * Hybrid MBRs do not necessarily comply with this.
 	 */
 	if (ret == GPT_MBR_PROTECTIVE) {
-		if (le32_to_cpu(pmbr->partition_record[part].size_in_lba) !=
-		    min((uint32_t) cxt->total_sectors - 1, 0xFFFFFFFF))
+		sz_lba = le32_to_cpu(pmbr->partition_record[part].size_in_lba);
+		if (sz_lba != (uint32_t) cxt->total_sectors - 1 && sz_lba != 0xFFFFFFFF)
 			ret = 0;
 	}
 done:
Signature · 1 line
1.7.11.7
maintainer Applied
#4 lore raw

Re: [PATCH 3/3] libfdisk: gpt: loosen check fot pmbr size in lba

Quoted 9 lines · Davidlohr Bueso

While most disk partitioning tools out there create a pMBR's size in lba to be the lesser of the whole disk or 2Tib, Microsoft apparently does not[1]. It always sets the entry to the maximum 32-bit limitation - even though a drive may be smaller than that.

Loosen this check and only verify that the size is either the whole disk or 0xFFFFFFFF. No tool in its right mind would set it to any value other than these.

Quoted 6 lines

[1] http://thestarman.pcministry.com/asm/mbr/GPT.htm#GPTPT

---
 libfdisk/src/gpt.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Applied, thanks.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com