From patchwork Fri Jan 18 19:19:03 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Protsenko X-Patchwork-Id: 156044 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp3612497jaa; Fri, 18 Jan 2019 11:19:41 -0800 (PST) X-Google-Smtp-Source: ALg8bN5Ejxi7rVQjTCxHS8N4spIzdY3EXR8aRXolUCsk85L5XpLwy1fy/m9V2LV/hvKRMQoItyb1 X-Received: by 2002:a17:906:2313:: with SMTP id l19-v6mr15738112eja.204.1547839180906; Fri, 18 Jan 2019 11:19:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547839180; cv=none; d=google.com; s=arc-20160816; b=Tl7hS+I87Uvu+uJ7W6YJHHB4i+MRm4XQxnfIVUKwSfigo68upGgOjQPvWrEUTad0qh Jlipl2DNYqkHpFvg8JppuCzVsh7U9JOwEbpj+dZIALlsRhDj6o1WYs0J+OV2KY79ZW0s unBxG5P/Y7zzOr+ADO2g+NzQyYbV0bgyWgB6CyeWV39LxjZ5MwVwuII2X3DWMevKNxx7 3WrA3eaFqq8opsKPSbJLUi689IBYdfUAHIbl+7T2tIdndMtXztYepPFfazJESwIY4b6d QQS/pInIioPUN/KI1FBB5oliIguGRvf+3YmG4As5stQW1VPJ3r9TBMMA/lv0ZcK+QFmp Yo7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :cc:mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature; bh=mDIDfyKdQYZlfBCHtlv4YKsdkl3yVk7bVEzxq8aCxSg=; b=Es408dmfg7WPPPhqTxjaHkU6BZMQh52UT6pcLMT1E6QS8FSbMc5G8N5cmYzFr5V3Rb VkG89NB0S0KJnxrLD1VmjpG/8992EbLxXz1fc2MoGO0bIewEX1SdKk2ZOTq8Kpedc7HP tuv5utboKdI+s8izCsVG9l5Vtnf7xru9qxmIqlZGP+W1SV2Et3ygYw5D4qOwZKir2jNE w1eDNNFj0LTEKcg1QPfaAh915V3wr2wNmCOXIwLh2Dm4hUqr2hlC54J6jyXK+/UV635o UEFcY/so0qhE71nZGG83/bAT7nqAL456m9yyAStSpvCVy9u3s4JFmycM4Z9U0+b7gDFp XA4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=ODaypQOB; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.denx.de (dione.denx.de. [81.169.180.215]) by mx.google.com with ESMTP id c33si2195270edf.294.2019.01.18.11.19.40; Fri, 18 Jan 2019 11:19:40 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) client-ip=81.169.180.215; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=ODaypQOB; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.denx.de (Postfix, from userid 105) id 7D6B7C21C6A; Fri, 18 Jan 2019 19:19:29 +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=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 7E52AC21C27; Fri, 18 Jan 2019 19:19:10 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 2F72FC21C6A; Fri, 18 Jan 2019 19:19:08 +0000 (UTC) Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) by lists.denx.de (Postfix) with ESMTPS id BD8DFC21C27 for ; Fri, 18 Jan 2019 19:19:07 +0000 (UTC) Received: by mail-lf1-f45.google.com with SMTP id y14so11262580lfg.13 for ; Fri, 18 Jan 2019 11:19:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tUvJGCqEVYWuCyNl2hRZYfKZvgtQnx0ZiquEh0A3vt0=; b=ODaypQOBv/tsQyYAA3cdl7eKyprLs3MgdZv5B4O6KQxe5zIXaEwKeFxJPUQ1ZBJfAk 9DdIv9Mteq2sumgYRVZk/Sm3+kd19+gdQBXCiY+HCOuC5mTHenr65YM8Lry2FwygFgoE e4vasTGhG6eWtLoLjPGOwXzGM/3I4K6CRXLjo= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=tUvJGCqEVYWuCyNl2hRZYfKZvgtQnx0ZiquEh0A3vt0=; b=AyMq7W/71HkLv3D4/JVthXY8IWrETLI2G5Je3ace3BE1zdgffXr88oFOk180KlRJ1R KaVXEccfAEShXld+tyBlXQ/8GBTk31B9Mt2o6kodOJHOhKPDUVpwTUrka6XdOMJvUUHU F0IonVZtqxhSFtD7j7QI2ZhoeDw8za4nU4WKUs6mXxf5KWmQa7F50LPKq7IMipO8TnLD kjPiutOqLiLjjFrfalVn9AbSyA6OGSZKyR91MnfFL03sCciGp5TtFwrzEni9e1l+gqaE g0hjf6hPsgcsOU/SIAN6kTIR4+ipxvhNoyuCdlMpgLklSc26Yf6dUQUK+V89h9lqInXZ 7XWA== X-Gm-Message-State: AJcUukfnFDo1VRxy8TCPycP8INGPkijmwic3/YCEKgl/W+fsSiK1HU1y zsn/EekyQONHoOZ+74ri7AulCP6cxTE= X-Received: by 2002:a19:c954:: with SMTP id z81mr13043433lff.150.1547839146950; Fri, 18 Jan 2019 11:19:06 -0800 (PST) Received: from localhost ([195.238.92.132]) by smtp.gmail.com with ESMTPSA id k3-v6sm874763lja.8.2019.01.18.11.19.06 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 18 Jan 2019 11:19:06 -0800 (PST) From: Sam Protsenko To: u-boot@lists.denx.de Date: Fri, 18 Jan 2019 21:19:03 +0200 Message-Id: <20190118191904.634-2-semen.protsenko@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190118191904.634-1-semen.protsenko@linaro.org> References: <20190118191904.634-1-semen.protsenko@linaro.org> MIME-Version: 1.0 Cc: Tom Rini , Nicholas Faustini , Maxime Ripard Subject: [U-Boot] [PATCH 1/2] env: common: Return specific error code on bad CRC 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" Callers of env_import*() functions might want to check the case when we have incorrect environment (with bad CRC). For example, when environment location is being defined in env_load(), call chain may look like this: env_load() -> drv->load() = env_mmc_load() -> env_import() Return code will be passed from env_import() all way up to env_load(). Right now both env_mmc_load() and env_import() return -EIO error code, so env_load() can't differentiate between two cases: 1. Driver reports the error, because device is not accessible 2. Device is actually accessible, but environment is broken Let's return -ENOMSG in env_import(), so we can distinguish two cases mentioned above. It will make it possible to continue working with "bad CRC" environment (like doing "env save"), instead of considering it not functional (implemented in subsequent patch). Signed-off-by: Sam Protsenko Reviewed-by: Simon Goldschmidt --- env/common.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/env/common.c b/env/common.c index d1a6a52860..324502ed82 100644 --- a/env/common.c +++ b/env/common.c @@ -115,7 +115,7 @@ int env_import(const char *buf, int check) if (crc32(0, ep->data, ENV_SIZE) != crc) { set_default_env("bad CRC", 0); - return -EIO; + return -ENOMSG; /* needed for env_load() */ } } @@ -169,7 +169,7 @@ int env_import_redund(const char *buf1, int buf1_read_fail, if (!crc1_ok && !crc2_ok) { set_default_env("bad CRC", 0); - return -EIO; + return -ENOMSG; /* needed for env_load() */ } else if (crc1_ok && !crc2_ok) { gd->env_valid = ENV_VALID; } else if (!crc1_ok && crc2_ok) { From patchwork Fri Jan 18 19:19:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Protsenko X-Patchwork-Id: 156045 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp3613297jaa; Fri, 18 Jan 2019 11:20:38 -0800 (PST) X-Google-Smtp-Source: ALg8bN6VrBS5dMRDdTyAc2WL09VtizS0I95g01dKo+Za9j3pdeRDialdUCaL5TnKPm6zb9VgbiKG X-Received: by 2002:a50:c11a:: with SMTP id l26mr17018716edf.233.1547839238895; Fri, 18 Jan 2019 11:20:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547839238; cv=none; d=google.com; s=arc-20160816; b=l06abBnz9hZw2uCGmhAlejyUIMs/oec9iSZsYYizc2hMncAAJlkkChM9pTfk4WVz06 BuCbmzIgNEzfMS6Gw878HNDejdjEHjyKi1J7959IbwOYqF29e6SBDKkDh9SqN6/40e/D O7LI48l/7A3KCppXgtUeK4W+SBRh3PJcOzS/L/r+7H9R3C0TuI4LbBiOv8SieRrVhgeH 6Boavy6MJqUCBtGufu1phs6hhLRsaLbm4zGxfl6QKorjCnYPOdI+X0kuylCy/uj1SLw6 GL78JxUjkKaBfaVP5B8dvc6zYX5hWaqSNuuJVahxvFawiuwjAjmwxxjpWF5WS5+u3kjO AceA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :cc:mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature; bh=xz8xkLeSyGDLyIaVi6quNhQzampp8gb6b6b/ef/s54Q=; b=NjYpP0VvY8ufyKr1NreLd9uEM2ixMAKiusOPjWRkppyHdE6pINIXkGLu7ibkd8Mp5I 2Q50VQj5XRdKjCpRojXj2e2c5ZGh+L4igcxfyvnW/aZbI+VI3q8fNHzz/FSAoPqfrxPh W/iKgNDJL/ZVpkpTxh1sP6R10Bf+eOleypS3Rlu4qAtWX2uygIcNmYLf8CiVau2Cut1v XNFV0+Vb5mgVDh+aeFWgG5g0QLO8U3MZS25+hsOV9/VLnK9vu1T4+D/N71SatYJOEZzg FsbQeqZtsl63ucewjraVB9rciQcHZBdW7d9nSoUTjPrG0hdZVBAGBPIuB1P6tChkuLOo V0ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=bPSYFits; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.denx.de (dione.denx.de. [81.169.180.215]) by mx.google.com with ESMTP id w10si3705815edq.75.2019.01.18.11.20.38; Fri, 18 Jan 2019 11:20:38 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) client-ip=81.169.180.215; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=bPSYFits; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.denx.de (Postfix, from userid 105) id 73EA9C2212D; Fri, 18 Jan 2019 19:19:44 +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 5F2BBC2214C; Fri, 18 Jan 2019 19:19:11 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 659FCC21C27; Fri, 18 Jan 2019 19:19:09 +0000 (UTC) Received: from mail-lf1-f68.google.com (mail-lf1-f68.google.com [209.85.167.68]) by lists.denx.de (Postfix) with ESMTPS id CB3E2C21DED for ; Fri, 18 Jan 2019 19:19:08 +0000 (UTC) Received: by mail-lf1-f68.google.com with SMTP id a8so11287092lfk.5 for ; Fri, 18 Jan 2019 11:19:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=H4SorGJWkvDmmArmksjvs+OizuG9kx0z7GUwlvugT6g=; b=bPSYFitstPTNhI6t/KYgpPTKk5x8nnkm+M0O+mxAu0dDSopGPOfqoEE71SXyc9pu+P jiZQIfxirbzXz+nrVfqPiaujiP80sTBjrj5gvrEYqTHblSgLwyUsu4nELwM9jKaEbt+B Fs3wg9CEXAaqMypE8NWf/eWxAdn7NedXVFpVQ= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=H4SorGJWkvDmmArmksjvs+OizuG9kx0z7GUwlvugT6g=; b=gfyC9bh61ZAhspKNjU8fBkHoZ3KJtWaFV4uw1ExCOUgRMMAl/3TY4nKKllEe8xQp8c JsmPL+uMv392GIIzggCS3/s6Q6ls/4IF7YkPpZb5wtx8GXwVhq+LWUs5lhHjHioqOLbs Clj4dK5FSgvfzNMT7ToC3t88kcbknUYXMYae8Wiop7c5TgOZW0Vw7/3JKzDIWs3wmN2F IAFx9Y9jSpN0VeqzHbpBUS9cmoFlp4mj+blMKgAeAvFPoVWaDwwc9oV8wOrWP/LpUCmg Wp3rxcUxtHH5UUhhSfhbgJPnOlJMHpNfAT2zs9mP3NlJ1NVvmZllPlPiZNt7/NS32x2q ZXvA== X-Gm-Message-State: AJcUukfyNyUb1BXe+ASyux4J67j8uAIHaTgHVgk1vg1yPVNVPcYRuTxc qThL2q6ROcWjueULm4hboOJ1rbT27vU= X-Received: by 2002:a19:40cc:: with SMTP id n195mr13440508lfa.40.1547839148067; Fri, 18 Jan 2019 11:19:08 -0800 (PST) Received: from localhost ([195.238.92.132]) by smtp.gmail.com with ESMTPSA id p77-v6sm993922lja.0.2019.01.18.11.19.07 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 18 Jan 2019 11:19:07 -0800 (PST) From: Sam Protsenko To: u-boot@lists.denx.de Date: Fri, 18 Jan 2019 21:19:04 +0200 Message-Id: <20190118191904.634-3-semen.protsenko@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190118191904.634-1-semen.protsenko@linaro.org> References: <20190118191904.634-1-semen.protsenko@linaro.org> MIME-Version: 1.0 Cc: Tom Rini , Nicholas Faustini , Maxime Ripard Subject: [U-Boot] [PATCH 2/2] env: Fix saving environment to "bad CRC" location 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" In case when the environment on some location is malformed (CRC isn't matching), there is a chance we won't be able to save the environment to that location. For example, consider the case when we only have the environment on eMMC, but it's zeroed out. In that case, we won't be able to "env save" to it, because of "bad CRC" error. That's happening because in env_load() function we consider malformed environment as incorrect one, and defaulting to the location with highest (0) priority, which can be different from one we are dealing with right now (e.g., highest priority can be ENV_FAT on SD card, which is not inserted, but we want to use ENV_MMC on eMMC, where we were booted from). This issue began to reproduce after commit d30ba2315ae3 ("u-boot: remove driver lookup loop from env_save()") on BeagleBone Black, but that commit didn't introduce the wrong logic, it just changed the behavior for default location to use, merely revealing this issue. To fix that, let's implement next logic in env_load(): 1. Try to find out correct environment; if found -- use it 2. If working environment wasn't found, but we found malformed one (with bad CRC), let's use it for further "env save". But make sure to use malformed environment location with highest priority. 3. If neither correct nor malformed environment was found, let's default to environment location with highest priority (0) Steps to reproduce mentioned issue on BeagleBone Black (fixed in this patch): 1. Boot from SD card and erase eMMC in U-Boot shell: => mmc dev 1 => mmc erase 0 100000 => gpt write mmc 1 $partitions 2. Write new SPL and U-Boot to eMMC; the rest of eMMC will stay filled with zeroes 3. Boot from eMMC; try to do: => env save 4. Observe the error (incorrect behavior). Correct behavior: environment should be stored correctly on eMMC, in spite of it has "bad CRC" Fixes: d30ba2315ae3 ("u-boot: remove driver lookup loop from env_save()") Signed-off-by: Sam Protsenko Reviewed-by: Simon Goldschmidt --- env/env.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/env/env.c b/env/env.c index 003509d342..4b417b90a2 100644 --- a/env/env.c +++ b/env/env.c @@ -177,6 +177,7 @@ int env_get_char(int index) int env_load(void) { struct env_driver *drv; + int best_prio = -1; int prio; for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) { @@ -195,20 +196,32 @@ int env_load(void) * one message. */ ret = drv->load(); - if (ret) { - debug("Failed (%d)\n", ret); - } else { + if (!ret) { printf("OK\n"); return 0; + } else if (ret == -ENOMSG) { + /* Handle "bad CRC" case */ + if (best_prio == -1) + best_prio = prio; + } else { + debug("Failed (%d)\n", ret); } } /* * In case of invalid environment, we set the 'default' env location - * to the highest priority. In this way, next calls to env_save() - * will restore the environment at the right place. + * to the best choice, i.e.: + * 1. Environment location with bad CRC, if such location was found + * 2. Otherwise use the location with highest priority + * + * This way, next calls to env_save() will restore the environment + * at the right place. */ - env_get_location(ENVOP_LOAD, 0); + if (best_prio >= 0) + debug("Selecting environment with bad CRC\n"); + else + best_prio = 0; + env_get_location(ENVOP_LOAD, best_prio); return -ENODEV; }