From: your name Date: Tue, 7 Nov 2017 16:43:15 -0800 Subject: [U-Boot] [PATCH] gpt: add part-uuid and part-num subcommands Message-ID: <20171108004315.18224-1-yurovsky@gmail.com> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de From: Andrey Yurovsky 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 --- 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