]> 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 f795b35a2617f28d752e243eb0015f94c3c3ab40..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"
 
@@ -293,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);
@@ -406,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)
 {
@@ -427,12 +434,49 @@ 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 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 !memcmp(&e->type, &GPT_UNUSED_ENTRY_GUID,
-                       sizeof(struct gpt_guid));
+       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];
@@ -442,7 +486,6 @@ static char *gpt_get_header_id(struct gpt_header *header)
        return strdup(str);
 }
 
-
 /*
  * Builds a clean new valid protective MBR - will wipe out any existing data.
  * Returns 0 on success, otherwise < 0 on error.
@@ -472,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;
 }
@@ -487,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);
        }
 }
 
@@ -561,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;
@@ -612,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) {
@@ -621,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."));
@@ -632,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."));
@@ -672,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)
@@ -710,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;
@@ -767,12 +812,11 @@ static int valid_pmbr(struct fdisk_context *cxt)
         * 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);
                }
        }
@@ -792,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;
 }
 
@@ -815,28 +859,38 @@ static ssize_t read_lba(struct fdisk_context *cxt, uint64_t lba,
 
 
 /* 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;
@@ -849,7 +903,7 @@ fail:
 static inline uint32_t count_crc32(const unsigned char *buf, size_t len,
                                   size_t ex_off, size_t ex_len)
 {
-       return (crc32_exclude_offset(~0L, buf, len, ex_off, ex_len) ^ ~0L);
+       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)
@@ -860,23 +914,23 @@ static inline uint32_t gpt_header_count_crc32(struct gpt_header *header)
                        sizeof(header->crc32));                 /* size of excluded area */
 }
 
-static inline uint32_t gpt_entryarr_count_crc32(struct gpt_header *header, struct gpt_entry *ents)
+static inline uint32_t gpt_entryarr_count_crc32(struct gpt_header *header, unsigned char *ents)
 {
        size_t arysz = 0;
 
-       arysz = le32_to_cpu(header->npartition_entries) *
-               le32_to_cpu(header->sizeof_partition_entry);
+       if (gpt_sizeof_ents(header, &arysz))
+               return 0;
 
-       return count_crc32((unsigned char *) ents, arysz, 0, 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)
 {
        if (!header)
                return;
@@ -891,7 +945,7 @@ static void gpt_recompute_crc(struct gpt_header *header, struct gpt_entry *ents)
  * 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 orgcrc = le32_to_cpu(header->crc32),
                 crc = gpt_header_count_crc32(header);
@@ -916,8 +970,7 @@ 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)
 {
        if (!header || !ents)
                return 0;
@@ -971,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)
@@ -1018,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;
 }
 
@@ -1060,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 */
        }
@@ -1119,7 +1170,7 @@ static int gpt_get_disklabel_item(struct fdisk_context *cxt, struct fdisk_labeli
                break;
        default:
                if (item->id < __FDISK_NLABELITEMS)
-                       rc = 1; /* unssupported generic item */
+                       rc = 1; /* unsupported generic item */
                else
                        rc = 2; /* out of range */
                break;
@@ -1131,17 +1182,20 @@ static int gpt_get_disklabel_item(struct fdisk_context *cxt, struct fdisk_labeli
 /*
  * 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;
 }
 
@@ -1150,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;
+
+       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))
                        continue;
-               if (gpt_partition_end(&ents[i]) >= sectors)
+               if (gpt_partition_end(e) >= sectors)
                        return i + 1;
        }
 
@@ -1169,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 < 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))
                        continue;
-               if (gpt_partition_start(&ents[i]) > gpt_partition_end(&ents[i]))
+               if (gpt_partition_start(e) > gpt_partition_end(e))
                        return i + 1;
        }
 
@@ -1200,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;
 
-       for (i = 0; i < le32_to_cpu(header->npartition_entries); i++)
+       assert(gpt);
+       assert(gpt->pheader);
+       assert(gpt->ents);
+
+       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;
                        }
                }
@@ -1223,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
@@ -1251,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;
                        }
                }
@@ -1272,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;
 }
 
@@ -1324,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;
 }
 
@@ -1355,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;
@@ -1367,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);
 
@@ -1445,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"));
@@ -1582,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;
@@ -1597,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
@@ -1615,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++;
@@ -1640,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;
 
@@ -1689,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];
@@ -1743,14 +1837,14 @@ static int gpt_set_partition(struct fdisk_context *cxt, size_t n,
 
        if (!FDISK_IS_UNDEF(start)) {
                if (start < le64_to_cpu(gpt->pheader->first_usable_lba)) {
-                       fdisk_warnx(cxt, _("The begin of the partition overflows FirstUsableLBA."));
+                       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 (end > le64_to_cpu(gpt->pheader->last_usable_lba)) {
-                       fdisk_warnx(cxt, _("The end of the partition overflows LastUsableLBA."));
+                       fdisk_warnx(cxt, _("The end of the partition oversteps LastUsableLBA."));
                        return -EINVAL;
                }
                e->lba_end = cpu_to_le64(end);
@@ -1769,21 +1863,25 @@ static int gpt_set_partition(struct fdisk_context *cxt, size_t n,
  * 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;
 }
 
 /*
@@ -1837,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))
@@ -1877,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 */
@@ -1928,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)
 {
@@ -1996,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."),
@@ -2024,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);
@@ -2061,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;
 }
