blob: eb21c4f38b49324c88ce55a1da416a67fc814dab [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
Joe Hershberger0c5faa82013-04-08 10:32:51 +000031#ifdef CONFIG_CMD_SAVEENV
Joe Hershbergerdb14e862013-04-08 10:32:52 +000032#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
Simon Glass082af922017-08-03 12:22:01 -060033static int env_ubi_save(void)
Joe Hershbergerdb14e862013-04-08 10:32:52 +000034{
35 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Marek Vasutd73c1292014-03-05 19:59:50 +010036 int ret;
Joe Hershbergerdb14e862013-04-08 10:32:52 +000037
Marek Vasutd73c1292014-03-05 19:59:50 +010038 ret = env_export(env_new);
39 if (ret)
40 return ret;
Joe Hershbergerdb14e862013-04-08 10:32:52 +000041
Hamish Guthrie674ab242019-05-15 15:15:55 +020042 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershbergerdb14e862013-04-08 10:32:52 +000043 printf("\n** Cannot find mtd partition \"%s\"\n",
44 CONFIG_ENV_UBI_PART);
45 return 1;
46 }
47
Simon Glass4bc2ad22017-08-03 12:21:56 -060048 if (gd->env_valid == ENV_VALID) {
Joe Hershbergerdb14e862013-04-08 10:32:52 +000049 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",
53 CONFIG_ENV_UBI_PART,
54 CONFIG_ENV_UBI_VOLUME_REDUND);
55 return 1;
56 }
57 } else {
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",
62 CONFIG_ENV_UBI_PART,
63 CONFIG_ENV_UBI_VOLUME);
64 return 1;
65 }
66 }
67
68 puts("done\n");
69
Simon Glass4bc2ad22017-08-03 12:21:56 -060070 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Joe Hershbergerdb14e862013-04-08 10:32:52 +000071
72 return 0;
73}
74#else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glass082af922017-08-03 12:22:01 -060075static int env_ubi_save(void)
Joe Hershberger0c5faa82013-04-08 10:32:51 +000076{
77 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Marek Vasutd73c1292014-03-05 19:59:50 +010078 int ret;
Joe Hershberger0c5faa82013-04-08 10:32:51 +000079
Marek Vasutd73c1292014-03-05 19:59:50 +010080 ret = env_export(env_new);
81 if (ret)
82 return ret;
Joe Hershberger0c5faa82013-04-08 10:32:51 +000083
Hamish Guthrie674ab242019-05-15 15:15:55 +020084 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershberger0c5faa82013-04-08 10:32:51 +000085 printf("\n** Cannot find mtd partition \"%s\"\n",
86 CONFIG_ENV_UBI_PART);
87 return 1;
88 }
89
Joe Hershberger0c5faa82013-04-08 10:32:51 +000090 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
91 CONFIG_ENV_SIZE)) {
92 printf("\n** Unable to write env to %s:%s **\n",
93 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
94 return 1;
95 }
96
97 puts("done\n");
98 return 0;
99}
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000100#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000101#endif /* CONFIG_CMD_SAVEENV */
102
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000103#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
Simon Glass99778492017-08-03 12:22:17 -0600104static int env_ubi_load(void)
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000105{
106 ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
107 ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100108 int read1_fail, read2_fail;
Fiach Antaw0a2e1762017-01-25 18:53:12 +1000109 env_t *tmp_env1, *tmp_env2;
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000110
Marcin Niestrojbd041272016-05-24 14:59:55 +0200111 /*
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
115 * buffer.
116 * We need to clear buffer manually here, so the invalid CRC will
117 * cause setting default environment as expected.
118 */
119 memset(env1_buf, 0x0, CONFIG_ENV_SIZE);
120 memset(env2_buf, 0x0, CONFIG_ENV_SIZE);
121
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000122 tmp_env1 = (env_t *)env1_buf;
123 tmp_env2 = (env_t *)env2_buf;
124
Hamish Guthrie674ab242019-05-15 15:15:55 +0200125 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000126 printf("\n** Cannot find mtd partition \"%s\"\n",
127 CONFIG_ENV_UBI_PART);
Simon Glass97385862019-08-01 09:47:00 -0600128 env_set_default(NULL, 0);
Simon Glass99778492017-08-03 12:22:17 -0600129 return -EIO;
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000130 }
131
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100132 read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
133 CONFIG_ENV_SIZE);
134 if (read1_fail)
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000135 printf("\n** Unable to read env from %s:%s **\n",
136 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000137
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100138 read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND,
139 (void *)tmp_env2, CONFIG_ENV_SIZE);
140 if (read2_fail)
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000141 printf("\n** Unable to read redundant env from %s:%s **\n",
142 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
Simon Glass99778492017-08-03 12:22:17 -0600143
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100144 return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200145 read2_fail, H_EXTERNAL);
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000146}
147#else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glass99778492017-08-03 12:22:17 -0600148static int env_ubi_load(void)
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000149{
150 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
151
Marcin Niestrojbd041272016-05-24 14:59:55 +0200152 /*
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
156 * buffer.
157 * We need to clear buffer manually here, so the invalid CRC will
158 * cause setting default environment as expected.
159 */
160 memset(buf, 0x0, CONFIG_ENV_SIZE);
161
Hamish Guthrie674ab242019-05-15 15:15:55 +0200162 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000163 printf("\n** Cannot find mtd partition \"%s\"\n",
164 CONFIG_ENV_UBI_PART);
Simon Glass97385862019-08-01 09:47:00 -0600165 env_set_default(NULL, 0);
Simon Glass99778492017-08-03 12:22:17 -0600166 return -EIO;
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000167 }
168
Kevin Smith78d30e52015-10-23 17:51:47 +0000169 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000170 printf("\n** Unable to read env from %s:%s **\n",
171 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
Simon Glass97385862019-08-01 09:47:00 -0600172 env_set_default(NULL, 0);
Simon Glass99778492017-08-03 12:22:17 -0600173 return -EIO;
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000174 }
175
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200176 return env_import(buf, 1, H_EXTERNAL);
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000177}
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000178#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glassc10a88e2017-08-03 12:21:58 -0600179
180U_BOOT_ENV_LOCATION(ubi) = {
181 .location = ENVL_UBI,
Marek Vasutadeec302018-08-21 15:53:33 +0200182 ENV_NAME("UBI")
Simon Glass082af922017-08-03 12:22:01 -0600183 .load = env_ubi_load,
184 .save = env_save_ptr(env_ubi_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600185};