]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - libfdisk/src/gpt.c
Fix minor typos
[thirdparty/util-linux.git] / libfdisk / src / gpt.c
index fd49c1349b4dc8acc10b47e492f3ec4a906ec649..6b894aa232c0287a403850beb3efc00e01ef296c 100644 (file)
@@ -21,7 +21,6 @@
 
 #include "fdiskP.h"
 
-#include "nls.h"
 #include "crc32.h"
 #include "blkdev.h"
 #include "bitops.h"
 #define GPT_MBR_PROTECTIVE  1
 #define GPT_MBR_HYBRID      2
 
-#define GPT_PRIMARY_PARTITION_TABLE_LBA 0x00000001
+#define GPT_PRIMARY_PARTITION_TABLE_LBA 0x00000001ULL
 
 #define EFI_PMBR_OSTYPE     0xEE
 #define MSDOS_MBR_SIGNATURE 0xAA55
 #define GPT_PART_NAME_LEN   (72 / sizeof(uint16_t))
-#define GPT_NPARTITIONS     128
+#define GPT_NPARTITIONS     FDISK_GPT_NPARTITIONS_DEFAULT
 
 /* Globally unique identifier */
 struct gpt_guid {
@@ -85,7 +84,8 @@ enum {
        GPT_ATTRBIT_GUID_COUNT  = 16
 };
 
-#define GPT_ATTRSTR_REQ                "RequiredPartiton"
+#define GPT_ATTRSTR_REQ                "RequiredPartition"
+#define GPT_ATTRSTR_REQ_TYPO   "RequiredPartiton"
 #define GPT_ATTRSTR_NOBLOCK    "NoBlockIOProtocol"
 #define GPT_ATTRSTR_LEGACY     "LegacyBIOSBootable"
 
@@ -150,6 +150,9 @@ struct gpt_legacy_mbr {
                .name = (_n),    \
        }
 
+/* Probably the most complete list of the GUIDs are at:
+ *      https://wikipedia.org/wiki/GUID_Partition_Table
+ */
 static struct fdisk_parttype gpt_parttypes[] =
 {
        /* Generic OS */
@@ -161,6 +164,17 @@ static struct fdisk_parttype gpt_parttypes[] =
        /* Hah!IdontneedEFI */
        DEF_GUID("21686148-6449-6E6F-744E-656564454649", N_("BIOS boot")),
 
+       /* NIH syndrome */
+       DEF_GUID("F4019732-066E-4E12-8273-346C5641494F", N_("Sony boot partition")),
+       DEF_GUID("BFBFAFE7-A34F-448A-9A5B-6213EB736C22", N_("Lenovo boot partition")),
+
+       /* PowerPC reference platform boot partition */
+       DEF_GUID("9E1A2D38-C612-4316-AA26-8B49521E5A8B", N_("PowerPC PReP boot")),
+
+       /* Open Network Install Environment */
+       DEF_GUID("7412F7D5-A156-4B13-81DC-867174929325", N_("ONIE boot")),
+       DEF_GUID("D4E6E2CD-4469-46F3-B5CB-1BFF57AFC149", N_("ONIE config")),
+
        /* Windows */
        DEF_GUID("E3C9E316-0B5C-4DB8-817D-F92DF00215AE", N_("Microsoft reserved")),
        DEF_GUID("EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", N_("Microsoft basic data")),
@@ -179,12 +193,19 @@ static struct fdisk_parttype gpt_parttypes[] =
        DEF_GUID("0FC63DAF-8483-4772-8E79-3D69D8477DE4", N_("Linux filesystem")),
        DEF_GUID("3B8F8425-20E0-4F3B-907F-1A25A76F98E8", N_("Linux server data")),
        DEF_GUID("44479540-F297-41B2-9AF7-D131D5F0458A", N_("Linux root (x86)")),
+       DEF_GUID("69DAD710-2CE4-4E3C-B16C-21A1D49ABED3", N_("Linux root (ARM)")),
        DEF_GUID("4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709", N_("Linux root (x86-64)")),
+       DEF_GUID("B921B045-1DF0-41C3-AF44-4C6F280D3FAE", N_("Linux root (ARM-64)")),
+       DEF_GUID("993D8D3D-F80E-4225-855A-9DAF8ED7EA97", N_("Linux root (IA-64)")),
        DEF_GUID("8DA63339-0007-60C0-C436-083AC8230908", N_("Linux reserved")),
        DEF_GUID("933AC7E1-2EB4-4F13-B844-0E14E2AEF915", N_("Linux home")),
        DEF_GUID("A19D880F-05FC-4D3B-A006-743F0F84911E", N_("Linux RAID")),
        DEF_GUID("BC13C2FF-59E6-4262-A352-B275FD6F7172", N_("Linux extended boot")),
        DEF_GUID("E6D6D379-F507-44C2-A23C-238F2A3DF928", N_("Linux LVM")),
+       /* ... too crazy, ignore for now:
+       DEF_GUID("7FFEC5C9-2D00-49B7-8941-3EA10A5586B7", N_("Linux plain dm-crypt")),
+       DEF_GUID("CA7D7CCB-63ED-4C53-861C-1742536059CC", N_("Linux LUKS")),
+       */
 
        /* FreeBSD */
        DEF_GUID("516E7CB4-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD data")),
@@ -237,9 +258,26 @@ static struct fdisk_parttype gpt_parttypes[] =
        DEF_GUID("85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD data")),
        DEF_GUID("85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD boot")),
        DEF_GUID("85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD swap")),
-       DEF_GUID("0394Ef8B-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD UFS")),
+       DEF_GUID("0394EF8B-237E-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD UFS")),
        DEF_GUID("85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD ZFS")),
        DEF_GUID("85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD Vinum")),
+
+       /* Ceph */
+       DEF_GUID("45B0969E-9B03-4F30-B4C6-B4B80CEFF106", N_("Ceph Journal")),
+       DEF_GUID("45B0969E-9B03-4F30-B4C6-5EC00CEFF106", N_("Ceph Encrypted Journal")),
+       DEF_GUID("4FBD7E29-9D25-41B8-AFD0-062C0CEFF05D", N_("Ceph OSD")),
+       DEF_GUID("4FBD7E29-9D25-41B8-AFD0-5EC00CEFF05D", N_("Ceph crypt OSD")),
+       DEF_GUID("89C57F98-2FE5-4DC0-89C1-F3AD0CEFF2BE", N_("Ceph disk in creation")),
+       DEF_GUID("89C57F98-2FE5-4DC0-89C1-5EC00CEFF2BE", N_("Ceph crypt disk in creation")),
+
+       /* OpenBSD */
+       DEF_GUID("824CC7A0-36A8-11E3-890A-952519AD3F61", N_("OpenBSD data")),
+
+       /* QNX */
+       DEF_GUID("CEF5A9AD-73BC-4601-89F3-CDEEEEE321A1", N_("QNX6 file system")),
+
+       /* Plan 9 */
+       DEF_GUID("C91818F9-8025-47AF-89D2-F030D7000C2C", N_("Plan 9 partition"))
 };
 
 /* gpt_entry macros */
@@ -255,7 +293,8 @@ struct fdisk_gpt_label {
        /* gpt specific part */
        struct gpt_header       *pheader;       /* primary header */
        struct gpt_header       *bheader;       /* backup header */
-       struct gpt_entry        *ents;          /* entries (partitions) */
+
+       unsigned char *ents;                    /* entries (partitions) */
 };
 
 static void gpt_deinit(struct fdisk_label *lb);
@@ -368,6 +407,12 @@ static int gpt_entry_set_uuid(struct gpt_entry *e, char *str)
        return 0;
 }
 
+static inline int gpt_entry_is_used(const struct gpt_entry *e)
+{
+       return memcmp(&e->type, &GPT_UNUSED_ENTRY_GUID,
+                       sizeof(struct gpt_guid)) != 0;
+}
+
 
 static const char *gpt_get_header_revstr(struct gpt_header *header)
 {
@@ -389,10 +434,56 @@ unknown:
        return "unknown";
 }
 
-static inline int partition_unused(const struct gpt_entry *e)
+static inline unsigned char *gpt_get_entry_ptr(struct fdisk_gpt_label *gpt, size_t i)
 {
-       return !memcmp(&e->type, &GPT_UNUSED_ENTRY_GUID,
-                       sizeof(struct gpt_guid));
+       return gpt->ents + le32_to_cpu(gpt->pheader->sizeof_partition_entry) * i;
+}
+
+static inline struct gpt_entry *gpt_get_entry(struct fdisk_gpt_label *gpt, size_t i)
+{
+       return (struct gpt_entry *) gpt_get_entry_ptr(gpt, i);
+}
+
+static inline struct gpt_entry *gpt_zeroize_entry(struct fdisk_gpt_label *gpt, size_t i)
+{
+       return (struct gpt_entry *) memset(gpt_get_entry_ptr(gpt, i),
+                       0, le32_to_cpu(gpt->pheader->sizeof_partition_entry));
+}
+
+/* Use to access array of entries, for() loops, etc. But don't use when
+ * you directly do something with GPT header, then use uint32_t.
+ */
+static inline size_t gpt_get_nentries(struct fdisk_gpt_label *gpt)
+{
+       return (size_t) le32_to_cpu(gpt->pheader->npartition_entries);
+}
+
+static inline int gpt_calculate_sizeof_ents(struct gpt_header *hdr, uint32_t nents, size_t *sz)
+{
+       uint32_t esz = le32_to_cpu(hdr->sizeof_partition_entry);
+
+       if (nents == 0 || esz == 0 || SIZE_MAX/esz < nents) {
+               DBG(LABEL, ul_debug("GPT entreis array size check failed"));
+               return -ERANGE;
+       }
+
+       *sz = nents * esz;
+       return 0;
+}
+
+static inline int gpt_sizeof_ents(struct gpt_header *hdr, size_t *sz)
+{
+       return gpt_calculate_sizeof_ents(hdr, le32_to_cpu(hdr->npartition_entries), sz);
+}
+
+
+static char *gpt_get_header_id(struct gpt_header *header)
+{
+       char str[37];
+
+       guid_to_string(&header->disk_guid, str);
+
+       return strdup(str);
 }
 
 /*
@@ -424,7 +515,7 @@ static int gpt_mknew_pmbr(struct fdisk_context *cxt)
        pmbr->partition_record[0].end_track    = 0xFF;
        pmbr->partition_record[0].starting_lba = cpu_to_le32(1);
        pmbr->partition_record[0].size_in_lba  =
-               cpu_to_le32(min((uint32_t) cxt->total_sectors - 1, 0xFFFFFFFF));
+               cpu_to_le32((uint32_t) min( cxt->total_sectors - 1ULL, 0xFFFFFFFFULL) );
 
        return 0;
 }
@@ -439,14 +530,15 @@ static void gpt_mknew_header_common(struct fdisk_context *cxt,
        header->my_lba = cpu_to_le64(lba);
 
        if (lba == GPT_PRIMARY_PARTITION_TABLE_LBA) { /* primary */
