1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * (c) Copyright 2012 by National Instruments,
3*4882a593Smuzhiyun * Joe Hershberger <joe.hershberger@ni.com>
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <common.h>
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <command.h>
11*4882a593Smuzhiyun #include <environment.h>
12*4882a593Smuzhiyun #include <errno.h>
13*4882a593Smuzhiyun #include <malloc.h>
14*4882a593Smuzhiyun #include <memalign.h>
15*4882a593Smuzhiyun #include <search.h>
16*4882a593Smuzhiyun #include <ubi_uboot.h>
17*4882a593Smuzhiyun #undef crc32
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #ifdef CONFIG_CMD_SAVEENV
22*4882a593Smuzhiyun #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
env_ubi_save(void)23*4882a593Smuzhiyun static int env_ubi_save(void)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
26*4882a593Smuzhiyun int ret;
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun ret = env_export(env_new);
29*4882a593Smuzhiyun if (ret)
30*4882a593Smuzhiyun return ret;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
33*4882a593Smuzhiyun printf("\n** Cannot find mtd partition \"%s\"\n",
34*4882a593Smuzhiyun CONFIG_ENV_UBI_PART);
35*4882a593Smuzhiyun return 1;
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun if (gd->env_valid == ENV_VALID) {
39*4882a593Smuzhiyun puts("Writing to redundant UBI... ");
40*4882a593Smuzhiyun if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
41*4882a593Smuzhiyun (void *)env_new, CONFIG_ENV_SIZE)) {
42*4882a593Smuzhiyun printf("\n** Unable to write env to %s:%s **\n",
43*4882a593Smuzhiyun CONFIG_ENV_UBI_PART,
44*4882a593Smuzhiyun CONFIG_ENV_UBI_VOLUME_REDUND);
45*4882a593Smuzhiyun return 1;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun } else {
48*4882a593Smuzhiyun puts("Writing to UBI... ");
49*4882a593Smuzhiyun if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
50*4882a593Smuzhiyun (void *)env_new, CONFIG_ENV_SIZE)) {
51*4882a593Smuzhiyun printf("\n** Unable to write env to %s:%s **\n",
52*4882a593Smuzhiyun CONFIG_ENV_UBI_PART,
53*4882a593Smuzhiyun CONFIG_ENV_UBI_VOLUME);
54*4882a593Smuzhiyun return 1;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun puts("done\n");
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun return 0;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
env_ubi_save(void)65*4882a593Smuzhiyun static int env_ubi_save(void)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
68*4882a593Smuzhiyun int ret;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun ret = env_export(env_new);
71*4882a593Smuzhiyun if (ret)
72*4882a593Smuzhiyun return ret;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
75*4882a593Smuzhiyun printf("\n** Cannot find mtd partition \"%s\"\n",
76*4882a593Smuzhiyun CONFIG_ENV_UBI_PART);
77*4882a593Smuzhiyun return 1;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
81*4882a593Smuzhiyun CONFIG_ENV_SIZE)) {
82*4882a593Smuzhiyun printf("\n** Unable to write env to %s:%s **\n",
83*4882a593Smuzhiyun CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
84*4882a593Smuzhiyun return 1;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun puts("done\n");
88*4882a593Smuzhiyun return 0;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
91*4882a593Smuzhiyun #endif /* CONFIG_CMD_SAVEENV */
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
env_ubi_load(void)94*4882a593Smuzhiyun static int env_ubi_load(void)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
97*4882a593Smuzhiyun ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
98*4882a593Smuzhiyun env_t *tmp_env1, *tmp_env2;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /*
101*4882a593Smuzhiyun * In case we have restarted u-boot there is a chance that buffer
102*4882a593Smuzhiyun * contains old environment (from the previous boot).
103*4882a593Smuzhiyun * If UBI volume is zero size, ubi_volume_read() doesn't modify the
104*4882a593Smuzhiyun * buffer.
105*4882a593Smuzhiyun * We need to clear buffer manually here, so the invalid CRC will
106*4882a593Smuzhiyun * cause setting default environment as expected.
107*4882a593Smuzhiyun */
108*4882a593Smuzhiyun memset(env1_buf, 0x0, CONFIG_ENV_SIZE);
109*4882a593Smuzhiyun memset(env2_buf, 0x0, CONFIG_ENV_SIZE);
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun tmp_env1 = (env_t *)env1_buf;
112*4882a593Smuzhiyun tmp_env2 = (env_t *)env2_buf;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
115*4882a593Smuzhiyun printf("\n** Cannot find mtd partition \"%s\"\n",
116*4882a593Smuzhiyun CONFIG_ENV_UBI_PART);
117*4882a593Smuzhiyun set_default_env(NULL);
118*4882a593Smuzhiyun return -EIO;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
122*4882a593Smuzhiyun CONFIG_ENV_SIZE)) {
123*4882a593Smuzhiyun printf("\n** Unable to read env from %s:%s **\n",
124*4882a593Smuzhiyun CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2,
128*4882a593Smuzhiyun CONFIG_ENV_SIZE)) {
129*4882a593Smuzhiyun printf("\n** Unable to read redundant env from %s:%s **\n",
130*4882a593Smuzhiyun CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun env_import_redund((char *)tmp_env1, (char *)tmp_env2);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun return 0;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
env_ubi_load(void)138*4882a593Smuzhiyun static int env_ubi_load(void)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /*
143*4882a593Smuzhiyun * In case we have restarted u-boot there is a chance that buffer
144*4882a593Smuzhiyun * contains old environment (from the previous boot).
145*4882a593Smuzhiyun * If UBI volume is zero size, ubi_volume_read() doesn't modify the
146*4882a593Smuzhiyun * buffer.
147*4882a593Smuzhiyun * We need to clear buffer manually here, so the invalid CRC will
148*4882a593Smuzhiyun * cause setting default environment as expected.
149*4882a593Smuzhiyun */
150*4882a593Smuzhiyun memset(buf, 0x0, CONFIG_ENV_SIZE);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
153*4882a593Smuzhiyun printf("\n** Cannot find mtd partition \"%s\"\n",
154*4882a593Smuzhiyun CONFIG_ENV_UBI_PART);
155*4882a593Smuzhiyun set_default_env(NULL);
156*4882a593Smuzhiyun return -EIO;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
160*4882a593Smuzhiyun printf("\n** Unable to read env from %s:%s **\n",
161*4882a593Smuzhiyun CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
162*4882a593Smuzhiyun set_default_env(NULL);
163*4882a593Smuzhiyun return -EIO;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun env_import(buf, 1);
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun return 0;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun U_BOOT_ENV_LOCATION(ubi) = {
173*4882a593Smuzhiyun .location = ENVL_UBI,
174*4882a593Smuzhiyun .load = env_ubi_load,
175*4882a593Smuzhiyun .save = env_save_ptr(env_ubi_save),
176*4882a593Smuzhiyun };
177