Skip to content
Snippets Groups Projects
Commit 5b3375ac authored by Mike Frysinger's avatar Mike Frysinger Committed by Wolfgang Denk
Browse files

env_sf: support embedded environments


If both CONFIG_ENV_SECT_SIZE and CONFIG_ENV_SIZE are defined, and the sect
size is larger than the env size, then it means the env is embedded in a
block.  So we have to save/restore the part of the sector which is not the
environment.  Previously, saving the environment in SPI flash in this
setup would probably brick the board as the rest of the sector tends to
contain actual U-Boot data/code.

Signed-off-by: default avatarMike Frysinger <vapier@gentoo.org>
Acked-by: default avatarHaavard Skinnemoen <haavard.skinnemoen@atmel.com>
parent ecf5f077
No related branches found
No related tags found
No related merge requests found
......@@ -27,6 +27,7 @@
*/
#include <common.h>
#include <environment.h>
#include <malloc.h>
#include <spi_flash.h>
#ifndef CONFIG_ENV_SPI_BUS
......@@ -60,13 +61,30 @@ uchar env_get_char_spec(int index)
int saveenv(void)
{
u32 saved_size, saved_offset;
char *saved_buffer = NULL;
u32 sector = 1;
int ret;
if (!env_flash) {
puts("Environment SPI flash not initialized\n");
return 1;
}
/* Is the sector larger than the env (i.e. embedded) */
if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
saved_buffer = malloc(saved_size);
if (!saved_buffer) {
ret = 1;
goto done;
}
ret = spi_flash_read(env_flash, saved_offset, saved_size, saved_buffer);
if (ret)
goto done;
}
if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
......@@ -74,15 +92,28 @@ int saveenv(void)
}
puts("Erasing SPI flash...");
if (spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, sector * CONFIG_ENV_SECT_SIZE))
return 1;
ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, sector * CONFIG_ENV_SECT_SIZE);
if (ret)
goto done;
puts("Writing to SPI flash...");
if (spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr))
return 1;
ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr);
if (ret)
goto done;
if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
ret = spi_flash_write(env_flash, saved_offset, saved_size, saved_buffer);
if (ret)
goto done;
}
ret = 0;
puts("done\n");
return 0;
done:
if (saved_buffer)
free(saved_buffer);
return ret;
}
void env_relocate_spec(void)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment