Commit 5b3375ac8c36c29c87abb132fede0509eb21e5c9
Committed by
Wolfgang Denk
1 parent
ecf5f077c8
Exists in
master
and in
54 other branches
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: Mike Frysinger <vapier@gentoo.org> Acked-by: Haavard Skinnemoen <haavard.skinnemoen@atmel.com>
Showing 1 changed file with 36 additions and 5 deletions Side-by-side Diff
common/env_sf.c
... | ... | @@ -27,6 +27,7 @@ |
27 | 27 | */ |
28 | 28 | #include <common.h> |
29 | 29 | #include <environment.h> |
30 | +#include <malloc.h> | |
30 | 31 | #include <spi_flash.h> |
31 | 32 | |
32 | 33 | #ifndef CONFIG_ENV_SPI_BUS |
33 | 34 | |
34 | 35 | |
... | ... | @@ -60,13 +61,30 @@ |
60 | 61 | |
61 | 62 | int saveenv(void) |
62 | 63 | { |
64 | + u32 saved_size, saved_offset; | |
65 | + char *saved_buffer = NULL; | |
63 | 66 | u32 sector = 1; |
67 | + int ret; | |
64 | 68 | |
65 | 69 | if (!env_flash) { |
66 | 70 | puts("Environment SPI flash not initialized\n"); |
67 | 71 | return 1; |
68 | 72 | } |
69 | 73 | |
74 | + /* Is the sector larger than the env (i.e. embedded) */ | |
75 | + if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
76 | + saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; | |
77 | + saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; | |
78 | + saved_buffer = malloc(saved_size); | |
79 | + if (!saved_buffer) { | |
80 | + ret = 1; | |
81 | + goto done; | |
82 | + } | |
83 | + ret = spi_flash_read(env_flash, saved_offset, saved_size, saved_buffer); | |
84 | + if (ret) | |
85 | + goto done; | |
86 | + } | |
87 | + | |
70 | 88 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
71 | 89 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; |
72 | 90 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) |
73 | 91 | |
74 | 92 | |
75 | 93 | |
... | ... | @@ -74,15 +92,28 @@ |
74 | 92 | } |
75 | 93 | |
76 | 94 | puts("Erasing SPI flash..."); |
77 | - if (spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, sector * CONFIG_ENV_SECT_SIZE)) | |
78 | - return 1; | |
95 | + ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, sector * CONFIG_ENV_SECT_SIZE); | |
96 | + if (ret) | |
97 | + goto done; | |
79 | 98 | |
80 | 99 | puts("Writing to SPI flash..."); |
81 | - if (spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr)) | |
82 | - return 1; | |
100 | + ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr); | |
101 | + if (ret) | |
102 | + goto done; | |
83 | 103 | |
104 | + if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
105 | + ret = spi_flash_write(env_flash, saved_offset, saved_size, saved_buffer); | |
106 | + if (ret) | |
107 | + goto done; | |
108 | + } | |
109 | + | |
110 | + ret = 0; | |
84 | 111 | puts("done\n"); |
85 | - return 0; | |
112 | + | |
113 | + done: | |
114 | + if (saved_buffer) | |
115 | + free(saved_buffer); | |
116 | + return ret; | |
86 | 117 | } |
87 | 118 | |
88 | 119 | void env_relocate_spec(void) |