From patchwork Wed Sep 26 15:07:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 975204 X-Patchwork-Delegate: jagannadh.teki@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bootlin.com Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42L1Zy1LzMz9s1c for ; Thu, 27 Sep 2018 01:10:18 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id D29C8C21E0F; Wed, 26 Sep 2018 15:09:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id B63EDC21E70; Wed, 26 Sep 2018 15:07:57 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 6D7E5C21C29; Wed, 26 Sep 2018 15:07:54 +0000 (UTC) Received: from mail.bootlin.com (mail.bootlin.com [62.4.15.54]) by lists.denx.de (Postfix) with ESMTP id 29FA8C21C6A for ; Wed, 26 Sep 2018 15:07:54 +0000 (UTC) Received: by mail.bootlin.com (Postfix, from userid 110) id E304920737; Wed, 26 Sep 2018 17:07:52 +0200 (CEST) Received: from localhost.localdomain (AAubervilliers-681-1-42-80.w90-88.abo.wanadoo.fr [90.88.160.80]) by mail.bootlin.com (Postfix) with ESMTPSA id 8A413207CA; Wed, 26 Sep 2018 17:07:42 +0200 (CEST) From: Miquel Raynal To: Jagan Teki , Tom Rini , Daniel Schwierzeck Date: Wed, 26 Sep 2018 17:07:35 +0200 Message-Id: <20180926150739.15316-4-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180926150739.15316-1-miquel.raynal@bootlin.com> References: <20180926150739.15316-1-miquel.raynal@bootlin.com> Cc: Boris Brezillon , Antoine Tenart , Allan Nielsen , u-boot@lists.denx.de, Miquel Raynal , Stefan Roese Subject: [U-Boot] [PATCH v9 3/7] mtd: mtdpart: search for an equivalent MTD name with the mtdids X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Using an MTD device (resp. partition) name in mtdparts is simple and straightforward. However, for a long time already, another name was given in mtdparts to indicate a device (resp. partition) so the "mtdids" environment variable was created to do the match. Let's create a function that, from an MTD device (resp. partition) name, search for the equivalent name in the "mtdparts" environment variable thanks to the "mtdids" string. Signed-off-by: Miquel Raynal Reviewed-by: Stefan Roese --- drivers/mtd/mtdpart.c | 61 ++++++++++++++++++++++++++++++++++ include/linux/mtd/partitions.h | 2 ++ 2 files changed, 63 insertions(+) diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c index 30b40cea22..eb4eba5c5b 100644 --- a/drivers/mtd/mtdpart.c +++ b/drivers/mtd/mtdpart.c @@ -285,6 +285,67 @@ void mtd_free_parsed_partitions(struct mtd_partition *parts, free(parts); } +/** + * mtd_search_alternate_name - Search an alternate name for @mtdname thanks to + * the mtdids legacy environment variable. + * + * The mtdids string is a list of comma-separated 'dev_id=mtd_id' tupples. + * Check if one of the mtd_id matches mtdname, in this case save dev_id in + * altname. + * + * @mtdname: Current MTD device name + * @altname: Alternate name to return + * @max_len: Length of the alternate name buffer + * + * @return 0 on success, an error otherwise. + */ +int mtd_search_alternate_name(const char *mtdname, char *altname, + unsigned int max_len) +{ + const char *mtdids, *equal, *comma, *dev_id, *mtd_id; + int dev_id_len, mtd_id_len; + + mtdids = env_get("mtdids"); + if (!mtdids) + return -EINVAL; + + do { + /* Find the '=' sign */ + dev_id = mtdids; + equal = strchr(dev_id, '='); + if (!equal) + break; + dev_id_len = equal - mtdids; + mtd_id = equal + 1; + + /* Find the end of the tupple */ + comma = strchr(mtdids, ','); + if (comma) + mtd_id_len = comma - mtd_id; + else + mtd_id_len = &mtdids[strlen(mtdids)] - mtd_id + 1; + + if (!dev_id_len || !mtd_id_len) + return -EINVAL; + + if (dev_id_len + 1 > max_len) + continue; + + /* Compare the name we search with the current mtd_id */ + if (!strncmp(mtdname, mtd_id, mtd_id_len)) { + strncpy(altname, dev_id, dev_id_len); + altname[dev_id_len] = 0; + + return 0; + } + + /* Go to the next tupple */ + mtdids = comma + 1; + } while (comma); + + return -EINVAL; +} + /* * MTD methods which simply translate the effective address and pass through * to the _real_ device. diff --git a/include/linux/mtd/partitions.h b/include/linux/mtd/partitions.h index 5e4aab1dea..d2fcd0e0d0 100644 --- a/include/linux/mtd/partitions.h +++ b/include/linux/mtd/partitions.h @@ -90,5 +90,7 @@ int mtd_parse_partitions(struct mtd_info *parent, const char **_mtdparts, struct mtd_partition **_parts, int *_nb_parts); void mtd_free_parsed_partitions(struct mtd_partition *parts, unsigned int nparts); +int mtd_search_alternate_name(const char *mtdname, char *altname, + unsigned int max_len); #endif