[U-Boot] [PATCH] gpt: add part-uuid and part-num subcommands

your name yurovsky at gmail.com
Wed Nov 8 00:43:15 UTC 2017


From: Andrey Yurovsky <yurovsky at gmail.com>

It is useful to be able to retrieve a partition UUID or number given
the partition label, for instance some systems use the partition label
to indicate the purpose of the partition (such as "rootfs0" being the
0th root file system in an A/B image scheme).

Add "gpt part-uuid" to retrieve the partition UUID for a given label and
"gpt part-num" to retrieve the partition number for a given label along
with some documentation.

Signed-off-by: Andrey Yurovsky <yurovsky at gmail.com>
---
 cmd/gpt.c      | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 doc/README.gpt | 21 ++++++++++++++++++
 2 files changed, 88 insertions(+)

diff --git a/cmd/gpt.c b/cmd/gpt.c
index 707d861766..bef292230e 100644
--- a/cmd/gpt.c
+++ b/cmd/gpt.c
@@ -355,6 +355,64 @@ static int do_get_gpt_info(struct blk_desc *dev_desc)
 	}
 	return ret;
 }
+
+static struct disk_part *find_part_by_label(const char *label)
+{
+	struct disk_part *part = NULL;
+	struct disk_part *curr;
+
+	list_for_each_entry(curr, &disk_partitions, list) {
+		/* Check for the first match of the label we're looking
+		 * for against the partition label */
+		if (!strcmp((const char*)curr->gpt_part_info.name,
+					label)) {
+			part = curr;
+			break;
+		}
+	}
+
+	return part;
+}
+
+/* Find a partition UUID by label and save that UUID to the environment
+ * variable specified */
+static int do_get_part_uuid(struct blk_desc *dev_desc, const char *label,
+		const char *namestr)
+{
+	int ret;
+
+	ret = get_gpt_info(dev_desc);
+	if (ret > 0) {
+		struct disk_part *part = find_part_by_label(label);
+		if (part) {
+			env_set(namestr, part->gpt_part_info.uuid);
+			ret = 0;
+		}
+
+		del_gpt_info();
+	}
+
+	return ret;
+}
+
+/* Find a partition number by label and save that number to the environment
+ * variable specified */
+static int do_get_part_num(struct blk_desc *dev_desc, const char *label,
+		const char *namestr)
+{
+	int ret = get_gpt_info(dev_desc);
+	if (ret > 0) {
+		struct disk_part *part = find_part_by_label(label);
+		if (part) {
+			env_set_ulong(namestr, part->partnum);
+			ret = 0;
+		}
+
+		del_gpt_info();
+	}
+
+	return ret;
+}
 #endif
 
 /**
@@ -851,6 +909,10 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 #ifdef CONFIG_CMD_GPT_RENAME
 	} else if (strcmp(argv[1], "read") == 0) {
 		ret = do_get_gpt_info(blk_dev_desc);
+	} else if (strcmp(argv[1], "part-uuid") == 0 && argc == 6) {
+		ret = do_get_part_uuid(blk_dev_desc, argv[4], argv[5]);
+	} else if (strcmp(argv[1], "part-num") == 0 && argc == 6) {
+		ret = do_get_part_num(blk_dev_desc, argv[4], argv[5]);
 	} else if ((strcmp(argv[1], "swap") == 0) ||
 		   (strcmp(argv[1], "rename") == 0)) {
 		ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]);
@@ -887,6 +949,11 @@ U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
 	" gpt guid mmc 0\n"
 	" gpt guid mmc 0 varname\n"
 #ifdef CONFIG_CMD_GPT_RENAME
+	"gpt partition label commands:\n"
+	"gpt part-uuid <interface> <dev> <label> <varname>\n"
+	"    - set environment variable to UUID of label\n"
+	"gpt part-num <interface> <dev> <label> <varname>\n"
+	"    - set environment variable to partition number of label\n"
 	"gpt partition renaming commands:\n"
 	"gpt swap <interface> <dev> <name1> <name2>\n"
 	"    - change all partitions named name1 to name2\n"
diff --git a/doc/README.gpt b/doc/README.gpt
index d3db8bce1c..edb99b6c68 100644
--- a/doc/README.gpt
+++ b/doc/README.gpt
@@ -275,6 +275,27 @@ Some strings can be also used at the place of known GUID :
 
 They are also used to display the type of partition in "part list" command.
 
+Identifying Partitions in U-Boot:
+=================================
+
+Two subcommands may be used to identify partitions by their label. This can be
+useful for determining which partition to use or for setting boot arguments.
+The 'gpt part-uuid' command looks up a partition UUID for a given label and
+stores it in an environment variable. The 'gpt part-num' command looks up a
+partition number for a given label and stores it in an environment variable.
+The first partition with a matching label is used.
+
+For example, to find the UUID of a partition named 'rootfs0' and then use it
+for boot arguments:
+
+U-BOOT> gpt part-uuid mmc 0 rootfs0 rootfsuuid
+U-BOOT> setenv bootargs root=PARTUUID=${rootfsuuid}
+
+Or, for example, to find the partition number for a partition named 'kernel'
+and load a file from it:
+
+U-BOOT> gpt part-num mmc 0 kernel kernelnum
+U-BOOT> fatload mmc 0:${kernelnum} ${loadaddr} zImage
 
 Useful info:
 ============
-- 
2.13.6



More information about the U-Boot mailing list