@@ -2092,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;
@@ -2102,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;
        }
@@ -2131,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);
@@ -2170,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;
@@ -2196,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;
                        }
@@ -2206,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 (;;) {
@@ -2246,7 +2350,7 @@ static int gpt_add_partition(
                        if (fdisk_ask_number_is_relative(ask)) {
                                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;
                        }
 
                        if (user_l >= user_f && user_l <= disk_l)
@@ -2280,8 +2384,9 @@ static int gpt_add_partition(
 
        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);
 
@@ -2308,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 */
        {
@@ -2324,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);
        }
@@ -2343,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;
 
@@ -2389,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;
@@ -2399,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);
@@ -2451,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;
@@ -2462,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);
 }
 
 /**
@@ -2510,14 +2747,16 @@ int fdisk_gpt_get_partition_attrs(
 
        assert(cxt);
        assert(cxt->label);
-       assert(fdisk_is_label(cxt, GPT));
+
+       if (!fdisk_is_label(cxt, GPT))
+               return -EINVAL;
 
        gpt = self_label(cxt);
 
-       if ((uint32_t) partnum >= le32_to_cpu(gpt->pheader->npartition_entries))
+       if (partnum >= gpt_get_nentries(gpt))
                return -EINVAL;
 
-       *attrs = le64_to_cpu(gpt->ents[partnum].attrs);
+       *attrs = le64_to_cpu(gpt_get_entry(gpt, partnum)->attrs);
        return 0;
 }
 
@@ -2540,15 +2779,17 @@ int fdisk_gpt_set_partition_attrs(
 
        assert(cxt);
        assert(cxt->label);
-       assert(fdisk_is_label(cxt, GPT));
+
+       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 ((uint32_t) partnum >= le32_to_cpu(gpt->pheader->npartition_entries))
+       if (partnum >= gpt_get_nentries(gpt))
                return -EINVAL;
 
-       gpt->ents[partnum].attrs = cpu_to_le64(attrs);
+       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);
 
@@ -2564,7 +2805,9 @@ static int gpt_toggle_partition_flag(
                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;
@@ -2576,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 = gpt->ents[i].attrs;
+       e = gpt_get_entry(gpt, i);
+       attrs = e->attrs;
        bits = (char *) &attrs;
 
        switch (flag) {
@@ -2620,7 +2864,7 @@ static int gpt_toggle_partition_flag(
        else
                clrbit(bits, bit);
 
-       gpt->ents[i].attrs = attrs;
+       e->attrs = attrs;
 
        if (flag == GPT_FLAG_GUIDSPECIFIC)
                fdisk_info(cxt, isset(bits, bit) ?
@@ -2643,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));
@@ -2667,12 +2911,12 @@ static int gpt_reorder(struct fdisk_context *cxt)
        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->ents[i],
-                               (const void *) &gpt->ents[i + 1]) > 0;
+                               (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."));