Add labels to MTD partitions.

Add labels to MTD partitions and clean up some of the show code, adding more
info on the MTD prints and eliminating duplicated code.

BRANCH=none
TEST=make runtests & manual cgpt add -l "label"; cgpt show to verify labels
BUG=none

Original-Change-Id: I59736128f394c2aca937a3a0bb5fc5d42b0149a9
Reviewed-on: https://gerrit.chromium.org/gerrit/63367
Reviewed-by: Bill Richardson <wfrichar@chromium.org>
Commit-Queue: Albert Chaulk <achaulk@chromium.org>
Tested-by: Albert Chaulk <achaulk@chromium.org>
(cherry picked from commit 32fd6dead10a8255fea27f6e5ce9ba92d8716008)

Change-Id: Ic24c047cc9680f6ef6143f776270d12f16608d68
Signed-off-by: Duncan Laurie <dlaurie@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/64225
diff --git a/cgpt/cgpt_add.c b/cgpt/cgpt_add.c
index e0e24b8..a93c60c 100644
--- a/cgpt/cgpt_add.c
+++ b/cgpt/cgpt_add.c
@@ -113,6 +113,9 @@
   }
   if (params->set_type)
     MtdSetEntryType(entry, LookupMtdTypeForGuid(&params->type_guid));
+  if (params->label) {
+    strncpy(entry->label, params->label, sizeof(entry->label));
+  }
 
   return 0;
 }
