From patchwork Fri Nov 15 07:27:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiko Schocher X-Patchwork-Id: 1195435 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=none (p=none dis=none) header.from=denx.de Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 47DqhX1DKyz9sP3 for ; Fri, 15 Nov 2019 18:28:28 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id D65CEC21DF9; Fri, 15 Nov 2019 07:28:13 +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_H3, RCVD_IN_MSPIKE_WL 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 6F66CC21DD3; Fri, 15 Nov 2019 07:28:01 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id A8EA0C21C3F; Fri, 15 Nov 2019 07:28:00 +0000 (UTC) Received: from mail-out.m-online.net (mail-out.m-online.net [212.18.0.9]) by lists.denx.de (Postfix) with ESMTPS id 5D72FC21C29 for ; Fri, 15 Nov 2019 07:28:00 +0000 (UTC) Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 47Dqh01Mp0z1rD9f; Fri, 15 Nov 2019 08:28:00 +0100 (CET) Received: from localhost (dynscan1.mnet-online.de [192.168.6.70]) by mail.m-online.net (Postfix) with ESMTP id 47Dqh00wXxz1qqkT; Fri, 15 Nov 2019 08:28:00 +0100 (CET) X-Virus-Scanned: amavisd-new at mnet-online.de Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan1.mail.m-online.net [192.168.6.70]) (amavisd-new, port 10024) with ESMTP id mkV-MZ_2Nj5R; Fri, 15 Nov 2019 08:27:59 +0100 (CET) X-Auth-Info: +sw/8AjMrwcOD9By8naMn61cnzVy260bW/8rAVNbn+g= Received: from mail-internal.denx.de (p578adb1c.dip0.t-ipconnect.de [87.138.219.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPSA; Fri, 15 Nov 2019 08:27:59 +0100 (CET) Received: from pollux.denx.de (pollux [192.168.1.1]) by mail-internal.denx.de (Postfix) with ESMTP id 590F81807A0; Fri, 15 Nov 2019 08:27:52 +0100 (CET) Received: by pollux.denx.de (Postfix, from userid 515) id 572EE1A0088; Fri, 15 Nov 2019 08:27:52 +0100 (CET) From: Heiko Schocher To: U-Boot Mailing List , Simon Goldschmidt Date: Fri, 15 Nov 2019 08:27:36 +0100 Message-Id: <20191115072737.3543518-2-hs@denx.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191115072737.3543518-1-hs@denx.de> References: <20191115072737.3543518-1-hs@denx.de> MIME-Version: 1.0 Cc: Joe Hershberger , Boris Brezillon Subject: [U-Boot] [PATCH v2 1/2] env: split env_import_redund() into 2 functions 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: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" split from env_import_redund() the part which checks which Environment is valid into a separate function called env_check_redund() and call it from env_import_redund(). So env_check_redund() can be used from places which also need to do this checks. Signed-off-by: Heiko Schocher --- The return values from env_check_redund() for the cases only one read buffer is OK are not perfect, may we should find better return codes? Changes in v2: - patch "env: split env_import_redund into 2 functions" is new in version 2. Idea is to not duplicate too much code as Simon suggested. env/common.c | 40 +++++++++++++++++++++++++++++++--------- include/env.h | 18 ++++++++++++++++++ 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/env/common.c b/env/common.c index 4daaa6faea..790829b71f 100644 --- a/env/common.c +++ b/env/common.c @@ -136,11 +136,11 @@ int env_import(const char *buf, int check) #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT static unsigned char env_flags; -int env_import_redund(const char *buf1, int buf1_read_fail, - const char *buf2, int buf2_read_fail) +int env_check_redund(const char *buf1, int buf1_read_fail, + const char *buf2, int buf2_read_fail) { int crc1_ok, crc2_ok; - env_t *ep, *tmp_env1, *tmp_env2; + env_t *tmp_env1, *tmp_env2; tmp_env1 = (env_t *)buf1; tmp_env2 = (env_t *)buf2; @@ -153,14 +153,13 @@ int env_import_redund(const char *buf1, int buf1_read_fail, } if (buf1_read_fail && buf2_read_fail) { - env_set_default("bad env area", 0); return -EIO; } else if (!buf1_read_fail && buf2_read_fail) { gd->env_valid = ENV_VALID; - return env_import((char *)tmp_env1, 1); + return -EINVAL; } else if (buf1_read_fail && !buf2_read_fail) { gd->env_valid = ENV_REDUND; - return env_import((char *)tmp_env2, 1); + return -ENOENT; } crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == @@ -169,7 +168,6 @@ int env_import_redund(const char *buf1, int buf1_read_fail, tmp_env2->crc; if (!crc1_ok && !crc2_ok) { - env_set_default("bad CRC", 0); return -ENOMSG; /* needed for env_load() */ } else if (crc1_ok && !crc2_ok) { gd->env_valid = ENV_VALID; @@ -189,12 +187,36 @@ int env_import_redund(const char *buf1, int buf1_read_fail, gd->env_valid = ENV_VALID; } + return 0; +} + +int env_import_redund(const char *buf1, int buf1_read_fail, + const char *buf2, int buf2_read_fail) +{ + env_t *ep; + int ret; + + ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail); + + if (ret == -EIO) { + env_set_default("bad env area", 0); + return -EIO; + } else if (ret == -EINVAL) { + return env_import((char *)buf1, 1); + } else if (ret == -ENOENT) { + return env_import((char *)buf2, 1); + } else if (ret == -ENOMSG) { + env_set_default("bad CRC", 0); + return -ENOMSG; + } + if (gd->env_valid == ENV_VALID) - ep = tmp_env1; + ep = (env_t *)buf1; else - ep = tmp_env2; + ep = (env_t *)buf2; env_flags = ep->flags; + return env_import((char *)ep, 0); } #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ diff --git a/include/env.h b/include/env.h index b72239f6a5..4616551afb 100644 --- a/include/env.h +++ b/include/env.h @@ -293,6 +293,24 @@ int env_import(const char *buf, int check); */ int env_export(struct environment_s *env_out); +/** + * env_check_redund() - check the two redundant environments + * and find out, which is the valid one. + * + * @buf1: First environment (struct environemnt_s *) + * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid + * @buf2: Second environment (struct environemnt_s *) + * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid + * @return 0 if OK, + * -EIO if no environment is valid, + * -EINVAL if read of second entry is good + * -ENOENT if read of first entry is good + * -ENOMSG if the CRC was bad + */ + +int env_check_redund(const char *buf1, int buf1_read_fail, + const char *buf2, int buf2_read_fail); + /** * env_import_redund() - Select and import one of two redundant environments * From patchwork Fri Nov 15 07:27:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiko Schocher X-Patchwork-Id: 1195436 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=none (p=none dis=none) header.from=denx.de Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 47Dqj13nSBz9sPJ for ; Fri, 15 Nov 2019 18:28:53 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 18437C21E39; Fri, 15 Nov 2019 07:28:49 +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_H3, RCVD_IN_MSPIKE_WL 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 E58B4C21EB9; Fri, 15 Nov 2019 07:28:47 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 15375C21DF9; Fri, 15 Nov 2019 07:28:21 +0000 (UTC) Received: from mail-out.m-online.net (mail-out.m-online.net [212.18.0.10]) by lists.denx.de (Postfix) with ESMTPS id 46E40C21C50 for ; Fri, 15 Nov 2019 07:28:16 +0000 (UTC) Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 47DqhJ0tbMz1rVw7 for ; Fri, 15 Nov 2019 08:28:16 +0100 (CET) Received: from localhost (dynscan1.mnet-online.de [192.168.6.70]) by mail.m-online.net (Postfix) with ESMTP id 47DqhJ0lRFz1qqkZ for ; Fri, 15 Nov 2019 08:28:16 +0100 (CET) X-Virus-Scanned: amavisd-new at mnet-online.de Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan1.mail.m-online.net [192.168.6.70]) (amavisd-new, port 10024) with ESMTP id fkv8VyixemKm for ; Fri, 15 Nov 2019 08:28:14 +0100 (CET) X-Auth-Info: UAWlCHzUi/jb7j6EInFuM49y27bkbBKVt9FL4ibJK00= Received: from mail-internal.denx.de (p578adb1c.dip0.t-ipconnect.de [87.138.219.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPSA for ; Fri, 15 Nov 2019 08:28:14 +0100 (CET) Received: from pollux.denx.de (pollux [192.168.1.1]) by mail-internal.denx.de (Postfix) with ESMTP id 173D4185311; Fri, 15 Nov 2019 08:27:53 +0100 (CET) Received: by pollux.denx.de (Postfix, from userid 515) id 149461A0088; Fri, 15 Nov 2019 08:27:53 +0100 (CET) From: Heiko Schocher To: U-Boot Mailing List , Simon Goldschmidt Date: Fri, 15 Nov 2019 08:27:37 +0100 Message-Id: <20191115072737.3543518-3-hs@denx.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191115072737.3543518-1-hs@denx.de> References: <20191115072737.3543518-1-hs@denx.de> MIME-Version: 1.0 Cc: Hamish Guthrie , T Karthik Reddy , Joe Hershberger Subject: [U-Boot] [PATCH v2 2/2] env: Access Environment in SPI flashes before relocation 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: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Enable the new Kconfig option ENV_SPI_EARLY if you want to use Environment in SPI flash before relocation. Call env_init() and than you can use env_get_f() for accessing Environment variables. Signed-off-by: Heiko Schocher --- Changes in v2: - env_sf_init_early() always return 0 now. If we do not return 0 in this function, env_set_inited() never get called, which has the consequence that env_load/save/erase never work, because they check if the init bit is set. env/Kconfig | 8 ++++++ env/sf.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/env/Kconfig b/env/Kconfig index 090cc795f9..76e4f30839 100644 --- a/env/Kconfig +++ b/env/Kconfig @@ -370,6 +370,14 @@ config ENV_SPI_MODE Value of the SPI work mode for environment. See include/spi.h for value. +config ENV_SPI_EARLY + bool "Access Environment in SPI flashes before relocation" + depends on ENV_IS_IN_SPI_FLASH + help + Enable this if you want to use Environment in SPI flash + before relocation. Call env_init() and than you can use + env_get_f() for accessing Environment variables. + config ENV_IS_IN_UBI bool "Environment in a UBI volume" depends on !CHAIN_OF_TRUST diff --git a/env/sf.c b/env/sf.c index 590d0cedd8..be2e7fc01c 100644 --- a/env/sf.c +++ b/env/sf.c @@ -308,6 +308,85 @@ static int env_sf_init(void) } #endif +#if defined(CONFIG_ENV_SPI_EARLY) +static int env_sf_init_early(void) +{ + int ret; + int read1_fail; +#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT + int read2_fail; +#else + int crc1_ok; +#endif + + /* + * if malloc is not ready yet, we cannot use + * this part yet. + */ + if (!gd->malloc_limit) + return -ENOENT; + + env_t *tmp_env2 = NULL; + env_t *tmp_env1; + + tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN, + CONFIG_ENV_SIZE); +#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT + tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN, + CONFIG_ENV_SIZE); +#endif + if (!tmp_env1 || !tmp_env2) + goto out; + + ret = setup_flash_device(); + if (ret) + goto out; + + read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, + CONFIG_ENV_SIZE, tmp_env1); +#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT + read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, + CONFIG_ENV_SIZE, tmp_env2); + ret = env_check_redund((char *)tmp_env1, read1_fail, + (char *)tmp_env2, read2_fail); + + if ((ret == -EIO) || (ret == -ENOMSG)) + goto err_read; + + if (gd->env_valid == ENV_VALID) + gd->env_addr = (unsigned long)&tmp_env1->data; + else + gd->env_addr = (unsigned long)&tmp_env2->data; + +#else + if (read1_fail) + goto err_read; + + crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == + tmp_env1->crc; + if (!crc1_ok) + goto err_read; + + /* if valid -> this is our env */ + gd->env_valid = ENV_VALID; + gd->env_addr = (unsigned long)&tmp_env1->data; +#endif + + return 0; +err_read: + spi_flash_free(env_flash); + env_flash = NULL; + free(tmp_env1); +#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT + free(tmp_env2); +#endif +out: + /* env is not valid. always return 0 */ + gd->env_valid = ENV_INVALID; + return 0; +} +#endif + U_BOOT_ENV_LOCATION(sf) = { .location = ENVL_SPI_FLASH, ENV_NAME("SPI Flash") @@ -317,5 +396,7 @@ U_BOOT_ENV_LOCATION(sf) = { #endif #if defined(INITENV) && defined(CONFIG_ENV_ADDR) .init = env_sf_init, +#elif defined(CONFIG_ENV_SPI_EARLY) + .init = env_sf_init_early, #endif };