From patchwork Fri Jul 8 21:50:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Vasut X-Patchwork-Id: 1654398 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=denx.de header.i=@denx.de header.a=rsa-sha256 header.s=phobos-20191101 header.b=NPEk3WoM; dkim=pass (2048-bit key) header.d=denx.de header.i=@denx.de header.a=rsa-sha256 header.s=phobos-20191101 header.b=Ls03ZG1X; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Lfn623FvDz9s0w for ; Sat, 9 Jul 2022 07:51:04 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A1506844CF; Fri, 8 Jul 2022 23:50:54 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1657317054; bh=JZIqfnWG4jvOc0tFMx+7OLjXgGnvfLIIfEHr+QmoVSY=; h=From:To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From; b=NPEk3WoMks+zk1lXaqUnF7sGn/f/CHVu5+VdAkuNKNID53Z2LqolJOmvbvleKdto0 i4JUerMH+7fAysLjMCGh8B4rLSdCq/FPsvOoNx++uKReNwQG0iGvCnGPBjWdug6a3l KHFKCr9xLEYWggdnSHSfjhqwHa040gUIdWp71Qws1GiEsLrbQwwWZ9aemJuU93B0z1 o1NxRr2QJ3dfPojtO4M8L2CNAN2k01kC1uJGxIfh2gsXZ5fMmjctTVd+UPLzvOpJn7 z2iYbE1SwRU8eiYSaWPWtocKC30k6lq9eQreDvGlZFmu4k5UIMJ0k5cdVD3YWv45y5 zQUf05jFURsxg== Received: from tr.lan (ip-86-49-12-201.bb.vodafone.cz [86.49.12.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: marex@denx.de) by phobos.denx.de (Postfix) with ESMTPSA id 2F1008447D; Fri, 8 Jul 2022 23:50:52 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1657317052; bh=JZIqfnWG4jvOc0tFMx+7OLjXgGnvfLIIfEHr+QmoVSY=; h=From:To:Cc:Subject:Date:From; b=Ls03ZG1XmNX+RV3N7tc2p5xQ0pCUT/srpFiIn+1nSAVk/NK0fKsk4T2q5GKD8g4me 7qr3iKgFftfq03xH+3u/hbhrPYzCQGco775xdtsDPhlq20UxVi8u6TSH7stBM+Mlpa x2emXqFQ01s9xYo4oddHhPMspzOKvUbIgxWhi8GIzAKBpeJ5o4shYpgThDamlyx4zj LSJNBBvIcRDk/FaMBof+wqHjPkl+DT0HVOaEfXcKzwGVPeQPEjk9gHuQz1/nDNokw+ zdJEwfOJHWiwyoR+Kc3Sp9OIDzuNx39MCWIq9VluGHA6GP8ItgMMwBBQj6hw6JGpFG LSIeO/QzB9S8g== From: Marek Vasut To: u-boot@lists.denx.de Cc: Marek Vasut , Heinrich Schuchardt , Simon Glass , Tom Rini Subject: [PATCH] cmd: fdt: Add support for reading stringlist property values Date: Fri, 8 Jul 2022 23:50:43 +0200 Message-Id: <20220708215043.120573-1-marex@denx.de> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 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 The fdt command currently handles stringlists as strings in 'fdt get value' subcommand. Since strings in FDT stringlists are separated by '\0', only the first value gets inserted into the environment variable passed to the 'fdt get value' command. Example, consider the following DT snippet: / { compatible = "foo", "bar" }; The following command only reports the first string in stringlist: => fdt get value var / compatible ; print var foo It is not possible to assign list of null-terminated strings into U-Boot environment variable. Add optional 'index' parameter to the subcommand 'fdt get value []' which lets user specify which string within the stringlist should be assigned into the 'var' variable. The default value of 'index' is 0 in case it is not present. This way the 'fdt' command API does not change and existing scripts are not broken. The following command now reports the Nth string in stringlist, counting from zero: => fdt get value var / compatible 1 ; print var bar Signed-off-by: Marek Vasut Cc: Heinrich Schuchardt Cc: Simon Glass Cc: Tom Rini --- cmd/fdt.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/cmd/fdt.c b/cmd/fdt.c index 842e6cb634b..6fbd9205d38 100644 --- a/cmd/fdt.c +++ b/cmd/fdt.c @@ -48,11 +48,27 @@ void set_working_fdt_addr(ulong addr) /* * Get a value from the fdt and format it to be set in the environment */ -static int fdt_value_env_set(const void *nodep, int len, const char *var) +static int fdt_value_env_set(const void *nodep, int len, + const char *var, int index) { - if (is_printable_string(nodep, len)) - env_set(var, (void *)nodep); - else if (len == 4) { + if (is_printable_string(nodep, len)) { + const char *nodec = (const char *)nodep; + int i; + + /* + * Iterate over all members in stringlist and find the one at + * offset $index. If no such index exists, indicate failure. + */ + for (i = 0; i < len; i += strlen(nodec) + 1) { + if (index-- > 0) + continue; + + env_set(var, nodec + i); + return 0; + } + + return 1; + } else if (len == 4) { char buf[11]; sprintf(buf, "0x%08X", fdt32_to_cpu(*(fdt32_t *)nodep)); @@ -426,10 +442,14 @@ static int do_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) return 0; } else if (nodep && len > 0) { if (subcmd[0] == 'v') { + int index = 0; int ret; + if (argc == 7) + index = simple_strtoul(argv[6], NULL, 10); + ret = fdt_value_env_set(nodep, len, - var); + var, index); if (ret != 0) return ret; } else if (subcmd[0] == 'a') { @@ -1085,7 +1105,9 @@ static char fdt_help_text[] = "fdt resize [] - Resize fdt to size + padding to 4k addr + some optional if needed\n" "fdt print [] - Recursive print starting at \n" "fdt list [] - Print one level starting at \n" - "fdt get value - Get and store in \n" + "fdt get value [] - Get and store in \n" + " In case of stringlist property, use optional \n" + " to select string within the stringlist. Default is 0.\n" "fdt get name - Get name of node and store in \n" "fdt get addr - Get start address of and store in \n" "fdt get size [] - Get size of [] or num nodes and store in \n"