Commit 9a9d66f5eff0f443de4c2c6ca3e27771ed14b1b4

Authored by Horatiu Vultur
Committed by Tom Rini
1 parent a38c3af868

env: add spi_flash_read_env function

The spi_flash_read_env 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
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@microchip.com>

Showing 1 changed file with 45 additions and 11 deletions Side-by-side Diff

... ... @@ -81,6 +81,40 @@
81 81 return 0;
82 82 }
83 83  
  84 +static int is_end(const char *addr, size_t size)
  85 +{
  86 + /* The end of env variables is marked by '\0\0' */
  87 + int i = 0;
  88 +
  89 + for (i = 0; i < size - 1; ++i)
  90 + if (addr[i] == 0x0 && addr[i + 1] == 0x0)
  91 + return 1;
  92 + return 0;
  93 +}
  94 +
  95 +static int spi_flash_read_env(struct spi_flash *flash, u32 offset, size_t len,
  96 + void *buf)
  97 +{
  98 + u32 addr = 0;
  99 + u32 page_size = flash->page_size;
  100 +
  101 + memset(buf, 0x0, len);
  102 + for (int i = 0; i < len / page_size; ++i) {
  103 + int ret = spi_flash_read(flash, offset, page_size,
  104 + &((char *)buf)[addr]);
  105 +
  106 + if (ret < 0)
  107 + return ret;
  108 +
  109 + if (is_end(&((char *)buf)[addr], page_size))
  110 + return 0;
  111 +
  112 + addr += page_size;
  113 + offset += page_size;
  114 + }
  115 + return 0;
  116 +}
  117 +
84 118 #if defined(CONFIG_ENV_OFFSET_REDUND)
85 119 #ifdef CMD_SAVEENV
86 120 static int env_sf_save(void)
... ... @@ -116,8 +150,8 @@
116 150 ret = -ENOMEM;
117 151 goto done;
118 152 }
119   - ret = spi_flash_read(env_flash, saved_offset,
120   - saved_size, saved_buffer);
  153 + ret = spi_flash_read_env(env_flash, saved_offset,
  154 + saved_size, saved_buffer);
121 155 if (ret)
122 156 goto done;
123 157 }
... ... @@ -183,10 +217,10 @@
183 217 if (ret)
184 218 goto out;
185 219  
186   - read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
187   - CONFIG_ENV_SIZE, tmp_env1);
188   - read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
189   - CONFIG_ENV_SIZE, tmp_env2);
  220 + read1_fail = spi_flash_read_env(env_flash, CONFIG_ENV_OFFSET,
  221 + CONFIG_ENV_SIZE, tmp_env1);
  222 + read2_fail = spi_flash_read_env(env_flash, CONFIG_ENV_OFFSET_REDUND,
  223 + CONFIG_ENV_SIZE, tmp_env2);
190 224  
191 225 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
192 226 read2_fail);
... ... @@ -220,8 +254,8 @@
220 254 if (!saved_buffer)
221 255 goto done;
222 256  
223   - ret = spi_flash_read(env_flash, saved_offset,
224   - saved_size, saved_buffer);
  257 + ret = spi_flash_read_env(env_flash, saved_offset,
  258 + saved_size, saved_buffer);
225 259 if (ret)
226 260 goto done;
227 261 }
228 262  
... ... @@ -277,10 +311,10 @@
277 311 if (ret)
278 312 goto out;
279 313  
280   - ret = spi_flash_read(env_flash,
281   - CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
  314 + ret = spi_flash_read_env(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
  315 + buf);
282 316 if (ret) {
283   - set_default_env("spi_flash_read() failed", 0);
  317 + set_default_env("spi_flash_read_env() failed", 0);
284 318 goto err_read;
285 319 }
286 320