-               header->alternative_lba = cpu_to_le64(cxt->total_sectors - 1);
-               header->partition_entry_lba = cpu_to_le64(2);
+               header->alternative_lba = cpu_to_le64(cxt->total_sectors - 1ULL);
+               header->partition_entry_lba = cpu_to_le64(2ULL);
        } else { /* backup */
-               uint64_t esz = le32_to_cpu(header->npartition_entries) * sizeof(struct gpt_entry);
+               uint64_t esz = (uint64_t) le32_to_cpu(header->npartition_entries) 
+                                                       * sizeof(struct gpt_entry);
                uint64_t esects = (esz + cxt->sector_size - 1) / cxt->sector_size;
 
                header->alternative_lba = cpu_to_le64(GPT_PRIMARY_PARTITION_TABLE_LBA);
-               header->partition_entry_lba = cpu_to_le64(cxt->total_sectors - 1 - esects);
+               header->partition_entry_lba = cpu_to_le64(cxt->total_sectors - 1ULL - esects);
        }
 }
 
@@ -513,12 +605,12 @@ static struct gpt_header *gpt_copy_header(struct fdisk_context *cxt,
 
 
        if (res->my_lba == GPT_PRIMARY_PARTITION_TABLE_LBA)
-               res->partition_entry_lba = cpu_to_le64(2);
+               res->partition_entry_lba = cpu_to_le64(2ULL);
        else {
-               uint64_t esz = le32_to_cpu(src->npartition_entries) * sizeof(struct gpt_entry);
+               uint64_t esz = (uint64_t) le32_to_cpu(src->npartition_entries) * sizeof(struct gpt_entry);
                uint64_t esects = (esz + cxt->sector_size - 1) / cxt->sector_size;
 
-               res->partition_entry_lba = cpu_to_le64(cxt->total_sectors - 1 - esects);
+               res->partition_entry_lba = cpu_to_le64(cxt->total_sectors - 1ULL - esects);
        }
 
        return res;
@@ -564,8 +656,8 @@ static int count_first_last_lba(struct fdisk_context *cxt,
 
        /* UEFI default */
        esz = sizeof(struct gpt_entry) * GPT_NPARTITIONS / cxt->sector_size;
-       llba = cxt->total_sectors - 2 - esz;
-       flba = esz + 2;
+       llba = cxt->total_sectors - 2ULL - esz;
+       flba = esz + 2ULL;
 
        /* script default */
        if (cxt->script) {
@@ -573,7 +665,8 @@ static int count_first_last_lba(struct fdisk_context *cxt,
                if (rc < 0)
                        return rc;
 
-               DBG(LABEL, ul_debug("FirstLBA: script=%ju, uefi=%ju, topology=%ju.", *first, flba, cxt->first_lba));
+               DBG(LABEL, ul_debug("FirstLBA: script=%"PRIu64", uefi=%"PRIu64", topology=%ju.",
+                                   *first, flba,  (uintmax_t)cxt->first_lba));
 
                if (rc == 0 && (*first < flba || *first > llba)) {
                        fdisk_warnx(cxt, _("First LBA specified by script is out of range."));
@@ -584,7 +677,8 @@ static int count_first_last_lba(struct fdisk_context *cxt,
                if (rc < 0)
                        return rc;
 
-               DBG(LABEL, ul_debug("LastLBA: script=%ju, uefi=%ju, topology=%ju.", *last, llba, cxt->last_lba));
+               DBG(LABEL, ul_debug("LastLBA: script=%"PRIu64", uefi=%"PRIu64", topology=%ju.",
+                                   *last, llba,  (uintmax_t)cxt->last_lba));
 
                if (rc == 0 && (*last > llba || *last < flba)) {
                        fdisk_warnx(cxt, _("Last LBA specified by script is out of range."));
@@ -624,7 +718,7 @@ static int gpt_mknew_header(struct fdisk_context *cxt,
 
        /* According to EFI standard it's valid to count all the first
         * sector into header size, but some tools may have a problem
-        * to accept it, so use the header without the zerozied area.
+        * to accept it, so use the header without the zeroed area.
         * This does not have any impact to CRC, etc.   --kzak Jan-2015
         */
        header->size = cpu_to_le32(sizeof(struct gpt_header)
@@ -662,13 +756,12 @@ static int gpt_mknew_header(struct fdisk_context *cxt,
 /*
  * Checks if there is a valid protective MBR partition table.
  * Returns 0 if it is invalid or failure. Otherwise, return
- * GPT_MBR_PROTECTIVE or GPT_MBR_HYBRID, depeding on the detection.
+ * GPT_MBR_PROTECTIVE or GPT_MBR_HYBRID, depending on the detection.
  */
 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;
@@ -678,11 +771,6 @@ static int valid_pmbr(struct fdisk_context *cxt)
        if (le16_to_cpu(pmbr->signature) != MSDOS_MBR_SIGNATURE)
                goto done;
 
-       /* LBA of the GPT partition header */
-       if (pmbr->partition_record[0].starting_lba !=
-           cpu_to_le32(GPT_PRIMARY_PARTITION_TABLE_LBA))
-               goto done;
-
        /* 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) {
@@ -693,13 +781,18 @@ static int valid_pmbr(struct fdisk_context *cxt)
                         */
                        part = i;
                        ret = GPT_MBR_PROTECTIVE;
-                       goto check_hybrid;
+                       break;
                }
        }
 
        if (ret != GPT_MBR_PROTECTIVE)
                goto done;
-check_hybrid:
+
+       /* LBA of the GPT partition header */
+       if (pmbr->partition_record[part].starting_lba !=
+           cpu_to_le32(GPT_PRIMARY_PARTITION_TABLE_LBA))
+               goto done;
+
        for (i = 0 ; i < 4; i++) {
                if ((pmbr->partition_record[i].os_type != EFI_PMBR_OSTYPE) &&
                    (pmbr->partition_record[i].os_type != 0x00))
@@ -719,12 +812,11 @@ check_hybrid:
         * an image from a smaller disk to a bigger disk.
         */
        if (ret == GPT_MBR_PROTECTIVE) {
-               sz_lba = le32_to_cpu(pmbr->partition_record[part].size_in_lba);
-               if (sz_lba != (uint32_t) cxt->total_sectors - 1 && sz_lba != 0xFFFFFFFF) {
-                       fdisk_warnx(cxt, _("GPT PMBR size mismatch (%u != %u) "
+               uint64_t sz_lba = (uint64_t) le32_to_cpu(pmbr->partition_record[part].size_in_lba);
+               if (sz_lba != cxt->total_sectors - 1ULL && sz_lba != 0xFFFFFFFFULL) {
+                       fdisk_warnx(cxt, _("GPT PMBR size mismatch (%"PRIu64" != %"PRIu64") "
                                           "will be corrected by w(rite)."),
-                                       sz_lba,
-                                       (uint32_t) cxt->total_sectors - 1);
+                                       sz_lba, cxt->total_sectors - 1ULL);
                        fdisk_label_set_changed(cxt->label, 1);
                }
        }
@@ -744,14 +836,14 @@ static uint64_t last_lba(struct fdisk_context *cxt)
        }
 
        if (S_ISBLK(s.st_mode))
-               sectors = cxt->total_sectors - 1;
+               sectors = cxt->total_sectors - 1ULL;
        else if (S_ISREG(s.st_mode))
                sectors = ((uint64_t) s.st_size /
                           (uint64_t) cxt->sector_size) - 1ULL;
        else
                fdisk_warnx(cxt, _("gpt: cannot handle files with mode %o"), s.st_mode);
 
-       DBG(LABEL, ul_debug("GPT last LBA: %ju", sectors));
+       DBG(LABEL, ul_debug("GPT last LBA: %"PRIu64"", sectors));
        return sectors;
 }
 
@@ -762,33 +854,43 @@ static ssize_t read_lba(struct fdisk_context *cxt, uint64_t lba,
 
        if (lseek(cxt->dev_fd, offset, SEEK_SET) == (off_t) -1)
                return -1;
-       return read(cxt->dev_fd, buffer, bytes) != bytes;
+       return (size_t)read(cxt->dev_fd, buffer, bytes) != bytes;
 }
 
 
 /* Returns the GPT entry array */
-static struct gpt_entry *gpt_read_entries(struct fdisk_context *cxt,
+static unsigned char *gpt_read_entries(struct fdisk_context *cxt,
                                         struct gpt_header *header)
 {
-       ssize_t sz;
-       struct gpt_entry *ret = NULL;
+       size_t sz;
+       ssize_t ssz;
+
+       unsigned char *ret = NULL;
        off_t offset;
 
        assert(cxt);
        assert(header);
 
-       sz = le32_to_cpu(header->npartition_entries) *
-            le32_to_cpu(header->sizeof_partition_entry);
+       if (gpt_sizeof_ents(header, &sz))
+               return NULL;
+
+       if (sz > (size_t) SSIZE_MAX) {
+               DBG(LABEL, ul_debug("GPT entries array too large to read()"));
+               return NULL;
+       }
 
        ret = calloc(1, sz);
        if (!ret)
                return NULL;
-       offset = le64_to_cpu(header->partition_entry_lba) *
+
+       offset = (off_t) le64_to_cpu(header->partition_entry_lba) *
                       cxt->sector_size;
 
        if (offset != lseek(cxt->dev_fd, offset, SEEK_SET))
                goto fail;
-       if (sz != read(cxt->dev_fd, ret, sz))
+
+       ssz = read(cxt->dev_fd, ret, sz);
+       if (ssz < 0 || (size_t) ssz != sz)
                goto fail;
 
        return ret;
@@ -798,66 +900,67 @@ fail:
        return NULL;
 }
 
-static inline uint32_t count_crc32(const unsigned char *buf, size_t len)
+static inline uint32_t count_crc32(const unsigned char *buf, size_t len,
+                                  size_t ex_off, size_t ex_len)
+{
+       return (ul_crc32_exclude_offset(~0L, buf, len, ex_off, ex_len) ^ ~0L);
+}
+
+static inline uint32_t gpt_header_count_crc32(struct gpt_header *header)
 {
-       return (crc32(~0L, buf, len) ^ ~0L);
+        return count_crc32((unsigned char *) header,           /* buffer */
+                       le32_to_cpu(header->size),              /* size of buffer */
+                       offsetof(struct gpt_header, crc32),     /* exclude */
+                       sizeof(header->crc32));                 /* size of excluded area */
 }
 
+static inline uint32_t gpt_entryarr_count_crc32(struct gpt_header *header, unsigned char *ents)
+{
+       size_t arysz = 0;
+
+       if (gpt_sizeof_ents(header, &arysz))
+               return 0;
+
+       return count_crc32(ents, arysz, 0, 0);
+}
+
+
 /*
  * Recompute header and partition array 32bit CRC checksums.
  * This function does not fail - if there's corruption, then it
- * will be reported when checksuming it again (ie: probing or verify).
+ * will be reported when checksumming it again (ie: probing or verify).
  */
-static void gpt_recompute_crc(struct gpt_header *header, struct gpt_entry *ents)
+static void gpt_recompute_crc(struct gpt_header *header, unsigned char *ents)
 {
-       uint32_t crc = 0;
-       size_t entry_sz = 0;
-
        if (!header)
                return;
 
-       /* header CRC */
-       header->crc32 = 0;
-       crc = count_crc32((unsigned char *) header, le32_to_cpu(header->size));
-       header->crc32 = cpu_to_le32(crc);
-
-       /* partition entry array CRC */
-       header->partition_entry_array_crc32 = 0;
-       entry_sz = le32_to_cpu(header->npartition_entries) *
-               le32_to_cpu(header->sizeof_partition_entry);
+       header->partition_entry_array_crc32 =
+                       cpu_to_le32( gpt_entryarr_count_crc32(header, ents) );
 
-       crc = count_crc32((unsigned char *) ents, entry_sz);
-       header->partition_entry_array_crc32 = cpu_to_le32(crc);
+       header->crc32 = cpu_to_le32( gpt_header_count_crc32(header) );
 }
 
 /*
  * Compute the 32bit CRC checksum of the partition table header.
  * Returns 1 if it is valid, otherwise 0.
  */
-static int gpt_check_header_crc(struct gpt_header *header, struct gpt_entry *ents)
+static int gpt_check_header_crc(struct gpt_header *header, unsigned char *ents)
 {
-       uint32_t crc, orgcrc = le32_to_cpu(header->crc32);
-
-       header->crc32 = 0;
-       crc = count_crc32((unsigned char *) header, le32_to_cpu(header->size));
-       header->crc32 = cpu_to_le32(orgcrc);
+       uint32_t orgcrc = le32_to_cpu(header->crc32),
+                crc = gpt_header_count_crc32(header);
 
-       if (crc == le32_to_cpu(header->crc32))
+       if (crc == orgcrc)
                return 1;
 
        /*
-        * If we have checksum mismatch it may be due to stale data,
-        * like a partition being added or deleted. Recompute the CRC again
-        * and make sure this is not the case.
+        * If we have checksum mismatch it may be due to stale data, like a
+        * partition being added or deleted. Recompute the CRC again and make
+        * sure this is not the case.
         */
        if (ents) {
                gpt_recompute_crc(header, ents);
-               orgcrc = le32_to_cpu(header->crc32);
-               header->crc32 = 0;
-               crc = count_crc32((unsigned char *) header, le32_to_cpu(header->size));
-               header->crc32 = cpu_to_le32(orgcrc);
-
-               return crc == le32_to_cpu(header->crc32);
+               return gpt_header_count_crc32(header) == orgcrc;
        }
 
        return 0;
@@ -867,26 +970,13 @@ static int gpt_check_header_crc(struct gpt_header *header, struct gpt_entry *ent
  * It initializes the partition entry array.
  * Returns 1 if the checksum is valid, otherwise 0.
  */
-static int gpt_check_entryarr_crc(struct gpt_header *header,
-                                 struct gpt_entry *ents)
+static int gpt_check_entryarr_crc(struct gpt_header *header, unsigned char *ents)
 {
-       int ret = 0;
-       ssize_t entry_sz;
-       uint32_t crc;
-
        if (!header || !ents)
-               goto done;
-
-       entry_sz = le32_to_cpu(header->npartition_entries) *
-                  le32_to_cpu(header->sizeof_partition_entry);
-
-       if (!entry_sz)
-               goto done;
+               return 0;
 
-       crc = count_crc32((unsigned char *) ents, entry_sz);
-       ret = (crc == le32_to_cpu(header->partition_entry_array_crc32));
-done:
-       return ret;
+       return gpt_entryarr_count_crc32(header, ents) ==
+                       le32_to_cpu(header->partition_entry_array_crc32);
 }
 
 static int gpt_check_lba_sanity(struct fdisk_context *cxt, struct gpt_header *header)
@@ -934,10 +1024,10 @@ static int gpt_check_signature(struct gpt_header *header)
  */
 static struct gpt_header *gpt_read_header(struct fdisk_context *cxt,
                                          uint64_t lba,
-                                         struct gpt_entry **_ents)
+                                         unsigned char **_ents)
 {
        struct gpt_header *header = NULL;
-       struct gpt_entry *ents = NULL;
+       unsigned char *ents = NULL;
        uint32_t hsz;
 
        if (!cxt)
@@ -981,19 +1071,18 @@ static struct gpt_header *gpt_read_header(struct fdisk_context *cxt,
        if (le64_to_cpu(header->my_lba) != lba)
                goto invalid;
 
-
        if (_ents)
                *_ents = ents;
        else
                free(ents);
 
-       DBG(LABEL, ul_debug("found valid GPT Header on LBA %ju", lba));
+       DBG(LABEL, ul_debug("found valid GPT Header on LBA %"PRIu64"", lba));
        return header;
 invalid:
        free(header);
        free(ents);
 
-       DBG(LABEL, ul_debug("read GPT Header on LBA %ju failed", lba));
+       DBG(LABEL, ul_debug("read GPT Header on LBA %"PRIu64" failed", lba));
        return NULL;
 }
 
@@ -1023,10 +1112,9 @@ static int gpt_locate_disklabel(struct fdisk_context *cxt, int n,
        case 2:
                *name = _("GPT Entries");
                gpt = self_label(cxt);
-               *offset = le64_to_cpu(gpt->pheader->partition_entry_lba) * cxt->sector_size;
-               *size = le32_to_cpu(gpt->pheader->npartition_entries) *
-                        le32_to_cpu(gpt->pheader->sizeof_partition_entry);
-               break;
+               *offset = (uint64_t) le64_to_cpu(gpt->pheader->partition_entry_lba) *
+                                    cxt->sector_size;
+               return gpt_sizeof_ents(gpt->pheader, size);
        default:
                return 1;                       /* no more chunks */
        }
@@ -1034,22 +1122,80 @@ static int gpt_locate_disklabel(struct fdisk_context *cxt, int n,
        return 0;
 }
 
+static int gpt_get_disklabel_item(struct fdisk_context *cxt, struct fdisk_labelitem *item)
+{
+       struct gpt_header *h;
+       int rc = 0;
+
+       assert(cxt);
+       assert(cxt->label);
+       assert(fdisk_is_label(cxt, GPT));
+
+       h = self_label(cxt)->pheader;
 
+       switch (item->id) {
+       case GPT_LABELITEM_ID:
+               item->name = _("Disk identifier");
+               item->type = 's';
+               item->data.str = gpt_get_header_id(h);
+               if (!item->data.str)
+                       rc = -ENOMEM;
+               break;
+       case GPT_LABELITEM_FIRSTLBA:
+               item->name = _("First LBA");
+               item->type = 'j';
+               item->data.num64 = le64_to_cpu(h->first_usable_lba);
+               break;
+       case GPT_LABELITEM_LASTLBA:
+               item->name = _("Last LBA");
+               item->type = 'j';
+               item->data.num64 = le64_to_cpu(h->last_usable_lba);
+               break;
+       case GPT_LABELITEM_ALTLBA:
+               /* TRANSLATORS: The LBA (Logical Block Address) of the backup GPT header. */
+               item->name = _("Alternative LBA");
+               item->type = 'j';
+               item->data.num64 = le64_to_cpu(h->alternative_lba);
+               break;
+       case GPT_LABELITEM_ENTRIESLBA:
+               /* TRANSLATORS: The start of the array of partition entries. */
+               item->name = _("Partition entries LBA");
+               item->type = 'j';
+               item->data.num64 = le64_to_cpu(h->partition_entry_lba);
+               break;
+       case GPT_LABELITEM_ENTRIESALLOC:
+               item->name = _("Allocated partition entries");
+               item->type = 'j';
+               item->data.num64 = le32_to_cpu(h->npartition_entries);
+               break;
+       default:
+               if (item->id < __FDISK_NLABELITEMS)
+                       rc = 1; /* unsupported generic item */
+               else
+                       rc = 2; /* out of range */
+               break;
+       }
+
+       return rc;
+}
 
 /*
  * Returns the number of partitions that are in use.
  */
-static unsigned partitions_in_use(struct gpt_header *header,
-                                 struct gpt_entry *ents)
+static size_t partitions_in_use(struct fdisk_gpt_label *gpt)
 {
-       uint32_t i, used = 0;
+       size_t i, used = 0;
 
-       if (!header || ! ents)
-               return 0;
+       assert(gpt);
+       assert(gpt->pheader);
+       assert(gpt->ents);
+
+       for (i = 0; i < gpt_get_nentries(gpt); i++) {
+               struct gpt_entry *e = gpt_get_entry(gpt, i);
 
-       for (i = 0; i < le32_to_cpu(header->npartition_entries); i++)
-               if (!partition_unused(&ents[i]))
+               if (gpt_entry_is_used(e))
                        used++;
+       }
        return used;
 }
 
@@ -1058,15 +1204,20 @@ static unsigned partitions_in_use(struct gpt_header *header,
  * Check if a partition is too big for the disk (sectors).
  * Returns the faulting partition number, otherwise 0.
  */
-static uint32_t check_too_big_partitions(struct gpt_header *header,
-                                  struct gpt_entry *ents, uint64_t sectors)
+static uint32_t check_too_big_partitions(struct fdisk_gpt_label *gpt, uint64_t sectors)
 {
-       uint32_t i;
+       size_t i;
 
-       for (i = 0; i < le32_to_cpu(header->npartition_entries); i++) {
-               if (partition_unused(&ents[i]))
+       assert(gpt);
+       assert(gpt->pheader);
+       assert(gpt->ents);
+
+       for (i = 0; i < gpt_get_nentries(gpt); i++) {
+               struct gpt_entry *e = gpt_get_entry(gpt, i);
+
+               if (!gpt_entry_is_used(e))
                        continue;
-               if (gpt_partition_end(&ents[i]) >= sectors)
+               if (gpt_partition_end(e) >= sectors)
                        return i + 1;
        }
 
@@ -1077,15 +1228,20 @@ static uint32_t check_too_big_partitions(struct gpt_header *header,
  * Check if a partition ends before it begins
  * Returns the faulting partition number, otherwise 0.
  */
-static uint32_t check_start_after_end_paritions(struct gpt_header *header,
-                                               struct gpt_entry *ents)
+static uint32_t check_start_after_end_partitions(struct fdisk_gpt_label *gpt)
 {
-       uint32_t i;
+       size_t i;
+
+       assert(gpt);
+       assert(gpt->pheader);
+       assert(gpt->ents);
 
-       for (i = 0; i < le32_to_cpu(header->npartition_entries); i++) {
-               if (partition_unused(&ents[i]))
+       for (i = 0; i < gpt_get_nentries(gpt); i++) {
+               struct gpt_entry *e = gpt_get_entry(gpt, i);
+
+               if (!gpt_entry_is_used(e))
                        continue;
-               if (gpt_partition_start(&ents[i]) > gpt_partition_end(&ents[i]))
+               if (gpt_partition_start(e) > gpt_partition_end(e))
                        return i + 1;
        }
 
@@ -1108,18 +1264,23 @@ static inline int partition_overlap(struct gpt_entry *e1, struct gpt_entry *e2)
 /*
  * Find any partitions that overlap.
  */
-static uint32_t check_overlap_partitions(struct gpt_header *header,
-                                        struct gpt_entry *ents)
+static uint32_t check_overlap_partitions(struct fdisk_gpt_label *gpt)
 {
-       uint32_t i, j;
+       size_t i, j;
+
+       assert(gpt);
+       assert(gpt->pheader);
+       assert(gpt->ents);
 
-       for (i = 0; i < le32_to_cpu(header->npartition_entries); i++)
+       for (i = 0; i < gpt_get_nentries(gpt); i++)
                for (j = 0; j < i; j++) {
-                       if (partition_unused(&ents[i]) ||
-                           partition_unused(&ents[j]))
+                       struct gpt_entry *ei = gpt_get_entry(gpt, i);
+                       struct gpt_entry *ej = gpt_get_entry(gpt, j);
+
+                       if (!gpt_entry_is_used(ei) || !gpt_entry_is_used(ej))
                                continue;
-                       if (partition_overlap(&ents[i], &ents[j])) {
-                               DBG(LABEL, ul_debug("GPT partitions overlap detected [%u vs. %u]", i, j));
+                       if (partition_overlap(ei, ej)) {
+                               DBG(LABEL, ul_debug("GPT partitions overlap detected [%zu vs. %zu]", i, j));
                                return i + 1;
                        }
                }
@@ -1131,19 +1292,18 @@ static uint32_t check_overlap_partitions(struct gpt_header *header,
  * Find the first available block after the starting point; returns 0 if
  * there are no available blocks left, or error. From gdisk.
  */
-static uint64_t find_first_available(struct gpt_header *header,
-                                    struct gpt_entry *ents, uint64_t start)
+static uint64_t find_first_available(struct fdisk_gpt_label *gpt, uint64_t start)
 {
+       int first_moved = 0;
        uint64_t first;
-       uint32_t i, first_moved = 0;
-
        uint64_t fu, lu;
 
-       if (!header || !ents)
-               return 0;
+       assert(gpt);
+       assert(gpt->pheader);
+       assert(gpt->ents);
 
-       fu = le64_to_cpu(header->first_usable_lba);
-       lu = le64_to_cpu(header->last_usable_lba);
+       fu = le64_to_cpu(gpt->pheader->first_usable_lba);
+       lu = le64_to_cpu(gpt->pheader->last_usable_lba);
 
        /*
         * Begin from the specified starting point or from the first usable
@@ -1159,14 +1319,18 @@ static uint64_t find_first_available(struct gpt_header *header,
         * cases where partitions are out of sequential order....
         */
        do {
+               size_t i;
+
                first_moved = 0;
-               for (i = 0; i < le32_to_cpu(header->npartition_entries); i++) {
-                       if (partition_unused(&ents[i]))
+               for (i = 0; i < gpt_get_nentries(gpt); i++) {
+                       struct gpt_entry *e = gpt_get_entry(gpt, i);
+
+                       if (!gpt_entry_is_used(e))
                                continue;
-                       if (first < gpt_partition_start(&ents[i]))
+                       if (first < gpt_partition_start(e))
                                continue;
-                       if (first <= gpt_partition_end(&ents[i])) {
-                               first = gpt_partition_end(&ents[i]) + 1;
+                       if (first <= gpt_partition_end(e)) {
+                               first = gpt_partition_end(e) + 1;
                                first_moved = 1;
                        }
                }
@@ -1180,50 +1344,55 @@ static uint64_t find_first_available(struct gpt_header *header,
 
 
 /* Returns last available sector in the free space pointed to by start. From gdisk. */
-static uint64_t find_last_free(struct gpt_header *header,
-                              struct gpt_entry *ents, uint64_t start)
+static uint64_t find_last_free(struct fdisk_gpt_label *gpt, uint64_t start)
 {
-       uint32_t i;
+       size_t i;
        uint64_t nearest_start;
 
-       if (!header || !ents)
-               return 0;
+       assert(gpt);
+       assert(gpt->pheader);
+       assert(gpt->ents);
 
-       nearest_start = le64_to_cpu(header->last_usable_lba);
+       nearest_start = le64_to_cpu(gpt->pheader->last_usable_lba);
 
-       for (i = 0; i < le32_to_cpu(header->npartition_entries); i++) {
-               uint64_t ps = gpt_partition_start(&ents[i]);
+       for (i = 0; i < gpt_get_nentries(gpt); i++) {
+               struct gpt_entry *e = gpt_get_entry(gpt, i);
+               uint64_t ps = gpt_partition_start(e);
 
                if (nearest_start > ps && ps > start)
-                       nearest_start = ps - 1;
+                       nearest_start = ps - 1ULL;
        }
 
        return nearest_start;
 }
 
 /* Returns the last free sector on the disk. From gdisk. */
-static uint64_t find_last_free_sector(struct gpt_header *header,
-                                     struct gpt_entry *ents)
+static uint64_t find_last_free_sector(struct fdisk_gpt_label *gpt)
 {
-       uint32_t i, last_moved;
+       int last_moved;
        uint64_t last = 0;
 
-       if (!header || !ents)
-               goto done;
+       assert(gpt);
+       assert(gpt->pheader);
+       assert(gpt->ents);
 
        /* start by assuming the last usable LBA is available */
-       last = le64_to_cpu(header->last_usable_lba);
+       last = le64_to_cpu(gpt->pheader->last_usable_lba);
        do {
+               size_t i;
+
                last_moved = 0;
-               for (i = 0; i < le32_to_cpu(header->npartition_entries); i++) {
-                       if ((last >= gpt_partition_start(&ents[i])) &&
-                           (last <= gpt_partition_end(&ents[i]))) {
-                               last = gpt_partition_start(&ents[i]) - 1;
+               for (i = 0; i < gpt_get_nentries(gpt); i++) {
+                       struct gpt_entry *e = gpt_get_entry(gpt, i);
+
+                       if (last >= gpt_partition_start(e) &&
+                           last <= gpt_partition_end(e)) {
+                               last = gpt_partition_start(e) - 1ULL;
                                last_moved = 1;
                        }
                }
        } while (last_moved == 1);
-done:
+
        return last;
 }
 
@@ -1232,30 +1401,29 @@ done:
  * space on the disk. Returns 0 if there are no available blocks left.
  * From gdisk.
  */
-static uint64_t find_first_in_largest(struct gpt_header *header,
-                                     struct gpt_entry *ents)
+static uint64_t find_first_in_largest(struct fdisk_gpt_label *gpt)
 {
        uint64_t start = 0, first_sect, last_sect;
        uint64_t segment_size, selected_size = 0, selected_segment = 0;
 
-       if (!header || !ents)
-               goto done;
+       assert(gpt);
+       assert(gpt->pheader);
+       assert(gpt->ents);
 
        do {
-               first_sect =  find_first_available(header, ents, start);
+               first_sect = find_first_available(gpt, start);
                if (first_sect != 0) {
-                       last_sect = find_last_free(header, ents, first_sect);
-                       segment_size = last_sect - first_sect + 1;
+                       last_sect = find_last_free(gpt, first_sect);
+                       segment_size = last_sect - first_sect + 1ULL;
 
                        if (segment_size > selected_size) {
                                selected_size = segment_size;
                                selected_segment = first_sect;
                        }
-                       start = last_sect + 1;
+                       start = last_sect + 1ULL;
                }
        } while (first_sect != 0);
 
-done:
        return selected_segment;
 }
 
@@ -1263,8 +1431,9 @@ done:
  * Find the total number of free sectors, the number of segments in which
  * they reside, and the size of the largest of those segments. From gdisk.
  */
-static uint64_t get_free_sectors(struct fdisk_context *cxt, struct gpt_header *header,
-                                struct gpt_entry *ents, uint32_t *nsegments,
+static uint64_t get_free_sectors(struct fdisk_context *cxt,
+                                struct fdisk_gpt_label *gpt,
+                                uint32_t *nsegments,
                                 uint64_t *largest_segment)
 {
        uint32_t num = 0;
@@ -1275,17 +1444,21 @@ static uint64_t get_free_sectors(struct fdisk_context *cxt, struct gpt_header *h
        if (!cxt->total_sectors)
                goto done;
 
+       assert(gpt);
+       assert(gpt->pheader);
+       assert(gpt->ents);
+
        do {
-               first_sect = find_first_available(header, ents, start);
+               first_sect = find_first_available(gpt, start);
                if (first_sect) {
-                       last_sect = find_last_free(header, ents, first_sect);
+                       last_sect = find_last_free(gpt, first_sect);
                        segment_sz = last_sect - first_sect + 1;
 
                        if (segment_sz > largest_seg)
                                largest_seg = segment_sz;
                        totfound += segment_sz;
                        num++;
-                       start = last_sect + 1;
+                       start = last_sect + 1ULL;
                }
        } while (first_sect);
 
@@ -1353,8 +1526,8 @@ static int gpt_probe_label(struct fdisk_context *cxt)
                gpt_recompute_crc(gpt->pheader, gpt->ents);
        }
 
-       cxt->label->nparts_max = le32_to_cpu(gpt->pheader->npartition_entries);
-       cxt->label->nparts_cur = partitions_in_use(gpt->pheader, gpt->ents);
+       cxt->label->nparts_max = gpt_get_nentries(gpt);
+       cxt->label->nparts_cur = partitions_in_use(gpt);
        return 1;
 failed:
        DBG(LABEL, ul_debug("GPT probe failed"));
@@ -1415,7 +1588,7 @@ static int gpt_entry_attrs_to_string(struct gpt_entry *e, char **res)
        assert(res);
 
        *res = NULL;
-       attrs = le64_to_cpu(e->attrs);
+       attrs = e->attrs;
        if (!attrs)
                return 0;       /* no attributes at all */
 
@@ -1490,13 +1663,14 @@ static int gpt_entry_attrs_from_string(
 
                DBG(LABEL, ul_debug(" parsing item '%s'", p));
 
-               if (strncmp(p, "GUID:", 5) == 0) {
-                       p += 5;
-                       continue;
-               } else if (strncmp(p, GPT_ATTRSTR_REQ,
+               if (strncmp(p, GPT_ATTRSTR_REQ,
                                        sizeof(GPT_ATTRSTR_REQ) - 1) == 0) {
                        bit = GPT_ATTRBIT_REQ;
                        p += sizeof(GPT_ATTRSTR_REQ) - 1;
+               } else if (strncmp(p, GPT_ATTRSTR_REQ_TYPO,
+                                       sizeof(GPT_ATTRSTR_REQ_TYPO) - 1) == 0) {
+                       bit = GPT_ATTRBIT_REQ;
+                       p += sizeof(GPT_ATTRSTR_REQ_TYPO) - 1;
                } else if (strncmp(p, GPT_ATTRSTR_LEGACY,
                                        sizeof(GPT_ATTRSTR_LEGACY) - 1) == 0) {
                        bit = GPT_ATTRBIT_LEGACY;
@@ -1505,9 +1679,16 @@ static int gpt_entry_attrs_from_string(
                                        sizeof(GPT_ATTRSTR_NOBLOCK) - 1) == 0) {
                        bit = GPT_ATTRBIT_NOBLOCK;
                        p += sizeof(GPT_ATTRSTR_NOBLOCK) - 1;
-               } else if (isdigit((unsigned int) *p)) {
+
+               /* GUID:<bit> as well as <bit> */
+               } else if (isdigit((unsigned char) *p)
+                          || (strncmp(p, "GUID:", 5) == 0
+                              && isdigit((unsigned char) *(p + 5)))) {
                        char *end = NULL;
 
+                       if (*p == 'G')
+                               p += 5;
+
                        errno = 0;
                        bit = strtol(p, &end, 0);
                        if (errno || !end || end == str
@@ -1523,6 +1704,11 @@ static int gpt_entry_attrs_from_string(
                        return -EINVAL;
                }
 
+               if (*p && *p != ',' && !isblank(*p)) {
+                       fdisk_warnx(cxt, _("failed to parse GPT attribute string '%s'"), str);
+                       return -EINVAL;
+               }
+
                setbit(bits, bit);
 
                while (isblank(*p)) p++;
@@ -1530,7 +1716,7 @@ static int gpt_entry_attrs_from_string(
                        p++;
        }
 
-       e->attrs = cpu_to_le64(attrs);
+       e->attrs = attrs;
        return 0;
 }
 
@@ -1548,13 +1734,13 @@ static int gpt_get_partition(struct fdisk_context *cxt, size_t n,
 
        gpt = self_label(cxt);
 
-       if ((uint32_t) n >= le32_to_cpu(gpt->pheader->npartition_entries))
+       if (n >= gpt_get_nentries(gpt))
                return -EINVAL;
 
        gpt = self_label(cxt);
-       e = &gpt->ents[n];
+       e = gpt_get_entry(gpt, n);
 
-       pa->used = !partition_unused(e) || gpt_partition_start(e);
+       pa->used = gpt_entry_is_used(e) || gpt_partition_start(e);
        if (!pa->used)
                return 0;
 
@@ -1597,14 +1783,14 @@ static int gpt_set_partition(struct fdisk_context *cxt, size_t n,
 
        gpt = self_label(cxt);
 
-       if ((uint32_t) n >= le32_to_cpu(gpt->pheader->npartition_entries))
+       if (n >= gpt_get_nentries(gpt))
                return -EINVAL;
 
        FDISK_INIT_UNDEF(start);
        FDISK_INIT_UNDEF(end);
 
        gpt = self_label(cxt);
-       e = &gpt->ents[n];
+       e = gpt_get_entry(gpt, n);
 
        if (pa->uuid) {
                char new_u[37], old_u[37];
@@ -1649,11 +1835,20 @@ static int gpt_set_partition(struct fdisk_context *cxt, size_t n,
                end = xstart + xsize - 1ULL;
        }
 
-       if (!FDISK_IS_UNDEF(start))
+       if (!FDISK_IS_UNDEF(start)) {
+               if (start < le64_to_cpu(gpt->pheader->first_usable_lba)) {
+                       fdisk_warnx(cxt, _("The start of the partition understeps FirstUsableLBA."));
+                       return -EINVAL;
+               }
                e->lba_start = cpu_to_le64(start);
-       if (!FDISK_IS_UNDEF(end))
+       }
+       if (!FDISK_IS_UNDEF(end)) {
+               if (end > le64_to_cpu(gpt->pheader->last_usable_lba)) {
+                       fdisk_warnx(cxt, _("The end of the partition oversteps LastUsableLBA."));
+                       return -EINVAL;
+               }
                e->lba_end = cpu_to_le64(end);
-
+       }
        gpt_recompute_crc(gpt->pheader, gpt->ents);
        gpt_recompute_crc(gpt->bheader, gpt->ents);
 
@@ -1662,50 +1857,31 @@ static int gpt_set_partition(struct fdisk_context *cxt, size_t n,
 }
 
 
-/*
- * List label partitions.
- */
-static int gpt_list_disklabel(struct fdisk_context *cxt)
-{
-       assert(cxt);
-       assert(cxt->label);
-       assert(fdisk_is_label(cxt, GPT));
-
-       if (fdisk_is_details(cxt)) {
-               struct gpt_header *h = self_label(cxt)->pheader;
-
-               fdisk_info(cxt, _("First LBA: %ju"), le64_to_cpu(h->first_usable_lba));
-               fdisk_info(cxt, _("Last LBA: %ju"), le64_to_cpu(h->last_usable_lba));
-               /* TRANSLATORS: The LBA (Logical Block Address) of the backup GPT header. */
-               fdisk_info(cxt, _("Alternative LBA: %ju"), le64_to_cpu(h->alternative_lba));
-               /* TRANSLATORS: The start of the array of partition entries. */
-               fdisk_info(cxt, _("Partition entries LBA: %ju"), le64_to_cpu(h->partition_entry_lba));
-               fdisk_info(cxt, _("Allocated partition entries: %u"), le32_to_cpu(h->npartition_entries));
-       }
-
-       return 0;
-}
 
 /*
  * Write partitions.
  * Returns 0 on success, or corresponding error otherwise.
  */
 static int gpt_write_partitions(struct fdisk_context *cxt,
-                               struct gpt_header *header, struct gpt_entry *ents)
+                               struct gpt_header *header, unsigned char *ents)
 {
-       off_t offset = le64_to_cpu(header->partition_entry_lba) * cxt->sector_size;
-       uint32_t nparts = le32_to_cpu(header->npartition_entries);
-       uint32_t totwrite = nparts * le32_to_cpu(header->sizeof_partition_entry);
-       ssize_t rc;
+       off_t offset = (off_t) le64_to_cpu(header->partition_entry_lba) * cxt->sector_size;
+       size_t towrite;
+       ssize_t ssz;
+       int rc;
+
+       rc = gpt_sizeof_ents(header, &towrite);
+       if (rc)
+               return rc;
 
        if (offset != lseek(cxt->dev_fd, offset, SEEK_SET))
-               goto fail;
+               return -errno;
 
-       rc = write(cxt->dev_fd, ents, totwrite);
-       if (rc > 0 && totwrite == (uint32_t) rc)
-               return 0;
-fail:
-       return -errno;
+       ssz = write(cxt->dev_fd, ents, towrite);
+       if (ssz < 0 || (ssize_t) towrite != ssz)
+               return -errno;
+
+       return 0;
 }
 
 /*
@@ -1759,11 +1935,11 @@ static int gpt_write_pmbr(struct fdisk_context *cxt)
         * Set size_in_lba to the size of the disk minus one. If the size of the disk
         * is too large to be represented by a 32bit LBA (2Tb), set it to 0xFFFFFFFF.
         */
-       if (cxt->total_sectors - 1 > 0xFFFFFFFFULL)
+       if (cxt->total_sectors - 1ULL > 0xFFFFFFFFULL)
                pmbr->partition_record[0].size_in_lba = cpu_to_le32(0xFFFFFFFF);
        else
                pmbr->partition_record[0].size_in_lba =
-                       cpu_to_le32(cxt->total_sectors - 1UL);
+                       cpu_to_le32((uint32_t) (cxt->total_sectors - 1ULL));
 
        offset = GPT_PMBR_LBA * cxt->sector_size;
        if (offset != lseek(cxt->dev_fd, offset, SEEK_SET))
@@ -1799,11 +1975,11 @@ static int gpt_write_disklabel(struct fdisk_context *cxt)
                goto err0;
 
        /* check that the backup header is properly placed */
-       if (le64_to_cpu(gpt->pheader->alternative_lba) < cxt->total_sectors - 1)
+       if (le64_to_cpu(gpt->pheader->alternative_lba) < cxt->total_sectors - 1ULL)
                /* TODO: correct this (with user authorization) and write */
                goto err0;
 
-       if (check_overlap_partitions(gpt->pheader, gpt->ents))
+       if (check_overlap_partitions(gpt))
                goto err0;
 
        /* recompute CRCs for both headers */
@@ -1850,7 +2026,7 @@ err1:
 /*
  * Verify data integrity and report any found problems for:
  *   - primary and backup header validations
- *   - paritition validations
+ *   - partition validations
  */
 static int gpt_verify_disklabel(struct fdisk_context *cxt)
 {
@@ -1863,8 +2039,10 @@ static int gpt_verify_disklabel(struct fdisk_context *cxt)
        assert(fdisk_is_label(cxt, GPT));
 
        gpt = self_label(cxt);
+       if (!gpt)
+               return -EINVAL;
 
-       if (!gpt || !gpt->bheader) {
+       if (!gpt->bheader) {
                nerror++;
                fdisk_warnx(cxt, _("Disk does not contain a valid backup header."));
        }
@@ -1916,21 +2094,21 @@ static int gpt_verify_disklabel(struct fdisk_context *cxt)
                fdisk_warnx(cxt, _("Primary and backup header mismatch."));
        }
 
-       ptnum = check_overlap_partitions(gpt->pheader, gpt->ents);
+       ptnum = check_overlap_partitions(gpt);
        if (ptnum) {
                nerror++;
                fdisk_warnx(cxt, _("Partition %u overlaps with partition %u."),
                                ptnum, ptnum+1);
        }
 
-       ptnum = check_too_big_partitions(gpt->pheader, gpt->ents, cxt->total_sectors);
+       ptnum = check_too_big_partitions(gpt, cxt->total_sectors);
        if (ptnum) {
                nerror++;
                fdisk_warnx(cxt, _("Partition %u is too big for the disk."),
                                ptnum);
        }
 
-       ptnum = check_start_after_end_paritions(gpt->pheader, gpt->ents);
+       ptnum = check_start_after_end_partitions(gpt);
        if (ptnum) {
                nerror++;
                fdisk_warnx(cxt, _("Partition %u ends before it starts."),
@@ -1944,12 +2122,11 @@ static int gpt_verify_disklabel(struct fdisk_context *cxt)
 
                fdisk_info(cxt, _("No errors detected."));
                fdisk_info(cxt, _("Header version: %s"), gpt_get_header_revstr(gpt->pheader));
-               fdisk_info(cxt, _("Using %u out of %d partitions."),
-                      partitions_in_use(gpt->pheader, gpt->ents),
-                      le32_to_cpu(gpt->pheader->npartition_entries));
+               fdisk_info(cxt, _("Using %zu out of %zu partitions."),
+                      partitions_in_use(gpt),
+                      gpt_get_nentries(gpt));
 
-               free_sectors = get_free_sectors(cxt, gpt->pheader, gpt->ents,
-                                               &nsegments, &largest_segment);
+               free_sectors = get_free_sectors(cxt, gpt, &nsegments, &largest_segment);
                if (largest_segment)
                        strsz = size_to_human_string(SIZE_SUFFIX_SPACE | SIZE_SUFFIX_3LETTER,
                                        largest_segment * cxt->sector_size);
@@ -1981,20 +2158,19 @@ static int gpt_delete_partition(struct fdisk_context *cxt,
 
        gpt = self_label(cxt);
 
-       if (partnum >= cxt->label->nparts_max
-           ||  partition_unused(&gpt->ents[partnum]))
+       if (partnum >= cxt->label->nparts_max)
                return -EINVAL;
 
-       /* hasta la vista, baby! */
-       memset(&gpt->ents[partnum], 0, sizeof(struct gpt_entry));
-       if (!partition_unused(&gpt->ents[partnum]))
+       if (!gpt_entry_is_used(gpt_get_entry(gpt, partnum)))
                return -EINVAL;
-       else {
-               gpt_recompute_crc(gpt->pheader, gpt->ents);
-               gpt_recompute_crc(gpt->bheader, gpt->ents);
-               cxt->label->nparts_cur--;
-               fdisk_label_set_changed(cxt->label, 1);
-       }
+
+       /* hasta la vista, baby! */
+       gpt_zeroize_entry(gpt, partnum);
+
+       gpt_recompute_crc(gpt->pheader, gpt->ents);
+       gpt_recompute_crc(gpt->bheader, gpt->ents);
+       cxt->label->nparts_cur--;
+       fdisk_label_set_changed(cxt->label, 1);
 
        return 0;
 }
@@ -2012,7 +2188,7 @@ static int gpt_add_partition(
        struct gpt_guid typeid;
        struct fdisk_gpt_label *gpt;
        struct gpt_header *pheader;
-       struct gpt_entry *e, *ents;
+       struct gpt_entry *e;
        struct fdisk_ask *ask = NULL;
        size_t partnum;
        int rc;
@@ -2022,25 +2198,31 @@ static int gpt_add_partition(
        assert(fdisk_is_label(cxt, GPT));
 
        gpt = self_label(cxt);
+
+       assert(gpt);
+       assert(gpt->pheader);
+       assert(gpt->ents);
+
        pheader = gpt->pheader;
-       ents = gpt->ents;
 
        rc = fdisk_partition_next_partno(pa, cxt, &partnum);
        if (rc) {
                DBG(LABEL, ul_debug("GPT failed to get next partno"));
                return rc;
        }
-       if (!partition_unused(&ents[partnum])) {
+
+       assert(partnum < gpt_get_nentries(gpt));
+
+       if (gpt_entry_is_used(gpt_get_entry(gpt, partnum))) {
                fdisk_warnx(cxt, _("Partition %zu is already defined.  "
                                   "Delete it before re-adding it."), partnum +1);
                return -ERANGE;
        }
-       if (le32_to_cpu(pheader->npartition_entries) ==
-                       partitions_in_use(pheader, ents)) {
+       if (gpt_get_nentries(gpt) == partitions_in_use(gpt)) {
                fdisk_warnx(cxt, _("All partitions are already in use."));
                return -ENOSPC;
        }
-       if (!get_free_sectors(cxt, pheader, ents, NULL, NULL)) {
+       if (!get_free_sectors(cxt, gpt, NULL, NULL)) {
                fdisk_warnx(cxt, _("No free sectors available."));
                return -ENOSPC;
        }
@@ -2051,36 +2233,38 @@ static int gpt_add_partition(
        if (rc)
                return rc;
 
-       disk_f = find_first_available(pheader, ents, le64_to_cpu(pheader->first_usable_lba));
+       disk_f = find_first_available(gpt, le64_to_cpu(pheader->first_usable_lba));
+       e = gpt_get_entry(gpt, 0);
 
        /* if first sector no explicitly defined then ignore small gaps before
         * the first partition */
        if ((!pa || !fdisk_partition_has_start(pa))
-           && !partition_unused(&ents[0])
-           && disk_f < gpt_partition_start(&ents[0])) {
+           && gpt_entry_is_used(e)
+           && disk_f < gpt_partition_start(e)) {
 
                do {
                        uint64_t x;
-                       DBG(LABEL, ul_debug("testing first sector %ju", disk_f));
-                       disk_f = find_first_available(pheader, ents, disk_f);
+                       DBG(LABEL, ul_debug("testing first sector %"PRIu64"", disk_f));
+                       disk_f = find_first_available(gpt, disk_f);
                        if (!disk_f)
                                break;
-                       x = find_last_free(pheader, ents, disk_f);
+                       x = find_last_free(gpt, disk_f);
                        if (x - disk_f >= cxt->grain / cxt->sector_size)
                                break;
-                       DBG(LABEL, ul_debug("first sector %ju addresses to small space, continue...", disk_f));
-                       disk_f = x + 1;
+                       DBG(LABEL, ul_debug("first sector %"PRIu64" addresses to small space, continue...", disk_f));
+                       disk_f = x + 1ULL;
                } while(1);
 
                if (disk_f == 0)
-                       disk_f = find_first_available(pheader, ents, le64_to_cpu(pheader->first_usable_lba));
+                       disk_f = find_first_available(gpt, le64_to_cpu(pheader->first_usable_lba));
        }
 
-       disk_l = find_last_free_sector(pheader, ents);
+       e = NULL;
+       disk_l = find_last_free_sector(gpt);
 
        /* the default is the largest free space */
-       dflt_f = find_first_in_largest(pheader, ents);
-       dflt_l = find_last_free(pheader, ents, dflt_f);
+       dflt_f = find_first_in_largest(gpt);
+       dflt_l = find_last_free(gpt, dflt_f);
 
        /* align the default in range <dflt_f,dflt_l>*/
        dflt_f = fdisk_align_lba_in_range(cxt, dflt_f, dflt_f, dflt_l);
@@ -2090,9 +2274,9 @@ static int gpt_add_partition(
                user_f = dflt_f;
 
        } else if (pa && fdisk_partition_has_start(pa)) {
-               DBG(LABEL, ul_debug("first sector defined: %ju", pa->start));
-               if (pa->start != find_first_available(pheader, ents, pa->start)) {
-                       fdisk_warnx(cxt, _("Sector %ju already used."), pa->start);
+               DBG(LABEL, ul_debug("first sector defined: %ju",  (uintmax_t)pa->start));
+               if (pa->start != find_first_available(gpt, pa->start)) {
+                       fdisk_warnx(cxt, _("Sector %ju already used."),  (uintmax_t)pa->start);
                        return -ERANGE;
                }
                user_f = pa->start;
@@ -2116,7 +2300,7 @@ static int gpt_add_partition(
                                goto done;
 
                        user_f = fdisk_ask_number_get_result(ask);
-                       if (user_f != find_first_available(pheader, ents, user_f)) {
+                       if (user_f != find_first_available(gpt, user_f)) {
                                fdisk_warnx(cxt, _("Sector %ju already used."), user_f);
                                continue;
                        }
@@ -2126,20 +2310,20 @@ static int gpt_add_partition(
 
 
        /* Last sector */
-       dflt_l = find_last_free(pheader, ents, user_f);
+       dflt_l = find_last_free(gpt, user_f);
 
        if (pa && pa->end_follow_default) {
                user_l = dflt_l;
 
        } else if (pa && fdisk_partition_has_size(pa)) {
                user_l = user_f + pa->size - 1;
-               DBG(LABEL, ul_debug("size defined: %ju, end: %ju (last possible: %ju)",
-                                       pa->size, user_l, dflt_l));
+               DBG(LABEL, ul_debug("size defined: %ju, end: %"PRIu64" (last possible: %"PRIu64")",
+                                        (uintmax_t)pa->size, user_l, dflt_l));
                if (user_l != dflt_l && !pa->size_explicit
                    && user_l - user_f > (cxt->grain / fdisk_get_sector_size(cxt))) {
                        user_l = fdisk_align_lba_in_range(cxt, user_l, user_f, dflt_l);
                        if (user_l > user_f)
-                               user_l -= 1;
+                               user_l -= 1ULL;
                }
        } else {
                for (;;) {
@@ -2147,6 +2331,8 @@ static int gpt_add_partition(
                                ask = fdisk_new_ask();
                        else
                                fdisk_reset_ask(ask);
+                       if (!ask)
+                               return -ENOMEM;
 
                        fdisk_ask_set_query(ask, _("Last sector, +sectors or +size{K,M,G,T,P}"));
                        fdisk_ask_set_type(ask, FDISK_ASKTYPE_OFFSET);
@@ -2162,16 +2348,15 @@ static int gpt_add_partition(
 
                        user_l = fdisk_ask_number_get_result(ask);
                        if (fdisk_ask_number_is_relative(ask)) {
-                               user_l = fdisk_align_lba_in_range(cxt, user_l, user_f, dflt_l) - 1;
-
-                               /* no space for anything useful, use all space
-                               if (user_l + (cxt->grain / cxt->sector_size) > dflt_l)
-                                       user_l = dflt_l;
-                               */
+                               user_l = fdisk_align_lba_in_range(cxt, user_l, user_f, dflt_l);
+                               if (user_l > user_f)
+                                       user_l -= 1ULL;
                        }
 
-                       if (user_l > user_f && user_l <= disk_l)
+                       if (user_l >= user_f && user_l <= disk_l)
                                break;
+
+                       fdisk_warnx(cxt, _("Value out of range."));
                }
        }
 
@@ -2182,10 +2367,26 @@ static int gpt_add_partition(
                goto done;
        }
 
+       /* Be paranoid and check against on-disk setting rather than against libfdisk cxt */
+       if (user_l > le64_to_cpu(pheader->last_usable_lba)) {
+               fdisk_warnx(cxt, _("The last usable GPT sector is %ju, but %ju is requested."),
+                               le64_to_cpu(pheader->last_usable_lba), user_l);
+               rc = -EINVAL;
+               goto done;
+       }
+
+       if (user_f < le64_to_cpu(pheader->first_usable_lba)) {
+               fdisk_warnx(cxt, _("The first usable GPT sector is %ju, but %ju is requested."),
+                               le64_to_cpu(pheader->first_usable_lba), user_f);
+               rc = -EINVAL;
+               goto done;
+       }
+
        assert(!FDISK_IS_UNDEF(user_l));
        assert(!FDISK_IS_UNDEF(user_f));
+       assert(partnum < gpt_get_nentries(gpt));
 
-       e = &ents[partnum];
+       e = gpt_get_entry(gpt, partnum);
        e->lba_end = cpu_to_le64(user_l);
        e->lba_start = cpu_to_le64(user_f);
 
@@ -2212,14 +2413,14 @@ static int gpt_add_partition(
        if (pa && pa->attrs)
                gpt_entry_attrs_from_string(cxt, e, pa->attrs);
 
-       DBG(LABEL, ul_debug("GPT new partition: partno=%zu, start=%ju, end=%ju, size=%ju",
+       DBG(LABEL, ul_debug("GPT new partition: partno=%zu, start=%"PRIu64", end=%"PRIu64", size=%"PRIu64"",
                                partnum,
                                gpt_partition_start(e),
                                gpt_partition_end(e),
                                gpt_partition_size(e)));
 
-       gpt_recompute_crc(gpt->pheader, ents);
-       gpt_recompute_crc(gpt->bheader, ents);
+       gpt_recompute_crc(gpt->pheader, gpt->ents);
+       gpt_recompute_crc(gpt->bheader, gpt->ents);
 
        /* report result */
        {
@@ -2228,7 +2429,7 @@ static int gpt_add_partition(
                cxt->label->nparts_cur++;
                fdisk_label_set_changed(cxt->label, 1);
 
-               t = gpt_partition_parttype(cxt, &ents[partnum]);
+               t = gpt_partition_parttype(cxt, e);
                fdisk_info_new_partition(cxt, partnum + 1, user_f, user_l, t);
                fdisk_unref_parttype(t);
        }
@@ -2247,7 +2448,7 @@ done:
 static int gpt_create_disklabel(struct fdisk_context *cxt)
 {
        int rc = 0;
-       ssize_t esz = 0;
+       size_t esz = 0;
        char str[37];
        struct fdisk_gpt_label *gpt;
 
@@ -2293,8 +2494,9 @@ static int gpt_create_disklabel(struct fdisk_context *cxt)
        if (rc < 0)
                goto done;
 
-       esz = le32_to_cpu(gpt->pheader->npartition_entries) *
-             le32_to_cpu(gpt->pheader->sizeof_partition_entry);
+       rc = gpt_sizeof_ents(gpt->pheader, &esz);
+       if (rc)
+               goto done;
        gpt->ents = calloc(1, esz);
        if (!gpt->ents) {
                rc = -ENOMEM;
@@ -2303,7 +2505,7 @@ static int gpt_create_disklabel(struct fdisk_context *cxt)
        gpt_recompute_crc(gpt->pheader, gpt->ents);
        gpt_recompute_crc(gpt->bheader, gpt->ents);
 
-       cxt->label->nparts_max = le32_to_cpu(gpt->pheader->npartition_entries);
+       cxt->label->nparts_max = gpt_get_nentries(gpt);
        cxt->label->nparts_cur = 0;
 
        guid_to_string(&gpt->pheader->disk_guid, str);
@@ -2313,25 +2515,6 @@ done:
        return rc;
 }
 
-static int gpt_get_disklabel_id(struct fdisk_context *cxt, char **id)
-{
-       struct fdisk_gpt_label *gpt;
-       char str[37];
-
-       assert(cxt);
-       assert(id);
-       assert(cxt->label);
-       assert(fdisk_is_label(cxt, GPT));
-
-       gpt = self_label(cxt);
-       guid_to_string(&gpt->pheader->disk_guid, str);
-
-       *id = strdup(str);
-       if (!*id)
-               return -ENOMEM;
-       return 0;
-}
-
 static int gpt_set_disklabel_id(struct fdisk_context *cxt)
 {
        struct fdisk_gpt_label *gpt;
@@ -2356,7 +2539,7 @@ static int gpt_set_disklabel_id(struct fdisk_context *cxt)
                return rc;
        }
 
-       gpt_get_disklabel_id(cxt, &old);
+       old = gpt_get_header_id(gpt->pheader);
 
        gpt->pheader->disk_guid = uuid;
        gpt->bheader->disk_guid = uuid;
@@ -2364,7 +2547,7 @@ static int gpt_set_disklabel_id(struct fdisk_context *cxt)
        gpt_recompute_crc(gpt->pheader, gpt->ents);
        gpt_recompute_crc(gpt->bheader, gpt->ents);
 
-       gpt_get_disklabel_id(cxt, &new);
+       new = gpt_get_header_id(gpt->pheader);
 
        fdisk_info(cxt, _("Disk identifier changed from %s to %s."), old, new);
 
@@ -2374,6 +2557,136 @@ static int gpt_set_disklabel_id(struct fdisk_context *cxt)
        return 0;
 }
 
+static int gpt_check_table_overlap(struct fdisk_context *cxt,
+                                  uint64_t first_usable,
+                                  uint64_t last_usable)
+{
+       struct fdisk_gpt_label *gpt = self_label(cxt);
+       size_t i;
+       int rc = 0;
+
+       /* First check if there's enough room for the table. last_lba may have wrapped */
+       if (first_usable > cxt->total_sectors || /* far too little space */
+           last_usable > cxt->total_sectors || /* wrapped */
+           first_usable > last_usable) { /* too little space */
+               fdisk_warnx(cxt, _("Not enough space for new partition table!"));
+               return -ENOSPC;
+       }
+
+       /* check that all partitions fit in the remaining space */
+       for (i = 0; i < gpt_get_nentries(gpt); i++) {
+               struct gpt_entry *e = gpt_get_entry(gpt, i);
+
+               if (!gpt_entry_is_used(e))
+                       continue;
+               if (gpt_partition_start(e) < first_usable) {
+                       fdisk_warnx(cxt, _("Partition #%zu out of range (minimal start is %"PRIu64" sectors)"),
+                                   i + 1, first_usable);
+                       rc = -EINVAL;
+               }
+               if (gpt_partition_end(e) > last_usable) {
+                       fdisk_warnx(cxt, _("Partition #%zu out of range (maximal end is %"PRIu64" sectors)"),
+                                   i + 1, last_usable - 1ULL);
+                       rc = -EINVAL;
+               }
+       }
+       return rc;
+}
+
+/**
+ * fdisk_gpt_set_npartitions:
+ * @cxt: context
+ * @entries: new size
+ *
+ * Elarge GPT entries array if possible. The function check if an existing
+ * partition does not overlap the entries array area. If yes, then it report
+ * warning and returns -EINVAL.
+ *
+ * Returns: 0 on success, < 0 on error.
+ * Since: 2.29
+ */
+int fdisk_gpt_set_npartitions(struct fdisk_context *cxt, uint32_t entries)
+{
+       struct fdisk_gpt_label *gpt;
+       size_t old_size, new_size;
+       uint32_t old;
+       uint64_t first_usable, last_usable;
+       int rc;
+
+       assert(cxt);
+       assert(cxt->label);
+
+       if (!fdisk_is_label(cxt, GPT))
+               return -EINVAL;
+
+       gpt = self_label(cxt);
+
+       old = le32_to_cpu(gpt->pheader->npartition_entries);
+       if (old == entries)
+               return 0;       /* do nothing, say nothing */
+
+       /* calculate the size (bytes) of the entries array */
+       rc = gpt_calculate_sizeof_ents(gpt->pheader, entries, &new_size);
+       if (rc) {
+               fdisk_warnx(cxt, _("The number of the partition has to be smaller than %zu."),
+                               UINT32_MAX / le32_to_cpu(gpt->pheader->sizeof_partition_entry));
+               return rc;
+       }
+
+       rc = gpt_calculate_sizeof_ents(gpt->pheader, old, &old_size);
+       if (rc)
+               return rc;
+
+       /* calculate new range of usable LBAs */
+       first_usable = (uint64_t) (new_size / cxt->sector_size) + 2ULL;
+       last_usable = cxt->total_sectors - 2ULL - (uint64_t) (new_size / cxt->sector_size);
+
+       /* if expanding the table, first check that everything fits,
+        * then allocate more memory and zero. */
+       if (entries > old) {
+               unsigned char *ents;
+
+               rc = gpt_check_table_overlap(cxt, first_usable, last_usable);
+               if (rc)
+                       return rc;
+               ents = realloc(gpt->ents, new_size);
+               if (!ents) {
+                       fdisk_warnx(cxt, _("Cannot allocate memory!"));
+                       return -ENOMEM;
+               }
+               memset(ents + old_size, 0, new_size - old_size);
+               gpt->ents = ents;
+       }
+
+       /* everything's ok, apply the new size */
+       gpt->pheader->npartition_entries = cpu_to_le32(entries);
+       gpt->bheader->npartition_entries = cpu_to_le32(entries);
+
+       /* usable LBA addresses will have changed */
+       fdisk_set_first_lba(cxt, first_usable);
+       fdisk_set_last_lba(cxt, last_usable);
+       gpt->pheader->first_usable_lba = cpu_to_le64(first_usable);
+       gpt->bheader->first_usable_lba = cpu_to_le64(first_usable);
+       gpt->pheader->last_usable_lba = cpu_to_le64(last_usable);
+       gpt->bheader->last_usable_lba = cpu_to_le64(last_usable);
+
+
+       /* The backup header must be recalculated */
+       gpt_mknew_header_common(cxt, gpt->bheader, le64_to_cpu(gpt->pheader->alternative_lba));
+
+       /* CRCs will have changed */
+       gpt_recompute_crc(gpt->pheader, gpt->ents);
+       gpt_recompute_crc(gpt->bheader, gpt->ents);
+
+       /* update library info */
+       cxt->label->nparts_max = gpt_get_nentries(gpt);
+
+       fdisk_info(cxt, _("Partition table length changed from %"PRIu32" to %"PRIu64"."), old, entries);
+
+       fdisk_label_set_changed(cxt->label, 1);
+       return 0;
+}
+
 static int gpt_part_is_used(struct fdisk_context *cxt, size_t i)
 {
        struct fdisk_gpt_label *gpt;
@@ -2385,11 +2698,12 @@ static int gpt_part_is_used(struct fdisk_context *cxt, size_t i)
 
        gpt = self_label(cxt);
 
-       if ((uint32_t) i >= le32_to_cpu(gpt->pheader->npartition_entries))
+       if (i >= gpt_get_nentries(gpt))
                return 0;
-       e = &gpt->ents[i];
 
-       return !partition_unused(e) || gpt_partition_start(e);
+       e = gpt_get_entry(gpt, i);
+
+       return gpt_entry_is_used(e) || gpt_partition_start(e);
 }
 
 /**
@@ -2414,13 +2728,86 @@ int fdisk_gpt_is_hybrid(struct fdisk_context *cxt)
        return valid_pmbr(cxt) == GPT_MBR_HYBRID;
 }
 
+/**
+ * fdisk_gpt_get_partition_attrs:
+ * @cxt: context
+ * @partnum: partition number
+ * @attrs: GPT partition attributes
+ *
+ * Sets @attrs for the given partition
+ *
+ * Returns: 0 on success, <0 on error.
+ */
+int fdisk_gpt_get_partition_attrs(
+               struct fdisk_context *cxt,
+               size_t partnum,
+               uint64_t *attrs)
+{
+       struct fdisk_gpt_label *gpt;
+
+       assert(cxt);
+       assert(cxt->label);
+
+       if (!fdisk_is_label(cxt, GPT))
+               return -EINVAL;
+
+       gpt = self_label(cxt);
+
+       if (partnum >= gpt_get_nentries(gpt))
+               return -EINVAL;
+
+       *attrs = le64_to_cpu(gpt_get_entry(gpt, partnum)->attrs);
+       return 0;
+}
+
+/**
+ * fdisk_gpt_set_partition_attrs:
+ * @cxt: context
+ * @partnum: partition number
+ * @attrs: GPT partition attributes
+ *
+ * Sets the GPT partition attributes field to @attrs.
+ *
+ * Returns: 0 on success, <0 on error.
+ */
+int fdisk_gpt_set_partition_attrs(
+               struct fdisk_context *cxt,
+               size_t partnum,
+               uint64_t attrs)
+{
+       struct fdisk_gpt_label *gpt;
+
+       assert(cxt);
+       assert(cxt->label);
+
+       if (!fdisk_is_label(cxt, GPT))
+               return -EINVAL;
+
+       DBG(LABEL, ul_debug("GPT entry attributes change requested partno=%zu", partnum));
+       gpt = self_label(cxt);
+
+       if (partnum >= gpt_get_nentries(gpt))
+               return -EINVAL;
+
+       gpt_get_entry(gpt, partnum)->attrs = cpu_to_le64(attrs);
+       fdisk_info(cxt, _("The attributes on partition %zu changed to 0x%016" PRIx64 "."),
+                       partnum + 1, attrs);
+
+       gpt_recompute_crc(gpt->pheader, gpt->ents);
+       gpt_recompute_crc(gpt->bheader, gpt->ents);
+       fdisk_label_set_changed(cxt->label, 1);
+       return 0;
+}
+
 static int gpt_toggle_partition_flag(
                struct fdisk_context *cxt,
                size_t i,
                unsigned long flag)
 {
        struct fdisk_gpt_label *gpt;
-       uint64_t attrs, tmp;
+       struct gpt_entry *e;
+       uint64_t attrs;
+       uintmax_t tmp;
        char *bits;
        const char *name = NULL;
        int bit = -1, rc;
@@ -2432,10 +2819,11 @@ static int gpt_toggle_partition_flag(
        DBG(LABEL, ul_debug("GPT entry attribute change requested partno=%zu", i));
        gpt = self_label(cxt);
 
-       if ((uint32_t) i >= le32_to_cpu(gpt->pheader->npartition_entries))
+       if (i >= gpt_get_nentries(gpt))
                return -EINVAL;
 
-       attrs = le64_to_cpu(gpt->ents[i].attrs);
+       e = gpt_get_entry(gpt, i);
+       attrs = e->attrs;
        bits = (char *) &attrs;
 
        switch (flag) {
@@ -2476,7 +2864,7 @@ static int gpt_toggle_partition_flag(
        else
                clrbit(bits, bit);
 
-       gpt->ents[i].attrs = cpu_to_le64(attrs);
+       e->attrs = attrs;
 
        if (flag == GPT_FLAG_GUIDSPECIFIC)
                fdisk_info(cxt, isset(bits, bit) ?
@@ -2499,14 +2887,14 @@ static int gpt_entry_cmp_start(const void *a, const void *b)
 {
        struct gpt_entry *ae = (struct gpt_entry *) a,
                         *be = (struct gpt_entry *) b;
-       int au = partition_unused(ae),
-           bu = partition_unused(be);
+       int au = gpt_entry_is_used(ae),
+           bu = gpt_entry_is_used(be);
 
-       if (au && bu)
+       if (!au && !bu)
                return 0;
-       if (au)
+       if (!au)
                return 1;
-       if (bu)
+       if (!bu)
                return -1;
 
        return cmp_numbers(gpt_partition_start(ae), gpt_partition_start(be));
@@ -2516,14 +2904,24 @@ static int gpt_entry_cmp_start(const void *a, const void *b)
 static int gpt_reorder(struct fdisk_context *cxt)
 {
        struct fdisk_gpt_label *gpt;
-       size_t nparts;
+       size_t i, nparts, mess;
 
        assert(cxt);
        assert(cxt->label);
        assert(fdisk_is_label(cxt, GPT));
 
        gpt = self_label(cxt);
-       nparts = le32_to_cpu(gpt->pheader->npartition_entries);
+       nparts = gpt_get_nentries(gpt);
+
+       for (i = 0, mess = 0; mess == 0 && i + 1 < nparts; i++)
+               mess = gpt_entry_cmp_start(
+                               (const void *) gpt_get_entry(gpt, i),
+                               (const void *) gpt_get_entry(gpt, i + 1)) > 0;
+
+       if (!mess) {
+               fdisk_info(cxt, _("Nothing to do. Ordering is correct already."));
+               return 1;
+       }
 
        qsort(gpt->ents, nparts, sizeof(struct gpt_entry),
                        gpt_entry_cmp_start);
@@ -2532,7 +2930,6 @@ static int gpt_reorder(struct fdisk_context *cxt)
        gpt_recompute_crc(gpt->bheader, gpt->ents);
        fdisk_label_set_changed(cxt->label, 1);
 
-       fdisk_info(cxt, _("Done."));
        return 0;
 }
 
@@ -2591,16 +2988,15 @@ static const struct fdisk_label_operations gpt_operations =
        .write          = gpt_write_disklabel,
        .verify         = gpt_verify_disklabel,
        .create         = gpt_create_disklabel,
-       .list           = gpt_list_disklabel,
        .locate         = gpt_locate_disklabel,
-       .reorder        = gpt_reorder,
-       .get_id         = gpt_get_disklabel_id,
+       .get_item       = gpt_get_disklabel_item,
        .set_id         = gpt_set_disklabel_id,
 
        .get_part       = gpt_get_partition,
        .set_part       = gpt_set_partition,
        .add_part       = gpt_add_partition,
        .del_part       = gpt_delete_partition,
+       .reorder        = gpt_reorder,
 
        .part_is_used   = gpt_part_is_used,
        .part_toggle_flag = gpt_toggle_partition_flag,
@@ -2653,3 +3049,62 @@ struct fdisk_label *fdisk_new_gpt_label(struct fdisk_context *cxt)
 
        return lb;
 }
+
+#ifdef TEST_PROGRAM
+static int test_getattr(struct fdisk_test *ts, int argc, char *argv[])
+{
+       const char *disk = argv[1];
+       size_t part = strtoul(argv[2], NULL, 0) - 1;
+       struct fdisk_context *cxt;
+       uint64_t atters = 0;
+
+       cxt = fdisk_new_context();
+       fdisk_assign_device(cxt, disk, 1);
+
+       if (!fdisk_is_label(cxt, GPT))
+               return EXIT_FAILURE;
+
+       if (fdisk_gpt_get_partition_attrs(cxt, part, &atters))
+               return EXIT_FAILURE;
+
+       printf("%s: 0x%016" PRIx64 "\n", argv[2], atters);
+
+       fdisk_unref_context(cxt);
+       return 0;
+}
+
+static int test_setattr(struct fdisk_test *ts, int argc, char *argv[])
+{
+       const char *disk = argv[1];
+       size_t part = strtoul(argv[2], NULL, 0) - 1;
+       uint64_t atters = strtoull(argv[3], NULL, 0);
+       struct fdisk_context *cxt;
+
+       cxt = fdisk_new_context();
+       fdisk_assign_device(cxt, disk, 0);
+
+       if (!fdisk_is_label(cxt, GPT))
+               return EXIT_FAILURE;
+
+       if (fdisk_gpt_set_partition_attrs(cxt, part, atters))
+               return EXIT_FAILURE;
+
+       if (fdisk_write_disklabel(cxt))
+               return EXIT_FAILURE;
+
+       fdisk_unref_context(cxt);
+       return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       struct fdisk_test tss[] = {
+               { "--getattr",  test_getattr,  "<disk> <partition>             print attributes" },
+               { "--setattr",  test_setattr,  "<disk> <partition> <value>     set attributes" },
+               { NULL }
+       };
+
+       return fdisk_run_test(tss, argc, argv);
+}
+
+#endif