2 * (c) Copyright 2012 by National Instruments,
3 * Joe Hershberger <joe.hershberger@ni.com>
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <environment.h>
15 #include <ubi_uboot.h>
18 char *env_name_spec = "UBI";
22 DECLARE_GLOBAL_DATA_PTR;
27 gd->env_addr = (ulong)&default_environment[0];
33 #ifdef CONFIG_CMD_SAVEENV
34 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
35 static unsigned char env_flags;
39 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
42 ret = env_export(env_new);
46 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
47 printf("\n** Cannot find mtd partition \"%s\"\n",
52 env_new->flags = ++env_flags; /* increase the serial */
54 if (gd->env_valid == 1) {
55 puts("Writing to redundant UBI... ");
56 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
57 (void *)env_new, CONFIG_ENV_SIZE)) {
58 printf("\n** Unable to write env to %s:%s **\n",
60 CONFIG_ENV_UBI_VOLUME_REDUND);
64 puts("Writing to UBI... ");
65 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
66 (void *)env_new, CONFIG_ENV_SIZE)) {
67 printf("\n** Unable to write env to %s:%s **\n",
69 CONFIG_ENV_UBI_VOLUME);
76 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
80 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
83 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
86 ret = env_export(env_new);
90 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
91 printf("\n** Cannot find mtd partition \"%s\"\n",
96 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
98 printf("\n** Unable to write env to %s:%s **\n",
99 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
106 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
107 #endif /* CONFIG_CMD_SAVEENV */
109 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
110 void env_relocate_spec(void)
112 ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
113 ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
114 int crc1_ok = 0, crc2_ok = 0;
115 env_t *ep, *tmp_env1, *tmp_env2;
117 tmp_env1 = (env_t *)env1_buf;
118 tmp_env2 = (env_t *)env2_buf;
120 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
121 printf("\n** Cannot find mtd partition \"%s\"\n",
122 CONFIG_ENV_UBI_PART);
123 set_default_env(NULL);
127 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
129 printf("\n** Unable to read env from %s:%s **\n",
130 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
133 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2,
135 printf("\n** Unable to read redundant env from %s:%s **\n",
136 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
139 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
140 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
142 if (!crc1_ok && !crc2_ok) {
143 set_default_env("!bad CRC");
145 } else if (crc1_ok && !crc2_ok) {
147 } else if (!crc1_ok && crc2_ok) {
150 /* both ok - check serial */
151 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
153 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
155 else if (tmp_env1->flags > tmp_env2->flags)
157 else if (tmp_env2->flags > tmp_env1->flags)
159 else /* flags are equal - almost impossible */
163 if (gd->env_valid == 1)
168 env_flags = ep->flags;
169 env_import((char *)ep, 0);
171 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
172 void env_relocate_spec(void)
174 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
176 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
177 printf("\n** Cannot find mtd partition \"%s\"\n",
178 CONFIG_ENV_UBI_PART);
179 set_default_env(NULL);
183 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)&buf,
185 printf("\n** Unable to read env from %s:%s **\n",
186 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
187 set_default_env(NULL);
193 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */