blob: d28247b34d32463899d4883e2490dc3ccee4a2b5 [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
Joe Hershberger2b744332013-04-08 10:32:51 +000018DECLARE_GLOBAL_DATA_PTR;
19
Joe Hershberger2b744332013-04-08 10:32:51 +000020#ifdef CONFIG_CMD_SAVEENV
Joe Hershberger785881f2013-04-08 10:32:52 +000021#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
Simon Glasse5bce242017-08-03 12:22:01 -060022static int env_ubi_save(void)
Joe Hershberger785881f2013-04-08 10:32:52 +000023{
24 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Marek Vasut7ce15262014-03-05 19:59:50 +010025 int ret;
Joe Hershberger785881f2013-04-08 10:32:52 +000026
Marek Vasut7ce15262014-03-05 19:59:50 +010027 ret = env_export(env_new);
28 if (ret)
29 return ret;
Joe Hershberger785881f2013-04-08 10:32:52 +000030
31 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
32 printf("\n** Cannot find mtd partition \"%s\"\n",
33 CONFIG_ENV_UBI_PART);
34 return 1;
35 }
36
Simon Glass203e94f2017-08-03 12:21:56 -060037 if (gd->env_valid == ENV_VALID) {
Joe Hershberger785881f2013-04-08 10:32:52 +000038 puts("Writing to redundant UBI... ");
39 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
40 (void *)env_new, CONFIG_ENV_SIZE)) {
41 printf("\n** Unable to write env to %s:%s **\n",
42 CONFIG_ENV_UBI_PART,
43 CONFIG_ENV_UBI_VOLUME_REDUND);
44 return 1;
45 }
46 } else {
47 puts("Writing to UBI... ");
48 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
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);
53 return 1;
54 }
55 }
56
57 puts("done\n");
58
Simon Glass203e94f2017-08-03 12:21:56 -060059 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Joe Hershberger785881f2013-04-08 10:32:52 +000060
61 return 0;
62}
63#else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glasse5bce242017-08-03 12:22:01 -060064static int env_ubi_save(void)
Joe Hershberger2b744332013-04-08 10:32:51 +000065{
66 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Marek Vasut7ce15262014-03-05 19:59:50 +010067 int ret;
Joe Hershberger2b744332013-04-08 10:32:51 +000068
Marek Vasut7ce15262014-03-05 19:59:50 +010069 ret = env_export(env_new);
70 if (ret)
71 return ret;
Joe Hershberger2b744332013-04-08 10:32:51 +000072
73 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
74 printf("\n** Cannot find mtd partition \"%s\"\n",
75 CONFIG_ENV_UBI_PART);
76 return 1;
77 }
78
Joe Hershberger2b744332013-04-08 10:32:51 +000079 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
80 CONFIG_ENV_SIZE)) {
81 printf("\n** Unable to write env to %s:%s **\n",
82 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
83 return 1;
84 }
85
86 puts("done\n");
87 return 0;
88}
Joe Hershberger785881f2013-04-08 10:32:52 +000089#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Joe Hershberger2b744332013-04-08 10:32:51 +000090#endif /* CONFIG_CMD_SAVEENV */
91
Joe Hershberger785881f2013-04-08 10:32:52 +000092#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
Simon Glassc5951992017-08-03 12:22:17 -060093static int env_ubi_load(void)
Joe Hershberger785881f2013-04-08 10:32:52 +000094{
95 ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
96 ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
Simon Goldschmidt31f044b2018-01-31 14:47:11 +010097 int read1_fail, read2_fail;
Fiach Antaw9d364af2017-01-25 18:53:12 +100098 env_t *tmp_env1, *tmp_env2;
Joe Hershberger785881f2013-04-08 10:32:52 +000099
Marcin Niestrojc1f51e02016-05-24 14:59:55 +0200100 /*
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
104 * buffer.
105 * We need to clear buffer manually here, so the invalid CRC will
106 * cause setting default environment as expected.
107 */
108 memset(env1_buf, 0x0, CONFIG_ENV_SIZE);
109 memset(env2_buf, 0x0, CONFIG_ENV_SIZE);
110
Joe Hershberger785881f2013-04-08 10:32:52 +0000111 tmp_env1 = (env_t *)env1_buf;
112 tmp_env2 = (env_t *)env2_buf;
113
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);
Simon Glassc5951992017-08-03 12:22:17 -0600118 return -EIO;
Joe Hershberger785881f2013-04-08 10:32:52 +0000119 }
120
Simon Goldschmidt31f044b2018-01-31 14:47:11 +0100121 read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
122 CONFIG_ENV_SIZE);
123 if (read1_fail)
Joe Hershberger785881f2013-04-08 10:32:52 +0000124 printf("\n** Unable to read env from %s:%s **\n",
125 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
Joe Hershberger785881f2013-04-08 10:32:52 +0000126
Simon Goldschmidt31f044b2018-01-31 14:47:11 +0100127 read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND,
128 (void *)tmp_env2, CONFIG_ENV_SIZE);
129 if (read2_fail)
Joe Hershberger785881f2013-04-08 10:32:52 +0000130 printf("\n** Unable to read redundant env from %s:%s **\n",
131 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
Joe Hershberger785881f2013-04-08 10:32:52 +0000132
Simon Goldschmidt31f044b2018-01-31 14:47:11 +0100133 return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
134 read2_fail);
Joe Hershberger785881f2013-04-08 10:32:52 +0000135}
136#else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glassc5951992017-08-03 12:22:17 -0600137static int env_ubi_load(void)
Joe Hershberger2b744332013-04-08 10:32:51 +0000138{
139 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
140
Marcin Niestrojc1f51e02016-05-24 14:59:55 +0200141 /*
142 * In case we have restarted u-boot there is a chance that buffer
143 * contains old environment (from the previous boot).
144 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
145 * buffer.
146 * We need to clear buffer manually here, so the invalid CRC will
147 * cause setting default environment as expected.
148 */
149 memset(buf, 0x0, CONFIG_ENV_SIZE);
150
Joe Hershberger2b744332013-04-08 10:32:51 +0000151 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
152 printf("\n** Cannot find mtd partition \"%s\"\n",
153 CONFIG_ENV_UBI_PART);
154 set_default_env(NULL);
Simon Glassc5951992017-08-03 12:22:17 -0600155 return -EIO;
Joe Hershberger2b744332013-04-08 10:32:51 +0000156 }
157
Kevin Smitha7c06cd2015-10-23 17:51:47 +0000158 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
Joe Hershberger2b744332013-04-08 10:32:51 +0000159 printf("\n** Unable to read env from %s:%s **\n",
160 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
161 set_default_env(NULL);
Simon Glassc5951992017-08-03 12:22:17 -0600162 return -EIO;
Joe Hershberger2b744332013-04-08 10:32:51 +0000163 }
164
Simon Goldschmidt2166ebf2018-01-31 14:47:12 +0100165 return env_import(buf, 1);
Joe Hershberger2b744332013-04-08 10:32:51 +0000166}
Joe Hershberger785881f2013-04-08 10:32:52 +0000167#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glass4415f1d2017-08-03 12:21:58 -0600168
169U_BOOT_ENV_LOCATION(ubi) = {
170 .location = ENVL_UBI,
Simon Glasse5bce242017-08-03 12:22:01 -0600171 .load = env_ubi_load,
172 .save = env_save_ptr(env_ubi_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600173};