2 * (c) Copyright 2012 by National Instruments,
3 * Joe Hershberger <joe.hershberger@ni.com>
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #include <environment.h>
31 #include <ubi_uboot.h>
34 char *env_name_spec = "UBI";
38 DECLARE_GLOBAL_DATA_PTR;
43 gd->env_addr = (ulong)&default_environment[0];
49 #ifdef CONFIG_CMD_SAVEENV
50 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
51 static unsigned char env_flags;
55 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
59 res = (char *)&env_new->data;
60 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
62 error("Cannot export environment: errno = %d\n", errno);
66 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
67 printf("\n** Cannot find mtd partition \"%s\"\n",
72 env_new->crc = crc32(0, env_new->data, ENV_SIZE);
73 env_new->flags = ++env_flags; /* increase the serial */
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",
81 CONFIG_ENV_UBI_VOLUME_REDUND);
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",
90 CONFIG_ENV_UBI_VOLUME);
97 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
101 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
104 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
108 res = (char *)&env_new->data;
109 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
111 error("Cannot export environment: errno = %d\n", errno);
115 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
116 printf("\n** Cannot find mtd partition \"%s\"\n",
117 CONFIG_ENV_UBI_PART);
121 env_new->crc = crc32(0, env_new->data, ENV_SIZE);
123 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
125 printf("\n** Unable to write env to %s:%s **\n",
126 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
133 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
134 #endif /* CONFIG_CMD_SAVEENV */
136 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
137 void env_relocate_spec(void)
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;
144 tmp_env1 = (env_t *)env1_buf;
145 tmp_env2 = (env_t *)env2_buf;
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);
154 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
156 printf("\n** Unable to read env from %s:%s **\n",
157 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
160 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2,
162 printf("\n** Unable to read redundant env from %s:%s **\n",
163 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
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;
169 if (!crc1_ok && !crc2_ok) {
170 set_default_env("!bad CRC");
172 } else if (crc1_ok && !crc2_ok) {
174 } else if (!crc1_ok && crc2_ok) {
177 /* both ok - check serial */
178 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
180 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
182 else if (tmp_env1->flags > tmp_env2->flags)
184 else if (tmp_env2->flags > tmp_env1->flags)
186 else /* flags are equal - almost impossible */
190 if (gd->env_valid == 1)
195 env_flags = ep->flags;
196 env_import((char *)ep, 0);
198 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
199 void env_relocate_spec(void)
201 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
203 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
204 printf("\n** Cannot find mtd partition \"%s\"\n",
205 CONFIG_ENV_UBI_PART);
206 set_default_env(NULL);
210 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)&buf,
212 printf("\n** Unable to read env from %s:%s **\n",
213 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
214 set_default_env(NULL);
220 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */