From patchwork Wed Dec 12 17:12:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ruslan Trofymenko X-Patchwork-Id: 153580 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2052449ljp; Wed, 12 Dec 2018 09:13:49 -0800 (PST) X-Google-Smtp-Source: AFSGD/WTt+fZPgF7wFm4Q2rXenNIFKUuFOW6iY//KZBWWdcCX+fuKLieu3C50NGD2Bj7Ik3tgbrC X-Received: by 2002:a05:6402:1643:: with SMTP id s3mr19364843edx.66.1544634829709; Wed, 12 Dec 2018 09:13:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544634829; cv=none; d=google.com; s=arc-20160816; b=Kq23mMBtKTEICPXje/AonArzHk8zCXdEqewL5yjq4aOv+8/s2R1Ocef8ucVTA8XCwO S2ZEmO6p/0d4rCX1vE7VXC0NoiEoyI8lclb9Wt8H54f7yFm076MrCSEK04tttIdUSCQq k1Ng+faSbDnNwevGm+06/NZGht0KPYmhrKHHN1KShttzQSvRRbVhH+dn1Rwh9QIbQ+ZF KWck5ISe3jVdtdBXGe7Pd0MyucP6t0MgCZkyx89i0kbvI72F8Esid2Tm4H685r4AKU5Q QqI71SeBoaAz4HXjhHbPwyK0xoZPmAL5X9WZnYFNRZ8KRdyQ1iqHLMyZhbHNYLO3aPrH t6cA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:cc:references:in-reply-to:message-id :date:to:from:dkim-signature; bh=5j8ri2vZlRnLANWIXhs/Wq2AYoWjeqB0ygLVH+oaVk0=; b=da1GVq6HmB+V4NaL4dDWzNV3hCgo+h83rGmOWDKOqCrUSPX01CEHFV+kJYWPAMjPAm eqzIhZhzmjkKpg5xcv3AdkAwj31g5oDvBAuQzq9fJdLH3MBJ5sBZ4xrUY5E39W7dAC6I zclRkRPiBaltPKTdnlsVySyF5EBchKL8bEJW4AZ4XA77+KPGhAXVEh2Zxl8yBr6VX783 +BG62olRDdZw6ANZZ4ywh7pCCKOfXCeoTemz3z8cXRHuU8NAea5i3oTiB2Npysh0bNCR l9Kai7eTXH6tYJr2Dbz3FATavwpTuUJBK2ds2ttbnem1q9yx9LIFSXofBlH56yxBCAZ5 qp5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=RchUe9zh; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.denx.de (dione.denx.de. [81.169.180.215]) by mx.google.com with ESMTP id gx11-v6si4467510ejb.297.2018.12.12.09.13.49; Wed, 12 Dec 2018 09:13:49 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) client-ip=81.169.180.215; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=RchUe9zh; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.denx.de (Postfix, from userid 105) id 4E920C22773; Wed, 12 Dec 2018 17:13:21 +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=RCVD_IN_MSPIKE_H2, T_DKIM_INVALID 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 AAF8DC22780; Wed, 12 Dec 2018 17:12:43 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 8A877C21F51; Wed, 12 Dec 2018 17:12:40 +0000 (UTC) Received: from mail-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) by lists.denx.de (Postfix) with ESMTPS id 2BD93C21F18 for ; Wed, 12 Dec 2018 17:12:40 +0000 (UTC) Received: by mail-lf1-f49.google.com with SMTP id p6so14135097lfc.1 for ; Wed, 12 Dec 2018 09:12:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=QqIbVjRsV6hkpdb57n+XKYo8jqMZRxMlVSSjl+oFxPM=; b=RchUe9zhUCtZvRkVdK3cvV8K0Izbj5aOfe2ge2l9fJiZ8PAvHaeFlv96wNU5eQVCpX peMdELsEAhOCfa06jdcCgIDml+4dzJUL03WnvV33XMsns82XJwLTuEx+V24QEL2jGchO v/pgyNNCeGZIlJ1RRTWL1gDUQA7plTASYny7g= 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:in-reply-to :references; bh=QqIbVjRsV6hkpdb57n+XKYo8jqMZRxMlVSSjl+oFxPM=; b=E9Owde/PSu5HMPp+hi8182o2DuIrzpCtnonSE6CQp0eTJGwSPru9HjxJri/PzPfEuw M+P24H+g6523av5aenziBQu1g5U/syqjFs7WiPAVQHh567jQZddu0mcNFey0ZdeiSTBR BuMLRl7GwR65WRoKcp26wPr5IpQpCI21JMEZHJ9vVr5yPS2boOcG3vuPPnFbSpdoWYhL QtWBbPf4+NNp9H/spsfOEfAIp45Fy7PGPiz5lwr9W+BnadrixLJPBfBy9OJmXj0kj0kF eX2BHaoZ2WbRl3OMEGjRR7p4veS9R/KOGdWB/R62X5oPYWlrSObadiXWhjd6oJry1IQU +u/g== X-Gm-Message-State: AA+aEWYH46Zb+LYphI8jFNJuVwDh0HHitud20nbAjS+lLwvz3xYCtzSR BgRtMYWpxGqQCkuv0CcYR+STl98GnD0= X-Received: by 2002:ac2:4343:: with SMTP id o3mr7089734lfl.129.1544634759062; Wed, 12 Dec 2018 09:12:39 -0800 (PST) Received: from kbp1-dhp-f54913.synapse.com ([195.238.92.132]) by smtp.gmail.com with ESMTPSA id t81sm3358020lfe.84.2018.12.12.09.12.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 12 Dec 2018 09:12:38 -0800 (PST) From: Ruslan Trofymenko To: u-boot@lists.denx.de Date: Wed, 12 Dec 2018 19:12:29 +0200 Message-Id: <1544634754-3435-3-git-send-email-ruslan.trofymenko@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1544634754-3435-1-git-send-email-ruslan.trofymenko@linaro.org> References: <1544634754-3435-1-git-send-email-ruslan.trofymenko@linaro.org> Cc: Tom Rini , Praneeth Bajjuri , Alistair Strachan Subject: [U-Boot] [PATCH v2 2/7] disk: part: Extend API to get partition info 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" This patch adds part_get_info_by_dev_and_name_or_num() function which allows us to get partition info from its number or name. Partition of interest is specified by string like "device_num:partition_number" or "device_num#partition_name". The patch was extracted from [1]. [1] https://android-review.googlesource.com/c/platform/external/u-boot/+/729880/2 Signed-off-by: Ruslan Trofymenko Reviewed-by: Alistair Strachan Reviewed-by: Sam Protsenko Reviewed-by: Simon Glass --- Changes in v2: * Error codes are changed to -EINVAL instead of -1 disk/part.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/part.h | 21 ++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/disk/part.c b/disk/part.c index f30f9e9..7b739ad 100644 --- a/disk/part.c +++ b/disk/part.c @@ -675,6 +675,74 @@ int part_get_info_by_name(struct blk_desc *dev_desc, const char *name, return part_get_info_by_name_type(dev_desc, name, info, PART_TYPE_ALL); } +/** + * Get partition info from device number and partition name. + * + * Parse a device number and partition name string in the form of + * "device_num#partition_name", for example "0#misc". If the partition + * is found, sets dev_desc and part_info accordingly with the information + * of the partition with the given partition_name. + * + * @param[in] dev_iface Device interface + * @param[in] dev_part_str Input string argument, like "0#misc" + * @param[out] dev_desc Place to store the device description pointer + * @param[out] part_info Place to store the partition information + * @return 0 on success, or a negative on error + */ +static int part_get_info_by_dev_and_name(const char *dev_iface, + const char *dev_part_str, + struct blk_desc **dev_desc, + disk_partition_t *part_info) +{ + char *ep; + const char *part_str; + int dev_num; + + part_str = strchr(dev_part_str, '#'); + if (!part_str || part_str == dev_part_str) + return -EINVAL; + + dev_num = simple_strtoul(dev_part_str, &ep, 16); + if (ep != part_str) { + /* Not all the first part before the # was parsed. */ + return -EINVAL; + } + part_str++; + + *dev_desc = blk_get_dev(dev_iface, dev_num); + if (!*dev_desc) { + printf("Could not find %s %d\n", dev_iface, dev_num); + return -EINVAL; + } + if (part_get_info_by_name(*dev_desc, part_str, part_info) < 0) { + printf("Could not find \"%s\" partition\n", part_str); + return -EINVAL; + } + return 0; +} + +int part_get_info_by_dev_and_name_or_num(const char *dev_iface, + const char *dev_part_str, + struct blk_desc **dev_desc, + disk_partition_t *part_info) +{ + /* Split the part_name if passed as "$dev_num#part_name". */ + if (!part_get_info_by_dev_and_name(dev_iface, dev_part_str, + dev_desc, part_info)) + return 0; + /* + * Couldn't lookup by name, try looking up the partition description + * directly. + */ + if (blk_get_device_part_str(dev_iface, dev_part_str, + dev_desc, part_info, 1) < 0) { + printf("Couldn't find partition %s %s\n", + dev_iface, dev_part_str); + return -EINVAL; + } + return 0; +} + void part_set_generic_name(const struct blk_desc *dev_desc, int part_num, char *name) { diff --git a/include/part.h b/include/part.h index 0750aee..9d57b65 100644 --- a/include/part.h +++ b/include/part.h @@ -202,6 +202,27 @@ int part_get_info_by_name(struct blk_desc *dev_desc, const char *name, disk_partition_t *info); /** + * Get partition info from dev number + part name, or dev number + part number. + * + * Parse a device number and partition description (either name or number) + * in the form of device number plus partition name separated by a "#" + * (like "device_num#partition_name") or a device number plus a partition number + * separated by a ":". For example both "0#misc" and "0:1" can be valid + * partition descriptions for a given interface. If the partition is found, sets + * dev_desc and part_info accordingly with the information of the partition. + * + * @param[in] dev_iface Device interface + * @param[in] dev_part_str Input partition description, like "0#misc" or "0:1" + * @param[out] dev_desc Place to store the device description pointer + * @param[out] part_info Place to store the partition information + * @return 0 on success, or a negative on error + */ +int part_get_info_by_dev_and_name_or_num(const char *dev_iface, + const char *dev_part_str, + struct blk_desc **dev_desc, + disk_partition_t *part_info); + +/** * part_set_generic_name() - create generic partition like hda1 or sdb2 * * Helper function for partition tables, which don't hold partition names