Port MTD structures to use 64-bit byte offsets instead of sectors.

As per the discussion on issue 221745 we will be using 64-bit byte offsets
for the MTD partition table and converting to/from sectors internally in cgpt.
Existing interfaces do not change, eg sizes are still reported in sectors, only
the on-disk representation is affected.

BRANCH=none
BUG=chromium:221745
TEST=unit tests pass

Change-Id: Id312d42783acfdabe6eb8aea11dcbd298e00a100
Reviewed-on: https://gerrit.chromium.org/gerrit/60919
Commit-Queue: Albert Chaulk <achaulk@chromium.org>
Reviewed-by: Albert Chaulk <achaulk@chromium.org>
Tested-by: Albert Chaulk <achaulk@chromium.org>
diff --git a/cgpt/cgpt_add.c b/cgpt/cgpt_add.c
index 8332f22..e0e24b8 100644
--- a/cgpt/cgpt_add.c
+++ b/cgpt/cgpt_add.c
@@ -103,9 +103,14 @@
 
   entry = MtdGetEntry(&drive->mtd, PRIMARY, index);
   if (params->set_begin)
-    entry->starting_lba = params->begin;
-  if (params->set_size)
-    entry->ending_lba = entry->starting_lba + params->size - 1;
+    memcpy(&entry->starting_offset, &params->begin, sizeof(params->begin));
+  if (params->set_size) {
+    uint64_t start;
+    uint64_t end;
+    MtdGetPartitionSize(entry, &start, NULL, NULL);
+    end = start + params->size - 1;
+    memcpy(&entry->ending_offset, &end, sizeof(end));
+  }
   if (params->set_type)
     MtdSetEntryType(entry, LookupMtdTypeForGuid(&params->type_guid));
 
@@ -273,14 +278,10 @@
 
   if(drive.is_mtd) {
     MtdDiskPartition *entry = MtdGetEntry(&drive.mtd, PRIMARY, index);
-    uint64_t start_lba, end_lba;
     const Guid *guid = LookupGuidForMtdType(MtdGetEntryType(entry));
     memcpy(&params->type_guid, guid, sizeof(params->type_guid));
     memset(&params->unique_guid, 0, sizeof(params->unique_guid));
-    start_lba = entry->starting_lba;
-    end_lba = entry->ending_lba;
-    params->begin = start_lba;
-    params->size = end_lba - start_lba + 1;
+    MtdGetPartitionSizeInSectors(entry, &params->begin, NULL, &params->size);
     params->raw_value = entry->flags;
   } else {
     // GPT-specific code
diff --git a/cgpt/cgpt_create.c b/cgpt/cgpt_create.c
index 0b116b2..e7cbadf 100644
--- a/cgpt/cgpt_create.c
+++ b/cgpt/cgpt_create.c
@@ -60,8 +60,8 @@
     // Prep basic parameters
     memcpy(h->signature, MTD_DRIVE_SIGNATURE, sizeof(h->signature));
     h->size = sizeof(*h);
-    h->first_lba = 0;
-    h->last_lba = drive->mtd.drive_sectors - 1;
+    h->first_offset = 0;
+    h->last_offset = (drive->mtd.drive_sectors * drive->mtd.sector_bytes) - 1;
     h->crc32 = MtdHeaderCrc(h);
   }
 
