From patchwork Mon Nov 9 06:19:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rayagonda Kokatanur X-Patchwork-Id: 1396507 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=2a01:238:438b:c500:173d:9f52:ddab:ee01; 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=SzhcfmqU; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CV17H5QVPz9sRK for ; Mon, 9 Nov 2020 17:19:48 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2E7A38234E; Mon, 9 Nov 2020 07:19:39 +0100 (CET) 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="SzhcfmqU"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id A44A48239B; Mon, 9 Nov 2020 07:19:37 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MIME_HEADER_CTYPE_ONLY, MIME_NO_TEXT,SPF_HELO_NONE,T_TVD_MIME_NO_HEADERS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) (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 965D6822D2 for ; Mon, 9 Nov 2020 07:19:32 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=broadcom.com Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=rayagonda.kokatanur@broadcom.com Received: by mail-pf1-x442.google.com with SMTP id w14so4518423pfd.7 for ; Sun, 08 Nov 2020 22:19:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id; bh=K0TDUUkoEeDyXq19mxqOGaAbyduq/TZoPkhLexZpgU0=; b=SzhcfmqU0ygWz2ZIrTjyy+OF+GGTAKQSkLU8YgbCL2pyYgt6zvDqpX61QSA0YJ3Wzc iAE1Vaxb1DUNZCbJydOgTdCmT1OHtRXNL0khOUHyWjmN2PyWGUta2oldOi3s5v8QO2Iw zFZTAXAxrcBQbSSXM5jZaRd2pSy2eLBT+swig= 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=K0TDUUkoEeDyXq19mxqOGaAbyduq/TZoPkhLexZpgU0=; b=r5hQDZSugbB6iJ6ggc33+r7Z5codDonD8W5va2oSxYYI/yXEjhaoRX14on+IljsbJL JLH75IqbTqqFdmsddbgMWoYMlAZYsR585qp9x473qjjqlbPgX9v2JIivFwBCS7JZI1HG gnaAX0mrMT1pM9DLu+sSWv6/2ESBd3EZo+zDz9hSTGFfqgw1l5JKp4BQiVTz+VBHcWu4 8fOCIq0Pf6Xf2JprY3T2pI+M9zCFPjzjJcTjNi4/i2mPIJXfWExtQRAzaevJQTEqCdb4 TwEp7HFlGkvHrGJ45eQhxe97Ewz+XCh/bvmj8hXft/oxlrb1ILh18hiZMKCy6njpelxJ Vp8g== X-Gm-Message-State: AOAM531Zkj6P/1hCKslMrApBAuVT3q4oAffUoD+XSpxLdvlsm9zUicqQ af3zDR19i+aLNSt5d+3zct38m/hrwCjjLy4entdGZvB2TSuNBrYaMeRmGhGGu7mUZjfJ+BDRyxl T1Y9JjiiBTADVEnKBJbALTgmiSZdUrnLN4GapStRImCVOGb16S6trI9c+qlzB9ZPua1Nr5AOvR+ duLoHCSzX4 X-Google-Smtp-Source: ABdhPJwbRQRj7Jlzkzc2EWLP+lFA0+c8f+igLYgoV4sW1Gk0BLmvfgep4gbwmDlWlNVxsOjHHykA4w== X-Received: by 2002:a63:4d0e:: with SMTP id a14mr11172563pgb.91.1604902769955; Sun, 08 Nov 2020 22:19:29 -0800 (PST) Received: from rayagonda.dhcp.broadcom.net ([192.19.234.250]) by smtp.gmail.com with ESMTPSA id gc17sm9913190pjb.47.2020.11.08.22.19.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Nov 2020 22:19:29 -0800 (PST) From: Rayagonda Kokatanur To: U-Boot Mailing List , Heinrich Schuchardt , Joe Hershberger , Anatolij Gustschin , Simon Goldschmidt , Corneliu Doban , Michael Nazzareno Trimarchi , Thirupathaiah Annapureddy Cc: Rayagonda Kokatanur Subject: [PATCH v5 1/1] cmd: gpt: add eMMC and GPT support Date: Mon, 9 Nov 2020 11:49:16 +0530 Message-Id: <20201109061916.30998-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.34 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.3 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 v4: -Address review comments from Thiru, Use env_set_hex() instead of env_set_ulong() in two place. Changes from v3: -Address review comments from Simon Glass, Return -ve number instead of 1 upon failure, Use shorter variable name, Modified code to avoid buffer overflow, Use if (!strcmp(...)) instead of if (strcmp(...) == 0) Changes from v2: -Address review comments from Simon Glass, Check for return value of part_driver_get_count(), Don't check return value of part_driver_get(), Rewrite part_driver_get() and rename to part_driver_get_first(), Use env_set_ulong() whereever applicable, -Address review comments from Michael Nazzareno Trimarchi, Add new function to set all env vriables, 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 | 161 +++++++++++++++++++++++++++++++++++++++++++++++++ include/part.h | 29 +++++++++ 2 files changed, 190 insertions(+) diff --git a/cmd/gpt.c b/cmd/gpt.c index df759416c8..56986ad554 100644 --- a/cmd/gpt.c +++ b/cmd/gpt.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -621,6 +622,152 @@ 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. + * + * @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; + + 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 @@ static int do_gpt(struct cmd_tbl *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 @@ -857,7 +1008,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 55be724d20..31a130311a 100644 --- a/include/part.h +++ b/include/part.h @@ -9,6 +9,7 @@ #include #include #include +#include #include struct block_drvr { @@ -476,5 +477,33 @@ 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_first() - get first partition driver + * + * @return - pointer to first partition driver on success, otherwise NULL + */ +static inline struct part_driver *part_driver_get_first(void) +{ + return ll_entry_start(struct part_driver, part_driver); +} + +#else +static inline int part_driver_get_count(void) +{ return 0; } + +static inline struct part_driver *part_driver_get_first(void) +{ return NULL; } +#endif /* CONFIG_PARTITIONS */ #endif /* _PART_H */