From patchwork Wed Jan 12 11:39:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 1579013 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=Ex2PH4M3; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JYlwX1bLcz9t25 for ; Wed, 12 Jan 2022 22:39:55 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 45EF78312A; Wed, 12 Jan 2022 12:39:44 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com 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=gmail.com header.i=@gmail.com header.b="Ex2PH4M3"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C421B83146; Wed, 12 Jan 2022 12:39:41 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lf1-x132.google.com (mail-lf1-x132.google.com [IPv6:2a00:1450:4864:20::132]) (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 ACA578313B for ; Wed, 12 Jan 2022 12:39:38 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=zajec5@gmail.com Received: by mail-lf1-x132.google.com with SMTP id x7so7030032lfu.8 for ; Wed, 12 Jan 2022 03:39:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=otMHdam+qY2S6zqNXVrQdJw78lzsgXp4QVxFi85iDPQ=; b=Ex2PH4M3pijWKCAIhDyVi4FRTpVIrxxCR4gNHdhPdoEWGAsTdUAX/X1RjNDXGTVdFG MA4XOxvtNhQIIzLgZRR0xvik3R+XnNl+hsbBoB8MoEkCzUt5qBNSpEUQcVLeRtkUVp3r QnRQi3O1dPc9il107Ts4G9Hx9Y10oWb+msDcC8MizmFWeKCmqG/PmDszFHRbngbyB+1u Bcgd6K2p4WQEFw1aBaCBQoy3jsDEkgkVulBMMINImIuqks3KsjLufsUDV3sYc76Wy1hQ gGB+0MRo4UYPw3MqyoZymkkGypJ+VEaUFVrsVPemeFkH3V6G1uQxqaDa+JVVqAdJ0A1F fblQ== 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=otMHdam+qY2S6zqNXVrQdJw78lzsgXp4QVxFi85iDPQ=; b=3rx95eI13QAKT34wfJYhcQcbZuD1mp9p5GE1OHajEgFkBLr2OOaAHIbESw/GYrFiwO fXa9rYLRUCuz7g0IkWm1f/VSlOa36PwnMjfrF7kQPniHe13LfxL5BTR+UFXiNYdRb+92 RkfG//KX0PQd5v6QK22pEFdZDrH58tkyxtwLKyFt0Ah33GD0vwFGUikcsIcux3kynlyN HkpMaXuYCXOmzUeRxpbRw10Arp67UG9OLC6CxED1N3LYrpEmDHpt2Z8YyL/qgnq+JGdn G9JKQWvEZpL6SiYVA1jaFN8YlYCuS8yooD1CWF3PvTYAKHBqmBVay85cXZoObUkSwJUR kwoA== X-Gm-Message-State: AOAM533z6yiz7lA9/TMKvNbf6urih7OwFo0HK6PrfUP6jB4jgx+B4MeN ltUdx94VLlMe54yiavUXrWp2SoGJNrU= X-Google-Smtp-Source: ABdhPJwUAJKbCnOR0z+luY1JP1Pm/MQNy0yFeyOv39ZDTZwTWFx/Z/Lp8xVYXAhpc48CBhLD5DG0Hg== X-Received: by 2002:a2e:9bcd:: with SMTP id w13mr2502292ljj.360.1641987578059; Wed, 12 Jan 2022 03:39:38 -0800 (PST) Received: from localhost.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id p14sm1578851ljj.12.2022.01.12.03.39.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jan 2022 03:39:37 -0800 (PST) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: u-boot@lists.denx.de Cc: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH V2 1/2] fw_env: make flash_io() take buffer as an argument Date: Wed, 12 Jan 2022 12:39:29 +0100 Message-Id: <20220112113930.7660-2-zajec5@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220112113930.7660-1-zajec5@gmail.com> References: <20220112113930.7660-1-zajec5@gmail.com> MIME-Version: 1.0 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.2 at phobos.denx.de X-Virus-Status: Clean From: Rafał Miłecki It's usually easier to understand code & follow it if all arguments are passed explicitly. Many coding styles also discourage using global variables. Behaviour of flash_io() was a bit unintuitive as it was writing to a buffer referenced in a global struct. That required developers to remember how it works and sometimes required hacking "environment" global struct variable to read data into a proper buffer. Signed-off-by: Rafał Miłecki --- V2: Pass correct buffer to the flash_io() --- tools/env/fw_env.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c index 3da75be783..84c88182db 100644 --- a/tools/env/fw_env.c +++ b/tools/env/fw_env.c @@ -346,7 +346,7 @@ static int ubi_write(int fd, const void *buf, size_t count) return 0; } -static int flash_io(int mode); +static int flash_io(int mode, void *buf, size_t count); static int parse_config(struct env_opts *opts); #if defined(CONFIG_FILE) @@ -516,7 +516,7 @@ int fw_env_flush(struct env_opts *opts) *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE); /* write environment back to flash */ - if (flash_io(O_RDWR)) { + if (flash_io(O_RDWR, environment.image, CUR_ENVSIZE)) { fprintf(stderr, "Error: can't write fw_env to flash\n"); return -1; } @@ -1185,7 +1185,8 @@ static int flash_flag_obsolete(int dev, int fd, off_t offset) return rc; } -static int flash_write(int fd_current, int fd_target, int dev_target) +static int flash_write(int fd_current, int fd_target, int dev_target, void *buf, + size_t count) { int rc; @@ -1212,11 +1213,10 @@ static int flash_write(int fd_current, int fd_target, int dev_target) if (IS_UBI(dev_target)) { if (ubi_update_start(fd_target, CUR_ENVSIZE) < 0) return -1; - return ubi_write(fd_target, environment.image, CUR_ENVSIZE); + return ubi_write(fd_target, buf, count); } - rc = flash_write_buf(dev_target, fd_target, environment.image, - CUR_ENVSIZE); + rc = flash_write_buf(dev_target, fd_target, buf, count); if (rc < 0) return rc; @@ -1235,17 +1235,17 @@ static int flash_write(int fd_current, int fd_target, int dev_target) return 0; } -static int flash_read(int fd) +static int flash_read(int fd, void *buf, size_t count) { int rc; if (IS_UBI(dev_current)) { DEVTYPE(dev_current) = MTD_ABSENT; - return ubi_read(fd, environment.image, CUR_ENVSIZE); + return ubi_read(fd, buf, count); } - rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE, + rc = flash_read_buf(dev_current, fd, buf, count, DEVOFFSET(dev_current)); if (rc != CUR_ENVSIZE) return -1; @@ -1291,7 +1291,7 @@ err: return rc; } -static int flash_io_write(int fd_current) +static int flash_io_write(int fd_current, void *buf, size_t count) { int fd_target = -1, rc, dev_target; const char *dname, *target_temp = NULL; @@ -1322,7 +1322,7 @@ static int flash_io_write(int fd_current) fd_target = fd_current; } - rc = flash_write(fd_current, fd_target, dev_target); + rc = flash_write(fd_current, fd_target, dev_target, buf, count); if (fsync(fd_current) && !(errno == EINVAL || errno == EROFS)) { fprintf(stderr, @@ -1377,7 +1377,7 @@ static int flash_io_write(int fd_current) return rc; } -static int flash_io(int mode) +static int flash_io(int mode, void *buf, size_t count) { int fd_current, rc; @@ -1391,9 +1391,9 @@ static int flash_io(int mode) } if (mode == O_RDWR) { - rc = flash_io_write(fd_current); + rc = flash_io_write(fd_current, buf, count); } else { - rc = flash_read(fd_current); + rc = flash_read(fd_current, buf, count); } if (close(fd_current)) { @@ -1455,7 +1455,7 @@ int fw_env_open(struct env_opts *opts) } dev_current = 0; - if (flash_io(O_RDONLY)) { + if (flash_io(O_RDONLY, environment.image, CUR_ENVSIZE)) { ret = -EIO; goto open_cleanup; } @@ -1490,7 +1490,7 @@ int fw_env_open(struct env_opts *opts) * other pointers in environment still point inside addr0 */ environment.image = addr1; - if (flash_io(O_RDONLY)) { + if (flash_io(O_RDONLY, environment.data, CUR_ENVSIZE)) { ret = -EIO; goto open_cleanup; }