1 // SPDX-License-Identifier: GPL-2.0+
3 * (c) Copyright 2012 by National Instruments,
7 #include <asm/global_data.h>
11 #include <env_internal.h>
16 #include <ubi_uboot.h>
20 #define QUOTE(x) _QUOTE(x)
22 #if (CONFIG_ENV_UBI_VID_OFFSET == 0)
23 #define UBI_VID_OFFSET NULL
25 #define UBI_VID_OFFSET QUOTE(CONFIG_ENV_UBI_VID_OFFSET)
28 DECLARE_GLOBAL_DATA_PTR;
30 #if CONFIG_SYS_REDUNDAND_ENVIRONMENT
31 #define ENV_UBI_VOLUME_REDUND CONFIG_ENV_UBI_VOLUME_REDUND
33 #define ENV_UBI_VOLUME_REDUND "invalid"
36 #ifdef CONFIG_CMD_SAVEENV
37 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
38 static int env_ubi_save(void)
40 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
43 ret = env_export(env_new);
47 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
48 printf("\n** Cannot find mtd partition \"%s\"\n",
53 if (gd->env_valid == ENV_VALID) {
54 puts("Writing to redundant UBI... ");
55 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
56 (void *)env_new, 0, CONFIG_ENV_SIZE)) {
57 printf("\n** Unable to write env to %s:%s **\n",
59 CONFIG_ENV_UBI_VOLUME_REDUND);
63 puts("Writing to UBI... ");
64 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
65 (void *)env_new, 0, CONFIG_ENV_SIZE)) {
66 printf("\n** Unable to write env to %s:%s **\n",
68 CONFIG_ENV_UBI_VOLUME);
75 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
79 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
80 static int env_ubi_save(void)
82 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
85 ret = env_export(env_new);
89 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
90 printf("\n** Cannot find mtd partition \"%s\"\n",
95 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new, 0,
97 printf("\n** Unable to write env to %s:%s **\n",
98 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
105 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
106 #endif /* CONFIG_CMD_SAVEENV */
108 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
109 static int env_ubi_load(void)
111 ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
112 ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
113 int read1_fail, read2_fail;
114 env_t *tmp_env1, *tmp_env2;
117 * In case we have restarted u-boot there is a chance that buffer
118 * contains old environment (from the previous boot).
119 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
121 * We need to clear buffer manually here, so the invalid CRC will
122 * cause setting default environment as expected.
124 memset(env1_buf, 0x0, CONFIG_ENV_SIZE);
125 memset(env2_buf, 0x0, CONFIG_ENV_SIZE);
127 tmp_env1 = (env_t *)env1_buf;
128 tmp_env2 = (env_t *)env2_buf;
130 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
131 printf("\n** Cannot find mtd partition \"%s\"\n",
132 CONFIG_ENV_UBI_PART);
133 env_set_default(NULL, 0);
137 read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1, 0,
140 printf("\n** Unable to read env from %s:%s **\n",
141 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
143 read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND,
144 (void *)tmp_env2, 0, CONFIG_ENV_SIZE);
146 printf("\n** Unable to read redundant env from %s:%s **\n",
147 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
149 return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
150 read2_fail, H_EXTERNAL);
152 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
153 static int env_ubi_load(void)
155 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
158 * In case we have restarted u-boot there is a chance that buffer
159 * contains old environment (from the previous boot).
160 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
162 * We need to clear buffer manually here, so the invalid CRC will
163 * cause setting default environment as expected.
165 memset(buf, 0x0, CONFIG_ENV_SIZE);
167 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
168 printf("\n** Cannot find mtd partition \"%s\"\n",
169 CONFIG_ENV_UBI_PART);
170 env_set_default(NULL, 0);
174 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, 0, CONFIG_ENV_SIZE)) {
175 printf("\n** Unable to read env from %s:%s **\n",
176 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
177 env_set_default(NULL, 0);
181 return env_import(buf, 1, H_EXTERNAL);
183 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
185 static int env_ubi_erase(void)
187 ALLOC_CACHE_ALIGN_BUFFER(char, env_buf, CONFIG_ENV_SIZE);
190 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
191 printf("\n** Cannot find mtd partition \"%s\"\n",
192 CONFIG_ENV_UBI_PART);
196 memset(env_buf, 0x0, CONFIG_ENV_SIZE);
198 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
199 (void *)env_buf, 0, CONFIG_ENV_SIZE)) {
200 printf("\n** Unable to erase env to %s:%s **\n",
202 CONFIG_ENV_UBI_VOLUME);
205 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
206 if (ubi_volume_write(ENV_UBI_VOLUME_REDUND,
207 (void *)env_buf, 0, CONFIG_ENV_SIZE)) {
208 printf("\n** Unable to erase env to %s:%s **\n",
210 ENV_UBI_VOLUME_REDUND);
218 U_BOOT_ENV_LOCATION(ubi) = {
219 .location = ENVL_UBI,
221 .load = env_ubi_load,
222 .save = env_save_ptr(env_ubi_save),
223 .erase = ENV_ERASE_PTR(env_ubi_erase),