diff options
author | Joe Hershberger | 2013-04-08 05:32:52 -0500 |
---|---|---|
committer | Tom Rini | 2013-04-11 14:52:55 -0500 |
commit | 785881f775252940185e10fbb2d5299c9ffa6bce (patch) | |
tree | b945a64033ed2a9eeb87527c81a746a53ba18c2a /common | |
parent | 2b74433f365fa677a60431a80e524b5d8d04e995 (diff) | |
download | u-boot-785881f775252940185e10fbb2d5299c9ffa6bce.tar.gz u-boot-785881f775252940185e10fbb2d5299c9ffa6bce.tar.xz u-boot-785881f775252940185e10fbb2d5299c9ffa6bce.zip |
env: Add redundant env support to UBI env
Allow the user to specify two UBI volumes to use for the environment
Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>
Diffstat (limited to 'common')
-rw-r--r-- | common/env_ubi.c | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/common/env_ubi.c b/common/env_ubi.c index 0c592a6f63..1ed8b02e86 100644 --- a/common/env_ubi.c +++ b/common/env_ubi.c | |||
@@ -47,6 +47,58 @@ int env_init(void) | |||
47 | } | 47 | } |
48 | 48 | ||
49 | #ifdef CONFIG_CMD_SAVEENV | 49 | #ifdef CONFIG_CMD_SAVEENV |
50 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT | ||
51 | static unsigned char env_flags; | ||
52 | |||
53 | int saveenv(void) | ||
54 | { | ||
55 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); | ||
56 | ssize_t len; | ||
57 | char *res; | ||
58 | |||
59 | res = (char *)&env_new->data; | ||
60 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); | ||
61 | if (len < 0) { | ||
62 | error("Cannot export environment: errno = %d\n", errno); | ||
63 | return 1; | ||
64 | } | ||
65 | |||
66 | if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { | ||
67 | printf("\n** Cannot find mtd partition \"%s\"\n", | ||
68 | CONFIG_ENV_UBI_PART); | ||
69 | return 1; | ||
70 | } | ||
71 | |||
72 | env_new->crc = crc32(0, env_new->data, ENV_SIZE); | ||
73 | env_new->flags = ++env_flags; /* increase the serial */ | ||
74 | |||
75 | if (gd->env_valid == 1) { | ||
76 | puts("Writing to redundant UBI... "); | ||
77 | if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND, | ||
78 | (void *)env_new, CONFIG_ENV_SIZE)) { | ||
79 | printf("\n** Unable to write env to %s:%s **\n", | ||
80 | CONFIG_ENV_UBI_PART, | ||
81 | CONFIG_ENV_UBI_VOLUME_REDUND); | ||
82 | return 1; | ||
83 | } | ||
84 | } else { | ||
85 | puts("Writing to UBI... "); | ||
86 | if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, | ||
87 | (void *)env_new, CONFIG_ENV_SIZE)) { | ||
88 | printf("\n** Unable to write env to %s:%s **\n", | ||
89 | CONFIG_ENV_UBI_PART, | ||
90 | CONFIG_ENV_UBI_VOLUME); | ||
91 | return 1; | ||
92 | } | ||
93 | } | ||
94 | |||
95 | puts("done\n"); | ||
96 | |||
97 | gd->env_valid = gd->env_valid == 2 ? 1 : 2; | ||
98 | |||
99 | return 0; | ||
100 | } | ||
101 | #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */ | ||
50 | int saveenv(void) | 102 | int saveenv(void) |
51 | { | 103 | { |
52 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); | 104 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
@@ -78,8 +130,72 @@ int saveenv(void) | |||
78 | puts("done\n"); | 130 | puts("done\n"); |
79 | return 0; | 131 | return 0; |
80 | } | 132 | } |
133 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ | ||
81 | #endif /* CONFIG_CMD_SAVEENV */ | 134 | #endif /* CONFIG_CMD_SAVEENV */ |
82 | 135 | ||
136 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT | ||
137 | void env_relocate_spec(void) | ||
138 | { | ||
139 | ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE); | ||
140 | ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE); | ||
141 | int crc1_ok = 0, crc2_ok = 0; | ||
142 | env_t *ep, *tmp_env1, *tmp_env2; | ||
143 | |||
144 | tmp_env1 = (env_t *)env1_buf; | ||
145 | tmp_env2 = (env_t *)env2_buf; | ||
146 | |||
147 | if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { | ||
148 | printf("\n** Cannot find mtd partition \"%s\"\n", | ||
149 | CONFIG_ENV_UBI_PART); | ||
150 | set_default_env(NULL); | ||
151 | return; | ||
152 | } | ||
153 | |||
154 | if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1, | ||
155 | CONFIG_ENV_SIZE)) { | ||
156 | printf("\n** Unable to read env from %s:%s **\n", | ||
157 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); | ||
158 | } | ||
159 | |||
160 | if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2, | ||
161 | CONFIG_ENV_SIZE)) { | ||
162 | printf("\n** Unable to read redundant env from %s:%s **\n", | ||
163 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND); | ||
164 | } | ||
165 | |||
166 | crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc; | ||
167 | crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc; | ||
168 | |||
169 | if (!crc1_ok && !crc2_ok) { | ||
170 | set_default_env("!bad CRC"); | ||
171 | return; | ||
172 | } else if (crc1_ok && !crc2_ok) { | ||
173 | gd->env_valid = 1; | ||
174 | } else if (!crc1_ok && crc2_ok) { | ||
175 | gd->env_valid = 2; | ||
176 | } else { | ||
177 | /* both ok - check serial */ | ||
178 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) | ||
179 | gd->env_valid = 2; | ||
180 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) | ||
181 | gd->env_valid = 1; | ||
182 | else if (tmp_env1->flags > tmp_env2->flags) | ||
183 | gd->env_valid = 1; | ||
184 | else if (tmp_env2->flags > tmp_env1->flags) | ||
185 | gd->env_valid = 2; | ||
186 | else /* flags are equal - almost impossible */ | ||
187 | gd->env_valid = 1; | ||
188 | } | ||
189 | |||
190 | if (gd->env_valid == 1) | ||
191 | ep = tmp_env1; | ||
192 | else | ||
193 | ep = tmp_env2; | ||
194 | |||
195 | env_flags = ep->flags; | ||
196 | env_import((char *)ep, 0); | ||
197 | } | ||
198 | #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */ | ||
83 | void env_relocate_spec(void) | 199 | void env_relocate_spec(void) |
84 | { | 200 | { |
85 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); | 201 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
@@ -101,3 +217,4 @@ void env_relocate_spec(void) | |||
101 | 217 | ||
102 | env_import(buf, 1); | 218 | env_import(buf, 1); |
103 | } | 219 | } |
220 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ | ||