blob: 445d34fedb893e9b447cb5c23b01567e599ee827 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Joe Hershberger0c5faa82013-04-08 10:32:51 +00002/*
3 * (c) Copyright 2012 by National Instruments,
4 * Joe Hershberger <joe.hershberger@ni.com>
Joe Hershberger0c5faa82013-04-08 10:32:51 +00005 */
6
7#include <common.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06008#include <asm/global_data.h>
Joe Hershberger0c5faa82013-04-08 10:32:51 +00009
10#include <command.h>
Simon Glass97385862019-08-01 09:47:00 -060011#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060012#include <env_internal.h>
Joe Hershberger0c5faa82013-04-08 10:32:51 +000013#include <errno.h>
14#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060015#include <memalign.h>
Joe Hershberger0c5faa82013-04-08 10:32:51 +000016#include <search.h>
17#include <ubi_uboot.h>
18#undef crc32
19
Hamish Guthrie674ab242019-05-15 15:15:55 +020020#define _QUOTE(x) #x
21#define QUOTE(x) _QUOTE(x)
22
23#if (CONFIG_ENV_UBI_VID_OFFSET == 0)
24 #define UBI_VID_OFFSET NULL
25#else
26 #define UBI_VID_OFFSET QUOTE(CONFIG_ENV_UBI_VID_OFFSET)
27#endif
28
Joe Hershberger0c5faa82013-04-08 10:32:51 +000029DECLARE_GLOBAL_DATA_PTR;
30
Patrick Delaunay86d13f62022-12-14 16:51:31 +010031#if CONFIG_SYS_REDUNDAND_ENVIRONMENT
32#define ENV_UBI_VOLUME_REDUND CONFIG_ENV_UBI_VOLUME_REDUND
33#else
34#define ENV_UBI_VOLUME_REDUND "invalid"
35#endif
36
Joe Hershberger0c5faa82013-04-08 10:32:51 +000037#ifdef CONFIG_CMD_SAVEENV
Joe Hershbergerdb14e862013-04-08 10:32:52 +000038#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
Simon Glass082af922017-08-03 12:22:01 -060039static int env_ubi_save(void)
Joe Hershbergerdb14e862013-04-08 10:32:52 +000040{
41 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Marek Vasutd73c1292014-03-05 19:59:50 +010042 int ret;
Joe Hershbergerdb14e862013-04-08 10:32:52 +000043
Marek Vasutd73c1292014-03-05 19:59:50 +010044 ret = env_export(env_new);
45 if (ret)
46 return ret;
Joe Hershbergerdb14e862013-04-08 10:32:52 +000047
Hamish Guthrie674ab242019-05-15 15:15:55 +020048 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershbergerdb14e862013-04-08 10:32:52 +000049 printf("\n** Cannot find mtd partition \"%s\"\n",
50 CONFIG_ENV_UBI_PART);
51 return 1;
52 }
53
Simon Glass4bc2ad22017-08-03 12:21:56 -060054 if (gd->env_valid == ENV_VALID) {
Joe Hershbergerdb14e862013-04-08 10:32:52 +000055 puts("Writing to redundant UBI... ");
56 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
57 (void *)env_new, CONFIG_ENV_SIZE)) {
58 printf("\n** Unable to write env to %s:%s **\n",
59 CONFIG_ENV_UBI_PART,
60 CONFIG_ENV_UBI_VOLUME_REDUND);
61 return 1;
62 }
63 } else {
64 puts("Writing to UBI... ");
65 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
66 (void *)env_new, CONFIG_ENV_SIZE)) {
67 printf("\n** Unable to write env to %s:%s **\n",
68 CONFIG_ENV_UBI_PART,
69 CONFIG_ENV_UBI_VOLUME);
70 return 1;
71 }
72 }
73
74 puts("done\n");
75
Simon Glass4bc2ad22017-08-03 12:21:56 -060076 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Joe Hershbergerdb14e862013-04-08 10:32:52 +000077
78 return 0;
79}
80#else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glass082af922017-08-03 12:22:01 -060081static int env_ubi_save(void)
Joe Hershberger0c5faa82013-04-08 10:32:51 +000082{
83 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Marek Vasutd73c1292014-03-05 19:59:50 +010084 int ret;
Joe Hershberger0c5faa82013-04-08 10:32:51 +000085
Marek Vasutd73c1292014-03-05 19:59:50 +010086 ret = env_export(env_new);
87 if (ret)
88 return ret;
Joe Hershberger0c5faa82013-04-08 10:32:51 +000089
Hamish Guthrie674ab242019-05-15 15:15:55 +020090 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershberger0c5faa82013-04-08 10:32:51 +000091 printf("\n** Cannot find mtd partition \"%s\"\n",
92 CONFIG_ENV_UBI_PART);
93 return 1;
94 }
95
Joe Hershberger0c5faa82013-04-08 10:32:51 +000096 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
97 CONFIG_ENV_SIZE)) {
98 printf("\n** Unable to write env to %s:%s **\n",
99 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
100 return 1;
101 }
102
103 puts("done\n");
104 return 0;
105}
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000106#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000107#endif /* CONFIG_CMD_SAVEENV */
108
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000109#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
Simon Glass99778492017-08-03 12:22:17 -0600110static int env_ubi_load(void)
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000111{
112 ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
113 ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100114 int read1_fail, read2_fail;
Fiach Antaw0a2e1762017-01-25 18:53:12 +1000115 env_t *tmp_env1, *tmp_env2;
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000116
Marcin Niestrojbd041272016-05-24 14:59:55 +0200117 /*
118 * In case we have restarted u-boot there is a chance that buffer
119 * contains old environment (from the previous boot).
120 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
121 * buffer.
122 * We need to clear buffer manually here, so the invalid CRC will
123 * cause setting default environment as expected.
124 */
125 memset(env1_buf, 0x0, CONFIG_ENV_SIZE);
126 memset(env2_buf, 0x0, CONFIG_ENV_SIZE);
127
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000128 tmp_env1 = (env_t *)env1_buf;
129 tmp_env2 = (env_t *)env2_buf;
130
Hamish Guthrie674ab242019-05-15 15:15:55 +0200131 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000132 printf("\n** Cannot find mtd partition \"%s\"\n",
133 CONFIG_ENV_UBI_PART);
Simon Glass97385862019-08-01 09:47:00 -0600134 env_set_default(NULL, 0);
Simon Glass99778492017-08-03 12:22:17 -0600135 return -EIO;
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000136 }
137
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100138 read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
139 CONFIG_ENV_SIZE);
140 if (read1_fail)
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000141 printf("\n** Unable to read env from %s:%s **\n",
142 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000143
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100144 read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND,
145 (void *)tmp_env2, CONFIG_ENV_SIZE);
146 if (read2_fail)
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000147 printf("\n** Unable to read redundant env from %s:%s **\n",
148 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
Simon Glass99778492017-08-03 12:22:17 -0600149
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100150 return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200151 read2_fail, H_EXTERNAL);
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000152}
153#else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glass99778492017-08-03 12:22:17 -0600154static int env_ubi_load(void)
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000155{
156 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
157
Marcin Niestrojbd041272016-05-24 14:59:55 +0200158 /*
159 * In case we have restarted u-boot there is a chance that buffer
160 * contains old environment (from the previous boot).
161 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
162 * buffer.
163 * We need to clear buffer manually here, so the invalid CRC will
164 * cause setting default environment as expected.
165 */
166 memset(buf, 0x0, CONFIG_ENV_SIZE);
167
Hamish Guthrie674ab242019-05-15 15:15:55 +0200168 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000169 printf("\n** Cannot find mtd partition \"%s\"\n",
170 CONFIG_ENV_UBI_PART);
Simon Glass97385862019-08-01 09:47:00 -0600171 env_set_default(NULL, 0);
Simon Glass99778492017-08-03 12:22:17 -0600172 return -EIO;
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000173 }
174
Kevin Smith78d30e52015-10-23 17:51:47 +0000175 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000176 printf("\n** Unable to read env from %s:%s **\n",
177 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
Simon Glass97385862019-08-01 09:47:00 -0600178 env_set_default(NULL, 0);
Simon Glass99778492017-08-03 12:22:17 -0600179 return -EIO;
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000180 }
181
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200182 return env_import(buf, 1, H_EXTERNAL);
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000183}
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000184#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glassc10a88e2017-08-03 12:21:58 -0600185
Patrick Delaunay86d13f62022-12-14 16:51:31 +0100186static int env_ubi_erase(void)
187{
188 ALLOC_CACHE_ALIGN_BUFFER(char, env_buf, CONFIG_ENV_SIZE);
189 int ret = 0;
190
191 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
192 printf("\n** Cannot find mtd partition \"%s\"\n",
193 CONFIG_ENV_UBI_PART);
194 return 1;
195 }
196
197 memset(env_buf, 0x0, CONFIG_ENV_SIZE);
198
199 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
200 (void *)env_buf, CONFIG_ENV_SIZE)) {
201 printf("\n** Unable to erase env to %s:%s **\n",
202 CONFIG_ENV_UBI_PART,
203 CONFIG_ENV_UBI_VOLUME);
204 ret = 1;
205 }
206 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
207 if (ubi_volume_write(ENV_UBI_VOLUME_REDUND,
208 (void *)env_buf, CONFIG_ENV_SIZE)) {
209 printf("\n** Unable to erase env to %s:%s **\n",
210 CONFIG_ENV_UBI_PART,
211 ENV_UBI_VOLUME_REDUND);
212 ret = 1;
213 }
214 }
215
216 return ret;
217}
218
Simon Glassc10a88e2017-08-03 12:21:58 -0600219U_BOOT_ENV_LOCATION(ubi) = {
220 .location = ENVL_UBI,
Marek Vasutadeec302018-08-21 15:53:33 +0200221 ENV_NAME("UBI")
Simon Glass082af922017-08-03 12:22:01 -0600222 .load = env_ubi_load,
223 .save = env_save_ptr(env_ubi_save),
Patrick Delaunay86d13f62022-12-14 16:51:31 +0100224 .erase = ENV_ERASE_PTR(env_ubi_erase),
Simon Glassc10a88e2017-08-03 12:21:58 -0600225};