cmd: gpt: add eMMC and GPT support

Add eMMC and GPT support.
- GPT partition list and command to create the GPT added to u-boot
  environment
- eMMC boot commands added to u-boot environment
- new gpt commands (enumarate and setenv) that are used by broadcom
  update scripts and boot commands
- eMMC specific u-boot configurations with environment saved in eMMC
  and GPT support

Signed-off-by: Corneliu Doban <cdoban@broadcom.com>
Signed-off-by: Rayagonda Kokatanur <rayagonda.kokatanur@broadcom.com>
[trini: Make gpt_setenv initialize ret to -1, given relevant return
        error values]
Signed-off-by: Tom Rini <trini@konsulko.com>
diff --git a/cmd/gpt.c b/cmd/gpt.c
index df75941..76a95ad 100644
--- a/cmd/gpt.c
+++ b/cmd/gpt.c
@@ -18,6 +18,7 @@
 #include <command.h>
 #include <part.h>
 #include <part_efi.h>
+#include <part.h>
 #include <exports.h>
 #include <uuid.h>
 #include <linux/ctype.h>
@@ -621,6 +622,152 @@
 	return ret;
 }
 
+/**
+ * gpt_enumerate() - Enumerate partition names into environment variable.
+ *
+ * Enumerate partition names. Partition names are stored in gpt_partition_list
+ * environment variable. Each partition name is delimited by space.
+ *
+ * @desc: block device descriptor
+ *
+ * @Return: '0' on success and -ve error on failure
+ */
+static int gpt_enumerate(struct blk_desc *desc)
+{
+	struct part_driver *first_drv, *part_drv;
+	int str_len = 0, tmp_len;
+	char part_list[2048];
+	int n_drvs;
+	char *ptr;
+
+	part_list[0] = 0;
+	n_drvs = part_driver_get_count();
+	if (!n_drvs) {
+		printf("Failed to get partition driver count\n");
+		return -ENOENT;
+	}
+
+	first_drv = part_driver_get_first();
+	for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
+		struct disk_partition pinfo;
+		int ret;
+		int i;
+
+		for (i = 1; i < part_drv->max_entries; i++) {
+			ret = part_drv->get_info(desc, i, &pinfo);
+			if (ret) {
+				/* no more entries in table */
+				break;
+			}
+
+			ptr = &part_list[str_len];
+			tmp_len = strlen((const char *)pinfo.name);
+			str_len += tmp_len;
+			/* +1 for space */
+			str_len++;
+			if (str_len > sizeof(part_list)) {
+				printf("Error insufficient memory\n");
+				return -ENOMEM;
+			}
+			strcpy(ptr, (const char *)pinfo.name);
+			/* One byte for space(" ") delimiter */
+			ptr[tmp_len] = ' ';
+		}
+	}
+	if (*part_list)
+		part_list[strlen(part_list) - 1] = 0;
+	debug("setenv gpt_partition_list %s\n", part_list);
+
+	return env_set("gpt_partition_list", part_list);
+}
+
+/**
+ * gpt_setenv_part_variables() - setup partition environmental variables
+ *
+ * Setup the gpt_partition_name, gpt_partition_entry, gpt_partition_addr
+ * and gpt_partition_size environment variables.
+ *
+ * @pinfo: pointer to disk partition
+ * @i: partition entry
+ *
+ * @Return: '0' on success and -ENOENT on failure
+ */
+static int gpt_setenv_part_variables(struct disk_partition *pinfo, int i)
+{
+	int ret;
+
+	ret = env_set_hex("gpt_partition_addr", pinfo->start);
+	if (ret)
+		goto fail;
+
+	ret = env_set_hex("gpt_partition_size", pinfo->size);
+	if (ret)
+		goto fail;
+
+	ret = env_set_ulong("gpt_partition_entry", i);
+	if (ret)
+		goto fail;
+
+	ret = env_set("gpt_partition_name", (const char *)pinfo->name);
+	if (ret)
+		goto fail;
+
+	return 0;
+
+fail:
+	return -ENOENT;
+}
+
+/**
+ * gpt_setenv() - Dynamically setup environment variables.
+ *
+ * Dynamically setup environment variables for name, index, offset and size
+ * for partition in GPT table after running "gpt setenv" for a partition name.
+ *
+ * @desc: block device descriptor
+ * @name: partition name
+ *
+ * @Return: '0' on success and -ve err on failure
+ */
+static int gpt_setenv(struct blk_desc *desc, const char *name)
+{
+	struct part_driver *first_drv, *part_drv;
+	int n_drvs;
+	int ret = -1;
+
+	n_drvs = part_driver_get_count();
+	if (!n_drvs) {
+		printf("Failed to get partition driver count\n");
+		goto fail;
+	}
+
+	first_drv = part_driver_get_first();
+	for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
+		struct disk_partition pinfo;
+		int i;
+
+		for (i = 1; i < part_drv->max_entries; i++) {
+			ret = part_drv->get_info(desc, i, &pinfo);
+			if (ret) {
+				/* no more entries in table */
+				break;
+			}
+
+			if (!strcmp(name, (const char *)pinfo.name)) {
+				/* match found, setup environment variables */
+				ret = gpt_setenv_part_variables(&pinfo, i);
+				if (ret)
+					goto fail;
+
+				return 0;
+			}
+		}
+	}
+
+fail:
+	return ret;
+}
+
 static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
 {
 	int ret;
@@ -827,6 +974,10 @@
 	} else if ((strcmp(argv[1], "verify") == 0)) {
 		ret = gpt_verify(blk_dev_desc, argv[4]);
 		printf("Verify GPT: ");
+	} else if ((strcmp(argv[1], "setenv") == 0)) {
+		ret = gpt_setenv(blk_dev_desc, argv[4]);
+	} else if ((strcmp(argv[1], "enumerate") == 0)) {
+		ret = gpt_enumerate(blk_dev_desc);
 	} else if (strcmp(argv[1], "guid") == 0) {
 		ret = do_disk_guid(blk_dev_desc, argv[4]);
 #ifdef CONFIG_CMD_GPT_RENAME
@@ -857,7 +1008,17 @@
 	" to interface\n"
 	" Example usage:\n"
 	" gpt write mmc 0 $partitions\n"
+	"    - write the GPT to device\n"
 	" gpt verify mmc 0 $partitions\n"
+	"    - verify the GPT on device against $partitions\n"
+	" gpt setenv mmc 0 $name\n"
+	"    - setup environment variables for partition $name:\n"
+	"      gpt_partition_addr, gpt_partition_size,\n"
+	"      gpt_partition_name, gpt_partition_entry\n"
+	" gpt enumerate mmc 0\n"
+	"    - store list of partitions to gpt_partition_list environment variable\n"
+	" read <interface> <dev>\n"
+	"    - read GPT into a data structure for manipulation\n"
 	" gpt guid <interface> <dev>\n"
 	"    - print disk GUID\n"
 	" gpt guid <interface> <dev> <varname>\n"