From patchwork Fri Jun 29 07:41:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Schulz X-Patchwork-Id: 936660 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=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 41H7vt4V44z9s0n for ; Fri, 29 Jun 2018 17:44:42 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 528EEC21DFD; Fri, 29 Jun 2018 07:43:38 +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=KHOP_BIG_TO_CC 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 335E1C21DCA; Fri, 29 Jun 2018 07:42:58 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 5C719C21C4A; Fri, 29 Jun 2018 07:42:55 +0000 (UTC) Received: from mail.bootlin.com (mail.bootlin.com [62.4.15.54]) by lists.denx.de (Postfix) with ESMTP id 1E8A5C21CB1 for ; Fri, 29 Jun 2018 07:42:55 +0000 (UTC) Received: by mail.bootlin.com (Postfix, from userid 110) id 04D4920DD8; Fri, 29 Jun 2018 09:42:54 +0200 (CEST) Received: from localhost.localdomain (AAubervilliers-681-1-87-188.w90-88.abo.wanadoo.fr [90.88.29.188]) by mail.bootlin.com (Postfix) with ESMTPSA id 7D43D20DB5; Fri, 29 Jun 2018 09:42:43 +0200 (CEST) From: Quentin Schulz To: agraf@suse.de, maxime.ripard@bootlin.com, sjg@chromium.org, LW@KARO-electronics.de, wd@denx.de, tuomas.tynkkynen@iki.fi, yamada.masahiro@socionext.com, michal.simek@xilinx.com, swarren@nvidia.com Date: Fri, 29 Jun 2018 09:41:41 +0200 Message-Id: <322e6866c43f4515240ddca9456ee390b6f334c7.1530257385.git-series.quentin.schulz@bootlin.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: References: In-Reply-To: References: Cc: u-boot@lists.denx.de, miquel.raynal@bootlin.com Subject: [U-Boot] [PATCH v5 4/5] cmd: nvedit: env import can now import only variables passed as parameters 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" While the `env export` can take as parameters variables to be exported, `env import` does not have such a mechanism of variable selection. Let's add the ability to add parameters at the end of the command for variables to be imported. Every env variable from the env to be imported passed by parameter to this command will override the value of the variable in the current env. If a variable exists in the current env but not in the imported env, if this variable is passed as a parameter to env import, the variable will be unset. If a variable exists in the imported env, the variable in the current env will be set to the value of the one from the imported env. All the remaining variables are left untouched. As the size parameter of env import is positional but optional, let's add the possibility to use the sentinel '-' for when we don't want to give the size parameter (when the env is '\0' terminated) but we pass a list of variables at the end of the command. env import addr env import addr - env import addr size env import addr - foo1 foo2 env import addr size foo1 foo2 are all valid. env import -c addr env import -c addr - env import -c addr - foo1 foo2 are all invalid because they don't pass the size parameter required for checking, while the following are valid. env import addr size env import addr size foo1 foo2 Nothing's changed for the other parameters or the overall behaviour. One of its use case could be to load a secure environment from the signed U-Boot binary and load only a handful of variables from an other, unsecure, environment without completely losing control of U-Boot. Signed-off-by: Quentin Schulz Tested-by: Alex Kiernan --- v4: - add tested-by by Alex, v3: - migrate to env import addr size var... instead of env import -w addr size so that the list of variables to load is more explicit and the behaviour of env import is closer to the one of env export, v2: - use strdup instead of malloc + strcpy, - NULL-check the result of strdup, - add common exit path for freeing memory in one unique place, - store token pointer from strtok within the char** array instead of strdup-ing token within elements of array, cmd/nvedit.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/cmd/nvedit.c b/cmd/nvedit.c index 70d7068..5000517 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -1004,7 +1004,7 @@ sep_err: #ifdef CONFIG_CMD_IMPORTENV /* - * env import [-d] [-t [-r] | -b | -c] addr [size] + * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] * -d: delete existing environment before importing; * otherwise overwrite / append to existing definitions * -t: assume text format; either "size" must be given or the @@ -1018,6 +1018,11 @@ sep_err: * addr: memory address to read from * size: length of input data; if missing, proper '\0' * termination is mandatory + * if var is set and size should be missing (i.e. '\0' + * termination), set size to '-' + * var... List of the names of the only variables that get imported from + * the environment at address 'addr'. Without arguments, the whole + * environment gets imported. */ static int do_env_import(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) @@ -1029,6 +1034,7 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag, int fmt = 0; int del = 0; int crlf_is_lf = 0; + int wl = 0; size_t size; cmd = *argv; @@ -1077,9 +1083,9 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag, addr = simple_strtoul(argv[0], NULL, 16); ptr = map_sysmem(addr, 0); - if (argc == 2) { + if (argc >= 2 && strcmp(argv[1], "-")) { size = simple_strtoul(argv[1], NULL, 16); - } else if (argc == 1 && chk) { + } else if (chk) { puts("## Error: external checksum format must pass size\n"); return CMD_RET_FAILURE; } else { @@ -1101,6 +1107,9 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag, printf("## Info: input data size = %zu = 0x%zX\n", size, size); } + if (argc > 2) + wl = 1; + if (chk) { uint32_t crc; env_t *ep = (env_t *)ptr; @@ -1115,8 +1124,8 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag, ptr = (char *)ep->data; } - if (himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR, - crlf_is_lf, 0, NULL) == 0) { + if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR, + crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) { pr_err("## Error: Environment import failed: errno = %d\n", errno); return 1; @@ -1246,7 +1255,7 @@ static char env_help_text[] = #endif #endif #if defined(CONFIG_CMD_IMPORTENV) - "env import [-d] [-t [-r] | -b | -c] addr [size] - import environment\n" + "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n" #endif "env print [-a | name ...] - print environment\n" #if defined(CONFIG_CMD_RUN)