From patchwork Wed Feb 5 01:08:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 1233629 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=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=prevas.dk Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=prevas.dk header.i=@prevas.dk header.a=rsa-sha256 header.s=selector1 header.b=b8NYhHL9; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (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 48C3NP2KxKz9sSN for ; Wed, 5 Feb 2020 12:08:37 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id C8B6F81A39; Wed, 5 Feb 2020 02:08:31 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=prevas.dk 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=prevas.dk header.i=@prevas.dk header.b="b8NYhHL9"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3EC1281A5E; Wed, 5 Feb 2020 02:08:30 +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.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU, FORGED_SPF_HELO, SPF_HELO_PASS, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-he1eur04on0714.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe0d::714]) (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 00805819D0 for ; Wed, 5 Feb 2020 02:08:24 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=prevas.dk Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=rasmus.villemoes@prevas.dk ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Cjhw6rqtA4OAzHsPS1c+b+PeNCwaS34aJ6IorgZ+FsV4wyhSHT1k0PqGsYR52qzrgIvU4x6TrX008dUAeZAicl18jmMcYhK2M6pwErA9gm5bQgOnq/dsC9pJ4QNmcMTYgok0aBH1ropLEzVcO2foYMP+cTKFomkSfm0FDoGn4bz11dVvtLer1/+ZNJW/go/u746IUPhGnjjoNKOIEIkNh3xP0IDIx6rrzCCMHfFFgORR8okNcsZ2ezU4PqHRxV5DYDJE72paMl78m/j5eBQlgbxT0ihiZB4TaK20lv1/ms/hNLhEEZj9afS1b6w/52yz8ScCswnlLnkpsc/k1fv5AA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=aWafe0Twtxrlk97PCqUPhFXNQNLMcSKhE1gILkRyXZA=; b=W5SwxRCU7d4U0kQ457cOWYbYsoBRsVgZa11L5QKl9WgDoveVyLwvos2i/tLm7l6uN0gw+vNMGA1SkDc4+9b5SfB5Wizeb5L0EjUey0t/uNQtEQhxcw/mbLK2pW/w4clg4o8OdZjUflSTiY/prufzya2pEWamNGjcPMGhp52N7YxzZj4jd4KOeoAj9ki8vDLLIMmVRXvIT0y3Mad93GJuB0nPSrkgFCipiqy8vawRGBnyQu5bh8RZkG/lPu7o5p91ksirI60WaX9XuA7JCFk/RWr+p+p//OL9oX8lpqC9P7YzM3EmfUu8WvT6tTwWmvx+NXuoKzWY8FXXVhqrG8wyew== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=prevas.dk; dmarc=pass action=none header.from=prevas.dk; dkim=pass header.d=prevas.dk; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=prevas.dk; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=aWafe0Twtxrlk97PCqUPhFXNQNLMcSKhE1gILkRyXZA=; b=b8NYhHL9AmFMF3H1mU0wOklO3gcMllE7AJYFxol5ltZVQ9FKrnn8WuWRszkzwdOUjQ3A17jkOA+Hx9shOfyMqIE5WWbjbd8/uqXTATVQkvUlPYWFvnNq4qSfexKl9G2JSe4a7r9SFmt/dhBYhSmeUQpK5jarrETo8zddXmQh45s= Received: from VI1PR10MB2765.EURPRD10.PROD.OUTLOOK.COM (20.178.126.85) by VI1PR10MB2079.EURPRD10.PROD.OUTLOOK.COM (52.134.28.161) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2686.29; Wed, 5 Feb 2020 01:08:20 +0000 Received: from VI1PR10MB2765.EURPRD10.PROD.OUTLOOK.COM ([fe80::8175:5aa2:2870:766d]) by VI1PR10MB2765.EURPRD10.PROD.OUTLOOK.COM ([fe80::8175:5aa2:2870:766d%6]) with mapi id 15.20.2686.031; Wed, 5 Feb 2020 01:08:20 +0000 Received: from prevas-ravi.prevas.se (5.186.115.54) by AM6PR02CA0036.eurprd02.prod.outlook.com (2603:10a6:20b:6e::49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2707.21 via Frontend Transport; Wed, 5 Feb 2020 01:08:19 +0000 From: Rasmus Villemoes To: "u-boot@lists.denx.de" Subject: [PATCH] RFC: nvedit: support doing one (extra) expansion of the value in "env set" Thread-Topic: [PATCH] RFC: nvedit: support doing one (extra) expansion of the value in "env set" Thread-Index: AQHV28DBgTvfDcxIJk+UFc8o4ABC4Q== Date: Wed, 5 Feb 2020 01:08:20 +0000 Message-ID: <20200205010812.20373-1-rasmus.villemoes@prevas.dk> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: AM6PR02CA0036.eurprd02.prod.outlook.com (2603:10a6:20b:6e::49) To VI1PR10MB2765.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:803:e1::21) x-mailer: git-send-email 2.23.0 authentication-results: spf=none (sender IP is ) smtp.mailfrom=rasmus.villemoes@prevas.dk; x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [5.186.115.54] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 82a7f33f-1ace-488a-a7e2-08d7a9d7e3df x-ms-traffictypediagnostic: VI1PR10MB2079: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:4941; x-forefront-prvs: 0304E36CA3 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(376002)(136003)(366004)(39850400004)(396003)(346002)(189003)(199004)(81166006)(8676002)(81156014)(5660300002)(6506007)(64756008)(6486002)(66556008)(66446008)(6666004)(8976002)(36756003)(71200400001)(16526019)(186003)(54906003)(66476007)(86362001)(8936002)(1076003)(6512007)(2906002)(44832011)(478600001)(107886003)(52116002)(6916009)(316002)(4326008)(66946007)(956004)(2616005)(26005); DIR:OUT; SFP:1102; SCL:1; SRVR:VI1PR10MB2079; H:VI1PR10MB2765.EURPRD10.PROD.OUTLOOK.COM; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; received-spf: None (protection.outlook.com: prevas.dk does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: BGRLmQ+QdIlGsvPm8Dq57WqRekaCys8vj203r0CsjzdsenIwMWyqD7Pwk/2Z2kf4NvZOLPhQsutOTWxR8e0S1VTk6w7zw4WbAVmNO0V56L24XhQ5frpo1VNqOZKhOL+SMHBuyFvDzyhtrl6poUNNL4FAbtPlPIr++NSdx1PSwkrrRkq29Ts2Lb4eYAC4sFcl4e8QjtehyPiPedgKumk3ieVdyuRUKof8Nn9D9uUhfMu7WA7jIUKYqKR/Ir47qKxACqdfatI033T7842Kt8DWM2m5IzNS5whwqhF4p1ZvcvjQ6mC9B+FjHT27efyynYLIwN6JNlnYLWp6LYg1DOPsMqWEj8YAWbeGz1LuwDvBNBGj6FaA84aWlQh2Ps8Tq/cLHf/hxJEsU02UQ1RN3F80t8gQgwbSUVaQhng1xbKE+URWBXjyjEQwdz4aCM5VFIJv x-ms-exchange-antispam-messagedata: Kn5ilTE5ASRBJyUsWWm02h7tvCMu8/VggFgHdPa7M01loxbq0NRc2JKcO5V9yi/grz6PgEIjHDKSYAKt15uK7P+7Pz+agYqE08emWQhkYOqLemAf+AT5xG+BYOD98o5Hb9KqFh0J6FNJcc1EYnPp4Q== MIME-Version: 1.0 X-OriginatorOrg: prevas.dk X-MS-Exchange-CrossTenant-Network-Message-Id: 82a7f33f-1ace-488a-a7e2-08d7a9d7e3df X-MS-Exchange-CrossTenant-originalarrivaltime: 05 Feb 2020 01:08:20.4151 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: d350cf71-778d-4780-88f5-071a4cb1ed61 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: F8BjkKgOhp5TbDXE/JBO+oN6irgkMI8bVmvUPTaM4A82OgxbGa1lTsVKDT036eoMVzizBe7XLeoYerJGvZVDmBgHOms86itlHN+pfYW4KJg= X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR10MB2079 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.26 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Joe Hershberger Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.101.4 at phobos.denx.de X-Virus-Status: Clean Currently, there's no way to fetch the value of an environment variable whose name is stored in some other variable, or generated from such - in non-working pseudo-code, ${${varname}} ${array${index}} This forces some scripts to needlessly duplicate logic and hardcode assumptions. For example, in an A/B scheme with three variables BOOT_ORDER # Either "A B" or "B A" depending on which slot was last updated BOOT_A_LEFT # 0..3 BOOT_B_LEFT # 0..3 when one needs to determine the slot to boot from, one does something like setenv found for slot in $BOOT_ORDER ; do if test "x$found" != "x" ; then # work around lack of break elif test "x$slot" = "xA" ; then if test $BOOT_A_LEFT -gt 0 ; then setexpr BOOT_A_LEFT $BOOT_A_LEFT - 1 setenv found A setenv bootargs ${bootargs_A} setenv ubivol ${ubivol_A} # more setup based on A fi elif test "x$slot" = "xB" ; then if test $BOOT_B_LEFT -gt 0 ; then # the same ... fi fi done This is already bad enough, but extending that to A/B/C is tedious and prone to copy-pastos. So this is an attempt at allowing one to do "env set -E var value1 value2" with the effect that, of course, normal variable expansion happens on the command line, the valueX are joined with spaces as usual, and then one more pass is done over that string replacing occurrences of ${foo}. The above would become setenv found for slot in $BOOT_ORDER ; do if test "x$found" != "x" ; then # work around lack of break else env set -E boot_left "\${BOOT_${slot}_LEFT}" if test $boot_left -gt 0 ; then setexpr BOOT_${slot}_LEFT $boot_left - 1 env set found $slot env set -E bootargs "\${bootargs_${slot}}" env set -E ubivol "\${ubivol_${slot}}" fi fi done I'm pleasantly surprised it was that easy to implement, but of course I'm cheating a bit (cli_simple_process_macros is only available if CONFIG_CMDLINE, though I think cli_simple.o could be unconditionally built and then link-time GC should get rid of the excess functions). This has been lightly tested in the sandbox. I'll add some proper unit tests, update the help texts and try to handle the Kconfig issue if this is something that might be accepted. Signed-off-by: Rasmus Villemoes --- cmd/nvedit.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/cmd/nvedit.c b/cmd/nvedit.c index 81d94cd193..ff6ffcb674 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -224,7 +224,7 @@ DONE: */ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag) { - int i, len; + int i, len, expand = 0; char *name, *value, *s; struct env_entry e, *ep; @@ -244,6 +244,9 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag) case 'f': /* force */ env_flag |= H_FORCE; break; + case 'E': + expand = 1; + break; default: return CMD_RET_USAGE; } @@ -287,6 +290,18 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag) if (s != value) *--s = '\0'; + if (expand) { + char *expanded = malloc(CONFIG_SYS_CBSIZE); + + if (expanded == NULL) { + printf("## Can't malloc %d bytes\n", CONFIG_SYS_CBSIZE); + free(value); + return 1; + } + cli_simple_process_macros(value, expanded); + free(value); + value = expanded; + } e.key = name; e.data = value; hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);