1 // SPDX-License-Identifier: GPL-2.0+
3 * (c) Copyright 2012 by National Instruments,
4 * Joe Hershberger <joe.hershberger@ni.com>
10 #include <environment.h>
15 #include <ubi_uboot.h>
19 #define QUOTE(x) _QUOTE(x)
21 #if (CONFIG_ENV_UBI_VID_OFFSET == 0)
22 #define UBI_VID_OFFSET NULL
24 #define UBI_VID_OFFSET QUOTE(CONFIG_ENV_UBI_VID_OFFSET)
27 DECLARE_GLOBAL_DATA_PTR;
29 #ifdef CONFIG_CMD_SAVEENV
30 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
31 static int env_ubi_save(void)
33 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
36 ret = env_export(env_new);
40 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
41 printf("\n** Cannot find mtd partition \"%s\"\n",
46 if (gd->env_valid == ENV_VALID) {
47 puts("Writing to redundant UBI... ");
48 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
49 (void *)env_new, CONFIG_ENV_SIZE)) {
50 printf("\n** Unable to write env to %s:%s **\n",
52 CONFIG_ENV_UBI_VOLUME_REDUND);
56 puts("Writing to UBI... ");
57 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
58 (void *)env_new, CONFIG_ENV_SIZE)) {
59 printf("\n** Unable to write env to %s:%s **\n",
61 CONFIG_ENV_UBI_VOLUME);
68 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
72 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
73 static int env_ubi_save(void)
75 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
78 ret = env_export(env_new);
82 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
83 printf("\n** Cannot find mtd partition \"%s\"\n",
88 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
90 printf("\n** Unable to write env to %s:%s **\n",
91 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
98 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
99 #endif /* CONFIG_CMD_SAVEENV */
101 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
102 static int env_ubi_load(void)
104 ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
105 ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
106 int read1_fail, read2_fail;
107 env_t *tmp_env1, *tmp_env2;
110 * In case we have restarted u-boot there is a chance that buffer
111 * contains old environment (from the previous boot).
112 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
114 * We need to clear buffer manually here, so the invalid CRC will
115 * cause setting default environment as expected.
117 memset(env1_buf, 0x0, CONFIG_ENV_SIZE);
118 memset(env2_buf, 0x0, CONFIG_ENV_SIZE);
120 tmp_env1 = (env_t *)env1_buf;
121 tmp_env2 = (env_t *)env2_buf;
123 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
124 printf("\n** Cannot find mtd partition \"%s\"\n",
125 CONFIG_ENV_UBI_PART);
126 set_default_env(NULL, 0);
130 read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
133 printf("\n** Unable to read env from %s:%s **\n",
134 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
136 read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND,
137 (void *)tmp_env2, CONFIG_ENV_SIZE);
139 printf("\n** Unable to read redundant env from %s:%s **\n",
140 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
142 return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
145 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
146 static int env_ubi_load(void)
148 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
151 * In case we have restarted u-boot there is a chance that buffer
152 * contains old environment (from the previous boot).
153 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
155 * We need to clear buffer manually here, so the invalid CRC will
156 * cause setting default environment as expected.
158 memset(buf, 0x0, CONFIG_ENV_SIZE);
160 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
161 printf("\n** Cannot find mtd partition \"%s\"\n",
162 CONFIG_ENV_UBI_PART);
163 set_default_env(NULL, 0);
167 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
168 printf("\n** Unable to read env from %s:%s **\n",
169 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
170 set_default_env(NULL, 0);
174 return env_import(buf, 1);
176 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
178 U_BOOT_ENV_LOCATION(ubi) = {
179 .location = ENVL_UBI,
181 .load = env_ubi_load,
182 .save = env_save_ptr(env_ubi_save),