[U-Boot] [PATCH] env: add flash_read function

Horatiu Vultur horatiu.vultur at microchip.com
Sat Dec 1 15:30:22 UTC 2018


The flash_read function is a wrapper over spi_flash_read, which enables
the env to read multiple flash page size from flash until '\0\0' is read
or the end of env partition is reached. Instead of reading the entire env
size. When it reads '\0\0', it stops reading further the env and assumes
that the rest of env is '\0'.

This is an optimization for large environments that contain few bytes of
environment variables. In this case it doesn't need to read the entire
environment and only few pages.

Signed-off-by: Horatiu Vultur <horatiu.vultur at microchip.com>
---
 env/sf.c | 54 +++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 43 insertions(+), 11 deletions(-)

diff --git a/env/sf.c b/env/sf.c
index 2e3c600..e1e1a94 100644
--- a/env/sf.c
+++ b/env/sf.c
@@ -81,6 +81,39 @@ static int setup_flash_device(void)
 	return 0;
 }
 
+static int is_end(char *addr, int size)
+{
+	/* The end of env variables is marked by '\0\0' */
+	int i = 0;
+
+	for (i = 0; i < size - 1; ++i)
+		if (addr[i] == 0x0 && addr[i + 1] == 0x0)
+			return 1;
+	return 0;
+}
+
+static int flash_read(struct spi_flash *flash, u32 offset, int size, void *buf)
+{
+	u32 addr = 0;
+	u32 page_size = flash->page_size;
+
+	memset(buf, 0x0, size);
+	for (int i = 0; i < size / page_size; ++i) {
+		int ret = spi_flash_read(flash, offset, page_size,
+					 &((char *)buf)[addr]);
+
+		if (ret < 0)
+			return ret;
+
+		if (is_end(&((char *)buf)[addr], page_size))
+			return 0;
+
+		addr += page_size;
+		offset += page_size;
+	}
+	return 0;
+}
+
 #if defined(CONFIG_ENV_OFFSET_REDUND)
 #ifdef CMD_SAVEENV
 static int env_sf_save(void)
@@ -116,8 +149,8 @@ static int env_sf_save(void)
 			ret = -ENOMEM;
 			goto done;
 		}
-		ret = spi_flash_read(env_flash, saved_offset,
-					saved_size, saved_buffer);
+		ret = flash_read(env_flash, saved_offset,
+				 saved_size, saved_buffer);
 		if (ret)
 			goto done;
 	}
@@ -183,10 +216,10 @@ static int env_sf_load(void)
 	if (ret)
 		goto out;
 
-	read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
-				    CONFIG_ENV_SIZE, tmp_env1);
-	read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
-				    CONFIG_ENV_SIZE, tmp_env2);
+	read1_fail = flash_read(env_flash, CONFIG_ENV_OFFSET,
+				CONFIG_ENV_SIZE, tmp_env1);
+	read2_fail = flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
+				CONFIG_ENV_SIZE, tmp_env2);
 
 	ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
 				read2_fail);
@@ -220,8 +253,8 @@ static int env_sf_save(void)
 		if (!saved_buffer)
 			goto done;
 
-		ret = spi_flash_read(env_flash, saved_offset,
-			saved_size, saved_buffer);
+		ret = flash_read(env_flash, saved_offset,
+				 saved_size, saved_buffer);
 		if (ret)
 			goto done;
 	}
@@ -277,10 +310,9 @@ static int env_sf_load(void)
 	if (ret)
 		goto out;
 
-	ret = spi_flash_read(env_flash,
-		CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
+	ret = flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
 	if (ret) {
-		set_default_env("spi_flash_read() failed", 0);
+		set_default_env("flash_read() failed", 0);
 		goto err_read;
 	}
 
-- 
2.7.4



More information about the U-Boot mailing list