From patchwork Fri May 8 06:05:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rayagonda Kokatanur X-Patchwork-Id: 1285843 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=broadcom.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=broadcom.com header.i=@broadcom.com header.a=rsa-sha256 header.s=google header.b=VNjJC6Xm; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49JKZF5fB1z9sSr for ; Fri, 8 May 2020 16:05:41 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B2CC781D1B; Fri, 8 May 2020 08:05:37 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=broadcom.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=broadcom.com header.i=@broadcom.com header.b="VNjJC6Xm"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E3FE981D1E; Fri, 8 May 2020 08:05:36 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 8E21D81D10 for ; Fri, 8 May 2020 08:05:33 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=broadcom.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=rayagonda.kokatanur@broadcom.com Received: by mail-pj1-x1044.google.com with SMTP id a7so3769371pju.2 for ; Thu, 07 May 2020 23:05:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id; bh=/bLMl5lycwulz2x1ALv4A0X6UEvzX8jWLN7v11/9c/w=; b=VNjJC6XmN6meM0nB4p6i7Ev5wI+qFX7YbbSP4JQalJ02dtKMIPhsNZ+gSUnJDFbEcv 2qbWF0w64w7saFplYTv8izGfnBeFAQheXOOoTTAigbkgL8a6QQu27sCVxEaPxKFsyfuQ DN4tK8SmY/VYj5VumvJoCMd1m0zs5eq0aTCX4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=/bLMl5lycwulz2x1ALv4A0X6UEvzX8jWLN7v11/9c/w=; b=dnh/DUnO9bBu3Uv23Ji+pv4DMwjzAw0TA0ch9lGOf1s5j2tGD4kGfASWpMwPVLqxMV RGHn3SLZpbK8IT4Vmm+fLoOhHjPgPGwswq/nnGVHRo0sh8kJC+lkXrJtjOiZ0TxMMQ+b PSkRGz6+vhknHei7emG4EzG46XdzCKOgIIeQfOFd8WH0DG6lccmgkZAmV2qkIncyA6lz t3znR7UPH2UcNgNNdXzHfAEdvo92FYUW5p9CWTH88Pb0jcMDOoTOSfW+EHPd0eWV044x QUJfpvvOuNZFOQCcA1m5leCEJyEm5yMw5QzGjIYdXEEIFRFWWWohVIxsTcQEWK30JunY YL7Q== X-Gm-Message-State: AGi0PubHiJAvjGk7Qz3STOPGYDsKTmqS0IFMFeOa9+WyZEKanqrJWuKJ 5Suq+8EhgL8OFWAsDH/LfmXVRvLZFq0H0DEwOVd/TB6dWWE2QjsBFIhluWfK+8RBE+troaZUKnm mpwhY9pGH6C2O83JnMEOJi47sW+W6gNlHIwD69GrYTCvIkELDncC+xV/vrBb4+Twvm5L/Ceg4wi 0Rp1sxWw== X-Google-Smtp-Source: APiQypIS5E1pxg83LyugGoooE+QClWzYm1/T1brumoIAWrd8NU6ECh7nowQy8+fgp8N+JImbLFpfhw== X-Received: by 2002:a17:90a:bd91:: with SMTP id z17mr4336700pjr.189.1588917931134; Thu, 07 May 2020 23:05:31 -0700 (PDT) Received: from rayagonda.dhcp.broadcom.net ([192.19.234.250]) by smtp.gmail.com with ESMTPSA id q21sm618676pfg.131.2020.05.07.23.05.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 May 2020 23:05:30 -0700 (PDT) From: Rayagonda Kokatanur To: u-boot@lists.denx.de, Simon Glass , Heinrich Schuchardt , Joe Hershberger , Anatolij Gustschin , Simon Goldschmidt , Corneliu Doban Cc: Rayagonda Kokatanur Subject: [PATCH v2 1/1] cmd: gpt: add eMMC and GPT support Date: Fri, 8 May 2020 11:35:17 +0530 Message-Id: <20200508060517.30336-1-rayagonda.kokatanur@broadcom.com> X-Mailer: git-send-email 2.17.1 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.30rc1 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.2 at phobos.denx.de X-Virus-Status: Clean From: Corneliu Doban 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 Signed-off-by: Rayagonda Kokatanur --- Changes from v1: -Address review comments from Simon Glass, Correct function comments, Check for return value, Add helper function in part.h cmd/gpt.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++ include/part.h | 42 +++++++++++++++ 2 files changed, 186 insertions(+) diff --git a/cmd/gpt.c b/cmd/gpt.c index b8d11c167d..3263ac42df 100644 --- a/cmd/gpt.c +++ b/cmd/gpt.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -616,6 +617,135 @@ static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part) 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. + * + * @blk_dev_desc: block device descriptor + * + * @Return: '0' on success and 1 on failure + */ +static int gpt_enumerate(struct blk_desc *blk_dev_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(); + first_drv = part_driver_get(0); + if (!first_drv) { + printf("Failed to get partition driver\n"); + return 1; + } + + for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) { + disk_partition_t pinfo; + int ret; + int i; + + for (i = 1; i < part_drv->max_entries; i++) { + ret = part_drv->get_info(blk_dev_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; + if (str_len > sizeof(part_list)) { + printf("Error insufficient memory\n"); + return -ENOMEM; + } + strncpy(ptr, (const char *)pinfo.name, tmp_len); + /* One byte for space(" ") delimiter */ + strncpy(&ptr[tmp_len], " ", 1); + str_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() - 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. + * gpt_partition_name, gpt_partition_entry, gpt_partition_addr and + * gpt_partition_size environment variables will be set. + * + * @blk_dev_desc: block device descriptor + * @name: partition name + * + * @Return: '0' on success and -ENOENT on failure + */ +static int gpt_setenv(struct blk_desc *blk_dev_desc, const char *name) +{ + struct part_driver *first_drv, *part_drv; + int n_drvs; + + n_drvs = part_driver_get_count(); + first_drv = part_driver_get(0); + if (!first_drv) { + printf("Failed to get partition driver\n"); + return -ENOENT; + } + + for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) { + disk_partition_t pinfo; + char buf[32]; + int ret; + int i; + + for (i = 1; i < part_drv->max_entries; i++) { + ret = part_drv->get_info(blk_dev_desc, i, &pinfo); + if (ret) { + /* no more entries in table */ + break; + } + if (strcmp(name, (const char *)pinfo.name) == 0) { + /* match found, setup environment variables */ + sprintf(buf, LBAF, pinfo.start); + debug("setenv gpt_partition_addr %s\n", buf); + ret = env_set("gpt_partition_addr", buf); + if (ret) + return -ENOENT; + + sprintf(buf, LBAF, pinfo.size); + debug("setenv gpt_partition_size %s\n", buf); + ret = env_set("gpt_partition_size", buf); + if (ret) + return -ENOENT; + + sprintf(buf, "%d", i); + debug("setenv gpt_partition_entry %s\n", buf); + ret = env_set("gpt_partition_entry", buf); + if (ret) + return -ENOENT; + + sprintf(buf, "%s", pinfo.name); + debug("setenv gpt_partition_name %s\n", buf); + ret = env_set("gpt_partition_name", buf); + if (ret) + return -ENOENT; + + return 0; + } + } + } + + return -ENOENT; +} + static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr) { int ret; @@ -822,6 +952,10 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) } 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 @@ -852,7 +986,17 @@ U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt, " 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 \n" + " - read GPT into a data structure for manipulation\n" " gpt guid \n" " - print disk GUID\n" " gpt guid \n" diff --git a/include/part.h b/include/part.h index 3693527397..014f4164e7 100644 --- a/include/part.h +++ b/include/part.h @@ -9,6 +9,7 @@ #include #include #include +#include #include struct block_drvr { @@ -474,5 +475,46 @@ int write_mbr_partition(struct blk_desc *dev_desc, void *buf); #endif +#ifdef CONFIG_PARTITIONS +/** + * part_driver_get_count() - get partition driver count + * + * @return - number of partition drivers + */ +static inline int part_driver_get_count(void) +{ + return ll_entry_count(struct part_driver, part_driver); +} + +/** + * part_driver_get() - get partition driver + * + * Get the partition driver from the partition driver list for the + * requested partition driver count + * + * @param n - partition driver count + * + * @return - pointer to partition driver on success, otherwise NULL + */ +static inline struct part_driver *part_driver_get(int n) +{ + struct part_driver *first_drv; + + if (n > part_driver_get_count()) { + printf("Invalid partition driver count (%d)\n", n); + return NULL; + } + + first_drv = ll_entry_start(struct part_driver, part_driver); + + return &first_drv[n]; +} +#else +static inline int part_driver_get_count(void) +{ return 0; } + +static inline struct part_driver *part_driver_get(int n) +{ return NULL; } +#endif /* CONFIG_PARTITIONS */ #endif /* _PART_H */