From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 131F8ECAAD5 for ; Mon, 12 Sep 2022 09:39:13 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id EF48F84A00; Mon, 12 Sep 2022 11:39:10 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="QPq6dic8"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 59FF3849AC; Mon, 12 Sep 2022 11:39:09 +0200 (CEST) Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1B21C84A27 for ; Mon, 12 Sep 2022 11:39:05 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=pali@kernel.org Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0B8A2610A3 for ; Mon, 12 Sep 2022 09:39:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4CC46C433C1 for ; Mon, 12 Sep 2022 09:39:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1662975542; bh=u5Se8OSrBD2ZiwTGgbtuhorZYELkJX7VqWLYplsAEKE=; h=From:To:Subject:Date:From; b=QPq6dic8ZAMWE/skRrTYQbFle+5GSaKda+zwy/zJi2g0HoBIDn1nce9mNyzW2KljG hWeoXFPC3j4Xd6NA43eSXYHpglc947t+n5Jy5THpyE+QO3n0SdFRcS5PIEg205ZBtZ CTuUYWH40zch6UoIifIJ51G5GVB8MB28GskA3XsY+YVIDGbBc/lf3bstGy0b3XU5hB 4JKf9xf38h48MWixDF382SEPbdd3nXNM/a5tp1rYMUsbg+xi577MJxjNWEPiXHqyRF 8n4iuQzy36oyJHPnxJ1ZrJlk9rChED5ao65JxUVJNhr0VW76gv87/8hTa+CGffv1cu K6zNYAndGkkBA== Received: by pali.im (Postfix) id 8AB5511D3; Mon, 12 Sep 2022 11:38:59 +0200 (CEST) From: =?UTF-8?q?Pali=20Roh=C3=A1r?= To: u-boot@lists.denx.de Subject: [PATCH] cmd: ubi: Add 'ubi list' command for printing list of all UBI volumes Date: Mon, 12 Sep 2022 11:38:55 +0200 Message-Id: <20220912093855.25110-1-pali@kernel.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.103.6 at phobos.denx.de X-Virus-Status: Clean To allow easily iterate over all UBI volumes, add a new command which either print all user UBI volumes on output or set them into env variable. As UBI volumes can have arbitrary name/label, in most cases it is useful to iterate them by their numbers. This can be achieved by -numeric flag. This functionality is similar to already existing 'part list' command which prints partitions on formatted block device. Signed-off-by: Pali Rohár --- cmd/ubi.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/cmd/ubi.c b/cmd/ubi.c index fe8ac58bac08..2a32575a257c 100644 --- a/cmd/ubi.c +++ b/cmd/ubi.c @@ -27,6 +27,7 @@ #include #include #include +#include #undef ubi_msg #define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__) @@ -84,6 +85,70 @@ static int ubi_info(int layout) return 0; } +static int ubi_list(const char *var, int numeric) +{ + size_t namelen, len, size; + char *str, *str2; + int i; + + if (!var) { + for (i = 0; i < (ubi->vtbl_slots + 1); i++) { + if (!ubi->volumes[i]) + continue; + if (ubi->volumes[i]->vol_id >= UBI_INTERNAL_VOL_START) + continue; + printf("%d: %s\n", + ubi->volumes[i]->vol_id, + ubi->volumes[i]->name); + } + return 0; + } + + len = 0; + size = 16; + str = malloc(size); + if (!str) + return 1; + + for (i = 0; i < (ubi->vtbl_slots + 1); i++) { + if (!ubi->volumes[i]) + continue; + if (ubi->volumes[i]->vol_id >= UBI_INTERNAL_VOL_START) + continue; + + if (numeric) + namelen = 10; /* strlen(stringify(INT_MAX)) */ + else + namelen = strlen(ubi->volumes[i]->name); + + if (len + namelen + 1 > size) { + size = roundup_pow_of_two(len + namelen + 1) * 2; + str2 = realloc(str, size); + if (!str2) { + free(str); + return 1; + } + str = str2; + } + + if (len) + str[len++] = ' '; + + if (numeric) { + len += sprintf(str + len, "%d", ubi->volumes[i]->vol_id) + 1; + } else { + memcpy(str + len, ubi->volumes[i]->name, namelen); + len += namelen; + str[len] = 0; + } + } + + env_set(var, str); + free(str); + + return 0; +} + static int ubi_check_volumename(const struct ubi_volume *vol, char *name) { return strcmp(vol->name, name); @@ -581,6 +646,21 @@ static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) return ubi_info(layout); } + if (strcmp(argv[1], "list") == 0) { + int numeric = 0; + if (argc >= 2 && argv[2][0] == '-') { + if (strcmp(argv[2], "-numeric") == 0) + numeric = 1; + else + return CMD_RET_USAGE; + } + if (!numeric && argc != 2 && argc != 3) + return CMD_RET_USAGE; + if (numeric && argc != 3 && argc != 4) + return CMD_RET_USAGE; + return ubi_list(argv[numeric ? 3 : 2], numeric); + } + if (strcmp(argv[1], "check") == 0) { if (argc > 2) return ubi_check(argv[2]); @@ -720,6 +800,11 @@ U_BOOT_CMD( " header offset)\n" "ubi info [l[ayout]]" " - Display volume and ubi layout information\n" + "ubi list [flags]" + " - print the list of volumes\n" + "ubi list [flags] " + " - set environment variable to the list of volumes" + " (flags can be -numeric)\n" "ubi check volumename" " - check if volumename exists\n" "ubi create[vol] volume [size] [type] [id] [--skipcheck]\n" -- 2.20.1