1 // SPDX-License-Identifier: GPL-2.0+
3 * (c) Copyright 2012 by National Instruments,
4 * Joe Hershberger <joe.hershberger@ni.com>
10 #include <environment.h>
15 #include <ubi_uboot.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 #ifdef CONFIG_CMD_SAVEENV
21 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
22 static int env_ubi_save(void)
24 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
27 ret = env_export(env_new);
31 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
32 printf("\n** Cannot find mtd partition \"%s\"\n",
37 if (gd->env_valid == ENV_VALID) {
38 puts("Writing to redundant UBI... ");
39 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
40 (void *)env_new, CONFIG_ENV_SIZE)) {
41 printf("\n** Unable to write env to %s:%s **\n",
43 CONFIG_ENV_UBI_VOLUME_REDUND);
47 puts("Writing to UBI... ");
48 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
49 (void *)env_new, CONFIG_ENV_SIZE)) {
50 printf("\n** Unable to write env to %s:%s **\n",
52 CONFIG_ENV_UBI_VOLUME);
59 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
63 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
64 static int env_ubi_save(void)
66 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
69 ret = env_export(env_new);
73 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
74 printf("\n** Cannot find mtd partition \"%s\"\n",
79 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
81 printf("\n** Unable to write env to %s:%s **\n",
82 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
89 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
90 #endif /* CONFIG_CMD_SAVEENV */
92 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
93 static int env_ubi_load(void)
95 ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
96 ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
97 int read1_fail, read2_fail;
98 env_t *tmp_env1, *tmp_env2;
101 * In case we have restarted u-boot there is a chance that buffer
102 * contains old environment (from the previous boot).
103 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
105 * We need to clear buffer manually here, so the invalid CRC will
106 * cause setting default environment as expected.
108 memset(env1_buf, 0x0, CONFIG_ENV_SIZE);
109 memset(env2_buf, 0x0, CONFIG_ENV_SIZE);
111 tmp_env1 = (env_t *)env1_buf;
112 tmp_env2 = (env_t *)env2_buf;
114 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
115 printf("\n** Cannot find mtd partition \"%s\"\n",
116 CONFIG_ENV_UBI_PART);
117 set_default_env(NULL, 0);
121 read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
124 printf("\n** Unable to read env from %s:%s **\n",
125 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
127 read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND,
128 (void *)tmp_env2, CONFIG_ENV_SIZE);
130 printf("\n** Unable to read redundant env from %s:%s **\n",
131 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
133 return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
136 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
137 static int env_ubi_load(void)
139 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
142 * In case we have restarted u-boot there is a chance that buffer
143 * contains old environment (from the previous boot).
144 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
146 * We need to clear buffer manually here, so the invalid CRC will
147 * cause setting default environment as expected.
149 memset(buf, 0x0, CONFIG_ENV_SIZE);
151 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
152 printf("\n** Cannot find mtd partition \"%s\"\n",
153 CONFIG_ENV_UBI_PART);
154 set_default_env(NULL, 0);
158 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
159 printf("\n** Unable to read env from %s:%s **\n",
160 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
161 set_default_env(NULL, 0);
165 return env_import(buf, 1);
167 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
169 U_BOOT_ENV_LOCATION(ubi) = {
170 .location = ENVL_UBI,
172 .load = env_ubi_load,
173 .save = env_save_ptr(env_ubi_save),