From patchwork Tue Nov 19 00:26:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vladimir Olovyannikov X-Patchwork-Id: 1197082 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 (no SPF record) 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=fail (p=quarantine dis=none) header.from=broadcom.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=broadcom.com header.i=@broadcom.com header.b="KwyG1dy4"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 47H7PB0HdHz9sPW for ; Tue, 19 Nov 2019 12:23:09 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id B6E55C21F0B; Tue, 19 Nov 2019 01:23:08 +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 935ECC21EBD; Tue, 19 Nov 2019 01:03:46 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id BD25DC21E07; Tue, 19 Nov 2019 00:27:02 +0000 (UTC) Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) by lists.denx.de (Postfix) with ESMTPS id C0D2BC21DA2 for ; Tue, 19 Nov 2019 00:26:58 +0000 (UTC) Received: by mail-wm1-f47.google.com with SMTP id a17so1136499wmb.0 for ; Mon, 18 Nov 2019 16:26:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id; bh=4OjTSy9FYmMAT+qmBpoV/1vJaL81WjbeuNEGw0A4qlw=; b=KwyG1dy4u2E1+3LQ/+OMbGIQD94mR6upsc2nehDlMPKGCHzV/Kra/0U65OlgRPXEMU AccNhn4Lsa3bEk1nGxkgJGy552k97E0ZqIGjGkCecNG2bp0DHMkZh4sbEx6g+zTad0u5 ZtILr7/PGHrBiWu3g6x//jrFuznM5EjkY2Fhg= 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; bh=4OjTSy9FYmMAT+qmBpoV/1vJaL81WjbeuNEGw0A4qlw=; b=HGDqnNR0rbrl0xXLlHLnKDivivM6bP0Gq0b92/ExlHclrnfoo1ZkKAkWQKTbYjs7qm EW/O8x7CT9dQtA+BZUMNHB2gRbZaFrSvCVk9oHoYMSbSnP2oLj9KV55xdPSc0CpCipBT c7fOog+Pl1xG23sW4A5hozSjlaFcMUl3aU1O8qv8skhBaElWC9JhNHvKUwdMz+5pyKQK Hsh4bMT/6LSYLpI2PHV5puYdEqyaJQz9S/jbmPRZYO1tbIM0LwSK/gAMOJ2pyuhlmzES zGMvoYQfVWl9JXmTMAtjio2bGT7de5Hen4cGrygQi0JRTKzJ8FdTAJpx079aAZSZCm7e y9lg== X-Gm-Message-State: APjAAAXKKS2UrpKdeiH+AF0xPOqgssUvBIZZnLUdyKlknaEkWzHqp3BK ZNrBPhXvWQc3qR13CvS4hTaXMsn0560h6bEYN0PRP+hj8RC2uhvz+omyrADVlClaVZFvmFMJrFI l1DwEXjE1SG8uj+finvnxTW6KccR0EZquakh6fgKlRZ9mL3Vau0H9FU4xFezHDV3wocHkTe4q7P r5dCe0bqEk X-Google-Smtp-Source: APXvYqxz2wDSS9rmx9m5jbjitP8wUQ1pwgfMOtX3tAEjhx0wxwHU9IrJIck3p7Lma9+fWjVA1RAylQ== X-Received: by 2002:a1c:7d95:: with SMTP id y143mr2384047wmc.143.1574123217712; Mon, 18 Nov 2019 16:26:57 -0800 (PST) Received: from LBRMN-LNXUB114.ric.broadcom.com ([192.19.228.250]) by smtp.gmail.com with ESMTPSA id c144sm1174022wmd.1.2019.11.18.16.26.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2019 16:26:57 -0800 (PST) From: Vladimir Olovyannikov To: u-boot@lists.denx.de Date: Mon, 18 Nov 2019 16:26:30 -0800 Message-Id: <20191119002630.15088-1-vladimir.olovyannikov@broadcom.com> X-Mailer: git-send-email 2.17.1 X-Mailman-Approved-At: Tue, 19 Nov 2019 01:03:45 +0000 Cc: Suji Velupiallai , Heinrich Schuchardt Subject: [U-Boot] [PATCH v1 1/1] cmd: adding malloc, math, and strcmp commands to u-boot 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" cmd: adding malloc, math, and strcmp u-boot commands. - malloc supports allocation of heap memory and free allocated memory via u-boot command line. - math provides math commands such as "add", "sub", "mul", "div", "shift", ability to convert dec->hex. - strcmp provides string compare command feature for a script. All these commands are introduced to be used in u-boot scripts. Signed-off-by: Suji Velupiallai Signed-off-by: Vladimir Olovyannikov --- cmd/Kconfig | 21 ++++++++++++++ cmd/Makefile | 4 +++ cmd/malloc.c | 54 ++++++++++++++++++++++++++++++++++++ cmd/math.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/strcmp.c | 28 +++++++++++++++++++ 5 files changed, 185 insertions(+) create mode 100644 cmd/malloc.c create mode 100644 cmd/math.c create mode 100644 cmd/strcmp.c diff --git a/cmd/Kconfig b/cmd/Kconfig index cf982ff65e..f11903fe3d 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -1286,6 +1286,20 @@ config CMD_ITEST help Return true/false on integer compare. +config CMD_MALLOC + bool "malloc" + default y + help + Supports allocation of heap memory and free allocated memory commands. + These commands are used by u-boot scripts. + +config CMD_MATH + bool "math" + default y + help + Provides math commands such as add, sub, mul, div, shift, + convert decimal to hex functionalities to be available in the script. + config CMD_SOURCE bool "source" default y @@ -1301,6 +1315,13 @@ config CMD_SETEXPR Also supports loading the value at a memory location into a variable. If CONFIG_REGEX is enabled, setexpr also supports a gsub function. +config CMD_STRCMP + bool "strcmp" + default y + help + Provides string compare command feature to u-boot scripts. + + endmenu menu "Android support commands" diff --git a/cmd/Makefile b/cmd/Makefile index 2d723ea0f0..942d60a0a2 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -164,6 +164,10 @@ obj-$(CONFIG_CMD_GPT) += gpt.o obj-$(CONFIG_CMD_ETHSW) += ethsw.o obj-$(CONFIG_CMD_AXI) += axi.o +obj-$(CONFIG_CMD_MALLOC) += malloc.o +obj-$(CONFIG_CMD_MATH) += math.o +obj-$(CONFIG_CMD_STRCMP) += strcmp.o + # Power obj-$(CONFIG_CMD_PMIC) += pmic.o obj-$(CONFIG_CMD_REGULATOR) += regulator.o diff --git a/cmd/malloc.c b/cmd/malloc.c new file mode 100644 index 0000000000..e11e030a59 --- /dev/null +++ b/cmd/malloc.c @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2019 Broadcom + */ + +#include +#include +#include +#include +#include + +static unsigned long get_value(const char *val) +{ + char *env = env_get((char *)val); + + if (env) + return simple_strtoul(env, NULL, 16); + else + return simple_strtoul(val, NULL, 16); +} + +static int do_malloc(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) +{ + char numberbuf[32]; + void *mem; + + if (argc < 3) + return cmd_usage(cmdtp); + + mem = memalign(ARCH_DMA_MINALIGN, get_value(argv[2])); + if (mem) { + sprintf(numberbuf, "%08x", (unsigned int)mem); + env_set(argv[1], numberbuf); + return 0; + } + return -EINVAL; +} + +static int do_free(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) +{ + if (argc < 2) + return cmd_usage(cmdtp); + + free((void *)get_value(argv[1])); + env_set(argv[1], ""); + return 0; +} + +U_BOOT_CMD(malloc, 3, 0, do_malloc, + "Allocate memory from u-boot heap and store pointer in environment variable.", + "target size\n"); + +U_BOOT_CMD(free, 2, 0, do_free, + "Release memory from u-boot heap at target.", "target\n"); diff --git a/cmd/math.c b/cmd/math.c new file mode 100644 index 0000000000..17de5ef70b --- /dev/null +++ b/cmd/math.c @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2010-2017 Broadcom + */ + +#include +#include +#include + +unsigned long long simple_strtoull(const char *cp, char **endp, + unsigned int base); + +static unsigned long long get_value(const char *val) +{ + char *env = env_get((char *)val); + + if (env) + return simple_strtoull(env, NULL, 16); + else + return simple_strtoull(val, NULL, 16); +} + +static unsigned long long get_value_base10(const char *val) +{ + char *env = env_get((char *)val); + + if (env) + return simple_strtoull(env, NULL, 10); + else + return simple_strtoull(val, NULL, 10); +} + +/* + * Top level addenv command + */ +#define IS_MATH_CMD(cmd, args) ((strcmp(argv[1], cmd) == 0) && \ + (argc == (args))) + +static int do_math(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + char numberbuf[32]; + unsigned long long i = 0; + + if (IS_MATH_CMD("add", 5)) + i = get_value(argv[3]) + get_value(argv[4]); + else if (IS_MATH_CMD("sub", 5)) + i = get_value(argv[3]) - get_value(argv[4]); + else if (IS_MATH_CMD("mul", 5)) + i = get_value(argv[3]) * get_value(argv[4]); + else if (IS_MATH_CMD("div", 5)) + i = get_value(argv[3]) / get_value(argv[4]); + else if (IS_MATH_CMD("shl", 5)) + i = get_value(argv[3]) << get_value(argv[4]); + else if (IS_MATH_CMD("d2h", 4)) + i = get_value_base10(argv[3]); + else + return cmd_usage(cmdtp); + + sprintf(numberbuf, "%llx", i); + env_set(argv[2], numberbuf); + return 0; +} + +U_BOOT_CMD(math, 5, 0, do_math, + "Environment variable math.", + "add a b c\n" + " - add b to c and store in a.\n" + "math sub a b c\n" + " - subtract b from c and store in a.\n" + "math mul a b c\n" + " - multiply b by c and store in a.\n" + "math div a b c\n" + " - divide b by c and store in a.\n" + "math shl a b c\n" + " - shift left b by c and store in a.\n" + "math d2h a b\n" + " - Convert b from decimal to hex and store in a.\n" +); diff --git a/cmd/strcmp.c b/cmd/strcmp.c new file mode 100644 index 0000000000..3abd270d40 --- /dev/null +++ b/cmd/strcmp.c @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2010-2017 Broadcom + */ + +#include +#include + +static int do_strcmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + if (argc != 3) + return cmd_usage(cmdtp); + + if (strlen(argv[1]) != strlen(argv[2])) + return CMD_RET_FAILURE; + + /* Compare the temporary string to the given parameter */ + if (!strncmp(argv[1], argv[2], strlen(argv[2]))) + return CMD_RET_SUCCESS; + + return CMD_RET_FAILURE; +} + +U_BOOT_CMD(strcmp, 3, 0, do_strcmp, + "String to string comparison.", + "[str] [str]\n" + " - Returns true if str are same, else returns false.\n" +);