U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Andreas Fenkart <andreas.fenkart@digitalstrom.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 1/6] tools/env: factor out environment_end function
Date: Mon, 29 Aug 2016 23:16:57 +0200	[thread overview]
Message-ID: <1472505422-26122-2-git-send-email-andreas.fenkart@digitalstrom.com> (raw)
In-Reply-To: <1472505422-26122-1-git-send-email-andreas.fenkart@digitalstrom.com>

instead of adhoc computation of the environment end,
use a function with a proper name

Signed-off-by: Andreas Fenkart <andreas.fenkart@digitalstrom.com>
---
 tools/env/fw_env.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)

diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index d27f57e..90eb5fa 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -643,6 +643,18 @@ int fw_parse_script(char *fname, struct env_opts *opts)
 	return ret;
 }
 
+/**
+ * environment_end() - compute offset of first byte right after environemnt
+ * @dev - index of enviroment buffer
+ * Return:
+ *  device offset of first byte right after environemnt
+ */
+off_t environment_end(int dev)
+{
+	/* environment is block aligned */
+	return DEVOFFSET(dev) + ENVSECTORS(dev) * DEVESIZE(dev);
+}
+
 /*
  * Test for bad block on NAND, just returns 0 on NOR, on NAND:
  * 0	- block is good
@@ -683,7 +695,6 @@ static int flash_read_buf (int dev, int fd, void *buf, size_t count,
 				   0 on NOR */
 	size_t processed = 0;	/* progress counter */
 	size_t readlen = count;	/* current read length */
-	off_t top_of_range;	/* end of the last block we may use */
 	off_t block_seek;	/* offset inside the current block to the start
 				   of the data */
 	loff_t blockstart;	/* running start of the current block -
@@ -702,19 +713,11 @@ static int flash_read_buf (int dev, int fd, void *buf, size_t count,
 		 */
 		blocklen = DEVESIZE (dev);
 
-		/*
-		 * To calculate the top of the range, we have to use the
-		 * global DEVOFFSET (dev), which can be different from offset
-		 */
-		top_of_range = ((DEVOFFSET(dev) / blocklen) +
-				ENVSECTORS (dev)) * blocklen;
-
 		/* Limit to one block for the first read */
 		if (readlen > blocklen - block_seek)
 			readlen = blocklen - block_seek;
 	} else {
 		blocklen = 0;
-		top_of_range = offset + count;
 	}
 
 	/* This only runs once on NOR flash */
@@ -723,7 +726,7 @@ static int flash_read_buf (int dev, int fd, void *buf, size_t count,
 		if (rc < 0)		/* block test failed */
 			return -1;
 
-		if (blockstart + block_seek + readlen > top_of_range) {
+		if (blockstart + block_seek + readlen > environment_end(dev)) {
 			/* End of range is reached */
 			fprintf (stderr,
 				 "Too few good blocks within range\n");
@@ -783,7 +786,6 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
 				   below offset */
 	off_t block_seek;	/* offset inside the erase block to the start
 				   of the data */
-	off_t top_of_range;	/* end of the last block we may use */
 	loff_t blockstart;	/* running start of the current block -
 				   MEMGETBADBLOCK needs 64 bits */
 	int rc;
@@ -793,7 +795,6 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
 	 */
 	if (mtd_type == MTD_ABSENT) {
 		blocklen = count;
-		top_of_range = offset + count;
 		erase_len = blocklen;
 		blockstart = offset;
 		block_seek = 0;
@@ -801,13 +802,10 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
 	} else {
 		blocklen = DEVESIZE(dev);
 
-		top_of_range = ((DEVOFFSET(dev) / blocklen) +
-					ENVSECTORS(dev)) * blocklen;
-
 		erase_offset = (offset / blocklen) * blocklen;
 
 		/* Maximum area we may use */
-		erase_len = top_of_range - erase_offset;
+		erase_len = environment_end(dev) - erase_offset;
 
 		blockstart = erase_offset;
 		/* Offset inside a block */
@@ -882,7 +880,7 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
 		if (rc < 0)		/* block test failed */
 			return rc;
 
-		if (blockstart + erasesize > top_of_range) {
+		if (blockstart + erasesize > environment_end(dev)) {
 			fprintf (stderr, "End of range reached, aborting\n");
 			return -1;
 		}
-- 
2.8.1

  reply	other threads:[~2016-08-29 21:16 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-29 21:16 [U-Boot] [PATCH 0/6] tools/env: refactor flash read/write function Andreas Fenkart
2016-08-29 21:16 ` Andreas Fenkart [this message]
2016-10-08 17:04   ` [U-Boot] [U-Boot, 1/6] tools/env: factor out environment_end function Tom Rini
2016-08-29 21:16 ` [U-Boot] [PATCH 2/6] tools/env: pass bad block offset by value Andreas Fenkart
2016-10-08 17:04   ` [U-Boot] [U-Boot, " Tom Rini
2016-08-29 21:16 ` [U-Boot] [PATCH 3/6] tools/env: lookup dev_type directly from flash_read_buf/flash_write_buf Andreas Fenkart
2016-10-08 17:04   ` [U-Boot] [U-Boot, " Tom Rini
2016-08-29 21:17 ` [U-Boot] Suspected Spam: Do not open attachements![PATCH 4/6] tools/env: flash_write_buf: enforce offset to be start of environment Andreas Fenkart
2016-10-08 17:04   ` Tom Rini

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=1472505422-26122-2-git-send-email-andreas.fenkart@digitalstrom.com \
    --to=andreas.fenkart@digitalstrom.com \
    --cc=u-boot@lists.denx.de \
    /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