Subject: bug#15356: [PATCH 14/19] libparted: Add UEFI System Partition flag.

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

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

This adds support for the ESP partition type on MS-DOS. It also aliases
it to the boot flag on GPT which sets the ESP GUID type.

* NEWS (Changes in behavior): Mention it.
* doc/C/parted.8: Document esp flag.
* doc/parted.texti: Document esp flag.
* include/parted/disk.in.h (_PedPartitionFlag): Add PED_PARTITION_ESP flag
* libparted/disk.c (ped_partition_flag_get_name): Add esp flag
* libparted/labels/dos.c (DosPartitionData): Likewise
(raw_part_parse): Likewise
(msdos_partition_new): Likewise
(msdos_partition_duplicate): Likewise
(msdos_partition_set_system): Likewise
(clear_flags): Likewise
(msdos_partition_set_flag): Likewise
(msdos_partition_get_flag): Likewise
(msdos_partition_is_flag_available): Likewise
* libparted/labels/gpt.c (gpt_partition_set_flag): Add PED_PARTITION_ESP
(gpt_partition_get_flag): Likewise
(gpt_partition_is_flag_available): Likewise
---
 NEWS                     |  3 +++
 doc/C/parted.8           |  2 +-
 doc/parted.texi          |  4 ++++
 include/parted/disk.in.h |  5 +++--
 libparted/disk.c         |  2 ++
 libparted/labels/dos.c   | 21 +++++++++++++++++++++
 libparted/labels/gpt.c   |  3 +++
 7 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/NEWS b/NEWS
index b53a9d0..541ea1c 100644
--- a/NEWS
+++ b/NEWS
@@ -25,6 +25,9 @@ GNU parted NEWS                                    -*- outline -*-
 
 ** Changes in behavior
 
+  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.
+
   Added new partition type flag, irst, for use with Intel Rapid Start
   Technology. On MS-DOS disk labels it sets the type to 0x84 and on GPT
   it sets the GUID to D3BFE2DE-3DAF-11DF-BA-40-E3A556D89593.
diff --git a/doc/C/parted.8 b/doc/C/parted.8
index 2f8e9f5..f8e6a3d 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" and "palo".
+"legacy_boot", "irst", "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 fd8ccb9..a5effd5 100644
--- a/doc/parted.texi
+++ b/doc/parted.texi
@@ -840,6 +840,10 @@ filesystems).
 (MS-DOS, GPT) - this flag identifies an Intel Rapid Start Technology
 partition.
 
+@item esp
+(MS-DOS, GPT) - this flag identifies a UEFI System Partition. On GPT
+it is an alias for boot.
+
 @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 585383d..d144e21 100644
--- a/include/parted/disk.in.h
+++ b/include/parted/disk.in.h
@@ -74,10 +74,11 @@ enum _PedPartitionFlag {
         PED_PARTITION_DIAG=14,
         PED_PARTITION_LEGACY_BOOT=15,
         PED_PARTITION_MSFT_DATA=16,
-        PED_PARTITION_IRST=17
+        PED_PARTITION_IRST=17,
+        PED_PARTITION_ESP=18
 };
 #define PED_PARTITION_FIRST_FLAG        PED_PARTITION_BOOT
