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 X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BDCA0C433F5 for ; Sun, 19 Sep 2021 19:01:12 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 2DEEC60E90 for ; Sun, 19 Sep 2021 19:01:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 2DEEC60E90 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0804982EEA; Sun, 19 Sep 2021 21:01:00 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org 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=chromium.org header.i=@chromium.org header.b="aEbVvO7n"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 05F2F81BF4; Sun, 19 Sep 2021 21:00:25 +0200 (CEST) Received: from mail-ot1-x333.google.com (mail-ot1-x333.google.com [IPv6:2607:f8b0:4864:20::333]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id A0E138322E for ; Sun, 19 Sep 2021 21:00:01 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-ot1-x333.google.com with SMTP id 77-20020a9d0ed3000000b00546e10e6699so9477224otj.2 for ; Sun, 19 Sep 2021 12:00:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xLd0w7NNdepZSsJp8ylZAEKO9BuSGbIOYaNjtL/EK/A=; b=aEbVvO7njklS5QLfVHo1tjlq1F26cSE1Yh9GUQCLS3CO1o4N3UrGJ+fV+/pD4vCxLS BvkOxR0k7/Zo0sPeZ0dARoGStx9vzZfLkN4vIXy311lq8Ze0HoKDt8L7DdNtr52UkJI8 pKuM8XYapW3LkvLtyL7gEv946NrDYo2/RlU0g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xLd0w7NNdepZSsJp8ylZAEKO9BuSGbIOYaNjtL/EK/A=; b=DMyCSLf/tub5mvPeWlC/ktL37cMDbAdHz0pa/YdFblGjnaB1ASoKZ8LTHrxtSZAld4 //jQVB2IFGZtuo0dzlJy8dnvKnTIX+nulKaWluQ3o55mZaHFyc+auVfVclG1GL6vjr6q vojU5gF9Sefsbg20y/Yt9yjtHW1Y0DrfbsQJ+ctyujXmSTcB2H69Hyww81TLSwfcXyzR BibY+YXVKXCg0+hZUS1eNpxmfqPbMwF5ltXckNcv3GRrfTnMD6A2IpdLhZkluNmV1JFN /hDglUNcb1T3tVIeGN1H72/tBUpMVy3wcgkv7BhTA523El3UjiKXdpgeYpes/Yzz8xr9 ARxQ== X-Gm-Message-State: AOAM531uF+eF3DiOK1A3bUtcNpDTPuUr/9qZm3gnoeuhJbokoCmMtXD3 EUKVv9ISmxsBm6IqEzlN6fFwHr2UvYDT/Q== X-Google-Smtp-Source: ABdhPJwAg7XBLX5TDTkETsmn+DGUnIL9I9jhUs1KAWcMylg2VDBDt4BBNcMNjgrDfVl171J2MhSYRg== X-Received: by 2002:a9d:719b:: with SMTP id o27mr18402792otj.257.1632077999996; Sun, 19 Sep 2021 11:59:59 -0700 (PDT) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id bj27sm2634540oib.58.2021.09.19.11.59.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Sep 2021 11:59:59 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Simon Glass , Bin Meng , Joe Hershberger , =?UTF-8?q?Marek=20Beh=C3=BAn?= , =?UTF-8?q?Pali=20Roh=C3=A1r?= , Wolfgang Denk Subject: [PATCH v4 3/5] env: Allow U-Boot scripts to be placed in a .env file Date: Sun, 19 Sep 2021 12:59:48 -0600 Message-Id: <20210919125937.v4.3.If789ba3e2667c46c03eda3386ca84a863baeda55@changeid> X-Mailer: git-send-email 2.33.0.464.g1972c5931b-goog In-Reply-To: <20210919185950.3813952-1-sjg@chromium.org> References: <20210919185950.3813952-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.2 at phobos.denx.de X-Virus-Status: Clean At present U-Boot environment variables, and thus scripts, are defined by CONFIG_EXTRA_ENV_SETTINGS. It is painful to add large amounts of text to this file and dealing with quoting and newlines is harder than it should be. It would be better if we could just type the script into a text file and have it included by U-Boot. Add a feature that brings in a .env file associated with the board config, if present. To use it, create a file in a board//env directory called .env (or common.env if you want the same environment for all boards). The environment variables should be of the form "var=value". Values can extend to multiple lines. See the README under 'Environment Variables:' for more information and an example. Comments are not permitted in the environment with this commit. Signed-off-by: Simon Glass --- Changes in v4: - Move this from being part of configuring U-Boot to part of building it - Don't put the environment in autoconf.mk as it is not needed - Add documentation in rST format instead of README - Drop mention of import/export - Update awk script to ignore blank lines, as generated by clang Changes in v3: - Adjust Makefile to generate the .inc and .h files in separate fules - Add more detail in the README about the format of .env files - Improve the comment about " in the awk script - Correctly terminate environment files with \n Changes in v2: - Move .env file from include/configs to board/ - Use awk script to process environment since it is much easier on the brain - Add information and updated example script to README - Add dependency rule so that the environment is rebuilt when it changes Makefile | 34 ++++++++++++++++++++++++++- config.mk | 2 ++ doc/usage/environment.rst | 41 ++++++++++++++++++++++++++++++++ env/embedded.c | 1 + include/env_default.h | 8 +++++++ scripts/env2string.awk | 49 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 scripts/env2string.awk diff --git a/Makefile b/Makefile index 3014788e14e..9e44af2ccb1 100644 --- a/Makefile +++ b/Makefile @@ -513,6 +513,7 @@ version_h := include/generated/version_autogenerated.h timestamp_h := include/generated/timestamp_autogenerated.h defaultenv_h := include/generated/defaultenv_autogenerated.h dt_h := include/generated/dt.h +env_h := include/generated/environment.h no-dot-config-targets := clean clobber mrproper distclean \ help %docs check% coccicheck \ @@ -1802,6 +1803,37 @@ quiet_cmd_sym ?= SYM $@ u-boot.sym: u-boot FORCE $(call if_changed,sym) +# We expect '.env' but failing that will use 'common.env' +ENV_DIR := $(if $(VENDOR),$(VENDOR)/env,$(BOARD)/env) +ENV_FILE_BOARD := $(srctree)/board/${ENV_DIR}/$(BOARD).env +ENV_FILE_COMMON := $(srctree)/board/${ENV_DIR}/common.env +ENV_FILE := $(if $(wildcard $(ENV_FILE_BOARD)),$(ENV_FILE_BOARD),$(ENV_FILE_COMMON)) + +# Run the environment text file through the preprocessor +quiet_cmd_gen_envp = ENVP $@ + cmd_gen_envp = \ + if [ -f "$(ENV_FILE)" ]; then \ + cat $(ENV_FILE) >$@; \ + else \ + echo -n >$@ ; \ + fi + +# Regenerate the environment if it changes +# We use 'wildcard' since the file is not required to exist (at present), in +# which case we don't want this dependency, but instead should create an empty +# file +include/generated/environment.in: \ + $(if $(wildcard $(ENV_FILE)),$(wildcard $(ENV_FILE)),FORCE) + $(call cmd,gen_envp) + +quiet_cmd_gen_envt = ENVT $@ + cmd_gen_envt = \ + echo -n "\#define CONFIG_EXTRA_ENV_TEXT " >$@; \ + awk -f $(srctree)/scripts/env2string.awk $< >>$@ + +$(env_h): include/generated/environment.in + $(call cmd,gen_envt) + # The actual objects are generated when descending, # make sure no implicit rule kicks in $(sort $(u-boot-init) $(u-boot-main)): $(u-boot-dirs) ; @@ -1857,7 +1889,7 @@ endif # prepare2 creates a makefile if using a separate output directory prepare2: prepare3 outputmakefile cfg -prepare1: prepare2 $(version_h) $(timestamp_h) $(dt_h) \ +prepare1: prepare2 $(version_h) $(timestamp_h) $(dt_h) $(env_h) \ include/config/auto.conf ifeq ($(wildcard $(LDSCRIPT)),) @echo >&2 " Could not find linker script." diff --git a/config.mk b/config.mk index 7bb1fd4ed1b..2595aed218b 100644 --- a/config.mk +++ b/config.mk @@ -50,8 +50,10 @@ endif ifneq ($(BOARD),) ifdef VENDOR BOARDDIR = $(VENDOR)/$(BOARD) +ENVDIR=${vendor}/env else BOARDDIR = $(BOARD) +ENVDIR=${board}/env endif endif ifdef BOARD diff --git a/doc/usage/environment.rst b/doc/usage/environment.rst index be785a8f717..81130d17f85 100644 --- a/doc/usage/environment.rst +++ b/doc/usage/environment.rst @@ -14,6 +14,47 @@ environment. As long as you don't save the environment you are working with an in-memory copy. In case the Flash area containing the environment is erased by accident, a default environment is provided. +The default environment is created in `include/env_default.h`, and can be +augmented by various `CONFIG` defines. See that file for details. In +particular you can define `CONFIG_EXTRA_ENV_SETTINGS` in your board file +to add environment variables (see `CONFIG_EXTRA_ENV_SETTINGS` for details). + +It is also possible to create an environment file with the name +`board//env/.env` for your board. If that file is not present +then U-Boot will look for `oard//env/common.env` so that you can +have a common environment for all vendor boards. + +This is a plain text file where you can type your environment variables in +the form `var=value`. Blank lines and multi-line variables are supported. +The conversion script looks for a line that starts with a letter or number +and has an equals sign immediately afterwards. Spaces before the = are not +permitted. It is a good idea to indent your scripts so that only the 'var=' +appears at the start of a line. + +For example, for snapper9260 you would create a text file called +`board/bluewater/env/snapper9260.env` containing the environment text. + +Example:: + + stdout=serial + #ifdef CONFIG_LCD + stdout+=,lcd + #endif + bootcmd= + /* U-Boot script for booting */ + + if [ -z ${tftpserverip} ]; then + echo "Use 'setenv tftpserverip a.b.c.d' to set IP address." + fi + + usb start; setenv autoload n; bootp; + tftpboot ${tftpserverip}: + bootm + failed= + /* Print a message when boot fails */ + echo CONFIG_SYS_BOARD boot failed - please check your image + echo Load address is CONFIG_SYS_LOAD_ADDR + Some configuration options can be set using Environment Variables. List of environment variables (most likely not complete): diff --git a/env/embedded.c b/env/embedded.c index 208553e6af1..9f26e6cad9c 100644 --- a/env/embedded.c +++ b/env/embedded.c @@ -66,6 +66,7 @@ #endif #define DEFAULT_ENV_INSTANCE_EMBEDDED +#include #include #ifdef CONFIG_ENV_ADDR_REDUND diff --git a/include/env_default.h b/include/env_default.h index 1ddd64ba8ff..b5484d08e9e 100644 --- a/include/env_default.h +++ b/include/env_default.h @@ -10,6 +10,10 @@ #include #include +#ifndef USE_HOSTCC +#include +#endif + #ifdef DEFAULT_ENV_INSTANCE_EMBEDDED env_t embedded_environment __UBOOT_ENV_SECTION__(environment) = { ENV_CRC, /* CRC Sum */ @@ -112,6 +116,10 @@ const uchar default_environment[] = { #endif #ifdef CONFIG_EXTRA_ENV_SETTINGS CONFIG_EXTRA_ENV_SETTINGS +#endif +#ifdef CONFIG_EXTRA_ENV_TEXT + /* This is created in the Makefile */ + CONFIG_EXTRA_ENV_TEXT #endif "\0" #else /* CONFIG_USE_DEFAULT_ENV_FILE */ diff --git a/scripts/env2string.awk b/scripts/env2string.awk new file mode 100644 index 00000000000..f442d3b7c44 --- /dev/null +++ b/scripts/env2string.awk @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Copyright 2021 Google, Inc +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Awk script to parse a text file containing an environment and convert it +# to a C string which can be compiled into U-Boot. + +# We output a double quote before starting, and again when we finish so that +# all output is quoted. +BEGIN { + # env holds the env variable we are currently processing + env = ""; + ORS = "" + print "\"" +} + +# Skip empty lines, as these are generated by the clang preprocessor +NF { + # Quote quotes + gsub("\"", "\\\"") + + # Is this the start of a new environment variable? + if (match($0, "^([^ =][^ =]*)=(.*)", arr)) { + if (length(env) != 0) { + # Record the value of the variable now completed + vars[var] = env + } + var = arr[1] + env = arr[2] + } else { + # Change newline to \n + env = env "\\n" $0; + } +} + +END { + # Record the value of the variable now completed + if (length(env) != 0) { + vars[var] = env + } + + # Print out all the variables + for (var in vars) { + print var "=" vars[var] "\\0"; + } + print "\"\n" +} -- 2.33.0.464.g1972c5931b-goog