blob: e4b85167ecf71780fd734ee898854460c2cfd4a4 [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>
8
9#include <command.h>
10#include <environment.h>
11#include <errno.h>
12#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060013#include <memalign.h>
Joe Hershberger0c5faa82013-04-08 10:32:51 +000014#include <search.h>
15#include <ubi_uboot.h>
16#undef crc32
17
Hamish Guthrie674ab242019-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 Hershberger0c5faa82013-04-08 10:32:51 +000027DECLARE_GLOBAL_DATA_PTR;
28
Joe Hershberger0c5faa82013-04-08 10:32:51 +000029#ifdef CONFIG_CMD_SAVEENV
Joe Hershbergerdb14e862013-04-08 10:32:52 +000030#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
Simon Glass082af922017-08-03 12:22:01 -060031static int env_ubi_save(void)
Joe Hershbergerdb14e862013-04-08 10:32:52 +000032{
33 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Marek Vasutd73c1292014-03-05 19:59:50 +010034 int ret;
Joe Hershbergerdb14e862013-04-08 10:32:52 +000035
Marek Vasutd73c1292014-03-05 19:59:50 +010036 ret = env_export(env_new);
37 if (ret)
38 return ret;
Joe Hershbergerdb14e862013-04-08 10:32:52 +000039
Hamish Guthrie674ab242019-05-15 15:15:55 +020040 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershbergerdb14e862013-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 Glass4bc2ad22017-08-03 12:21:56 -060046 if (gd->env_valid == ENV_VALID) {
Joe Hershbergerdb14e862013-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 Glass4bc2ad22017-08-03 12:21:56 -060068 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Joe Hershbergerdb14e862013-04-08 10:32:52 +000069
70 return 0;
71}
72#else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glass082af922017-08-03 12:22:01 -060073static int env_ubi_save(void)
Joe Hershberger0c5faa82013-04-08 10:32:51 +000074{
75 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Marek Vasutd73c1292014-03-05 19:59:50 +010076 int ret;
Joe Hershberger0c5faa82013-04-08 10:32:51 +000077
Marek Vasutd73c1292014-03-05 19:59:50 +010078 ret = env_export(env_new);
79 if (ret)
80 return ret;
Joe Hershberger0c5faa82013-04-08 10:32:51 +000081
Hamish Guthrie674ab242019-05-15 15:15:55 +020082 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershberger0c5faa82013-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 Hershberger0c5faa82013-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 Hershbergerdb14e862013-04-08 10:32:52 +000098#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Joe Hershberger0c5faa82013-04-08 10:32:51 +000099#endif /* CONFIG_CMD_SAVEENV */
100
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000101#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
Simon Glass99778492017-08-03 12:22:17 -0600102static int env_ubi_load(void)
Joe Hershbergerdb14e862013-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 Goldschmidte07096e2018-01-31 14:47:11 +0100106 int read1_fail, read2_fail;
Fiach Antaw0a2e1762017-01-25 18:53:12 +1000107 env_t *tmp_env1, *tmp_env2;
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000108
Marcin Niestrojbd041272016-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 Hershbergerdb14e862013-04-08 10:32:52 +0000120 tmp_env1 = (env_t *)env1_buf;
121 tmp_env2 = (env_t *)env2_buf;
122
Hamish Guthrie674ab242019-05-15 15:15:55 +0200123 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000124 printf("\n** Cannot find mtd partition \"%s\"\n",
125 CONFIG_ENV_UBI_PART);
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300126 set_default_env(NULL, 0);
Simon Glass99778492017-08-03 12:22:17 -0600127 return -EIO;
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000128 }
129
Simon Goldschmidte07096e2018-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 Hershbergerdb14e862013-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 Hershbergerdb14e862013-04-08 10:32:52 +0000135
Simon Goldschmidte07096e2018-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 Hershbergerdb14e862013-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);
Simon Glass99778492017-08-03 12:22:17 -0600141
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100142 return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
143 read2_fail);
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000144}
145#else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glass99778492017-08-03 12:22:17 -0600146static int env_ubi_load(void)
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000147{
148 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
149
Marcin Niestrojbd041272016-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 Guthrie674ab242019-05-15 15:15:55 +0200160 if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000161 printf("\n** Cannot find mtd partition \"%s\"\n",
162 CONFIG_ENV_UBI_PART);
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300163 set_default_env(NULL, 0);
Simon Glass99778492017-08-03 12:22:17 -0600164 return -EIO;
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000165 }
166
Kevin Smith78d30e52015-10-23 17:51:47 +0000167 if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
Joe Hershberger0c5faa82013-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 Levinskyb2bb9bc2018-06-24 19:16:57 +0300170 set_default_env(NULL, 0);
Simon Glass99778492017-08-03 12:22:17 -0600171 return -EIO;
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000172 }
173
Simon Goldschmidt35b4c322018-01-31 14:47:12 +0100174 return env_import(buf, 1);
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000175}
Joe Hershbergerdb14e862013-04-08 10:32:52 +0000176#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glassc10a88e2017-08-03 12:21:58 -0600177
178U_BOOT_ENV_LOCATION(ubi) = {
179 .location = ENVL_UBI,
Marek Vasutadeec302018-08-21 15:53:33 +0200180 ENV_NAME("UBI")
Simon Glass082af922017-08-03 12:22:01 -0600181 .load = env_ubi_load,
182 .save = env_save_ptr(env_ubi_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600183};