Subject: bug#15356: [PATCH 15/19] libparted: Add hfs_esp partition flag to GPT.

From: "Brian C. Lane" <[email-masked]>
Date: Wed, 11 Sep 2013 12:25:05 -0700

From: "Brian C. Lane" <[email-masked]>

Mac uses a HFS+ formatted partition for ESP. When dual booting with
Linux we need a way to differentiate between the Mac partition and the
one created by Linux which contains just enough stuff to fake OSX into
displaying a nice logo and allowing it to be selected for boot. I
generated a GUID (47CB5633-7E3E-408B-B7B8-2D915B7B21B1) and added a new
flag to control it.

* NEWS (Changes in behavior): Mention it.
* doc/C/parted.8: Document hfs_esp flag.
* doc/parted.texti: Document hfs_esp flag.
* include/parted/disk.in.h (_PedPartitionFlag): Add PED_PARTITION_HFS_ESP flag
* libparted/disk.c (ped_partition_flag_get_name): Add hfs_esp flag
* libparted/labels/gpt.c: Add PARTITION_HFS_ESP_GUID
(GPTPartitionData): Add hfs_esp flag
(_parse_part_entry): Likewise
(gpt_partition_new): Likewise
(gpt_partition_set_system): Set PARTITION_HFS_ESP_GUID
(gpt_partition_set_flag): Add hfs_esp and PED_PARTITION_HFS_ESP
(gpt_partition_get_flag): Likewise
(gpt_partition_is_flag_available): Likewise
---
 NEWS                     |  8 ++++++++
 doc/C/parted.8           |  2 +-
 doc/parted.texi          |  4 ++++
 include/parted/disk.in.h |  5 +++--
 libparted/disk.c         |  2 ++
 libparted/labels/gpt.c   | 41 +++++++++++++++++++++++++++++++++++++++++
 6 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/NEWS b/NEWS
index 541ea1c..026e897 100644
--- a/NEWS
+++ b/NEWS
@@ -25,6 +25,14 @@ GNU parted NEWS                                    -*- outline -*-
 
 ** Changes in behavior
 
+  Added new GPT partition type flag, hfs_esp, that sets the GUID to
+  47CB5633-7E3E-408B-B7B8-2D915B7B21B1 so that you can distinguish
+  between OSX's native HFS+ partition and one used Linux on UEFI. The
+  way Mac handles UEFI is unique, it only uses the standard ESP for
+  firmware updates and needs a HFS+ formatted partition for UEFI. This
+  GUID will allow dual booting Linux systems to determine the correct
+  partition to use when upgrading or re-installing systems.
+
   Added new partition type flag, esp, to set the type to 0xEF on MS-DOS.
   Also aliased to boot on GPT to set the UEFI ESP GUID.
 
diff --git a/doc/C/parted.8 b/doc/C/parted.8
index f8e6a3d..ccbfd18 100644
--- a/doc/C/parted.8
+++ b/doc/C/parted.8
@@ -104,7 +104,7 @@ or an LVM logical volume if necessary.
 .B set \fIpartition\fP \fIflag\fP \fIstate\fP
 Change the state of the \fIflag\fP on \fIpartition\fP to \fIstate\fP.
 Supported flags are: "boot", "root", "swap", "hidden", "raid", "lvm", "lba",
-"legacy_boot", "irst", "esp" and "palo".
+"legacy_boot", "irst", "esp", "hfs_esp" and "palo".
 \fIstate\fP should be either "on" or "off".
 .TP
 .B unit \fIunit\fP
diff --git a/doc/parted.texi b/doc/parted.texi
index a5effd5..ee5b3f7 100644
--- a/doc/parted.texi
+++ b/doc/parted.texi
@@ -844,6 +844,10 @@ partition.
 (MS-DOS, GPT) - this flag identifies a UEFI System Partition. On GPT
 it is an alias for boot.
 
+@item hfs_esp
+(GPT) - this flag identifies a special UEFI HFS+ ESP for use with Mac
+hardware.
+
 @item lba
 (MS-DOS) - this flag can be enabled to tell MS DOS, MS Windows 9x and
 MS Windows ME based operating systems to use Linear (LBA) mode.
diff --git a/include/parted/disk.in.h b/include/parted/disk.in.h
index d144e21..c34c294 100644
--- a/include/parted/disk.in.h
+++ b/include/parted/disk.in.h
@@ -75,10 +75,11 @@ enum _PedPartitionFlag {
         PED_PARTITION_LEGACY_BOOT=15,
         PED_PARTITION_MSFT_DATA=16,
         PED_PARTITION_IRST=17,
-        PED_PARTITION_ESP=18
+        PED_PARTITION_ESP=18,
+        PED_PARTITION_HFS_ESP=19
 };
 #define PED_PARTITION_FIRST_FLAG        PED_PARTITION_BOOT
