blob: 1c4653d4f6ac440e3b2e7b89c9815c26a77e6cd1 [file] [log] [blame]
Joe Hershberger0c5faa82013-04-08 10:32:51 +00001/*
2 * (c) Copyright 2012 by National Instruments,
3 * Joe Hershberger <joe.hershberger@ni.com>
4 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +02005 * SPDX-License-Identifier: GPL-2.0+
Joe Hershberger0c5faa82013-04-08 10:32:51 +00006 */
7
8#include <common.h>
9
10#include <command.h>
11#include <environment.h>
12#include <errno.h>
13#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060014#include <memalign.h>
Joe Hershberger0c5faa82013-04-08 10:32:51 +000015#include <search.h>
16#include <ubi_uboot.h>
17#undef crc32
18
Joe Hershberger0c5faa82013-04-08 10:32:51 +000019DECLARE_GLOBAL_DATA_PTR;
20
Joe Hershberger0c5faa82013-04-08 10:32:51 +000021#ifdef CONFIG_CMD_SAVEENV
Joe Hershbergerdb14e862013-04-08 10:32:52 +000022#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
Simon Glass082af922017-08-03 12:22:01 -060023static int env_ubi_save(void)
Joe Hershbergerdb14e862013-04-08 10:32:52 +000024{
25 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Marek Vasutd73c1292014-03-05 19:59:50 +010026 int ret;
Joe Hershbergerdb14e862013-04-08 10:32:52 +000027
Marek Vasutd73c1292014-03-05 19:59:50 +010028 ret = env_export(env_new);
29 if (ret)
30 return ret;
Joe Hershbergerdb14e862013-04-08 10:32:52 +000031
32 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
33 printf("\n** Cannot find mtd partition \"%s\"\n",
34 CONFIG_ENV_UBI_PART);
35 return 1;
36 }
37
Simon Glass4bc2ad22017-08-03 12:21:56 -060038 if (gd->env_valid == ENV_VALID) {
Joe Hershbergerdb14e862013-04-08 10:32:52 +000039 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",
43 CONFIG_ENV_UBI_PART,
44 CONFIG_ENV_UBI_VOLUME_REDUND);
45 return 1;
46 }
47 } else {
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",
52 CONFIG_ENV_UBI_PART,
53 CONFIG_ENV_UBI_VOLUME);
54 return 1;
55 }
56 }
57
58 puts("done\n");
59
Simon Glass4bc2ad22017-08-03 12:21:56 -060060 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Joe Hershbergerdb14e862013-04-08 10:32:52 +000061
62 return 0;
63}
64#else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glass082af922017-08-03 12:22:01 -060065static int env_ubi_save(void)
Joe Hershberger0c5faa82013-04-08 10:32:51 +000066{
67 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Marek Vasutd73c1292014-03-05 19:59:50 +010068 int ret;
Joe Hershberger0c5faa82013-04-08 10:32:51 +000069
Marek Vasutd73c1292014-03-05 19:59:50 +010070 ret = env_export(env_new);
71 if (ret)
72 return ret;
Joe Hershberger0c5faa82013-04-08 10:32:51 +000073
74 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
75 printf("\n** Cannot find mtd partition \"%s\"\n",
76 CONFIG_ENV_UBI_PART);
77 return 1;
78 }
79
Joe Hershberger0c5faa82013-04-08 10:32:51 +000080 if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
81 CONFIG_ENV_SIZE)) {
82 printf("\n** Unable to write env to %s:%s **\n",
83 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
84 return 1;
85 }
86
87 puts("done\n");
88 return 0;
89}
Joe Hershbergerdb14e862013-04-08 10:32:52 +000090#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Joe Hershberger0c5faa82013-04-08 10:32:51 +000091#endif /* CONFIG_CMD_SAVEENV */
92
Joe Hershbergerdb14e862013-04-08 10:32:52 +000093#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
Simon Glass99778492017-08-03 12:22:17 -060094static int env_ubi_load(void)
Joe Hershbergerdb14e862013-04-08 10:32:52 +000095{
96 ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
97 ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
Fiach Antaw0a2e1762017-01-25 18:53:12 +100098 env_t *tmp_env1, *tmp_env2;
Joe Hershbergerdb14e862013-04-08 10:32:52 +000099
Marcin Niestrojbd041272016-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 Hershbergerdb14e862013-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 Glass99778492017-08-03 12:22:17 -0600118 return -EIO;
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000119 }
120
121 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
122 CONFIG_ENV_SIZE)) {
123 printf("\n** Unable to read env from %s:%s **\n",
124 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
125 }
126
127 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2,
128 CONFIG_ENV_SIZE)) {
129 printf("\n** Unable to read redundant env from %s:%s **\n",
130 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
131 }
132
Fiach Antaw0a2e1762017-01-25 18:53:12 +1000133 env_import_redund((char *)tmp_env1, (char *)tmp_env2);
Simon Glass99778492017-08-03 12:22:17 -0600134
135 return 0;
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000136}
137#else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glass99778492017-08-03 12:22:17 -0600138static int env_ubi_load(void)
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000139{
140 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
141
Marcin Niestrojbd041272016-05-24 14:59:55 +0200142 /*
143 * In case we have restarted u-boot there is a chance that buffer
144 * contains old environment (from the previous boot).
145 * If UBI volume is zero size, ubi_volume_read() doesn't modify the
146 * buffer.
147 * We need to clear buffer manually here, so the invalid CRC will
148 * cause setting default environment as expected.
149 */
150 memset(buf, 0x0, CONFIG_ENV_SIZE);
151
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000152 if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
153 printf("\n** Cannot find mtd partition \"%s\"\n",
154 CONFIG_ENV_UBI_PART);
155 set_default_env(NULL);
Simon Glass99778492017-08-03 12:22:17 -0600156 return -EIO;
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000157 }
158
Kevin Smith78d30e52015-10-23 17:51:47 +0000159 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000160 printf("\n** Unable to read env from %s:%s **\n",
161 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
162 set_default_env(NULL);
Simon Glass99778492017-08-03 12:22:17 -0600163 return -EIO;
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000164 }
165
166 env_import(buf, 1);
Simon Glass99778492017-08-03 12:22:17 -0600167
168 return 0;
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000169}
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000170#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glassc10a88e2017-08-03 12:21:58 -0600171
172U_BOOT_ENV_LOCATION(ubi) = {
173 .location = ENVL_UBI,
Simon Glass082af922017-08-03 12:22:01 -0600174 .load = env_ubi_load,
175 .save = env_save_ptr(env_ubi_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600176};