-#define PED_PARTITION_LAST_FLAG         PED_PARTITION_IRST
+#define PED_PARTITION_LAST_FLAG         PED_PARTITION_ESP
 
 enum _PedDiskTypeFeature {
         PED_DISK_TYPE_EXTENDED=1,       /**< supports extended partitions */
diff --git a/libparted/disk.c b/libparted/disk.c
index 4ca7255..c22b2a2 100644
--- a/libparted/disk.c
+++ b/libparted/disk.c
@@ -2443,6 +2443,8 @@ ped_partition_flag_get_name (PedPartitionFlag flag)
                 return N_("legacy_boot");
         case PED_PARTITION_IRST:
                 return N_("irst");
+        case PED_PARTITION_ESP:
+                return N_("esp");
 
 	default:
 		ped_exception_throw (
diff --git a/libparted/labels/dos.c b/libparted/labels/dos.c
index f8591ff..fc7292a 100644
--- a/libparted/labels/dos.c
+++ b/libparted/labels/dos.c
@@ -92,6 +92,7 @@ static const char MBR_BOOT_CODE[] = {
 #define PARTITION_SUN_UFS	0xbf
 #define PARTITION_DELL_DIAG	0xde
 #define PARTITION_GPT		0xee
+#define PARTITION_ESP		0xef
 #define PARTITION_PALO		0xf0
 #define PARTITION_PREP		0x41
 #define PARTITION_LINUX_RAID	0xfd
@@ -161,6 +162,7 @@ typedef struct {
 	int		prep;
 	int		diag;
 	int		irst;
+	int		esp;
 	OrigState*	orig;			/* used for CHS stuff */
 } DosPartitionData;
 
@@ -927,6 +929,7 @@ raw_part_parse (const PedDisk* disk, const DosRawPartition* raw_part,
 	dos_data->palo = raw_part->type == PARTITION_PALO;
 	dos_data->prep = raw_part->type == PARTITION_PREP;
 	dos_data->irst = raw_part->type == PARTITION_IRST;
+	dos_data->esp = raw_part->type == PARTITION_ESP;
 	dos_data->orig = ped_malloc (sizeof (OrigState));
 	if (!dos_data->orig) {
 		ped_partition_destroy (part);
@@ -1330,6 +1333,7 @@ msdos_partition_new (const PedDisk* disk, PedPartitionType part_type,
 		dos_data->palo = 0;
 		dos_data->prep = 0;
 		dos_data->irst = 0;
+		dos_data->esp = 0;
 	} else {
 		part->disk_specific = NULL;
 	}
@@ -1366,6 +1370,7 @@ msdos_partition_duplicate (const PedPartition* part)
 	new_dos_data->palo = old_dos_data->palo;
 	new_dos_data->prep = old_dos_data->prep;
 	new_dos_data->irst = old_dos_data->irst;
+	new_dos_data->esp = old_dos_data->esp;
 
 	if (old_dos_data->orig) {
 		new_dos_data->orig = ped_malloc (sizeof (OrigState));
@@ -1415,6 +1420,7 @@ msdos_partition_set_system (PedPartition* part,
 		dos_data->palo = 0;
 		dos_data->prep = 0;
 		dos_data->irst = 0;
+		dos_data->esp = 0;
 		if (dos_data->lba)
 			dos_data->system = PARTITION_EXT_LBA;
 		else
@@ -1451,6 +1457,10 @@ msdos_partition_set_system (PedPartition* part,
 		dos_data->system = PARTITION_IRST;
 		return 1;
 	}
+	if (dos_data->esp) {
+		dos_data->system = PARTITION_ESP;
+		return 1;
+	}
 
 	if (!fs_type)
 		dos_data->system = PARTITION_LINUX;
@@ -1488,6 +1498,7 @@ clear_flags (DosPartitionData *dos_data)
   dos_data->palo = 0;
   dos_data->prep = 0;
   dos_data->irst = 0;
+  dos_data->esp = 0;
   dos_data->raid = 0;
 }
 
@@ -1572,6 +1583,12 @@ msdos_partition_set_flag (PedPartition* part,
 		dos_data->irst = state;
 		return ped_partition_set_system (part, part->fs_type);
 
+	case PED_PARTITION_ESP:
+		if (state)
+			clear_flags (dos_data);
+		dos_data->esp = state;
+		return ped_partition_set_system (part, part->fs_type);
+
 	default:
 		return 0;
 	}
@@ -1617,6 +1634,9 @@ msdos_partition_get_flag (const PedPartition* part, PedPartitionFlag flag)
 	case PED_PARTITION_IRST:
 		return dos_data->irst;
 
+	case PED_PARTITION_ESP:
+		return dos_data->esp;
+
 	default:
 		return 0;
 	}
@@ -1640,6 +1660,7 @@ msdos_partition_is_flag_available (const PedPartition* part,
 	case PED_PARTITION_PALO:
 	case PED_PARTITION_PREP:
 	case PED_PARTITION_IRST:
+	case PED_PARTITION_ESP:
 	case PED_PARTITION_DIAG:
 		return 1;
 
diff --git a/libparted/labels/gpt.c b/libparted/labels/gpt.c
index 0b7d08b..64b92b4 100644
--- a/libparted/labels/gpt.c
+++ b/libparted/labels/gpt.c
@@ -1592,6 +1592,7 @@ gpt_partition_set_flag (PedPartition *part, PedPartitionFlag flag, int state)
 
   switch (flag)
     {
+    case PED_PARTITION_ESP:
     case PED_PARTITION_BOOT:
       gpt_part_data->boot = state;
       if (state)
@@ -1754,6 +1755,7 @@ gpt_partition_get_flag (const PedPartition *part, PedPartitionFlag flag)
       return gpt_part_data->raid;
     case PED_PARTITION_LVM:
       return gpt_part_data->lvm;
+    case PED_PARTITION_ESP:
     case PED_PARTITION_BOOT:
       return gpt_part_data->boot;
     case PED_PARTITION_BIOS_GRUB:
@@ -1801,6 +1803,7 @@ gpt_partition_is_flag_available (const PedPartition *part,
     case PED_PARTITION_HIDDEN:
     case PED_PARTITION_LEGACY_BOOT:
     case PED_PARTITION_IRST:
+    case PED_PARTITION_ESP:
       return 1;
     case PED_PARTITION_SWAP:
     case PED_PARTITION_ROOT:
-- 
1.8.3.1