-#define PED_PARTITION_LAST_FLAG         PED_PARTITION_ESP
+#define PED_PARTITION_LAST_FLAG         PED_PARTITION_HFS_ESP
 
 enum _PedDiskTypeFeature {
         PED_DISK_TYPE_EXTENDED=1,       /**< supports extended partitions */
diff --git a/libparted/disk.c b/libparted/disk.c
index c22b2a2..7d24f74 100644
--- a/libparted/disk.c
+++ b/libparted/disk.c
@@ -2445,6 +2445,8 @@ ped_partition_flag_get_name (PedPartitionFlag flag)
                 return N_("irst");
         case PED_PARTITION_ESP:
                 return N_("esp");
+        case PED_PARTITION_HFS_ESP:
+                return N_("hfs_esp");
 
 	default:
 		ped_exception_throw (
diff --git a/libparted/labels/gpt.c b/libparted/labels/gpt.c
index 64b92b4..c72573c 100644
--- a/libparted/labels/gpt.c
+++ b/libparted/labels/gpt.c
@@ -150,6 +150,10 @@ typedef struct
     ((efi_guid_t) { PED_CPU_TO_LE32 (0xD3BFE2DE), PED_CPU_TO_LE16 (0x3DAF), \
                     PED_CPU_TO_LE16 (0x11DF), 0xba, 0x40, \
                     { 0xE3, 0xA5, 0x56, 0xD8, 0x95, 0x93 }})
+#define PARTITION_HFS_ESP_GUID \
+    ((efi_guid_t) { PED_CPU_TO_LE32 (0x47CB5633), PED_CPU_TO_LE16 (0x7E3E), \
+                    PED_CPU_TO_LE16 (0x408B), 0xB7, 0xB8, \
+                    { 0x2D, 0x91, 0x5B, 0x7B, 0x21, 0xB1 }})
 
 struct __attribute__ ((packed)) _GuidPartitionTableHeader_t
 {
@@ -293,6 +297,7 @@ typedef struct _GPTPartitionData
   int msftrecv;
   int legacy_boot;
   int irst;
+  int hfs_esp;
 } GPTPartitionData;
 
 static PedDiskType gpt_disk_type;
@@ -803,6 +808,7 @@ _parse_part_entry (PedDisk *disk, GuidPartitionEntry_t *pte)
     = gpt_part_data->msftrecv
     = gpt_part_data->legacy_boot
     = gpt_part_data->irst
+    = gpt_part_data->hfs_esp
     = gpt_part_data->bios_grub = gpt_part_data->atvrecv = 0;
 
   if (pte->Attributes.RequiredToFunction & 0x1)
@@ -830,6 +836,8 @@ _parse_part_entry (PedDisk *disk, GuidPartitionEntry_t *pte)
     gpt_part_data->atvrecv = 1;
   else if (!guid_cmp (gpt_part_data->type, PARTITION_IRST_GUID))
     gpt_part_data->irst = 1;
+  else if (!guid_cmp (gpt_part_data->type, PARTITION_HFS_ESP_GUID))
+    gpt_part_data->hfs_esp = 1;
 
   return part;
 }
@@ -1348,6 +1356,7 @@ gpt_partition_new (const PedDisk *disk,
   gpt_part_data->atvrecv = 0;
   gpt_part_data->legacy_boot = 0;
   gpt_part_data->irst = 0;
+  gpt_part_data->hfs_esp = 0;
   uuid_generate ((unsigned char *) &gpt_part_data->uuid);
   swap_uuid_and_efi_guid ((unsigned char *) (&gpt_part_data->uuid));
   memset (gpt_part_data->name, 0, sizeof gpt_part_data->name);
@@ -1461,6 +1470,11 @@ gpt_partition_set_system (PedPartition *part,
       gpt_part_data->type = PARTITION_IRST_GUID;
       return 1;
     }
+  if (gpt_part_data->hfs_esp)
+    {
+      gpt_part_data->type = PARTITION_HFS_ESP_GUID;
+      return 1;
+    }
 
   if (fs_type)
     {
@@ -1604,6 +1618,7 @@ gpt_partition_set_flag (PedPartition *part, PedPartitionFlag flag, int state)
           = gpt_part_data->msftdata
           = gpt_part_data->msftrecv
           = gpt_part_data->irst
+          = gpt_part_data->hfs_esp
           = gpt_part_data->atvrecv = 0;
       return gpt_partition_set_system (part, part->fs_type);
     case PED_PARTITION_BIOS_GRUB:
@@ -1617,6 +1632,7 @@ gpt_partition_set_flag (PedPartition *part, PedPartitionFlag flag, int state)
           = gpt_part_data->msftdata
           = gpt_part_data->msftrecv
           = gpt_part_data->irst
+          = gpt_part_data->hfs_esp
           = gpt_part_data->atvrecv = 0;
       return gpt_partition_set_system (part, part->fs_type);
     case PED_PARTITION_RAID:
@@ -1630,6 +1646,7 @@ gpt_partition_set_flag (PedPartition *part, PedPartitionFlag flag, int state)
           = gpt_part_data->msftdata
           = gpt_part_data->msftrecv
           = gpt_part_data->irst
+          = gpt_part_data->hfs_esp
           = gpt_part_data->atvrecv = 0;
       return gpt_partition_set_system (part, part->fs_type);
     case PED_PARTITION_LVM:
@@ -1643,6 +1660,7 @@ gpt_partition_set_flag (PedPartition *part, PedPartitionFlag flag, int state)
           = gpt_part_data->msftdata
           = gpt_part_data->msftrecv
           = gpt_part_data->irst
+          = gpt_part_data->hfs_esp
           = gpt_part_data->atvrecv = 0;
       return gpt_partition_set_system (part, part->fs_type);
     case PED_PARTITION_HPSERVICE:
@@ -1656,6 +1674,7 @@ gpt_partition_set_flag (PedPartition *part, PedPartitionFlag flag, int state)
           = gpt_part_data->msftdata
           = gpt_part_data->msftrecv
           = gpt_part_data->irst
+          = gpt_part_data->hfs_esp
           = gpt_part_data->atvrecv = 0;
       return gpt_partition_set_system (part, part->fs_type);
     case PED_PARTITION_MSFT_RESERVED:
@@ -1669,6 +1688,7 @@ gpt_partition_set_flag (PedPartition *part, PedPartitionFlag flag, int state)
           = gpt_part_data->msftdata
           = gpt_part_data->msftrecv
           = gpt_part_data->irst
+          = gpt_part_data->hfs_esp
           = gpt_part_data->atvrecv = 0;
       return gpt_partition_set_system (part, part->fs_type);
     case PED_PARTITION_MSFT_DATA:
@@ -1682,6 +1702,7 @@ gpt_partition_set_flag (PedPartition *part, PedPartitionFlag flag, int state)
           = gpt_part_data->msftres
           = gpt_part_data->msftrecv
           = gpt_part_data->irst
+          = gpt_part_data->hfs_esp
           = gpt_part_data->atvrecv = 0;
         gpt_part_data->msftdata = 1;
       } else {
@@ -1699,6 +1720,7 @@ gpt_partition_set_flag (PedPartition *part, PedPartitionFlag flag, int state)
           = gpt_part_data->msftdata
           = gpt_part_data->msftres
           = gpt_part_data->irst
+          = gpt_part_data->hfs_esp
           = gpt_part_data->atvrecv = 0;
       return gpt_partition_set_system (part, part->fs_type);
     case PED_PARTITION_APPLE_TV_RECOVERY:
@@ -1712,6 +1734,7 @@ gpt_partition_set_flag (PedPartition *part, PedPartitionFlag flag, int state)
           = gpt_part_data->msftres
           = gpt_part_data->msftdata
           = gpt_part_data->irst
+          = gpt_part_data->hfs_esp
           = gpt_part_data->msftrecv = 0;
       return gpt_partition_set_system (part, part->fs_type);
     case PED_PARTITION_IRST:
@@ -1725,6 +1748,21 @@ gpt_partition_set_flag (PedPartition *part, PedPartitionFlag flag, int state)
           = gpt_part_data->msftres
           = gpt_part_data->msftdata
           = gpt_part_data->msftrecv
+          = gpt_part_data->hfs_esp
+          = gpt_part_data->atvrecv = 0;
+      return gpt_partition_set_system (part, part->fs_type);
+    case PED_PARTITION_HFS_ESP:
+      gpt_part_data->hfs_esp = state;
+      if (state)
+        gpt_part_data->boot
+          = gpt_part_data->raid
+          = gpt_part_data->lvm
+          = gpt_part_data->bios_grub
+          = gpt_part_data->hp_service
+          = gpt_part_data->msftres
+          = gpt_part_data->msftdata
+          = gpt_part_data->msftrecv
+          = gpt_part_data->irst
           = gpt_part_data->atvrecv = 0;
       return gpt_partition_set_system (part, part->fs_type);
     case PED_PARTITION_HIDDEN:
@@ -1776,6 +1814,8 @@ gpt_partition_get_flag (const PedPartition *part, PedPartitionFlag flag)
       return gpt_part_data->legacy_boot;
     case PED_PARTITION_IRST:
       return gpt_part_data->irst;
+    case PED_PARTITION_HFS_ESP:
+      return gpt_part_data->hfs_esp;
     case PED_PARTITION_SWAP:
     case PED_PARTITION_LBA:
     case PED_PARTITION_ROOT:
@@ -1804,6 +1844,7 @@ gpt_partition_is_flag_available (const PedPartition *part,
     case PED_PARTITION_LEGACY_BOOT:
     case PED_PARTITION_IRST:
     case PED_PARTITION_ESP:
+    case PED_PARTITION_HFS_ESP:
       return 1;
     case PED_PARTITION_SWAP:
     case PED_PARTITION_ROOT:
-- 
1.8.3.1