From patchwork Thu Sep 27 20:45:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 975887 X-Patchwork-Delegate: joe.hershberger@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) 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=prevas.dk Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=prevas.se header.i=@prevas.se header.b="k0SKdu11"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42Lmzg5xcPz9s1x for ; Fri, 28 Sep 2018 06:45:47 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 5C4C4C21E07; Thu, 27 Sep 2018 20:45:41 +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=BAD_ENC_HEADER, RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS, 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 16A89C21C2F; Thu, 27 Sep 2018 20:45:39 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 21788C21C8B; Thu, 27 Sep 2018 20:45:38 +0000 (UTC) Received: from EUR02-VE1-obe.outbound.protection.outlook.com (mail-eopbgr20097.outbound.protection.outlook.com [40.107.2.97]) by lists.denx.de (Postfix) with ESMTPS id B2642C21C2F for ; Thu, 27 Sep 2018 20:45:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=prevas.se; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=bbAD1AkQ4znNKSuPMluGJgoAoWG3c26UaGDtnUB8F1I=; b=k0SKdu111jfjvDAMLLs9GpugN+eSuzI8TZ91AsZFX6jnfUATpDsz4lEmMiVH4Q/BWqn57VrYQk/taB1MfC+5m3mpK+z9Lo/9ZeLTapKjkWXebCHpIS+QBQcKSjqY2WRh93AKK5sttk1jTsgdnlQ/nlZnYnIZwWYgPOWQDBYGhgQ= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Rasmus.Villemoes@prevas.se; Received: from prevas-ravi.waoo.dk (5.186.116.96) by VI1PR1001MB1341.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:800:e5::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1164.25; Thu, 27 Sep 2018 20:45:36 +0000 From: Rasmus Villemoes To: u-boot@lists.denx.de Date: Thu, 27 Sep 2018 22:45:26 +0200 Message-Id: <20180927204526.5206-1-rasmus.villemoes@prevas.dk> X-Mailer: git-send-email 2.16.4 MIME-Version: 1.0 X-Originating-IP: [5.186.116.96] X-ClientProxiedBy: CWXP265CA0048.GBRP265.PROD.OUTLOOK.COM (2603:10a6:400:2d::36) To VI1PR1001MB1341.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:800:e5::10) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: bfd399fe-06fe-492d-ccf0-08d624ba2d77 X-Microsoft-Antispam: BCL:0; PCL:0; RULEID:(7020095)(4652040)(8989299)(4534165)(4627221)(201703031133081)(201702281549075)(8990200)(5600074)(711020)(2017052603328)(7153060)(7193020); SRVR:VI1PR1001MB1341; X-Microsoft-Exchange-Diagnostics: 1; VI1PR1001MB1341; 3:vrLVPEFuSCWFluIJbO1s13QdPBMnEMvPl07vCs7FDlv8soDW7OjTk28VvIW0zkJkwvAZySyy9B/Qb13WJ8gZ8riQcLv0tgThvqPnOrZYSngwYV/I8R6KRw4/geTTU2dtxrNnFHZGuryv6y2JdXxYtsNHYn4rKavR2UtzUFrSPRH+sLj3V2wUT06Uhd6kzPrKGeQJoELHz6l29ghjq5wYp04ZzUwMzvl3/PvkbXlMrED4LEY6wQD9/l0z+zTisU2V; 25:bnLFlCYG6gbMIWB+0/ZqII5y4sJ/COZL13Gbsh8fu3dxcs2SKwq7H4cUskhc6p68DzA08fiZBmdghx9WcK6w4n9xvhOKJGqiT0o6S0wvAuVThLLSc0pv1zL+KNmMZhYxGBvmGITmnh6NEiFnLAfDqUPxZ8IuTo6/puPV7G+6RObhOKiTYdLcJKkud2cfsIRfbvNlkuiG1GoILDz3qKdCsO0mv7YTfGRZYGbX7h9fZ2IGlqppYsVacSH9s7JPFdQBAgGwreC9PwkQM+zmUPW8LQdz2TflzGnliub25xi4Ya9R+SrFJw0Bp6Q6M+pWRJZipT2ZFnNuDa44XWru9nQUrg==; 31:szKIDEaaKv1QtwreoUoTfhopjmYCzTIPDNrL3RMTDSWPTRNQ09y7NR5tGJYKtsMTO0s23/egC6b5rqKrFlIZbljzj385F/g+8y9qKWUrGh+GDbbSrpaBPnOoDTiw2h47PrOBFc+V0b5HfO2EociWgEZmuOkQg/J42rG3Dngn1m8XorC5i1af7G5pixy5y340EPWSsNCB8pPg3XuQUEyj5AVZFCvsTQjqjteMOaty8ck= X-MS-TrafficTypeDiagnostic: VI1PR1001MB1341: X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-MS-Exchange-SenderADCheck: 1 X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040522)(2401047)(5005006)(8121501046)(10201501046)(3002001)(93006095)(3231355)(944501410)(52105095)(149066)(150057)(6041310)(20161123564045)(20161123560045)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(201708071742011)(7699051); SRVR:VI1PR1001MB1341; BCL:0; PCL:0; RULEID:; SRVR:VI1PR1001MB1341; X-Microsoft-Exchange-Diagnostics: 1; VI1PR1001MB1341; 4:cNUsyp6PuU8Dfzu73paaeazCovLu3vEnYSUy/JTXEOe/3rXuHemJpdHx6ZplgBkCzwBRzgY+uIWRteJuEJI6nhYZvvUcya0NO0p+oQGmt484BmXys3VKWryegDV865W6MokFjFXwBKjD8l6qsGOL3+59PGbJaLfsEvrMBMJ6LpeMKkOu5DENVu6wO5x8bTTwGemJfpZ40dsN+5Hh1llbPu73n3a+GvIo8smEnczDcL6zud2NstOexqF2g5pKiptp4UySBAsvfe29KxOPgYGxtg== X-Forefront-PRVS: 0808323E97 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(346002)(376002)(39850400004)(136003)(396003)(366004)(189003)(199004)(74482002)(956004)(305945005)(97736004)(14444005)(8676002)(50466002)(2361001)(486006)(2906002)(2616005)(476003)(72206003)(50226002)(36756003)(5660300001)(68736007)(44832011)(47776003)(7736002)(42882007)(478600001)(66066001)(6116002)(3846002)(6512007)(52116002)(186003)(26005)(39060400002)(16526019)(8936002)(51416003)(8976002)(107886003)(25786009)(81166006)(386003)(106356001)(4326008)(6486002)(81156014)(6506007)(1076002)(6916009)(105586002)(54906003)(48376002)(69596002)(16586007)(2351001)(34290500001)(316002)(53936002)(71446004)(6666003)(53416004); DIR:OUT; SFP:1102; SCL:1; SRVR:VI1PR1001MB1341; H:prevas-ravi.waoo.dk; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; Received-SPF: None (protection.outlook.com: prevas.se does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; VI1PR1001MB1341; 23:b3Ruuo8mHZkMAqZeC3tpva3GUoY6+9qKp+Jhby7?= tYjJiiRB+xFdzcQMqXUn9HgjffANU4mpGk/igOuksked7Z5/354EL3E9weQGiqvElhtZbmtP6G3FQt7HFznBUiDFz8rxFMtz2nJrPo77w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ycuvsVYT8oLO9EwRqWZkj40bY8oP91A==; 5:zzWJe2LbPSBR3q4TzlKHRrCNOrPebhg+wFDARfJoeCEO5rjc5J94msxkNlHcLdQDig7INVYucJmqX9wEf3klhxnT6IGiEi2txvfPcwzmVWSep1v5BAD2bhbGMtMB9NafyuhCo9gjtqRNpo/rPTn4fuUWi/AlMdBgiuY2bw/7WuE=; 7:e9/9cXlrUGmmzOBwfNFMyEDntpjfYsXpyd4Ih7dVy/W8F1KDkb1ePKo7d0Fa1EyXtTkxq/ansl73mtfFeTMn1khxuIMkT715TfZXfNtBatcfV8rkV1f6eFr2mhtPiEyS7dInNSJmmRhfW84qwSABR1zwT0cjoweDHn5jbGREO879N8UWVNMNXzRupWMfn982jXbIuBYG6i8+3pwZ2X3QqaOlDVLY3i/nIKDVEA0zJricvRkYzDt8omTKOBPva8Ke SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: prevas.dk X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Sep 2018 20:45:36.0042 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bfd399fe-06fe-492d-ccf0-08d624ba2d77 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: d350cf71-778d-4780-88f5-071a4cb1ed61 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR1001MB1341 Subject: [U-Boot] [PATCH v2] fw_setenv: avoid writing environment when nothing has changed 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 the case where one deletes an already-non-existing variable, or sets a variable to the value it already has, there is no point in writing the environment back, thus reducing wear on the underlying storage device. In the case of redundant environments, if the two environments differ (e.g. because one is corrupt), make sure that any call of fw_setenv causes the two to become synchronized, even if the fw_setenv call does not change anything in the good copy. Signed-off-by: Rasmus Villemoes Acked-by: Joe Hershberger --- Add logic to ensure a corrupt copy gets replaced, even if fw_setenv wouldn't change anything in the good copy. tools/env/fw_env.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c index a5d75958e1..66372dad55 100644 --- a/tools/env/fw_env.c +++ b/tools/env/fw_env.c @@ -110,6 +110,7 @@ struct environment { unsigned char *flags; char *data; enum flag_scheme flag_scheme; + int dirty; }; static struct environment environment = { @@ -508,6 +509,9 @@ int fw_env_flush(struct env_opts *opts) if (!opts) opts = &default_opts; + if (!environment.dirty) + return 0; + /* * Update CRC */ @@ -553,7 +557,8 @@ int fw_env_write(char *name, char *value) deleting = (oldval && !(value && strlen(value))); creating = (!oldval && (value && strlen(value))); - overwriting = (oldval && (value && strlen(value))); + overwriting = (oldval && (value && strlen(value) && + strcmp(oldval, value))); /* check for permission */ if (deleting) { @@ -593,6 +598,7 @@ int fw_env_write(char *name, char *value) /* Nothing to do */ return 0; + environment.dirty = 1; if (deleting || overwriting) { if (*++nxt == '\0') { *env = '\0'; @@ -1441,6 +1447,7 @@ int fw_env_open(struct env_opts *opts) "Warning: Bad CRC, using default environment\n"); memcpy(environment.data, default_environment, sizeof(default_environment)); + environment.dirty = 1; } } else { flag0 = *environment.flags; @@ -1494,6 +1501,16 @@ int fw_env_open(struct env_opts *opts) crc1_ok = (crc1 == redundant->crc); flag1 = redundant->flags; + /* + * environment.data still points to ((struct + * env_image_redundant *)addr0)->data. If the two + * environments differ, or one has bad crc, force a + * write-out by marking the environment dirty. + */ + if (memcmp(environment.data, redundant->data, ENV_SIZE) || + !crc0_ok || !crc1_ok) + environment.dirty = 1; + if (crc0_ok && !crc1_ok) { dev_current = 0; } else if (!crc0_ok && crc1_ok) { @@ -1503,6 +1520,7 @@ int fw_env_open(struct env_opts *opts) "Warning: Bad CRC, using default environment\n"); memcpy(environment.data, default_environment, sizeof(default_environment)); + environment.dirty = 1; dev_current = 0; } else { switch (environment.flag_scheme) {