From e6536360bd4496cee1f1bf2dfb0b11f6bdbbfd4b Mon Sep 17 00:00:00 2001
From: "Roderick W. Smith" <rodsmith@rodsbooks.com>
Date: Sun, 23 Sep 2012 21:29:10 +0200
Subject: [PATCH] add support for a new Linux-specific GPT partition type code

* NEWS: Describe the new Linux-specific partition type code
and the new msftres flag that can be used to override this type
code, should it be necessary.
* doc/parted.texi: Describe of the new msftres flag.
* include/parted/disk.in.h [_PedPartitionFlag]: Add
PED_PARTITION_MSFT_DATA.
* libparted/disk.c: Add check for PED_PARTITION_MSFT_DATA, with
return of "msftdata", to ped_partition_flag_get_name()
* libparted/labels/gpt.c (PARTITION_LINUX_DATA_GUID): Define.
[_GPTPartitionData]: New member, "int msftdata".
(_parse_part_entry): Set the msftdata flag if and only if the
PARTITION_BASIC_DATA_GUID type code is in use;
(gpt_partition_new): Use the PARTITION_LINUX_DATA_GUID
type as the default type code
(gpt_partition_set_system): Set the PARTITION_BASIC_DATA_GUID type
code on partitions on which the msftdata flag is set and set
PARTITION_LINUX_DATA_GUID as the type by default.
Clear the msftdata flag in most tests in gpt_partition_set_flag()
(gpt_partition_set_flag): Add test for PED_PARTITION_MSFT_DATA, which
sets msftdata and other flags appropriately
(gpt_partition_get_flag): Add test for the PED_PARTITION_MSFT_DATA
item that returns the status of the msftdata flag
(gpt_partition_is_flag_available): Add test for
PED_PARTITION_MSFT_DATA item to
For more discussion and justification, see
http://thread.gmane.org/gmane.comp.gnu.parted.bugs/10456
---
 doc/parted.texi        |   14 ++++++++++++++
 include/parted/disk.h  |    5 +++--
 libparted/disk.c       |    2 ++
 libparted/labels/gpt.c |   47 +++++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 64 insertions(+), 4 deletions(-)

Index: parted-2.3/doc/parted.texi
===================================================================
--- parted-2.3.orig/doc/parted.texi
+++ parted-2.3/doc/parted.texi
@@ -1061,6 +1061,20 @@ on a partition that partition must be bo
 For PC98 disk labels, all ext2 partitions must be bootable (this is
 enforced by Parted).
 
+@item msftdata
+(GPT) - This flag identifies partitions that contain Microsoft filesystems
+(NTFS or FAT).  It may optionally be set on Linux filesystems to mimic the
+type of configuration created by parted 3.0 and earlier, in which a
+separate Linux filesystem type code was not available on GPT disks.  This
+flag can only be removed within parted by replacing it with a competing
+flag, such as boot or msftres.
+
+@item msftres
+(GPT) - This flag identifies a "Microsoft Reserved" partition, which is
+used by Windows on GPT disks.  Note that this flag should not normally be
+set on Windows filesystem partitions (those that contain NTFS or FAT
+filesystems).
+
 @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.
Index: parted-2.3/include/parted/disk.h
===================================================================
--- parted-2.3.orig/include/parted/disk.h
+++ parted-2.3/include/parted/disk.h
@@ -71,10 +71,11 @@ enum _PedPartitionFlag {
         PED_PARTITION_APPLE_TV_RECOVERY=13,
         PED_PARTITION_DIAG=14,
         PED_PARTITION_TYPE=15,
-        PED_PARTITION_LEGACY_BOOT=16
+        PED_PARTITION_LEGACY_BOOT=16,
+	PED_PARTITION_MSFT_DATA=17
 };
 #define PED_PARTITION_FIRST_FLAG        PED_PARTITION_BOOT
