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>
16 #include <ubi_uboot.h>
19 char *env_name_spec = "UBI";
23 DECLARE_GLOBAL_DATA_PTR;
25 #ifdef CONFIG_CMD_SAVEENV
26 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
27 static int env_ubi_save(void)
29 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
32 ret = env_export(env_new);
36 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
37 printf("\n** Cannot find mtd partition \"%s\"\n",
42 if (gd->env_valid == ENV_VALID) {
43 puts("Writing to redundant UBI... ");
44 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
45 (void *)env_new, CONFIG_ENV_SIZE)) {
46 printf("\n** Unable to write env to %s:%s **\n",
48 CONFIG_ENV_UBI_VOLUME_REDUND);
52 puts("Writing to UBI... ");
53 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
54 (void *)env_new, CONFIG_ENV_SIZE)) {
55 printf("\n** Unable to write env to %s:%s **\n",
57 CONFIG_ENV_UBI_VOLUME);
64 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
68 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
69 static int env_ubi_save(void)
71 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
74 ret = env_export(env_new);
78 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
79 printf("\n** Cannot find mtd partition \"%s\"\n",
84 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
86 printf("\n** Unable to write env to %s:%s **\n",
87 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
94 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
95 #endif /* CONFIG_CMD_SAVEENV */
97 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
98 static void env_ubi_load(void)
100 ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
101 ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
102 env_t *tmp_env1, *tmp_env2;
105 * In case we have restarted u-boot there is a chance that buffer
106 * contains old environment (from the previous boot).
107 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
109 * We need to clear buffer manually here, so the invalid CRC will
110 * cause setting default environment as expected.
112 memset(env1_buf, 0x0, CONFIG_ENV_SIZE);
113 memset(env2_buf, 0x0, CONFIG_ENV_SIZE);
115 tmp_env1 = (env_t *)env1_buf;
116 tmp_env2 = (env_t *)env2_buf;
118 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
119 printf("\n** Cannot find mtd partition \"%s\"\n",
120 CONFIG_ENV_UBI_PART);
121 set_default_env(NULL);
125 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
127 printf("\n** Unable to read env from %s:%s **\n",
128 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
131 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2,
133 printf("\n** Unable to read redundant env from %s:%s **\n",
134 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
137 env_import_redund((char *)tmp_env1, (char *)tmp_env2);
139 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
140 static void env_ubi_load(void)
142 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
145 * In case we have restarted u-boot there is a chance that buffer
146 * contains old environment (from the previous boot).
147 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
149 * We need to clear buffer manually here, so the invalid CRC will
150 * cause setting default environment as expected.
152 memset(buf, 0x0, CONFIG_ENV_SIZE);
154 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
155 printf("\n** Cannot find mtd partition \"%s\"\n",
156 CONFIG_ENV_UBI_PART);
157 set_default_env(NULL);
161 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
162 printf("\n** Unable to read env from %s:%s **\n",
163 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
164 set_default_env(NULL);
170 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
172 U_BOOT_ENV_LOCATION(ubi) = {
173 .location = ENVL_UBI,
174 .load = env_ubi_load,
175 .save = env_save_ptr(env_ubi_save),