blob: e4b85167ecf71780fd734ee898854460c2cfd4a4 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Joe Hershberger2b744332013-04-08 10:32:51 +00002/*
3 * (c) Copyright 2012 by National Instruments,
4 * Joe Hershberger <joe.hershberger@ni.com>
Joe Hershberger2b744332013-04-08 10:32:51 +00005 */
6
7#include <common.h>
8
9#include <command.h>
10#include <environment.h>
11#include <errno.h>
12#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060013#include <memalign.h>
Joe Hershberger2b744332013-04-08 10:32:51 +000014#include <search.h>
15#include <ubi_uboot.h>
16#undef crc32
17
Hamish Guthrie985186d2019-05-15 15:15:55 +020018#define _QUOTE(x) #x
19#define QUOTE(x) _QUOTE(x)
20
21#if (CONFIG_ENV_UBI_VID_OFFSET == 0)
22 #define UBI_VID_OFFSET NULL
23#else
24 #define UBI_VID_OFFSET QUOTE(CONFIG_ENV_UBI_VID_OFFSET)
25#endif
26
Joe Hershberger2b744332013-04-08 10:32:51 +000027DECLARE_GLOBAL_DATA_PTR;
28
Joe Hershberger2b744332013-04-08 10:32:51 +000029#ifdef CONFIG_CMD_SAVEENV
Joe Hershberger785881f2013-04-08 10:32:52 +000030#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
Simon Glasse5bce242017-08-03 12:22:01 -060031static int env_ubi_save(void)
Joe Hershberger785881f2013-04-08 10:32:52 +000032{
33 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Marek Vasut7ce15262014-03-05 19:59:50 +010034 int ret;
Joe Hershberger785881f2013-04-08 10:32:52 +000035
Marek Vasut7ce15262014-03-05 19:59:50 +010036 ret = env_export(env_new);
37 if (ret)
38 return ret;
Joe Hershberger785881f2013-04-08 10:32:52 +000039
Hamish Guthrie985186d2019-05-15 15:15:55 +020040 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershberger785881f2013-04-08 10:32:52 +000041 printf("\n** Cannot find mtd partition \"%s\"\n",
42 CONFIG_ENV_UBI_PART);
43 return 1;
44 }
45
Simon Glass203e94f2017-08-03 12:21:56 -060046 if (gd->env_valid == ENV_VALID) {
Joe Hershberger785881f2013-04-08 10:32:52 +000047 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",
51 CONFIG_ENV_UBI_PART,
52 CONFIG_ENV_UBI_VOLUME_REDUND);
53 return 1;
54 }
55 } else {
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",
60 CONFIG_ENV_UBI_PART,
61 CONFIG_ENV_UBI_VOLUME);
62 return 1;
63 }
64 }
65
66 puts("done\n");
67
Simon Glass203e94f2017-08-03 12:21:56 -060068 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Joe Hershberger785881f2013-04-08 10:32:52 +000069
70 return 0;
71}
72#else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glasse5bce242017-08-03 12:22:01 -060073static int env_ubi_save(void)
Joe Hershberger2b744332013-04-08 10:32:51 +000074{
75 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Marek Vasut7ce15262014-03-05 19:59:50 +010076 int ret;
Joe Hershberger2b744332013-04-08 10:32:51 +000077
Marek Vasut7ce15262014-03-05 19:59:50 +010078 ret = env_export(env_new);
79 if (ret)
80 return ret;
Joe Hershberger2b744332013-04-08 10:32:51 +000081
Hamish Guthrie985186d2019-05-15 15:15:55 +020082 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershberger2b744332013-04-08 10:32:51 +000083 printf("\n** Cannot find mtd partition \"%s\"\n",
84 CONFIG_ENV_UBI_PART);
85 return 1;
86 }
87
Joe Hershberger2b744332013-04-08 10:32:51 +000088 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
89 CONFIG_ENV_SIZE)) {
90 printf("\n** Unable to write env to %s:%s **\n",
91 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
92 return 1;
93 }
94
95 puts("done\n");
96 return 0;
97}
Joe Hershberger785881f2013-04-08 10:32:52 +000098#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Joe Hershberger2b744332013-04-08 10:32:51 +000099#endif /* CONFIG_CMD_SAVEENV */
100
Joe Hershberger785881f2013-04-08 10:32:52 +0000101#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
Simon Glassc5951992017-08-03 12:22:17 -0600102static int env_ubi_load(void)
Joe Hershberger785881f2013-04-08 10:32:52 +0000103{
104 ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
105 ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
Simon Goldschmidt31f044b2018-01-31 14:47:11 +0100106 int read1_fail, read2_fail;
Fiach Antaw9d364af2017-01-25 18:53:12 +1000107 env_t *tmp_env1, *tmp_env2;
Joe Hershberger785881f2013-04-08 10:32:52 +0000108
Marcin Niestrojc1f51e02016-05-24 14:59:55 +0200109 /*
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
113 * buffer.
114 * We need to clear buffer manually here, so the invalid CRC will
115 * cause setting default environment as expected.
116 */
117 memset(env1_buf, 0x0, CONFIG_ENV_SIZE);
118 memset(env2_buf, 0x0, CONFIG_ENV_SIZE);
119
Joe Hershberger785881f2013-04-08 10:32:52 +0000120 tmp_env1 = (env_t *)env1_buf;
121 tmp_env2 = (env_t *)env2_buf;
122
Hamish Guthrie985186d2019-05-15 15:15:55 +0200123 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershberger785881f2013-04-08 10:32:52 +0000124 printf("\n** Cannot find mtd partition \"%s\"\n",
125 CONFIG_ENV_UBI_PART);
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300126 set_default_env(NULL, 0);
Simon Glassc5951992017-08-03 12:22:17 -0600127 return -EIO;
Joe Hershberger785881f2013-04-08 10:32:52 +0000128 }
129
Simon Goldschmidt31f044b2018-01-31 14:47:11 +0100130 read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
131 CONFIG_ENV_SIZE);
132 if (read1_fail)
Joe Hershberger785881f2013-04-08 10:32:52 +0000133 printf("\n** Unable to read env from %s:%s **\n",
134 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
Joe Hershberger785881f2013-04-08 10:32:52 +0000135
Simon Goldschmidt31f044b2018-01-31 14:47:11 +0100136 read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND,
137 (void *)tmp_env2, CONFIG_ENV_SIZE);
138 if (read2_fail)
Joe Hershberger785881f2013-04-08 10:32:52 +0000139 printf("\n** Unable to read redundant env from %s:%s **\n",
140 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
Joe Hershberger785881f2013-04-08 10:32:52 +0000141
Simon Goldschmidt31f044b2018-01-31 14:47:11 +0100142 return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
143 read2_fail);
Joe Hershberger785881f2013-04-08 10:32:52 +0000144}
145#else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glassc5951992017-08-03 12:22:17 -0600146static int env_ubi_load(void)
Joe Hershberger2b744332013-04-08 10:32:51 +0000147{
148 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
149
Marcin Niestrojc1f51e02016-05-24 14:59:55 +0200150 /*
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
154 * buffer.
155 * We need to clear buffer manually here, so the invalid CRC will
156 * cause setting default environment as expected.
157 */
158 memset(buf, 0x0, CONFIG_ENV_SIZE);
159
Hamish Guthrie985186d2019-05-15 15:15:55 +0200160 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershberger2b744332013-04-08 10:32:51 +0000161 printf("\n** Cannot find mtd partition \"%s\"\n",
162 CONFIG_ENV_UBI_PART);
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300163 set_default_env(NULL, 0);
Simon Glassc5951992017-08-03 12:22:17 -0600164 return -EIO;
Joe Hershberger2b744332013-04-08 10:32:51 +0000165 }
166
Kevin Smitha7c06cd2015-10-23 17:51:47 +0000167 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
Joe Hershberger2b744332013-04-08 10:32:51 +0000168 printf("\n** Unable to read env from %s:%s **\n",
169 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300170 set_default_env(NULL, 0);
Simon Glassc5951992017-08-03 12:22:17 -0600171 return -EIO;
Joe Hershberger2b744332013-04-08 10:32:51 +0000172 }
173
Simon Goldschmidt2166ebf2018-01-31 14:47:12 +0100174 return env_import(buf, 1);
Joe Hershberger2b744332013-04-08 10:32:51 +0000175}
Joe Hershberger785881f2013-04-08 10:32:52 +0000176#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glass4415f1d2017-08-03 12:21:58 -0600177
178U_BOOT_ENV_LOCATION(ubi) = {
179 .location = ENVL_UBI,
Marek Vasut344ca792018-08-21 15:53:33 +0200180 ENV_NAME("UBI")
Simon Glasse5bce242017-08-03 12:22:01 -0600181 .load = env_ubi_load,
182 .save = env_save_ptr(env_ubi_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600183};