mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Andrey Smirnov <andrew.smirnov@gmail.com>
To: barebox@lists.infradead.org
Cc: Andrey Smirnov <andrew.smirnov@gmail.com>
Subject: [PATCH v2 13/24] ARM: i.MX: bbu: Move protect code into a separate routine
Date: Thu, 23 Aug 2018 19:52:32 -0700	[thread overview]
Message-ID: <20180824025243.19479-14-andrew.smirnov@gmail.com> (raw)
In-Reply-To: <20180824025243.19479-1-andrew.smirnov@gmail.com>

Both code sections responsible for enabling/disabling underlying
device write protection are almost identical to each other. To avoid
code duplication move them into a separate routine and adjust the rest
of the code accordignly.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 arch/arm/mach-imx/imx-bbu-internal.c | 62 +++++++++++++++++++---------
 1 file changed, 42 insertions(+), 20 deletions(-)

diff --git a/arch/arm/mach-imx/imx-bbu-internal.c b/arch/arm/mach-imx/imx-bbu-internal.c
index fef3797a1..12d48401d 100644
--- a/arch/arm/mach-imx/imx-bbu-internal.c
+++ b/arch/arm/mach-imx/imx-bbu-internal.c
@@ -44,6 +44,41 @@ struct imx_internal_bbu_handler {
 	enum filetype expected_type;
 };
 
+static bool
+imx_bbu_erase_required(struct imx_internal_bbu_handler *imx_handler)
+{
+	return imx_handler->handler.flags & IMX_INTERNAL_FLAG_ERASE;
+}
+
+static int imx_bbu_protect(int fd, struct imx_internal_bbu_handler *imx_handler,
+			   const char *devicefile, int offset, int image_len,
+			   int prot)
+{
+	const char *prefix = prot ? "" : "un";
+	int ret;
+
+	if (!imx_bbu_erase_required(imx_handler))
+		return 0;
+
+	pr_debug("%s: %sprotecting %s from 0x%08x to 0x%08x\n", __func__,
+		 prefix, devicefile, offset, image_len);
+
+	ret = protect(fd, image_len, offset, prot);
+	if (ret) {
+		/*
+		 * If protect() is not implemented for this device,
+		 * just report success
+		 */
+		if (ret == -ENOSYS)
+			return 0;
+
+		pr_err("%sprotecting %s failed with %s\n", prefix, devicefile,
+		       strerror(-ret));
+	}
+
+	return ret;
+}
+
 /*
  * Actually write an image to the target device, eventually keeping a
  * DOS partition table on the device
@@ -67,16 +102,12 @@ static int imx_bbu_write_device(struct imx_internal_bbu_handler *imx_handler,
 	if (imx_handler->handler.flags & IMX_BBU_FLAG_KEEP_HEAD)
 		offset += imx_handler->flash_header_offset;
 
-	if (imx_handler->handler.flags & IMX_INTERNAL_FLAG_ERASE) {
-		pr_debug("%s: unprotecting %s from 0x%08x to 0x%08x\n", __func__,
-				devicefile, offset, image_len);
-		ret = protect(fd, image_len, offset, 0);
-		if (ret && ret != -ENOSYS) {
-			pr_err("unprotecting %s failed with %s\n", devicefile,
-					strerror(-ret));
-			goto err_close;
-		}
+	ret = imx_bbu_protect(fd, imx_handler, devicefile, offset,
+			      image_len, 0);
+	if (ret)
+		goto err_close;
 
+	if (imx_bbu_erase_required(imx_handler)) {
 		pr_debug("%s: erasing %s from 0x%08x to 0x%08x\n", __func__,
 				devicefile, offset, image_len);
 		ret = erase(fd, image_len, offset);
@@ -91,17 +122,8 @@ static int imx_bbu_write_device(struct imx_internal_bbu_handler *imx_handler,
 	if (ret < 0)
 		goto err_close;
 
-	if (imx_handler->handler.flags & IMX_INTERNAL_FLAG_ERASE) {
-		pr_debug("%s: protecting %s from 0x%08x to 0x%08x\n", __func__,
-				devicefile, offset, image_len);
-		ret = protect(fd, image_len, offset, 1);
-		if (ret && ret != -ENOSYS) {
-			pr_err("protecting %s failed with %s\n", devicefile,
-					strerror(-ret));
-		}
-	}
-
-	ret = 0;
+	imx_bbu_protect(fd, imx_handler, devicefile, offset,
+			image_len, 1);
 
 err_close:
 	close(fd);
-- 
2.17.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2018-08-24  2:53 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-24  2:52 [PATCH v2 00/24] i.MX BBU improvements and bugfixes Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 01/24] ARM: i.MX: bbu: Remove unused define Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 02/24] filetype: Add arch/ to include path Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 03/24] filetype: Add code to detect i.MX image v1 Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 04/24] filetype: Add code to detect i.MX image v2 Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 05/24] ARM: i.MX: bbu: Move inner-image type check Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 06/24] ARM: i.MX: bbu: Drop IMX_INTERNAL_FLAG_NAND Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 07/24] ARM: i.MX: bbu: Consolidate various update helpers Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 08/24] ARM: i.MX: bbu: Simplify imx53_bbu_internal_nand_register_handler() Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 09/24] ARM: i.MX: bbu: Constify all 'devicefile' arguments Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 10/24] ARM: i.MX: bbu: Detect which platforms need v2 i.MX header Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 11/24] ARM: i.MX: bbu: Alias imx5*_bbu_internal_mmc_register_handler() Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 12/24] ARM: i.MX: bbu: Alias imx5*_bbu_internal_spi_i2c_register_handler() Andrey Smirnov
2018-08-24  2:52 ` Andrey Smirnov [this message]
2018-08-24  2:52 ` [PATCH v2 14/24] ARM: i.MX: bbu: Adjust FLASH_HEADER_OFFSET_MMC for i.MX8MQ Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 15/24] ARM: i.MX: bbu: Add support for SPI/I2C on VFxxx Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 16/24] ARM: i.MX: zii-vf610-dev-rev-b/c: Add support for BBU on SPI-NOR Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 17/24] ARM: i.MX: bbu: Add support for MMC on i.MX8MQ Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 18/24] ARM: nxp-imx8mq-evk: Add eMMC BBU configuration Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 19/24] libfile: Introduce pwrite_full() Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 20/24] ARM: i.MX: bbu: Use pwrite_full() instead of pwrite() Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 21/24] bbu: Remove logical negation in barebox_update_handler_exists() Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 22/24] block: Do not ignore error in blk->ops->write() Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 23/24] bbu: Report update failures explicitly Andrey Smirnov
2018-08-24  2:52 ` [PATCH v2 24/24] bbu: command: Make sure specified update handler exists Andrey Smirnov
2018-08-24  8:09 ` [PATCH v2 00/24] i.MX BBU improvements and bugfixes Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180824025243.19479-14-andrew.smirnov@gmail.com \
    --to=andrew.smirnov@gmail.com \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox