1 // SPDX-License-Identifier: GPL-2.0+
3 * (c) Copyright 2012 by National Instruments,
4 * Joe Hershberger <joe.hershberger@ni.com>
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 #ifdef CONFIG_CMD_SAVEENV
31 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
32 static int env_ubi_save(void)
34 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
37 ret = env_export(env_new);
41 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
42 printf("\n** Cannot find mtd partition \"%s\"\n",
47 if (gd->env_valid == ENV_VALID) {
48 puts("Writing to redundant UBI... ");
49 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
50 (void *)env_new, CONFIG_ENV_SIZE)) {
51 printf("\n** Unable to write env to %s:%s **\n",
53 CONFIG_ENV_UBI_VOLUME_REDUND);
57 puts("Writing to UBI... ");
58 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
59 (void *)env_new, CONFIG_ENV_SIZE)) {
60 printf("\n** Unable to write env to %s:%s **\n",
62 CONFIG_ENV_UBI_VOLUME);
69 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
73 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
74 static int env_ubi_save(void)
76 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
79 ret = env_export(env_new);
83 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
84 printf("\n** Cannot find mtd partition \"%s\"\n",
89 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
91 printf("\n** Unable to write env to %s:%s **\n",
92 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
99 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
100 #endif /* CONFIG_CMD_SAVEENV */
102 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
103 static int env_ubi_load(void)
105 ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
106 ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
107 int read1_fail, read2_fail;
108 env_t *tmp_env1, *tmp_env2;
111 * In case we have restarted u-boot there is a chance that buffer
112 * contains old environment (from the previous boot).
113 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
115 * We need to clear buffer manually here, so the invalid CRC will
116 * cause setting default environment as expected.
118 memset(env1_buf, 0x0, CONFIG_ENV_SIZE);
119 memset(env2_buf, 0x0, CONFIG_ENV_SIZE);
121 tmp_env1 = (env_t *)env1_buf;
122 tmp_env2 = (env_t *)env2_buf;
124 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
125 printf("\n** Cannot find mtd partition \"%s\"\n",
126 CONFIG_ENV_UBI_PART);
127 env_set_default(NULL, 0);
131 read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
134 printf("\n** Unable to read env from %s:%s **\n",
135 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
137 read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND,
138 (void *)tmp_env2, CONFIG_ENV_SIZE);
140 printf("\n** Unable to read redundant env from %s:%s **\n",
141 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
143 return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
146 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
147 static int env_ubi_load(void)
149 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
152 * In case we have restarted u-boot there is a chance that buffer
153 * contains old environment (from the previous boot).
154 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
156 * We need to clear buffer manually here, so the invalid CRC will
157 * cause setting default environment as expected.
159 memset(buf, 0x0, CONFIG_ENV_SIZE);
161 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
162 printf("\n** Cannot find mtd partition \"%s\"\n",
163 CONFIG_ENV_UBI_PART);
164 env_set_default(NULL, 0);
168 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
169 printf("\n** Unable to read env from %s:%s **\n",
170 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
171 env_set_default(NULL, 0);
175 return env_import(buf, 1);
177 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
179 U_BOOT_ENV_LOCATION(ubi) = {
180 .location = ENVL_UBI,
182 .load = env_ubi_load,
183 .save = env_save_ptr(env_ubi_save),