diff --git a/cgpt/cgpt_find.c b/cgpt/cgpt_find.c
index df6c4d3..fc223a0 100644
--- a/cgpt/cgpt_find.c
+++ b/cgpt/cgpt_find.c
@@ -150,13 +150,13 @@
 
 static int mtd_match_content(CgptFindParams *params, struct drive *drive,
                              MtdDiskPartition *entry) {
-  uint64_t part_size;
+  uint64_t start, part_size;
 
   if (!params->matchlen)
     return 1;
 
   // Ensure that the region we want to match against is inside the partition.
-  part_size = LBA_SIZE * (entry->ending_lba - entry->starting_lba + 1);
+  MtdGetPartitionSize(entry, &start, NULL, &part_size);
   if (params->matchoffset + params->matchlen > part_size) {
     return 0;
   }
@@ -164,7 +164,7 @@
   // Read the partition data.
   if (!FillBuffer(params,
                   drive->fd,
-                  (LBA_SIZE * entry->starting_lba) + params->matchoffset,
+                  start + params->matchoffset,
                   params->matchlen)) {
     Error("unable to read partition data\n");
     return 0;
diff --git a/cgpt/cgpt_show.c b/cgpt/cgpt_show.c
index c3c222e..78a285c 100644
--- a/cgpt/cgpt_show.c
+++ b/cgpt/cgpt_show.c
@@ -73,8 +73,10 @@
   printf("%sSize: %d\n", indent, header->size);
   printf("%sCRC: 0x%08x %s\n", indent, header->crc32,
          (MtdHeaderCrc(header) != header->crc32) ? "(INVALID)" : "");
-  printf("%sFirst LBA: %u\n", indent, header->first_lba);
-  printf("%sLast LBA: %u\n", indent, header->last_lba);
+  printf("%sFirst LBA: %llu\n", indent,
+    (unsigned long long)header->first_offset);
+  printf("%sLast LBA: %llu\n", indent,
+    (unsigned long long)header->last_offset);
 }
 
 static void HeaderDetails(GptHeader *header, GptEntry *entries,
@@ -110,20 +112,19 @@
 void MtdEntryDetails(MtdDiskPartition *entry, uint32_t index, int raw) {
   const Guid *guid = LookupGuidForMtdType(MtdGetEntryType(entry));
   char buf[256];                   // scratch buffer for formatting output
+  uint64_t start, size;
   if (guid) {
     ResolveType(guid, buf);
   } else {
     snprintf(buf, sizeof(buf), "MTD partition type %d", MtdGetEntryType(entry));
   }
 
+  MtdGetPartitionSizeInSectors(entry, &start, NULL, &size);
+
   if (!raw) {
-    printf(PARTITION_FMT, (int)entry->starting_lba,
-           (int)(entry->ending_lba - entry->starting_lba + 1),
-           index+1, buf);
+    printf(PARTITION_FMT, (int)start, (int)size, index+1, buf);
   } else {
-    printf(PARTITION_FMT, (int)entry->starting_lba,
-           (int)(entry->ending_lba - entry->starting_lba + 1),
-           index+1, buf);
+    printf(PARTITION_FMT, (int)start, (int)size, index+1, buf);
   }
 }
 
@@ -257,14 +258,17 @@
     MtdDiskPartition *entry = MtdGetEntry(&drive->mtd, ANY_VALID, index);
     char buf[256];                      // scratch buffer for string conversion
     const Guid *guid;
+    uint64_t start, size;
+
+    MtdGetPartitionSizeInSectors(entry, &start, NULL, &size);
 
     if (params->single_item) {
       switch(params->single_item) {
       case 'b':
-        printf("%u\n", entry->starting_lba);
+        printf("%u\n", (int)start);
         break;
       case 's':
-        printf("%u\n", entry->ending_lba - entry->starting_lba + 1);
+        printf("%u\n", (int)size);
         break;
       case 't':
         guid = LookupGuidForMtdType(MtdGetEntryType(entry));
@@ -295,6 +299,9 @@
     for (i = 0; i < GetNumberOfEntries(drive); ++i) {
       MtdDiskPartition *entry = MtdGetEntry(&drive->mtd, ANY_VALID, i);
       const Guid *guid = LookupGuidForMtdType(MtdGetEntryType(entry));
+      uint64_t start, size;
+
+      MtdGetPartitionSizeInSectors(entry, &start, NULL, &size);
 
       if (IsUnused(drive, ANY_VALID, i))
         continue;
@@ -305,9 +312,7 @@
         snprintf(type, sizeof(type), "MTD partition type %d",
                  MtdGetEntryType(entry));
       }
-      printf(PARTITION_FMT, (int)entry->starting_lba,
-             (int)(entry->ending_lba - entry->starting_lba + 1),
-             i+1, type);
+      printf(PARTITION_FMT, (int)start, (int)size, i+1, type);
     }
   } else {                              // show all partitions
     if (params->debug || params->verbose) {