From patchwork Mon Dec 12 18:01:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brandon Maier X-Patchwork-Id: 1715118 X-Patchwork-Delegate: david.oberhollenzer@sigma-star.at Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=ddeJBaAl; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=collins.com header.i=@collins.com header.a=rsa-sha256 header.s=POD051818 header.b=pW2ysjev; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NW8cZ3Thyz240L for ; Tue, 13 Dec 2022 05:03:10 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ZUFptJvCmxmLgQodZr6qRZ7Bw1XZ4IMYDUuIw2typJo=; b=ddeJBaAlUlrQDI 6hOLlwQYRZl6Emf8SNoNqO04UDU7k+KwZdXbg9uNOUYyhfZa+c8bwXJk6ffxCdtOYwtz/8s5LK+8H kj5qS7vJG4vmyW/TTo6iSlfwDQEHLpq1umEmC3hojV78dBT2kRMBwfcPX+LlDGOjdNuEcFL4IF7Uq qpiw2OPnU+cFDB4ulzC6SbVnNZ6/QVFAba8S7vA/iJulnIZAhmkzhTLS7O6So5ZwcB758takL5sIn 5pYzLjlt0wpceBaw0qBYRvF90eU7JVIkCz6Ikqli/SrcZPLhEODiTWCwMU5Tt4C09VkXsOyTGdAeP t8h49OsieD7t+qV9soCw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1p4n8F-003OcI-Uo; Mon, 12 Dec 2022 18:02:27 +0000 Received: from mx0b-00105401.pphosted.com ([67.231.152.184] helo=mx0a-00105401.pphosted.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1p4n8C-003OWt-Nl for linux-mtd@lists.infradead.org; Mon, 12 Dec 2022 18:02:26 +0000 Received: from pps.filterd (m0081755.ppops.net [127.0.0.1]) by mx0b-00105401.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 2BCHwUN3045791; Mon, 12 Dec 2022 18:02:16 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=collins.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=POD051818; bh=bBAN1PGlfNqdwz57J+l7D/AqdaGoH7YC6LCfXRgW8Wg=; b=pW2ysjevg5ZYrLZrXlv5dagm+8dz0fUDe6is6lIzlXKHYfrd7f4vtvIRqaHoHEfLjrt0 jPoMBssxpYeAOk6DEevdjAZUgkXuB6FuhCGE0nlk42oQLqaSS8C9aOI5beXDSu4BNILD FFw2G4bsA5wyQBqtH5nxyS8XjDV8VHGgJnarOYJt1xCt1f+NCBgCjj9jxI/DbJHQxFG+ ef1g1mVeX5COOuScSlS5nDO336CSbmKXLMl0BGP0NO7AF6m0tgVe/LAkx4LlgbvkiXt6 StE5W2bcsygKHFQvLdZ+kF5qqf1zJumvFp+Bhq12sNjUB14r2w3iE011LP+rLskzdcOm gw== Received: from xmnpv38.utc.com ([167.17.239.12]) by mx0b-00105401.pphosted.com with ESMTP id 3md3sepgbk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 12 Dec 2022 18:02:15 +0000 Received: from qusnwafe.utcapp.com (qusnwafe.utcapp.com [10.161.48.101]) by xmnpv38.utc.com (8.16.1.2/8.16.1.2) with ESMTPS id 2BCI2E45129599 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 12 Dec 2022 18:02:14 GMT Received: from dtulimr02.rockwellcollins.com ([10.172.224.19]) by qusnwafe.utcapp.com (8.16.1.2/8.16.1.2) with ESMTPS id 2BCI2D5l024458 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Mon, 12 Dec 2022 18:02:14 GMT X-Received: from kirk.rockwellcollins.lab (kirk.rockwellcollins.lab [10.148.204.208]) by dtulimr02.rockwellcollins.com (Postfix) with ESMTP id B251E20068; Mon, 12 Dec 2022 12:02:13 -0600 (CST) From: Brandon Maier To: MTD Mailing List Cc: David Oberhollenzer , Brandon Maier Subject: [PATCH mtd-utils 1/2] libmtd: Add function to get MTD info by device name Date: Mon, 12 Dec 2022 12:01:57 -0600 Message-Id: <20221212180158.57833-2-brandon.maier@collins.com> X-Mailer: git-send-email 2.38.2 In-Reply-To: <20221212180158.57833-1-brandon.maier@collins.com> References: <20221212180158.57833-1-brandon.maier@collins.com> MIME-Version: 1.0 X-Proofpoint-GUID: OKSPinePCPlae_ZCxrOSBwpiBMirFxAH X-Proofpoint-ORIG-GUID: OKSPinePCPlae_ZCxrOSBwpiBMirFxAH X-Proofpoint-Spam-Details: rule=outbound_default_notspam policy=outbound_default score=0 clxscore=1011 priorityscore=1501 suspectscore=0 lowpriorityscore=0 impostorscore=0 mlxlogscore=999 bulkscore=0 malwarescore=0 spamscore=0 phishscore=0 adultscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2212120163 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221212_100224_922621_9101413B X-CRM114-Status: GOOD ( 23.76 ) X-Spam-Score: -0.9 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: This is a convenience function for end users. In some situations it's easier to reference MTD device's by their name then by MTD number, as the name may be more reliable if device partitioning is dyna [...] Content analysis details: (-0.9 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at https://www.dnswl.org/, low trust [67.231.152.184 listed in list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 SPF_NONE SPF: sender does not publish an SPF Record 0.0 RCVD_IN_MSPIKE_H3 RBL: Good reputation (+3) [67.231.152.184 listed in wl.mailspike.net] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.0 RCVD_IN_MSPIKE_WL Mailspike good senders X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org This is a convenience function for end users. In some situations it's easier to reference MTD device's by their name then by MTD number, as the name may be more reliable if device partitioning is dynamic or for porting between systems. Signed-off-by: Brandon Maier --- include/libmtd.h | 13 ++++++++- lib/libmtd.c | 68 ++++++++++++++++++++++++++++++++++++++++++++- lib/libmtd_int.h | 1 + lib/libmtd_legacy.c | 38 +++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 2 deletions(-) diff --git a/include/libmtd.h b/include/libmtd.h index 6ab0de5..f909a16 100644 --- a/include/libmtd.h +++ b/include/libmtd.h @@ -130,7 +130,7 @@ int mtd_get_info(libmtd_t desc, struct mtd_info *info); /** * mtd_get_dev_info - get information about an MTD device. * @desc: MTD library descriptor - * @node: name of the MTD device node + * @node: path of the MTD device node * @mtd: the MTD device information is returned here * * This function gets information about MTD device defined by the @node device @@ -151,6 +151,17 @@ int mtd_get_dev_info(libmtd_t desc, const char *node, struct mtd_dev_info *mtd); */ int mtd_get_dev_info1(libmtd_t desc, int mtd_num, struct mtd_dev_info *mtd); +/** + * mtd_get_dev_info2 - get information about an MTD device. + * @desc: MTD library descriptor + * @name: name of the MTD device + * @mtd: the MTD device information is returned here + * + * This function is identical to 'mtd_get_dev_info()' except that it accepts + * MTD device's name, not MTD character device. + */ +int mtd_get_dev_info2(libmtd_t desc, const char *name, struct mtd_dev_info *mtd); + /** * mtd_lock - lock eraseblocks. * @desc: MTD library descriptor diff --git a/lib/libmtd.c b/lib/libmtd.c index 4aee947..f588e09 100644 --- a/lib/libmtd.c +++ b/lib/libmtd.c @@ -427,7 +427,7 @@ static int type_str2int(const char *str) /** * dev_node2num - find MTD device number by its character device node. * @lib: MTD library descriptor - * @node: name of the MTD device node + * @node: path of the MTD device node * @mtd_num: MTD device number is returned here * * This function returns %0 in case of success and %-1 in case of failure. @@ -476,6 +476,58 @@ static int dev_node2num(struct libmtd *lib, const char *node, int *mtd_num) return -1; } +/** + * dev_name2num - find MTD device number by its MTD name + * @lib: MTD library descriptor + * @name: name of the MTD device + * @mtd_num: MTD device number is returned here + * + * This function returns %0 in case of success and %-1 in case of failure. + */ +static int dev_name2num(struct libmtd *lib, const char *name, int *mtd_num) +{ + struct mtd_info info; + char name2[MTD_NAME_MAX + 1]; + int i, mtd_num_tmp = -1; + + if (mtd_get_info((libmtd_t *)lib, &info)) + return -1; + + for (i = info.lowest_mtd_num; i <= info.highest_mtd_num; i++) { + int ret; + + ret = dev_read_data(lib->mtd_name, i, name2, + MTD_NAME_MAX + 1); + if (ret < 0) { + if (errno == ENOENT) + continue; + if (!errno) + break; + return -1; + } + name2[ret - 1] = '\0'; + + if (!strcmp(name, name2)) { + // Device name collision + if (mtd_num_tmp >= 0) { + errmsg("Multiple MTD's found matching name %s", name); + errno = ENODEV; + return -1; + } + + mtd_num_tmp = i; + } + } + + if (mtd_num_tmp < 0) { + errno = ENODEV; + return -1; + } + + *mtd_num = mtd_num_tmp; + return 0; +} + /** * sysfs_is_supported - check whether the MTD sub-system supports MTD. * @lib: MTD library descriptor @@ -817,6 +869,20 @@ int mtd_get_dev_info(libmtd_t desc, const char *node, struct mtd_dev_info *mtd) return mtd_get_dev_info1(desc, mtd_num, mtd); } +int mtd_get_dev_info2(libmtd_t desc, const char *name, struct mtd_dev_info *mtd) +{ + int mtd_num; + struct libmtd *lib = (struct libmtd *)desc; + + if (!lib->sysfs_supported) + return legacy_get_dev_info2(name, mtd); + + if (dev_name2num(lib, name, &mtd_num)) + return -1; + + return mtd_get_dev_info1(desc, mtd_num, mtd); +} + static inline int mtd_ioctl_error(const struct mtd_dev_info *mtd, int eb, const char *sreq) { diff --git a/lib/libmtd_int.h b/lib/libmtd_int.h index c0514d2..79200f7 100644 --- a/lib/libmtd_int.h +++ b/lib/libmtd_int.h @@ -106,6 +106,7 @@ int legacy_dev_present(int mtd_num); int legacy_mtd_get_info(struct mtd_info *info); int legacy_get_dev_info(const char *node, struct mtd_dev_info *mtd); int legacy_get_dev_info1(int dev_num, struct mtd_dev_info *mtd); +int legacy_get_dev_info2(const char *name, struct mtd_dev_info *mtd); int legacy_get_mtd_oobavail(const char *node); int legacy_get_mtd_oobavail1(int mtd_num); diff --git a/lib/libmtd_legacy.c b/lib/libmtd_legacy.c index 4eb4a70..e0ecf49 100644 --- a/lib/libmtd_legacy.c +++ b/lib/libmtd_legacy.c @@ -428,3 +428,41 @@ int legacy_get_dev_info1(int mtd_num, struct mtd_dev_info *mtd) sprintf(node, MTD_DEV_PATT, mtd_num); return legacy_get_dev_info(node, mtd); } + +/** + * legacy_get_dev_info2 - legacy version of `mtd_get_dev_info2()` + * @name: name of the MTD device + * @mtd: the MTD device information is returned here + * + * This function is similar to 'mtd_get_dev_info2()' and has the same + * conventions. + */ +int legacy_get_dev_info2(const char *name, struct mtd_dev_info *mtd) +{ + struct proc_parse_info pi; + int ret, mtd_num = -1; + + ret = proc_parse_start(&pi); + if (ret) + return -1; + + while (proc_parse_next(&pi)) { + if (!strcmp(name, pi.name)) { + // Device name collision + if (mtd_num >= 0) { + errmsg("Multiple MTD's found matching name %s", name); + errno = ENODEV; + return -1; + } + + mtd_num = pi.mtd_num; + } + } + + if (mtd_num < 0) { + errno = ENODEV; + return -1; + } + + return legacy_get_dev_info1(mtd_num, mtd); +} From patchwork Mon Dec 12 18:01:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brandon Maier X-Patchwork-Id: 1715117 X-Patchwork-Delegate: david.oberhollenzer@sigma-star.at Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=BLZA78O8; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=collins.com header.i=@collins.com header.a=rsa-sha256 header.s=POD051818 header.b=bqCLWO7n; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NW8cZ1JYJz23pD for ; Tue, 13 Dec 2022 05:03:10 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=4r0UReDOHzVKArHlChOlQE11aJthV3X1kUT1ek0Q488=; b=BLZA78O89Pfh5Y m3AOLeMmfshQ4VRlYWNLr1SZggaKH351ml7mmi535JXuQB+cGMqVoLuehBfw1baZLPa9WjciUdC3q 7W61Uj7VSk9Rm4gKkxvzGvq4u7BxjPnWNrTKw3c+4/VNCsLl5p0boDgLe/wYOnlkGmkhp9y8QqOnj 4H10I+1GdXFWwVJPSGTEGZc2fibIu5ijldXJGMaqeDVmOk9hRKGNgtk1jQrc+zrk47JHqAY6yPHEY cR0MOZ56uOfUCjwWj3zMuzB7310ptritZme7ZlXlbgZlr2nE7ulqvQx4v0RM//53T69FSlLdcDaxy c7WmKMixbRD3d/GIjRnA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1p4n8I-003Ocq-Ew; Mon, 12 Dec 2022 18:02:30 +0000 Received: from mx0b-00105401.pphosted.com ([67.231.152.184]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1p4n8C-003OWu-N2 for linux-mtd@lists.infradead.org; Mon, 12 Dec 2022 18:02:27 +0000 Received: from pps.filterd (m0074334.ppops.net [127.0.0.1]) by mx0b-00105401.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 2BCHxEmD004751; Mon, 12 Dec 2022 18:02:16 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=collins.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=POD051818; bh=GuNYFGyH9Nlp3LquvN8+j/Ql/o5oRPsruOOvfPeIuGo=; b=bqCLWO7niY2q/dBNnyp2Zsh91XwzGsuXNSf+ecwFX2sHPtywS+YAr0IFECsoME/zzVTo roL5OPEu/u6AKVsIxHqpAMEpcVYlcsCYgzIFsy+1JDB8dIozLwFnfT37EMF1Kezz3/Ct zDvw0ZQihTK9vYX2Ge8LQmScvKQj0IxDmSIx5lmZZI8cngXmEoL0ERbDMnNz2a/ub2fN P5fIKEXCuwW6s9Q+pGlhDAvJ/lbzqx3xUPSzm+3OZPWROJHHD+YsPHOzgxEolvNkKPgw 3+77MJrKTq2XHXswBJZzHqgcBb63/b4RD5BB9DsOb0xKjDkeQhJjk8i5LrjEMDEcY3Qq mA== Received: from xmnpv38.utc.com ([167.17.239.12]) by mx0b-00105401.pphosted.com with ESMTP id 3md445pdsd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 12 Dec 2022 18:02:15 +0000 Received: from qusnwafg.utcapp.com (qusnwafg.utcapp.com [10.161.48.103]) by xmnpv38.utc.com (8.16.1.2/8.16.1.2) with ESMTPS id 2BCI2EOq129612 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 12 Dec 2022 18:02:14 GMT Received: from dtulimr02.rockwellcollins.com ([10.172.224.19]) by qusnwafg.utcapp.com (8.16.1.2/8.16.1.2) with ESMTPS id 2BCI2EA5014040 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Mon, 12 Dec 2022 18:02:14 GMT X-Received: from kirk.rockwellcollins.lab (kirk.rockwellcollins.lab [10.148.204.208]) by dtulimr02.rockwellcollins.com (Postfix) with ESMTP id E94802006C; Mon, 12 Dec 2022 12:02:13 -0600 (CST) From: Brandon Maier To: MTD Mailing List Cc: David Oberhollenzer , Brandon Maier Subject: [PATCH mtd-utils 2/2] mtd-utils: Add new syntax to get devices by name Date: Mon, 12 Dec 2022 12:01:58 -0600 Message-Id: <20221212180158.57833-3-brandon.maier@collins.com> X-Mailer: git-send-email 2.38.2 In-Reply-To: <20221212180158.57833-1-brandon.maier@collins.com> References: <20221212180158.57833-1-brandon.maier@collins.com> MIME-Version: 1.0 X-Proofpoint-GUID: ESrpmErtG61EJVU6A9BykAKXoPCtgVlR X-Proofpoint-ORIG-GUID: ESrpmErtG61EJVU6A9BykAKXoPCtgVlR X-Proofpoint-Spam-Details: rule=outbound_default_notspam policy=outbound_default score=0 impostorscore=0 priorityscore=1501 lowpriorityscore=0 bulkscore=0 adultscore=0 clxscore=1011 mlxscore=0 phishscore=0 spamscore=0 suspectscore=0 malwarescore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2212120163 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221212_100224_927869_96FA91B7 X-CRM114-Status: GOOD ( 33.93 ) X-Spam-Score: -0.9 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: This introduces a new feature to the MTD command line utilities that allows MTD devices to be referenced by name instead of device node. For example this looks like: > # Display info for the MTD device with name "data" > mtdinfo mtd:data > # Copy file to MTD device with name "data" > flashcp /my/file mtd:data Content analysis details: (-0.9 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at https://www.dnswl.org/, low trust [67.231.152.184 listed in list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 SPF_NONE SPF: sender does not publish an SPF Record 0.0 RCVD_IN_MSPIKE_H3 RBL: Good reputation (+3) [67.231.152.184 listed in wl.mailspike.net] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.0 RCVD_IN_MSPIKE_WL Mailspike good senders X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org This introduces a new feature to the MTD command line utilities that allows MTD devices to be referenced by name instead of device node. For example this looks like: > # Display info for the MTD device with name "data" > mtdinfo mtd:data > # Copy file to MTD device with name "data" > flashcp /my/file mtd:data This follows the syntax supported by the kernel which allows MTD device's to be mounted by name[1]. Add the function mtd_find_dev_node() that accepts an MTD "identifier" and returns the MTD's device node. The function accepts a string starting with "mtd:" which it treats as the MTD's name. It then attempts to search for the MTD, and if found maps it back to the /dev/mtdX device node. If the string does not start with "mtd:", then assume it's the old style and refers directly to a MTD device node. The function is then hooked into existing tools like flashcp, mtdinfo, flash_unlock, etc. To load in the new MTD parsing code in a consistent way across programs. [1] http://www.linux-mtd.infradead.org/faq/jffs2.html#L_mtdblock Signed-off-by: Brandon Maier --- include/common.h | 1 + lib/common.c | 46 ++++++++++++++++++++++++++++++++ misc-utils/Makemodule.am | 5 ++++ misc-utils/flash_erase.c | 6 ++++- misc-utils/flash_unlock.c | 9 ++++++- misc-utils/flashcp.c | 7 +++-- misc-utils/mtd_debug.c | 7 ++++- misc-utils/mtdpart.c | 7 ++++- tests/mtd-tests/flash_readtest.c | 10 ++++--- tests/mtd-tests/flash_speed.c | 10 ++++--- tests/mtd-tests/flash_stress.c | 10 ++++--- tests/mtd-tests/flash_torture.c | 10 ++++--- ubi-utils/mtdinfo.c | 13 ++++++--- 13 files changed, 119 insertions(+), 22 deletions(-) diff --git a/include/common.h b/include/common.h index 31b6cd1..303d30d 100644 --- a/include/common.h +++ b/include/common.h @@ -236,6 +236,7 @@ do { \ long long util_get_bytes(const char *str); void util_print_bytes(long long bytes, int bracket); int util_srand(void); +char *mtd_find_dev_node(const char *id); /* * The following helpers are here to avoid compiler complaints about unchecked diff --git a/lib/common.c b/lib/common.c index 8041878..e278593 100644 --- a/lib/common.c +++ b/lib/common.c @@ -33,6 +33,9 @@ #include #include #include "common.h" +#include "libmtd.h" + +#define MTD_DEV_PATT "/dev/mtd%d" /** * get_multiplier - convert size specifier to an integer multiplier. @@ -162,3 +165,46 @@ int util_srand(void) srand(seed); return 0; } + +/** + * mtd_find_dev_node - Find the device node for an MTD + * @id: Identifier for the MTD. this can be the device node itself, or + * "mtd:" to look up MTD by name + * + * This is a helper function to convert MTD device identifiers into their + * device node. + * + * Returns a pointer to a string containing the device node that must be + * free'd, or NULL on failure. + */ +char *mtd_find_dev_node(const char *id) +{ + struct mtd_dev_info info; + struct libmtd_t *lib_mtd; + char *node; + int ret; + + if (strncmp(id, "mtd:", 4)) { + /* Assume @id is the device node */ + return strdup(id); + } + + /* Search for MTD matching name */ + id += 4; + + lib_mtd = libmtd_open(); + if (!lib_mtd) + return NULL; + + ret = mtd_get_dev_info2(lib_mtd, id, &info); + libmtd_close(lib_mtd); + if (ret < 0) + return NULL; + + node = malloc(strlen(MTD_DEV_PATT) + 20); + if (!node) + return NULL; + + sprintf(node, MTD_DEV_PATT, info.mtd_num); + return node; +} diff --git a/misc-utils/Makemodule.am b/misc-utils/Makemodule.am index bc69b1c..1ce1a68 100644 --- a/misc-utils/Makemodule.am +++ b/misc-utils/Makemodule.am @@ -7,8 +7,10 @@ ftl_check_SOURCES = misc-utils/ftl_check.c include/mtd_swab.h ftl_check_SOURCES += include/mtd/ftl-user.h mtd_debug_SOURCES = misc-utils/mtd_debug.c +mtd_debug_LDADD = libmtd.a mtdpart_SOURCES = misc-utils/mtdpart.c +mtdpart_LDADD = libmtd.a docfdisk_SOURCES = misc-utils/docfdisk.c include/mtd_swab.h docfdisk_SOURCES += include/mtd/inftl-user.h include/mtd/ftl-user.h @@ -23,8 +25,10 @@ fectest_SOURCES = misc-utils/fectest.c misc-utils/mcast_image.h fectest_LDADD = libmtd.a flash_lock_SOURCES = misc-utils/flash_lock.c +flash_lock_LDADD = libmtd.a flash_unlock_SOURCES = misc-utils/flash_unlock.c +flash_unlock_LDADD = libmtd.a flash_otp_info_SOURCES = misc-utils/flash_otp_info.c @@ -37,6 +41,7 @@ flash_otp_erase_SOURCES = misc-utils/flash_otp_erase.c flash_otp_write_SOURCES = misc-utils/flash_otp_write.c flashcp_SOURCES = misc-utils/flashcp.c +flashcp_LDADD = libmtd.a flash_erase_SOURCES = misc-utils/flash_erase.c flash_erase_LDADD = libmtd.a diff --git a/misc-utils/flash_erase.c b/misc-utils/flash_erase.c index 49a880f..000f94a 100644 --- a/misc-utils/flash_erase.c +++ b/misc-utils/flash_erase.c @@ -71,6 +71,8 @@ static void display_help (void) " --silent same as --quiet\n" " --help display this help and exit\n" " --version output version information and exit\n", + "\n" + " MTD_DEVICE MTD device node or 'mtd:'\n" PROGRAM_NAME); } @@ -169,7 +171,9 @@ int main(int argc, char *argv[]) } switch (argc - optind) { case 3: - mtd_device = argv[optind]; + mtd_device = mtd_find_dev_node(argv[optind]); + if (!mtd_device) + return errmsg("Can't find MTD device %s", argv[optind]); start = simple_strtoull(argv[optind + 1], &error); eb_cnt = simple_strtoul(argv[optind + 2], &error); break; diff --git a/misc-utils/flash_unlock.c b/misc-utils/flash_unlock.c index fbbfa51..fa5decb 100644 --- a/misc-utils/flash_unlock.c +++ b/misc-utils/flash_unlock.c @@ -51,6 +51,8 @@ static NORETURN void usage(int status) " -l --lock Lock a region of flash\n" " -u --unlock Unlock a region of flash\n" "\n" + " MTD device node or 'mtd:'\n" + "\n" "If offset is not specified, it defaults to 0.\n" "If block count is not specified, it defaults to all blocks.\n" "A block count of -1 means all blocks.\n", @@ -125,7 +127,12 @@ static void process_args(int argc, char *argv[]) } /* First non-option argument */ - dev = argv[arg_idx++]; + dev = mtd_find_dev_node(argv[arg_idx]); + if (!dev) { + errmsg("MTD device not found %s", argv[arg_idx]); + usage(EXIT_FAILURE); + } + arg_idx++; /* Second non-option argument */ if (arg_idx < argc) diff --git a/misc-utils/flashcp.c b/misc-utils/flashcp.c index 2e8d4c1..1411b2d 100644 --- a/misc-utils/flashcp.c +++ b/misc-utils/flashcp.c @@ -110,7 +110,7 @@ static NORETURN void showusage(bool error) " -A | --erase-all Erases the whole device regardless of the image size\n" " -V | --version Show version information and exit\n" " File which you want to copy to flash\n" - " Flash device to write to (e.g. /dev/mtd0, /dev/mtd1, etc.)\n" + " Flash device node or 'mtd:' to write to (e.g. /dev/mtd0, /dev/mtd1, mtd:data, etc.)\n" "\n", PROGRAM_NAME); @@ -275,7 +275,10 @@ int main (int argc,char *argv[]) DEBUG("Got filename: %s\n",filename); flags |= FLAG_DEVICE; - device = argv[optind+1]; + device = mtd_find_dev_node(argv[optind+1]); + if (!device) + log_failure("Failed to find device %s\n", argv[optind+1]); + DEBUG("Got device: %s\n",device); } diff --git a/misc-utils/mtd_debug.c b/misc-utils/mtd_debug.c index c0b7109..abee5e3 100644 --- a/misc-utils/mtd_debug.c +++ b/misc-utils/mtd_debug.c @@ -348,6 +348,7 @@ int main(int argc, char *argv[]) { int err = 0, fd; int open_flag; + char *dev; enum { OPT_INFO, @@ -369,8 +370,12 @@ int main(int argc, char *argv[]) showusage(); /* open device */ + dev = mtd_find_dev_node(argv[2]); + if (!dev) + errmsg_die("Failed to find MTD device %s", argv[2]); + open_flag = (option == OPT_INFO || option == OPT_READ) ? O_RDONLY : O_RDWR; - if ((fd = open(argv[2], O_SYNC | open_flag)) < 0) + if ((fd = open(dev, O_SYNC | open_flag)) < 0) errmsg_die("open()"); switch (option) { diff --git a/misc-utils/mtdpart.c b/misc-utils/mtdpart.c index c8cd79b..a341148 100644 --- a/misc-utils/mtdpart.c +++ b/misc-utils/mtdpart.c @@ -36,6 +36,8 @@ static void display_help(int status) " -h, --help Display this help and exit\n" " -V, --version Output version information and exit\n" "\n" +" MTD device node or 'mtd:'\n" +"\n" "START location and SIZE of the partition are in bytes. They should align on\n" "eraseblock size. If SIZE is 0 the partition will go to end of MTD device.\n", PROGRAM_NAME @@ -106,7 +108,10 @@ static void process_options(int argc, char * const argv[]) display_help(EXIT_FAILURE); const char *s_command = argv[optind++]; - mtddev = argv[optind++]; + mtddev = mtd_find_dev_node(argv[optind]); + if (!mtddev) + errmsg_die("MTD device not found %s", argv[optind]); + optind++; if (strcmp(s_command, "del") == 0 && (argc - optind) == 1) { const char *s_part_no = argv[optind++]; diff --git a/tests/mtd-tests/flash_readtest.c b/tests/mtd-tests/flash_readtest.c index b4f4e10..519ff89 100644 --- a/tests/mtd-tests/flash_readtest.c +++ b/tests/mtd-tests/flash_readtest.c @@ -125,10 +125,14 @@ static void process_options(int argc, char **argv) } } - if (optind < argc) - mtddev = argv[optind++]; - else + if (optind < argc) { + mtddev = mtd_find_dev_node(argv[optind]); + if (!mtddev) + errmsg_die("Can't find MTD device %s", argv[optind]); + optind++; + } else { errmsg_die("No device specified!\n"); + } if (optind < argc) usage(EXIT_FAILURE); diff --git a/tests/mtd-tests/flash_speed.c b/tests/mtd-tests/flash_speed.c index 0f82047..5721dfb 100644 --- a/tests/mtd-tests/flash_speed.c +++ b/tests/mtd-tests/flash_speed.c @@ -141,10 +141,14 @@ static void process_options(int argc, char **argv) } } - if (optind < argc) - mtddev = argv[optind++]; - else + if (optind < argc) { + mtddev = mtd_find_dev_node(argv[optind]); + if (!mtddev) + errmsg_die("Can't find MTD device %s", argv[optind]); + optind++; + } else { errmsg_die("No device specified!\n"); + } if (optind < argc) usage(EXIT_FAILURE); diff --git a/tests/mtd-tests/flash_stress.c b/tests/mtd-tests/flash_stress.c index b7a0fec..da39e14 100644 --- a/tests/mtd-tests/flash_stress.c +++ b/tests/mtd-tests/flash_stress.c @@ -126,10 +126,14 @@ static void process_options(int argc, char **argv) } } - if (optind < argc) - mtddev = argv[optind++]; - else + if (optind < argc) { + mtddev = mtd_find_dev_node(argv[optind]); + if (!mtddev) + errmsg_die("Can't find MTD device %s", argv[optind]); + optind++; + } else { errmsg_die("No device specified!\n"); + } if (optind < argc) usage(EXIT_FAILURE); diff --git a/tests/mtd-tests/flash_torture.c b/tests/mtd-tests/flash_torture.c index 5aad8e0..6363f9e 100644 --- a/tests/mtd-tests/flash_torture.c +++ b/tests/mtd-tests/flash_torture.c @@ -144,10 +144,14 @@ static void process_options(int argc, char **argv) } } - if (optind < argc) - mtddev = argv[optind++]; - else + if (optind < argc) { + mtddev = mtd_find_dev_node(argv[optind]); + if (!mtddev) + errmsg_die("Can't find MTD device %s", argv[optind]); + optind++; + } else { errmsg_die("No device specified!\n"); + } if (optind < argc) usage(EXIT_FAILURE); diff --git a/ubi-utils/mtdinfo.c b/ubi-utils/mtdinfo.c index 8bd0fc8..154872d 100644 --- a/ubi-utils/mtdinfo.c +++ b/ubi-utils/mtdinfo.c @@ -54,7 +54,7 @@ static void display_help(void) printf( "%1$s version %2$s - a tool to print MTD information.\n" "\n" - "Usage: %1$s [--map | -M] [--ubi-info | -u]\n" + "Usage: %1$s [--map | -M] [--ubi-info | -u]\n" " %1$s --all [--ubi-info | -u]\n" " %1$s [--help | --version]\n" "\n" @@ -68,6 +68,8 @@ static void display_help(void) "-h, --help print help message\n" "-V, --version print program version\n" "\n" + " MTD device node or 'mtd:'\n" + "\n" "Examples:\n" " %1$s /dev/mtd0 print information MTD device /dev/mtd0\n" " %1$s /dev/mtd0 -u print information MTD device /dev/mtd0\n" @@ -124,10 +126,13 @@ static int parse_opt(int argc, char * const argv[]) } } - if (optind == argc - 1) - args.node = argv[optind]; - else if (optind < argc) + if (optind == argc - 1) { + args.node = mtd_find_dev_node(argv[optind]); + if (!args.node) + return errmsg("Failed to find MTD device %s", argv[optind]); + } else if (optind < argc) { return errmsg("more then one MTD device specified (use -h for help)"); + } if (args.all && args.node) args.node = NULL;