Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 7DAEEC001E0 for ; Tue, 1 Aug 2023 14:46:57 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5827586B1B; Tue, 1 Aug 2023 16:46:55 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=fris.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=fris.de header.i=@fris.de header.b="Pzu6oM2K"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CBFC286B04; Tue, 1 Aug 2023 16:46:53 +0200 (CEST) Received: from mail.fris.de (mail.fris.de [116.203.77.234]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 4F96986C81 for ; Tue, 1 Aug 2023 16:46:50 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=fris.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=frieder@fris.de Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 716E5C0201; Tue, 1 Aug 2023 16:46:44 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fris.de; s=dkim; t=1690901208; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding; bh=QQF1KPbdK+6XGig7iXvD8nZStg+y7s9UiAAmp2UqSCc=; b=Pzu6oM2KA2AItMb19yfcxtjvYi65BBiy4BrSRtemAns2Tqhfcxe6ddTLZYhYPPKC/tcI1O ZMR86XxTb7wJwCAbjrAT+H1r1p1OqG8WI/2HPVMw1IBsgJ7ETXzOatT/BlPTdbhPZ39LzR VEiFuucIEp/8bYJSKDXnLY+eREk8dHNkwpKTuPAdb5NmkydOzCA/WNsr6VMWz5LeP5Gquc a/EfLQEcAzzOp27HoiD+46gfHVycm9MiiwswBva1j9qObZUULP1R4lzv25glU/9pAYVaeM tEeSfVnv2MKVHH7GhQ3VQCGnMO34DKmeTGLWidcEnGIFoVE042k0WfqYuwn1DA== From: Frieder Schrempf To: Joe Hershberger , u-boot@lists.denx.de Cc: Frieder Schrempf , Stefano Babic , Michael Walle , Heinrich Schuchardt , Jan Kiszka , Marek Vasut , Mattijs Korpershoek , Patrice Chotard , Patrick Delaunay , Safae Ouajih , Simon Glass , Stefan Herbrechtsmeier , Wolfgang Denk Subject: [RFC PATCH] env: Export environment config to OS devicetree Date: Tue, 1 Aug 2023 16:46:05 +0200 Message-ID: <20230801144606.689900-1-frieder@fris.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Last-TLS-Session-Version: TLSv1.3 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.8 at phobos.denx.de X-Virus-Status: Clean From: Frieder Schrempf There are plenty of cases where the OS wants to know where the persistent environment is stored in order to print or manipulate it. In most cases a userspace tool like libubootenv [1] is used to handle the environment. It uses hardcoded settings in a config file in the rootfs to determine the exact location of the environment. In order to make systems more flexible and let userspace tools detect the location of the environment currently in use, let's add an option to export the runtime configuration to the FDT passed to the OS. This is especially useful when your device is able to use multiple locations for the environment and the rootfs containing the fw_env.config file should be kept universal. Currently the general information like location/type, size, offset and redundancy are exported. Userspace tools might already be able to guess the correct device to access based on this information. For further device-specific information two additional properties 'id1' and 'id2' are used. The current implementation uses them for MMC and SPI FLASH like this: | Type | id1 | id2 | | ---------- | -------------------------- | -------------------------- | | MMC | MMC device index in U-Boot | MMC hwpart index in U-Boot | | SPI FLASH | SPI bus index in U-Boot | SPI CS index in U-Boot | Further extensions for other environment devices are possible. We add the necessary information to the '/chosen' node. The following shows two examples: Redundant environment in MMC: /chosen { u-boot,env-config { location = <5>; # according to 'enum env_location' offset = <0x1E0000>; size = <0x100000>; sect_size = <0x100000>; id1 = <1>; # MMC device index id2 = <0>; # MMC HW partition index }; u-boot,env-redund-config { offset = <0x1F0000>; }; }; Redundant environment in SPI NOR: /chosen { u-boot,env-config { location = <10>; # according to 'enum env_location' offset = <0x1E0000>; size = <0x100000>; sect_size = <0x100000>; id1 = <0>; # SPI bus id2 = <0>; # SPI CS }; u-boot,env-redund-config { offset = <0x1F0000>; }; }; See [2] for an example parser implementation for libubootenv. [1] https://github.com/sbabic/libubootenv [2] https://github.com/fschrempf/libubootenv/commit/726a7ac9b1b1020354ee8fe750f4cad3df01f5e7 Cc: Stefano Babic Cc: Michael Walle Signed-off-by: Frieder Schrempf --- boot/Kconfig | 9 ++++++++ boot/image-fdt.c | 8 ++++++++ env/env.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ include/env.h | 4 ++++ 4 files changed, 74 insertions(+) diff --git a/boot/Kconfig b/boot/Kconfig index e8fb03b801..16a94f9b35 100644 --- a/boot/Kconfig +++ b/boot/Kconfig @@ -702,6 +702,15 @@ config OF_BOARD_SETUP board-specific information in the device tree for use by the OS. The device tree is then passed to the OS. +config OF_EXPORT_ENV_CONFIG + bool "Export environment config to device tree before boot" + depends on OF_LIBFDT + help + This causes U-Boot to call fdt_environment_config() before booting into + the operating system. This function exports the currently in use + environemnt configuration to the "chosen" node of the fdt. This allows + the OS to determine where the environment is located. + config OF_SYSTEM_SETUP bool "Set up system-specific details in device tree before boot" depends on OF_LIBFDT diff --git a/boot/image-fdt.c b/boot/image-fdt.c index f10200f647..c02c8f33ef 100644 --- a/boot/image-fdt.c +++ b/boot/image-fdt.c @@ -643,6 +643,14 @@ int image_setup_libfdt(struct bootm_headers *images, void *blob, /* Append PStore configuration */ fdt_fixup_pstore(blob); #endif + if (IS_ENABLED(CONFIG_OF_EXPORT_ENV_CONFIG)) { + fdt_ret = fdt_environment_config(blob); + if (fdt_ret) { + printf("ERROR: environment config fdt fixup failed: %s\n", + fdt_strerror(fdt_ret)); + goto err; + } + } if (IS_ENABLED(CONFIG_OF_BOARD_SETUP)) { const char *skip_board_fixup; diff --git a/env/env.c b/env/env.c index 2aa52c98f8..a640977205 100644 --- a/env/env.c +++ b/env/env.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -156,6 +157,58 @@ __weak enum env_location env_get_location(enum env_operation op, int prio) return arch_env_get_location(op, prio); } +#ifdef CONFIG_OF_EXPORT_ENV_CONFIG +int fdt_environment_config(void *blob) +{ + struct mmc *mmc; + u32 location; + int chosen_offs, offs; + int ret; + + if (CONFIG_IS_ENABLED(ENV_IS_NOWHERE)) + return 0; + + chosen_offs = fdt_path_offset(blob, "/chosen"); + if (chosen_offs < 0) { + printf("Could not find chosen node.\n"); + return chosen_offs; + } + + offs = fdt_add_subnode(blob, chosen_offs, "u-boot,env-config"); + if (offs < 0) { + printf("Could not create env-config node.\n"); + return offs; + } + + location = env_get_location(0, 0); + fdt_setprop_u32(blob, offs, "location", location); + fdt_setprop_u32(blob, offs, "offset", CONFIG_ENV_OFFSET); + fdt_setprop_u32(blob, offs, "size", CONFIG_ENV_SIZE); + fdt_setprop_u32(blob, offs, "sect_size", CONFIG_ENV_SECT_SIZE); + + if (CONFIG_IS_ENABLED(ENV_IS_IN_MMC) && location == ENVL_MMC) { + fdt_setprop_u32(blob, offs, "id1", mmc_get_env_dev()); + mmc = find_mmc_device(mmc_get_env_dev()); + if (mmc) + fdt_setprop_u32(blob, offs, "id2", mmc_get_env_part(mmc)); + } else if (CONFIG_IS_ENABLED(ENV_IS_IN_SPI_FLASH) && location == ENVL_SPI_FLASH) { + fdt_setprop_u32(blob, offs, "id1", CONFIG_ENV_SPI_BUS); + fdt_setprop_u32(blob, offs, "id2", CONFIG_ENV_SPI_CS); + } + + if (CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)) { + offs = fdt_add_subnode(blob, chosen_offs, "u-boot,env-redund-config"); + if (offs < 0) { + printf("Could not create env-redund-config node.\n"); + return offs; + } + fdt_setprop_u32(blob, offs, "offset", CONFIG_ENV_OFFSET_REDUND); + } + + return 0; +} +#endif + /** * env_driver_lookup() - Finds the most suited environment location * @op: operations performed on the environment diff --git a/include/env.h b/include/env.h index 1480efa59e..f9b45a4925 100644 --- a/include/env.h +++ b/include/env.h @@ -377,4 +377,8 @@ void env_import_fdt(void); static inline void env_import_fdt(void) {} #endif +#ifdef CONFIG_OF_EXPORT_ENV_CONFIG +int fdt_environment_config(void *blob); +#endif + #endif -- 2.41.0