9f3dc635cf1f0804d93f5ce0ec8748e00af92340
[oweals/u-boot.git] / env / nand.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2010
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  *
6  * (C) Copyright 2008
7  * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
8  *
9  * (C) Copyright 2004
10  * Jian Zhang, Texas Instruments, jzhang@ti.com.
11  *
12  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
13  * Andreas Heppel <aheppel@sysgo.de>
14  */
15
16 #include <common.h>
17 #include <command.h>
18 #include <env.h>
19 #include <env_internal.h>
20 #include <linux/stddef.h>
21 #include <malloc.h>
22 #include <memalign.h>
23 #include <nand.h>
24 #include <search.h>
25 #include <errno.h>
26
27 #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND) && \
28                 !defined(CONFIG_SPL_BUILD)
29 #define CMD_SAVEENV
30 #elif defined(CONFIG_ENV_OFFSET_REDUND) && !defined(CONFIG_SPL_BUILD)
31 #error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
32 #endif
33
34 #if defined(CONFIG_ENV_SIZE_REDUND) &&  \
35         (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
36 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
37 #endif
38
39 #ifndef CONFIG_ENV_RANGE
40 #define CONFIG_ENV_RANGE        CONFIG_ENV_SIZE
41 #endif
42
43 #if defined(ENV_IS_EMBEDDED)
44 static env_t *env_ptr = &environment;
45 #elif defined(CONFIG_NAND_ENV_DST)
46 static env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
47 #endif /* ENV_IS_EMBEDDED */
48
49 DECLARE_GLOBAL_DATA_PTR;
50
51 /*
52  * This is called before nand_init() so we can't read NAND to
53  * validate env data.
54  *
55  * Mark it OK for now. env_relocate() in env_common.c will call our
56  * relocate function which does the real validation.
57  *
58  * When using a NAND boot image (like sequoia_nand), the environment
59  * can be embedded or attached to the U-Boot image in NAND flash.
60  * This way the SPL loads not only the U-Boot image from NAND but
61  * also the environment.
62  */
63 static int env_nand_init(void)
64 {
65 #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
66         int crc1_ok = 0, crc2_ok = 0;
67         env_t *tmp_env1;
68
69 #ifdef CONFIG_ENV_OFFSET_REDUND
70         env_t *tmp_env2;
71
72         tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
73         crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
74 #endif
75         tmp_env1 = env_ptr;
76         crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
77
78         if (!crc1_ok && !crc2_ok) {
79                 gd->env_addr    = 0;
80                 gd->env_valid   = ENV_INVALID;
81
82                 return 0;
83         } else if (crc1_ok && !crc2_ok) {
84                 gd->env_valid = ENV_VALID;
85         }
86 #ifdef CONFIG_ENV_OFFSET_REDUND
87         else if (!crc1_ok && crc2_ok) {
88                 gd->env_valid = ENV_REDUND;
89         } else {
90                 /* both ok - check serial */
91                 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
92                         gd->env_valid = ENV_REDUND;
93                 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
94                         gd->env_valid = ENV_VALID;
95                 else if (tmp_env1->flags > tmp_env2->flags)
96                         gd->env_valid = ENV_VALID;
97                 else if (tmp_env2->flags > tmp_env1->flags)
98                         gd->env_valid = ENV_REDUND;
99                 else /* flags are equal - almost impossible */
100                         gd->env_valid = ENV_VALID;
101         }
102
103         if (gd->env_valid == ENV_REDUND)
104                 env_ptr = tmp_env2;
105         else
106 #endif
107         if (gd->env_valid == ENV_VALID)
108                 env_ptr = tmp_env1;
109
110         gd->env_addr = (ulong)env_ptr->data;
111
112 #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
113         gd->env_addr    = (ulong)&default_environment[0];
114         gd->env_valid   = ENV_VALID;
115 #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
116
117         return 0;
118 }
119
120 #ifdef CMD_SAVEENV
121 /*
122  * The legacy NAND code saved the environment in the first NAND device i.e.,
123  * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
124  */
125 static int writeenv(size_t offset, u_char *buf)
126 {
127         size_t end = offset + CONFIG_ENV_RANGE;
128         size_t amount_saved = 0;
129         size_t blocksize, len;
130         struct mtd_info *mtd;
131         u_char *char_ptr;
132
133         mtd = get_nand_dev_by_index(0);
134         if (!mtd)
135                 return 1;
136
137         blocksize = mtd->erasesize;
138         len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
139
140         while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
141                 if (nand_block_isbad(mtd, offset)) {
142                         offset += blocksize;
143                 } else {
144                         char_ptr = &buf[amount_saved];
145                         if (nand_write(mtd, offset, &len, char_ptr))
146                                 return 1;
147
148                         offset += blocksize;
149                         amount_saved += len;
150                 }
151         }
152         if (amount_saved != CONFIG_ENV_SIZE)
153                 return 1;
154
155         return 0;
156 }
157
158 struct nand_env_location {
159         const char *name;
160         const nand_erase_options_t erase_opts;
161 };
162
163 static int erase_and_write_env(const struct nand_env_location *location,
164                 u_char *env_new)
165 {
166         struct mtd_info *mtd;
167         int ret = 0;
168
169         mtd = get_nand_dev_by_index(0);
170         if (!mtd)
171                 return 1;
172
173         printf("Erasing %s...\n", location->name);
174         if (nand_erase_opts(mtd, &location->erase_opts))
175                 return 1;
176
177         printf("Writing to %s... ", location->name);
178         ret = writeenv(location->erase_opts.offset, env_new);
179         puts(ret ? "FAILED!\n" : "OK\n");
180
181         return ret;
182 }
183
184 static int env_nand_save(void)
185 {
186         int     ret = 0;
187         ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
188         int     env_idx = 0;
189         static const struct nand_env_location location[] = {
190                 {
191                         .name = "NAND",
192                         .erase_opts = {
193                                 .length = CONFIG_ENV_RANGE,
194                                 .offset = CONFIG_ENV_OFFSET,
195                         },
196                 },
197 #ifdef CONFIG_ENV_OFFSET_REDUND
198                 {
199                         .name = "redundant NAND",
200                         .erase_opts = {
201                                 .length = CONFIG_ENV_RANGE,
202                                 .offset = CONFIG_ENV_OFFSET_REDUND,
203                         },
204                 },
205 #endif
206         };
207
208
209         if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
210                 return 1;
211
212         ret = env_export(env_new);
213         if (ret)
214                 return ret;
215
216 #ifdef CONFIG_ENV_OFFSET_REDUND
217         env_idx = (gd->env_valid == ENV_VALID);
218 #endif
219
220         ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
221 #ifdef CONFIG_ENV_OFFSET_REDUND
222         if (!ret) {
223                 /* preset other copy for next write */
224                 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID :
225                                 ENV_REDUND;
226                 return ret;
227         }
228
229         env_idx = (env_idx + 1) & 1;
230         ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
231         if (!ret)
232                 printf("Warning: primary env write failed,"
233                                 " redundancy is lost!\n");
234 #endif
235
236         return ret;
237 }
238 #endif /* CMD_SAVEENV */
239
240 #if defined(CONFIG_SPL_BUILD)
241 static int readenv(size_t offset, u_char *buf)
242 {
243         return nand_spl_load_image(offset, CONFIG_ENV_SIZE, buf);
244 }
245 #else
246 static int readenv(size_t offset, u_char *buf)
247 {
248         size_t end = offset + CONFIG_ENV_RANGE;
249         size_t amount_loaded = 0;
250         size_t blocksize, len;
251         struct mtd_info *mtd;
252         u_char *char_ptr;
253
254         mtd = get_nand_dev_by_index(0);
255         if (!mtd)
256                 return 1;
257
258         blocksize = mtd->erasesize;
259         len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
260
261         while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
262                 if (nand_block_isbad(mtd, offset)) {
263                         offset += blocksize;
264                 } else {
265                         char_ptr = &buf[amount_loaded];
266                         if (nand_read_skip_bad(mtd, offset,
267                                                &len, NULL,
268                                                mtd->size, char_ptr))
269                                 return 1;
270
271                         offset += blocksize;
272                         amount_loaded += len;
273                 }
274         }
275
276         if (amount_loaded != CONFIG_ENV_SIZE)
277                 return 1;
278
279         return 0;
280 }
281 #endif /* #if defined(CONFIG_SPL_BUILD) */
282
283 #ifdef CONFIG_ENV_OFFSET_OOB
284 int get_nand_env_oob(struct mtd_info *mtd, unsigned long *result)
285 {
286         struct mtd_oob_ops ops;
287         uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
288         int ret;
289
290         ops.datbuf      = NULL;
291         ops.mode        = MTD_OOB_AUTO;
292         ops.ooboffs     = 0;
293         ops.ooblen      = ENV_OFFSET_SIZE;
294         ops.oobbuf      = (void *)oob_buf;
295
296         ret = mtd->read_oob(mtd, ENV_OFFSET_SIZE, &ops);
297         if (ret) {
298                 printf("error reading OOB block 0\n");
299                 return ret;
300         }
301
302         if (oob_buf[0] == ENV_OOB_MARKER) {
303                 *result = ovoid ob_buf[1] * mtd->erasesize;
304         } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
305                 *result = oob_buf[1];
306         } else {
307                 printf("No dynamic environment marker in OOB block 0\n");
308                 return -ENOENT;
309         }
310
311         return 0;
312 }
313 #endif
314
315 #ifdef CONFIG_ENV_OFFSET_REDUND
316 static int env_nand_load(void)
317 {
318 #if defined(ENV_IS_EMBEDDED)
319         return 0;
320 #else
321         int read1_fail, read2_fail;
322         env_t *tmp_env1, *tmp_env2;
323         int ret = 0;
324
325         tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
326         tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
327         if (tmp_env1 == NULL || tmp_env2 == NULL) {
328                 puts("Can't allocate buffers for environment\n");
329                 env_set_default("malloc() failed", 0);
330                 ret = -EIO;
331                 goto done;
332         }
333
334         read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
335         read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
336
337         ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
338                                 read2_fail);
339
340 done:
341         free(tmp_env1);
342         free(tmp_env2);
343
344         return ret;
345 #endif /* ! ENV_IS_EMBEDDED */
346 }
347 #else /* ! CONFIG_ENV_OFFSET_REDUND */
348 /*
349  * The legacy NAND code saved the environment in the first NAND
350  * device i.e., nand_dev_desc + 0. This is also the behaviour using
351  * the new NAND code.
352  */
353 static int env_nand_load(void)
354 {
355 #if !defined(ENV_IS_EMBEDDED)
356         int ret;
357         ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
358
359 #if defined(CONFIG_ENV_OFFSET_OOB)
360         struct mtd_info *mtd  = get_nand_dev_by_index(0);
361         /*
362          * If unable to read environment offset from NAND OOB then fall through
363          * to the normal environment reading code below
364          */
365         if (mtd && !get_nand_env_oob(mtd, &nand_env_oob_offset)) {
366                 printf("Found Environment offset in OOB..\n");
367         } else {
368                 env_set_default("no env offset in OOB", 0);
369                 return;
370         }
371 #endif
372
373         ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
374         if (ret) {
375                 env_set_default("readenv() failed", 0);
376                 return -EIO;
377         }
378
379         return env_import(buf, 1);
380 #endif /* ! ENV_IS_EMBEDDED */
381
382         return 0;
383 }
384 #endif /* CONFIG_ENV_OFFSET_REDUND */
385
386 U_BOOT_ENV_LOCATION(nand) = {
387         .location       = ENVL_NAND,
388         ENV_NAME("NAND")
389         .load           = env_nand_load,
390 #if defined(CMD_SAVEENV)
391         .save           = env_save_ptr(env_nand_save),
392 #endif
393         .init           = env_nand_init,
394 };