1 // SPDX-License-Identifier: GPL-2.0+
3 * (c) Copyright 2012 by National Instruments,
4 * Joe Hershberger <joe.hershberger@ni.com>
8 #include <asm/global_data.h>
12 #include <env_internal.h>
17 #include <ubi_uboot.h>
21 #define QUOTE(x) _QUOTE(x)
23 #if (CONFIG_ENV_UBI_VID_OFFSET == 0)
24 #define UBI_VID_OFFSET NULL
26 #define UBI_VID_OFFSET QUOTE(CONFIG_ENV_UBI_VID_OFFSET)
29 DECLARE_GLOBAL_DATA_PTR;
31 #ifdef CONFIG_CMD_SAVEENV
32 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
33 static int env_ubi_save(void)
35 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
38 ret = env_export(env_new);
42 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
43 printf("\n** Cannot find mtd partition \"%s\"\n",
48 if (gd->env_valid == ENV_VALID) {
49 puts("Writing to redundant UBI... ");
50 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
51 (void *)env_new, CONFIG_ENV_SIZE)) {
52 printf("\n** Unable to write env to %s:%s **\n",
54 CONFIG_ENV_UBI_VOLUME_REDUND);
58 puts("Writing to UBI... ");
59 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
60 (void *)env_new, CONFIG_ENV_SIZE)) {
61 printf("\n** Unable to write env to %s:%s **\n",
63 CONFIG_ENV_UBI_VOLUME);
70 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
74 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
75 static int env_ubi_save(void)
77 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
80 ret = env_export(env_new);
84 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
85 printf("\n** Cannot find mtd partition \"%s\"\n",
90 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
92 printf("\n** Unable to write env to %s:%s **\n",
93 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
100 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
101 #endif /* CONFIG_CMD_SAVEENV */
103 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
104 static int env_ubi_load(void)
106 ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
107 ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
108 int read1_fail, read2_fail;
109 env_t *tmp_env1, *tmp_env2;
112 * In case we have restarted u-boot there is a chance that buffer
113 * contains old environment (from the previous boot).
114 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
116 * We need to clear buffer manually here, so the invalid CRC will
117 * cause setting default environment as expected.
119 memset(env1_buf, 0x0, CONFIG_ENV_SIZE);
120 memset(env2_buf, 0x0, CONFIG_ENV_SIZE);
122 tmp_env1 = (env_t *)env1_buf;
123 tmp_env2 = (env_t *)env2_buf;
125 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
126 printf("\n** Cannot find mtd partition \"%s\"\n",
127 CONFIG_ENV_UBI_PART);
128 env_set_default(NULL, 0);
132 read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
135 printf("\n** Unable to read env from %s:%s **\n",
136 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
138 read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND,
139 (void *)tmp_env2, CONFIG_ENV_SIZE);
141 printf("\n** Unable to read redundant env from %s:%s **\n",
142 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
144 return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
145 read2_fail, H_EXTERNAL);
147 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
148 static int env_ubi_load(void)
150 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
153 * In case we have restarted u-boot there is a chance that buffer
154 * contains old environment (from the previous boot).
155 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
157 * We need to clear buffer manually here, so the invalid CRC will
158 * cause setting default environment as expected.
160 memset(buf, 0x0, CONFIG_ENV_SIZE);
162 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
163 printf("\n** Cannot find mtd partition \"%s\"\n",
164 CONFIG_ENV_UBI_PART);
165 env_set_default(NULL, 0);
169 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
170 printf("\n** Unable to read env from %s:%s **\n",
171 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
172 env_set_default(NULL, 0);
176 return env_import(buf, 1, H_EXTERNAL);
178 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
180 U_BOOT_ENV_LOCATION(ubi) = {
181 .location = ENVL_UBI,
183 .load = env_ubi_load,
184 .save = env_save_ptr(env_ubi_save),