diff --git a/cgpt/cgpt_show.c b/cgpt/cgpt_show.c
index 78a285c..41f9d00 100644
--- a/cgpt/cgpt_show.c
+++ b/cgpt/cgpt_show.c
@@ -111,45 +111,66 @@
 
 void MtdEntryDetails(MtdDiskPartition *entry, uint32_t index, int raw) {
   const Guid *guid = LookupGuidForMtdType(MtdGetEntryType(entry));
-  char buf[256];                   // scratch buffer for formatting output
+  char type[256];
+  char contents[256];
+  char name[sizeof(entry->label) + 1];
   uint64_t start, size;
   if (guid) {
-    ResolveType(guid, buf);
+    ResolveType(guid, type);
   } else {
-    snprintf(buf, sizeof(buf), "MTD partition type %d", MtdGetEntryType(entry));
+    snprintf(type, sizeof(type), "MTD partition type %d",
+             MtdGetEntryType(entry));
   }
 
   MtdGetPartitionSizeInSectors(entry, &start, NULL, &size);
 
-  if (!raw) {
-    printf(PARTITION_FMT, (int)start, (int)size, index+1, buf);
+  // Provide a NUL if we are at maximum size.
+  name[sizeof(name)-1] = '\0';
+  memcpy(name, entry->label, sizeof(entry->label));
+  require(snprintf(contents, sizeof(contents),
+                   "Label: \"%s\"", name) < sizeof(contents));
+
+  printf(PARTITION_FMT, (int)start, (int)size, index+1, contents);
+  printf(PARTITION_MORE, "Type: ", type);
+
+  if (raw && MtdGetEntryType(entry) == MTD_PARTITION_TYPE_CHROMEOS_KERNEL) {
+    int tries = MtdGetEntryTries(entry);
+    int successful = MtdGetEntrySuccessful(entry);
+    int priority = MtdGetEntryPriority(entry);
+    require(snprintf(contents, sizeof(contents),
+                     "priority=%d tries=%d successful=%d",
+                     priority, tries, successful) < sizeof(contents));
+    printf(PARTITION_MORE, "Attr: ", contents);
   } else {
-    printf(PARTITION_FMT, (int)start, (int)size, index+1, buf);
+    require(snprintf(contents, sizeof(contents),
+                     "[%x]", entry->flags) < sizeof(contents));
+    printf(PARTITION_MORE, "Attr: ", contents);
   }
 }
 
 void EntryDetails(GptEntry *entry, uint32_t index, int raw) {
   char contents[256];                   // scratch buffer for formatting output
   uint8_t label[GPT_PARTNAME_LEN];
+  char type[GUID_STRLEN], unique[GUID_STRLEN];
+
+  UTF16ToUTF8(entry->name, sizeof(entry->name) / sizeof(entry->name[0]),
+              label, sizeof(label));
+  require(snprintf(contents, sizeof(contents),
+                   "Label: \"%s\"", label) < sizeof(contents));
+  printf(PARTITION_FMT, (int)entry->starting_lba,
+         (int)(entry->ending_lba - entry->starting_lba + 1),
+         index+1, contents);
+
+  if (!raw && CGPT_OK == ResolveType(&entry->type, type)) {
+    printf(PARTITION_MORE, "Type: ", type);
+  } else {
+    GuidToStr(&entry->type, type, GUID_STRLEN);
+    printf(PARTITION_MORE, "Type: ", type);
+  }
+  GuidToStr(&entry->unique, unique, GUID_STRLEN);
+  printf(PARTITION_MORE, "UUID: ", unique);
 
   if (!raw) {
-    char type[GUID_STRLEN], unique[GUID_STRLEN];
-
-    UTF16ToUTF8(entry->name, sizeof(entry->name) / sizeof(entry->name[0]),
-                label, sizeof(label));
-    require(snprintf(contents, sizeof(contents),
-                     "Label: \"%s\"", label) < sizeof(contents));
-    printf(PARTITION_FMT, (int)entry->starting_lba,
-           (int)(entry->ending_lba - entry->starting_lba + 1),
-           index+1, contents);
-    if (CGPT_OK == ResolveType(&entry->type, type)) {
-      printf(PARTITION_MORE, "Type: ", type);
-    } else {
-      GuidToStr(&entry->type, type, GUID_STRLEN);
-      printf(PARTITION_MORE, "Type: ", type);
-    }
-    GuidToStr(&entry->unique, unique, GUID_STRLEN);
-    printf(PARTITION_MORE, "UUID: ", unique);
     if (GuidEqual(&guid_chromeos_kernel, &entry->type)) {
       int tries = (entry->attrs.fields.gpt_att &
                    CGPT_ATTRIBUTE_TRIES_MASK) >>
@@ -166,19 +187,6 @@
       printf(PARTITION_MORE, "Attr: ", contents);
     }
   } else {
-    char type[GUID_STRLEN], unique[GUID_STRLEN];
-
-    UTF16ToUTF8(entry->name, sizeof(entry->name) / sizeof(entry->name[0]),
-                label, sizeof(label));
-    require(snprintf(contents, sizeof(contents),
-                     "Label: \"%s\"", label) < sizeof(contents));
-    printf(PARTITION_FMT, (int)entry->starting_lba,
-           (int)(entry->ending_lba - entry->starting_lba + 1),
-           index+1, contents);
-    GuidToStr(&entry->type, type, GUID_STRLEN);
-    printf(PARTITION_MORE, "Type: ", type);
-    GuidToStr(&entry->unique, unique, GUID_STRLEN);
-    printf(PARTITION_MORE, "UUID: ", unique);
     require(snprintf(contents, sizeof(contents),
                      "[%x]", entry->attrs.fields.gpt_att) < sizeof(contents));
     printf(PARTITION_MORE, "Attr: ", contents);
@@ -321,7 +329,7 @@
       require(snprintf(indent, sizeof(indent), GPT_MORE) < sizeof(indent));
       MtdHeaderDetails(&drive->mtd.primary, indent, 0);
     }
-
+    printf(TITLE_FMT, "start", "size", "part", "contents");
     MtdEntriesDetails(drive, PRIMARY, params->numeric);
   }
 
diff --git a/firmware/lib/cgptlib/include/mtdlib.h b/firmware/lib/cgptlib/include/mtdlib.h
index 0c87822..fd749ac 100644
--- a/firmware/lib/cgptlib/include/mtdlib.h
+++ b/firmware/lib/cgptlib/include/mtdlib.h
@@ -66,6 +66,11 @@
   uint64_t starting_offset;
   uint64_t ending_offset;
   uint32_t flags;
+
+  /* 28 characters is a balance between GPT parity and size constraints, at
+   * current sizes this table occupies 10% of the FTS data store.
+   */
+  char label[28];
 } __attribute__((packed)) MtdDiskPartition;
 
 typedef struct {
@@ -81,9 +86,9 @@
   MtdDiskPartition partitions[MTD_MAX_PARTITIONS];
 } __attribute__((packed)) MtdDiskLayout;
 
-#define MTD_DRIVE_V1_SIZE (32 + 16*20)
+#define MTD_DRIVE_V1_SIZE (32 + 16*48)
 
-#define MTDENTRY_EXPECTED_SIZE (20)
+#define MTDENTRY_EXPECTED_SIZE (48)
 #define MTDLAYOUT_EXPECTED_SIZE (32 + 16 * MTDENTRY_EXPECTED_SIZE)