2 * (c) Copyright 2012 by National Instruments,
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <environment.h>
16 #include <ubi_uboot.h>
19 char *env_name_spec = "UBI";
23 DECLARE_GLOBAL_DATA_PTR;
25 static int env_ubi_init(void)
28 gd->env_addr = (ulong)&default_environment[0];
29 gd->env_valid = ENV_VALID;
34 #ifdef CONFIG_CMD_SAVEENV
35 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
36 static int env_ubi_save(void)
38 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
41 ret = env_export(env_new);
45 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
46 printf("\n** Cannot find mtd partition \"%s\"\n",
51 if (gd->env_valid == ENV_VALID) {
52 puts("Writing to redundant UBI... ");
53 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
54 (void *)env_new, CONFIG_ENV_SIZE)) {
55 printf("\n** Unable to write env to %s:%s **\n",
57 CONFIG_ENV_UBI_VOLUME_REDUND);
61 puts("Writing to UBI... ");
62 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
63 (void *)env_new, CONFIG_ENV_SIZE)) {
64 printf("\n** Unable to write env to %s:%s **\n",
66 CONFIG_ENV_UBI_VOLUME);
73 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
77 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
78 static int env_ubi_save(void)
80 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
83 ret = env_export(env_new);
87 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
88 printf("\n** Cannot find mtd partition \"%s\"\n",
93 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
95 printf("\n** Unable to write env to %s:%s **\n",
96 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
103 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
104 #endif /* CONFIG_CMD_SAVEENV */
106 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
107 static void env_ubi_load(void)
109 ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
110 ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
111 env_t *tmp_env1, *tmp_env2;
114 * In case we have restarted u-boot there is a chance that buffer
115 * contains old environment (from the previous boot).
116 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
118 * We need to clear buffer manually here, so the invalid CRC will
119 * cause setting default environment as expected.
121 memset(env1_buf, 0x0, CONFIG_ENV_SIZE);
122 memset(env2_buf, 0x0, CONFIG_ENV_SIZE);
124 tmp_env1 = (env_t *)env1_buf;
125 tmp_env2 = (env_t *)env2_buf;
127 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
128 printf("\n** Cannot find mtd partition \"%s\"\n",
129 CONFIG_ENV_UBI_PART);
130 set_default_env(NULL);
134 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
136 printf("\n** Unable to read env from %s:%s **\n",
137 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
140 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2,
142 printf("\n** Unable to read redundant env from %s:%s **\n",
143 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
146 env_import_redund((char *)tmp_env1, (char *)tmp_env2);
148 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
149 static void env_ubi_load(void)
151 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
154 * In case we have restarted u-boot there is a chance that buffer
155 * contains old environment (from the previous boot).
156 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
158 * We need to clear buffer manually here, so the invalid CRC will
159 * cause setting default environment as expected.
161 memset(buf, 0x0, CONFIG_ENV_SIZE);
163 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
164 printf("\n** Cannot find mtd partition \"%s\"\n",
165 CONFIG_ENV_UBI_PART);
166 set_default_env(NULL);
170 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
171 printf("\n** Unable to read env from %s:%s **\n",
172 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
173 set_default_env(NULL);
179 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
181 U_BOOT_ENV_LOCATION(ubi) = {
182 .location = ENVL_UBI,
183 .load = env_ubi_load,
184 .save = env_save_ptr(env_ubi_save),
185 .init = env_ubi_init,