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 DECLARE_GLOBAL_DATA_PTR
;
21 #ifdef CONFIG_CMD_SAVEENV
22 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
23 static int env_ubi_save(void)
25 ALLOC_CACHE_ALIGN_BUFFER(env_t
, env_new
, 1);
28 ret
= env_export(env_new
);
32 if (ubi_part(CONFIG_ENV_UBI_PART
, NULL
)) {
33 printf("\n** Cannot find mtd partition \"%s\"\n",
38 if (gd
->env_valid
== ENV_VALID
) {
39 puts("Writing to redundant UBI... ");
40 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND
,
41 (void *)env_new
, CONFIG_ENV_SIZE
)) {
42 printf("\n** Unable to write env to %s:%s **\n",
44 CONFIG_ENV_UBI_VOLUME_REDUND
);
48 puts("Writing to UBI... ");
49 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME
,
50 (void *)env_new
, CONFIG_ENV_SIZE
)) {
51 printf("\n** Unable to write env to %s:%s **\n",
53 CONFIG_ENV_UBI_VOLUME
);
60 gd
->env_valid
= gd
->env_valid
== ENV_REDUND
? ENV_VALID
: ENV_REDUND
;
64 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
65 static int env_ubi_save(void)
67 ALLOC_CACHE_ALIGN_BUFFER(env_t
, env_new
, 1);
70 ret
= env_export(env_new
);
74 if (ubi_part(CONFIG_ENV_UBI_PART
, NULL
)) {
75 printf("\n** Cannot find mtd partition \"%s\"\n",
80 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME
, (void *)env_new
,
82 printf("\n** Unable to write env to %s:%s **\n",
83 CONFIG_ENV_UBI_PART
, CONFIG_ENV_UBI_VOLUME
);
90 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
91 #endif /* CONFIG_CMD_SAVEENV */
93 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
94 static void env_ubi_load(void)
96 ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf
, CONFIG_ENV_SIZE
);
97 ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf
, CONFIG_ENV_SIZE
);
98 env_t
*tmp_env1
, *tmp_env2
;
101 * In case we have restarted u-boot there is a chance that buffer
102 * contains old environment (from the previous boot).
103 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
105 * We need to clear buffer manually here, so the invalid CRC will
106 * cause setting default environment as expected.
108 memset(env1_buf
, 0x0, CONFIG_ENV_SIZE
);
109 memset(env2_buf
, 0x0, CONFIG_ENV_SIZE
);
111 tmp_env1
= (env_t
*)env1_buf
;
112 tmp_env2
= (env_t
*)env2_buf
;
114 if (ubi_part(CONFIG_ENV_UBI_PART
, NULL
)) {
115 printf("\n** Cannot find mtd partition \"%s\"\n",
116 CONFIG_ENV_UBI_PART
);
117 set_default_env(NULL
);
121 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME
, (void *)tmp_env1
,
123 printf("\n** Unable to read env from %s:%s **\n",
124 CONFIG_ENV_UBI_PART
, CONFIG_ENV_UBI_VOLUME
);
127 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND
, (void *)tmp_env2
,
129 printf("\n** Unable to read redundant env from %s:%s **\n",
130 CONFIG_ENV_UBI_PART
, CONFIG_ENV_UBI_VOLUME_REDUND
);
133 env_import_redund((char *)tmp_env1
, (char *)tmp_env2
);
135 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
136 static void env_ubi_load(void)
138 ALLOC_CACHE_ALIGN_BUFFER(char, buf
, CONFIG_ENV_SIZE
);
141 * In case we have restarted u-boot there is a chance that buffer
142 * contains old environment (from the previous boot).
143 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
145 * We need to clear buffer manually here, so the invalid CRC will
146 * cause setting default environment as expected.
148 memset(buf
, 0x0, CONFIG_ENV_SIZE
);
150 if (ubi_part(CONFIG_ENV_UBI_PART
, NULL
)) {
151 printf("\n** Cannot find mtd partition \"%s\"\n",
152 CONFIG_ENV_UBI_PART
);
153 set_default_env(NULL
);
157 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME
, buf
, CONFIG_ENV_SIZE
)) {
158 printf("\n** Unable to read env from %s:%s **\n",
159 CONFIG_ENV_UBI_PART
, CONFIG_ENV_UBI_VOLUME
);
160 set_default_env(NULL
);
166 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
168 U_BOOT_ENV_LOCATION(ubi
) = {
169 .location
= ENVL_UBI
,
170 .load
= env_ubi_load
,
171 .save
= env_save_ptr(env_ubi_save
),