-#define PED_PARTITION_LAST_FLAG         PED_PARTITION_LEGACY_BOOT
+#define PED_PARTITION_LAST_FLAG         PED_PARTITION_MSFT_DATA
 
 enum _PedDiskTypeFeature {
         PED_DISK_TYPE_EXTENDED=1,       /**< supports extended partitions */
Index: parted-2.3/libparted/disk.c
===================================================================
--- parted-2.3.orig/libparted/disk.c
+++ parted-2.3/libparted/disk.c
@@ -2476,6 +2476,8 @@ ped_partition_flag_get_name (PedPartitio
 		return N_("prep");
 	case PED_PARTITION_MSFT_RESERVED:
 		return N_("msftres");
+	case PED_PARTITION_MSFT_DATA:
+		return N_("msftdata");
         case PED_PARTITION_APPLE_TV_RECOVERY:
                 return N_("atvrecv");
         case PED_PARTITION_DIAG:
Index: parted-2.3/libparted/labels/gpt.c
===================================================================
--- parted-2.3.orig/libparted/labels/gpt.c
+++ parted-2.3/libparted/labels/gpt.c
@@ -119,6 +119,10 @@ typedef struct
     ((efi_guid_t) { PED_CPU_TO_LE32 (0x0657fd6d), PED_CPU_TO_LE16 (0xa4ab), \
                     PED_CPU_TO_LE16 (0x43c4), 0x84, 0xe5, \
                     { 0x09, 0x33, 0xc8, 0x4b, 0x4f, 0x4f }})
+#define PARTITION_LINUX_DATA_GUID \
+    ((efi_guid_t) { PED_CPU_TO_LE32 (0x0FC63DAF), PED_CPU_TO_LE16 (0x8483), \
+                    PED_CPU_TO_LE16 (0x4772), 0x8E, 0x79, \
+                    { 0x3D, 0x69, 0xD8, 0x47, 0x7D, 0xE4 }})
 #define PARTITION_LVM_GUID \
     ((efi_guid_t) { PED_CPU_TO_LE32 (0xe6d6d379), PED_CPU_TO_LE16 (0xf507), \
                     PED_CPU_TO_LE16 (0x44c2), 0xa2, 0x3c, \
@@ -280,6 +284,7 @@ typedef struct _GPTPartitionData
   int hp_service;
   int hidden;
   int msftres;
+  int msftdata;
   int atvrecv;
   int msftrecv;
   int legacy_boot;
@@ -873,6 +878,7 @@ _parse_part_entry (PedDisk *disk, GuidPa
   gpt_part_data->lvm = gpt_part_data->raid
     = gpt_part_data->boot = gpt_part_data->hp_service
     = gpt_part_data->hidden = gpt_part_data->msftres
+    = gpt_part_data->msftdata
     = gpt_part_data->msftrecv
     = gpt_part_data->legacy_boot
     = gpt_part_data->prep
@@ -895,6 +901,8 @@ _parse_part_entry (PedDisk *disk, GuidPa
     gpt_part_data->hp_service = 1;
   else if (!guid_cmp (gpt_part_data->type, PARTITION_MSFT_RESERVED_GUID))
     gpt_part_data->msftres = 1;
+  else if (!guid_cmp (gpt_part_data->type, PARTITION_BASIC_DATA_GUID))
+    gpt_part_data->msftdata = 1;
   else if (!guid_cmp (gpt_part_data->type, PARTITION_MSFT_RECOVERY))
     gpt_part_data->msftrecv = 1;
   else if (!guid_cmp (gpt_part_data->type, PARTITION_APPLE_TV_RECOVERY_GUID))
@@ -1501,7 +1509,7 @@ gpt_partition_new (const PedDisk *disk,
   if (!gpt_part_data)
     goto error_free_part;
 
-  gpt_part_data->type = PARTITION_BASIC_DATA_GUID;
+  gpt_part_data->type = PARTITION_LINUX_DATA_GUID;
   gpt_part_data->lvm = 0;
   gpt_part_data->raid = 0;
   gpt_part_data->boot = 0;
@@ -1509,6 +1517,7 @@ gpt_partition_new (const PedDisk *disk,
   gpt_part_data->hp_service = 0;
   gpt_part_data->hidden = 0;
   gpt_part_data->msftres = 0;
+  gpt_part_data->msftdata = 0;
   gpt_part_data->msftrecv = 0;
   gpt_part_data->atvrecv = 0;
   gpt_part_data->legacy_boot = 0;
@@ -1613,6 +1622,11 @@ gpt_partition_set_system (PedPartition *
       gpt_part_data->type = PARTITION_MSFT_RESERVED_GUID;
       return 1;
     }
+  if (gpt_part_data->msftdata)
+    {
+      gpt_part_data->type = PARTITION_BASIC_DATA_GUID;
+      return 1;
+    }
   if (gpt_part_data->msftrecv)
     {
       gpt_part_data->type = PARTITION_MSFT_RECOVERY;
@@ -1644,7 +1658,7 @@ gpt_partition_set_system (PedPartition *
         }
     }
 
-  gpt_part_data->type = PARTITION_BASIC_DATA_GUID;
+  gpt_part_data->type = PARTITION_LINUX_DATA_GUID;
   return 1;
 }
 
@@ -1722,6 +1736,7 @@ gpt_partition_set_flag (PedPartition *pa
           = gpt_part_data->bios_grub
           = gpt_part_data->hp_service
           = gpt_part_data->msftres
+          = gpt_part_data->msftdata
           = gpt_part_data->prep
           = gpt_part_data->msftrecv
           = gpt_part_data->atvrecv = 0;
@@ -1734,6 +1749,7 @@ gpt_partition_set_flag (PedPartition *pa
           = gpt_part_data->boot
           = gpt_part_data->hp_service
           = gpt_part_data->msftres
+          = gpt_part_data->msftdata
           = gpt_part_data->prep
           = gpt_part_data->msftrecv
           = gpt_part_data->atvrecv = 0;
@@ -1746,6 +1762,7 @@ gpt_partition_set_flag (PedPartition *pa
           = 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->prep 
           = gpt_part_data->atvrecv = 0;
@@ -1758,6 +1775,7 @@ gpt_partition_set_flag (PedPartition *pa
           = 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->prep
           = gpt_part_data->atvrecv = 0;
@@ -1770,6 +1788,7 @@ gpt_partition_set_flag (PedPartition *pa
           = gpt_part_data->lvm
           = gpt_part_data->bios_grub
           = gpt_part_data->msftres
+          = gpt_part_data->msftdata
           = gpt_part_data->msftrecv
           = gpt_part_data->prep
           = gpt_part_data->atvrecv = 0;
@@ -1783,9 +1802,27 @@ gpt_partition_set_flag (PedPartition *pa
           = gpt_part_data->bios_grub
           = gpt_part_data->hp_service
           = gpt_part_data->msftrecv
+          = gpt_part_data->msftdata
           = gpt_part_data->prep
           = gpt_part_data->atvrecv = 0;
       return gpt_partition_set_system (part, part->fs_type);
+    case PED_PARTITION_MSFT_DATA:
+      gpt_part_data->msftres = 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->msftrecv
+          = gpt_part_data->prep
+          = gpt_part_data->atvrecv = 0;
+        gpt_part_data->msftdata = 1;
+      } else {
+        gpt_part_data->msftdata = 0;
+      }
+      return gpt_partition_set_system (part, part->fs_type);
     case PED_PARTITION_DIAG:
       gpt_part_data->msftrecv = state;
       if (state)
@@ -1795,6 +1832,7 @@ gpt_partition_set_flag (PedPartition *pa
           = gpt_part_data->bios_grub
           = gpt_part_data->hp_service
           = gpt_part_data->msftres
+          = gpt_part_data->msftdata
           = gpt_part_data->prep
           = gpt_part_data->atvrecv = 0;
       return gpt_partition_set_system (part, part->fs_type);
@@ -1807,6 +1845,7 @@ gpt_partition_set_flag (PedPartition *pa
           = gpt_part_data->bios_grub
           = gpt_part_data->hp_service
           = gpt_part_data->msftres
+          = gpt_part_data->msftdata
           = gpt_part_data->prep
           = gpt_part_data->msftrecv = 0;
       return gpt_partition_set_system (part, part->fs_type);
@@ -1819,6 +1858,7 @@ gpt_partition_set_flag (PedPartition *pa
           = 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->atvrecv = 0;
       return gpt_partition_set_system (part, part->fs_type);
@@ -1858,6 +1898,8 @@ gpt_partition_get_flag (const PedPartiti
       return gpt_part_data->hp_service;
     case PED_PARTITION_MSFT_RESERVED:
       return gpt_part_data->msftres;
+    case PED_PARTITION_MSFT_DATA:
+      return gpt_part_data->msftdata;
     case PED_PARTITION_DIAG:
       return gpt_part_data->msftrecv;
     case PED_PARTITION_APPLE_TV_RECOVERY:
@@ -1889,6 +1931,7 @@ gpt_partition_is_flag_available (const P
     case PED_PARTITION_BIOS_GRUB:
     case PED_PARTITION_HPSERVICE:
     case PED_PARTITION_MSFT_RESERVED:
+    case PED_PARTITION_MSFT_DATA:
     case PED_PARTITION_DIAG:
     case PED_PARTITION_APPLE_TV_RECOVERY:
     case PED_PARTITION_HIDDEN:
